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 <liblustre.h>
73 #include <lnet/lnetctl.h>
74 #include <lustre/lustreapi.h>
75 #include <lustre_ioctl.h>
76 #include "lustreapi_internal.h"
78 static unsigned llapi_dir_filetype_table[] = {
87 #if defined(DT_DOOR) && defined(S_IFDOOR)
92 #if defined(DT_DOOR) && defined(S_IFDOOR)
93 static const int DT_MAX = DT_DOOR;
95 static const int DT_MAX = DT_SOCK;
98 static unsigned llapi_filetype_dir_table[] = {
107 #if defined(DT_DOOR) && defined(S_IFDOOR)
112 #if defined(DT_DOOR) && defined(S_IFDOOR)
113 static const int S_IFMAX = DT_DOOR;
115 static const int S_IFMAX = DT_SOCK;
118 /* liblustreapi message level */
119 static int llapi_msg_level = LLAPI_MSG_MAX;
121 void llapi_msg_set_level(int level)
123 /* ensure level is in the good range */
124 if (level < LLAPI_MSG_OFF)
125 llapi_msg_level = LLAPI_MSG_OFF;
126 else if (level > LLAPI_MSG_MAX)
127 llapi_msg_level = LLAPI_MSG_MAX;
129 llapi_msg_level = level;
132 static void error_callback_default(enum llapi_message_level level, int err,
133 const char *fmt, va_list ap)
135 vfprintf(stderr, fmt, ap);
136 if (level & LLAPI_MSG_NO_ERRNO)
137 fprintf(stderr, "\n");
139 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
142 static void info_callback_default(enum llapi_message_level level, int err,
143 const char *fmt, va_list ap)
145 vfprintf(stdout, fmt, ap);
148 static llapi_log_callback_t llapi_error_callback = error_callback_default;
149 static llapi_log_callback_t llapi_info_callback = info_callback_default;
152 /* llapi_error will preserve errno */
153 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
156 int tmp_errno = errno;
158 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
162 llapi_error_callback(level, abs(err), fmt, args);
167 /* llapi_printf will preserve errno */
168 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
171 int tmp_errno = errno;
173 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
177 llapi_info_callback(level, 0, fmt, args);
183 * Set a custom error logging function. Passing in NULL will reset the logging
184 * callback to its default value.
186 * This function returns the value of the old callback.
188 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
190 llapi_log_callback_t old = llapi_error_callback;
193 llapi_error_callback = cb;
195 llapi_error_callback = error_callback_default;
201 * Set a custom info logging function. Passing in NULL will reset the logging
202 * callback to its default value.
204 * This function returns the value of the old callback.
206 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
208 llapi_log_callback_t old = llapi_info_callback;
211 llapi_info_callback = cb;
213 llapi_info_callback = info_callback_default;
219 * size_units is to be initialized (or zeroed) by caller.
221 int llapi_parse_size(const char *optarg, unsigned long long *size,
222 unsigned long long *size_units, int bytes_spec)
226 if (strncmp(optarg, "-", 1) == 0)
229 if (*size_units == 0)
232 *size = strtoull(optarg, &end, 0);
235 if ((*end == 'b') && *(end + 1) == '\0' &&
236 (*size & (~0ULL << (64 - 9))) == 0 &&
238 *size_units = 1 << 9;
239 } else if ((*end == 'b') &&
240 *(end + 1) == '\0' &&
243 } else if ((*end == 'k' || *end == 'K') &&
244 *(end + 1) == '\0' &&
245 (*size & (~0ULL << (64 - 10))) == 0) {
246 *size_units = 1 << 10;
247 } else if ((*end == 'm' || *end == 'M') &&
248 *(end + 1) == '\0' &&
249 (*size & (~0ULL << (64 - 20))) == 0) {
250 *size_units = 1 << 20;
251 } else if ((*end == 'g' || *end == 'G') &&
252 *(end + 1) == '\0' &&
253 (*size & (~0ULL << (64 - 30))) == 0) {
254 *size_units = 1 << 30;
255 } else if ((*end == 't' || *end == 'T') &&
256 *(end + 1) == '\0' &&
257 (*size & (~0ULL << (64 - 40))) == 0) {
258 *size_units = 1ULL << 40;
259 } else if ((*end == 'p' || *end == 'P') &&
260 *(end + 1) == '\0' &&
261 (*size & (~0ULL << (64 - 50))) == 0) {
262 *size_units = 1ULL << 50;
263 } else if ((*end == 'e' || *end == 'E') &&
264 *(end + 1) == '\0' &&
265 (*size & (~0ULL << (64 - 60))) == 0) {
266 *size_units = 1ULL << 60;
271 *size *= *size_units;
275 /* XXX: llapi_xxx() functions return negative values upon failure */
277 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
278 int stripe_count, int stripe_pattern)
282 /* 64 KB is the largest common page size I'm aware of (on ia64), but
283 * check the local page size just in case. */
284 page_size = LOV_MIN_STRIPE_SIZE;
285 if (getpagesize() > page_size) {
286 page_size = getpagesize();
287 llapi_err_noerrno(LLAPI_MSG_WARN,
288 "warning: your page size (%u) is "
289 "larger than expected (%u)", page_size,
290 LOV_MIN_STRIPE_SIZE);
292 if ((stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
294 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
295 "must be an even multiple of %d bytes",
296 stripe_size, page_size);
299 if (stripe_offset < -1) {
301 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
305 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
307 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
311 if (stripe_size >= (1ULL << 32)) {
313 llapi_error(LLAPI_MSG_ERROR, rc,
314 "warning: stripe size 4G or larger "
315 "is not currently supported and would wrap");
321 /* return the first file matching this pattern */
322 static int first_match(char *pattern, char *buffer)
326 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
329 if (glob_info.gl_pathc < 1) {
330 globfree(&glob_info);
334 strcpy(buffer, glob_info.gl_pathv[0]);
336 globfree(&glob_info);
340 static int find_target_obdpath(char *fsname, char *path)
343 char pattern[PATH_MAX + 1];
346 snprintf(pattern, PATH_MAX,
347 "/proc/fs/lustre/lov/%s-*/target_obd",
349 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
350 if (rc == GLOB_NOMATCH)
355 strcpy(path, glob_info.gl_pathv[0]);
356 globfree(&glob_info);
360 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
363 char pattern[PATH_MAX + 1];
366 snprintf(pattern, PATH_MAX,
367 "/proc/fs/lustre/lov/%s-*/pools/%s",
369 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
370 /* If no pools, make sure the lov is available */
371 if ((rc == GLOB_NOMATCH) &&
372 (find_target_obdpath(fsname, poolpath) == -ENODEV))
377 strcpy(poolpath, glob_info.gl_pathv[0]);
378 globfree(&glob_info);
383 * Trim a trailing newline from a string, if it exists.
385 int llapi_chomp_string(char *buf)
401 * return a parameter string for a specific device type or mountpoint
403 * \param param_path the path to the file containing parameter data
404 * \param result buffer for parameter value string
405 * \param result_size size of buffer for return value
407 * The \param param_path is appended to /proc/{fs,sys}/{lnet,lustre} to
408 * complete the absolute path to the file containing the parameter data
409 * the user is requesting. If that file exist then the data is read from
410 * the file and placed into the \param result buffer that is passed by
411 * the user. Data is only copied up to the \param result_size to prevent
412 * overflow of the array.
414 * Return 0 for success, with a NUL-terminated string in \param result.
415 * Return -ve value for error.
417 int get_param(const char *param_path, char *result,
418 unsigned int result_size)
420 char file[PATH_MAX + 1], pattern[PATH_MAX + 1], buf[result_size];
424 snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
426 rc = first_match(pattern, file);
427 if (rc != 0 || result == NULL)
430 fp = fopen(file, "r");
432 while (fgets(buf, result_size, fp) != NULL)
441 #define DEVICES_LIST "/proc/fs/lustre/devices"
444 * return a parameter string for a specific device type or mountpoint
446 * \param fsname Lustre filesystem name (optional)
447 * \param file_path path to file in filesystem (optional, if fsname unset)
448 * \param obd_type Lustre OBD device type
449 * \param param_name parameter name to fetch
450 * \param value return buffer for parameter value string
451 * \param val_len size of buffer for return value
453 * If fsname is specified then the parameter will be from that filesystem
454 * (if it exists). If file_path is given and it is in a mounted Lustre
455 * filesystem, then the parameter will be otherwise the value may be
456 * from any mounted filesystem (if there is more than one).
458 * If "obd_type" matches a Lustre device then the first matching device
459 * (as with "lctl dl", constrained by \param fsname or \param mount_path)
460 * will be used to provide the return value, otherwise the first such
461 * device found will be used.
463 * Return 0 for success, with a NUL-terminated string in \param buffer.
464 * Return -ve value for error.
466 static int get_param_obdvar(const char *fsname, const char *file_path,
467 const char *obd_type, const char *param_name,
468 char *value, unsigned int val_len)
470 char devices[PATH_MAX + 1], dev[PATH_MAX + 1] = "*", fs[PATH_MAX + 1];
471 FILE *fp = fopen(DEVICES_LIST, "r");
474 if (!fsname && file_path) {
475 rc = llapi_search_fsname(file_path, fs);
477 llapi_error(LLAPI_MSG_ERROR, rc,
478 "'%s' is not on a Lustre filesystem",
485 if (strlen(fsname) > sizeof(fs)-1) {
490 strncpy(fs, fsname, sizeof(fs));
495 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
499 while (fgets(devices, sizeof(devices), fp) != NULL) {
500 char *bufp = devices, *tmp;
502 while (bufp[0] == ' ')
505 tmp = strstr(bufp, obd_type);
507 tmp += strlen(obd_type) + 1;
510 if (strlen(tmp) > sizeof(dev)-1) {
514 strncpy(dev, tmp, sizeof(dev));
515 tmp = strchr(dev, ' ');
522 if (dev[0] == '*' && strlen(fs))
523 snprintf(dev, PATH_MAX, "%s-*", fs);
524 snprintf(devices, PATH_MAX, "%s/%s/%s", obd_type, dev, param_name);
526 return get_param(devices, value, val_len);
530 * TYPE one of llite, lmv, lov.
531 * /proc/fs/lustre/TYPE/INST the directory of interest.
533 static int get_param_cli(const char *type, const char *inst,
534 const char *param, char *buf, size_t buf_size)
536 char param_path[PATH_MAX + 1];
537 FILE *param_file = NULL;
540 snprintf(param_path, sizeof(param_path),
541 "/proc/fs/lustre/%s/%s/%s", type, inst, param);
543 param_file = fopen(param_path, "r");
544 if (param_file == NULL) {
549 if (fgets(buf, buf_size, param_file) == NULL) {
556 if (param_file != NULL)
562 static int get_param_llite(const char *path,
563 const char *param, char *buf, size_t buf_size)
568 rc = llapi_getname(path, inst, sizeof(inst));
572 return get_param_cli("llite", inst, param, buf, buf_size);
575 static int get_param_lov(const char *path,
576 const char *param, char *buf, size_t buf_size)
578 struct obd_uuid uuid;
581 rc = llapi_file_get_lov_uuid(path, &uuid);
585 return get_param_cli("lov", uuid.uuid, param, buf, buf_size);
588 static int get_param_lmv(const char *path,
589 const char *param, char *buf, size_t buf_size)
591 struct obd_uuid uuid;
594 rc = llapi_file_get_lmv_uuid(path, &uuid);
598 return get_param_cli("lmv", uuid.uuid, param, buf, buf_size);
601 static int get_mds_md_size(const char *path)
603 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
607 /* Get the max ea size from llite proc. */
608 rc = get_param_llite(path, "max_easize", buf, sizeof(buf));
620 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
622 return get_param_lmv(path, "uuid", buf, bufsize);
626 * if pool is NULL, search ostname in target_obd
627 * if pool is not NULL:
628 * if pool not found returns errno < 0
629 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
630 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
632 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
635 char buffer[PATH_MAX + 1];
639 len = strlen(ostname);
641 if (poolname == NULL) {
645 rc = find_target_obdpath(fsname, buffer);
647 rc = find_poolpath(fsname, poolname, buffer);
652 fd = fopen(buffer, "r");
656 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
657 if (poolname == NULL) {
659 /* Search for an ostname in the list of OSTs
660 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
661 ptr = strchr(buffer, ' ');
663 (strncmp(ptr + 1, ostname, len) == 0)) {
668 /* Search for an ostname in a pool,
669 (or an existing non-empty pool if no ostname) */
670 if ((ostname == NULL) ||
671 (strncmp(buffer, ostname, len) == 0)) {
681 int llapi_file_open_pool(const char *name, int flags, int mode,
682 unsigned long long stripe_size, int stripe_offset,
683 int stripe_count, int stripe_pattern, char *pool_name)
685 struct lov_user_md_v3 lum = { 0 };
688 /* Make sure we have a good pool */
689 if (pool_name != NULL) {
690 char fsname[MAX_OBD_NAME + 1], *ptr;
692 rc = llapi_search_fsname(name, fsname);
694 llapi_error(LLAPI_MSG_ERROR, rc,
695 "'%s' is not on a Lustre filesystem",
700 /* in case user gives the full pool name <fsname>.<poolname>,
701 * strip the fsname */
702 ptr = strchr(pool_name, '.');
705 if (strcmp(pool_name, fsname) != 0) {
707 llapi_err_noerrno(LLAPI_MSG_ERROR,
708 "Pool '%s' is not on filesystem '%s'",
715 /* Make sure the pool exists and is non-empty */
716 rc = llapi_search_ost(fsname, pool_name, NULL);
718 llapi_err_noerrno(LLAPI_MSG_ERROR,
719 "pool '%s.%s' %s", fsname, pool_name,
720 rc == 0 ? "has no OSTs" : "does not exist");
726 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
728 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
729 flags = O_DIRECTORY | O_RDONLY;
736 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
740 rc = llapi_stripe_limit_check(stripe_size, stripe_offset, stripe_count,
745 /* Initialize IOCTL striping pattern structure */
746 lum.lmm_magic = LOV_USER_MAGIC_V3;
747 lum.lmm_pattern = stripe_pattern;
748 lum.lmm_stripe_size = stripe_size;
749 lum.lmm_stripe_count = stripe_count;
750 lum.lmm_stripe_offset = stripe_offset;
751 if (pool_name != NULL) {
752 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
754 /* If no pool is specified at all, use V1 request */
755 lum.lmm_magic = LOV_USER_MAGIC_V1;
758 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
759 char *errmsg = "stripe already set";
761 if (errno != EEXIST && errno != EALREADY)
762 errmsg = strerror(errno);
764 llapi_err_noerrno(LLAPI_MSG_ERROR,
765 "error on ioctl "LPX64" for '%s' (%d): %s",
766 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
777 int llapi_file_open(const char *name, int flags, int mode,
778 unsigned long long stripe_size, int stripe_offset,
779 int stripe_count, int stripe_pattern)
781 return llapi_file_open_pool(name, flags, mode, stripe_size,
782 stripe_offset, stripe_count,
783 stripe_pattern, NULL);
786 int llapi_file_create(const char *name, unsigned long long stripe_size,
787 int stripe_offset, int stripe_count, int stripe_pattern)
791 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
792 stripe_offset, stripe_count, stripe_pattern,
801 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
802 int stripe_offset, int stripe_count,
803 int stripe_pattern, char *pool_name)
807 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
808 stripe_offset, stripe_count, stripe_pattern,
817 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
818 int stripe_count, int stripe_pattern,
819 const char *pool_name)
821 struct lmv_user_md lum = { 0 };
825 lum.lum_magic = LMV_USER_MAGIC;
826 lum.lum_stripe_offset = stripe_offset;
827 lum.lum_stripe_count = stripe_count;
828 lum.lum_hash_type = stripe_pattern;
829 if (pool_name != NULL) {
830 if (strlen(pool_name) >= sizeof(lum.lum_pool_name)) {
831 llapi_err_noerrno(LLAPI_MSG_ERROR,
832 "error LL_IOC_LMV_SET_DEFAULT_STRIPE '%s'"
833 ": too large pool name: %s", name, pool_name);
836 strncpy(lum.lum_pool_name, pool_name, strlen(pool_name));
839 fd = open(name, O_DIRECTORY | O_RDONLY);
842 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
846 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lum);
848 char *errmsg = "stripe already set";
850 if (errno != EEXIST && errno != EALREADY)
851 errmsg = strerror(errno);
853 llapi_err_noerrno(LLAPI_MSG_ERROR,
854 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
861 int llapi_dir_create_pool(const char *name, int flags, int stripe_offset,
862 int stripe_count, int stripe_pattern,
863 const char *pool_name)
865 struct lmv_user_md lmu = { 0 };
866 struct obd_ioctl_data data = { 0 };
869 char *dirpath = NULL;
870 char *namepath = NULL;
876 dirpath = strdup(name);
877 namepath = strdup(name);
878 if (!dirpath || !namepath)
881 lmu.lum_magic = LMV_USER_MAGIC;
882 lmu.lum_stripe_offset = stripe_offset;
883 lmu.lum_stripe_count = stripe_count;
884 lmu.lum_hash_type = stripe_pattern;
885 if (pool_name != NULL) {
886 if (strlen(pool_name) >= LOV_MAXPOOLNAME) {
887 llapi_err_noerrno(LLAPI_MSG_ERROR,
888 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
889 "pool name: %s", name, pool_name);
893 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
896 filename = basename(namepath);
897 dir = dirname(dirpath);
899 data.ioc_inlbuf1 = (char *)filename;
900 data.ioc_inllen1 = strlen(filename) + 1;
901 data.ioc_inlbuf2 = (char *)&lmu;
902 data.ioc_inllen2 = sizeof(struct lmv_user_md);
903 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
905 llapi_error(LLAPI_MSG_ERROR, rc,
906 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
911 fd = open(dir, O_DIRECTORY | O_RDONLY);
914 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
918 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
919 char *errmsg = "stripe already set";
921 if (errno != EEXIST && errno != EALREADY)
922 errmsg = strerror(errno);
924 llapi_err_noerrno(LLAPI_MSG_ERROR,
925 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
935 int llapi_direntry_remove(char *dname)
937 char *dirpath = NULL;
938 char *namepath = NULL;
944 dirpath = strdup(dname);
945 namepath = strdup(dname);
946 if (!dirpath || !namepath)
949 filename = basename(namepath);
951 dir = dirname(dirpath);
953 fd = open(dir, O_DIRECTORY | O_RDONLY);
956 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
961 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
962 char *errmsg = strerror(errno);
963 llapi_err_noerrno(LLAPI_MSG_ERROR,
964 "error on ioctl "LPX64" for '%s' (%d): %s",
965 (__u64)LL_IOC_LMV_SETSTRIPE, filename,
977 * Find the fsname, the full path, and/or an open fd.
978 * Either the fsname or path must not be NULL
980 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
983 char buf[PATH_MAX], mntdir[PATH_MAX];
986 int idx = 0, len = 0, mntlen, fd;
989 /* get the mount point */
990 fp = setmntent(MOUNTED, "r");
993 llapi_error(LLAPI_MSG_ERROR, rc,
994 "setmntent(%s) failed", MOUNTED);
998 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
1001 if (!llapi_is_lustre_mnt(&mnt))
1004 if ((want & WANT_INDEX) && (idx++ != index))
1007 mntlen = strlen(mnt.mnt_dir);
1008 ptr = strrchr(mnt.mnt_fsname, '/');
1009 /* thanks to the call to llapi_is_lustre_mnt() above,
1010 * we are sure that mnt.mnt_fsname contains ":/",
1011 * so ptr should never be NULL */
1016 /* Check the fsname for a match, if given */
1017 if (!(want & WANT_FSNAME) && fsname != NULL &&
1018 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
1021 /* If the path isn't set return the first one we find */
1022 if (path == NULL || strlen(path) == 0) {
1023 strcpy(mntdir, mnt.mnt_dir);
1024 if ((want & WANT_FSNAME) && fsname != NULL)
1025 strcpy(fsname, ptr);
1028 /* Otherwise find the longest matching path */
1029 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1030 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1031 strcpy(mntdir, mnt.mnt_dir);
1033 if ((want & WANT_FSNAME) && fsname != NULL)
1034 strcpy(fsname, ptr);
1042 if ((want & WANT_PATH) && path != NULL)
1043 strcpy(path, mntdir);
1044 if (want & WANT_FD) {
1045 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1048 llapi_error(LLAPI_MSG_ERROR, rc,
1049 "error opening '%s'", mntdir);
1055 } else if (want & WANT_ERROR)
1056 llapi_err_noerrno(LLAPI_MSG_ERROR,
1057 "can't find fs root for '%s': %d",
1058 (want & WANT_PATH) ? fsname : path, rc);
1063 * search lustre mounts
1065 * Calling this function will return to the user the mount point, mntdir, and
1066 * the file system name, fsname, if the user passed a buffer to this routine.
1068 * The user inputs are pathname and index. If the pathname is supplied then
1069 * the value of the index will be ignored. The pathname will return data if
1070 * the pathname is located on a lustre mount. Index is used to pick which
1071 * mount point you want in the case of multiple mounted lustre file systems.
1072 * See function lfs_osts in lfs.c for a example of the index use.
1074 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1077 int want = WANT_PATH, idx = -1;
1079 if (!pathname || pathname[0] == '\0') {
1083 strcpy(mntdir, pathname);
1086 want |= WANT_FSNAME;
1087 return get_root_path(want, fsname, NULL, mntdir, idx);
1090 /* Given a path, find the corresponding Lustre fsname */
1091 int llapi_search_fsname(const char *pathname, char *fsname)
1096 path = realpath(pathname, NULL);
1098 char buf[PATH_MAX + 1], *ptr;
1101 if (pathname[0] != '/') {
1102 /* Need an absolute path, but realpath() only works for
1103 * pathnames that actually exist. We go through the
1104 * extra hurdle of dirname(getcwd() + pathname) in
1105 * case the relative pathname contains ".." in it. */
1106 if (getcwd(buf, sizeof(buf) - 1) == NULL)
1110 strncat(buf, pathname, sizeof(buf) - strlen(buf));
1111 path = realpath(buf, NULL);
1113 ptr = strrchr(buf, '/');
1117 path = realpath(buf, NULL);
1120 llapi_error(LLAPI_MSG_ERROR, rc,
1121 "pathname '%s' cannot expand",
1127 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1132 int llapi_search_rootpath(char *pathname, const char *fsname)
1134 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1137 int llapi_getname(const char *path, char *buf, size_t size)
1139 struct obd_uuid uuid_buf;
1140 char *uuid = uuid_buf.uuid;
1143 memset(&uuid_buf, 0, sizeof(uuid_buf));
1144 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1148 /* We want to turn lustre-clilov-ffff88002738bc00 into
1149 * lustre-ffff88002738bc00. */
1151 nr = snprintf(buf, size, "%.*s-%s",
1152 (int) (strlen(uuid) - 24), uuid,
1153 uuid + strlen(uuid) - 16);
1163 * find the pool directory path under /proc
1164 * (can be also used to test if a fsname is known)
1166 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
1169 char pattern[PATH_MAX + 1];
1170 char buffer[PATH_MAX];
1172 if (fsname == NULL) {
1173 rc = llapi_search_fsname(pathname, buffer);
1177 strcpy(pathname, fsname);
1180 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
1181 rc = first_match(pattern, buffer);
1185 /* in fsname test mode, pool_pathname is NULL */
1186 if (pool_pathname != NULL)
1187 strcpy(pool_pathname, buffer);
1193 * Get the list of pool members.
1194 * \param poolname string of format \<fsname\>.\<poolname\>
1195 * \param members caller-allocated array of char*
1196 * \param list_size size of the members array
1197 * \param buffer caller-allocated buffer for storing OST names
1198 * \param buffer_size size of the buffer
1200 * \return number of members retrieved for this pool
1201 * \retval -error failure
1203 int llapi_get_poolmembers(const char *poolname, char **members,
1204 int list_size, char *buffer, int buffer_size)
1206 char fsname[PATH_MAX + 1];
1208 char pathname[PATH_MAX + 1];
1209 char path[PATH_MAX + 1];
1216 /* name is FSNAME.POOLNAME */
1217 if (strlen(poolname) > PATH_MAX)
1219 strcpy(fsname, poolname);
1220 pool = strchr(fsname, '.');
1227 rc = poolpath(fsname, NULL, pathname);
1229 llapi_error(LLAPI_MSG_ERROR, rc,
1230 "Lustre filesystem '%s' not found",
1235 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1236 sprintf(path, "%s/%s", pathname, pool);
1237 fd = fopen(path, "r");
1240 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
1245 while (fgets(buf, sizeof(buf), fd) != NULL) {
1246 if (nb_entries >= list_size) {
1251 tmp = strchr(buf, '\n');
1254 if (used + strlen(buf) + 1 > buffer_size) {
1259 strcpy(buffer + used, buf);
1260 members[nb_entries] = buffer + used;
1261 used += strlen(buf) + 1;
1271 * Get the list of pools in a filesystem.
1272 * \param name filesystem name or path
1273 * \param poollist caller-allocated array of char*
1274 * \param list_size size of the poollist array
1275 * \param buffer caller-allocated buffer for storing pool names
1276 * \param buffer_size size of the buffer
1278 * \return number of pools retrieved for this filesystem
1279 * \retval -error failure
1281 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1282 char *buffer, int buffer_size)
1284 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
1288 struct dirent *cookie = NULL;
1290 unsigned int nb_entries = 0;
1291 unsigned int used = 0;
1294 /* initilize output array */
1295 for (i = 0; i < list_size; i++)
1298 /* is name a pathname ? */
1299 ptr = strchr(name, '/');
1301 /* only absolute pathname is supported */
1305 if (!realpath(name, rname)) {
1307 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1312 rc = poolpath(NULL, rname, pathname);
1314 llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
1315 " a Lustre filesystem", name);
1318 if (strlen(rname) > sizeof(fsname)-1)
1320 strncpy(fsname, rname, sizeof(fsname));
1322 /* name is FSNAME */
1323 if (strlen(name) > sizeof(fsname)-1)
1325 strncpy(fsname, name, sizeof(fsname));
1326 rc = poolpath(fsname, NULL, pathname);
1329 llapi_error(LLAPI_MSG_ERROR, rc,
1330 "Lustre filesystem '%s' not found", name);
1334 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1335 dir = opendir(pathname);
1338 llapi_error(LLAPI_MSG_ERROR, rc,
1339 "Could not open pool list for '%s'",
1345 rc = readdir_r(dir, &pool, &cookie);
1349 llapi_error(LLAPI_MSG_ERROR, rc,
1350 "Error reading pool list for '%s'", name);
1352 } else if ((rc == 0) && (cookie == NULL)) {
1353 /* end of directory */
1357 /* ignore . and .. */
1358 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1361 /* check output bounds */
1362 if (nb_entries >= list_size) {
1367 /* +2 for '.' and final '\0' */
1368 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1374 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1375 poollist[nb_entries] = buffer + used;
1376 used += strlen(pool.d_name) + strlen(fsname) + 2;
1382 return ((rc != 0) ? rc : nb_entries);
1385 /* wrapper for lfs.c and obd.c */
1386 int llapi_poollist(const char *name)
1388 /* list of pool names (assume that pool count is smaller
1390 char **list, *buffer = NULL, *path = NULL, *fsname = NULL;
1391 int obdcount, bufsize, rc, nb, i;
1392 char *poolname = NULL, *tmp = NULL, data[16];
1394 if (name[0] != '/') {
1395 fsname = strdup(name);
1396 poolname = strchr(fsname, '.');
1400 path = (char *) name;
1403 rc = get_param_obdvar(fsname, path, "lov", "numobd",
1404 data, sizeof(data));
1407 obdcount = atoi(data);
1409 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1410 * and also an array to store the pointers for all that
1411 * allocated space. */
1413 bufsize = sizeof(struct obd_uuid) * obdcount;
1414 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1415 if (buffer == NULL) {
1419 list = (char **) (buffer + bufsize);
1422 /* name is a path or fsname */
1423 nb = llapi_get_poollist(name, list, obdcount,
1426 /* name is a pool name (<fsname>.<poolname>) */
1427 nb = llapi_get_poolmembers(name, list, obdcount,
1431 if (nb == -EOVERFLOW) {
1434 goto retry_get_pools;
1437 for (i = 0; i < nb; i++)
1438 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1439 rc = (nb < 0 ? nb : 0);
1448 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1449 void *data, struct dirent64 *de);
1451 #define OBD_NOT_FOUND (-1)
1453 static int common_param_init(struct find_param *param, char *path)
1455 int lumlen = get_mds_md_size(path);
1457 if (lumlen < PATH_MAX + 1)
1458 lumlen = PATH_MAX + 1;
1460 param->lumlen = lumlen;
1461 param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
1462 if (param->lmd == NULL) {
1463 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1464 "error: allocation of %zu bytes for ioctl",
1465 sizeof(lstat_t) + param->lumlen);
1469 param->fp_lmv_count = 256;
1470 param->fp_lmv_md = malloc(lmv_user_md_size(256, LMV_MAGIC_V1));
1471 if (param->fp_lmv_md == NULL) {
1472 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1473 "error: allocation of %d bytes for ioctl",
1474 lmv_user_md_size(256, LMV_MAGIC_V1));
1478 param->got_uuids = 0;
1479 param->obdindexes = NULL;
1480 param->obdindex = OBD_NOT_FOUND;
1481 if (!param->migrate)
1482 param->mdtindex = OBD_NOT_FOUND;
1486 static void find_param_fini(struct find_param *param)
1488 if (param->obdindexes)
1489 free(param->obdindexes);
1494 if (param->fp_lmv_md)
1495 free(param->fp_lmv_md);
1498 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1499 struct dirent64 *de)
1501 struct find_param *param = (struct find_param *)data;
1506 /* set errno upon failure */
1507 static DIR *opendir_parent(char *path)
1513 fname = strrchr(path, '/');
1515 return opendir(".");
1519 parent = opendir(path);
1524 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1526 struct lmv_user_md *lmv = (struct lmv_user_md *)param->fp_lmv_md;
1529 lmv->lum_stripe_count = param->fp_lmv_count;
1530 if (param->get_default_lmv)
1531 lmv->lum_magic = LMV_USER_MAGIC;
1533 lmv->lum_magic = LMV_MAGIC_V1;
1534 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, lmv);
1539 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1540 struct lov_user_mds_data *lmd, int lumlen)
1542 lstat_t *st = &lmd->lmd_st;
1545 if (parent == NULL && dir == NULL)
1549 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1550 } else if (parent) {
1551 char *fname = strrchr(path, '/');
1553 fname = (fname == NULL ? path : fname + 1);
1554 /* retrieve needed file info */
1555 strncpy((char *)lmd, fname, lumlen);
1556 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1560 if (errno == ENOTTY) {
1561 /* ioctl is not supported, it is not a lustre fs.
1562 * Do the regular lstat(2) instead. */
1563 ret = lstat_f(path, st);
1566 llapi_error(LLAPI_MSG_ERROR, ret,
1567 "error: %s: lstat failed for %s",
1570 } else if (errno == ENOENT) {
1572 llapi_error(LLAPI_MSG_WARN, ret,
1573 "warning: %s: %s does not exist",
1575 } else if (errno != EISDIR) {
1577 llapi_error(LLAPI_MSG_ERROR, ret,
1578 "%s ioctl failed for %s.",
1579 dir ? "LL_IOC_MDC_GETINFO" :
1580 "IOC_MDC_GETFILEINFO", path);
1583 llapi_error(LLAPI_MSG_ERROR, ret,
1584 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1591 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1592 semantic_func_t sem_init,
1593 semantic_func_t sem_fini, void *data,
1594 struct dirent64 *de)
1596 struct find_param *param = (struct find_param *)data;
1597 struct dirent64 *dent;
1605 if (!d && errno != ENOTDIR) {
1607 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1610 } else if (!d && !parent) {
1611 /* ENOTDIR. Open the parent dir. */
1612 p = opendir_parent(path);
1619 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1622 if (!d || (param->get_lmv && !param->recursive)) {
1627 while ((dent = readdir64(d)) != NULL) {
1628 param->have_fileinfo = 0;
1630 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1633 /* Don't traverse .lustre directory */
1634 if (!(strcmp(dent->d_name, dot_lustre_name)))
1638 if ((len + dent->d_reclen + 2) > size) {
1639 llapi_err_noerrno(LLAPI_MSG_ERROR,
1640 "error: %s: string buffer is too small",
1645 strcat(path, dent->d_name);
1647 if (dent->d_type == DT_UNKNOWN) {
1648 lstat_t *st = ¶m->lmd->lmd_st;
1650 ret = get_lmd_info(path, d, NULL, param->lmd,
1654 llapi_filetype_dir_table[st->st_mode &
1660 switch (dent->d_type) {
1662 llapi_err_noerrno(LLAPI_MSG_ERROR,
1663 "error: %s: '%s' is UNKNOWN type %d",
1664 __func__, dent->d_name, dent->d_type);
1667 ret = llapi_semantic_traverse(path, size, d, sem_init,
1668 sem_fini, data, dent);
1675 ret = sem_init(path, d, NULL, data, dent);
1679 if (sem_fini && ret == 0)
1680 sem_fini(path, d, NULL, data, dent);
1688 sem_fini(path, parent, &d, data, de);
1697 static int param_callback(char *path, semantic_func_t sem_init,
1698 semantic_func_t sem_fini, struct find_param *param)
1700 int ret, len = strlen(path);
1703 if (len > PATH_MAX) {
1705 llapi_error(LLAPI_MSG_ERROR, ret,
1706 "Path name '%s' is too long", path);
1710 buf = (char *)malloc(PATH_MAX + 1);
1714 strncpy(buf, path, PATH_MAX + 1);
1715 ret = common_param_init(param, buf);
1720 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1721 sem_fini, param, NULL);
1723 find_param_fini(param);
1725 return ret < 0 ? ret : 0;
1728 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1730 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1733 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1738 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1740 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1743 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1748 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1752 fd = open(path, O_RDONLY);
1755 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1759 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1765 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1769 fd = open(path, O_RDONLY);
1772 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1776 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1787 * If uuidp is NULL, return the number of available obd uuids.
1788 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1789 * there are more OSTs then allocated to uuidp, then an error is returned with
1790 * the ost_count set to number of available obd uuids.
1792 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1793 int *ost_count, enum tgt_type type)
1795 struct obd_uuid name;
1798 int rc = 0, index = 0;
1800 /* Get the lov name */
1801 if (type == LOV_TYPE) {
1802 rc = llapi_file_fget_lov_uuid(fd, &name);
1806 rc = llapi_file_fget_lmv_uuid(fd, &name);
1811 /* Now get the ost uuids from /proc */
1812 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1813 type == LOV_TYPE ? "lov" : "lmv", name.uuid);
1814 fp = fopen(buf, "r");
1817 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1821 while (fgets(buf, sizeof(buf), fp) != NULL) {
1822 if (uuidp && (index < *ost_count)) {
1823 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
1831 if (uuidp && (index > *ost_count))
1838 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1840 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1843 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1848 root = opendir(mnt);
1851 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1856 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1864 /* Check if user specified value matches a real uuid. Ignore _UUID,
1865 * -osc-4ba41334, other trailing gunk in comparison.
1866 * @param real_uuid ends in "_UUID"
1867 * @param search_uuid may or may not end in "_UUID"
1869 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1871 int cmplen = strlen(real_uuid);
1872 int searchlen = strlen(search_uuid);
1874 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1876 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1879 /* The UUIDs may legitimately be different lengths, if
1880 * the system was upgraded from an older version. */
1881 if (cmplen != searchlen)
1884 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1887 /* Here, param->obduuid points to a single obduuid, the index of which is
1888 * returned in param->obdindex */
1889 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1891 struct obd_uuid obd_uuid;
1892 char uuid[sizeof(struct obd_uuid)];
1897 if (param->got_uuids)
1900 /* Get the lov/lmv name */
1902 rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
1904 rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
1906 if (rc != -ENOTTY) {
1907 llapi_error(LLAPI_MSG_ERROR, rc,
1908 "error: can't get lov name: %s", dname);
1915 param->got_uuids = 1;
1917 /* Now get the ost uuids from /proc */
1918 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1919 param->get_lmv ? "lmv" : "lov", obd_uuid.uuid);
1920 fp = fopen(buf, "r");
1923 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1927 if (!param->obduuid && !param->quiet && !param->obds_printed)
1928 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1929 param->get_lmv ? "MDTS" : "OBDS:");
1931 while (fgets(buf, sizeof(buf), fp) != NULL) {
1932 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
1935 if (param->obduuid) {
1936 if (llapi_uuid_match(uuid, param->obduuid->uuid)) {
1937 param->obdindex = index;
1940 } else if (!param->quiet && !param->obds_printed) {
1941 /* Print everything */
1942 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1945 param->obds_printed = 1;
1949 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1950 llapi_err_noerrno(LLAPI_MSG_ERROR,
1951 "error: %s: unknown obduuid: %s",
1952 __func__, param->obduuid->uuid);
1959 /* In this case, param->obduuid will be an array of obduuids and
1960 * obd index for all these obduuids will be returned in
1961 * param->obdindexes */
1962 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1963 int num_obds, int **obdindexes, int *obdindex,
1966 int ret, obdcount, obd_valid = 0, obdnum;
1968 struct obd_uuid *uuids = NULL;
1972 if (type == LOV_TYPE)
1973 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
1975 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
1979 obdcount = atoi(buf);
1980 uuids = (struct obd_uuid *)malloc(obdcount *
1981 sizeof(struct obd_uuid));
1986 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1988 struct obd_uuid *uuids_temp;
1990 if (ret == -EOVERFLOW) {
1991 uuids_temp = realloc(uuids, obdcount *
1992 sizeof(struct obd_uuid));
1993 if (uuids_temp != NULL) {
1995 goto retry_get_uuids;
2001 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
2005 indexes = malloc(num_obds * sizeof(*obdindex));
2006 if (indexes == NULL) {
2011 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2014 /* The user may have specified a simple index */
2015 i = strtol(obduuids[obdnum].uuid, &end, 0);
2016 if (end && *end == '\0' && i < obdcount) {
2017 indexes[obdnum] = i;
2020 for (i = 0; i < obdcount; i++) {
2021 if (llapi_uuid_match(uuids[i].uuid,
2022 obduuids[obdnum].uuid)) {
2023 indexes[obdnum] = i;
2029 if (i >= obdcount) {
2030 indexes[obdnum] = OBD_NOT_FOUND;
2031 llapi_err_noerrno(LLAPI_MSG_ERROR,
2032 "error: %s: unknown obduuid: %s",
2033 __func__, obduuids[obdnum].uuid);
2039 *obdindex = OBD_NOT_FOUND;
2041 *obdindex = obd_valid;
2043 *obdindexes = indexes;
2051 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2055 if (param->mdtuuid) {
2056 ret = setup_indexes(dir, path, param->mdtuuid, param->num_mdts,
2057 ¶m->mdtindexes, ¶m->mdtindex, LMV_TYPE);
2061 if (param->obduuid) {
2062 ret = setup_indexes(dir, path, param->obduuid, param->num_obds,
2063 ¶m->obdindexes, ¶m->obdindex, LOV_TYPE);
2067 param->got_uuids = 1;
2071 int llapi_ostlist(char *path, struct find_param *param)
2076 dir = opendir(path);
2080 ret = setup_obd_uuid(dir, path, param);
2087 * Given a filesystem name, or a pathname of a file on a lustre filesystem,
2088 * tries to determine the path to the filesystem's clilov directory under /proc
2090 * fsname is limited to MTI_NAME_MAXLEN in lustre_idl.h
2091 * The NUL terminator is compensated by the additional "%s" bytes. */
2092 #define LOV_LEN (sizeof("/proc/fs/lustre/lov/%s-clilov-*") + MTI_NAME_MAXLEN)
2093 static int clilovpath(const char *fsname, const char *const pathname,
2097 char pattern[LOV_LEN];
2098 char buffer[PATH_MAX + 1];
2100 if (fsname == NULL) {
2101 rc = llapi_search_fsname(pathname, buffer);
2107 snprintf(pattern, sizeof(pattern), "/proc/fs/lustre/lov/%s-clilov-*",
2110 rc = first_match(pattern, buffer);
2114 strncpy(clilovpath, buffer, sizeof(buffer));
2120 * Given the path to a stripe attribute proc file, tries to open and
2121 * read the attribute and return the value using the attr parameter
2123 static int sattr_read_attr(const char *const fpath,
2128 char line[PATH_MAX + 1];
2131 f = fopen(fpath, "r");
2134 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open '%s'", fpath);
2138 if (fgets(line, sizeof(line), f) != NULL) {
2141 llapi_error(LLAPI_MSG_ERROR, errno, "Cannot read from '%s'", fpath);
2150 * Tries to determine the default stripe attributes for a given filesystem. The
2151 * filesystem to check should be specified by fsname, or will be determined
2154 static int sattr_get_defaults(const char *const fsname,
2155 const char *const pathname,
2156 unsigned int *scount,
2157 unsigned int *ssize,
2158 unsigned int *soffset)
2161 char dpath[PATH_MAX + 1];
2162 char fpath[PATH_MAX + 1];
2164 rc = clilovpath(fsname, pathname, dpath);
2169 snprintf(fpath, PATH_MAX, "%s/stripecount", dpath);
2170 rc = sattr_read_attr(fpath, scount);
2176 snprintf(fpath, PATH_MAX, "%s/stripesize", dpath);
2177 rc = sattr_read_attr(fpath, ssize);
2183 snprintf(fpath, PATH_MAX, "%s/stripeoffset", dpath);
2184 rc = sattr_read_attr(fpath, soffset);
2193 * Tries to gather the default stripe attributes for a given filesystem. If
2194 * the attributes can be determined, they are cached for easy retreival the
2195 * next time they are needed. Only a single filesystem's attributes are
2198 static int sattr_cache_get_defaults(const char *const fsname,
2199 const char *const pathname,
2200 unsigned int *scount,
2201 unsigned int *ssize,
2202 unsigned int *soffset)
2205 char fsname[PATH_MAX + 1];
2206 unsigned int stripecount;
2207 unsigned int stripesize;
2208 unsigned int stripeoffset;
2214 char fsname_buf[PATH_MAX + 1];
2215 unsigned int tmp[3];
2217 if (fsname == NULL) {
2218 rc = llapi_search_fsname(pathname, fsname_buf);
2222 strncpy(fsname_buf, fsname, PATH_MAX);
2225 if (strncmp(fsname_buf, cache.fsname, PATH_MAX) != 0) {
2227 * Ensure all 3 sattrs (count, size, and offset) are
2228 * successfully retrieved and stored in tmp before writing to
2231 rc = sattr_get_defaults(fsname_buf, NULL, &tmp[0], &tmp[1],
2236 cache.stripecount = tmp[0];
2237 cache.stripesize = tmp[1];
2238 cache.stripeoffset = tmp[2];
2239 strncpy(cache.fsname, fsname_buf, PATH_MAX);
2243 *scount = cache.stripecount;
2245 *ssize = cache.stripesize;
2247 *soffset = cache.stripeoffset;
2252 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2253 struct lov_user_ost_data_v1 *objects,
2254 int is_dir, int verbose, int depth,
2255 int raw, char *pool_name)
2257 char *prefix = is_dir ? "" : "lmm_";
2258 char *seperator = "";
2261 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2262 lmm_oi_set_seq(&lum->lmm_oi, 0);
2263 if (verbose & VERBOSE_DETAIL)
2264 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
2267 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2268 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2270 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2271 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
2273 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
2274 lmm_oi_seq(&lum->lmm_oi));
2275 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
2276 lmm_oi_id(&lum->lmm_oi));
2279 if (verbose & VERBOSE_COUNT) {
2280 if (verbose & ~VERBOSE_COUNT)
2281 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
2284 if (!raw && lum->lmm_stripe_count == 0) {
2285 unsigned int scount;
2286 rc = sattr_cache_get_defaults(NULL, path,
2290 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2293 llapi_error(LLAPI_MSG_ERROR, rc,
2294 "Cannot determine default"
2297 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2298 lum->lmm_stripe_count ==
2299 (typeof(lum->lmm_stripe_count))(-1)
2300 ? -1 : lum->lmm_stripe_count);
2303 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2304 (__s16)lum->lmm_stripe_count);
2306 seperator = is_dir ? " " : "\n";
2309 if (verbose & VERBOSE_SIZE) {
2310 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2311 if (verbose & ~VERBOSE_SIZE)
2312 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
2314 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
2316 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2319 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2321 llapi_error(LLAPI_MSG_ERROR, rc,
2322 "Cannot determine default"
2325 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2326 lum->lmm_stripe_size);
2328 seperator = is_dir ? " " : "\n";
2331 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2332 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2333 if (verbose & ~VERBOSE_LAYOUT)
2334 llapi_printf(LLAPI_MSG_NORMAL, "%spattern: ",
2336 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2340 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2341 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2342 if (verbose & ~VERBOSE_GENERATION)
2343 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
2345 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2346 (int)lum->lmm_layout_gen);
2350 if (verbose & VERBOSE_OFFSET) {
2351 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2352 if (verbose & ~VERBOSE_OFFSET)
2353 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
2356 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2357 lum->lmm_stripe_offset ==
2358 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2359 lum->lmm_stripe_offset);
2361 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2362 objects[0].l_ost_idx);
2363 seperator = is_dir ? " " : "\n";
2366 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
2367 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2368 if (verbose & ~VERBOSE_POOL)
2369 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
2371 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2374 if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
2375 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2378 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2379 struct lov_user_ost_data_v1 *objects,
2380 char *path, int is_dir, int obdindex,
2381 int depth, int header, int raw)
2383 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2386 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2387 if (obdindex == objects[i].l_ost_idx) {
2395 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
2396 depth, raw, pool_name);
2398 if (!is_dir && (header & VERBOSE_OBJID) &&
2399 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2401 llapi_printf(LLAPI_MSG_NORMAL,
2402 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2404 for (i = 0; i < lum->lmm_stripe_count; i++) {
2405 int idx = objects[i].l_ost_idx;
2406 long long oid = ostid_id(&objects[i].l_ost_oi);
2407 long long gr = ostid_seq(&objects[i].l_ost_oi);
2408 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) {
2410 sprintf(fmt, "%s%s%s\n",
2411 "\t%6u\t%14llu\t%#13llx\t",
2412 (fid_seq_is_rsvd(gr) ||
2413 fid_seq_is_mdt0(gr)) ?
2414 "%14llu" : "%#14llx", "%s");
2415 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2417 obdindex == idx ? " *" : "");
2421 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2425 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2426 char *path, int obdindex, int depth, int verbose)
2428 struct lmv_user_mds_data *objects = lum->lum_objects;
2429 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2430 int i, obdstripe = 0;
2432 if (obdindex != OBD_NOT_FOUND) {
2433 for (i = 0; i < lum->lum_stripe_count; i++) {
2434 if (obdindex == objects[i].lum_mds) {
2435 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix,
2445 /* show all information default */
2447 if (lum->lum_magic == LMV_USER_MAGIC)
2448 verbose = VERBOSE_POOL | VERBOSE_COUNT | VERBOSE_OFFSET;
2450 verbose = VERBOSE_OBJID;
2453 if (depth && path && ((verbose != VERBOSE_OBJID)))
2454 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2456 if (verbose & VERBOSE_COUNT) {
2457 if (verbose & ~VERBOSE_COUNT)
2458 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2459 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
2460 (int)lum->lum_stripe_count);
2463 if (verbose & VERBOSE_OFFSET) {
2464 if (verbose & ~VERBOSE_OFFSET)
2465 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2466 llapi_printf(LLAPI_MSG_NORMAL, "%d\n",
2467 (int)lum->lum_stripe_offset);
2470 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2471 if ((obdstripe == 1))
2472 llapi_printf(LLAPI_MSG_NORMAL,
2473 "mdtidx\t\t FID[seq:oid:ver]\n");
2474 for (i = 0; i < lum->lum_stripe_count; i++) {
2475 int idx = objects[i].lum_mds;
2476 struct lu_fid *fid = &objects[i].lum_fid;
2477 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2478 llapi_printf(LLAPI_MSG_NORMAL,
2479 "%6u\t\t "DFID"\t\t%s\n",
2481 obdindex == idx ? " *" : "");
2486 if ((verbose & VERBOSE_POOL) && (pool_name[0] != '\0')) {
2487 if (verbose & ~VERBOSE_POOL)
2488 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2490 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2492 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2495 void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
2499 if (param->get_lmv || param->get_default_lmv)
2500 magic = (__u32)param->fp_lmv_md->lum_magic;
2502 magic = *(__u32 *)¶m->lmd->lmd_lmm; /* lum->lmm_magic */
2505 case LOV_USER_MAGIC_V1:
2506 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
2507 param->lmd->lmd_lmm.lmm_objects,
2509 param->obdindex, param->maxdepth,
2510 param->verbose, param->raw);
2512 case LOV_USER_MAGIC_V3: {
2513 char pool_name[LOV_MAXPOOLNAME + 1];
2514 struct lov_user_ost_data_v1 *objects;
2515 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2517 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
2518 pool_name[LOV_MAXPOOLNAME] = '\0';
2519 objects = lmmv3->lmm_objects;
2520 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
2521 objects, path, is_dir,
2522 param->obdindex, param->maxdepth,
2523 param->verbose, param->raw);
2527 case LMV_USER_MAGIC: {
2528 char pool_name[LOV_MAXPOOLNAME + 1];
2529 struct lmv_user_md *lum;
2531 lum = (struct lmv_user_md *)param->fp_lmv_md;
2532 strncpy(pool_name, lum->lum_pool_name, LOV_MAXPOOLNAME);
2533 lmv_dump_user_lmm(lum, pool_name, path,
2534 param->obdindex, param->maxdepth,
2539 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2540 "(expecting one of %#x %#x %#x %#x)\n",
2541 *(__u32 *)¶m->lmd->lmd_lmm,
2542 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2543 LMV_USER_MAGIC, LMV_MAGIC_V1);
2548 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2554 fname = strrchr(path, '/');
2556 /* It should be a file (or other non-directory) */
2557 if (fname == NULL) {
2558 dname = (char *)malloc(2);
2562 fname = (char *)path;
2564 dname = (char *)malloc(fname - path + 1);
2567 strncpy(dname, path, fname - path);
2568 dname[fname - path] = '\0';
2572 fd = open(dname, O_RDONLY);
2579 strcpy((char *)lum, fname);
2580 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2583 if (close(fd) == -1 && rc == 0)
2590 int llapi_file_lookup(int dirfd, const char *name)
2592 struct obd_ioctl_data data = { 0 };
2597 if (dirfd < 0 || name == NULL)
2600 data.ioc_version = OBD_IOCTL_VERSION;
2601 data.ioc_len = sizeof(data);
2602 data.ioc_inlbuf1 = (char *)name;
2603 data.ioc_inllen1 = strlen(name) + 1;
2605 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2607 llapi_error(LLAPI_MSG_ERROR, rc,
2608 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2613 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2619 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2620 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2622 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2623 * The table below gives the answers for the specified parameters (value and
2624 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2625 * --------------------------------------
2626 * 1 | file > limit; sign > 0 | -1 / -1 |
2627 * 2 | file = limit; sign > 0 | -1 / -1 |
2628 * 3 | file < limit; sign > 0 | ? / 1 |
2629 * 4 | file > limit; sign = 0 | -1 / -1 |
2630 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2631 * 6 | file < limit; sign = 0 | ? / -1 |
2632 * 7 | file > limit; sign < 0 | 1 / 1 |
2633 * 8 | file = limit; sign < 0 | ? / -1 |
2634 * 9 | file < limit; sign < 0 | ? / -1 |
2635 * --------------------------------------
2636 * Note: 5th actually means that the value is within the interval
2637 * (limit - margin, limit]. */
2638 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2639 int sign, int negopt, unsigned long long margin,
2645 /* Drop the fraction of margin (of days). */
2646 if (file + margin <= limit)
2648 } else if (sign == 0) {
2649 if (file <= limit && file + margin > limit)
2651 else if (file + margin <= limit)
2653 } else if (sign < 0) {
2660 return negopt ? ~ret + 1 : ret;
2663 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2664 * Return -1 or 1 if file timestamp does not or does match the given criteria
2665 * correspondingly. Return 0 if the MDS time is being checked and there are
2666 * attributes on OSTs and it is not yet clear if the timespamp matches.
2668 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2669 * updated timestamps. */
2670 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2675 /* Check if file is accepted. */
2677 ret = find_value_cmp(st->st_atime, param->atime,
2678 param->asign, param->exclude_atime,
2686 ret = find_value_cmp(st->st_mtime, param->mtime,
2687 param->msign, param->exclude_mtime,
2692 /* If the previous check matches, but this one is not yet clear,
2693 * we should return 0 to do an RPC on OSTs. */
2699 ret = find_value_cmp(st->st_ctime, param->ctime,
2700 param->csign, param->exclude_ctime,
2705 /* If the previous check matches, but this one is not yet clear,
2706 * we should return 0 to do an RPC on OSTs. */
2715 * Check whether the stripes matches the indexes user provided
2719 static int check_obd_match(struct find_param *param)
2721 lstat_t *st = ¶m->lmd->lmd_st;
2722 struct lov_user_ost_data_v1 *lmm_objects;
2725 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
2728 if (!S_ISREG(st->st_mode))
2731 /* Only those files should be accepted, which have a
2732 * stripe on the specified OST. */
2733 if (!param->lmd->lmd_lmm.lmm_stripe_count)
2736 if (param->lmd->lmd_lmm.lmm_magic ==
2737 LOV_USER_MAGIC_V3) {
2738 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2740 lmm_objects = lmmv3->lmm_objects;
2741 } else if (param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2742 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
2744 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2745 __func__, param->lmd->lmd_lmm.lmm_magic);
2749 for (i = 0; i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
2750 for (j = 0; j < param->num_obds; j++) {
2751 if (param->obdindexes[j] ==
2752 lmm_objects[i].l_ost_idx) {
2753 if (param->exclude_obd)
2760 if (param->exclude_obd)
2765 static int check_mdt_match(struct find_param *param)
2769 if (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND)
2772 /* FIXME: For striped dir, we should get stripe information and check */
2773 for (i = 0; i < param->num_mdts; i++) {
2774 if (param->mdtindexes[i] == param->file_mdtindex)
2775 return !param->exclude_mdt;
2778 if (param->exclude_mdt)
2784 * Check whether the obd is active or not, if it is
2785 * not active, just print the object affected by this
2788 static int print_failed_tgt(struct find_param *param, char *path, int type)
2790 struct obd_statfs stat_buf;
2791 struct obd_uuid uuid_buf;
2794 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2796 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2797 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2798 ret = llapi_obd_statfs(path, type,
2799 param->obdindex, &stat_buf,
2802 llapi_printf(LLAPI_MSG_NORMAL,
2803 "obd_uuid: %s failed %s ",
2804 param->obduuid->uuid,
2810 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
2811 void *data, struct dirent64 *de)
2813 struct find_param *param = (struct find_param *)data;
2814 DIR *dir = dirp == NULL ? NULL : *dirp;
2815 int decision = 1; /* 1 is accepted; -1 is rejected. */
2816 lstat_t *st = ¶m->lmd->lmd_st;
2818 int checked_type = 0;
2821 LASSERT(parent != NULL || dir != NULL);
2823 if (param->have_fileinfo == 0)
2824 param->lmd->lmd_lmm.lmm_stripe_count = 0;
2826 /* If a regular expression is presented, make the initial decision */
2827 if (param->pattern != NULL) {
2828 char *fname = strrchr(path, '/');
2829 fname = (fname == NULL ? path : fname + 1);
2830 ret = fnmatch(param->pattern, fname, 0);
2831 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
2832 (ret == 0 && param->exclude_pattern))
2836 /* See if we can check the file type from the dirent. */
2837 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
2838 de->d_type < DT_MAX) {
2840 if (llapi_dir_filetype_table[de->d_type] == param->type) {
2841 if (param->exclude_type)
2844 if (!param->exclude_type)
2851 /* Request MDS for the stat info if some of these parameters need
2852 * to be compared. */
2853 if (param->obduuid || param->mdtuuid || param->check_uid ||
2854 param->check_gid || param->check_pool || param->atime ||
2855 param->ctime || param->mtime || param->check_size ||
2856 param->check_stripecount || param->check_stripesize ||
2857 param->check_layout)
2860 if (param->type && checked_type == 0)
2863 if (param->have_fileinfo == 0 && decision == 0) {
2864 ret = get_lmd_info(path, parent, dir, param->lmd,
2868 ret = llapi_file_fget_mdtidx(dirfd(dir),
2869 ¶m->file_mdtindex);
2874 ret = lstat_f(path, &tmp_st);
2877 llapi_error(LLAPI_MSG_ERROR, ret,
2878 "error: %s: lstat failed"
2879 "for %s", __func__, path);
2882 if (S_ISREG(tmp_st.st_mode)) {
2883 fd = open(path, O_RDONLY);
2885 ret = llapi_file_fget_mdtidx(fd,
2886 ¶m->file_mdtindex);
2892 /* For special inode, it assumes to
2893 * reside on the same MDT with the
2896 ret = llapi_file_fget_mdtidx(fd,
2897 ¶m->file_mdtindex);
2910 if (param->type && !checked_type) {
2911 if ((st->st_mode & S_IFMT) == param->type) {
2912 if (param->exclude_type)
2915 if (!param->exclude_type)
2921 if (param->obduuid || param->mdtuuid) {
2922 if (lustre_fs && param->got_uuids &&
2923 param->st_dev != st->st_dev) {
2924 /* A lustre/lustre mount point is crossed. */
2925 param->got_uuids = 0;
2926 param->obds_printed = 0;
2927 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2930 if (lustre_fs && !param->got_uuids) {
2931 ret = setup_target_indexes(dir ? dir : parent, path,
2936 param->st_dev = st->st_dev;
2937 } else if (!lustre_fs && param->got_uuids) {
2938 /* A lustre/non-lustre mount point is crossed. */
2939 param->got_uuids = 0;
2940 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2944 if (param->check_stripesize) {
2945 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_size,
2947 param->stripesize_sign,
2948 param->exclude_stripesize,
2949 param->stripesize_units, 0);
2954 if (param->check_stripecount) {
2955 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_count,
2957 param->stripecount_sign,
2958 param->exclude_stripecount, 1, 0);
2963 if (param->check_layout) {
2966 found = (param->lmd->lmd_lmm.lmm_pattern & param->layout);
2967 if ((param->lmd->lmd_lmm.lmm_pattern == 0xFFFFFFFF) ||
2968 (found && param->exclude_layout) ||
2969 (!found && !param->exclude_layout)) {
2975 /* If an OBD UUID is specified but none matches, skip this file. */
2976 if ((param->obduuid && param->obdindex == OBD_NOT_FOUND) ||
2977 (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND))
2980 /* If a OST or MDT UUID is given, and some OST matches,
2982 if (param->obdindex != OBD_NOT_FOUND ||
2983 param->mdtindex != OBD_NOT_FOUND) {
2984 if (param->obduuid) {
2985 if (check_obd_match(param)) {
2986 /* If no mdtuuid is given, we are done.
2987 * Otherwise, fall through to the mdtuuid
2989 if (!param->mdtuuid)
2995 if (param->mdtuuid) {
2996 if (check_mdt_match(param))
3002 if (param->check_uid) {
3003 if (st->st_uid == param->uid) {
3004 if (param->exclude_uid)
3007 if (!param->exclude_uid)
3012 if (param->check_gid) {
3013 if (st->st_gid == param->gid) {
3014 if (param->exclude_gid)
3017 if (!param->exclude_gid)
3022 if (param->check_pool) {
3023 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
3025 /* empty requested pool is taken as no pool search => V1 */
3026 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
3027 (param->poolname[0] == '\0')) ||
3028 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3029 (strncmp(lmmv3->lmm_pool_name,
3030 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
3031 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3032 (strcmp(param->poolname, "*") == 0))) {
3033 if (param->exclude_pool)
3036 if (!param->exclude_pool)
3041 /* Check the time on mds. */
3043 if (param->atime || param->ctime || param->mtime) {
3046 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
3047 param->lmd->lmd_lmm.lmm_stripe_count)
3049 decision = find_time_check(st, param, for_mds);
3054 /* If file still fits the request, ask ost for updated info.
3055 The regular stat is almost of the same speed as some new
3056 'glimpse-size-ioctl'. */
3058 if (param->check_size && S_ISREG(st->st_mode) &&
3059 param->lmd->lmd_lmm.lmm_stripe_count)
3063 /* For regular files with the stripe the decision may have not
3064 * been taken yet if *time or size is to be checked. */
3065 LASSERT((S_ISREG(st->st_mode) &&
3066 param->lmd->lmd_lmm.lmm_stripe_count) ||
3067 param->mdtindex != OBD_NOT_FOUND);
3069 if (param->obdindex != OBD_NOT_FOUND)
3070 print_failed_tgt(param, path, LL_STATFS_LOV);
3072 if (param->mdtindex != OBD_NOT_FOUND)
3073 print_failed_tgt(param, path, LL_STATFS_LMV);
3076 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
3077 (void *)param->lmd);
3078 } else if (parent) {
3079 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
3080 (void *)param->lmd);
3084 if (errno == ENOENT) {
3085 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
3086 "warning: %s: %s does not exist",
3091 llapi_error(LLAPI_MSG_ERROR, ret,
3092 "%s: IOC_LOV_GETINFO on %s failed",
3098 /* Check the time on osc. */
3099 decision = find_time_check(st, param, 0);
3106 if (param->check_size)
3107 decision = find_value_cmp(st->st_size, param->size,
3108 param->size_sign, param->exclude_size,
3109 param->size_units, 0);
3111 if (decision != -1) {
3112 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
3114 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
3116 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3120 /* Do not get down anymore? */
3121 if (param->depth == param->maxdepth)
3128 static int cb_mv_init(char *path, DIR *parent, DIR **dirp,
3129 void *param_data, struct dirent64 *de)
3131 struct find_param *param = (struct find_param *)param_data;
3133 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3135 struct obd_ioctl_data data = { 0 };
3140 LASSERT(parent != NULL || dirp != NULL);
3144 if (parent == NULL) {
3145 dir = opendir_parent(path);
3148 fprintf(stderr, "can not open %s ret %d\n",
3156 filename = basename(path);
3157 data.ioc_inlbuf1 = (char *)filename;
3158 data.ioc_inllen1 = strlen(filename) + 1;
3159 data.ioc_inlbuf2 = (char *)¶m->mdtindex;
3160 data.ioc_inllen2 = sizeof(param->mdtindex);
3161 ret = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3163 llapi_error(LLAPI_MSG_ERROR, ret,
3164 "llapi_obd_statfs: error packing ioctl data");
3168 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
3171 fprintf(stderr, "%s migrate failed %d\n", path, ret);
3173 } else if (param->verbose & VERBOSE_DETAIL) {
3174 fprintf(stdout, "migrate %s to MDT%d\n", path, param->mdtindex);
3179 /* If the directory is being migration, we need
3180 * close the directory after migration,
3181 * so the old directory cache will be cleanup
3182 * on the client side, and re-open to get the
3183 * new directory handle */
3184 *dirp = opendir(path);
3187 llapi_error(LLAPI_MSG_ERROR, ret,
3188 "%s: Failed to open '%s'", __func__, path);
3199 int llapi_mv(char *path, struct find_param *param)
3201 return param_callback(path, cb_mv_init, cb_common_fini, param);
3204 int llapi_find(char *path, struct find_param *param)
3206 return param_callback(path, cb_find_init, cb_common_fini, param);
3210 * Get MDT number that the file/directory inode referenced
3211 * by the open fd resides on.
3212 * Return 0 and mdtidx on success, or -ve errno.
3214 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
3216 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
3221 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
3222 struct dirent64 *de)
3224 struct find_param *param = (struct find_param *)data;
3225 DIR *d = dirp == NULL ? NULL : *dirp;
3229 LASSERT(parent != NULL || d != NULL);
3232 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3233 } else if (parent) {
3236 fd = open(path, O_RDONLY);
3238 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3246 if (ret == -ENODATA) {
3247 if (!param->obduuid)
3248 llapi_printf(LLAPI_MSG_NORMAL,
3249 "%s has no stripe info\n", path);
3251 } else if (ret == -ENOENT) {
3252 llapi_error(LLAPI_MSG_WARN, ret,
3253 "warning: %s: %s does not exist",
3256 } else if (ret == -ENOTTY) {
3257 llapi_error(LLAPI_MSG_ERROR, ret,
3258 "%s: '%s' not on a Lustre fs?",
3261 llapi_error(LLAPI_MSG_ERROR, ret,
3262 "error: %s: LL_IOC_GET_MDTIDX failed for %s",
3268 /* The 'LASSERT(parent != NULL || d != NULL);' guarantees
3269 * that either 'd' or 'parent' is not null.
3270 * So in all cases llapi_file_fget_mdtidx() is called,
3271 * thus initializing 'mdtidx'. */
3272 if (param->quiet || !(param->verbose & VERBOSE_DETAIL))
3273 /* coverity[uninit_use_in_call] */
3274 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3276 /* coverity[uninit_use_in_call] */
3277 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3281 /* Do not get down anymore? */
3282 if (param->depth == param->maxdepth)
3289 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
3290 struct dirent64 *de)
3292 struct find_param *param = (struct find_param *)data;
3293 DIR *d = dirp == NULL ? NULL : *dirp;
3296 LASSERT(parent != NULL || d != NULL);
3298 if (param->obduuid) {
3300 ret = setup_obd_uuid(d ? d : parent, path, param);
3306 if (param->get_lmv || param->get_default_lmv) {
3307 ret = cb_get_dirstripe(path, d, param);
3309 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
3310 (void *)¶m->lmd->lmd_lmm);
3313 } else if (parent) {
3314 char *fname = strrchr(path, '/');
3315 fname = (fname == NULL ? path : fname + 1);
3317 if (param->get_lmv) {
3318 llapi_printf(LLAPI_MSG_NORMAL,
3319 "%s get dirstripe information for file\n",
3324 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
3326 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
3327 (void *)¶m->lmd->lmd_lmm);
3331 if (errno == ENODATA && d != NULL) {
3332 /* We need to "fake" the "use the default" values
3333 * since the lmm struct is zeroed out at this point.
3334 * The magic needs to be set in order to satisfy
3335 * a check later on in the code path.
3336 * The object_seq needs to be set for the "(Default)"
3337 * prefix to be displayed. */
3338 if (param->get_default_lmv) {
3339 struct lmv_user_md *lum = param->fp_lmv_md;
3341 lum->lum_magic = LMV_USER_MAGIC;
3342 lum->lum_stripe_count = 0;
3343 lum->lum_stripe_offset = -1;
3346 struct lov_user_md *lmm = ¶m->lmd->lmd_lmm;
3347 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3349 ostid_set_seq(&lmm->lmm_oi,
3350 FID_SEQ_LOV_DEFAULT);
3351 lmm->lmm_stripe_count = 0;
3352 lmm->lmm_stripe_size = 0;
3353 lmm->lmm_stripe_offset = -1;
3356 } else if (errno == ENODATA && parent != NULL) {
3357 if (!param->obduuid && !param->mdtuuid)
3358 llapi_printf(LLAPI_MSG_NORMAL,
3359 "%s has no stripe info\n", path);
3361 } else if (errno == ENOENT) {
3362 llapi_error(LLAPI_MSG_WARN, -ENOENT,
3363 "warning: %s: %s does not exist",
3366 } else if (errno == ENOTTY) {
3368 llapi_error(LLAPI_MSG_ERROR, ret,
3369 "%s: '%s' not on a Lustre fs?",
3373 llapi_error(LLAPI_MSG_ERROR, ret,
3374 "error: %s: %s failed for %s",
3375 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
3376 "IOC_MDC_GETFILESTRIPE", path);
3383 if (!(param->verbose & VERBOSE_MDTINDEX))
3384 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
3387 /* Do not get down anymore? */
3388 if (param->depth == param->maxdepth)
3395 int llapi_getstripe(char *path, struct find_param *param)
3397 return param_callback(path, (param->verbose & VERBOSE_MDTINDEX) ?
3398 cb_get_mdt_index : cb_getstripe,
3399 cb_common_fini, param);
3402 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
3403 struct obd_statfs *stat_buf,
3404 struct obd_uuid *uuid_buf)
3407 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3409 struct obd_ioctl_data data = { 0 };
3412 data.ioc_inlbuf1 = (char *)&type;
3413 data.ioc_inllen1 = sizeof(__u32);
3414 data.ioc_inlbuf2 = (char *)&index;
3415 data.ioc_inllen2 = sizeof(__u32);
3416 data.ioc_pbuf1 = (char *)stat_buf;
3417 data.ioc_plen1 = sizeof(struct obd_statfs);
3418 data.ioc_pbuf2 = (char *)uuid_buf;
3419 data.ioc_plen2 = sizeof(struct obd_uuid);
3421 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3423 llapi_error(LLAPI_MSG_ERROR, rc,
3424 "llapi_obd_statfs: error packing ioctl data");
3428 fd = open(path, O_RDONLY);
3429 if (errno == EISDIR)
3430 fd = open(path, O_DIRECTORY | O_RDONLY);
3433 rc = errno ? -errno : -EBADF;
3434 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
3436 /* If we can't even open a file on the filesystem (e.g. with
3437 * -ESHUTDOWN), force caller to exit or it will loop forever. */
3440 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
3442 rc = errno ? -errno : -EINVAL;
3448 #define MAX_STRING_SIZE 128
3450 int llapi_ping(char *obd_type, char *obd_name)
3452 char path[MAX_STRING_SIZE];
3456 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
3457 obd_type, obd_name);
3459 fd = open(path, O_WRONLY);
3462 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
3466 /* The purpose is to send a byte as a ping, whatever this byte is. */
3467 /* coverity[uninit_use_in_call] */
3468 rc = write(fd, buf, 1);
3478 int llapi_target_iterate(int type_num, char **obd_type,
3479 void *args, llapi_cb_t cb)
3481 char buf[MAX_STRING_SIZE];
3482 FILE *fp = fopen(DEVICES_LIST, "r");
3487 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
3491 while (fgets(buf, sizeof(buf), fp) != NULL) {
3492 char *obd_type_name = NULL;
3493 char *obd_name = NULL;
3494 char *obd_uuid = NULL;
3496 struct obd_statfs osfs_buffer;
3498 while(bufp[0] == ' ')
3501 for(i = 0; i < 3; i++) {
3502 obd_type_name = strsep(&bufp, " ");
3504 obd_name = strsep(&bufp, " ");
3505 obd_uuid = strsep(&bufp, " ");
3507 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
3509 for (i = 0; i < type_num; i++) {
3510 if (strcmp(obd_type_name, obd_type[i]) != 0)
3513 cb(obd_type_name, obd_name, obd_uuid, args);
3520 static void do_target_check(char *obd_type_name, char *obd_name,
3521 char *obd_uuid, void *args)
3525 rc = llapi_ping(obd_type_name, obd_name);
3526 if (rc == ENOTCONN) {
3527 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
3529 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
3531 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
3535 int llapi_target_check(int type_num, char **obd_type, char *dir)
3537 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
3540 #undef MAX_STRING_SIZE
3542 /* Is this a lustre fs? */
3543 int llapi_is_lustre_mnttype(const char *type)
3545 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
3548 /* Is this a lustre client fs? */
3549 int llapi_is_lustre_mnt(struct mntent *mnt)
3551 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
3552 strstr(mnt->mnt_fsname, ":/") != NULL);
3555 int llapi_quotacheck(char *mnt, int check_type)
3560 root = opendir(mnt);
3563 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3567 rc = ioctl(dirfd(root), OBD_IOC_QUOTACHECK, check_type);
3575 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
3581 root = opendir(mnt);
3584 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3589 rc = ioctl(dirfd(root), OBD_IOC_POLL_QUOTACHECK, qchk);
3593 if (poll_intvl < 30)
3601 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3606 root = opendir(mnt);
3609 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3613 rc = ioctl(dirfd(root), OBD_IOC_QUOTACTL, qctl);
3621 static int cb_quotachown(char *path, DIR *parent, DIR **dirp, void *data,
3622 struct dirent64 *de)
3624 struct find_param *param = (struct find_param *)data;
3625 DIR *d = dirp == NULL ? NULL : *dirp;
3629 LASSERT(parent != NULL || d != NULL);
3631 rc = get_lmd_info(path, parent, d, param->lmd, param->lumlen);
3633 if (rc == -ENODATA) {
3634 if (!param->obduuid && !param->quiet)
3635 llapi_error(LLAPI_MSG_ERROR, -ENODATA,
3636 "%s has no stripe info", path);
3638 } else if (rc == -ENOENT) {
3644 st = ¶m->lmd->lmd_st;
3646 /* libc chown() will do extra check, and if the real owner is
3647 * the same as the ones to set, it won't fall into kernel, so
3648 * invoke syscall directly. */
3649 rc = syscall(SYS_chown, path, -1, -1);
3651 llapi_error(LLAPI_MSG_ERROR, errno,
3652 "error: chown %s", path);
3654 rc = chmod(path, st->st_mode);
3657 llapi_error(LLAPI_MSG_ERROR, rc, "error: chmod %s (%hu)",
3664 int llapi_quotachown(char *path, int flag)
3666 struct find_param param;
3668 memset(¶m, 0, sizeof(param));
3669 param.recursive = 1;
3673 return param_callback(path, cb_quotachown, NULL, ¶m);
3679 #include <sys/wait.h>
3683 static int rmtacl_notify(int ops)
3687 int found = 0, fd = 0, rc = 0;
3689 fp = setmntent(MOUNTED, "r");
3692 llapi_error(LLAPI_MSG_ERROR, rc,
3693 "error setmntent(%s)", MOUNTED);
3698 mnt = getmntent(fp);
3702 if (!llapi_is_lustre_mnt(mnt))
3705 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
3708 llapi_error(LLAPI_MSG_ERROR, rc,
3709 "Can't open '%s'\n", mnt->mnt_dir);
3713 rc = ioctl(fd, LL_IOC_RMTACL, ops);
3717 llapi_error(LLAPI_MSG_ERROR, rc,
3718 "ioctl RMTACL on '%s' err %d\n",
3728 return ((rc != 0) ? rc : found);
3731 static char *next_token(char *p, int div)
3737 while (*p && *p != ':' && !isspace(*p))
3740 while (*p == ':' || isspace(*p))
3743 return *p ? p : NULL;
3746 static int rmtacl_name2id(char *name, int is_user)
3751 pw = getpwnam(name);
3755 return (int)(pw->pw_uid);
3759 gr = getgrnam(name);
3763 return (int)(gr->gr_gid);
3767 static int isodigit(int c)
3769 return (c >= '0' && c <= '7') ? 1 : 0;
3773 * Whether the name is just digits string (uid/gid) already or not.
3778 static int str_is_id(char *str)
3785 if (*str == 'x' || *str == 'X') { /* for Hex. */
3786 if (!isxdigit(*(++str)))
3789 while (isxdigit(*(++str)));
3790 } else if (isodigit(*str)) { /* for Oct. */
3791 while (isodigit(*(++str)));
3793 } else if (isdigit(*str)) { /* for Dec. */
3794 while (isdigit(*(++str)));
3797 return (*str == 0) ? 1 : 0;
3807 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
3809 static rmtacl_name_t rmtacl_namelist[] = {
3810 { RMTACL_OPTNAME("user:"), 1, 0 },
3811 { RMTACL_OPTNAME("group:"), 0, 0 },
3812 { RMTACL_OPTNAME("default:user:"), 1, 0 },
3813 { RMTACL_OPTNAME("default:group:"), 0, 0 },
3814 /* for --tabular option */
3815 { RMTACL_OPTNAME("user"), 1, 1 },
3816 { RMTACL_OPTNAME("group"), 0, 1 },
3820 static int rgetfacl_output(char *str)
3822 char *start = NULL, *end = NULL;
3823 int is_user = 0, n, id;
3830 for (rn = rmtacl_namelist; rn->name; rn++) {
3831 if(strncmp(str, rn->name, rn->length) == 0) {
3832 if (!rn->next_token)
3833 start = str + rn->length;
3835 start = next_token(str + rn->length, 0);
3836 is_user = rn->is_user;
3841 end = next_token(start, 1);
3842 if (end == NULL || start == end) {
3843 n = printf("%s", str);
3849 id = rmtacl_name2id(start, is_user);
3850 if (id == INVALID_ID) {
3851 if (str_is_id(start)) {
3853 n = printf("%s", str);
3856 } else if ((id == NOBODY_UID && is_user) ||
3857 (id == NOBODY_GID && !is_user)) {
3859 n = printf("%s", str);
3863 n = printf("%s%d%s", str, id, end);
3868 static int child_status(int status)
3870 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
3873 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
3876 int fd[2], status, rc;
3883 llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe\n");
3890 llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork\n");
3895 /* child process redirects its output. */
3898 if (dup2(fd[1], 1) < 0) {
3900 llapi_error(LLAPI_MSG_ERROR, rc,
3901 "Can't dup2 %d\n", fd[1]);
3911 status = rmtacl_notify(ops);
3915 exit(execvp(argv[0], argv));
3918 /* the following is parent process */
3919 fp = fdopen(fd[0], "r");
3922 llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed\n", fd[0]);
3928 while (fgets(buf, PIPE_BUF, fp) != NULL) {
3929 if (output_func(buf) < 0)
3930 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
3936 if (waitpid(pid, &status, 0) < 0) {
3938 llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed\n", pid);
3942 return child_status(status);
3945 int llapi_lsetfacl(int argc, char *argv[])
3947 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
3950 int llapi_lgetfacl(int argc, char *argv[])
3952 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
3955 int llapi_rsetfacl(int argc, char *argv[])
3957 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
3960 int llapi_rgetfacl(int argc, char *argv[])
3962 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
3965 int llapi_cp(int argc, char *argv[])
3969 rc = rmtacl_notify(RMT_RSETFACL);
3973 exit(execvp(argv[0], argv));
3976 int llapi_ls(int argc, char *argv[])
3980 rc = rmtacl_notify(RMT_LGETFACL);
3984 exit(execvp(argv[0], argv));
3987 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
3988 * format must have %s%s, buf must be > 16
3989 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
3990 * then buf = "lustre-MDT0000"
3992 static int get_mdtname(char *name, char *format, char *buf)
3994 char suffix[]="-MDT0000";
3995 int len = strlen(name);
3997 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
3998 name[len - 5] = '\0';
4003 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
4006 /* Not enough room to add suffix */
4007 llapi_err_noerrno(LLAPI_MSG_ERROR,
4008 "MDT name too long |%s|", name);
4013 return sprintf(buf, format, name, suffix);
4016 /** ioctl on filsystem root, with mdtindex sent as data
4017 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
4018 * \param mdtidxp pointer to integer within data to be filled in with the
4019 * mdt index (0 if no mdt is specified). NULL won't be filled.
4021 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
4029 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
4030 Open root and parse mdt index. */
4031 if (mdtname[0] == '/') {
4033 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
4034 (char *)mdtname, -1);
4036 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
4038 ptr = fsname + strlen(fsname) - 8;
4040 index = strtol(ptr + 4, NULL, 10);
4041 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
4045 llapi_err_noerrno(LLAPI_MSG_ERROR,
4046 "Can't open %s: %d\n", mdtname, rc);
4053 rc = ioctl(fd, opc, data);
4058 if (rc && want_error)
4059 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
4065 /****** Changelog API ********/
4067 static int changelog_ioctl(const char *mdtname, int opc, int id,
4068 long long recno, int flags)
4070 struct ioc_changelog data;
4074 data.icc_recno = recno;
4075 data.icc_flags = flags;
4076 idx = (int *)(&data.icc_mdtindex);
4078 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
4081 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
4082 struct changelog_private {
4085 lustre_kernelcomm kuc;
4088 /** Start reading from a changelog
4089 * @param priv Opaque private control structure
4090 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
4091 * @param device Report changes recorded on this MDT
4092 * @param startrec Report changes beginning with this record number
4093 * (just call llapi_changelog_fini when done; don't need an endrec)
4095 int llapi_changelog_start(void **priv, int flags, const char *device,
4098 struct changelog_private *cp;
4101 /* Set up the receiver control struct */
4102 cp = calloc(1, sizeof(*cp));
4106 cp->magic = CHANGELOG_PRIV_MAGIC;
4109 /* Set up the receiver */
4110 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */);
4116 /* Tell the kernel to start sending */
4117 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
4119 /* Only the kernel reference keeps the write side open */
4120 close(cp->kuc.lk_wfd);
4121 cp->kuc.lk_wfd = LK_NOFD;
4123 /* frees and clears priv */
4124 llapi_changelog_fini(priv);
4135 /** Finish reading from a changelog */
4136 int llapi_changelog_fini(void **priv)
4138 struct changelog_private *cp = (struct changelog_private *)*priv;
4140 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4143 libcfs_ukuc_stop(&cp->kuc);
4149 /** Convert a changelog_rec to changelog_ext_rec, in this way client can treat
4150 * all records in the format of changelog_ext_rec, this can make record
4153 static inline int changelog_extend_rec(struct changelog_ext_rec *ext)
4155 if (!CHANGELOG_REC_EXTENDED(ext)) {
4156 struct changelog_rec *rec = (struct changelog_rec *)ext;
4158 memmove(ext->cr_name, rec->cr_name, rec->cr_namelen);
4159 fid_zero(&ext->cr_sfid);
4160 fid_zero(&ext->cr_spfid);
4167 /** Read the next changelog entry
4168 * @param priv Opaque private control structure
4169 * @param rech Changelog record handle; record will be allocated here
4170 * @return 0 valid message received; rec is set
4174 int llapi_changelog_recv(void *priv, struct changelog_ext_rec **rech)
4176 struct changelog_private *cp = (struct changelog_private *)priv;
4177 struct kuc_hdr *kuch;
4180 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4184 kuch = malloc(KUC_CHANGELOG_MSG_MAXSIZE);
4189 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
4190 KUC_CHANGELOG_MSG_MAXSIZE,
4191 KUC_TRANSPORT_CHANGELOG);
4195 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
4196 ((kuch->kuc_msgtype != CL_RECORD) &&
4197 (kuch->kuc_msgtype != CL_EOF))) {
4198 llapi_err_noerrno(LLAPI_MSG_ERROR,
4199 "Unknown changelog message type %d:%d\n",
4200 kuch->kuc_transport, kuch->kuc_msgtype);
4205 if (kuch->kuc_msgtype == CL_EOF) {
4206 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
4215 /* Our message is a changelog_ext_rec. Use pointer math to skip
4216 * kuch_hdr and point directly to the message payload.
4218 *rech = (struct changelog_ext_rec *)(kuch + 1);
4219 changelog_extend_rec(*rech);
4229 /** Release the changelog record when done with it. */
4230 int llapi_changelog_free(struct changelog_ext_rec **rech)
4233 /* We allocated memory starting at the kuc_hdr, but passed
4234 * the consumer a pointer to the payload.
4235 * Use pointer math to get back to the header.
4237 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
4244 int llapi_changelog_clear(const char *mdtname, const char *idstr,
4250 llapi_err_noerrno(LLAPI_MSG_ERROR,
4251 "can't purge negative records\n");
4255 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
4256 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
4257 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
4258 llapi_err_noerrno(LLAPI_MSG_ERROR,
4259 "expecting id of the form '"
4260 CHANGELOG_USER_PREFIX
4261 "<num>'; got '%s'\n", idstr);
4265 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
4268 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
4269 int buflen, long long *recno, int *linkno)
4272 struct getinfo_fid2path *gf;
4275 while (*fidstr == '[')
4278 sscanf(fidstr, SFID, RFID(&fid));
4279 if (!fid_is_sane(&fid)) {
4280 llapi_err_noerrno(LLAPI_MSG_ERROR,
4281 "bad FID format [%s], should be "DFID"\n",
4282 fidstr, (__u64)1, 2, 0);
4286 gf = malloc(sizeof(*gf) + buflen);
4290 gf->gf_recno = *recno;
4291 gf->gf_linkno = *linkno;
4292 gf->gf_pathlen = buflen;
4294 /* Take path or fsname */
4295 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
4298 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
4300 memcpy(buf, gf->gf_path, gf->gf_pathlen);
4301 *recno = gf->gf_recno;
4302 *linkno = gf->gf_linkno;
4309 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
4312 struct lustre_mdt_attrs *lma;
4316 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
4318 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
4321 lma = (struct lustre_mdt_attrs *)buf;
4322 fid_le_to_cpu(fid, &lma->lma_self_fid);
4326 int llapi_fd2fid(const int fd, lustre_fid *fid)
4330 memset(fid, 0, sizeof(*fid));
4332 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
4333 if (rc == -EINVAL || rc == -ENOTTY)
4334 rc = fid_from_lma(NULL, fd, fid);
4339 int llapi_path2fid(const char *path, lustre_fid *fid)
4343 memset(fid, 0, sizeof(*fid));
4344 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4346 if (errno == ELOOP || errno == ENXIO)
4347 return fid_from_lma(path, -1, fid);
4351 rc = llapi_fd2fid(fd, fid);
4352 if (rc == -EINVAL || rc == -ENOTTY)
4353 rc = fid_from_lma(path, -1, fid);
4359 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4364 root = opendir(mnt);
4367 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4371 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
4374 llapi_error(LLAPI_MSG_ERROR, rc,
4375 "ioctl on %s for getting connect flags failed", mnt);
4381 int llapi_get_version(char *buffer, int buffer_size,
4386 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
4388 fd = open(OBD_DEV_PATH, O_RDONLY);
4392 memset(buffer, 0, buffer_size);
4393 data->ioc_version = OBD_IOCTL_VERSION;
4394 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
4395 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
4396 data->ioc_len = obd_ioctl_packlen(data);
4398 rc = ioctl(fd, OBD_GET_VERSION, buffer);
4405 *version = data->ioc_bulk;
4410 * Get a 64-bit value representing the version of file data pointed by fd.
4412 * Each write or truncate, flushed on OST, will change this value. You can use
4413 * this value to verify if file data was modified. This only checks the file
4414 * data, not metadata.
4416 * \param flags 0: no flush pages, usually used it the process has already
4418 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
4420 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
4421 * pages from clients.
4423 * \retval 0 on success.
4424 * \retval -errno on error.
4426 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4429 struct ioc_data_version idv;
4431 idv.idv_flags = flags;
4433 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4437 *data_version = idv.idv_version;
4443 * Create a volatile file and open it for write:
4444 * - file is created as a standard file in the directory
4445 * - file does not appears in directory and directory mtime does not change
4446 * - file is removed at close
4447 * - file modes are rw-------, if user wants another one it must use fchmod()
4448 * \param directory Directory where the file is created
4449 * \param idx MDT index on which the file is created
4450 * \param open_flags Standard open flags
4452 * \retval 0 on success.
4453 * \retval -errno on error.
4455 int llapi_create_volatile_idx(char *directory, int idx, int open_flags)
4457 char file_path[PATH_MAX];
4458 char filename[PATH_MAX];
4463 fd = open("/dev/urandom", O_RDONLY);
4465 llapi_error(LLAPI_MSG_ERROR, errno,
4466 "Cannot open /dev/urandom\n");
4469 rc = read(fd, &random, sizeof(random));
4471 if (rc < sizeof(random)) {
4472 llapi_error(LLAPI_MSG_ERROR, errno,
4473 "cannot read %zu bytes from /dev/urandom",
4478 snprintf(filename, sizeof(filename),
4479 LUSTRE_VOLATILE_HDR"::%.4X", random);
4481 snprintf(filename, sizeof(filename),
4482 LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, random);
4484 rc = snprintf(file_path, sizeof(file_path),
4485 "%s/%s", directory, filename);
4486 if (rc >= sizeof(file_path))
4489 fd = open(file_path, O_RDWR | O_CREAT | open_flags, S_IRUSR | S_IWUSR);
4491 llapi_error(LLAPI_MSG_ERROR, errno,
4492 "Cannot create volatile file %s in %s\n",
4493 filename + LUSTRE_VOLATILE_HDR_LEN,
4501 * Swap the layouts between 2 file descriptors
4502 * the 2 files must be open in write
4503 * first fd received the ioctl, second fd is passed as arg
4504 * this is assymetric but avoid use of root path for ioctl
4506 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4508 struct lustre_swap_layouts lsl;
4511 srandom(time(NULL));
4513 lsl.sl_flags = flags;
4514 lsl.sl_gid = random();
4517 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4524 * Swap the layouts between 2 files
4525 * the 2 files are open in write
4527 int llapi_swap_layouts(const char *path1, const char *path2,
4528 __u64 dv1, __u64 dv2, __u64 flags)
4532 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4535 llapi_error(LLAPI_MSG_ERROR, rc,
4536 "error: cannot open '%s' for write", path1);
4540 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4543 llapi_error(LLAPI_MSG_ERROR, rc,
4544 "error: cannot open '%s' for write", path2);
4548 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4550 llapi_error(LLAPI_MSG_ERROR, rc,
4551 "error: cannot swap layout between '%s' and '%s'\n",