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,
2201 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2202 struct lov_user_ost_data_v1 *objects,
2203 int verbose, int depth, char *pool_name,
2204 enum lov_dump_flags flags)
2206 bool is_dir = flags & LDF_IS_DIR;
2207 bool is_raw = flags & LDF_IS_RAW;
2208 bool indent = flags & LDF_INDENT;
2209 bool skip_objs = flags & LDF_SKIP_OBJS;
2210 char *prefix = is_dir ? "" : "lmm_";
2211 char *separator = "";
2212 char *space = indent ? " " : "";
2215 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2216 lmm_oi_set_seq(&lum->lmm_oi, 0);
2217 if (!indent && (verbose & VERBOSE_DETAIL))
2218 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2221 if (!indent && depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2222 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2224 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2225 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2226 space, prefix, lum->lmm_magic);
2227 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2229 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2230 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2232 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2234 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2235 if (verbose & ~VERBOSE_DFID)
2236 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2238 /* This needs a bit of hand-holding since old 1.x lmm_oi
2239 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2240 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2241 * a real FID. Ideally the 2.x code would have stored this
2242 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2243 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2244 * worked properly (especially since IGIF FIDs use mds_inum as
2245 * the FID SEQ), but unfortunately that didn't happen.
2247 * Print it to look like an IGIF FID, even though the fields
2248 * are reversed on disk, so that it makes sense to userspace.
2250 * Don't use ostid_id() and ostid_seq(), since they assume the
2251 * oi_fid fields are in the right order. This is why there are
2252 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2254 * For newer layout types hopefully this will be a real FID. */
2255 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2256 lmm_oi_seq(&lum->lmm_oi) == 0 ?
2257 lmm_oi_id(&lum->lmm_oi) :
2258 lmm_oi_seq(&lum->lmm_oi),
2259 lmm_oi_seq(&lum->lmm_oi) == 0 ?
2260 0 : (__u32)lmm_oi_id(&lum->lmm_oi),
2261 (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32));
2264 if (verbose & VERBOSE_COUNT) {
2265 if (verbose & ~VERBOSE_COUNT)
2266 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2269 if (!is_raw && lum->lmm_stripe_count == 0) {
2270 unsigned int scount;
2271 rc = sattr_cache_get_defaults(NULL, path,
2275 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2278 llapi_error(LLAPI_MSG_ERROR, rc,
2279 "Cannot determine default"
2282 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2283 lum->lmm_stripe_count ==
2284 (typeof(lum->lmm_stripe_count))(-1)
2285 ? -1 : lum->lmm_stripe_count);
2288 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2289 (__s16)lum->lmm_stripe_count);
2291 separator = is_dir ? " " : "\n";
2294 if (verbose & VERBOSE_SIZE) {
2295 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2296 if (verbose & ~VERBOSE_SIZE)
2297 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2299 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2301 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2304 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2306 llapi_error(LLAPI_MSG_ERROR, rc,
2307 "Cannot determine default"
2310 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2311 lum->lmm_stripe_size);
2313 separator = is_dir ? " " : "\n";
2316 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2317 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2318 if (verbose & ~VERBOSE_LAYOUT)
2319 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2321 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2325 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2326 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2327 if (verbose & ~VERBOSE_GENERATION)
2328 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2330 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2331 (int)lum->lmm_layout_gen);
2335 if (verbose & VERBOSE_OFFSET) {
2336 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2337 if (verbose & ~VERBOSE_OFFSET)
2338 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2340 if (is_dir || skip_objs)
2341 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2342 lum->lmm_stripe_offset ==
2343 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2344 lum->lmm_stripe_offset);
2346 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2347 objects[0].l_ost_idx);
2348 separator = is_dir ? " " : "\n";
2351 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2352 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2353 if (verbose & ~VERBOSE_POOL)
2354 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2356 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2357 separator = is_dir ? " " : "\n";
2360 if (strlen(separator) != 0)
2361 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2364 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2365 struct lov_user_ost_data_v1 *objects,
2366 char *path, int obdindex, int depth,
2367 int header, enum lov_dump_flags flags)
2369 bool is_dir = flags & LDF_IS_DIR;
2370 bool indent = flags & LDF_INDENT;
2371 bool skip_objs = flags & LDF_SKIP_OBJS;
2372 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2375 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2376 if (obdindex == objects[i].l_ost_idx) {
2386 lov_dump_user_lmm_header(lum, path, objects, header, depth, pool_name,
2389 if (!is_dir && !skip_objs && (header & VERBOSE_OBJID) &&
2390 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2391 char *space = " - ";
2394 llapi_printf(LLAPI_MSG_NORMAL,
2395 "%6slmm_objects:\n", " ");
2397 llapi_printf(LLAPI_MSG_NORMAL,
2398 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2400 for (i = 0; i < lum->lmm_stripe_count; i++) {
2401 int idx = objects[i].l_ost_idx;
2402 long long oid = ostid_id(&objects[i].l_ost_oi);
2403 long long gr = ostid_seq(&objects[i].l_ost_oi);
2405 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2409 struct lu_fid fid = { 0 };
2411 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2412 llapi_printf(LLAPI_MSG_NORMAL,
2413 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2414 space, i, idx, PFID(&fid));
2417 sprintf(fmt, "%s%s%s\n",
2418 "\t%6u\t%14llu\t%#13llx\t",
2419 (fid_seq_is_rsvd(gr) ||
2420 fid_seq_is_mdt0(gr)) ?
2421 "%14llu" : "%#14llx", "%s");
2422 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2424 obdindex == idx ? " *" : "");
2427 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2431 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2432 char *path, int obdindex, int depth, int verbose)
2434 struct lmv_user_mds_data *objects = lum->lum_objects;
2435 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2436 int i, obdstripe = 0;
2437 char *separator = "";
2439 if (obdindex != OBD_NOT_FOUND) {
2440 if (lum->lum_stripe_count == 0) {
2441 if (obdindex == lum->lum_stripe_offset)
2444 for (i = 0; i < lum->lum_stripe_count; i++) {
2445 if (obdindex == objects[i].lum_mds) {
2446 llapi_printf(LLAPI_MSG_NORMAL,
2461 /* show all information default */
2463 if (lum->lum_magic == LMV_USER_MAGIC)
2464 verbose = VERBOSE_POOL | VERBOSE_COUNT |
2465 VERBOSE_OFFSET | VERBOSE_HASH_TYPE;
2467 verbose = VERBOSE_OBJID;
2470 if (depth && path && ((verbose != VERBOSE_OBJID)))
2471 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2473 if (verbose & VERBOSE_COUNT) {
2474 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2475 if (verbose & ~VERBOSE_COUNT)
2476 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2477 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2478 (int)lum->lum_stripe_count);
2479 if (verbose & VERBOSE_OFFSET)
2485 if (verbose & VERBOSE_OFFSET) {
2486 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2487 if (verbose & ~VERBOSE_OFFSET)
2488 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2489 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2490 (int)lum->lum_stripe_offset);
2491 if (verbose & VERBOSE_HASH_TYPE)
2497 if (verbose & VERBOSE_HASH_TYPE) {
2498 unsigned int type = lum->lum_hash_type;
2500 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2501 if (verbose & ~VERBOSE_HASH_TYPE)
2502 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
2503 if (type < LMV_HASH_TYPE_MAX)
2504 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2505 mdt_hash_name[type]);
2507 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2512 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2513 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2514 if (obdstripe == 1 && lum->lum_stripe_count > 0)
2515 llapi_printf(LLAPI_MSG_NORMAL,
2516 "mdtidx\t\t FID[seq:oid:ver]\n");
2517 for (i = 0; i < lum->lum_stripe_count; i++) {
2518 int idx = objects[i].lum_mds;
2519 struct lu_fid *fid = &objects[i].lum_fid;
2520 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2521 llapi_printf(LLAPI_MSG_NORMAL,
2522 "%6u\t\t "DFID"\t\t%s\n",
2524 obdindex == idx ? " *" : "");
2529 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
2530 pool_name[0] != '\0') {
2531 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2532 if (verbose & ~VERBOSE_POOL)
2533 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2535 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2539 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
2540 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2543 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
2544 enum lov_dump_flags flags)
2546 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
2547 int depth = param->fp_max_depth;
2548 int verbose = param->fp_verbose;
2550 if (depth && path && ((verbose != VERBOSE_OBJID) ||
2551 !(flags & LDF_IS_DIR)))
2552 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2554 if (verbose & VERBOSE_DETAIL) {
2555 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
2556 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
2557 " ", comp_v1->lcm_magic);
2558 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
2559 " ", comp_v1->lcm_size);
2560 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_flags: %u\n",
2561 " ", comp_v1->lcm_flags);
2564 if (verbose & VERBOSE_GENERATION) {
2565 if (verbose & ~VERBOSE_GENERATION)
2566 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
2568 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
2571 if (verbose & VERBOSE_COMP_COUNT) {
2572 if (verbose & ~VERBOSE_COMP_COUNT)
2573 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
2575 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
2576 comp_v1->lcm_entry_count);
2579 if (verbose & VERBOSE_DETAIL)
2580 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
2583 static void lov_dump_comp_v1_entry(struct find_param *param,
2584 enum lov_dump_flags flags, int index)
2586 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
2587 struct lov_comp_md_entry_v1 *entry;
2588 char *separator = "";
2589 int verbose = param->fp_verbose;
2591 entry = &comp_v1->lcm_entries[index];
2593 if (verbose & VERBOSE_COMP_ID) {
2594 if (verbose & VERBOSE_DETAIL)
2595 llapi_printf(LLAPI_MSG_NORMAL,
2596 "%slcme_id: ", " - ");
2597 else if (verbose & ~VERBOSE_COMP_ID)
2598 llapi_printf(LLAPI_MSG_NORMAL,
2599 "%4slcme_id: ", " ");
2600 if (!(flags & LDF_IS_DIR))
2601 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
2603 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
2607 if (verbose & VERBOSE_COMP_FLAGS) {
2608 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2609 if (verbose & ~VERBOSE_COMP_FLAGS)
2610 llapi_printf(LLAPI_MSG_NORMAL,
2611 "%4slcme_flags: ", " ");
2612 llapi_printf(LLAPI_MSG_NORMAL, "%#x", entry->lcme_flags);
2616 if (verbose & VERBOSE_COMP_START) {
2617 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2618 if (verbose & ~VERBOSE_COMP_START)
2619 llapi_printf(LLAPI_MSG_NORMAL,
2620 "%4slcme_extent.e_start: ", " ");
2621 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2622 entry->lcme_extent.e_start);
2626 if (verbose & VERBOSE_COMP_END) {
2627 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2628 if (verbose & ~VERBOSE_COMP_END)
2629 llapi_printf(LLAPI_MSG_NORMAL,
2630 "%4slcme_extent.e_end: ", " ");
2631 if (entry->lcme_extent.e_end == LUSTRE_EOF)
2632 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
2634 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2635 entry->lcme_extent.e_end);
2639 if (verbose & VERBOSE_DETAIL) {
2640 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2641 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
2642 " ", entry->lcme_offset);
2643 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
2644 " ", entry->lcme_size);
2645 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
2647 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2651 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2652 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2654 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2655 * The table below gives the answers for the specified parameters (value and
2656 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2657 * --------------------------------------
2658 * 1 | file > limit; sign > 0 | -1 / -1 |
2659 * 2 | file = limit; sign > 0 | -1 / -1 |
2660 * 3 | file < limit; sign > 0 | ? / 1 |
2661 * 4 | file > limit; sign = 0 | -1 / -1 |
2662 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2663 * 6 | file < limit; sign = 0 | ? / -1 |
2664 * 7 | file > limit; sign < 0 | 1 / 1 |
2665 * 8 | file = limit; sign < 0 | ? / -1 |
2666 * 9 | file < limit; sign < 0 | ? / -1 |
2667 * --------------------------------------
2668 * Note: 5th actually means that the value is within the interval
2669 * (limit - margin, limit]. */
2670 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2671 int sign, int negopt, unsigned long long margin,
2677 /* Drop the fraction of margin (of days). */
2678 if (file + margin <= limit)
2680 } else if (sign == 0) {
2681 if (file <= limit && file + margin > limit)
2683 else if (file + margin <= limit)
2685 } else if (sign < 0) {
2692 return negopt ? ~ret + 1 : ret;
2695 static inline struct lov_user_md *
2696 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
2698 return (struct lov_user_md *)((char *)comp_v1 +
2699 comp_v1->lcm_entries[ent_idx].lcme_offset);
2702 static inline struct lov_user_ost_data_v1 *
2703 lov_v1v3_objects(struct lov_user_md *v1)
2705 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
2706 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
2708 return v1->lmm_objects;
2712 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
2714 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
2715 strlcpy(pool_name, ((struct lov_user_md_v3 *)v1)->lmm_pool_name,
2718 pool_name[0] = '\0';
2722 * An example of "getstripe -v" for a two components PFL file:
2725 * lcm_magic: 0x0BD60BD0
2729 * lcm_entry_count: 2
2733 * lcme_extent.e_start: 0
2734 * lcme_extent.e_end: 1048576
2738 * lmm_magic: 0x0BD10BD0
2739 * lmm_seq: 0x200000401
2740 * lmm_object_id: 0x1
2741 * lmm_fid: [0x200000401:0x1:0x0]
2742 * lmm_stripe_count: 1
2743 * lmm_stripe_size: 1048576
2746 * lmm_stripe_offset: 0
2748 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
2752 * lcme_extent.e_start: 1048576
2753 * lcme_extent.e_end: EOF
2757 * lmm_magic: 0x0BD10BD0
2758 * lmm_seq: 0x200000401
2759 * lmm_object_id: 0x1
2760 * lmm_fid: [0x200000401:0x1:0x0]
2761 * lmm_stripe_count: 2
2762 * lmm_stripe_size: 1048576
2765 * lmm_stripe_offset: 1
2767 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
2768 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
2770 static void lov_dump_comp_v1(struct find_param *param, char *path,
2771 enum lov_dump_flags flags)
2773 struct lov_comp_md_entry_v1 *entry;
2774 struct lov_user_ost_data_v1 *objects;
2775 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
2776 struct lov_user_md_v1 *v1;
2777 char pool_name[LOV_MAXPOOLNAME + 1];
2778 int obdindex = param->fp_obd_index;
2779 int i, j, match, obdstripe = 0;
2781 if (obdindex != OBD_NOT_FOUND) {
2782 for (i = 0; !(flags & LDF_IS_DIR) &&
2783 i < comp_v1->lcm_entry_count; i++) {
2784 if (!(comp_v1->lcm_entries[i].lcme_flags &
2788 v1 = lov_comp_entry(comp_v1, i);
2789 objects = lov_v1v3_objects(v1);
2791 for (j = 0; j < v1->lmm_stripe_count; j++) {
2792 if (obdindex == objects[j].l_ost_idx) {
2805 lov_dump_comp_v1_header(param, path, flags);
2807 flags |= LDF_INDENT;
2809 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2810 entry = &comp_v1->lcm_entries[i];
2812 if (param->fp_check_comp_flags &&
2813 !(param->fp_comp_flags & entry->lcme_flags))
2816 if (entry->lcme_flags & LCME_FL_INIT)
2817 flags &= ~LDF_SKIP_OBJS;
2819 flags |= LDF_SKIP_OBJS;
2821 if (param->fp_check_comp_id &&
2822 param->fp_comp_id != entry->lcme_id)
2825 if (param->fp_check_comp_start) {
2826 match = find_value_cmp(entry->lcme_extent.e_start,
2827 param->fp_comp_start,
2828 param->fp_comp_start_sign,
2830 param->fp_comp_start_units, 0);
2835 if (param->fp_check_comp_end) {
2836 unsigned long long margin;
2838 margin = entry->lcme_extent.e_end == LUSTRE_EOF ?
2839 0 : param->fp_comp_end_units;
2841 match = find_value_cmp(entry->lcme_extent.e_end,
2843 param->fp_comp_end_sign,
2849 lov_dump_comp_v1_entry(param, flags, i);
2851 v1 = lov_comp_entry(comp_v1, i);
2852 objects = lov_v1v3_objects(v1);
2853 lov_v1v3_pool_name(v1, pool_name);
2855 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
2856 param->fp_max_depth, param->fp_verbose,
2861 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
2862 enum lov_dump_flags flags)
2866 if (param->fp_get_lmv || param->fp_get_default_lmv)
2867 magic = (__u32)param->fp_lmv_md->lum_magic;
2869 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
2872 flags |= LDF_IS_RAW;
2875 case LOV_USER_MAGIC_V1:
2876 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
2877 param->fp_lmd->lmd_lmm.lmm_objects,
2878 path, param->fp_obd_index,
2879 param->fp_max_depth, param->fp_verbose,
2882 case LOV_USER_MAGIC_V3: {
2883 char pool_name[LOV_MAXPOOLNAME + 1];
2884 struct lov_user_ost_data_v1 *objects;
2885 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2887 strlcpy(pool_name, lmmv3->lmm_pool_name, sizeof(pool_name));
2888 objects = lmmv3->lmm_objects;
2889 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
2890 objects, path, param->fp_obd_index,
2891 param->fp_max_depth, param->fp_verbose,
2896 case LMV_USER_MAGIC: {
2897 char pool_name[LOV_MAXPOOLNAME + 1];
2898 struct lmv_user_md *lum;
2900 lum = (struct lmv_user_md *)param->fp_lmv_md;
2901 strlcpy(pool_name, lum->lum_pool_name, sizeof(pool_name));
2902 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
2903 param->fp_max_depth, param->fp_verbose);
2906 case LOV_USER_MAGIC_COMP_V1:
2907 lov_dump_comp_v1(param, path, flags);
2910 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2911 "(expecting one of %#x %#x %#x %#x)\n",
2912 *(__u32 *)¶m->fp_lmd->lmd_lmm,
2913 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2914 LMV_USER_MAGIC, LMV_MAGIC_V1);
2919 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2925 fname = strrchr(path, '/');
2927 /* It should be a file (or other non-directory) */
2928 if (fname == NULL) {
2929 dname = (char *)malloc(2);
2933 fname = (char *)path;
2935 dname = (char *)malloc(fname - path + 1);
2938 strncpy(dname, path, fname - path);
2939 dname[fname - path] = '\0';
2943 fd = open(dname, O_RDONLY | O_NONBLOCK);
2950 strcpy((char *)lum, fname);
2951 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2954 if (close(fd) == -1 && rc == 0)
2961 int llapi_file_lookup(int dirfd, const char *name)
2963 struct obd_ioctl_data data = { 0 };
2968 if (dirfd < 0 || name == NULL)
2971 data.ioc_version = OBD_IOCTL_VERSION;
2972 data.ioc_len = sizeof(data);
2973 data.ioc_inlbuf1 = (char *)name;
2974 data.ioc_inllen1 = strlen(name) + 1;
2976 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2978 llapi_error(LLAPI_MSG_ERROR, rc,
2979 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2984 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2990 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2991 * Return -1 or 1 if file timestamp does not or does match the given criteria
2992 * correspondingly. Return 0 if the MDS time is being checked and there are
2993 * attributes on OSTs and it is not yet clear if the timespamp matches.
2995 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2996 * updated timestamps. */
2997 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
3002 /* Check if file is accepted. */
3003 if (param->fp_atime) {
3004 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
3005 param->fp_asign, param->fp_exclude_atime,
3012 if (param->fp_mtime) {
3013 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
3014 param->fp_msign, param->fp_exclude_mtime,
3019 /* If the previous check matches, but this one is not yet clear,
3020 * we should return 0 to do an RPC on OSTs. */
3025 if (param->fp_ctime) {
3026 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
3027 param->fp_csign, param->fp_exclude_ctime,
3032 /* If the previous check matches, but this one is not yet clear,
3033 * we should return 0 to do an RPC on OSTs. */
3042 * Check whether the stripes matches the indexes user provided
3046 static int check_obd_match(struct find_param *param)
3048 struct lov_user_ost_data_v1 *objects;
3049 struct lov_comp_md_v1 *comp_v1 = NULL;
3050 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3051 lstat_t *st = ¶m->fp_lmd->lmd_st;
3052 int i, j, k, count = 1;
3054 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
3057 if (!S_ISREG(st->st_mode))
3060 /* Only those files should be accepted, which have a
3061 * stripe on the specified OST. */
3062 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3063 comp_v1 = (struct lov_comp_md_v1 *)v1;
3064 count = comp_v1->lcm_entry_count;
3067 for (i = 0; i < count; i++) {
3069 v1 = lov_comp_entry(comp_v1, i);
3071 objects = lov_v1v3_objects(v1);
3073 for (j = 0; j < v1->lmm_stripe_count; j++) {
3074 for (k = 0; k < param->fp_num_obds; k++) {
3075 if (param->fp_obd_indexes[k] ==
3076 objects[j].l_ost_idx)
3077 return !param->fp_exclude_obd;
3082 return param->fp_exclude_obd;
3085 static int check_mdt_match(struct find_param *param)
3089 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
3092 /* FIXME: For striped dir, we should get stripe information and check */
3093 for (i = 0; i < param->fp_num_mdts; i++) {
3094 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
3095 return !param->fp_exclude_mdt;
3098 if (param->fp_exclude_mdt)
3105 * Check whether the obd is active or not, if it is
3106 * not active, just print the object affected by this
3109 static int print_failed_tgt(struct find_param *param, char *path, int type)
3111 struct obd_statfs stat_buf;
3112 struct obd_uuid uuid_buf;
3115 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
3118 memset(&stat_buf, 0, sizeof(struct obd_statfs));
3119 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
3120 ret = llapi_obd_statfs(path, type,
3121 param->fp_obd_index, &stat_buf,
3124 llapi_printf(LLAPI_MSG_NORMAL,
3125 "obd_uuid: %s failed %s ",
3126 param->fp_obd_uuid->uuid,
3133 static int find_check_stripe_size(struct find_param *param)
3135 struct lov_comp_md_v1 *comp_v1 = NULL;
3136 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3137 int ret, i, count = 1;
3139 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3140 comp_v1 = (struct lov_comp_md_v1 *)v1;
3141 count = comp_v1->lcm_entry_count;
3142 ret = param->fp_exclude_stripe_size ? 1 : -1;
3145 for (i = 0; i < count; i++) {
3147 v1 = lov_comp_entry(comp_v1, i);
3149 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_stripe_size,
3150 param->fp_stripe_size_sign,
3151 param->fp_exclude_stripe_size,
3152 param->fp_stripe_size_units, 0);
3153 /* If any stripe_size matches */
3161 static __u32 find_get_stripe_count(struct find_param *param)
3163 struct lov_comp_md_v1 *comp_v1 = NULL;
3164 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3166 __u32 stripe_count = 0;
3168 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3169 comp_v1 = (struct lov_comp_md_v1 *)v1;
3170 count = comp_v1->lcm_entry_count;
3173 for (i = 0; i < count; i++) {
3175 v1 = lov_comp_entry(comp_v1, i);
3176 stripe_count += v1->lmm_stripe_count;
3179 return stripe_count;
3182 #define LOV_PATTERN_INVALID 0xFFFFFFFF
3184 static int find_check_layout(struct find_param *param)
3186 struct lov_comp_md_v1 *comp_v1 = NULL;
3187 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3189 bool found = false, valid = false;
3191 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3192 comp_v1 = (struct lov_comp_md_v1 *)v1;
3193 count = comp_v1->lcm_entry_count;
3196 for (i = 0; i < count; i++) {
3198 v1 = lov_comp_entry(comp_v1, i);
3200 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
3204 if (v1->lmm_pattern & param->fp_layout) {
3213 if ((found && !param->fp_exclude_layout) ||
3214 (!found && param->fp_exclude_layout))
3220 static int find_check_pool(struct find_param *param)
3222 struct lov_comp_md_v1 *comp_v1 = NULL;
3223 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3224 struct lov_user_md_v3 *v3 = (void *)v1;
3228 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3229 comp_v1 = (struct lov_comp_md_v1 *)v1;
3230 count = comp_v1->lcm_entry_count;
3231 /* empty requested pool is taken as no pool search */
3232 if (count == 0 && param->fp_poolname[0] == '\0')
3236 for (i = 0; i < count; i++) {
3237 if (comp_v1 != NULL)
3238 v1 = lov_comp_entry(comp_v1, i);
3240 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
3241 (param->fp_poolname[0] == '\0')) ||
3242 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
3243 (strncmp(v3->lmm_pool_name,
3244 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
3245 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
3246 (strcmp(param->fp_poolname, "*") == 0))) {
3252 if ((found && !param->fp_exclude_pool) ||
3253 (!found && param->fp_exclude_pool))
3259 static int find_check_comp_options(struct find_param *param)
3261 struct lov_comp_md_v1 *comp_v1;
3262 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3263 struct lov_comp_md_entry_v1 *entry;
3266 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1) {
3267 if ((param->fp_check_comp_count &&
3268 !param->fp_exclude_comp_count) ||
3269 (param->fp_check_comp_flags &&
3270 !param->fp_exclude_comp_flags) ||
3271 (param->fp_check_comp_start &&
3272 !param->fp_exclude_comp_start) ||
3273 (param->fp_check_comp_end &&
3274 !param->fp_exclude_comp_end))
3280 comp_v1 = (struct lov_comp_md_v1 *)v1;
3282 if (param->fp_check_comp_count) {
3283 ret = find_value_cmp(comp_v1->lcm_entry_count,
3284 param->fp_comp_count,
3285 param->fp_comp_count_sign,
3286 param->fp_exclude_comp_count, 1, 0);
3291 if (comp_v1->lcm_entry_count == 0) {
3292 if ((param->fp_check_comp_flags &&
3293 !param->fp_exclude_comp_flags) ||
3294 (param->fp_check_comp_start &&
3295 !param->fp_exclude_comp_start) ||
3296 (param->fp_check_comp_end &&
3297 !param->fp_exclude_comp_end))
3301 if (param->fp_check_comp_flags) {
3302 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3303 entry = &comp_v1->lcm_entries[i];
3305 if (((entry->lcme_flags & param->fp_comp_flags) &&
3306 param->fp_exclude_comp_flags) ||
3307 (!(entry->lcme_flags & param->fp_comp_flags) &&
3308 !param->fp_exclude_comp_flags))
3312 /* If any flags matches */
3320 if (param->fp_check_comp_start) {
3321 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3322 entry = &comp_v1->lcm_entries[i];
3324 ret = find_value_cmp(entry->lcme_extent.e_start,
3325 param->fp_comp_start,
3326 param->fp_comp_start_sign,
3327 param->fp_exclude_comp_start,
3328 param->fp_comp_start_units, 0);
3329 /* If any extent start matches */
3337 if (param->fp_check_comp_end) {
3338 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3339 unsigned long long margin;
3340 entry = &comp_v1->lcm_entries[i];
3342 margin = entry->lcme_extent.e_end == LUSTRE_EOF ?
3343 0 : param->fp_comp_end_units;
3345 ret = find_value_cmp(entry->lcme_extent.e_end,
3347 param->fp_comp_end_sign,
3348 param->fp_exclude_comp_end, margin,
3350 /* If any extent end matches */
3361 static bool find_check_lmm_info(struct find_param *param)
3363 return param->fp_check_pool || param->fp_check_stripe_count ||
3364 param->fp_check_stripe_size || param->fp_check_layout ||
3365 param->fp_check_comp_count || param->fp_check_comp_end ||
3366 param->fp_check_comp_start || param->fp_check_comp_flags ||
3367 param->fp_check_projid;
3371 * Get file/directory project id.
3372 * by the open fd resides on.
3373 * Return 0 and project id on success, or -ve errno.
3375 static int fget_projid(int fd, int *projid)
3380 rc = ioctl(fd, LL_IOC_FSGETXATTR, &fsx);
3384 *projid = fsx.fsx_projid;
3388 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
3389 void *data, struct dirent64 *de)
3391 struct find_param *param = (struct find_param *)data;
3392 DIR *dir = dirp == NULL ? NULL : *dirp;
3393 int decision = 1; /* 1 is accepted; -1 is rejected. */
3394 lstat_t *st = ¶m->fp_lmd->lmd_st;
3396 int checked_type = 0;
3398 __u32 stripe_count = 0;
3401 if (parent == NULL && dir == NULL)
3404 /* If a regular expression is presented, make the initial decision */
3405 if (param->fp_pattern != NULL) {
3406 char *fname = strrchr(path, '/');
3407 fname = (fname == NULL ? path : fname + 1);
3408 ret = fnmatch(param->fp_pattern, fname, 0);
3409 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
3410 (ret == 0 && param->fp_exclude_pattern))
3414 /* See if we can check the file type from the dirent. */
3415 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
3418 if (DTTOIF(de->d_type) == param->fp_type) {
3419 if (param->fp_exclude_type)
3422 if (!param->fp_exclude_type)
3429 /* Request MDS for the stat info if some of these parameters need
3430 * to be compared. */
3431 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
3432 param->fp_check_uid || param->fp_check_gid ||
3433 param->fp_atime || param->fp_mtime || param->fp_ctime ||
3434 param->fp_check_size || find_check_lmm_info(param) ||
3435 param->fp_check_mdt_count || param->fp_check_hash_type)
3438 if (param->fp_type != 0 && checked_type == 0)
3441 if (decision == 0) {
3442 if (param->fp_check_mdt_count || param->fp_check_hash_type) {
3443 param->fp_get_lmv = 1;
3444 ret = cb_get_dirstripe(path, dir, param);
3449 param->fp_lmd->lmd_lmm.lmm_magic = 0;
3450 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
3451 param->fp_lum_size);
3452 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
3453 find_check_lmm_info(param)) {
3454 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
3456 /* We need to "fake" the "use the default" values
3457 * since the lmm struct is zeroed out at this point. */
3458 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3459 lmm->lmm_pattern = 0xFFFFFFFF;
3461 ostid_set_seq(&lmm->lmm_oi,
3462 FID_SEQ_LOV_DEFAULT);
3463 lmm->lmm_stripe_size = 0;
3464 lmm->lmm_stripe_count = 0;
3465 lmm->lmm_stripe_offset = -1;
3467 if (ret == 0 && param->fp_mdt_uuid != NULL) {
3469 ret = llapi_file_fget_mdtidx(dirfd(dir),
3470 ¶m->fp_file_mdt_index);
3471 } else if (S_ISREG(st->st_mode)) {
3472 /* FIXME: we could get the MDT index from the
3473 * file's FID in lmd->lmd_lmm.lmm_oi without
3474 * opening the file, once we are sure that
3475 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
3476 * That would still be an ioctl() to map the
3477 * FID to the MDT, but not an open RPC. */
3478 fd = open(path, O_RDONLY);
3480 ret = llapi_file_fget_mdtidx(fd,
3481 ¶m->fp_file_mdt_index);
3486 /* For a special file, we assume it resides on
3487 * the same MDT as the parent directory. */
3488 ret = llapi_file_fget_mdtidx(dirfd(parent),
3489 ¶m->fp_file_mdt_index);
3500 stripe_count = find_get_stripe_count(param);
3504 if (param->fp_type && !checked_type) {
3505 if ((st->st_mode & S_IFMT) == param->fp_type) {
3506 if (param->fp_exclude_type)
3509 if (!param->fp_exclude_type)
3515 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
3516 if (lustre_fs && param->fp_got_uuids &&
3517 param->fp_dev != st->st_dev) {
3518 /* A lustre/lustre mount point is crossed. */
3519 param->fp_got_uuids = 0;
3520 param->fp_obds_printed = 0;
3521 param->fp_mdt_index = OBD_NOT_FOUND;
3522 param->fp_obd_index = OBD_NOT_FOUND;
3525 if (lustre_fs && !param->fp_got_uuids) {
3526 ret = setup_target_indexes(dir ? dir : parent, path,
3531 param->fp_dev = st->st_dev;
3532 } else if (!lustre_fs && param->fp_got_uuids) {
3533 /* A lustre/non-lustre mount point is crossed. */
3534 param->fp_got_uuids = 0;
3535 param->fp_mdt_index = OBD_NOT_FOUND;
3536 param->fp_obd_index = OBD_NOT_FOUND;
3540 if (param->fp_check_stripe_size) {
3541 decision = find_check_stripe_size(param);
3546 if (param->fp_check_stripe_count) {
3547 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
3548 param->fp_stripe_count_sign,
3549 param->fp_exclude_stripe_count, 1, 0);
3554 if (param->fp_check_mdt_count) {
3555 decision = find_value_cmp(
3556 param->fp_lmv_md->lum_stripe_count,
3557 param->fp_mdt_count,
3558 param->fp_mdt_count_sign,
3559 param->fp_exclude_mdt_count, 1, 0);
3564 if (param->fp_check_layout) {
3565 decision = find_check_layout(param);
3570 if (param->fp_check_hash_type) {
3573 found = param->fp_lmv_md->lum_hash_type & param->fp_hash_type;
3574 if ((found && param->fp_exclude_hash_type) ||
3575 (!found && !param->fp_exclude_hash_type)) {
3581 /* If an OBD UUID is specified but none matches, skip this file. */
3582 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
3583 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
3586 /* If an OST or MDT UUID is given, and some OST matches,
3588 if (param->fp_obd_index != OBD_NOT_FOUND ||
3589 param->fp_mdt_index != OBD_NOT_FOUND) {
3590 if (param->fp_obd_uuid) {
3591 if (check_obd_match(param)) {
3592 /* If no mdtuuid is given, we are done.
3593 * Otherwise, fall through to the mdtuuid
3595 if (!param->fp_mdt_uuid)
3602 if (param->fp_mdt_uuid) {
3603 if (check_mdt_match(param))
3610 if (param->fp_check_uid) {
3611 if (st->st_uid == param->fp_uid) {
3612 if (param->fp_exclude_uid)
3615 if (!param->fp_exclude_uid)
3620 if (param->fp_check_gid) {
3621 if (st->st_gid == param->fp_gid) {
3622 if (param->fp_exclude_gid)
3625 if (!param->fp_exclude_gid)
3630 if (param->fp_check_projid) {
3634 fd = open(path, O_RDONLY);
3637 ret = fget_projid(fd, &projid);
3642 if (projid == param->fp_projid) {
3643 if (param->fp_exclude_uid)
3646 if (!param->fp_exclude_projid)
3651 if (param->fp_check_pool) {
3652 decision = find_check_pool(param);
3657 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
3658 param->fp_check_comp_start || param->fp_check_comp_end) {
3659 decision = find_check_comp_options(param);
3664 /* Check the time on mds. */
3666 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
3669 for_mds = lustre_fs ?
3670 (S_ISREG(st->st_mode) && stripe_count) : 0;
3671 decision = find_time_check(st, param, for_mds);
3676 /* If file still fits the request, ask ost for updated info.
3677 The regular stat is almost of the same speed as some new
3678 'glimpse-size-ioctl'. */
3680 if (param->fp_check_size && S_ISREG(st->st_mode) && stripe_count)
3683 if (param->fp_check_size && S_ISDIR(st->st_mode))
3687 /* For regular files with the stripe the decision may have not
3688 * been taken yet if *time or size is to be checked. */
3689 if (param->fp_obd_index != OBD_NOT_FOUND)
3690 print_failed_tgt(param, path, LL_STATFS_LOV);
3692 if (param->fp_mdt_index != OBD_NOT_FOUND)
3693 print_failed_tgt(param, path, LL_STATFS_LMV);
3696 ret = fstat_f(dirfd(dir), st);
3697 else if (de != NULL)
3698 ret = fstatat_f(dirfd(parent), de->d_name, st,
3699 AT_SYMLINK_NOFOLLOW);
3701 ret = lstat_f(path, st);
3704 if (errno == ENOENT) {
3705 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
3706 "warning: %s: %s does not exist",
3711 llapi_error(LLAPI_MSG_ERROR, ret,
3712 "%s: IOC_LOV_GETINFO on %s failed",
3718 /* Check the time on osc. */
3719 decision = find_time_check(st, param, 0);
3724 if (param->fp_check_size)
3725 decision = find_value_cmp(st->st_size, param->fp_size,
3726 param->fp_size_sign,
3727 param->fp_exclude_size,
3728 param->fp_size_units, 0);
3730 if (decision != -1) {
3731 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
3732 if (param->fp_zero_end)
3733 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
3735 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3740 /* Do not get down anymore? */
3741 if (param->fp_depth == param->fp_max_depth) {
3752 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
3753 void *param_data, struct dirent64 *de)
3755 struct find_param *param = (struct find_param *)param_data;
3756 DIR *tmp_parent = parent;
3757 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3759 struct obd_ioctl_data data = { 0 };
3766 if (parent == NULL && dirp == NULL)
3772 if (parent == NULL) {
3773 tmp_parent = opendir_parent(path);
3774 if (tmp_parent == NULL) {
3777 llapi_error(LLAPI_MSG_ERROR, ret,
3778 "can not open %s", path);
3783 fd = dirfd(tmp_parent);
3785 path_copy = strdup(path);
3786 filename = basename(path_copy);
3787 data.ioc_inlbuf1 = (char *)filename;
3788 data.ioc_inllen1 = strlen(filename) + 1;
3789 data.ioc_inlbuf2 = (char *)¶m->fp_mdt_index;
3790 data.ioc_inllen2 = sizeof(param->fp_mdt_index);
3791 ret = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3793 llapi_error(LLAPI_MSG_ERROR, ret,
3794 "llapi_obd_statfs: error packing ioctl data");
3799 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
3801 if (errno == EBUSY && !retry) {
3802 /* because migrate may not be able to lock all involved
3803 * objects in order, for some of them it try lock, while
3804 * there may be conflicting COS locks and cause migrate
3805 * fail with EBUSY, hope a sync() could cause
3806 * transaction commit and release these COS locks. */
3812 fprintf(stderr, "%s migrate failed: %s (%d)\n",
3813 path, strerror(-ret), ret);
3815 } else if (param->fp_verbose & VERBOSE_DETAIL) {
3816 fprintf(stdout, "migrate %s to MDT%d\n",
3817 path, param->fp_mdt_index);
3822 /* If the directory is being migration, we need
3823 * close the directory after migration,
3824 * so the old directory cache will be cleanup
3825 * on the client side, and re-open to get the
3826 * new directory handle */
3827 *dirp = opendir(path);
3828 if (*dirp == NULL) {
3830 llapi_error(LLAPI_MSG_ERROR, ret,
3831 "%s: Failed to open '%s'", __func__, path);
3836 closedir(tmp_parent);
3843 int llapi_migrate_mdt(char *path, struct find_param *param)
3845 return param_callback(path, cb_migrate_mdt_init, cb_common_fini, param);
3848 int llapi_mv(char *path, struct find_param *param)
3850 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
3851 static bool printed;
3854 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
3855 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
3859 return llapi_migrate_mdt(path, param);
3862 int llapi_find(char *path, struct find_param *param)
3864 return param_callback(path, cb_find_init, cb_common_fini, param);
3868 * Get MDT number that the file/directory inode referenced
3869 * by the open fd resides on.
3870 * Return 0 and mdtidx on success, or -ve errno.
3872 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
3874 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
3879 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
3880 struct dirent64 *de)
3882 struct find_param *param = (struct find_param *)data;
3883 DIR *d = dirp == NULL ? NULL : *dirp;
3887 if (parent == NULL && d == NULL)
3891 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3892 } else /* if (parent) */ {
3895 fd = open(path, O_RDONLY | O_NOCTTY);
3897 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3905 if (ret == -ENODATA) {
3906 if (!param->fp_obd_uuid)
3907 llapi_printf(LLAPI_MSG_NORMAL,
3908 "'%s' has no stripe info\n", path);
3910 } else if (ret == -ENOENT) {
3911 llapi_error(LLAPI_MSG_WARN, ret,
3912 "warning: %s: '%s' does not exist",
3915 } else if (ret == -ENOTTY) {
3916 llapi_error(LLAPI_MSG_ERROR, ret,
3917 "%s: '%s' not on a Lustre fs",
3920 llapi_error(LLAPI_MSG_ERROR, ret,
3921 "error: %s: '%s' failed get_mdtidx",
3927 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
3928 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3930 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3934 /* Do not go down anymore? */
3935 if (param->fp_depth == param->fp_max_depth)
3943 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
3944 struct dirent64 *de)
3946 struct find_param *param = (struct find_param *)data;
3947 DIR *d = dirp == NULL ? NULL : *dirp;
3950 if (parent == NULL && d == NULL)
3953 if (param->fp_obd_uuid) {
3954 param->fp_quiet = 1;
3955 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
3961 if (param->fp_get_lmv || param->fp_get_default_lmv) {
3962 ret = cb_get_dirstripe(path, d, param);
3964 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
3965 (void *)¶m->fp_lmd->lmd_lmm);
3968 } else if (parent && !param->fp_get_lmv && !param->fp_get_default_lmv) {
3969 char *fname = strrchr(path, '/');
3970 fname = (fname == NULL ? path : fname + 1);
3972 strlcpy((char *)¶m->fp_lmd->lmd_lmm, fname,
3973 param->fp_lum_size);
3975 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
3976 (void *)¶m->fp_lmd->lmd_lmm);
3982 if (errno == ENODATA && d != NULL) {
3983 /* We need to "fake" the "use the default" values
3984 * since the lmm struct is zeroed out at this point.
3985 * The magic needs to be set in order to satisfy
3986 * a check later on in the code path.
3987 * The object_seq needs to be set for the "(Default)"
3988 * prefix to be displayed. */
3989 if (param->fp_get_default_lmv) {
3990 struct lmv_user_md *lum = param->fp_lmv_md;
3992 lum->lum_magic = LMV_USER_MAGIC;
3993 lum->lum_stripe_count = 0;
3994 lum->lum_stripe_offset = -1;
3996 } else if (param->fp_get_lmv) {
3997 struct lmv_user_md *lum = param->fp_lmv_md;
4000 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
4003 lum->lum_magic = LMV_MAGIC_V1;
4004 lum->lum_stripe_count = 0;
4005 lum->lum_stripe_offset = mdtidx;
4008 struct lov_user_md *lmm =
4009 ¶m->fp_lmd->lmd_lmm;
4011 lmm->lmm_magic = LOV_USER_MAGIC_V1;
4013 ostid_set_seq(&lmm->lmm_oi,
4014 FID_SEQ_LOV_DEFAULT);
4015 lmm->lmm_stripe_count = 0;
4016 lmm->lmm_stripe_size = 0;
4017 lmm->lmm_stripe_offset = -1;
4020 } else if (errno == ENODATA && parent != NULL) {
4021 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
4022 llapi_printf(LLAPI_MSG_NORMAL,
4023 "%s has no stripe info\n", path);
4025 } else if (errno == ENOENT) {
4026 llapi_error(LLAPI_MSG_WARN, -ENOENT,
4027 "warning: %s: %s does not exist",
4030 } else if (errno == ENOTTY) {
4032 llapi_error(LLAPI_MSG_ERROR, ret,
4033 "%s: '%s' not on a Lustre fs?",
4038 llapi_error(LLAPI_MSG_ERROR, ret,
4039 "error: %s: %s failed for %s",
4040 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
4041 "IOC_MDC_GETFILESTRIPE", path);
4048 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
4049 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
4052 /* Do not get down anymore? */
4053 if (param->fp_depth == param->fp_max_depth)
4061 int llapi_getstripe(char *path, struct find_param *param)
4063 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
4064 cb_get_mdt_index : cb_getstripe,
4065 cb_common_fini, param);
4068 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
4069 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
4071 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
4073 struct obd_ioctl_data data = { 0 };
4076 data.ioc_inlbuf1 = (char *)&type;
4077 data.ioc_inllen1 = sizeof(__u32);
4078 data.ioc_inlbuf2 = (char *)&index;
4079 data.ioc_inllen2 = sizeof(__u32);
4080 data.ioc_pbuf1 = (char *)stat_buf;
4081 data.ioc_plen1 = sizeof(struct obd_statfs);
4082 data.ioc_pbuf2 = (char *)uuid_buf;
4083 data.ioc_plen2 = sizeof(struct obd_uuid);
4085 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
4087 llapi_error(LLAPI_MSG_ERROR, rc,
4088 "llapi_obd_statfs: error packing ioctl data");
4092 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
4094 return rc < 0 ? -errno : 0;
4097 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
4098 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
4103 fd = open(path, O_RDONLY);
4106 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
4108 /* If we can't even open a file on the filesystem (e.g. with
4109 * -ESHUTDOWN), force caller to exit or it will loop forever. */
4113 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
4120 #define MAX_STRING_SIZE 128
4122 int llapi_ping(char *obd_type, char *obd_name)
4128 rc = cfs_get_param_paths(&path, "%s/%s/ping",
4129 obd_type, obd_name);
4133 fd = open(path.gl_pathv[0], O_WRONLY);
4136 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
4141 /* The purpose is to send a byte as a ping, whatever this byte is. */
4142 /* coverity[uninit_use_in_call] */
4143 rc = write(fd, buf, 1);
4151 cfs_free_param_data(&path);
4155 int llapi_target_iterate(int type_num, char **obd_type,
4156 void *args, llapi_cb_t cb)
4158 char buf[MAX_STRING_SIZE];
4163 rc = cfs_get_param_paths(¶m, "devices");
4167 fp = fopen(param.gl_pathv[0], "r");
4170 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
4175 while (fgets(buf, sizeof(buf), fp) != NULL) {
4176 char *obd_type_name = NULL;
4177 char *obd_name = NULL;
4178 char *obd_uuid = NULL;
4180 struct obd_statfs osfs_buffer;
4182 while(bufp[0] == ' ')
4185 for(i = 0; i < 3; i++) {
4186 obd_type_name = strsep(&bufp, " ");
4188 obd_name = strsep(&bufp, " ");
4189 obd_uuid = strsep(&bufp, " ");
4191 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
4193 for (i = 0; i < type_num; i++) {
4194 if (strcmp(obd_type_name, obd_type[i]) != 0)
4197 cb(obd_type_name, obd_name, obd_uuid, args);
4202 cfs_free_param_data(¶m);
4206 static void do_target_check(char *obd_type_name, char *obd_name,
4207 char *obd_uuid, void *args)
4211 rc = llapi_ping(obd_type_name, obd_name);
4212 if (rc == ENOTCONN) {
4213 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
4215 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
4217 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
4221 int llapi_target_check(int type_num, char **obd_type, char *dir)
4223 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
4226 #undef MAX_STRING_SIZE
4228 /* Is this a lustre fs? */
4229 int llapi_is_lustre_mnttype(const char *type)
4231 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
4234 /* Is this a lustre client fs? */
4235 int llapi_is_lustre_mnt(struct mntent *mnt)
4237 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
4238 strstr(mnt->mnt_fsname, ":/") != NULL);
4241 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
4243 char fsname[PATH_MAX + 1];
4247 rc = llapi_search_fsname(mnt, fsname);
4249 llapi_err_noerrno(LLAPI_MSG_ERROR,
4250 "'%s' isn't on Lustre filesystem", mnt);
4254 root = open(mnt, O_RDONLY | O_DIRECTORY);
4257 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4261 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
4269 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
4270 * format must have %s%s, buf must be > 16
4271 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
4272 * then buf = "lustre-MDT0000"
4274 static int get_mdtname(char *name, char *format, char *buf)
4276 char suffix[]="-MDT0000";
4277 int len = strlen(name);
4279 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
4280 name[len - 5] = '\0';
4285 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
4288 /* Not enough room to add suffix */
4289 llapi_err_noerrno(LLAPI_MSG_ERROR,
4290 "MDT name too long |%s|", name);
4295 return sprintf(buf, format, name, suffix);
4298 /** ioctl on filsystem root, with mdtindex sent as data
4299 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
4300 * \param mdtidxp pointer to integer within data to be filled in with the
4301 * mdt index (0 if no mdt is specified). NULL won't be filled.
4303 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
4311 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
4312 Open root and parse mdt index. */
4313 if (mdtname[0] == '/') {
4315 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
4316 (char *)mdtname, -1);
4318 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
4320 ptr = fsname + strlen(fsname) - 8;
4322 index = strtol(ptr + 4, NULL, 10);
4323 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
4327 llapi_err_noerrno(LLAPI_MSG_ERROR,
4328 "Can't open %s: %d\n", mdtname, rc);
4335 rc = ioctl(fd, opc, data);
4344 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
4345 int buflen, long long *recno, int *linkno)
4347 const char *fidstr_orig = fidstr;
4349 struct getinfo_fid2path *gf;
4352 while (*fidstr == '[')
4355 sscanf(fidstr, SFID, RFID(&fid));
4356 if (!fid_is_sane(&fid)) {
4357 llapi_err_noerrno(LLAPI_MSG_ERROR,
4358 "bad FID format '%s', should be [seq:oid:ver]"
4359 " (e.g. "DFID")\n", fidstr_orig,
4360 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
4364 gf = malloc(sizeof(*gf) + buflen);
4369 gf->gf_recno = *recno;
4370 gf->gf_linkno = *linkno;
4371 gf->gf_pathlen = buflen;
4373 /* Take path or fsname */
4374 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
4378 memcpy(buf, gf->gf_u.gf_path, gf->gf_pathlen);
4379 if (buf[0] == '\0') { /* ROOT path */
4383 *recno = gf->gf_recno;
4384 *linkno = gf->gf_linkno;
4391 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
4394 struct lustre_mdt_attrs *lma;
4398 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
4400 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
4403 lma = (struct lustre_mdt_attrs *)buf;
4404 fid_le_to_cpu(fid, &lma->lma_self_fid);
4408 int llapi_get_mdt_index_by_fid(int fd, const lustre_fid *fid,
4413 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
4422 int llapi_fd2fid(const int fd, lustre_fid *fid)
4426 memset(fid, 0, sizeof(*fid));
4428 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
4429 if (rc == -EINVAL || rc == -ENOTTY)
4430 rc = fid_from_lma(NULL, fd, fid);
4435 int llapi_path2fid(const char *path, lustre_fid *fid)
4439 memset(fid, 0, sizeof(*fid));
4440 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4442 if (errno == ELOOP || errno == ENXIO)
4443 return fid_from_lma(path, -1, fid);
4447 rc = llapi_fd2fid(fd, fid);
4448 if (rc == -EINVAL || rc == -ENOTTY)
4449 rc = fid_from_lma(path, -1, fid);
4455 int llapi_fd2parent(int fd, unsigned int linkno, lustre_fid *parent_fid,
4456 char *name, size_t name_size)
4458 struct getparent *gp;
4461 gp = malloc(sizeof(*gp) + name_size);
4465 gp->gp_linkno = linkno;
4466 gp->gp_name_size = name_size;
4468 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
4474 *parent_fid = gp->gp_fid;
4476 strncpy(name, gp->gp_name, name_size);
4477 name[name_size - 1] = '\0';
4484 int llapi_path2parent(const char *path, unsigned int linkno,
4485 lustre_fid *parent_fid, char *name, size_t name_size)
4490 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4494 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
4499 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4504 root = open(mnt, O_RDONLY | O_DIRECTORY);
4507 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4511 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
4514 llapi_error(LLAPI_MSG_ERROR, rc,
4515 "ioctl on %s for getting connect flags failed", mnt);
4522 * Get a 64-bit value representing the version of file data pointed by fd.
4524 * Each write or truncate, flushed on OST, will change this value. You can use
4525 * this value to verify if file data was modified. This only checks the file
4526 * data, not metadata.
4528 * \param flags 0: no flush pages, usually used it the process has already
4530 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
4532 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
4533 * pages from clients.
4535 * \retval 0 on success.
4536 * \retval -errno on error.
4538 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4541 struct ioc_data_version idv;
4543 idv.idv_flags = flags;
4545 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4549 *data_version = idv.idv_version;
4555 * Create a file without any name open it for read/write
4557 * - file is created as if it were a standard file in the given \a directory
4558 * - file does not appear in \a directory and mtime does not change because
4559 * the filename is handled specially by the Lustre MDS.
4560 * - file is removed at final close
4561 * - file modes are rw------- since it doesn't make sense to have a read-only
4562 * or write-only file that cannot be opened again.
4563 * - if user wants another mode it must use fchmod() on the open file, no
4564 * security problems arise because it cannot be opened by another process.
4566 * \param[in] directory directory from which to inherit layout/MDT idx
4567 * \param[in] idx MDT index on which the file is created,
4568 * \a idx == -1 means no specific MDT is requested
4569 * \param[in] open_flags standard open(2) flags
4571 * \retval 0 on success.
4572 * \retval -errno on error.
4574 int llapi_create_volatile_idx(char *directory, int idx, int open_flags)
4576 char file_path[PATH_MAX];
4577 char filename[PATH_MAX];
4578 int saved_errno = errno;
4586 snprintf(filename, sizeof(filename),
4587 LUSTRE_VOLATILE_HDR"::%.4X", rnumber);
4589 snprintf(filename, sizeof(filename),
4590 LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, rnumber);
4592 rc = snprintf(file_path, sizeof(file_path),
4593 "%s/%s", directory, filename);
4594 if (rc >= sizeof(file_path))
4597 fd = open(file_path,
4598 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | open_flags,
4600 } while (fd < 0 && errno == EEXIST);
4603 llapi_error(LLAPI_MSG_ERROR, errno,
4604 "Cannot create volatile file '%s' in '%s'",
4605 filename + LUSTRE_VOLATILE_HDR_LEN,
4610 /* Unlink file in case this wasn't a Lustre filesystem and the
4611 * magic volatile filename wasn't handled as intended. The
4612 * effect is the same. If volatile open was supported then we
4613 * expect unlink() to return -ENOENT. */
4614 (void)unlink(file_path);
4616 /* Since we are returning successfully we restore errno (and
4617 * mask out possible EEXIST from open() and ENOENT from
4619 errno = saved_errno;
4625 * Swap the layouts between 2 file descriptors
4626 * the 2 files must be open for writing
4627 * first fd received the ioctl, second fd is passed as arg
4628 * this is assymetric but avoid use of root path for ioctl
4630 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
4631 int gid, __u64 flags)
4633 struct lustre_swap_layouts lsl;
4638 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4639 rc = fstat(fd1, &st1);
4643 rc = fstat(fd2, &st2);
4648 lsl.sl_flags = flags;
4652 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4656 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4657 struct timeval tv1[2];
4658 struct timeval tv2[2];
4660 memset(tv1, 0, sizeof(tv1));
4661 memset(tv2, 0, sizeof(tv2));
4663 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
4664 tv1[0].tv_sec = st1.st_atime;
4665 tv2[0].tv_sec = st2.st_atime;
4667 tv1[0].tv_sec = st2.st_atime;
4668 tv2[0].tv_sec = st1.st_atime;
4671 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
4672 tv1[1].tv_sec = st1.st_mtime;
4673 tv2[1].tv_sec = st2.st_mtime;
4675 tv1[1].tv_sec = st2.st_mtime;
4676 tv2[1].tv_sec = st1.st_mtime;
4679 rc = futimes(fd1, tv1);
4683 rc = futimes(fd2, tv2);
4691 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4698 while (grp_id == 0);
4700 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
4708 * Swap the layouts between 2 files
4709 * the 2 files are open in write
4711 int llapi_swap_layouts(const char *path1, const char *path2,
4712 __u64 dv1, __u64 dv2, __u64 flags)
4716 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4719 llapi_error(LLAPI_MSG_ERROR, rc,
4720 "error: cannot open '%s' for write", path1);
4724 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4727 llapi_error(LLAPI_MSG_ERROR, rc,
4728 "error: cannot open '%s' for write", path2);
4732 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4734 llapi_error(LLAPI_MSG_ERROR, rc,
4735 "error: cannot swap layout between '%s' and '%s'",
4746 * Attempt to open a file with Lustre file identifier \a fid
4747 * and return an open file descriptor.
4749 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
4750 * \param[in] fid Lustre file identifier of file to open
4751 * \param[in] flags open() flags
4753 * \retval non-negative file descriptor on successful open
4754 * \retval -1 if an error occurred
4756 int llapi_open_by_fid(const char *lustre_dir, const lustre_fid *fid, int flags)
4758 char mntdir[PATH_MAX];
4759 char path[PATH_MAX];
4762 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
4766 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
4767 return open(path, flags);
4773 * \param fd File to lock.
4774 * \param gid Group Identifier.
4776 * \retval 0 on success.
4777 * \retval -errno on failure.
4779 int llapi_group_lock(int fd, int gid)
4783 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
4786 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
4794 * \param fd File to unlock.
4795 * \param gid Group Identifier.
4797 * \retval 0 on success.
4798 * \retval -errno on failure.
4800 int llapi_group_unlock(int fd, int gid)
4804 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
4807 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");