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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/liblustreapi.c
38 * Author: Peter J. Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
40 * Author: Robert Read <rread@clusterfs.com>
53 #include <sys/ioctl.h>
60 #include <sys/statfs.h>
61 #include <sys/types.h>
62 #include <sys/syscall.h>
63 #include <sys/xattr.h>
65 #include <libgen.h> /* for dirname() */
66 #ifdef HAVE_LINUX_UNISTD_H
67 #include <linux/unistd.h>
73 #include <libcfs/util/param.h>
74 #include <libcfs/util/string.h>
75 #include <lnet/lnetctl.h>
76 #include <lustre/lustreapi.h>
77 #include <lustre_ioctl.h>
78 #include "lustreapi_internal.h"
80 static int llapi_msg_level = LLAPI_MSG_MAX;
82 void llapi_msg_set_level(int level)
84 /* ensure level is in the good range */
85 if (level < LLAPI_MSG_OFF)
86 llapi_msg_level = LLAPI_MSG_OFF;
87 else if (level > LLAPI_MSG_MAX)
88 llapi_msg_level = LLAPI_MSG_MAX;
90 llapi_msg_level = level;
93 int llapi_msg_get_level(void)
95 return llapi_msg_level;
98 static void error_callback_default(enum llapi_message_level level, int err,
99 const char *fmt, va_list ap)
101 vfprintf(stderr, fmt, ap);
102 if (level & LLAPI_MSG_NO_ERRNO)
103 fprintf(stderr, "\n");
105 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
108 static void info_callback_default(enum llapi_message_level level, int err,
109 const char *fmt, va_list ap)
111 vfprintf(stdout, fmt, ap);
114 static llapi_log_callback_t llapi_error_callback = error_callback_default;
115 static llapi_log_callback_t llapi_info_callback = info_callback_default;
118 /* llapi_error will preserve errno */
119 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
122 int tmp_errno = errno;
124 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
128 llapi_error_callback(level, abs(err), fmt, args);
133 /* llapi_printf will preserve errno */
134 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
137 int tmp_errno = errno;
139 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
143 llapi_info_callback(level, 0, fmt, args);
149 * Set a custom error logging function. Passing in NULL will reset the logging
150 * callback to its default value.
152 * This function returns the value of the old callback.
154 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
156 llapi_log_callback_t old = llapi_error_callback;
159 llapi_error_callback = cb;
161 llapi_error_callback = error_callback_default;
167 * Set a custom info logging function. Passing in NULL will reset the logging
168 * callback to its default value.
170 * This function returns the value of the old callback.
172 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
174 llapi_log_callback_t old = llapi_info_callback;
177 llapi_info_callback = cb;
179 llapi_info_callback = info_callback_default;
185 * size_units is to be initialized (or zeroed) by caller.
187 int llapi_parse_size(const char *optarg, unsigned long long *size,
188 unsigned long long *size_units, int bytes_spec)
191 char *argbuf = (char *)optarg;
192 unsigned long long frac = 0, frac_d = 1;
194 if (strncmp(optarg, "-", 1) == 0)
197 if (*size_units == 0)
200 *size = strtoull(argbuf, &end, 0);
201 if (end != NULL && *end == '.') {
205 frac = strtoull(argbuf, &end, 10);
206 /* count decimal places */
207 for (i = 0; i < (end - argbuf); i++)
212 if ((*end == 'b') && *(end + 1) == '\0' &&
213 (*size & (~0ULL << (64 - 9))) == 0 &&
215 *size_units = 1 << 9;
216 } else if ((*end == 'b') &&
217 *(end + 1) == '\0' &&
220 } else if ((*end == 'k' || *end == 'K') &&
221 *(end + 1) == '\0' &&
222 (*size & (~0ULL << (64 - 10))) == 0) {
223 *size_units = 1 << 10;
224 } else if ((*end == 'm' || *end == 'M') &&
225 *(end + 1) == '\0' &&
226 (*size & (~0ULL << (64 - 20))) == 0) {
227 *size_units = 1 << 20;
228 } else if ((*end == 'g' || *end == 'G') &&
229 *(end + 1) == '\0' &&
230 (*size & (~0ULL << (64 - 30))) == 0) {
231 *size_units = 1 << 30;
232 } else if ((*end == 't' || *end == 'T') &&
233 *(end + 1) == '\0' &&
234 (*size & (~0ULL << (64 - 40))) == 0) {
235 *size_units = 1ULL << 40;
236 } else if ((*end == 'p' || *end == 'P') &&
237 *(end + 1) == '\0' &&
238 (*size & (~0ULL << (64 - 50))) == 0) {
239 *size_units = 1ULL << 50;
240 } else if ((*end == 'e' || *end == 'E') &&
241 *(end + 1) == '\0' &&
242 (*size & (~0ULL << (64 - 60))) == 0) {
243 *size_units = 1ULL << 60;
248 *size = *size * *size_units + frac * *size_units / frac_d;
253 /* XXX: llapi_xxx() functions return negative values upon failure */
255 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
256 int stripe_count, int stripe_pattern)
260 /* 64 KB is the largest common page size I'm aware of (on ia64), but
261 * check the local page size just in case. */
262 page_size = LOV_MIN_STRIPE_SIZE;
263 if (getpagesize() > page_size) {
264 page_size = getpagesize();
265 llapi_err_noerrno(LLAPI_MSG_WARN,
266 "warning: your page size (%u) is "
267 "larger than expected (%u)", page_size,
268 LOV_MIN_STRIPE_SIZE);
270 if (!llapi_stripe_size_is_aligned(stripe_size)) {
272 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
273 "must be an even multiple of %d bytes",
274 stripe_size, page_size);
277 if (!llapi_stripe_index_is_valid(stripe_offset)) {
279 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
283 if (!llapi_stripe_count_is_valid(stripe_count)) {
285 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
289 if (llapi_stripe_size_is_too_big(stripe_size)) {
291 llapi_error(LLAPI_MSG_ERROR, rc,
292 "warning: stripe size 4G or larger "
293 "is not currently supported and would wrap");
300 * Trim a trailing newline from a string, if it exists.
302 int llapi_chomp_string(char *buf)
318 * Wrapper to grab parameter settings for lov.*-clilov-*.* values
320 static int get_param_lov(const char *path, const char *param,
321 char *buf, size_t buf_size)
323 struct obd_uuid uuid;
326 rc = llapi_file_get_lov_uuid(path, &uuid);
330 return get_lustre_param_value("lov", uuid.uuid, FILTER_BY_EXACT, param,
335 * Wrapper to grab parameter settings for lmv.*-clilov-*.* values
337 static int get_param_lmv(const char *path, const char *param,
338 char *buf, size_t buf_size)
340 struct obd_uuid uuid;
343 rc = llapi_file_get_lmv_uuid(path, &uuid);
347 return get_lustre_param_value("lmv", uuid.uuid, FILTER_BY_EXACT, param,
351 static int get_mds_md_size(const char *path)
353 char buf[PATH_MAX], inst[PATH_MAX];
354 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
357 rc = llapi_getname(path, inst, sizeof(inst));
361 /* Get the max ea size from llite parameters. */
362 rc = get_lustre_param_value("llite", inst, FILTER_BY_EXACT,
363 "max_easize", buf, sizeof(buf));
369 return rc > 0 ? rc : md_size;
372 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
374 return get_param_lmv(path, "uuid", buf, bufsize);
378 * if pool is NULL, search ostname in target_obd
379 * if pool is not NULL:
380 * if pool not found returns errno < 0
381 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
382 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
384 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
386 char buffer[PATH_MAX];
392 /* You need one or the other */
393 if (poolname == NULL && fsname == NULL)
397 len = strlen(ostname);
399 if (poolname == NULL && len == 0)
402 /* Search by poolname and fsname if is not NULL */
403 if (poolname != NULL) {
404 rc = poolpath(¶m, fsname, NULL);
406 snprintf(buffer, sizeof(buffer), "%s/%s",
407 param.gl_pathv[0], poolname);
409 } else if (fsname != NULL) {
410 rc = get_lustre_param_path("lov", fsname,
412 "target_obd", ¶m);
414 strncpy(buffer, param.gl_pathv[0],
420 cfs_free_param_data(¶m);
424 fd = fopen(buffer, "r");
428 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
429 if (poolname == NULL) {
431 /* Search for an ostname in the list of OSTs
432 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
433 ptr = strchr(buffer, ' ');
435 (strncmp(ptr + 1, ostname, len) == 0)) {
440 /* Search for an ostname in a pool,
441 (or an existing non-empty pool if no ostname) */
442 if ((ostname == NULL) ||
443 (strncmp(buffer, ostname, len) == 0)) {
454 * Open a Lustre file.
456 * \param name the name of the file to be opened
457 * \param flags access mode, see flags in open(2)
458 * \param mode permission of the file if it is created, see mode in open(2)
459 * \param param stripe pattern of the newly created file
461 * \retval file descriptor of opened file
462 * \retval negative errno on failure
464 int llapi_file_open_param(const char *name, int flags, mode_t mode,
465 const struct llapi_stripe_param *param)
467 char fsname[MAX_OBD_NAME + 1] = { 0 };
468 char *pool_name = param->lsp_pool;
469 struct lov_user_md *lum = NULL;
470 size_t lum_size = sizeof(*lum);
473 /* Make sure we are on a Lustre file system */
474 rc = llapi_search_fsname(name, fsname);
476 llapi_error(LLAPI_MSG_ERROR, rc,
477 "'%s' is not on a Lustre filesystem",
482 /* Check if the stripe pattern is sane. */
483 rc = llapi_stripe_limit_check(param->lsp_stripe_size,
484 param->lsp_stripe_offset,
485 param->lsp_stripe_count,
486 param->lsp_stripe_pattern);
490 /* Make sure we have a good pool */
491 if (pool_name != NULL) {
492 /* in case user gives the full pool name <fsname>.<poolname>,
493 * strip the fsname */
494 char *ptr = strchr(pool_name, '.');
497 if (strcmp(pool_name, fsname) != 0) {
499 llapi_err_noerrno(LLAPI_MSG_ERROR,
500 "Pool '%s' is not on filesystem '%s'",
507 /* Make sure the pool exists and is non-empty */
508 rc = llapi_search_ost(fsname, pool_name, NULL);
510 char *err = rc == 0 ? "has no OSTs" : "does not exist";
512 llapi_err_noerrno(LLAPI_MSG_ERROR, "pool '%s.%s' %s",
513 fsname, pool_name, err);
517 lum_size = sizeof(struct lov_user_md_v3);
520 /* sanity check of target list */
521 if (param->lsp_is_specific) {
522 char ostname[MAX_OBD_NAME + 1];
526 for (i = 0; i < param->lsp_stripe_count; i++) {
527 snprintf(ostname, sizeof(ostname), "%s-OST%04x_UUID",
528 fsname, param->lsp_osts[i]);
529 rc = llapi_search_ost(fsname, pool_name, ostname);
534 llapi_error(LLAPI_MSG_ERROR, rc,
535 "%s: cannot find OST %s in %s",
542 /* Make sure stripe offset is in OST list. */
543 if (param->lsp_osts[i] == param->lsp_stripe_offset)
547 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
548 "%s: stripe offset '%d' is not in the "
550 __func__, param->lsp_stripe_offset);
554 lum_size = lov_user_md_size(param->lsp_stripe_count,
555 LOV_USER_MAGIC_SPECIFIC);
558 lum = calloc(1, lum_size);
563 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
565 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
566 flags = O_DIRECTORY | O_RDONLY;
573 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
578 /* Initialize IOCTL striping pattern structure */
579 lum->lmm_magic = LOV_USER_MAGIC_V1;
580 lum->lmm_pattern = param->lsp_stripe_pattern;
581 lum->lmm_stripe_size = param->lsp_stripe_size;
582 lum->lmm_stripe_count = param->lsp_stripe_count;
583 lum->lmm_stripe_offset = param->lsp_stripe_offset;
584 if (pool_name != NULL) {
585 struct lov_user_md_v3 *lumv3 = (void *)lum;
587 lumv3->lmm_magic = LOV_USER_MAGIC_V3;
588 strncpy(lumv3->lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
590 if (param->lsp_is_specific) {
591 struct lov_user_md_v3 *lumv3 = (void *)lum;
594 lumv3->lmm_magic = LOV_USER_MAGIC_SPECIFIC;
595 if (pool_name == NULL) {
596 /* LOV_USER_MAGIC_SPECIFIC uses v3 format plus specified
597 * OST list, therefore if pool is not specified we have
598 * to pack a null pool name for placeholder. */
599 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
602 for (i = 0; i < param->lsp_stripe_count; i++)
603 lumv3->lmm_objects[i].l_ost_idx = param->lsp_osts[i];
606 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lum) != 0) {
607 char *errmsg = "stripe already set";
610 if (errno != EEXIST && errno != EALREADY)
611 errmsg = strerror(errno);
613 llapi_err_noerrno(LLAPI_MSG_ERROR,
614 "error on ioctl "LPX64" for '%s' (%d): %s",
615 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,
627 int llapi_file_open_pool(const char *name, int flags, int mode,
628 unsigned long long stripe_size, int stripe_offset,
629 int stripe_count, int stripe_pattern, char *pool_name)
631 const struct llapi_stripe_param param = {
632 .lsp_stripe_size = stripe_size,
633 .lsp_stripe_count = stripe_count,
634 .lsp_stripe_pattern = stripe_pattern,
635 .lsp_stripe_offset = stripe_offset,
636 .lsp_pool = pool_name
638 return llapi_file_open_param(name, flags, mode, ¶m);
641 int llapi_file_open(const char *name, int flags, int mode,
642 unsigned long long stripe_size, int stripe_offset,
643 int stripe_count, int stripe_pattern)
645 return llapi_file_open_pool(name, flags, mode, stripe_size,
646 stripe_offset, stripe_count,
647 stripe_pattern, NULL);
650 int llapi_file_create(const char *name, unsigned long long stripe_size,
651 int stripe_offset, int stripe_count, int stripe_pattern)
655 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
656 stripe_offset, stripe_count, stripe_pattern,
665 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
666 int stripe_offset, int stripe_count,
667 int stripe_pattern, char *pool_name)
671 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
672 stripe_offset, stripe_count, stripe_pattern,
681 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
682 int stripe_count, int stripe_pattern,
683 const char *pool_name)
685 struct lmv_user_md lum = { 0 };
689 lum.lum_magic = LMV_USER_MAGIC;
690 lum.lum_stripe_offset = stripe_offset;
691 lum.lum_stripe_count = stripe_count;
692 lum.lum_hash_type = stripe_pattern;
693 if (pool_name != NULL) {
694 if (strlen(pool_name) >= sizeof(lum.lum_pool_name)) {
695 llapi_err_noerrno(LLAPI_MSG_ERROR,
696 "error LL_IOC_LMV_SET_DEFAULT_STRIPE '%s'"
697 ": too large pool name: %s", name, pool_name);
700 strncpy(lum.lum_pool_name, pool_name,
701 sizeof(lum.lum_pool_name));
704 fd = open(name, O_DIRECTORY | O_RDONLY);
707 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
711 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lum);
713 char *errmsg = "stripe already set";
715 if (errno != EEXIST && errno != EALREADY)
716 errmsg = strerror(errno);
718 llapi_err_noerrno(LLAPI_MSG_ERROR,
719 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
726 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
727 int stripe_count, int stripe_pattern,
728 const char *pool_name)
730 struct lmv_user_md lmu = { 0 };
731 struct obd_ioctl_data data = { 0 };
734 char *dirpath = NULL;
735 char *namepath = NULL;
741 dirpath = strdup(name);
742 namepath = strdup(name);
743 if (!dirpath || !namepath)
746 lmu.lum_magic = LMV_USER_MAGIC;
747 lmu.lum_stripe_offset = stripe_offset;
748 lmu.lum_stripe_count = stripe_count;
749 lmu.lum_hash_type = stripe_pattern;
750 if (pool_name != NULL) {
751 if (strlen(pool_name) > LOV_MAXPOOLNAME) {
752 llapi_err_noerrno(LLAPI_MSG_ERROR,
753 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
754 "pool name: %s", name, pool_name);
758 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
761 filename = basename(namepath);
762 dir = dirname(dirpath);
764 data.ioc_inlbuf1 = (char *)filename;
765 data.ioc_inllen1 = strlen(filename) + 1;
766 data.ioc_inlbuf2 = (char *)&lmu;
767 data.ioc_inllen2 = sizeof(struct lmv_user_md);
768 data.ioc_type = mode;
769 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
771 llapi_error(LLAPI_MSG_ERROR, rc,
772 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
777 fd = open(dir, O_DIRECTORY | O_RDONLY);
780 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
784 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
785 char *errmsg = "stripe already set";
787 if (errno != EEXIST && errno != EALREADY)
788 errmsg = strerror(errno);
790 llapi_err_noerrno(LLAPI_MSG_ERROR,
791 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
801 int llapi_direntry_remove(char *dname)
803 char *dirpath = NULL;
804 char *namepath = NULL;
810 dirpath = strdup(dname);
811 namepath = strdup(dname);
812 if (!dirpath || !namepath)
815 filename = basename(namepath);
817 dir = dirname(dirpath);
819 fd = open(dir, O_DIRECTORY | O_RDONLY);
822 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
827 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
828 char *errmsg = strerror(errno);
829 llapi_err_noerrno(LLAPI_MSG_ERROR,
830 "error on ioctl "LPX64" for '%s' (%d): %s",
831 (__u64)LL_IOC_LMV_SETSTRIPE, filename,
843 * Find the fsname, the full path, and/or an open fd.
844 * Either the fsname or path must not be NULL
846 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
849 char buf[PATH_MAX], mntdir[PATH_MAX];
852 int idx = 0, len = 0, mntlen, fd;
855 /* get the mount point */
856 fp = setmntent(MOUNTED, "r");
859 llapi_error(LLAPI_MSG_ERROR, rc,
860 "setmntent(%s) failed", MOUNTED);
864 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
867 if (!llapi_is_lustre_mnt(&mnt))
870 if ((want & WANT_INDEX) && (idx++ != index))
873 mntlen = strlen(mnt.mnt_dir);
874 ptr = strrchr(mnt.mnt_fsname, '/');
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 */
882 /* Check the fsname for a match, if given */
883 if (!(want & WANT_FSNAME) && fsname != NULL &&
884 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
887 /* If the path isn't set return the first one we find */
888 if (path == NULL || strlen(path) == 0) {
889 strcpy(mntdir, mnt.mnt_dir);
890 if ((want & WANT_FSNAME) && fsname != NULL)
894 /* Otherwise find the longest matching path */
895 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
896 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
897 strcpy(mntdir, mnt.mnt_dir);
899 if ((want & WANT_FSNAME) && fsname != NULL)
908 if ((want & WANT_PATH) && path != NULL)
909 strcpy(path, mntdir);
910 if (want & WANT_FD) {
911 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
914 llapi_error(LLAPI_MSG_ERROR, rc,
915 "error opening '%s'", mntdir);
921 } else if (want & WANT_ERROR)
922 llapi_err_noerrno(LLAPI_MSG_ERROR,
923 "can't find fs root for '%s': %d",
924 (want & WANT_PATH) ? fsname : path, rc);
929 * search lustre mounts
931 * Calling this function will return to the user the mount point, mntdir, and
932 * the file system name, fsname, if the user passed a buffer to this routine.
934 * The user inputs are pathname and index. If the pathname is supplied then
935 * the value of the index will be ignored. The pathname will return data if
936 * the pathname is located on a lustre mount. Index is used to pick which
937 * mount point you want in the case of multiple mounted lustre file systems.
938 * See function lfs_osts in lfs.c for an example of the index use.
940 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
943 int want = WANT_PATH, idx = -1;
945 if (!pathname || pathname[0] == '\0') {
949 strcpy(mntdir, pathname);
953 return get_root_path(want, fsname, NULL, mntdir, idx);
956 /* Given a path, find the corresponding Lustre fsname */
957 int llapi_search_fsname(const char *pathname, char *fsname)
962 path = realpath(pathname, NULL);
964 char buf[PATH_MAX], *ptr;
967 if (pathname[0] != '/') {
968 /* Need an absolute path, but realpath() only works for
969 * pathnames that actually exist. We go through the
970 * extra hurdle of dirname(getcwd() + pathname) in
971 * case the relative pathname contains ".." in it. */
972 if (getcwd(buf, sizeof(buf) - 2) == NULL)
974 rc = strlcat(buf, "/", sizeof(buf));
975 if (rc >= sizeof(buf))
978 rc = strlcat(buf, pathname, sizeof(buf));
979 if (rc >= sizeof(buf))
981 path = realpath(buf, NULL);
983 ptr = strrchr(buf, '/');
987 path = realpath(buf, NULL);
990 llapi_error(LLAPI_MSG_ERROR, rc,
991 "pathname '%s' cannot expand",
997 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1002 int llapi_search_rootpath(char *pathname, const char *fsname)
1004 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1007 int llapi_getname(const char *path, char *buf, size_t size)
1009 struct obd_uuid uuid_buf;
1010 char *uuid = uuid_buf.uuid;
1013 memset(&uuid_buf, 0, sizeof(uuid_buf));
1014 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1018 /* We want to turn lustre-clilov-ffff88002738bc00 into
1019 * lustre-ffff88002738bc00. */
1021 nr = snprintf(buf, size, "%.*s-%s",
1022 (int) (strlen(uuid) - 24), uuid,
1023 uuid + strlen(uuid) - 16);
1032 * Get the list of pool members.
1033 * \param poolname string of format \<fsname\>.\<poolname\>
1034 * \param members caller-allocated array of char*
1035 * \param list_size size of the members array
1036 * \param buffer caller-allocated buffer for storing OST names
1037 * \param buffer_size size of the buffer
1039 * \return number of members retrieved for this pool
1040 * \retval -error failure
1042 int llapi_get_poolmembers(const char *poolname, char **members,
1043 int list_size, char *buffer, int buffer_size)
1045 char fsname[PATH_MAX];
1054 /* name is FSNAME.POOLNAME */
1055 if (strlen(poolname) >= sizeof(fsname))
1057 strlcpy(fsname, poolname, sizeof(fsname));
1058 pool = strchr(fsname, '.');
1065 rc = poolpath(&pathname, fsname, NULL);
1067 llapi_error(LLAPI_MSG_ERROR, rc,
1068 "Lustre filesystem '%s' not found",
1073 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1074 rc = snprintf(buf, sizeof(buf), "%s/%s", pathname.gl_pathv[0], pool);
1075 cfs_free_param_data(&pathname);
1076 if (rc >= sizeof(buf))
1078 fd = fopen(buf, "r");
1081 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open %s", buf);
1086 while (fgets(buf, sizeof(buf), fd) != NULL) {
1087 if (nb_entries >= list_size) {
1091 buf[sizeof(buf) - 1] = '\0';
1093 tmp = strchr(buf, '\n');
1096 if (used + strlen(buf) + 1 > buffer_size) {
1101 strcpy(buffer + used, buf);
1102 members[nb_entries] = buffer + used;
1103 used += strlen(buf) + 1;
1113 * Get the list of pools in a filesystem.
1114 * \param name filesystem name or path
1115 * \param poollist caller-allocated array of char*
1116 * \param list_size size of the poollist array
1117 * \param buffer caller-allocated buffer for storing pool names
1118 * \param buffer_size size of the buffer
1120 * \return number of pools retrieved for this filesystem
1121 * \retval -error failure
1123 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1124 char *buffer, int buffer_size)
1126 char rname[PATH_MAX];
1132 struct dirent *cookie = NULL;
1134 unsigned int nb_entries = 0;
1135 unsigned int used = 0;
1138 /* initialize output array */
1139 for (i = 0; i < list_size; i++)
1142 /* is name a pathname ? */
1143 ptr = strchr(name, '/');
1145 /* only absolute pathname is supported */
1149 if (!realpath(name, rname)) {
1151 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1156 fsname = strdup(rname);
1160 rc = poolpath(&pathname, NULL, rname);
1162 /* name is FSNAME */
1163 fsname = strdup(name);
1166 rc = poolpath(&pathname, fsname, NULL);
1169 llapi_error(LLAPI_MSG_ERROR, rc,
1170 "Lustre filesystem '%s' not found", name);
1174 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1175 dir = opendir(pathname.gl_pathv[0]);
1178 llapi_error(LLAPI_MSG_ERROR, rc,
1179 "Could not open pool list for '%s'",
1185 rc = readdir_r(dir, &pool, &cookie);
1188 llapi_error(LLAPI_MSG_ERROR, rc,
1189 "Error reading pool list for '%s'", name);
1191 } else if ((rc == 0) && (cookie == NULL)) {
1192 /* end of directory */
1196 /* ignore . and .. */
1197 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1200 /* check output bounds */
1201 if (nb_entries >= list_size) {
1206 /* +2 for '.' and final '\0' */
1207 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1213 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1214 poollist[nb_entries] = buffer + used;
1215 used += strlen(pool.d_name) + strlen(fsname) + 2;
1222 cfs_free_param_data(&pathname);
1225 return rc != 0 ? rc : nb_entries;
1228 /* wrapper for lfs.c and obd.c */
1229 int llapi_poollist(const char *name)
1231 /* list of pool names (assume that pool count is smaller
1233 char **list, *buffer = NULL, *fsname = (char *)name;
1234 char *poolname = NULL, *tmp = NULL, data[16];
1235 enum param_filter type = FILTER_BY_PATH;
1236 int obdcount, bufsize, rc, nb, i;
1241 if (name[0] != '/') {
1242 fsname = strdup(name);
1246 poolname = strchr(fsname, '.');
1249 type = FILTER_BY_FS_NAME;
1252 rc = get_lustre_param_value("lov", fsname, type, "numobd",
1253 data, sizeof(data));
1256 obdcount = atoi(data);
1258 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1259 * and also an array to store the pointers for all that
1260 * allocated space. */
1262 bufsize = sizeof(struct obd_uuid) * obdcount;
1263 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1264 if (buffer == NULL) {
1268 list = (char **) (buffer + bufsize);
1271 /* name is a path or fsname */
1272 nb = llapi_get_poollist(name, list, obdcount,
1275 /* name is a pool name (<fsname>.<poolname>) */
1276 nb = llapi_get_poolmembers(name, list, obdcount,
1280 if (nb == -EOVERFLOW) {
1283 goto retry_get_pools;
1286 for (i = 0; i < nb; i++)
1287 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1288 rc = (nb < 0 ? nb : 0);
1292 if (fsname != NULL && type == FILTER_BY_FS_NAME)
1297 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1298 void *data, struct dirent64 *de);
1300 #define OBD_NOT_FOUND (-1)
1302 static int common_param_init(struct find_param *param, char *path)
1304 int lum_size = get_mds_md_size(path);
1306 if (lum_size < PATH_MAX + 1)
1307 lum_size = PATH_MAX + 1;
1309 param->fp_lum_size = lum_size;
1310 param->fp_lmd = calloc(1, sizeof(lstat_t) + param->fp_lum_size);
1311 if (param->fp_lmd == NULL) {
1312 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1313 "error: allocation of %zu bytes for ioctl",
1314 sizeof(lstat_t) + param->fp_lum_size);
1318 param->fp_lmv_stripe_count = 256;
1319 param->fp_lmv_md = calloc(1,
1320 lmv_user_md_size(param->fp_lmv_stripe_count,
1322 if (param->fp_lmv_md == NULL) {
1323 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1324 "error: allocation of %d bytes for ioctl",
1325 lmv_user_md_size(param->fp_lmv_stripe_count,
1330 param->fp_got_uuids = 0;
1331 param->fp_obd_indexes = NULL;
1332 param->fp_obd_index = OBD_NOT_FOUND;
1333 if (!param->fp_migrate)
1334 param->fp_mdt_index = OBD_NOT_FOUND;
1338 static void find_param_fini(struct find_param *param)
1340 if (param->fp_obd_indexes)
1341 free(param->fp_obd_indexes);
1344 free(param->fp_lmd);
1346 if (param->fp_lmv_md)
1347 free(param->fp_lmv_md);
1350 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1351 struct dirent64 *de)
1353 struct find_param *param = data;
1359 /* set errno upon failure */
1360 static DIR *opendir_parent(char *path)
1366 fname = strrchr(path, '/');
1368 return opendir(".");
1372 parent = opendir(path);
1377 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1382 param->fp_lmv_md->lum_stripe_count = param->fp_lmv_stripe_count;
1383 if (param->fp_get_default_lmv)
1384 param->fp_lmv_md->lum_magic = LMV_USER_MAGIC;
1386 param->fp_lmv_md->lum_magic = LMV_MAGIC_V1;
1388 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, param->fp_lmv_md);
1389 if (errno == E2BIG && ret != 0) {
1393 stripe_count = (__u32)param->fp_lmv_md->lum_stripe_count;
1394 if (stripe_count <= param->fp_lmv_stripe_count)
1397 free(param->fp_lmv_md);
1398 param->fp_lmv_stripe_count = stripe_count;
1399 lmv_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
1400 param->fp_lmv_md = malloc(lmv_size);
1401 if (param->fp_lmv_md == NULL) {
1402 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1403 "error: allocation of %d bytes for ioctl",
1404 lmv_user_md_size(param->fp_lmv_stripe_count,
1413 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1414 struct lov_user_mds_data *lmd, int lumlen)
1416 lstat_t *st = &lmd->lmd_st;
1419 if (parent == NULL && dir == NULL)
1423 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1424 } else if (parent) {
1425 char *fname = strrchr(path, '/');
1427 /* To avoid opening, locking, and closing each file on the
1428 * client if that is not needed. The GETFILEINFO ioctl can
1429 * be done on the patent dir with a single open for all
1430 * files in that directory, and it also doesn't pollute the
1431 * client dcache with millions of dentries when traversing
1432 * a large filesystem. */
1433 fname = (fname == NULL ? path : fname + 1);
1434 /* retrieve needed file info */
1435 strlcpy((char *)lmd, fname, lumlen);
1436 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1440 if (errno == ENOTTY) {
1441 /* ioctl is not supported, it is not a lustre fs.
1442 * Do the regular lstat(2) instead. */
1443 ret = lstat_f(path, st);
1446 llapi_error(LLAPI_MSG_ERROR, ret,
1447 "error: %s: lstat failed for %s",
1450 } else if (errno == ENOENT) {
1452 llapi_error(LLAPI_MSG_WARN, ret,
1453 "warning: %s: %s does not exist",
1455 } else if (errno != EISDIR) {
1457 llapi_error(LLAPI_MSG_ERROR, ret,
1458 "%s ioctl failed for %s.",
1459 dir ? "LL_IOC_MDC_GETINFO" :
1460 "IOC_MDC_GETFILEINFO", path);
1463 llapi_error(LLAPI_MSG_ERROR, ret,
1464 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1471 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1472 semantic_func_t sem_init,
1473 semantic_func_t sem_fini, void *data,
1474 struct dirent64 *de)
1476 struct find_param *param = (struct find_param *)data;
1477 struct dirent64 *dent;
1485 if (!d && errno != ENOTDIR) {
1487 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1490 } else if (!d && !parent) {
1491 /* ENOTDIR. Open the parent dir. */
1492 p = opendir_parent(path);
1499 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1505 while ((dent = readdir64(d)) != NULL) {
1508 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1511 /* Don't traverse .lustre directory */
1512 if (!(strcmp(dent->d_name, dot_lustre_name)))
1516 if ((len + dent->d_reclen + 2) > size) {
1517 llapi_err_noerrno(LLAPI_MSG_ERROR,
1518 "error: %s: string buffer is too small",
1523 strcat(path, dent->d_name);
1525 if (dent->d_type == DT_UNKNOWN) {
1526 lstat_t *st = ¶m->fp_lmd->lmd_st;
1528 rc = get_lmd_info(path, d, NULL, param->fp_lmd,
1529 param->fp_lum_size);
1531 dent->d_type = IFTODT(st->st_mode);
1538 switch (dent->d_type) {
1540 llapi_err_noerrno(LLAPI_MSG_ERROR,
1541 "error: %s: '%s' is UNKNOWN type %d",
1542 __func__, dent->d_name, dent->d_type);
1545 rc = llapi_semantic_traverse(path, size, d, sem_init,
1546 sem_fini, data, dent);
1547 if (rc != 0 && ret == 0)
1553 rc = sem_init(path, d, NULL, data, dent);
1554 if (rc < 0 && ret == 0)
1557 if (sem_fini && rc == 0)
1558 sem_fini(path, d, NULL, data, dent);
1566 sem_fini(path, parent, &d, data, de);
1575 static int param_callback(char *path, semantic_func_t sem_init,
1576 semantic_func_t sem_fini, struct find_param *param)
1578 int ret, len = strlen(path);
1581 if (len > PATH_MAX) {
1583 llapi_error(LLAPI_MSG_ERROR, ret,
1584 "Path name '%s' is too long", path);
1588 buf = (char *)malloc(PATH_MAX + 1);
1592 strlcpy(buf, path, PATH_MAX + 1);
1593 ret = common_param_init(param, buf);
1597 param->fp_depth = 0;
1599 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1600 sem_fini, param, NULL);
1602 find_param_fini(param);
1604 return ret < 0 ? ret : 0;
1607 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1609 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1612 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1617 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1619 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1622 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1627 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1631 fd = open(path, O_RDONLY);
1634 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1638 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1644 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1648 fd = open(path, O_RDONLY);
1651 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1655 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1667 * If uuidp is NULL, return the number of available obd uuids.
1668 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1669 * there are more OSTs than allocated to uuidp, then an error is returned with
1670 * the ost_count set to number of available obd uuids.
1672 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1673 int *ost_count, enum tgt_type type)
1675 char buf[PATH_MAX], format[32];
1676 int rc = 0, index = 0;
1677 struct obd_uuid name;
1681 /* Get the lov name */
1682 if (type == LOV_TYPE)
1683 rc = llapi_file_fget_lov_uuid(fd, &name);
1685 rc = llapi_file_fget_lmv_uuid(fd, &name);
1689 /* Now get the ost uuids */
1690 rc = get_lustre_param_path(type == LOV_TYPE ? "lov" : "lmv", name.uuid,
1691 FILTER_BY_EXACT, "target_obd", ¶m);
1695 fp = fopen(param.gl_pathv[0], "r");
1698 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
1703 snprintf(format, sizeof(format),
1704 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
1705 while (fgets(buf, sizeof(buf), fp) != NULL) {
1706 if (uuidp && (index < *ost_count)) {
1707 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
1715 if (uuidp && (index > *ost_count))
1720 cfs_free_param_data(¶m);
1724 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1726 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1729 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1734 root = opendir(mnt);
1737 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1742 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1750 /* Check if user specified value matches a real uuid. Ignore _UUID,
1751 * -osc-4ba41334, other trailing gunk in comparison.
1752 * @param real_uuid ends in "_UUID"
1753 * @param search_uuid may or may not end in "_UUID"
1755 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1757 int cmplen = strlen(real_uuid);
1758 int searchlen = strlen(search_uuid);
1760 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1762 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1765 /* The UUIDs may legitimately be different lengths, if
1766 * the system was upgraded from an older version. */
1767 if (cmplen != searchlen)
1770 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1773 /* Here, param->fp_obd_uuid points to a single obduuid, the index of which is
1774 * returned in param->fp_obd_index */
1775 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1777 struct obd_uuid obd_uuid;
1784 if (param->fp_got_uuids)
1787 /* Get the lov/lmv name */
1788 if (param->fp_get_lmv)
1789 rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
1791 rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
1793 if (rc != -ENOTTY) {
1794 llapi_error(LLAPI_MSG_ERROR, rc,
1795 "error: can't get %s name: %s",
1796 param->fp_get_lmv ? "lmv" : "lov",
1804 param->fp_got_uuids = 1;
1806 /* Now get the ost uuids */
1807 rc = get_lustre_param_path(param->fp_get_lmv ? "lmv" : "lov",
1808 obd_uuid.uuid, FILTER_BY_EXACT,
1809 "target_obd", ¶m_data);
1813 fp = fopen(param_data.gl_pathv[0], "r");
1816 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
1817 param_data.gl_pathv[0]);
1821 if (!param->fp_obd_uuid && !param->fp_quiet && !param->fp_obds_printed)
1822 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1823 param->fp_get_lmv ? "MDTS" : "OBDS");
1825 snprintf(format, sizeof(format),
1826 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
1827 while (fgets(buf, sizeof(buf), fp) != NULL) {
1830 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
1833 if (param->fp_obd_uuid) {
1834 if (llapi_uuid_match(obd_uuid.uuid,
1835 param->fp_obd_uuid->uuid)) {
1836 param->fp_obd_index = index;
1839 } else if (!param->fp_quiet && !param->fp_obds_printed) {
1840 /* Print everything */
1841 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1844 param->fp_obds_printed = 1;
1848 if (param->fp_obd_uuid && (param->fp_obd_index == OBD_NOT_FOUND)) {
1849 llapi_err_noerrno(LLAPI_MSG_ERROR,
1850 "error: %s: unknown obduuid: %s",
1851 __func__, param->fp_obd_uuid->uuid);
1855 cfs_free_param_data(¶m_data);
1859 /* In this case, param->fp_obd_uuid will be an array of obduuids and
1860 * obd index for all these obduuids will be returned in
1861 * param->fp_obd_indexes */
1862 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1863 int num_obds, int **obdindexes, int *obdindex,
1866 int ret, obdcount, obd_valid = 0, obdnum;
1868 struct obd_uuid *uuids = NULL;
1872 if (type == LOV_TYPE)
1873 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
1875 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
1879 obdcount = atoi(buf);
1880 uuids = malloc(obdcount * sizeof(struct obd_uuid));
1885 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1887 if (ret == -EOVERFLOW) {
1888 struct obd_uuid *uuids_temp;
1890 uuids_temp = realloc(uuids, obdcount *
1891 sizeof(struct obd_uuid));
1892 if (uuids_temp != NULL) {
1895 goto retry_get_uuids;
1900 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
1904 indexes = malloc(num_obds * sizeof(*obdindex));
1905 if (indexes == NULL) {
1910 for (obdnum = 0; obdnum < num_obds; obdnum++) {
1913 /* The user may have specified a simple index */
1914 i = strtol(obduuids[obdnum].uuid, &end, 0);
1915 if (end && *end == '\0' && i < obdcount) {
1916 indexes[obdnum] = i;
1919 for (i = 0; i < obdcount; i++) {
1920 if (llapi_uuid_match(uuids[i].uuid,
1921 obduuids[obdnum].uuid)) {
1922 indexes[obdnum] = i;
1928 if (i >= obdcount) {
1929 indexes[obdnum] = OBD_NOT_FOUND;
1930 llapi_err_noerrno(LLAPI_MSG_ERROR,
1931 "error: %s: unknown obduuid: %s",
1932 __func__, obduuids[obdnum].uuid);
1938 *obdindex = OBD_NOT_FOUND;
1940 *obdindex = obd_valid;
1942 *obdindexes = indexes;
1950 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
1954 if (param->fp_mdt_uuid) {
1955 ret = setup_indexes(dir, path, param->fp_mdt_uuid,
1957 ¶m->fp_mdt_indexes,
1958 ¶m->fp_mdt_index, LMV_TYPE);
1963 if (param->fp_obd_uuid) {
1964 ret = setup_indexes(dir, path, param->fp_obd_uuid,
1966 ¶m->fp_obd_indexes,
1967 ¶m->fp_obd_index, LOV_TYPE);
1972 param->fp_got_uuids = 1;
1977 int llapi_ostlist(char *path, struct find_param *param)
1982 dir = opendir(path);
1986 ret = setup_obd_uuid(dir, path, param);
1993 * Tries to determine the default stripe attributes for a given filesystem. The
1994 * filesystem to check should be specified by fsname, or will be determined
1997 static int sattr_get_defaults(const char *const fsname,
1998 unsigned int *scount,
1999 unsigned int *ssize,
2000 unsigned int *soffset)
2006 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2007 "stripecount", val, sizeof(val));
2010 *scount = atoi(val);
2014 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2015 "stripesize", val, sizeof(val));
2022 rc = get_lustre_param_value("lov", fsname, FILTER_BY_FS_NAME,
2023 "stripeoffset", val, sizeof(val));
2026 *soffset = atoi(val);
2033 * Tries to gather the default stripe attributes for a given filesystem. If
2034 * the attributes can be determined, they are cached for easy retreival the
2035 * next time they are needed. Only a single filesystem's attributes are
2038 static int sattr_cache_get_defaults(const char *const fsname,
2039 const char *const pathname,
2040 unsigned int *scount,
2041 unsigned int *ssize,
2042 unsigned int *soffset)
2045 char fsname[PATH_MAX + 1];
2046 unsigned int stripecount;
2047 unsigned int stripesize;
2048 unsigned int stripeoffset;
2054 char fsname_buf[PATH_MAX + 1];
2055 unsigned int tmp[3];
2057 if (fsname == NULL) {
2058 rc = llapi_search_fsname(pathname, fsname_buf);
2062 strlcpy(fsname_buf, fsname, sizeof(fsname_buf));
2065 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2067 * Ensure all 3 sattrs (count, size, and offset) are
2068 * successfully retrieved and stored in tmp before writing to
2071 rc = sattr_get_defaults(fsname_buf, &tmp[0], &tmp[1], &tmp[2]);
2075 cache.stripecount = tmp[0];
2076 cache.stripesize = tmp[1];
2077 cache.stripeoffset = tmp[2];
2078 strlcpy(cache.fsname, fsname_buf, sizeof(cache.fsname));
2082 *scount = cache.stripecount;
2084 *ssize = cache.stripesize;
2086 *soffset = cache.stripeoffset;
2091 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2092 struct lov_user_ost_data_v1 *objects,
2093 int is_dir, int verbose, int depth,
2094 int raw, char *pool_name)
2096 char *prefix = is_dir ? "" : "lmm_";
2097 char *separator = "";
2100 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2101 lmm_oi_set_seq(&lum->lmm_oi, 0);
2102 if (verbose & VERBOSE_DETAIL)
2103 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
2106 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2107 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2109 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2110 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
2112 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
2113 lmm_oi_seq(&lum->lmm_oi));
2114 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
2115 lmm_oi_id(&lum->lmm_oi));
2118 if (verbose & VERBOSE_COUNT) {
2119 if (verbose & ~VERBOSE_COUNT)
2120 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
2123 if (!raw && lum->lmm_stripe_count == 0) {
2124 unsigned int scount;
2125 rc = sattr_cache_get_defaults(NULL, path,
2129 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2132 llapi_error(LLAPI_MSG_ERROR, rc,
2133 "Cannot determine default"
2136 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2137 lum->lmm_stripe_count ==
2138 (typeof(lum->lmm_stripe_count))(-1)
2139 ? -1 : lum->lmm_stripe_count);
2142 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2143 (__s16)lum->lmm_stripe_count);
2145 separator = is_dir ? " " : "\n";
2148 if (verbose & VERBOSE_SIZE) {
2149 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2150 if (verbose & ~VERBOSE_SIZE)
2151 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
2153 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
2155 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2158 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2160 llapi_error(LLAPI_MSG_ERROR, rc,
2161 "Cannot determine default"
2164 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2165 lum->lmm_stripe_size);
2167 separator = is_dir ? " " : "\n";
2170 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2171 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2172 if (verbose & ~VERBOSE_LAYOUT)
2173 llapi_printf(LLAPI_MSG_NORMAL, "%spattern: ",
2175 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2179 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2180 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2181 if (verbose & ~VERBOSE_GENERATION)
2182 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
2184 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2185 (int)lum->lmm_layout_gen);
2189 if (verbose & VERBOSE_OFFSET) {
2190 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2191 if (verbose & ~VERBOSE_OFFSET)
2192 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
2195 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2196 lum->lmm_stripe_offset ==
2197 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2198 lum->lmm_stripe_offset);
2200 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2201 objects[0].l_ost_idx);
2202 separator = is_dir ? " " : "\n";
2205 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
2206 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2207 if (verbose & ~VERBOSE_POOL)
2208 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
2210 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2213 if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
2214 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2217 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2218 struct lov_user_ost_data_v1 *objects,
2219 char *path, int is_dir, int obdindex,
2220 int depth, int header, int raw)
2222 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2225 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2226 if (obdindex == objects[i].l_ost_idx) {
2234 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
2235 depth, raw, pool_name);
2237 if (!is_dir && (header & VERBOSE_OBJID) &&
2238 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2240 llapi_printf(LLAPI_MSG_NORMAL,
2241 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2243 for (i = 0; i < lum->lmm_stripe_count; i++) {
2244 int idx = objects[i].l_ost_idx;
2245 long long oid = ostid_id(&objects[i].l_ost_oi);
2246 long long gr = ostid_seq(&objects[i].l_ost_oi);
2247 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) {
2249 sprintf(fmt, "%s%s%s\n",
2250 "\t%6u\t%14llu\t%#13llx\t",
2251 (fid_seq_is_rsvd(gr) ||
2252 fid_seq_is_mdt0(gr)) ?
2253 "%14llu" : "%#14llx", "%s");
2254 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2256 obdindex == idx ? " *" : "");
2260 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2264 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2265 char *path, int obdindex, int depth, int verbose)
2267 struct lmv_user_mds_data *objects = lum->lum_objects;
2268 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2269 int i, obdstripe = 0;
2270 char *separator = "";
2272 if (obdindex != OBD_NOT_FOUND) {
2273 if (lum->lum_stripe_count == 0) {
2274 if (obdindex == lum->lum_stripe_offset)
2277 for (i = 0; i < lum->lum_stripe_count; i++) {
2278 if (obdindex == objects[i].lum_mds) {
2279 llapi_printf(LLAPI_MSG_NORMAL,
2294 /* show all information default */
2296 if (lum->lum_magic == LMV_USER_MAGIC)
2297 verbose = VERBOSE_POOL | VERBOSE_COUNT | VERBOSE_OFFSET;
2299 verbose = VERBOSE_OBJID;
2302 if (depth && path && ((verbose != VERBOSE_OBJID)))
2303 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2305 if (verbose & VERBOSE_COUNT) {
2306 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2307 if (verbose & ~VERBOSE_COUNT)
2308 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2309 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2310 (int)lum->lum_stripe_count);
2311 if (verbose & VERBOSE_OFFSET)
2317 if (verbose & VERBOSE_OFFSET) {
2318 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2319 if (verbose & ~VERBOSE_OFFSET)
2320 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2321 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2322 (int)lum->lum_stripe_offset);
2326 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2327 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2328 if (obdstripe == 1 && lum->lum_stripe_count > 0)
2329 llapi_printf(LLAPI_MSG_NORMAL,
2330 "mdtidx\t\t FID[seq:oid:ver]\n");
2331 for (i = 0; i < lum->lum_stripe_count; i++) {
2332 int idx = objects[i].lum_mds;
2333 struct lu_fid *fid = &objects[i].lum_fid;
2334 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2335 llapi_printf(LLAPI_MSG_NORMAL,
2336 "%6u\t\t "DFID"\t\t%s\n",
2338 obdindex == idx ? " *" : "");
2343 if ((verbose & VERBOSE_POOL) && pool_name != NULL &&
2344 pool_name[0] != '\0') {
2345 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2346 if (verbose & ~VERBOSE_POOL)
2347 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2349 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2353 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
2354 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2357 void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
2361 if (param->fp_get_lmv || param->fp_get_default_lmv)
2362 magic = (__u32)param->fp_lmv_md->lum_magic;
2364 magic = *(__u32 *)¶m->fp_lmd->lmd_lmm; /* lum->lmm_magic */
2367 case LOV_USER_MAGIC_V1:
2368 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm, NULL,
2369 param->fp_lmd->lmd_lmm.lmm_objects,
2371 param->fp_obd_index, param->fp_max_depth,
2372 param->fp_verbose, param->fp_raw);
2374 case LOV_USER_MAGIC_V3: {
2375 char pool_name[LOV_MAXPOOLNAME + 1];
2376 struct lov_user_ost_data_v1 *objects;
2377 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2379 strlcpy(pool_name, lmmv3->lmm_pool_name, sizeof(pool_name));
2380 objects = lmmv3->lmm_objects;
2381 lov_dump_user_lmm_v1v3(¶m->fp_lmd->lmd_lmm,
2382 pool_name[0] == '\0' ? NULL : pool_name,
2383 objects, path, is_dir,
2384 param->fp_obd_index, param->fp_max_depth,
2385 param->fp_verbose, param->fp_raw);
2389 case LMV_USER_MAGIC: {
2390 char pool_name[LOV_MAXPOOLNAME + 1];
2391 struct lmv_user_md *lum;
2393 lum = (struct lmv_user_md *)param->fp_lmv_md;
2394 strlcpy(pool_name, lum->lum_pool_name, sizeof(pool_name));
2395 lmv_dump_user_lmm(lum,
2396 pool_name[0] == '\0' ? NULL : pool_name,
2397 path, param->fp_obd_index,
2398 param->fp_max_depth, param->fp_verbose);
2402 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2403 "(expecting one of %#x %#x %#x %#x)\n",
2404 *(__u32 *)¶m->fp_lmd->lmd_lmm,
2405 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2406 LMV_USER_MAGIC, LMV_MAGIC_V1);
2411 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2417 fname = strrchr(path, '/');
2419 /* It should be a file (or other non-directory) */
2420 if (fname == NULL) {
2421 dname = (char *)malloc(2);
2425 fname = (char *)path;
2427 dname = (char *)malloc(fname - path + 1);
2430 strncpy(dname, path, fname - path);
2431 dname[fname - path] = '\0';
2435 fd = open(dname, O_RDONLY);
2442 strcpy((char *)lum, fname);
2443 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2446 if (close(fd) == -1 && rc == 0)
2453 int llapi_file_lookup(int dirfd, const char *name)
2455 struct obd_ioctl_data data = { 0 };
2460 if (dirfd < 0 || name == NULL)
2463 data.ioc_version = OBD_IOCTL_VERSION;
2464 data.ioc_len = sizeof(data);
2465 data.ioc_inlbuf1 = (char *)name;
2466 data.ioc_inllen1 = strlen(name) + 1;
2468 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2470 llapi_error(LLAPI_MSG_ERROR, rc,
2471 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2476 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2482 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2483 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2485 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2486 * The table below gives the answers for the specified parameters (value and
2487 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2488 * --------------------------------------
2489 * 1 | file > limit; sign > 0 | -1 / -1 |
2490 * 2 | file = limit; sign > 0 | -1 / -1 |
2491 * 3 | file < limit; sign > 0 | ? / 1 |
2492 * 4 | file > limit; sign = 0 | -1 / -1 |
2493 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2494 * 6 | file < limit; sign = 0 | ? / -1 |
2495 * 7 | file > limit; sign < 0 | 1 / 1 |
2496 * 8 | file = limit; sign < 0 | ? / -1 |
2497 * 9 | file < limit; sign < 0 | ? / -1 |
2498 * --------------------------------------
2499 * Note: 5th actually means that the value is within the interval
2500 * (limit - margin, limit]. */
2501 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2502 int sign, int negopt, unsigned long long margin,
2508 /* Drop the fraction of margin (of days). */
2509 if (file + margin <= limit)
2511 } else if (sign == 0) {
2512 if (file <= limit && file + margin > limit)
2514 else if (file + margin <= limit)
2516 } else if (sign < 0) {
2523 return negopt ? ~ret + 1 : ret;
2526 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2527 * Return -1 or 1 if file timestamp does not or does match the given criteria
2528 * correspondingly. Return 0 if the MDS time is being checked and there are
2529 * attributes on OSTs and it is not yet clear if the timespamp matches.
2531 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2532 * updated timestamps. */
2533 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2538 /* Check if file is accepted. */
2539 if (param->fp_atime) {
2540 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
2541 param->fp_asign, param->fp_exclude_atime,
2548 if (param->fp_mtime) {
2549 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
2550 param->fp_msign, param->fp_exclude_mtime,
2555 /* If the previous check matches, but this one is not yet clear,
2556 * we should return 0 to do an RPC on OSTs. */
2561 if (param->fp_ctime) {
2562 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
2563 param->fp_csign, param->fp_exclude_ctime,
2568 /* If the previous check matches, but this one is not yet clear,
2569 * we should return 0 to do an RPC on OSTs. */
2578 * Check whether the stripes matches the indexes user provided
2582 static int check_obd_match(struct find_param *param)
2584 lstat_t *st = ¶m->fp_lmd->lmd_st;
2585 struct lov_user_ost_data_v1 *lmm_objects;
2588 if (param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND)
2591 if (!S_ISREG(st->st_mode))
2594 /* Only those files should be accepted, which have a
2595 * stripe on the specified OST. */
2596 if (!param->fp_lmd->lmd_lmm.lmm_stripe_count)
2599 if (param->fp_lmd->lmd_lmm.lmm_magic ==
2600 LOV_USER_MAGIC_V3) {
2601 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2603 lmm_objects = lmmv3->lmm_objects;
2604 } else if (param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2605 lmm_objects = param->fp_lmd->lmd_lmm.lmm_objects;
2607 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2608 __func__, param->fp_lmd->lmd_lmm.lmm_magic);
2612 for (i = 0; i < param->fp_lmd->lmd_lmm.lmm_stripe_count; i++) {
2613 for (j = 0; j < param->fp_num_obds; j++) {
2614 if (param->fp_obd_indexes[j] ==
2615 lmm_objects[i].l_ost_idx) {
2616 if (param->fp_exclude_obd)
2623 if (param->fp_exclude_obd)
2629 static int check_mdt_match(struct find_param *param)
2633 if (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND)
2636 /* FIXME: For striped dir, we should get stripe information and check */
2637 for (i = 0; i < param->fp_num_mdts; i++) {
2638 if (param->fp_mdt_indexes[i] == param->fp_file_mdt_index)
2639 return !param->fp_exclude_mdt;
2642 if (param->fp_exclude_mdt)
2649 * Check whether the obd is active or not, if it is
2650 * not active, just print the object affected by this
2653 static int print_failed_tgt(struct find_param *param, char *path, int type)
2655 struct obd_statfs stat_buf;
2656 struct obd_uuid uuid_buf;
2659 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2661 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2662 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2663 ret = llapi_obd_statfs(path, type,
2664 param->fp_obd_index, &stat_buf,
2667 llapi_printf(LLAPI_MSG_NORMAL,
2668 "obd_uuid: %s failed %s ",
2669 param->fp_obd_uuid->uuid,
2676 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
2677 void *data, struct dirent64 *de)
2679 struct find_param *param = (struct find_param *)data;
2680 DIR *dir = dirp == NULL ? NULL : *dirp;
2681 int decision = 1; /* 1 is accepted; -1 is rejected. */
2682 lstat_t *st = ¶m->fp_lmd->lmd_st;
2684 int checked_type = 0;
2687 LASSERT(parent != NULL || dir != NULL);
2689 param->fp_lmd->lmd_lmm.lmm_stripe_count = 0;
2691 /* If a regular expression is presented, make the initial decision */
2692 if (param->fp_pattern != NULL) {
2693 char *fname = strrchr(path, '/');
2694 fname = (fname == NULL ? path : fname + 1);
2695 ret = fnmatch(param->fp_pattern, fname, 0);
2696 if ((ret == FNM_NOMATCH && !param->fp_exclude_pattern) ||
2697 (ret == 0 && param->fp_exclude_pattern))
2701 /* See if we can check the file type from the dirent. */
2702 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
2705 if (DTTOIF(de->d_type) == param->fp_type) {
2706 if (param->fp_exclude_type)
2709 if (!param->fp_exclude_type)
2716 /* Request MDS for the stat info if some of these parameters need
2717 * to be compared. */
2718 if (param->fp_obd_uuid || param->fp_mdt_uuid ||
2719 param->fp_check_uid || param->fp_check_gid ||
2720 param->fp_atime || param->fp_mtime || param->fp_ctime ||
2721 param->fp_check_pool || param->fp_check_size ||
2722 param->fp_check_stripe_count || param->fp_check_stripe_size ||
2723 param->fp_check_layout)
2726 if (param->fp_type != 0 && checked_type == 0)
2729 if (decision == 0) {
2730 ret = get_lmd_info(path, parent, dir, param->fp_lmd,
2731 param->fp_lum_size);
2732 if (ret == 0 && param->fp_lmd->lmd_lmm.lmm_magic == 0 &&
2733 (param->fp_check_pool || param->fp_check_stripe_count ||
2734 param->fp_check_stripe_size || param->fp_check_layout)) {
2735 struct lov_user_md *lmm = ¶m->fp_lmd->lmd_lmm;
2737 /* We need to "fake" the "use the default" values
2738 * since the lmm struct is zeroed out at this point. */
2739 lmm->lmm_magic = LOV_USER_MAGIC_V1;
2740 lmm->lmm_pattern = 0xFFFFFFFF;
2742 ostid_set_seq(&lmm->lmm_oi,
2743 FID_SEQ_LOV_DEFAULT);
2744 lmm->lmm_stripe_size = 0;
2745 lmm->lmm_stripe_count = 0;
2746 lmm->lmm_stripe_offset = -1;
2748 if (ret == 0 && param->fp_mdt_uuid != NULL) {
2750 ret = llapi_file_fget_mdtidx(dirfd(dir),
2751 ¶m->fp_file_mdt_index);
2752 } else if (S_ISREG(st->st_mode)) {
2755 /* FIXME: we could get the MDT index from the
2756 * file's FID in lmd->lmd_lmm.lmm_oi without
2757 * opening the file, once we are sure that
2758 * LFSCK2 (2.6) has fixed up pre-2.0 LOV EAs.
2759 * That would still be an ioctl() to map the
2760 * FID to the MDT, but not an open RPC. */
2761 fd = open(path, O_RDONLY);
2763 ret = llapi_file_fget_mdtidx(fd,
2764 ¶m->fp_file_mdt_index);
2770 /* For a special file, we assume it resides on
2771 * the same MDT as the parent directory. */
2772 ret = llapi_file_fget_mdtidx(dirfd(parent),
2773 ¶m->fp_file_mdt_index);
2786 if (param->fp_type && !checked_type) {
2787 if ((st->st_mode & S_IFMT) == param->fp_type) {
2788 if (param->fp_exclude_type)
2791 if (!param->fp_exclude_type)
2797 if (param->fp_obd_uuid || param->fp_mdt_uuid) {
2798 if (lustre_fs && param->fp_got_uuids &&
2799 param->fp_dev != st->st_dev) {
2800 /* A lustre/lustre mount point is crossed. */
2801 param->fp_got_uuids = 0;
2802 param->fp_obds_printed = 0;
2803 param->fp_mdt_index = OBD_NOT_FOUND;
2804 param->fp_obd_index = OBD_NOT_FOUND;
2807 if (lustre_fs && !param->fp_got_uuids) {
2808 ret = setup_target_indexes(dir ? dir : parent, path,
2813 param->fp_dev = st->st_dev;
2814 } else if (!lustre_fs && param->fp_got_uuids) {
2815 /* A lustre/non-lustre mount point is crossed. */
2816 param->fp_got_uuids = 0;
2817 param->fp_mdt_index = OBD_NOT_FOUND;
2818 param->fp_obd_index = OBD_NOT_FOUND;
2822 if (param->fp_check_stripe_size) {
2823 decision = find_value_cmp(
2824 param->fp_lmd->lmd_lmm.lmm_stripe_size,
2825 param->fp_stripe_size,
2826 param->fp_stripe_size_sign,
2827 param->fp_exclude_stripe_size,
2828 param->fp_stripe_size_units, 0);
2833 if (param->fp_check_stripe_count) {
2834 decision = find_value_cmp(
2835 param->fp_lmd->lmd_lmm.lmm_stripe_count,
2836 param->fp_stripe_count,
2837 param->fp_stripe_count_sign,
2838 param->fp_exclude_stripe_count, 1, 0);
2843 if (param->fp_check_layout) {
2846 found = (param->fp_lmd->lmd_lmm.lmm_pattern & param->fp_layout);
2847 if ((param->fp_lmd->lmd_lmm.lmm_pattern == 0xFFFFFFFF) ||
2848 (found && param->fp_exclude_layout) ||
2849 (!found && !param->fp_exclude_layout)) {
2855 /* If an OBD UUID is specified but none matches, skip this file. */
2856 if ((param->fp_obd_uuid && param->fp_obd_index == OBD_NOT_FOUND) ||
2857 (param->fp_mdt_uuid && param->fp_mdt_index == OBD_NOT_FOUND))
2860 /* If an OST or MDT UUID is given, and some OST matches,
2862 if (param->fp_obd_index != OBD_NOT_FOUND ||
2863 param->fp_mdt_index != OBD_NOT_FOUND) {
2864 if (param->fp_obd_uuid) {
2865 if (check_obd_match(param)) {
2866 /* If no mdtuuid is given, we are done.
2867 * Otherwise, fall through to the mdtuuid
2869 if (!param->fp_mdt_uuid)
2876 if (param->fp_mdt_uuid) {
2877 if (check_mdt_match(param))
2884 if (param->fp_check_uid) {
2885 if (st->st_uid == param->fp_uid) {
2886 if (param->fp_exclude_uid)
2889 if (!param->fp_exclude_uid)
2894 if (param->fp_check_gid) {
2895 if (st->st_gid == param->fp_gid) {
2896 if (param->fp_exclude_gid)
2899 if (!param->fp_exclude_gid)
2904 if (param->fp_check_pool) {
2905 struct lov_user_md_v3 *lmmv3 = (void *)¶m->fp_lmd->lmd_lmm;
2907 /* empty requested pool is taken as no pool search => V1 */
2908 if (((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
2909 (param->fp_poolname[0] == '\0')) ||
2910 ((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2911 (strncmp(lmmv3->lmm_pool_name,
2912 param->fp_poolname, LOV_MAXPOOLNAME) == 0)) ||
2913 ((param->fp_lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2914 (strcmp(param->fp_poolname, "*") == 0))) {
2915 if (param->fp_exclude_pool)
2918 if (!param->fp_exclude_pool)
2923 /* Check the time on mds. */
2925 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
2928 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
2929 param->fp_lmd->lmd_lmm.lmm_stripe_count)
2931 decision = find_time_check(st, param, for_mds);
2936 /* If file still fits the request, ask ost for updated info.
2937 The regular stat is almost of the same speed as some new
2938 'glimpse-size-ioctl'. */
2940 if (param->fp_check_size && S_ISREG(st->st_mode) &&
2941 param->fp_lmd->lmd_lmm.lmm_stripe_count)
2944 if (param->fp_check_size && S_ISDIR(st->st_mode))
2948 /* For regular files with the stripe the decision may have not
2949 * been taken yet if *time or size is to be checked. */
2950 if (param->fp_obd_index != OBD_NOT_FOUND)
2951 print_failed_tgt(param, path, LL_STATFS_LOV);
2953 if (param->fp_mdt_index != OBD_NOT_FOUND)
2954 print_failed_tgt(param, path, LL_STATFS_LMV);
2957 ret = fstat_f(dirfd(dir), st);
2958 else if (de != NULL)
2959 ret = fstatat_f(dirfd(parent), de->d_name, st,
2960 AT_SYMLINK_NOFOLLOW);
2962 ret = lstat_f(path, st);
2965 if (errno == ENOENT) {
2966 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
2967 "warning: %s: %s does not exist",
2972 llapi_error(LLAPI_MSG_ERROR, ret,
2973 "%s: IOC_LOV_GETINFO on %s failed",
2979 /* Check the time on osc. */
2980 decision = find_time_check(st, param, 0);
2985 if (param->fp_check_size)
2986 decision = find_value_cmp(st->st_size, param->fp_size,
2987 param->fp_size_sign,
2988 param->fp_exclude_size,
2989 param->fp_size_units, 0);
2991 if (decision != -1) {
2992 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
2993 if (param->fp_zero_end)
2994 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
2996 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3000 /* Do not get down anymore? */
3001 if (param->fp_depth == param->fp_max_depth)
3009 static int cb_migrate_mdt_init(char *path, DIR *parent, DIR **dirp,
3010 void *param_data, struct dirent64 *de)
3012 struct find_param *param = (struct find_param *)param_data;
3014 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3016 struct obd_ioctl_data data = { 0 };
3021 LASSERT(parent != NULL || dirp != NULL);
3025 if (parent == NULL) {
3026 dir = opendir_parent(path);
3030 llapi_error(LLAPI_MSG_ERROR, ret,
3031 "can not open %s", path);
3038 filename = basename(path);
3039 data.ioc_inlbuf1 = (char *)filename;
3040 data.ioc_inllen1 = strlen(filename) + 1;
3041 data.ioc_inlbuf2 = (char *)¶m->fp_mdt_index;
3042 data.ioc_inllen2 = sizeof(param->fp_mdt_index);
3043 ret = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3045 llapi_error(LLAPI_MSG_ERROR, ret,
3046 "llapi_obd_statfs: error packing ioctl data");
3050 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
3053 fprintf(stderr, "%s migrate failed: %s (%d)\n",
3054 path, strerror(-ret), ret);
3056 } else if (param->fp_verbose & VERBOSE_DETAIL) {
3057 fprintf(stdout, "migrate %s to MDT%d\n",
3058 path, param->fp_mdt_index);
3063 /* If the directory is being migration, we need
3064 * close the directory after migration,
3065 * so the old directory cache will be cleanup
3066 * on the client side, and re-open to get the
3067 * new directory handle */
3068 *dirp = opendir(path);
3071 llapi_error(LLAPI_MSG_ERROR, ret,
3072 "%s: Failed to open '%s'", __func__, path);
3083 int llapi_migrate_mdt(char *path, struct find_param *param)
3085 return param_callback(path, cb_migrate_mdt_init, cb_common_fini, param);
3088 int llapi_mv(char *path, struct find_param *param)
3090 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 53, 0)
3091 static bool printed;
3094 llapi_error(LLAPI_MSG_ERROR, -ESTALE,
3095 "llapi_mv() is deprecated, use llapi_migrate_mdt()\n");
3099 return llapi_migrate_mdt(path, param);
3102 int llapi_find(char *path, struct find_param *param)
3104 return param_callback(path, cb_find_init, cb_common_fini, param);
3108 * Get MDT number that the file/directory inode referenced
3109 * by the open fd resides on.
3110 * Return 0 and mdtidx on success, or -ve errno.
3112 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
3114 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
3119 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
3120 struct dirent64 *de)
3122 struct find_param *param = (struct find_param *)data;
3123 DIR *d = dirp == NULL ? NULL : *dirp;
3127 LASSERT(parent != NULL || d != NULL);
3130 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3131 } else /* if (parent) */ {
3134 fd = open(path, O_RDONLY | O_NOCTTY);
3136 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3144 if (ret == -ENODATA) {
3145 if (!param->fp_obd_uuid)
3146 llapi_printf(LLAPI_MSG_NORMAL,
3147 "'%s' has no stripe info\n", path);
3149 } else if (ret == -ENOENT) {
3150 llapi_error(LLAPI_MSG_WARN, ret,
3151 "warning: %s: '%s' does not exist",
3154 } else if (ret == -ENOTTY) {
3155 llapi_error(LLAPI_MSG_ERROR, ret,
3156 "%s: '%s' not on a Lustre fs",
3159 llapi_error(LLAPI_MSG_ERROR, ret,
3160 "error: %s: '%s' failed get_mdtidx",
3166 if (param->fp_quiet || !(param->fp_verbose & VERBOSE_DETAIL))
3167 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3169 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3173 /* Do not go down anymore? */
3174 if (param->fp_depth == param->fp_max_depth)
3182 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
3183 struct dirent64 *de)
3185 struct find_param *param = (struct find_param *)data;
3186 DIR *d = dirp == NULL ? NULL : *dirp;
3189 LASSERT(parent != NULL || d != NULL);
3191 if (param->fp_obd_uuid) {
3192 param->fp_quiet = 1;
3193 ret = setup_obd_uuid(d ? d : parent, path, param);
3199 if (param->fp_get_lmv || param->fp_get_default_lmv) {
3200 ret = cb_get_dirstripe(path, d, param);
3202 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
3203 (void *)¶m->fp_lmd->lmd_lmm);
3206 } else if (parent && !param->fp_get_lmv && !param->fp_get_default_lmv) {
3207 char *fname = strrchr(path, '/');
3208 fname = (fname == NULL ? path : fname + 1);
3210 strlcpy((char *)¶m->fp_lmd->lmd_lmm, fname,
3211 param->fp_lum_size);
3213 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
3214 (void *)¶m->fp_lmd->lmd_lmm);
3220 if (errno == ENODATA && d != NULL) {
3221 /* We need to "fake" the "use the default" values
3222 * since the lmm struct is zeroed out at this point.
3223 * The magic needs to be set in order to satisfy
3224 * a check later on in the code path.
3225 * The object_seq needs to be set for the "(Default)"
3226 * prefix to be displayed. */
3227 if (param->fp_get_default_lmv) {
3228 struct lmv_user_md *lum = param->fp_lmv_md;
3230 lum->lum_magic = LMV_USER_MAGIC;
3231 lum->lum_stripe_count = 0;
3232 lum->lum_stripe_offset = -1;
3234 } else if (param->fp_get_lmv) {
3235 struct lmv_user_md *lum = param->fp_lmv_md;
3238 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3241 lum->lum_magic = LMV_MAGIC_V1;
3242 lum->lum_stripe_count = 0;
3243 lum->lum_stripe_offset = mdtidx;
3246 struct lov_user_md *lmm =
3247 ¶m->fp_lmd->lmd_lmm;
3249 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3251 ostid_set_seq(&lmm->lmm_oi,
3252 FID_SEQ_LOV_DEFAULT);
3253 lmm->lmm_stripe_count = 0;
3254 lmm->lmm_stripe_size = 0;
3255 lmm->lmm_stripe_offset = -1;
3258 } else if (errno == ENODATA && parent != NULL) {
3259 if (!param->fp_obd_uuid && !param->fp_mdt_uuid)
3260 llapi_printf(LLAPI_MSG_NORMAL,
3261 "%s has no stripe info\n", path);
3263 } else if (errno == ENOENT) {
3264 llapi_error(LLAPI_MSG_WARN, -ENOENT,
3265 "warning: %s: %s does not exist",
3268 } else if (errno == ENOTTY) {
3270 llapi_error(LLAPI_MSG_ERROR, ret,
3271 "%s: '%s' not on a Lustre fs?",
3276 llapi_error(LLAPI_MSG_ERROR, ret,
3277 "error: %s: %s failed for %s",
3278 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
3279 "IOC_MDC_GETFILESTRIPE", path);
3286 if (!(param->fp_verbose & VERBOSE_MDTINDEX))
3287 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
3290 /* Do not get down anymore? */
3291 if (param->fp_depth == param->fp_max_depth)
3299 int llapi_getstripe(char *path, struct find_param *param)
3301 return param_callback(path, (param->fp_verbose & VERBOSE_MDTINDEX) ?
3302 cb_get_mdt_index : cb_getstripe,
3303 cb_common_fini, param);
3306 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
3307 struct obd_statfs *stat_buf,
3308 struct obd_uuid *uuid_buf)
3311 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3313 struct obd_ioctl_data data = { 0 };
3316 data.ioc_inlbuf1 = (char *)&type;
3317 data.ioc_inllen1 = sizeof(__u32);
3318 data.ioc_inlbuf2 = (char *)&index;
3319 data.ioc_inllen2 = sizeof(__u32);
3320 data.ioc_pbuf1 = (char *)stat_buf;
3321 data.ioc_plen1 = sizeof(struct obd_statfs);
3322 data.ioc_pbuf2 = (char *)uuid_buf;
3323 data.ioc_plen2 = sizeof(struct obd_uuid);
3325 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3327 llapi_error(LLAPI_MSG_ERROR, rc,
3328 "llapi_obd_statfs: error packing ioctl data");
3332 fd = open(path, O_RDONLY);
3333 if (errno == EISDIR)
3334 fd = open(path, O_DIRECTORY | O_RDONLY);
3337 rc = errno ? -errno : -EBADF;
3338 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
3340 /* If we can't even open a file on the filesystem (e.g. with
3341 * -ESHUTDOWN), force caller to exit or it will loop forever. */
3344 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
3346 rc = errno ? -errno : -EINVAL;
3352 #define MAX_STRING_SIZE 128
3354 int llapi_ping(char *obd_type, char *obd_name)
3360 rc = cfs_get_param_paths(&path, "%s/%s/ping",
3361 obd_type, obd_name);
3365 fd = open(path.gl_pathv[0], O_WRONLY);
3368 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
3373 /* The purpose is to send a byte as a ping, whatever this byte is. */
3374 /* coverity[uninit_use_in_call] */
3375 rc = write(fd, buf, 1);
3383 cfs_free_param_data(&path);
3387 int llapi_target_iterate(int type_num, char **obd_type,
3388 void *args, llapi_cb_t cb)
3390 char buf[MAX_STRING_SIZE];
3395 rc = cfs_get_param_paths(¶m, "devices");
3399 fp = fopen(param.gl_pathv[0], "r");
3402 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
3407 while (fgets(buf, sizeof(buf), fp) != NULL) {
3408 char *obd_type_name = NULL;
3409 char *obd_name = NULL;
3410 char *obd_uuid = NULL;
3412 struct obd_statfs osfs_buffer;
3414 while(bufp[0] == ' ')
3417 for(i = 0; i < 3; i++) {
3418 obd_type_name = strsep(&bufp, " ");
3420 obd_name = strsep(&bufp, " ");
3421 obd_uuid = strsep(&bufp, " ");
3423 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
3425 for (i = 0; i < type_num; i++) {
3426 if (strcmp(obd_type_name, obd_type[i]) != 0)
3429 cb(obd_type_name, obd_name, obd_uuid, args);
3434 cfs_free_param_data(¶m);
3438 static void do_target_check(char *obd_type_name, char *obd_name,
3439 char *obd_uuid, void *args)
3443 rc = llapi_ping(obd_type_name, obd_name);
3444 if (rc == ENOTCONN) {
3445 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
3447 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
3449 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
3453 int llapi_target_check(int type_num, char **obd_type, char *dir)
3455 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
3458 #undef MAX_STRING_SIZE
3460 /* Is this a lustre fs? */
3461 int llapi_is_lustre_mnttype(const char *type)
3463 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
3466 /* Is this a lustre client fs? */
3467 int llapi_is_lustre_mnt(struct mntent *mnt)
3469 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
3470 strstr(mnt->mnt_fsname, ":/") != NULL);
3473 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3478 root = opendir(mnt);
3481 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3485 rc = ioctl(dirfd(root), OBD_IOC_QUOTACTL, qctl);
3496 #include <sys/wait.h>
3500 static int rmtacl_notify(int ops)
3504 int found = 0, fd = 0, rc = 0;
3506 fp = setmntent(MOUNTED, "r");
3509 llapi_error(LLAPI_MSG_ERROR, rc,
3510 "error setmntent(%s)", MOUNTED);
3515 mnt = getmntent(fp);
3519 if (!llapi_is_lustre_mnt(mnt))
3522 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
3525 llapi_error(LLAPI_MSG_ERROR, rc,
3526 "Can't open '%s'", mnt->mnt_dir);
3530 rc = ioctl(fd, LL_IOC_RMTACL, ops);
3534 llapi_error(LLAPI_MSG_ERROR, rc,
3535 "ioctl RMTACL on '%s' err %d",
3545 return ((rc != 0) ? rc : found);
3548 static char *next_token(char *p, int div)
3554 while (*p && *p != ':' && !isspace(*p))
3557 while (*p == ':' || isspace(*p))
3560 return *p ? p : NULL;
3563 static int rmtacl_name2id(char *name, int is_user)
3568 pw = getpwnam(name);
3572 return (int)(pw->pw_uid);
3576 gr = getgrnam(name);
3580 return (int)(gr->gr_gid);
3584 static int isodigit(int c)
3586 return (c >= '0' && c <= '7') ? 1 : 0;
3590 * Whether the name is just digits string (uid/gid) already or not.
3595 static int str_is_id(char *str)
3602 if (*str == 'x' || *str == 'X') { /* for Hex. */
3603 if (!isxdigit(*(++str)))
3606 while (isxdigit(*(++str)));
3607 } else if (isodigit(*str)) { /* for Oct. */
3608 while (isodigit(*(++str)));
3610 } else if (isdigit(*str)) { /* for Dec. */
3611 while (isdigit(*(++str)));
3614 return (*str == 0) ? 1 : 0;
3624 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
3626 static rmtacl_name_t rmtacl_namelist[] = {
3627 { RMTACL_OPTNAME("user:"), 1, 0 },
3628 { RMTACL_OPTNAME("group:"), 0, 0 },
3629 { RMTACL_OPTNAME("default:user:"), 1, 0 },
3630 { RMTACL_OPTNAME("default:group:"), 0, 0 },
3631 /* for --tabular option */
3632 { RMTACL_OPTNAME("user"), 1, 1 },
3633 { RMTACL_OPTNAME("group"), 0, 1 },
3637 static int rgetfacl_output(char *str)
3639 char *start = NULL, *end = NULL;
3640 int is_user = 0, n, id;
3647 for (rn = rmtacl_namelist; rn->name; rn++) {
3648 if(strncmp(str, rn->name, rn->length) == 0) {
3649 if (!rn->next_token)
3650 start = str + rn->length;
3652 start = next_token(str + rn->length, 0);
3653 is_user = rn->is_user;
3658 end = next_token(start, 1);
3659 if (end == NULL || start == end) {
3660 n = printf("%s", str);
3666 id = rmtacl_name2id(start, is_user);
3667 if (id == INVALID_ID) {
3668 if (str_is_id(start)) {
3670 n = printf("%s", str);
3673 } else if ((id == NOBODY_UID && is_user) ||
3674 (id == NOBODY_GID && !is_user)) {
3676 n = printf("%s", str);
3680 n = printf("%s%d%s", str, id, end);
3685 static int child_status(int status)
3687 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
3690 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
3693 int fd[2], status, rc;
3700 llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe");
3707 llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork");
3712 /* child process redirects its output. */
3715 if (dup2(fd[1], 1) < 0) {
3717 llapi_error(LLAPI_MSG_ERROR, rc,
3718 "Can't dup2 %d", fd[1]);
3728 status = rmtacl_notify(ops);
3732 exit(execvp(argv[0], argv));
3735 /* the following is parent process */
3736 fp = fdopen(fd[0], "r");
3739 llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed", fd[0]);
3745 while (fgets(buf, PIPE_BUF, fp) != NULL) {
3746 if (output_func(buf) < 0)
3747 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
3753 if (waitpid(pid, &status, 0) < 0) {
3755 llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed", pid);
3759 return child_status(status);
3762 int llapi_lsetfacl(int argc, char *argv[])
3764 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
3767 int llapi_lgetfacl(int argc, char *argv[])
3769 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
3772 int llapi_rsetfacl(int argc, char *argv[])
3774 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
3777 int llapi_rgetfacl(int argc, char *argv[])
3779 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
3782 int llapi_cp(int argc, char *argv[])
3786 rc = rmtacl_notify(RMT_RSETFACL);
3790 exit(execvp(argv[0], argv));
3793 int llapi_ls(int argc, char *argv[])
3797 rc = rmtacl_notify(RMT_LGETFACL);
3801 exit(execvp(argv[0], argv));
3804 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
3805 * format must have %s%s, buf must be > 16
3806 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
3807 * then buf = "lustre-MDT0000"
3809 static int get_mdtname(char *name, char *format, char *buf)
3811 char suffix[]="-MDT0000";
3812 int len = strlen(name);
3814 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
3815 name[len - 5] = '\0';
3820 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
3823 /* Not enough room to add suffix */
3824 llapi_err_noerrno(LLAPI_MSG_ERROR,
3825 "MDT name too long |%s|", name);
3830 return sprintf(buf, format, name, suffix);
3833 /** ioctl on filsystem root, with mdtindex sent as data
3834 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
3835 * \param mdtidxp pointer to integer within data to be filled in with the
3836 * mdt index (0 if no mdt is specified). NULL won't be filled.
3838 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
3846 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
3847 Open root and parse mdt index. */
3848 if (mdtname[0] == '/') {
3850 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
3851 (char *)mdtname, -1);
3853 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
3855 ptr = fsname + strlen(fsname) - 8;
3857 index = strtol(ptr + 4, NULL, 10);
3858 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
3862 llapi_err_noerrno(LLAPI_MSG_ERROR,
3863 "Can't open %s: %d\n", mdtname, rc);
3870 rc = ioctl(fd, opc, data);
3875 if (rc && want_error)
3876 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
3882 /****** Changelog API ********/
3884 static int changelog_ioctl(const char *mdtname, int opc, int id,
3885 long long recno, int flags)
3887 struct ioc_changelog data;
3891 data.icc_recno = recno;
3892 data.icc_flags = flags;
3893 idx = (int *)(&data.icc_mdtindex);
3895 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
3898 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
3899 struct changelog_private {
3901 enum changelog_send_flag flags;
3902 struct lustre_kernelcomm kuc;
3905 /** Start reading from a changelog
3906 * @param priv Opaque private control structure
3907 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
3908 * @param device Report changes recorded on this MDT
3909 * @param startrec Report changes beginning with this record number
3910 * (just call llapi_changelog_fini when done; don't need an endrec)
3912 int llapi_changelog_start(void **priv, enum changelog_send_flag flags,
3913 const char *device, long long startrec)
3915 struct changelog_private *cp;
3919 /* Set up the receiver control struct */
3920 cp = calloc(1, sizeof(*cp));
3924 cp->magic = CHANGELOG_PRIV_MAGIC;
3927 /* Set up the receiver */
3928 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */, 0);
3934 /* CHANGELOG_FLAG_JOBID will eventually become mandatory. Display a
3935 * warning if it's missing. */
3936 if (!(flags & CHANGELOG_FLAG_JOBID) && !warned) {
3937 llapi_err_noerrno(LLAPI_MSG_WARN, "warning: %s() called "
3938 "w/o CHANGELOG_FLAG_JOBID", __func__);
3942 /* Tell the kernel to start sending */
3943 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
3945 /* Only the kernel reference keeps the write side open */
3946 close(cp->kuc.lk_wfd);
3947 cp->kuc.lk_wfd = LK_NOFD;
3949 /* frees and clears priv */
3950 llapi_changelog_fini(priv);
3961 /** Finish reading from a changelog */
3962 int llapi_changelog_fini(void **priv)
3964 struct changelog_private *cp = (struct changelog_private *)*priv;
3966 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3969 libcfs_ukuc_stop(&cp->kuc);
3976 * Convert all records to a same format according to the caller's wishes.
3977 * Default is CLF_VERSION | CLF_RENAME.
3978 * Add CLF_JOBID if explicitely requested.
3980 * \param rec The record to remap. It is expected to be big enough to
3981 * properly handle the final format.
3982 * \return 1 if anything changed. 0 otherwise.
3984 /** Read the next changelog entry
3985 * @param priv Opaque private control structure
3986 * @param rech Changelog record handle; record will be allocated here
3987 * @return 0 valid message received; rec is set
3991 #define DEFAULT_RECORD_FMT (CLF_VERSION | CLF_RENAME)
3992 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
3994 struct changelog_private *cp = (struct changelog_private *)priv;
3995 struct kuc_hdr *kuch;
3996 enum changelog_rec_flags rec_fmt = DEFAULT_RECORD_FMT;
3999 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4003 kuch = malloc(KUC_CHANGELOG_MSG_MAXSIZE);
4007 if (cp->flags & CHANGELOG_FLAG_JOBID)
4008 rec_fmt |= CLF_JOBID;
4011 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
4012 KUC_CHANGELOG_MSG_MAXSIZE,
4013 KUC_TRANSPORT_CHANGELOG);
4017 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
4018 ((kuch->kuc_msgtype != CL_RECORD) &&
4019 (kuch->kuc_msgtype != CL_EOF))) {
4020 llapi_err_noerrno(LLAPI_MSG_ERROR,
4021 "Unknown changelog message type %d:%d\n",
4022 kuch->kuc_transport, kuch->kuc_msgtype);
4027 if (kuch->kuc_msgtype == CL_EOF) {
4028 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
4037 /* Our message is a changelog_rec. Use pointer math to skip
4038 * kuch_hdr and point directly to the message payload. */
4039 *rech = (struct changelog_rec *)(kuch + 1);
4040 changelog_remap_rec(*rech, rec_fmt);
4050 /** Release the changelog record when done with it. */
4051 int llapi_changelog_free(struct changelog_rec **rech)
4054 /* We allocated memory starting at the kuc_hdr, but passed
4055 * the consumer a pointer to the payload.
4056 * Use pointer math to get back to the header.
4058 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
4065 int llapi_changelog_clear(const char *mdtname, const char *idstr,
4071 llapi_err_noerrno(LLAPI_MSG_ERROR,
4072 "can't purge negative records\n");
4076 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
4077 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
4078 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
4079 llapi_err_noerrno(LLAPI_MSG_ERROR,
4080 "expecting id of the form '"
4081 CHANGELOG_USER_PREFIX
4082 "<num>'; got '%s'\n", idstr);
4086 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
4089 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
4090 int buflen, long long *recno, int *linkno)
4093 struct getinfo_fid2path *gf;
4096 while (*fidstr == '[')
4099 sscanf(fidstr, SFID, RFID(&fid));
4100 if (!fid_is_sane(&fid)) {
4101 llapi_err_noerrno(LLAPI_MSG_ERROR,
4102 "bad FID format [%s], should be [seq:oid:ver]"
4103 " (e.g. "DFID")\n", fidstr,
4104 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
4108 gf = malloc(sizeof(*gf) + buflen);
4112 gf->gf_recno = *recno;
4113 gf->gf_linkno = *linkno;
4114 gf->gf_pathlen = buflen;
4116 /* Take path or fsname */
4117 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
4120 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
4122 memcpy(buf, gf->gf_path, gf->gf_pathlen);
4123 if (buf[0] == '\0') { /* ROOT path */
4127 *recno = gf->gf_recno;
4128 *linkno = gf->gf_linkno;
4135 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
4138 struct lustre_mdt_attrs *lma;
4142 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
4144 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
4147 lma = (struct lustre_mdt_attrs *)buf;
4148 fid_le_to_cpu(fid, &lma->lma_self_fid);
4152 int llapi_get_mdt_index_by_fid(int fd, const lustre_fid *fid,
4157 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
4166 int llapi_fd2fid(const int fd, lustre_fid *fid)
4170 memset(fid, 0, sizeof(*fid));
4172 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
4173 if (rc == -EINVAL || rc == -ENOTTY)
4174 rc = fid_from_lma(NULL, fd, fid);
4179 int llapi_path2fid(const char *path, lustre_fid *fid)
4183 memset(fid, 0, sizeof(*fid));
4184 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4186 if (errno == ELOOP || errno == ENXIO)
4187 return fid_from_lma(path, -1, fid);
4191 rc = llapi_fd2fid(fd, fid);
4192 if (rc == -EINVAL || rc == -ENOTTY)
4193 rc = fid_from_lma(path, -1, fid);
4199 int llapi_fd2parent(int fd, unsigned int linkno, lustre_fid *parent_fid,
4200 char *name, size_t name_size)
4202 struct getparent *gp;
4205 gp = malloc(sizeof(*gp) + name_size);
4209 gp->gp_linkno = linkno;
4210 gp->gp_name_size = name_size;
4212 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
4218 *parent_fid = gp->gp_fid;
4220 strncpy(name, gp->gp_name, name_size);
4221 name[name_size - 1] = '\0';
4228 int llapi_path2parent(const char *path, unsigned int linkno,
4229 lustre_fid *parent_fid, char *name, size_t name_size)
4234 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4238 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
4243 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4248 root = opendir(mnt);
4251 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4255 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
4258 llapi_error(LLAPI_MSG_ERROR, rc,
4259 "ioctl on %s for getting connect flags failed", mnt);
4265 int llapi_get_version(char *buffer, int buffer_size,
4270 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
4272 fd = open(OBD_DEV_PATH, O_RDONLY);
4276 memset(buffer, 0, buffer_size);
4277 data->ioc_version = OBD_IOCTL_VERSION;
4278 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
4279 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
4280 data->ioc_len = obd_ioctl_packlen(data);
4282 rc = ioctl(fd, OBD_GET_VERSION, buffer);
4289 *version = data->ioc_bulk;
4294 * Get a 64-bit value representing the version of file data pointed by fd.
4296 * Each write or truncate, flushed on OST, will change this value. You can use
4297 * this value to verify if file data was modified. This only checks the file
4298 * data, not metadata.
4300 * \param flags 0: no flush pages, usually used it the process has already
4302 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
4304 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
4305 * pages from clients.
4307 * \retval 0 on success.
4308 * \retval -errno on error.
4310 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4313 struct ioc_data_version idv;
4315 idv.idv_flags = flags;
4317 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4321 *data_version = idv.idv_version;
4327 * Create a file without any name open it for read/write
4329 * - file is created as if it were a standard file in the given \a directory
4330 * - file does not appear in \a directory and mtime does not change because
4331 * the filename is handled specially by the Lustre MDS.
4332 * - file is removed at final close
4333 * - file modes are rw------- since it doesn't make sense to have a read-only
4334 * or write-only file that cannot be opened again.
4335 * - if user wants another mode it must use fchmod() on the open file, no
4336 * security problems arise because it cannot be opened by another process.
4338 * \param[in] directory directory from which to inherit layout/MDT idx
4339 * \param[in] idx MDT index on which the file is created,
4340 * \a idx == -1 means no specific MDT is requested
4341 * \param[in] open_flags standard open(2) flags
4343 * \retval 0 on success.
4344 * \retval -errno on error.
4346 int llapi_create_volatile_idx(char *directory, int idx, int open_flags)
4348 char file_path[PATH_MAX];
4349 char filename[PATH_MAX];
4350 int saved_errno = errno;
4358 snprintf(filename, sizeof(filename),
4359 LUSTRE_VOLATILE_HDR"::%.4X", rnumber);
4361 snprintf(filename, sizeof(filename),
4362 LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, rnumber);
4364 rc = snprintf(file_path, sizeof(file_path),
4365 "%s/%s", directory, filename);
4366 if (rc >= sizeof(file_path))
4369 fd = open(file_path,
4370 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | open_flags,
4372 } while (fd < 0 && errno == EEXIST);
4375 llapi_error(LLAPI_MSG_ERROR, errno,
4376 "Cannot create volatile file '%s' in '%s'",
4377 filename + LUSTRE_VOLATILE_HDR_LEN,
4382 /* Unlink file in case this wasn't a Lustre filesystem and the
4383 * magic volatile filename wasn't handled as intended. The
4384 * effect is the same. If volatile open was supported then we
4385 * expect unlink() to return -ENOENT. */
4386 (void)unlink(file_path);
4388 /* Since we are returning successfully we restore errno (and
4389 * mask out possible EEXIST from open() and ENOENT from
4391 errno = saved_errno;
4397 * Swap the layouts between 2 file descriptors
4398 * the 2 files must be open for writing
4399 * first fd received the ioctl, second fd is passed as arg
4400 * this is assymetric but avoid use of root path for ioctl
4402 int llapi_fswap_layouts_grouplock(int fd1, int fd2, __u64 dv1, __u64 dv2,
4403 int gid, __u64 flags)
4405 struct lustre_swap_layouts lsl;
4410 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4411 rc = fstat(fd1, &st1);
4415 rc = fstat(fd2, &st2);
4420 lsl.sl_flags = flags;
4424 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4428 if (flags & (SWAP_LAYOUTS_KEEP_ATIME | SWAP_LAYOUTS_KEEP_MTIME)) {
4429 struct timeval tv1[2];
4430 struct timeval tv2[2];
4432 memset(tv1, 0, sizeof(tv1));
4433 memset(tv2, 0, sizeof(tv2));
4435 if (flags & SWAP_LAYOUTS_KEEP_ATIME) {
4436 tv1[0].tv_sec = st1.st_atime;
4437 tv2[0].tv_sec = st2.st_atime;
4439 tv1[0].tv_sec = st2.st_atime;
4440 tv2[0].tv_sec = st1.st_atime;
4443 if (flags & SWAP_LAYOUTS_KEEP_MTIME) {
4444 tv1[1].tv_sec = st1.st_mtime;
4445 tv2[1].tv_sec = st2.st_mtime;
4447 tv1[1].tv_sec = st2.st_mtime;
4448 tv2[1].tv_sec = st1.st_mtime;
4451 rc = futimes(fd1, tv1);
4455 rc = futimes(fd2, tv2);
4463 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4470 while (grp_id == 0);
4472 rc = llapi_fswap_layouts_grouplock(fd1, fd2, dv1, dv2, grp_id, flags);
4480 * Swap the layouts between 2 files
4481 * the 2 files are open in write
4483 int llapi_swap_layouts(const char *path1, const char *path2,
4484 __u64 dv1, __u64 dv2, __u64 flags)
4488 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4491 llapi_error(LLAPI_MSG_ERROR, rc,
4492 "error: cannot open '%s' for write", path1);
4496 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4499 llapi_error(LLAPI_MSG_ERROR, rc,
4500 "error: cannot open '%s' for write", path2);
4504 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4506 llapi_error(LLAPI_MSG_ERROR, rc,
4507 "error: cannot swap layout between '%s' and '%s'",
4518 * Attempt to open a file with Lustre file identifier \a fid
4519 * and return an open file descriptor.
4521 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
4522 * \param[in] fid Lustre file identifier of file to open
4523 * \param[in] flags open() flags
4525 * \retval non-negative file descriptor on successful open
4526 * \retval -1 if an error occurred
4528 int llapi_open_by_fid(const char *lustre_dir, const lustre_fid *fid, int flags)
4530 char mntdir[PATH_MAX];
4531 char path[PATH_MAX];
4534 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
4538 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
4539 return open(path, flags);
4545 * \param fd File to lock.
4546 * \param gid Group Identifier.
4548 * \retval 0 on success.
4549 * \retval -errno on failure.
4551 int llapi_group_lock(int fd, int gid)
4555 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
4558 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
4566 * \param fd File to unlock.
4567 * \param gid Group Identifier.
4569 * \retval 0 on success.
4570 * \retval -errno on failure.
4572 int llapi_group_unlock(int fd, int gid)
4576 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
4579 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");