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/types.h>
60 #include <sys/syscall.h>
61 #include <sys/xattr.h>
64 #include <libgen.h> /* for dirname() */
65 #ifdef HAVE_LINUX_UNISTD_H
66 #include <linux/unistd.h>
72 #include <libcfs/libcfs.h>
73 #include <lnet/lnetctl.h>
74 #include <lustre/lustreapi.h>
75 #include <lustre_ioctl.h>
76 #include "lustreapi_internal.h"
78 static int llapi_msg_level = LLAPI_MSG_MAX;
80 void llapi_msg_set_level(int level)
82 /* ensure level is in the good range */
83 if (level < LLAPI_MSG_OFF)
84 llapi_msg_level = LLAPI_MSG_OFF;
85 else if (level > LLAPI_MSG_MAX)
86 llapi_msg_level = LLAPI_MSG_MAX;
88 llapi_msg_level = level;
91 static void error_callback_default(enum llapi_message_level level, int err,
92 const char *fmt, va_list ap)
94 vfprintf(stderr, fmt, ap);
95 if (level & LLAPI_MSG_NO_ERRNO)
96 fprintf(stderr, "\n");
98 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
101 static void info_callback_default(enum llapi_message_level level, int err,
102 const char *fmt, va_list ap)
104 vfprintf(stdout, fmt, ap);
107 static llapi_log_callback_t llapi_error_callback = error_callback_default;
108 static llapi_log_callback_t llapi_info_callback = info_callback_default;
111 /* llapi_error will preserve errno */
112 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
115 int tmp_errno = errno;
117 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
121 llapi_error_callback(level, abs(err), fmt, args);
126 /* llapi_printf will preserve errno */
127 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
130 int tmp_errno = errno;
132 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
136 llapi_info_callback(level, 0, fmt, args);
142 * Set a custom error logging function. Passing in NULL will reset the logging
143 * callback to its default value.
145 * This function returns the value of the old callback.
147 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
149 llapi_log_callback_t old = llapi_error_callback;
152 llapi_error_callback = cb;
154 llapi_error_callback = error_callback_default;
160 * Set a custom info logging function. Passing in NULL will reset the logging
161 * callback to its default value.
163 * This function returns the value of the old callback.
165 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
167 llapi_log_callback_t old = llapi_info_callback;
170 llapi_info_callback = cb;
172 llapi_info_callback = info_callback_default;
178 * size_units is to be initialized (or zeroed) by caller.
180 int llapi_parse_size(const char *optarg, unsigned long long *size,
181 unsigned long long *size_units, int bytes_spec)
185 if (strncmp(optarg, "-", 1) == 0)
188 if (*size_units == 0)
191 *size = strtoull(optarg, &end, 0);
194 if ((*end == 'b') && *(end + 1) == '\0' &&
195 (*size & (~0ULL << (64 - 9))) == 0 &&
197 *size_units = 1 << 9;
198 } else if ((*end == 'b') &&
199 *(end + 1) == '\0' &&
202 } else if ((*end == 'k' || *end == 'K') &&
203 *(end + 1) == '\0' &&
204 (*size & (~0ULL << (64 - 10))) == 0) {
205 *size_units = 1 << 10;
206 } else if ((*end == 'm' || *end == 'M') &&
207 *(end + 1) == '\0' &&
208 (*size & (~0ULL << (64 - 20))) == 0) {
209 *size_units = 1 << 20;
210 } else if ((*end == 'g' || *end == 'G') &&
211 *(end + 1) == '\0' &&
212 (*size & (~0ULL << (64 - 30))) == 0) {
213 *size_units = 1 << 30;
214 } else if ((*end == 't' || *end == 'T') &&
215 *(end + 1) == '\0' &&
216 (*size & (~0ULL << (64 - 40))) == 0) {
217 *size_units = 1ULL << 40;
218 } else if ((*end == 'p' || *end == 'P') &&
219 *(end + 1) == '\0' &&
220 (*size & (~0ULL << (64 - 50))) == 0) {
221 *size_units = 1ULL << 50;
222 } else if ((*end == 'e' || *end == 'E') &&
223 *(end + 1) == '\0' &&
224 (*size & (~0ULL << (64 - 60))) == 0) {
225 *size_units = 1ULL << 60;
230 *size *= *size_units;
234 /* XXX: llapi_xxx() functions return negative values upon failure */
236 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
237 int stripe_count, int stripe_pattern)
241 /* 64 KB is the largest common page size I'm aware of (on ia64), but
242 * check the local page size just in case. */
243 page_size = LOV_MIN_STRIPE_SIZE;
244 if (getpagesize() > page_size) {
245 page_size = getpagesize();
246 llapi_err_noerrno(LLAPI_MSG_WARN,
247 "warning: your page size (%u) is "
248 "larger than expected (%u)", page_size,
249 LOV_MIN_STRIPE_SIZE);
251 if ((stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
253 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
254 "must be an even multiple of %d bytes",
255 stripe_size, page_size);
258 if (stripe_offset < -1) {
260 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
264 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
266 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
270 if (stripe_size >= (1ULL << 32)) {
272 llapi_error(LLAPI_MSG_ERROR, rc,
273 "warning: stripe size 4G or larger "
274 "is not currently supported and would wrap");
280 /* return the first file matching this pattern */
281 static int first_match(char *pattern, char *buffer)
285 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
288 if (glob_info.gl_pathc < 1) {
289 globfree(&glob_info);
293 strcpy(buffer, glob_info.gl_pathv[0]);
295 globfree(&glob_info);
299 static int find_target_obdpath(char *fsname, char *path)
302 char pattern[PATH_MAX + 1];
305 snprintf(pattern, PATH_MAX,
306 "/proc/fs/lustre/lov/%s-*/target_obd",
308 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
309 if (rc == GLOB_NOMATCH)
314 strcpy(path, glob_info.gl_pathv[0]);
315 globfree(&glob_info);
319 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
322 char pattern[PATH_MAX + 1];
325 snprintf(pattern, PATH_MAX,
326 "/proc/fs/lustre/lov/%s-*/pools/%s",
328 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
329 /* If no pools, make sure the lov is available */
330 if ((rc == GLOB_NOMATCH) &&
331 (find_target_obdpath(fsname, poolpath) == -ENODEV))
336 strcpy(poolpath, glob_info.gl_pathv[0]);
337 globfree(&glob_info);
342 * Trim a trailing newline from a string, if it exists.
344 int llapi_chomp_string(char *buf)
360 * return a parameter string for a specific device type or mountpoint
362 * \param param_path the path to the file containing parameter data
363 * \param result buffer for parameter value string
364 * \param result_size size of buffer for return value
366 * The \param param_path is appended to /proc/{fs,sys}/{lnet,lustre} to
367 * complete the absolute path to the file containing the parameter data
368 * the user is requesting. If that file exist then the data is read from
369 * the file and placed into the \param result buffer that is passed by
370 * the user. Data is only copied up to the \param result_size to prevent
371 * overflow of the array.
373 * Return 0 for success, with a NUL-terminated string in \param result.
374 * Return -ve value for error.
376 int get_param(const char *param_path, char *result,
377 unsigned int result_size)
379 char file[PATH_MAX + 1], pattern[PATH_MAX + 1], buf[result_size];
383 snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
385 rc = first_match(pattern, file);
386 if (rc != 0 || result == NULL)
389 fp = fopen(file, "r");
391 while (fgets(buf, result_size, fp) != NULL)
400 #define DEVICES_LIST "/proc/fs/lustre/devices"
403 * return a parameter string for a specific device type or mountpoint
405 * \param fsname Lustre filesystem name (optional)
406 * \param file_path path to file in filesystem (optional, if fsname unset)
407 * \param obd_type Lustre OBD device type
408 * \param param_name parameter name to fetch
409 * \param value return buffer for parameter value string
410 * \param val_len size of buffer for return value
412 * If fsname is specified then the parameter will be from that filesystem
413 * (if it exists). If file_path is given and it is in a mounted Lustre
414 * filesystem, then the parameter will be otherwise the value may be
415 * from any mounted filesystem (if there is more than one).
417 * If "obd_type" matches a Lustre device then the first matching device
418 * (as with "lctl dl", constrained by \param fsname or \param mount_path)
419 * will be used to provide the return value, otherwise the first such
420 * device found will be used.
422 * Return 0 for success, with a NUL-terminated string in \param buffer.
423 * Return -ve value for error.
425 static int get_param_obdvar(const char *fsname, const char *file_path,
426 const char *obd_type, const char *param_name,
427 char *value, unsigned int val_len)
429 char devices[PATH_MAX];
430 char dev[PATH_MAX] = "*";
435 fp = fopen(DEVICES_LIST, "r");
438 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
442 if (fsname == NULL && file_path != NULL) {
443 rc = llapi_search_fsname(file_path, fs);
445 llapi_error(LLAPI_MSG_ERROR, rc,
446 "'%s' is not on a Lustre filesystem",
450 } else if (fsname != NULL) {
451 rc = strlcpy(fs, fsname, sizeof(fs));
452 if (rc >= sizeof(fs))
453 GOTO(out, rc = -E2BIG);
456 while (fgets(devices, sizeof(devices) - 1, fp) != NULL) {
457 char *bufp = devices, *tmp;
459 devices[sizeof(devices) - 1] = '\0';
460 while (bufp[0] == ' ')
463 tmp = strstr(bufp, obd_type);
465 tmp += strlen(obd_type) + 1;
468 rc = strlcpy(dev, tmp, sizeof(dev));
469 if (rc >= sizeof(dev))
470 GOTO(out, rc = -E2BIG);
471 tmp = strchr(dev, ' ');
478 if (dev[0] == '*' && strlen(fs)) {
479 rc = snprintf(dev, sizeof(dev), "%s-*", fs);
480 if (rc >= sizeof(dev))
481 GOTO(out, rc = -E2BIG);
483 rc = snprintf(devices, sizeof(devices), "%s/%s/%s", obd_type, dev,
485 if (rc >= sizeof(devices))
486 GOTO(out, rc = -E2BIG);
488 return get_param(devices, value, val_len);
496 * TYPE one of llite, lmv, lov.
497 * /proc/fs/lustre/TYPE/INST the directory of interest.
499 static int get_param_cli(const char *type, const char *inst,
500 const char *param, char *buf, size_t buf_size)
502 char param_path[PATH_MAX + 1];
503 FILE *param_file = NULL;
506 snprintf(param_path, sizeof(param_path),
507 "/proc/fs/lustre/%s/%s/%s", type, inst, param);
509 param_file = fopen(param_path, "r");
510 if (param_file == NULL) {
515 if (fgets(buf, buf_size, param_file) == NULL) {
522 if (param_file != NULL)
528 static int get_param_llite(const char *path,
529 const char *param, char *buf, size_t buf_size)
534 rc = llapi_getname(path, inst, sizeof(inst));
538 return get_param_cli("llite", inst, param, buf, buf_size);
541 static int get_param_lov(const char *path,
542 const char *param, char *buf, size_t buf_size)
544 struct obd_uuid uuid;
547 rc = llapi_file_get_lov_uuid(path, &uuid);
551 return get_param_cli("lov", uuid.uuid, param, buf, buf_size);
554 static int get_param_lmv(const char *path,
555 const char *param, char *buf, size_t buf_size)
557 struct obd_uuid uuid;
560 rc = llapi_file_get_lmv_uuid(path, &uuid);
564 return get_param_cli("lmv", uuid.uuid, param, buf, buf_size);
567 static int get_mds_md_size(const char *path)
569 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
573 /* Get the max ea size from llite proc. */
574 rc = get_param_llite(path, "max_easize", buf, sizeof(buf));
586 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
588 return get_param_lmv(path, "uuid", buf, bufsize);
592 * if pool is NULL, search ostname in target_obd
593 * if pool is not NULL:
594 * if pool not found returns errno < 0
595 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
596 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
598 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
601 char buffer[PATH_MAX + 1];
605 len = strlen(ostname);
607 if (poolname == NULL) {
611 rc = find_target_obdpath(fsname, buffer);
613 rc = find_poolpath(fsname, poolname, buffer);
618 fd = fopen(buffer, "r");
622 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
623 if (poolname == NULL) {
625 /* Search for an ostname in the list of OSTs
626 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
627 ptr = strchr(buffer, ' ');
629 (strncmp(ptr + 1, ostname, len) == 0)) {
634 /* Search for an ostname in a pool,
635 (or an existing non-empty pool if no ostname) */
636 if ((ostname == NULL) ||
637 (strncmp(buffer, ostname, len) == 0)) {
647 int llapi_file_open_pool(const char *name, int flags, int mode,
648 unsigned long long stripe_size, int stripe_offset,
649 int stripe_count, int stripe_pattern, char *pool_name)
651 struct lov_user_md_v3 lum = { 0 };
654 /* Make sure we have a good pool */
655 if (pool_name != NULL) {
656 char fsname[MAX_OBD_NAME + 1], *ptr;
658 rc = llapi_search_fsname(name, fsname);
660 llapi_error(LLAPI_MSG_ERROR, rc,
661 "'%s' is not on a Lustre filesystem",
666 /* in case user gives the full pool name <fsname>.<poolname>,
667 * strip the fsname */
668 ptr = strchr(pool_name, '.');
671 if (strcmp(pool_name, fsname) != 0) {
673 llapi_err_noerrno(LLAPI_MSG_ERROR,
674 "Pool '%s' is not on filesystem '%s'",
681 /* Make sure the pool exists and is non-empty */
682 rc = llapi_search_ost(fsname, pool_name, NULL);
684 llapi_err_noerrno(LLAPI_MSG_ERROR,
685 "pool '%s.%s' %s", fsname, pool_name,
686 rc == 0 ? "has no OSTs" : "does not exist");
692 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
694 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
695 flags = O_DIRECTORY | O_RDONLY;
702 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
706 rc = llapi_stripe_limit_check(stripe_size, stripe_offset, stripe_count,
711 /* Initialize IOCTL striping pattern structure */
712 lum.lmm_magic = LOV_USER_MAGIC_V3;
713 lum.lmm_pattern = stripe_pattern;
714 lum.lmm_stripe_size = stripe_size;
715 lum.lmm_stripe_count = stripe_count;
716 lum.lmm_stripe_offset = stripe_offset;
717 if (pool_name != NULL) {
718 strlcpy(lum.lmm_pool_name, pool_name,
719 sizeof(lum.lmm_pool_name));
721 /* If no pool is specified at all, use V1 request */
722 lum.lmm_magic = LOV_USER_MAGIC_V1;
725 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
726 char *errmsg = "stripe already set";
728 if (errno != EEXIST && errno != EALREADY)
729 errmsg = strerror(errno);
731 llapi_err_noerrno(LLAPI_MSG_ERROR,
732 "error on ioctl "LPX64" for '%s' (%d): %s",
733 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
744 int llapi_file_open(const char *name, int flags, int mode,
745 unsigned long long stripe_size, int stripe_offset,
746 int stripe_count, int stripe_pattern)
748 return llapi_file_open_pool(name, flags, mode, stripe_size,
749 stripe_offset, stripe_count,
750 stripe_pattern, NULL);
753 int llapi_file_create(const char *name, unsigned long long stripe_size,
754 int stripe_offset, int stripe_count, int stripe_pattern)
758 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
759 stripe_offset, stripe_count, stripe_pattern,
768 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
769 int stripe_offset, int stripe_count,
770 int stripe_pattern, char *pool_name)
774 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
775 stripe_offset, stripe_count, stripe_pattern,
784 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
785 int stripe_count, int stripe_pattern,
786 const char *pool_name)
788 struct lmv_user_md lum = { 0 };
792 lum.lum_magic = LMV_USER_MAGIC;
793 lum.lum_stripe_offset = stripe_offset;
794 lum.lum_stripe_count = stripe_count;
795 lum.lum_hash_type = stripe_pattern;
796 if (pool_name != NULL) {
797 if (strlen(pool_name) >= sizeof(lum.lum_pool_name)) {
798 llapi_err_noerrno(LLAPI_MSG_ERROR,
799 "error LL_IOC_LMV_SET_DEFAULT_STRIPE '%s'"
800 ": too large pool name: %s", name, pool_name);
803 strncpy(lum.lum_pool_name, pool_name,
804 sizeof(lum.lum_pool_name));
807 fd = open(name, O_DIRECTORY | O_RDONLY);
810 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
814 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lum);
816 char *errmsg = "stripe already set";
818 if (errno != EEXIST && errno != EALREADY)
819 errmsg = strerror(errno);
821 llapi_err_noerrno(LLAPI_MSG_ERROR,
822 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
829 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
830 int stripe_count, int stripe_pattern,
831 const char *pool_name)
833 struct lmv_user_md lmu = { 0 };
834 struct obd_ioctl_data data = { 0 };
837 char *dirpath = NULL;
838 char *namepath = NULL;
844 dirpath = strdup(name);
845 namepath = strdup(name);
846 if (!dirpath || !namepath)
849 lmu.lum_magic = LMV_USER_MAGIC;
850 lmu.lum_stripe_offset = stripe_offset;
851 lmu.lum_stripe_count = stripe_count;
852 lmu.lum_hash_type = stripe_pattern;
853 if (pool_name != NULL) {
854 if (strlen(pool_name) >= LOV_MAXPOOLNAME) {
855 llapi_err_noerrno(LLAPI_MSG_ERROR,
856 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
857 "pool name: %s", name, pool_name);
861 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
864 filename = basename(namepath);
865 dir = dirname(dirpath);
867 data.ioc_inlbuf1 = (char *)filename;
868 data.ioc_inllen1 = strlen(filename) + 1;
869 data.ioc_inlbuf2 = (char *)&lmu;
870 data.ioc_inllen2 = sizeof(struct lmv_user_md);
871 data.ioc_type = mode;
872 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
874 llapi_error(LLAPI_MSG_ERROR, rc,
875 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
880 fd = open(dir, O_DIRECTORY | O_RDONLY);
883 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
887 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
888 char *errmsg = "stripe already set";
890 if (errno != EEXIST && errno != EALREADY)
891 errmsg = strerror(errno);
893 llapi_err_noerrno(LLAPI_MSG_ERROR,
894 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
904 int llapi_direntry_remove(char *dname)
906 char *dirpath = NULL;
907 char *namepath = NULL;
913 dirpath = strdup(dname);
914 namepath = strdup(dname);
915 if (!dirpath || !namepath)
918 filename = basename(namepath);
920 dir = dirname(dirpath);
922 fd = open(dir, O_DIRECTORY | O_RDONLY);
925 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
930 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
931 char *errmsg = strerror(errno);
932 llapi_err_noerrno(LLAPI_MSG_ERROR,
933 "error on ioctl "LPX64" for '%s' (%d): %s",
934 (__u64)LL_IOC_LMV_SETSTRIPE, filename,
946 * Find the fsname, the full path, and/or an open fd.
947 * Either the fsname or path must not be NULL
949 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
952 char buf[PATH_MAX], mntdir[PATH_MAX];
955 int idx = 0, len = 0, mntlen, fd;
958 /* get the mount point */
959 fp = setmntent(MOUNTED, "r");
962 llapi_error(LLAPI_MSG_ERROR, rc,
963 "setmntent(%s) failed", MOUNTED);
967 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
970 if (!llapi_is_lustre_mnt(&mnt))
973 if ((want & WANT_INDEX) && (idx++ != index))
976 mntlen = strlen(mnt.mnt_dir);
977 ptr = strrchr(mnt.mnt_fsname, '/');
978 /* thanks to the call to llapi_is_lustre_mnt() above,
979 * we are sure that mnt.mnt_fsname contains ":/",
980 * so ptr should never be NULL */
985 /* Check the fsname for a match, if given */
986 if (!(want & WANT_FSNAME) && fsname != NULL &&
987 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
990 /* If the path isn't set return the first one we find */
991 if (path == NULL || strlen(path) == 0) {
992 strcpy(mntdir, mnt.mnt_dir);
993 if ((want & WANT_FSNAME) && fsname != NULL)
997 /* Otherwise find the longest matching path */
998 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
999 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1000 strcpy(mntdir, mnt.mnt_dir);
1002 if ((want & WANT_FSNAME) && fsname != NULL)
1003 strcpy(fsname, ptr);
1011 if ((want & WANT_PATH) && path != NULL)
1012 strcpy(path, mntdir);
1013 if (want & WANT_FD) {
1014 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1017 llapi_error(LLAPI_MSG_ERROR, rc,
1018 "error opening '%s'", mntdir);
1024 } else if (want & WANT_ERROR)
1025 llapi_err_noerrno(LLAPI_MSG_ERROR,
1026 "can't find fs root for '%s': %d",
1027 (want & WANT_PATH) ? fsname : path, rc);
1032 * search lustre mounts
1034 * Calling this function will return to the user the mount point, mntdir, and
1035 * the file system name, fsname, if the user passed a buffer to this routine.
1037 * The user inputs are pathname and index. If the pathname is supplied then
1038 * the value of the index will be ignored. The pathname will return data if
1039 * the pathname is located on a lustre mount. Index is used to pick which
1040 * mount point you want in the case of multiple mounted lustre file systems.
1041 * See function lfs_osts in lfs.c for a example of the index use.
1043 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1046 int want = WANT_PATH, idx = -1;
1048 if (!pathname || pathname[0] == '\0') {
1052 strcpy(mntdir, pathname);
1055 want |= WANT_FSNAME;
1056 return get_root_path(want, fsname, NULL, mntdir, idx);
1059 /* Given a path, find the corresponding Lustre fsname */
1060 int llapi_search_fsname(const char *pathname, char *fsname)
1065 path = realpath(pathname, NULL);
1067 char buf[PATH_MAX], *ptr;
1070 if (pathname[0] != '/') {
1071 /* Need an absolute path, but realpath() only works for
1072 * pathnames that actually exist. We go through the
1073 * extra hurdle of dirname(getcwd() + pathname) in
1074 * case the relative pathname contains ".." in it. */
1075 if (getcwd(buf, sizeof(buf) - 2) == NULL)
1077 rc = strlcat(buf, "/", sizeof(buf));
1078 if (rc >= sizeof(buf))
1081 rc = strlcat(buf, pathname, sizeof(buf));
1082 if (rc >= sizeof(buf))
1084 path = realpath(buf, NULL);
1086 ptr = strrchr(buf, '/');
1090 path = realpath(buf, NULL);
1093 llapi_error(LLAPI_MSG_ERROR, rc,
1094 "pathname '%s' cannot expand",
1100 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1105 int llapi_search_rootpath(char *pathname, const char *fsname)
1107 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1110 int llapi_getname(const char *path, char *buf, size_t size)
1112 struct obd_uuid uuid_buf;
1113 char *uuid = uuid_buf.uuid;
1116 memset(&uuid_buf, 0, sizeof(uuid_buf));
1117 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1121 /* We want to turn lustre-clilov-ffff88002738bc00 into
1122 * lustre-ffff88002738bc00. */
1124 nr = snprintf(buf, size, "%.*s-%s",
1125 (int) (strlen(uuid) - 24), uuid,
1126 uuid + strlen(uuid) - 16);
1136 * find the pool directory path under /proc
1137 * (can be also used to test if a fsname is known)
1139 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
1142 char pattern[PATH_MAX + 1];
1143 char buffer[PATH_MAX];
1145 if (fsname == NULL) {
1146 rc = llapi_search_fsname(pathname, buffer);
1150 strcpy(pathname, fsname);
1153 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
1154 rc = first_match(pattern, buffer);
1158 /* in fsname test mode, pool_pathname is NULL */
1159 if (pool_pathname != NULL)
1160 strcpy(pool_pathname, buffer);
1166 * Get the list of pool members.
1167 * \param poolname string of format \<fsname\>.\<poolname\>
1168 * \param members caller-allocated array of char*
1169 * \param list_size size of the members array
1170 * \param buffer caller-allocated buffer for storing OST names
1171 * \param buffer_size size of the buffer
1173 * \return number of members retrieved for this pool
1174 * \retval -error failure
1176 int llapi_get_poolmembers(const char *poolname, char **members,
1177 int list_size, char *buffer, int buffer_size)
1179 char fsname[PATH_MAX];
1181 char pathname[PATH_MAX];
1182 char path[PATH_MAX];
1189 /* name is FSNAME.POOLNAME */
1190 if (strlen(poolname) >= sizeof(fsname))
1192 strlcpy(fsname, poolname, sizeof(fsname));
1193 pool = strchr(fsname, '.');
1200 rc = poolpath(fsname, NULL, pathname);
1202 llapi_error(LLAPI_MSG_ERROR, rc,
1203 "Lustre filesystem '%s' not found",
1208 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1209 rc = snprintf(path, sizeof(path), "%s/%s", pathname, pool);
1210 if (rc >= sizeof(path))
1212 fd = fopen(path, "r");
1215 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
1220 while (fgets(buf, sizeof(buf), fd) != NULL) {
1221 if (nb_entries >= list_size) {
1225 buf[sizeof(buf) - 1] = '\0';
1227 tmp = strchr(buf, '\n');
1230 if (used + strlen(buf) + 1 > buffer_size) {
1235 strcpy(buffer + used, buf);
1236 members[nb_entries] = buffer + used;
1237 used += strlen(buf) + 1;
1247 * Get the list of pools in a filesystem.
1248 * \param name filesystem name or path
1249 * \param poollist caller-allocated array of char*
1250 * \param list_size size of the poollist array
1251 * \param buffer caller-allocated buffer for storing pool names
1252 * \param buffer_size size of the buffer
1254 * \return number of pools retrieved for this filesystem
1255 * \retval -error failure
1257 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1258 char *buffer, int buffer_size)
1260 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
1264 struct dirent *cookie = NULL;
1266 unsigned int nb_entries = 0;
1267 unsigned int used = 0;
1270 /* initilize output array */
1271 for (i = 0; i < list_size; i++)
1274 /* is name a pathname ? */
1275 ptr = strchr(name, '/');
1277 /* only absolute pathname is supported */
1281 if (!realpath(name, rname)) {
1283 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1288 rc = poolpath(NULL, rname, pathname);
1290 llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
1291 " a Lustre filesystem", name);
1294 if (strlen(rname) > sizeof(fsname)-1)
1296 strncpy(fsname, rname, sizeof(fsname));
1298 /* name is FSNAME */
1299 if (strlen(name) > sizeof(fsname)-1)
1301 strncpy(fsname, name, sizeof(fsname));
1302 rc = poolpath(fsname, NULL, pathname);
1305 llapi_error(LLAPI_MSG_ERROR, rc,
1306 "Lustre filesystem '%s' not found", name);
1310 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1311 dir = opendir(pathname);
1314 llapi_error(LLAPI_MSG_ERROR, rc,
1315 "Could not open pool list for '%s'",
1321 rc = readdir_r(dir, &pool, &cookie);
1325 llapi_error(LLAPI_MSG_ERROR, rc,
1326 "Error reading pool list for '%s'", name);
1328 } else if ((rc == 0) && (cookie == NULL)) {
1329 /* end of directory */
1333 /* ignore . and .. */
1334 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1337 /* check output bounds */
1338 if (nb_entries >= list_size) {
1343 /* +2 for '.' and final '\0' */
1344 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1350 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1351 poollist[nb_entries] = buffer + used;
1352 used += strlen(pool.d_name) + strlen(fsname) + 2;
1358 return ((rc != 0) ? rc : nb_entries);
1361 /* wrapper for lfs.c and obd.c */
1362 int llapi_poollist(const char *name)
1364 /* list of pool names (assume that pool count is smaller
1366 char **list, *buffer = NULL, *path = NULL, *fsname = NULL;
1367 int obdcount, bufsize, rc, nb, i;
1368 char *poolname = NULL, *tmp = NULL, data[16];
1373 if (name[0] != '/') {
1374 fsname = strdup(name);
1378 poolname = strchr(fsname, '.');
1382 path = (char *) name;
1385 rc = get_param_obdvar(fsname, path, "lov", "numobd",
1386 data, sizeof(data));
1389 obdcount = atoi(data);
1391 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1392 * and also an array to store the pointers for all that
1393 * allocated space. */
1395 bufsize = sizeof(struct obd_uuid) * obdcount;
1396 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1397 if (buffer == NULL) {
1401 list = (char **) (buffer + bufsize);
1404 /* name is a path or fsname */
1405 nb = llapi_get_poollist(name, list, obdcount,
1408 /* name is a pool name (<fsname>.<poolname>) */
1409 nb = llapi_get_poolmembers(name, list, obdcount,
1413 if (nb == -EOVERFLOW) {
1416 goto retry_get_pools;
1419 for (i = 0; i < nb; i++)
1420 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1421 rc = (nb < 0 ? nb : 0);
1430 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1431 void *data, struct dirent64 *de);
1433 #define OBD_NOT_FOUND (-1)
1435 static int common_param_init(struct find_param *param, char *path)
1437 int lumlen = get_mds_md_size(path);
1439 if (lumlen < PATH_MAX + 1)
1440 lumlen = PATH_MAX + 1;
1442 param->lumlen = lumlen;
1443 param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
1444 if (param->lmd == NULL) {
1445 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1446 "error: allocation of %zu bytes for ioctl",
1447 sizeof(lstat_t) + param->lumlen);
1451 param->fp_lmv_count = 256;
1452 param->fp_lmv_md = malloc(lmv_user_md_size(256, LMV_MAGIC_V1));
1453 if (param->fp_lmv_md == NULL) {
1454 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1455 "error: allocation of %d bytes for ioctl",
1456 lmv_user_md_size(256, LMV_MAGIC_V1));
1460 param->got_uuids = 0;
1461 param->obdindexes = NULL;
1462 param->obdindex = OBD_NOT_FOUND;
1463 if (!param->migrate)
1464 param->mdtindex = OBD_NOT_FOUND;
1468 static void find_param_fini(struct find_param *param)
1470 if (param->obdindexes)
1471 free(param->obdindexes);
1476 if (param->fp_lmv_md)
1477 free(param->fp_lmv_md);
1480 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1481 struct dirent64 *de)
1483 struct find_param *param = data;
1489 /* set errno upon failure */
1490 static DIR *opendir_parent(char *path)
1496 fname = strrchr(path, '/');
1498 return opendir(".");
1502 parent = opendir(path);
1507 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1509 struct lmv_user_md *lmv = (struct lmv_user_md *)param->fp_lmv_md;
1512 lmv->lum_stripe_count = param->fp_lmv_count;
1513 if (param->get_default_lmv)
1514 lmv->lum_magic = LMV_USER_MAGIC;
1516 lmv->lum_magic = LMV_MAGIC_V1;
1517 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, lmv);
1522 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1523 struct lov_user_mds_data *lmd, int lumlen)
1525 lstat_t *st = &lmd->lmd_st;
1528 if (parent == NULL && dir == NULL)
1532 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1533 } else if (parent) {
1534 char *fname = strrchr(path, '/');
1536 fname = (fname == NULL ? path : fname + 1);
1537 /* retrieve needed file info */
1538 strlcpy((char *)lmd, fname, lumlen);
1539 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1543 if (errno == ENOTTY) {
1544 /* ioctl is not supported, it is not a lustre fs.
1545 * Do the regular lstat(2) instead. */
1546 ret = lstat_f(path, st);
1549 llapi_error(LLAPI_MSG_ERROR, ret,
1550 "error: %s: lstat failed for %s",
1553 } else if (errno == ENOENT) {
1555 llapi_error(LLAPI_MSG_WARN, ret,
1556 "warning: %s: %s does not exist",
1558 } else if (errno != EISDIR) {
1560 llapi_error(LLAPI_MSG_ERROR, ret,
1561 "%s ioctl failed for %s.",
1562 dir ? "LL_IOC_MDC_GETINFO" :
1563 "IOC_MDC_GETFILEINFO", path);
1566 llapi_error(LLAPI_MSG_ERROR, ret,
1567 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1574 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1575 semantic_func_t sem_init,
1576 semantic_func_t sem_fini, void *data,
1577 struct dirent64 *de)
1579 struct find_param *param = (struct find_param *)data;
1580 struct dirent64 *dent;
1588 if (!d && errno != ENOTDIR) {
1590 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1593 } else if (!d && !parent) {
1594 /* ENOTDIR. Open the parent dir. */
1595 p = opendir_parent(path);
1602 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1608 while ((dent = readdir64(d)) != NULL) {
1611 param->have_fileinfo = 0;
1613 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1616 /* Don't traverse .lustre directory */
1617 if (!(strcmp(dent->d_name, dot_lustre_name)))
1621 if ((len + dent->d_reclen + 2) > size) {
1622 llapi_err_noerrno(LLAPI_MSG_ERROR,
1623 "error: %s: string buffer is too small",
1628 strcat(path, dent->d_name);
1630 if (dent->d_type == DT_UNKNOWN) {
1631 lstat_t *st = ¶m->lmd->lmd_st;
1633 rc = get_lmd_info(path, d, NULL, param->lmd,
1636 dent->d_type = IFTODT(st->st_mode);
1643 switch (dent->d_type) {
1645 llapi_err_noerrno(LLAPI_MSG_ERROR,
1646 "error: %s: '%s' is UNKNOWN type %d",
1647 __func__, dent->d_name, dent->d_type);
1650 rc = llapi_semantic_traverse(path, size, d, sem_init,
1651 sem_fini, data, dent);
1652 if (rc != 0 && ret == 0)
1658 rc = sem_init(path, d, NULL, data, dent);
1659 if (rc < 0 && ret == 0)
1662 if (sem_fini && rc == 0)
1663 sem_fini(path, d, NULL, data, dent);
1671 sem_fini(path, parent, &d, data, de);
1680 static int param_callback(char *path, semantic_func_t sem_init,
1681 semantic_func_t sem_fini, struct find_param *param)
1683 int ret, len = strlen(path);
1686 if (len > PATH_MAX) {
1688 llapi_error(LLAPI_MSG_ERROR, ret,
1689 "Path name '%s' is too long", path);
1693 buf = (char *)malloc(PATH_MAX + 1);
1697 strlcpy(buf, path, PATH_MAX + 1);
1698 ret = common_param_init(param, buf);
1702 param->fp_depth = 0;
1704 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1705 sem_fini, param, NULL);
1707 find_param_fini(param);
1709 return ret < 0 ? ret : 0;
1712 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1714 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1717 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1722 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1724 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1727 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1732 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1736 fd = open(path, O_RDONLY);
1739 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1743 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1749 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1753 fd = open(path, O_RDONLY);
1756 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1760 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1771 * If uuidp is NULL, return the number of available obd uuids.
1772 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1773 * there are more OSTs then allocated to uuidp, then an error is returned with
1774 * the ost_count set to number of available obd uuids.
1776 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1777 int *ost_count, enum tgt_type type)
1779 struct obd_uuid name;
1783 int rc = 0, index = 0;
1785 /* Get the lov name */
1786 if (type == LOV_TYPE) {
1787 rc = llapi_file_fget_lov_uuid(fd, &name);
1791 rc = llapi_file_fget_lmv_uuid(fd, &name);
1796 /* Now get the ost uuids from /proc */
1797 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1798 type == LOV_TYPE ? "lov" : "lmv", name.uuid);
1799 fp = fopen(buf, "r");
1802 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1806 snprintf(format, sizeof(format),
1807 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
1808 while (fgets(buf, sizeof(buf), fp) != NULL) {
1809 if (uuidp && (index < *ost_count)) {
1810 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
1818 if (uuidp && (index > *ost_count))
1825 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1827 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1830 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1835 root = opendir(mnt);
1838 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1843 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1851 /* Check if user specified value matches a real uuid. Ignore _UUID,
1852 * -osc-4ba41334, other trailing gunk in comparison.
1853 * @param real_uuid ends in "_UUID"
1854 * @param search_uuid may or may not end in "_UUID"
1856 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1858 int cmplen = strlen(real_uuid);
1859 int searchlen = strlen(search_uuid);
1861 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1863 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1866 /* The UUIDs may legitimately be different lengths, if
1867 * the system was upgraded from an older version. */
1868 if (cmplen != searchlen)
1871 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1874 /* Here, param->obduuid points to a single obduuid, the index of which is
1875 * returned in param->obdindex */
1876 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1878 struct obd_uuid obd_uuid;
1884 if (param->got_uuids)
1887 /* Get the lov/lmv name */
1889 rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
1891 rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
1893 if (rc != -ENOTTY) {
1894 llapi_error(LLAPI_MSG_ERROR, rc,
1895 "error: can't get lov name: %s", dname);
1902 param->got_uuids = 1;
1904 /* Now get the ost uuids from /proc */
1905 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1906 param->get_lmv ? "lmv" : "lov", obd_uuid.uuid);
1907 fp = fopen(buf, "r");
1910 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1914 if (!param->obduuid && !param->quiet && !param->obds_printed)
1915 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1916 param->get_lmv ? "MDTS" : "OBDS:");
1918 snprintf(format, sizeof(format),
1919 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
1920 while (fgets(buf, sizeof(buf), fp) != NULL) {
1923 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
1926 if (param->obduuid) {
1927 if (llapi_uuid_match(obd_uuid.uuid,
1928 param->obduuid->uuid)) {
1929 param->obdindex = index;
1932 } else if (!param->quiet && !param->obds_printed) {
1933 /* Print everything */
1934 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1937 param->obds_printed = 1;
1941 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1942 llapi_err_noerrno(LLAPI_MSG_ERROR,
1943 "error: %s: unknown obduuid: %s",
1944 __func__, param->obduuid->uuid);
1951 /* In this case, param->obduuid will be an array of obduuids and
1952 * obd index for all these obduuids will be returned in
1953 * param->obdindexes */
1954 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1955 int num_obds, int **obdindexes, int *obdindex,
1958 int ret, obdcount, obd_valid = 0, obdnum;
1960 struct obd_uuid *uuids = NULL;
1964 if (type == LOV_TYPE)
1965 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
1967 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
1971 obdcount = atoi(buf);
1972 uuids = (struct obd_uuid *)malloc(obdcount *
1973 sizeof(struct obd_uuid));
1978 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1980 struct obd_uuid *uuids_temp;
1982 if (ret == -EOVERFLOW) {
1983 uuids_temp = realloc(uuids, obdcount *
1984 sizeof(struct obd_uuid));
1985 if (uuids_temp != NULL) {
1987 goto retry_get_uuids;
1993 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
1997 indexes = malloc(num_obds * sizeof(*obdindex));
1998 if (indexes == NULL) {
2003 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2006 /* The user may have specified a simple index */
2007 i = strtol(obduuids[obdnum].uuid, &end, 0);
2008 if (end && *end == '\0' && i < obdcount) {
2009 indexes[obdnum] = i;
2012 for (i = 0; i < obdcount; i++) {
2013 if (llapi_uuid_match(uuids[i].uuid,
2014 obduuids[obdnum].uuid)) {
2015 indexes[obdnum] = i;
2021 if (i >= obdcount) {
2022 indexes[obdnum] = OBD_NOT_FOUND;
2023 llapi_err_noerrno(LLAPI_MSG_ERROR,
2024 "error: %s: unknown obduuid: %s",
2025 __func__, obduuids[obdnum].uuid);
2031 *obdindex = OBD_NOT_FOUND;
2033 *obdindex = obd_valid;
2035 *obdindexes = indexes;
2043 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2047 if (param->mdtuuid) {
2048 ret = setup_indexes(dir, path, param->mdtuuid, param->num_mdts,
2049 ¶m->mdtindexes, ¶m->mdtindex, LMV_TYPE);
2053 if (param->obduuid) {
2054 ret = setup_indexes(dir, path, param->obduuid, param->num_obds,
2055 ¶m->obdindexes, ¶m->obdindex, LOV_TYPE);
2059 param->got_uuids = 1;
2063 int llapi_ostlist(char *path, struct find_param *param)
2068 dir = opendir(path);
2072 ret = setup_obd_uuid(dir, path, param);
2079 * Given a filesystem name, or a pathname of a file on a lustre filesystem,
2080 * tries to determine the path to the filesystem's clilov directory under /proc
2082 * fsname is limited to MTI_NAME_MAXLEN in lustre_idl.h
2083 * The NUL terminator is compensated by the additional "%s" bytes. */
2084 #define LOV_LEN (sizeof("/proc/fs/lustre/lov/%s-clilov-*") + MTI_NAME_MAXLEN)
2085 static int clilovpath(const char *fsname, const char *const pathname,
2089 char pattern[LOV_LEN];
2090 char buffer[PATH_MAX + 1];
2092 if (fsname == NULL) {
2093 rc = llapi_search_fsname(pathname, buffer);
2099 snprintf(pattern, sizeof(pattern), "/proc/fs/lustre/lov/%s-clilov-*",
2102 rc = first_match(pattern, buffer);
2106 strlcpy(clilovpath, buffer, sizeof(buffer));
2112 * Given the path to a stripe attribute proc file, tries to open and
2113 * read the attribute and return the value using the attr parameter
2115 static int sattr_read_attr(const char *const fpath,
2120 char line[PATH_MAX + 1];
2123 f = fopen(fpath, "r");
2126 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open '%s'", fpath);
2130 if (fgets(line, sizeof(line), f) != NULL) {
2133 llapi_error(LLAPI_MSG_ERROR, errno, "Cannot read from '%s'", fpath);
2142 * Tries to determine the default stripe attributes for a given filesystem. The
2143 * filesystem to check should be specified by fsname, or will be determined
2146 static int sattr_get_defaults(const char *const fsname,
2147 const char *const pathname,
2148 unsigned int *scount,
2149 unsigned int *ssize,
2150 unsigned int *soffset)
2153 char dpath[PATH_MAX + 1];
2154 char fpath[PATH_MAX + 1];
2156 rc = clilovpath(fsname, pathname, dpath);
2161 snprintf(fpath, PATH_MAX, "%s/stripecount", dpath);
2162 rc = sattr_read_attr(fpath, scount);
2168 snprintf(fpath, PATH_MAX, "%s/stripesize", dpath);
2169 rc = sattr_read_attr(fpath, ssize);
2175 snprintf(fpath, PATH_MAX, "%s/stripeoffset", dpath);
2176 rc = sattr_read_attr(fpath, soffset);
2185 * Tries to gather the default stripe attributes for a given filesystem. If
2186 * the attributes can be determined, they are cached for easy retreival the
2187 * next time they are needed. Only a single filesystem's attributes are
2190 static int sattr_cache_get_defaults(const char *const fsname,
2191 const char *const pathname,
2192 unsigned int *scount,
2193 unsigned int *ssize,
2194 unsigned int *soffset)
2197 char fsname[PATH_MAX + 1];
2198 unsigned int stripecount;
2199 unsigned int stripesize;
2200 unsigned int stripeoffset;
2206 char fsname_buf[PATH_MAX + 1];
2207 unsigned int tmp[3];
2209 if (fsname == NULL) {
2210 rc = llapi_search_fsname(pathname, fsname_buf);
2214 strlcpy(fsname_buf, fsname, sizeof(fsname_buf));
2217 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2219 * Ensure all 3 sattrs (count, size, and offset) are
2220 * successfully retrieved and stored in tmp before writing to
2223 rc = sattr_get_defaults(fsname_buf, NULL, &tmp[0], &tmp[1],
2228 cache.stripecount = tmp[0];
2229 cache.stripesize = tmp[1];
2230 cache.stripeoffset = tmp[2];
2231 strlcpy(cache.fsname, fsname_buf, sizeof(cache.fsname));
2235 *scount = cache.stripecount;
2237 *ssize = cache.stripesize;
2239 *soffset = cache.stripeoffset;
2244 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2245 struct lov_user_ost_data_v1 *objects,
2246 int is_dir, int verbose, int depth,
2247 int raw, char *pool_name)
2249 char *prefix = is_dir ? "" : "lmm_";
2250 char *seperator = "";
2253 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2254 lmm_oi_set_seq(&lum->lmm_oi, 0);
2255 if (verbose & VERBOSE_DETAIL)
2256 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
2259 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2260 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2262 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2263 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
2265 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
2266 lmm_oi_seq(&lum->lmm_oi));
2267 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
2268 lmm_oi_id(&lum->lmm_oi));
2271 if (verbose & VERBOSE_COUNT) {
2272 if (verbose & ~VERBOSE_COUNT)
2273 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
2276 if (!raw && lum->lmm_stripe_count == 0) {
2277 unsigned int scount;
2278 rc = sattr_cache_get_defaults(NULL, path,
2282 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2285 llapi_error(LLAPI_MSG_ERROR, rc,
2286 "Cannot determine default"
2289 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2290 lum->lmm_stripe_count ==
2291 (typeof(lum->lmm_stripe_count))(-1)
2292 ? -1 : lum->lmm_stripe_count);
2295 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2296 (__s16)lum->lmm_stripe_count);
2298 seperator = is_dir ? " " : "\n";
2301 if (verbose & VERBOSE_SIZE) {
2302 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2303 if (verbose & ~VERBOSE_SIZE)
2304 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
2306 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
2308 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2311 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2313 llapi_error(LLAPI_MSG_ERROR, rc,
2314 "Cannot determine default"
2317 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2318 lum->lmm_stripe_size);
2320 seperator = is_dir ? " " : "\n";
2323 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2324 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2325 if (verbose & ~VERBOSE_LAYOUT)
2326 llapi_printf(LLAPI_MSG_NORMAL, "%spattern: ",
2328 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2332 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2333 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2334 if (verbose & ~VERBOSE_GENERATION)
2335 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
2337 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2338 (int)lum->lmm_layout_gen);
2342 if (verbose & VERBOSE_OFFSET) {
2343 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2344 if (verbose & ~VERBOSE_OFFSET)
2345 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
2348 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2349 lum->lmm_stripe_offset ==
2350 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2351 lum->lmm_stripe_offset);
2353 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2354 objects[0].l_ost_idx);
2355 seperator = is_dir ? " " : "\n";
2358 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
2359 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2360 if (verbose & ~VERBOSE_POOL)
2361 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
2363 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2366 if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
2367 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2370 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2371 struct lov_user_ost_data_v1 *objects,
2372 char *path, int is_dir, int obdindex,
2373 int depth, int header, int raw)
2375 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2378 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2379 if (obdindex == objects[i].l_ost_idx) {
2387 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
2388 depth, raw, pool_name);
2390 if (!is_dir && (header & VERBOSE_OBJID) &&
2391 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2393 llapi_printf(LLAPI_MSG_NORMAL,
2394 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2396 for (i = 0; i < lum->lmm_stripe_count; i++) {
2397 int idx = objects[i].l_ost_idx;
2398 long long oid = ostid_id(&objects[i].l_ost_oi);
2399 long long gr = ostid_seq(&objects[i].l_ost_oi);
2400 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) {
2402 sprintf(fmt, "%s%s%s\n",
2403 "\t%6u\t%14llu\t%#13llx\t",
2404 (fid_seq_is_rsvd(gr) ||
2405 fid_seq_is_mdt0(gr)) ?
2406 "%14llu" : "%#14llx", "%s");
2407 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2409 obdindex == idx ? " *" : "");
2413 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2417 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2418 char *path, int obdindex, int depth, int verbose)
2420 struct lmv_user_mds_data *objects = lum->lum_objects;
2421 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2422 int i, obdstripe = 0;
2423 char *separator = "";
2425 if (obdindex != OBD_NOT_FOUND) {
2426 if (lum->lum_stripe_count == 0) {
2427 if (obdindex == lum->lum_stripe_offset)
2430 for (i = 0; i < lum->lum_stripe_count; i++) {
2431 if (obdindex == objects[i].lum_mds) {
2432 llapi_printf(LLAPI_MSG_NORMAL,
2447 /* show all information default */
2449 if (lum->lum_magic == LMV_USER_MAGIC)
2450 verbose = VERBOSE_POOL | VERBOSE_COUNT | VERBOSE_OFFSET;
2452 verbose = VERBOSE_OBJID;
2455 if (depth && path && ((verbose != VERBOSE_OBJID)))
2456 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2458 if (verbose & VERBOSE_COUNT) {
2459 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2460 if (verbose & ~VERBOSE_COUNT)
2461 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2462 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2463 (int)lum->lum_stripe_count);
2464 if (verbose & VERBOSE_OFFSET)
2470 if (verbose & VERBOSE_OFFSET) {
2471 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2472 if (verbose & ~VERBOSE_OFFSET)
2473 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2474 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2475 (int)lum->lum_stripe_offset);
2479 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2480 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2481 if (obdstripe == 1 && lum->lum_stripe_count > 0)
2482 llapi_printf(LLAPI_MSG_NORMAL,
2483 "mdtidx\t\t FID[seq:oid:ver]\n");
2484 for (i = 0; i < lum->lum_stripe_count; i++) {
2485 int idx = objects[i].lum_mds;
2486 struct lu_fid *fid = &objects[i].lum_fid;
2487 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2488 llapi_printf(LLAPI_MSG_NORMAL,
2489 "%6u\t\t "DFID"\t\t%s\n",
2491 obdindex == idx ? " *" : "");
2496 if ((verbose & VERBOSE_POOL) && (pool_name[0] != '\0')) {
2497 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2498 if (verbose & ~VERBOSE_POOL)
2499 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2501 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2505 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
2506 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2509 void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
2513 if (param->get_lmv || param->get_default_lmv)
2514 magic = (__u32)param->fp_lmv_md->lum_magic;
2516 magic = *(__u32 *)¶m->lmd->lmd_lmm; /* lum->lmm_magic */
2519 case LOV_USER_MAGIC_V1:
2520 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
2521 param->lmd->lmd_lmm.lmm_objects,
2523 param->obdindex, param->fp_max_depth,
2524 param->verbose, param->raw);
2526 case LOV_USER_MAGIC_V3: {
2527 char pool_name[LOV_MAXPOOLNAME + 1];
2528 struct lov_user_ost_data_v1 *objects;
2529 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2531 strlcpy(pool_name, lmmv3->lmm_pool_name, sizeof(pool_name));
2532 objects = lmmv3->lmm_objects;
2533 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
2534 objects, path, is_dir,
2535 param->obdindex, param->fp_max_depth,
2536 param->verbose, param->raw);
2540 case LMV_USER_MAGIC: {
2541 char pool_name[LOV_MAXPOOLNAME + 1];
2542 struct lmv_user_md *lum;
2544 lum = (struct lmv_user_md *)param->fp_lmv_md;
2545 strlcpy(pool_name, lum->lum_pool_name, sizeof(pool_name));
2546 lmv_dump_user_lmm(lum, pool_name, path,
2547 param->obdindex, param->fp_max_depth,
2552 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2553 "(expecting one of %#x %#x %#x %#x)\n",
2554 *(__u32 *)¶m->lmd->lmd_lmm,
2555 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2556 LMV_USER_MAGIC, LMV_MAGIC_V1);
2561 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2567 fname = strrchr(path, '/');
2569 /* It should be a file (or other non-directory) */
2570 if (fname == NULL) {
2571 dname = (char *)malloc(2);
2575 fname = (char *)path;
2577 dname = (char *)malloc(fname - path + 1);
2580 strncpy(dname, path, fname - path);
2581 dname[fname - path] = '\0';
2585 fd = open(dname, O_RDONLY);
2592 strcpy((char *)lum, fname);
2593 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2596 if (close(fd) == -1 && rc == 0)
2603 int llapi_file_lookup(int dirfd, const char *name)
2605 struct obd_ioctl_data data = { 0 };
2610 if (dirfd < 0 || name == NULL)
2613 data.ioc_version = OBD_IOCTL_VERSION;
2614 data.ioc_len = sizeof(data);
2615 data.ioc_inlbuf1 = (char *)name;
2616 data.ioc_inllen1 = strlen(name) + 1;
2618 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2620 llapi_error(LLAPI_MSG_ERROR, rc,
2621 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2626 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2632 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2633 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2635 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2636 * The table below gives the answers for the specified parameters (value and
2637 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2638 * --------------------------------------
2639 * 1 | file > limit; sign > 0 | -1 / -1 |
2640 * 2 | file = limit; sign > 0 | -1 / -1 |
2641 * 3 | file < limit; sign > 0 | ? / 1 |
2642 * 4 | file > limit; sign = 0 | -1 / -1 |
2643 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2644 * 6 | file < limit; sign = 0 | ? / -1 |
2645 * 7 | file > limit; sign < 0 | 1 / 1 |
2646 * 8 | file = limit; sign < 0 | ? / -1 |
2647 * 9 | file < limit; sign < 0 | ? / -1 |
2648 * --------------------------------------
2649 * Note: 5th actually means that the value is within the interval
2650 * (limit - margin, limit]. */
2651 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2652 int sign, int negopt, unsigned long long margin,
2658 /* Drop the fraction of margin (of days). */
2659 if (file + margin <= limit)
2661 } else if (sign == 0) {
2662 if (file <= limit && file + margin > limit)
2664 else if (file + margin <= limit)
2666 } else if (sign < 0) {
2673 return negopt ? ~ret + 1 : ret;
2676 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2677 * Return -1 or 1 if file timestamp does not or does match the given criteria
2678 * correspondingly. Return 0 if the MDS time is being checked and there are
2679 * attributes on OSTs and it is not yet clear if the timespamp matches.
2681 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2682 * updated timestamps. */
2683 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2688 /* Check if file is accepted. */
2689 if (param->fp_atime) {
2690 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
2691 param->fp_asign, param->fp_exclude_atime,
2698 if (param->fp_mtime) {
2699 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
2700 param->fp_msign, param->fp_exclude_mtime,
2705 /* If the previous check matches, but this one is not yet clear,
2706 * we should return 0 to do an RPC on OSTs. */
2711 if (param->fp_ctime) {
2712 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
2713 param->fp_csign, param->fp_exclude_ctime,
2718 /* If the previous check matches, but this one is not yet clear,
2719 * we should return 0 to do an RPC on OSTs. */
2728 * Check whether the stripes matches the indexes user provided
2732 static int check_obd_match(struct find_param *param)
2734 lstat_t *st = ¶m->lmd->lmd_st;
2735 struct lov_user_ost_data_v1 *lmm_objects;
2738 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
2741 if (!S_ISREG(st->st_mode))
2744 /* Only those files should be accepted, which have a
2745 * stripe on the specified OST. */
2746 if (!param->lmd->lmd_lmm.lmm_stripe_count)
2749 if (param->lmd->lmd_lmm.lmm_magic ==
2750 LOV_USER_MAGIC_V3) {
2751 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2753 lmm_objects = lmmv3->lmm_objects;
2754 } else if (param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2755 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
2757 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2758 __func__, param->lmd->lmd_lmm.lmm_magic);
2762 for (i = 0; i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
2763 for (j = 0; j < param->num_obds; j++) {
2764 if (param->obdindexes[j] ==
2765 lmm_objects[i].l_ost_idx) {
2766 if (param->exclude_obd)
2773 if (param->exclude_obd)
2778 static int check_mdt_match(struct find_param *param)
2782 if (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND)
2785 /* FIXME: For striped dir, we should get stripe information and check */
2786 for (i = 0; i < param->num_mdts; i++) {
2787 if (param->mdtindexes[i] == param->file_mdtindex)
2788 return !param->exclude_mdt;
2791 if (param->exclude_mdt)
2797 * Check whether the obd is active or not, if it is
2798 * not active, just print the object affected by this
2801 static int print_failed_tgt(struct find_param *param, char *path, int type)
2803 struct obd_statfs stat_buf;
2804 struct obd_uuid uuid_buf;
2807 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2809 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2810 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2811 ret = llapi_obd_statfs(path, type,
2812 param->obdindex, &stat_buf,
2815 llapi_printf(LLAPI_MSG_NORMAL,
2816 "obd_uuid: %s failed %s ",
2817 param->obduuid->uuid,
2823 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
2824 void *data, struct dirent64 *de)
2826 struct find_param *param = (struct find_param *)data;
2827 DIR *dir = dirp == NULL ? NULL : *dirp;
2828 int decision = 1; /* 1 is accepted; -1 is rejected. */
2829 lstat_t *st = ¶m->lmd->lmd_st;
2831 int checked_type = 0;
2834 LASSERT(parent != NULL || dir != NULL);
2836 if (param->have_fileinfo == 0)
2837 param->lmd->lmd_lmm.lmm_stripe_count = 0;
2839 /* If a regular expression is presented, make the initial decision */
2840 if (param->pattern != NULL) {
2841 char *fname = strrchr(path, '/');
2842 fname = (fname == NULL ? path : fname + 1);
2843 ret = fnmatch(param->pattern, fname, 0);
2844 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
2845 (ret == 0 && param->exclude_pattern))
2849 /* See if we can check the file type from the dirent. */
2850 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
2853 if (DTTOIF(de->d_type) == param->fp_type) {
2854 if (param->fp_exclude_type)
2857 if (!param->fp_exclude_type)
2864 /* Request MDS for the stat info if some of these parameters need
2865 * to be compared. */
2866 if (param->obduuid || param->mdtuuid ||
2867 param->fp_check_uid || param->fp_check_gid ||
2868 param->fp_atime || param->fp_mtime || param->fp_ctime ||
2869 param->check_pool || param->check_size ||
2870 param->check_stripecount || param->check_stripesize ||
2871 param->check_layout)
2874 if (param->fp_type != 0 && checked_type == 0)
2877 if (param->have_fileinfo == 0 && decision == 0) {
2878 ret = get_lmd_info(path, parent, dir, param->lmd,
2882 ret = llapi_file_fget_mdtidx(dirfd(dir),
2883 ¶m->file_mdtindex);
2888 ret = lstat_f(path, &tmp_st);
2891 llapi_error(LLAPI_MSG_ERROR, ret,
2892 "error: %s: lstat failed"
2893 "for %s", __func__, path);
2896 if (S_ISREG(tmp_st.st_mode)) {
2897 fd = open(path, O_RDONLY);
2899 ret = llapi_file_fget_mdtidx(fd,
2900 ¶m->file_mdtindex);
2906 /* For special inode, it assumes to
2907 * reside on the same MDT with the
2910 ret = llapi_file_fget_mdtidx(fd,
2911 ¶m->file_mdtindex);
2924 if (param->fp_type && !checked_type) {
2925 if ((st->st_mode & S_IFMT) == param->fp_type) {
2926 if (param->fp_exclude_type)
2929 if (!param->fp_exclude_type)
2935 if (param->obduuid || param->mdtuuid) {
2936 if (lustre_fs && param->got_uuids &&
2937 param->fp_dev != st->st_dev) {
2938 /* A lustre/lustre mount point is crossed. */
2939 param->got_uuids = 0;
2940 param->obds_printed = 0;
2941 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2944 if (lustre_fs && !param->got_uuids) {
2945 ret = setup_target_indexes(dir ? dir : parent, path,
2950 param->fp_dev = st->st_dev;
2951 } else if (!lustre_fs && param->got_uuids) {
2952 /* A lustre/non-lustre mount point is crossed. */
2953 param->got_uuids = 0;
2954 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2958 if (param->check_stripesize) {
2959 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_size,
2961 param->stripesize_sign,
2962 param->exclude_stripesize,
2963 param->stripesize_units, 0);
2968 if (param->check_stripecount) {
2969 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_count,
2971 param->stripecount_sign,
2972 param->exclude_stripecount, 1, 0);
2977 if (param->check_layout) {
2980 found = (param->lmd->lmd_lmm.lmm_pattern & param->layout);
2981 if ((param->lmd->lmd_lmm.lmm_pattern == 0xFFFFFFFF) ||
2982 (found && param->exclude_layout) ||
2983 (!found && !param->exclude_layout)) {
2989 /* If an OBD UUID is specified but none matches, skip this file. */
2990 if ((param->obduuid && param->obdindex == OBD_NOT_FOUND) ||
2991 (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND))
2994 /* If a OST or MDT UUID is given, and some OST matches,
2996 if (param->obdindex != OBD_NOT_FOUND ||
2997 param->mdtindex != OBD_NOT_FOUND) {
2998 if (param->obduuid) {
2999 if (check_obd_match(param)) {
3000 /* If no mdtuuid is given, we are done.
3001 * Otherwise, fall through to the mdtuuid
3003 if (!param->mdtuuid)
3009 if (param->mdtuuid) {
3010 if (check_mdt_match(param))
3016 if (param->fp_check_uid) {
3017 if (st->st_uid == param->fp_uid) {
3018 if (param->fp_exclude_uid)
3021 if (!param->fp_exclude_uid)
3026 if (param->fp_check_gid) {
3027 if (st->st_gid == param->fp_gid) {
3028 if (param->fp_exclude_gid)
3031 if (!param->fp_exclude_gid)
3036 if (param->check_pool) {
3037 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
3039 /* empty requested pool is taken as no pool search => V1 */
3040 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
3041 (param->poolname[0] == '\0')) ||
3042 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3043 (strncmp(lmmv3->lmm_pool_name,
3044 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
3045 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3046 (strcmp(param->poolname, "*") == 0))) {
3047 if (param->exclude_pool)
3050 if (!param->exclude_pool)
3055 /* Check the time on mds. */
3057 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
3060 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
3061 param->lmd->lmd_lmm.lmm_stripe_count)
3063 decision = find_time_check(st, param, for_mds);
3068 /* If file still fits the request, ask ost for updated info.
3069 The regular stat is almost of the same speed as some new
3070 'glimpse-size-ioctl'. */
3072 if (param->check_size && S_ISREG(st->st_mode) &&
3073 param->lmd->lmd_lmm.lmm_stripe_count)
3077 /* For regular files with the stripe the decision may have not
3078 * been taken yet if *time or size is to be checked. */
3079 LASSERT((S_ISREG(st->st_mode) &&
3080 param->lmd->lmd_lmm.lmm_stripe_count) ||
3081 param->mdtindex != OBD_NOT_FOUND);
3083 if (param->obdindex != OBD_NOT_FOUND)
3084 print_failed_tgt(param, path, LL_STATFS_LOV);
3086 if (param->mdtindex != OBD_NOT_FOUND)
3087 print_failed_tgt(param, path, LL_STATFS_LMV);
3090 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
3091 (void *)param->lmd);
3092 } else if (parent) {
3093 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
3094 (void *)param->lmd);
3098 if (errno == ENOENT) {
3099 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
3100 "warning: %s: %s does not exist",
3105 llapi_error(LLAPI_MSG_ERROR, ret,
3106 "%s: IOC_LOV_GETINFO on %s failed",
3112 /* Check the time on osc. */
3113 decision = find_time_check(st, param, 0);
3120 if (param->check_size)
3121 decision = find_value_cmp(st->st_size, param->size,
3122 param->size_sign, param->exclude_size,
3123 param->size_units, 0);
3125 if (decision != -1) {
3126 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
3128 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
3130 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3134 /* Do not get down anymore? */
3135 if (param->fp_depth == param->fp_max_depth)
3143 static int cb_mv_init(char *path, DIR *parent, DIR **dirp,
3144 void *param_data, struct dirent64 *de)
3146 struct find_param *param = (struct find_param *)param_data;
3148 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3150 struct obd_ioctl_data data = { 0 };
3155 LASSERT(parent != NULL || dirp != NULL);
3159 if (parent == NULL) {
3160 dir = opendir_parent(path);
3164 llapi_error(LLAPI_MSG_ERROR, ret,
3165 "can not open %s\n", path);
3172 filename = basename(path);
3173 data.ioc_inlbuf1 = (char *)filename;
3174 data.ioc_inllen1 = strlen(filename) + 1;
3175 data.ioc_inlbuf2 = (char *)¶m->mdtindex;
3176 data.ioc_inllen2 = sizeof(param->mdtindex);
3177 ret = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3179 llapi_error(LLAPI_MSG_ERROR, ret,
3180 "llapi_obd_statfs: error packing ioctl data");
3184 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
3187 fprintf(stderr, "%s migrate failed %d\n", path, ret);
3189 } else if (param->verbose & VERBOSE_DETAIL) {
3190 fprintf(stdout, "migrate %s to MDT%d\n", path, param->mdtindex);
3195 /* If the directory is being migration, we need
3196 * close the directory after migration,
3197 * so the old directory cache will be cleanup
3198 * on the client side, and re-open to get the
3199 * new directory handle */
3200 *dirp = opendir(path);
3203 llapi_error(LLAPI_MSG_ERROR, ret,
3204 "%s: Failed to open '%s'", __func__, path);
3215 int llapi_mv(char *path, struct find_param *param)
3217 return param_callback(path, cb_mv_init, cb_common_fini, param);
3220 int llapi_find(char *path, struct find_param *param)
3222 return param_callback(path, cb_find_init, cb_common_fini, param);
3226 * Get MDT number that the file/directory inode referenced
3227 * by the open fd resides on.
3228 * Return 0 and mdtidx on success, or -ve errno.
3230 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
3232 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
3237 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
3238 struct dirent64 *de)
3240 struct find_param *param = (struct find_param *)data;
3241 DIR *d = dirp == NULL ? NULL : *dirp;
3245 LASSERT(parent != NULL || d != NULL);
3248 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3249 } else if (parent) {
3252 fd = open(path, O_RDONLY);
3254 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3262 if (ret == -ENODATA) {
3263 if (!param->obduuid)
3264 llapi_printf(LLAPI_MSG_NORMAL,
3265 "%s has no stripe info\n", path);
3267 } else if (ret == -ENOENT) {
3268 llapi_error(LLAPI_MSG_WARN, ret,
3269 "warning: %s: %s does not exist",
3272 } else if (ret == -ENOTTY) {
3273 llapi_error(LLAPI_MSG_ERROR, ret,
3274 "%s: '%s' not on a Lustre fs?",
3277 llapi_error(LLAPI_MSG_ERROR, ret,
3278 "error: %s: LL_IOC_GET_MDTIDX failed for %s",
3284 /* The 'LASSERT(parent != NULL || d != NULL);' guarantees
3285 * that either 'd' or 'parent' is not null.
3286 * So in all cases llapi_file_fget_mdtidx() is called,
3287 * thus initializing 'mdtidx'. */
3288 if (param->quiet || !(param->verbose & VERBOSE_DETAIL))
3289 /* coverity[uninit_use_in_call] */
3290 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3292 /* coverity[uninit_use_in_call] */
3293 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3297 /* Do not get down anymore? */
3298 if (param->fp_depth == param->fp_max_depth)
3306 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
3307 struct dirent64 *de)
3309 struct find_param *param = (struct find_param *)data;
3310 DIR *d = dirp == NULL ? NULL : *dirp;
3313 LASSERT(parent != NULL || d != NULL);
3315 if (param->obduuid) {
3317 ret = setup_obd_uuid(d ? d : parent, path, param);
3323 if (param->get_lmv || param->get_default_lmv) {
3324 ret = cb_get_dirstripe(path, d, param);
3326 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
3327 (void *)¶m->lmd->lmd_lmm);
3330 } else if (parent && !param->get_lmv && !param->get_default_lmv) {
3331 char *fname = strrchr(path, '/');
3332 fname = (fname == NULL ? path : fname + 1);
3334 strlcpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
3336 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
3337 (void *)¶m->lmd->lmd_lmm);
3341 if (errno == ENODATA && d != NULL) {
3342 /* We need to "fake" the "use the default" values
3343 * since the lmm struct is zeroed out at this point.
3344 * The magic needs to be set in order to satisfy
3345 * a check later on in the code path.
3346 * The object_seq needs to be set for the "(Default)"
3347 * prefix to be displayed. */
3348 if (param->get_default_lmv) {
3349 struct lmv_user_md *lum = param->fp_lmv_md;
3351 lum->lum_magic = LMV_USER_MAGIC;
3352 lum->lum_stripe_count = 0;
3353 lum->lum_stripe_offset = -1;
3355 } else if (param->get_lmv) {
3356 struct lmv_user_md *lum = param->fp_lmv_md;
3359 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3362 lum->lum_magic = LMV_MAGIC_V1;
3363 lum->lum_stripe_count = 0;
3364 lum->lum_stripe_offset = mdtidx;
3367 struct lov_user_md *lmm = ¶m->lmd->lmd_lmm;
3368 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3370 ostid_set_seq(&lmm->lmm_oi,
3371 FID_SEQ_LOV_DEFAULT);
3372 lmm->lmm_stripe_count = 0;
3373 lmm->lmm_stripe_size = 0;
3374 lmm->lmm_stripe_offset = -1;
3377 } else if (errno == ENODATA && parent != NULL) {
3378 if (!param->obduuid && !param->mdtuuid)
3379 llapi_printf(LLAPI_MSG_NORMAL,
3380 "%s has no stripe info\n", path);
3382 } else if (errno == ENOENT) {
3383 llapi_error(LLAPI_MSG_WARN, -ENOENT,
3384 "warning: %s: %s does not exist",
3387 } else if (errno == ENOTTY) {
3389 llapi_error(LLAPI_MSG_ERROR, ret,
3390 "%s: '%s' not on a Lustre fs?",
3395 llapi_error(LLAPI_MSG_ERROR, ret,
3396 "error: %s: %s failed for %s",
3397 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
3398 "IOC_MDC_GETFILESTRIPE", path);
3405 if (!(param->verbose & VERBOSE_MDTINDEX))
3406 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
3409 /* Do not get down anymore? */
3410 if (param->fp_depth == param->fp_max_depth)
3418 int llapi_getstripe(char *path, struct find_param *param)
3420 return param_callback(path, (param->verbose & VERBOSE_MDTINDEX) ?
3421 cb_get_mdt_index : cb_getstripe,
3422 cb_common_fini, param);
3425 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
3426 struct obd_statfs *stat_buf,
3427 struct obd_uuid *uuid_buf)
3430 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3432 struct obd_ioctl_data data = { 0 };
3435 data.ioc_inlbuf1 = (char *)&type;
3436 data.ioc_inllen1 = sizeof(__u32);
3437 data.ioc_inlbuf2 = (char *)&index;
3438 data.ioc_inllen2 = sizeof(__u32);
3439 data.ioc_pbuf1 = (char *)stat_buf;
3440 data.ioc_plen1 = sizeof(struct obd_statfs);
3441 data.ioc_pbuf2 = (char *)uuid_buf;
3442 data.ioc_plen2 = sizeof(struct obd_uuid);
3444 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3446 llapi_error(LLAPI_MSG_ERROR, rc,
3447 "llapi_obd_statfs: error packing ioctl data");
3451 fd = open(path, O_RDONLY);
3452 if (errno == EISDIR)
3453 fd = open(path, O_DIRECTORY | O_RDONLY);
3456 rc = errno ? -errno : -EBADF;
3457 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
3459 /* If we can't even open a file on the filesystem (e.g. with
3460 * -ESHUTDOWN), force caller to exit or it will loop forever. */
3463 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
3465 rc = errno ? -errno : -EINVAL;
3471 #define MAX_STRING_SIZE 128
3473 int llapi_ping(char *obd_type, char *obd_name)
3475 char path[MAX_STRING_SIZE];
3479 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
3480 obd_type, obd_name);
3482 fd = open(path, O_WRONLY);
3485 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
3489 /* The purpose is to send a byte as a ping, whatever this byte is. */
3490 /* coverity[uninit_use_in_call] */
3491 rc = write(fd, buf, 1);
3501 int llapi_target_iterate(int type_num, char **obd_type,
3502 void *args, llapi_cb_t cb)
3504 char buf[MAX_STRING_SIZE];
3505 FILE *fp = fopen(DEVICES_LIST, "r");
3510 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
3514 while (fgets(buf, sizeof(buf), fp) != NULL) {
3515 char *obd_type_name = NULL;
3516 char *obd_name = NULL;
3517 char *obd_uuid = NULL;
3519 struct obd_statfs osfs_buffer;
3521 while(bufp[0] == ' ')
3524 for(i = 0; i < 3; i++) {
3525 obd_type_name = strsep(&bufp, " ");
3527 obd_name = strsep(&bufp, " ");
3528 obd_uuid = strsep(&bufp, " ");
3530 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
3532 for (i = 0; i < type_num; i++) {
3533 if (strcmp(obd_type_name, obd_type[i]) != 0)
3536 cb(obd_type_name, obd_name, obd_uuid, args);
3543 static void do_target_check(char *obd_type_name, char *obd_name,
3544 char *obd_uuid, void *args)
3548 rc = llapi_ping(obd_type_name, obd_name);
3549 if (rc == ENOTCONN) {
3550 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
3552 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
3554 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
3558 int llapi_target_check(int type_num, char **obd_type, char *dir)
3560 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
3563 #undef MAX_STRING_SIZE
3565 /* Is this a lustre fs? */
3566 int llapi_is_lustre_mnttype(const char *type)
3568 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
3571 /* Is this a lustre client fs? */
3572 int llapi_is_lustre_mnt(struct mntent *mnt)
3574 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
3575 strstr(mnt->mnt_fsname, ":/") != NULL);
3578 int llapi_quotacheck(char *mnt, int check_type)
3583 root = opendir(mnt);
3586 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3590 rc = ioctl(dirfd(root), OBD_IOC_QUOTACHECK, check_type);
3598 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
3604 root = opendir(mnt);
3607 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3612 rc = ioctl(dirfd(root), OBD_IOC_POLL_QUOTACHECK, qchk);
3616 if (poll_intvl < 30)
3624 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3629 root = opendir(mnt);
3632 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3636 rc = ioctl(dirfd(root), OBD_IOC_QUOTACTL, qctl);
3644 static int cb_quotachown(char *path, DIR *parent, DIR **dirp, void *data,
3645 struct dirent64 *de)
3647 struct find_param *param = (struct find_param *)data;
3648 DIR *d = dirp == NULL ? NULL : *dirp;
3652 LASSERT(parent != NULL || d != NULL);
3654 rc = get_lmd_info(path, parent, d, param->lmd, param->lumlen);
3656 if (rc == -ENODATA) {
3657 if (!param->obduuid && !param->quiet)
3658 llapi_error(LLAPI_MSG_ERROR, -ENODATA,
3659 "%s has no stripe info", path);
3661 } else if (rc == -ENOENT) {
3667 st = ¶m->lmd->lmd_st;
3669 /* libc chown() will do extra check, and if the real owner is
3670 * the same as the ones to set, it won't fall into kernel, so
3671 * invoke syscall directly. */
3672 rc = syscall(SYS_chown, path, -1, -1);
3674 llapi_error(LLAPI_MSG_ERROR, errno,
3675 "error: chown %s", path);
3677 rc = chmod(path, st->st_mode);
3680 llapi_error(LLAPI_MSG_ERROR, rc, "error: chmod %s (%hu)",
3687 int llapi_quotachown(char *path, int flag)
3689 struct find_param param;
3691 memset(¶m, 0, sizeof(param));
3692 param.recursive = 1;
3696 return param_callback(path, cb_quotachown, NULL, ¶m);
3702 #include <sys/wait.h>
3706 static int rmtacl_notify(int ops)
3710 int found = 0, fd = 0, rc = 0;
3712 fp = setmntent(MOUNTED, "r");
3715 llapi_error(LLAPI_MSG_ERROR, rc,
3716 "error setmntent(%s)", MOUNTED);
3721 mnt = getmntent(fp);
3725 if (!llapi_is_lustre_mnt(mnt))
3728 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
3731 llapi_error(LLAPI_MSG_ERROR, rc,
3732 "Can't open '%s'\n", mnt->mnt_dir);
3736 rc = ioctl(fd, LL_IOC_RMTACL, ops);
3740 llapi_error(LLAPI_MSG_ERROR, rc,
3741 "ioctl RMTACL on '%s' err %d\n",
3751 return ((rc != 0) ? rc : found);
3754 static char *next_token(char *p, int div)
3760 while (*p && *p != ':' && !isspace(*p))
3763 while (*p == ':' || isspace(*p))
3766 return *p ? p : NULL;
3769 static int rmtacl_name2id(char *name, int is_user)
3774 pw = getpwnam(name);
3778 return (int)(pw->pw_uid);
3782 gr = getgrnam(name);
3786 return (int)(gr->gr_gid);
3790 static int isodigit(int c)
3792 return (c >= '0' && c <= '7') ? 1 : 0;
3796 * Whether the name is just digits string (uid/gid) already or not.
3801 static int str_is_id(char *str)
3808 if (*str == 'x' || *str == 'X') { /* for Hex. */
3809 if (!isxdigit(*(++str)))
3812 while (isxdigit(*(++str)));
3813 } else if (isodigit(*str)) { /* for Oct. */
3814 while (isodigit(*(++str)));
3816 } else if (isdigit(*str)) { /* for Dec. */
3817 while (isdigit(*(++str)));
3820 return (*str == 0) ? 1 : 0;
3830 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
3832 static rmtacl_name_t rmtacl_namelist[] = {
3833 { RMTACL_OPTNAME("user:"), 1, 0 },
3834 { RMTACL_OPTNAME("group:"), 0, 0 },
3835 { RMTACL_OPTNAME("default:user:"), 1, 0 },
3836 { RMTACL_OPTNAME("default:group:"), 0, 0 },
3837 /* for --tabular option */
3838 { RMTACL_OPTNAME("user"), 1, 1 },
3839 { RMTACL_OPTNAME("group"), 0, 1 },
3843 static int rgetfacl_output(char *str)
3845 char *start = NULL, *end = NULL;
3846 int is_user = 0, n, id;
3853 for (rn = rmtacl_namelist; rn->name; rn++) {
3854 if(strncmp(str, rn->name, rn->length) == 0) {
3855 if (!rn->next_token)
3856 start = str + rn->length;
3858 start = next_token(str + rn->length, 0);
3859 is_user = rn->is_user;
3864 end = next_token(start, 1);
3865 if (end == NULL || start == end) {
3866 n = printf("%s", str);
3872 id = rmtacl_name2id(start, is_user);
3873 if (id == INVALID_ID) {
3874 if (str_is_id(start)) {
3876 n = printf("%s", str);
3879 } else if ((id == NOBODY_UID && is_user) ||
3880 (id == NOBODY_GID && !is_user)) {
3882 n = printf("%s", str);
3886 n = printf("%s%d%s", str, id, end);
3891 static int child_status(int status)
3893 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
3896 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
3899 int fd[2], status, rc;
3906 llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe\n");
3913 llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork\n");
3918 /* child process redirects its output. */
3921 if (dup2(fd[1], 1) < 0) {
3923 llapi_error(LLAPI_MSG_ERROR, rc,
3924 "Can't dup2 %d\n", fd[1]);
3934 status = rmtacl_notify(ops);
3938 exit(execvp(argv[0], argv));
3941 /* the following is parent process */
3942 fp = fdopen(fd[0], "r");
3945 llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed\n", fd[0]);
3951 while (fgets(buf, PIPE_BUF, fp) != NULL) {
3952 if (output_func(buf) < 0)
3953 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
3959 if (waitpid(pid, &status, 0) < 0) {
3961 llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed\n", pid);
3965 return child_status(status);
3968 int llapi_lsetfacl(int argc, char *argv[])
3970 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
3973 int llapi_lgetfacl(int argc, char *argv[])
3975 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
3978 int llapi_rsetfacl(int argc, char *argv[])
3980 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
3983 int llapi_rgetfacl(int argc, char *argv[])
3985 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
3988 int llapi_cp(int argc, char *argv[])
3992 rc = rmtacl_notify(RMT_RSETFACL);
3996 exit(execvp(argv[0], argv));
3999 int llapi_ls(int argc, char *argv[])
4003 rc = rmtacl_notify(RMT_LGETFACL);
4007 exit(execvp(argv[0], argv));
4010 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
4011 * format must have %s%s, buf must be > 16
4012 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
4013 * then buf = "lustre-MDT0000"
4015 static int get_mdtname(char *name, char *format, char *buf)
4017 char suffix[]="-MDT0000";
4018 int len = strlen(name);
4020 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
4021 name[len - 5] = '\0';
4026 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
4029 /* Not enough room to add suffix */
4030 llapi_err_noerrno(LLAPI_MSG_ERROR,
4031 "MDT name too long |%s|", name);
4036 return sprintf(buf, format, name, suffix);
4039 /** ioctl on filsystem root, with mdtindex sent as data
4040 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
4041 * \param mdtidxp pointer to integer within data to be filled in with the
4042 * mdt index (0 if no mdt is specified). NULL won't be filled.
4044 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
4052 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
4053 Open root and parse mdt index. */
4054 if (mdtname[0] == '/') {
4056 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
4057 (char *)mdtname, -1);
4059 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
4061 ptr = fsname + strlen(fsname) - 8;
4063 index = strtol(ptr + 4, NULL, 10);
4064 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
4068 llapi_err_noerrno(LLAPI_MSG_ERROR,
4069 "Can't open %s: %d\n", mdtname, rc);
4076 rc = ioctl(fd, opc, data);
4081 if (rc && want_error)
4082 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
4088 /****** Changelog API ********/
4090 static int changelog_ioctl(const char *mdtname, int opc, int id,
4091 long long recno, int flags)
4093 struct ioc_changelog data;
4097 data.icc_recno = recno;
4098 data.icc_flags = flags;
4099 idx = (int *)(&data.icc_mdtindex);
4101 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
4104 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
4105 struct changelog_private {
4108 lustre_kernelcomm kuc;
4111 /** Start reading from a changelog
4112 * @param priv Opaque private control structure
4113 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
4114 * @param device Report changes recorded on this MDT
4115 * @param startrec Report changes beginning with this record number
4116 * (just call llapi_changelog_fini when done; don't need an endrec)
4118 int llapi_changelog_start(void **priv, int flags, const char *device,
4121 struct changelog_private *cp;
4124 /* Set up the receiver control struct */
4125 cp = calloc(1, sizeof(*cp));
4129 cp->magic = CHANGELOG_PRIV_MAGIC;
4132 /* Set up the receiver */
4133 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */);
4139 /* Tell the kernel to start sending */
4140 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
4142 /* Only the kernel reference keeps the write side open */
4143 close(cp->kuc.lk_wfd);
4144 cp->kuc.lk_wfd = LK_NOFD;
4146 /* frees and clears priv */
4147 llapi_changelog_fini(priv);
4158 /** Finish reading from a changelog */
4159 int llapi_changelog_fini(void **priv)
4161 struct changelog_private *cp = (struct changelog_private *)*priv;
4163 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4166 libcfs_ukuc_stop(&cp->kuc);
4172 /** Convert a changelog_rec to changelog_ext_rec, in this way client can treat
4173 * all records in the format of changelog_ext_rec, this can make record
4176 static inline int changelog_extend_rec(struct changelog_ext_rec *ext)
4178 if (!CHANGELOG_REC_EXTENDED(ext)) {
4179 struct changelog_rec *rec = (struct changelog_rec *)ext;
4181 memmove(ext->cr_name, rec->cr_name, rec->cr_namelen);
4182 fid_zero(&ext->cr_sfid);
4183 fid_zero(&ext->cr_spfid);
4190 /** Read the next changelog entry
4191 * @param priv Opaque private control structure
4192 * @param rech Changelog record handle; record will be allocated here
4193 * @return 0 valid message received; rec is set
4197 int llapi_changelog_recv(void *priv, struct changelog_ext_rec **rech)
4199 struct changelog_private *cp = (struct changelog_private *)priv;
4200 struct kuc_hdr *kuch;
4203 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4207 kuch = malloc(KUC_CHANGELOG_MSG_MAXSIZE);
4212 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
4213 KUC_CHANGELOG_MSG_MAXSIZE,
4214 KUC_TRANSPORT_CHANGELOG);
4218 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
4219 ((kuch->kuc_msgtype != CL_RECORD) &&
4220 (kuch->kuc_msgtype != CL_EOF))) {
4221 llapi_err_noerrno(LLAPI_MSG_ERROR,
4222 "Unknown changelog message type %d:%d\n",
4223 kuch->kuc_transport, kuch->kuc_msgtype);
4228 if (kuch->kuc_msgtype == CL_EOF) {
4229 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
4238 /* Our message is a changelog_ext_rec. Use pointer math to skip
4239 * kuch_hdr and point directly to the message payload.
4241 *rech = (struct changelog_ext_rec *)(kuch + 1);
4242 changelog_extend_rec(*rech);
4252 /** Release the changelog record when done with it. */
4253 int llapi_changelog_free(struct changelog_ext_rec **rech)
4256 /* We allocated memory starting at the kuc_hdr, but passed
4257 * the consumer a pointer to the payload.
4258 * Use pointer math to get back to the header.
4260 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
4267 int llapi_changelog_clear(const char *mdtname, const char *idstr,
4273 llapi_err_noerrno(LLAPI_MSG_ERROR,
4274 "can't purge negative records\n");
4278 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
4279 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
4280 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
4281 llapi_err_noerrno(LLAPI_MSG_ERROR,
4282 "expecting id of the form '"
4283 CHANGELOG_USER_PREFIX
4284 "<num>'; got '%s'\n", idstr);
4288 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
4291 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
4292 int buflen, long long *recno, int *linkno)
4295 struct getinfo_fid2path *gf;
4298 while (*fidstr == '[')
4301 sscanf(fidstr, SFID, RFID(&fid));
4302 if (!fid_is_sane(&fid)) {
4303 llapi_err_noerrno(LLAPI_MSG_ERROR,
4304 "bad FID format [%s], should be "DFID"\n",
4305 fidstr, (__u64)1, 2, 0);
4309 gf = malloc(sizeof(*gf) + buflen);
4313 gf->gf_recno = *recno;
4314 gf->gf_linkno = *linkno;
4315 gf->gf_pathlen = buflen;
4317 /* Take path or fsname */
4318 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
4321 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
4323 memcpy(buf, gf->gf_path, gf->gf_pathlen);
4324 if (buf[0] == '\0') { /* ROOT path */
4328 *recno = gf->gf_recno;
4329 *linkno = gf->gf_linkno;
4336 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
4339 struct lustre_mdt_attrs *lma;
4343 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
4345 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
4348 lma = (struct lustre_mdt_attrs *)buf;
4349 fid_le_to_cpu(fid, &lma->lma_self_fid);
4353 int llapi_fd2fid(const int fd, lustre_fid *fid)
4357 memset(fid, 0, sizeof(*fid));
4359 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
4360 if (rc == -EINVAL || rc == -ENOTTY)
4361 rc = fid_from_lma(NULL, fd, fid);
4366 int llapi_path2fid(const char *path, lustre_fid *fid)
4370 memset(fid, 0, sizeof(*fid));
4371 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4373 if (errno == ELOOP || errno == ENXIO)
4374 return fid_from_lma(path, -1, fid);
4378 rc = llapi_fd2fid(fd, fid);
4379 if (rc == -EINVAL || rc == -ENOTTY)
4380 rc = fid_from_lma(path, -1, fid);
4386 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4391 root = opendir(mnt);
4394 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4398 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
4401 llapi_error(LLAPI_MSG_ERROR, rc,
4402 "ioctl on %s for getting connect flags failed", mnt);
4408 int llapi_get_version(char *buffer, int buffer_size,
4413 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
4415 fd = open(OBD_DEV_PATH, O_RDONLY);
4419 memset(buffer, 0, buffer_size);
4420 data->ioc_version = OBD_IOCTL_VERSION;
4421 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
4422 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
4423 data->ioc_len = obd_ioctl_packlen(data);
4425 rc = ioctl(fd, OBD_GET_VERSION, buffer);
4432 *version = data->ioc_bulk;
4437 * Get a 64-bit value representing the version of file data pointed by fd.
4439 * Each write or truncate, flushed on OST, will change this value. You can use
4440 * this value to verify if file data was modified. This only checks the file
4441 * data, not metadata.
4443 * \param flags 0: no flush pages, usually used it the process has already
4445 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
4447 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
4448 * pages from clients.
4450 * \retval 0 on success.
4451 * \retval -errno on error.
4453 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4456 struct ioc_data_version idv;
4458 idv.idv_flags = flags;
4460 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4464 *data_version = idv.idv_version;
4470 * Create a file without any name open it for read/write
4472 * - file is created as if it were a standard file in the given \a directory
4473 * - file does not appear in \a directory and mtime does not change because
4474 * the filename is handled specially by the Lustre MDS.
4475 * - file is removed at final close
4476 * - file modes are rw------- since it doesn't make sense to have a read-only
4477 * or write-only file that cannot be opened again.
4478 * - if user wants another mode it must use fchmod() on the open file, no
4479 * security problems arise because it cannot be opened by another process.
4481 * \param[in] directory directory from which to inherit layout/MDT idx
4482 * \param[in] idx MDT index on which the file is created,
4483 * \a idx == -1 means no specific MDT is requested
4484 * \param[in] open_flags standard open(2) flags
4486 * \retval 0 on success.
4487 * \retval -errno on error.
4489 int llapi_create_volatile_idx(char *directory, int idx, int open_flags)
4491 char file_path[PATH_MAX];
4492 char filename[PATH_MAX];
4497 fd = open("/dev/urandom", O_RDONLY);
4499 llapi_error(LLAPI_MSG_ERROR, errno,
4500 "Cannot open /dev/urandom\n");
4503 rc = read(fd, &random, sizeof(random));
4505 if (rc < sizeof(random)) {
4506 llapi_error(LLAPI_MSG_ERROR, errno,
4507 "cannot read %zu bytes from /dev/urandom",
4512 snprintf(filename, sizeof(filename),
4513 LUSTRE_VOLATILE_HDR"::%.4X", random);
4515 snprintf(filename, sizeof(filename),
4516 LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, random);
4518 rc = snprintf(file_path, sizeof(file_path),
4519 "%s/%s", directory, filename);
4520 if (rc >= sizeof(file_path))
4523 fd = open(file_path, O_RDWR | O_CREAT | open_flags, S_IRUSR | S_IWUSR);
4525 llapi_error(LLAPI_MSG_ERROR, errno,
4526 "Cannot create volatile file '%s' in '%s'\n",
4527 filename + LUSTRE_VOLATILE_HDR_LEN,
4531 /* unlink file in case this wasn't a Lustre filesystem, and the
4532 * magic volatile filename wasn't handled as intended. The effect
4540 * Swap the layouts between 2 file descriptors
4541 * the 2 files must be open in write
4542 * first fd received the ioctl, second fd is passed as arg
4543 * this is assymetric but avoid use of root path for ioctl
4545 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4547 struct lustre_swap_layouts lsl;
4550 srandom(time(NULL));
4552 lsl.sl_flags = flags;
4553 lsl.sl_gid = random();
4556 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4563 * Swap the layouts between 2 files
4564 * the 2 files are open in write
4566 int llapi_swap_layouts(const char *path1, const char *path2,
4567 __u64 dv1, __u64 dv2, __u64 flags)
4571 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4574 llapi_error(LLAPI_MSG_ERROR, rc,
4575 "error: cannot open '%s' for write", path1);
4579 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4582 llapi_error(LLAPI_MSG_ERROR, rc,
4583 "error: cannot open '%s' for write", path2);
4587 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4589 llapi_error(LLAPI_MSG_ERROR, rc,
4590 "error: cannot swap layout between '%s' and '%s'\n",