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, 2013, 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>
52 #include <sys/ioctl.h>
59 #include <sys/statfs.h>
60 #include <sys/types.h>
61 #include <sys/syscall.h>
62 #include <sys/xattr.h>
65 #include <libgen.h> /* for dirname() */
66 #ifdef HAVE_LINUX_UNISTD_H
67 #include <linux/unistd.h>
73 #include <libcfs/libcfs.h>
74 #include <lnet/lnetctl.h>
75 #include <lustre/lustreapi.h>
76 #include <lustre_ioctl.h>
77 #include "lustreapi_internal.h"
79 static int llapi_msg_level = LLAPI_MSG_MAX;
81 void llapi_msg_set_level(int level)
83 /* ensure level is in the good range */
84 if (level < LLAPI_MSG_OFF)
85 llapi_msg_level = LLAPI_MSG_OFF;
86 else if (level > LLAPI_MSG_MAX)
87 llapi_msg_level = LLAPI_MSG_MAX;
89 llapi_msg_level = level;
92 int llapi_msg_get_level(void)
94 return llapi_msg_level;
97 static void error_callback_default(enum llapi_message_level level, int err,
98 const char *fmt, va_list ap)
100 vfprintf(stderr, fmt, ap);
101 if (level & LLAPI_MSG_NO_ERRNO)
102 fprintf(stderr, "\n");
104 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
107 static void info_callback_default(enum llapi_message_level level, int err,
108 const char *fmt, va_list ap)
110 vfprintf(stdout, fmt, ap);
113 static llapi_log_callback_t llapi_error_callback = error_callback_default;
114 static llapi_log_callback_t llapi_info_callback = info_callback_default;
117 /* llapi_error will preserve errno */
118 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
121 int tmp_errno = errno;
123 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
127 llapi_error_callback(level, abs(err), fmt, args);
132 /* llapi_printf will preserve errno */
133 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
136 int tmp_errno = errno;
138 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
142 llapi_info_callback(level, 0, fmt, args);
148 * Set a custom error logging function. Passing in NULL will reset the logging
149 * callback to its default value.
151 * This function returns the value of the old callback.
153 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
155 llapi_log_callback_t old = llapi_error_callback;
158 llapi_error_callback = cb;
160 llapi_error_callback = error_callback_default;
166 * Set a custom info logging function. Passing in NULL will reset the logging
167 * callback to its default value.
169 * This function returns the value of the old callback.
171 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
173 llapi_log_callback_t old = llapi_info_callback;
176 llapi_info_callback = cb;
178 llapi_info_callback = info_callback_default;
184 * size_units is to be initialized (or zeroed) by caller.
186 int llapi_parse_size(const char *optarg, unsigned long long *size,
187 unsigned long long *size_units, int bytes_spec)
191 if (strncmp(optarg, "-", 1) == 0)
194 if (*size_units == 0)
197 *size = strtoull(optarg, &end, 0);
200 if ((*end == 'b') && *(end + 1) == '\0' &&
201 (*size & (~0ULL << (64 - 9))) == 0 &&
203 *size_units = 1 << 9;
204 } else if ((*end == 'b') &&
205 *(end + 1) == '\0' &&
208 } else if ((*end == 'k' || *end == 'K') &&
209 *(end + 1) == '\0' &&
210 (*size & (~0ULL << (64 - 10))) == 0) {
211 *size_units = 1 << 10;
212 } else if ((*end == 'm' || *end == 'M') &&
213 *(end + 1) == '\0' &&
214 (*size & (~0ULL << (64 - 20))) == 0) {
215 *size_units = 1 << 20;
216 } else if ((*end == 'g' || *end == 'G') &&
217 *(end + 1) == '\0' &&
218 (*size & (~0ULL << (64 - 30))) == 0) {
219 *size_units = 1 << 30;
220 } else if ((*end == 't' || *end == 'T') &&
221 *(end + 1) == '\0' &&
222 (*size & (~0ULL << (64 - 40))) == 0) {
223 *size_units = 1ULL << 40;
224 } else if ((*end == 'p' || *end == 'P') &&
225 *(end + 1) == '\0' &&
226 (*size & (~0ULL << (64 - 50))) == 0) {
227 *size_units = 1ULL << 50;
228 } else if ((*end == 'e' || *end == 'E') &&
229 *(end + 1) == '\0' &&
230 (*size & (~0ULL << (64 - 60))) == 0) {
231 *size_units = 1ULL << 60;
236 *size *= *size_units;
240 /* XXX: llapi_xxx() functions return negative values upon failure */
242 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
243 int stripe_count, int stripe_pattern)
247 /* 64 KB is the largest common page size I'm aware of (on ia64), but
248 * check the local page size just in case. */
249 page_size = LOV_MIN_STRIPE_SIZE;
250 if (getpagesize() > page_size) {
251 page_size = getpagesize();
252 llapi_err_noerrno(LLAPI_MSG_WARN,
253 "warning: your page size (%u) is "
254 "larger than expected (%u)", page_size,
255 LOV_MIN_STRIPE_SIZE);
257 if (!llapi_stripe_size_is_aligned(stripe_size)) {
259 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
260 "must be an even multiple of %d bytes",
261 stripe_size, page_size);
264 if (!llapi_stripe_offset_is_valid(stripe_offset)) {
266 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
270 if (!llapi_stripe_count_is_valid(stripe_count)) {
272 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
276 if (llapi_stripe_size_is_too_big(stripe_size)) {
278 llapi_error(LLAPI_MSG_ERROR, rc,
279 "warning: stripe size 4G or larger "
280 "is not currently supported and would wrap");
286 /* return the first file matching this pattern */
287 static int first_match(char *pattern, char *buffer)
291 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
294 if (glob_info.gl_pathc < 1) {
295 globfree(&glob_info);
299 strcpy(buffer, glob_info.gl_pathv[0]);
301 globfree(&glob_info);
305 static int find_target_obdpath(char *fsname, char *path)
308 char pattern[PATH_MAX + 1];
311 snprintf(pattern, PATH_MAX,
312 "/proc/fs/lustre/lov/%s-*/target_obd",
314 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
315 if (rc == GLOB_NOMATCH)
320 strcpy(path, glob_info.gl_pathv[0]);
321 globfree(&glob_info);
325 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
328 char pattern[PATH_MAX + 1];
331 snprintf(pattern, PATH_MAX,
332 "/proc/fs/lustre/lov/%s-*/pools/%s",
334 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
335 /* If no pools, make sure the lov is available */
336 if ((rc == GLOB_NOMATCH) &&
337 (find_target_obdpath(fsname, poolpath) == -ENODEV))
342 strcpy(poolpath, glob_info.gl_pathv[0]);
343 globfree(&glob_info);
348 * Trim a trailing newline from a string, if it exists.
350 int llapi_chomp_string(char *buf)
366 * return a parameter string for a specific device type or mountpoint
368 * \param param_path the path to the file containing parameter data
369 * \param result buffer for parameter value string
370 * \param result_size size of buffer for return value
372 * The \param param_path is appended to /proc/{fs,sys}/{lnet,lustre} to
373 * complete the absolute path to the file containing the parameter data
374 * the user is requesting. If that file exist then the data is read from
375 * the file and placed into the \param result buffer that is passed by
376 * the user. Data is only copied up to the \param result_size to prevent
377 * overflow of the array.
379 * Return 0 for success, with a NUL-terminated string in \param result.
380 * Return -ve value for error.
382 int get_param(const char *param_path, char *result,
383 unsigned int result_size)
385 char file[PATH_MAX + 1], pattern[PATH_MAX + 1], buf[result_size];
389 snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
391 rc = first_match(pattern, file);
392 if (rc != 0 || result == NULL)
395 fp = fopen(file, "r");
397 while (fgets(buf, result_size, fp) != NULL)
406 #define DEVICES_LIST "/proc/fs/lustre/devices"
409 * return a parameter string for a specific device type or mountpoint
411 * \param fsname Lustre filesystem name (optional)
412 * \param file_path path to file in filesystem (optional, if fsname unset)
413 * \param obd_type Lustre OBD device type
414 * \param param_name parameter name to fetch
415 * \param value return buffer for parameter value string
416 * \param val_len size of buffer for return value
418 * If fsname is specified then the parameter will be from that filesystem
419 * (if it exists). If file_path is given and it is in a mounted Lustre
420 * filesystem, then the parameter will be otherwise the value may be
421 * from any mounted filesystem (if there is more than one).
423 * If "obd_type" matches a Lustre device then the first matching device
424 * (as with "lctl dl", constrained by \param fsname or \param mount_path)
425 * will be used to provide the return value, otherwise the first such
426 * device found will be used.
428 * Return 0 for success, with a NUL-terminated string in \param buffer.
429 * Return -ve value for error.
431 static int get_param_obdvar(const char *fsname, const char *file_path,
432 const char *obd_type, const char *param_name,
433 char *value, unsigned int val_len)
435 char devices[PATH_MAX];
436 char dev[PATH_MAX] = "*";
441 fp = fopen(DEVICES_LIST, "r");
444 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
448 if (fsname == NULL && file_path != NULL) {
449 rc = llapi_search_fsname(file_path, fs);
451 llapi_error(LLAPI_MSG_ERROR, rc,
452 "'%s' is not on a Lustre filesystem",
456 } else if (fsname != NULL) {
457 rc = strlcpy(fs, fsname, sizeof(fs));
458 if (rc >= sizeof(fs)) {
464 while (fgets(devices, sizeof(devices) - 1, fp) != NULL) {
465 char *bufp = devices, *tmp;
467 devices[sizeof(devices) - 1] = '\0';
468 while (bufp[0] == ' ')
471 tmp = strstr(bufp, obd_type);
473 tmp += strlen(obd_type) + 1;
476 rc = strlcpy(dev, tmp, sizeof(dev));
477 if (rc >= sizeof(dev)) {
482 tmp = strchr(dev, ' ');
489 if (dev[0] == '*' && strlen(fs)) {
490 rc = snprintf(dev, sizeof(dev), "%s-*", fs);
491 if (rc >= sizeof(dev)) {
496 rc = snprintf(devices, sizeof(devices), "%s/%s/%s", obd_type, dev,
498 if (rc >= sizeof(devices)) {
504 return get_param(devices, value, val_len);
512 * TYPE one of llite, lmv, lov.
513 * /proc/fs/lustre/TYPE/INST the directory of interest.
515 static int get_param_cli(const char *type, const char *inst,
516 const char *param, char *buf, size_t buf_size)
518 char param_path[PATH_MAX + 1];
519 FILE *param_file = NULL;
522 snprintf(param_path, sizeof(param_path),
523 "/proc/fs/lustre/%s/%s/%s", type, inst, param);
525 param_file = fopen(param_path, "r");
526 if (param_file == NULL) {
531 if (fgets(buf, buf_size, param_file) == NULL) {
538 if (param_file != NULL)
544 static int get_param_llite(const char *path,
545 const char *param, char *buf, size_t buf_size)
550 rc = llapi_getname(path, inst, sizeof(inst));
554 return get_param_cli("llite", inst, param, buf, buf_size);
557 static int get_param_lov(const char *path,
558 const char *param, char *buf, size_t buf_size)
560 struct obd_uuid uuid;
563 rc = llapi_file_get_lov_uuid(path, &uuid);
567 return get_param_cli("lov", uuid.uuid, param, buf, buf_size);
570 static int get_param_lmv(const char *path,
571 const char *param, char *buf, size_t buf_size)
573 struct obd_uuid uuid;
576 rc = llapi_file_get_lmv_uuid(path, &uuid);
580 return get_param_cli("lmv", uuid.uuid, param, buf, buf_size);
583 static int get_mds_md_size(const char *path)
585 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
589 /* Get the max ea size from llite proc. */
590 rc = get_param_llite(path, "max_easize", buf, sizeof(buf));
602 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
604 return get_param_lmv(path, "uuid", buf, bufsize);
608 * if pool is NULL, search ostname in target_obd
609 * if pool is not NULL:
610 * if pool not found returns errno < 0
611 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
612 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
614 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
617 char buffer[PATH_MAX + 1];
621 len = strlen(ostname);
623 if (poolname == NULL) {
627 rc = find_target_obdpath(fsname, buffer);
629 rc = find_poolpath(fsname, poolname, buffer);
634 fd = fopen(buffer, "r");
638 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
639 if (poolname == NULL) {
641 /* Search for an ostname in the list of OSTs
642 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
643 ptr = strchr(buffer, ' ');
645 (strncmp(ptr + 1, ostname, len) == 0)) {
650 /* Search for an ostname in a pool,
651 (or an existing non-empty pool if no ostname) */
652 if ((ostname == NULL) ||
653 (strncmp(buffer, ostname, len) == 0)) {
663 int llapi_file_open_pool(const char *name, int flags, int mode,
664 unsigned long long stripe_size, int stripe_offset,
665 int stripe_count, int stripe_pattern, char *pool_name)
667 struct lov_user_md_v3 lum = { 0 };
670 /* Make sure we have a good pool */
671 if (pool_name != NULL) {
672 char fsname[MAX_OBD_NAME + 1], *ptr;
674 rc = llapi_search_fsname(name, fsname);
676 llapi_error(LLAPI_MSG_ERROR, rc,
677 "'%s' is not on a Lustre filesystem",
682 /* in case user gives the full pool name <fsname>.<poolname>,
683 * strip the fsname */
684 ptr = strchr(pool_name, '.');
687 if (strcmp(pool_name, fsname) != 0) {
689 llapi_err_noerrno(LLAPI_MSG_ERROR,
690 "Pool '%s' is not on filesystem '%s'",
697 /* Make sure the pool exists and is non-empty */
698 rc = llapi_search_ost(fsname, pool_name, NULL);
700 llapi_err_noerrno(LLAPI_MSG_ERROR,
701 "pool '%s.%s' %s", fsname, pool_name,
702 rc == 0 ? "has no OSTs" : "does not exist");
708 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
710 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
711 flags = O_DIRECTORY | O_RDONLY;
718 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
722 rc = llapi_stripe_limit_check(stripe_size, stripe_offset, stripe_count,
727 /* Initialize IOCTL striping pattern structure */
728 lum.lmm_magic = LOV_USER_MAGIC_V3;
729 lum.lmm_pattern = stripe_pattern;
730 lum.lmm_stripe_size = stripe_size;
731 lum.lmm_stripe_count = stripe_count;
732 lum.lmm_stripe_offset = stripe_offset;
733 if (pool_name != NULL) {
734 strlcpy(lum.lmm_pool_name, pool_name,
735 sizeof(lum.lmm_pool_name));
737 /* If no pool is specified at all, use V1 request */
738 lum.lmm_magic = LOV_USER_MAGIC_V1;
741 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
742 char *errmsg = "stripe already set";
744 if (errno != EEXIST && errno != EALREADY)
745 errmsg = strerror(errno);
747 llapi_err_noerrno(LLAPI_MSG_ERROR,
748 "error on ioctl "LPX64" for '%s' (%d): %s",
749 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
760 int llapi_file_open(const char *name, int flags, int mode,
761 unsigned long long stripe_size, int stripe_offset,
762 int stripe_count, int stripe_pattern)
764 return llapi_file_open_pool(name, flags, mode, stripe_size,
765 stripe_offset, stripe_count,
766 stripe_pattern, NULL);
769 int llapi_file_create(const char *name, unsigned long long stripe_size,
770 int stripe_offset, int stripe_count, int stripe_pattern)
774 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
775 stripe_offset, stripe_count, stripe_pattern,
784 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
785 int stripe_offset, int stripe_count,
786 int stripe_pattern, char *pool_name)
790 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
791 stripe_offset, stripe_count, stripe_pattern,
800 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
801 int stripe_count, int stripe_pattern,
802 const char *pool_name)
804 struct lmv_user_md lum = { 0 };
808 lum.lum_magic = LMV_USER_MAGIC;
809 lum.lum_stripe_offset = stripe_offset;
810 lum.lum_stripe_count = stripe_count;
811 lum.lum_hash_type = stripe_pattern;
812 if (pool_name != NULL) {
813 if (strlen(pool_name) >= sizeof(lum.lum_pool_name)) {
814 llapi_err_noerrno(LLAPI_MSG_ERROR,
815 "error LL_IOC_LMV_SET_DEFAULT_STRIPE '%s'"
816 ": too large pool name: %s", name, pool_name);
819 strncpy(lum.lum_pool_name, pool_name,
820 sizeof(lum.lum_pool_name));
823 fd = open(name, O_DIRECTORY | O_RDONLY);
826 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
830 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lum);
832 char *errmsg = "stripe already set";
834 if (errno != EEXIST && errno != EALREADY)
835 errmsg = strerror(errno);
837 llapi_err_noerrno(LLAPI_MSG_ERROR,
838 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
845 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
846 int stripe_count, int stripe_pattern,
847 const char *pool_name)
849 struct lmv_user_md lmu = { 0 };
850 struct obd_ioctl_data data = { 0 };
853 char *dirpath = NULL;
854 char *namepath = NULL;
860 dirpath = strdup(name);
861 namepath = strdup(name);
862 if (!dirpath || !namepath)
865 lmu.lum_magic = LMV_USER_MAGIC;
866 lmu.lum_stripe_offset = stripe_offset;
867 lmu.lum_stripe_count = stripe_count;
868 lmu.lum_hash_type = stripe_pattern;
869 if (pool_name != NULL) {
870 if (strlen(pool_name) >= LOV_MAXPOOLNAME) {
871 llapi_err_noerrno(LLAPI_MSG_ERROR,
872 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
873 "pool name: %s", name, pool_name);
877 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
880 filename = basename(namepath);
881 dir = dirname(dirpath);
883 data.ioc_inlbuf1 = (char *)filename;
884 data.ioc_inllen1 = strlen(filename) + 1;
885 data.ioc_inlbuf2 = (char *)&lmu;
886 data.ioc_inllen2 = sizeof(struct lmv_user_md);
887 data.ioc_type = mode;
888 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
890 llapi_error(LLAPI_MSG_ERROR, rc,
891 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
896 fd = open(dir, O_DIRECTORY | O_RDONLY);
899 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
903 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
904 char *errmsg = "stripe already set";
906 if (errno != EEXIST && errno != EALREADY)
907 errmsg = strerror(errno);
909 llapi_err_noerrno(LLAPI_MSG_ERROR,
910 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
920 int llapi_direntry_remove(char *dname)
922 char *dirpath = NULL;
923 char *namepath = NULL;
929 dirpath = strdup(dname);
930 namepath = strdup(dname);
931 if (!dirpath || !namepath)
934 filename = basename(namepath);
936 dir = dirname(dirpath);
938 fd = open(dir, O_DIRECTORY | O_RDONLY);
941 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
946 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
947 char *errmsg = strerror(errno);
948 llapi_err_noerrno(LLAPI_MSG_ERROR,
949 "error on ioctl "LPX64" for '%s' (%d): %s",
950 (__u64)LL_IOC_LMV_SETSTRIPE, filename,
962 * Find the fsname, the full path, and/or an open fd.
963 * Either the fsname or path must not be NULL
965 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
968 char buf[PATH_MAX], mntdir[PATH_MAX];
971 int idx = 0, len = 0, mntlen, fd;
974 /* get the mount point */
975 fp = setmntent(MOUNTED, "r");
978 llapi_error(LLAPI_MSG_ERROR, rc,
979 "setmntent(%s) failed", MOUNTED);
983 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
986 if (!llapi_is_lustre_mnt(&mnt))
989 if ((want & WANT_INDEX) && (idx++ != index))
992 mntlen = strlen(mnt.mnt_dir);
993 ptr = strrchr(mnt.mnt_fsname, '/');
994 /* thanks to the call to llapi_is_lustre_mnt() above,
995 * we are sure that mnt.mnt_fsname contains ":/",
996 * so ptr should never be NULL */
1001 /* Check the fsname for a match, if given */
1002 if (!(want & WANT_FSNAME) && fsname != NULL &&
1003 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
1006 /* If the path isn't set return the first one we find */
1007 if (path == NULL || strlen(path) == 0) {
1008 strcpy(mntdir, mnt.mnt_dir);
1009 if ((want & WANT_FSNAME) && fsname != NULL)
1010 strcpy(fsname, ptr);
1013 /* Otherwise find the longest matching path */
1014 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1015 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1016 strcpy(mntdir, mnt.mnt_dir);
1018 if ((want & WANT_FSNAME) && fsname != NULL)
1019 strcpy(fsname, ptr);
1027 if ((want & WANT_PATH) && path != NULL)
1028 strcpy(path, mntdir);
1029 if (want & WANT_FD) {
1030 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1033 llapi_error(LLAPI_MSG_ERROR, rc,
1034 "error opening '%s'", mntdir);
1040 } else if (want & WANT_ERROR)
1041 llapi_err_noerrno(LLAPI_MSG_ERROR,
1042 "can't find fs root for '%s': %d",
1043 (want & WANT_PATH) ? fsname : path, rc);
1048 * search lustre mounts
1050 * Calling this function will return to the user the mount point, mntdir, and
1051 * the file system name, fsname, if the user passed a buffer to this routine.
1053 * The user inputs are pathname and index. If the pathname is supplied then
1054 * the value of the index will be ignored. The pathname will return data if
1055 * the pathname is located on a lustre mount. Index is used to pick which
1056 * mount point you want in the case of multiple mounted lustre file systems.
1057 * See function lfs_osts in lfs.c for a example of the index use.
1059 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1062 int want = WANT_PATH, idx = -1;
1064 if (!pathname || pathname[0] == '\0') {
1068 strcpy(mntdir, pathname);
1071 want |= WANT_FSNAME;
1072 return get_root_path(want, fsname, NULL, mntdir, idx);
1075 /* Given a path, find the corresponding Lustre fsname */
1076 int llapi_search_fsname(const char *pathname, char *fsname)
1081 path = realpath(pathname, NULL);
1083 char buf[PATH_MAX], *ptr;
1086 if (pathname[0] != '/') {
1087 /* Need an absolute path, but realpath() only works for
1088 * pathnames that actually exist. We go through the
1089 * extra hurdle of dirname(getcwd() + pathname) in
1090 * case the relative pathname contains ".." in it. */
1091 if (getcwd(buf, sizeof(buf) - 2) == NULL)
1093 rc = strlcat(buf, "/", sizeof(buf));
1094 if (rc >= sizeof(buf))
1097 rc = strlcat(buf, pathname, sizeof(buf));
1098 if (rc >= sizeof(buf))
1100 path = realpath(buf, NULL);
1102 ptr = strrchr(buf, '/');
1106 path = realpath(buf, NULL);
1109 llapi_error(LLAPI_MSG_ERROR, rc,
1110 "pathname '%s' cannot expand",
1116 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1121 int llapi_search_rootpath(char *pathname, const char *fsname)
1123 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1126 int llapi_getname(const char *path, char *buf, size_t size)
1128 struct obd_uuid uuid_buf;
1129 char *uuid = uuid_buf.uuid;
1132 memset(&uuid_buf, 0, sizeof(uuid_buf));
1133 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1137 /* We want to turn lustre-clilov-ffff88002738bc00 into
1138 * lustre-ffff88002738bc00. */
1140 nr = snprintf(buf, size, "%.*s-%s",
1141 (int) (strlen(uuid) - 24), uuid,
1142 uuid + strlen(uuid) - 16);
1152 * find the pool directory path under /proc
1153 * (can be also used to test if a fsname is known)
1155 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
1158 char pattern[PATH_MAX + 1];
1159 char buffer[PATH_MAX];
1161 if (fsname == NULL) {
1162 rc = llapi_search_fsname(pathname, buffer);
1166 strcpy(pathname, fsname);
1169 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
1170 rc = first_match(pattern, buffer);
1174 /* in fsname test mode, pool_pathname is NULL */
1175 if (pool_pathname != NULL)
1176 strcpy(pool_pathname, buffer);
1182 * Get the list of pool members.
1183 * \param poolname string of format \<fsname\>.\<poolname\>
1184 * \param members caller-allocated array of char*
1185 * \param list_size size of the members array
1186 * \param buffer caller-allocated buffer for storing OST names
1187 * \param buffer_size size of the buffer
1189 * \return number of members retrieved for this pool
1190 * \retval -error failure
1192 int llapi_get_poolmembers(const char *poolname, char **members,
1193 int list_size, char *buffer, int buffer_size)
1195 char fsname[PATH_MAX];
1197 char pathname[PATH_MAX];
1198 char path[PATH_MAX];
1205 /* name is FSNAME.POOLNAME */
1206 if (strlen(poolname) >= sizeof(fsname))
1208 strlcpy(fsname, poolname, sizeof(fsname));
1209 pool = strchr(fsname, '.');
1216 rc = poolpath(fsname, NULL, pathname);
1218 llapi_error(LLAPI_MSG_ERROR, rc,
1219 "Lustre filesystem '%s' not found",
1224 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1225 rc = snprintf(path, sizeof(path), "%s/%s", pathname, pool);
1226 if (rc >= sizeof(path))
1228 fd = fopen(path, "r");
1231 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
1236 while (fgets(buf, sizeof(buf), fd) != NULL) {
1237 if (nb_entries >= list_size) {
1241 buf[sizeof(buf) - 1] = '\0';
1243 tmp = strchr(buf, '\n');
1246 if (used + strlen(buf) + 1 > buffer_size) {
1251 strcpy(buffer + used, buf);
1252 members[nb_entries] = buffer + used;
1253 used += strlen(buf) + 1;
1263 * Get the list of pools in a filesystem.
1264 * \param name filesystem name or path
1265 * \param poollist caller-allocated array of char*
1266 * \param list_size size of the poollist array
1267 * \param buffer caller-allocated buffer for storing pool names
1268 * \param buffer_size size of the buffer
1270 * \return number of pools retrieved for this filesystem
1271 * \retval -error failure
1273 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1274 char *buffer, int buffer_size)
1276 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
1280 struct dirent *cookie = NULL;
1282 unsigned int nb_entries = 0;
1283 unsigned int used = 0;
1286 /* initilize output array */
1287 for (i = 0; i < list_size; i++)
1290 /* is name a pathname ? */
1291 ptr = strchr(name, '/');
1293 /* only absolute pathname is supported */
1297 if (!realpath(name, rname)) {
1299 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1304 rc = poolpath(NULL, rname, pathname);
1306 llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
1307 " a Lustre filesystem", name);
1310 if (strlen(rname) > sizeof(fsname)-1)
1312 strncpy(fsname, rname, sizeof(fsname));
1314 /* name is FSNAME */
1315 if (strlen(name) > sizeof(fsname)-1)
1317 strncpy(fsname, name, sizeof(fsname));
1318 rc = poolpath(fsname, NULL, pathname);
1321 llapi_error(LLAPI_MSG_ERROR, rc,
1322 "Lustre filesystem '%s' not found", name);
1326 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1327 dir = opendir(pathname);
1330 llapi_error(LLAPI_MSG_ERROR, rc,
1331 "Could not open pool list for '%s'",
1337 rc = readdir_r(dir, &pool, &cookie);
1341 llapi_error(LLAPI_MSG_ERROR, rc,
1342 "Error reading pool list for '%s'", name);
1344 } else if ((rc == 0) && (cookie == NULL)) {
1345 /* end of directory */
1349 /* ignore . and .. */
1350 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1353 /* check output bounds */
1354 if (nb_entries >= list_size) {
1359 /* +2 for '.' and final '\0' */
1360 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1366 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1367 poollist[nb_entries] = buffer + used;
1368 used += strlen(pool.d_name) + strlen(fsname) + 2;
1374 return ((rc != 0) ? rc : nb_entries);
1377 /* wrapper for lfs.c and obd.c */
1378 int llapi_poollist(const char *name)
1380 /* list of pool names (assume that pool count is smaller
1382 char **list, *buffer = NULL, *path = NULL, *fsname = NULL;
1383 int obdcount, bufsize, rc, nb, i;
1384 char *poolname = NULL, *tmp = NULL, data[16];
1389 if (name[0] != '/') {
1390 fsname = strdup(name);
1394 poolname = strchr(fsname, '.');
1398 path = (char *) name;
1401 rc = get_param_obdvar(fsname, path, "lov", "numobd",
1402 data, sizeof(data));
1405 obdcount = atoi(data);
1407 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1408 * and also an array to store the pointers for all that
1409 * allocated space. */
1411 bufsize = sizeof(struct obd_uuid) * obdcount;
1412 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1413 if (buffer == NULL) {
1417 list = (char **) (buffer + bufsize);
1420 /* name is a path or fsname */
1421 nb = llapi_get_poollist(name, list, obdcount,
1424 /* name is a pool name (<fsname>.<poolname>) */
1425 nb = llapi_get_poolmembers(name, list, obdcount,
1429 if (nb == -EOVERFLOW) {
1432 goto retry_get_pools;
1435 for (i = 0; i < nb; i++)
1436 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1437 rc = (nb < 0 ? nb : 0);
1446 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1447 void *data, struct dirent64 *de);
1449 #define OBD_NOT_FOUND (-1)
1451 static int common_param_init(struct find_param *param, char *path)
1453 int lumlen = get_mds_md_size(path);
1455 if (lumlen < PATH_MAX + 1)
1456 lumlen = PATH_MAX + 1;
1458 param->lumlen = lumlen;
1459 param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
1460 if (param->lmd == NULL) {
1461 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1462 "error: allocation of %zu bytes for ioctl",
1463 sizeof(lstat_t) + param->lumlen);
1467 param->fp_lmv_count = 256;
1468 param->fp_lmv_md = malloc(lmv_user_md_size(256, LMV_MAGIC_V1));
1469 if (param->fp_lmv_md == NULL) {
1470 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1471 "error: allocation of %d bytes for ioctl",
1472 lmv_user_md_size(256, LMV_MAGIC_V1));
1476 param->got_uuids = 0;
1477 param->obdindexes = NULL;
1478 param->obdindex = OBD_NOT_FOUND;
1479 if (!param->migrate)
1480 param->mdtindex = OBD_NOT_FOUND;
1484 static void find_param_fini(struct find_param *param)
1486 if (param->obdindexes)
1487 free(param->obdindexes);
1492 if (param->fp_lmv_md)
1493 free(param->fp_lmv_md);
1496 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1497 struct dirent64 *de)
1499 struct find_param *param = data;
1505 /* set errno upon failure */
1506 static DIR *opendir_parent(char *path)
1512 fname = strrchr(path, '/');
1514 return opendir(".");
1518 parent = opendir(path);
1523 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1525 struct lmv_user_md *lmv = (struct lmv_user_md *)param->fp_lmv_md;
1528 lmv->lum_stripe_count = param->fp_lmv_count;
1529 if (param->get_default_lmv)
1530 lmv->lum_magic = LMV_USER_MAGIC;
1532 lmv->lum_magic = LMV_MAGIC_V1;
1533 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, lmv);
1538 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1539 struct lov_user_mds_data *lmd, int lumlen)
1541 lstat_t *st = &lmd->lmd_st;
1544 if (parent == NULL && dir == NULL)
1548 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1549 } else if (parent) {
1550 char *fname = strrchr(path, '/');
1552 fname = (fname == NULL ? path : fname + 1);
1553 /* retrieve needed file info */
1554 strlcpy((char *)lmd, fname, lumlen);
1555 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1559 if (errno == ENOTTY) {
1560 /* ioctl is not supported, it is not a lustre fs.
1561 * Do the regular lstat(2) instead. */
1562 ret = lstat_f(path, st);
1565 llapi_error(LLAPI_MSG_ERROR, ret,
1566 "error: %s: lstat failed for %s",
1569 } else if (errno == ENOENT) {
1571 llapi_error(LLAPI_MSG_WARN, ret,
1572 "warning: %s: %s does not exist",
1574 } else if (errno != EISDIR) {
1576 llapi_error(LLAPI_MSG_ERROR, ret,
1577 "%s ioctl failed for %s.",
1578 dir ? "LL_IOC_MDC_GETINFO" :
1579 "IOC_MDC_GETFILEINFO", path);
1582 llapi_error(LLAPI_MSG_ERROR, ret,
1583 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1590 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1591 semantic_func_t sem_init,
1592 semantic_func_t sem_fini, void *data,
1593 struct dirent64 *de)
1595 struct find_param *param = (struct find_param *)data;
1596 struct dirent64 *dent;
1604 if (!d && errno != ENOTDIR) {
1606 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1609 } else if (!d && !parent) {
1610 /* ENOTDIR. Open the parent dir. */
1611 p = opendir_parent(path);
1618 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1624 while ((dent = readdir64(d)) != NULL) {
1627 param->have_fileinfo = 0;
1629 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1632 /* Don't traverse .lustre directory */
1633 if (!(strcmp(dent->d_name, dot_lustre_name)))
1637 if ((len + dent->d_reclen + 2) > size) {
1638 llapi_err_noerrno(LLAPI_MSG_ERROR,
1639 "error: %s: string buffer is too small",
1644 strcat(path, dent->d_name);
1646 if (dent->d_type == DT_UNKNOWN) {
1647 lstat_t *st = ¶m->lmd->lmd_st;
1649 rc = get_lmd_info(path, d, NULL, param->lmd,
1652 dent->d_type = IFTODT(st->st_mode);
1659 switch (dent->d_type) {
1661 llapi_err_noerrno(LLAPI_MSG_ERROR,
1662 "error: %s: '%s' is UNKNOWN type %d",
1663 __func__, dent->d_name, dent->d_type);
1666 rc = llapi_semantic_traverse(path, size, d, sem_init,
1667 sem_fini, data, dent);
1668 if (rc != 0 && ret == 0)
1674 rc = sem_init(path, d, NULL, data, dent);
1675 if (rc < 0 && ret == 0)
1678 if (sem_fini && rc == 0)
1679 sem_fini(path, d, NULL, data, dent);
1687 sem_fini(path, parent, &d, data, de);
1696 static int param_callback(char *path, semantic_func_t sem_init,
1697 semantic_func_t sem_fini, struct find_param *param)
1699 int ret, len = strlen(path);
1702 if (len > PATH_MAX) {
1704 llapi_error(LLAPI_MSG_ERROR, ret,
1705 "Path name '%s' is too long", path);
1709 buf = (char *)malloc(PATH_MAX + 1);
1713 strlcpy(buf, path, PATH_MAX + 1);
1714 ret = common_param_init(param, buf);
1718 param->fp_depth = 0;
1720 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1721 sem_fini, param, NULL);
1723 find_param_fini(param);
1725 return ret < 0 ? ret : 0;
1728 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1730 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1733 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1738 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1740 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1743 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1748 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1752 fd = open(path, O_RDONLY);
1755 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1759 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1765 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1769 fd = open(path, O_RDONLY);
1772 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1776 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1787 * If uuidp is NULL, return the number of available obd uuids.
1788 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1789 * there are more OSTs then allocated to uuidp, then an error is returned with
1790 * the ost_count set to number of available obd uuids.
1792 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1793 int *ost_count, enum tgt_type type)
1795 struct obd_uuid name;
1799 int rc = 0, index = 0;
1801 /* Get the lov name */
1802 if (type == LOV_TYPE) {
1803 rc = llapi_file_fget_lov_uuid(fd, &name);
1807 rc = llapi_file_fget_lmv_uuid(fd, &name);
1812 /* Now get the ost uuids from /proc */
1813 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1814 type == LOV_TYPE ? "lov" : "lmv", name.uuid);
1815 fp = fopen(buf, "r");
1818 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1822 snprintf(format, sizeof(format),
1823 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
1824 while (fgets(buf, sizeof(buf), fp) != NULL) {
1825 if (uuidp && (index < *ost_count)) {
1826 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
1834 if (uuidp && (index > *ost_count))
1841 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1843 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1846 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1851 root = opendir(mnt);
1854 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1859 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1867 /* Check if user specified value matches a real uuid. Ignore _UUID,
1868 * -osc-4ba41334, other trailing gunk in comparison.
1869 * @param real_uuid ends in "_UUID"
1870 * @param search_uuid may or may not end in "_UUID"
1872 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1874 int cmplen = strlen(real_uuid);
1875 int searchlen = strlen(search_uuid);
1877 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1879 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1882 /* The UUIDs may legitimately be different lengths, if
1883 * the system was upgraded from an older version. */
1884 if (cmplen != searchlen)
1887 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1890 /* Here, param->obduuid points to a single obduuid, the index of which is
1891 * returned in param->obdindex */
1892 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1894 struct obd_uuid obd_uuid;
1900 if (param->got_uuids)
1903 /* Get the lov/lmv name */
1905 rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
1907 rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
1909 if (rc != -ENOTTY) {
1910 llapi_error(LLAPI_MSG_ERROR, rc,
1911 "error: can't get lov name: %s", dname);
1918 param->got_uuids = 1;
1920 /* Now get the ost uuids from /proc */
1921 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1922 param->get_lmv ? "lmv" : "lov", obd_uuid.uuid);
1923 fp = fopen(buf, "r");
1926 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1930 if (!param->obduuid && !param->quiet && !param->obds_printed)
1931 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1932 param->get_lmv ? "MDTS" : "OBDS:");
1934 snprintf(format, sizeof(format),
1935 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
1936 while (fgets(buf, sizeof(buf), fp) != NULL) {
1939 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
1942 if (param->obduuid) {
1943 if (llapi_uuid_match(obd_uuid.uuid,
1944 param->obduuid->uuid)) {
1945 param->obdindex = index;
1948 } else if (!param->quiet && !param->obds_printed) {
1949 /* Print everything */
1950 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1953 param->obds_printed = 1;
1957 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1958 llapi_err_noerrno(LLAPI_MSG_ERROR,
1959 "error: %s: unknown obduuid: %s",
1960 __func__, param->obduuid->uuid);
1967 /* In this case, param->obduuid will be an array of obduuids and
1968 * obd index for all these obduuids will be returned in
1969 * param->obdindexes */
1970 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1971 int num_obds, int **obdindexes, int *obdindex,
1974 int ret, obdcount, obd_valid = 0, obdnum;
1976 struct obd_uuid *uuids = NULL;
1980 if (type == LOV_TYPE)
1981 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
1983 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
1987 obdcount = atoi(buf);
1988 uuids = (struct obd_uuid *)malloc(obdcount *
1989 sizeof(struct obd_uuid));
1994 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1996 struct obd_uuid *uuids_temp;
1998 if (ret == -EOVERFLOW) {
1999 uuids_temp = realloc(uuids, obdcount *
2000 sizeof(struct obd_uuid));
2001 if (uuids_temp != NULL) {
2003 goto retry_get_uuids;
2009 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
2013 indexes = malloc(num_obds * sizeof(*obdindex));
2014 if (indexes == NULL) {
2019 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2022 /* The user may have specified a simple index */
2023 i = strtol(obduuids[obdnum].uuid, &end, 0);
2024 if (end && *end == '\0' && i < obdcount) {
2025 indexes[obdnum] = i;
2028 for (i = 0; i < obdcount; i++) {
2029 if (llapi_uuid_match(uuids[i].uuid,
2030 obduuids[obdnum].uuid)) {
2031 indexes[obdnum] = i;
2037 if (i >= obdcount) {
2038 indexes[obdnum] = OBD_NOT_FOUND;
2039 llapi_err_noerrno(LLAPI_MSG_ERROR,
2040 "error: %s: unknown obduuid: %s",
2041 __func__, obduuids[obdnum].uuid);
2047 *obdindex = OBD_NOT_FOUND;
2049 *obdindex = obd_valid;
2051 *obdindexes = indexes;
2059 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2063 if (param->mdtuuid) {
2064 ret = setup_indexes(dir, path, param->mdtuuid, param->num_mdts,
2065 ¶m->mdtindexes, ¶m->mdtindex, LMV_TYPE);
2069 if (param->obduuid) {
2070 ret = setup_indexes(dir, path, param->obduuid, param->num_obds,
2071 ¶m->obdindexes, ¶m->obdindex, LOV_TYPE);
2075 param->got_uuids = 1;
2079 int llapi_ostlist(char *path, struct find_param *param)
2084 dir = opendir(path);
2088 ret = setup_obd_uuid(dir, path, param);
2095 * Given a filesystem name, or a pathname of a file on a lustre filesystem,
2096 * tries to determine the path to the filesystem's clilov directory under /proc
2098 * fsname is limited to MTI_NAME_MAXLEN in lustre_idl.h
2099 * The NUL terminator is compensated by the additional "%s" bytes. */
2100 #define LOV_LEN (sizeof("/proc/fs/lustre/lov/%s-clilov-*") + MTI_NAME_MAXLEN)
2101 static int clilovpath(const char *fsname, const char *const pathname,
2105 char pattern[LOV_LEN];
2106 char buffer[PATH_MAX + 1];
2108 if (fsname == NULL) {
2109 rc = llapi_search_fsname(pathname, buffer);
2115 snprintf(pattern, sizeof(pattern), "/proc/fs/lustre/lov/%s-clilov-*",
2118 rc = first_match(pattern, buffer);
2122 strlcpy(clilovpath, buffer, sizeof(buffer));
2128 * Given the path to a stripe attribute proc file, tries to open and
2129 * read the attribute and return the value using the attr parameter
2131 static int sattr_read_attr(const char *const fpath,
2136 char line[PATH_MAX + 1];
2139 f = fopen(fpath, "r");
2142 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open '%s'", fpath);
2146 if (fgets(line, sizeof(line), f) != NULL) {
2149 llapi_error(LLAPI_MSG_ERROR, errno, "Cannot read from '%s'", fpath);
2158 * Tries to determine the default stripe attributes for a given filesystem. The
2159 * filesystem to check should be specified by fsname, or will be determined
2162 static int sattr_get_defaults(const char *const fsname,
2163 const char *const pathname,
2164 unsigned int *scount,
2165 unsigned int *ssize,
2166 unsigned int *soffset)
2169 char dpath[PATH_MAX + 1];
2170 char fpath[PATH_MAX + 1];
2172 rc = clilovpath(fsname, pathname, dpath);
2177 snprintf(fpath, PATH_MAX, "%s/stripecount", dpath);
2178 rc = sattr_read_attr(fpath, scount);
2184 snprintf(fpath, PATH_MAX, "%s/stripesize", dpath);
2185 rc = sattr_read_attr(fpath, ssize);
2191 snprintf(fpath, PATH_MAX, "%s/stripeoffset", dpath);
2192 rc = sattr_read_attr(fpath, soffset);
2201 * Tries to gather the default stripe attributes for a given filesystem. If
2202 * the attributes can be determined, they are cached for easy retreival the
2203 * next time they are needed. Only a single filesystem's attributes are
2206 static int sattr_cache_get_defaults(const char *const fsname,
2207 const char *const pathname,
2208 unsigned int *scount,
2209 unsigned int *ssize,
2210 unsigned int *soffset)
2213 char fsname[PATH_MAX + 1];
2214 unsigned int stripecount;
2215 unsigned int stripesize;
2216 unsigned int stripeoffset;
2222 char fsname_buf[PATH_MAX + 1];
2223 unsigned int tmp[3];
2225 if (fsname == NULL) {
2226 rc = llapi_search_fsname(pathname, fsname_buf);
2230 strlcpy(fsname_buf, fsname, sizeof(fsname_buf));
2233 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2235 * Ensure all 3 sattrs (count, size, and offset) are
2236 * successfully retrieved and stored in tmp before writing to
2239 rc = sattr_get_defaults(fsname_buf, NULL, &tmp[0], &tmp[1],
2244 cache.stripecount = tmp[0];
2245 cache.stripesize = tmp[1];
2246 cache.stripeoffset = tmp[2];
2247 strlcpy(cache.fsname, fsname_buf, sizeof(cache.fsname));
2251 *scount = cache.stripecount;
2253 *ssize = cache.stripesize;
2255 *soffset = cache.stripeoffset;
2260 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2261 struct lov_user_ost_data_v1 *objects,
2262 int is_dir, int verbose, int depth,
2263 int raw, char *pool_name)
2265 char *prefix = is_dir ? "" : "lmm_";
2266 char *seperator = "";
2269 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2270 lmm_oi_set_seq(&lum->lmm_oi, 0);
2271 if (verbose & VERBOSE_DETAIL)
2272 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
2275 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2276 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2278 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2279 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
2281 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
2282 lmm_oi_seq(&lum->lmm_oi));
2283 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
2284 lmm_oi_id(&lum->lmm_oi));
2287 if (verbose & VERBOSE_COUNT) {
2288 if (verbose & ~VERBOSE_COUNT)
2289 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
2292 if (!raw && lum->lmm_stripe_count == 0) {
2293 unsigned int scount;
2294 rc = sattr_cache_get_defaults(NULL, path,
2298 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2301 llapi_error(LLAPI_MSG_ERROR, rc,
2302 "Cannot determine default"
2305 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2306 lum->lmm_stripe_count ==
2307 (typeof(lum->lmm_stripe_count))(-1)
2308 ? -1 : lum->lmm_stripe_count);
2311 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2312 (__s16)lum->lmm_stripe_count);
2314 seperator = is_dir ? " " : "\n";
2317 if (verbose & VERBOSE_SIZE) {
2318 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2319 if (verbose & ~VERBOSE_SIZE)
2320 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
2322 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
2324 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2327 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2329 llapi_error(LLAPI_MSG_ERROR, rc,
2330 "Cannot determine default"
2333 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2334 lum->lmm_stripe_size);
2336 seperator = is_dir ? " " : "\n";
2339 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2340 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2341 if (verbose & ~VERBOSE_LAYOUT)
2342 llapi_printf(LLAPI_MSG_NORMAL, "%spattern: ",
2344 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2348 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2349 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2350 if (verbose & ~VERBOSE_GENERATION)
2351 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
2353 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2354 (int)lum->lmm_layout_gen);
2358 if (verbose & VERBOSE_OFFSET) {
2359 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2360 if (verbose & ~VERBOSE_OFFSET)
2361 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
2364 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2365 lum->lmm_stripe_offset ==
2366 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2367 lum->lmm_stripe_offset);
2369 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2370 objects[0].l_ost_idx);
2371 seperator = is_dir ? " " : "\n";
2374 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
2375 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2376 if (verbose & ~VERBOSE_POOL)
2377 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
2379 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2382 if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
2383 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2386 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2387 struct lov_user_ost_data_v1 *objects,
2388 char *path, int is_dir, int obdindex,
2389 int depth, int header, int raw)
2391 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2394 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2395 if (obdindex == objects[i].l_ost_idx) {
2403 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
2404 depth, raw, pool_name);
2406 if (!is_dir && (header & VERBOSE_OBJID) &&
2407 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2409 llapi_printf(LLAPI_MSG_NORMAL,
2410 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2412 for (i = 0; i < lum->lmm_stripe_count; i++) {
2413 int idx = objects[i].l_ost_idx;
2414 long long oid = ostid_id(&objects[i].l_ost_oi);
2415 long long gr = ostid_seq(&objects[i].l_ost_oi);
2416 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) {
2418 sprintf(fmt, "%s%s%s\n",
2419 "\t%6u\t%14llu\t%#13llx\t",
2420 (fid_seq_is_rsvd(gr) ||
2421 fid_seq_is_mdt0(gr)) ?
2422 "%14llu" : "%#14llx", "%s");
2423 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2425 obdindex == idx ? " *" : "");
2429 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2433 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2434 char *path, int obdindex, int depth, int verbose)
2436 struct lmv_user_mds_data *objects = lum->lum_objects;
2437 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2438 int i, obdstripe = 0;
2439 char *separator = "";
2441 if (obdindex != OBD_NOT_FOUND) {
2442 if (lum->lum_stripe_count == 0) {
2443 if (obdindex == lum->lum_stripe_offset)
2446 for (i = 0; i < lum->lum_stripe_count; i++) {
2447 if (obdindex == objects[i].lum_mds) {
2448 llapi_printf(LLAPI_MSG_NORMAL,
2463 /* show all information default */
2465 if (lum->lum_magic == LMV_USER_MAGIC)
2466 verbose = VERBOSE_POOL | VERBOSE_COUNT | VERBOSE_OFFSET;
2468 verbose = VERBOSE_OBJID;
2471 if (depth && path && ((verbose != VERBOSE_OBJID)))
2472 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2474 if (verbose & VERBOSE_COUNT) {
2475 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2476 if (verbose & ~VERBOSE_COUNT)
2477 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2478 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2479 (int)lum->lum_stripe_count);
2480 if (verbose & VERBOSE_OFFSET)
2486 if (verbose & VERBOSE_OFFSET) {
2487 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2488 if (verbose & ~VERBOSE_OFFSET)
2489 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2490 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2491 (int)lum->lum_stripe_offset);
2495 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2496 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2497 if (obdstripe == 1 && lum->lum_stripe_count > 0)
2498 llapi_printf(LLAPI_MSG_NORMAL,
2499 "mdtidx\t\t FID[seq:oid:ver]\n");
2500 for (i = 0; i < lum->lum_stripe_count; i++) {
2501 int idx = objects[i].lum_mds;
2502 struct lu_fid *fid = &objects[i].lum_fid;
2503 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2504 llapi_printf(LLAPI_MSG_NORMAL,
2505 "%6u\t\t "DFID"\t\t%s\n",
2507 obdindex == idx ? " *" : "");
2512 if ((verbose & VERBOSE_POOL) && (pool_name[0] != '\0')) {
2513 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2514 if (verbose & ~VERBOSE_POOL)
2515 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2517 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2521 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
2522 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2525 void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
2529 if (param->get_lmv || param->get_default_lmv)
2530 magic = (__u32)param->fp_lmv_md->lum_magic;
2532 magic = *(__u32 *)¶m->lmd->lmd_lmm; /* lum->lmm_magic */
2535 case LOV_USER_MAGIC_V1:
2536 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
2537 param->lmd->lmd_lmm.lmm_objects,
2539 param->obdindex, param->fp_max_depth,
2540 param->verbose, param->raw);
2542 case LOV_USER_MAGIC_V3: {
2543 char pool_name[LOV_MAXPOOLNAME + 1];
2544 struct lov_user_ost_data_v1 *objects;
2545 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2547 strlcpy(pool_name, lmmv3->lmm_pool_name, sizeof(pool_name));
2548 objects = lmmv3->lmm_objects;
2549 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
2550 objects, path, is_dir,
2551 param->obdindex, param->fp_max_depth,
2552 param->verbose, param->raw);
2556 case LMV_USER_MAGIC: {
2557 char pool_name[LOV_MAXPOOLNAME + 1];
2558 struct lmv_user_md *lum;
2560 lum = (struct lmv_user_md *)param->fp_lmv_md;
2561 strlcpy(pool_name, lum->lum_pool_name, sizeof(pool_name));
2562 lmv_dump_user_lmm(lum, pool_name, path,
2563 param->obdindex, param->fp_max_depth,
2568 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2569 "(expecting one of %#x %#x %#x %#x)\n",
2570 *(__u32 *)¶m->lmd->lmd_lmm,
2571 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2572 LMV_USER_MAGIC, LMV_MAGIC_V1);
2577 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2583 fname = strrchr(path, '/');
2585 /* It should be a file (or other non-directory) */
2586 if (fname == NULL) {
2587 dname = (char *)malloc(2);
2591 fname = (char *)path;
2593 dname = (char *)malloc(fname - path + 1);
2596 strncpy(dname, path, fname - path);
2597 dname[fname - path] = '\0';
2601 fd = open(dname, O_RDONLY);
2608 strcpy((char *)lum, fname);
2609 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2612 if (close(fd) == -1 && rc == 0)
2619 int llapi_file_lookup(int dirfd, const char *name)
2621 struct obd_ioctl_data data = { 0 };
2626 if (dirfd < 0 || name == NULL)
2629 data.ioc_version = OBD_IOCTL_VERSION;
2630 data.ioc_len = sizeof(data);
2631 data.ioc_inlbuf1 = (char *)name;
2632 data.ioc_inllen1 = strlen(name) + 1;
2634 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2636 llapi_error(LLAPI_MSG_ERROR, rc,
2637 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2642 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2648 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2649 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2651 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2652 * The table below gives the answers for the specified parameters (value and
2653 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2654 * --------------------------------------
2655 * 1 | file > limit; sign > 0 | -1 / -1 |
2656 * 2 | file = limit; sign > 0 | -1 / -1 |
2657 * 3 | file < limit; sign > 0 | ? / 1 |
2658 * 4 | file > limit; sign = 0 | -1 / -1 |
2659 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2660 * 6 | file < limit; sign = 0 | ? / -1 |
2661 * 7 | file > limit; sign < 0 | 1 / 1 |
2662 * 8 | file = limit; sign < 0 | ? / -1 |
2663 * 9 | file < limit; sign < 0 | ? / -1 |
2664 * --------------------------------------
2665 * Note: 5th actually means that the value is within the interval
2666 * (limit - margin, limit]. */
2667 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2668 int sign, int negopt, unsigned long long margin,
2674 /* Drop the fraction of margin (of days). */
2675 if (file + margin <= limit)
2677 } else if (sign == 0) {
2678 if (file <= limit && file + margin > limit)
2680 else if (file + margin <= limit)
2682 } else if (sign < 0) {
2689 return negopt ? ~ret + 1 : ret;
2692 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2693 * Return -1 or 1 if file timestamp does not or does match the given criteria
2694 * correspondingly. Return 0 if the MDS time is being checked and there are
2695 * attributes on OSTs and it is not yet clear if the timespamp matches.
2697 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2698 * updated timestamps. */
2699 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2704 /* Check if file is accepted. */
2705 if (param->fp_atime) {
2706 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
2707 param->fp_asign, param->fp_exclude_atime,
2714 if (param->fp_mtime) {
2715 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
2716 param->fp_msign, param->fp_exclude_mtime,
2721 /* If the previous check matches, but this one is not yet clear,
2722 * we should return 0 to do an RPC on OSTs. */
2727 if (param->fp_ctime) {
2728 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
2729 param->fp_csign, param->fp_exclude_ctime,
2734 /* If the previous check matches, but this one is not yet clear,
2735 * we should return 0 to do an RPC on OSTs. */
2744 * Check whether the stripes matches the indexes user provided
2748 static int check_obd_match(struct find_param *param)
2750 lstat_t *st = ¶m->lmd->lmd_st;
2751 struct lov_user_ost_data_v1 *lmm_objects;
2754 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
2757 if (!S_ISREG(st->st_mode))
2760 /* Only those files should be accepted, which have a
2761 * stripe on the specified OST. */
2762 if (!param->lmd->lmd_lmm.lmm_stripe_count)
2765 if (param->lmd->lmd_lmm.lmm_magic ==
2766 LOV_USER_MAGIC_V3) {
2767 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2769 lmm_objects = lmmv3->lmm_objects;
2770 } else if (param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2771 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
2773 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2774 __func__, param->lmd->lmd_lmm.lmm_magic);
2778 for (i = 0; i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
2779 for (j = 0; j < param->num_obds; j++) {
2780 if (param->obdindexes[j] ==
2781 lmm_objects[i].l_ost_idx) {
2782 if (param->exclude_obd)
2789 if (param->exclude_obd)
2794 static int check_mdt_match(struct find_param *param)
2798 if (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND)
2801 /* FIXME: For striped dir, we should get stripe information and check */
2802 for (i = 0; i < param->num_mdts; i++) {
2803 if (param->mdtindexes[i] == param->file_mdtindex)
2804 return !param->exclude_mdt;
2807 if (param->exclude_mdt)
2813 * Check whether the obd is active or not, if it is
2814 * not active, just print the object affected by this
2817 static int print_failed_tgt(struct find_param *param, char *path, int type)
2819 struct obd_statfs stat_buf;
2820 struct obd_uuid uuid_buf;
2823 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2825 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2826 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2827 ret = llapi_obd_statfs(path, type,
2828 param->obdindex, &stat_buf,
2831 llapi_printf(LLAPI_MSG_NORMAL,
2832 "obd_uuid: %s failed %s ",
2833 param->obduuid->uuid,
2839 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
2840 void *data, struct dirent64 *de)
2842 struct find_param *param = (struct find_param *)data;
2843 DIR *dir = dirp == NULL ? NULL : *dirp;
2844 int decision = 1; /* 1 is accepted; -1 is rejected. */
2845 lstat_t *st = ¶m->lmd->lmd_st;
2847 int checked_type = 0;
2850 LASSERT(parent != NULL || dir != NULL);
2852 if (param->have_fileinfo == 0)
2853 param->lmd->lmd_lmm.lmm_stripe_count = 0;
2855 /* If a regular expression is presented, make the initial decision */
2856 if (param->pattern != NULL) {
2857 char *fname = strrchr(path, '/');
2858 fname = (fname == NULL ? path : fname + 1);
2859 ret = fnmatch(param->pattern, fname, 0);
2860 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
2861 (ret == 0 && param->exclude_pattern))
2865 /* See if we can check the file type from the dirent. */
2866 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
2869 if (DTTOIF(de->d_type) == param->fp_type) {
2870 if (param->fp_exclude_type)
2873 if (!param->fp_exclude_type)
2880 /* Request MDS for the stat info if some of these parameters need
2881 * to be compared. */
2882 if (param->obduuid || param->mdtuuid ||
2883 param->fp_check_uid || param->fp_check_gid ||
2884 param->fp_atime || param->fp_mtime || param->fp_ctime ||
2885 param->check_pool || param->check_size ||
2886 param->check_stripecount || param->check_stripesize ||
2887 param->check_layout)
2890 if (param->fp_type != 0 && checked_type == 0)
2893 if (param->have_fileinfo == 0 && decision == 0) {
2894 ret = get_lmd_info(path, parent, dir, param->lmd,
2898 ret = llapi_file_fget_mdtidx(dirfd(dir),
2899 ¶m->file_mdtindex);
2904 ret = lstat_f(path, &tmp_st);
2907 llapi_error(LLAPI_MSG_ERROR, ret,
2908 "error: %s: lstat failed"
2909 "for %s", __func__, path);
2912 if (S_ISREG(tmp_st.st_mode)) {
2913 fd = open(path, O_RDONLY);
2915 ret = llapi_file_fget_mdtidx(fd,
2916 ¶m->file_mdtindex);
2922 /* For special inode, it assumes to
2923 * reside on the same MDT with the
2926 ret = llapi_file_fget_mdtidx(fd,
2927 ¶m->file_mdtindex);
2940 if (param->fp_type && !checked_type) {
2941 if ((st->st_mode & S_IFMT) == param->fp_type) {
2942 if (param->fp_exclude_type)
2945 if (!param->fp_exclude_type)
2951 if (param->obduuid || param->mdtuuid) {
2952 if (lustre_fs && param->got_uuids &&
2953 param->fp_dev != st->st_dev) {
2954 /* A lustre/lustre mount point is crossed. */
2955 param->got_uuids = 0;
2956 param->obds_printed = 0;
2957 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2960 if (lustre_fs && !param->got_uuids) {
2961 ret = setup_target_indexes(dir ? dir : parent, path,
2966 param->fp_dev = st->st_dev;
2967 } else if (!lustre_fs && param->got_uuids) {
2968 /* A lustre/non-lustre mount point is crossed. */
2969 param->got_uuids = 0;
2970 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2974 if (param->check_stripesize) {
2975 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_size,
2977 param->stripesize_sign,
2978 param->exclude_stripesize,
2979 param->stripesize_units, 0);
2984 if (param->check_stripecount) {
2985 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_count,
2987 param->stripecount_sign,
2988 param->exclude_stripecount, 1, 0);
2993 if (param->check_layout) {
2996 found = (param->lmd->lmd_lmm.lmm_pattern & param->layout);
2997 if ((param->lmd->lmd_lmm.lmm_pattern == 0xFFFFFFFF) ||
2998 (found && param->exclude_layout) ||
2999 (!found && !param->exclude_layout)) {
3005 /* If an OBD UUID is specified but none matches, skip this file. */
3006 if ((param->obduuid && param->obdindex == OBD_NOT_FOUND) ||
3007 (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND))
3010 /* If a OST or MDT UUID is given, and some OST matches,
3012 if (param->obdindex != OBD_NOT_FOUND ||
3013 param->mdtindex != OBD_NOT_FOUND) {
3014 if (param->obduuid) {
3015 if (check_obd_match(param)) {
3016 /* If no mdtuuid is given, we are done.
3017 * Otherwise, fall through to the mdtuuid
3019 if (!param->mdtuuid)
3025 if (param->mdtuuid) {
3026 if (check_mdt_match(param))
3032 if (param->fp_check_uid) {
3033 if (st->st_uid == param->fp_uid) {
3034 if (param->fp_exclude_uid)
3037 if (!param->fp_exclude_uid)
3042 if (param->fp_check_gid) {
3043 if (st->st_gid == param->fp_gid) {
3044 if (param->fp_exclude_gid)
3047 if (!param->fp_exclude_gid)
3052 if (param->check_pool) {
3053 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
3055 /* empty requested pool is taken as no pool search => V1 */
3056 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
3057 (param->poolname[0] == '\0')) ||
3058 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3059 (strncmp(lmmv3->lmm_pool_name,
3060 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
3061 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3062 (strcmp(param->poolname, "*") == 0))) {
3063 if (param->exclude_pool)
3066 if (!param->exclude_pool)
3071 /* Check the time on mds. */
3073 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
3076 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
3077 param->lmd->lmd_lmm.lmm_stripe_count)
3079 decision = find_time_check(st, param, for_mds);
3084 /* If file still fits the request, ask ost for updated info.
3085 The regular stat is almost of the same speed as some new
3086 'glimpse-size-ioctl'. */
3088 if (param->check_size && S_ISREG(st->st_mode) &&
3089 param->lmd->lmd_lmm.lmm_stripe_count)
3093 /* For regular files with the stripe the decision may have not
3094 * been taken yet if *time or size is to be checked. */
3095 LASSERT((S_ISREG(st->st_mode) &&
3096 param->lmd->lmd_lmm.lmm_stripe_count) ||
3097 param->mdtindex != OBD_NOT_FOUND);
3099 if (param->obdindex != OBD_NOT_FOUND)
3100 print_failed_tgt(param, path, LL_STATFS_LOV);
3102 if (param->mdtindex != OBD_NOT_FOUND)
3103 print_failed_tgt(param, path, LL_STATFS_LMV);
3106 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
3107 (void *)param->lmd);
3108 } else if (parent) {
3109 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
3110 (void *)param->lmd);
3114 if (errno == ENOENT) {
3115 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
3116 "warning: %s: %s does not exist",
3121 llapi_error(LLAPI_MSG_ERROR, ret,
3122 "%s: IOC_LOV_GETINFO on %s failed",
3128 /* Check the time on osc. */
3129 decision = find_time_check(st, param, 0);
3136 if (param->check_size)