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/param.h>
72 #include <libcfs/util/string.h>
73 #include <lnet/lnetctl.h>
74 #include <lustre/lustreapi.h>
75 #include <lustre/lustre_ostid.h>
76 #include <lustre_ioctl.h>
77 #include "lustreapi_internal.h"
79 static int llapi_msg_level = LLAPI_MSG_MAX;
81 char *mdt_hash_name[] = { "none",
82 LMV_HASH_NAME_ALL_CHARS,
83 LMV_HASH_NAME_FNV_1A_64 };
85 void llapi_msg_set_level(int level)
87 /* ensure level is in the good range */
88 if (level < LLAPI_MSG_OFF)
89 llapi_msg_level = LLAPI_MSG_OFF;
90 else if (level > LLAPI_MSG_MAX)
91 llapi_msg_level = LLAPI_MSG_MAX;
93 llapi_msg_level = level;
96 int llapi_msg_get_level(void)
98 return llapi_msg_level;
101 static void error_callback_default(enum llapi_message_level level, int err,
102 const char *fmt, va_list ap)
104 vfprintf(stderr, fmt, ap);
105 if (level & LLAPI_MSG_NO_ERRNO)
106 fprintf(stderr, "\n");
108 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
111 static void info_callback_default(enum llapi_message_level level, int err,
112 const char *fmt, va_list ap)
114 vfprintf(stdout, fmt, ap);
117 static llapi_log_callback_t llapi_error_callback = error_callback_default;
118 static llapi_log_callback_t llapi_info_callback = info_callback_default;
121 /* llapi_error will preserve errno */
122 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
125 int tmp_errno = errno;
127 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
131 llapi_error_callback(level, abs(err), fmt, args);
136 /* llapi_printf will preserve errno */
137 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
140 int tmp_errno = errno;
142 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
146 llapi_info_callback(level, 0, fmt, args);
152 * Set a custom error logging function. Passing in NULL will reset the logging
153 * callback to its default value.
155 * This function returns the value of the old callback.
157 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
159 llapi_log_callback_t old = llapi_error_callback;
162 llapi_error_callback = cb;
164 llapi_error_callback = error_callback_default;
170 * Set a custom info logging function. Passing in NULL will reset the logging
171 * callback to its default value.
173 * This function returns the value of the old callback.
175 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
177 llapi_log_callback_t old = llapi_info_callback;
180 llapi_info_callback = cb;
182 llapi_info_callback = info_callback_default;
188 * size_units is to be initialized (or zeroed) by caller.
190 int llapi_parse_size(const char *optarg, unsigned long long *size,
191 unsigned long long *size_units, int bytes_spec)
194 char *argbuf = (char *)optarg;
195 unsigned long long frac = 0, frac_d = 1;
197 if (strncmp(optarg, "-", 1) == 0)
200 if (*size_units == 0)
203 *size = strtoull(argbuf, &end, 0);
204 if (end != NULL && *end == '.') {
208 frac = strtoull(argbuf, &end, 10);
209 /* count decimal places */
210 for (i = 0; i < (end - argbuf); i++)
215 if ((*end == 'b') && *(end + 1) == '\0' &&
216 (*size & (~0ULL << (64 - 9))) == 0 &&
218 *size_units = 1 << 9;
219 } else if ((*end == 'b') &&
220 *(end + 1) == '\0' &&
223 } else if ((*end == 'k' || *end == 'K') &&
224 *(end + 1) == '\0' &&
225 (*size & (~0ULL << (64 - 10))) == 0) {
226 *size_units = 1 << 10;
227 } else if ((*end == 'm' || *end == 'M') &&
228 *(end + 1) == '\0' &&
229 (*size & (~0ULL << (64 - 20))) == 0) {
230 *size_units = 1 << 20;
231 } else if ((*end == 'g' || *end == 'G') &&
232 *(end + 1) == '\0' &&
233 (*size & (~0ULL << (64 - 30))) == 0) {
234 *size_units = 1 << 30;
235 } else if ((*end == 't' || *end == 'T') &&
236 *(end + 1) == '\0' &&
237 (*size & (~0ULL << (64 - 40))) == 0) {
238 *size_units = 1ULL << 40;
239 } else if ((*end == 'p' || *end == 'P') &&
240 *(end + 1) == '\0' &&
241 (*size & (~0ULL << (64 - 50))) == 0) {
242 *size_units = 1ULL << 50;
243 } else if ((*end == 'e' || *end == 'E') &&
244 *(end + 1) == '\0' &&
245 (*size & (~0ULL << (64 - 60))) == 0) {
246 *size_units = 1ULL << 60;
251 *size = *size * *size_units + frac * *size_units / frac_d;
256 /* XXX: llapi_xxx() functions return negative values upon failure */
258 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
259 int stripe_count, int stripe_pattern)
263 /* 64 KB is the largest common page size I'm aware of (on ia64), but
264 * check the local page size just in case. */
265 page_size = LOV_MIN_STRIPE_SIZE;
266 if (getpagesize() > page_size) {
267 page_size = getpagesize();
268 llapi_err_noerrno(LLAPI_MSG_WARN,
269 "warning: your page size (%u) is "
270 "larger than expected (%u)", page_size,
271 LOV_MIN_STRIPE_SIZE);
273 if (!llapi_stripe_size_is_aligned(stripe_size)) {
275 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
276 "must be an even multiple of %d bytes",
277 stripe_size, page_size);
280 if (!llapi_stripe_index_is_valid(stripe_offset)) {
282 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
286 if (!llapi_stripe_count_is_valid(stripe_count)) {
288 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
292 if (llapi_stripe_size_is_too_big(stripe_size)) {
294 llapi_error(LLAPI_MSG_ERROR, rc,
295 "warning: stripe size 4G or larger "
296 "is not currently supported and would wrap");
303 * Trim a trailing newline from a string, if it exists.
305 int llapi_chomp_string(char *buf)
321 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
323 static int get_param_lov(const char *path, const char *param,
324 char *buf, size_t buf_size)
326 struct obd_uuid uuid;
329 rc = llapi_file_get_lov_uuid(path, &uuid);
333 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
338 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
340 static int get_param_lmv(const char *path, const char *param,
341 char *buf, size_t buf_size)
343 struct obd_uuid uuid;
346 rc = llapi_file_get_lmv_uuid(path, &uuid);
350 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
354 static int get_mds_md_size(const char *path)
356 char buf[PATH_MAX], inst[PATH_MAX];
357 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
360 rc = llapi_getname(path, inst, sizeof(inst));
364 /* Get the max ea size from llite parameters. */
365 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
366 "max_easize", buf, sizeof(buf));
372 return rc > 0 ? rc : md_size;
375 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
377 return get_param_lmv(path, "uuid", buf, bufsize);
381 * if pool is NULL, search ostname in target_obd
382 * if pool is not NULL:
383 * if pool not found returns errno < 0
384 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
385 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
387 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
389 char buffer[PATH_MAX];
395 /* You need one or the other */
396 if (poolname == NULL && fsname == NULL)
400 len = strlen(ostname);
402 if (poolname == NULL && len == 0)
405 /* Search by poolname and fsname if is not NULL */
406 if (poolname != NULL) {
407 rc = poolpath(¶m, fsname, NULL);
409 snprintf(buffer, sizeof(buffer), "%s/%s",
410 param.gl_pathv[0], poolname);
412 } else if (fsname != NULL) {
413 rc = get_lustre_param_path("lov", fsname,
415 "target_obd", ¶m);
417 strncpy(buffer, param.gl_pathv[0],
423 cfs_free_param_data(¶m);
427 fd = fopen(buffer, "r");
431 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
432 if (poolname == NULL) {
434 /* Search for an ostname in the list of OSTs
435 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
436 ptr = strchr(buffer, ' ');
438 (strncmp(ptr + 1, ostname, len) == 0)) {
443 /* Search for an ostname in a pool,
444 (or an existing non-empty pool if no ostname) */
445 if ((ostname == NULL) ||
446 (strncmp(buffer, ostname, len) == 0)) {
457 * Open a Lustre file.
459 * \param name the name of the file to be opened
460 * \param flags access mode, see flags in open(2)
461 * \param mode permission of the file if it is created, see mode in open(2)
462 * \param param stripe pattern of the newly created file
464 * \retval file descriptor of opened file
465 * \retval negative errno on failure
467 int llapi_file_open_param(const char *name, int flags, mode_t mode,
468 const struct llapi_stripe_param *param)
470 char fsname[MAX_OBD_NAME + 1] = { 0 };
471 char *pool_name = param->lsp_pool;
472 struct lov_user_md *lum = NULL;
473 size_t lum_size = sizeof(*lum);
476 /* Make sure we are on a Lustre file system */
477 rc = llapi_search_fsname(name, fsname);
479 llapi_error(LLAPI_MSG_ERROR, rc,
480 "'%s' is not on a Lustre filesystem",
485 /* Check if the stripe pattern is sane. */
486 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
487 param->lsp_stripe_offset,
488 param->lsp_stripe_count,
489 param->lsp_stripe_pattern);
493 /* Make sure we have a good pool */
494 if (pool_name != NULL) {
495 /* in case user gives the full pool name <fsname>.<poolname>,
496 * strip the fsname */
497 char *ptr = strchr(pool_name, '.');
500 if (strcmp(pool_name, fsname) != 0) {
502 llapi_err_noerrno(LLAPI_MSG_ERROR,
503 "Pool '%s' is not on filesystem '%s'",
510 /* Make sure the pool exists and is non-empty */
511 rc = llapi_search_ost(fsname, pool_name, NULL);
513 char *err = rc == 0 ? "has no OSTs" : "does not exist";
515 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
516 fsname, pool_name, err);
520 lum_size = sizeof(struct lov_user_md_v3);
523 /* sanity check of target list */
524 if (param->lsp_is_specific) {
525 char ostname[MAX_OBD_NAME + 1];
529 for (i = 0; i < param->lsp_stripe_count; i++) {
530 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
531 fsname, param->lsp_osts[i]);
532 rc = llapi_search_ost(fsname, pool_name, ostname);
537 llapi_error(LLAPI_MSG_ERROR, rc,
538 "%s: cannot find OST %s in %s",
545 /* Make sure stripe offset is in OST list. */
546 if (param->lsp_osts[i] == param->lsp_stripe_offset)
550 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
551 "%s: stripe offset '%d' is not in the "
553 __func__, param->lsp_stripe_offset);
557 lum_size = lov_user_md_size(param->lsp_stripe_count,
558 LOV_USER_MAGIC_SPECIFIC);
561 lum = calloc(1, lum_size);
566 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
568 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
569 flags = O_DIRECTORY | O_RDONLY;
576 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
581 /* Initialize IOCTL striping pattern structure */
582 lum->lmm_magic = LOV_USER_MAGIC_V1;
583 lum->lmm_pattern = param->lsp_stripe_pattern;
584 lum->lmm_stripe_size = param->lsp_stripe_size;
585 lum->lmm_stripe_count = param->lsp_stripe_count;
586 lum->lmm_stripe_offset = param->lsp_stripe_offset;
587 if (pool_name != NULL) {
588 struct lov_user_md_v3 *lumv3 = (void *)lum;
590 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
591 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
593 if (param->lsp_is_specific) {
594 struct lov_user_md_v3 *lumv3 = (void *)lum;
597 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
598 if (pool_name == NULL) {
599 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
600 * OST list, therefore if pool is not specified we have
601 * to pack a null pool name for placeholder. */
602 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
605 for (i = 0; i < param->lsp_stripe_count; i++)
606 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
609 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
610 char *errmsg = "stripe already set";
613 if (errno != EEXIST && errno != EALREADY)
614 errmsg = strerror(errno);
616 llapi_err_noerrno(LLAPI_MSG_ERROR,
617 "error on ioctl %#jx for '%s' (%d): %s",
618 (uintmax_t)LL_IOC_LOV_SETSTRIPE, name, fd,
630 int llapi_file_open_pool(const char *name, int flags, int mode,
631 unsigned long long stripe_size, int stripe_offset,
632 int stripe_count, int stripe_pattern, char *pool_name)
634 const struct llapi_stripe_param param = {
635 .lsp_stripe_size = stripe_size,
636 .lsp_stripe_count = stripe_count,
637 .lsp_stripe_pattern = stripe_pattern,
638 .lsp_stripe_offset = stripe_offset,
639 .lsp_pool = pool_name
641 return llapi_file_open_param(name, flags, mode, ¶m);
644 int llapi_file_open(const char *name, int flags, int mode,
645 unsigned long long stripe_size, int stripe_offset,
646 int stripe_count, int stripe_pattern)
648 return llapi_file_open_pool(name, flags, mode, stripe_size,
649 stripe_offset, stripe_count,
650 stripe_pattern, NULL);
653 int llapi_file_create(const char *name, unsigned long long stripe_size,
654 int stripe_offset, int stripe_count, int stripe_pattern)
658 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
659 stripe_offset, stripe_count, stripe_pattern,
668 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
669 int stripe_offset, int stripe_count,
670 int stripe_pattern, char *pool_name)
674 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
675 stripe_offset, stripe_count, stripe_pattern,
684 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
685 int stripe_count, int stripe_pattern,
686 const char *pool_name)
688 struct lmv_user_md lum = { 0 };
692 lum.lum_magic = LMV_USER_MAGIC;
693 lum.lum_stripe_offset = stripe_offset;
694 lum.lum_stripe_count = stripe_count;
695 lum.lum_hash_type = stripe_pattern;
696 if (pool_name != NULL) {
697 if (strlen(pool_name) >= sizeof(lum.lum_pool_name)) {
698 llapi_err_noerrno(LLAPI_MSG_ERROR,
699 "error LL_IOC_LMV_SET_DEFAULT_STRIPE '%s'"
700 ": too large pool name: %s", name, pool_name);
703 strncpy(lum.lum_pool_name, pool_name,
704 sizeof(lum.lum_pool_name));
707 fd = open(name, O_DIRECTORY | O_RDONLY);
710 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
714 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lum);
716 char *errmsg = "stripe already set";
718 if (errno != EEXIST && errno != EALREADY)
719 errmsg = strerror(errno);
721 llapi_err_noerrno(LLAPI_MSG_ERROR,
722 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
729 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
730 int stripe_count, int stripe_pattern,
731 const char *pool_name)
733 struct lmv_user_md lmu = { 0 };
734 struct obd_ioctl_data data = { 0 };
737 char *dirpath = NULL;
738 char *namepath = NULL;
744 dirpath = strdup(name);
745 namepath = strdup(name);
746 if (!dirpath || !namepath)
749 lmu.lum_magic = LMV_USER_MAGIC;
750 lmu.lum_stripe_offset = stripe_offset;
751 lmu.lum_stripe_count = stripe_count;
752 lmu.lum_hash_type = stripe_pattern;
753 if (pool_name != NULL) {
754 if (strlen(pool_name) > LOV_MAXPOOLNAME) {
755 llapi_err_noerrno(LLAPI_MSG_ERROR,
756 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
757 "pool name: %s", name, pool_name);
761 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
764 filename = basename(namepath);
765 dir = dirname(dirpath);
767 data.ioc_inlbuf1 = (char *)filename;
768 data.ioc_inllen1 = strlen(filename) + 1;
769 data.ioc_inlbuf2 = (char *)&lmu;
770 data.ioc_inllen2 = sizeof(struct lmv_user_md);
771 data.ioc_type = mode;
772 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
774 llapi_error(LLAPI_MSG_ERROR, rc,
775 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
780 fd = open(dir, O_DIRECTORY | O_RDONLY);
783 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
787 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
788 char *errmsg = "stripe already set";
790 if (errno != EEXIST && errno != EALREADY)
791 errmsg = strerror(errno);
793 llapi_err_noerrno(LLAPI_MSG_ERROR,
794 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
804 int llapi_direntry_remove(char *dname)
806 char *dirpath = NULL;
807 char *namepath = NULL;
813 dirpath = strdup(dname);
814 namepath = strdup(dname);
815 if (!dirpath || !namepath)
818 filename = basename(namepath);
820 dir = dirname(dirpath);
822 fd = open(dir, O_DIRECTORY | O_RDONLY);
825 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
830 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
831 char *errmsg = strerror(errno);
832 llapi_err_noerrno(LLAPI_MSG_ERROR,
833 "error on ioctl %#jx for '%s' (%d): %s",
834 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
846 * Find the fsname, the full path, and/or an open fd.
847 * Either the fsname or path must not be NULL
849 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
852 char buf[PATH_MAX], mntdir[PATH_MAX];
855 int idx = 0, len = 0, mntlen, fd;
858 /* get the mount point */
859 fp = setmntent(PROC_MOUNTS, "r");
862 llapi_error(LLAPI_MSG_ERROR, rc,
863 "setmntent(%s) failed", PROC_MOUNTS);
867 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
870 if (!llapi_is_lustre_mnt(&mnt))
873 if ((want & WANT_INDEX) && (idx++ != index))
876 mntlen = strlen(mnt.mnt_dir);
877 ptr = strchr(mnt.mnt_fsname, '/');
878 while (ptr && *ptr == '/')
880 /* thanks to the call to llapi_is_lustre_mnt() above,
881 * we are sure that mnt.mnt_fsname contains ":/",
882 * so ptr should never be NULL */
886 while (*ptr_end != '/' && *ptr_end != '\0')
889 /* Check the fsname for a match, if given */
890 if (!(want & WANT_FSNAME) && fsname != NULL &&
891 (strlen(fsname) > 0) &&
892 (strncmp(ptr, fsname, ptr_end - ptr) != 0))
895 /* If the path isn't set return the first one we find */
896 if (path == NULL || strlen(path) == 0) {
897 strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
898 mntdir[strlen(mnt.mnt_dir)] = '\0';
899 if ((want & WANT_FSNAME) && fsname != NULL) {
900 strncpy(fsname, ptr, ptr_end - ptr);
901 fsname[ptr_end - ptr] = '\0';
905 /* Otherwise find the longest matching path */
906 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
907 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
908 strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
909 mntdir[strlen(mnt.mnt_dir)] = '\0';
911 if ((want & WANT_FSNAME) && fsname != NULL) {
912 strncpy(fsname, ptr, ptr_end - ptr);
913 fsname[ptr_end - ptr] = '\0';
922 if ((want & WANT_PATH) && path != NULL) {
923 strncpy(path, mntdir, strlen(mntdir));
924 path[strlen(mntdir)] = '\0';
926 if (want & WANT_FD) {
927 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
930 llapi_error(LLAPI_MSG_ERROR, rc,
931 "error opening '%s'", mntdir);
937 } else if (want & WANT_ERROR)
938 llapi_err_noerrno(LLAPI_MSG_ERROR,
939 "can't find fs root for '%s': %d",
940 (want & WANT_PATH) ? fsname : path, rc);
945 * search lustre mounts
947 * Calling this function will return to the user the mount point, mntdir, and
948 * the file system name, fsname, if the user passed a buffer to this routine.
950 * The user inputs are pathname and index. If the pathname is supplied then
951 * the value of the index will be ignored. The pathname will return data if
952 * the pathname is located on a lustre mount. Index is used to pick which
953 * mount point you want in the case of multiple mounted lustre file systems.
954 * See function lfs_osts in lfs.c for an example of the index use.
956 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
959 int want = WANT_PATH, idx = -1;
961 if (!pathname || pathname[0] == '\0') {
965 strcpy(mntdir, pathname);
969 return get_root_path(want, fsname, NULL, mntdir, idx);
972 /* Given a path, find the corresponding Lustre fsname */
973 int llapi_search_fsname(const char *pathname, char *fsname)
978 path = realpath(pathname, NULL);
980 char buf[PATH_MAX], *ptr;
983 if (pathname[0] != '/') {
984 /* Need an absolute path, but realpath() only works for
985 * pathnames that actually exist. We go through the
986 * extra hurdle of dirname(getcwd() + pathname) in
987 * case the relative pathname contains ".." in it. */
988 if (getcwd(buf, sizeof(buf) - 2) == NULL)
990 rc = strlcat(buf, "/", sizeof(buf));
991 if (rc >= sizeof(buf))
994 rc = strlcat(buf, pathname, sizeof(buf));
995 if (rc >= sizeof(buf))
997 path = realpath(buf, NULL);
999 ptr = strrchr(buf, '/');
1003 path = realpath(buf, NULL);
1006 llapi_error(LLAPI_MSG_ERROR, rc,
1007 "pathname '%s' cannot expand",
1013 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1018 int llapi_search_rootpath(char *pathname, const char *fsname)
1020 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1023 int llapi_getname(const char *path, char *buf, size_t size)
1025 struct obd_uuid uuid_buf;
1026 char *uuid = uuid_buf.uuid;
1029 memset(&uuid_buf, 0, sizeof(uuid_buf));
1030 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1034 /* We want to turn lustre-clilov-ffff88002738bc00 into
1035 * lustre-ffff88002738bc00. */
1037 nr = snprintf(buf, size, "%.*s-%s",
1038 (int) (strlen(uuid) - 24), uuid,
1039 uuid + strlen(uuid) - 16);
1048 * Get the list of pool members.
1049 * \param poolname string of format \<fsname\>.\<poolname\>
1050 * \param members caller-allocated array of char*
1051 * \param list_size size of the members array
1052 * \param buffer caller-allocated buffer for storing OST names
1053 * \param buffer_size size of the buffer
1055 * \return number of members retrieved for this pool
1056 * \retval -error failure
1058 int llapi_get_poolmembers(const char *poolname, char **members,
1059 int list_size, char *buffer, int buffer_size)
1061 char fsname[PATH_MAX];
1070 /* name is FSNAME.POOLNAME */
1071 if (strlen(poolname) >= sizeof(fsname))
1073 strlcpy(fsname, poolname, sizeof(fsname));
1074 pool = strchr(fsname, '.');
1081 rc = poolpath(&pathname, fsname, NULL);
1083 llapi_error(LLAPI_MSG_ERROR, rc,
1084 "Lustre filesystem '%s' not found",
1089 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1090 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1091 cfs_free_param_data(&pathname);
1092 if (rc >= sizeof(buf))
1094 fd = fopen(buf, "r");
1097 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1102 while (fgets(buf, sizeof(buf), fd) != NULL) {
1103 if (nb_entries >= list_size) {
1107 buf[sizeof(buf) - 1] = '\0';
1109 tmp = strchr(buf, '\n');
1112 if (used + strlen(buf) + 1 > buffer_size) {
1117 strcpy(buffer + used, buf);
1118 members[nb_entries] = buffer + used;
1119 used += strlen(buf) + 1;
1129 * Get the list of pools in a filesystem.
1130 * \param name filesystem name or path
1131 * \param poollist caller-allocated array of char*
1132 * \param list_size size of the poollist array
1133 * \param buffer caller-allocated buffer for storing pool names
1134 * \param buffer_size size of the buffer
1136 * \return number of pools retrieved for this filesystem
1137 * \retval -error failure
1139 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1140 char *buffer, int buffer_size)
1142 char rname[PATH_MAX];
1148 struct dirent *cookie = NULL;
1150 unsigned int nb_entries = 0;
1151 unsigned int used = 0;
1154 /* initialize output array */
1155 for (i = 0; i < list_size; i++)
1158 /* is name a pathname ? */
1159 ptr = strchr(name, '/');
1161 /* only absolute pathname is supported */
1165 if (!realpath(name, rname)) {
1167 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1172 fsname = strdup(rname);
1176 rc = poolpath(&pathname, NULL, rname);
1178 /* name is FSNAME */
1179 fsname = strdup(name);
1182 rc = poolpath(&pathname, fsname, NULL);
1185 llapi_error(LLAPI_MSG_ERROR, rc,
1186 "Lustre filesystem '%s' not found", name);
1190 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1191 dir = opendir(pathname.gl_pathv[0]);
1194 llapi_error(LLAPI_MSG_ERROR, rc,
1195 "Could not open pool list for '%s'",
1201 rc = readdir_r(dir, &pool, &cookie);
1204 llapi_error(LLAPI_MSG_ERROR, rc,
1205 "Error reading pool list for '%s'", name);
1207 } else if ((rc == 0) && (cookie == NULL)) {
1208 /* end of directory */
1212 /* ignore . and .. */
1213 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1216 /* check output bounds */
1217 if (nb_entries >= list_size) {
1222 /* +2 for '.' and final '\0' */
1223 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1229 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1230 poollist[nb_entries] = buffer + used;
1231 used += strlen(pool.d_name) + strlen(fsname) + 2;
1238 cfs_free_param_data(&pathname);
1241 return rc != 0 ? rc : nb_entries;
1244 /* wrapper for lfs.c and obd.c */
1245 int llapi_poollist(const char *name)
1247 /* list of pool names (assume that pool count is smaller
1249 char **list, *buffer = NULL, *fsname = (char *)name;
1250 char *poolname = NULL, *tmp = NULL, data[16];
1251 enum param_filter type = FILTER_BY_PATH;
1252 int obdcount, bufsize, rc, nb, i;
1257 if (name[0] != '/') {
1258 fsname = strdup(name);
1262 poolname = strchr(fsname, '.');
1265 type = FILTER_BY_FS_NAME;
1268 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1269 data, sizeof(data));
1272 obdcount = atoi(data);
1274 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1275 * and also an array to store the pointers for all that
1276 * allocated space. */
1278 bufsize = sizeof(struct obd_uuid) * obdcount;
1279 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1280 if (buffer == NULL) {
1284 list = (char **) (buffer + bufsize);
1287 /* name is a path or fsname */
1288 nb = llapi_get_poollist(name, list, obdcount,
1291 /* name is a pool name (<fsname>.<poolname>) */
1292 nb = llapi_get_poolmembers(name, list, obdcount,
1296 if (nb == -EOVERFLOW) {
1299 goto retry_get_pools;
1302 for (i = 0; i < nb; i++)
1303 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1304 rc = (nb < 0 ? nb : 0);
1308 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1313 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1314 void *data, struct dirent64 *de);
1316 #define OBD_NOT_FOUND (-1)
1318 static int common_param_init(struct find_param *param, char *path)
1320 int lum_size = get_mds_md_size(path);
1322 if (lum_size < PATH_MAX + 1)
1323 lum_size = PATH_MAX + 1;
1325 param->fp_lum_size = lum_size;
1326 param->fp_lmd = calloc(1, sizeof(lstat_t) + param->fp_lum_size);
1327 if (param->fp_lmd == NULL) {
1328 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1329 "error: allocation of %zu bytes for ioctl",
1330 sizeof(lstat_t) + param->fp_lum_size);
1334 param->fp_lmv_stripe_count = 256;
1335 param->fp_lmv_md = calloc(1,
1336 lmv_user_md_size(param->fp_lmv_stripe_count,
1338 if (param->fp_lmv_md == NULL) {
1339 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1340 "error: allocation of %d bytes for ioctl",
1341 lmv_user_md_size(param->fp_lmv_stripe_count,
1346 param->fp_got_uuids = 0;
1347 param->fp_obd_indexes = NULL;
1348 param->fp_obd_index = OBD_NOT_FOUND;
1349 if (!param->fp_migrate)
1350 param->fp_mdt_index = OBD_NOT_FOUND;
1354 static void find_param_fini(struct find_param *param)
1356 if (param->fp_obd_indexes)
1357 free(param->fp_obd_indexes);
1360 free(param->fp_lmd);
1362 if (param->fp_lmv_md)
1363 free(param->fp_lmv_md);
1366 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1367 struct dirent64 *de)
1369 struct find_param *param = data;
1375 /* set errno upon failure */
1376 static DIR *opendir_parent(const char *path)
1382 path_copy = strdup(path);
1383 if (path_copy == NULL)
1386 parent_path = dirname(path_copy);
1387 parent = opendir(parent_path);
1393 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1398 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1399 if (param->fp_get_default_lmv)
1400 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1402 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1404 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1405 if (errno == E2BIG && ret != 0) {
1409 stripe_count = (__u32)param->fp_lmv_md->lum_stripe_count;
1410 if (stripe_count <= param->fp_lmv_stripe_count)
1413 free(param->fp_lmv_md);
1414 param->fp_lmv_stripe_count = stripe_count;
1415 lmv_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1416 param->fp_lmv_md = malloc(lmv_size);
1417 if (param->fp_lmv_md == NULL) {
1418 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1419 "error: allocation of %d bytes for ioctl",
1420 lmv_user_md_size(param->fp_lmv_stripe_count,
1429 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1430 struct lov_user_mds_data *lmd, int lumlen)
1432 lstat_t *st = &lmd->lmd_st;
1435 if (parent == NULL && dir == NULL)
1439 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1440 } else if (parent) {
1441 char *fname = strrchr(path, '/');
1443 /* To avoid opening, locking, and closing each file on the
1444 * client if that is not needed. The GETFILEINFO ioctl can
1445 * be done on the patent dir with a single open for all
1446 * files in that directory, and it also doesn't pollute the
1447 * client dcache with millions of dentries when traversing
1448 * a large filesystem. */
1449 fname = (fname == NULL ? path : fname + 1);
1450 /* retrieve needed file info */
1451 strlcpy((char *)lmd, fname, lumlen);
1452 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1456 if (errno == ENOTTY) {
1457 /* ioctl is not supported, it is not a lustre fs.
1458 * Do the regular lstat(2) instead. */
1459 ret = lstat_f(path, st);
1462 llapi_error(LLAPI_MSG_ERROR, ret,
1463 "error: %s: lstat failed for %s",
1466 } else if (errno == ENOENT) {
1468 llapi_error(LLAPI_MSG_WARN, ret,
1469 "warning: %s: %s does not exist",
1471 } else if (errno != EISDIR) {
1473 llapi_error(LLAPI_MSG_ERROR, ret,
1474 "%s ioctl failed for %s.",
1475 dir ? "LL_IOC_MDC_GETINFO" :
1476 "IOC_MDC_GETFILEINFO", path);
1479 llapi_error(LLAPI_MSG_ERROR, ret,
1480 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1487 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1488 semantic_func_t sem_init,
1489 semantic_func_t sem_fini, void *data,
1490 struct dirent64 *de)
1492 struct find_param *param = (struct find_param *)data;
1493 struct dirent64 *dent;
1501 if (!d && errno != ENOTDIR) {
1503 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1506 } else if (!d && !parent) {
1507 /* ENOTDIR. Open the parent dir. */
1508 p = opendir_parent(path);
1515 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1521 while ((dent = readdir64(d)) != NULL) {
1524 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1527 /* Don't traverse .lustre directory */
1528 if (!(strcmp(dent->d_name, dot_lustre_name)))
1532 if ((len + dent->d_reclen + 2) > size) {
1533 llapi_err_noerrno(LLAPI_MSG_ERROR,
1534 "error: %s: string buffer is too small",
1539 strcat(path, dent->d_name);
1541 if (dent->d_type == DT_UNKNOWN) {
1542 lstat_t *st = ¶m->fp_lmd->lmd_st;
1544 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
1545 param->fp_lum_size);
1547 dent->d_type = IFTODT(st->st_mode);
1554 switch (dent->d_type) {
1556 llapi_err_noerrno(LLAPI_MSG_ERROR,
1557 "error: %s: '%s' is UNKNOWN type %d",
1558 __func__, dent->d_name, dent->d_type);
1561 rc = llapi_semantic_traverse(path, size, d, sem_init,
1562 sem_fini, data, dent);
1563 if (rc != 0 && ret == 0)
1569 rc = sem_init(path, d, NULL, data, dent);
1570 if (rc < 0 && ret == 0)
1573 if (sem_fini && rc == 0)
1574 sem_fini(path, d, NULL, data, dent);
1582 sem_fini(path, parent, &d, data, de);
1591 static int param_callback(char *path, semantic_func_t sem_init,
1592 semantic_func_t sem_fini, struct find_param *param)
1594 int ret, len = strlen(path);
1597 if (len > PATH_MAX) {
1599 llapi_error(LLAPI_MSG_ERROR, ret,
1600 "Path name '%s' is too long", path);
1604 buf = (char *)malloc(PATH_MAX + 1);
1608 strlcpy(buf, path, PATH_MAX + 1);
1609 ret = common_param_init(param, buf);
1613 param->fp_depth = 0;
1615 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1616 sem_fini, param, NULL);
1618 find_param_fini(param);
1620 return ret < 0 ? ret : 0;
1623 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1625 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1628 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1633 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1635 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1638 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1643 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1647 fd = open(path, O_RDONLY | O_NONBLOCK);
1650 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1654 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1660 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1664 fd = open(path, O_RDONLY | O_NONBLOCK);
1667 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1671 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1683 * If uuidp is NULL, return the number of available obd uuids.
1684 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1685 * there are more OSTs than allocated to uuidp, then an error is returned with
1686 * the ost_count set to number of available obd uuids.
1688 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1689 int *ost_count, enum tgt_type type)
1691 char buf[PATH_MAX], format[32];
1692 int rc = 0, index = 0;
1693 struct obd_uuid name;
1697 /* Get the lov name */
1698 if (type == LOV_TYPE)
1699 rc = llapi_file_fget_lov_uuid(fd, &name);
1701 rc = llapi_file_fget_lmv_uuid(fd, &name);
1705 /* Now get the ost uuids */
1706 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
1707 FILTER_BY_EXACT, "target_obd", ¶m);
1711 fp = fopen(param.gl_pathv[0], "r");
1714 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
1719 snprintf(format, sizeof(format),
1720 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
1721 while (fgets(buf, sizeof(buf), fp) != NULL) {
1722 if (uuidp && (index < *ost_count)) {
1723 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
1731 if (uuidp && (index > *ost_count))
1736 cfs_free_param_data(¶m);
1740 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1742 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1745 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1750 root = open(mnt, O_RDONLY | O_DIRECTORY);
1753 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1758 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
1766 /* Check if user specified value matches a real uuid. Ignore _UUID,
1767 * -osc-4ba41334, other trailing gunk in comparison.
1768 * @param real_uuid ends in "_UUID"
1769 * @param search_uuid may or may not end in "_UUID"
1771 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1773 int cmplen = strlen(real_uuid);
1774 int searchlen = strlen(search_uuid);
1776 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1778 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1781 /* The UUIDs may legitimately be different lengths, if
1782 * the system was upgraded from an older version. */
1783 if (cmplen != searchlen)
1786 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1789 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
1790 * returned in param->fp_obd_index */
1791 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
1793 struct obd_uuid obd_uuid;
1800 if (param->fp_got_uuids)
1803 /* Get the lov/lmv name */
1804 if (param->fp_get_lmv)
1805 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
1807 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
1809 if (rc != -ENOTTY) {
1810 llapi_error(LLAPI_MSG_ERROR, rc,
1811 "error: can't get %s name: %s",
1812 param->fp_get_lmv ? "lmv" : "lov",
1820 param->fp_got_uuids = 1;
1822 /* Now get the ost uuids */
1823 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
1824 obd_uuid.uuid, FILTER_BY_EXACT,
1825 "target_obd", ¶m_data);
1829 fp = fopen(param_data.gl_pathv[0], "r");
1832 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
1833 param_data.gl_pathv[0]);
1837 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
1838 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1839 param->fp_get_lmv ? "MDTS" : "OBDS");
1841 snprintf(format, sizeof(format),
1842 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
1843 while (fgets(buf, sizeof(buf), fp) != NULL) {
1846 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
1849 if (param->fp_obd_uuid) {
1850 if (llapi_uuid_match(obd_uuid.uuid,
1851 param->fp_obd_uuid->uuid)) {
1852 param->fp_obd_index = index;
1855 } else if (!param->fp_quiet && !param->fp_obds_printed) {
1856 /* Print everything */
1857 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1860 param->fp_obds_printed = 1;
1864 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
1865 llapi_err_noerrno(LLAPI_MSG_ERROR,
1866 "error: %s: unknown obduuid: %s",
1867 __func__, param->fp_obd_uuid->uuid);
1871 cfs_free_param_data(¶m_data);
1875 /* In this case, param->fp_obd_uuid will be an array of obduuids and
1876 * obd index for all these obduuids will be returned in
1877 * param->fp_obd_indexes */
1878 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1879 int num_obds, int **obdindexes, int *obdindex,
1882 int ret, obdcount, obd_valid = 0, obdnum;
1884 struct obd_uuid *uuids = NULL;
1888 if (type == LOV_TYPE)
1889 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
1891 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
1895 obdcount = atoi(buf);
1896 uuids = malloc(obdcount * sizeof(struct obd_uuid));
1901 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1903 if (ret == -EOVERFLOW) {
1904 struct obd_uuid *uuids_temp;
1906 uuids_temp = realloc(uuids, obdcount *
1907 sizeof(struct obd_uuid));
1908 if (uuids_temp != NULL) {
1910 goto retry_get_uuids;
1915 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
1919 indexes = malloc(num_obds * sizeof(*obdindex));
1920 if (indexes == NULL) {
1925 for (obdnum = 0; obdnum < num_obds; obdnum++) {
1928 /* The user may have specified a simple index */
1929 i = strtol(obduuids[obdnum].uuid, &end, 0);
1930 if (end && *end == '\0' && i < obdcount) {
1931 indexes[obdnum] = i;
1934 for (i = 0; i < obdcount; i++) {
1935 if (llapi_uuid_match(uuids[i].uuid,
1936 obduuids[obdnum].uuid)) {
1937 indexes[obdnum] = i;
1943 if (i >= obdcount) {
1944 indexes[obdnum] = OBD_NOT_FOUND;
1945 llapi_err_noerrno(LLAPI_MSG_ERROR,
1946 "error: %s: unknown obduuid: %s",
1947 __func__, obduuids[obdnum].uuid);
1953 *obdindex = OBD_NOT_FOUND;
1955 *obdindex = obd_valid;
1957 *obdindexes = indexes;
1965 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
1969 if (param->fp_mdt_uuid) {
1970 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
1972 ¶m->fp_mdt_indexes,
1973 ¶m->fp_mdt_index, LMV_TYPE);
1978 if (param->fp_obd_uuid) {
1979 ret = setup_indexes(dir, path, param->fp_obd_uuid,
1981 ¶m->fp_obd_indexes,
1982 ¶m->fp_obd_index, LOV_TYPE);
1987 param->fp_got_uuids = 1;
1992 int llapi_ostlist(char *path, struct find_param *param)
1997 fd = open(path, O_RDONLY | O_DIRECTORY);
2001 ret = setup_obd_uuid(fd, path, param);
2008 * Tries to determine the default stripe attributes for a given filesystem. The
2009 * filesystem to check should be specified by fsname, or will be determined
2012 static int sattr_get_defaults(const char *const fsname,
2013 unsigned int *scount,
2014 unsigned int *ssize,
2015 unsigned int *soffset)
2021 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2022 "stripecount", val, sizeof(val));
2025 *scount = atoi(val);
2029 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2030 "stripesize", val, sizeof(val));
2037 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2038 "stripeoffset", val, sizeof(val));
2041 *soffset = atoi(val);
2048 * Tries to gather the default stripe attributes for a given filesystem. If
2049 * the attributes can be determined, they are cached for easy retreival the
2050 * next time they are needed. Only a single filesystem's attributes are
2053 int sattr_cache_get_defaults(const char *const fsname,
2054 const char *const pathname, unsigned int *scount,
2055 unsigned int *ssize, unsigned int *soffset)
2058 char fsname[PATH_MAX + 1];
2059 unsigned int stripecount;
2060 unsigned int stripesize;
2061 unsigned int stripeoffset;
2067 char fsname_buf[PATH_MAX + 1];
2068 unsigned int tmp[3];
2070 if (fsname == NULL) {
2071 rc = llapi_search_fsname(pathname, fsname_buf);
2075 strlcpy(fsname_buf, fsname, sizeof(fsname_buf));
2078 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2080 * Ensure all 3 sattrs (count, size, and offset) are
2081 * successfully retrieved and stored in tmp before writing to
2084 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2088 cache.stripecount = tmp[0];
2089 cache.stripesize = tmp[1];
2090 cache.stripeoffset = tmp[2];
2091 strlcpy(cache.fsname, fsname_buf, sizeof(cache.fsname));
2095 *scount = cache.stripecount;
2097 *ssize = cache.stripesize;
2099 *soffset = cache.stripeoffset;
2104 enum lov_dump_flags {
2105 LDF_IS_DIR = 0x0001,
2106 LDF_IS_RAW = 0x0002,
2107 LDF_INDENT = 0x0004,
2108 LDF_SKIP_OBJS = 0x0008,
2111 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2112 struct lov_user_ost_data_v1 *objects,
2113 int verbose, int depth, char *pool_name,
2114 enum lov_dump_flags flags)
2116 bool is_dir = flags & LDF_IS_DIR;
2117 bool is_raw = flags & LDF_IS_RAW;
2118 bool indent = flags & LDF_INDENT;
2119 bool skip_objs = flags & LDF_SKIP_OBJS;
2120 char *prefix = is_dir ? "" : "lmm_";
2121 char *separator = "";
2122 char *space = indent ? " " : "";
2125 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2126 lmm_oi_set_seq(&lum->lmm_oi, 0);
2127 if (!indent && (verbose & VERBOSE_DETAIL))
2128 llapi_printf(LLAPI_MSG_NORMAL, "%s(Default) ", space);
2131 if (!indent && depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2132 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2134 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2135 llapi_printf(LLAPI_MSG_NORMAL, "%s%smagic: 0x%08X\n",
2136 space, prefix, lum->lmm_magic);
2137 llapi_printf(LLAPI_MSG_NORMAL, "%s%sseq: %#jx\n",
2139 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2140 llapi_printf(LLAPI_MSG_NORMAL, "%s%sobject_id: %#jx\n",
2142 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2144 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2145 if (verbose & ~VERBOSE_DFID)
2146 llapi_printf(LLAPI_MSG_NORMAL, "%slmm_fid: ",
2148 /* This needs a bit of hand-holding since old 1.x lmm_oi
2149 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2150 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2151 * a real FID. Ideally the 2.x code would have stored this
2152 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2153 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2154 * worked properly (especially since IGIF FIDs use mds_inum as
2155 * the FID SEQ), but unfortunately that didn't happen.
2157 * Print it to look like an IGIF FID, even though the fields
2158 * are reversed on disk, so that it makes sense to userspace.
2160 * Don't use ostid_id() and ostid_seq(), since they assume the
2161 * oi_fid fields are in the right order. This is why there are
2162 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2164 * For newer layout types hopefully this will be a real FID. */
2165 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2166 lmm_oi_seq(&lum->lmm_oi) == 0 ?
2167 lmm_oi_id(&lum->lmm_oi) :
2168 lmm_oi_seq(&lum->lmm_oi),
2169 lmm_oi_seq(&lum->lmm_oi) == 0 ?
2170 0 : (__u32)lmm_oi_id(&lum->lmm_oi),
2171 (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32));
2174 if (verbose & VERBOSE_COUNT) {
2175 if (verbose & ~VERBOSE_COUNT)
2176 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_count: ",
2179 if (!is_raw && lum->lmm_stripe_count == 0) {
2180 unsigned int scount;
2181 rc = sattr_cache_get_defaults(NULL, path,
2185 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2188 llapi_error(LLAPI_MSG_ERROR, rc,
2189 "Cannot determine default"
2192 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2193 lum->lmm_stripe_count ==
2194 (typeof(lum->lmm_stripe_count))(-1)
2195 ? -1 : lum->lmm_stripe_count);
2198 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2199 (__s16)lum->lmm_stripe_count);
2201 separator = is_dir ? " " : "\n";
2204 if (verbose & VERBOSE_SIZE) {
2205 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2206 if (verbose & ~VERBOSE_SIZE)
2207 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_size: ",
2209 if (is_dir && !is_raw && lum->lmm_stripe_size == 0) {
2211 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2214 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2216 llapi_error(LLAPI_MSG_ERROR, rc,
2217 "Cannot determine default"
2220 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2221 lum->lmm_stripe_size);
2223 separator = is_dir ? " " : "\n";
2226 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2227 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2228 if (verbose & ~VERBOSE_LAYOUT)
2229 llapi_printf(LLAPI_MSG_NORMAL, "%s%spattern: ",
2231 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2235 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2236 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2237 if (verbose & ~VERBOSE_GENERATION)
2238 llapi_printf(LLAPI_MSG_NORMAL, "%s%slayout_gen: ",
2240 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2241 (int)lum->lmm_layout_gen);
2245 if (verbose & VERBOSE_OFFSET) {
2246 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2247 if (verbose & ~VERBOSE_OFFSET)
2248 llapi_printf(LLAPI_MSG_NORMAL, "%s%sstripe_offset: ",
2250 if (is_dir || skip_objs)
2251 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2252 lum->lmm_stripe_offset ==
2253 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2254 lum->lmm_stripe_offset);
2256 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2257 objects[0].l_ost_idx);
2258 separator = is_dir ? " " : "\n";
2261 if ((verbose & VERBOSE_POOL) && pool_name && (pool_name[0] != '\0')) {
2262 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2263 if (verbose & ~VERBOSE_POOL)
2264 llapi_printf(LLAPI_MSG_NORMAL, "%s%spool: ",
2266 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2267 separator = is_dir ? " " : "\n";
2270 if (strlen(separator) != 0)
2271 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2274 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2275 struct lov_user_ost_data_v1 *objects,
2276 char *path, int obdindex, int depth,
2277 int header, enum lov_dump_flags flags)
2279 bool is_dir = flags & LDF_IS_DIR;
2280 bool indent = flags & LDF_INDENT;
2281 bool skip_objs = flags & LDF_SKIP_OBJS;
2282 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2285 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2286 if (obdindex == objects[i].l_ost_idx) {
2296 lov_dump_user_lmm_header(lum, path, objects, header, depth, pool_name,
2299 if (!is_dir && !skip_objs && (header & VERBOSE_OBJID) &&
2300 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2301 char *space = " - ";
2304 llapi_printf(LLAPI_MSG_NORMAL,
2305 "%6slmm_objects:\n", " ");
2307 llapi_printf(LLAPI_MSG_NORMAL,
2308 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2310 for (i = 0; i < lum->lmm_stripe_count; i++) {
2311 int idx = objects[i].l_ost_idx;
2312 long long oid = ostid_id(&objects[i].l_ost_oi);
2313 long long gr = ostid_seq(&objects[i].l_ost_oi);
2315 if (obdindex != OBD_NOT_FOUND && obdindex != idx)
2319 struct lu_fid fid = { 0 };
2321 ostid_to_fid(&fid, &objects[i].l_ost_oi, idx);
2322 llapi_printf(LLAPI_MSG_NORMAL,
2323 "%s%d: { l_ost_idx: %d, l_fid: "DFID" }\n",
2324 space, i, idx, PFID(&fid));
2327 sprintf(fmt, "%s%s%s\n",
2328 "\t%6u\t%14llu\t%#13llx\t",
2329 (fid_seq_is_rsvd(gr) ||
2330 fid_seq_is_mdt0(gr)) ?
2331 "%14llu" : "%#14llx", "%s");
2332 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2334 obdindex == idx ? " *" : "");
2337 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2341 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2342 char *path, int obdindex, int depth, int verbose)
2344 struct lmv_user_mds_data *objects = lum->lum_objects;
2345 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2346 int i, obdstripe = 0;
2347 char *separator = "";
2349 if (obdindex != OBD_NOT_FOUND) {
2350 if (lum->lum_stripe_count == 0) {
2351 if (obdindex == lum->lum_stripe_offset)
2354 for (i = 0; i < lum->lum_stripe_count; i++) {
2355 if (obdindex == objects[i].lum_mds) {
2356 llapi_printf(LLAPI_MSG_NORMAL,
2371 /* show all information default */
2373 if (lum->lum_magic == LMV_USER_MAGIC)
2374 verbose = VERBOSE_POOL | VERBOSE_COUNT |
2375 VERBOSE_OFFSET | VERBOSE_HASH_TYPE;
2377 verbose = VERBOSE_OBJID;
2380 if (depth && path && ((verbose != VERBOSE_OBJID)))
2381 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2383 if (verbose & VERBOSE_COUNT) {
2384 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2385 if (verbose & ~VERBOSE_COUNT)
2386 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2387 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2388 (int)lum->lum_stripe_count);
2389 if (verbose & VERBOSE_OFFSET)
2395 if (verbose & VERBOSE_OFFSET) {
2396 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2397 if (verbose & ~VERBOSE_OFFSET)
2398 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2399 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2400 (int)lum->lum_stripe_offset);
2401 if (verbose & VERBOSE_HASH_TYPE)
2407 if (verbose & VERBOSE_HASH_TYPE) {
2408 unsigned int type = lum->lum_hash_type;
2410 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2411 if (verbose & ~VERBOSE_HASH_TYPE)
2412 llapi_printf(LLAPI_MSG_NORMAL, "lmv_hash_type: ");
2413 if (type < LMV_HASH_TYPE_MAX)
2414 llapi_printf(LLAPI_MSG_NORMAL, "%s",
2415 mdt_hash_name[type]);
2417 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2422 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2423 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2424 if (obdstripe == 1 && lum->lum_stripe_count > 0)
2425 llapi_printf(LLAPI_MSG_NORMAL,
2426 "mdtidx\t\t FID[seq:oid:ver]\n");
2427 for (i = 0; i < lum->lum_stripe_count; i++) {
2428 int idx = objects[i].lum_mds;
2429 struct lu_fid *fid = &objects[i].lum_fid;
2430 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2431 llapi_printf(LLAPI_MSG_NORMAL,
2432 "%6u\t\t "DFID"\t\t%s\n",
2434 obdindex == idx ? " *" : "");
2439 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
2440 pool_name[0] != '\0') {
2441 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2442 if (verbose & ~VERBOSE_POOL)
2443 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2445 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2449 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
2450 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2453 static void lov_dump_comp_v1_header(struct find_param *param, char *path,
2454 enum lov_dump_flags flags)
2456 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
2457 int depth = param->fp_max_depth;
2458 int verbose = param->fp_verbose;
2460 if (depth && path && ((verbose != VERBOSE_OBJID) ||
2461 !(flags & LDF_IS_DIR)))
2462 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2464 if (verbose & VERBOSE_DETAIL) {
2465 llapi_printf(LLAPI_MSG_NORMAL, "composite_header:\n");
2466 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_magic: 0x%08X\n",
2467 " ", comp_v1->lcm_magic);
2468 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_size: %u\n",
2469 " ", comp_v1->lcm_size);
2470 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_flags: %u\n",
2471 " ", comp_v1->lcm_flags);
2474 if (verbose & VERBOSE_GENERATION) {
2475 if (verbose & ~VERBOSE_GENERATION)
2476 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_layout_gen: ",
2478 llapi_printf(LLAPI_MSG_NORMAL, "%u\n", comp_v1->lcm_layout_gen);
2481 if (verbose & VERBOSE_COMP_COUNT) {
2482 if (verbose & ~VERBOSE_COMP_COUNT)
2483 llapi_printf(LLAPI_MSG_NORMAL, "%2slcm_entry_count: ",
2485 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
2486 comp_v1->lcm_entry_count);
2489 if (verbose & VERBOSE_DETAIL)
2490 llapi_printf(LLAPI_MSG_NORMAL, "components:\n");
2493 static void lov_dump_comp_v1_entry(struct find_param *param,
2494 enum lov_dump_flags flags, int index)
2496 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
2497 struct lov_comp_md_entry_v1 *entry;
2498 char *separator = "";
2499 int verbose = param->fp_verbose;
2501 entry = &comp_v1->lcm_entries[index];
2503 if (verbose & VERBOSE_COMP_ID) {
2504 if (verbose & VERBOSE_DETAIL)
2505 llapi_printf(LLAPI_MSG_NORMAL,
2506 "%slcme_id: ", " - ");
2507 else if (verbose & ~VERBOSE_COMP_ID)
2508 llapi_printf(LLAPI_MSG_NORMAL,
2509 "%4slcme_id: ", " ");
2510 if (!(flags & LDF_IS_DIR))
2511 llapi_printf(LLAPI_MSG_NORMAL, "%u", entry->lcme_id);
2513 llapi_printf(LLAPI_MSG_NORMAL, "N/A");
2517 if (verbose & VERBOSE_COMP_FLAGS) {
2518 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2519 if (verbose & ~VERBOSE_COMP_FLAGS)
2520 llapi_printf(LLAPI_MSG_NORMAL,
2521 "%4slcme_flags: ", " ");
2522 llapi_printf(LLAPI_MSG_NORMAL, "%#x", entry->lcme_flags);
2526 if (verbose & VERBOSE_COMP_START) {
2527 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2528 if (verbose & ~VERBOSE_COMP_START)
2529 llapi_printf(LLAPI_MSG_NORMAL,
2530 "%4slcme_extent.e_start: ", " ");
2531 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2532 entry->lcme_extent.e_start);
2536 if (verbose & VERBOSE_COMP_END) {
2537 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2538 if (verbose & ~VERBOSE_COMP_END)
2539 llapi_printf(LLAPI_MSG_NORMAL,
2540 "%4slcme_extent.e_end: ", " ");
2541 if (entry->lcme_extent.e_end == LUSTRE_EOF)
2542 llapi_printf(LLAPI_MSG_NORMAL, "%s", "EOF");
2544 llapi_printf(LLAPI_MSG_NORMAL, "%llu",
2545 entry->lcme_extent.e_end);
2549 if (verbose & VERBOSE_DETAIL) {
2550 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2551 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_offset: %u\n",
2552 " ", entry->lcme_offset);
2553 llapi_printf(LLAPI_MSG_NORMAL, "%4slcme_size: %u\n",
2554 " ", entry->lcme_size);
2555 llapi_printf(LLAPI_MSG_NORMAL, "%4ssub_layout:\n", " ");
2557 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2561 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2562 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2564 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2565 * The table below gives the answers for the specified parameters (value and
2566 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2567 * --------------------------------------
2568 * 1 | file > limit; sign > 0 | -1 / -1 |
2569 * 2 | file = limit; sign > 0 | -1 / -1 |
2570 * 3 | file < limit; sign > 0 | ? / 1 |
2571 * 4 | file > limit; sign = 0 | -1 / -1 |
2572 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2573 * 6 | file < limit; sign = 0 | ? / -1 |
2574 * 7 | file > limit; sign < 0 | 1 / 1 |
2575 * 8 | file = limit; sign < 0 | ? / -1 |
2576 * 9 | file < limit; sign < 0 | ? / -1 |
2577 * --------------------------------------
2578 * Note: 5th actually means that the value is within the interval
2579 * (limit - margin, limit]. */
2580 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2581 int sign, int negopt, unsigned long long margin,
2587 /* Drop the fraction of margin (of days). */
2588 if (file + margin <= limit)
2590 } else if (sign == 0) {
2591 if (file <= limit && file + margin > limit)
2593 else if (file + margin <= limit)
2595 } else if (sign < 0) {
2602 return negopt ? ~ret + 1 : ret;
2605 static inline struct lov_user_md *
2606 lov_comp_entry(struct lov_comp_md_v1 *comp_v1, int ent_idx)
2608 return (struct lov_user_md *)((char *)comp_v1 +
2609 comp_v1->lcm_entries[ent_idx].lcme_offset);
2612 static inline struct lov_user_ost_data_v1 *
2613 lov_v1v3_objects(struct lov_user_md *v1)
2615 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
2616 return ((struct lov_user_md_v3 *)v1)->lmm_objects;
2618 return v1->lmm_objects;
2622 lov_v1v3_pool_name(struct lov_user_md *v1, char *pool_name)
2624 if (v1->lmm_magic == LOV_USER_MAGIC_V3)
2625 strlcpy(pool_name, ((struct lov_user_md_v3 *)v1)->lmm_pool_name,
2628 pool_name[0] = '\0';
2632 * An example of "getstripe -v" for a two components PFL file:
2635 * lcm_magic: 0x0BD60BD0
2639 * lcm_entry_count: 2
2643 * lcme_extent.e_start: 0
2644 * lcme_extent.e_end: 1048576
2648 * lmm_magic: 0x0BD10BD0
2649 * lmm_seq: 0x200000401
2650 * lmm_object_id: 0x1
2651 * lmm_fid: [0x200000401:0x1:0x0]
2652 * lmm_stripe_count: 1
2653 * lmm_stripe_size: 1048576
2656 * lmm_stripe_offset: 0
2658 * - 0: { l_ost_idx: 0, l_fid: [0x100000000:0x2:0x0] }
2662 * lcme_extent.e_start: 1048576
2663 * lcme_extent.e_end: EOF
2667 * lmm_magic: 0x0BD10BD0
2668 * lmm_seq: 0x200000401
2669 * lmm_object_id: 0x1
2670 * lmm_fid: [0x200000401:0x1:0x0]
2671 * lmm_stripe_count: 2
2672 * lmm_stripe_size: 1048576
2675 * lmm_stripe_offset: 1
2677 * - 0: { l_ost_idx: 1, l_fid: [0x100010000:0x2:0x0] }
2678 * - 1: { l_ost_idx: 0, l_fid: [0x100000000:0x3:0x0] }
2680 static void lov_dump_comp_v1(struct find_param *param, char *path,
2681 enum lov_dump_flags flags)
2683 struct lov_comp_md_entry_v1 *entry;
2684 struct lov_user_ost_data_v1 *objects;
2685 struct lov_comp_md_v1 *comp_v1 = (void *)¶m->fp_lmd->lmd_lmm;
2686 struct lov_user_md_v1 *v1;
2687 char pool_name[LOV_MAXPOOLNAME + 1];
2688 int obdindex = param->fp_obd_index;
2689 int i, j, match, obdstripe = 0;
2691 if (obdindex != OBD_NOT_FOUND) {
2692 for (i = 0; !(flags & LDF_IS_DIR) &&
2693 i < comp_v1->lcm_entry_count; i++) {
2694 if (!(comp_v1->lcm_entries[i].lcme_flags &
2698 v1 = lov_comp_entry(comp_v1, i);
2699 objects = lov_v1v3_objects(v1);
2701 for (j = 0; j < v1->lmm_stripe_count; j++) {
2702 if (obdindex == objects[j].l_ost_idx) {
2715 lov_dump_comp_v1_header(param, path, flags);
2717 flags |= LDF_INDENT;
2719 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2720 entry = &comp_v1->lcm_entries[i];
2722 if (param->fp_check_comp_flags &&
2723 !(param->fp_comp_flags & entry->lcme_flags))
2726 if (entry->lcme_flags & LCME_FL_INIT)
2727 flags &= ~LDF_SKIP_OBJS;
2729 flags |= LDF_SKIP_OBJS;
2731 if (param->fp_check_comp_id &&
2732 param->fp_comp_id != entry->lcme_id)
2735 if (param->fp_check_comp_start) {
2736 match = find_value_cmp(entry->lcme_extent.e_start,
2737 param->fp_comp_start,
2738 param->fp_comp_start_sign,
2740 param->fp_comp_start_units, 0);
2745 if (param->fp_check_comp_end) {
2746 unsigned long long margin;
2748 margin = entry->lcme_extent.e_end == LUSTRE_EOF ?
2749 0 : param->fp_comp_end_units;
2751 match = find_value_cmp(entry->lcme_extent.e_end,
2753 param->fp_comp_end_sign,
2759 lov_dump_comp_v1_entry(param, flags, i);
2761 v1 = lov_comp_entry(comp_v1, i);
2762 objects = lov_v1v3_objects(v1);
2763 lov_v1v3_pool_name(v1, pool_name);
2765 lov_dump_user_lmm_v1v3(v1, pool_name, objects, path, obdindex,
2766 param->fp_max_depth, param->fp_verbose,
2771 static void llapi_lov_dump_user_lmm(struct find_param *param, char *path,
2772 enum lov_dump_flags flags)
2776 if (param->fp_get_lmv || param->fp_get_default_lmv)
2777 magic = (__u32)param->fp_lmv_md->lum_magic;
2779 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
2782 flags |= LDF_IS_RAW;
2785 case LOV_USER_MAGIC_V1:
2786 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
2787 param->fp_lmd->lmd_lmm.lmm_objects,
2788 path, param->fp_obd_index,
2789 param->fp_max_depth, param->fp_verbose,
2792 case LOV_USER_MAGIC_V3: {
2793 char pool_name[LOV_MAXPOOLNAME + 1];
2794 struct lov_user_ost_data_v1 *objects;
2795 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2797 strlcpy(pool_name, lmmv3->lmm_pool_name, sizeof(pool_name));
2798 objects = lmmv3->lmm_objects;
2799 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, pool_name,
2800 objects, path, param->fp_obd_index,
2801 param->fp_max_depth, param->fp_verbose,
2806 case LMV_USER_MAGIC: {
2807 char pool_name[LOV_MAXPOOLNAME + 1];
2808 struct lmv_user_md *lum;
2810 lum = (struct lmv_user_md *)param->fp_lmv_md;
2811 strlcpy(pool_name, lum->lum_pool_name, sizeof(pool_name));
2812 lmv_dump_user_lmm(lum, pool_name, path, param->fp_obd_index,
2813 param->fp_max_depth, param->fp_verbose);
2816 case LOV_USER_MAGIC_COMP_V1:
2817 lov_dump_comp_v1(param, path, flags);
2820 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2821 "(expecting one of %#x %#x %#x %#x)\n",
2822 *(__u32 *)¶m->fp_lmd->lmd_lmm,
2823 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2824 LMV_USER_MAGIC, LMV_MAGIC_V1);
2829 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2835 fname = strrchr(path, '/');
2837 /* It should be a file (or other non-directory) */
2838 if (fname == NULL) {
2839 dname = (char *)malloc(2);
2843 fname = (char *)path;
2845 dname = (char *)malloc(fname - path + 1);
2848 strncpy(dname, path, fname - path);
2849 dname[fname - path] = '\0';
2853 fd = open(dname, O_RDONLY | O_NONBLOCK);
2860 strcpy((char *)lum, fname);
2861 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2864 if (close(fd) == -1 && rc == 0)
2871 int llapi_file_lookup(int dirfd, const char *name)
2873 struct obd_ioctl_data data = { 0 };
2878 if (dirfd < 0 || name == NULL)
2881 data.ioc_version = OBD_IOCTL_VERSION;
2882 data.ioc_len = sizeof(data);
2883 data.ioc_inlbuf1 = (char *)name;
2884 data.ioc_inllen1 = strlen(name) + 1;
2886 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2888 llapi_error(LLAPI_MSG_ERROR, rc,
2889 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2894 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2900 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2901 * Return -1 or 1 if file timestamp does not or does match the given criteria
2902 * correspondingly. Return 0 if the MDS time is being checked and there are
2903 * attributes on OSTs and it is not yet clear if the timespamp matches.
2905 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2906 * updated timestamps. */
2907 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2912 /* Check if file is accepted. */
2913 if (param->fp_atime) {
2914 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
2915 param->fp_asign, param->fp_exclude_atime,
2922 if (param->fp_mtime) {
2923 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
2924 param->fp_msign, param->fp_exclude_mtime,
2929 /* If the previous check matches, but this one is not yet clear,
2930 * we should return 0 to do an RPC on OSTs. */
2935 if (param->fp_ctime) {
2936 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
2937 param->fp_csign, param->fp_exclude_ctime,
2942 /* If the previous check matches, but this one is not yet clear,
2943 * we should return 0 to do an RPC on OSTs. */
2952 * Check whether the stripes matches the indexes user provided
2956 static int check_obd_match(struct find_param *param)
2958 struct lov_user_ost_data_v1 *objects;
2959 struct lov_comp_md_v1 *comp_v1 = NULL;
2960 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
2961 lstat_t *st = ¶m->fp_lmd->lmd_st;
2962 int i, j, k, count = 1;
2964 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
2967 if (!S_ISREG(st->st_mode))
2970 /* Only those files should be accepted, which have a
2971 * stripe on the specified OST. */
2972 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
2973 comp_v1 = (struct lov_comp_md_v1 *)v1;
2974 count = comp_v1->lcm_entry_count;
2977 for (i = 0; i < count; i++) {
2979 v1 = lov_comp_entry(comp_v1, i);
2981 objects = lov_v1v3_objects(v1);
2983 for (j = 0; j < v1->lmm_stripe_count; j++) {
2984 for (k = 0; k < param->fp_num_obds; k++) {
2985 if (param->fp_obd_indexes[k] ==
2986 objects[j].l_ost_idx)
2987 return !param->fp_exclude_obd;
2992 return param->fp_exclude_obd;
2995 static int check_mdt_match(struct find_param *param)
2999 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
3002 /* FIXME: For striped dir, we should get stripe information and check */
3003 for (i = 0; i < param->fp_num_mdts; i++) {
3004 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
3005 return !param->fp_exclude_mdt;
3008 if (param->fp_exclude_mdt)
3015 * Check whether the obd is active or not, if it is
3016 * not active, just print the object affected by this
3019 static int print_failed_tgt(struct find_param *param, char *path, int type)
3021 struct obd_statfs stat_buf;
3022 struct obd_uuid uuid_buf;
3025 if (type != LL_STATFS_LOV && type != LL_STATFS_LMV)
3028 memset(&stat_buf, 0, sizeof(struct obd_statfs));
3029 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
3030 ret = llapi_obd_statfs(path, type,
3031 param->fp_obd_index, &stat_buf,
3034 llapi_printf(LLAPI_MSG_NORMAL,
3035 "obd_uuid: %s failed %s ",
3036 param->fp_obd_uuid->uuid,
3043 static int find_check_stripe_size(struct find_param *param)
3045 struct lov_comp_md_v1 *comp_v1 = NULL;
3046 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3047 int ret, i, count = 1;
3049 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3050 comp_v1 = (struct lov_comp_md_v1 *)v1;
3051 count = comp_v1->lcm_entry_count;
3052 ret = param->fp_exclude_stripe_size ? 1 : -1;
3055 for (i = 0; i < count; i++) {
3057 v1 = lov_comp_entry(comp_v1, i);
3059 ret = find_value_cmp(v1->lmm_stripe_size, param->fp_stripe_size,
3060 param->fp_stripe_size_sign,
3061 param->fp_exclude_stripe_size,
3062 param->fp_stripe_size_units, 0);
3063 /* If any stripe_size matches */
3071 static __u32 find_get_stripe_count(struct find_param *param)
3073 struct lov_comp_md_v1 *comp_v1 = NULL;
3074 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3076 __u32 stripe_count = 0;
3078 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3079 comp_v1 = (struct lov_comp_md_v1 *)v1;
3080 count = comp_v1->lcm_entry_count;
3083 for (i = 0; i < count; i++) {
3085 v1 = lov_comp_entry(comp_v1, i);
3086 stripe_count += v1->lmm_stripe_count;
3089 return stripe_count;
3092 #define LOV_PATTERN_INVALID 0xFFFFFFFF
3094 static int find_check_layout(struct find_param *param)
3096 struct lov_comp_md_v1 *comp_v1 = NULL;
3097 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3099 bool found = false, valid = false;
3101 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3102 comp_v1 = (struct lov_comp_md_v1 *)v1;
3103 count = comp_v1->lcm_entry_count;
3106 for (i = 0; i < count; i++) {
3108 v1 = lov_comp_entry(comp_v1, i);
3110 if (v1->lmm_pattern == LOV_PATTERN_INVALID)
3114 if (v1->lmm_pattern & param->fp_layout) {
3123 if ((found && !param->fp_exclude_layout) ||
3124 (!found && param->fp_exclude_layout))
3130 static int find_check_pool(struct find_param *param)
3132 struct lov_comp_md_v1 *comp_v1 = NULL;
3133 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3134 struct lov_user_md_v3 *v3 = (void *)v1;
3138 if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
3139 comp_v1 = (struct lov_comp_md_v1 *)v1;
3140 count = comp_v1->lcm_entry_count;
3141 /* empty requested pool is taken as no pool search */
3142 if (count == 0 && param->fp_poolname[0] == '\0')
3146 for (i = 0; i < count; i++) {
3147 if (comp_v1 != NULL)
3148 v1 = lov_comp_entry(comp_v1, i);
3150 if (((v1->lmm_magic == LOV_USER_MAGIC_V1) &&
3151 (param->fp_poolname[0] == '\0')) ||
3152 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
3153 (strncmp(v3->lmm_pool_name,
3154 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
3155 ((v1->lmm_magic == LOV_USER_MAGIC_V3) &&
3156 (strcmp(param->fp_poolname, "*") == 0))) {
3162 if ((found && !param->fp_exclude_pool) ||
3163 (!found && param->fp_exclude_pool))
3169 static int find_check_comp_options(struct find_param *param)
3171 struct lov_comp_md_v1 *comp_v1;
3172 struct lov_user_md_v1 *v1 = ¶m->fp_lmd->lmd_lmm;
3173 struct lov_comp_md_entry_v1 *entry;
3176 if (v1->lmm_magic != LOV_USER_MAGIC_COMP_V1) {
3177 if ((param->fp_check_comp_count &&
3178 !param->fp_exclude_comp_count) ||
3179 (param->fp_check_comp_flags &&
3180 !param->fp_exclude_comp_flags) ||
3181 (param->fp_check_comp_start &&
3182 !param->fp_exclude_comp_start) ||
3183 (param->fp_check_comp_end &&
3184 !param->fp_exclude_comp_end))
3190 comp_v1 = (struct lov_comp_md_v1 *)v1;
3192 if (param->fp_check_comp_count) {
3193 ret = find_value_cmp(comp_v1->lcm_entry_count,
3194 param->fp_comp_count,
3195 param->fp_comp_count_sign,
3196 param->fp_exclude_comp_count, 1, 0);
3201 if (comp_v1->lcm_entry_count == 0) {
3202 if ((param->fp_check_comp_flags &&
3203 !param->fp_exclude_comp_flags) ||
3204 (param->fp_check_comp_start &&
3205 !param->fp_exclude_comp_start) ||
3206 (param->fp_check_comp_end &&
3207 !param->fp_exclude_comp_end))
3211 if (param->fp_check_comp_flags) {
3212 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3213 entry = &comp_v1->lcm_entries[i];
3215 if (((entry->lcme_flags & param->fp_comp_flags) &&
3216 param->fp_exclude_comp_flags) ||
3217 (!(entry->lcme_flags & param->fp_comp_flags) &&
3218 !param->fp_exclude_comp_flags))
3222 /* If any flags matches */
3230 if (param->fp_check_comp_start) {
3231 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3232 entry = &comp_v1->lcm_entries[i];
3234 ret = find_value_cmp(entry->lcme_extent.e_start,
3235 param->fp_comp_start,
3236 param->fp_comp_start_sign,
3237 param->fp_exclude_comp_start,
3238 param->fp_comp_start_units, 0);
3239 /* If any extent start matches */
3247 if (param->fp_check_comp_end) {
3248 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3249 unsigned long long margin;
3250 entry = &comp_v1->lcm_entries[i];
3252 margin = entry->lcme_extent.e_end == LUSTRE_EOF ?
3253 0 : param->fp_comp_end_units;
3255 ret = find_value_cmp(entry->lcme_extent.e_end,
3257 param->fp_comp_end_sign,
3258 param->fp_exclude_comp_end, margin,
3260 /* If any extent end matches */
3271 static bool find_check_lmm_info(struct find_param *param)
3273 return param->fp_check_pool || param->fp_check_stripe_count ||
3274 param->fp_check_stripe_size || param->fp_check_layout ||
3275 param->fp_check_comp_count || param->fp_check_comp_end ||
3276 param->fp_check_comp_start || param->fp_check_comp_flags;
3279 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
3280 void *data, struct dirent64 *de)
3282 struct find_param *param = (struct find_param *)data;
3283 DIR *dir = dirp == NULL ? NULL : *dirp;
3284 int decision = 1; /* 1 is accepted; -1 is rejected. */
3285 lstat_t *st = ¶m->fp_lmd->lmd_st;
3287 int checked_type = 0;
3289 __u32 stripe_count = 0;
3291 if (parent == NULL && dir == NULL)
3294 /* If a regular expression is presented, make the initial decision */
3295 if (param->fp_pattern != NULL) {
3296 char *fname = strrchr(path, '/');
3297 fname = (fname == NULL ? path : fname + 1);
3298 ret = fnmatch(param->fp_pattern, fname, 0);
3299 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
3300 (ret == 0 && param->fp_exclude_pattern))
3304 /* See if we can check the file type from the dirent. */
3305 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
3308 if (DTTOIF(de->d_type) == param->fp_type) {
3309 if (param->fp_exclude_type)
3312 if (!param->fp_exclude_type)
3319 /* Request MDS for the stat info if some of these parameters need
3320 * to be compared. */
3321 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
3322 param->fp_check_uid || param->fp_check_gid ||
3323 param->fp_atime || param->fp_mtime || param->fp_ctime ||
3324 param->fp_check_size || find_check_lmm_info(param))
3327 if (param->fp_type != 0 && checked_type == 0)
3330 if (decision == 0) {
3331 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
3332 param->fp_lum_size);
3333 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
3334 find_check_lmm_info(param)) {
3335 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
3337 /* We need to "fake" the "use the default" values
3338 * since the lmm struct is zeroed out at this point. */
3339 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3340 lmm->lmm_pattern = 0xFFFFFFFF;
3342 ostid_set_seq(&lmm->lmm_oi,
3343 FID_SEQ_LOV_DEFAULT);
3344 lmm->lmm_stripe_size = 0;
3345 lmm->lmm_stripe_count = 0;
3346 lmm->lmm_stripe_offset = -1;
3348 if (ret == 0 && param->fp_mdt_uuid != NULL) {
3350 ret = llapi_file_fget_mdtidx(dirfd(dir),
3351 ¶m->fp_file_mdt_index);
3352 } else if (S_ISREG(st->st_mode)) {
3355 /* FIXME: we could get the MDT index from the
3356 * file's FID in lmd->lmd_lmm.lmm_oi without
3357 * opening the file, once we are sure that
3358 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
3359 * That would still be an ioctl() to map the
3360 * FID to the MDT, but not an open RPC. */
3361 fd = open(path, O_RDONLY);
3363 ret = llapi_file_fget_mdtidx(fd,
3364 ¶m->fp_file_mdt_index);
3370 /* For a special file, we assume it resides on
3371 * the same MDT as the parent directory. */
3372 ret = llapi_file_fget_mdtidx(dirfd(parent),
3373 ¶m->fp_file_mdt_index);
3384 stripe_count = find_get_stripe_count(param);
3388 if (param->fp_type && !checked_type) {
3389 if ((st->st_mode & S_IFMT) == param->fp_type) {
3390 if (param->fp_exclude_type)
3393 if (!param->fp_exclude_type)
3399 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
3400 if (lustre_fs && param->fp_got_uuids &&
3401 param->fp_dev != st->st_dev) {
3402 /* A lustre/lustre mount point is crossed. */
3403 param->fp_got_uuids = 0;
3404 param->fp_obds_printed = 0;
3405 param->fp_mdt_index = OBD_NOT_FOUND;
3406 param->fp_obd_index = OBD_NOT_FOUND;
3409 if (lustre_fs && !param->fp_got_uuids) {
3410 ret = setup_target_indexes(dir ? dir : parent, path,
3415 param->fp_dev = st->st_dev;
3416 } else if (!lustre_fs && param->fp_got_uuids) {
3417 /* A lustre/non-lustre mount point is crossed. */
3418 param->fp_got_uuids = 0;
3419 param->fp_mdt_index = OBD_NOT_FOUND;
3420 param->fp_obd_index = OBD_NOT_FOUND;
3424 if (param->fp_check_stripe_size) {
3425 decision = find_check_stripe_size(param);
3430 if (param->fp_check_stripe_count) {
3431 decision = find_value_cmp(stripe_count, param->fp_stripe_count,
3432 param->fp_stripe_count_sign,
3433 param->fp_exclude_stripe_count, 1, 0);
3438 if (param->fp_check_layout) {
3439 decision = find_check_layout(param);
3444 /* If an OBD UUID is specified but none matches, skip this file. */
3445 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
3446 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
3449 /* If an OST or MDT UUID is given, and some OST matches,
3451 if (param->fp_obd_index != OBD_NOT_FOUND ||
3452 param->fp_mdt_index != OBD_NOT_FOUND) {
3453 if (param->fp_obd_uuid) {
3454 if (check_obd_match(param)) {
3455 /* If no mdtuuid is given, we are done.
3456 * Otherwise, fall through to the mdtuuid
3458 if (!param->fp_mdt_uuid)
3465 if (param->fp_mdt_uuid) {
3466 if (check_mdt_match(param))
3473 if (param->fp_check_uid) {
3474 if (st->st_uid == param->fp_uid) {
3475 if (param->fp_exclude_uid)
3478 if (!param->fp_exclude_uid)
3483 if (param->fp_check_gid) {
3484 if (st->st_gid == param->fp_gid) {
3485 if (param->fp_exclude_gid)
3488 if (!param->fp_exclude_gid)
3493 if (param->fp_check_pool) {
3494 decision = find_check_pool(param);
3499 if (param->fp_check_comp_count || param->fp_check_comp_flags ||
3500 param->fp_check_comp_start || param->fp_check_comp_end) {
3501 decision = find_check_comp_options(param);
3506 /* Check the time on mds. */
3508 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
3511 for_mds = lustre_fs ?
3512 (S_ISREG(st->st_mode) && stripe_count) : 0;
3513 decision = find_time_check(st, param, for_mds);
3518 /* If file still fits the request, ask ost for updated info.
3519 The regular stat is almost of the same speed as some new
3520 'glimpse-size-ioctl'. */
3522 if (param->fp_check_size && S_ISREG(st->st_mode) && stripe_count)
3525 if (param->fp_check_size && S_ISDIR(st->st_mode))
3529 /* For regular files with the stripe the decision may have not
3530 * been taken yet if *time or size is to be checked. */
3531 if (param->fp_obd_index != OBD_NOT_FOUND)
3532 print_failed_tgt(param, path, LL_STATFS_LOV);
3534 if (param->fp_mdt_index != OBD_NOT_FOUND)
3535 print_failed_tgt(param, path, LL_STATFS_LMV);
3538 ret = fstat_f(dirfd(dir), st);
3539 else if (de != NULL)
3540 ret = fstatat_f(dirfd(parent), de->d_name, st,
3541 AT_SYMLINK_NOFOLLOW);
3543 ret = lstat_f(path, st);
3546 if (errno == ENOENT) {
3547 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
3548 "warning: %s: %s does not exist",
3553 llapi_error(LLAPI_MSG_ERROR, ret,
3554 "%s: IOC_LOV_GETINFO on %s failed",
3560 /* Check the time on osc. */
3561 decision = find_time_check(st, param, 0);
3566 if (param->fp_check_size)
3567 decision = find_value_cmp(st->st_size, param->fp_size,
3568 param->fp_size_sign,
3569 param->fp_exclude_size,
3570 param->fp_size_units, 0);
3572 if (decision != -1) {
3573 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
3574 if (param->fp_zero_end)
3575 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
3577 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3581 /* Do not get down anymore? */
3582 if (param->fp_depth == param->fp_max_depth)
3590 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
3591 void *param_data, struct dirent64 *de)
3593 struct find_param *param = (struct find_param *)param_data;
3594 DIR *tmp_parent = parent;
3595 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3597 struct obd_ioctl_data data = { 0 };
3604 if (parent == NULL && dirp == NULL)
3610 if (parent == NULL) {
3611 tmp_parent = opendir_parent(path);
3612 if (tmp_parent == NULL) {
3615 llapi_error(LLAPI_MSG_ERROR, ret,
3616 "can not open %s", path);
3621 fd = dirfd(tmp_parent);
3623 path_copy = strdup(path);
3624 filename = basename(path_copy);
3625 data.ioc_inlbuf1 = (char *)filename;
3626 data.ioc_inllen1 = strlen(filename) + 1;
3627 data.ioc_inlbuf2 = (char *)¶m->fp_mdt_index;
3628 data.ioc_inllen2 = sizeof(param->fp_mdt_index);
3629 ret = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3631 llapi_error(LLAPI_MSG_ERROR, ret,
3632 "llapi_obd_statfs: error packing ioctl data");
3637 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
3639 if (errno == EBUSY && !retry) {
3640 /* because migrate may not be able to lock all involved
3641 * objects in order, for some of them it try lock, while
3642 * there may be conflicting COS locks and cause migrate
3643 * fail with EBUSY, hope a sync() could cause
3644 * transaction commit and release these COS locks. */
3650 fprintf(stderr, "%s migrate failed: %s (%d)\n",
3651 path, strerror(-ret), ret);
3653 } else if (param->fp_verbose & VERBOSE_DETAIL) {
3654 fprintf(stdout, "migrate %s to MDT%d\n",
3655 path, param->fp_mdt_index);
3660 /* If the directory is being migration, we need
3661 * close the directory after migration,
3662 * so the old directory cache will be cleanup
3663 * on the client side, and re-open to get the
3664 * new directory handle */
3665 *dirp = opendir(path);
3666 if (*dirp == NULL) {
3668 llapi_error(LLAPI_MSG_ERROR, ret,
3669 "%s: Failed to open '%s'", __func__, path);
3674 closedir(tmp_parent);
3681 int llapi_migrate_mdt(char *path, struct find_param *param)
3683 return param_callback(path, cb_migrate_mdt_init, cb_common_fini, param);
3686 int llapi_mv(char *path, struct find_param *param)
3688 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 59, 0)
3689 static bool printed;
3692 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
3693 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
3697 return llapi_migrate_mdt(path, param);
3700 int llapi_find(char *path, struct find_param *param)
3702 return param_callback(path, cb_find_init, cb_common_fini, param);
3706 * Get MDT number that the file/directory inode referenced
3707 * by the open fd resides on.
3708 * Return 0 and mdtidx on success, or -ve errno.
3710 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
3712 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
3717 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
3718 struct dirent64 *de)
3720 struct find_param *param = (struct find_param *)data;
3721 DIR *d = dirp == NULL ? NULL : *dirp;
3725 if (parent == NULL && d == NULL)
3729 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3730 } else /* if (parent) */ {
3733 fd = open(path, O_RDONLY | O_NOCTTY);
3735 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3743 if (ret == -ENODATA) {
3744 if (!param->fp_obd_uuid)
3745 llapi_printf(LLAPI_MSG_NORMAL,
3746 "'%s' has no stripe info\n", path);
3748 } else if (ret == -ENOENT) {
3749 llapi_error(LLAPI_MSG_WARN, ret,
3750 "warning: %s: '%s' does not exist",
3753 } else if (ret == -ENOTTY) {
3754 llapi_error(LLAPI_MSG_ERROR, ret,
3755 "%s: '%s' not on a Lustre fs",
3758 llapi_error(LLAPI_MSG_ERROR, ret,
3759 "error: %s: '%s' failed get_mdtidx",
3765 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
3766 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3768 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3772 /* Do not go down anymore? */
3773 if (param->fp_depth == param->fp_max_depth)
3781 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
3782 struct dirent64 *de)
3784 struct find_param *param = (struct find_param *)data;
3785 DIR *d = dirp == NULL ? NULL : *dirp;
3788 if (parent == NULL && d == NULL)
3791 if (param->fp_obd_uuid) {
3792 param->fp_quiet = 1;
3793 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
3799 if (param->fp_get_lmv || param->fp_get_default_lmv) {
3800 ret = cb_get_dirstripe(path, d, param);
3802 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
3803 (void *)¶m->fp_lmd->lmd_lmm);
3806 } else if (parent && !param->fp_get_lmv && !param->fp_get_default_lmv) {
3807 char *fname = strrchr(path, '/');
3808 fname = (fname == NULL ? path : fname + 1);
3810 strlcpy((char *)¶m->fp_lmd->lmd_lmm, fname,
3811 param->fp_lum_size);
3813 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
3814 (void *)¶m->fp_lmd->lmd_lmm);
3820 if (errno == ENODATA && d != NULL) {
3821 /* We need to "fake" the "use the default" values
3822 * since the lmm struct is zeroed out at this point.
3823 * The magic needs to be set in order to satisfy
3824 * a check later on in the code path.
3825 * The object_seq needs to be set for the "(Default)"
3826 * prefix to be displayed. */
3827 if (param->fp_get_default_lmv) {
3828 struct lmv_user_md *lum = param->fp_lmv_md;
3830 lum->lum_magic = LMV_USER_MAGIC;
3831 lum->lum_stripe_count = 0;
3832 lum->lum_stripe_offset = -1;
3834 } else if (param->fp_get_lmv) {
3835 struct lmv_user_md *lum = param->fp_lmv_md;
3838 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3841 lum->lum_magic = LMV_MAGIC_V1;
3842 lum->lum_stripe_count = 0;
3843 lum->lum_stripe_offset = mdtidx;
3846 struct lov_user_md *lmm =
3847 ¶m->fp_lmd->lmd_lmm;
3849 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3851 ostid_set_seq(&lmm->lmm_oi,
3852 FID_SEQ_LOV_DEFAULT);
3853 lmm->lmm_stripe_count = 0;
3854 lmm->lmm_stripe_size = 0;
3855 lmm->lmm_stripe_offset = -1;
3858 } else if (errno == ENODATA && parent != NULL) {
3859 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
3860 llapi_printf(LLAPI_MSG_NORMAL,
3861 "%s has no stripe info\n", path);
3863 } else if (errno == ENOENT) {
3864 llapi_error(LLAPI_MSG_WARN, -ENOENT,
3865 "warning: %s: %s does not exist",
3868 } else if (errno == ENOTTY) {
3870 llapi_error(LLAPI_MSG_ERROR, ret,
3871 "%s: '%s' not on a Lustre fs?",
3876 llapi_error(LLAPI_MSG_ERROR, ret,
3877 "error: %s: %s failed for %s",
3878 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
3879 "IOC_MDC_GETFILESTRIPE", path);
3886 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
3887 llapi_lov_dump_user_lmm(param, path, d ? LDF_IS_DIR : 0);
3890 /* Do not get down anymore? */
3891 if (param->fp_depth == param->fp_max_depth)
3899 int llapi_getstripe(char *path, struct find_param *param)
3901 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
3902 cb_get_mdt_index : cb_getstripe,
3903 cb_common_fini, param);
3906 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
3907 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
3909 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3911 struct obd_ioctl_data data = { 0 };
3914 data.ioc_inlbuf1 = (char *)&type;
3915 data.ioc_inllen1 = sizeof(__u32);
3916 data.ioc_inlbuf2 = (char *)&index;
3917 data.ioc_inllen2 = sizeof(__u32);
3918 data.ioc_pbuf1 = (char *)stat_buf;
3919 data.ioc_plen1 = sizeof(struct obd_statfs);
3920 data.ioc_pbuf2 = (char *)uuid_buf;
3921 data.ioc_plen2 = sizeof(struct obd_uuid);
3923 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3925 llapi_error(LLAPI_MSG_ERROR, rc,
3926 "llapi_obd_statfs: error packing ioctl data");
3930 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
3932 return rc < 0 ? -errno : 0;
3935 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
3936 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
3941 fd = open(path, O_RDONLY);
3944 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
3946 /* If we can't even open a file on the filesystem (e.g. with
3947 * -ESHUTDOWN), force caller to exit or it will loop forever. */
3951 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
3958 #define MAX_STRING_SIZE 128
3960 int llapi_ping(char *obd_type, char *obd_name)
3966 rc = cfs_get_param_paths(&path, "%s/%s/ping",
3967 obd_type, obd_name);
3971 fd = open(path.gl_pathv[0], O_WRONLY);
3974 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
3979 /* The purpose is to send a byte as a ping, whatever this byte is. */
3980 /* coverity[uninit_use_in_call] */
3981 rc = write(fd, buf, 1);
3989 cfs_free_param_data(&path);
3993 int llapi_target_iterate(int type_num, char **obd_type,
3994 void *args, llapi_cb_t cb)
3996 char buf[MAX_STRING_SIZE];
4001 rc = cfs_get_param_paths(¶m, "devices");
4005 fp = fopen(param.gl_pathv[0], "r");
4008 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
4013 while (fgets(buf, sizeof(buf), fp) != NULL) {
4014 char *obd_type_name = NULL;
4015 char *obd_name = NULL;
4016 char *obd_uuid = NULL;
4018 struct obd_statfs osfs_buffer;
4020 while(bufp[0] == ' ')
4023 for(i = 0; i < 3; i++) {
4024 obd_type_name = strsep(&bufp, " ");
4026 obd_name = strsep(&bufp, " ");
4027 obd_uuid = strsep(&bufp, " ");
4029 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
4031 for (i = 0; i < type_num; i++) {
4032 if (strcmp(obd_type_name, obd_type[i]) != 0)
4035 cb(obd_type_name, obd_name, obd_uuid, args);
4040 cfs_free_param_data(¶m);
4044 static void do_target_check(char *obd_type_name, char *obd_name,
4045 char *obd_uuid, void *args)
4049 rc = llapi_ping(obd_type_name, obd_name);
4050 if (rc == ENOTCONN) {
4051 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
4053 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
4055 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
4059 int llapi_target_check(int type_num, char **obd_type, char *dir)
4061 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
4064 #undef MAX_STRING_SIZE
4066 /* Is this a lustre fs? */
4067 int llapi_is_lustre_mnttype(const char *type)
4069 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
4072 /* Is this a lustre client fs? */
4073 int llapi_is_lustre_mnt(struct mntent *mnt)
4075 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
4076 strstr(mnt->mnt_fsname, ":/") != NULL);
4079 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
4081 char fsname[PATH_MAX + 1];
4085 rc = llapi_search_fsname(mnt, fsname);
4087 llapi_err_noerrno(LLAPI_MSG_ERROR,
4088 "'%s' isn't on Lustre filesystem", mnt);
4092 root = open(mnt, O_RDONLY | O_DIRECTORY);
4095 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4099 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
4107 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
4108 * format must have %s%s, buf must be > 16
4109 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
4110 * then buf = "lustre-MDT0000"
4112 static int get_mdtname(char *name, char *format, char *buf)
4114 char suffix[]="-MDT0000";
4115 int len = strlen(name);
4117 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
4118 name[len - 5] = '\0';
4123 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
4126 /* Not enough room to add suffix */
4127 llapi_err_noerrno(LLAPI_MSG_ERROR,
4128 "MDT name too long |%s|", name);
4133 return sprintf(buf, format, name, suffix);
4136 /** ioctl on filsystem root, with mdtindex sent as data
4137 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
4138 * \param mdtidxp pointer to integer within data to be filled in with the
4139 * mdt index (0 if no mdt is specified). NULL won't be filled.
4141 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
4149 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
4150 Open root and parse mdt index. */
4151 if (mdtname[0] == '/') {
4153 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
4154 (char *)mdtname, -1);
4156 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
4158 ptr = fsname + strlen(fsname) - 8;
4160 index = strtol(ptr + 4, NULL, 10);
4161 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
4165 llapi_err_noerrno(LLAPI_MSG_ERROR,
4166 "Can't open %s: %d\n", mdtname, rc);
4173 rc = ioctl(fd, opc, data);
4182 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
4183 int buflen, long long *recno, int *linkno)
4185 const char *fidstr_orig = fidstr;
4187 struct getinfo_fid2path *gf;
4190 while (*fidstr == '[')
4193 sscanf(fidstr, SFID, RFID(&fid));
4194 if (!fid_is_sane(&fid)) {
4195 llapi_err_noerrno(LLAPI_MSG_ERROR,
4196 "bad FID format '%s', should be [seq:oid:ver]"
4197 " (e.g. "DFID")\n", fidstr_orig,
4198 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
4202 gf = malloc(sizeof(*gf) + buflen);
4207 gf->gf_recno = *recno;
4208 gf->gf_linkno = *linkno;
4209 gf->gf_pathlen = buflen;
4211 /* Take path or fsname */
4212 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
4216 memcpy(buf, gf->gf_u.gf_path, gf->gf_pathlen);
4217 if (buf[0] == '\0') { /* ROOT path */
4221 *recno = gf->gf_recno;
4222 *linkno = gf->gf_linkno;
4229 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
4232 struct lustre_mdt_attrs *lma;
4236 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
4238 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
4241 lma = (struct lustre_mdt_attrs *)buf;
4242 fid_le_to_cpu(fid, &lma->lma_self_fid);
4246 int llapi_get_mdt_index_by_fid(int fd, const lustre_fid *fid,
4251 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
4260 int llapi_fd2fid(const int fd, lustre_fid *fid)
4264 memset(fid, 0, sizeof(*fid));
4266 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
4267 if (rc == -EINVAL || rc == -ENOTTY)
4268 rc = fid_from_lma(NULL, fd, fid);
4273 int llapi_path2fid(const char *path, lustre_fid *fid)
4277 memset(fid, 0, sizeof(*fid));
4278 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4280 if (errno == ELOOP || errno == ENXIO)
4281 return fid_from_lma(path, -1, fid);
4285 rc = llapi_fd2fid(fd, fid);
4286 if (rc == -EINVAL || rc == -ENOTTY)
4287 rc = fid_from_lma(path, -1, fid);
4293 int llapi_fd2parent(int fd, unsigned int linkno, lustre_fid *parent_fid,
4294 char *name, size_t name_size)
4296 struct getparent *gp;
4299 gp = malloc(sizeof(*gp) + name_size);
4303 gp->gp_linkno = linkno;
4304 gp->gp_name_size = name_size;
4306 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
4312 *parent_fid = gp->gp_fid;
4314 strncpy(name, gp->gp_name, name_size);
4315 name[name_size - 1] = '\0';
4322 int llapi_path2parent(const char *path, unsigned int linkno,
4323 lustre_fid *parent_fid, char *name, size_t name_size)
4328 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4332 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
4337 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4342 root = open(mnt, O_RDONLY | O_DIRECTORY);
4345 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4349 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
4352 llapi_error(LLAPI_MSG_ERROR, rc,
4353 "ioctl on %s for getting connect flags failed", mnt);
4360 * Get a 64-bit value representing the version of file data pointed by fd.
4362 * Each write or truncate, flushed on OST, will change this value. You can use
4363 * this value to verify if file data was modified. This only checks the file
4364 * data, not metadata.
4366 * \param flags 0: no flush pages, usually used it the process has already
4368 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
4370 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
4371 * pages from clients.
4373 * \retval 0 on success.
4374 * \retval -errno on error.
4376 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4379 struct ioc_data_version idv;
4381 idv.idv_flags = flags;
4383 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4387 *data_version = idv.idv_version;
4393 * Create a file without any name open it for read/write
4395 * - file is created as if it were a standard file in the given \a directory
4396 * - file does not appear in \a directory and mtime does not change because
4397 * the filename is handled specially by the Lustre MDS.
4398 * - file is removed at final close
4399 * - file modes are rw------- since it doesn't make sense to have a read-only
4400 * or write-only file that cannot be opened again.
4401 * - if user wants another mode it must use fchmod() on the open file, no
4402 * security problems arise because it cannot be opened by another process.
4404 * \param[in] directory directory from which to inherit layout/MDT idx
4405 * \param[in] idx MDT index on which the file is created,
4406 * \a idx == -1 means no specific MDT is requested
4407 * \param[in] open_flags standard open(2) flags
4409 * \retval 0 on success.
4410 * \retval -errno on error.
4412 int llapi_create_volatile_idx(char *directory, int idx, int open_flags)
4414 char file_path[PATH_MAX];
4415 char filename[PATH_MAX];
4416 int saved_errno = errno;
4424 snprintf(filename, sizeof(filename),
4425 LUSTRE_VOLATILE_HDR"::%.4X", rnumber);
4427 snprintf(filename, sizeof(filename),
4428 LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, rnumber);
4430 rc = snprintf(file_path, sizeof(file_path),
4431 "%s/%s", directory, filename);
4432 if (rc >= sizeof(file_path))
4435 fd = open(file_path,
4436 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | open_flags,
4438 } while (fd < 0 && errno == EEXIST);
4441 llapi_error(LLAPI_MSG_ERROR, errno,
4442 "Cannot create volatile file '%s' in '%s'",
4443 filename + LUSTRE_VOLATILE_HDR_LEN,
4448 /* Unlink file in case this wasn't a Lustre filesystem and the
4449 * magic volatile filename wasn't handled as intended. The
4450 * effect is the same. If volatile open was supported then we
4451 * expect unlink() to return -ENOENT. */
4452 (void)unlink(file_path);
4454 /* Since we are returning successfully we restore errno (and
4455 * mask out possible EEXIST from open() and ENOENT from
4457 errno = saved_errno;
4463 * Swap the layouts between 2 file descriptors
4464 * the 2 files must be open for writing
4465 * first fd received the ioctl, second fd is passed as arg
4466 * this is assymetric but avoid use of root path for ioctl
4468 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
4469 int gid, __u64 flags)
4471 struct lustre_swap_layouts lsl;
4476 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4477 rc = fstat(fd1, &st1);
4481 rc = fstat(fd2, &st2);
4486 lsl.sl_flags = flags;
4490 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4494 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4495 struct timeval tv1[2];
4496 struct timeval tv2[2];
4498 memset(tv1, 0, sizeof(tv1));
4499 memset(tv2, 0, sizeof(tv2));
4501 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
4502 tv1[0].tv_sec = st1.st_atime;
4503 tv2[0].tv_sec = st2.st_atime;
4505 tv1[0].tv_sec = st2.st_atime;
4506 tv2[0].tv_sec = st1.st_atime;
4509 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
4510 tv1[1].tv_sec = st1.st_mtime;
4511 tv2[1].tv_sec = st2.st_mtime;
4513 tv1[1].tv_sec = st2.st_mtime;
4514 tv2[1].tv_sec = st1.st_mtime;
4517 rc = futimes(fd1, tv1);
4521 rc = futimes(fd2, tv2);
4529 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4536 while (grp_id == 0);
4538 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
4546 * Swap the layouts between 2 files
4547 * the 2 files are open in write
4549 int llapi_swap_layouts(const char *path1, const char *path2,
4550 __u64 dv1, __u64 dv2, __u64 flags)
4554 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4557 llapi_error(LLAPI_MSG_ERROR, rc,
4558 "error: cannot open '%s' for write", path1);
4562 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4565 llapi_error(LLAPI_MSG_ERROR, rc,
4566 "error: cannot open '%s' for write", path2);
4570 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4572 llapi_error(LLAPI_MSG_ERROR, rc,
4573 "error: cannot swap layout between '%s' and '%s'",
4584 * Attempt to open a file with Lustre file identifier \a fid
4585 * and return an open file descriptor.
4587 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
4588 * \param[in] fid Lustre file identifier of file to open
4589 * \param[in] flags open() flags
4591 * \retval non-negative file descriptor on successful open
4592 * \retval -1 if an error occurred
4594 int llapi_open_by_fid(const char *lustre_dir, const lustre_fid *fid, int flags)
4596 char mntdir[PATH_MAX];
4597 char path[PATH_MAX];
4600 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
4604 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
4605 return open(path, flags);
4611 * \param fd File to lock.
4612 * \param gid Group Identifier.
4614 * \retval 0 on success.
4615 * \retval -errno on failure.
4617 int llapi_group_lock(int fd, int gid)
4621 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
4624 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
4632 * \param fd File to unlock.
4633 * \param gid Group Identifier.
4635 * \retval 0 on success.
4636 * \retval -errno on failure.
4638 int llapi_group_unlock(int fd, int gid)
4642 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
4645 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");