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>
64 #include <libgen.h> /* for dirname() */
65 #ifdef HAVE_LINUX_UNISTD_H
66 #include <linux/unistd.h>
73 #include <libcfs/util/ioctl.h>
74 #include <libcfs/util/param.h>
75 #include <libcfs/util/string.h>
76 #include <linux/lnet/lnetctl.h>
77 #include <lustre/lustreapi.h>
78 #include <linux/lustre/lustre_ostid.h>
79 #include <linux/lustre/lustre_ioctl.h>
80 #include "lustreapi_internal.h"
82 static int llapi_msg_level = LLAPI_MSG_MAX;
83 const char *liblustreapi_cmd;
85 char *mdt_hash_name[] = { "none",
86 LMV_HASH_NAME_ALL_CHARS,
87 LMV_HASH_NAME_FNV_1A_64 };
89 struct lustre_foreign_type lu_foreign_types[] = {
90 {.lft_type = LU_FOREIGN_TYPE_NONE, .lft_name = "none"},
91 {.lft_type = LU_FOREIGN_TYPE_DAOS, .lft_name = "daos"},
92 /* must be the last element */
93 {.lft_type = LU_FOREIGN_TYPE_UNKNOWN, .lft_name = NULL}
94 /* array max dimension must be <= UINT32_MAX */
97 void llapi_msg_set_level(int level)
99 /* ensure level is in the good range */
100 if (level < LLAPI_MSG_OFF)
101 llapi_msg_level = LLAPI_MSG_OFF;
102 else if (level > LLAPI_MSG_MAX)
103 llapi_msg_level = LLAPI_MSG_MAX;
105 llapi_msg_level = level;
108 int llapi_msg_get_level(void)
110 return llapi_msg_level;
113 void llapi_set_command_name(const char *cmd)
115 liblustreapi_cmd = cmd;
118 void llapi_clear_command_name(void)
120 liblustreapi_cmd = NULL;
123 static void error_callback_default(enum llapi_message_level level, int err,
124 const char *fmt, va_list ap)
126 if (liblustreapi_cmd != NULL)
127 fprintf(stderr, "%s %s: ", program_invocation_short_name,
130 fprintf(stderr, "%s: ", program_invocation_short_name);
131 vfprintf(stderr, fmt, ap);
132 if (level & LLAPI_MSG_NO_ERRNO)
133 fprintf(stderr, "\n");
135 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
138 static void info_callback_default(enum llapi_message_level level, int err,
139 const char *fmt, va_list ap)
142 if (liblustreapi_cmd != NULL) {
143 fprintf(stdout, "%s %s: ",
144 program_invocation_short_name,
147 fprintf(stdout, "%s: ", program_invocation_short_name);
150 vfprintf(stdout, fmt, ap);
153 static llapi_log_callback_t llapi_error_callback = error_callback_default;
154 static llapi_log_callback_t llapi_info_callback = info_callback_default;
157 /* llapi_error will preserve errno */
158 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
161 int tmp_errno = errno;
163 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
167 llapi_error_callback(level, abs(err), fmt, args);
172 /* llapi_printf will preserve errno */
173 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
176 int tmp_errno = errno;
178 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
182 llapi_info_callback(level, 0, fmt, args);
188 * Set a custom error logging function. Passing in NULL will reset the logging
189 * callback to its default value.
191 * This function returns the value of the old callback.
193 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
195 llapi_log_callback_t old = llapi_error_callback;
198 llapi_error_callback = cb;
200 llapi_error_callback = error_callback_default;
206 * Set a custom info logging function. Passing in NULL will reset the logging
207 * callback to its default value.
209 * This function returns the value of the old callback.
211 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
213 llapi_log_callback_t old = llapi_info_callback;
216 llapi_info_callback = cb;
218 llapi_info_callback = info_callback_default;
224 * size_units is to be initialized (or zeroed) by caller.
226 int llapi_parse_size(const char *optarg, unsigned long long *size,
227 unsigned long long *size_units, int bytes_spec)
230 char *argbuf = (char *)optarg;
231 unsigned long long frac = 0, frac_d = 1;
233 if (strncmp(optarg, "-", 1) == 0)
236 if (*size_units == 0)
239 *size = strtoull(argbuf, &end, 0);
240 if (end != NULL && *end == '.') {
244 frac = strtoull(argbuf, &end, 10);
245 /* count decimal places */
246 for (i = 0; i < (end - argbuf); i++)
251 if ((*end == 'b') && *(end + 1) == '\0' &&
252 (*size & (~0ULL << (64 - 9))) == 0 &&
254 *size_units = 1 << 9;
255 } else if ((*end == 'b') &&
256 *(end + 1) == '\0' &&
259 } else if ((*end == 'k' || *end == 'K') &&
260 *(end + 1) == '\0' &&
261 (*size & (~0ULL << (64 - 10))) == 0) {
262 *size_units = 1 << 10;
263 } else if ((*end == 'm' || *end == 'M') &&
264 *(end + 1) == '\0' &&
265 (*size & (~0ULL << (64 - 20))) == 0) {
266 *size_units = 1 << 20;
267 } else if ((*end == 'g' || *end == 'G') &&
268 *(end + 1) == '\0' &&
269 (*size & (~0ULL << (64 - 30))) == 0) {
270 *size_units = 1 << 30;
271 } else if ((*end == 't' || *end == 'T') &&
272 *(end + 1) == '\0' &&
273 (*size & (~0ULL << (64 - 40))) == 0) {
274 *size_units = 1ULL << 40;
275 } else if ((*end == 'p' || *end == 'P') &&
276 *(end + 1) == '\0' &&
277 (*size & (~0ULL << (64 - 50))) == 0) {
278 *size_units = 1ULL << 50;
279 } else if ((*end == 'e' || *end == 'E') &&
280 *(end + 1) == '\0' &&
281 (*size & (~0ULL << (64 - 60))) == 0) {
282 *size_units = 1ULL << 60;
287 *size = *size * *size_units + frac * *size_units / frac_d;
292 int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
294 struct obd_ioctl_data *overlay;
297 data->ioc_len = obd_ioctl_packlen(data);
298 data->ioc_version = OBD_IOCTL_VERSION;
300 if (*pbuf != NULL && data->ioc_len > max_len) {
301 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
302 "pbuf = %p, ioc_len = %u, max_len = %d\n",
303 *pbuf, data->ioc_len, max_len);
308 *pbuf = malloc(data->ioc_len);
313 overlay = (struct obd_ioctl_data *)*pbuf;
314 memcpy(*pbuf, data, sizeof(*data));
316 ptr = overlay->ioc_bulk;
317 if (data->ioc_inlbuf1) {
318 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
319 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
322 if (data->ioc_inlbuf2) {
323 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
324 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
327 if (data->ioc_inlbuf3) {
328 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
329 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
332 if (data->ioc_inlbuf4) {
333 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
334 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
340 int llapi_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
342 struct obd_ioctl_data *overlay;
348 overlay = (struct obd_ioctl_data *)pbuf;
350 /* Preserve the caller's buffer pointers */
351 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
352 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
353 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
354 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
356 memcpy(data, pbuf, sizeof(*data));
358 ptr = overlay->ioc_bulk;
359 if (data->ioc_inlbuf1) {
360 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
361 ptr += __ALIGN_KERNEL(data->ioc_inllen1, 8);
364 if (data->ioc_inlbuf2) {
365 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
366 ptr += __ALIGN_KERNEL(data->ioc_inllen2, 8);
369 if (data->ioc_inlbuf3) {
370 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
371 ptr += __ALIGN_KERNEL(data->ioc_inllen3, 8);
374 if (data->ioc_inlbuf4) {
375 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
376 ptr += __ALIGN_KERNEL(data->ioc_inllen4, 8);
382 /* XXX: llapi_xxx() functions return negative values upon failure */
384 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
385 int stripe_count, int stripe_pattern)
389 /* 64 KB is the largest common page size I'm aware of (on ia64), but
390 * check the local page size just in case. */
391 page_size = LOV_MIN_STRIPE_SIZE;
392 if (getpagesize() > page_size) {
393 page_size = getpagesize();
394 llapi_err_noerrno(LLAPI_MSG_WARN,
395 "warning: your page size (%u) is "
396 "larger than expected (%u)", page_size,
397 LOV_MIN_STRIPE_SIZE);
399 if (!llapi_stripe_size_is_aligned(stripe_size)) {
401 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
402 "must be an even multiple of %d bytes",
403 stripe_size, page_size);
406 if (!llapi_stripe_index_is_valid(stripe_offset)) {
408 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
412 if (!llapi_stripe_count_is_valid(stripe_count)) {
414 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
418 if (llapi_stripe_size_is_too_big(stripe_size)) {
420 llapi_error(LLAPI_MSG_ERROR, rc,
421 "warning: stripe size 4G or larger "
422 "is not currently supported and would wrap");
428 int llapi_dir_stripe_limit_check(int stripe_offset, int stripe_count,
433 if (!llapi_dir_stripe_index_is_valid(stripe_offset)) {
435 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
439 if (!llapi_dir_stripe_count_is_valid(stripe_count)) {
441 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
446 if (!llapi_dir_hash_type_is_valid(hash_type)) {
448 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad hash type %d",
456 * Trim a trailing newline from a string, if it exists.
458 int llapi_chomp_string(char *buf)
474 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
476 static int get_param_lov(const char *path, const char *param,
477 char *buf, size_t buf_size)
479 struct obd_uuid uuid;
482 rc = llapi_file_get_lov_uuid(path, &uuid);
486 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
491 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
493 static int get_param_lmv(const char *path, const char *param,
494 char *buf, size_t buf_size)
496 struct obd_uuid uuid;
499 rc = llapi_file_get_lmv_uuid(path, &uuid);
503 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
507 static int get_mds_md_size(const char *path)
509 char buf[PATH_MAX], inst[PATH_MAX];
510 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
513 rc = llapi_getname(path, inst, sizeof(inst));
517 /* Get the max ea size from llite parameters. */
518 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
519 "max_easize", buf, sizeof(buf));
525 return rc > 0 ? rc : md_size;
528 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
530 return get_param_lmv(path, "uuid", buf, bufsize);
534 * if pool is NULL, search tgtname in target_obd
535 * if pool is not NULL:
536 * if pool not found returns errno < 0
537 * if tgtname is NULL, returns 1 if pool is not empty and 0 if pool empty
538 * if tgtname is not NULL, returns 1 if OST is in pool and 0 if not
540 int llapi_search_tgt(char *fsname, char *poolname, char *tgtname, bool is_mdt)
542 char buffer[PATH_MAX];
548 /* You need one or the other */
549 if (poolname == NULL && fsname == NULL)
553 len = strlen(tgtname);
555 if (poolname == NULL && len == 0)
558 /* Search by poolname and fsname if is not NULL */
559 if (poolname != NULL) {
560 rc = poolpath(¶m, fsname, NULL);
562 snprintf(buffer, sizeof(buffer), "%s/%s",
563 param.gl_pathv[0], poolname);
565 } else if (fsname != NULL) {
566 rc = get_lustre_param_path(is_mdt ? "lmv" : "lov", fsname,
568 "target_obd", ¶m);
570 strncpy(buffer, param.gl_pathv[0],
576 cfs_free_param_data(¶m);
580 fd = fopen(buffer, "r");
584 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
585 if (poolname == NULL) {
587 /* Search for an tgtname in the list of targets
588 * Line format is IDX: fsname-OST/MDTxxxx_UUID STATUS */
589 ptr = strchr(buffer, ' ');
591 (strncmp(ptr + 1, tgtname, len) == 0)) {
596 /* Search for an tgtname in a pool,
597 * (or an existing non-empty pool if no tgtname) */
598 if ((tgtname == NULL) ||
599 (strncmp(buffer, tgtname, len) == 0)) {
609 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
611 return llapi_search_tgt(fsname, poolname, ostname, false);
615 * Open a Lustre file.
617 * \param name the name of the file to be opened
618 * \param flags access mode, see flags in open(2)
619 * \param mode permission of the file if it is created, see mode in open(2)
620 * \param param stripe pattern of the newly created file
622 * \retval file descriptor of opened file
623 * \retval negative errno on failure
625 int llapi_file_open_param(const char *name, int flags, mode_t mode,
626 const struct llapi_stripe_param *param)
628 char fsname[MAX_OBD_NAME + 1] = { 0 };
629 char *pool_name = param->lsp_pool;
630 struct lov_user_md *lum = NULL;
631 size_t lum_size = sizeof(*lum);
634 /* Make sure we are on a Lustre file system */
635 rc = llapi_search_fsname(name, fsname);
637 llapi_error(LLAPI_MSG_ERROR, rc,
638 "'%s' is not on a Lustre filesystem",
643 /* Check if the stripe pattern is sane. */
644 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
645 param->lsp_stripe_offset,
646 param->lsp_stripe_count,
647 param->lsp_stripe_pattern);
651 /* Make sure we have a good pool */
652 if (pool_name != NULL) {
653 /* in case user gives the full pool name <fsname>.<poolname>,
654 * strip the fsname */
655 char *ptr = strchr(pool_name, '.');
658 if (strcmp(pool_name, fsname) != 0) {
660 llapi_err_noerrno(LLAPI_MSG_ERROR,
661 "Pool '%s' is not on filesystem '%s'",
668 /* Make sure the pool exists and is non-empty */
669 rc = llapi_search_ost(fsname, pool_name, NULL);
671 char *err = rc == 0 ? "has no OSTs" : "does not exist";
673 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
674 fsname, pool_name, err);
678 lum_size = sizeof(struct lov_user_md_v3);
681 /* sanity check of target list */
682 if (param->lsp_is_specific) {
683 char ostname[MAX_OBD_NAME + 1];
687 for (i = 0; i < param->lsp_stripe_count; i++) {
688 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
689 fsname, param->lsp_osts[i]);
690 rc = llapi_search_ost(fsname, pool_name, ostname);
695 llapi_error(LLAPI_MSG_ERROR, rc,
696 "%s: cannot find OST %s in %s",
703 /* Make sure stripe offset is in OST list. */
704 if (param->lsp_osts[i] == param->lsp_stripe_offset)
708 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
709 "%s: stripe offset '%d' is not in the "
711 __func__, param->lsp_stripe_offset);
715 lum_size = lov_user_md_size(param->lsp_stripe_count,
716 LOV_USER_MAGIC_SPECIFIC);
719 lum = calloc(1, lum_size);
724 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
726 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
727 flags = O_DIRECTORY | O_RDONLY;
734 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
739 /* Initialize IOCTL striping pattern structure */
740 lum->lmm_magic = LOV_USER_MAGIC_V1;
741 lum->lmm_pattern = param->lsp_stripe_pattern;
742 lum->lmm_stripe_size = param->lsp_stripe_size;
743 lum->lmm_stripe_count = param->lsp_stripe_count;
744 lum->lmm_stripe_offset = param->lsp_stripe_offset;
745 if (pool_name != NULL) {
746 struct lov_user_md_v3 *lumv3 = (void *)lum;
748 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
749 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
751 if (param->lsp_is_specific) {
752 struct lov_user_md_v3 *lumv3 = (void *)lum;
755 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
756 if (pool_name == NULL) {
757 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
758 * OST list, therefore if pool is not specified we have
759 * to pack a null pool name for placeholder. */
760 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
763 for (i = 0; i < param->lsp_stripe_count; i++)
764 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
767 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
768 char *errmsg = "stripe already set";
771 if (errno != EEXIST && errno != EALREADY)
772 errmsg = strerror(errno);
774 llapi_err_noerrno(LLAPI_MSG_ERROR,
775 "setstripe error for '%s': %s", name, errmsg);
786 int llapi_file_open_pool(const char *name, int flags, int mode,
787 unsigned long long stripe_size, int stripe_offset,
788 int stripe_count, int stripe_pattern, char *pool_name)
790 const struct llapi_stripe_param param = {
791 .lsp_stripe_size = stripe_size,
792 .lsp_stripe_count = stripe_count,
793 .lsp_stripe_pattern = stripe_pattern,
794 .lsp_stripe_offset = stripe_offset,
795 .lsp_pool = pool_name
797 return llapi_file_open_param(name, flags, mode, ¶m);
800 int llapi_file_open(const char *name, int flags, int mode,
801 unsigned long long stripe_size, int stripe_offset,
802 int stripe_count, int stripe_pattern)
804 return llapi_file_open_pool(name, flags, mode, stripe_size,
805 stripe_offset, stripe_count,
806 stripe_pattern, NULL);
809 int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
810 __u32 flags, char *foreign_lov)
813 struct lov_foreign_md *lfm;
816 if (foreign_lov == NULL) {
817 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
818 "foreign LOV EA content must be provided");
822 len = strlen(foreign_lov);
823 if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
825 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
826 "foreign LOV EA size %zu (must be 0 < len < %zu)",
827 len, XATTR_SIZE_MAX -
828 offsetof(struct lov_foreign_md, lfm_value));
832 lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
834 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
835 "failed to allocate lov_foreign_md");
839 fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
846 lfm->lfm_magic = LOV_USER_MAGIC_FOREIGN;
847 lfm->lfm_length = len;
848 lfm->lfm_type = type;
849 lfm->lfm_flags = flags;
850 memcpy(lfm->lfm_value, foreign_lov, len);
852 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
853 char *errmsg = "stripe already set";
854 char fsname[MAX_OBD_NAME + 1] = { 0 };
857 if (errno != EEXIST && errno != EALREADY)
858 errmsg = strerror(errno);
860 llapi_err_noerrno(LLAPI_MSG_ERROR,
861 "setstripe error for '%s': %s", name, errmsg);
863 /* Make sure we are on a Lustre file system */
864 if (rc == -ENOTTY && llapi_search_fsname(name, fsname))
865 llapi_error(LLAPI_MSG_ERROR, rc,
866 "'%s' is not on a Lustre filesystem",
879 int llapi_file_create(const char *name, unsigned long long stripe_size,
880 int stripe_offset, int stripe_count, int stripe_pattern)
884 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
885 stripe_offset, stripe_count, stripe_pattern,
894 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
895 int stripe_offset, int stripe_count,
896 int stripe_pattern, char *pool_name)
900 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
901 stripe_offset, stripe_count, stripe_pattern,
910 static int verify_dir_param(const char *name,
911 const struct llapi_stripe_param *param)
913 char fsname[MAX_OBD_NAME + 1] = { 0 };
914 char *pool_name = param->lsp_pool;
917 /* Make sure we are on a Lustre file system */
918 rc = llapi_search_fsname(name, fsname);
920 llapi_error(LLAPI_MSG_ERROR, rc,
921 "'%s' is not on a Lustre filesystem",
926 /* Check if the stripe pattern is sane. */
927 rc = llapi_dir_stripe_limit_check(param->lsp_stripe_offset,
928 param->lsp_stripe_count,
929 param->lsp_stripe_pattern);
933 /* Make sure we have a good pool */
934 if (pool_name != NULL) {
935 /* in case user gives the full pool name <fsname>.<poolname>,
936 * strip the fsname */
937 char *ptr = strchr(pool_name, '.');
941 if (strcmp(pool_name, fsname) != 0) {
943 llapi_err_noerrno(LLAPI_MSG_ERROR,
944 "Pool '%s' is not on filesystem '%s'",
951 /* Make sure the pool exists and is non-empty */
952 rc = llapi_search_tgt(fsname, pool_name, NULL, true);
954 char *err = rc == 0 ? "has no OSTs" : "does not exist";
956 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
957 fsname, pool_name, err);
962 /* sanity check of target list */
963 if (param->lsp_is_specific) {
964 char mdtname[MAX_OBD_NAME + 1];
968 for (i = 0; i < param->lsp_stripe_count; i++) {
969 snprintf(mdtname, sizeof(mdtname), "%s-MDT%04x_UUID",
970 fsname, param->lsp_tgts[i]);
971 rc = llapi_search_tgt(fsname, pool_name, mdtname, true);
976 llapi_error(LLAPI_MSG_ERROR, rc,
977 "%s: cannot find MDT %s in %s",
984 /* Make sure stripe offset is in MDT list. */
985 if (param->lsp_tgts[i] == param->lsp_stripe_offset)
989 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
990 "%s: stripe offset '%d' is not in the "
992 __func__, param->lsp_stripe_offset);
1000 static inline void param2lmu(struct lmv_user_md *lmu,
1001 const struct llapi_stripe_param *param)
1003 lmu->lum_magic = param->lsp_is_specific ? LMV_USER_MAGIC_SPECIFIC :
1005 lmu->lum_stripe_count = param->lsp_stripe_count;
1006 lmu->lum_stripe_offset = param->lsp_stripe_offset;
1007 lmu->lum_hash_type = param->lsp_stripe_pattern;
1008 if (param->lsp_pool != NULL)
1009 strncpy(lmu->lum_pool_name, param->lsp_pool, LOV_MAXPOOLNAME);
1010 if (param->lsp_is_specific) {
1013 for (i = 0; i < param->lsp_stripe_count; i++)
1014 lmu->lum_objects[i].lum_mds = param->lsp_tgts[i];
1018 int llapi_dir_set_default_lmv(const char *name,
1019 const struct llapi_stripe_param *param)
1021 struct lmv_user_md lmu = { 0 };
1025 rc = verify_dir_param(name, param);
1029 /* TODO: default lmv doesn't support specific targets yet */
1030 if (param->lsp_is_specific)
1033 param2lmu(&lmu, param);
1035 fd = open(name, O_DIRECTORY | O_RDONLY);
1038 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1042 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lmu);
1044 char *errmsg = "stripe already set";
1046 if (errno != EEXIST && errno != EALREADY)
1047 errmsg = strerror(errno);
1049 llapi_err_noerrno(LLAPI_MSG_ERROR,
1050 "default dirstripe error on '%s': %s",
1057 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
1058 int stripe_count, int stripe_pattern,
1059 const char *pool_name)
1061 const struct llapi_stripe_param param = {
1062 .lsp_stripe_count = stripe_count,
1063 .lsp_stripe_offset = stripe_offset,
1064 .lsp_stripe_pattern = stripe_pattern,
1065 .lsp_pool = (char *)pool_name
1068 return llapi_dir_set_default_lmv(name, ¶m);
1072 * Create a Lustre directory.
1074 * \param name the name of the directory to be created
1075 * \param mode permission of the file if it is created, see mode in open(2)
1076 * \param param stripe pattern of the newly created directory
1078 * \retval 0 on success
1079 * \retval negative errno on failure
1081 int llapi_dir_create(const char *name, mode_t mode,
1082 const struct llapi_stripe_param *param)
1084 struct lmv_user_md *lmu = NULL;
1086 struct obd_ioctl_data data = { 0 };
1089 char *dirpath = NULL;
1090 char *namepath = NULL;
1095 rc = verify_dir_param(name, param);
1099 lmu_size = lmv_user_md_size(param->lsp_stripe_count,
1100 param->lsp_is_specific ?
1101 LMV_USER_MAGIC_SPECIFIC :
1104 lmu = calloc(1, lmu_size);
1108 dirpath = strdup(name);
1114 namepath = strdup(name);
1121 param2lmu(lmu, param);
1123 filename = basename(namepath);
1124 dir = dirname(dirpath);
1126 data.ioc_inlbuf1 = (char *)filename;
1127 data.ioc_inllen1 = strlen(filename) + 1;
1128 data.ioc_inlbuf2 = (char *)lmu;
1129 data.ioc_inllen2 = lmu_size;
1130 data.ioc_type = mode;
1131 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1133 llapi_error(LLAPI_MSG_ERROR, rc,
1134 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1139 fd = open(dir, O_DIRECTORY | O_RDONLY);
1142 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1146 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1147 char *errmsg = "stripe already set";
1150 if (errno != EEXIST && errno != EALREADY)
1151 errmsg = strerror(errno);
1153 llapi_err_noerrno(LLAPI_MSG_ERROR,
1154 "dirstripe error on '%s': %s", name, errmsg);
1165 * Create a foreign directory.
1167 * \param name the name of the directory to be created
1168 * \param mode permission of the file if it is created, see mode in open(2)
1169 * \param type foreign type to be set in LMV EA
1170 * \param flags foreign flags to be set in LMV EA
1171 * \param value foreign pattern to be set in LMV EA
1173 * \retval 0 on success
1174 * \retval negative errno on failure
1176 int llapi_dir_create_foreign(const char *name, mode_t mode, __u32 type,
1177 __u32 flags, const char *value)
1179 struct lmv_foreign_md *lfm = NULL;
1180 size_t lfm_size, len;
1181 struct obd_ioctl_data data = { 0 };
1184 char *dirpath = NULL;
1185 char *namepath = NULL;
1190 len = strlen(value);
1191 if (len > XATTR_SIZE_MAX - offsetof(struct lmv_foreign_md, lfm_value) ||
1194 llapi_error(LLAPI_MSG_ERROR, rc, "invalid LOV EA length %zu "
1195 "(must be 0 < len < %zu)\n", len,
1197 offsetof(struct lmv_foreign_md, lfm_value));
1200 lfm_size = len + offsetof(struct lmv_foreign_md, lfm_value);
1201 lfm = calloc(1, lfm_size);
1205 dirpath = strdup(name);
1211 namepath = strdup(name);
1218 lfm->lfm_magic = LMV_MAGIC_FOREIGN;
1219 lfm->lfm_length = len;
1220 lfm->lfm_type = type;
1221 lfm->lfm_flags = flags;
1222 strncpy(lfm->lfm_value, value, len);
1224 filename = basename(namepath);
1225 dir = dirname(dirpath);
1227 data.ioc_inlbuf1 = (char *)filename;
1228 data.ioc_inllen1 = strlen(filename) + 1;
1229 data.ioc_inlbuf2 = (char *)lfm;
1230 data.ioc_inllen2 = lfm_size;
1231 data.ioc_type = mode;
1232 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
1234 llapi_error(LLAPI_MSG_ERROR, rc,
1235 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
1240 fd = open(dir, O_DIRECTORY | O_RDONLY);
1243 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
1247 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
1248 char *errmsg = "stripe already set";
1251 if (errno != EEXIST && errno != EALREADY)
1252 errmsg = strerror(errno);
1254 llapi_err_noerrno(LLAPI_MSG_ERROR,
1255 "dirstripe error on '%s': %s", name, errmsg);
1265 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
1266 int stripe_count, int stripe_pattern,
1267 const char *pool_name)
1269 const struct llapi_stripe_param param = {
1270 .lsp_stripe_count = stripe_count,
1271 .lsp_stripe_offset = stripe_offset,
1272 .lsp_stripe_pattern = stripe_pattern,
1273 .lsp_pool = (char *)pool_name
1276 return llapi_dir_create(name, mode, ¶m);
1279 int llapi_direntry_remove(char *dname)
1281 char *dirpath = NULL;
1282 char *namepath = NULL;
1288 dirpath = strdup(dname);
1289 namepath = strdup(dname);
1290 if (!dirpath || !namepath)
1293 filename = basename(namepath);
1295 dir = dirname(dirpath);
1297 fd = open(dir, O_DIRECTORY | O_RDONLY);
1300 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
1305 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
1306 char *errmsg = strerror(errno);
1307 llapi_err_noerrno(LLAPI_MSG_ERROR,
1308 "error on ioctl %#jx for '%s' (%d): %s",
1309 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
1321 * Find the fsname, the full path, and/or an open fd.
1322 * Either the fsname or path must not be NULL
1324 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
1327 char buf[PATH_MAX], mntdir[PATH_MAX];
1328 char *ptr, *ptr_end;
1330 int idx = 0, len = 0, mntlen, fd;
1333 /* get the mount point */
1334 fp = setmntent(PROC_MOUNTS, "r");
1337 llapi_error(LLAPI_MSG_ERROR, rc,
1338 "cannot retrieve filesystem mount point");
1342 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1345 if (!llapi_is_lustre_mnt(&mnt))
1348 if ((want & WANT_INDEX) && (idx++ != index))
1351 mntlen = strlen(mnt.mnt_dir);
1352 ptr = strchr(mnt.mnt_fsname, '/');
1353 while (ptr && *ptr == '/')
1355 /* thanks to the call to llapi_is_lustre_mnt() above,
1356 * we are sure that mnt.mnt_fsname contains ":/",
1357 * so ptr should never be NULL */
1361 while (*ptr_end != '/' && *ptr_end != '\0')
1364 /* Check the fsname for a match, if given */
1365 if (!(want & WANT_FSNAME) && fsname != NULL &&
1366 (strlen(fsname) > 0) &&
1367 (strncmp(ptr, fsname, ptr_end - ptr) != 0))
1370 /* If the path isn't set return the first one we find */
1371 if (path == NULL || strlen(path) == 0) {
1372 strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
1373 mntdir[strlen(mnt.mnt_dir)] = '\0';
1374 if ((want & WANT_FSNAME) && fsname != NULL) {
1375 strncpy(fsname, ptr, ptr_end - ptr);
1376 fsname[ptr_end - ptr] = '\0';
1380 /* Otherwise find the longest matching path */
1381 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1382 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1383 strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
1384 mntdir[strlen(mnt.mnt_dir)] = '\0';
1386 if ((want & WANT_FSNAME) && fsname != NULL) {
1387 strncpy(fsname, ptr, ptr_end - ptr);
1388 fsname[ptr_end - ptr] = '\0';
1397 if ((want & WANT_PATH) && path != NULL) {
1398 strncpy(path, mntdir, strlen(mntdir));
1399 path[strlen(mntdir)] = '\0';
1401 if (want & WANT_FD) {
1402 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1405 llapi_error(LLAPI_MSG_ERROR, rc,
1406 "cannot open '%s': %s", mntdir,
1413 } else if (want & WANT_ERROR)
1414 llapi_err_noerrno(LLAPI_MSG_ERROR,
1415 "'%s' not on a mounted Lustre filesystem",
1416 (want & WANT_PATH) ? fsname : path);
1421 * search lustre mounts
1423 * Calling this function will return to the user the mount point, mntdir, and
1424 * the file system name, fsname, if the user passed a buffer to this routine.
1426 * The user inputs are pathname and index. If the pathname is supplied then
1427 * the value of the index will be ignored. The pathname will return data if
1428 * the pathname is located on a lustre mount. Index is used to pick which
1429 * mount point you want in the case of multiple mounted lustre file systems.
1430 * See function lfs_osts in lfs.c for an example of the index use.
1432 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1435 int want = WANT_PATH, idx = -1;
1437 if (!pathname || pathname[0] == '\0') {
1441 strcpy(mntdir, pathname);
1444 want |= WANT_FSNAME;
1445 return get_root_path(want, fsname, NULL, mntdir, idx);
1448 /* Given a path, find the corresponding Lustre fsname */
1449 int llapi_search_fsname(const char *pathname, char *fsname)
1454 path = realpath(pathname, NULL);
1456 char tmp[PATH_MAX - 1];
1462 if (pathname[0] != '/') {
1463 /* Need an absolute path, but realpath() only works for
1464 * pathnames that actually exist. We go through the
1465 * extra hurdle of dirname(getcwd() + pathname) in
1466 * case the relative pathname contains ".." in it. */
1467 char realpath[PATH_MAX - 1];
1469 if (getcwd(realpath, sizeof(realpath) - 2) == NULL) {
1471 llapi_error(LLAPI_MSG_ERROR, rc,
1472 "cannot get current working directory");
1476 rc = snprintf(tmp, sizeof(tmp), "%s/", realpath);
1477 if (rc >= sizeof(tmp)) {
1479 llapi_error(LLAPI_MSG_ERROR, rc,
1480 "invalid parent path '%s'",
1486 rc = snprintf(buf, sizeof(buf), "%s%s", tmp, pathname);
1487 if (rc >= sizeof(buf)) {
1489 llapi_error(LLAPI_MSG_ERROR, rc,
1490 "invalid path '%s'", pathname);
1493 path = realpath(buf, NULL);
1495 ptr = strrchr(buf, '/');
1497 llapi_error(LLAPI_MSG_ERROR |
1498 LLAPI_MSG_NO_ERRNO, 0,
1499 "cannot resolve path '%s'",
1504 path = realpath(buf, NULL);
1507 llapi_error(LLAPI_MSG_ERROR, rc,
1508 "cannot resolve path '%s'",
1514 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1519 int llapi_search_rootpath(char *pathname, const char *fsname)
1521 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1524 int llapi_getname(const char *path, char *buf, size_t size)
1526 struct obd_uuid uuid_buf;
1527 char *uuid = uuid_buf.uuid;
1529 int rc, len, fsname_len;
1531 memset(&uuid_buf, 0, sizeof(uuid_buf));
1532 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1537 * We want to turn testfs-clilov-ffff88002738bc00 into
1538 * testfs-ffff88002738bc00 in a portable way that doesn't depend
1539 * on what is after "-clilov-" as it may change in the future.
1540 * Unfortunately, the "fsname" part may contain a dash, so we
1541 * can't just skip to the first dash, and the "instance" may be a
1542 * UUID in the future, so we can't necessarily go to the last dash.
1544 cfg_instance = strstr(uuid, "-clilov-");
1548 fsname_len = cfg_instance - uuid;
1549 cfg_instance += strlen("-clilov-");
1550 len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
1559 * Get the list of pool members.
1560 * \param poolname string of format \<fsname\>.\<poolname\>
1561 * \param members caller-allocated array of char*
1562 * \param list_size size of the members array
1563 * \param buffer caller-allocated buffer for storing OST names
1564 * \param buffer_size size of the buffer
1566 * \return number of members retrieved for this pool
1567 * \retval -error failure
1569 int llapi_get_poolmembers(const char *poolname, char **members,
1570 int list_size, char *buffer, int buffer_size)
1572 char fsname[PATH_MAX];
1581 /* name is FSNAME.POOLNAME */
1582 if (strlen(poolname) >= sizeof(fsname))
1585 snprintf(fsname, sizeof(fsname), "%s", poolname);
1586 pool = strchr(fsname, '.');
1593 rc = poolpath(&pathname, fsname, NULL);
1595 llapi_error(LLAPI_MSG_ERROR, rc,
1596 "Lustre filesystem '%s' not found",
1601 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1602 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1603 cfs_free_param_data(&pathname);
1604 if (rc >= sizeof(buf))
1606 fd = fopen(buf, "r");
1609 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1614 while (fgets(buf, sizeof(buf), fd) != NULL) {
1615 if (nb_entries >= list_size) {
1619 buf[sizeof(buf) - 1] = '\0';
1621 tmp = strchr(buf, '\n');
1624 if (used + strlen(buf) + 1 > buffer_size) {
1629 strcpy(buffer + used, buf);
1630 members[nb_entries] = buffer + used;
1631 used += strlen(buf) + 1;
1641 * Get the list of pools in a filesystem.
1642 * \param name filesystem name or path
1643 * \param poollist caller-allocated array of char*
1644 * \param list_size size of the poollist array
1645 * \param buffer caller-allocated buffer for storing pool names
1646 * \param buffer_size size of the buffer
1648 * \return number of pools retrieved for this filesystem
1649 * \retval -error failure
1651 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1652 char *buffer, int buffer_size)
1654 char rname[PATH_MAX];
1659 struct dirent *pool;
1661 unsigned int nb_entries = 0;
1662 unsigned int used = 0;
1665 /* initialize output array */
1666 for (i = 0; i < list_size; i++)
1669 /* is name a pathname ? */
1670 ptr = strchr(name, '/');
1672 /* only absolute pathname is supported */
1676 if (!realpath(name, rname)) {
1678 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1683 fsname = strdup(rname);
1687 rc = poolpath(&pathname, NULL, rname);
1689 /* name is FSNAME */
1690 fsname = strdup(name);
1693 rc = poolpath(&pathname, fsname, NULL);
1696 llapi_error(LLAPI_MSG_ERROR, rc,
1697 "Lustre filesystem '%s' not found", name);
1701 dir = opendir(pathname.gl_pathv[0]);
1704 llapi_error(LLAPI_MSG_ERROR, rc,
1705 "Could not open pool list for '%s'",
1712 pool = readdir(dir);
1718 /* ignore . and .. */
1719 if (!strcmp(pool->d_name, ".") || !strcmp(pool->d_name, ".."))
1722 /* check output bounds */
1723 if (nb_entries >= list_size) {
1725 goto free_dir_no_msg;
1728 /* +2 for '.' and final '\0' */
1729 if (used + strlen(pool->d_name) + strlen(fsname) + 2
1732 goto free_dir_no_msg;
1735 sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
1736 poollist[nb_entries] = buffer + used;
1737 used += strlen(pool->d_name) + strlen(fsname) + 2;
1743 llapi_error(LLAPI_MSG_ERROR, rc,
1744 "Error reading pool list for '%s'", name);
1746 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1751 cfs_free_param_data(&pathname);
1754 return rc != 0 ? rc : nb_entries;
1757 /* wrapper for lfs.c and obd.c */
1758 int llapi_poollist(const char *name)
1760 /* list of pool names (assume that pool count is smaller
1762 char **list, *buffer = NULL, *fsname = (char *)name;
1763 char *poolname = NULL, *tmp = NULL, data[16];
1764 enum param_filter type = FILTER_BY_PATH;
1765 int obdcount, bufsize, rc, nb, i;
1770 if (name[0] != '/') {
1771 fsname = strdup(name);
1775 poolname = strchr(fsname, '.');
1778 type = FILTER_BY_FS_NAME;
1781 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1782 data, sizeof(data));
1785 obdcount = atoi(data);
1787 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1788 * and also an array to store the pointers for all that
1789 * allocated space. */
1791 bufsize = sizeof(struct obd_uuid) * obdcount;
1792 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1793 if (buffer == NULL) {
1797 list = (char **) (buffer + bufsize);
1800 /* name is a path or fsname */
1801 nb = llapi_get_poollist(name, list, obdcount,
1804 /* name is a pool name (<fsname>.<poolname>) */
1805 nb = llapi_get_poolmembers(name, list, obdcount,
1809 if (nb == -EOVERFLOW) {
1812 goto retry_get_pools;
1815 for (i = 0; i < nb; i++)
1816 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1817 rc = (nb < 0 ? nb : 0);
1821 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1826 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1827 void *data, struct dirent64 *de);
1829 #define OBD_NOT_FOUND (-1)
1831 static void find_param_fini(struct find_param *param)
1833 if (param->fp_migrate)
1836 if (param->fp_obd_indexes) {
1837 free(param->fp_obd_indexes);
1838 param->fp_obd_indexes = NULL;
1841 if (param->fp_lmd) {
1842 free(param->fp_lmd);
1843 param->fp_lmd = NULL;
1846 if (param->fp_lmv_md) {
1847 free(param->fp_lmv_md);
1848 param->fp_lmv_md = NULL;
1852 static int common_param_init(struct find_param *param, char *path)
1854 int lum_size = get_mds_md_size(path);
1859 /* migrate has fp_lmv_md initialized outside */
1860 if (param->fp_migrate)
1863 if (lum_size < PATH_MAX + 1)
1864 lum_size = PATH_MAX + 1;
1866 param->fp_lum_size = lum_size;
1867 param->fp_lmd = calloc(1, sizeof(lstat_t) + lum_size);
1868 if (param->fp_lmd == NULL) {
1869 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1870 "error: allocation of %zu bytes for ioctl",
1871 sizeof(lstat_t) + param->fp_lum_size);
1875 param->fp_lmv_stripe_count = 256;
1876 param->fp_lmv_md = calloc(1,
1877 lmv_user_md_size(param->fp_lmv_stripe_count,
1878 LMV_USER_MAGIC_SPECIFIC));
1879 if (param->fp_lmv_md == NULL) {
1880 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1881 "error: allocation of %d bytes for ioctl",
1882 lmv_user_md_size(param->fp_lmv_stripe_count,
1883 LMV_USER_MAGIC_SPECIFIC));
1884 find_param_fini(param);
1888 param->fp_got_uuids = 0;
1889 param->fp_obd_indexes = NULL;
1890 param->fp_obd_index = OBD_NOT_FOUND;
1891 param->fp_mdt_index = OBD_NOT_FOUND;
1895 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1896 struct dirent64 *de)
1898 struct find_param *param = data;
1904 /* set errno upon failure */
1905 static DIR *opendir_parent(const char *path)
1911 path_copy = strdup(path);
1912 if (path_copy == NULL)
1915 parent_path = dirname(path_copy);
1916 parent = opendir(parent_path);
1922 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1927 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1928 if (param->fp_get_default_lmv)
1929 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1931 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1933 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1934 if (errno == E2BIG && ret != 0) {
1938 /* if foreign LMV case, fake stripes number */
1939 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
1940 struct lmv_foreign_md *lfm;
1942 lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
1943 if (lfm->lfm_length < XATTR_SIZE_MAX -
1944 offsetof(typeof(*lfm), lfm_value)) {
1945 uint32_t size = lfm->lfm_length +
1946 offsetof(typeof(*lfm), lfm_value);
1948 stripe_count = lmv_foreign_to_md_stripes(size);
1950 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
1951 "error: invalid %d foreign size "
1952 "returned from ioctl",
1957 stripe_count = param->fp_lmv_md->lum_stripe_count;
1959 if (stripe_count <= param->fp_lmv_stripe_count)
1962 free(param->fp_lmv_md);
1963 param->fp_lmv_stripe_count = stripe_count;
1964 lmv_size = lmv_user_md_size(stripe_count,
1965 LMV_USER_MAGIC_SPECIFIC);
1966 param->fp_lmv_md = malloc(lmv_size);
1967 if (param->fp_lmv_md == NULL) {
1968 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1969 "error: allocation of %d bytes for ioctl",
1970 lmv_user_md_size(param->fp_lmv_stripe_count,
1971 LMV_USER_MAGIC_SPECIFIC));
1979 int get_lmd_info_fd(char *path, int parent_fd, int dir_fd,
1980 void *lmdbuf, int lmdlen, enum get_lmd_info_type type)
1982 struct lov_user_mds_data *lmd = lmdbuf;
1983 lstat_t *st = &lmd->lmd_st;
1986 if (parent_fd < 0 && dir_fd < 0)
1988 if (type != GET_LMD_INFO && type != GET_LMD_STRIPE)
1992 /* LL_IOC_MDC_GETINFO operates on the current directory inode
1993 * and returns struct lov_user_mds_data, while
1994 * LL_IOC_LOV_GETSTRIPE returns only struct lov_user_md.
1996 ret = ioctl(dir_fd, type == GET_LMD_INFO ? LL_IOC_MDC_GETINFO :
1997 LL_IOC_LOV_GETSTRIPE,
1999 } else if (parent_fd >= 0) {
2000 char *fname = strrchr(path, '/');
2002 /* IOC_MDC_GETFILEINFO takes as input the filename (relative to
2003 * the parent directory) and returns struct lov_user_mds_data,
2004 * while IOC_MDC_GETFILESTRIPE returns only struct lov_user_md.
2006 * This avoids opening, locking, and closing each file on the
2007 * client if that is not needed. Multiple of these ioctl() can
2008 * be done on the parent dir with a single open for all
2009 * files in that directory, and it also doesn't pollute the
2010 * client dcache with millions of dentries when traversing
2011 * a large filesystem.
2013 fname = (fname == NULL ? path : fname + 1);
2015 ret = snprintf(lmdbuf, lmdlen, "%s", fname);
2018 else if (ret >= lmdlen || ret++ == 0)
2021 ret = ioctl(parent_fd, type == GET_LMD_INFO ?
2022 IOC_MDC_GETFILEINFO :
2023 IOC_MDC_GETFILESTRIPE, lmdbuf);
2026 if (ret && type == GET_LMD_INFO) {
2027 if (errno == ENOTTY) {
2028 /* ioctl is not supported, it is not a lustre fs.
2029 * Do the regular lstat(2) instead.
2031 ret = lstat_f(path, st);
2034 llapi_error(LLAPI_MSG_ERROR, ret,
2035 "error: %s: lstat failed for %s",
2038 } else if (errno == ENOENT) {
2040 llapi_error(LLAPI_MSG_WARN, ret,
2041 "warning: %s does not exist", path);
2042 } else if (errno != EISDIR && errno != ENODATA) {
2044 llapi_error(LLAPI_MSG_ERROR, ret,
2045 "%s ioctl failed for %s.",
2046 dir_fd >= 0 ? "LL_IOC_MDC_GETINFO" :
2047 "IOC_MDC_GETFILEINFO", path);
2054 static int get_lmd_info(char *path, DIR *parent, DIR *dir, void *lmdbuf,
2055 int lmdlen, enum get_lmd_info_type type)
2061 parent_fd = dirfd(parent);
2063 dir_fd = dirfd(dir);
2065 return get_lmd_info_fd(path, parent_fd, dir_fd, lmdbuf, lmdlen, type);
2068 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
2069 semantic_func_t sem_init,
2070 semantic_func_t sem_fini, void *data,
2071 struct dirent64 *de)
2073 struct find_param *param = (struct find_param *)data;
2074 struct dirent64 *dent;
2082 if (!d && errno != ENOTDIR) {
2084 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
2087 } else if (!d && !parent) {
2088 /* ENOTDIR. Open the parent dir. */
2089 p = opendir_parent(path);
2096 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
2102 while ((dent = readdir64(d)) != NULL) {
2105 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
2109 if ((len + dent->d_reclen + 2) > size) {
2110 llapi_err_noerrno(LLAPI_MSG_ERROR,
2111 "error: %s: string buffer too small",
2116 strcat(path, dent->d_name);
2118 if (dent->d_type == DT_UNKNOWN) {
2119 lstat_t *st = ¶m->fp_lmd->lmd_st;
2121 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
2122 param->fp_lum_size, GET_LMD_INFO);
2124 dent->d_type = IFTODT(st->st_mode);
2131 switch (dent->d_type) {
2133 llapi_err_noerrno(LLAPI_MSG_ERROR,
2134 "error: %s: '%s' is UNKNOWN type %d",
2135 __func__, dent->d_name, dent->d_type);
2138 rc = llapi_semantic_traverse(path, size, d, sem_init,
2139 sem_fini, data, dent);
2140 if (rc != 0 && ret == 0)
2146 rc = sem_init(path, d, NULL, data, dent);
2147 if (rc < 0 && ret == 0) {
2152 if (sem_fini && rc == 0)
2153 sem_fini(path, d, NULL, data, dent);
2161 sem_fini(path, parent, &d, data, de);
2170 static int param_callback(char *path, semantic_func_t sem_init,
2171 semantic_func_t sem_fini, struct find_param *param)
2173 int ret, len = strlen(path);
2176 if (len > PATH_MAX) {
2178 llapi_error(LLAPI_MSG_ERROR, ret,
2179 "Path name '%s' is too long", path);
2183 buf = (char *)malloc(PATH_MAX + 1);
2187 snprintf(buf, PATH_MAX + 1, "%s", path);
2188 ret = common_param_init(param, buf);
2192 param->fp_depth = 0;
2194 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
2195 sem_fini, param, NULL);
2197 find_param_fini(param);
2199 return ret < 0 ? ret : 0;
2202 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
2204 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
2208 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get lov name");
2213 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
2215 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
2218 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
2223 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
2227 fd = open(path, O_RDONLY | O_NONBLOCK);
2230 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
2234 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
2240 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
2244 fd = open(path, O_RDONLY | O_NONBLOCK);
2247 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2251 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
2263 * If uuidp is NULL, return the number of available obd uuids.
2264 * If uuidp is non-NULL, then it will return the uuids of the obds. If
2265 * there are more OSTs than allocated to uuidp, then an error is returned with
2266 * the ost_count set to number of available obd uuids.
2268 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
2269 int *ost_count, enum tgt_type type)
2271 char buf[PATH_MAX], format[32];
2272 int rc = 0, index = 0;
2273 struct obd_uuid name;
2277 /* Get the lov name */
2278 if (type == LOV_TYPE)
2279 rc = llapi_file_fget_lov_uuid(fd, &name);
2281 rc = llapi_file_fget_lmv_uuid(fd, &name);
2285 /* Now get the ost uuids */
2286 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
2287 FILTER_BY_EXACT, "target_obd", ¶m);
2291 fp = fopen(param.gl_pathv[0], "r");
2294 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2299 snprintf(format, sizeof(format),
2300 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
2301 while (fgets(buf, sizeof(buf), fp) != NULL) {
2302 if (uuidp && (index < *ost_count)) {
2303 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
2311 if (uuidp && (index > *ost_count))
2316 cfs_free_param_data(¶m);
2320 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
2322 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
2325 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
2330 root = open(mnt, O_RDONLY | O_DIRECTORY);
2333 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2338 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
2346 /* Check if user specified value matches a real uuid. Ignore _UUID,
2347 * -osc-4ba41334, other trailing gunk in comparison.
2348 * @param real_uuid ends in "_UUID"
2349 * @param search_uuid may or may not end in "_UUID"
2351 int llapi_uuid_match(char *real_uuid, char *search_uuid)
2353 int cmplen = strlen(real_uuid);
2354 int searchlen = strlen(search_uuid);
2356 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
2358 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
2361 /* The UUIDs may legitimately be different lengths, if
2362 * the system was upgraded from an older version. */
2363 if (cmplen != searchlen)
2366 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
2369 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
2370 * returned in param->fp_obd_index */
2371 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
2373 struct obd_uuid obd_uuid;
2380 if (param->fp_got_uuids)
2383 /* Get the lov/lmv name */
2384 if (param->fp_get_lmv)
2385 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
2387 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
2389 if (rc != -ENOTTY) {
2390 llapi_error(LLAPI_MSG_ERROR, rc,
2391 "error: can't get %s name: %s",
2392 param->fp_get_lmv ? "lmv" : "lov",
2400 param->fp_got_uuids = 1;
2402 /* Now get the ost uuids */
2403 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
2404 obd_uuid.uuid, FILTER_BY_EXACT,
2405 "target_obd", ¶m_data);
2409 fp = fopen(param_data.gl_pathv[0], "r");
2412 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
2413 param_data.gl_pathv[0]);
2417 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
2418 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
2419 param->fp_get_lmv ? "MDTS" : "OBDS");
2421 snprintf(format, sizeof(format),
2422 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
2423 while (fgets(buf, sizeof(buf), fp) != NULL) {
2426 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
2429 if (param->fp_obd_uuid) {
2430 if (llapi_uuid_match(obd_uuid.uuid,
2431 param->fp_obd_uuid->uuid)) {
2432 param->fp_obd_index = index;
2435 } else if (!param->fp_quiet && !param->fp_obds_printed) {
2436 /* Print everything */
2437 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
2440 param->fp_obds_printed = 1;
2444 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
2445 llapi_err_noerrno(LLAPI_MSG_ERROR,
2446 "error: %s: unknown obduuid: %s",
2447 __func__, param->fp_obd_uuid->uuid);
2451 cfs_free_param_data(¶m_data);
2455 /* In this case, param->fp_obd_uuid will be an array of obduuids and
2456 * obd index for all these obduuids will be returned in
2457 * param->fp_obd_indexes */
2458 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
2459 int num_obds, int **obdindexes, int *obdindex,
2462 int ret, obdcount, obd_valid = 0, obdnum;
2464 struct obd_uuid *uuids = NULL;
2468 if (type == LOV_TYPE)
2469 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
2471 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
2475 obdcount = atoi(buf);
2476 uuids = malloc(obdcount * sizeof(struct obd_uuid));
2481 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
2483 if (ret == -EOVERFLOW) {
2484 struct obd_uuid *uuids_temp;
2486 uuids_temp = realloc(uuids, obdcount *
2487 sizeof(struct obd_uuid));
2488 if (uuids_temp != NULL) {
2490 goto retry_get_uuids;
2495 llapi_error(LLAPI_MSG_ERROR, ret, "cannot get ost uuid");
2499 indexes = malloc(num_obds * sizeof(*obdindex));
2500 if (indexes == NULL) {
2505 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2508 /* The user may have specified a simple index */
2509 i = strtol(obduuids[obdnum].uuid, &end, 0);
2510 if (end && *end == '\0' && i < obdcount) {
2511 indexes[obdnum] = i;
2514 for (i = 0; i < obdcount; i++) {
2515 if (llapi_uuid_match(uuids[i].uuid,
2516 obduuids[obdnum].uuid)) {
2517 indexes[obdnum] = i;
2523 if (i >= obdcount) {
2524 indexes[obdnum] = OBD_NOT_FOUND;
2525 llapi_err_noerrno(LLAPI_MSG_ERROR,
2526 "invalid obduuid '%s'",
2527 obduuids[obdnum].uuid);
2533 *obdindex = OBD_NOT_FOUND;
2535 *obdindex = obd_valid;
2537 *obdindexes = indexes;
2545 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2549 if (param->fp_mdt_uuid) {
2550 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2552 ¶m->fp_mdt_indexes,
2553 ¶m->fp_mdt_index, LMV_TYPE);
2558 if (param->fp_obd_uuid) {
2559 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2561 ¶m->fp_obd_indexes,
2562 ¶m->fp_obd_index, LOV_TYPE);
2567 param->fp_got_uuids = 1;
2572 int llapi_ostlist(char *path, struct find_param *param)
2577 fd = open(path, O_RDONLY | O_DIRECTORY);
2581 ret = setup_obd_uuid(fd, path, param);
2588 * Tries to determine the default stripe attributes for a given filesystem. The
2589 * filesystem to check should be specified by fsname, or will be determined
2592 static int sattr_get_defaults(const char *const fsname,
2593 unsigned int *scount,
2594 unsigned int *ssize,
2595 unsigned int *soffset)
2601 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2602 "stripecount", val, sizeof(val));
2605 *scount = atoi(val);
2609 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2610 "stripesize", val, sizeof(val));
2617 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2618 "stripeoffset", val, sizeof(val));
2621 *soffset = atoi(val);
2628 * Tries to gather the default stripe attributes for a given filesystem. If
2629 * the attributes can be determined, they are cached for easy retreival the
2630 * next time they are needed. Only a single filesystem's attributes are
2633 int sattr_cache_get_defaults(const char *const fsname,
2634 const char *const pathname, unsigned int *scount,
2635 unsigned int *ssize, unsigned int *soffset)
2638 char fsname[PATH_MAX + 1];
2639 unsigned int stripecount;
2640 unsigned int stripesize;
2641 unsigned int stripeoffset;
2647 char fsname_buf[PATH_MAX + 1];
2648 unsigned int tmp[3];
2650 if (fsname == NULL) {
2651 rc = llapi_search_fsname(pathname, fsname_buf);
2655 snprintf(fsname_buf, sizeof(fsname_buf), "%s", fsname);
2658 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2660 * Ensure all 3 sattrs (count, size, and offset) are
2661 * successfully retrieved and stored in tmp before writing to
2664 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2668 cache.stripecount = tmp[0];
2669 cache.stripesize = tmp[1];
2670 cache.stripeoffset = tmp[2];
2671 snprintf(cache.fsname, sizeof(cache.fsname), "%s", fsname_buf);
2675 *scount = cache.stripecount;
2677 *ssize = cache.stripesize;
2679 *soffset = cache.stripeoffset;
2684 static char *layout2name(__u32 layout_pattern)
2686 if (layout_pattern == LOV_PATTERN_MDT)
2688 else if (layout_pattern == LOV_PATTERN_RAID0)
2690 else if (layout_pattern == (LOV_PATTERN_RAID0 | LOV_PATTERN_F_RELEASED))
2696 enum lov_dump_flags {
2697 LDF_IS_DIR = 0x0001,
2698 LDF_IS_RAW = 0x0002,
2699 LDF_INDENT = 0x0004,
2700 LDF_SKIP_OBJS = 0x0008,
2704 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2705 struct lov_user_ost_data_v1 *objects,
2706 enum llapi_layout_verbose verbose,
2707 int depth, char *pool_name,
2708 enum lov_dump_flags flags)
2710 bool is_dir = flags & LDF_IS_DIR;
2711 bool is_raw = flags & LDF_IS_RAW;
2712 bool indent = flags & LDF_INDENT;
2713 bool yaml = flags & LDF_YAML;
2714 bool skip_objs = flags & LDF_SKIP_OBJS;
2715 char *prefix = is_dir ? "" : "lmm_";
2716 char *separator = "";
2717 char *space = indent ? " " : "";
2720 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2721 lmm_oi_set_seq(&lum->lmm_oi, 0);
2722 if (!indent && (verbose & VERBOSE_DETAIL))
2723 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2726 if (!yaml && !indent && depth && path &&
2727 ((verbose != VERBOSE_OBJID) || !is_dir))
2728 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2730 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2731 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2732 space, prefix, lum->lmm_magic);
2733 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2735 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2736 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2738 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2740 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2745 if (verbose & ~VERBOSE_DFID)
2746 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2748 /* This needs a bit of hand-holding since old 1.x lmm_oi
2749 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2750 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2751 * a real FID. Ideally the 2.x code would have stored this
2752 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2753 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2754 * worked properly (especially since IGIF FIDs use mds_inum as
2755 * the FID SEQ), but unfortunately that didn't happen.
2757 * Print it to look like an IGIF FID, even though the fields
2758 * are reversed on disk, so that it makes sense to userspace.
2760 * Don't use ostid_id() and ostid_seq(), since they assume the
2761 * oi_fid fields are in the right order. This is why there are
2762 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2764 * For newer layout types hopefully this will be a real FID. */
2765 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2766 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2767 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2768 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2769 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2771 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2774 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2778 if (verbose & VERBOSE_STRIPE_COUNT) {
2779 if (verbose & ~VERBOSE_STRIPE_COUNT)
2780 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2783 if (!is_raw && lum->lmm_stripe_count == 0 &&
2784 lov_pattern(lum->lmm_pattern) != LOV_PATTERN_MDT) {
2785 unsigned int scount;
2786 rc = sattr_cache_get_defaults(NULL, path,
2790 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2793 llapi_error(LLAPI_MSG_ERROR, rc,
2794 "Cannot determine default"
2797 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2798 lum->lmm_stripe_count ==
2799 (typeof(lum->lmm_stripe_count))(-1)
2800 ? -1 : lum->lmm_stripe_count);
2803 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2804 (__s16)lum->lmm_stripe_count);
2806 if (!yaml && is_dir)
2812 if (verbose & VERBOSE_STRIPE_SIZE) {
2813 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2814 if (verbose & ~VERBOSE_STRIPE_SIZE)
2815 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2817 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2819 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2822 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2824 llapi_error(LLAPI_MSG_ERROR, rc,
2825 "Cannot determine default"
2828 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2829 lum->lmm_stripe_size);
2831 if (!yaml && is_dir)
2837 if ((verbose & VERBOSE_PATTERN)) {
2838 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2839 if (verbose & ~VERBOSE_PATTERN)
2840 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2842 if (lov_pattern_supported(lum->lmm_pattern))
2843 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2844 layout2name(lum->lmm_pattern));
2846 llapi_printf(LLAPI_MSG_NORMAL, "%x", lum->lmm_pattern);
2847 separator = (!yaml && is_dir) ? " " : "\n";
2850 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2851 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2852 if (verbose & ~VERBOSE_GENERATION)
2853 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2855 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2856 skip_objs ? 0 : (int)lum->lmm_layout_gen);
2860 if (verbose & VERBOSE_STRIPE_OFFSET) {
2861 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2862 if (verbose & ~VERBOSE_STRIPE_OFFSET)
2863 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2865 if (is_dir || skip_objs)
2866 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2867 lum->lmm_stripe_offset ==
2868 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2869 lum->lmm_stripe_offset);
2870 else if (lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)
2871 llapi_printf(LLAPI_MSG_NORMAL, "0");
2873 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2874 objects[0].l_ost_idx);
2875 if (!yaml && is_dir)
2881 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2882 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2883 if (verbose & ~VERBOSE_POOL)
2884 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2886 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2887 if (!yaml && is_dir)
2893 if (strlen(separator) != 0)
2894 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2897 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2898 struct lov_user_ost_data_v1 *objects,
2899 char *path, int obdindex, int depth,
2900 enum llapi_layout_verbose verbose,
2901 enum lov_dump_flags flags)
2903 bool is_dir = flags & LDF_IS_DIR;
2904 bool indent = flags & LDF_INDENT;
2905 bool skip_objs = flags & LDF_SKIP_OBJS;
2906 bool yaml = flags & LDF_YAML;
2907 bool obdstripe = obdindex == OBD_NOT_FOUND;
2910 if (!obdstripe && !skip_objs) {
2911 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2912 if (obdindex == objects[i].l_ost_idx) {
2922 lov_dump_user_lmm_header(lum, path, objects, verbose, depth, pool_name,
2925 if (!is_dir && !skip_objs && (verbose & VERBOSE_OBJID) &&
2926 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED ||
2927 lov_pattern(lum->lmm_pattern) == LOV_PATTERN_MDT)) {
2928 char *space = " - ";
2931 llapi_printf(LLAPI_MSG_NORMAL,
2932 "%6slmm_objects:\n", " ");
2934 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2936 llapi_printf(LLAPI_MSG_NORMAL,
2937 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2939 for (i = 0; i < lum->lmm_stripe_count; i++) {
2940 int idx = objects[i].l_ost_idx;
2941 long long oid = ostid_id(&objects[i].l_ost_oi);
2942 long long gr = ostid_seq(&objects[i].l_ost_oi);
2944 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2948 struct lu_fid fid = { 0 };
2950 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2951 llapi_printf(LLAPI_MSG_NORMAL,
2952 "%sl_ost_idx: %d\n", space, idx);
2953 llapi_printf(LLAPI_MSG_NORMAL,
2954 "%8sl_fid: "DFID_NOBRACE"\n",
2956 } else if (indent) {
2957 struct lu_fid fid = { 0 };
2959 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2960 llapi_printf(LLAPI_MSG_NORMAL,
2961 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2962 space, i, idx, PFID(&fid));
2965 sprintf(fmt, "%s%s%s\n",
2966 "\t%6u\t%14llu\t%#13llx\t",
2967 (fid_seq_is_rsvd(gr) ||
2968 fid_seq_is_mdt0(gr)) ?
2969 "%14llu" : "%#14llx", "%s");
2970 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2972 obdindex == idx ? " *" : "");
2976 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2979 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2980 char *path, int obdindex, int depth,
2981 enum llapi_layout_verbose verbose,
2982 enum lov_dump_flags flags)
2984 struct lmv_user_mds_data *objects = lum->lum_objects;
2985 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2986 char *separator = "";
2987 bool yaml = flags & LDF_YAML;
2988 bool obdstripe = false;
2991 if (obdindex != OBD_NOT_FOUND) {
2992 if (lum->lum_stripe_count == 0) {
2993 if (obdindex == lum->lum_stripe_offset)
2996 for (i = 0; i < lum->lum_stripe_count; i++) {
2997 if (obdindex == objects[i].lum_mds) {
2998 llapi_printf(LLAPI_MSG_NORMAL,
3013 /* show all information default */
3015 if (lum->lum_magic == LMV_USER_MAGIC)
3016 verbose = VERBOSE_POOL | VERBOSE_STRIPE_COUNT |
3017 VERBOSE_STRIPE_OFFSET | VERBOSE_HASH_TYPE;
3019 verbose = VERBOSE_OBJID;
3022 if (depth && path && ((verbose != VERBOSE_OBJID)))
3023 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
3025 if (verbose & VERBOSE_STRIPE_COUNT) {
3026 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3027 if (verbose & ~VERBOSE_STRIPE_COUNT)
3028 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
3029 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3030 (int)lum->lum_stripe_count);
3031 if ((verbose & VERBOSE_STRIPE_OFFSET) && !yaml)
3037 if (verbose & VERBOSE_STRIPE_OFFSET) {
3038 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3039 if (verbose & ~VERBOSE_STRIPE_OFFSET)
3040 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
3041 llapi_printf(LLAPI_MSG_NORMAL, "%d",
3042 (int)lum->lum_stripe_offset);
3043 if (verbose & VERBOSE_HASH_TYPE && !yaml)
3049 if (verbose & VERBOSE_HASH_TYPE) {
3050 unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
3051 unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
3053 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3054 if (verbose & ~VERBOSE_HASH_TYPE)
3055 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
3056 if (type < LMV_HASH_TYPE_MAX)
3057 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3058 mdt_hash_name[type]);
3060 llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
3062 if (flags & LMV_HASH_FLAG_MIGRATION)
3063 llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
3064 if (flags & LMV_HASH_FLAG_DEAD)
3065 llapi_printf(LLAPI_MSG_NORMAL, ",dead");
3066 if (flags & LMV_HASH_FLAG_BAD_TYPE)
3067 llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
3068 if (flags & LMV_HASH_FLAG_LOST_LMV)
3069 llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
3074 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
3075 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3076 if (lum->lum_stripe_count > 0)
3077 llapi_printf(LLAPI_MSG_NORMAL,
3078 "mdtidx\t\t FID[seq:oid:ver]\n");
3079 for (i = 0; i < lum->lum_stripe_count; i++) {
3080 int idx = objects[i].lum_mds;
3081 struct lu_fid *fid = &objects[i].lum_fid;
3082 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
3083 llapi_printf(LLAPI_MSG_NORMAL,
3084 "%6u\t\t "DFID"\t\t%s\n",
3086 obdindex == idx ? " *" : "");
3091 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
3092 pool_name[0] != '\0') {
3093 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3094 if (verbose & ~VERBOSE_POOL)
3095 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
3097 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
3101 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
3102 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3105 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
3106 enum lov_dump_flags flags)
3108 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3109 int depth = param->fp_max_depth;
3110 enum llapi_layout_verbose verbose = param->fp_verbose;
3111 bool yaml = flags & LDF_YAML;
3113 if (depth && path && ((verbose != VERBOSE_OBJID) ||
3114 !(flags & LDF_IS_DIR)) && !yaml)
3115 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3117 if (verbose & VERBOSE_DETAIL) {
3118 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
3119 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
3120 " ", comp_v1->lcm_magic);
3121 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
3122 " ", comp_v1->lcm_size);
3123 if (flags & LDF_IS_DIR)
3124 llapi_printf(LLAPI_MSG_NORMAL,
3125 "%2slcm_flags: %s\n", " ",
3126 comp_v1->lcm_mirror_count > 0 ?
3129 llapi_printf(LLAPI_MSG_NORMAL,
3130 "%2slcm_flags: %s\n", " ",
3131 llapi_layout_flags_string(comp_v1->lcm_flags));
3134 if (verbose & VERBOSE_GENERATION) {
3135 if (verbose & ~VERBOSE_GENERATION)
3136 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
3138 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
3141 if (verbose & VERBOSE_MIRROR_COUNT) {
3142 if (verbose & ~VERBOSE_MIRROR_COUNT)
3143 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_mirror_count: ",
3145 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3146 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3147 comp_v1->lcm_mirror_count + 1 : 1);
3150 if (verbose & VERBOSE_COMP_COUNT) {
3151 if (verbose & ~VERBOSE_COMP_COUNT)
3152 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
3154 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
3155 comp_v1->lcm_magic == LOV_USER_MAGIC_COMP_V1 ?
3156 comp_v1->lcm_entry_count : 0);
3159 if (verbose & VERBOSE_DETAIL && !yaml)
3160 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
3163 static void lcme_flags2str(__u32 comp_flags)
3169 llapi_printf(LLAPI_MSG_NORMAL, "0");
3172 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
3173 if (comp_flags & comp_flags_table[i].cfn_flag) {
3175 llapi_printf(LLAPI_MSG_NORMAL, ",");
3176 llapi_printf(LLAPI_MSG_NORMAL, "%s",
3177 comp_flags_table[i].cfn_name);
3178 comp_flags &= ~comp_flags_table[i].cfn_flag;
3184 llapi_printf(LLAPI_MSG_NORMAL, ",");
3185 llapi_printf(LLAPI_MSG_NORMAL, "%#x", comp_flags);
3189 static void lov_dump_comp_v1_entry(struct find_param *param,
3190 enum lov_dump_flags flags, int index)
3192 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3193 struct lov_comp_md_entry_v1 *entry;
3194 char *separator = "";
3195 enum llapi_layout_verbose verbose = param->fp_verbose;
3196 bool yaml = flags & LDF_YAML;
3198 entry = &comp_v1->lcm_entries[index];
3201 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
3203 if (verbose & VERBOSE_COMP_ID) {
3204 if (verbose & VERBOSE_DETAIL && !yaml)
3205 llapi_printf(LLAPI_MSG_NORMAL,
3206 "%slcme_id: ", " - ");
3207 else if (verbose & ~VERBOSE_COMP_ID)
3208 llapi_printf(LLAPI_MSG_NORMAL,
3209 "%4slcme_id: ", " ");
3210 if (entry->lcme_id != LCME_ID_INVAL)
3211 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
3213 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3217 if (verbose & VERBOSE_MIRROR_ID) {
3218 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3219 if (verbose & ~VERBOSE_MIRROR_ID)
3220 llapi_printf(LLAPI_MSG_NORMAL,
3221 "%4slcme_mirror_id: ", " ");
3222 if (entry->lcme_id != LCME_ID_INVAL)
3223 llapi_printf(LLAPI_MSG_NORMAL, "%u",
3224 mirror_id_of(entry->lcme_id));
3226 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
3230 if (verbose & VERBOSE_COMP_FLAGS) {
3231 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3232 if (verbose & ~VERBOSE_COMP_FLAGS)
3233 llapi_printf(LLAPI_MSG_NORMAL,
3234 "%4slcme_flags: ", " ");
3235 lcme_flags2str(entry->lcme_flags);
3238 /* print snapshot timestamp if its a nosync comp */
3239 if ((verbose & VERBOSE_COMP_FLAGS) &&
3240 (entry->lcme_flags & LCME_FL_NOSYNC)) {
3241 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3242 if (verbose & ~VERBOSE_COMP_FLAGS)
3243 llapi_printf(LLAPI_MSG_NORMAL,
3244 "%4slcme_timestamp: ", " ");
3246 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3247 entry->lcme_timestamp);
3249 time_t stamp = entry->lcme_timestamp;
3250 char *date_str = asctime(localtime(&stamp));
3252 date_str[strlen(date_str) - 1] = '\0';
3253 llapi_printf(LLAPI_MSG_NORMAL, "'%s'", date_str);
3259 if (verbose & VERBOSE_COMP_START) {
3260 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3261 if (verbose & ~VERBOSE_COMP_START)
3262 llapi_printf(LLAPI_MSG_NORMAL,
3263 "%4slcme_extent.e_start: ", " ");
3264 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3265 entry->lcme_extent.e_start);
3269 if (verbose & VERBOSE_COMP_END) {
3270 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3271 if (verbose & ~VERBOSE_COMP_END)
3272 llapi_printf(LLAPI_MSG_NORMAL,
3273 "%4slcme_extent.e_end: ", " ");
3274 if (entry->lcme_extent.e_end == LUSTRE_EOF)
3275 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
3277 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
3278 entry->lcme_extent.e_end);
3283 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3284 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3285 } else if (verbose & VERBOSE_DETAIL) {
3286 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3287 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
3288 " ", entry->lcme_offset);
3289 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
3290 " ", entry->lcme_size);
3291 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
3293 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
3297 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
3298 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
3300 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
3301 * The table below gives the answers for the specified parameters (value and
3302 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
3303 * --------------------------------------
3304 * 1 | file > limit; sign > 0 | -1 / -1 |
3305 * 2 | file = limit; sign > 0 | -1 / -1 |
3306 * 3 | file < limit; sign > 0 | ? / 1 |
3307 * 4 | file > limit; sign = 0 | -1 / -1 |
3308 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
3309 * 6 | file < limit; sign = 0 | ? / -1 |
3310 * 7 | file > limit; sign < 0 | 1 / 1 |
3311 * 8 | file = limit; sign < 0 | ? / -1 |
3312 * 9 | file < limit; sign < 0 | ? / -1 |
3313 * --------------------------------------
3314 * Note: 5th actually means that the value is within the interval
3315 * (limit - margin, limit]. */
3316 static int find_value_cmp(unsigned long long file, unsigned long long limit,
3317 int sign, int negopt, unsigned long long margin,
3323 /* Drop the fraction of margin (of days or size). */
3324 if (file + margin <= limit)
3326 } else if (sign == 0) {
3327 if (file <= limit && file + margin > limit)
3329 else if (file + margin <= limit)
3331 } else if (sign < 0) {
3338 return negopt ? ~ret + 1 : ret;
3341 static inline struct lov_user_md *
3342 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
3344 return (struct lov_user_md *)((char *)comp_v1 +
3345 comp_v1->lcm_entries[ent_idx].lcme_offset);
3348 static inline struct lov_user_ost_data_v1 *
3349 lov_v1v3_objects(struct lov_user_md *v1)
3351 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3352 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
3354 return v1->lmm_objects;
3358 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
3360 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
3361 snprintf(pool_name, LOV_MAXPOOLNAME, "%s",
3362 ((struct lov_user_md_v3 *)v1)->lmm_pool_name);
3364 pool_name[0] = '\0';
3368 print_last_init_comp(struct find_param *param)
3370 /* print all component info */
3371 if ((param->fp_verbose & VERBOSE_DEFAULT) == VERBOSE_DEFAULT)
3374 /* print specific component info */
3375 if (param->fp_check_comp_id || param->fp_check_comp_flags ||
3376 param->fp_check_comp_start || param->fp_check_comp_end ||
3377 param->fp_check_mirror_id || param->fp_check_mirror_index)
3383 static int find_comp_end_cmp(unsigned long long end, struct find_param *param)
3387 if (param->fp_comp_end == LUSTRE_EOF) {
3388 if (param->fp_comp_end_sign == 0) /* equal to EOF */
3389 match = end == LUSTRE_EOF ? 1 : -1;
3390 else if (param->fp_comp_end_sign > 0) /* at most EOF */
3391 match = end == LUSTRE_EOF ? -1 : 1;
3392 else /* at least EOF */
3394 if (param->fp_exclude_comp_end)
3397 unsigned long long margin;
3399 margin = end == LUSTRE_EOF ? 0 : param->fp_comp_end_units;
3400 match = find_value_cmp(end, param->fp_comp_end,
3401 param->fp_comp_end_sign,
3402 param->fp_exclude_comp_end, margin, 0);
3409 * An example of "getstripe -v" for a two components PFL file:
3412 * lcm_magic: 0x0BD60BD0
3416 * lcm_entry_count: 2
3420 * lcme_extent.e_start: 0
3421 * lcme_extent.e_end: 1048576
3425 * lmm_magic: 0x0BD10BD0
3426 * lmm_seq: 0x200000401
3427 * lmm_object_id: 0x1
3428 * lmm_fid: [0x200000401:0x1:0x0]
3429 * lmm_stripe_count: 1
3430 * lmm_stripe_size: 1048576
3431 * lmm_pattern: raid0
3433 * lmm_stripe_offset: 0
3435 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
3439 * lcme_extent.e_start: 1048576
3440 * lcme_extent.e_end: EOF
3444 * lmm_magic: 0x0BD10BD0
3445 * lmm_seq: 0x200000401
3446 * lmm_object_id: 0x1
3447 * lmm_fid: [0x200000401:0x1:0x0]
3448 * lmm_stripe_count: 2
3449 * lmm_stripe_size: 1048576
3450 * lmm_pattern: raid0
3452 * lmm_stripe_offset: 1
3454 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
3455 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
3457 static void lov_dump_comp_v1(struct find_param *param, char *path,
3458 enum lov_dump_flags flags)
3460 struct lov_comp_md_entry_v1 *entry;
3461 struct lov_user_ost_data_v1 *objects;
3462 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
3463 struct lov_user_md_v1 *v1;
3464 char pool_name[LOV_MAXPOOLNAME + 1];
3465 int obdindex = param->fp_obd_index;
3467 bool obdstripe = false;
3468 __u16 mirror_index = 0;
3469 __u16 mirror_id = 0;
3471 if (obdindex != OBD_NOT_FOUND) {
3472 for (i = 0; !(flags & LDF_IS_DIR) && !obdstripe &&
3473 i < comp_v1->lcm_entry_count; i++) {
3474 if (!(comp_v1->lcm_entries[i].lcme_flags &
3478 v1 = lov_comp_entry(comp_v1, i);
3479 objects = lov_v1v3_objects(v1);
3481 for (j = 0; j < v1->lmm_stripe_count; j++) {
3482 if (obdindex == objects[j].l_ost_idx) {
3495 lov_dump_comp_v1_header(param, path, flags);
3497 flags |= LDF_INDENT;
3499 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3500 entry = &comp_v1->lcm_entries[i];
3502 if (param->fp_check_comp_flags) {
3503 if (((param->fp_comp_flags & entry->lcme_flags) !=
3504 param->fp_comp_flags) ||
3505 (param->fp_comp_neg_flags & entry->lcme_flags))
3509 if (param->fp_check_comp_id &&
3510 param->fp_comp_id != entry->lcme_id)
3513 if (param->fp_check_comp_start) {
3514 match = find_value_cmp(entry->lcme_extent.e_start,
3515 param->fp_comp_start,
3516 param->fp_comp_start_sign,
3518 param->fp_comp_start_units, 0);
3523 if (param->fp_check_comp_end) {
3524 match = find_comp_end_cmp(entry->lcme_extent.e_end,
3530 if (param->fp_check_mirror_index) {
3531 if (mirror_id != mirror_id_of(entry->lcme_id)) {
3533 mirror_id = mirror_id_of(entry->lcme_id);
3536 match = find_value_cmp(mirror_index,
3537 param->fp_mirror_index,
3538 param->fp_mirror_index_sign,
3539 param->fp_exclude_mirror_index,
3543 } else if (param->fp_check_mirror_id) {
3544 if (mirror_id != mirror_id_of(entry->lcme_id))
3545 mirror_id = mirror_id_of(entry->lcme_id);
3547 match = find_value_cmp(mirror_id,
3548 param->fp_mirror_id,
3549 param->fp_mirror_id_sign,
3550 param->fp_exclude_mirror_id,
3556 if (print_last_init_comp(param)) {
3558 * if part of stripe info is needed, we'd print only
3559 * the last instantiated component info.
3561 if (entry->lcme_flags & LCME_FL_INIT)
3567 if (entry->lcme_flags & LCME_FL_INIT) {
3568 if (obdindex != OBD_NOT_FOUND) {
3569 flags |= LDF_SKIP_OBJS;
3570 v1 = lov_comp_entry(comp_v1, i);
3571 objects = lov_v1v3_objects(v1);
3573 for (j = 0; j < v1->lmm_stripe_count; j++) {
3574 if (obdindex == objects[j].l_ost_idx) {
3575 flags &= ~LDF_SKIP_OBJS;
3580 flags &= ~LDF_SKIP_OBJS;
3583 flags |= LDF_SKIP_OBJS;
3586 if (obdindex != OBD_NOT_FOUND && (flags & LDF_SKIP_OBJS))
3588 lov_dump_comp_v1_entry(param, flags, i);
3590 v1 = lov_comp_entry(comp_v1, i);
3591 objects = lov_v1v3_objects(v1);
3592 lov_v1v3_pool_name(v1, pool_name);
3594 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3595 param->fp_max_depth, param->fp_verbose,
3598 if (print_last_init_comp(param)) {
3600 * directory layout contains only layout template, print the
3604 i = comp_v1->lcm_entry_count - 1;
3607 flags &= ~LDF_SKIP_OBJS;
3609 lov_dump_comp_v1_entry(param, flags, i);
3611 v1 = lov_comp_entry(comp_v1, i);
3612 objects = lov_v1v3_objects(v1);
3613 lov_v1v3_pool_name(v1, pool_name);
3615 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
3616 param->fp_max_depth, param->fp_verbose,
3621 #define VERBOSE_COMP_OPTS (VERBOSE_COMP_COUNT | VERBOSE_COMP_ID | \
3622 VERBOSE_COMP_START | VERBOSE_COMP_END | \
3625 static inline bool has_any_comp_options(struct find_param *param)
3627 enum llapi_layout_verbose verbose = param->fp_verbose;
3629 if (param->fp_check_comp_id || param->fp_check_comp_count ||
3630 param->fp_check_comp_start || param->fp_check_comp_end ||
3631 param->fp_check_comp_flags)
3634 /* show full layout information, not component specific */
3635 if ((verbose & ~VERBOSE_DETAIL) == VERBOSE_DEFAULT)
3638 return verbose & VERBOSE_COMP_OPTS;
3641 struct lov_user_mds_data *lov_forge_comp_v1(struct lov_user_mds_data *orig,
3644 struct lov_user_md *lum = &orig->lmd_lmm;
3645 struct lov_user_mds_data *new;
3646 struct lov_comp_md_v1 *comp_v1;
3647 struct lov_comp_md_entry_v1 *ent;
3648 int lum_off = sizeof(*comp_v1) + sizeof(*ent);
3649 int lum_size = lov_user_md_size(is_dir ? 0 : lum->lmm_stripe_count,
3652 new = malloc(sizeof(lstat_t) + lum_off + lum_size);
3654 llapi_printf(LLAPI_MSG_NORMAL, "out of memory\n");
3658 memcpy(new, orig, sizeof(lstat_t));
3660 comp_v1 = (struct lov_comp_md_v1 *)&new->lmd_lmm;
3661 comp_v1->lcm_magic = lum->lmm_magic;
3662 comp_v1->lcm_size = lum_off + lum_size;
3663 comp_v1->lcm_layout_gen = is_dir ? 0 : lum->lmm_layout_gen;
3664 comp_v1->lcm_flags = 0;
3665 comp_v1->lcm_entry_count = 1;
3667 ent = &comp_v1->lcm_entries[0];
3669 ent->lcme_flags = is_dir ? 0 : LCME_FL_INIT;
3670 ent->lcme_extent.e_start = 0;
3671 ent->lcme_extent.e_end = LUSTRE_EOF;
3672 ent->lcme_offset = lum_off;
3673 ent->lcme_size = lum_size;
3675 memcpy((char *)comp_v1 + lum_off, lum, lum_size);
3680 static void lov_dump_plain_user_lmm(struct find_param *param, char *path,
3681 enum lov_dump_flags flags)
3683 __u32 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm;
3685 if (has_any_comp_options(param)) {
3686 struct lov_user_mds_data *new_lmd, *orig_lmd;
3688 orig_lmd = param->fp_lmd;
3689 new_lmd = lov_forge_comp_v1(orig_lmd, flags & LDF_IS_DIR);
3690 if (new_lmd != NULL) {
3691 param->fp_lmd = new_lmd;
3692 lov_dump_comp_v1(param, path, flags);
3693 param->fp_lmd = orig_lmd;
3699 if (magic == LOV_USER_MAGIC_V1) {
3700 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
3701 param->fp_lmd->lmd_lmm.lmm_objects,
3702 path, param->fp_obd_index,
3703 param->fp_max_depth, param->fp_verbose,
3706 char pool_name[LOV_MAXPOOLNAME + 1];
3707 struct lov_user_ost_data_v1 *objects;
3708 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
3710 snprintf(pool_name, sizeof(pool_name), "%s",
3711 lmmv3->lmm_pool_name);
3712 objects = lmmv3->lmm_objects;
3713 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
3714 objects, path, param->fp_obd_index,
3715 param->fp_max_depth, param->fp_verbose,
3720 static uint32_t check_foreign_type(uint32_t foreign_type)
3724 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
3725 if (lu_foreign_types[i].lft_name == NULL)
3727 if (foreign_type == lu_foreign_types[i].lft_type)
3731 return LU_FOREIGN_TYPE_UNKNOWN;
3734 static void lov_dump_foreign_lmm(struct find_param *param, char *path,
3735 enum lov_dump_flags flags)
3737 struct lov_foreign_md *lfm = (void *)¶m->fp_lmd->lmd_lmm;
3738 bool yaml = flags & LDF_YAML;
3740 if (!yaml && param->fp_depth && path)
3741 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3743 if (param->fp_verbose & VERBOSE_DETAIL) {
3744 uint32_t type = check_foreign_type(lfm->lfm_type);
3746 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3748 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3750 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3752 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3753 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3754 lu_foreign_types[type].lft_name);
3756 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3758 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3761 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3762 lfm->lfm_length, lfm->lfm_value);
3763 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3766 static void lmv_dump_foreign_lmm(struct find_param *param, char *path,
3767 enum lov_dump_flags flags)
3769 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)param->fp_lmv_md;
3770 bool yaml = flags & LDF_YAML;
3772 if (!yaml && param->fp_depth && path)
3773 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
3775 if (param->fp_verbose & VERBOSE_DETAIL) {
3776 uint32_t type = check_foreign_type(lfm->lfm_type);
3778 llapi_printf(LLAPI_MSG_NORMAL, "lfm_magic: 0x%08X\n",
3780 llapi_printf(LLAPI_MSG_NORMAL, "lfm_length: %u\n",
3782 llapi_printf(LLAPI_MSG_NORMAL, "lfm_type: 0x%08X",
3784 if (type < LU_FOREIGN_TYPE_UNKNOWN)
3785 llapi_printf(LLAPI_MSG_NORMAL, " (%s)\n",
3786 lu_foreign_types[type].lft_name);
3788 llapi_printf(LLAPI_MSG_NORMAL, " (unknown)\n");
3790 llapi_printf(LLAPI_MSG_NORMAL, "lfm_flags: 0x%08X\n",
3793 llapi_printf(LLAPI_MSG_NORMAL, "lfm_value: '%.*s'\n",
3794 lfm->lfm_length, lfm->lfm_value);
3795 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3798 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
3799 enum lov_dump_flags flags)
3803 if (param->fp_get_lmv || param->fp_get_default_lmv)
3804 magic = (__u32)param->fp_lmv_md->lum_magic;
3806 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
3809 flags |= LDF_IS_RAW;
3814 case LOV_USER_MAGIC_V1:
3815 case LOV_USER_MAGIC_V3:
3816 case LOV_USER_MAGIC_SPECIFIC:
3817 lov_dump_plain_user_lmm(param, path, flags);
3819 case LOV_USER_MAGIC_FOREIGN:
3820 lov_dump_foreign_lmm(param, path, flags);
3823 case LMV_USER_MAGIC: {
3824 char pool_name[LOV_MAXPOOLNAME + 1];
3825 struct lmv_user_md *lum;
3827 lum = (struct lmv_user_md *)param->fp_lmv_md;
3828 snprintf(pool_name, sizeof(pool_name), "%s",
3829 lum->lum_pool_name);
3830 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
3831 param->fp_max_depth, param->fp_verbose,
3835 case LOV_USER_MAGIC_COMP_V1:
3836 lov_dump_comp_v1(param, path, flags);
3838 case LMV_MAGIC_FOREIGN:
3839 lmv_dump_foreign_lmm(param, path, flags);
3842 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
3843 "(expecting one of %#x %#x %#x %#x)\n",
3844 *(__u32 *)¶m->fp_lmd->lmd_lmm,
3845 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
3846 LMV_USER_MAGIC, LMV_MAGIC_V1);
3851 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
3857 fname = strrchr(path, '/');
3859 /* It should be a file (or other non-directory) */
3860 if (fname == NULL) {
3861 dname = (char *)malloc(2);
3865 fname = (char *)path;
3867 dname = (char *)malloc(fname - path + 1);
3870 strncpy(dname, path, fname - path);
3871 dname[fname - path] = '\0';
3875 fd = open(dname, O_RDONLY | O_NONBLOCK);
3881 strcpy((char *)lum, fname);
3882 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3885 if (close(fd) == -1 && rc == 0)
3893 int llapi_file_lookup(int dirfd, const char *name)
3895 struct obd_ioctl_data data = { 0 };
3900 if (dirfd < 0 || name == NULL)
3903 data.ioc_version = OBD_IOCTL_VERSION;
3904 data.ioc_len = sizeof(data);
3905 data.ioc_inlbuf1 = (char *)name;
3906 data.ioc_inllen1 = strlen(name) + 1;
3908 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
3910 llapi_error(LLAPI_MSG_ERROR, rc,
3911 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3916 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3922 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3923 * Return -1 or 1 if file timestamp does not or does match the given criteria
3924 * correspondingly. Return 0 if the MDS time is being checked and there are
3925 * attributes on OSTs and it is not yet clear if the timespamp matches.
3927 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3928 * updated timestamps. */
3929 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
3934 /* Check if file is accepted. */
3935 if (param->fp_atime) {
3936 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
3937 param->fp_asign, param->fp_exclude_atime,
3938 param->fp_time_margin, mds);
3944 if (param->fp_mtime) {
3945 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
3946 param->fp_msign, param->fp_exclude_mtime,
3947 param->fp_time_margin, mds);
3951 /* If the previous check matches, but this one is not yet clear,
3952 * we should return 0 to do an RPC on OSTs. */
3957 if (param->fp_ctime) {
3958 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
3959 param->fp_csign, param->fp_exclude_ctime,
3960 param->fp_time_margin, mds);
3964 /* If the previous check matches, but this one is not yet clear,
3965 * we should return 0 to do an RPC on OSTs. */
3974 * Check whether the stripes matches the indexes user provided
3978 static int check_obd_match(struct find_param *param)
3980 struct lov_user_ost_data_v1 *objects;
3981 struct lov_comp_md_v1 *comp_v1 = NULL;
3982 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3983 lstat_t *st = ¶m->fp_lmd->lmd_st;
3984 int i, j, k, count = 1;
3986 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
3989 if (!S_ISREG(st->st_mode))
3992 /* exclude foreign */
3993 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
3994 return param->fp_exclude_obd;
3996 /* Only those files should be accepted, which have a
3997 * stripe on the specified OST. */
3998 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3999 comp_v1 = (struct lov_comp_md_v1 *)v1;
4000 count = comp_v1->lcm_entry_count;
4003 for (i = 0; i < count; i++) {
4005 v1 = lov_comp_entry(comp_v1, i);
4007 objects = lov_v1v3_objects(v1);
4009 for (j = 0; j < v1->lmm_stripe_count; j++) {
4010 if (comp_v1 && !(comp_v1->lcm_entries[i].lcme_flags &
4013 for (k = 0; k < param->fp_num_obds; k++) {
4014 if (param->fp_obd_indexes[k] ==
4015 objects[j].l_ost_idx)
4016 return !param->fp_exclude_obd;
4021 return param->fp_exclude_obd;
4024 static int check_mdt_match(struct find_param *param)
4028 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
4031 /* FIXME: For striped dir, we should get stripe information and check */
4032 for (i = 0; i < param->fp_num_mdts; i++) {
4033 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
4034 return !param->fp_exclude_mdt;
4037 if (param->fp_exclude_mdt)
4044 * Check whether the obd is active or not, if it is
4045 * not active, just print the object affected by this
4048 static int print_failed_tgt(struct find_param *param, char *path, int type)
4050 struct obd_statfs stat_buf;
4051 struct obd_uuid uuid_buf;
4054 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
4057 memset(&stat_buf, 0, sizeof(struct obd_statfs));
4058 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4059 ret = llapi_obd_statfs(path, type,
4060 param->fp_obd_index, &stat_buf,
4063 llapi_printf(LLAPI_MSG_NORMAL,
4064 "obd_uuid: %s failed %s ",
4065 param->fp_obd_uuid->uuid,
4072 static int find_check_stripe_size(struct find_param *param)
4074 struct lov_comp_md_v1 *comp_v1 = NULL;
4075 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4076 int ret, i, count = 1;
4078 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4079 return param->fp_exclude_stripe_size ? 1 : -1;
4081 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4082 comp_v1 = (struct lov_comp_md_v1 *)v1;
4083 count = comp_v1->lcm_entry_count;
4084 ret = param->fp_exclude_stripe_size ? 1 : -1;
4087 for (i = 0; i < count; i++) {
4089 v1 = lov_comp_entry(comp_v1, i);
4091 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_stripe_size,
4092 param->fp_stripe_size_sign,
4093 param->fp_exclude_stripe_size,
4094 param->fp_stripe_size_units, 0);
4095 /* If any stripe_size matches */
4103 static __u32 find_get_stripe_count(struct find_param *param)
4105 struct lov_comp_md_v1 *comp_v1 = NULL;
4106 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4108 __u32 stripe_count = 0;
4110 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4113 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4114 comp_v1 = (struct lov_comp_md_v1 *)v1;
4115 count = comp_v1->lcm_entry_count;
4118 for (i = 0; i < count; i++) {
4120 v1 = lov_comp_entry(comp_v1, i);
4121 stripe_count += v1->lmm_stripe_count;
4124 return stripe_count;
4127 #define LOV_PATTERN_INVALID 0xFFFFFFFF
4129 static int find_check_layout(struct find_param *param)
4131 struct lov_comp_md_v1 *comp_v1 = NULL;
4132 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4134 bool found = false, valid = false;
4136 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4137 comp_v1 = (struct lov_comp_md_v1 *)v1;
4138 count = comp_v1->lcm_entry_count;
4141 for (i = 0; i < count; i++) {
4143 v1 = lov_comp_entry(comp_v1, i);
4145 /* foreign file have a special magic but no pattern field */
4146 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4149 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
4153 if (v1->lmm_pattern & param->fp_layout) {
4162 if ((found && !param->fp_exclude_layout) ||
4163 (!found && param->fp_exclude_layout))
4169 /* if no type specified, check/exclude all foreign
4170 * if type specified, check all foreign&type and exclude !foreign + foreign&type
4172 static int find_check_foreign(struct find_param *param)
4174 if (S_ISREG(param->fp_lmd->lmd_st.st_mode)) {
4175 struct lov_foreign_md *lfm;
4177 lfm = (void *)¶m->fp_lmd->lmd_lmm;
4178 if (lfm->lfm_magic != LOV_USER_MAGIC_FOREIGN) {
4179 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4180 return param->fp_exclude_foreign ? 1 : -1;
4183 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4184 lfm->lfm_type == param->fp_foreign_type)
4185 return param->fp_exclude_foreign ? -1 : 1;
4186 return param->fp_exclude_foreign ? 1 : -1;
4190 if (S_ISDIR(param->fp_lmd->lmd_st.st_mode)) {
4191 struct lmv_foreign_md *lfm;
4193 lfm = (void *)param->fp_lmv_md;
4194 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
4195 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN)
4196 return param->fp_exclude_foreign ? 1 : -1;
4199 if (param->fp_foreign_type == LU_FOREIGN_TYPE_UNKNOWN ||
4200 lfm->lfm_type == param->fp_foreign_type)
4201 return param->fp_exclude_foreign ? -1 : 1;
4202 return param->fp_exclude_foreign ? 1 : -1;
4208 static int find_check_pool(struct find_param *param)
4210 struct lov_comp_md_v1 *comp_v1 = NULL;
4211 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4212 struct lov_user_md_v3 *v3 = (void *)v1;
4216 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4217 comp_v1 = (struct lov_comp_md_v1 *)v1;
4218 count = comp_v1->lcm_entry_count;
4219 /* empty requested pool is taken as no pool search */
4220 if (count == 0 && param->fp_poolname[0] == '\0')
4224 for (i = 0; i < count; i++) {
4225 if (comp_v1 != NULL)
4226 v1 = lov_comp_entry(comp_v1, i);
4228 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4231 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
4232 (param->fp_poolname[0] == '\0')) ||
4233 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4234 (strncmp(v3->lmm_pool_name,
4235 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
4236 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
4237 (strcmp(param->fp_poolname, "*") == 0))) {
4243 if ((found && !param->fp_exclude_pool) ||
4244 (!found && param->fp_exclude_pool))
4250 static int find_check_comp_options(struct find_param *param)
4252 lstat_t *st = ¶m->fp_lmd->lmd_st;
4253 struct lov_comp_md_v1 *comp_v1, *forged_v1 = NULL;
4254 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4255 struct lov_comp_md_entry_v1 *entry;
4258 if (v1->lmm_magic == LOV_USER_MAGIC_FOREIGN)
4261 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
4262 comp_v1 = (struct lov_comp_md_v1 *)v1;
4264 forged_v1 = malloc(sizeof(*forged_v1) + sizeof(*entry));
4265 if (forged_v1 == NULL)
4267 comp_v1 = forged_v1;
4268 comp_v1->lcm_entry_count = 1;
4269 entry = &comp_v1->lcm_entries[0];
4270 entry->lcme_flags = S_ISDIR(st->st_mode) ? 0 : LCME_FL_INIT;
4271 entry->lcme_extent.e_start = 0;
4272 entry->lcme_extent.e_end = LUSTRE_EOF;
4275 /* invalid case, don't match for any kind of search. */
4276 if (comp_v1->lcm_entry_count == 0) {
4281 if (param->fp_check_comp_count) {
4282 ret = find_value_cmp(forged_v1 ? 0 : comp_v1->lcm_entry_count,
4283 param->fp_comp_count,
4284 param->fp_comp_count_sign,
4285 param->fp_exclude_comp_count, 1, 0);
4291 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
4292 entry = &comp_v1->lcm_entries[i];
4294 if (param->fp_check_comp_flags) {
4296 if (((param->fp_comp_flags & entry->lcme_flags) !=
4297 param->fp_comp_flags) ||
4298 (param->fp_comp_neg_flags & entry->lcme_flags)) {
4304 if (param->fp_check_comp_start) {
4305 ret = find_value_cmp(entry->lcme_extent.e_start,
4306 param->fp_comp_start,
4307 param->fp_comp_start_sign,
4308 param->fp_exclude_comp_start,
4309 param->fp_comp_start_units, 0);
4314 if (param->fp_check_comp_end) {
4315 ret = find_comp_end_cmp(entry->lcme_extent.e_end,
4321 /* the component matches all criteria */
4330 static int find_check_mirror_options(struct find_param *param)
4332 struct lov_comp_md_v1 *comp_v1;
4333 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
4336 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1)
4339 comp_v1 = (struct lov_comp_md_v1 *)v1;
4341 if (param->fp_check_mirror_count) {
4342 ret = find_value_cmp(comp_v1->lcm_mirror_count + 1,
4343 param->fp_mirror_count,
4344 param->fp_mirror_count_sign,
4345 param->fp_exclude_mirror_count, 1, 0);
4350 if (param->fp_check_mirror_state) {
4352 __u16 file_state = comp_v1->lcm_flags & LCM_FL_FLR_MASK;
4354 if ((param->fp_mirror_state != 0 &&
4355 file_state != param->fp_mirror_state) ||
4356 file_state == param->fp_mirror_neg_state)
4363 static bool find_check_lmm_info(struct find_param *param)
4365 return param->fp_check_pool || param->fp_check_stripe_count ||
4366 param->fp_check_stripe_size || param->fp_check_layout ||
4367 param->fp_check_comp_count || param->fp_check_comp_end ||
4368 param->fp_check_comp_start || param->fp_check_comp_flags ||
4369 param->fp_check_mirror_count || param->fp_check_foreign ||
4370 param->fp_check_mirror_state ||
4371 param->fp_check_projid;
4375 * Get file/directory project id.
4376 * by the open fd resides on.
4377 * Return 0 and project id on success, or -ve errno.
4379 static int fget_projid(int fd, int *projid)
4384 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
4388 *projid = fsx.fsx_projid;
4392 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
4393 void *data, struct dirent64 *de)
4395 struct find_param *param = (struct find_param *)data;
4396 DIR *dir = dirp == NULL ? NULL : *dirp;
4397 int decision = 1; /* 1 is accepted; -1 is rejected. */
4398 lstat_t *st = ¶m->fp_lmd->lmd_st;
4400 int checked_type = 0;
4402 __u32 stripe_count = 0;
4405 if (parent == NULL && dir == NULL)
4408 /* If a regular expression is presented, make the initial decision */
4409 if (param->fp_pattern != NULL) {
4410 char *fname = strrchr(path, '/');
4411 fname = (fname == NULL ? path : fname + 1);
4412 ret = fnmatch(param->fp_pattern, fname, 0);
4413 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
4414 (ret == 0 && param->fp_exclude_pattern))
4418 /* See if we can check the file type from the dirent. */
4419 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
4422 if (DTTOIF(de->d_type) == param->fp_type) {
4423 if (param->fp_exclude_type)
4426 if (!param->fp_exclude_type)
4433 /* Request MDS for the stat info if some of these parameters need
4434 * to be compared. */
4435 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
4436 param->fp_check_uid || param->fp_check_gid ||
4437 param->fp_atime || param->fp_mtime || param->fp_ctime ||
4438 param->fp_check_size || param->fp_check_blocks ||
4439 find_check_lmm_info(param) ||
4440 param->fp_check_mdt_count || param->fp_check_hash_type)
4443 if (param->fp_type != 0 && checked_type == 0)
4446 if (decision == 0) {
4447 if (dir && (param->fp_check_mdt_count ||
4448 param->fp_check_hash_type || param->fp_check_foreign)) {
4449 param->fp_get_lmv = 1;
4450 ret = cb_get_dirstripe(path, dir, param);
4452 /* XXX this works to decide for foreign
4455 if (errno == ENODATA &&
4456 param->fp_check_foreign) {
4457 if (param->fp_exclude_foreign)
4465 param->fp_lmd->lmd_lmm.lmm_magic = 0;
4466 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
4467 param->fp_lum_size, GET_LMD_INFO);
4468 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
4469 find_check_lmm_info(param)) {
4470 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
4472 /* We need to "fake" the "use the default" values
4473 * since the lmm struct is zeroed out at this point. */
4474 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4475 lmm->lmm_pattern = LOV_PATTERN_DEFAULT;
4477 ostid_set_seq(&lmm->lmm_oi,
4478 FID_SEQ_LOV_DEFAULT);
4479 lmm->lmm_stripe_size = 0;
4480 lmm->lmm_stripe_count = 0;
4481 lmm->lmm_stripe_offset = -1;
4483 if (ret == 0 && param->fp_mdt_uuid != NULL) {
4485 ret = llapi_file_fget_mdtidx(dirfd(dir),
4486 ¶m->fp_file_mdt_index);
4487 } else if (S_ISREG(st->st_mode)) {
4488 /* FIXME: we could get the MDT index from the
4489 * file's FID in lmd->lmd_lmm.lmm_oi without
4490 * opening the file, once we are sure that
4491 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
4492 * That would still be an ioctl() to map the
4493 * FID to the MDT, but not an open RPC. */
4494 fd = open(path, O_RDONLY);
4496 ret = llapi_file_fget_mdtidx(fd,
4497 ¶m->fp_file_mdt_index);
4502 /* For a special file, we assume it resides on
4503 * the same MDT as the parent directory. */
4504 ret = llapi_file_fget_mdtidx(dirfd(parent),
4505 ¶m->fp_file_mdt_index);
4516 stripe_count = find_get_stripe_count(param);
4520 if (param->fp_type && !checked_type) {
4521 if ((st->st_mode & S_IFMT) == param->fp_type) {
4522 if (param->fp_exclude_type)
4525 if (!param->fp_exclude_type)
4531 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
4532 if (lustre_fs && param->fp_got_uuids &&
4533 param->fp_dev != st->st_dev) {
4534 /* A lustre/lustre mount point is crossed. */
4535 param->fp_got_uuids = 0;
4536 param->fp_obds_printed = 0;
4537 param->fp_mdt_index = OBD_NOT_FOUND;
4538 param->fp_obd_index = OBD_NOT_FOUND;
4541 if (lustre_fs && !param->fp_got_uuids) {
4542 ret = setup_target_indexes(dir ? dir : parent, path,
4547 param->fp_dev = st->st_dev;
4548 } else if (!lustre_fs && param->fp_got_uuids) {
4549 /* A lustre/non-lustre mount point is crossed. */
4550 param->fp_got_uuids = 0;
4551 param->fp_mdt_index = OBD_NOT_FOUND;
4552 param->fp_obd_index = OBD_NOT_FOUND;
4556 if (param->fp_check_foreign) {
4557 decision = find_check_foreign(param);
4562 if (param->fp_check_stripe_size) {
4563 decision = find_check_stripe_size(param);
4568 if (param->fp_check_stripe_count) {
4569 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
4570 param->fp_stripe_count_sign,
4571 param->fp_exclude_stripe_count, 1, 0);
4576 if (param->fp_check_mdt_count) {
4577 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4582 decision = find_value_cmp(
4583 param->fp_lmv_md->lum_stripe_count,
4584 param->fp_mdt_count,
4585 param->fp_mdt_count_sign,
4586 param->fp_exclude_mdt_count, 1, 0);
4591 if (param->fp_check_layout) {
4592 decision = find_check_layout(param);
4597 if (param->fp_check_hash_type) {
4600 if (param->fp_lmv_md->lum_magic == LMV_MAGIC_FOREIGN) {
4605 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
4606 if ((found && param->fp_exclude_hash_type) ||
4607 (!found && !param->fp_exclude_hash_type)) {
4613 /* If an OBD UUID is specified but none matches, skip this file. */
4614 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
4615 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
4618 /* If an OST or MDT UUID is given, and some OST matches,
4620 if (param->fp_obd_index != OBD_NOT_FOUND ||
4621 param->fp_mdt_index != OBD_NOT_FOUND) {
4622 if (param->fp_obd_uuid) {
4623 if (check_obd_match(param)) {
4624 /* If no mdtuuid is given, we are done.
4625 * Otherwise, fall through to the mdtuuid
4627 if (!param->fp_mdt_uuid)
4634 if (param->fp_mdt_uuid) {
4635 if (check_mdt_match(param))
4642 if (param->fp_check_uid) {
4643 if (st->st_uid == param->fp_uid) {
4644 if (param->fp_exclude_uid)
4647 if (!param->fp_exclude_uid)
4652 if (param->fp_check_gid) {
4653 if (st->st_gid == param->fp_gid) {
4654 if (param->fp_exclude_gid)
4657 if (!param->fp_exclude_gid)
4662 if (param->fp_check_projid) {
4666 fd = open(path, O_RDONLY);
4669 ret = fget_projid(fd, &projid);
4674 if (projid == param->fp_projid) {
4675 if (param->fp_exclude_projid)
4678 if (!param->fp_exclude_projid)
4683 if (param->fp_check_pool) {
4684 decision = find_check_pool(param);
4689 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
4690 param->fp_check_comp_start || param->fp_check_comp_end) {
4691 decision = find_check_comp_options(param);
4696 if (param->fp_check_mirror_count || param->fp_check_mirror_state) {
4697 decision = find_check_mirror_options(param);
4702 /* Check the time on mds. */
4704 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
4707 for_mds = lustre_fs ?
4708 (S_ISREG(st->st_mode) && stripe_count) : 0;
4709 decision = find_time_check(st, param, for_mds);
4714 /* If file still fits the request, ask ost for updated info.
4715 The regular stat is almost of the same speed as some new
4716 'glimpse-size-ioctl'. */
4718 if ((param->fp_check_size || param->fp_check_blocks) &&
4719 ((S_ISREG(st->st_mode) && stripe_count) || S_ISDIR(st->st_mode)))
4723 /* For regular files with the stripe the decision may have not
4724 * been taken yet if *time or size is to be checked. */
4725 if (param->fp_obd_index != OBD_NOT_FOUND)
4726 print_failed_tgt(param, path, LL_STATFS_LOV);
4728 if (param->fp_mdt_index != OBD_NOT_FOUND)
4729 print_failed_tgt(param, path, LL_STATFS_LMV);
4732 ret = fstat_f(dirfd(dir), st);
4733 else if (de != NULL)
4734 ret = fstatat_f(dirfd(parent), de->d_name, st,
4735 AT_SYMLINK_NOFOLLOW);
4737 ret = lstat_f(path, st);
4740 if (errno == ENOENT) {
4741 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
4742 "warning: %s: %s does not exist",
4747 llapi_error(LLAPI_MSG_ERROR, ret,
4748 "%s: IOC_LOV_GETINFO on %s failed",
4754 /* Check the time on osc. */
4755 decision = find_time_check(st, param, 0);
4760 if (param->fp_check_size) {
4761 decision = find_value_cmp(st->st_size, param->fp_size,
4762 param->fp_size_sign,
4763 param->fp_exclude_size,
4764 param->fp_size_units, 0);
4769 if (param->fp_check_blocks) { /* convert st_blocks to bytes */
4770 decision = find_value_cmp(st->st_blocks * 512, param->fp_blocks,
4771 param->fp_blocks_sign,
4772 param->fp_exclude_blocks,
4773 param->fp_blocks_units, 0);
4779 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
4780 if (param->fp_zero_end)
4781 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
4783 llapi_printf(LLAPI_MSG_NORMAL, "\n");
4787 /* Do not get down anymore? */
4788 if (param->fp_depth == param->fp_max_depth) {
4799 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
4800 void *param_data, struct dirent64 *de)
4802 struct find_param *param = (struct find_param *)param_data;
4803 struct lmv_user_md *lmu = param->fp_lmv_md;
4804 DIR *tmp_parent = parent;
4805 char raw[MAX_IOC_BUFLEN] = {'\0'};
4807 struct obd_ioctl_data data = { 0 };
4814 if (parent == NULL && dirp == NULL)
4823 if (parent == NULL) {
4824 tmp_parent = opendir_parent(path);
4825 if (tmp_parent == NULL) {
4828 llapi_error(LLAPI_MSG_ERROR, ret,
4829 "can not open %s", path);
4834 fd = dirfd(tmp_parent);
4836 path_copy = strdup(path);
4837 filename = basename(path_copy);
4839 data.ioc_inlbuf1 = (char *)filename;
4840 data.ioc_inllen1 = strlen(filename) + 1;
4841 data.ioc_inlbuf2 = (char *)lmu;
4842 data.ioc_inllen2 = lmv_user_md_size(lmu->lum_stripe_count,
4844 ret = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
4846 llapi_error(LLAPI_MSG_ERROR, ret,
4847 "llapi_obd_statfs: error packing ioctl data");
4852 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
4854 if (errno == EBUSY && !retry) {
4855 /* because migrate may not be able to lock all involved
4856 * objects in order, for some of them it try lock, while
4857 * there may be conflicting COS locks and cause migrate
4858 * fail with EBUSY, hope a sync() could cause
4859 * transaction commit and release these COS locks. */
4863 } else if (errno == EALREADY) {
4864 if (param->fp_verbose & VERBOSE_DETAIL)
4865 llapi_printf(LLAPI_MSG_NORMAL,
4866 "%s was migrated to MDT%d already\n",
4867 path, lmu->lum_stripe_offset);
4871 llapi_error(LLAPI_MSG_ERROR, ret,
4872 "%s migrate failed: %s (%d)\n",
4873 path, strerror(-ret), ret);
4876 } else if (param->fp_verbose & VERBOSE_DETAIL) {
4877 llapi_printf(LLAPI_MSG_NORMAL,
4878 "migrate %s to MDT%d stripe count %d\n",
4879 path, lmu->lum_stripe_offset,
4880 lmu->lum_stripe_count);
4885 /* If the directory is being migration, we need
4886 * close the directory after migration,
4887 * so the old directory cache will be cleanup
4888 * on the client side, and re-open to get the
4889 * new directory handle */
4890 *dirp = opendir(path);
4891 if (*dirp == NULL) {
4893 llapi_error(LLAPI_MSG_ERROR, ret,
4894 "%s: Failed to open '%s'", __func__, path);
4899 closedir(tmp_parent);
4906 /* dir migration finished, shrink its stripes */
4907 static int cb_migrate_mdt_fini(char *path, DIR *parent, DIR **dirp, void *data,
4908 struct dirent64 *de)
4910 struct find_param *param = data;
4911 struct lmv_user_md *lmu = param->fp_lmv_md;
4912 int lmulen = lmv_user_md_size(lmu->lum_stripe_count, lmu->lum_magic);
4915 if (de && de->d_type != DT_DIR)
4920 * close it before setxattr because the latter may destroy the
4921 * original object, and cause close fail.
4923 ret = closedir(*dirp);
4929 ret = setxattr(path, XATTR_NAME_LMV, lmu, lmulen, 0);
4930 if (ret == -EALREADY)
4933 cb_common_fini(path, parent, dirp, data, de);
4937 int llapi_migrate_mdt(char *path, struct find_param *param)
4939 return param_callback(path, cb_migrate_mdt_init, cb_migrate_mdt_fini,
4943 int llapi_mv(char *path, struct find_param *param)
4945 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
4946 static bool printed;
4949 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
4950 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
4954 return llapi_migrate_mdt(path, param);
4957 int llapi_find(char *path, struct find_param *param)
4959 return param_callback(path, cb_find_init, cb_common_fini, param);
4963 * Get MDT number that the file/directory inode referenced
4964 * by the open fd resides on.
4965 * Return 0 and mdtidx on success, or -ve errno.
4967 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
4969 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
4974 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
4975 struct dirent64 *de)
4977 struct find_param *param = (struct find_param *)data;
4978 DIR *d = dirp == NULL ? NULL : *dirp;
4982 if (parent == NULL && d == NULL)
4986 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
4987 } else /* if (parent) */ {
4990 fd = open(path, O_RDONLY | O_NOCTTY);
4992 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
5000 if (ret == -ENODATA) {
5001 if (!param->fp_obd_uuid)
5002 llapi_printf(LLAPI_MSG_NORMAL,
5003 "'%s' has no stripe info\n", path);
5005 } else if (ret == -ENOENT) {
5006 llapi_error(LLAPI_MSG_WARN, ret,
5007 "warning: %s: '%s' does not exist",
5010 } else if (ret == -ENOTTY) {
5011 llapi_error(LLAPI_MSG_ERROR, ret,
5012 "%s: '%s' not on a Lustre fs",
5015 llapi_error(LLAPI_MSG_ERROR, ret,
5016 "error: %s: '%s' failed get_mdtidx",
5022 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
5023 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
5025 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
5029 /* Do not go down anymore? */
5030 if (param->fp_depth == param->fp_max_depth)
5038 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
5039 struct dirent64 *de)
5041 struct find_param *param = (struct find_param *)data;
5042 DIR *d = dirp == NULL ? NULL : *dirp;
5045 if (parent == NULL && d == NULL)
5048 if (param->fp_obd_uuid) {
5049 param->fp_quiet = 1;
5050 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
5055 if (d && (param->fp_get_lmv || param->fp_get_default_lmv))
5056 ret = cb_get_dirstripe(path, d, param);
5058 (parent && !param->fp_get_lmv && !param->fp_get_default_lmv))
5059 ret = get_lmd_info(path, parent, d, ¶m->fp_lmd->lmd_lmm,
5060 param->fp_lum_size, GET_LMD_STRIPE);
5065 if (errno == ENODATA && d != NULL) {
5066 /* We need to "fake" the "use the default" values
5067 * since the lmm struct is zeroed out at this point.
5068 * The magic needs to be set in order to satisfy
5069 * a check later on in the code path.
5070 * The object_seq needs to be set for the "(Default)"
5071 * prefix to be displayed. */
5072 if (param->fp_get_default_lmv) {
5073 struct lmv_user_md *lum = param->fp_lmv_md;
5075 lum->lum_magic = LMV_USER_MAGIC;
5076 lum->lum_stripe_count = 0;
5077 lum->lum_stripe_offset = -1;
5079 } else if (param->fp_get_lmv) {
5080 struct lmv_user_md *lum = param->fp_lmv_md;
5083 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
5086 lum->lum_magic = LMV_MAGIC_V1;
5087 lum->lum_stripe_count = 0;
5088 lum->lum_stripe_offset = mdtidx;
5091 struct lov_user_md *lmm =
5092 ¶m->fp_lmd->lmd_lmm;
5094 lmm->lmm_magic = LOV_USER_MAGIC_V1;
5096 ostid_set_seq(&lmm->lmm_oi,
5097 FID_SEQ_LOV_DEFAULT);
5098 lmm->lmm_stripe_count = 0;
5099 lmm->lmm_stripe_size = 0;
5100 lmm->lmm_stripe_offset = -1;
5103 } else if (errno == ENODATA && parent != NULL) {
5104 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
5105 llapi_printf(LLAPI_MSG_NORMAL,
5106 "%s has no stripe info\n", path);
5108 } else if (errno == ENOENT) {
5109 llapi_error(LLAPI_MSG_WARN, -ENOENT,
5110 "warning: %s: %s does not exist",
5113 } else if (errno == ENOTTY) {
5115 llapi_error(LLAPI_MSG_ERROR, ret,
5116 "%s: '%s' not on a Lustre fs?",
5121 llapi_error(LLAPI_MSG_ERROR, ret,
5122 "error: %s: %s failed for %s",
5123 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
5124 "IOC_MDC_GETFILESTRIPE", path);
5131 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
5132 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
5135 /* Do not get down anymore? */
5136 if (param->fp_depth == param->fp_max_depth)
5144 int llapi_getstripe(char *path, struct find_param *param)
5146 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
5147 cb_get_mdt_index : cb_getstripe,
5148 cb_common_fini, param);
5151 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
5152 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5154 char raw[MAX_IOC_BUFLEN] = {'\0'};
5156 struct obd_ioctl_data data = { 0 };
5159 data.ioc_inlbuf1 = (char *)&type;
5160 data.ioc_inllen1 = sizeof(__u32);
5161 data.ioc_inlbuf2 = (char *)&index;
5162 data.ioc_inllen2 = sizeof(__u32);
5163 data.ioc_pbuf1 = (char *)stat_buf;
5164 data.ioc_plen1 = sizeof(struct obd_statfs);
5165 data.ioc_pbuf2 = (char *)uuid_buf;
5166 data.ioc_plen2 = sizeof(struct obd_uuid);
5168 rc = llapi_ioctl_pack(&data, &rawbuf, sizeof(raw));
5170 llapi_error(LLAPI_MSG_ERROR, rc,
5171 "llapi_obd_statfs: error packing ioctl data");
5175 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
5177 return rc < 0 ? -errno : 0;
5180 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
5181 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
5186 fd = open(path, O_RDONLY);
5189 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
5191 /* If we can't even open a file on the filesystem (e.g. with
5192 * -ESHUTDOWN), force caller to exit or it will loop forever. */
5196 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
5203 #define MAX_STRING_SIZE 128
5205 int llapi_ping(char *obd_type, char *obd_name)
5207 int flags = O_RDONLY;
5208 char buf[1] = { 0 };
5212 rc = cfs_get_param_paths(&path, "%s/%s/ping",
5213 obd_type, obd_name);
5217 fd = open(path.gl_pathv[0], flags);
5219 if (errno == EACCES && flags == O_RDONLY) {
5224 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
5229 if (flags == O_RDONLY)
5230 rc = read(fd, buf, sizeof(buf));
5232 rc = write(fd, buf, sizeof(buf));
5240 cfs_free_param_data(&path);
5244 int llapi_target_iterate(int type_num, char **obd_type,
5245 void *args, llapi_cb_t cb)
5251 for (i = 0; i < type_num; i++) {
5254 rc = cfs_get_param_paths(¶m, "%s/*/uuid", obd_type[i]);
5258 for (j = 0; j < param.gl_pathc; j++) {
5259 char obd_uuid[UUID_MAX + 1];
5263 fp = fopen(param.gl_pathv[j], "r");
5266 llapi_error(LLAPI_MSG_ERROR, rc,
5267 "error: opening '%s'",
5272 if (fgets(obd_uuid, sizeof(obd_uuid), fp) == NULL) {
5274 llapi_error(LLAPI_MSG_ERROR, rc,
5275 "error: reading '%s'",
5280 /* Extract the obd_name from the sysfs path.
5281 * 'topsysfs'/fs/lustre/'obd_type'/'obd_name'.
5283 obd_name = strstr(param.gl_pathv[j], "/fs/lustre/");
5289 /* skip /fs/lustre/'obd_type'/ */
5290 obd_name += strlen(obd_type[i]) + 12;
5291 /* chop off after obd_name */
5292 ptr = strrchr(obd_name, '/');
5296 cb(obd_type[i], obd_name, obd_uuid, args);
5305 cfs_free_param_data(¶m);
5309 static void do_target_check(char *obd_type_name, char *obd_name,
5310 char *obd_uuid, void *args)
5314 rc = llapi_ping(obd_type_name, obd_name);
5315 if (rc == ENOTCONN) {
5316 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
5318 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
5320 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
5324 int llapi_target_check(int type_num, char **obd_type, char *dir)
5326 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
5329 #undef MAX_STRING_SIZE
5331 /* Is this a lustre fs? */
5332 int llapi_is_lustre_mnttype(const char *type)
5334 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
5337 /* Is this a lustre client fs? */
5338 int llapi_is_lustre_mnt(struct mntent *mnt)
5340 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
5341 strstr(mnt->mnt_fsname, ":/") != NULL);
5344 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
5346 char fsname[PATH_MAX + 1];
5350 rc = llapi_search_fsname(mnt, fsname);
5354 root = open(mnt, O_RDONLY | O_DIRECTORY);
5357 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", mnt);
5361 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
5369 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
5370 * format must have %s%s, buf must be > 16
5371 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
5372 * then buf = "lustre-MDT0000"
5374 static int get_mdtname(char *name, char *format, char *buf)
5376 char suffix[]="-MDT0000";
5377 int len = strlen(name);
5379 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
5380 name[len - 5] = '\0';
5385 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
5388 /* Not enough room to add suffix */
5389 llapi_err_noerrno(LLAPI_MSG_ERROR,
5390 "Invalid MDT name |%s|", name);
5395 return sprintf(buf, format, name, suffix);
5398 /** ioctl on filsystem root, with mdtindex sent as data
5399 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
5400 * \param mdtidxp pointer to integer within data to be filled in with the
5401 * mdt index (0 if no mdt is specified). NULL won't be filled.
5403 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
5411 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
5412 Open root and parse mdt index. */
5413 if (mdtname[0] == '/') {
5415 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
5416 (char *)mdtname, -1);
5418 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
5420 ptr = fsname + strlen(fsname) - 8;
5422 index = strtol(ptr + 4, NULL, 16);
5423 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
5427 llapi_err_noerrno(LLAPI_MSG_ERROR,
5428 "Can't open %s: %d\n", mdtname, rc);
5435 rc = ioctl(fd, opc, data);
5444 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
5445 int buflen, long long *recno, int *linkno)
5447 const char *fidstr_orig = fidstr;
5449 struct getinfo_fid2path *gf;
5454 while (*fidstr == '[')
5457 sscanf(fidstr, SFID, RFID(&fid));
5458 if (!fid_is_sane(&fid)) {
5459 llapi_err_noerrno(LLAPI_MSG_ERROR,
5460 "bad FID format '%s', should be [seq:oid:ver]"
5461 " (e.g. "DFID")\n", fidstr_orig,
5462 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
5466 gf = malloc(sizeof(*gf) + buflen);
5471 gf->gf_recno = *recno;
5472 gf->gf_linkno = *linkno;
5473 gf->gf_pathlen = buflen;
5475 /* Take path or fsname */
5476 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
5481 /* strip out instances of // */
5482 for (a = gf->gf_u.gf_path; *a != '\0'; a++) {
5483 if ((*a == '/') && (*(a + 1) == '/'))
5490 if (buf[0] == '\0') { /* ROOT path */
5495 *recno = gf->gf_recno;
5496 *linkno = gf->gf_linkno;
5503 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
5506 struct lustre_mdt_attrs *lma;
5510 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
5512 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
5515 lma = (struct lustre_mdt_attrs *)buf;
5516 fid_le_to_cpu(fid, &lma->lma_self_fid);
5520 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
5525 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
5534 int llapi_fd2fid(int fd, struct lu_fid *fid)
5538 memset(fid, 0, sizeof(*fid));
5540 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
5541 if (rc == -EINVAL || rc == -ENOTTY)
5542 rc = fid_from_lma(NULL, fd, fid);
5547 int llapi_path2fid(const char *path, struct lu_fid *fid)
5551 memset(fid, 0, sizeof(*fid));
5552 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5554 if (errno == ELOOP || errno == ENXIO)
5555 return fid_from_lma(path, -1, fid);
5559 rc = llapi_fd2fid(fd, fid);
5560 if (rc == -EINVAL || rc == -ENOTTY)
5561 rc = fid_from_lma(path, -1, fid);
5567 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
5568 char *name, size_t name_size)
5570 struct getparent *gp;
5573 gp = malloc(sizeof(*gp) + name_size);
5577 gp->gp_linkno = linkno;
5578 gp->gp_name_size = name_size;
5580 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
5586 *parent_fid = gp->gp_fid;
5588 strncpy(name, gp->gp_name, name_size);
5589 name[name_size - 1] = '\0';
5596 int llapi_path2parent(const char *path, unsigned int linkno,
5597 struct lu_fid *parent_fid, char *name, size_t name_size)
5602 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
5606 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
5611 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
5616 root = open(mnt, O_RDONLY | O_DIRECTORY);
5619 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
5623 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
5626 llapi_error(LLAPI_MSG_ERROR, rc,
5627 "ioctl on %s for getting connect flags failed", mnt);
5634 * Get a 64-bit value representing the version of file data pointed by fd.
5636 * Each write or truncate, flushed on OST, will change this value. You can use
5637 * this value to verify if file data was modified. This only checks the file
5638 * data, not metadata.
5640 * \param flags 0: no flush pages, usually used it the process has already
5642 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
5644 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
5645 * pages from clients.
5647 * \retval 0 on success.
5648 * \retval -errno on error.
5650 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
5653 struct ioc_data_version idv;
5655 idv.idv_flags = (__u32)flags;
5657 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5661 *data_version = idv.idv_version;
5667 * Flush cached pages from all clients.
5669 * \param fd File descriptor
5673 int llapi_file_flush(int fd)
5677 return llapi_get_data_version(fd, &dv, LL_DV_WR_FLUSH);
5681 * Fetch layout version from OST objects. Layout version on OST objects are
5682 * only set when the file is a mirrored file AND after the file has been
5683 * written at least once.
5685 * It actually fetches the least layout version from the objects.
5687 int llapi_get_ost_layout_version(int fd, __u32 *layout_version)
5690 struct ioc_data_version idv = { 0 };
5692 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
5696 *layout_version = idv.idv_layout_version;
5702 * Create a file without any name and open it for read/write
5704 * - file is created as if it were a standard file in the given \a directory
5705 * - file does not appear in \a directory and mtime does not change because
5706 * the filename is handled specially by the Lustre MDS.
5707 * - file is destroyed at final close
5709 * \param[in] directory directory from which to inherit layout/MDT idx
5710 * \param[in] mdt_idx MDT index on which the file is created,
5711 * \a idx == -1 means no specific MDT is requested
5712 * \param[in] mode standard open(2) mode
5713 * \param[in] stripe_param stripe parameters. May be NULL.
5715 * \retval a file descriptor on success.
5716 * \retval -errno on error.
5718 int llapi_create_volatile_param(const char *directory, int mdt_idx,
5719 int open_flags, mode_t mode,
5720 const struct llapi_stripe_param *stripe_param)
5722 char file_path[PATH_MAX];
5723 int saved_errno = errno;
5725 unsigned int rnumber;
5731 rc = snprintf(file_path, sizeof(file_path),
5732 "%s/" LUSTRE_VOLATILE_HDR "::%.4X",
5733 directory, rnumber);
5735 rc = snprintf(file_path, sizeof(file_path),
5736 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X",
5737 directory, mdt_idx, rnumber);
5739 if (rc < 0 || rc >= sizeof(file_path))
5740 return -ENAMETOOLONG;
5743 * Either open O_WRONLY or O_RDWR, creating RDONLY
5744 * is non-sensical here
5746 if ((open_flags & O_ACCMODE) == O_RDONLY)
5747 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
5749 open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
5751 if (stripe_param != NULL) {
5752 fd = llapi_file_open_param(file_path, open_flags,
5753 mode, stripe_param);
5757 fd = open(file_path, open_flags, mode);
5761 } while (fd < 0 && rc == -EEXIST);
5764 llapi_error(LLAPI_MSG_ERROR, rc,
5765 "Cannot create volatile file '%s' in '%s'",
5766 file_path + strlen(directory) + 1 +
5767 LUSTRE_VOLATILE_HDR_LEN,
5773 * Unlink file in case this wasn't a Lustre filesystem and the magic
5774 * volatile filename wasn't handled as intended. The effect is the
5775 * same. If volatile open was supported then we expect unlink() to
5778 (void)unlink(file_path);
5781 * Since we are returning successfully we restore errno (and
5782 * mask out possible EEXIST from open() and ENOENT from unlink().
5784 errno = saved_errno;
5790 * Create a file without any name open it for read/write
5792 * - file is created as if it were a standard file in the given \a directory
5793 * - file does not appear in \a directory and mtime does not change because
5794 * the filename is handled specially by the Lustre MDS.
5795 * - file is removed at final close
5796 * - file modes are rw------- since it doesn't make sense to have a read-only
5797 * or write-only file that cannot be opened again.
5798 * - if user wants another mode it must use fchmod() on the open file, no
5799 * security problems arise because it cannot be opened by another process.
5801 * \param[in] directory directory from which to inherit layout/MDT idx
5802 * \param[in] idx MDT index on which the file is created,
5803 * \a idx == -1 means no specific MDT is requested
5804 * \param[in] open_flags standard open(2) flags
5806 * \retval a file descriptor on success.
5807 * \retval -errno on error.
5809 int llapi_create_volatile_idx(const char *directory, int mdt_idx,
5812 return llapi_create_volatile_param(directory, mdt_idx, open_flags,
5813 S_IRUSR | S_IWUSR, NULL);
5817 * Swap the layouts between 2 file descriptors
5818 * the 2 files must be open for writing
5819 * first fd received the ioctl, second fd is passed as arg
5820 * this is assymetric but avoid use of root path for ioctl
5822 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
5823 int gid, __u64 flags)
5825 struct lustre_swap_layouts lsl;
5830 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5831 rc = fstat(fd1, &st1);
5835 rc = fstat(fd2, &st2);
5840 lsl.sl_flags = flags;
5844 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
5848 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
5849 struct timeval tv1[2];
5850 struct timeval tv2[2];
5852 memset(tv1, 0, sizeof(tv1));
5853 memset(tv2, 0, sizeof(tv2));
5855 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
5856 tv1[0].tv_sec = st1.st_atime;
5857 tv2[0].tv_sec = st2.st_atime;
5859 tv1[0].tv_sec = st2.st_atime;
5860 tv2[0].tv_sec = st1.st_atime;
5863 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
5864 tv1[1].tv_sec = st1.st_mtime;
5865 tv2[1].tv_sec = st2.st_mtime;
5867 tv1[1].tv_sec = st2.st_mtime;
5868 tv2[1].tv_sec = st1.st_mtime;
5871 rc = futimes(fd1, tv1);
5875 rc = futimes(fd2, tv2);
5883 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
5890 while (grp_id == 0);
5892 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
5900 * Swap the layouts between 2 files
5901 * the 2 files are open in write
5903 int llapi_swap_layouts(const char *path1, const char *path2,
5904 __u64 dv1, __u64 dv2, __u64 flags)
5908 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
5911 llapi_error(LLAPI_MSG_ERROR, rc,
5912 "error: cannot open '%s' for write", path1);
5916 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
5919 llapi_error(LLAPI_MSG_ERROR, rc,
5920 "error: cannot open '%s' for write", path2);
5924 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
5926 llapi_error(LLAPI_MSG_ERROR, rc,
5927 "error: cannot swap layout between '%s' and '%s'",
5938 * Attempt to open a file with Lustre file identifier \a fid
5939 * and return an open file descriptor.
5941 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
5942 * \param[in] fid Lustre file identifier of file to open
5943 * \param[in] flags open() flags
5945 * \retval non-negative file descriptor on successful open
5946 * \retval -1 if an error occurred
5948 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
5951 char mntdir[PATH_MAX];
5952 char path[PATH_MAX];
5955 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
5959 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
5960 return open(path, flags);
5966 * \param fd File to lock.
5967 * \param gid Group Identifier.
5969 * \retval 0 on success.
5970 * \retval -errno on failure.
5972 int llapi_group_lock(int fd, int gid)
5976 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
5979 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
5987 * \param fd File to unlock.
5988 * \param gid Group Identifier.
5990 * \retval 0 on success.
5991 * \retval -errno on failure.
5993 int llapi_group_unlock(int fd, int gid)
5997 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
6000 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");