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, 2016, 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>
49 #include <sys/ioctl.h>
56 #include <sys/statfs.h>
57 #include <sys/syscall.h>
59 #include <sys/types.h>
60 #include <sys/xattr.h>
63 #include <libgen.h> /* for dirname() */
64 #ifdef HAVE_LINUX_UNISTD_H
65 #include <linux/unistd.h>
71 #include <libcfs/util/ioctl.h>
72 #include <libcfs/util/param.h>
73 #include <libcfs/util/string.h>
74 #include <lnet/lnetctl.h>
75 #include <lustre/lustreapi.h>
76 #include <linux/lustre_ostid.h>
77 #include <linux/lustre_ioctl.h>
78 #include "lustreapi_internal.h"
80 static int llapi_msg_level = LLAPI_MSG_MAX;
82 char *mdt_hash_name[] = { "none",
83 LMV_HASH_NAME_ALL_CHARS,
84 LMV_HASH_NAME_FNV_1A_64 };
86 void llapi_msg_set_level(int level)
88 /* ensure level is in the good range */
89 if (level < LLAPI_MSG_OFF)
90 llapi_msg_level = LLAPI_MSG_OFF;
91 else if (level > LLAPI_MSG_MAX)
92 llapi_msg_level = LLAPI_MSG_MAX;
94 llapi_msg_level = level;
97 int llapi_msg_get_level(void)
99 return llapi_msg_level;
102 static void error_callback_default(enum llapi_message_level level, int err,
103 const char *fmt, va_list ap)
105 vfprintf(stderr, fmt, ap);
106 if (level & LLAPI_MSG_NO_ERRNO)
107 fprintf(stderr, "\n");
109 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
112 static void info_callback_default(enum llapi_message_level level, int err,
113 const char *fmt, va_list ap)
115 vfprintf(stdout, fmt, ap);
118 static llapi_log_callback_t llapi_error_callback = error_callback_default;
119 static llapi_log_callback_t llapi_info_callback = info_callback_default;
122 /* llapi_error will preserve errno */
123 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
126 int tmp_errno = errno;
128 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
132 llapi_error_callback(level, abs(err), fmt, args);
137 /* llapi_printf will preserve errno */
138 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
141 int tmp_errno = errno;
143 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
147 llapi_info_callback(level, 0, fmt, args);
153 * Set a custom error logging function. Passing in NULL will reset the logging
154 * callback to its default value.
156 * This function returns the value of the old callback.
158 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
160 llapi_log_callback_t old = llapi_error_callback;
163 llapi_error_callback = cb;
165 llapi_error_callback = error_callback_default;
171 * Set a custom info logging function. Passing in NULL will reset the logging
172 * callback to its default value.
174 * This function returns the value of the old callback.
176 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
178 llapi_log_callback_t old = llapi_info_callback;
181 llapi_info_callback = cb;
183 llapi_info_callback = info_callback_default;
189 * size_units is to be initialized (or zeroed) by caller.
191 int llapi_parse_size(const char *optarg, unsigned long long *size,
192 unsigned long long *size_units, int bytes_spec)
195 char *argbuf = (char *)optarg;
196 unsigned long long frac = 0, frac_d = 1;
198 if (strncmp(optarg, "-", 1) == 0)
201 if (*size_units == 0)
204 *size = strtoull(argbuf, &end, 0);
205 if (end != NULL && *end == '.') {
209 frac = strtoull(argbuf, &end, 10);
210 /* count decimal places */
211 for (i = 0; i < (end - argbuf); i++)
216 if ((*end == 'b') && *(end + 1) == '\0' &&
217 (*size & (~0ULL << (64 - 9))) == 0 &&
219 *size_units = 1 << 9;
220 } else if ((*end == 'b') &&
221 *(end + 1) == '\0' &&
224 } else if ((*end == 'k' || *end == 'K') &&
225 *(end + 1) == '\0' &&
226 (*size & (~0ULL << (64 - 10))) == 0) {
227 *size_units = 1 << 10;
228 } else if ((*end == 'm' || *end == 'M') &&
229 *(end + 1) == '\0' &&
230 (*size & (~0ULL << (64 - 20))) == 0) {
231 *size_units = 1 << 20;
232 } else if ((*end == 'g' || *end == 'G') &&
233 *(end + 1) == '\0' &&
234 (*size & (~0ULL << (64 - 30))) == 0) {
235 *size_units = 1 << 30;
236 } else if ((*end == 't' || *end == 'T') &&
237 *(end + 1) == '\0' &&
238 (*size & (~0ULL << (64 - 40))) == 0) {
239 *size_units = 1ULL << 40;
240 } else if ((*end == 'p' || *end == 'P') &&
241 *(end + 1) == '\0' &&
242 (*size & (~0ULL << (64 - 50))) == 0) {
243 *size_units = 1ULL << 50;
244 } else if ((*end == 'e' || *end == 'E') &&
245 *(end + 1) == '\0' &&
246 (*size & (~0ULL << (64 - 60))) == 0) {
247 *size_units = 1ULL << 60;
252 *size = *size * *size_units + frac * *size_units / frac_d;
257 int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
259 struct obd_ioctl_data *overlay;
262 data->ioc_len = obd_ioctl_packlen(data);
263 data->ioc_version = OBD_IOCTL_VERSION;
265 if (*pbuf != NULL && data->ioc_len > max_len) {
266 fprintf(stderr, "pbuf = %p, ioc_len = %u, max_len = %d\n",
267 *pbuf, data->ioc_len, max_len);
272 *pbuf = malloc(data->ioc_len);
277 overlay = (struct obd_ioctl_data *)*pbuf;
278 memcpy(*pbuf, data, sizeof(*data));
280 ptr = overlay->ioc_bulk;
281 if (data->ioc_inlbuf1) {
282 memcpy(ptr, data->ioc_inlbuf1, data->ioc_inllen1);
283 ptr += cfs_size_round(data->ioc_inllen1);
286 if (data->ioc_inlbuf2) {
287 memcpy(ptr, data->ioc_inlbuf2, data->ioc_inllen2);
288 ptr += cfs_size_round(data->ioc_inllen2);
291 if (data->ioc_inlbuf3) {
292 memcpy(ptr, data->ioc_inlbuf3, data->ioc_inllen3);
293 ptr += cfs_size_round(data->ioc_inllen3);
296 if (data->ioc_inlbuf4) {
297 memcpy(ptr, data->ioc_inlbuf4, data->ioc_inllen4);
298 ptr += cfs_size_round(data->ioc_inllen4);
304 int obd_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf, int max_len)
306 struct obd_ioctl_data *overlay;
312 overlay = (struct obd_ioctl_data *)pbuf;
314 /* Preserve the caller's buffer pointers */
315 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
316 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
317 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
318 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
320 memcpy(data, pbuf, sizeof(*data));
322 ptr = overlay->ioc_bulk;
323 if (data->ioc_inlbuf1) {
324 memcpy(data->ioc_inlbuf1, ptr, data->ioc_inllen1);
325 ptr += cfs_size_round(data->ioc_inllen1);
328 if (data->ioc_inlbuf2) {
329 memcpy(data->ioc_inlbuf2, ptr, data->ioc_inllen2);
330 ptr += cfs_size_round(data->ioc_inllen2);
333 if (data->ioc_inlbuf3) {
334 memcpy(data->ioc_inlbuf3, ptr, data->ioc_inllen3);
335 ptr += cfs_size_round(data->ioc_inllen3);
338 if (data->ioc_inlbuf4) {
339 memcpy(data->ioc_inlbuf4, ptr, data->ioc_inllen4);
340 ptr += cfs_size_round(data->ioc_inllen4);
346 /* XXX: llapi_xxx() functions return negative values upon failure */
348 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
349 int stripe_count, int stripe_pattern)
353 /* 64 KB is the largest common page size I'm aware of (on ia64), but
354 * check the local page size just in case. */
355 page_size = LOV_MIN_STRIPE_SIZE;
356 if (getpagesize() > page_size) {
357 page_size = getpagesize();
358 llapi_err_noerrno(LLAPI_MSG_WARN,
359 "warning: your page size (%u) is "
360 "larger than expected (%u)", page_size,
361 LOV_MIN_STRIPE_SIZE);
363 if (!llapi_stripe_size_is_aligned(stripe_size)) {
365 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
366 "must be an even multiple of %d bytes",
367 stripe_size, page_size);
370 if (!llapi_stripe_index_is_valid(stripe_offset)) {
372 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
376 if (!llapi_stripe_count_is_valid(stripe_count)) {
378 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
382 if (llapi_stripe_size_is_too_big(stripe_size)) {
384 llapi_error(LLAPI_MSG_ERROR, rc,
385 "warning: stripe size 4G or larger "
386 "is not currently supported and would wrap");
393 * Trim a trailing newline from a string, if it exists.
395 int llapi_chomp_string(char *buf)
411 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
413 static int get_param_lov(const char *path, const char *param,
414 char *buf, size_t buf_size)
416 struct obd_uuid uuid;
419 rc = llapi_file_get_lov_uuid(path, &uuid);
423 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
428 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
430 static int get_param_lmv(const char *path, const char *param,
431 char *buf, size_t buf_size)
433 struct obd_uuid uuid;
436 rc = llapi_file_get_lmv_uuid(path, &uuid);
440 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
444 static int get_mds_md_size(const char *path)
446 char buf[PATH_MAX], inst[PATH_MAX];
447 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
450 rc = llapi_getname(path, inst, sizeof(inst));
454 /* Get the max ea size from llite parameters. */
455 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
456 "max_easize", buf, sizeof(buf));
462 return rc > 0 ? rc : md_size;
465 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
467 return get_param_lmv(path, "uuid", buf, bufsize);
471 * if pool is NULL, search ostname in target_obd
472 * if pool is not NULL:
473 * if pool not found returns errno < 0
474 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
475 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
477 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
479 char buffer[PATH_MAX];
485 /* You need one or the other */
486 if (poolname == NULL && fsname == NULL)
490 len = strlen(ostname);
492 if (poolname == NULL && len == 0)
495 /* Search by poolname and fsname if is not NULL */
496 if (poolname != NULL) {
497 rc = poolpath(¶m, fsname, NULL);
499 snprintf(buffer, sizeof(buffer), "%s/%s",
500 param.gl_pathv[0], poolname);
502 } else if (fsname != NULL) {
503 rc = get_lustre_param_path("lov", fsname,
505 "target_obd", ¶m);
507 strncpy(buffer, param.gl_pathv[0],
513 cfs_free_param_data(¶m);
517 fd = fopen(buffer, "r");
521 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
522 if (poolname == NULL) {
524 /* Search for an ostname in the list of OSTs
525 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
526 ptr = strchr(buffer, ' ');
528 (strncmp(ptr + 1, ostname, len) == 0)) {
533 /* Search for an ostname in a pool,
534 (or an existing non-empty pool if no ostname) */
535 if ((ostname == NULL) ||
536 (strncmp(buffer, ostname, len) == 0)) {
547 * Open a Lustre file.
549 * \param name the name of the file to be opened
550 * \param flags access mode, see flags in open(2)
551 * \param mode permission of the file if it is created, see mode in open(2)
552 * \param param stripe pattern of the newly created file
554 * \retval file descriptor of opened file
555 * \retval negative errno on failure
557 int llapi_file_open_param(const char *name, int flags, mode_t mode,
558 const struct llapi_stripe_param *param)
560 char fsname[MAX_OBD_NAME + 1] = { 0 };
561 char *pool_name = param->lsp_pool;
562 struct lov_user_md *lum = NULL;
563 size_t lum_size = sizeof(*lum);
566 /* Make sure we are on a Lustre file system */
567 rc = llapi_search_fsname(name, fsname);
569 llapi_error(LLAPI_MSG_ERROR, rc,
570 "'%s' is not on a Lustre filesystem",
575 /* Check if the stripe pattern is sane. */
576 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
577 param->lsp_stripe_offset,
578 param->lsp_stripe_count,
579 param->lsp_stripe_pattern);
583 /* Make sure we have a good pool */
584 if (pool_name != NULL) {
585 /* in case user gives the full pool name <fsname>.<poolname>,
586 * strip the fsname */
587 char *ptr = strchr(pool_name, '.');
590 if (strcmp(pool_name, fsname) != 0) {
592 llapi_err_noerrno(LLAPI_MSG_ERROR,
593 "Pool '%s' is not on filesystem '%s'",
600 /* Make sure the pool exists and is non-empty */
601 rc = llapi_search_ost(fsname, pool_name, NULL);
603 char *err = rc == 0 ? "has no OSTs" : "does not exist";
605 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
606 fsname, pool_name, err);
610 lum_size = sizeof(struct lov_user_md_v3);
613 /* sanity check of target list */
614 if (param->lsp_is_specific) {
615 char ostname[MAX_OBD_NAME + 1];
619 for (i = 0; i < param->lsp_stripe_count; i++) {
620 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
621 fsname, param->lsp_osts[i]);
622 rc = llapi_search_ost(fsname, pool_name, ostname);
627 llapi_error(LLAPI_MSG_ERROR, rc,
628 "%s: cannot find OST %s in %s",
635 /* Make sure stripe offset is in OST list. */
636 if (param->lsp_osts[i] == param->lsp_stripe_offset)
640 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
641 "%s: stripe offset '%d' is not in the "
643 __func__, param->lsp_stripe_offset);
647 lum_size = lov_user_md_size(param->lsp_stripe_count,
648 LOV_USER_MAGIC_SPECIFIC);
651 lum = calloc(1, lum_size);
656 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
658 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
659 flags = O_DIRECTORY | O_RDONLY;
666 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
671 /* Initialize IOCTL striping pattern structure */
672 lum->lmm_magic = LOV_USER_MAGIC_V1;
673 lum->lmm_pattern = param->lsp_stripe_pattern;
674 lum->lmm_stripe_size = param->lsp_stripe_size;
675 lum->lmm_stripe_count = param->lsp_stripe_count;
676 lum->lmm_stripe_offset = param->lsp_stripe_offset;
677 if (pool_name != NULL) {
678 struct lov_user_md_v3 *lumv3 = (void *)lum;
680 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
681 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
683 if (param->lsp_is_specific) {
684 struct lov_user_md_v3 *lumv3 = (void *)lum;
687 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
688 if (pool_name == NULL) {
689 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
690 * OST list, therefore if pool is not specified we have
691 * to pack a null pool name for placeholder. */
692 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
695 for (i = 0; i < param->lsp_stripe_count; i++)
696 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
699 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
700 char *errmsg = "stripe already set";
703 if (errno != EEXIST && errno != EALREADY)
704 errmsg = strerror(errno);
706 llapi_err_noerrno(LLAPI_MSG_ERROR,
707 "error on ioctl %#jx for '%s' (%d): %s",
708 (uintmax_t)LL_IOC_LOV_SETSTRIPE, name, fd,
720 int llapi_file_open_pool(const char *name, int flags, int mode,
721 unsigned long long stripe_size, int stripe_offset,
722 int stripe_count, int stripe_pattern, char *pool_name)
724 const struct llapi_stripe_param param = {
725 .lsp_stripe_size = stripe_size,
726 .lsp_stripe_count = stripe_count,
727 .lsp_stripe_pattern = stripe_pattern,
728 .lsp_stripe_offset = stripe_offset,
729 .lsp_pool = pool_name
731 return llapi_file_open_param(name, flags, mode, ¶m);
734 int llapi_file_open(const char *name, int flags, int mode,
735 unsigned long long stripe_size, int stripe_offset,
736 int stripe_count, int stripe_pattern)
738 return llapi_file_open_pool(name, flags, mode, stripe_size,
739 stripe_offset, stripe_count,
740 stripe_pattern, NULL);
743 int llapi_file_create(const char *name, unsigned long long stripe_size,
744 int stripe_offset, int stripe_count, int stripe_pattern)
748 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
749 stripe_offset, stripe_count, stripe_pattern,
758 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
759 int stripe_offset, int stripe_count,
760 int stripe_pattern, char *pool_name)
764 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
765 stripe_offset, stripe_count, stripe_pattern,
774 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
775 int stripe_count, int stripe_pattern,
776 const char *pool_name)
778 struct lmv_user_md lum = { 0 };
782 lum.lum_magic = LMV_USER_MAGIC;
783 lum.lum_stripe_offset = stripe_offset;
784 lum.lum_stripe_count = stripe_count;
785 lum.lum_hash_type = stripe_pattern;
786 if (pool_name != NULL) {
787 if (strlen(pool_name) >= sizeof(lum.lum_pool_name)) {
788 llapi_err_noerrno(LLAPI_MSG_ERROR,
789 "error LL_IOC_LMV_SET_DEFAULT_STRIPE '%s'"
790 ": too large pool name: %s", name, pool_name);
793 strncpy(lum.lum_pool_name, pool_name,
794 sizeof(lum.lum_pool_name));
797 fd = open(name, O_DIRECTORY | O_RDONLY);
800 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
804 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lum);
806 char *errmsg = "stripe already set";
808 if (errno != EEXIST && errno != EALREADY)
809 errmsg = strerror(errno);
811 llapi_err_noerrno(LLAPI_MSG_ERROR,
812 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
819 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
820 int stripe_count, int stripe_pattern,
821 const char *pool_name)
823 struct lmv_user_md lmu = { 0 };
824 struct obd_ioctl_data data = { 0 };
827 char *dirpath = NULL;
828 char *namepath = NULL;
834 dirpath = strdup(name);
835 namepath = strdup(name);
836 if (!dirpath || !namepath)
839 lmu.lum_magic = LMV_USER_MAGIC;
840 lmu.lum_stripe_offset = stripe_offset;
841 lmu.lum_stripe_count = stripe_count;
842 lmu.lum_hash_type = stripe_pattern;
843 if (pool_name != NULL) {
844 if (strlen(pool_name) > LOV_MAXPOOLNAME) {
845 llapi_err_noerrno(LLAPI_MSG_ERROR,
846 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
847 "pool name: %s", name, pool_name);
851 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
854 filename = basename(namepath);
855 dir = dirname(dirpath);
857 data.ioc_inlbuf1 = (char *)filename;
858 data.ioc_inllen1 = strlen(filename) + 1;
859 data.ioc_inlbuf2 = (char *)&lmu;
860 data.ioc_inllen2 = sizeof(struct lmv_user_md);
861 data.ioc_type = mode;
862 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
864 llapi_error(LLAPI_MSG_ERROR, rc,
865 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
870 fd = open(dir, O_DIRECTORY | O_RDONLY);
873 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
877 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
878 char *errmsg = "stripe already set";
880 if (errno != EEXIST && errno != EALREADY)
881 errmsg = strerror(errno);
883 llapi_err_noerrno(LLAPI_MSG_ERROR,
884 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
894 int llapi_direntry_remove(char *dname)
896 char *dirpath = NULL;
897 char *namepath = NULL;
903 dirpath = strdup(dname);
904 namepath = strdup(dname);
905 if (!dirpath || !namepath)
908 filename = basename(namepath);
910 dir = dirname(dirpath);
912 fd = open(dir, O_DIRECTORY | O_RDONLY);
915 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
920 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
921 char *errmsg = strerror(errno);
922 llapi_err_noerrno(LLAPI_MSG_ERROR,
923 "error on ioctl %#jx for '%s' (%d): %s",
924 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
936 * Find the fsname, the full path, and/or an open fd.
937 * Either the fsname or path must not be NULL
939 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
942 char buf[PATH_MAX], mntdir[PATH_MAX];
945 int idx = 0, len = 0, mntlen, fd;
948 /* get the mount point */
949 fp = setmntent(PROC_MOUNTS, "r");
952 llapi_error(LLAPI_MSG_ERROR, rc,
953 "setmntent(%s) failed", PROC_MOUNTS);
957 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
960 if (!llapi_is_lustre_mnt(&mnt))
963 if ((want & WANT_INDEX) && (idx++ != index))
966 mntlen = strlen(mnt.mnt_dir);
967 ptr = strchr(mnt.mnt_fsname, '/');
968 while (ptr && *ptr == '/')
970 /* thanks to the call to llapi_is_lustre_mnt() above,
971 * we are sure that mnt.mnt_fsname contains ":/",
972 * so ptr should never be NULL */
976 while (*ptr_end != '/' && *ptr_end != '\0')
979 /* Check the fsname for a match, if given */
980 if (!(want & WANT_FSNAME) && fsname != NULL &&
981 (strlen(fsname) > 0) &&
982 (strncmp(ptr, fsname, ptr_end - ptr) != 0))
985 /* If the path isn't set return the first one we find */
986 if (path == NULL || strlen(path) == 0) {
987 strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
988 mntdir[strlen(mnt.mnt_dir)] = '\0';
989 if ((want & WANT_FSNAME) && fsname != NULL) {
990 strncpy(fsname, ptr, ptr_end - ptr);
991 fsname[ptr_end - ptr] = '\0';
995 /* Otherwise find the longest matching path */
996 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
997 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
998 strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
999 mntdir[strlen(mnt.mnt_dir)] = '\0';
1001 if ((want & WANT_FSNAME) && fsname != NULL) {
1002 strncpy(fsname, ptr, ptr_end - ptr);
1003 fsname[ptr_end - ptr] = '\0';
1012 if ((want & WANT_PATH) && path != NULL) {
1013 strncpy(path, mntdir, strlen(mntdir));
1014 path[strlen(mntdir)] = '\0';
1016 if (want & WANT_FD) {
1017 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1020 llapi_error(LLAPI_MSG_ERROR, rc,
1021 "error opening '%s'", mntdir);
1027 } else if (want & WANT_ERROR)
1028 llapi_err_noerrno(LLAPI_MSG_ERROR,
1029 "can't find fs root for '%s': %d",
1030 (want & WANT_PATH) ? fsname : path, rc);
1035 * search lustre mounts
1037 * Calling this function will return to the user the mount point, mntdir, and
1038 * the file system name, fsname, if the user passed a buffer to this routine.
1040 * The user inputs are pathname and index. If the pathname is supplied then
1041 * the value of the index will be ignored. The pathname will return data if
1042 * the pathname is located on a lustre mount. Index is used to pick which
1043 * mount point you want in the case of multiple mounted lustre file systems.
1044 * See function lfs_osts in lfs.c for an example of the index use.
1046 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1049 int want = WANT_PATH, idx = -1;
1051 if (!pathname || pathname[0] == '\0') {
1055 strcpy(mntdir, pathname);
1058 want |= WANT_FSNAME;
1059 return get_root_path(want, fsname, NULL, mntdir, idx);
1062 /* Given a path, find the corresponding Lustre fsname */
1063 int llapi_search_fsname(const char *pathname, char *fsname)
1068 path = realpath(pathname, NULL);
1070 char buf[PATH_MAX], *ptr;
1073 if (pathname[0] != '/') {
1074 /* Need an absolute path, but realpath() only works for
1075 * pathnames that actually exist. We go through the
1076 * extra hurdle of dirname(getcwd() + pathname) in
1077 * case the relative pathname contains ".." in it. */
1078 if (getcwd(buf, sizeof(buf) - 2) == NULL)
1080 rc = strlcat(buf, "/", sizeof(buf));
1081 if (rc >= sizeof(buf))
1084 rc = strlcat(buf, pathname, sizeof(buf));
1085 if (rc >= sizeof(buf))
1087 path = realpath(buf, NULL);
1089 ptr = strrchr(buf, '/');
1093 path = realpath(buf, NULL);
1096 llapi_error(LLAPI_MSG_ERROR, rc,
1097 "pathname '%s' cannot expand",
1103 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1108 int llapi_search_rootpath(char *pathname, const char *fsname)
1110 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1113 int llapi_getname(const char *path, char *buf, size_t size)
1115 struct obd_uuid uuid_buf;
1116 char *uuid = uuid_buf.uuid;
1119 memset(&uuid_buf, 0, sizeof(uuid_buf));
1120 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1124 /* We want to turn lustre-clilov-ffff88002738bc00 into
1125 * lustre-ffff88002738bc00. */
1127 nr = snprintf(buf, size, "%.*s-%s",
1128 (int) (strlen(uuid) - 24), uuid,
1129 uuid + strlen(uuid) - 16);
1138 * Get the list of pool members.
1139 * \param poolname string of format \<fsname\>.\<poolname\>
1140 * \param members caller-allocated array of char*
1141 * \param list_size size of the members array
1142 * \param buffer caller-allocated buffer for storing OST names
1143 * \param buffer_size size of the buffer
1145 * \return number of members retrieved for this pool
1146 * \retval -error failure
1148 int llapi_get_poolmembers(const char *poolname, char **members,
1149 int list_size, char *buffer, int buffer_size)
1151 char fsname[PATH_MAX];
1160 /* name is FSNAME.POOLNAME */
1161 if (strlen(poolname) >= sizeof(fsname))
1163 strlcpy(fsname, poolname, sizeof(fsname));
1164 pool = strchr(fsname, '.');
1171 rc = poolpath(&pathname, fsname, NULL);
1173 llapi_error(LLAPI_MSG_ERROR, rc,
1174 "Lustre filesystem '%s' not found",
1179 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1180 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1181 cfs_free_param_data(&pathname);
1182 if (rc >= sizeof(buf))
1184 fd = fopen(buf, "r");
1187 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1192 while (fgets(buf, sizeof(buf), fd) != NULL) {
1193 if (nb_entries >= list_size) {
1197 buf[sizeof(buf) - 1] = '\0';
1199 tmp = strchr(buf, '\n');
1202 if (used + strlen(buf) + 1 > buffer_size) {
1207 strcpy(buffer + used, buf);
1208 members[nb_entries] = buffer + used;
1209 used += strlen(buf) + 1;
1219 * Get the list of pools in a filesystem.
1220 * \param name filesystem name or path
1221 * \param poollist caller-allocated array of char*
1222 * \param list_size size of the poollist array
1223 * \param buffer caller-allocated buffer for storing pool names
1224 * \param buffer_size size of the buffer
1226 * \return number of pools retrieved for this filesystem
1227 * \retval -error failure
1229 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1230 char *buffer, int buffer_size)
1232 char rname[PATH_MAX];
1238 struct dirent *cookie = NULL;
1240 unsigned int nb_entries = 0;
1241 unsigned int used = 0;
1244 /* initialize output array */
1245 for (i = 0; i < list_size; i++)
1248 /* is name a pathname ? */
1249 ptr = strchr(name, '/');
1251 /* only absolute pathname is supported */
1255 if (!realpath(name, rname)) {
1257 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1262 fsname = strdup(rname);
1266 rc = poolpath(&pathname, NULL, rname);
1268 /* name is FSNAME */
1269 fsname = strdup(name);
1272 rc = poolpath(&pathname, fsname, NULL);
1275 llapi_error(LLAPI_MSG_ERROR, rc,
1276 "Lustre filesystem '%s' not found", name);
1280 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1281 dir = opendir(pathname.gl_pathv[0]);
1284 llapi_error(LLAPI_MSG_ERROR, rc,
1285 "Could not open pool list for '%s'",
1291 rc = readdir_r(dir, &pool, &cookie);
1294 llapi_error(LLAPI_MSG_ERROR, rc,
1295 "Error reading pool list for '%s'", name);
1297 } else if ((rc == 0) && (cookie == NULL)) {
1298 /* end of directory */
1302 /* ignore . and .. */
1303 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1306 /* check output bounds */
1307 if (nb_entries >= list_size) {
1312 /* +2 for '.' and final '\0' */
1313 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1319 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1320 poollist[nb_entries] = buffer + used;
1321 used += strlen(pool.d_name) + strlen(fsname) + 2;
1328 cfs_free_param_data(&pathname);
1331 return rc != 0 ? rc : nb_entries;
1334 /* wrapper for lfs.c and obd.c */
1335 int llapi_poollist(const char *name)
1337 /* list of pool names (assume that pool count is smaller
1339 char **list, *buffer = NULL, *fsname = (char *)name;
1340 char *poolname = NULL, *tmp = NULL, data[16];
1341 enum param_filter type = FILTER_BY_PATH;
1342 int obdcount, bufsize, rc, nb, i;
1347 if (name[0] != '/') {
1348 fsname = strdup(name);
1352 poolname = strchr(fsname, '.');
1355 type = FILTER_BY_FS_NAME;
1358 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1359 data, sizeof(data));
1362 obdcount = atoi(data);
1364 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1365 * and also an array to store the pointers for all that
1366 * allocated space. */
1368 bufsize = sizeof(struct obd_uuid) * obdcount;
1369 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1370 if (buffer == NULL) {
1374 list = (char **) (buffer + bufsize);
1377 /* name is a path or fsname */
1378 nb = llapi_get_poollist(name, list, obdcount,
1381 /* name is a pool name (<fsname>.<poolname>) */
1382 nb = llapi_get_poolmembers(name, list, obdcount,
1386 if (nb == -EOVERFLOW) {
1389 goto retry_get_pools;
1392 for (i = 0; i < nb; i++)
1393 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1394 rc = (nb < 0 ? nb : 0);
1398 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1403 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1404 void *data, struct dirent64 *de);
1406 #define OBD_NOT_FOUND (-1)
1408 static int common_param_init(struct find_param *param, char *path)
1410 int lum_size = get_mds_md_size(path);
1412 if (lum_size < PATH_MAX + 1)
1413 lum_size = PATH_MAX + 1;
1415 param->fp_lum_size = lum_size;
1416 param->fp_lmd = calloc(1, sizeof(lstat_t) + param->fp_lum_size);
1417 if (param->fp_lmd == NULL) {
1418 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1419 "error: allocation of %zu bytes for ioctl",
1420 sizeof(lstat_t) + param->fp_lum_size);
1424 param->fp_lmv_stripe_count = 256;
1425 param->fp_lmv_md = calloc(1,
1426 lmv_user_md_size(param->fp_lmv_stripe_count,
1428 if (param->fp_lmv_md == NULL) {
1429 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1430 "error: allocation of %d bytes for ioctl",
1431 lmv_user_md_size(param->fp_lmv_stripe_count,
1436 param->fp_got_uuids = 0;
1437 param->fp_obd_indexes = NULL;
1438 param->fp_obd_index = OBD_NOT_FOUND;
1439 if (!param->fp_migrate)
1440 param->fp_mdt_index = OBD_NOT_FOUND;
1444 static void find_param_fini(struct find_param *param)
1446 if (param->fp_obd_indexes)
1447 free(param->fp_obd_indexes);
1450 free(param->fp_lmd);
1452 if (param->fp_lmv_md)
1453 free(param->fp_lmv_md);
1456 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1457 struct dirent64 *de)
1459 struct find_param *param = data;
1465 /* set errno upon failure */
1466 static DIR *opendir_parent(const char *path)
1472 path_copy = strdup(path);
1473 if (path_copy == NULL)
1476 parent_path = dirname(path_copy);
1477 parent = opendir(parent_path);
1483 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1488 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1489 if (param->fp_get_default_lmv)
1490 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1492 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1494 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1495 if (errno == E2BIG && ret != 0) {
1499 stripe_count = (__u32)param->fp_lmv_md->lum_stripe_count;
1500 if (stripe_count <= param->fp_lmv_stripe_count)
1503 free(param->fp_lmv_md);
1504 param->fp_lmv_stripe_count = stripe_count;
1505 lmv_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1506 param->fp_lmv_md = malloc(lmv_size);
1507 if (param->fp_lmv_md == NULL) {
1508 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1509 "error: allocation of %d bytes for ioctl",
1510 lmv_user_md_size(param->fp_lmv_stripe_count,
1519 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1520 struct lov_user_mds_data *lmd, int lumlen)
1522 lstat_t *st = &lmd->lmd_st;
1525 if (parent == NULL && dir == NULL)
1529 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1530 } else if (parent) {
1531 char *fname = strrchr(path, '/');
1533 /* To avoid opening, locking, and closing each file on the
1534 * client if that is not needed. The GETFILEINFO ioctl can
1535 * be done on the patent dir with a single open for all
1536 * files in that directory, and it also doesn't pollute the
1537 * client dcache with millions of dentries when traversing
1538 * a large filesystem. */
1539 fname = (fname == NULL ? path : fname + 1);
1540 /* retrieve needed file info */
1541 strlcpy((char *)lmd, fname, lumlen);
1542 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1546 if (errno == ENOTTY) {
1547 /* ioctl is not supported, it is not a lustre fs.
1548 * Do the regular lstat(2) instead. */
1549 ret = lstat_f(path, st);
1552 llapi_error(LLAPI_MSG_ERROR, ret,
1553 "error: %s: lstat failed for %s",
1556 } else if (errno == ENOENT) {
1558 llapi_error(LLAPI_MSG_WARN, ret,
1559 "warning: %s: %s does not exist",
1561 } else if (errno != EISDIR) {
1563 llapi_error(LLAPI_MSG_ERROR, ret,
1564 "%s ioctl failed for %s.",
1565 dir ? "LL_IOC_MDC_GETINFO" :
1566 "IOC_MDC_GETFILEINFO", path);
1569 llapi_error(LLAPI_MSG_ERROR, ret,
1570 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1577 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1578 semantic_func_t sem_init,
1579 semantic_func_t sem_fini, void *data,
1580 struct dirent64 *de)
1582 struct find_param *param = (struct find_param *)data;
1583 struct dirent64 *dent;
1591 if (!d && errno != ENOTDIR) {
1593 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1596 } else if (!d && !parent) {
1597 /* ENOTDIR. Open the parent dir. */
1598 p = opendir_parent(path);
1605 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1611 while ((dent = readdir64(d)) != NULL) {
1614 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1617 /* Don't traverse .lustre directory */
1618 if (!(strcmp(dent->d_name, dot_lustre_name)))
1622 if ((len + dent->d_reclen + 2) > size) {
1623 llapi_err_noerrno(LLAPI_MSG_ERROR,
1624 "error: %s: string buffer is too small",
1629 strcat(path, dent->d_name);
1631 if (dent->d_type == DT_UNKNOWN) {
1632 lstat_t *st = ¶m->fp_lmd->lmd_st;
1634 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
1635 param->fp_lum_size);
1637 dent->d_type = IFTODT(st->st_mode);
1644 switch (dent->d_type) {
1646 llapi_err_noerrno(LLAPI_MSG_ERROR,
1647 "error: %s: '%s' is UNKNOWN type %d",
1648 __func__, dent->d_name, dent->d_type);
1651 rc = llapi_semantic_traverse(path, size, d, sem_init,
1652 sem_fini, data, dent);
1653 if (rc != 0 && ret == 0)
1659 rc = sem_init(path, d, NULL, data, dent);
1660 if (rc < 0 && ret == 0)
1663 if (sem_fini && rc == 0)
1664 sem_fini(path, d, NULL, data, dent);
1672 sem_fini(path, parent, &d, data, de);
1681 static int param_callback(char *path, semantic_func_t sem_init,
1682 semantic_func_t sem_fini, struct find_param *param)
1684 int ret, len = strlen(path);
1687 if (len > PATH_MAX) {
1689 llapi_error(LLAPI_MSG_ERROR, ret,
1690 "Path name '%s' is too long", path);
1694 buf = (char *)malloc(PATH_MAX + 1);
1698 strlcpy(buf, path, PATH_MAX + 1);
1699 ret = common_param_init(param, buf);
1703 param->fp_depth = 0;
1705 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1706 sem_fini, param, NULL);
1708 find_param_fini(param);
1710 return ret < 0 ? ret : 0;
1713 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1715 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1718 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1723 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1725 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1728 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1733 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1737 fd = open(path, O_RDONLY | O_NONBLOCK);
1740 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1744 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1750 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1754 fd = open(path, O_RDONLY | O_NONBLOCK);
1757 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1761 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1773 * If uuidp is NULL, return the number of available obd uuids.
1774 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1775 * there are more OSTs than allocated to uuidp, then an error is returned with
1776 * the ost_count set to number of available obd uuids.
1778 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1779 int *ost_count, enum tgt_type type)
1781 char buf[PATH_MAX], format[32];
1782 int rc = 0, index = 0;
1783 struct obd_uuid name;
1787 /* Get the lov name */
1788 if (type == LOV_TYPE)
1789 rc = llapi_file_fget_lov_uuid(fd, &name);
1791 rc = llapi_file_fget_lmv_uuid(fd, &name);
1795 /* Now get the ost uuids */
1796 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
1797 FILTER_BY_EXACT, "target_obd", ¶m);
1801 fp = fopen(param.gl_pathv[0], "r");
1804 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
1809 snprintf(format, sizeof(format),
1810 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
1811 while (fgets(buf, sizeof(buf), fp) != NULL) {
1812 if (uuidp && (index < *ost_count)) {
1813 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
1821 if (uuidp && (index > *ost_count))
1826 cfs_free_param_data(¶m);
1830 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1832 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1835 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1840 root = open(mnt, O_RDONLY | O_DIRECTORY);
1843 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1848 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
1856 /* Check if user specified value matches a real uuid. Ignore _UUID,
1857 * -osc-4ba41334, other trailing gunk in comparison.
1858 * @param real_uuid ends in "_UUID"
1859 * @param search_uuid may or may not end in "_UUID"
1861 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1863 int cmplen = strlen(real_uuid);
1864 int searchlen = strlen(search_uuid);
1866 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1868 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1871 /* The UUIDs may legitimately be different lengths, if
1872 * the system was upgraded from an older version. */
1873 if (cmplen != searchlen)
1876 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1879 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
1880 * returned in param->fp_obd_index */
1881 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
1883 struct obd_uuid obd_uuid;
1890 if (param->fp_got_uuids)
1893 /* Get the lov/lmv name */
1894 if (param->fp_get_lmv)
1895 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
1897 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
1899 if (rc != -ENOTTY) {
1900 llapi_error(LLAPI_MSG_ERROR, rc,
1901 "error: can't get %s name: %s",
1902 param->fp_get_lmv ? "lmv" : "lov",
1910 param->fp_got_uuids = 1;
1912 /* Now get the ost uuids */
1913 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
1914 obd_uuid.uuid, FILTER_BY_EXACT,
1915 "target_obd", ¶m_data);
1919 fp = fopen(param_data.gl_pathv[0], "r");
1922 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
1923 param_data.gl_pathv[0]);
1927 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
1928 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1929 param->fp_get_lmv ? "MDTS" : "OBDS");
1931 snprintf(format, sizeof(format),
1932 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
1933 while (fgets(buf, sizeof(buf), fp) != NULL) {
1936 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
1939 if (param->fp_obd_uuid) {
1940 if (llapi_uuid_match(obd_uuid.uuid,
1941 param->fp_obd_uuid->uuid)) {
1942 param->fp_obd_index = index;
1945 } else if (!param->fp_quiet && !param->fp_obds_printed) {
1946 /* Print everything */
1947 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1950 param->fp_obds_printed = 1;
1954 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
1955 llapi_err_noerrno(LLAPI_MSG_ERROR,
1956 "error: %s: unknown obduuid: %s",
1957 __func__, param->fp_obd_uuid->uuid);
1961 cfs_free_param_data(¶m_data);
1965 /* In this case, param->fp_obd_uuid will be an array of obduuids and
1966 * obd index for all these obduuids will be returned in
1967 * param->fp_obd_indexes */
1968 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1969 int num_obds, int **obdindexes, int *obdindex,
1972 int ret, obdcount, obd_valid = 0, obdnum;
1974 struct obd_uuid *uuids = NULL;
1978 if (type == LOV_TYPE)
1979 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
1981 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
1985 obdcount = atoi(buf);
1986 uuids = malloc(obdcount * sizeof(struct obd_uuid));
1991 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1993 if (ret == -EOVERFLOW) {
1994 struct obd_uuid *uuids_temp;
1996 uuids_temp = realloc(uuids, obdcount *
1997 sizeof(struct obd_uuid));
1998 if (uuids_temp != NULL) {
2000 goto retry_get_uuids;
2005 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
2009 indexes = malloc(num_obds * sizeof(*obdindex));
2010 if (indexes == NULL) {
2015 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2018 /* The user may have specified a simple index */
2019 i = strtol(obduuids[obdnum].uuid, &end, 0);
2020 if (end && *end == '\0' && i < obdcount) {
2021 indexes[obdnum] = i;
2024 for (i = 0; i < obdcount; i++) {
2025 if (llapi_uuid_match(uuids[i].uuid,
2026 obduuids[obdnum].uuid)) {
2027 indexes[obdnum] = i;
2033 if (i >= obdcount) {
2034 indexes[obdnum] = OBD_NOT_FOUND;
2035 llapi_err_noerrno(LLAPI_MSG_ERROR,
2036 "error: %s: unknown obduuid: %s",
2037 __func__, obduuids[obdnum].uuid);
2043 *obdindex = OBD_NOT_FOUND;
2045 *obdindex = obd_valid;
2047 *obdindexes = indexes;
2055 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2059 if (param->fp_mdt_uuid) {
2060 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
2062 ¶m->fp_mdt_indexes,
2063 ¶m->fp_mdt_index, LMV_TYPE);
2068 if (param->fp_obd_uuid) {
2069 ret = setup_indexes(dir, path, param->fp_obd_uuid,
2071 ¶m->fp_obd_indexes,
2072 ¶m->fp_obd_index, LOV_TYPE);
2077 param->fp_got_uuids = 1;
2082 int llapi_ostlist(char *path, struct find_param *param)
2087 fd = open(path, O_RDONLY | O_DIRECTORY);
2091 ret = setup_obd_uuid(fd, path, param);
2098 * Tries to determine the default stripe attributes for a given filesystem. The
2099 * filesystem to check should be specified by fsname, or will be determined
2102 static int sattr_get_defaults(const char *const fsname,
2103 unsigned int *scount,
2104 unsigned int *ssize,
2105 unsigned int *soffset)
2111 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2112 "stripecount", val, sizeof(val));
2115 *scount = atoi(val);
2119 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2120 "stripesize", val, sizeof(val));
2127 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2128 "stripeoffset", val, sizeof(val));
2131 *soffset = atoi(val);
2138 * Tries to gather the default stripe attributes for a given filesystem. If
2139 * the attributes can be determined, they are cached for easy retreival the
2140 * next time they are needed. Only a single filesystem's attributes are
2143 int sattr_cache_get_defaults(const char *const fsname,
2144 const char *const pathname, unsigned int *scount,
2145 unsigned int *ssize, unsigned int *soffset)
2148 char fsname[PATH_MAX + 1];
2149 unsigned int stripecount;
2150 unsigned int stripesize;
2151 unsigned int stripeoffset;
2157 char fsname_buf[PATH_MAX + 1];
2158 unsigned int tmp[3];
2160 if (fsname == NULL) {
2161 rc = llapi_search_fsname(pathname, fsname_buf);
2165 strlcpy(fsname_buf, fsname, sizeof(fsname_buf));
2168 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2170 * Ensure all 3 sattrs (count, size, and offset) are
2171 * successfully retrieved and stored in tmp before writing to
2174 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2178 cache.stripecount = tmp[0];
2179 cache.stripesize = tmp[1];
2180 cache.stripeoffset = tmp[2];
2181 strlcpy(cache.fsname, fsname_buf, sizeof(cache.fsname));
2185 *scount = cache.stripecount;
2187 *ssize = cache.stripesize;
2189 *soffset = cache.stripeoffset;
2194 enum lov_dump_flags {
2195 LDF_IS_DIR = 0x0001,
2196 LDF_IS_RAW = 0x0002,
2197 LDF_INDENT = 0x0004,
2198 LDF_SKIP_OBJS = 0x0008,
2202 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2203 struct lov_user_ost_data_v1 *objects,
2204 int verbose, int depth, char *pool_name,
2205 enum lov_dump_flags flags)
2207 bool is_dir = flags & LDF_IS_DIR;
2208 bool is_raw = flags & LDF_IS_RAW;
2209 bool indent = flags & LDF_INDENT;
2210 bool yaml = flags & LDF_YAML;
2211 bool skip_objs = flags & LDF_SKIP_OBJS;
2212 char *prefix = is_dir ? "" : "lmm_";
2213 char *separator = "";
2214 char *space = indent ? " " : "";
2217 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2218 lmm_oi_set_seq(&lum->lmm_oi, 0);
2219 if (!indent && (verbose & VERBOSE_DETAIL))
2220 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2223 if (!yaml && !indent && depth && path &&
2224 ((verbose != VERBOSE_OBJID) || !is_dir))
2225 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2227 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2228 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2229 space, prefix, lum->lmm_magic);
2230 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2232 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2233 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2235 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2237 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2242 if (verbose & ~VERBOSE_DFID)
2243 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2245 /* This needs a bit of hand-holding since old 1.x lmm_oi
2246 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2247 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2248 * a real FID. Ideally the 2.x code would have stored this
2249 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2250 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2251 * worked properly (especially since IGIF FIDs use mds_inum as
2252 * the FID SEQ), but unfortunately that didn't happen.
2254 * Print it to look like an IGIF FID, even though the fields
2255 * are reversed on disk, so that it makes sense to userspace.
2257 * Don't use ostid_id() and ostid_seq(), since they assume the
2258 * oi_fid fields are in the right order. This is why there are
2259 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2261 * For newer layout types hopefully this will be a real FID. */
2262 seq = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2263 lmm_oi_id(&lum->lmm_oi) : lmm_oi_seq(&lum->lmm_oi);
2264 oid = lmm_oi_seq(&lum->lmm_oi) == 0 ?
2265 0 : (__u32)lmm_oi_id(&lum->lmm_oi);
2266 ver = (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32);
2268 llapi_printf(LLAPI_MSG_NORMAL, DFID_NOBRACE"\n",
2271 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2275 if (verbose & VERBOSE_COUNT) {
2276 if (verbose & ~VERBOSE_COUNT)
2277 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2280 if (!is_raw && lum->lmm_stripe_count == 0) {
2281 unsigned int scount;
2282 rc = sattr_cache_get_defaults(NULL, path,
2286 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2289 llapi_error(LLAPI_MSG_ERROR, rc,
2290 "Cannot determine default"
2293 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2294 lum->lmm_stripe_count ==
2295 (typeof(lum->lmm_stripe_count))(-1)
2296 ? -1 : lum->lmm_stripe_count);
2299 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2300 (__s16)lum->lmm_stripe_count);
2302 if (!yaml && is_dir)
2308 if (verbose & VERBOSE_SIZE) {
2309 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2310 if (verbose & ~VERBOSE_SIZE)
2311 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2313 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2315 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2318 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2320 llapi_error(LLAPI_MSG_ERROR, rc,
2321 "Cannot determine default"
2324 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2325 lum->lmm_stripe_size);
2327 if (!yaml && is_dir)
2333 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2334 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2335 if (verbose & ~VERBOSE_LAYOUT)
2336 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2338 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2342 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2343 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2344 if (verbose & ~VERBOSE_GENERATION)
2345 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2347 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2348 (int)lum->lmm_layout_gen);
2352 if (verbose & VERBOSE_OFFSET) {
2353 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2354 if (verbose & ~VERBOSE_OFFSET)
2355 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2357 if (is_dir || skip_objs)
2358 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2359 lum->lmm_stripe_offset ==
2360 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2361 lum->lmm_stripe_offset);
2363 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2364 objects[0].l_ost_idx);
2365 if (!yaml && is_dir)
2371 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2372 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2373 if (verbose & ~VERBOSE_POOL)
2374 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2376 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2377 if (!yaml && is_dir)
2383 if (strlen(separator) != 0)
2384 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2387 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2388 struct lov_user_ost_data_v1 *objects,
2389 char *path, int obdindex, int depth,
2390 int header, enum lov_dump_flags flags)
2392 bool is_dir = flags & LDF_IS_DIR;
2393 bool indent = flags & LDF_INDENT;
2394 bool skip_objs = flags & LDF_SKIP_OBJS;
2395 bool yaml = flags & LDF_YAML;
2396 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2399 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2400 if (obdindex == objects[i].l_ost_idx) {
2410 lov_dump_user_lmm_header(lum, path, objects, header, depth, pool_name,
2413 if (!is_dir && !skip_objs && (header & VERBOSE_OBJID) &&
2414 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2415 char *space = " - ";
2418 llapi_printf(LLAPI_MSG_NORMAL,
2419 "%6slmm_objects:\n", " ");
2421 llapi_printf(LLAPI_MSG_NORMAL, "lmm_objects:\n");
2423 llapi_printf(LLAPI_MSG_NORMAL,
2424 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2426 for (i = 0; i < lum->lmm_stripe_count; i++) {
2427 int idx = objects[i].l_ost_idx;
2428 long long oid = ostid_id(&objects[i].l_ost_oi);
2429 long long gr = ostid_seq(&objects[i].l_ost_oi);
2431 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2435 struct lu_fid fid = { 0 };
2437 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2438 llapi_printf(LLAPI_MSG_NORMAL,
2439 "%sl_ost_idx: %d\n", space, idx);
2440 llapi_printf(LLAPI_MSG_NORMAL,
2441 "%8sl_fid: "DFID_NOBRACE"\n",
2443 } else if (indent) {
2444 struct lu_fid fid = { 0 };
2446 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2447 llapi_printf(LLAPI_MSG_NORMAL,
2448 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2449 space, i, idx, PFID(&fid));
2452 sprintf(fmt, "%s%s%s\n",
2453 "\t%6u\t%14llu\t%#13llx\t",
2454 (fid_seq_is_rsvd(gr) ||
2455 fid_seq_is_mdt0(gr)) ?
2456 "%14llu" : "%#14llx", "%s");
2457 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2459 obdindex == idx ? " *" : "");
2462 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2466 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2467 char *path, int obdindex, int depth, int verbose,
2468 enum lov_dump_flags flags)
2470 struct lmv_user_mds_data *objects = lum->lum_objects;
2471 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2472 int i, obdstripe = 0;
2473 char *separator = "";
2474 bool yaml = flags & LDF_YAML;
2476 if (obdindex != OBD_NOT_FOUND) {
2477 if (lum->lum_stripe_count == 0) {
2478 if (obdindex == lum->lum_stripe_offset)
2481 for (i = 0; i < lum->lum_stripe_count; i++) {
2482 if (obdindex == objects[i].lum_mds) {
2483 llapi_printf(LLAPI_MSG_NORMAL,
2498 /* show all information default */
2500 if (lum->lum_magic == LMV_USER_MAGIC)
2501 verbose = VERBOSE_POOL | VERBOSE_COUNT |
2502 VERBOSE_OFFSET | VERBOSE_HASH_TYPE;
2504 verbose = VERBOSE_OBJID;
2507 if (depth && path && ((verbose != VERBOSE_OBJID)))
2508 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2510 if (verbose & VERBOSE_COUNT) {
2511 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2512 if (verbose & ~VERBOSE_COUNT)
2513 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2514 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2515 (int)lum->lum_stripe_count);
2516 if ((verbose & VERBOSE_OFFSET) && !yaml)
2522 if (verbose & VERBOSE_OFFSET) {
2523 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2524 if (verbose & ~VERBOSE_OFFSET)
2525 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2526 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2527 (int)lum->lum_stripe_offset);
2528 if (verbose & VERBOSE_HASH_TYPE && !yaml)
2534 if (verbose & VERBOSE_HASH_TYPE) {
2535 unsigned int type = lum->lum_hash_type;
2537 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2538 if (verbose & ~VERBOSE_HASH_TYPE)
2539 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
2540 if (type < LMV_HASH_TYPE_MAX)
2541 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2542 mdt_hash_name[type]);
2544 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2549 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2550 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2551 if (obdstripe == 1 && lum->lum_stripe_count > 0)
2552 llapi_printf(LLAPI_MSG_NORMAL,
2553 "mdtidx\t\t FID[seq:oid:ver]\n");
2554 for (i = 0; i < lum->lum_stripe_count; i++) {
2555 int idx = objects[i].lum_mds;
2556 struct lu_fid *fid = &objects[i].lum_fid;
2557 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2558 llapi_printf(LLAPI_MSG_NORMAL,
2559 "%6u\t\t "DFID"\t\t%s\n",
2561 obdindex == idx ? " *" : "");
2566 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
2567 pool_name[0] != '\0') {
2568 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2569 if (verbose & ~VERBOSE_POOL)
2570 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2572 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2576 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
2577 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2580 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
2581 enum lov_dump_flags flags)
2583 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
2584 int depth = param->fp_max_depth;
2585 int verbose = param->fp_verbose;
2586 bool yaml = flags & LDF_YAML;
2588 if (depth && path && ((verbose != VERBOSE_OBJID) ||
2589 !(flags & LDF_IS_DIR)) && !yaml)
2590 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2592 if (verbose & VERBOSE_DETAIL) {
2593 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
2594 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
2595 " ", comp_v1->lcm_magic);
2596 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
2597 " ", comp_v1->lcm_size);
2598 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_flags: %u\n",
2599 " ", comp_v1->lcm_flags);
2602 if (verbose & VERBOSE_GENERATION) {
2603 if (verbose & ~VERBOSE_GENERATION)
2604 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
2606 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
2609 if (verbose & VERBOSE_COMP_COUNT) {
2610 if (verbose & ~VERBOSE_COMP_COUNT)
2611 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
2613 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
2614 comp_v1->lcm_entry_count);
2617 if (verbose & VERBOSE_DETAIL && !yaml)
2618 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
2621 static void lov_dump_comp_v1_entry(struct find_param *param,
2622 enum lov_dump_flags flags, int index)
2624 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
2625 struct lov_comp_md_entry_v1 *entry;
2626 char *separator = "";
2627 int verbose = param->fp_verbose;
2628 bool yaml = flags & LDF_YAML;
2630 entry = &comp_v1->lcm_entries[index];
2633 llapi_printf(LLAPI_MSG_NORMAL, "%2scomponent%d:\n", " ", index);
2635 if (verbose & VERBOSE_COMP_ID) {
2636 if (verbose & VERBOSE_DETAIL && !yaml)
2637 llapi_printf(LLAPI_MSG_NORMAL,
2638 "%slcme_id: ", " - ");
2639 else if (verbose & ~VERBOSE_COMP_ID)
2640 llapi_printf(LLAPI_MSG_NORMAL,
2641 "%4slcme_id: ", " ");
2642 if (!(flags & LDF_IS_DIR))
2643 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
2645 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
2649 if (verbose & VERBOSE_COMP_FLAGS) {
2650 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2651 if (verbose & ~VERBOSE_COMP_FLAGS)
2652 llapi_printf(LLAPI_MSG_NORMAL,
2653 "%4slcme_flags: ", " ");
2654 llapi_printf(LLAPI_MSG_NORMAL, "%#x", entry->lcme_flags);
2658 if (verbose & VERBOSE_COMP_START) {
2659 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2660 if (verbose & ~VERBOSE_COMP_START)
2661 llapi_printf(LLAPI_MSG_NORMAL,
2662 "%4slcme_extent.e_start: ", " ");
2663 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2664 entry->lcme_extent.e_start);
2668 if (verbose & VERBOSE_COMP_END) {
2669 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2670 if (verbose & ~VERBOSE_COMP_END)
2671 llapi_printf(LLAPI_MSG_NORMAL,
2672 "%4slcme_extent.e_end: ", " ");
2673 if (entry->lcme_extent.e_end == LUSTRE_EOF)
2674 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
2676 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2677 entry->lcme_extent.e_end);
2682 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2683 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
2684 } else if (verbose & VERBOSE_DETAIL) {
2685 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2686 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
2687 " ", entry->lcme_offset);
2688 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
2689 " ", entry->lcme_size);
2690 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
2692 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2696 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2697 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2699 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2700 * The table below gives the answers for the specified parameters (value and
2701 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2702 * --------------------------------------
2703 * 1 | file > limit; sign > 0 | -1 / -1 |
2704 * 2 | file = limit; sign > 0 | -1 / -1 |
2705 * 3 | file < limit; sign > 0 | ? / 1 |
2706 * 4 | file > limit; sign = 0 | -1 / -1 |
2707 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2708 * 6 | file < limit; sign = 0 | ? / -1 |
2709 * 7 | file > limit; sign < 0 | 1 / 1 |
2710 * 8 | file = limit; sign < 0 | ? / -1 |
2711 * 9 | file < limit; sign < 0 | ? / -1 |
2712 * --------------------------------------
2713 * Note: 5th actually means that the value is within the interval
2714 * (limit - margin, limit]. */
2715 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2716 int sign, int negopt, unsigned long long margin,
2722 /* Drop the fraction of margin (of days). */
2723 if (file + margin <= limit)
2725 } else if (sign == 0) {
2726 if (file <= limit && file + margin > limit)
2728 else if (file + margin <= limit)
2730 } else if (sign < 0) {
2737 return negopt ? ~ret + 1 : ret;
2740 static inline struct lov_user_md *
2741 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
2743 return (struct lov_user_md *)((char *)comp_v1 +
2744 comp_v1->lcm_entries[ent_idx].lcme_offset);
2747 static inline struct lov_user_ost_data_v1 *
2748 lov_v1v3_objects(struct lov_user_md *v1)
2750 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
2751 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
2753 return v1->lmm_objects;
2757 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
2759 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
2760 strlcpy(pool_name, ((struct lov_user_md_v3 *)v1)->lmm_pool_name,
2763 pool_name[0] = '\0';
2767 * An example of "getstripe -v" for a two components PFL file:
2770 * lcm_magic: 0x0BD60BD0
2774 * lcm_entry_count: 2
2778 * lcme_extent.e_start: 0
2779 * lcme_extent.e_end: 1048576
2783 * lmm_magic: 0x0BD10BD0
2784 * lmm_seq: 0x200000401
2785 * lmm_object_id: 0x1
2786 * lmm_fid: [0x200000401:0x1:0x0]
2787 * lmm_stripe_count: 1
2788 * lmm_stripe_size: 1048576
2791 * lmm_stripe_offset: 0
2793 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
2797 * lcme_extent.e_start: 1048576
2798 * lcme_extent.e_end: EOF
2802 * lmm_magic: 0x0BD10BD0
2803 * lmm_seq: 0x200000401
2804 * lmm_object_id: 0x1
2805 * lmm_fid: [0x200000401:0x1:0x0]
2806 * lmm_stripe_count: 2
2807 * lmm_stripe_size: 1048576
2810 * lmm_stripe_offset: 1
2812 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
2813 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
2815 static void lov_dump_comp_v1(struct find_param *param, char *path,
2816 enum lov_dump_flags flags)
2818 struct lov_comp_md_entry_v1 *entry;
2819 struct lov_user_ost_data_v1 *objects;
2820 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
2821 struct lov_user_md_v1 *v1;
2822 char pool_name[LOV_MAXPOOLNAME + 1];
2823 int obdindex = param->fp_obd_index;
2824 int i, j, match, obdstripe = 0;
2826 if (obdindex != OBD_NOT_FOUND) {
2827 for (i = 0; !(flags & LDF_IS_DIR) &&
2828 i < comp_v1->lcm_entry_count; i++) {
2829 if (!(comp_v1->lcm_entries[i].lcme_flags &
2833 v1 = lov_comp_entry(comp_v1, i);
2834 objects = lov_v1v3_objects(v1);
2836 for (j = 0; j < v1->lmm_stripe_count; j++) {
2837 if (obdindex == objects[j].l_ost_idx) {
2850 lov_dump_comp_v1_header(param, path, flags);
2852 flags |= LDF_INDENT;
2854 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2855 entry = &comp_v1->lcm_entries[i];
2857 if (param->fp_check_comp_flags &&
2858 !(param->fp_comp_flags & entry->lcme_flags))
2861 if (entry->lcme_flags & LCME_FL_INIT)
2862 flags &= ~LDF_SKIP_OBJS;
2864 flags |= LDF_SKIP_OBJS;
2866 if (param->fp_check_comp_id &&
2867 param->fp_comp_id != entry->lcme_id)
2870 if (param->fp_check_comp_start) {
2871 match = find_value_cmp(entry->lcme_extent.e_start,
2872 param->fp_comp_start,
2873 param->fp_comp_start_sign,
2875 param->fp_comp_start_units, 0);
2880 if (param->fp_check_comp_end) {
2881 unsigned long long margin;
2883 margin = entry->lcme_extent.e_end == LUSTRE_EOF ?
2884 0 : param->fp_comp_end_units;
2886 match = find_value_cmp(entry->lcme_extent.e_end,
2888 param->fp_comp_end_sign,
2894 lov_dump_comp_v1_entry(param, flags, i);
2896 v1 = lov_comp_entry(comp_v1, i);
2897 objects = lov_v1v3_objects(v1);
2898 lov_v1v3_pool_name(v1, pool_name);
2900 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
2901 param->fp_max_depth, param->fp_verbose,
2906 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
2907 enum lov_dump_flags flags)
2911 if (param->fp_get_lmv || param->fp_get_default_lmv)
2912 magic = (__u32)param->fp_lmv_md->lum_magic;
2914 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
2917 flags |= LDF_IS_RAW;
2922 case LOV_USER_MAGIC_V1:
2923 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
2924 param->fp_lmd->lmd_lmm.lmm_objects,
2925 path, param->fp_obd_index,
2926 param->fp_max_depth, param->fp_verbose,
2929 case LOV_USER_MAGIC_V3: {
2930 char pool_name[LOV_MAXPOOLNAME + 1];
2931 struct lov_user_ost_data_v1 *objects;
2932 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2934 strlcpy(pool_name, lmmv3->lmm_pool_name, sizeof(pool_name));
2935 objects = lmmv3->lmm_objects;
2936 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
2937 objects, path, param->fp_obd_index,
2938 param->fp_max_depth, param->fp_verbose,
2943 case LMV_USER_MAGIC: {
2944 char pool_name[LOV_MAXPOOLNAME + 1];
2945 struct lmv_user_md *lum;
2947 lum = (struct lmv_user_md *)param->fp_lmv_md;
2948 strlcpy(pool_name, lum->lum_pool_name, sizeof(pool_name));
2949 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
2950 param->fp_max_depth, param->fp_verbose,
2954 case LOV_USER_MAGIC_COMP_V1:
2955 lov_dump_comp_v1(param, path, flags);
2958 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2959 "(expecting one of %#x %#x %#x %#x)\n",
2960 *(__u32 *)¶m->fp_lmd->lmd_lmm,
2961 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2962 LMV_USER_MAGIC, LMV_MAGIC_V1);
2967 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2973 fname = strrchr(path, '/');
2975 /* It should be a file (or other non-directory) */
2976 if (fname == NULL) {
2977 dname = (char *)malloc(2);
2981 fname = (char *)path;
2983 dname = (char *)malloc(fname - path + 1);
2986 strncpy(dname, path, fname - path);
2987 dname[fname - path] = '\0';
2991 fd = open(dname, O_RDONLY | O_NONBLOCK);
2998 strcpy((char *)lum, fname);
2999 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
3002 if (close(fd) == -1 && rc == 0)
3009 int llapi_file_lookup(int dirfd, const char *name)
3011 struct obd_ioctl_data data = { 0 };
3016 if (dirfd < 0 || name == NULL)
3019 data.ioc_version = OBD_IOCTL_VERSION;
3020 data.ioc_len = sizeof(data);
3021 data.ioc_inlbuf1 = (char *)name;
3022 data.ioc_inllen1 = strlen(name) + 1;
3024 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
3026 llapi_error(LLAPI_MSG_ERROR, rc,
3027 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
3032 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
3038 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
3039 * Return -1 or 1 if file timestamp does not or does match the given criteria
3040 * correspondingly. Return 0 if the MDS time is being checked and there are
3041 * attributes on OSTs and it is not yet clear if the timespamp matches.
3043 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
3044 * updated timestamps. */
3045 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
3050 /* Check if file is accepted. */
3051 if (param->fp_atime) {
3052 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
3053 param->fp_asign, param->fp_exclude_atime,
3060 if (param->fp_mtime) {
3061 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
3062 param->fp_msign, param->fp_exclude_mtime,
3067 /* If the previous check matches, but this one is not yet clear,
3068 * we should return 0 to do an RPC on OSTs. */
3073 if (param->fp_ctime) {
3074 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
3075 param->fp_csign, param->fp_exclude_ctime,
3080 /* If the previous check matches, but this one is not yet clear,
3081 * we should return 0 to do an RPC on OSTs. */
3090 * Check whether the stripes matches the indexes user provided
3094 static int check_obd_match(struct find_param *param)
3096 struct lov_user_ost_data_v1 *objects;
3097 struct lov_comp_md_v1 *comp_v1 = NULL;
3098 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3099 lstat_t *st = ¶m->fp_lmd->lmd_st;
3100 int i, j, k, count = 1;
3102 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
3105 if (!S_ISREG(st->st_mode))
3108 /* Only those files should be accepted, which have a
3109 * stripe on the specified OST. */
3110 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3111 comp_v1 = (struct lov_comp_md_v1 *)v1;
3112 count = comp_v1->lcm_entry_count;
3115 for (i = 0; i < count; i++) {
3117 v1 = lov_comp_entry(comp_v1, i);
3119 objects = lov_v1v3_objects(v1);
3121 for (j = 0; j < v1->lmm_stripe_count; j++) {
3122 for (k = 0; k < param->fp_num_obds; k++) {
3123 if (param->fp_obd_indexes[k] ==
3124 objects[j].l_ost_idx)
3125 return !param->fp_exclude_obd;
3130 return param->fp_exclude_obd;
3133 static int check_mdt_match(struct find_param *param)
3137 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
3140 /* FIXME: For striped dir, we should get stripe information and check */
3141 for (i = 0; i < param->fp_num_mdts; i++) {
3142 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
3143 return !param->fp_exclude_mdt;
3146 if (param->fp_exclude_mdt)
3153 * Check whether the obd is active or not, if it is
3154 * not active, just print the object affected by this
3157 static int print_failed_tgt(struct find_param *param, char *path, int type)
3159 struct obd_statfs stat_buf;
3160 struct obd_uuid uuid_buf;
3163 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
3166 memset(&stat_buf, 0, sizeof(struct obd_statfs));
3167 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
3168 ret = llapi_obd_statfs(path, type,
3169 param->fp_obd_index, &stat_buf,
3172 llapi_printf(LLAPI_MSG_NORMAL,
3173 "obd_uuid: %s failed %s ",
3174 param->fp_obd_uuid->uuid,
3181 static int find_check_stripe_size(struct find_param *param)
3183 struct lov_comp_md_v1 *comp_v1 = NULL;
3184 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3185 int ret, i, count = 1;
3187 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3188 comp_v1 = (struct lov_comp_md_v1 *)v1;
3189 count = comp_v1->lcm_entry_count;
3190 ret = param->fp_exclude_stripe_size ? 1 : -1;
3193 for (i = 0; i < count; i++) {
3195 v1 = lov_comp_entry(comp_v1, i);
3197 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_stripe_size,
3198 param->fp_stripe_size_sign,
3199 param->fp_exclude_stripe_size,
3200 param->fp_stripe_size_units, 0);
3201 /* If any stripe_size matches */
3209 static __u32 find_get_stripe_count(struct find_param *param)
3211 struct lov_comp_md_v1 *comp_v1 = NULL;
3212 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3214 __u32 stripe_count = 0;
3216 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3217 comp_v1 = (struct lov_comp_md_v1 *)v1;
3218 count = comp_v1->lcm_entry_count;
3221 for (i = 0; i < count; i++) {
3223 v1 = lov_comp_entry(comp_v1, i);
3224 stripe_count += v1->lmm_stripe_count;
3227 return stripe_count;
3230 #define LOV_PATTERN_INVALID 0xFFFFFFFF
3232 static int find_check_layout(struct find_param *param)
3234 struct lov_comp_md_v1 *comp_v1 = NULL;
3235 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3237 bool found = false, valid = false;
3239 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3240 comp_v1 = (struct lov_comp_md_v1 *)v1;
3241 count = comp_v1->lcm_entry_count;
3244 for (i = 0; i < count; i++) {
3246 v1 = lov_comp_entry(comp_v1, i);
3248 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
3252 if (v1->lmm_pattern & param->fp_layout) {
3261 if ((found && !param->fp_exclude_layout) ||
3262 (!found && param->fp_exclude_layout))
3268 static int find_check_pool(struct find_param *param)
3270 struct lov_comp_md_v1 *comp_v1 = NULL;
3271 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3272 struct lov_user_md_v3 *v3 = (void *)v1;
3276 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3277 comp_v1 = (struct lov_comp_md_v1 *)v1;
3278 count = comp_v1->lcm_entry_count;
3279 /* empty requested pool is taken as no pool search */
3280 if (count == 0 && param->fp_poolname[0] == '\0')
3284 for (i = 0; i < count; i++) {
3285 if (comp_v1 != NULL)
3286 v1 = lov_comp_entry(comp_v1, i);
3288 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
3289 (param->fp_poolname[0] == '\0')) ||
3290 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
3291 (strncmp(v3->lmm_pool_name,
3292 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
3293 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
3294 (strcmp(param->fp_poolname, "*") == 0))) {
3300 if ((found && !param->fp_exclude_pool) ||
3301 (!found && param->fp_exclude_pool))
3307 static int find_check_comp_options(struct find_param *param)
3309 struct lov_comp_md_v1 *comp_v1;
3310 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3311 struct lov_comp_md_entry_v1 *entry;
3314 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1) {
3315 if ((param->fp_check_comp_count &&
3316 !param->fp_exclude_comp_count) ||
3317 (param->fp_check_comp_flags &&
3318 !param->fp_exclude_comp_flags) ||
3319 (param->fp_check_comp_start &&
3320 !param->fp_exclude_comp_start) ||
3321 (param->fp_check_comp_end &&
3322 !param->fp_exclude_comp_end))
3328 comp_v1 = (struct lov_comp_md_v1 *)v1;
3330 if (param->fp_check_comp_count) {
3331 ret = find_value_cmp(comp_v1->lcm_entry_count,
3332 param->fp_comp_count,
3333 param->fp_comp_count_sign,
3334 param->fp_exclude_comp_count, 1, 0);
3339 if (comp_v1->lcm_entry_count == 0) {
3340 if ((param->fp_check_comp_flags &&
3341 !param->fp_exclude_comp_flags) ||
3342 (param->fp_check_comp_start &&
3343 !param->fp_exclude_comp_start) ||
3344 (param->fp_check_comp_end &&
3345 !param->fp_exclude_comp_end))
3349 if (param->fp_check_comp_flags) {
3350 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3351 entry = &comp_v1->lcm_entries[i];
3353 if (((entry->lcme_flags & param->fp_comp_flags) &&
3354 param->fp_exclude_comp_flags) ||
3355 (!(entry->lcme_flags & param->fp_comp_flags) &&
3356 !param->fp_exclude_comp_flags))
3360 /* If any flags matches */
3368 if (param->fp_check_comp_start) {
3369 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3370 entry = &comp_v1->lcm_entries[i];
3372 ret = find_value_cmp(entry->lcme_extent.e_start,
3373 param->fp_comp_start,
3374 param->fp_comp_start_sign,
3375 param->fp_exclude_comp_start,
3376 param->fp_comp_start_units, 0);
3377 /* If any extent start matches */
3385 if (param->fp_check_comp_end) {
3386 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3387 unsigned long long margin;
3388 entry = &comp_v1->lcm_entries[i];
3390 margin = entry->lcme_extent.e_end == LUSTRE_EOF ?
3391 0 : param->fp_comp_end_units;
3393 ret = find_value_cmp(entry->lcme_extent.e_end,
3395 param->fp_comp_end_sign,
3396 param->fp_exclude_comp_end, margin,
3398 /* If any extent end matches */
3409 static bool find_check_lmm_info(struct find_param *param)
3411 return param->fp_check_pool || param->fp_check_stripe_count ||
3412 param->fp_check_stripe_size || param->fp_check_layout ||
3413 param->fp_check_comp_count || param->fp_check_comp_end ||
3414 param->fp_check_comp_start || param->fp_check_comp_flags ||
3415 param->fp_check_projid;
3419 * Get file/directory project id.
3420 * by the open fd resides on.
3421 * Return 0 and project id on success, or -ve errno.
3423 static int fget_projid(int fd, int *projid)
3428 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
3432 *projid = fsx.fsx_projid;
3436 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
3437 void *data, struct dirent64 *de)
3439 struct find_param *param = (struct find_param *)data;
3440 DIR *dir = dirp == NULL ? NULL : *dirp;
3441 int decision = 1; /* 1 is accepted; -1 is rejected. */
3442 lstat_t *st = ¶m->fp_lmd->lmd_st;
3444 int checked_type = 0;
3446 __u32 stripe_count = 0;
3449 if (parent == NULL && dir == NULL)
3452 /* If a regular expression is presented, make the initial decision */
3453 if (param->fp_pattern != NULL) {
3454 char *fname = strrchr(path, '/');
3455 fname = (fname == NULL ? path : fname + 1);
3456 ret = fnmatch(param->fp_pattern, fname, 0);
3457 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
3458 (ret == 0 && param->fp_exclude_pattern))
3462 /* See if we can check the file type from the dirent. */
3463 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
3466 if (DTTOIF(de->d_type) == param->fp_type) {
3467 if (param->fp_exclude_type)
3470 if (!param->fp_exclude_type)
3477 /* Request MDS for the stat info if some of these parameters need
3478 * to be compared. */
3479 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
3480 param->fp_check_uid || param->fp_check_gid ||
3481 param->fp_atime || param->fp_mtime || param->fp_ctime ||
3482 param->fp_check_size || find_check_lmm_info(param) ||
3483 param->fp_check_mdt_count || param->fp_check_hash_type)
3486 if (param->fp_type != 0 && checked_type == 0)
3489 if (decision == 0) {
3490 if (param->fp_check_mdt_count || param->fp_check_hash_type) {
3491 param->fp_get_lmv = 1;
3492 ret = cb_get_dirstripe(path, dir, param);
3497 param->fp_lmd->lmd_lmm.lmm_magic = 0;
3498 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
3499 param->fp_lum_size);
3500 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
3501 find_check_lmm_info(param)) {
3502 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
3504 /* We need to "fake" the "use the default" values
3505 * since the lmm struct is zeroed out at this point. */
3506 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3507 lmm->lmm_pattern = 0xFFFFFFFF;
3509 ostid_set_seq(&lmm->lmm_oi,
3510 FID_SEQ_LOV_DEFAULT);
3511 lmm->lmm_stripe_size = 0;
3512 lmm->lmm_stripe_count = 0;
3513 lmm->lmm_stripe_offset = -1;
3515 if (ret == 0 && param->fp_mdt_uuid != NULL) {
3517 ret = llapi_file_fget_mdtidx(dirfd(dir),
3518 ¶m->fp_file_mdt_index);
3519 } else if (S_ISREG(st->st_mode)) {
3520 /* FIXME: we could get the MDT index from the
3521 * file's FID in lmd->lmd_lmm.lmm_oi without
3522 * opening the file, once we are sure that
3523 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
3524 * That would still be an ioctl() to map the
3525 * FID to the MDT, but not an open RPC. */
3526 fd = open(path, O_RDONLY);
3528 ret = llapi_file_fget_mdtidx(fd,
3529 ¶m->fp_file_mdt_index);
3534 /* For a special file, we assume it resides on
3535 * the same MDT as the parent directory. */
3536 ret = llapi_file_fget_mdtidx(dirfd(parent),
3537 ¶m->fp_file_mdt_index);
3548 stripe_count = find_get_stripe_count(param);
3552 if (param->fp_type && !checked_type) {
3553 if ((st->st_mode & S_IFMT) == param->fp_type) {
3554 if (param->fp_exclude_type)
3557 if (!param->fp_exclude_type)
3563 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
3564 if (lustre_fs && param->fp_got_uuids &&
3565 param->fp_dev != st->st_dev) {
3566 /* A lustre/lustre mount point is crossed. */
3567 param->fp_got_uuids = 0;
3568 param->fp_obds_printed = 0;
3569 param->fp_mdt_index = OBD_NOT_FOUND;
3570 param->fp_obd_index = OBD_NOT_FOUND;
3573 if (lustre_fs && !param->fp_got_uuids) {
3574 ret = setup_target_indexes(dir ? dir : parent, path,
3579 param->fp_dev = st->st_dev;
3580 } else if (!lustre_fs && param->fp_got_uuids) {
3581 /* A lustre/non-lustre mount point is crossed. */
3582 param->fp_got_uuids = 0;
3583 param->fp_mdt_index = OBD_NOT_FOUND;
3584 param->fp_obd_index = OBD_NOT_FOUND;
3588 if (param->fp_check_stripe_size) {
3589 decision = find_check_stripe_size(param);
3594 if (param->fp_check_stripe_count) {
3595 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
3596 param->fp_stripe_count_sign,
3597 param->fp_exclude_stripe_count, 1, 0);
3602 if (param->fp_check_mdt_count) {
3603 decision = find_value_cmp(
3604 param->fp_lmv_md->lum_stripe_count,
3605 param->fp_mdt_count,
3606 param->fp_mdt_count_sign,
3607 param->fp_exclude_mdt_count, 1, 0);
3612 if (param->fp_check_layout) {
3613 decision = find_check_layout(param);
3618 if (param->fp_check_hash_type) {
3621 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
3622 if ((found && param->fp_exclude_hash_type) ||
3623 (!found && !param->fp_exclude_hash_type)) {
3629 /* If an OBD UUID is specified but none matches, skip this file. */
3630 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
3631 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
3634 /* If an OST or MDT UUID is given, and some OST matches,
3636 if (param->fp_obd_index != OBD_NOT_FOUND ||
3637 param->fp_mdt_index != OBD_NOT_FOUND) {
3638 if (param->fp_obd_uuid) {
3639 if (check_obd_match(param)) {
3640 /* If no mdtuuid is given, we are done.
3641 * Otherwise, fall through to the mdtuuid
3643 if (!param->fp_mdt_uuid)
3650 if (param->fp_mdt_uuid) {
3651 if (check_mdt_match(param))
3658 if (param->fp_check_uid) {
3659 if (st->st_uid == param->fp_uid) {
3660 if (param->fp_exclude_uid)
3663 if (!param->fp_exclude_uid)
3668 if (param->fp_check_gid) {
3669 if (st->st_gid == param->fp_gid) {
3670 if (param->fp_exclude_gid)
3673 if (!param->fp_exclude_gid)
3678 if (param->fp_check_projid) {
3682 fd = open(path, O_RDONLY);
3685 ret = fget_projid(fd, &projid);
3690 if (projid == param->fp_projid) {
3691 if (param->fp_exclude_uid)
3694 if (!param->fp_exclude_projid)
3699 if (param->fp_check_pool) {
3700 decision = find_check_pool(param);
3705 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
3706 param->fp_check_comp_start || param->fp_check_comp_end) {
3707 decision = find_check_comp_options(param);
3712 /* Check the time on mds. */
3714 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
3717 for_mds = lustre_fs ?
3718 (S_ISREG(st->st_mode) && stripe_count) : 0;
3719 decision = find_time_check(st, param, for_mds);
3724 /* If file still fits the request, ask ost for updated info.
3725 The regular stat is almost of the same speed as some new
3726 'glimpse-size-ioctl'. */
3728 if (param->fp_check_size && S_ISREG(st->st_mode) && stripe_count)
3731 if (param->fp_check_size && S_ISDIR(st->st_mode))
3735 /* For regular files with the stripe the decision may have not
3736 * been taken yet if *time or size is to be checked. */
3737 if (param->fp_obd_index != OBD_NOT_FOUND)
3738 print_failed_tgt(param, path, LL_STATFS_LOV);
3740 if (param->fp_mdt_index != OBD_NOT_FOUND)
3741 print_failed_tgt(param, path, LL_STATFS_LMV);
3744 ret = fstat_f(dirfd(dir), st);
3745 else if (de != NULL)
3746 ret = fstatat_f(dirfd(parent), de->d_name, st,
3747 AT_SYMLINK_NOFOLLOW);
3749 ret = lstat_f(path, st);
3752 if (errno == ENOENT) {
3753 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
3754 "warning: %s: %s does not exist",
3759 llapi_error(LLAPI_MSG_ERROR, ret,
3760 "%s: IOC_LOV_GETINFO on %s failed",
3766 /* Check the time on osc. */
3767 decision = find_time_check(st, param, 0);
3772 if (param->fp_check_size)
3773 decision = find_value_cmp(st->st_size, param->fp_size,
3774 param->fp_size_sign,
3775 param->fp_exclude_size,
3776 param->fp_size_units, 0);
3778 if (decision != -1) {
3779 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
3780 if (param->fp_zero_end)
3781 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
3783 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3788 /* Do not get down anymore? */
3789 if (param->fp_depth == param->fp_max_depth) {
3800 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
3801 void *param_data, struct dirent64 *de)
3803 struct find_param *param = (struct find_param *)param_data;
3804 DIR *tmp_parent = parent;
3805 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3807 struct obd_ioctl_data data = { 0 };
3814 if (parent == NULL && dirp == NULL)
3820 if (parent == NULL) {
3821 tmp_parent = opendir_parent(path);
3822 if (tmp_parent == NULL) {
3825 llapi_error(LLAPI_MSG_ERROR, ret,
3826 "can not open %s", path);
3831 fd = dirfd(tmp_parent);
3833 path_copy = strdup(path);
3834 filename = basename(path_copy);
3835 data.ioc_inlbuf1 = (char *)filename;
3836 data.ioc_inllen1 = strlen(filename) + 1;
3837 data.ioc_inlbuf2 = (char *)¶m->fp_mdt_index;
3838 data.ioc_inllen2 = sizeof(param->fp_mdt_index);
3839 ret = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3841 llapi_error(LLAPI_MSG_ERROR, ret,
3842 "llapi_obd_statfs: error packing ioctl data");
3847 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
3849 if (errno == EBUSY && !retry) {
3850 /* because migrate may not be able to lock all involved
3851 * objects in order, for some of them it try lock, while
3852 * there may be conflicting COS locks and cause migrate
3853 * fail with EBUSY, hope a sync() could cause
3854 * transaction commit and release these COS locks. */
3860 fprintf(stderr, "%s migrate failed: %s (%d)\n",
3861 path, strerror(-ret), ret);
3863 } else if (param->fp_verbose & VERBOSE_DETAIL) {
3864 fprintf(stdout, "migrate %s to MDT%d\n",
3865 path, param->fp_mdt_index);
3870 /* If the directory is being migration, we need
3871 * close the directory after migration,
3872 * so the old directory cache will be cleanup
3873 * on the client side, and re-open to get the
3874 * new directory handle */
3875 *dirp = opendir(path);
3876 if (*dirp == NULL) {
3878 llapi_error(LLAPI_MSG_ERROR, ret,
3879 "%s: Failed to open '%s'", __func__, path);
3884 closedir(tmp_parent);
3891 int llapi_migrate_mdt(char *path, struct find_param *param)
3893 return param_callback(path, cb_migrate_mdt_init, cb_common_fini, param);
3896 int llapi_mv(char *path, struct find_param *param)
3898 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
3899 static bool printed;
3902 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
3903 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
3907 return llapi_migrate_mdt(path, param);
3910 int llapi_find(char *path, struct find_param *param)
3912 return param_callback(path, cb_find_init, cb_common_fini, param);
3916 * Get MDT number that the file/directory inode referenced
3917 * by the open fd resides on.
3918 * Return 0 and mdtidx on success, or -ve errno.
3920 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
3922 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
3927 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
3928 struct dirent64 *de)
3930 struct find_param *param = (struct find_param *)data;
3931 DIR *d = dirp == NULL ? NULL : *dirp;
3935 if (parent == NULL && d == NULL)
3939 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3940 } else /* if (parent) */ {
3943 fd = open(path, O_RDONLY | O_NOCTTY);
3945 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3953 if (ret == -ENODATA) {
3954 if (!param->fp_obd_uuid)
3955 llapi_printf(LLAPI_MSG_NORMAL,
3956 "'%s' has no stripe info\n", path);
3958 } else if (ret == -ENOENT) {
3959 llapi_error(LLAPI_MSG_WARN, ret,
3960 "warning: %s: '%s' does not exist",
3963 } else if (ret == -ENOTTY) {
3964 llapi_error(LLAPI_MSG_ERROR, ret,
3965 "%s: '%s' not on a Lustre fs",
3968 llapi_error(LLAPI_MSG_ERROR, ret,
3969 "error: %s: '%s' failed get_mdtidx",
3975 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
3976 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3978 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3982 /* Do not go down anymore? */
3983 if (param->fp_depth == param->fp_max_depth)
3991 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
3992 struct dirent64 *de)
3994 struct find_param *param = (struct find_param *)data;
3995 DIR *d = dirp == NULL ? NULL : *dirp;
3998 if (parent == NULL && d == NULL)
4001 if (param->fp_obd_uuid) {
4002 param->fp_quiet = 1;
4003 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
4009 if (param->fp_get_lmv || param->fp_get_default_lmv) {
4010 ret = cb_get_dirstripe(path, d, param);
4012 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
4013 (void *)¶m->fp_lmd->lmd_lmm);
4016 } else if (parent && !param->fp_get_lmv && !param->fp_get_default_lmv) {
4017 char *fname = strrchr(path, '/');
4018 fname = (fname == NULL ? path : fname + 1);
4020 strlcpy((char *)¶m->fp_lmd->lmd_lmm, fname,
4021 param->fp_lum_size);
4023 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
4024 (void *)¶m->fp_lmd->lmd_lmm);
4030 if (errno == ENODATA && d != NULL) {
4031 /* We need to "fake" the "use the default" values
4032 * since the lmm struct is zeroed out at this point.
4033 * The magic needs to be set in order to satisfy
4034 * a check later on in the code path.
4035 * The object_seq needs to be set for the "(Default)"
4036 * prefix to be displayed. */
4037 if (param->fp_get_default_lmv) {
4038 struct lmv_user_md *lum = param->fp_lmv_md;
4040 lum->lum_magic = LMV_USER_MAGIC;
4041 lum->lum_stripe_count = 0;
4042 lum->lum_stripe_offset = -1;
4044 } else if (param->fp_get_lmv) {
4045 struct lmv_user_md *lum = param->fp_lmv_md;
4048 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
4051 lum->lum_magic = LMV_MAGIC_V1;
4052 lum->lum_stripe_count = 0;
4053 lum->lum_stripe_offset = mdtidx;
4056 struct lov_user_md *lmm =
4057 ¶m->fp_lmd->lmd_lmm;
4059 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4061 ostid_set_seq(&lmm->lmm_oi,
4062 FID_SEQ_LOV_DEFAULT);
4063 lmm->lmm_stripe_count = 0;
4064 lmm->lmm_stripe_size = 0;
4065 lmm->lmm_stripe_offset = -1;
4068 } else if (errno == ENODATA && parent != NULL) {
4069 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
4070 llapi_printf(LLAPI_MSG_NORMAL,
4071 "%s has no stripe info\n", path);
4073 } else if (errno == ENOENT) {
4074 llapi_error(LLAPI_MSG_WARN, -ENOENT,
4075 "warning: %s: %s does not exist",
4078 } else if (errno == ENOTTY) {
4080 llapi_error(LLAPI_MSG_ERROR, ret,
4081 "%s: '%s' not on a Lustre fs?",
4086 llapi_error(LLAPI_MSG_ERROR, ret,
4087 "error: %s: %s failed for %s",
4088 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
4089 "IOC_MDC_GETFILESTRIPE", path);
4096 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
4097 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
4100 /* Do not get down anymore? */
4101 if (param->fp_depth == param->fp_max_depth)
4109 int llapi_getstripe(char *path, struct find_param *param)
4111 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
4112 cb_get_mdt_index : cb_getstripe,
4113 cb_common_fini, param);
4116 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
4117 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
4119 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
4121 struct obd_ioctl_data data = { 0 };
4124 data.ioc_inlbuf1 = (char *)&type;
4125 data.ioc_inllen1 = sizeof(__u32);
4126 data.ioc_inlbuf2 = (char *)&index;
4127 data.ioc_inllen2 = sizeof(__u32);
4128 data.ioc_pbuf1 = (char *)stat_buf;
4129 data.ioc_plen1 = sizeof(struct obd_statfs);
4130 data.ioc_pbuf2 = (char *)uuid_buf;
4131 data.ioc_plen2 = sizeof(struct obd_uuid);
4133 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
4135 llapi_error(LLAPI_MSG_ERROR, rc,
4136 "llapi_obd_statfs: error packing ioctl data");
4140 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
4142 return rc < 0 ? -errno : 0;
4145 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
4146 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
4151 fd = open(path, O_RDONLY);
4154 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
4156 /* If we can't even open a file on the filesystem (e.g. with
4157 * -ESHUTDOWN), force caller to exit or it will loop forever. */
4161 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
4168 #define MAX_STRING_SIZE 128
4170 int llapi_ping(char *obd_type, char *obd_name)
4176 rc = cfs_get_param_paths(&path, "%s/%s/ping",
4177 obd_type, obd_name);
4181 fd = open(path.gl_pathv[0], O_WRONLY);
4184 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
4189 /* The purpose is to send a byte as a ping, whatever this byte is. */
4190 /* coverity[uninit_use_in_call] */
4191 rc = write(fd, buf, 1);
4199 cfs_free_param_data(&path);
4203 int llapi_target_iterate(int type_num, char **obd_type,
4204 void *args, llapi_cb_t cb)
4206 char buf[MAX_STRING_SIZE];
4211 rc = cfs_get_param_paths(¶m, "devices");
4215 fp = fopen(param.gl_pathv[0], "r");
4218 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
4223 while (fgets(buf, sizeof(buf), fp) != NULL) {
4224 char *obd_type_name = NULL;
4225 char *obd_name = NULL;
4226 char *obd_uuid = NULL;
4228 struct obd_statfs osfs_buffer;
4230 while(bufp[0] == ' ')
4233 for(i = 0; i < 3; i++) {
4234 obd_type_name = strsep(&bufp, " ");
4236 obd_name = strsep(&bufp, " ");
4237 obd_uuid = strsep(&bufp, " ");
4239 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
4241 for (i = 0; i < type_num; i++) {
4242 if (strcmp(obd_type_name, obd_type[i]) != 0)
4245 cb(obd_type_name, obd_name, obd_uuid, args);
4250 cfs_free_param_data(¶m);
4254 static void do_target_check(char *obd_type_name, char *obd_name,
4255 char *obd_uuid, void *args)
4259 rc = llapi_ping(obd_type_name, obd_name);
4260 if (rc == ENOTCONN) {
4261 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
4263 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
4265 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
4269 int llapi_target_check(int type_num, char **obd_type, char *dir)
4271 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
4274 #undef MAX_STRING_SIZE
4276 /* Is this a lustre fs? */
4277 int llapi_is_lustre_mnttype(const char *type)
4279 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
4282 /* Is this a lustre client fs? */
4283 int llapi_is_lustre_mnt(struct mntent *mnt)
4285 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
4286 strstr(mnt->mnt_fsname, ":/") != NULL);
4289 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
4291 char fsname[PATH_MAX + 1];
4295 rc = llapi_search_fsname(mnt, fsname);
4297 llapi_err_noerrno(LLAPI_MSG_ERROR,
4298 "'%s' isn't on Lustre filesystem", mnt);
4302 root = open(mnt, O_RDONLY | O_DIRECTORY);
4305 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4309 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
4317 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
4318 * format must have %s%s, buf must be > 16
4319 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
4320 * then buf = "lustre-MDT0000"
4322 static int get_mdtname(char *name, char *format, char *buf)
4324 char suffix[]="-MDT0000";
4325 int len = strlen(name);
4327 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
4328 name[len - 5] = '\0';
4333 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
4336 /* Not enough room to add suffix */
4337 llapi_err_noerrno(LLAPI_MSG_ERROR,
4338 "MDT name too long |%s|", name);
4343 return sprintf(buf, format, name, suffix);
4346 /** ioctl on filsystem root, with mdtindex sent as data
4347 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
4348 * \param mdtidxp pointer to integer within data to be filled in with the
4349 * mdt index (0 if no mdt is specified). NULL won't be filled.
4351 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
4359 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
4360 Open root and parse mdt index. */
4361 if (mdtname[0] == '/') {
4363 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
4364 (char *)mdtname, -1);
4366 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
4368 ptr = fsname + strlen(fsname) - 8;
4370 index = strtol(ptr + 4, NULL, 10);
4371 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
4375 llapi_err_noerrno(LLAPI_MSG_ERROR,
4376 "Can't open %s: %d\n", mdtname, rc);
4383 rc = ioctl(fd, opc, data);
4392 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
4393 int buflen, long long *recno, int *linkno)
4395 const char *fidstr_orig = fidstr;
4397 struct getinfo_fid2path *gf;
4400 while (*fidstr == '[')
4403 sscanf(fidstr, SFID, RFID(&fid));
4404 if (!fid_is_sane(&fid)) {
4405 llapi_err_noerrno(LLAPI_MSG_ERROR,
4406 "bad FID format '%s', should be [seq:oid:ver]"
4407 " (e.g. "DFID")\n", fidstr_orig,
4408 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
4412 gf = malloc(sizeof(*gf) + buflen);
4417 gf->gf_recno = *recno;
4418 gf->gf_linkno = *linkno;
4419 gf->gf_pathlen = buflen;
4421 /* Take path or fsname */
4422 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
4426 memcpy(buf, gf->gf_u.gf_path, gf->gf_pathlen);
4427 if (buf[0] == '\0') { /* ROOT path */
4431 *recno = gf->gf_recno;
4432 *linkno = gf->gf_linkno;
4439 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
4442 struct lustre_mdt_attrs *lma;
4446 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
4448 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
4451 lma = (struct lustre_mdt_attrs *)buf;
4452 fid_le_to_cpu(fid, &lma->lma_self_fid);
4456 int llapi_get_mdt_index_by_fid(int fd, const lustre_fid *fid,
4461 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
4470 int llapi_fd2fid(const int fd, lustre_fid *fid)
4474 memset(fid, 0, sizeof(*fid));
4476 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
4477 if (rc == -EINVAL || rc == -ENOTTY)
4478 rc = fid_from_lma(NULL, fd, fid);
4483 int llapi_path2fid(const char *path, lustre_fid *fid)
4487 memset(fid, 0, sizeof(*fid));
4488 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4490 if (errno == ELOOP || errno == ENXIO)
4491 return fid_from_lma(path, -1, fid);
4495 rc = llapi_fd2fid(fd, fid);
4496 if (rc == -EINVAL || rc == -ENOTTY)
4497 rc = fid_from_lma(path, -1, fid);
4503 int llapi_fd2parent(int fd, unsigned int linkno, lustre_fid *parent_fid,
4504 char *name, size_t name_size)
4506 struct getparent *gp;
4509 gp = malloc(sizeof(*gp) + name_size);
4513 gp->gp_linkno = linkno;
4514 gp->gp_name_size = name_size;
4516 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
4522 *parent_fid = gp->gp_fid;
4524 strncpy(name, gp->gp_name, name_size);
4525 name[name_size - 1] = '\0';
4532 int llapi_path2parent(const char *path, unsigned int linkno,
4533 lustre_fid *parent_fid, char *name, size_t name_size)
4538 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4542 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
4547 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4552 root = open(mnt, O_RDONLY | O_DIRECTORY);
4555 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4559 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
4562 llapi_error(LLAPI_MSG_ERROR, rc,
4563 "ioctl on %s for getting connect flags failed", mnt);
4570 * Get a 64-bit value representing the version of file data pointed by fd.
4572 * Each write or truncate, flushed on OST, will change this value. You can use
4573 * this value to verify if file data was modified. This only checks the file
4574 * data, not metadata.
4576 * \param flags 0: no flush pages, usually used it the process has already
4578 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
4580 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
4581 * pages from clients.
4583 * \retval 0 on success.
4584 * \retval -errno on error.
4586 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4589 struct ioc_data_version idv;
4591 idv.idv_flags = flags;
4593 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4597 *data_version = idv.idv_version;
4603 * Create a file without any name open it for read/write
4605 * - file is created as if it were a standard file in the given \a directory
4606 * - file does not appear in \a directory and mtime does not change because
4607 * the filename is handled specially by the Lustre MDS.
4608 * - file is removed at final close
4609 * - file modes are rw------- since it doesn't make sense to have a read-only
4610 * or write-only file that cannot be opened again.
4611 * - if user wants another mode it must use fchmod() on the open file, no
4612 * security problems arise because it cannot be opened by another process.
4614 * \param[in] directory directory from which to inherit layout/MDT idx
4615 * \param[in] idx MDT index on which the file is created,
4616 * \a idx == -1 means no specific MDT is requested
4617 * \param[in] open_flags standard open(2) flags
4619 * \retval 0 on success.
4620 * \retval -errno on error.
4622 int llapi_create_volatile_idx(char *directory, int idx, int open_flags)
4624 char file_path[PATH_MAX];
4625 char filename[PATH_MAX];
4626 int saved_errno = errno;
4634 snprintf(filename, sizeof(filename),
4635 LUSTRE_VOLATILE_HDR"::%.4X", rnumber);
4637 snprintf(filename, sizeof(filename),
4638 LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, rnumber);
4640 rc = snprintf(file_path, sizeof(file_path),
4641 "%s/%s", directory, filename);
4642 if (rc >= sizeof(file_path))
4645 fd = open(file_path,
4646 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | open_flags,
4648 } while (fd < 0 && errno == EEXIST);
4651 llapi_error(LLAPI_MSG_ERROR, errno,
4652 "Cannot create volatile file '%s' in '%s'",
4653 filename + LUSTRE_VOLATILE_HDR_LEN,
4658 /* Unlink file in case this wasn't a Lustre filesystem and the
4659 * magic volatile filename wasn't handled as intended. The
4660 * effect is the same. If volatile open was supported then we
4661 * expect unlink() to return -ENOENT. */
4662 (void)unlink(file_path);
4664 /* Since we are returning successfully we restore errno (and
4665 * mask out possible EEXIST from open() and ENOENT from
4667 errno = saved_errno;
4673 * Swap the layouts between 2 file descriptors
4674 * the 2 files must be open for writing
4675 * first fd received the ioctl, second fd is passed as arg
4676 * this is assymetric but avoid use of root path for ioctl
4678 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
4679 int gid, __u64 flags)
4681 struct lustre_swap_layouts lsl;
4686 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4687 rc = fstat(fd1, &st1);
4691 rc = fstat(fd2, &st2);
4696 lsl.sl_flags = flags;
4700 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4704 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4705 struct timeval tv1[2];
4706 struct timeval tv2[2];
4708 memset(tv1, 0, sizeof(tv1));
4709 memset(tv2, 0, sizeof(tv2));
4711 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
4712 tv1[0].tv_sec = st1.st_atime;
4713 tv2[0].tv_sec = st2.st_atime;
4715 tv1[0].tv_sec = st2.st_atime;
4716 tv2[0].tv_sec = st1.st_atime;
4719 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
4720 tv1[1].tv_sec = st1.st_mtime;
4721 tv2[1].tv_sec = st2.st_mtime;
4723 tv1[1].tv_sec = st2.st_mtime;
4724 tv2[1].tv_sec = st1.st_mtime;
4727 rc = futimes(fd1, tv1);
4731 rc = futimes(fd2, tv2);
4739 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4746 while (grp_id == 0);
4748 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
4756 * Swap the layouts between 2 files
4757 * the 2 files are open in write
4759 int llapi_swap_layouts(const char *path1, const char *path2,
4760 __u64 dv1, __u64 dv2, __u64 flags)
4764 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4767 llapi_error(LLAPI_MSG_ERROR, rc,
4768 "error: cannot open '%s' for write", path1);
4772 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4775 llapi_error(LLAPI_MSG_ERROR, rc,
4776 "error: cannot open '%s' for write", path2);
4780 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4782 llapi_error(LLAPI_MSG_ERROR, rc,
4783 "error: cannot swap layout between '%s' and '%s'",
4794 * Attempt to open a file with Lustre file identifier \a fid
4795 * and return an open file descriptor.
4797 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
4798 * \param[in] fid Lustre file identifier of file to open
4799 * \param[in] flags open() flags
4801 * \retval non-negative file descriptor on successful open
4802 * \retval -1 if an error occurred
4804 int llapi_open_by_fid(const char *lustre_dir, const lustre_fid *fid, int flags)
4806 char mntdir[PATH_MAX];
4807 char path[PATH_MAX];
4810 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
4814 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
4815 return open(path, flags);
4821 * \param fd File to lock.
4822 * \param gid Group Identifier.
4824 * \retval 0 on success.
4825 * \retval -errno on failure.
4827 int llapi_group_lock(int fd, int gid)
4831 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
4834 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
4842 * \param fd File to unlock.
4843 * \param gid Group Identifier.
4845 * \retval 0 on success.
4846 * \retval -errno on failure.
4848 int llapi_group_unlock(int fd, int gid)
4852 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
4855 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");