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 };
91 void llapi_msg_set_level(int level)
93 /* ensure level is in the good range */
94 if (level < LLAPI_MSG_OFF)
95 llapi_msg_level = LLAPI_MSG_OFF;
96 else if (level > LLAPI_MSG_MAX)
97 llapi_msg_level = LLAPI_MSG_MAX;
99 llapi_msg_level = level;
102 int llapi_msg_get_level(void)
104 return llapi_msg_level;
107 void llapi_set_command_name(const char *cmd)
109 liblustreapi_cmd = cmd;
112 void llapi_clear_command_name(void)
114 liblustreapi_cmd = NULL;
117 static void error_callback_default(enum llapi_message_level level, int err,
118 const char *fmt, va_list ap)
120 if (liblustreapi_cmd != NULL)
121 fprintf(stderr, "%s %s: ", program_invocation_short_name,
124 fprintf(stderr, "%s: ", program_invocation_short_name);
125 vfprintf(stderr, fmt, ap);
126 if (level & LLAPI_MSG_NO_ERRNO)
127 fprintf(stderr, "\n");
129 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
132 static void info_callback_default(enum llapi_message_level level, int err,
133 const char *fmt, va_list ap)
136 if (liblustreapi_cmd != NULL) {
137 fprintf(stdout, "%s %s: ",
138 program_invocation_short_name,
141 fprintf(stdout, "%s: ", program_invocation_short_name);
144 vfprintf(stdout, fmt, ap);
147 static llapi_log_callback_t llapi_error_callback = error_callback_default;
148 static llapi_log_callback_t llapi_info_callback = info_callback_default;
151 /* llapi_error will preserve errno */
152 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
155 int tmp_errno = errno;
157 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
161 llapi_error_callback(level, abs(err), fmt, args);
166 /* llapi_printf will preserve errno */
167 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
170 int tmp_errno = errno;
172 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
176 llapi_info_callback(level, 0, fmt, args);
182 * Set a custom error logging function. Passing in NULL will reset the logging
183 * callback to its default value.
185 * This function returns the value of the old callback.
187 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
189 llapi_log_callback_t old = llapi_error_callback;
192 llapi_error_callback = cb;
194 llapi_error_callback = error_callback_default;
200 * Set a custom info logging function. Passing in NULL will reset the logging
201 * callback to its default value.
203 * This function returns the value of the old callback.
205 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
207 llapi_log_callback_t old = llapi_info_callback;
210 llapi_info_callback = cb;
212 llapi_info_callback = info_callback_default;
218 * size_units is to be initialized (or zeroed) by caller.
220 int llapi_parse_size(const char *optarg, unsigned long long *size,
221 unsigned long long *size_units, int bytes_spec)
224 char *argbuf = (char *)optarg;
225 unsigned long long frac = 0, frac_d = 1;
227 if (strncmp(optarg, "-", 1) == 0)
230 if (*size_units == 0)
233 *size = strtoull(argbuf, &end, 0);
234 if (end != NULL && *end == '.') {
238 frac = strtoull(argbuf, &end, 10);
239 /* count decimal places */
240 for (i = 0; i < (end - argbuf); i++)
245 if ((*end == 'b') && *(end + 1) == '\0' &&
246 (*size & (~0ULL << (64 - 9))) == 0 &&
248 *size_units = 1 << 9;
249 } else if ((*end == 'b') &&
250 *(end + 1) == '\0' &&
253 } else if ((*end == 'k' || *end == 'K') &&
254 *(end + 1) == '\0' &&
255 (*size & (~0ULL << (64 - 10))) == 0) {
256 *size_units = 1 << 10;
257 } else if ((*end == 'm' || *end == 'M') &&
258 *(end + 1) == '\0' &&
259 (*size & (~0ULL << (64 - 20))) == 0) {
260 *size_units = 1 << 20;
261 } else if ((*end == 'g' || *end == 'G') &&
262 *(end + 1) == '\0' &&
263 (*size & (~0ULL << (64 - 30))) == 0) {
264 *size_units = 1 << 30;
265 } else if ((*end == 't' || *end == 'T') &&
266 *(end + 1) == '\0' &&
267 (*size & (~0ULL << (64 - 40))) == 0) {
268 *size_units = 1ULL << 40;
269 } else if ((*end == 'p' || *end == 'P') &&
270 *(end + 1) == '\0' &&
271 (*size & (~0ULL << (64 - 50))) == 0) {
272 *size_units = 1ULL << 50;
273 } else if ((*end == 'e' || *end == 'E') &&
274 *(end + 1) == '\0' &&
275 (*size & (~0ULL << (64 - 60))) == 0) {
276 *size_units = 1ULL << 60;
281 *size = *size * *size_units + frac * *size_units / frac_d;
286 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
288 struct obd_ioctl_data *overlay;
291 data->ioc_len = obd_ioctl_packlen(data);
292 data->ioc_version = OBD_IOCTL_VERSION;
294 if (*pbuf != NULL && data->ioc_len > max_len) {
295 fprintf(stderr, "pbuf = %p, ioc_len = %u, max_len = %d\n",
296 *pbuf, data->ioc_len, max_len);
301 *pbuf = malloc(data->ioc_len);
306 overlay = (struct obd_ioctl_data *)*pbuf;
307 memcpy(*pbuf, data, sizeof(*data));
309 ptr = overlay->ioc_bulk;
310 if (data->ioc_inlbuf1) {
311 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
312 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
315 if (data->ioc_inlbuf2) {
316 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
317 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
320 if (data->ioc_inlbuf3) {
321 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
322 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
325 if (data->ioc_inlbuf4) {
326 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
327 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
333 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
335 struct obd_ioctl_data *overlay;
341 overlay = (struct obd_ioctl_data *)pbuf;
343 /* Preserve the caller's buffer pointers */
344 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
345 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
346 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
347 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
349 memcpy(data, pbuf, sizeof(*data));
351 ptr = overlay->ioc_bulk;
352 if (data->ioc_inlbuf1) {
353 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
354 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
357 if (data->ioc_inlbuf2) {
358 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
359 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
362 if (data->ioc_inlbuf3) {
363 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
364 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
367 if (data->ioc_inlbuf4) {
368 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
369 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
375 /* XXX: llapi_xxx() functions return negative values upon failure */
377 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
378 int stripe_count, int stripe_pattern)
382 /* 64 KB is the largest common page size I'm aware of (on ia64), but
383 * check the local page size just in case. */
384 page_size = LOV_MIN_STRIPE_SIZE;
385 if (getpagesize() > page_size) {
386 page_size = getpagesize();
387 llapi_err_noerrno(LLAPI_MSG_WARN,
388 "warning: your page size (%u) is "
389 "larger than expected (%u)", page_size,
390 LOV_MIN_STRIPE_SIZE);
392 if (!llapi_stripe_size_is_aligned(stripe_size)) {
394 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
395 "must be an even multiple of %d bytes",
396 (unsigned long long)stripe_size, page_size);
399 if (!llapi_stripe_index_is_valid(stripe_offset)) {
401 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
405 if (!llapi_stripe_count_is_valid(stripe_count)) {
407 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
411 if (llapi_stripe_size_is_too_big(stripe_size)) {
413 llapi_error(LLAPI_MSG_ERROR, rc,
414 "warning: stripe size 4G or larger "
415 "is not currently supported and would wrap");
421 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
426 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
428 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
432 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
434 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
439 if (!llapi_dir_hash_type_is_valid(hash_type)) {
441 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
449 * Trim a trailing newline from a string, if it exists.
451 int llapi_chomp_string(char *buf)
467 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
469 static int get_param_lov(const char *path, const char *param,
470 char *buf, size_t buf_size)
472 struct obd_uuid uuid;
475 rc = llapi_file_get_lov_uuid(path, &uuid);
479 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
484 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
486 static int get_param_lmv(const char *path, const char *param,
487 char *buf, size_t buf_size)
489 struct obd_uuid uuid;
492 rc = llapi_file_get_lmv_uuid(path, &uuid);
496 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
500 static int get_mds_md_size(const char *path)
502 char buf[PATH_MAX], inst[PATH_MAX];
503 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
506 rc = llapi_getname(path, inst, sizeof(inst));
510 /* Get the max ea size from llite parameters. */
511 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
512 "max_easize", buf, sizeof(buf));
518 return rc > 0 ? rc : md_size;
521 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
523 return get_param_lmv(path, "uuid", buf, bufsize);
527 * if pool is NULL, search tgtname in target_obd
528 * if pool is not NULL:
529 * if pool not found returns errno < 0
530 * if tgtname is NULL, returns 1 if pool is not empty and 0 if pool empty
531 * if tgtname is not NULL, returns 1 if OST is in pool and 0 if not
533 int llapi_search_tgt(char *fsname, char *poolname, char *tgtname, bool is_mdt)
535 char buffer[PATH_MAX];
541 /* You need one or the other */
542 if (poolname == NULL && fsname == NULL)
546 len = strlen(tgtname);
548 if (poolname == NULL && len == 0)
551 /* Search by poolname and fsname if is not NULL */
552 if (poolname != NULL) {
553 rc = poolpath(¶m, fsname, NULL);
555 snprintf(buffer, sizeof(buffer), "%s/%s",
556 param.gl_pathv[0], poolname);
558 } else if (fsname != NULL) {
559 rc = get_lustre_param_path(is_mdt ? "lmv" : "lov", fsname,
561 "target_obd", ¶m);
563 strncpy(buffer, param.gl_pathv[0],
569 cfs_free_param_data(¶m);
573 fd = fopen(buffer, "r");
577 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
578 if (poolname == NULL) {
580 /* Search for an tgtname in the list of targets
581 * Line format is IDX: fsname-OST/MDTxxxx_UUID STATUS */
582 ptr = strchr(buffer, ' ');
584 (strncmp(ptr + 1, tgtname, len) == 0)) {
589 /* Search for an tgtname in a pool,
590 * (or an existing non-empty pool if no tgtname) */
591 if ((tgtname == NULL) ||
592 (strncmp(buffer, tgtname, len) == 0)) {
602 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
604 return llapi_search_tgt(fsname, poolname, ostname, false);
608 * Open a Lustre file.
610 * \param name the name of the file to be opened
611 * \param flags access mode, see flags in open(2)
612 * \param mode permission of the file if it is created, see mode in open(2)
613 * \param param stripe pattern of the newly created file
615 * \retval file descriptor of opened file
616 * \retval negative errno on failure
618 int llapi_file_open_param(const char *name, int flags, mode_t mode,
619 const struct llapi_stripe_param *param)
621 char fsname[MAX_OBD_NAME + 1] = { 0 };
622 char *pool_name = param->lsp_pool;
623 struct lov_user_md *lum = NULL;
624 size_t lum_size = sizeof(*lum);
627 /* Make sure we are on a Lustre file system */
628 rc = llapi_search_fsname(name, fsname);
630 llapi_error(LLAPI_MSG_ERROR, rc,
631 "'%s' is not on a Lustre filesystem",
636 /* Check if the stripe pattern is sane. */
637 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
638 param->lsp_stripe_offset,
639 param->lsp_stripe_count,
640 param->lsp_stripe_pattern);
644 /* Make sure we have a good pool */
645 if (pool_name != NULL) {
646 /* in case user gives the full pool name <fsname>.<poolname>,
647 * strip the fsname */
648 char *ptr = strchr(pool_name, '.');
651 if (strcmp(pool_name, fsname) != 0) {
653 llapi_err_noerrno(LLAPI_MSG_ERROR,
654 "Pool '%s' is not on filesystem '%s'",
661 /* Make sure the pool exists and is non-empty */
662 rc = llapi_search_ost(fsname, pool_name, NULL);
664 char *err = rc == 0 ? "has no OSTs" : "does not exist";
666 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
667 fsname, pool_name, err);
671 lum_size = sizeof(struct lov_user_md_v3);
674 /* sanity check of target list */
675 if (param->lsp_is_specific) {
676 char ostname[MAX_OBD_NAME + 64];
680 for (i = 0; i < param->lsp_stripe_count; i++) {
681 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
682 fsname, param->lsp_osts[i]);
683 rc = llapi_search_ost(fsname, pool_name, ostname);
688 llapi_error(LLAPI_MSG_ERROR, rc,
689 "%s: cannot find OST %s in %s",
696 /* Make sure stripe offset is in OST list. */
697 if (param->lsp_osts[i] == param->lsp_stripe_offset)
701 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
702 "%s: stripe offset '%d' is not in the "
704 __func__, param->lsp_stripe_offset);
708 lum_size = lov_user_md_size(param->lsp_stripe_count,
709 LOV_USER_MAGIC_SPECIFIC);
712 lum = calloc(1, lum_size);
717 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
719 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
720 flags = O_DIRECTORY | O_RDONLY;
727 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
732 /* Initialize IOCTL striping pattern structure */
733 lum->lmm_magic = LOV_USER_MAGIC_V1;
734 lum->lmm_pattern = param->lsp_stripe_pattern;
735 lum->lmm_stripe_size = param->lsp_stripe_size;
736 lum->lmm_stripe_count = param->lsp_stripe_count;
737 lum->lmm_stripe_offset = param->lsp_stripe_offset;
738 if (pool_name != NULL) {
739 struct lov_user_md_v3 *lumv3 = (void *)lum;
741 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
742 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
744 if (param->lsp_is_specific) {
745 struct lov_user_md_v3 *lumv3 = (void *)lum;
748 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
749 if (pool_name == NULL) {
750 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
751 * OST list, therefore if pool is not specified we have
752 * to pack a null pool name for placeholder. */
753 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
756 for (i = 0; i < param->lsp_stripe_count; i++)
757 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
760 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
761 char *errmsg = "stripe already set";
764 if (errno != EEXIST && errno != EALREADY)
765 errmsg = strerror(errno);
767 llapi_err_noerrno(LLAPI_MSG_ERROR,
768 "setstripe error for '%s': %s", name, errmsg);
779 int llapi_file_open_pool(const char *name, int flags, int mode,
780 unsigned long long stripe_size, int stripe_offset,
781 int stripe_count, int stripe_pattern, char *pool_name)
783 const struct llapi_stripe_param param = {
784 .lsp_stripe_size = stripe_size,
785 .lsp_stripe_count = stripe_count,
786 .lsp_stripe_pattern = stripe_pattern,
787 .lsp_stripe_offset = stripe_offset,
788 .lsp_pool = pool_name
790 return llapi_file_open_param(name, flags, mode, ¶m);
793 int llapi_file_open(const char *name, int flags, int mode,
794 unsigned long long stripe_size, int stripe_offset,
795 int stripe_count, int stripe_pattern)
797 return llapi_file_open_pool(name, flags, mode, stripe_size,
798 stripe_offset, stripe_count,
799 stripe_pattern, NULL);
802 int llapi_file_create(const char *name, unsigned long long stripe_size,
803 int stripe_offset, int stripe_count, int stripe_pattern)
807 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
808 stripe_offset, stripe_count, stripe_pattern,
817 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
818 int stripe_offset, int stripe_count,
819 int stripe_pattern, char *pool_name)
823 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
824 stripe_offset, stripe_count, stripe_pattern,
833 static int verify_dir_param(const char *name,
834 const struct llapi_stripe_param *param)
836 char fsname[MAX_OBD_NAME + 1] = { 0 };
837 char *pool_name = param->lsp_pool;
840 /* Make sure we are on a Lustre file system */
841 rc = llapi_search_fsname(name, fsname);
843 llapi_error(LLAPI_MSG_ERROR, rc,
844 "'%s' is not on a Lustre filesystem",
849 /* Check if the stripe pattern is sane. */
850 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
851 param->lsp_stripe_count,
852 param->lsp_stripe_pattern);
856 /* Make sure we have a good pool */
857 if (pool_name != NULL) {
858 /* in case user gives the full pool name <fsname>.<poolname>,
859 * strip the fsname */
860 char *ptr = strchr(pool_name, '.');
864 if (strcmp(pool_name, fsname) != 0) {
866 llapi_err_noerrno(LLAPI_MSG_ERROR,
867 "Pool '%s' is not on filesystem '%s'",
874 /* Make sure the pool exists and is non-empty */
875 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
877 char *err = rc == 0 ? "has no OSTs" : "does not exist";
879 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
880 fsname, pool_name, err);
885 /* sanity check of target list */
886 if (param->lsp_is_specific) {
887 char mdtname[MAX_OBD_NAME + 64];
891 for (i = 0; i < param->lsp_stripe_count; i++) {
892 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
893 fsname, param->lsp_tgts[i]);
894 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
899 llapi_error(LLAPI_MSG_ERROR, rc,
900 "%s: cannot find MDT %s in %s",
907 /* Make sure stripe offset is in MDT list. */
908 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
912 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
913 "%s: stripe offset '%d' is not in the "
915 __func__, param->lsp_stripe_offset);
923 static inline void param2lmu(struct lmv_user_md *lmu,
924 const struct llapi_stripe_param *param)
926 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
928 lmu->lum_stripe_count = param->lsp_stripe_count;
929 lmu->lum_stripe_offset = param->lsp_stripe_offset;
930 lmu->lum_hash_type = param->lsp_stripe_pattern;
931 if (param->lsp_pool != NULL)
932 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
933 if (param->lsp_is_specific) {
936 for (i = 0; i < param->lsp_stripe_count; i++)
937 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
941 int llapi_dir_set_default_lmv(const char *name,
942 const struct llapi_stripe_param *param)
944 struct lmv_user_md lmu = { 0 };
948 rc = verify_dir_param(name, param);
952 /* TODO: default lmv doesn't support specific targets yet */
953 if (param->lsp_is_specific)
956 param2lmu(&lmu, param);
958 fd = open(name, O_DIRECTORY | O_RDONLY);
961 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
965 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
967 char *errmsg = "stripe already set";
969 if (errno != EEXIST && errno != EALREADY)
970 errmsg = strerror(errno);
972 llapi_err_noerrno(LLAPI_MSG_ERROR,
973 "default dirstripe error on '%s': %s",
980 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
981 int stripe_count, int stripe_pattern,
982 const char *pool_name)
984 const struct llapi_stripe_param param = {
985 .lsp_stripe_count = stripe_count,
986 .lsp_stripe_offset = stripe_offset,
987 .lsp_stripe_pattern = stripe_pattern,
988 .lsp_pool = (char *)pool_name
991 return llapi_dir_set_default_lmv(name, ¶m);
995 * Create a Lustre directory.
997 * \param name the name of the directory to be created
998 * \param mode permission of the file if it is created, see mode in open(2)
999 * \param param stripe pattern of the newly created directory
1001 * \retval 0 on success
1002 * \retval negative errno on failure
1004 int llapi_dir_create(const char *name, mode_t mode,
1005 const struct llapi_stripe_param *param)
1007 struct lmv_user_md *lmu = NULL;
1009 struct obd_ioctl_data data = { 0 };
1012 char *dirpath = NULL;
1013 char *namepath = NULL;
1018 rc = verify_dir_param(name, param);
1022 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1023 param->lsp_is_specific ?
1024 LMV_USER_MAGIC_SPECIFIC :
1027 lmu = calloc(1, lmu_size);
1031 dirpath = strdup(name);
1037 namepath = strdup(name);
1044 param2lmu(lmu, param);
1046 filename = basename(namepath);
1047 dir = dirname(dirpath);
1049 data.ioc_inlbuf1 = (char *)filename;
1050 data.ioc_inllen1 = strlen(filename) + 1;
1051 data.ioc_inlbuf2 = (char *)lmu;
1052 data.ioc_inllen2 = lmu_size;
1053 data.ioc_type = mode;
1054 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1056 llapi_error(LLAPI_MSG_ERROR, rc,
1057 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1062 fd = open(dir, O_DIRECTORY | O_RDONLY);
1065 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1069 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1070 char *errmsg = "stripe already set";
1073 if (errno != EEXIST && errno != EALREADY)
1074 errmsg = strerror(errno);
1076 llapi_err_noerrno(LLAPI_MSG_ERROR,
1077 "dirstripe error on '%s': %s", name, errmsg);
1087 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1088 int stripe_count, int stripe_pattern,
1089 const char *pool_name)
1091 const struct llapi_stripe_param param = {
1092 .lsp_stripe_count = stripe_count,
1093 .lsp_stripe_offset = stripe_offset,
1094 .lsp_stripe_pattern = stripe_pattern,
1095 .lsp_pool = (char *)pool_name
1098 return llapi_dir_create(name, mode, ¶m);
1101 int llapi_direntry_remove(char *dname)
1103 char *dirpath = NULL;
1104 char *namepath = NULL;
1110 dirpath = strdup(dname);
1111 namepath = strdup(dname);
1112 if (!dirpath || !namepath)
1115 filename = basename(namepath);
1117 dir = dirname(dirpath);
1119 fd = open(dir, O_DIRECTORY | O_RDONLY);
1122 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1127 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
1128 char *errmsg = strerror(errno);
1129 llapi_err_noerrno(LLAPI_MSG_ERROR,
1130 "error on ioctl %#jx for '%s' (%d): %s",
1131 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
1143 * Find the fsname, the full path, and/or an open fd.
1144 * Either the fsname or path must not be NULL
1146 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1149 char buf[PATH_MAX], mntdir[PATH_MAX];
1150 char *ptr, *ptr_end;
1152 int idx = 0, len = 0, mntlen, fd;
1154 int fsnamelen, mountlen;
1156 /* get the mount point */
1157 fp = setmntent(PROC_MOUNTS, "r");
1160 llapi_error(LLAPI_MSG_ERROR, rc,
1161 "cannot retrieve filesystem mount point");
1165 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1168 if (!llapi_is_lustre_mnt(&mnt))
1171 if ((want & WANT_INDEX) && (idx++ != index))
1174 mntlen = strlen(mnt.mnt_dir);
1175 ptr = strchr(mnt.mnt_fsname, '/');
1176 while (ptr && *ptr == '/')
1178 /* thanks to the call to llapi_is_lustre_mnt() above,
1179 * we are sure that mnt.mnt_fsname contains ":/",
1180 * so ptr should never be NULL */
1184 while (*ptr_end != '/' && *ptr_end != '\0')
1187 /* Check the fsname for a match, if given */
1188 mountlen = ptr_end - ptr;
1189 if (!(want & WANT_FSNAME) && fsname != NULL &&
1190 (fsnamelen = strlen(fsname)) > 0 &&
1191 (fsnamelen != mountlen ||
1192 (strncmp(ptr, fsname, mountlen) != 0)))
1195 /* If the path isn't set return the first one we find */
1196 if (path == NULL || strlen(path) == 0) {
1197 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1198 mntdir[sizeof(mntdir) - 1] = '\0';
1199 if ((want & WANT_FSNAME) && fsname != NULL) {
1200 strncpy(fsname, ptr, mountlen);
1201 fsname[mountlen] = '\0';
1205 /* Otherwise find the longest matching path */
1206 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1207 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1208 strncpy(mntdir, mnt.mnt_dir, sizeof(mntdir) - 1);
1209 mntdir[sizeof(mntdir) - 1] = '\0';
1211 if ((want & WANT_FSNAME) && fsname != NULL) {
1212 strncpy(fsname, ptr, mountlen);
1213 fsname[mountlen] = '\0';
1222 if ((want & WANT_PATH) && path != NULL) {
1223 strncpy(path, mntdir, PATH_MAX);
1224 path[strlen(mntdir)] = '\0';
1226 if (want & WANT_FD) {
1227 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1230 llapi_error(LLAPI_MSG_ERROR, rc,
1231 "cannot open '%s': %s", mntdir,
1238 } else if (want & WANT_ERROR)
1239 llapi_err_noerrno(LLAPI_MSG_ERROR,
1240 "'%s' not on a mounted Lustre filesystem",
1241 (want & WANT_PATH) ? fsname : path);
1246 * search lustre mounts
1248 * Calling this function will return to the user the mount point, mntdir, and
1249 * the file system name, fsname, if the user passed a buffer to this routine.
1251 * The user inputs are pathname and index. If the pathname is supplied then
1252 * the value of the index will be ignored. The pathname will return data if
1253 * the pathname is located on a lustre mount. Index is used to pick which
1254 * mount point you want in the case of multiple mounted lustre file systems.
1255 * See function lfs_osts in lfs.c for an example of the index use.
1257 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1260 int want = WANT_PATH, idx = -1;
1262 if (!pathname || pathname[0] == '\0') {
1266 strcpy(mntdir, pathname);
1269 want |= WANT_FSNAME;
1270 return get_root_path(want, fsname, NULL, mntdir, idx);
1273 /* Given a path, find the corresponding Lustre fsname */
1274 int llapi_search_fsname(const char *pathname, char *fsname)
1279 path = realpath(pathname, NULL);
1281 char tmp[PATH_MAX - 1];
1287 if (pathname[0] != '/') {
1288 /* Need an absolute path, but realpath() only works for
1289 * pathnames that actually exist. We go through the
1290 * extra hurdle of dirname(getcwd() + pathname) in
1291 * case the relative pathname contains ".." in it. */
1292 char realpath[PATH_MAX - 1];
1294 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1296 llapi_error(LLAPI_MSG_ERROR, rc,
1297 "cannot get current working directory");
1301 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1302 if (rc >= sizeof(tmp)) {
1304 llapi_error(LLAPI_MSG_ERROR, rc,
1305 "invalid parent path '%s'",
1311 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1312 if (rc >= sizeof(buf)) {
1314 llapi_error(LLAPI_MSG_ERROR, rc,
1315 "invalid path '%s'", pathname);
1318 path = realpath(buf, NULL);
1320 ptr = strrchr(buf, '/');
1322 llapi_error(LLAPI_MSG_ERROR |
1323 LLAPI_MSG_NO_ERRNO, 0,
1324 "cannot resolve path '%s'",
1329 path = realpath(buf, NULL);
1332 llapi_error(LLAPI_MSG_ERROR, rc,
1333 "cannot resolve path '%s'",
1339 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1344 int llapi_search_rootpath(char *pathname, const char *fsname)
1346 /* pathname can be used as an argument by get_root_path(),
1347 * clear it for safety */
1349 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1352 int llapi_getname(const char *path, char *buf, size_t size)
1354 struct obd_uuid uuid_buf;
1355 char *uuid = uuid_buf.uuid;
1357 int rc, len, fsname_len;
1359 memset(&uuid_buf, 0, sizeof(uuid_buf));
1360 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1365 * We want to turn testfs-clilov-ffff88002738bc00 into
1366 * testfs-ffff88002738bc00 in a portable way that doesn't depend
1367 * on what is after "-clilov-" as it may change in the future.
1368 * Unfortunately, the "fsname" part may contain a dash, so we
1369 * can't just skip to the first dash, and the "instance" may be a
1370 * UUID in the future, so we can't necessarily go to the last dash.
1372 cfg_instance = strstr(uuid, "-clilov-");
1376 fsname_len = cfg_instance - uuid;
1377 cfg_instance += strlen("-clilov-");
1378 len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
1387 * Get the list of pool members.
1388 * \param poolname string of format \<fsname\>.\<poolname\>
1389 * \param members caller-allocated array of char*
1390 * \param list_size size of the members array
1391 * \param buffer caller-allocated buffer for storing OST names
1392 * \param buffer_size size of the buffer
1394 * \return number of members retrieved for this pool
1395 * \retval -error failure
1397 int llapi_get_poolmembers(const char *poolname, char **members,
1398 int list_size, char *buffer, int buffer_size)
1400 char fsname[PATH_MAX];
1409 /* name is FSNAME.POOLNAME */
1410 if (strlen(poolname) >= sizeof(fsname))
1413 snprintf(fsname, sizeof(fsname), "%s", poolname);
1414 pool = strchr(fsname, '.');
1421 rc = poolpath(&pathname, fsname, NULL);
1423 llapi_error(LLAPI_MSG_ERROR, rc,
1424 "Lustre filesystem '%s' not found",
1429 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1430 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1431 cfs_free_param_data(&pathname);
1432 if (rc >= sizeof(buf))
1434 fd = fopen(buf, "r");
1437 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1442 while (fgets(buf, sizeof(buf), fd) != NULL) {
1443 if (nb_entries >= list_size) {
1447 buf[sizeof(buf) - 1] = '\0';
1449 tmp = strchr(buf, '\n');
1452 if (used + strlen(buf) + 1 > buffer_size) {
1457 strcpy(buffer + used, buf);
1458 members[nb_entries] = buffer + used;
1459 used += strlen(buf) + 1;
1469 * Get the list of pools in a filesystem.
1470 * \param name filesystem name or path
1471 * \param poollist caller-allocated array of char*
1472 * \param list_size size of the poollist array
1473 * \param buffer caller-allocated buffer for storing pool names
1474 * \param buffer_size size of the buffer
1476 * \return number of pools retrieved for this filesystem
1477 * \retval -error failure
1479 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1480 char *buffer, int buffer_size)
1482 char rname[PATH_MAX];
1487 struct dirent *pool;
1489 unsigned int nb_entries = 0;
1490 unsigned int used = 0;
1493 /* initialize output array */
1494 for (i = 0; i < list_size; i++)
1497 /* is name a pathname ? */
1498 ptr = strchr(name, '/');
1500 /* only absolute pathname is supported */
1504 if (!realpath(name, rname)) {
1506 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1511 fsname = strdup(rname);
1515 rc = poolpath(&pathname, NULL, rname);
1517 /* name is FSNAME */
1518 fsname = strdup(name);
1521 rc = poolpath(&pathname, fsname, NULL);
1524 llapi_error(LLAPI_MSG_ERROR, rc,
1525 "Lustre filesystem '%s' not found", name);
1529 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1530 dir = opendir(pathname.gl_pathv[0]);
1533 llapi_error(LLAPI_MSG_ERROR, rc,
1534 "Could not open pool list for '%s'",
1541 pool = readdir(dir);
1547 /* ignore . and .. */
1548 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1551 /* check output bounds */
1552 if (nb_entries >= list_size) {
1557 /* +2 for '.' and final '\0' */
1558 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1564 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1565 poollist[nb_entries] = buffer + used;
1566 used += strlen(pool->d_name) + strlen(fsname) + 2;
1572 llapi_error(LLAPI_MSG_ERROR, rc,
1573 "Error reading pool list for '%s'", name);
1576 cfs_free_param_data(&pathname);
1579 return rc != 0 ? rc : nb_entries;
1582 /* wrapper for lfs.c and obd.c */
1583 int llapi_poollist(const char *name)
1585 /* list of pool names (assume that pool count is smaller
1587 char **list, *buffer = NULL, *fsname = (char *)name;
1588 char *poolname = NULL, *tmp = NULL, data[16];
1589 enum param_filter type = FILTER_BY_PATH;
1590 int obdcount, bufsize, rc, nb, i;
1595 if (name[0] != '/') {
1596 fsname = strdup(name);
1600 poolname = strchr(fsname, '.');
1603 type = FILTER_BY_FS_NAME;
1606 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1607 data, sizeof(data));
1610 obdcount = atoi(data);
1612 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1613 * and also an array to store the pointers for all that
1614 * allocated space. */
1616 bufsize = sizeof(struct obd_uuid) * obdcount;
1617 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1618 if (buffer == NULL) {
1622 list = (char **) (buffer + bufsize);
1625 /* name is a path or fsname */
1626 nb = llapi_get_poollist(name, list, obdcount,
1629 /* name is a pool name (<fsname>.<poolname>) */
1630 nb = llapi_get_poolmembers(name, list, obdcount,
1634 if (nb == -EOVERFLOW) {
1637 goto retry_get_pools;
1640 for (i = 0; i < nb; i++)
1641 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1642 rc = (nb < 0 ? nb : 0);
1646 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1651 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1652 void *data, struct dirent64 *de);
1654 #define OBD_NOT_FOUND (-1)
1656 static void find_param_fini(struct find_param *param)
1658 if (param->fp_migrate)
1661 if (param->fp_obd_indexes) {
1662 free(param->fp_obd_indexes);
1663 param->fp_obd_indexes = NULL;
1666 if (param->fp_lmd) {
1667 free(param->fp_lmd);
1668 param->fp_lmd = NULL;
1671 if (param->fp_lmv_md) {
1672 free(param->fp_lmv_md);
1673 param->fp_lmv_md = NULL;
1677 static int common_param_init(struct find_param *param, char *path)
1679 int lum_size = get_mds_md_size(path);
1684 /* migrate has fp_lmv_md initialized outside */
1685 if (param->fp_migrate)
1688 if (lum_size < PATH_MAX + 1)
1689 lum_size = PATH_MAX + 1;
1691 param->fp_lum_size = lum_size;
1692 param->fp_lmd = calloc(1, offsetof(typeof(*param->fp_lmd), lmd_lmm) +
1694 if (param->fp_lmd == NULL) {
1695 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1696 "error: allocate %zu bytes for layout failed",
1697 sizeof(lstat_t) + param->fp_lum_size);
1701 param->fp_lmv_stripe_count = 256;
1702 param->fp_lmv_md = calloc(1,
1703 lmv_user_md_size(param->fp_lmv_stripe_count,
1704 LMV_USER_MAGIC_SPECIFIC));
1705 if (param->fp_lmv_md == NULL) {
1706 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1707 "error: allocation of %d bytes for ioctl",
1708 lmv_user_md_size(param->fp_lmv_stripe_count,
1709 LMV_USER_MAGIC_SPECIFIC));
1710 find_param_fini(param);
1714 param->fp_got_uuids = 0;
1715 param->fp_obd_indexes = NULL;
1716 param->fp_obd_index = OBD_NOT_FOUND;
1717 param->fp_mdt_index = OBD_NOT_FOUND;
1721 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1722 struct dirent64 *de)
1724 struct find_param *param = data;
1730 /* set errno upon failure */
1731 static DIR *opendir_parent(const char *path)
1737 path_copy = strdup(path);
1738 if (path_copy == NULL)
1741 parent_path = dirname(path_copy);
1742 parent = opendir(parent_path);
1748 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1753 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1754 if (param->fp_get_default_lmv)
1755 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1757 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1759 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1760 if (errno == E2BIG && ret != 0) {
1764 stripe_count = (__u32)param->fp_lmv_md->lum_stripe_count;
1765 if (stripe_count <= param->fp_lmv_stripe_count)
1768 free(param->fp_lmv_md);
1769 param->fp_lmv_stripe_count = stripe_count;
1770 lmv_size = lmv_user_md_size(stripe_count,
1771 LMV_USER_MAGIC_SPECIFIC);
1772 param->fp_lmv_md = malloc(lmv_size);
1773 if (param->fp_lmv_md == NULL) {
1774 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1775 "error: allocation of %d bytes for ioctl",
1776 lmv_user_md_size(param->fp_lmv_stripe_count,
1777 LMV_USER_MAGIC_SPECIFIC));
1785 static void convert_lmd_statx(struct lov_user_mds_data *lmd_v2, lstat_t *st,
1788 memset(&lmd_v2->lmd_stx, 0, sizeof(lmd_v2->lmd_stx));
1789 lmd_v2->lmd_stx.stx_blksize = st->st_blksize;
1790 lmd_v2->lmd_stx.stx_nlink = st->st_nlink;
1791 lmd_v2->lmd_stx.stx_uid = st->st_uid;
1792 lmd_v2->lmd_stx.stx_gid = st->st_gid;
1793 lmd_v2->lmd_stx.stx_mode = st->st_mode;
1794 lmd_v2->lmd_stx.stx_ino = st->st_ino;
1795 lmd_v2->lmd_stx.stx_size = st->st_size;
1796 lmd_v2->lmd_stx.stx_blocks = st->st_blocks;
1797 lmd_v2->lmd_stx.stx_atime.tv_sec = st->st_atime;
1798 lmd_v2->lmd_stx.stx_ctime.tv_sec = st->st_ctime;
1799 lmd_v2->lmd_stx.stx_mtime.tv_sec = st->st_mtime;
1800 lmd_v2->lmd_stx.stx_rdev_major = major(st->st_rdev);
1801 lmd_v2->lmd_stx.stx_rdev_minor = minor(st->st_rdev);
1802 lmd_v2->lmd_stx.stx_dev_major = major(st->st_dev);
1803 lmd_v2->lmd_stx.stx_dev_minor = minor(st->st_dev);
1804 lmd_v2->lmd_stx.stx_mask |= STATX_BASIC_STATS;
1806 lmd_v2->lmd_flags = 0;
1808 lmd_v2->lmd_flags |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1810 lmd_v2->lmd_stx.stx_mask &= ~(STATX_SIZE | STATX_BLOCKS);
1811 if (lmd_v2->lmd_stx.stx_size)
1812 lmd_v2->lmd_flags |= OBD_MD_FLLAZYSIZE;
1813 if (lmd_v2->lmd_stx.stx_blocks)
1814 lmd_v2->lmd_flags |= OBD_MD_FLLAZYBLOCKS;
1816 lmd_v2->lmd_flags |= OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1817 OBD_MD_FLBLKSZ | OBD_MD_FLMODE | OBD_MD_FLTYPE |
1818 OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLNLINK |
1823 static int convert_lmdbuf_v1v2(void *lmdbuf, int lmdlen)
1825 struct lov_user_mds_data_v1 *lmd_v1 = lmdbuf;
1826 struct lov_user_mds_data *lmd_v2 = lmdbuf;
1830 size = lov_comp_md_size((struct lov_comp_md_v1 *)&lmd_v1->lmd_lmm);
1834 if (lmdlen < sizeof(lmd_v1->lmd_st) + size)
1837 st = lmd_v1->lmd_st;
1838 memmove(&lmd_v2->lmd_lmm, &lmd_v1->lmd_lmm,
1839 lmdlen - (&lmd_v2->lmd_lmm - &lmd_v1->lmd_lmm));
1840 convert_lmd_statx(lmd_v2, &st, false);
1841 lmd_v2->lmd_lmmsize = 0;
1842 lmd_v2->lmd_padding = 0;
1847 int get_lmd_info_fd(const char *path, int parent_fd, int dir_fd,
1848 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1850 struct lov_user_mds_data *lmd = lmdbuf;
1851 static bool use_old_ioctl;
1855 if (parent_fd < 0 && dir_fd < 0)
1857 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1861 /* LL_IOC_MDC_GETINFO operates on the current directory inode
1862 * and returns struct lov_user_mds_data, while
1863 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1865 if (type == GET_LMD_INFO)
1866 cmd = use_old_ioctl ? LL_IOC_MDC_GETINFO_OLD :
1869 cmd = LL_IOC_LOV_GETSTRIPE;
1872 ret = ioctl(dir_fd, cmd, lmdbuf);
1873 if (ret < 0 && errno == ENOTTY && cmd == LL_IOC_MDC_GETINFO) {
1874 cmd = LL_IOC_MDC_GETINFO_OLD;
1875 use_old_ioctl = true;
1879 if (cmd == LL_IOC_MDC_GETINFO_OLD && !ret)
1880 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1881 } else if (parent_fd >= 0) {
1882 const char *fname = strrchr(path, '/');
1884 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
1885 * the parent directory) and returns struct lov_user_mds_data,
1886 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
1888 * This avoids opening, locking, and closing each file on the
1889 * client if that is not needed. Multiple of these ioctl() can
1890 * be done on the parent dir with a single open for all
1891 * files in that directory, and it also doesn't pollute the
1892 * client dcache with millions of dentries when traversing
1893 * a large filesystem.
1895 fname = (fname == NULL ? path : fname + 1);
1897 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
1900 else if (ret >= lmdlen || ret++ == 0)
1903 if (type == GET_LMD_INFO)
1904 cmd = use_old_ioctl ? IOC_MDC_GETFILEINFO_OLD :
1905 IOC_MDC_GETFILEINFO;
1907 cmd = IOC_MDC_GETFILESTRIPE;
1910 ret = ioctl(parent_fd, cmd, lmdbuf);
1911 if (ret < 0 && errno == ENOTTY &&
1912 cmd == IOC_MDC_GETFILEINFO) {
1913 cmd = IOC_MDC_GETFILEINFO_OLD;
1914 use_old_ioctl = true;
1915 goto retry_getfileinfo;
1918 if (cmd == IOC_MDC_GETFILEINFO_OLD && !ret)
1919 ret = convert_lmdbuf_v1v2(lmdbuf, lmdlen);
1923 if (ret && type == GET_LMD_INFO) {
1924 if (errno == ENOTTY) {
1927 /* ioctl is not supported, it is not a lustre fs.
1928 * Do the regular lstat(2) instead.
1930 ret = lstat_f(path, &st);
1933 llapi_error(LLAPI_MSG_ERROR, ret,
1934 "error: %s: lstat failed for %s",
1938 convert_lmd_statx(lmd, &st, true);
1939 /* It may be wrong to set use_old_ioctl with true as
1940 * the file is not a lustre fs. So reset it with false
1943 use_old_ioctl = false;
1944 } else if (errno == ENOENT) {
1946 llapi_error(LLAPI_MSG_WARN, ret,
1947 "warning: %s does not exist", path);
1948 } else if (errno != EISDIR && errno != ENODATA) {
1950 llapi_error(LLAPI_MSG_ERROR, ret,
1951 "%s ioctl failed for %s.",
1952 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
1953 "IOC_MDC_GETFILEINFO", path);
1960 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
1961 int lmdlen, enum get_lmd_info_type type)
1967 parent_fd = dirfd(parent);
1969 dir_fd = dirfd(dir);
1971 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
1974 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1975 semantic_func_t sem_init,
1976 semantic_func_t sem_fini, void *data,
1977 struct dirent64 *de)
1979 struct find_param *param = (struct find_param *)data;
1980 struct dirent64 *dent;
1988 if (!d && errno != ENOTDIR) {
1990 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1993 } else if (!d && !parent) {
1994 /* ENOTDIR. Open the parent dir. */
1995 p = opendir_parent(path);
2002 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2008 while ((dent = readdir64(d)) != NULL) {
2011 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2015 if ((len + dent->d_reclen + 2) > size) {
2016 llapi_err_noerrno(LLAPI_MSG_ERROR,
2017 "error: %s: string buffer too small",
2022 strcat(path, dent->d_name);
2024 if (dent->d_type == DT_UNKNOWN) {
2025 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
2027 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
2028 param->fp_lum_size, GET_LMD_INFO);
2030 dent->d_type = IFTODT(stx->stx_mode);
2037 switch (dent->d_type) {
2039 llapi_err_noerrno(LLAPI_MSG_ERROR,
2040 "error: %s: '%s' is UNKNOWN type %d",
2041 __func__, dent->d_name, dent->d_type);
2044 rc = llapi_semantic_traverse(path, size, d, sem_init,
2045 sem_fini, data, dent);
2046 if (rc != 0 && ret == 0)
2052 rc = sem_init(path, d, NULL, data, dent);
2053 if (rc < 0 && ret == 0) {
2058 if (sem_fini && rc == 0)
2059 sem_fini(path, d, NULL, data, dent);
2067 sem_fini(path, parent, &d, data, de);
2076 static int param_callback(char *path, semantic_func_t sem_init,
2077 semantic_func_t sem_fini, struct find_param *param)
2079 int ret, len = strlen(path);
2082 if (len > PATH_MAX) {
2084 llapi_error(LLAPI_MSG_ERROR, ret,
2085 "Path name '%s' is too long", path);
2089 buf = (char *)malloc(PATH_MAX + 1);
2093 snprintf(buf, PATH_MAX + 1, "%s", path);
2094 ret = common_param_init(param, buf);
2098 param->fp_depth = 0;
2100 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2101 sem_fini, param, NULL);
2103 find_param_fini(param);
2105 return ret < 0 ? ret : 0;
2108 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2110 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2114 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2119 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2121 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2124 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2129 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2133 fd = open(path, O_RDONLY | O_NONBLOCK);
2136 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2140 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2146 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2150 fd = open(path, O_RDONLY | O_NONBLOCK);
2153 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2157 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2169 * If uuidp is NULL, return the number of available obd uuids.
2170 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2171 * there are more OSTs than allocated to uuidp, then an error is returned with
2172 * the ost_count set to number of available obd uuids.
2174 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2175 int *ost_count, enum tgt_type type)
2177 char buf[PATH_MAX], format[32];
2178 int rc = 0, index = 0;
2179 struct obd_uuid name;
2183 /* Get the lov name */
2184 if (type == LOV_TYPE)
2185 rc = llapi_file_fget_lov_uuid(fd, &name);
2187 rc = llapi_file_fget_lmv_uuid(fd, &name);
2191 /* Now get the ost uuids */
2192 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2193 FILTER_BY_EXACT, "target_obd", ¶m);
2197 fp = fopen(param.gl_pathv[0], "r");
2200 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2205 snprintf(format, sizeof(format),
2206 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2207 while (fgets(buf, sizeof(buf), fp) != NULL) {
2208 if (uuidp && (index < *ost_count)) {
2209 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2217 if (uuidp && (index > *ost_count))
2222 cfs_free_param_data(¶m);
2226 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2228 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2231 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2236 root = open(mnt, O_RDONLY | O_DIRECTORY);
2239 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2244 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2252 /* Check if user specified value matches a real uuid. Ignore _UUID,
2253 * -osc-4ba41334, other trailing gunk in comparison.
2254 * @param real_uuid ends in "_UUID"
2255 * @param search_uuid may or may not end in "_UUID"
2257 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2259 int cmplen = strlen(real_uuid);
2260 int searchlen = strlen(search_uuid);
2262 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2264 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2267 /* The UUIDs may legitimately be different lengths, if
2268 * the system was upgraded from an older version. */
2269 if (cmplen != searchlen)
2272 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2275 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2276 * returned in param->fp_obd_index */
2277 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2279 struct obd_uuid obd_uuid;
2286 if (param->fp_got_uuids)
2289 /* Get the lov/lmv name */
2290 if (param->fp_get_lmv)
2291 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2293 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2295 if (rc != -ENOTTY) {
2296 llapi_error(LLAPI_MSG_ERROR, rc,
2297 "error: can't get %s name: %s",
2298 param->fp_get_lmv ? "lmv" : "lov",
2306 param->fp_got_uuids = 1;
2308 /* Now get the ost uuids */
2309 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2310 obd_uuid.uuid, FILTER_BY_EXACT,
2311 "target_obd", ¶m_data);
2315 fp = fopen(param_data.gl_pathv[0], "r");
2318 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2319 param_data.gl_pathv[0]);
2323 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2324 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2325 param->fp_get_lmv ? "MDTS" : "OBDS");
2327 snprintf(format, sizeof(format),
2328 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2329 while (fgets(buf, sizeof(buf), fp) != NULL) {
2332 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2335 if (param->fp_obd_uuid) {
2336 if (llapi_uuid_match(obd_uuid.uuid,
2337 param->fp_obd_uuid->uuid)) {
2338 param->fp_obd_index = index;
2341 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2342 /* Print everything */
2343 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2346 param->fp_obds_printed = 1;
2350 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2351 llapi_err_noerrno(LLAPI_MSG_ERROR,
2352 "error: %s: unknown obduuid: %s",
2353 __func__, param->fp_obd_uuid->uuid);
2357 cfs_free_param_data(¶m_data);
2361 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2362 * obd index for all these obduuids will be returned in
2363 * param->fp_obd_indexes */
2364 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2365 int num_obds, int **obdindexes, int *obdindex,
2368 int ret, obdcount, obd_valid = 0, obdnum;
2370 struct obd_uuid *uuids = NULL;
2374 if (type == LOV_TYPE)
2375 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2377 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2381 obdcount = atoi(buf);
2382 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2387 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2389 if (ret == -EOVERFLOW) {
2390 struct obd_uuid *uuids_temp;
2392 uuids_temp = realloc(uuids, obdcount *
2393 sizeof(struct obd_uuid));
2394 if (uuids_temp != NULL) {
2396 goto retry_get_uuids;
2401 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2405 indexes = malloc(num_obds * sizeof(*obdindex));
2406 if (indexes == NULL) {
2411 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2414 /* The user may have specified a simple index */
2415 i = strtol(obduuids[obdnum].uuid, &end, 0);
2416 if (end && *end == '\0' && i < obdcount) {
2417 indexes[obdnum] = i;
2420 for (i = 0; i < obdcount; i++) {
2421 if (llapi_uuid_match(uuids[i].uuid,
2422 obduuids[obdnum].uuid)) {
2423 indexes[obdnum] = i;
2429 if (i >= obdcount) {
2430 indexes[obdnum] = OBD_NOT_FOUND;
2431 llapi_err_noerrno(LLAPI_MSG_ERROR,
2432 "invalid obduuid '%s'",
2433 obduuids[obdnum].uuid);
2439 *obdindex = OBD_NOT_FOUND;
2441 *obdindex = obd_valid;
2443 *obdindexes = indexes;
2451 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2455 if (param->fp_mdt_uuid) {
2456 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2458 ¶m->fp_mdt_indexes,
2459 ¶m->fp_mdt_index, LMV_TYPE);
2464 if (param->fp_obd_uuid) {
2465 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2467 ¶m->fp_obd_indexes,
2468 ¶m->fp_obd_index, LOV_TYPE);
2473 param->fp_got_uuids = 1;
2478 int llapi_ostlist(char *path, struct find_param *param)
2483 fd = open(path, O_RDONLY | O_DIRECTORY);
2487 ret = setup_obd_uuid(fd, path, param);
2494 * Tries to determine the default stripe attributes for a given filesystem. The
2495 * filesystem to check should be specified by fsname, or will be determined
2498 static int sattr_get_defaults(const char *const fsname,
2499 unsigned int *scount,
2500 unsigned int *ssize,
2501 unsigned int *soffset)
2507 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2508 "stripecount", val, sizeof(val));
2511 *scount = atoi(val);
2515 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2516 "stripesize", val, sizeof(val));
2523 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2524 "stripeoffset", val, sizeof(val));
2527 *soffset = atoi(val);
2534 * Tries to gather the default stripe attributes for a given filesystem. If
2535 * the attributes can be determined, they are cached for easy retreival the
2536 * next time they are needed. Only a single filesystem's attributes are
2539 int sattr_cache_get_defaults(const char *const fsname,
2540 const char *const pathname, unsigned int *scount,
2541 unsigned int *ssize, unsigned int *soffset)
2544 char fsname[PATH_MAX + 1];
2545 unsigned int stripecount;
2546 unsigned int stripesize;
2547 unsigned int stripeoffset;
2553 char fsname_buf[PATH_MAX + 1];
2554 unsigned int tmp[3];
2556 if (fsname == NULL) {
2557 rc = llapi_search_fsname(pathname, fsname_buf);
2561 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2564 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2566 * Ensure all 3 sattrs (count, size, and offset) are
2567 * successfully retrieved and stored in tmp before writing to
2570 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2574 cache.stripecount = tmp[0];
2575 cache.stripesize = tmp[1];
2576 cache.stripeoffset = tmp[2];
2577 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2581 *scount = cache.stripecount;
2583 *ssize = cache.stripesize;
2585 *soffset = cache.stripeoffset;
2590 static char *layout2name(__u32 layout_pattern)
2592 if (layout_pattern == LOV_PATTERN_MDT)
2594 else if (layout_pattern == LOV_PATTERN_RAID0)
2596 else if (layout_pattern == (LOV_PATTERN_RAID0 | LOV_PATTERN_F_RELEASED))
2602 enum lov_dump_flags {
2603 LDF_IS_DIR = 0x0001,
2604 LDF_IS_RAW = 0x0002,
2605 LDF_INDENT = 0x0004,
2606 LDF_SKIP_OBJS = 0x0008,
2610 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2611 struct lov_user_ost_data_v1 *objects,
2612 enum llapi_layout_verbose verbose,
2613 int depth, char *pool_name,
2614 enum lov_dump_flags flags)
2616 bool is_dir = flags & LDF_IS_DIR;
2617 bool is_raw = flags & LDF_IS_RAW;
2618 bool indent = flags & LDF_INDENT;
2619 bool yaml = flags & LDF_YAML;
2620 bool skip_objs = flags & LDF_SKIP_OBJS;
2621 char *prefix = is_dir ? "" : "lmm_";
2622 char *separator = "";
2623 char *space = indent ? " " : "";
2626 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2627 lmm_oi_set_seq(&lum->lmm_oi, 0);
2628 if (!indent && (verbose & VERBOSE_DETAIL))
2629 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2632 if (!yaml && !indent && depth && path &&
2633 ((verbose != VERBOSE_OBJID) || !is_dir))
2634 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2636 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2637 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2638 space, prefix, lum->lmm_magic);
2639 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2641 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2642 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2644 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2646 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2651 if (verbose & ~VERBOSE_DFID)
2652 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2654 /* This needs a bit of hand-holding since old 1.x lmm_oi
2655 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2656 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2657 * a real FID. Ideally the 2.x code would have stored this
2658 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2659 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2660 * worked properly (especially since IGIF FIDs use mds_inum as
2661 * the FID SEQ), but unfortunately that didn't happen.
2663 * Print it to look like an IGIF FID, even though the fields
2664 * are reversed on disk, so that it makes sense to userspace.
2666 * Don't use ostid_id() and ostid_seq(), since they assume the
2667 * oi_fid fields are in the right order. This is why there are
2668 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2670 * For newer layout types hopefully this will be a real FID. */
2671 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2672 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2673 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2674 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2675 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2677 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2678 (unsigned long long)seq, oid, ver);
2680 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2681 (unsigned long long)seq, oid, ver);
2684 if (verbose & VERBOSE_STRIPE_COUNT) {
2685 if (verbose & ~VERBOSE_STRIPE_COUNT)
2686 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2689 if (!is_raw && lum->lmm_stripe_count == 0 &&
2690 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2691 unsigned int scount;
2692 rc = sattr_cache_get_defaults(NULL, path,
2696 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2699 llapi_error(LLAPI_MSG_ERROR, rc,
2700 "Cannot determine default"
2703 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2704 lum->lmm_stripe_count ==
2705 (typeof(lum->lmm_stripe_count))(-1)
2706 ? -1 : lum->lmm_stripe_count);
2709 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2710 (__s16)lum->lmm_stripe_count);
2712 if (!yaml && is_dir)
2718 if (verbose & VERBOSE_STRIPE_SIZE) {
2719 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2720 if (verbose & ~VERBOSE_STRIPE_SIZE)
2721 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2723 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2725 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2728 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2730 llapi_error(LLAPI_MSG_ERROR, rc,
2731 "Cannot determine default"
2734 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2735 (unsigned long long)lum->lmm_stripe_size);
2737 if (!yaml && is_dir)
2743 if ((verbose & VERBOSE_PATTERN)) {
2744 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2745 if (verbose & ~VERBOSE_PATTERN)
2746 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2748 if (lov_pattern_supported(lum->lmm_pattern))
2749 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2750 layout2name(lum->lmm_pattern));
2752 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2753 separator = (!yaml && is_dir) ? " " : "\n";
2756 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2757 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2758 if (verbose & ~VERBOSE_GENERATION)
2759 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2761 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2762 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2766 if (verbose & VERBOSE_STRIPE_OFFSET) {
2767 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2768 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2769 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2771 if (is_dir || skip_objs)
2772 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2773 lum->lmm_stripe_offset ==
2774 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2775 lum->lmm_stripe_offset);
2776 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2777 llapi_printf(LLAPI_MSG_NORMAL, "0");
2779 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2780 objects[0].l_ost_idx);
2781 if (!yaml && is_dir)
2787 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2788 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2789 if (verbose & ~VERBOSE_POOL)
2790 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2792 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2793 if (!yaml && is_dir)
2799 if (strlen(separator) != 0)
2800 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2803 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2804 struct lov_user_ost_data_v1 *objects,
2805 char *path, int obdindex, int depth,
2806 enum llapi_layout_verbose verbose,
2807 enum lov_dump_flags flags)
2809 bool is_dir = flags & LDF_IS_DIR;
2810 bool indent = flags & LDF_INDENT;
2811 bool skip_objs = flags & LDF_SKIP_OBJS;
2812 bool yaml = flags & LDF_YAML;
2813 bool obdstripe = obdindex == OBD_NOT_FOUND;
2816 if (!obdstripe && !skip_objs) {
2817 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2818 if (obdindex == objects[i].l_ost_idx) {
2828 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2831 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2832 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2833 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2834 char *space = " - ";
2837 llapi_printf(LLAPI_MSG_NORMAL,
2838 "%6slmm_objects:\n", " ");
2840 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2842 llapi_printf(LLAPI_MSG_NORMAL,
2843 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2845 for (i = 0; i < lum->lmm_stripe_count; i++) {
2846 int idx = objects[i].l_ost_idx;
2847 long long oid = ostid_id(&objects[i].l_ost_oi);
2848 long long gr = ostid_seq(&objects[i].l_ost_oi);
2850 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2854 struct lu_fid fid = { 0 };
2856 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2857 llapi_printf(LLAPI_MSG_NORMAL,
2858 "%sl_ost_idx: %d\n", space, idx);
2859 llapi_printf(LLAPI_MSG_NORMAL,
2860 "%8sl_fid: "DFID_NOBRACE"\n",
2862 } else if (indent) {
2863 struct lu_fid fid = { 0 };
2865 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2866 llapi_printf(LLAPI_MSG_NORMAL,
2867 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2868 space, i, idx, PFID(&fid));
2871 sprintf(fmt, "%s%s%s\n",
2872 "\t%6u\t%14llu\t%#13llx\t",
2873 (fid_seq_is_rsvd(gr) ||
2874 fid_seq_is_mdt0(gr)) ?
2875 "%14llu" : "%#14llx", "%s");
2876 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2878 obdindex == idx ? " *" : "");
2882 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2885 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2886 char *path, int obdindex, int depth,
2887 enum llapi_layout_verbose verbose,
2888 enum lov_dump_flags flags)
2890 struct lmv_user_mds_data *objects = lum->lum_objects;
2891 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2892 char *separator = "";
2893 bool yaml = flags & LDF_YAML;
2894 bool obdstripe = false;
2897 if (obdindex != OBD_NOT_FOUND) {
2898 if (lum->lum_stripe_count == 0) {
2899 if (obdindex == lum->lum_stripe_offset)
2902 for (i = 0; i < lum->lum_stripe_count; i++) {
2903 if (obdindex == objects[i].lum_mds) {
2904 llapi_printf(LLAPI_MSG_NORMAL,
2919 /* show all information default */
2921 if (lum->lum_magic == LMV_USER_MAGIC)
2922 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
2923 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
2925 verbose = VERBOSE_OBJID;
2928 if (depth && path && ((verbose != VERBOSE_OBJID)))
2929 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2931 if (verbose & VERBOSE_STRIPE_COUNT) {
2932 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2933 if (verbose & ~VERBOSE_STRIPE_COUNT)
2934 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2935 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2936 (int)lum->lum_stripe_count);
2937 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
2943 if (verbose & VERBOSE_STRIPE_OFFSET) {
2944 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2945 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2946 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2947 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2948 (int)lum->lum_stripe_offset);
2949 if (verbose & VERBOSE_HASH_TYPE && !yaml)
2955 if (verbose & VERBOSE_HASH_TYPE) {
2956 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
2957 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
2959 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2960 if (verbose & ~VERBOSE_HASH_TYPE)
2961 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
2962 if (type < LMV_HASH_TYPE_MAX)
2963 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2964 mdt_hash_name[type]);
2966 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
2968 if (flags & LMV_HASH_FLAG_MIGRATION)
2969 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
2970 if (flags & LMV_HASH_FLAG_DEAD)
2971 llapi_printf(LLAPI_MSG_NORMAL, ",dead");
2972 if (flags & LMV_HASH_FLAG_BAD_TYPE)
2973 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
2974 if (flags & LMV_HASH_FLAG_LOST_LMV)
2975 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
2980 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2981 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2982 if (lum->lum_stripe_count > 0)
2983 llapi_printf(LLAPI_MSG_NORMAL,
2984 "mdtidx\t\t FID[seq:oid:ver]\n");
2985 for (i = 0; i < lum->lum_stripe_count; i++) {
2986 int idx = objects[i].lum_mds;
2987 struct lu_fid *fid = &objects[i].lum_fid;
2988 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2989 llapi_printf(LLAPI_MSG_NORMAL,
2990 "%6u\t\t "DFID"\t\t%s\n",
2992 obdindex == idx ? " *" : "");
2997 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
2998 pool_name[0] != '\0') {
2999 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3000 if (verbose & ~VERBOSE_POOL)
3001 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3003 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3007 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3008 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3011 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3012 enum lov_dump_flags flags)
3014 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3015 int depth = param->fp_max_depth;
3016 enum llapi_layout_verbose verbose = param->fp_verbose;
3017 bool yaml = flags & LDF_YAML;
3019 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3020 !(flags & LDF_IS_DIR)) && !yaml)
3021 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3023 if (verbose & VERBOSE_DETAIL) {
3024 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3025 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3026 " ", comp_v1->lcm_magic);
3027 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3028 " ", comp_v1->lcm_size);
3029 if (flags & LDF_IS_DIR)
3030 llapi_printf(LLAPI_MSG_NORMAL,
3031 "%2slcm_flags: %s\n", " ",
3032 comp_v1->lcm_mirror_count > 0 ?
3035 llapi_printf(LLAPI_MSG_NORMAL,
3036 "%2slcm_flags: %s\n", " ",
3037 llapi_layout_flags_string(comp_v1->lcm_flags));
3040 if (verbose & VERBOSE_GENERATION) {
3041 if (verbose & ~VERBOSE_GENERATION)
3042 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3044 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3047 if (verbose & VERBOSE_MIRROR_COUNT) {
3048 if (verbose & ~VERBOSE_MIRROR_COUNT)
3049 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3051 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3052 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3053 comp_v1->lcm_mirror_count + 1 : 1);
3056 if (verbose & VERBOSE_COMP_COUNT) {
3057 if (verbose & ~VERBOSE_COMP_COUNT)
3058 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3060 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3061 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3062 comp_v1->lcm_entry_count : 0);
3065 if (verbose & VERBOSE_DETAIL && !yaml)
3066 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3069 static void lcme_flags2str(__u32 comp_flags)
3075 llapi_printf(LLAPI_MSG_NORMAL, "0");
3078 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3079 if (comp_flags & comp_flags_table[i].cfn_flag) {
3081 llapi_printf(LLAPI_MSG_NORMAL, ",");
3082 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3083 comp_flags_table[i].cfn_name);
3084 comp_flags &= ~comp_flags_table[i].cfn_flag;
3090 llapi_printf(LLAPI_MSG_NORMAL, ",");
3091 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3095 static void lov_dump_comp_v1_entry(struct find_param *param,
3096 enum lov_dump_flags flags, int index)
3098 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3099 struct lov_comp_md_entry_v1 *entry;
3100 char *separator = "";
3101 enum llapi_layout_verbose verbose = param->fp_verbose;
3102 bool yaml = flags & LDF_YAML;
3104 entry = &comp_v1->lcm_entries[index];
3107 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3109 if (verbose & VERBOSE_COMP_ID) {
3110 if (verbose & VERBOSE_DETAIL && !yaml)
3111 llapi_printf(LLAPI_MSG_NORMAL,
3112 "%slcme_id: ", " - ");
3113 else if (verbose & ~VERBOSE_COMP_ID)
3114 llapi_printf(LLAPI_MSG_NORMAL,
3115 "%4slcme_id: ", " ");
3116 if (entry->lcme_id != LCME_ID_INVAL)
3117 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3119 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3123 if (verbose & VERBOSE_MIRROR_ID) {
3124 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3125 if (verbose & ~VERBOSE_MIRROR_ID)
3126 llapi_printf(LLAPI_MSG_NORMAL,
3127 "%4slcme_mirror_id: ", " ");
3128 if (entry->lcme_id != LCME_ID_INVAL)
3129 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3130 mirror_id_of(entry->lcme_id));
3132 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3136 if (verbose & VERBOSE_COMP_FLAGS) {
3137 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3138 if (verbose & ~VERBOSE_COMP_FLAGS)
3139 llapi_printf(LLAPI_MSG_NORMAL,
3140 "%4slcme_flags: ", " ");
3141 lcme_flags2str(entry->lcme_flags);
3144 /* print snapshot timestamp if its a nosync comp */
3145 if ((verbose & VERBOSE_COMP_FLAGS) &&
3146 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3147 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3148 if (verbose & ~VERBOSE_COMP_FLAGS)
3149 llapi_printf(LLAPI_MSG_NORMAL,
3150 "%4slcme_timestamp: ", " ");
3152 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3153 (unsigned long long)entry->lcme_timestamp);
3155 time_t stamp = entry->lcme_timestamp;
3156 char *date_str = asctime(localtime(&stamp));
3158 date_str[strlen(date_str) - 1] = '\0';
3159 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3165 if (verbose & VERBOSE_COMP_START) {
3166 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3167 if (verbose & ~VERBOSE_COMP_START)
3168 llapi_printf(LLAPI_MSG_NORMAL,
3169 "%4slcme_extent.e_start: ", " ");
3170 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3171 (unsigned long long)entry->lcme_extent.e_start);
3175 if (verbose & VERBOSE_COMP_END) {
3176 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3177 if (verbose & ~VERBOSE_COMP_END)
3178 llapi_printf(LLAPI_MSG_NORMAL,
3179 "%4slcme_extent.e_end: ", " ");
3180 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3181 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3183 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3184 (unsigned long long)entry->lcme_extent.e_end);
3189 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3190 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3191 } else if (verbose & VERBOSE_DETAIL) {
3192 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3193 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3194 " ", entry->lcme_offset);
3195 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3196 " ", entry->lcme_size);
3197 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3199 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3203 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3204 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3206 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3207 * The table below gives the answers for the specified parameters (value and
3208 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3209 * --------------------------------------
3210 * 1 | file > limit; sign > 0 | -1 / -1 |
3211 * 2 | file = limit; sign > 0 | -1 / -1 |
3212 * 3 | file < limit; sign > 0 | ? / 1 |
3213 * 4 | file > limit; sign = 0 | -1 / -1 |
3214 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3215 * 6 | file < limit; sign = 0 | ? / -1 |
3216 * 7 | file > limit; sign < 0 | 1 / 1 |
3217 * 8 | file = limit; sign < 0 | ? / -1 |
3218 * 9 | file < limit; sign < 0 | ? / -1 |
3219 * --------------------------------------
3220 * Note: 5th actually means that the value is within the interval
3221 * (limit - margin, limit]. */
3222 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3223 int sign, int negopt, unsigned long long margin,
3229 /* Drop the fraction of margin (of days). */
3230 if (file + margin <= limit)
3232 } else if (sign == 0) {
3233 if (file <= limit && file + margin > limit)
3235 else if (file + margin <= limit)
3237 } else if (sign < 0) {
3244 return negopt ? ~ret + 1 : ret;
3247 static inline struct lov_user_md *
3248 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3250 return (struct lov_user_md *)((char *)comp_v1 +
3251 comp_v1->lcm_entries[ent_idx].lcme_offset);
3254 static inline struct lov_user_ost_data_v1 *
3255 lov_v1v3_objects(struct lov_user_md *v1)
3257 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3258 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3260 return v1->lmm_objects;
3264 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3266 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3267 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3268 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3270 pool_name[0] = '\0';
3274 print_last_init_comp(struct find_param *param)
3276 /* print all component info */
3277 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3280 /* print specific component info */
3281 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3282 param->fp_check_comp_start || param->fp_check_comp_end ||
3283 param->fp_check_mirror_id || param->fp_check_mirror_index)
3289 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3293 if (param->fp_comp_end == LUSTRE_EOF) {
3294 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3295 match = end == LUSTRE_EOF ? 1 : -1;
3296 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3297 match = end == LUSTRE_EOF ? -1 : 1;
3298 else /* at least EOF */
3300 if (param->fp_exclude_comp_end)
3303 unsigned long long margin;
3305 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3306 match = find_value_cmp(end, param->fp_comp_end,
3307 param->fp_comp_end_sign,
3308 param->fp_exclude_comp_end, margin, 0);
3315 * An example of "getstripe -v" for a two components PFL file:
3318 * lcm_magic: 0x0BD60BD0
3322 * lcm_entry_count: 2
3326 * lcme_extent.e_start: 0
3327 * lcme_extent.e_end: 1048576
3331 * lmm_magic: 0x0BD10BD0
3332 * lmm_seq: 0x200000401
3333 * lmm_object_id: 0x1
3334 * lmm_fid: [0x200000401:0x1:0x0]
3335 * lmm_stripe_count: 1
3336 * lmm_stripe_size: 1048576
3337 * lmm_pattern: raid0
3339 * lmm_stripe_offset: 0
3341 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3345 * lcme_extent.e_start: 1048576
3346 * lcme_extent.e_end: EOF
3350 * lmm_magic: 0x0BD10BD0
3351 * lmm_seq: 0x200000401
3352 * lmm_object_id: 0x1
3353 * lmm_fid: [0x200000401:0x1:0x0]
3354 * lmm_stripe_count: 2
3355 * lmm_stripe_size: 1048576
3356 * lmm_pattern: raid0
3358 * lmm_stripe_offset: 1
3360 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3361 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3363 static void lov_dump_comp_v1(struct find_param *param, char *path,
3364 enum lov_dump_flags flags)
3366 struct lov_comp_md_entry_v1 *entry;
3367 struct lov_user_ost_data_v1 *objects;
3368 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3369 struct lov_user_md_v1 *v1;
3370 char pool_name[LOV_MAXPOOLNAME + 1];
3371 int obdindex = param->fp_obd_index;
3373 bool obdstripe = false;
3374 __u16 mirror_index = 0;
3375 __u16 mirror_id = 0;
3377 if (obdindex != OBD_NOT_FOUND) {
3378 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3379 i < comp_v1->lcm_entry_count; i++) {
3380 if (!(comp_v1->lcm_entries[i].lcme_flags &
3384 v1 = lov_comp_entry(comp_v1, i);
3385 objects = lov_v1v3_objects(v1);
3387 for (j = 0; j < v1->lmm_stripe_count; j++) {
3388 if (obdindex == objects[j].l_ost_idx) {
3401 lov_dump_comp_v1_header(param, path, flags);
3403 flags |= LDF_INDENT;
3405 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3406 entry = &comp_v1->lcm_entries[i];
3408 if (param->fp_check_comp_flags) {
3409 if (((param->fp_comp_flags & entry->lcme_flags) !=
3410 param->fp_comp_flags) ||
3411 (param->fp_comp_neg_flags & entry->lcme_flags))
3415 if (param->fp_check_comp_id &&
3416 param->fp_comp_id != entry->lcme_id)
3419 if (param->fp_check_comp_start) {
3420 match = find_value_cmp(entry->lcme_extent.e_start,
3421 param->fp_comp_start,
3422 param->fp_comp_start_sign,
3424 param->fp_comp_start_units, 0);
3429 if (param->fp_check_comp_end) {
3430 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3436 if (param->fp_check_mirror_index) {
3437 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3439 mirror_id = mirror_id_of(entry->lcme_id);
3442 match = find_value_cmp(mirror_index,
3443 param->fp_mirror_index,
3444 param->fp_mirror_index_sign,
3445 param->fp_exclude_mirror_index,
3449 } else if (param->fp_check_mirror_id) {
3450 if (mirror_id != mirror_id_of(entry->lcme_id))
3451 mirror_id = mirror_id_of(entry->lcme_id);
3453 match = find_value_cmp(mirror_id,
3454 param->fp_mirror_id,
3455 param->fp_mirror_id_sign,
3456 param->fp_exclude_mirror_id,
3462 if (print_last_init_comp(param)) {
3464 * if part of stripe info is needed, we'd print only
3465 * the last instantiated component info.
3467 if (entry->lcme_flags & LCME_FL_INIT)
3473 if (entry->lcme_flags & LCME_FL_INIT) {
3474 if (obdindex != OBD_NOT_FOUND) {
3475 flags |= LDF_SKIP_OBJS;
3476 v1 = lov_comp_entry(comp_v1, i);
3477 objects = lov_v1v3_objects(v1);
3479 for (j = 0; j < v1->lmm_stripe_count; j++) {
3480 if (obdindex == objects[j].l_ost_idx) {
3481 flags &= ~LDF_SKIP_OBJS;
3486 flags &= ~LDF_SKIP_OBJS;
3489 flags |= LDF_SKIP_OBJS;
3492 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3494 lov_dump_comp_v1_entry(param, flags, i);
3496 v1 = lov_comp_entry(comp_v1, i);
3497 objects = lov_v1v3_objects(v1);
3498 lov_v1v3_pool_name(v1, pool_name);
3500 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3501 param->fp_max_depth, param->fp_verbose,
3504 if (print_last_init_comp(param)) {
3506 * directory layout contains only layout template, print the
3510 i = comp_v1->lcm_entry_count - 1;
3513 flags &= ~LDF_SKIP_OBJS;
3515 lov_dump_comp_v1_entry(param, flags, i);
3517 v1 = lov_comp_entry(comp_v1, i);
3518 objects = lov_v1v3_objects(v1);
3519 lov_v1v3_pool_name(v1, pool_name);
3521 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3522 param->fp_max_depth, param->fp_verbose,
3527 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3528 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3531 static inline bool has_any_comp_options(struct find_param *param)
3533 enum llapi_layout_verbose verbose = param->fp_verbose;
3535 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3536 param->fp_check_comp_start || param->fp_check_comp_end ||
3537 param->fp_check_comp_flags)
3540 /* show full layout information, not component specific */
3541 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3544 return verbose & VERBOSE_COMP_OPTS;
3547 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3550 struct lov_user_md *lum = &orig->lmd_lmm;
3551 struct lov_user_mds_data *new;
3552 struct lov_comp_md_v1 *comp_v1;
3553 struct lov_comp_md_entry_v1 *ent;
3554 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3555 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3558 new = malloc(offsetof(typeof(*new), lmd_lmm) + lum_off + lum_size);
3560 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3564 memcpy(new, orig, sizeof(new->lmd_stx) + sizeof(new->lmd_flags)
3565 + sizeof(new->lmd_lmmsize));
3567 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3568 comp_v1->lcm_magic = lum->lmm_magic;
3569 comp_v1->lcm_size = lum_off + lum_size;
3570 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3571 comp_v1->lcm_flags = 0;
3572 comp_v1->lcm_entry_count = 1;
3574 ent = &comp_v1->lcm_entries[0];
3576 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3577 ent->lcme_extent.e_start = 0;
3578 ent->lcme_extent.e_end = LUSTRE_EOF;
3579 ent->lcme_offset = lum_off;
3580 ent->lcme_size = lum_size;
3582 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3587 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3588 enum lov_dump_flags flags)
3590 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3592 if (has_any_comp_options(param)) {
3593 struct lov_user_mds_data *new_lmd, *orig_lmd;
3595 orig_lmd = param->fp_lmd;
3596 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3597 if (new_lmd != NULL) {
3598 param->fp_lmd = new_lmd;
3599 lov_dump_comp_v1(param, path, flags);
3600 param->fp_lmd = orig_lmd;
3606 if (magic == LOV_USER_MAGIC_V1) {
3607 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3608 param->fp_lmd->lmd_lmm.lmm_objects,
3609 path, param->fp_obd_index,
3610 param->fp_max_depth, param->fp_verbose,
3613 char pool_name[LOV_MAXPOOLNAME + 1];
3614 struct lov_user_ost_data_v1 *objects;
3615 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3617 snprintf(pool_name, sizeof(pool_name), "%s",
3618 lmmv3->lmm_pool_name);
3619 objects = lmmv3->lmm_objects;
3620 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3621 objects, path, param->fp_obd_index,
3622 param->fp_max_depth, param->fp_verbose,
3627 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3628 enum lov_dump_flags flags)
3632 if (param->fp_get_lmv || param->fp_get_default_lmv)
3633 magic = (__u32)param->fp_lmv_md->lum_magic;
3635 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3638 flags |= LDF_IS_RAW;
3643 case LOV_USER_MAGIC_V1:
3644 case LOV_USER_MAGIC_V3:
3645 case LOV_USER_MAGIC_SPECIFIC:
3646 lov_dump_plain_user_lmm(param, path, flags);
3649 case LMV_USER_MAGIC: {
3650 char pool_name[LOV_MAXPOOLNAME + 1];
3651 struct lmv_user_md *lum;
3653 lum = (struct lmv_user_md *)param->fp_lmv_md;
3654 snprintf(pool_name, sizeof(pool_name), "%s",
3655 lum->lum_pool_name);
3656 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3657 param->fp_max_depth, param->fp_verbose,
3661 case LOV_USER_MAGIC_COMP_V1:
3662 lov_dump_comp_v1(param, path, flags);
3665 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3666 "(expecting one of %#x %#x %#x %#x)\n",
3667 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3668 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3669 LMV_USER_MAGIC, LMV_MAGIC_V1);
3674 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3680 fname = strrchr(path, '/');
3682 /* It should be a file (or other non-directory) */
3683 if (fname == NULL) {
3684 dname = (char *)malloc(2);
3688 fname = (char *)path;
3690 dname = (char *)malloc(fname - path + 1);
3693 strncpy(dname, path, fname - path);
3694 dname[fname - path] = '\0';
3698 fd = open(dname, O_RDONLY | O_NONBLOCK);
3704 strcpy((char *)lum, fname);
3705 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3708 if (close(fd) == -1 && rc == 0)
3716 int llapi_file_lookup(int dirfd, const char *name)
3718 struct obd_ioctl_data data = { 0 };
3723 if (dirfd < 0 || name == NULL)
3726 data.ioc_version = OBD_IOCTL_VERSION;
3727 data.ioc_len = sizeof(data);
3728 data.ioc_inlbuf1 = (char *)name;
3729 data.ioc_inllen1 = strlen(name) + 1;
3731 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3733 llapi_error(LLAPI_MSG_ERROR, rc,
3734 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3739 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3745 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3746 * Return -1 or 1 if file timestamp does not or does match the given criteria
3747 * correspondingly. Return 0 if the MDS time is being checked and there are
3748 * attributes on OSTs and it is not yet clear if the timespamp matches.
3750 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3751 * updated timestamps. */
3752 static int find_time_check(lstatx_t *stx, struct find_param *param, int mds)
3757 /* Check if file is accepted. */
3758 if (param->fp_atime) {
3759 rc2 = find_value_cmp(stx->stx_atime.tv_sec, param->fp_atime,
3760 param->fp_asign, param->fp_exclude_atime,
3767 if (param->fp_mtime) {
3768 rc2 = find_value_cmp(stx->stx_mtime.tv_sec, param->fp_mtime,
3769 param->fp_msign, param->fp_exclude_mtime,
3774 /* If the previous check matches, but this one is not yet clear,
3775 * we should return 0 to do an RPC on OSTs. */
3780 if (param->fp_ctime) {
3781 rc2 = find_value_cmp(stx->stx_ctime.tv_sec, param->fp_ctime,
3782 param->fp_csign, param->fp_exclude_ctime,
3787 /* If the previous check matches, but this one is not yet clear,
3788 * we should return 0 to do an RPC on OSTs. */
3797 * Check whether the stripes matches the indexes user provided
3801 static int check_obd_match(struct find_param *param)
3803 struct lov_user_ost_data_v1 *objects;
3804 struct lov_comp_md_v1 *comp_v1 = NULL;
3805 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3806 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
3807 int i, j, k, count = 1;
3809 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
3812 if (!S_ISREG(stx->stx_mode))
3815 /* Only those files should be accepted, which have a
3816 * stripe on the specified OST. */
3817 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3818 comp_v1 = (struct lov_comp_md_v1 *)v1;
3819 count = comp_v1->lcm_entry_count;
3822 for (i = 0; i < count; i++) {
3824 v1 = lov_comp_entry(comp_v1, i);
3826 objects = lov_v1v3_objects(v1);
3828 for (j = 0; j < v1->lmm_stripe_count; j++) {
3829 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
3832 for (k = 0; k < param->fp_num_obds; k++) {
3833 if (param->fp_obd_indexes[k] ==
3834 objects[j].l_ost_idx)
3835 return !param->fp_exclude_obd;
3840 return param->fp_exclude_obd;
3843 static int check_mdt_match(struct find_param *param)
3847 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
3850 /* FIXME: For striped dir, we should get stripe information and check */
3851 for (i = 0; i < param->fp_num_mdts; i++) {
3852 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
3853 return !param->fp_exclude_mdt;
3856 if (param->fp_exclude_mdt)
3863 * Check whether the obd is active or not, if it is
3864 * not active, just print the object affected by this
3867 static int print_failed_tgt(struct find_param *param, char *path, int type)
3869 struct obd_statfs stat_buf;
3870 struct obd_uuid uuid_buf;
3873 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
3876 memset(&stat_buf, 0, sizeof(struct obd_statfs));
3877 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
3878 ret = llapi_obd_statfs(path, type,
3879 param->fp_obd_index, &stat_buf,
3882 llapi_printf(LLAPI_MSG_NORMAL,
3883 "obd_uuid: %s failed %s ",
3884 param->fp_obd_uuid->uuid,
3891 static int find_check_stripe_size(struct find_param *param)
3893 struct lov_comp_md_v1 *comp_v1 = NULL;
3894 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3895 __u32 stripe_size = 0;
3896 int ret, i, count = 1;
3898 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3899 comp_v1 = (struct lov_comp_md_v1 *)v1;
3900 count = comp_v1->lcm_entry_count;
3901 ret = param->fp_exclude_stripe_size ? 1 : -1;
3904 for (i = 0; i < count; i++) {
3906 v1 = lov_comp_entry(comp_v1, i);
3907 stripe_size = v1->lmm_stripe_size;
3910 ret = find_value_cmp(stripe_size, param->fp_stripe_size,
3911 param->fp_stripe_size_sign,
3912 param->fp_exclude_stripe_size,
3913 param->fp_stripe_size_units, 0);
3918 static __u32 find_get_stripe_count(struct find_param *param)
3920 struct lov_comp_md_v1 *comp_v1 = NULL;
3921 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3923 __u32 stripe_count = 0;
3925 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3926 comp_v1 = (struct lov_comp_md_v1 *)v1;
3927 count = comp_v1->lcm_entry_count;
3930 for (i = 0; i < count; i++) {
3932 v1 = lov_comp_entry(comp_v1, i);
3933 stripe_count = v1->lmm_stripe_count;
3936 return stripe_count;
3939 #define LOV_PATTERN_INVALID 0xFFFFFFFF
3941 static int find_check_layout(struct find_param *param)
3943 struct lov_comp_md_v1 *comp_v1 = NULL;
3944 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3946 bool found = false, valid = false;
3948 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3949 comp_v1 = (struct lov_comp_md_v1 *)v1;
3950 count = comp_v1->lcm_entry_count;
3953 for (i = 0; i < count; i++) {
3955 v1 = lov_comp_entry(comp_v1, i);
3957 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
3961 if (v1->lmm_pattern & param->fp_layout) {
3970 if ((found && !param->fp_exclude_layout) ||
3971 (!found && param->fp_exclude_layout))
3977 static int find_check_pool(struct find_param *param)
3979 struct lov_comp_md_v1 *comp_v1 = NULL;
3980 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3981 struct lov_user_md_v3 *v3 = (void *)v1;
3985 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3986 comp_v1 = (struct lov_comp_md_v1 *)v1;
3987 count = comp_v1->lcm_entry_count;
3988 /* empty requested pool is taken as no pool search */
3989 if (count == 0 && param->fp_poolname[0] == '\0')
3993 for (i = 0; i < count; i++) {
3994 if (comp_v1 != NULL)
3995 v1 = lov_comp_entry(comp_v1, i);
3997 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
3998 (param->fp_poolname[0] == '\0')) ||
3999 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4000 (strncmp(v3->lmm_pool_name,
4001 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4002 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4003 (strcmp(param->fp_poolname, "*") == 0))) {
4009 if ((found && !param->fp_exclude_pool) ||
4010 (!found && param->fp_exclude_pool))
4016 static int find_check_comp_options(struct find_param *param)
4018 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4019 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4020 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4021 struct lov_comp_md_entry_v1 *entry;
4024 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4025 comp_v1 = (struct lov_comp_md_v1 *)v1;
4027 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4028 if (forged_v1 == NULL)
4030 comp_v1 = forged_v1;
4031 comp_v1->lcm_entry_count = 1;
4032 entry = &comp_v1->lcm_entries[0];
4033 entry->lcme_flags = S_ISDIR(stx->stx_mode) ? 0 : LCME_FL_INIT;
4034 entry->lcme_extent.e_start = 0;
4035 entry->lcme_extent.e_end = LUSTRE_EOF;
4038 /* invalid case, don't match for any kind of search. */
4039 if (comp_v1->lcm_entry_count == 0) {
4044 if (param->fp_check_comp_count) {
4045 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4046 param->fp_comp_count,
4047 param->fp_comp_count_sign,
4048 param->fp_exclude_comp_count, 1, 0);
4054 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4055 entry = &comp_v1->lcm_entries[i];
4057 if (param->fp_check_comp_flags) {
4059 if (((param->fp_comp_flags & entry->lcme_flags) !=
4060 param->fp_comp_flags) ||
4061 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4067 if (param->fp_check_comp_start) {
4068 ret = find_value_cmp(entry->lcme_extent.e_start,
4069 param->fp_comp_start,
4070 param->fp_comp_start_sign,
4071 param->fp_exclude_comp_start,
4072 param->fp_comp_start_units, 0);
4077 if (param->fp_check_comp_end) {
4078 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4084 /* the component matches all criteria */
4093 static int find_check_mirror_options(struct find_param *param)
4095 struct lov_comp_md_v1 *comp_v1;
4096 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4099 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4102 comp_v1 = (struct lov_comp_md_v1 *)v1;
4104 if (param->fp_check_mirror_count) {
4105 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4106 param->fp_mirror_count,
4107 param->fp_mirror_count_sign,
4108 param->fp_exclude_mirror_count, 1, 0);
4113 if (param->fp_check_mirror_state) {
4115 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4117 if ((param->fp_mirror_state != 0 &&
4118 file_state != param->fp_mirror_state) ||
4119 file_state == param->fp_mirror_neg_state)
4126 static bool find_check_lmm_info(struct find_param *param)
4128 return param->fp_check_pool || param->fp_check_stripe_count ||
4129 param->fp_check_stripe_size || param->fp_check_layout ||
4130 param->fp_check_comp_count || param->fp_check_comp_end ||
4131 param->fp_check_comp_start || param->fp_check_comp_flags ||
4132 param->fp_check_mirror_count ||
4133 param->fp_check_mirror_state ||
4134 param->fp_check_projid;
4138 * Get file/directory project id.
4139 * by the open fd resides on.
4140 * Return 0 and project id on success, or -ve errno.
4142 static int fget_projid(int fd, int *projid)
4147 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4151 *projid = fsx.fsx_projid;
4155 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4156 void *data, struct dirent64 *de)
4158 struct find_param *param = (struct find_param *)data;
4159 DIR *dir = dirp == NULL ? NULL : *dirp;
4160 int decision = 1; /* 1 is accepted; -1 is rejected. */
4161 lstatx_t *stx = ¶m->fp_lmd->lmd_stx;
4163 int checked_type = 0;
4165 __u32 stripe_count = 0;
4169 if (parent == NULL && dir == NULL)
4172 /* If a regular expression is presented, make the initial decision */
4173 if (param->fp_pattern != NULL) {
4174 char *fname = strrchr(path, '/');
4175 fname = (fname == NULL ? path : fname + 1);
4176 ret = fnmatch(param->fp_pattern, fname, 0);
4177 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4178 (ret == 0 && param->fp_exclude_pattern))
4182 /* See if we can check the file type from the dirent. */
4183 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4186 if (DTTOIF(de->d_type) == param->fp_type) {
4187 if (param->fp_exclude_type)
4190 if (!param->fp_exclude_type)
4197 /* Request MDS for the stat info if some of these parameters need
4198 * to be compared. */
4199 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4200 param->fp_check_uid || param->fp_check_gid ||
4201 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4202 param->fp_check_size || param->fp_check_blocks ||
4203 find_check_lmm_info(param) ||
4204 param->fp_check_mdt_count || param->fp_check_hash_type)
4207 if (param->fp_type != 0 && checked_type == 0)
4210 if (decision == 0) {
4211 if (param->fp_check_mdt_count || param->fp_check_hash_type) {
4212 param->fp_get_lmv = 1;
4213 ret = cb_get_dirstripe(path, dir, param);
4218 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4219 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4220 param->fp_lum_size, GET_LMD_INFO);
4221 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4222 find_check_lmm_info(param)) {
4223 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4225 /* We need to "fake" the "use the default" values
4226 * since the lmm struct is zeroed out at this point. */
4227 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4228 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4230 ostid_set_seq(&lmm->lmm_oi,
4231 FID_SEQ_LOV_DEFAULT);
4232 lmm->lmm_stripe_size = 0;
4233 lmm->lmm_stripe_count = 0;
4234 lmm->lmm_stripe_offset = -1;
4236 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4238 ret = llapi_file_fget_mdtidx(dirfd(dir),
4239 ¶m->fp_file_mdt_index);
4240 } else if (S_ISREG(stx->stx_mode)) {
4241 /* FIXME: we could get the MDT index from the
4242 * file's FID in lmd->lmd_lmm.lmm_oi without
4243 * opening the file, once we are sure that
4244 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4245 * That would still be an ioctl() to map the
4246 * FID to the MDT, but not an open RPC. */
4247 fd = open(path, O_RDONLY);
4249 ret = llapi_file_fget_mdtidx(fd,
4250 ¶m->fp_file_mdt_index);
4255 /* For a special file, we assume it resides on
4256 * the same MDT as the parent directory. */
4257 ret = llapi_file_fget_mdtidx(dirfd(parent),
4258 ¶m->fp_file_mdt_index);
4269 stripe_count = find_get_stripe_count(param);
4273 if (param->fp_type && !checked_type) {
4274 if ((stx->stx_mode & S_IFMT) == param->fp_type) {
4275 if (param->fp_exclude_type)
4278 if (!param->fp_exclude_type)
4284 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4285 if (lustre_fs && param->fp_got_uuids &&
4286 param->fp_dev != makedev(stx->stx_dev_major,
4287 stx->stx_dev_minor)) {
4288 /* A lustre/lustre mount point is crossed. */
4289 param->fp_got_uuids = 0;
4290 param->fp_obds_printed = 0;
4291 param->fp_mdt_index = OBD_NOT_FOUND;
4292 param->fp_obd_index = OBD_NOT_FOUND;
4295 if (lustre_fs && !param->fp_got_uuids) {
4296 ret = setup_target_indexes(dir ? dir : parent, path,
4301 param->fp_dev = makedev(stx->stx_dev_major,
4302 stx->stx_dev_minor);
4303 } else if (!lustre_fs && param->fp_got_uuids) {
4304 /* A lustre/non-lustre mount point is crossed. */
4305 param->fp_got_uuids = 0;
4306 param->fp_mdt_index = OBD_NOT_FOUND;
4307 param->fp_obd_index = OBD_NOT_FOUND;
4311 if (param->fp_check_stripe_size) {
4312 decision = find_check_stripe_size(param);
4317 if (param->fp_check_stripe_count) {
4318 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4319 param->fp_stripe_count_sign,
4320 param->fp_exclude_stripe_count, 1, 0);
4325 if (param->fp_check_mdt_count) {
4326 decision = find_value_cmp(
4327 param->fp_lmv_md->lum_stripe_count,
4328 param->fp_mdt_count,
4329 param->fp_mdt_count_sign,
4330 param->fp_exclude_mdt_count, 1, 0);
4335 if (param->fp_check_layout) {
4336 decision = find_check_layout(param);
4341 if (param->fp_check_hash_type) {
4344 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4345 if ((found && param->fp_exclude_hash_type) ||
4346 (!found && !param->fp_exclude_hash_type)) {
4352 /* If an OBD UUID is specified but none matches, skip this file. */
4353 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4354 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4357 /* If an OST or MDT UUID is given, and some OST matches,
4359 if (param->fp_obd_index != OBD_NOT_FOUND ||
4360 param->fp_mdt_index != OBD_NOT_FOUND) {
4361 if (param->fp_obd_uuid) {
4362 if (check_obd_match(param)) {
4363 /* If no mdtuuid is given, we are done.
4364 * Otherwise, fall through to the mdtuuid
4366 if (!param->fp_mdt_uuid)
4373 if (param->fp_mdt_uuid) {
4374 if (check_mdt_match(param))
4381 if (param->fp_check_uid) {
4382 if (stx->stx_uid == param->fp_uid) {
4383 if (param->fp_exclude_uid)
4386 if (!param->fp_exclude_uid)
4391 if (param->fp_check_gid) {
4392 if (stx->stx_gid == param->fp_gid) {
4393 if (param->fp_exclude_gid)
4396 if (!param->fp_exclude_gid)
4401 if (param->fp_check_projid) {
4405 fd = open(path, O_RDONLY);
4408 ret = fget_projid(fd, &projid);
4413 if (projid == param->fp_projid) {
4414 if (param->fp_exclude_projid)
4417 if (!param->fp_exclude_projid)
4422 if (param->fp_check_pool) {
4423 decision = find_check_pool(param);
4428 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4429 param->fp_check_comp_start || param->fp_check_comp_end) {
4430 decision = find_check_comp_options(param);
4435 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4436 decision = find_check_mirror_options(param);
4441 /* Check the time on mds. */
4443 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4446 for_mds = lustre_fs ?
4447 (S_ISREG(stx->stx_mode) && stripe_count) : 0;
4448 decision = find_time_check(stx, param, for_mds);
4453 flags = param->fp_lmd->lmd_flags;
4454 if (param->fp_check_size &&
4455 ((S_ISREG(stx->stx_mode) && stripe_count) ||
4456 S_ISDIR(stx->stx_mode)) &&
4457 !(flags & OBD_MD_FLSIZE ||
4458 (param->fp_lazy && flags & OBD_MD_FLLAZYSIZE)))
4461 if (param->fp_check_blocks &&
4462 ((S_ISREG(stx->stx_mode) && stripe_count) ||
4463 S_ISDIR(stx->stx_mode)) &&
4464 !(flags & OBD_MD_FLBLOCKS ||
4465 (param->fp_lazy && flags & OBD_MD_FLLAZYBLOCKS)))
4468 /* If file still fits the request, ask ost for updated info.
4469 * The regular stat is almost of the same speed as some new
4470 * 'glimpse-size-ioctl'.
4475 /* For regular files with the stripe the decision may have not
4476 * been taken yet if *time or size is to be checked. */
4477 if (param->fp_obd_index != OBD_NOT_FOUND)
4478 print_failed_tgt(param, path, LL_STATFS_LOV);
4480 if (param->fp_mdt_index != OBD_NOT_FOUND)
4481 print_failed_tgt(param, path, LL_STATFS_LMV);
4484 ret = fstat_f(dirfd(dir), &st);
4485 else if (de != NULL)
4486 ret = fstatat_f(dirfd(parent), de->d_name, &st,
4487 AT_SYMLINK_NOFOLLOW);
4489 ret = lstat_f(path, &st);
4492 if (errno == ENOENT) {
4493 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4494 "warning: %s: %s does not exist",
4499 llapi_error(LLAPI_MSG_ERROR, ret,
4500 "%s: stat on %s failed",
4506 convert_lmd_statx(param->fp_lmd, &st, true);
4507 /* Check the time on osc. */
4508 decision = find_time_check(stx, param, 0);
4513 if (param->fp_check_size) {
4514 decision = find_value_cmp(stx->stx_size, param->fp_size,
4515 param->fp_size_sign,
4516 param->fp_exclude_size,
4517 param->fp_size_units, 0);
4522 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
4523 decision = find_value_cmp(stx->stx_blocks * 512,
4525 param->fp_blocks_sign,
4526 param->fp_exclude_blocks,
4527 param->fp_blocks_units, 0);
4532 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
4533 if (param->fp_zero_end)
4534 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
4536 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4540 /* Do not get down anymore? */
4541 if (param->fp_depth == param->fp_max_depth) {
4552 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
4553 void *param_data, struct dirent64 *de)
4555 struct find_param *param = (struct find_param *)param_data;
4556 struct lmv_user_md *lmu = param->fp_lmv_md;
4557 DIR *tmp_parent = parent;
4558 char raw[MAX_IOC_BUFLEN] = {'\0'};
4560 struct obd_ioctl_data data = { 0 };
4567 if (parent == NULL && dirp == NULL)
4576 if (parent == NULL) {
4577 tmp_parent = opendir_parent(path);
4578 if (tmp_parent == NULL) {
4581 llapi_error(LLAPI_MSG_ERROR, ret,
4582 "can not open %s", path);
4587 fd = dirfd(tmp_parent);
4589 path_copy = strdup(path);
4590 filename = basename(path_copy);
4592 data.ioc_inlbuf1 = (char *)filename;
4593 data.ioc_inllen1 = strlen(filename) + 1;
4594 data.ioc_inlbuf2 = (char *)lmu;
4595 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
4597 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
4599 llapi_error(LLAPI_MSG_ERROR, ret,
4600 "llapi_obd_statfs: error packing ioctl data");
4605 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
4607 if (errno == EBUSY && !retry) {
4608 /* because migrate may not be able to lock all involved
4609 * objects in order, for some of them it try lock, while
4610 * there may be conflicting COS locks and cause migrate
4611 * fail with EBUSY, hope a sync() could cause
4612 * transaction commit and release these COS locks. */
4616 } else if (errno == EALREADY) {
4617 if (param->fp_verbose & VERBOSE_DETAIL)
4619 "%s was migrated to MDT%d already\n",
4620 path, lmu->lum_stripe_offset);
4624 fprintf(stderr, "%s migrate failed: %s (%d)\n",
4625 path, strerror(-ret), ret);
4628 } else if (param->fp_verbose & VERBOSE_DETAIL) {
4629 fprintf(stdout, "migrate %s to MDT%d stripe count %d\n",
4630 path, lmu->lum_stripe_offset, lmu->lum_stripe_count);
4635 /* If the directory is being migration, we need
4636 * close the directory after migration,
4637 * so the old directory cache will be cleanup
4638 * on the client side, and re-open to get the
4639 * new directory handle */
4640 *dirp = opendir(path);
4641 if (*dirp == NULL) {
4643 llapi_error(LLAPI_MSG_ERROR, ret,
4644 "%s: Failed to open '%s'", __func__, path);
4649 closedir(tmp_parent);
4656 /* dir migration finished, shrink its stripes */
4657 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
4658 struct dirent64 *de)
4660 struct find_param *param = data;
4661 struct lmv_user_md *lmu = param->fp_lmv_md;
4662 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
4665 if (de && de->d_type != DT_DIR)
4670 * close it before setxattr because the latter may destroy the
4671 * original object, and cause close fail.
4673 ret = closedir(*dirp);
4679 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
4680 if (ret == -EALREADY)
4683 cb_common_fini(path, parent, dirp, data, de);
4687 int llapi_migrate_mdt(char *path, struct find_param *param)
4689 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
4693 int llapi_mv(char *path, struct find_param *param)
4695 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
4696 static bool printed;
4699 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
4700 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
4704 return llapi_migrate_mdt(path, param);
4707 int llapi_find(char *path, struct find_param *param)
4709 return param_callback(path, cb_find_init, cb_common_fini, param);
4713 * Get MDT number that the file/directory inode referenced
4714 * by the open fd resides on.
4715 * Return 0 and mdtidx on success, or -ve errno.
4717 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
4719 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
4724 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
4725 struct dirent64 *de)
4727 struct find_param *param = (struct find_param *)data;
4728 DIR *d = dirp == NULL ? NULL : *dirp;
4732 if (parent == NULL && d == NULL)
4736 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
4737 } else /* if (parent) */ {
4740 fd = open(path, O_RDONLY | O_NOCTTY);
4742 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
4750 if (ret == -ENODATA) {
4751 if (!param->fp_obd_uuid)
4752 llapi_printf(LLAPI_MSG_NORMAL,
4753 "'%s' has no stripe info\n", path);
4755 } else if (ret == -ENOENT) {
4756 llapi_error(LLAPI_MSG_WARN, ret,
4757 "warning: %s: '%s' does not exist",
4760 } else if (ret == -ENOTTY) {
4761 llapi_error(LLAPI_MSG_ERROR, ret,
4762 "%s: '%s' not on a Lustre fs",
4765 llapi_error(LLAPI_MSG_ERROR, ret,
4766 "error: %s: '%s' failed get_mdtidx",
4772 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
4773 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
4775 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
4779 /* Do not go down anymore? */
4780 if (param->fp_depth == param->fp_max_depth)
4788 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
4789 struct dirent64 *de)
4791 struct find_param *param = (struct find_param *)data;
4792 DIR *d = dirp == NULL ? NULL : *dirp;
4795 if (parent == NULL && d == NULL)
4798 if (param->fp_obd_uuid) {
4799 param->fp_quiet = 1;
4800 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
4805 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
4806 ret = cb_get_dirstripe(path, d, param);
4808 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
4809 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
4810 param->fp_lum_size, GET_LMD_STRIPE);
4815 if (errno == ENODATA && d != NULL) {
4816 /* We need to "fake" the "use the default" values
4817 * since the lmm struct is zeroed out at this point.
4818 * The magic needs to be set in order to satisfy
4819 * a check later on in the code path.
4820 * The object_seq needs to be set for the "(Default)"
4821 * prefix to be displayed. */
4822 if (param->fp_get_default_lmv) {
4823 struct lmv_user_md *lum = param->fp_lmv_md;
4825 lum->lum_magic = LMV_USER_MAGIC;
4826 lum->lum_stripe_count = 0;
4827 lum->lum_stripe_offset = -1;
4829 } else if (param->fp_get_lmv) {
4830 struct lmv_user_md *lum = param->fp_lmv_md;
4833 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
4836 lum->lum_magic = LMV_MAGIC_V1;
4837 lum->lum_stripe_count = 0;
4838 lum->lum_stripe_offset = mdtidx;
4841 struct lov_user_md *lmm =
4842 ¶m->fp_lmd->lmd_lmm;
4844 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4846 ostid_set_seq(&lmm->lmm_oi,
4847 FID_SEQ_LOV_DEFAULT);
4848 lmm->lmm_stripe_count = 0;
4849 lmm->lmm_stripe_size = 0;
4850 lmm->lmm_stripe_offset = -1;
4853 } else if (errno == ENODATA && parent != NULL) {
4854 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
4855 llapi_printf(LLAPI_MSG_NORMAL,
4856 "%s has no stripe info\n", path);
4858 } else if (errno == ENOENT) {
4859 llapi_error(LLAPI_MSG_WARN, -ENOENT,
4860 "warning: %s: %s does not exist",
4863 } else if (errno == ENOTTY) {
4865 llapi_error(LLAPI_MSG_ERROR, ret,
4866 "%s: '%s' not on a Lustre fs?",
4871 llapi_error(LLAPI_MSG_ERROR, ret,
4872 "error: %s: %s failed for %s",
4873 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
4874 "IOC_MDC_GETFILESTRIPE", path);
4881 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
4882 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
4885 /* Do not get down anymore? */
4886 if (param->fp_depth == param->fp_max_depth)
4894 int llapi_getstripe(char *path, struct find_param *param)
4896 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
4897 cb_get_mdt_index : cb_getstripe,
4898 cb_common_fini, param);
4901 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
4902 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
4904 char raw[MAX_IOC_BUFLEN] = {'\0'};
4906 struct obd_ioctl_data data = { 0 };
4909 data.ioc_inlbuf1 = (char *)&type;
4910 data.ioc_inllen1 = sizeof(__u32);
4911 data.ioc_inlbuf2 = (char *)&index;
4912 data.ioc_inllen2 = sizeof(__u32);
4913 data.ioc_pbuf1 = (char *)stat_buf;
4914 data.ioc_plen1 = sizeof(struct obd_statfs);
4915 data.ioc_pbuf2 = (char *)uuid_buf;
4916 data.ioc_plen2 = sizeof(struct obd_uuid);
4918 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
4920 llapi_error(LLAPI_MSG_ERROR, rc,
4921 "llapi_obd_statfs: error packing ioctl data");
4925 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
4927 return rc < 0 ? -errno : 0;
4930 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
4931 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
4936 fd = open(path, O_RDONLY);
4939 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
4941 /* If we can't even open a file on the filesystem (e.g. with
4942 * -ESHUTDOWN), force caller to exit or it will loop forever. */
4946 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
4953 #define MAX_STRING_SIZE 128
4955 int llapi_ping(char *obd_type, char *obd_name)
4957 int flags = O_RDONLY;
4958 char buf[1] = { 0 };
4962 rc = cfs_get_param_paths(&path, "%s/%s/ping",
4963 obd_type, obd_name);
4967 fd = open(path.gl_pathv[0], flags);
4969 if (errno == EACCES && flags == O_RDONLY) {
4974 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
4979 if (flags == O_RDONLY)
4980 rc = read(fd, buf, sizeof(buf));
4982 rc = write(fd, buf, sizeof(buf));
4990 cfs_free_param_data(&path);
4994 int llapi_target_iterate(int type_num, char **obd_type,
4995 void *args, llapi_cb_t cb)
5001 for (i = 0; i < type_num; i++) {
5004 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5008 for (j = 0; j < param.gl_pathc; j++) {
5009 char obd_uuid[UUID_MAX + 1];
5013 fp = fopen(param.gl_pathv[j], "r");
5016 llapi_error(LLAPI_MSG_ERROR, rc,
5017 "error: opening '%s'",
5022 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5024 llapi_error(LLAPI_MSG_ERROR, rc,
5025 "error: reading '%s'",
5030 /* Extract the obd_name from the sysfs path.
5031 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5033 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5039 /* skip /fs/lustre/'obd_type'/ */
5040 obd_name += strlen(obd_type[i]) + 12;
5041 /* chop off after obd_name */
5042 ptr = strrchr(obd_name, '/');
5046 cb(obd_type[i], obd_name, obd_uuid, args);
5055 cfs_free_param_data(¶m);
5059 static void do_target_check(char *obd_type_name, char *obd_name,
5060 char *obd_uuid, void *args)
5064 rc = llapi_ping(obd_type_name, obd_name);
5065 if (rc == ENOTCONN) {
5066 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5068 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5070 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5074 int llapi_target_check(int type_num, char **obd_type, char *dir)
5076 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5079 #undef MAX_STRING_SIZE
5081 /* Is this a lustre fs? */
5082 int llapi_is_lustre_mnttype(const char *type)
5084 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
5087 /* Is this a lustre client fs? */
5088 int llapi_is_lustre_mnt(struct mntent *mnt)
5090 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5091 strstr(mnt->mnt_fsname, ":/") != NULL);
5094 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5096 char fsname[PATH_MAX + 1];
5100 rc = llapi_search_fsname(mnt, fsname);
5104 root = open(mnt, O_RDONLY | O_DIRECTORY);
5107 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5111 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5119 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
5120 * format must have %s%s, buf must be > 16
5121 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
5122 * then buf = "lustre-MDT0000"
5124 static int get_mdtname(char *name, char *format, char *buf)
5126 char suffix[]="-MDT0000";
5127 int len = strlen(name);
5129 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
5130 name[len - 5] = '\0';
5135 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
5138 /* Not enough room to add suffix */
5139 llapi_err_noerrno(LLAPI_MSG_ERROR,
5140 "Invalid MDT name |%s|", name);
5145 return sprintf(buf, format, name, suffix);
5148 /** ioctl on filsystem root, with mdtindex sent as data
5149 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
5150 * \param mdtidxp pointer to integer within data to be filled in with the
5151 * mdt index (0 if no mdt is specified). NULL won't be filled.
5153 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
5161 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
5162 Open root and parse mdt index. */
5163 if (mdtname[0] == '/') {
5165 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
5166 (char *)mdtname, -1);
5168 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
5170 ptr = fsname + strlen(fsname) - 8;
5172 index = strtol(ptr + 4, NULL, 16);
5173 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
5177 llapi_err_noerrno(LLAPI_MSG_ERROR,
5178 "Can't open %s: %d\n", mdtname, rc);
5185 rc = ioctl(fd, opc, data);
5194 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
5195 int buflen, long long *recno, int *linkno)
5197 const char *fidstr_orig = fidstr;
5199 struct getinfo_fid2path *gf;
5204 while (*fidstr == '[')
5207 sscanf(fidstr, SFID, RFID(&fid));
5208 if (!fid_is_sane(&fid)) {
5209 llapi_err_noerrno(LLAPI_MSG_ERROR,
5210 "bad FID format '%s', should be [seq:oid:ver]"
5211 " (e.g. "DFID")\n", fidstr_orig,
5212 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
5216 gf = malloc(sizeof(*gf) + buflen);
5221 gf->gf_recno = *recno;
5222 gf->gf_linkno = *linkno;
5223 gf->gf_pathlen = buflen;
5225 /* Take path or fsname */
5226 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
5231 /* strip out instances of // */
5232 for (a = gf->gf_u.gf_path; *a != '\0'; a++) {
5233 if ((*a == '/') && (*(a + 1) == '/'))
5240 if (buf[0] == '\0') { /* ROOT path */
5245 *recno = gf->gf_recno;
5246 *linkno = gf->gf_linkno;
5253 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
5256 struct lustre_mdt_attrs *lma;
5260 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
5262 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
5265 lma = (struct lustre_mdt_attrs *)buf;
5266 fid_le_to_cpu(fid, &lma->lma_self_fid);
5270 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
5275 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
5284 int llapi_fd2fid(int fd, struct lu_fid *fid)
5288 memset(fid, 0, sizeof(*fid));
5290 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
5291 if (rc == -EINVAL || rc == -ENOTTY)
5292 rc = fid_from_lma(NULL, fd, fid);
5297 int llapi_path2fid(const char *path, struct lu_fid *fid)
5301 memset(fid, 0, sizeof(*fid));
5302 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5304 if (errno == ELOOP || errno == ENXIO)
5305 return fid_from_lma(path, -1, fid);
5309 rc = llapi_fd2fid(fd, fid);
5310 if (rc == -EINVAL || rc == -ENOTTY)
5311 rc = fid_from_lma(path, -1, fid);
5317 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
5318 char *name, size_t name_size)
5320 struct getparent *gp;
5323 gp = malloc(sizeof(*gp) + name_size);
5327 gp->gp_linkno = linkno;
5328 gp->gp_name_size = name_size;
5330 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
5336 *parent_fid = gp->gp_fid;
5338 strncpy(name, gp->gp_name, name_size);
5339 name[name_size - 1] = '\0';
5346 int llapi_path2parent(const char *path, unsigned int linkno,
5347 struct lu_fid *parent_fid, char *name, size_t name_size)
5352 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5356 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
5361 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5366 root = open(mnt, O_RDONLY | O_DIRECTORY);
5369 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5373 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5376 llapi_error(LLAPI_MSG_ERROR, rc,
5377 "ioctl on %s for getting connect flags failed", mnt);
5384 * Get a 64-bit value representing the version of file data pointed by fd.
5386 * Each write or truncate, flushed on OST, will change this value. You can use
5387 * this value to verify if file data was modified. This only checks the file
5388 * data, not metadata.
5390 * \param flags 0: no flush pages, usually used it the process has already
5392 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5394 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5395 * pages from clients.
5397 * \retval 0 on success.
5398 * \retval -errno on error.
5400 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5403 struct ioc_data_version idv;
5405 idv.idv_flags = (__u32)flags;
5407 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5411 *data_version = idv.idv_version;
5417 * Flush cached pages from all clients.
5419 * \param fd File descriptor
5423 int llapi_file_flush(int fd)
5427 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5431 * Fetch layout version from OST objects. Layout version on OST objects are
5432 * only set when the file is a mirrored file AND after the file has been
5433 * written at least once.
5435 * It actually fetches the least layout version from the objects.
5437 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5440 struct ioc_data_version idv = { 0 };
5442 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5446 *layout_version = idv.idv_layout_version;
5452 * Create a file without any name and open it for read/write
5454 * - file is created as if it were a standard file in the given \a directory
5455 * - file does not appear in \a directory and mtime does not change because
5456 * the filename is handled specially by the Lustre MDS.
5457 * - file is destroyed at final close
5459 * \param[in] directory directory from which to inherit layout/MDT idx
5460 * \param[in] mdt_idx MDT index on which the file is created,
5461 * \a idx == -1 means no specific MDT is requested
5462 * \param[in] mode standard open(2) mode
5463 * \param[in] stripe_param stripe parameters. May be NULL.
5465 * \retval a file descriptor on success.
5466 * \retval -errno on error.
5468 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5469 int open_flags, mode_t mode,
5470 const struct llapi_stripe_param *stripe_param)
5472 char file_path[PATH_MAX];
5473 int saved_errno = errno;
5475 unsigned int rnumber;
5481 rc = snprintf(file_path, sizeof(file_path),
5482 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5483 directory, rnumber);
5485 rc = snprintf(file_path, sizeof(file_path),
5486 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5487 directory, mdt_idx, rnumber);
5489 if (rc < 0 || rc >= sizeof(file_path))
5490 return -ENAMETOOLONG;
5493 * Either open O_WRONLY or O_RDWR, creating RDONLY
5494 * is non-sensical here
5496 if ((open_flags & O_ACCMODE) == O_RDONLY)
5497 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5499 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5501 if (stripe_param != NULL) {
5502 fd = llapi_file_open_param(file_path, open_flags,
5503 mode, stripe_param);
5507 fd = open(file_path, open_flags, mode);
5511 } while (fd < 0 && rc == -EEXIST);
5514 llapi_error(LLAPI_MSG_ERROR, rc,
5515 "Cannot create volatile file '%s' in '%s'",
5516 file_path + strlen(directory) + 1 +
5517 LUSTRE_VOLATILE_HDR_LEN,
5523 * Unlink file in case this wasn't a Lustre filesystem and the magic
5524 * volatile filename wasn't handled as intended. The effect is the
5525 * same. If volatile open was supported then we expect unlink() to
5528 (void)unlink(file_path);
5531 * Since we are returning successfully we restore errno (and
5532 * mask out possible EEXIST from open() and ENOENT from unlink().
5534 errno = saved_errno;
5540 * Create a file without any name open it for read/write
5542 * - file is created as if it were a standard file in the given \a directory
5543 * - file does not appear in \a directory and mtime does not change because
5544 * the filename is handled specially by the Lustre MDS.
5545 * - file is removed at final close
5546 * - file modes are rw------- since it doesn't make sense to have a read-only
5547 * or write-only file that cannot be opened again.
5548 * - if user wants another mode it must use fchmod() on the open file, no
5549 * security problems arise because it cannot be opened by another process.
5551 * \param[in] directory directory from which to inherit layout/MDT idx
5552 * \param[in] idx MDT index on which the file is created,
5553 * \a idx == -1 means no specific MDT is requested
5554 * \param[in] open_flags standard open(2) flags
5556 * \retval a file descriptor on success.
5557 * \retval -errno on error.
5559 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5562 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5563 S_IRUSR | S_IWUSR, NULL);
5567 * Swap the layouts between 2 file descriptors
5568 * the 2 files must be open for writing
5569 * first fd received the ioctl, second fd is passed as arg
5570 * this is assymetric but avoid use of root path for ioctl
5572 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5573 int gid, __u64 flags)
5575 struct lustre_swap_layouts lsl;
5580 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5581 rc = fstat(fd1, &st1);
5585 rc = fstat(fd2, &st2);
5590 lsl.sl_flags = flags;
5594 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5598 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5599 struct timeval tv1[2];
5600 struct timeval tv2[2];
5602 memset(tv1, 0, sizeof(tv1));
5603 memset(tv2, 0, sizeof(tv2));
5605 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5606 tv1[0].tv_sec = st1.st_atime;
5607 tv2[0].tv_sec = st2.st_atime;
5609 tv1[0].tv_sec = st2.st_atime;
5610 tv2[0].tv_sec = st1.st_atime;
5613 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5614 tv1[1].tv_sec = st1.st_mtime;
5615 tv2[1].tv_sec = st2.st_mtime;
5617 tv1[1].tv_sec = st2.st_mtime;
5618 tv2[1].tv_sec = st1.st_mtime;
5621 rc = futimes(fd1, tv1);
5625 rc = futimes(fd2, tv2);
5633 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5640 while (grp_id == 0);
5642 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5650 * Swap the layouts between 2 files
5651 * the 2 files are open in write
5653 int llapi_swap_layouts(const char *path1, const char *path2,
5654 __u64 dv1, __u64 dv2, __u64 flags)
5658 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5661 llapi_error(LLAPI_MSG_ERROR, rc,
5662 "error: cannot open '%s' for write", path1);
5666 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5669 llapi_error(LLAPI_MSG_ERROR, rc,
5670 "error: cannot open '%s' for write", path2);
5674 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5676 llapi_error(LLAPI_MSG_ERROR, rc,
5677 "error: cannot swap layout between '%s' and '%s'",
5688 * Attempt to open a file with Lustre file identifier \a fid
5689 * and return an open file descriptor.
5691 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
5692 * \param[in] fid Lustre file identifier of file to open
5693 * \param[in] flags open() flags
5695 * \retval non-negative file descriptor on successful open
5696 * \retval -1 if an error occurred
5698 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
5701 char mntdir[PATH_MAX];
5702 char path[PATH_MAX + 64];
5705 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
5709 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
5710 return open(path, flags);
5716 * \param fd File to lock.
5717 * \param gid Group Identifier.
5719 * \retval 0 on success.
5720 * \retval -errno on failure.
5722 int llapi_group_lock(int fd, int gid)
5726 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
5729 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
5737 * \param fd File to unlock.
5738 * \param gid Group Identifier.
5740 * \retval 0 on success.
5741 * \retval -errno on failure.
5743 int llapi_group_unlock(int fd, int gid)
5747 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
5750 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");