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_ioctl.h>
76 #include "lustreapi_internal.h"
78 static int llapi_msg_level = LLAPI_MSG_MAX;
80 void llapi_msg_set_level(int level)
82 /* ensure level is in the good range */
83 if (level < LLAPI_MSG_OFF)
84 llapi_msg_level = LLAPI_MSG_OFF;
85 else if (level > LLAPI_MSG_MAX)
86 llapi_msg_level = LLAPI_MSG_MAX;
88 llapi_msg_level = level;
91 int llapi_msg_get_level(void)
93 return llapi_msg_level;
96 static void error_callback_default(enum llapi_message_level level, int err,
97 const char *fmt, va_list ap)
99 vfprintf(stderr, fmt, ap);
100 if (level & LLAPI_MSG_NO_ERRNO)
101 fprintf(stderr, "\n");
103 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
106 static void info_callback_default(enum llapi_message_level level, int err,
107 const char *fmt, va_list ap)
109 vfprintf(stdout, fmt, ap);
112 static llapi_log_callback_t llapi_error_callback = error_callback_default;
113 static llapi_log_callback_t llapi_info_callback = info_callback_default;
116 /* llapi_error will preserve errno */
117 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
120 int tmp_errno = errno;
122 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
126 llapi_error_callback(level, abs(err), fmt, args);
131 /* llapi_printf will preserve errno */
132 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
135 int tmp_errno = errno;
137 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
141 llapi_info_callback(level, 0, fmt, args);
147 * Set a custom error logging function. Passing in NULL will reset the logging
148 * callback to its default value.
150 * This function returns the value of the old callback.
152 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
154 llapi_log_callback_t old = llapi_error_callback;
157 llapi_error_callback = cb;
159 llapi_error_callback = error_callback_default;
165 * Set a custom info logging function. Passing in NULL will reset the logging
166 * callback to its default value.
168 * This function returns the value of the old callback.
170 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
172 llapi_log_callback_t old = llapi_info_callback;
175 llapi_info_callback = cb;
177 llapi_info_callback = info_callback_default;
183 * size_units is to be initialized (or zeroed) by caller.
185 int llapi_parse_size(const char *optarg, unsigned long long *size,
186 unsigned long long *size_units, int bytes_spec)
189 char *argbuf = (char *)optarg;
190 unsigned long long frac = 0, frac_d = 1;
192 if (strncmp(optarg, "-", 1) == 0)
195 if (*size_units == 0)
198 *size = strtoull(argbuf, &end, 0);
199 if (end != NULL && *end == '.') {
203 frac = strtoull(argbuf, &end, 10);
204 /* count decimal places */
205 for (i = 0; i < (end - argbuf); i++)
210 if ((*end == 'b') && *(end + 1) == '\0' &&
211 (*size & (~0ULL << (64 - 9))) == 0 &&
213 *size_units = 1 << 9;
214 } else if ((*end == 'b') &&
215 *(end + 1) == '\0' &&
218 } else if ((*end == 'k' || *end == 'K') &&
219 *(end + 1) == '\0' &&
220 (*size & (~0ULL << (64 - 10))) == 0) {
221 *size_units = 1 << 10;
222 } else if ((*end == 'm' || *end == 'M') &&
223 *(end + 1) == '\0' &&
224 (*size & (~0ULL << (64 - 20))) == 0) {
225 *size_units = 1 << 20;
226 } else if ((*end == 'g' || *end == 'G') &&
227 *(end + 1) == '\0' &&
228 (*size & (~0ULL << (64 - 30))) == 0) {
229 *size_units = 1 << 30;
230 } else if ((*end == 't' || *end == 'T') &&
231 *(end + 1) == '\0' &&
232 (*size & (~0ULL << (64 - 40))) == 0) {
233 *size_units = 1ULL << 40;
234 } else if ((*end == 'p' || *end == 'P') &&
235 *(end + 1) == '\0' &&
236 (*size & (~0ULL << (64 - 50))) == 0) {
237 *size_units = 1ULL << 50;
238 } else if ((*end == 'e' || *end == 'E') &&
239 *(end + 1) == '\0' &&
240 (*size & (~0ULL << (64 - 60))) == 0) {
241 *size_units = 1ULL << 60;
246 *size = *size * *size_units + frac * *size_units / frac_d;
251 /* XXX: llapi_xxx() functions return negative values upon failure */
253 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
254 int stripe_count, int stripe_pattern)
258 /* 64 KB is the largest common page size I'm aware of (on ia64), but
259 * check the local page size just in case. */
260 page_size = LOV_MIN_STRIPE_SIZE;
261 if (getpagesize() > page_size) {
262 page_size = getpagesize();
263 llapi_err_noerrno(LLAPI_MSG_WARN,
264 "warning: your page size (%u) is "
265 "larger than expected (%u)", page_size,
266 LOV_MIN_STRIPE_SIZE);
268 if (!llapi_stripe_size_is_aligned(stripe_size)) {
270 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
271 "must be an even multiple of %d bytes",
272 stripe_size, page_size);
275 if (!llapi_stripe_index_is_valid(stripe_offset)) {
277 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
281 if (!llapi_stripe_count_is_valid(stripe_count)) {
283 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
287 if (llapi_stripe_size_is_too_big(stripe_size)) {
289 llapi_error(LLAPI_MSG_ERROR, rc,
290 "warning: stripe size 4G or larger "
291 "is not currently supported and would wrap");
298 * Trim a trailing newline from a string, if it exists.
300 int llapi_chomp_string(char *buf)
316 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
318 static int get_param_lov(const char *path, const char *param,
319 char *buf, size_t buf_size)
321 struct obd_uuid uuid;
324 rc = llapi_file_get_lov_uuid(path, &uuid);
328 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
333 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
335 static int get_param_lmv(const char *path, const char *param,
336 char *buf, size_t buf_size)
338 struct obd_uuid uuid;
341 rc = llapi_file_get_lmv_uuid(path, &uuid);
345 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
349 static int get_mds_md_size(const char *path)
351 char buf[PATH_MAX], inst[PATH_MAX];
352 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
355 rc = llapi_getname(path, inst, sizeof(inst));
359 /* Get the max ea size from llite parameters. */
360 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
361 "max_easize", buf, sizeof(buf));
367 return rc > 0 ? rc : md_size;
370 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
372 return get_param_lmv(path, "uuid", buf, bufsize);
376 * if pool is NULL, search ostname in target_obd
377 * if pool is not NULL:
378 * if pool not found returns errno < 0
379 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
380 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
382 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
384 char buffer[PATH_MAX];
390 /* You need one or the other */
391 if (poolname == NULL && fsname == NULL)
395 len = strlen(ostname);
397 if (poolname == NULL && len == 0)
400 /* Search by poolname and fsname if is not NULL */
401 if (poolname != NULL) {
402 rc = poolpath(¶m, fsname, NULL);
404 snprintf(buffer, sizeof(buffer), "%s/%s",
405 param.gl_pathv[0], poolname);
407 } else if (fsname != NULL) {
408 rc = get_lustre_param_path("lov", fsname,
410 "target_obd", ¶m);
412 strncpy(buffer, param.gl_pathv[0],
418 cfs_free_param_data(¶m);
422 fd = fopen(buffer, "r");
426 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
427 if (poolname == NULL) {
429 /* Search for an ostname in the list of OSTs
430 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
431 ptr = strchr(buffer, ' ');
433 (strncmp(ptr + 1, ostname, len) == 0)) {
438 /* Search for an ostname in a pool,
439 (or an existing non-empty pool if no ostname) */
440 if ((ostname == NULL) ||
441 (strncmp(buffer, ostname, len) == 0)) {
452 * Open a Lustre file.
454 * \param name the name of the file to be opened
455 * \param flags access mode, see flags in open(2)
456 * \param mode permission of the file if it is created, see mode in open(2)
457 * \param param stripe pattern of the newly created file
459 * \retval file descriptor of opened file
460 * \retval negative errno on failure
462 int llapi_file_open_param(const char *name, int flags, mode_t mode,
463 const struct llapi_stripe_param *param)
465 char fsname[MAX_OBD_NAME + 1] = { 0 };
466 char *pool_name = param->lsp_pool;
467 struct lov_user_md *lum = NULL;
468 size_t lum_size = sizeof(*lum);
471 /* Make sure we are on a Lustre file system */
472 rc = llapi_search_fsname(name, fsname);
474 llapi_error(LLAPI_MSG_ERROR, rc,
475 "'%s' is not on a Lustre filesystem",
480 /* Check if the stripe pattern is sane. */
481 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
482 param->lsp_stripe_offset,
483 param->lsp_stripe_count,
484 param->lsp_stripe_pattern);
488 /* Make sure we have a good pool */
489 if (pool_name != NULL) {
490 /* in case user gives the full pool name <fsname>.<poolname>,
491 * strip the fsname */
492 char *ptr = strchr(pool_name, '.');
495 if (strcmp(pool_name, fsname) != 0) {
497 llapi_err_noerrno(LLAPI_MSG_ERROR,
498 "Pool '%s' is not on filesystem '%s'",
505 /* Make sure the pool exists and is non-empty */
506 rc = llapi_search_ost(fsname, pool_name, NULL);
508 char *err = rc == 0 ? "has no OSTs" : "does not exist";
510 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
511 fsname, pool_name, err);
515 lum_size = sizeof(struct lov_user_md_v3);
518 /* sanity check of target list */
519 if (param->lsp_is_specific) {
520 char ostname[MAX_OBD_NAME + 1];
524 for (i = 0; i < param->lsp_stripe_count; i++) {
525 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
526 fsname, param->lsp_osts[i]);
527 rc = llapi_search_ost(fsname, pool_name, ostname);
532 llapi_error(LLAPI_MSG_ERROR, rc,
533 "%s: cannot find OST %s in %s",
540 /* Make sure stripe offset is in OST list. */
541 if (param->lsp_osts[i] == param->lsp_stripe_offset)
545 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
546 "%s: stripe offset '%d' is not in the "
548 __func__, param->lsp_stripe_offset);
552 lum_size = lov_user_md_size(param->lsp_stripe_count,
553 LOV_USER_MAGIC_SPECIFIC);
556 lum = calloc(1, lum_size);
561 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
563 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
564 flags = O_DIRECTORY | O_RDONLY;
571 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
576 /* Initialize IOCTL striping pattern structure */
577 lum->lmm_magic = LOV_USER_MAGIC_V1;
578 lum->lmm_pattern = param->lsp_stripe_pattern;
579 lum->lmm_stripe_size = param->lsp_stripe_size;
580 lum->lmm_stripe_count = param->lsp_stripe_count;
581 lum->lmm_stripe_offset = param->lsp_stripe_offset;
582 if (pool_name != NULL) {
583 struct lov_user_md_v3 *lumv3 = (void *)lum;
585 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
586 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
588 if (param->lsp_is_specific) {
589 struct lov_user_md_v3 *lumv3 = (void *)lum;
592 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
593 if (pool_name == NULL) {
594 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
595 * OST list, therefore if pool is not specified we have
596 * to pack a null pool name for placeholder. */
597 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
600 for (i = 0; i < param->lsp_stripe_count; i++)
601 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
604 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
605 char *errmsg = "stripe already set";
608 if (errno != EEXIST && errno != EALREADY)
609 errmsg = strerror(errno);
611 llapi_err_noerrno(LLAPI_MSG_ERROR,
612 "error on ioctl %#jx for '%s' (%d): %s",
613 (uintmax_t)LL_IOC_LOV_SETSTRIPE, name, fd,
625 int llapi_file_open_pool(const char *name, int flags, int mode,
626 unsigned long long stripe_size, int stripe_offset,
627 int stripe_count, int stripe_pattern, char *pool_name)
629 const struct llapi_stripe_param param = {
630 .lsp_stripe_size = stripe_size,
631 .lsp_stripe_count = stripe_count,
632 .lsp_stripe_pattern = stripe_pattern,
633 .lsp_stripe_offset = stripe_offset,
634 .lsp_pool = pool_name
636 return llapi_file_open_param(name, flags, mode, ¶m);
639 int llapi_file_open(const char *name, int flags, int mode,
640 unsigned long long stripe_size, int stripe_offset,
641 int stripe_count, int stripe_pattern)
643 return llapi_file_open_pool(name, flags, mode, stripe_size,
644 stripe_offset, stripe_count,
645 stripe_pattern, NULL);
648 int llapi_file_create(const char *name, unsigned long long stripe_size,
649 int stripe_offset, int stripe_count, int stripe_pattern)
653 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
654 stripe_offset, stripe_count, stripe_pattern,
663 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
664 int stripe_offset, int stripe_count,
665 int stripe_pattern, char *pool_name)
669 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
670 stripe_offset, stripe_count, stripe_pattern,
679 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
680 int stripe_count, int stripe_pattern,
681 const char *pool_name)
683 struct lmv_user_md lum = { 0 };
687 lum.lum_magic = LMV_USER_MAGIC;
688 lum.lum_stripe_offset = stripe_offset;
689 lum.lum_stripe_count = stripe_count;
690 lum.lum_hash_type = stripe_pattern;
691 if (pool_name != NULL) {
692 if (strlen(pool_name) >= sizeof(lum.lum_pool_name)) {
693 llapi_err_noerrno(LLAPI_MSG_ERROR,
694 "error LL_IOC_LMV_SET_DEFAULT_STRIPE '%s'"
695 ": too large pool name: %s", name, pool_name);
698 strncpy(lum.lum_pool_name, pool_name,
699 sizeof(lum.lum_pool_name));
702 fd = open(name, O_DIRECTORY | O_RDONLY);
705 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
709 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lum);
711 char *errmsg = "stripe already set";
713 if (errno != EEXIST && errno != EALREADY)
714 errmsg = strerror(errno);
716 llapi_err_noerrno(LLAPI_MSG_ERROR,
717 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
724 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
725 int stripe_count, int stripe_pattern,
726 const char *pool_name)
728 struct lmv_user_md lmu = { 0 };
729 struct obd_ioctl_data data = { 0 };
732 char *dirpath = NULL;
733 char *namepath = NULL;
739 dirpath = strdup(name);
740 namepath = strdup(name);
741 if (!dirpath || !namepath)
744 lmu.lum_magic = LMV_USER_MAGIC;
745 lmu.lum_stripe_offset = stripe_offset;
746 lmu.lum_stripe_count = stripe_count;
747 lmu.lum_hash_type = stripe_pattern;
748 if (pool_name != NULL) {
749 if (strlen(pool_name) > LOV_MAXPOOLNAME) {
750 llapi_err_noerrno(LLAPI_MSG_ERROR,
751 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
752 "pool name: %s", name, pool_name);
756 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
759 filename = basename(namepath);
760 dir = dirname(dirpath);
762 data.ioc_inlbuf1 = (char *)filename;
763 data.ioc_inllen1 = strlen(filename) + 1;
764 data.ioc_inlbuf2 = (char *)&lmu;
765 data.ioc_inllen2 = sizeof(struct lmv_user_md);
766 data.ioc_type = mode;
767 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
769 llapi_error(LLAPI_MSG_ERROR, rc,
770 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
775 fd = open(dir, O_DIRECTORY | O_RDONLY);
778 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
782 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
783 char *errmsg = "stripe already set";
785 if (errno != EEXIST && errno != EALREADY)
786 errmsg = strerror(errno);
788 llapi_err_noerrno(LLAPI_MSG_ERROR,
789 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
799 int llapi_direntry_remove(char *dname)
801 char *dirpath = NULL;
802 char *namepath = NULL;
808 dirpath = strdup(dname);
809 namepath = strdup(dname);
810 if (!dirpath || !namepath)
813 filename = basename(namepath);
815 dir = dirname(dirpath);
817 fd = open(dir, O_DIRECTORY | O_RDONLY);
820 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
825 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
826 char *errmsg = strerror(errno);
827 llapi_err_noerrno(LLAPI_MSG_ERROR,
828 "error on ioctl %#jx for '%s' (%d): %s",
829 (uintmax_t)LL_IOC_LMV_SETSTRIPE, filename,
841 * Find the fsname, the full path, and/or an open fd.
842 * Either the fsname or path must not be NULL
844 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
847 char buf[PATH_MAX], mntdir[PATH_MAX];
850 int idx = 0, len = 0, mntlen, fd;
853 /* get the mount point */
854 fp = setmntent(PROC_MOUNTS, "r");
857 llapi_error(LLAPI_MSG_ERROR, rc,
858 "setmntent(%s) failed", PROC_MOUNTS);
862 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
865 if (!llapi_is_lustre_mnt(&mnt))
868 if ((want & WANT_INDEX) && (idx++ != index))
871 mntlen = strlen(mnt.mnt_dir);
872 ptr = strchr(mnt.mnt_fsname, '/');
873 while (ptr && *ptr == '/')
875 /* thanks to the call to llapi_is_lustre_mnt() above,
876 * we are sure that mnt.mnt_fsname contains ":/",
877 * so ptr should never be NULL */
881 while (*ptr_end != '/' && *ptr_end != '\0')
884 /* Check the fsname for a match, if given */
885 if (!(want & WANT_FSNAME) && fsname != NULL &&
886 (strlen(fsname) > 0) &&
887 (strncmp(ptr, fsname, ptr_end - ptr) != 0))
890 /* If the path isn't set return the first one we find */
891 if (path == NULL || strlen(path) == 0) {
892 strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
893 mntdir[strlen(mnt.mnt_dir)] = '\0';
894 if ((want & WANT_FSNAME) && fsname != NULL) {
895 strncpy(fsname, ptr, ptr_end - ptr);
896 fsname[ptr_end - ptr] = '\0';
900 /* Otherwise find the longest matching path */
901 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
902 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
903 strncpy(mntdir, mnt.mnt_dir, strlen(mnt.mnt_dir));
904 mntdir[strlen(mnt.mnt_dir)] = '\0';
906 if ((want & WANT_FSNAME) && fsname != NULL) {
907 strncpy(fsname, ptr, ptr_end - ptr);
908 fsname[ptr_end - ptr] = '\0';
917 if ((want & WANT_PATH) && path != NULL) {
918 strncpy(path, mntdir, strlen(mntdir));
919 path[strlen(mntdir)] = '\0';
921 if (want & WANT_FD) {
922 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
925 llapi_error(LLAPI_MSG_ERROR, rc,
926 "error opening '%s'", mntdir);
932 } else if (want & WANT_ERROR)
933 llapi_err_noerrno(LLAPI_MSG_ERROR,
934 "can't find fs root for '%s': %d",
935 (want & WANT_PATH) ? fsname : path, rc);
940 * search lustre mounts
942 * Calling this function will return to the user the mount point, mntdir, and
943 * the file system name, fsname, if the user passed a buffer to this routine.
945 * The user inputs are pathname and index. If the pathname is supplied then
946 * the value of the index will be ignored. The pathname will return data if
947 * the pathname is located on a lustre mount. Index is used to pick which
948 * mount point you want in the case of multiple mounted lustre file systems.
949 * See function lfs_osts in lfs.c for an example of the index use.
951 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
954 int want = WANT_PATH, idx = -1;
956 if (!pathname || pathname[0] == '\0') {
960 strcpy(mntdir, pathname);
964 return get_root_path(want, fsname, NULL, mntdir, idx);
967 /* Given a path, find the corresponding Lustre fsname */
968 int llapi_search_fsname(const char *pathname, char *fsname)
973 path = realpath(pathname, NULL);
975 char buf[PATH_MAX], *ptr;
978 if (pathname[0] != '/') {
979 /* Need an absolute path, but realpath() only works for
980 * pathnames that actually exist. We go through the
981 * extra hurdle of dirname(getcwd() + pathname) in
982 * case the relative pathname contains ".." in it. */
983 if (getcwd(buf, sizeof(buf) - 2) == NULL)
985 rc = strlcat(buf, "/", sizeof(buf));
986 if (rc >= sizeof(buf))
989 rc = strlcat(buf, pathname, sizeof(buf));
990 if (rc >= sizeof(buf))
992 path = realpath(buf, NULL);
994 ptr = strrchr(buf, '/');
998 path = realpath(buf, NULL);
1001 llapi_error(LLAPI_MSG_ERROR, rc,
1002 "pathname '%s' cannot expand",
1008 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1013 int llapi_search_rootpath(char *pathname, const char *fsname)
1015 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1018 int llapi_getname(const char *path, char *buf, size_t size)
1020 struct obd_uuid uuid_buf;
1021 char *uuid = uuid_buf.uuid;
1024 memset(&uuid_buf, 0, sizeof(uuid_buf));
1025 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1029 /* We want to turn lustre-clilov-ffff88002738bc00 into
1030 * lustre-ffff88002738bc00. */
1032 nr = snprintf(buf, size, "%.*s-%s",
1033 (int) (strlen(uuid) - 24), uuid,
1034 uuid + strlen(uuid) - 16);
1043 * Get the list of pool members.
1044 * \param poolname string of format \<fsname\>.\<poolname\>
1045 * \param members caller-allocated array of char*
1046 * \param list_size size of the members array
1047 * \param buffer caller-allocated buffer for storing OST names
1048 * \param buffer_size size of the buffer
1050 * \return number of members retrieved for this pool
1051 * \retval -error failure
1053 int llapi_get_poolmembers(const char *poolname, char **members,
1054 int list_size, char *buffer, int buffer_size)
1056 char fsname[PATH_MAX];
1065 /* name is FSNAME.POOLNAME */
1066 if (strlen(poolname) >= sizeof(fsname))
1068 strlcpy(fsname, poolname, sizeof(fsname));
1069 pool = strchr(fsname, '.');
1076 rc = poolpath(&pathname, fsname, NULL);
1078 llapi_error(LLAPI_MSG_ERROR, rc,
1079 "Lustre filesystem '%s' not found",
1084 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1085 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1086 cfs_free_param_data(&pathname);
1087 if (rc >= sizeof(buf))
1089 fd = fopen(buf, "r");
1092 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1097 while (fgets(buf, sizeof(buf), fd) != NULL) {
1098 if (nb_entries >= list_size) {
1102 buf[sizeof(buf) - 1] = '\0';
1104 tmp = strchr(buf, '\n');
1107 if (used + strlen(buf) + 1 > buffer_size) {
1112 strcpy(buffer + used, buf);
1113 members[nb_entries] = buffer + used;
1114 used += strlen(buf) + 1;
1124 * Get the list of pools in a filesystem.
1125 * \param name filesystem name or path
1126 * \param poollist caller-allocated array of char*
1127 * \param list_size size of the poollist array
1128 * \param buffer caller-allocated buffer for storing pool names
1129 * \param buffer_size size of the buffer
1131 * \return number of pools retrieved for this filesystem
1132 * \retval -error failure
1134 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1135 char *buffer, int buffer_size)
1137 char rname[PATH_MAX];
1143 struct dirent *cookie = NULL;
1145 unsigned int nb_entries = 0;
1146 unsigned int used = 0;
1149 /* initialize output array */
1150 for (i = 0; i < list_size; i++)
1153 /* is name a pathname ? */
1154 ptr = strchr(name, '/');
1156 /* only absolute pathname is supported */
1160 if (!realpath(name, rname)) {
1162 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1167 fsname = strdup(rname);
1171 rc = poolpath(&pathname, NULL, rname);
1173 /* name is FSNAME */
1174 fsname = strdup(name);
1177 rc = poolpath(&pathname, fsname, NULL);
1180 llapi_error(LLAPI_MSG_ERROR, rc,
1181 "Lustre filesystem '%s' not found", name);
1185 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1186 dir = opendir(pathname.gl_pathv[0]);
1189 llapi_error(LLAPI_MSG_ERROR, rc,
1190 "Could not open pool list for '%s'",
1196 rc = readdir_r(dir, &pool, &cookie);
1199 llapi_error(LLAPI_MSG_ERROR, rc,
1200 "Error reading pool list for '%s'", name);
1202 } else if ((rc == 0) && (cookie == NULL)) {
1203 /* end of directory */
1207 /* ignore . and .. */
1208 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1211 /* check output bounds */
1212 if (nb_entries >= list_size) {
1217 /* +2 for '.' and final '\0' */
1218 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1224 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1225 poollist[nb_entries] = buffer + used;
1226 used += strlen(pool.d_name) + strlen(fsname) + 2;
1233 cfs_free_param_data(&pathname);
1236 return rc != 0 ? rc : nb_entries;
1239 /* wrapper for lfs.c and obd.c */
1240 int llapi_poollist(const char *name)
1242 /* list of pool names (assume that pool count is smaller
1244 char **list, *buffer = NULL, *fsname = (char *)name;
1245 char *poolname = NULL, *tmp = NULL, data[16];
1246 enum param_filter type = FILTER_BY_PATH;
1247 int obdcount, bufsize, rc, nb, i;
1252 if (name[0] != '/') {
1253 fsname = strdup(name);
1257 poolname = strchr(fsname, '.');
1260 type = FILTER_BY_FS_NAME;
1263 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1264 data, sizeof(data));
1267 obdcount = atoi(data);
1269 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1270 * and also an array to store the pointers for all that
1271 * allocated space. */
1273 bufsize = sizeof(struct obd_uuid) * obdcount;
1274 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1275 if (buffer == NULL) {
1279 list = (char **) (buffer + bufsize);
1282 /* name is a path or fsname */
1283 nb = llapi_get_poollist(name, list, obdcount,
1286 /* name is a pool name (<fsname>.<poolname>) */
1287 nb = llapi_get_poolmembers(name, list, obdcount,
1291 if (nb == -EOVERFLOW) {
1294 goto retry_get_pools;
1297 for (i = 0; i < nb; i++)
1298 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1299 rc = (nb < 0 ? nb : 0);
1303 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1308 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1309 void *data, struct dirent64 *de);
1311 #define OBD_NOT_FOUND (-1)
1313 static int common_param_init(struct find_param *param, char *path)
1315 int lum_size = get_mds_md_size(path);
1317 if (lum_size < PATH_MAX + 1)
1318 lum_size = PATH_MAX + 1;
1320 param->fp_lum_size = lum_size;
1321 param->fp_lmd = calloc(1, sizeof(lstat_t) + param->fp_lum_size);
1322 if (param->fp_lmd == NULL) {
1323 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1324 "error: allocation of %zu bytes for ioctl",
1325 sizeof(lstat_t) + param->fp_lum_size);
1329 param->fp_lmv_stripe_count = 256;
1330 param->fp_lmv_md = calloc(1,
1331 lmv_user_md_size(param->fp_lmv_stripe_count,
1333 if (param->fp_lmv_md == NULL) {
1334 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1335 "error: allocation of %d bytes for ioctl",
1336 lmv_user_md_size(param->fp_lmv_stripe_count,
1341 param->fp_got_uuids = 0;
1342 param->fp_obd_indexes = NULL;
1343 param->fp_obd_index = OBD_NOT_FOUND;
1344 if (!param->fp_migrate)
1345 param->fp_mdt_index = OBD_NOT_FOUND;
1349 static void find_param_fini(struct find_param *param)
1351 if (param->fp_obd_indexes)
1352 free(param->fp_obd_indexes);
1355 free(param->fp_lmd);
1357 if (param->fp_lmv_md)
1358 free(param->fp_lmv_md);
1361 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1362 struct dirent64 *de)
1364 struct find_param *param = data;
1370 /* set errno upon failure */
1371 static DIR *opendir_parent(const char *path)
1377 path_copy = strdup(path);
1378 if (path_copy == NULL)
1381 parent_path = dirname(path_copy);
1382 parent = opendir(parent_path);
1388 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1393 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1394 if (param->fp_get_default_lmv)
1395 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1397 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1399 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1400 if (errno == E2BIG && ret != 0) {
1404 stripe_count = (__u32)param->fp_lmv_md->lum_stripe_count;
1405 if (stripe_count <= param->fp_lmv_stripe_count)
1408 free(param->fp_lmv_md);
1409 param->fp_lmv_stripe_count = stripe_count;
1410 lmv_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1411 param->fp_lmv_md = malloc(lmv_size);
1412 if (param->fp_lmv_md == NULL) {
1413 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1414 "error: allocation of %d bytes for ioctl",
1415 lmv_user_md_size(param->fp_lmv_stripe_count,
1424 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1425 struct lov_user_mds_data *lmd, int lumlen)
1427 lstat_t *st = &lmd->lmd_st;
1430 if (parent == NULL && dir == NULL)
1434 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1435 } else if (parent) {
1436 char *fname = strrchr(path, '/');
1438 /* To avoid opening, locking, and closing each file on the
1439 * client if that is not needed. The GETFILEINFO ioctl can
1440 * be done on the patent dir with a single open for all
1441 * files in that directory, and it also doesn't pollute the
1442 * client dcache with millions of dentries when traversing
1443 * a large filesystem. */
1444 fname = (fname == NULL ? path : fname + 1);
1445 /* retrieve needed file info */
1446 strlcpy((char *)lmd, fname, lumlen);
1447 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1451 if (errno == ENOTTY) {
1452 /* ioctl is not supported, it is not a lustre fs.
1453 * Do the regular lstat(2) instead. */
1454 ret = lstat_f(path, st);
1457 llapi_error(LLAPI_MSG_ERROR, ret,
1458 "error: %s: lstat failed for %s",
1461 } else if (errno == ENOENT) {
1463 llapi_error(LLAPI_MSG_WARN, ret,
1464 "warning: %s: %s does not exist",
1466 } else if (errno != EISDIR) {
1468 llapi_error(LLAPI_MSG_ERROR, ret,
1469 "%s ioctl failed for %s.",
1470 dir ? "LL_IOC_MDC_GETINFO" :
1471 "IOC_MDC_GETFILEINFO", path);
1474 llapi_error(LLAPI_MSG_ERROR, ret,
1475 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1482 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1483 semantic_func_t sem_init,
1484 semantic_func_t sem_fini, void *data,
1485 struct dirent64 *de)
1487 struct find_param *param = (struct find_param *)data;
1488 struct dirent64 *dent;
1496 if (!d && errno != ENOTDIR) {
1498 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1501 } else if (!d && !parent) {
1502 /* ENOTDIR. Open the parent dir. */
1503 p = opendir_parent(path);
1510 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1516 while ((dent = readdir64(d)) != NULL) {
1519 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1522 /* Don't traverse .lustre directory */
1523 if (!(strcmp(dent->d_name, dot_lustre_name)))
1527 if ((len + dent->d_reclen + 2) > size) {
1528 llapi_err_noerrno(LLAPI_MSG_ERROR,
1529 "error: %s: string buffer is too small",
1534 strcat(path, dent->d_name);
1536 if (dent->d_type == DT_UNKNOWN) {
1537 lstat_t *st = ¶m->fp_lmd->lmd_st;
1539 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
1540 param->fp_lum_size);
1542 dent->d_type = IFTODT(st->st_mode);
1549 switch (dent->d_type) {
1551 llapi_err_noerrno(LLAPI_MSG_ERROR,
1552 "error: %s: '%s' is UNKNOWN type %d",
1553 __func__, dent->d_name, dent->d_type);
1556 rc = llapi_semantic_traverse(path, size, d, sem_init,
1557 sem_fini, data, dent);
1558 if (rc != 0 && ret == 0)
1564 rc = sem_init(path, d, NULL, data, dent);
1565 if (rc < 0 && ret == 0)
1568 if (sem_fini && rc == 0)
1569 sem_fini(path, d, NULL, data, dent);
1577 sem_fini(path, parent, &d, data, de);
1586 static int param_callback(char *path, semantic_func_t sem_init,
1587 semantic_func_t sem_fini, struct find_param *param)
1589 int ret, len = strlen(path);
1592 if (len > PATH_MAX) {
1594 llapi_error(LLAPI_MSG_ERROR, ret,
1595 "Path name '%s' is too long", path);
1599 buf = (char *)malloc(PATH_MAX + 1);
1603 strlcpy(buf, path, PATH_MAX + 1);
1604 ret = common_param_init(param, buf);
1608 param->fp_depth = 0;
1610 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1611 sem_fini, param, NULL);
1613 find_param_fini(param);
1615 return ret < 0 ? ret : 0;
1618 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1620 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1623 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1628 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1630 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1633 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1638 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1642 fd = open(path, O_RDONLY | O_NONBLOCK);
1645 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1649 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1655 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1659 fd = open(path, O_RDONLY | O_NONBLOCK);
1662 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1666 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1678 * If uuidp is NULL, return the number of available obd uuids.
1679 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1680 * there are more OSTs than allocated to uuidp, then an error is returned with
1681 * the ost_count set to number of available obd uuids.
1683 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1684 int *ost_count, enum tgt_type type)
1686 char buf[PATH_MAX], format[32];
1687 int rc = 0, index = 0;
1688 struct obd_uuid name;
1692 /* Get the lov name */
1693 if (type == LOV_TYPE)
1694 rc = llapi_file_fget_lov_uuid(fd, &name);
1696 rc = llapi_file_fget_lmv_uuid(fd, &name);
1700 /* Now get the ost uuids */
1701 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
1702 FILTER_BY_EXACT, "target_obd", ¶m);
1706 fp = fopen(param.gl_pathv[0], "r");
1709 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
1714 snprintf(format, sizeof(format),
1715 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
1716 while (fgets(buf, sizeof(buf), fp) != NULL) {
1717 if (uuidp && (index < *ost_count)) {
1718 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
1726 if (uuidp && (index > *ost_count))
1731 cfs_free_param_data(¶m);
1735 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1737 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1740 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1745 root = open(mnt, O_RDONLY | O_DIRECTORY);
1748 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1753 rc = ioctl(root, LL_IOC_GETOBDCOUNT, count);
1761 /* Check if user specified value matches a real uuid. Ignore _UUID,
1762 * -osc-4ba41334, other trailing gunk in comparison.
1763 * @param real_uuid ends in "_UUID"
1764 * @param search_uuid may or may not end in "_UUID"
1766 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1768 int cmplen = strlen(real_uuid);
1769 int searchlen = strlen(search_uuid);
1771 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1773 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1776 /* The UUIDs may legitimately be different lengths, if
1777 * the system was upgraded from an older version. */
1778 if (cmplen != searchlen)
1781 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1784 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
1785 * returned in param->fp_obd_index */
1786 static int setup_obd_uuid(int fd, char *dname, struct find_param *param)
1788 struct obd_uuid obd_uuid;
1795 if (param->fp_got_uuids)
1798 /* Get the lov/lmv name */
1799 if (param->fp_get_lmv)
1800 rc = llapi_file_fget_lmv_uuid(fd, &obd_uuid);
1802 rc = llapi_file_fget_lov_uuid(fd, &obd_uuid);
1804 if (rc != -ENOTTY) {
1805 llapi_error(LLAPI_MSG_ERROR, rc,
1806 "error: can't get %s name: %s",
1807 param->fp_get_lmv ? "lmv" : "lov",
1815 param->fp_got_uuids = 1;
1817 /* Now get the ost uuids */
1818 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
1819 obd_uuid.uuid, FILTER_BY_EXACT,
1820 "target_obd", ¶m_data);
1824 fp = fopen(param_data.gl_pathv[0], "r");
1827 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
1828 param_data.gl_pathv[0]);
1832 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
1833 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1834 param->fp_get_lmv ? "MDTS" : "OBDS");
1836 snprintf(format, sizeof(format),
1837 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
1838 while (fgets(buf, sizeof(buf), fp) != NULL) {
1841 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
1844 if (param->fp_obd_uuid) {
1845 if (llapi_uuid_match(obd_uuid.uuid,
1846 param->fp_obd_uuid->uuid)) {
1847 param->fp_obd_index = index;
1850 } else if (!param->fp_quiet && !param->fp_obds_printed) {
1851 /* Print everything */
1852 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1855 param->fp_obds_printed = 1;
1859 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
1860 llapi_err_noerrno(LLAPI_MSG_ERROR,
1861 "error: %s: unknown obduuid: %s",
1862 __func__, param->fp_obd_uuid->uuid);
1866 cfs_free_param_data(¶m_data);
1870 /* In this case, param->fp_obd_uuid will be an array of obduuids and
1871 * obd index for all these obduuids will be returned in
1872 * param->fp_obd_indexes */
1873 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1874 int num_obds, int **obdindexes, int *obdindex,
1877 int ret, obdcount, obd_valid = 0, obdnum;
1879 struct obd_uuid *uuids = NULL;
1883 if (type == LOV_TYPE)
1884 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
1886 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
1890 obdcount = atoi(buf);
1891 uuids = malloc(obdcount * sizeof(struct obd_uuid));
1896 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1898 if (ret == -EOVERFLOW) {
1899 struct obd_uuid *uuids_temp;
1901 uuids_temp = realloc(uuids, obdcount *
1902 sizeof(struct obd_uuid));
1903 if (uuids_temp != NULL) {
1905 goto retry_get_uuids;
1910 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
1914 indexes = malloc(num_obds * sizeof(*obdindex));
1915 if (indexes == NULL) {
1920 for (obdnum = 0; obdnum < num_obds; obdnum++) {
1923 /* The user may have specified a simple index */
1924 i = strtol(obduuids[obdnum].uuid, &end, 0);
1925 if (end && *end == '\0' && i < obdcount) {
1926 indexes[obdnum] = i;
1929 for (i = 0; i < obdcount; i++) {
1930 if (llapi_uuid_match(uuids[i].uuid,
1931 obduuids[obdnum].uuid)) {
1932 indexes[obdnum] = i;
1938 if (i >= obdcount) {
1939 indexes[obdnum] = OBD_NOT_FOUND;
1940 llapi_err_noerrno(LLAPI_MSG_ERROR,
1941 "error: %s: unknown obduuid: %s",
1942 __func__, obduuids[obdnum].uuid);
1948 *obdindex = OBD_NOT_FOUND;
1950 *obdindex = obd_valid;
1952 *obdindexes = indexes;
1960 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
1964 if (param->fp_mdt_uuid) {
1965 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
1967 ¶m->fp_mdt_indexes,
1968 ¶m->fp_mdt_index, LMV_TYPE);
1973 if (param->fp_obd_uuid) {
1974 ret = setup_indexes(dir, path, param->fp_obd_uuid,
1976 ¶m->fp_obd_indexes,
1977 ¶m->fp_obd_index, LOV_TYPE);
1982 param->fp_got_uuids = 1;
1987 int llapi_ostlist(char *path, struct find_param *param)
1992 fd = open(path, O_RDONLY | O_DIRECTORY);
1996 ret = setup_obd_uuid(fd, path, param);
2003 * Tries to determine the default stripe attributes for a given filesystem. The
2004 * filesystem to check should be specified by fsname, or will be determined
2007 static int sattr_get_defaults(const char *const fsname,
2008 unsigned int *scount,
2009 unsigned int *ssize,
2010 unsigned int *soffset)
2016 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2017 "stripecount", val, sizeof(val));
2020 *scount = atoi(val);
2024 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2025 "stripesize", val, sizeof(val));
2032 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2033 "stripeoffset", val, sizeof(val));
2036 *soffset = atoi(val);
2043 * Tries to gather the default stripe attributes for a given filesystem. If
2044 * the attributes can be determined, they are cached for easy retreival the
2045 * next time they are needed. Only a single filesystem's attributes are
2048 static int sattr_cache_get_defaults(const char *const fsname,
2049 const char *const pathname,
2050 unsigned int *scount,
2051 unsigned int *ssize,
2052 unsigned int *soffset)
2055 char fsname[PATH_MAX + 1];
2056 unsigned int stripecount;
2057 unsigned int stripesize;
2058 unsigned int stripeoffset;
2064 char fsname_buf[PATH_MAX + 1];
2065 unsigned int tmp[3];
2067 if (fsname == NULL) {
2068 rc = llapi_search_fsname(pathname, fsname_buf);
2072 strlcpy(fsname_buf, fsname, sizeof(fsname_buf));
2075 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2077 * Ensure all 3 sattrs (count, size, and offset) are
2078 * successfully retrieved and stored in tmp before writing to
2081 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2085 cache.stripecount = tmp[0];
2086 cache.stripesize = tmp[1];
2087 cache.stripeoffset = tmp[2];
2088 strlcpy(cache.fsname, fsname_buf, sizeof(cache.fsname));
2092 *scount = cache.stripecount;
2094 *ssize = cache.stripesize;
2096 *soffset = cache.stripeoffset;
2101 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2102 struct lov_user_ost_data_v1 *objects,
2103 int is_dir, int verbose, int depth,
2104 int raw, char *pool_name)
2106 char *prefix = is_dir ? "" : "lmm_";
2107 char *separator = "";
2110 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2111 lmm_oi_set_seq(&lum->lmm_oi, 0);
2112 if (verbose & VERBOSE_DETAIL)
2113 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
2116 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2117 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2119 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2120 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
2122 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: %#jx\n",
2123 (uintmax_t)lmm_oi_seq(&lum->lmm_oi));
2124 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: %#jx\n",
2125 (uintmax_t)lmm_oi_id(&lum->lmm_oi));
2127 if ((verbose & (VERBOSE_DETAIL | VERBOSE_DFID)) && !is_dir) {
2128 if (verbose & ~VERBOSE_DFID)
2129 llapi_printf(LLAPI_MSG_NORMAL, "lmm_fid: ");
2130 /* This needs a bit of hand-holding since old 1.x lmm_oi
2131 * have { oi.oi_id = mds_inum, oi.oi_seq = 0 } and 2.x lmm_oi
2132 * have { oi.oi_id = mds_oid, oi.oi_seq = mds_seq } instead of
2133 * a real FID. Ideally the 2.x code would have stored this
2134 * like a FID with { oi_id = mds_seq, oi_seq = mds_oid } so the
2135 * ostid union lu_fid { f_seq = mds_seq, f_oid = mds_oid }
2136 * worked properly (especially since IGIF FIDs use mds_inum as
2137 * the FID SEQ), but unfortunately that didn't happen.
2139 * Print it to look like an IGIF FID, even though the fields
2140 * are reversed on disk, so that it makes sense to userspace.
2142 * Don't use ostid_id() and ostid_seq(), since they assume the
2143 * oi_fid fields are in the right order. This is why there are
2144 * separate lmm_oi_seq() and lmm_oi_id() routines for this.
2146 * For newer layout types hopefully this will be a real FID. */
2147 llapi_printf(LLAPI_MSG_NORMAL, DFID"\n",
2148 lmm_oi_seq(&lum->lmm_oi) == 0 ?
2149 lmm_oi_id(&lum->lmm_oi) :
2150 lmm_oi_seq(&lum->lmm_oi),
2151 lmm_oi_seq(&lum->lmm_oi) == 0 ?
2152 0 : (__u32)lmm_oi_id(&lum->lmm_oi),
2153 (__u32)(lmm_oi_id(&lum->lmm_oi) >> 32));
2156 if (verbose & VERBOSE_COUNT) {
2157 if (verbose & ~VERBOSE_COUNT)
2158 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
2161 if (!raw && lum->lmm_stripe_count == 0) {
2162 unsigned int scount;
2163 rc = sattr_cache_get_defaults(NULL, path,
2167 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2170 llapi_error(LLAPI_MSG_ERROR, rc,
2171 "Cannot determine default"
2174 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2175 lum->lmm_stripe_count ==
2176 (typeof(lum->lmm_stripe_count))(-1)
2177 ? -1 : lum->lmm_stripe_count);
2180 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2181 (__s16)lum->lmm_stripe_count);
2183 separator = is_dir ? " " : "\n";
2186 if (verbose & VERBOSE_SIZE) {
2187 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2188 if (verbose & ~VERBOSE_SIZE)
2189 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
2191 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
2193 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2196 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2198 llapi_error(LLAPI_MSG_ERROR, rc,
2199 "Cannot determine default"
2202 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2203 lum->lmm_stripe_size);
2205 separator = is_dir ? " " : "\n";
2208 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2209 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2210 if (verbose & ~VERBOSE_LAYOUT)
2211 llapi_printf(LLAPI_MSG_NORMAL, "%spattern: ",
2213 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2217 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2218 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2219 if (verbose & ~VERBOSE_GENERATION)
2220 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
2222 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2223 (int)lum->lmm_layout_gen);
2227 if (verbose & VERBOSE_OFFSET) {
2228 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2229 if (verbose & ~VERBOSE_OFFSET)
2230 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
2233 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2234 lum->lmm_stripe_offset ==
2235 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2236 lum->lmm_stripe_offset);
2238 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2239 objects[0].l_ost_idx);
2240 separator = is_dir ? " " : "\n";
2243 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
2244 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2245 if (verbose & ~VERBOSE_POOL)
2246 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
2248 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2251 if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
2252 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2255 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2256 struct lov_user_ost_data_v1 *objects,
2257 char *path, int is_dir, int obdindex,
2258 int depth, int header, int raw)
2260 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2263 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2264 if (obdindex == objects[i].l_ost_idx) {
2272 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
2273 depth, raw, pool_name);
2275 if (!is_dir && (header & VERBOSE_OBJID) &&
2276 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2278 llapi_printf(LLAPI_MSG_NORMAL,
2279 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2281 for (i = 0; i < lum->lmm_stripe_count; i++) {
2282 int idx = objects[i].l_ost_idx;
2283 long long oid = ostid_id(&objects[i].l_ost_oi);
2284 long long gr = ostid_seq(&objects[i].l_ost_oi);
2285 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) {
2287 sprintf(fmt, "%s%s%s\n",
2288 "\t%6u\t%14llu\t%#13llx\t",
2289 (fid_seq_is_rsvd(gr) ||
2290 fid_seq_is_mdt0(gr)) ?
2291 "%14llu" : "%#14llx", "%s");
2292 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2294 obdindex == idx ? " *" : "");
2298 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2302 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2303 char *path, int obdindex, int depth, int verbose)
2305 struct lmv_user_mds_data *objects = lum->lum_objects;
2306 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2307 int i, obdstripe = 0;
2308 char *separator = "";
2310 if (obdindex != OBD_NOT_FOUND) {
2311 if (lum->lum_stripe_count == 0) {
2312 if (obdindex == lum->lum_stripe_offset)
2315 for (i = 0; i < lum->lum_stripe_count; i++) {
2316 if (obdindex == objects[i].lum_mds) {
2317 llapi_printf(LLAPI_MSG_NORMAL,
2332 /* show all information default */
2334 if (lum->lum_magic == LMV_USER_MAGIC)
2335 verbose = VERBOSE_POOL | VERBOSE_COUNT | VERBOSE_OFFSET;
2337 verbose = VERBOSE_OBJID;
2340 if (depth && path && ((verbose != VERBOSE_OBJID)))
2341 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2343 if (verbose & VERBOSE_COUNT) {
2344 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2345 if (verbose & ~VERBOSE_COUNT)
2346 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2347 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2348 (int)lum->lum_stripe_count);
2349 if (verbose & VERBOSE_OFFSET)
2355 if (verbose & VERBOSE_OFFSET) {
2356 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2357 if (verbose & ~VERBOSE_OFFSET)
2358 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2359 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2360 (int)lum->lum_stripe_offset);
2364 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2365 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2366 if (obdstripe == 1 && lum->lum_stripe_count > 0)
2367 llapi_printf(LLAPI_MSG_NORMAL,
2368 "mdtidx\t\t FID[seq:oid:ver]\n");
2369 for (i = 0; i < lum->lum_stripe_count; i++) {
2370 int idx = objects[i].lum_mds;
2371 struct lu_fid *fid = &objects[i].lum_fid;
2372 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2373 llapi_printf(LLAPI_MSG_NORMAL,
2374 "%6u\t\t "DFID"\t\t%s\n",
2376 obdindex == idx ? " *" : "");
2381 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
2382 pool_name[0] != '\0') {
2383 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2384 if (verbose & ~VERBOSE_POOL)
2385 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2387 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2391 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
2392 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2395 void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
2399 if (param->fp_get_lmv || param->fp_get_default_lmv)
2400 magic = (__u32)param->fp_lmv_md->lum_magic;
2402 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
2405 case LOV_USER_MAGIC_V1:
2406 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
2407 param->fp_lmd->lmd_lmm.lmm_objects,
2409 param->fp_obd_index, param->fp_max_depth,
2410 param->fp_verbose, param->fp_raw);
2412 case LOV_USER_MAGIC_V3: {
2413 char pool_name[LOV_MAXPOOLNAME + 1];
2414 struct lov_user_ost_data_v1 *objects;
2415 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2417 strlcpy(pool_name, lmmv3->lmm_pool_name, sizeof(pool_name));
2418 objects = lmmv3->lmm_objects;
2419 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm,
2420 pool_name[0] == '\0' ? NULL : pool_name,
2421 objects, path, is_dir,
2422 param->fp_obd_index, param->fp_max_depth,
2423 param->fp_verbose, param->fp_raw);
2427 case LMV_USER_MAGIC: {
2428 char pool_name[LOV_MAXPOOLNAME + 1];
2429 struct lmv_user_md *lum;
2431 lum = (struct lmv_user_md *)param->fp_lmv_md;
2432 strlcpy(pool_name, lum->lum_pool_name, sizeof(pool_name));
2433 lmv_dump_user_lmm(lum,
2434 pool_name[0] == '\0' ? NULL : pool_name,
2435 path, param->fp_obd_index,
2436 param->fp_max_depth, param->fp_verbose);
2440 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2441 "(expecting one of %#x %#x %#x %#x)\n",
2442 *(__u32 *)¶m->fp_lmd->lmd_lmm,
2443 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2444 LMV_USER_MAGIC, LMV_MAGIC_V1);
2449 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2455 fname = strrchr(path, '/');
2457 /* It should be a file (or other non-directory) */
2458 if (fname == NULL) {
2459 dname = (char *)malloc(2);
2463 fname = (char *)path;
2465 dname = (char *)malloc(fname - path + 1);
2468 strncpy(dname, path, fname - path);
2469 dname[fname - path] = '\0';
2473 fd = open(dname, O_RDONLY | O_NONBLOCK);
2480 strcpy((char *)lum, fname);
2481 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2484 if (close(fd) == -1 && rc == 0)
2491 int llapi_file_lookup(int dirfd, const char *name)
2493 struct obd_ioctl_data data = { 0 };
2498 if (dirfd < 0 || name == NULL)
2501 data.ioc_version = OBD_IOCTL_VERSION;
2502 data.ioc_len = sizeof(data);
2503 data.ioc_inlbuf1 = (char *)name;
2504 data.ioc_inllen1 = strlen(name) + 1;
2506 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2508 llapi_error(LLAPI_MSG_ERROR, rc,
2509 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2514 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2520 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2521 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2523 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2524 * The table below gives the answers for the specified parameters (value and
2525 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2526 * --------------------------------------
2527 * 1 | file > limit; sign > 0 | -1 / -1 |
2528 * 2 | file = limit; sign > 0 | -1 / -1 |
2529 * 3 | file < limit; sign > 0 | ? / 1 |
2530 * 4 | file > limit; sign = 0 | -1 / -1 |
2531 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2532 * 6 | file < limit; sign = 0 | ? / -1 |
2533 * 7 | file > limit; sign < 0 | 1 / 1 |
2534 * 8 | file = limit; sign < 0 | ? / -1 |
2535 * 9 | file < limit; sign < 0 | ? / -1 |
2536 * --------------------------------------
2537 * Note: 5th actually means that the value is within the interval
2538 * (limit - margin, limit]. */
2539 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2540 int sign, int negopt, unsigned long long margin,
2546 /* Drop the fraction of margin (of days). */
2547 if (file + margin <= limit)
2549 } else if (sign == 0) {
2550 if (file <= limit && file + margin > limit)
2552 else if (file + margin <= limit)
2554 } else if (sign < 0) {
2561 return negopt ? ~ret + 1 : ret;
2564 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2565 * Return -1 or 1 if file timestamp does not or does match the given criteria
2566 * correspondingly. Return 0 if the MDS time is being checked and there are
2567 * attributes on OSTs and it is not yet clear if the timespamp matches.
2569 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2570 * updated timestamps. */
2571 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2576 /* Check if file is accepted. */
2577 if (param->fp_atime) {
2578 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
2579 param->fp_asign, param->fp_exclude_atime,
2586 if (param->fp_mtime) {
2587 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
2588 param->fp_msign, param->fp_exclude_mtime,
2593 /* If the previous check matches, but this one is not yet clear,
2594 * we should return 0 to do an RPC on OSTs. */
2599 if (param->fp_ctime) {
2600 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
2601 param->fp_csign, param->fp_exclude_ctime,
2606 /* If the previous check matches, but this one is not yet clear,
2607 * we should return 0 to do an RPC on OSTs. */
2616 * Check whether the stripes matches the indexes user provided
2620 static int check_obd_match(struct find_param *param)
2622 lstat_t *st = ¶m->fp_lmd->lmd_st;
2623 struct lov_user_ost_data_v1 *lmm_objects;
2626 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
2629 if (!S_ISREG(st->st_mode))
2632 /* Only those files should be accepted, which have a
2633 * stripe on the specified OST. */
2634 if (!param->fp_lmd->lmd_lmm.lmm_stripe_count)
2637 if (param->fp_lmd->lmd_lmm.lmm_magic ==
2638 LOV_USER_MAGIC_V3) {
2639 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2641 lmm_objects = lmmv3->lmm_objects;
2642 } else if (param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2643 lmm_objects = param->fp_lmd->lmd_lmm.lmm_objects;
2645 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2646 __func__, param->fp_lmd->lmd_lmm.lmm_magic);
2650 for (i = 0; i < param->fp_lmd->lmd_lmm.lmm_stripe_count; i++) {
2651 for (j = 0; j < param->fp_num_obds; j++) {
2652 if (param->fp_obd_indexes[j] ==
2653 lmm_objects[i].l_ost_idx) {
2654 if (param->fp_exclude_obd)
2661 if (param->fp_exclude_obd)
2667 static int check_mdt_match(struct find_param *param)
2671 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
2674 /* FIXME: For striped dir, we should get stripe information and check */
2675 for (i = 0; i < param->fp_num_mdts; i++) {
2676 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
2677 return !param->fp_exclude_mdt;
2680 if (param->fp_exclude_mdt)
2687 * Check whether the obd is active or not, if it is
2688 * not active, just print the object affected by this
2691 static int print_failed_tgt(struct find_param *param, char *path, int type)
2693 struct obd_statfs stat_buf;
2694 struct obd_uuid uuid_buf;
2697 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2699 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2700 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2701 ret = llapi_obd_statfs(path, type,
2702 param->fp_obd_index, &stat_buf,
2705 llapi_printf(LLAPI_MSG_NORMAL,
2706 "obd_uuid: %s failed %s ",
2707 param->fp_obd_uuid->uuid,
2714 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
2715 void *data, struct dirent64 *de)
2717 struct find_param *param = (struct find_param *)data;
2718 DIR *dir = dirp == NULL ? NULL : *dirp;
2719 int decision = 1; /* 1 is accepted; -1 is rejected. */
2720 lstat_t *st = ¶m->fp_lmd->lmd_st;
2722 int checked_type = 0;
2725 LASSERT(parent != NULL || dir != NULL);
2727 param->fp_lmd->lmd_lmm.lmm_stripe_count = 0;
2729 /* If a regular expression is presented, make the initial decision */
2730 if (param->fp_pattern != NULL) {
2731 char *fname = strrchr(path, '/');
2732 fname = (fname == NULL ? path : fname + 1);
2733 ret = fnmatch(param->fp_pattern, fname, 0);
2734 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
2735 (ret == 0 && param->fp_exclude_pattern))
2739 /* See if we can check the file type from the dirent. */
2740 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
2743 if (DTTOIF(de->d_type) == param->fp_type) {
2744 if (param->fp_exclude_type)
2747 if (!param->fp_exclude_type)
2754 /* Request MDS for the stat info if some of these parameters need
2755 * to be compared. */
2756 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
2757 param->fp_check_uid || param->fp_check_gid ||
2758 param->fp_atime || param->fp_mtime || param->fp_ctime ||
2759 param->fp_check_pool || param->fp_check_size ||
2760 param->fp_check_stripe_count || param->fp_check_stripe_size ||
2761 param->fp_check_layout)
2764 if (param->fp_type != 0 && checked_type == 0)
2767 if (decision == 0) {
2768 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
2769 param->fp_lum_size);
2770 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
2771 (param->fp_check_pool || param->fp_check_stripe_count ||
2772 param->fp_check_stripe_size || param->fp_check_layout)) {
2773 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
2775 /* We need to "fake" the "use the default" values
2776 * since the lmm struct is zeroed out at this point. */
2777 lmm->lmm_magic = LOV_USER_MAGIC_V1;
2778 lmm->lmm_pattern = 0xFFFFFFFF;
2780 ostid_set_seq(&lmm->lmm_oi,
2781 FID_SEQ_LOV_DEFAULT);
2782 lmm->lmm_stripe_size = 0;
2783 lmm->lmm_stripe_count = 0;
2784 lmm->lmm_stripe_offset = -1;
2786 if (ret == 0 && param->fp_mdt_uuid != NULL) {
2788 ret = llapi_file_fget_mdtidx(dirfd(dir),
2789 ¶m->fp_file_mdt_index);
2790 } else if (S_ISREG(st->st_mode)) {
2793 /* FIXME: we could get the MDT index from the
2794 * file's FID in lmd->lmd_lmm.lmm_oi without
2795 * opening the file, once we are sure that
2796 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
2797 * That would still be an ioctl() to map the
2798 * FID to the MDT, but not an open RPC. */
2799 fd = open(path, O_RDONLY);
2801 ret = llapi_file_fget_mdtidx(fd,
2802 ¶m->fp_file_mdt_index);
2808 /* For a special file, we assume it resides on
2809 * the same MDT as the parent directory. */
2810 ret = llapi_file_fget_mdtidx(dirfd(parent),
2811 ¶m->fp_file_mdt_index);
2824 if (param->fp_type && !checked_type) {
2825 if ((st->st_mode & S_IFMT) == param->fp_type) {
2826 if (param->fp_exclude_type)
2829 if (!param->fp_exclude_type)
2835 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
2836 if (lustre_fs && param->fp_got_uuids &&
2837 param->fp_dev != st->st_dev) {
2838 /* A lustre/lustre mount point is crossed. */
2839 param->fp_got_uuids = 0;
2840 param->fp_obds_printed = 0;
2841 param->fp_mdt_index = OBD_NOT_FOUND;
2842 param->fp_obd_index = OBD_NOT_FOUND;
2845 if (lustre_fs && !param->fp_got_uuids) {
2846 ret = setup_target_indexes(dir ? dir : parent, path,
2851 param->fp_dev = st->st_dev;
2852 } else if (!lustre_fs && param->fp_got_uuids) {
2853 /* A lustre/non-lustre mount point is crossed. */
2854 param->fp_got_uuids = 0;
2855 param->fp_mdt_index = OBD_NOT_FOUND;
2856 param->fp_obd_index = OBD_NOT_FOUND;
2860 if (param->fp_check_stripe_size) {
2861 decision = find_value_cmp(
2862 param->fp_lmd->lmd_lmm.lmm_stripe_size,
2863 param->fp_stripe_size,
2864 param->fp_stripe_size_sign,
2865 param->fp_exclude_stripe_size,
2866 param->fp_stripe_size_units, 0);
2871 if (param->fp_check_stripe_count) {
2872 decision = find_value_cmp(
2873 param->fp_lmd->lmd_lmm.lmm_stripe_count,
2874 param->fp_stripe_count,
2875 param->fp_stripe_count_sign,
2876 param->fp_exclude_stripe_count, 1, 0);
2881 if (param->fp_check_layout) {
2884 found = (param->fp_lmd->lmd_lmm.lmm_pattern & param->fp_layout);
2885 if ((param->fp_lmd->lmd_lmm.lmm_pattern == 0xFFFFFFFF) ||
2886 (found && param->fp_exclude_layout) ||
2887 (!found && !param->fp_exclude_layout)) {
2893 /* If an OBD UUID is specified but none matches, skip this file. */
2894 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
2895 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
2898 /* If an OST or MDT UUID is given, and some OST matches,
2900 if (param->fp_obd_index != OBD_NOT_FOUND ||
2901 param->fp_mdt_index != OBD_NOT_FOUND) {
2902 if (param->fp_obd_uuid) {
2903 if (check_obd_match(param)) {
2904 /* If no mdtuuid is given, we are done.
2905 * Otherwise, fall through to the mdtuuid
2907 if (!param->fp_mdt_uuid)
2914 if (param->fp_mdt_uuid) {
2915 if (check_mdt_match(param))
2922 if (param->fp_check_uid) {
2923 if (st->st_uid == param->fp_uid) {
2924 if (param->fp_exclude_uid)
2927 if (!param->fp_exclude_uid)
2932 if (param->fp_check_gid) {
2933 if (st->st_gid == param->fp_gid) {
2934 if (param->fp_exclude_gid)
2937 if (!param->fp_exclude_gid)
2942 if (param->fp_check_pool) {
2943 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2945 /* empty requested pool is taken as no pool search => V1 */
2946 if (((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
2947 (param->fp_poolname[0] == '\0')) ||
2948 ((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2949 (strncmp(lmmv3->lmm_pool_name,
2950 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
2951 ((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2952 (strcmp(param->fp_poolname, "*") == 0))) {
2953 if (param->fp_exclude_pool)
2956 if (!param->fp_exclude_pool)
2961 /* Check the time on mds. */
2963 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
2966 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
2967 param->fp_lmd->lmd_lmm.lmm_stripe_count)
2969 decision = find_time_check(st, param, for_mds);
2974 /* If file still fits the request, ask ost for updated info.
2975 The regular stat is almost of the same speed as some new
2976 'glimpse-size-ioctl'. */
2978 if (param->fp_check_size && S_ISREG(st->st_mode) &&
2979 param->fp_lmd->lmd_lmm.lmm_stripe_count)
2982 if (param->fp_check_size && S_ISDIR(st->st_mode))
2986 /* For regular files with the stripe the decision may have not
2987 * been taken yet if *time or size is to be checked. */
2988 if (param->fp_obd_index != OBD_NOT_FOUND)
2989 print_failed_tgt(param, path, LL_STATFS_LOV);
2991 if (param->fp_mdt_index != OBD_NOT_FOUND)
2992 print_failed_tgt(param, path, LL_STATFS_LMV);
2995 ret = fstat_f(dirfd(dir), st);
2996 else if (de != NULL)
2997 ret = fstatat_f(dirfd(parent), de->d_name, st,
2998 AT_SYMLINK_NOFOLLOW);
3000 ret = lstat_f(path, st);
3003 if (errno == ENOENT) {
3004 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
3005 "warning: %s: %s does not exist",
3010 llapi_error(LLAPI_MSG_ERROR, ret,
3011 "%s: IOC_LOV_GETINFO on %s failed",
3017 /* Check the time on osc. */
3018 decision = find_time_check(st, param, 0);
3023 if (param->fp_check_size)
3024 decision = find_value_cmp(st->st_size, param->fp_size,
3025 param->fp_size_sign,
3026 param->fp_exclude_size,
3027 param->fp_size_units, 0);
3029 if (decision != -1) {
3030 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
3031 if (param->fp_zero_end)
3032 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
3034 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3038 /* Do not get down anymore? */
3039 if (param->fp_depth == param->fp_max_depth)
3047 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
3048 void *param_data, struct dirent64 *de)
3050 struct find_param *param = (struct find_param *)param_data;
3051 DIR *tmp_parent = parent;
3052 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3054 struct obd_ioctl_data data = { 0 };
3061 LASSERT(parent != NULL || dirp != NULL);
3065 if (parent == NULL) {
3066 tmp_parent = opendir_parent(path);
3067 if (tmp_parent == NULL) {
3070 llapi_error(LLAPI_MSG_ERROR, ret,
3071 "can not open %s", path);
3076 fd = dirfd(tmp_parent);
3078 path_copy = strdup(path);
3079 filename = basename(path_copy);
3080 data.ioc_inlbuf1 = (char *)filename;
3081 data.ioc_inllen1 = strlen(filename) + 1;
3082 data.ioc_inlbuf2 = (char *)¶m->fp_mdt_index;
3083 data.ioc_inllen2 = sizeof(param->fp_mdt_index);
3084 ret = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3086 llapi_error(LLAPI_MSG_ERROR, ret,
3087 "llapi_obd_statfs: error packing ioctl data");
3092 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
3094 if (errno == EBUSY && !retry) {
3095 /* because migrate may not be able to lock all involved
3096 * objects in order, for some of them it try lock, while
3097 * there may be conflicting COS locks and cause migrate
3098 * fail with EBUSY, hope a sync() could cause
3099 * transaction commit and release these COS locks. */
3105 fprintf(stderr, "%s migrate failed: %s (%d)\n",
3106 path, strerror(-ret), ret);
3108 } else if (param->fp_verbose & VERBOSE_DETAIL) {
3109 fprintf(stdout, "migrate %s to MDT%d\n",
3110 path, param->fp_mdt_index);
3115 /* If the directory is being migration, we need
3116 * close the directory after migration,
3117 * so the old directory cache will be cleanup
3118 * on the client side, and re-open to get the
3119 * new directory handle */
3120 *dirp = opendir(path);
3121 if (*dirp == NULL) {
3123 llapi_error(LLAPI_MSG_ERROR, ret,
3124 "%s: Failed to open '%s'", __func__, path);
3129 closedir(tmp_parent);
3136 int llapi_migrate_mdt(char *path, struct find_param *param)
3138 return param_callback(path, cb_migrate_mdt_init, cb_common_fini, param);
3141 int llapi_mv(char *path, struct find_param *param)
3143 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 53, 0)
3144 static bool printed;
3147 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
3148 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
3152 return llapi_migrate_mdt(path, param);
3155 int llapi_find(char *path, struct find_param *param)
3157 return param_callback(path, cb_find_init, cb_common_fini, param);
3161 * Get MDT number that the file/directory inode referenced
3162 * by the open fd resides on.
3163 * Return 0 and mdtidx on success, or -ve errno.
3165 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
3167 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
3172 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
3173 struct dirent64 *de)
3175 struct find_param *param = (struct find_param *)data;
3176 DIR *d = dirp == NULL ? NULL : *dirp;
3180 LASSERT(parent != NULL || d != NULL);
3183 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3184 } else /* if (parent) */ {
3187 fd = open(path, O_RDONLY | O_NOCTTY);
3189 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3197 if (ret == -ENODATA) {
3198 if (!param->fp_obd_uuid)
3199 llapi_printf(LLAPI_MSG_NORMAL,
3200 "'%s' has no stripe info\n", path);
3202 } else if (ret == -ENOENT) {
3203 llapi_error(LLAPI_MSG_WARN, ret,
3204 "warning: %s: '%s' does not exist",
3207 } else if (ret == -ENOTTY) {
3208 llapi_error(LLAPI_MSG_ERROR, ret,
3209 "%s: '%s' not on a Lustre fs",
3212 llapi_error(LLAPI_MSG_ERROR, ret,
3213 "error: %s: '%s' failed get_mdtidx",
3219 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
3220 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3222 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3226 /* Do not go down anymore? */
3227 if (param->fp_depth == param->fp_max_depth)
3235 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
3236 struct dirent64 *de)
3238 struct find_param *param = (struct find_param *)data;
3239 DIR *d = dirp == NULL ? NULL : *dirp;
3242 LASSERT(parent != NULL || d != NULL);
3244 if (param->fp_obd_uuid) {
3245 param->fp_quiet = 1;
3246 ret = setup_obd_uuid(d ? dirfd(d) : dirfd(parent), path, param);
3252 if (param->fp_get_lmv || param->fp_get_default_lmv) {
3253 ret = cb_get_dirstripe(path, d, param);
3255 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
3256 (void *)¶m->fp_lmd->lmd_lmm);
3259 } else if (parent && !param->fp_get_lmv && !param->fp_get_default_lmv) {
3260 char *fname = strrchr(path, '/');
3261 fname = (fname == NULL ? path : fname + 1);
3263 strlcpy((char *)¶m->fp_lmd->lmd_lmm, fname,
3264 param->fp_lum_size);
3266 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
3267 (void *)¶m->fp_lmd->lmd_lmm);
3273 if (errno == ENODATA && d != NULL) {
3274 /* We need to "fake" the "use the default" values
3275 * since the lmm struct is zeroed out at this point.
3276 * The magic needs to be set in order to satisfy
3277 * a check later on in the code path.
3278 * The object_seq needs to be set for the "(Default)"
3279 * prefix to be displayed. */
3280 if (param->fp_get_default_lmv) {
3281 struct lmv_user_md *lum = param->fp_lmv_md;
3283 lum->lum_magic = LMV_USER_MAGIC;
3284 lum->lum_stripe_count = 0;
3285 lum->lum_stripe_offset = -1;
3287 } else if (param->fp_get_lmv) {
3288 struct lmv_user_md *lum = param->fp_lmv_md;
3291 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3294 lum->lum_magic = LMV_MAGIC_V1;
3295 lum->lum_stripe_count = 0;
3296 lum->lum_stripe_offset = mdtidx;
3299 struct lov_user_md *lmm =
3300 ¶m->fp_lmd->lmd_lmm;
3302 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3304 ostid_set_seq(&lmm->lmm_oi,
3305 FID_SEQ_LOV_DEFAULT);
3306 lmm->lmm_stripe_count = 0;
3307 lmm->lmm_stripe_size = 0;
3308 lmm->lmm_stripe_offset = -1;
3311 } else if (errno == ENODATA && parent != NULL) {
3312 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
3313 llapi_printf(LLAPI_MSG_NORMAL,
3314 "%s has no stripe info\n", path);
3316 } else if (errno == ENOENT) {
3317 llapi_error(LLAPI_MSG_WARN, -ENOENT,
3318 "warning: %s: %s does not exist",
3321 } else if (errno == ENOTTY) {
3323 llapi_error(LLAPI_MSG_ERROR, ret,
3324 "%s: '%s' not on a Lustre fs?",
3329 llapi_error(LLAPI_MSG_ERROR, ret,
3330 "error: %s: %s failed for %s",
3331 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
3332 "IOC_MDC_GETFILESTRIPE", path);
3339 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
3340 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
3343 /* Do not get down anymore? */
3344 if (param->fp_depth == param->fp_max_depth)
3352 int llapi_getstripe(char *path, struct find_param *param)
3354 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
3355 cb_get_mdt_index : cb_getstripe,
3356 cb_common_fini, param);
3359 int llapi_obd_fstatfs(int fd, __u32 type, __u32 index,
3360 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
3362 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3364 struct obd_ioctl_data data = { 0 };
3367 data.ioc_inlbuf1 = (char *)&type;
3368 data.ioc_inllen1 = sizeof(__u32);
3369 data.ioc_inlbuf2 = (char *)&index;
3370 data.ioc_inllen2 = sizeof(__u32);
3371 data.ioc_pbuf1 = (char *)stat_buf;
3372 data.ioc_plen1 = sizeof(struct obd_statfs);
3373 data.ioc_pbuf2 = (char *)uuid_buf;
3374 data.ioc_plen2 = sizeof(struct obd_uuid);
3376 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3378 llapi_error(LLAPI_MSG_ERROR, rc,
3379 "llapi_obd_statfs: error packing ioctl data");
3383 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
3385 return rc < 0 ? -errno : 0;
3388 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
3389 struct obd_statfs *stat_buf, struct obd_uuid *uuid_buf)
3394 fd = open(path, O_RDONLY);
3397 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
3399 /* If we can't even open a file on the filesystem (e.g. with
3400 * -ESHUTDOWN), force caller to exit or it will loop forever. */
3404 rc = llapi_obd_fstatfs(fd, type, index, stat_buf, uuid_buf);
3411 #define MAX_STRING_SIZE 128
3413 int llapi_ping(char *obd_type, char *obd_name)
3419 rc = cfs_get_param_paths(&path, "%s/%s/ping",
3420 obd_type, obd_name);
3424 fd = open(path.gl_pathv[0], O_WRONLY);
3427 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
3432 /* The purpose is to send a byte as a ping, whatever this byte is. */
3433 /* coverity[uninit_use_in_call] */
3434 rc = write(fd, buf, 1);
3442 cfs_free_param_data(&path);
3446 int llapi_target_iterate(int type_num, char **obd_type,
3447 void *args, llapi_cb_t cb)
3449 char buf[MAX_STRING_SIZE];
3454 rc = cfs_get_param_paths(¶m, "devices");
3458 fp = fopen(param.gl_pathv[0], "r");
3461 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
3466 while (fgets(buf, sizeof(buf), fp) != NULL) {
3467 char *obd_type_name = NULL;
3468 char *obd_name = NULL;
3469 char *obd_uuid = NULL;
3471 struct obd_statfs osfs_buffer;
3473 while(bufp[0] == ' ')
3476 for(i = 0; i < 3; i++) {
3477 obd_type_name = strsep(&bufp, " ");
3479 obd_name = strsep(&bufp, " ");
3480 obd_uuid = strsep(&bufp, " ");
3482 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
3484 for (i = 0; i < type_num; i++) {
3485 if (strcmp(obd_type_name, obd_type[i]) != 0)
3488 cb(obd_type_name, obd_name, obd_uuid, args);
3493 cfs_free_param_data(¶m);
3497 static void do_target_check(char *obd_type_name, char *obd_name,
3498 char *obd_uuid, void *args)
3502 rc = llapi_ping(obd_type_name, obd_name);
3503 if (rc == ENOTCONN) {
3504 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
3506 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
3508 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
3512 int llapi_target_check(int type_num, char **obd_type, char *dir)
3514 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
3517 #undef MAX_STRING_SIZE
3519 /* Is this a lustre fs? */
3520 int llapi_is_lustre_mnttype(const char *type)
3522 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
3525 /* Is this a lustre client fs? */
3526 int llapi_is_lustre_mnt(struct mntent *mnt)
3528 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
3529 strstr(mnt->mnt_fsname, ":/") != NULL);
3532 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3534 char fsname[PATH_MAX + 1];
3538 rc = llapi_search_fsname(mnt, fsname);
3540 llapi_err_noerrno(LLAPI_MSG_ERROR,
3541 "'%s' isn't on Lustre filesystem", mnt);
3545 root = open(mnt, O_RDONLY | O_DIRECTORY);
3548 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3552 rc = ioctl(root, OBD_IOC_QUOTACTL, qctl);
3560 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
3561 * format must have %s%s, buf must be > 16
3562 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
3563 * then buf = "lustre-MDT0000"
3565 static int get_mdtname(char *name, char *format, char *buf)
3567 char suffix[]="-MDT0000";
3568 int len = strlen(name);
3570 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
3571 name[len - 5] = '\0';
3576 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
3579 /* Not enough room to add suffix */
3580 llapi_err_noerrno(LLAPI_MSG_ERROR,
3581 "MDT name too long |%s|", name);
3586 return sprintf(buf, format, name, suffix);
3589 /** ioctl on filsystem root, with mdtindex sent as data
3590 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
3591 * \param mdtidxp pointer to integer within data to be filled in with the
3592 * mdt index (0 if no mdt is specified). NULL won't be filled.
3594 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
3602 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
3603 Open root and parse mdt index. */
3604 if (mdtname[0] == '/') {
3606 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
3607 (char *)mdtname, -1);
3609 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
3611 ptr = fsname + strlen(fsname) - 8;
3613 index = strtol(ptr + 4, NULL, 10);
3614 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
3618 llapi_err_noerrno(LLAPI_MSG_ERROR,
3619 "Can't open %s: %d\n", mdtname, rc);
3626 rc = ioctl(fd, opc, data);
3635 /****** Changelog API ********/
3637 static int changelog_ioctl(const char *mdtname, int opc, int id,
3638 long long recno, int flags)
3640 struct ioc_changelog data;
3644 data.icc_recno = recno;
3645 data.icc_flags = flags;
3646 idx = (int *)(&data.icc_mdtindex);
3648 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
3651 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
3652 struct changelog_private {
3654 enum changelog_send_flag flags;
3655 struct lustre_kernelcomm kuc;
3658 /** Start reading from a changelog
3659 * @param priv Opaque private control structure
3660 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
3661 * @param device Report changes recorded on this MDT
3662 * @param startrec Report changes beginning with this record number
3663 * (just call llapi_changelog_fini when done; don't need an endrec)
3665 int llapi_changelog_start(void **priv, enum changelog_send_flag flags,
3666 const char *device, long long startrec)
3668 struct changelog_private *cp;
3672 /* Set up the receiver control struct */
3673 cp = calloc(1, sizeof(*cp));
3677 cp->magic = CHANGELOG_PRIV_MAGIC;
3680 /* Set up the receiver */
3681 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */, 0);
3687 /* CHANGELOG_FLAG_JOBID will eventually become mandatory. Display a
3688 * warning if it's missing. */
3689 if (!(flags & CHANGELOG_FLAG_JOBID) && !warned) {
3690 llapi_err_noerrno(LLAPI_MSG_WARN, "warning: %s() called "
3691 "w/o CHANGELOG_FLAG_JOBID", __func__);
3695 /* Tell the kernel to start sending */
3696 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
3698 /* Only the kernel reference keeps the write side open */
3699 close(cp->kuc.lk_wfd);
3700 cp->kuc.lk_wfd = LK_NOFD;
3702 /* frees and clears priv */
3703 llapi_changelog_fini(priv);
3714 /** Finish reading from a changelog */
3715 int llapi_changelog_fini(void **priv)
3717 struct changelog_private *cp = (struct changelog_private *)*priv;
3719 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3722 libcfs_ukuc_stop(&cp->kuc);
3729 * Convert all records to a same format according to the caller's wishes.
3730 * Default is CLF_VERSION | CLF_RENAME.
3731 * Add CLF_JOBID if explicitely requested.
3733 * \param rec The record to remap. It is expected to be big enough to
3734 * properly handle the final format.
3735 * \return 1 if anything changed. 0 otherwise.
3737 /** Read the next changelog entry
3738 * @param priv Opaque private control structure
3739 * @param rech Changelog record handle; record will be allocated here
3740 * @return 0 valid message received; rec is set
3744 #define DEFAULT_RECORD_FMT (CLF_VERSION | CLF_RENAME)
3745 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
3747 struct changelog_private *cp = (struct changelog_private *)priv;
3748 struct kuc_hdr *kuch;
3749 enum changelog_rec_flags rec_fmt = DEFAULT_RECORD_FMT;
3752 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3756 kuch = malloc(KUC_CHANGELOG_MSG_MAXSIZE);
3760 if (cp->flags & CHANGELOG_FLAG_JOBID)
3761 rec_fmt |= CLF_JOBID;
3764 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
3765 KUC_CHANGELOG_MSG_MAXSIZE,
3766 KUC_TRANSPORT_CHANGELOG);
3770 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
3771 ((kuch->kuc_msgtype != CL_RECORD) &&
3772 (kuch->kuc_msgtype != CL_EOF))) {
3773 llapi_err_noerrno(LLAPI_MSG_ERROR,
3774 "Unknown changelog message type %d:%d\n",
3775 kuch->kuc_transport, kuch->kuc_msgtype);
3780 if (kuch->kuc_msgtype == CL_EOF) {
3781 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
3790 /* Our message is a changelog_rec. Use pointer math to skip
3791 * kuch_hdr and point directly to the message payload. */
3792 *rech = (struct changelog_rec *)(kuch + 1);
3793 changelog_remap_rec(*rech, rec_fmt);
3803 /** Release the changelog record when done with it. */
3804 int llapi_changelog_free(struct changelog_rec **rech)
3807 /* We allocated memory starting at the kuc_hdr, but passed
3808 * the consumer a pointer to the payload.
3809 * Use pointer math to get back to the header.
3811 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
3818 int llapi_changelog_clear(const char *mdtname, const char *idstr,
3824 llapi_err_noerrno(LLAPI_MSG_ERROR,
3825 "can't purge negative records\n");
3829 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
3830 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
3831 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
3832 llapi_err_noerrno(LLAPI_MSG_ERROR,
3833 "expecting id of the form '"
3834 CHANGELOG_USER_PREFIX
3835 "<num>'; got '%s'\n", idstr);
3839 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
3842 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
3843 int buflen, long long *recno, int *linkno)
3845 const char *fidstr_orig = fidstr;
3847 struct getinfo_fid2path *gf;
3850 while (*fidstr == '[')
3853 sscanf(fidstr, SFID, RFID(&fid));
3854 if (!fid_is_sane(&fid)) {
3855 llapi_err_noerrno(LLAPI_MSG_ERROR,
3856 "bad FID format '%s', should be [seq:oid:ver]"
3857 " (e.g. "DFID")\n", fidstr_orig,
3858 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
3862 gf = malloc(sizeof(*gf) + buflen);
3867 gf->gf_recno = *recno;
3868 gf->gf_linkno = *linkno;
3869 gf->gf_pathlen = buflen;
3871 /* Take path or fsname */
3872 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
3876 memcpy(buf, gf->gf_u.gf_path, gf->gf_pathlen);
3877 if (buf[0] == '\0') { /* ROOT path */
3881 *recno = gf->gf_recno;
3882 *linkno = gf->gf_linkno;
3889 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
3892 struct lustre_mdt_attrs *lma;
3896 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
3898 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
3901 lma = (struct lustre_mdt_attrs *)buf;
3902 fid_le_to_cpu(fid, &lma->lma_self_fid);
3906 int llapi_get_mdt_index_by_fid(int fd, const lustre_fid *fid,
3911 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
3920 int llapi_fd2fid(const int fd, lustre_fid *fid)
3924 memset(fid, 0, sizeof(*fid));
3926 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
3927 if (rc == -EINVAL || rc == -ENOTTY)
3928 rc = fid_from_lma(NULL, fd, fid);
3933 int llapi_path2fid(const char *path, lustre_fid *fid)
3937 memset(fid, 0, sizeof(*fid));
3938 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
3940 if (errno == ELOOP || errno == ENXIO)
3941 return fid_from_lma(path, -1, fid);
3945 rc = llapi_fd2fid(fd, fid);
3946 if (rc == -EINVAL || rc == -ENOTTY)
3947 rc = fid_from_lma(path, -1, fid);
3953 int llapi_fd2parent(int fd, unsigned int linkno, lustre_fid *parent_fid,
3954 char *name, size_t name_size)
3956 struct getparent *gp;
3959 gp = malloc(sizeof(*gp) + name_size);
3963 gp->gp_linkno = linkno;
3964 gp->gp_name_size = name_size;
3966 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
3972 *parent_fid = gp->gp_fid;
3974 strncpy(name, gp->gp_name, name_size);
3975 name[name_size - 1] = '\0';
3982 int llapi_path2parent(const char *path, unsigned int linkno,
3983 lustre_fid *parent_fid, char *name, size_t name_size)
3988 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
3992 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
3997 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4002 root = open(mnt, O_RDONLY | O_DIRECTORY);
4005 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4009 rc = ioctl(root, LL_IOC_GET_CONNECT_FLAGS, flags);
4012 llapi_error(LLAPI_MSG_ERROR, rc,
4013 "ioctl on %s for getting connect flags failed", mnt);
4020 * Get a 64-bit value representing the version of file data pointed by fd.
4022 * Each write or truncate, flushed on OST, will change this value. You can use
4023 * this value to verify if file data was modified. This only checks the file
4024 * data, not metadata.
4026 * \param flags 0: no flush pages, usually used it the process has already
4028 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
4030 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
4031 * pages from clients.
4033 * \retval 0 on success.
4034 * \retval -errno on error.
4036 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4039 struct ioc_data_version idv;
4041 idv.idv_flags = flags;
4043 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4047 *data_version = idv.idv_version;
4053 * Create a file without any name open it for read/write
4055 * - file is created as if it were a standard file in the given \a directory
4056 * - file does not appear in \a directory and mtime does not change because
4057 * the filename is handled specially by the Lustre MDS.
4058 * - file is removed at final close
4059 * - file modes are rw------- since it doesn't make sense to have a read-only
4060 * or write-only file that cannot be opened again.
4061 * - if user wants another mode it must use fchmod() on the open file, no
4062 * security problems arise because it cannot be opened by another process.
4064 * \param[in] directory directory from which to inherit layout/MDT idx
4065 * \param[in] idx MDT index on which the file is created,
4066 * \a idx == -1 means no specific MDT is requested
4067 * \param[in] open_flags standard open(2) flags
4069 * \retval 0 on success.
4070 * \retval -errno on error.
4072 int llapi_create_volatile_idx(char *directory, int idx, int open_flags)
4074 char file_path[PATH_MAX];
4075 char filename[PATH_MAX];
4076 int saved_errno = errno;
4084 snprintf(filename, sizeof(filename),
4085 LUSTRE_VOLATILE_HDR"::%.4X", rnumber);
4087 snprintf(filename, sizeof(filename),
4088 LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, rnumber);
4090 rc = snprintf(file_path, sizeof(file_path),
4091 "%s/%s", directory, filename);
4092 if (rc >= sizeof(file_path))
4095 fd = open(file_path,
4096 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | open_flags,
4098 } while (fd < 0 && errno == EEXIST);
4101 llapi_error(LLAPI_MSG_ERROR, errno,
4102 "Cannot create volatile file '%s' in '%s'",
4103 filename + LUSTRE_VOLATILE_HDR_LEN,
4108 /* Unlink file in case this wasn't a Lustre filesystem and the
4109 * magic volatile filename wasn't handled as intended. The
4110 * effect is the same. If volatile open was supported then we
4111 * expect unlink() to return -ENOENT. */
4112 (void)unlink(file_path);
4114 /* Since we are returning successfully we restore errno (and
4115 * mask out possible EEXIST from open() and ENOENT from
4117 errno = saved_errno;
4123 * Swap the layouts between 2 file descriptors
4124 * the 2 files must be open for writing
4125 * first fd received the ioctl, second fd is passed as arg
4126 * this is assymetric but avoid use of root path for ioctl
4128 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
4129 int gid, __u64 flags)
4131 struct lustre_swap_layouts lsl;
4136 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4137 rc = fstat(fd1, &st1);
4141 rc = fstat(fd2, &st2);
4146 lsl.sl_flags = flags;
4150 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4154 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4155 struct timeval tv1[2];
4156 struct timeval tv2[2];
4158 memset(tv1, 0, sizeof(tv1));
4159 memset(tv2, 0, sizeof(tv2));
4161 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
4162 tv1[0].tv_sec = st1.st_atime;
4163 tv2[0].tv_sec = st2.st_atime;
4165 tv1[0].tv_sec = st2.st_atime;
4166 tv2[0].tv_sec = st1.st_atime;
4169 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
4170 tv1[1].tv_sec = st1.st_mtime;
4171 tv2[1].tv_sec = st2.st_mtime;
4173 tv1[1].tv_sec = st2.st_mtime;
4174 tv2[1].tv_sec = st1.st_mtime;
4177 rc = futimes(fd1, tv1);
4181 rc = futimes(fd2, tv2);
4189 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4196 while (grp_id == 0);
4198 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
4206 * Swap the layouts between 2 files
4207 * the 2 files are open in write
4209 int llapi_swap_layouts(const char *path1, const char *path2,
4210 __u64 dv1, __u64 dv2, __u64 flags)
4214 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4217 llapi_error(LLAPI_MSG_ERROR, rc,
4218 "error: cannot open '%s' for write", path1);
4222 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4225 llapi_error(LLAPI_MSG_ERROR, rc,
4226 "error: cannot open '%s' for write", path2);
4230 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4232 llapi_error(LLAPI_MSG_ERROR, rc,
4233 "error: cannot swap layout between '%s' and '%s'",
4244 * Attempt to open a file with Lustre file identifier \a fid
4245 * and return an open file descriptor.
4247 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
4248 * \param[in] fid Lustre file identifier of file to open
4249 * \param[in] flags open() flags
4251 * \retval non-negative file descriptor on successful open
4252 * \retval -1 if an error occurred
4254 int llapi_open_by_fid(const char *lustre_dir, const lustre_fid *fid, int flags)
4256 char mntdir[PATH_MAX];
4257 char path[PATH_MAX];
4260 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
4264 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
4265 return open(path, flags);
4271 * \param fd File to lock.
4272 * \param gid Group Identifier.
4274 * \retval 0 on success.
4275 * \retval -errno on failure.
4277 int llapi_group_lock(int fd, int gid)
4281 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
4284 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
4292 * \param fd File to unlock.
4293 * \param gid Group Identifier.
4295 * \retval 0 on success.
4296 * \retval -errno on failure.
4298 int llapi_group_unlock(int fd, int gid)
4302 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
4305 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");