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 + 1], dev[PATH_MAX + 1] = "*", fs[PATH_MAX + 1];
430 FILE *fp = fopen(DEVICES_LIST, "r");
433 if (!fsname && file_path) {
434 rc = llapi_search_fsname(file_path, fs);
436 llapi_error(LLAPI_MSG_ERROR, rc,
437 "'%s' is not on a Lustre filesystem",
444 if (strlen(fsname) > sizeof(fs)-1) {
449 strncpy(fs, fsname, sizeof(fs));
454 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
458 while (fgets(devices, sizeof(devices), fp) != NULL) {
459 char *bufp = devices, *tmp;
461 while (bufp[0] == ' ')
464 tmp = strstr(bufp, obd_type);
466 tmp += strlen(obd_type) + 1;
469 if (strlen(tmp) > sizeof(dev)-1) {
473 strncpy(dev, tmp, sizeof(dev));
474 tmp = strchr(dev, ' ');
481 if (dev[0] == '*' && strlen(fs))
482 snprintf(dev, PATH_MAX, "%s-*", fs);
483 snprintf(devices, PATH_MAX, "%s/%s/%s", obd_type, dev, param_name);
485 return get_param(devices, value, val_len);
489 * TYPE one of llite, lmv, lov.
490 * /proc/fs/lustre/TYPE/INST the directory of interest.
492 static int get_param_cli(const char *type, const char *inst,
493 const char *param, char *buf, size_t buf_size)
495 char param_path[PATH_MAX + 1];
496 FILE *param_file = NULL;
499 snprintf(param_path, sizeof(param_path),
500 "/proc/fs/lustre/%s/%s/%s", type, inst, param);
502 param_file = fopen(param_path, "r");
503 if (param_file == NULL) {
508 if (fgets(buf, buf_size, param_file) == NULL) {
515 if (param_file != NULL)
521 static int get_param_llite(const char *path,
522 const char *param, char *buf, size_t buf_size)
527 rc = llapi_getname(path, inst, sizeof(inst));
531 return get_param_cli("llite", inst, param, buf, buf_size);
534 static int get_param_lov(const char *path,
535 const char *param, char *buf, size_t buf_size)
537 struct obd_uuid uuid;
540 rc = llapi_file_get_lov_uuid(path, &uuid);
544 return get_param_cli("lov", uuid.uuid, param, buf, buf_size);
547 static int get_param_lmv(const char *path,
548 const char *param, char *buf, size_t buf_size)
550 struct obd_uuid uuid;
553 rc = llapi_file_get_lmv_uuid(path, &uuid);
557 return get_param_cli("lmv", uuid.uuid, param, buf, buf_size);
560 static int get_mds_md_size(const char *path)
562 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
566 /* Get the max ea size from llite proc. */
567 rc = get_param_llite(path, "max_easize", buf, sizeof(buf));
579 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
581 return get_param_lmv(path, "uuid", buf, bufsize);
585 * if pool is NULL, search ostname in target_obd
586 * if pool is not NULL:
587 * if pool not found returns errno < 0
588 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
589 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
591 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
594 char buffer[PATH_MAX + 1];
598 len = strlen(ostname);
600 if (poolname == NULL) {
604 rc = find_target_obdpath(fsname, buffer);
606 rc = find_poolpath(fsname, poolname, buffer);
611 fd = fopen(buffer, "r");
615 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
616 if (poolname == NULL) {
618 /* Search for an ostname in the list of OSTs
619 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
620 ptr = strchr(buffer, ' ');
622 (strncmp(ptr + 1, ostname, len) == 0)) {
627 /* Search for an ostname in a pool,
628 (or an existing non-empty pool if no ostname) */
629 if ((ostname == NULL) ||
630 (strncmp(buffer, ostname, len) == 0)) {
640 int llapi_file_open_pool(const char *name, int flags, int mode,
641 unsigned long long stripe_size, int stripe_offset,
642 int stripe_count, int stripe_pattern, char *pool_name)
644 struct lov_user_md_v3 lum = { 0 };
647 /* Make sure we have a good pool */
648 if (pool_name != NULL) {
649 char fsname[MAX_OBD_NAME + 1], *ptr;
651 rc = llapi_search_fsname(name, fsname);
653 llapi_error(LLAPI_MSG_ERROR, rc,
654 "'%s' is not on a Lustre filesystem",
659 /* in case user gives the full pool name <fsname>.<poolname>,
660 * strip the fsname */
661 ptr = strchr(pool_name, '.');
664 if (strcmp(pool_name, fsname) != 0) {
666 llapi_err_noerrno(LLAPI_MSG_ERROR,
667 "Pool '%s' is not on filesystem '%s'",
674 /* Make sure the pool exists and is non-empty */
675 rc = llapi_search_ost(fsname, pool_name, NULL);
677 llapi_err_noerrno(LLAPI_MSG_ERROR,
678 "pool '%s.%s' %s", fsname, pool_name,
679 rc == 0 ? "has no OSTs" : "does not exist");
685 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
687 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
688 flags = O_DIRECTORY | O_RDONLY;
695 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
699 rc = llapi_stripe_limit_check(stripe_size, stripe_offset, stripe_count,
704 /* Initialize IOCTL striping pattern structure */
705 lum.lmm_magic = LOV_USER_MAGIC_V3;
706 lum.lmm_pattern = stripe_pattern;
707 lum.lmm_stripe_size = stripe_size;
708 lum.lmm_stripe_count = stripe_count;
709 lum.lmm_stripe_offset = stripe_offset;
710 if (pool_name != NULL) {
711 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
713 /* If no pool is specified at all, use V1 request */
714 lum.lmm_magic = LOV_USER_MAGIC_V1;
717 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
718 char *errmsg = "stripe already set";
720 if (errno != EEXIST && errno != EALREADY)
721 errmsg = strerror(errno);
723 llapi_err_noerrno(LLAPI_MSG_ERROR,
724 "error on ioctl "LPX64" for '%s' (%d): %s",
725 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
736 int llapi_file_open(const char *name, int flags, int mode,
737 unsigned long long stripe_size, int stripe_offset,
738 int stripe_count, int stripe_pattern)
740 return llapi_file_open_pool(name, flags, mode, stripe_size,
741 stripe_offset, stripe_count,
742 stripe_pattern, NULL);
745 int llapi_file_create(const char *name, unsigned long long stripe_size,
746 int stripe_offset, int stripe_count, int stripe_pattern)
750 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
751 stripe_offset, stripe_count, stripe_pattern,
760 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
761 int stripe_offset, int stripe_count,
762 int stripe_pattern, char *pool_name)
766 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
767 stripe_offset, stripe_count, stripe_pattern,
776 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
777 int stripe_count, int stripe_pattern,
778 const char *pool_name)
780 struct lmv_user_md lum = { 0 };
784 lum.lum_magic = LMV_USER_MAGIC;
785 lum.lum_stripe_offset = stripe_offset;
786 lum.lum_stripe_count = stripe_count;
787 lum.lum_hash_type = stripe_pattern;
788 if (pool_name != NULL) {
789 if (strlen(pool_name) >= sizeof(lum.lum_pool_name)) {
790 llapi_err_noerrno(LLAPI_MSG_ERROR,
791 "error LL_IOC_LMV_SET_DEFAULT_STRIPE '%s'"
792 ": too large pool name: %s", name, pool_name);
795 strncpy(lum.lum_pool_name, pool_name, strlen(pool_name));
798 fd = open(name, O_DIRECTORY | O_RDONLY);
801 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
805 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lum);
807 char *errmsg = "stripe already set";
809 if (errno != EEXIST && errno != EALREADY)
810 errmsg = strerror(errno);
812 llapi_err_noerrno(LLAPI_MSG_ERROR,
813 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
820 int llapi_dir_create_pool(const char *name, int flags, int stripe_offset,
821 int stripe_count, int stripe_pattern,
822 const char *pool_name)
824 struct lmv_user_md lmu = { 0 };
825 struct obd_ioctl_data data = { 0 };
828 char *dirpath = NULL;
829 char *namepath = NULL;
835 dirpath = strdup(name);
836 namepath = strdup(name);
837 if (!dirpath || !namepath)
840 lmu.lum_magic = LMV_USER_MAGIC;
841 lmu.lum_stripe_offset = stripe_offset;
842 lmu.lum_stripe_count = stripe_count;
843 lmu.lum_hash_type = stripe_pattern;
844 if (pool_name != NULL) {
845 if (strlen(pool_name) >= LOV_MAXPOOLNAME) {
846 llapi_err_noerrno(LLAPI_MSG_ERROR,
847 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
848 "pool name: %s", name, pool_name);
852 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
855 filename = basename(namepath);
856 dir = dirname(dirpath);
858 data.ioc_inlbuf1 = (char *)filename;
859 data.ioc_inllen1 = strlen(filename) + 1;
860 data.ioc_inlbuf2 = (char *)&lmu;
861 data.ioc_inllen2 = sizeof(struct lmv_user_md);
862 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
864 llapi_error(LLAPI_MSG_ERROR, rc,
865 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
870 fd = open(dir, O_DIRECTORY | O_RDONLY);
873 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
877 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
878 char *errmsg = "stripe already set";
880 if (errno != EEXIST && errno != EALREADY)
881 errmsg = strerror(errno);
883 llapi_err_noerrno(LLAPI_MSG_ERROR,
884 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
894 int llapi_direntry_remove(char *dname)
896 char *dirpath = NULL;
897 char *namepath = NULL;
903 dirpath = strdup(dname);
904 namepath = strdup(dname);
905 if (!dirpath || !namepath)
908 filename = basename(namepath);
910 dir = dirname(dirpath);
912 fd = open(dir, O_DIRECTORY | O_RDONLY);
915 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
920 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
921 char *errmsg = strerror(errno);
922 llapi_err_noerrno(LLAPI_MSG_ERROR,
923 "error on ioctl "LPX64" for '%s' (%d): %s",
924 (__u64)LL_IOC_LMV_SETSTRIPE, filename,
936 * Find the fsname, the full path, and/or an open fd.
937 * Either the fsname or path must not be NULL
939 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
942 char buf[PATH_MAX], mntdir[PATH_MAX];
945 int idx = 0, len = 0, mntlen, fd;
948 /* get the mount point */
949 fp = setmntent(MOUNTED, "r");
952 llapi_error(LLAPI_MSG_ERROR, rc,
953 "setmntent(%s) failed", MOUNTED);
957 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
960 if (!llapi_is_lustre_mnt(&mnt))
963 if ((want & WANT_INDEX) && (idx++ != index))
966 mntlen = strlen(mnt.mnt_dir);
967 ptr = strrchr(mnt.mnt_fsname, '/');
968 /* thanks to the call to llapi_is_lustre_mnt() above,
969 * we are sure that mnt.mnt_fsname contains ":/",
970 * so ptr should never be NULL */
975 /* Check the fsname for a match, if given */
976 if (!(want & WANT_FSNAME) && fsname != NULL &&
977 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
980 /* If the path isn't set return the first one we find */
981 if (path == NULL || strlen(path) == 0) {
982 strcpy(mntdir, mnt.mnt_dir);
983 if ((want & WANT_FSNAME) && fsname != NULL)
987 /* Otherwise find the longest matching path */
988 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
989 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
990 strcpy(mntdir, mnt.mnt_dir);
992 if ((want & WANT_FSNAME) && fsname != NULL)
1001 if ((want & WANT_PATH) && path != NULL)
1002 strcpy(path, mntdir);
1003 if (want & WANT_FD) {
1004 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1007 llapi_error(LLAPI_MSG_ERROR, rc,
1008 "error opening '%s'", mntdir);
1014 } else if (want & WANT_ERROR)
1015 llapi_err_noerrno(LLAPI_MSG_ERROR,
1016 "can't find fs root for '%s': %d",
1017 (want & WANT_PATH) ? fsname : path, rc);
1022 * search lustre mounts
1024 * Calling this function will return to the user the mount point, mntdir, and
1025 * the file system name, fsname, if the user passed a buffer to this routine.
1027 * The user inputs are pathname and index. If the pathname is supplied then
1028 * the value of the index will be ignored. The pathname will return data if
1029 * the pathname is located on a lustre mount. Index is used to pick which
1030 * mount point you want in the case of multiple mounted lustre file systems.
1031 * See function lfs_osts in lfs.c for a example of the index use.
1033 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1036 int want = WANT_PATH, idx = -1;
1038 if (!pathname || pathname[0] == '\0') {
1042 strcpy(mntdir, pathname);
1045 want |= WANT_FSNAME;
1046 return get_root_path(want, fsname, NULL, mntdir, idx);
1049 /* Given a path, find the corresponding Lustre fsname */
1050 int llapi_search_fsname(const char *pathname, char *fsname)
1055 path = realpath(pathname, NULL);
1057 char buf[PATH_MAX + 1], *ptr;
1060 if (pathname[0] != '/') {
1061 /* Need an absolute path, but realpath() only works for
1062 * pathnames that actually exist. We go through the
1063 * extra hurdle of dirname(getcwd() + pathname) in
1064 * case the relative pathname contains ".." in it. */
1065 if (getcwd(buf, sizeof(buf) - 1) == NULL)
1069 strncat(buf, pathname, sizeof(buf) - strlen(buf));
1070 path = realpath(buf, NULL);
1072 ptr = strrchr(buf, '/');
1076 path = realpath(buf, NULL);
1079 llapi_error(LLAPI_MSG_ERROR, rc,
1080 "pathname '%s' cannot expand",
1086 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1091 int llapi_search_rootpath(char *pathname, const char *fsname)
1093 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1096 int llapi_getname(const char *path, char *buf, size_t size)
1098 struct obd_uuid uuid_buf;
1099 char *uuid = uuid_buf.uuid;
1102 memset(&uuid_buf, 0, sizeof(uuid_buf));
1103 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1107 /* We want to turn lustre-clilov-ffff88002738bc00 into
1108 * lustre-ffff88002738bc00. */
1110 nr = snprintf(buf, size, "%.*s-%s",
1111 (int) (strlen(uuid) - 24), uuid,
1112 uuid + strlen(uuid) - 16);
1122 * find the pool directory path under /proc
1123 * (can be also used to test if a fsname is known)
1125 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
1128 char pattern[PATH_MAX + 1];
1129 char buffer[PATH_MAX];
1131 if (fsname == NULL) {
1132 rc = llapi_search_fsname(pathname, buffer);
1136 strcpy(pathname, fsname);
1139 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
1140 rc = first_match(pattern, buffer);
1144 /* in fsname test mode, pool_pathname is NULL */
1145 if (pool_pathname != NULL)
1146 strcpy(pool_pathname, buffer);
1152 * Get the list of pool members.
1153 * \param poolname string of format \<fsname\>.\<poolname\>
1154 * \param members caller-allocated array of char*
1155 * \param list_size size of the members array
1156 * \param buffer caller-allocated buffer for storing OST names
1157 * \param buffer_size size of the buffer
1159 * \return number of members retrieved for this pool
1160 * \retval -error failure
1162 int llapi_get_poolmembers(const char *poolname, char **members,
1163 int list_size, char *buffer, int buffer_size)
1165 char fsname[PATH_MAX + 1];
1167 char pathname[PATH_MAX + 1];
1168 char path[PATH_MAX + 1];
1175 /* name is FSNAME.POOLNAME */
1176 if (strlen(poolname) > PATH_MAX)
1178 strcpy(fsname, poolname);
1179 pool = strchr(fsname, '.');
1186 rc = poolpath(fsname, NULL, pathname);
1188 llapi_error(LLAPI_MSG_ERROR, rc,
1189 "Lustre filesystem '%s' not found",
1194 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1195 sprintf(path, "%s/%s", pathname, pool);
1196 fd = fopen(path, "r");
1199 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
1204 while (fgets(buf, sizeof(buf), fd) != NULL) {
1205 if (nb_entries >= list_size) {
1210 tmp = strchr(buf, '\n');
1213 if (used + strlen(buf) + 1 > buffer_size) {
1218 strcpy(buffer + used, buf);
1219 members[nb_entries] = buffer + used;
1220 used += strlen(buf) + 1;
1230 * Get the list of pools in a filesystem.
1231 * \param name filesystem name or path
1232 * \param poollist caller-allocated array of char*
1233 * \param list_size size of the poollist array
1234 * \param buffer caller-allocated buffer for storing pool names
1235 * \param buffer_size size of the buffer
1237 * \return number of pools retrieved for this filesystem
1238 * \retval -error failure
1240 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1241 char *buffer, int buffer_size)
1243 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
1247 struct dirent *cookie = NULL;
1249 unsigned int nb_entries = 0;
1250 unsigned int used = 0;
1253 /* initilize output array */
1254 for (i = 0; i < list_size; i++)
1257 /* is name a pathname ? */
1258 ptr = strchr(name, '/');
1260 /* only absolute pathname is supported */
1264 if (!realpath(name, rname)) {
1266 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1271 rc = poolpath(NULL, rname, pathname);
1273 llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
1274 " a Lustre filesystem", name);
1277 if (strlen(rname) > sizeof(fsname)-1)
1279 strncpy(fsname, rname, sizeof(fsname));
1281 /* name is FSNAME */
1282 if (strlen(name) > sizeof(fsname)-1)
1284 strncpy(fsname, name, sizeof(fsname));
1285 rc = poolpath(fsname, NULL, pathname);
1288 llapi_error(LLAPI_MSG_ERROR, rc,
1289 "Lustre filesystem '%s' not found", name);
1293 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1294 dir = opendir(pathname);
1297 llapi_error(LLAPI_MSG_ERROR, rc,
1298 "Could not open pool list for '%s'",
1304 rc = readdir_r(dir, &pool, &cookie);
1308 llapi_error(LLAPI_MSG_ERROR, rc,
1309 "Error reading pool list for '%s'", name);
1311 } else if ((rc == 0) && (cookie == NULL)) {
1312 /* end of directory */
1316 /* ignore . and .. */
1317 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1320 /* check output bounds */
1321 if (nb_entries >= list_size) {
1326 /* +2 for '.' and final '\0' */
1327 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1333 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1334 poollist[nb_entries] = buffer + used;
1335 used += strlen(pool.d_name) + strlen(fsname) + 2;
1341 return ((rc != 0) ? rc : nb_entries);
1344 /* wrapper for lfs.c and obd.c */
1345 int llapi_poollist(const char *name)
1347 /* list of pool names (assume that pool count is smaller
1349 char **list, *buffer = NULL, *path = NULL, *fsname = NULL;
1350 int obdcount, bufsize, rc, nb, i;
1351 char *poolname = NULL, *tmp = NULL, data[16];
1353 if (name[0] != '/') {
1354 fsname = strdup(name);
1355 poolname = strchr(fsname, '.');
1359 path = (char *) name;
1362 rc = get_param_obdvar(fsname, path, "lov", "numobd",
1363 data, sizeof(data));
1366 obdcount = atoi(data);
1368 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1369 * and also an array to store the pointers for all that
1370 * allocated space. */
1372 bufsize = sizeof(struct obd_uuid) * obdcount;
1373 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1374 if (buffer == NULL) {
1378 list = (char **) (buffer + bufsize);
1381 /* name is a path or fsname */
1382 nb = llapi_get_poollist(name, list, obdcount,
1385 /* name is a pool name (<fsname>.<poolname>) */
1386 nb = llapi_get_poolmembers(name, list, obdcount,
1390 if (nb == -EOVERFLOW) {
1393 goto retry_get_pools;
1396 for (i = 0; i < nb; i++)
1397 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1398 rc = (nb < 0 ? nb : 0);
1407 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1408 void *data, struct dirent64 *de);
1410 #define OBD_NOT_FOUND (-1)
1412 static int common_param_init(struct find_param *param, char *path)
1414 int lumlen = get_mds_md_size(path);
1416 if (lumlen < PATH_MAX + 1)
1417 lumlen = PATH_MAX + 1;
1419 param->lumlen = lumlen;
1420 param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
1421 if (param->lmd == NULL) {
1422 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1423 "error: allocation of %zu bytes for ioctl",
1424 sizeof(lstat_t) + param->lumlen);
1428 param->fp_lmv_count = 256;
1429 param->fp_lmv_md = malloc(lmv_user_md_size(256, LMV_MAGIC_V1));
1430 if (param->fp_lmv_md == NULL) {
1431 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1432 "error: allocation of %d bytes for ioctl",
1433 lmv_user_md_size(256, LMV_MAGIC_V1));
1437 param->got_uuids = 0;
1438 param->obdindexes = NULL;
1439 param->obdindex = OBD_NOT_FOUND;
1440 if (!param->migrate)
1441 param->mdtindex = OBD_NOT_FOUND;
1445 static void find_param_fini(struct find_param *param)
1447 if (param->obdindexes)
1448 free(param->obdindexes);
1453 if (param->fp_lmv_md)
1454 free(param->fp_lmv_md);
1457 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1458 struct dirent64 *de)
1460 struct find_param *param = data;
1466 /* set errno upon failure */
1467 static DIR *opendir_parent(char *path)
1473 fname = strrchr(path, '/');
1475 return opendir(".");
1479 parent = opendir(path);
1484 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1486 struct lmv_user_md *lmv = (struct lmv_user_md *)param->fp_lmv_md;
1489 lmv->lum_stripe_count = param->fp_lmv_count;
1490 if (param->get_default_lmv)
1491 lmv->lum_magic = LMV_USER_MAGIC;
1493 lmv->lum_magic = LMV_MAGIC_V1;
1494 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, lmv);
1499 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1500 struct lov_user_mds_data *lmd, int lumlen)
1502 lstat_t *st = &lmd->lmd_st;
1505 if (parent == NULL && dir == NULL)
1509 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1510 } else if (parent) {
1511 char *fname = strrchr(path, '/');
1513 fname = (fname == NULL ? path : fname + 1);
1514 /* retrieve needed file info */
1515 strncpy((char *)lmd, fname, lumlen);
1516 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1520 if (errno == ENOTTY) {
1521 /* ioctl is not supported, it is not a lustre fs.
1522 * Do the regular lstat(2) instead. */
1523 ret = lstat_f(path, st);
1526 llapi_error(LLAPI_MSG_ERROR, ret,
1527 "error: %s: lstat failed for %s",
1530 } else if (errno == ENOENT) {
1532 llapi_error(LLAPI_MSG_WARN, ret,
1533 "warning: %s: %s does not exist",
1535 } else if (errno != EISDIR) {
1537 llapi_error(LLAPI_MSG_ERROR, ret,
1538 "%s ioctl failed for %s.",
1539 dir ? "LL_IOC_MDC_GETINFO" :
1540 "IOC_MDC_GETFILEINFO", path);
1543 llapi_error(LLAPI_MSG_ERROR, ret,
1544 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1551 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1552 semantic_func_t sem_init,
1553 semantic_func_t sem_fini, void *data,
1554 struct dirent64 *de)
1556 struct find_param *param = (struct find_param *)data;
1557 struct dirent64 *dent;
1565 if (!d && errno != ENOTDIR) {
1567 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1570 } else if (!d && !parent) {
1571 /* ENOTDIR. Open the parent dir. */
1572 p = opendir_parent(path);
1579 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1582 if (!d || (param->get_lmv && !param->recursive)) {
1587 while ((dent = readdir64(d)) != NULL) {
1588 param->have_fileinfo = 0;
1590 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1593 /* Don't traverse .lustre directory */
1594 if (!(strcmp(dent->d_name, dot_lustre_name)))
1598 if ((len + dent->d_reclen + 2) > size) {
1599 llapi_err_noerrno(LLAPI_MSG_ERROR,
1600 "error: %s: string buffer is too small",
1605 strcat(path, dent->d_name);
1607 if (dent->d_type == DT_UNKNOWN) {
1608 lstat_t *st = ¶m->lmd->lmd_st;
1610 ret = get_lmd_info(path, d, NULL, param->lmd,
1613 dent->d_type = IFTODT(st->st_mode);
1618 switch (dent->d_type) {
1620 llapi_err_noerrno(LLAPI_MSG_ERROR,
1621 "error: %s: '%s' is UNKNOWN type %d",
1622 __func__, dent->d_name, dent->d_type);
1625 ret = llapi_semantic_traverse(path, size, d, sem_init,
1626 sem_fini, data, dent);
1633 ret = sem_init(path, d, NULL, data, dent);
1637 if (sem_fini && ret == 0)
1638 sem_fini(path, d, NULL, data, dent);
1646 sem_fini(path, parent, &d, data, de);
1655 static int param_callback(char *path, semantic_func_t sem_init,
1656 semantic_func_t sem_fini, struct find_param *param)
1658 int ret, len = strlen(path);
1661 if (len > PATH_MAX) {
1663 llapi_error(LLAPI_MSG_ERROR, ret,
1664 "Path name '%s' is too long", path);
1668 buf = (char *)malloc(PATH_MAX + 1);
1672 strncpy(buf, path, PATH_MAX + 1);
1673 ret = common_param_init(param, buf);
1677 param->fp_depth = 0;
1679 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1680 sem_fini, param, NULL);
1682 find_param_fini(param);
1684 return ret < 0 ? ret : 0;
1687 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1689 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1692 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1697 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1699 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1702 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1707 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1711 fd = open(path, O_RDONLY);
1714 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1718 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1724 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1728 fd = open(path, O_RDONLY);
1731 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1735 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1746 * If uuidp is NULL, return the number of available obd uuids.
1747 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1748 * there are more OSTs then allocated to uuidp, then an error is returned with
1749 * the ost_count set to number of available obd uuids.
1751 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1752 int *ost_count, enum tgt_type type)
1754 struct obd_uuid name;
1757 int rc = 0, index = 0;
1759 /* Get the lov name */
1760 if (type == LOV_TYPE) {
1761 rc = llapi_file_fget_lov_uuid(fd, &name);
1765 rc = llapi_file_fget_lmv_uuid(fd, &name);
1770 /* Now get the ost uuids from /proc */
1771 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1772 type == LOV_TYPE ? "lov" : "lmv", name.uuid);
1773 fp = fopen(buf, "r");
1776 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1780 while (fgets(buf, sizeof(buf), fp) != NULL) {
1781 if (uuidp && (index < *ost_count)) {
1782 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
1790 if (uuidp && (index > *ost_count))
1797 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1799 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1802 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1807 root = opendir(mnt);
1810 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1815 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1823 /* Check if user specified value matches a real uuid. Ignore _UUID,
1824 * -osc-4ba41334, other trailing gunk in comparison.
1825 * @param real_uuid ends in "_UUID"
1826 * @param search_uuid may or may not end in "_UUID"
1828 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1830 int cmplen = strlen(real_uuid);
1831 int searchlen = strlen(search_uuid);
1833 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1835 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1838 /* The UUIDs may legitimately be different lengths, if
1839 * the system was upgraded from an older version. */
1840 if (cmplen != searchlen)
1843 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1846 /* Here, param->obduuid points to a single obduuid, the index of which is
1847 * returned in param->obdindex */
1848 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1850 struct obd_uuid obd_uuid;
1851 char uuid[sizeof(struct obd_uuid)];
1856 if (param->got_uuids)
1859 /* Get the lov/lmv name */
1861 rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
1863 rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
1865 if (rc != -ENOTTY) {
1866 llapi_error(LLAPI_MSG_ERROR, rc,
1867 "error: can't get lov name: %s", dname);
1874 param->got_uuids = 1;
1876 /* Now get the ost uuids from /proc */
1877 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1878 param->get_lmv ? "lmv" : "lov", obd_uuid.uuid);
1879 fp = fopen(buf, "r");
1882 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1886 if (!param->obduuid && !param->quiet && !param->obds_printed)
1887 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1888 param->get_lmv ? "MDTS" : "OBDS:");
1890 while (fgets(buf, sizeof(buf), fp) != NULL) {
1891 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
1894 if (param->obduuid) {
1895 if (llapi_uuid_match(uuid, param->obduuid->uuid)) {
1896 param->obdindex = index;
1899 } else if (!param->quiet && !param->obds_printed) {
1900 /* Print everything */
1901 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1904 param->obds_printed = 1;
1908 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1909 llapi_err_noerrno(LLAPI_MSG_ERROR,
1910 "error: %s: unknown obduuid: %s",
1911 __func__, param->obduuid->uuid);
1918 /* In this case, param->obduuid will be an array of obduuids and
1919 * obd index for all these obduuids will be returned in
1920 * param->obdindexes */
1921 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1922 int num_obds, int **obdindexes, int *obdindex,
1925 int ret, obdcount, obd_valid = 0, obdnum;
1927 struct obd_uuid *uuids = NULL;
1931 if (type == LOV_TYPE)
1932 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
1934 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
1938 obdcount = atoi(buf);
1939 uuids = (struct obd_uuid *)malloc(obdcount *
1940 sizeof(struct obd_uuid));
1945 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1947 struct obd_uuid *uuids_temp;
1949 if (ret == -EOVERFLOW) {
1950 uuids_temp = realloc(uuids, obdcount *
1951 sizeof(struct obd_uuid));
1952 if (uuids_temp != NULL) {
1954 goto retry_get_uuids;
1960 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
1964 indexes = malloc(num_obds * sizeof(*obdindex));
1965 if (indexes == NULL) {
1970 for (obdnum = 0; obdnum < num_obds; obdnum++) {
1973 /* The user may have specified a simple index */
1974 i = strtol(obduuids[obdnum].uuid, &end, 0);
1975 if (end && *end == '\0' && i < obdcount) {
1976 indexes[obdnum] = i;
1979 for (i = 0; i < obdcount; i++) {
1980 if (llapi_uuid_match(uuids[i].uuid,
1981 obduuids[obdnum].uuid)) {
1982 indexes[obdnum] = i;
1988 if (i >= obdcount) {
1989 indexes[obdnum] = OBD_NOT_FOUND;
1990 llapi_err_noerrno(LLAPI_MSG_ERROR,
1991 "error: %s: unknown obduuid: %s",
1992 __func__, obduuids[obdnum].uuid);
1998 *obdindex = OBD_NOT_FOUND;
2000 *obdindex = obd_valid;
2002 *obdindexes = indexes;
2010 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2014 if (param->mdtuuid) {
2015 ret = setup_indexes(dir, path, param->mdtuuid, param->num_mdts,
2016 ¶m->mdtindexes, ¶m->mdtindex, LMV_TYPE);
2020 if (param->obduuid) {
2021 ret = setup_indexes(dir, path, param->obduuid, param->num_obds,
2022 ¶m->obdindexes, ¶m->obdindex, LOV_TYPE);
2026 param->got_uuids = 1;
2030 int llapi_ostlist(char *path, struct find_param *param)
2035 dir = opendir(path);
2039 ret = setup_obd_uuid(dir, path, param);
2046 * Given a filesystem name, or a pathname of a file on a lustre filesystem,
2047 * tries to determine the path to the filesystem's clilov directory under /proc
2049 * fsname is limited to MTI_NAME_MAXLEN in lustre_idl.h
2050 * The NUL terminator is compensated by the additional "%s" bytes. */
2051 #define LOV_LEN (sizeof("/proc/fs/lustre/lov/%s-clilov-*") + MTI_NAME_MAXLEN)
2052 static int clilovpath(const char *fsname, const char *const pathname,
2056 char pattern[LOV_LEN];
2057 char buffer[PATH_MAX + 1];
2059 if (fsname == NULL) {
2060 rc = llapi_search_fsname(pathname, buffer);
2066 snprintf(pattern, sizeof(pattern), "/proc/fs/lustre/lov/%s-clilov-*",
2069 rc = first_match(pattern, buffer);
2073 strncpy(clilovpath, buffer, sizeof(buffer));
2079 * Given the path to a stripe attribute proc file, tries to open and
2080 * read the attribute and return the value using the attr parameter
2082 static int sattr_read_attr(const char *const fpath,
2087 char line[PATH_MAX + 1];
2090 f = fopen(fpath, "r");
2093 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open '%s'", fpath);
2097 if (fgets(line, sizeof(line), f) != NULL) {
2100 llapi_error(LLAPI_MSG_ERROR, errno, "Cannot read from '%s'", fpath);
2109 * Tries to determine the default stripe attributes for a given filesystem. The
2110 * filesystem to check should be specified by fsname, or will be determined
2113 static int sattr_get_defaults(const char *const fsname,
2114 const char *const pathname,
2115 unsigned int *scount,
2116 unsigned int *ssize,
2117 unsigned int *soffset)
2120 char dpath[PATH_MAX + 1];
2121 char fpath[PATH_MAX + 1];
2123 rc = clilovpath(fsname, pathname, dpath);
2128 snprintf(fpath, PATH_MAX, "%s/stripecount", dpath);
2129 rc = sattr_read_attr(fpath, scount);
2135 snprintf(fpath, PATH_MAX, "%s/stripesize", dpath);
2136 rc = sattr_read_attr(fpath, ssize);
2142 snprintf(fpath, PATH_MAX, "%s/stripeoffset", dpath);
2143 rc = sattr_read_attr(fpath, soffset);
2152 * Tries to gather the default stripe attributes for a given filesystem. If
2153 * the attributes can be determined, they are cached for easy retreival the
2154 * next time they are needed. Only a single filesystem's attributes are
2157 static int sattr_cache_get_defaults(const char *const fsname,
2158 const char *const pathname,
2159 unsigned int *scount,
2160 unsigned int *ssize,
2161 unsigned int *soffset)
2164 char fsname[PATH_MAX + 1];
2165 unsigned int stripecount;
2166 unsigned int stripesize;
2167 unsigned int stripeoffset;
2173 char fsname_buf[PATH_MAX + 1];
2174 unsigned int tmp[3];
2176 if (fsname == NULL) {
2177 rc = llapi_search_fsname(pathname, fsname_buf);
2181 strncpy(fsname_buf, fsname, PATH_MAX);
2184 if (strncmp(fsname_buf, cache.fsname, PATH_MAX) != 0) {
2186 * Ensure all 3 sattrs (count, size, and offset) are
2187 * successfully retrieved and stored in tmp before writing to
2190 rc = sattr_get_defaults(fsname_buf, NULL, &tmp[0], &tmp[1],
2195 cache.stripecount = tmp[0];
2196 cache.stripesize = tmp[1];
2197 cache.stripeoffset = tmp[2];
2198 strncpy(cache.fsname, fsname_buf, PATH_MAX);
2202 *scount = cache.stripecount;
2204 *ssize = cache.stripesize;
2206 *soffset = cache.stripeoffset;
2211 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2212 struct lov_user_ost_data_v1 *objects,
2213 int is_dir, int verbose, int depth,
2214 int raw, char *pool_name)
2216 char *prefix = is_dir ? "" : "lmm_";
2217 char *seperator = "";
2220 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2221 lmm_oi_set_seq(&lum->lmm_oi, 0);
2222 if (verbose & VERBOSE_DETAIL)
2223 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
2226 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2227 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2229 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2230 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
2232 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
2233 lmm_oi_seq(&lum->lmm_oi));
2234 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
2235 lmm_oi_id(&lum->lmm_oi));
2238 if (verbose & VERBOSE_COUNT) {
2239 if (verbose & ~VERBOSE_COUNT)
2240 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
2243 if (!raw && lum->lmm_stripe_count == 0) {
2244 unsigned int scount;
2245 rc = sattr_cache_get_defaults(NULL, path,
2249 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2252 llapi_error(LLAPI_MSG_ERROR, rc,
2253 "Cannot determine default"
2256 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2257 lum->lmm_stripe_count ==
2258 (typeof(lum->lmm_stripe_count))(-1)
2259 ? -1 : lum->lmm_stripe_count);
2262 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2263 (__s16)lum->lmm_stripe_count);
2265 seperator = is_dir ? " " : "\n";
2268 if (verbose & VERBOSE_SIZE) {
2269 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2270 if (verbose & ~VERBOSE_SIZE)
2271 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
2273 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
2275 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2278 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2280 llapi_error(LLAPI_MSG_ERROR, rc,
2281 "Cannot determine default"
2284 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2285 lum->lmm_stripe_size);
2287 seperator = is_dir ? " " : "\n";
2290 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2291 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2292 if (verbose & ~VERBOSE_LAYOUT)
2293 llapi_printf(LLAPI_MSG_NORMAL, "%spattern: ",
2295 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2299 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2300 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2301 if (verbose & ~VERBOSE_GENERATION)
2302 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
2304 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2305 (int)lum->lmm_layout_gen);
2309 if (verbose & VERBOSE_OFFSET) {
2310 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2311 if (verbose & ~VERBOSE_OFFSET)
2312 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
2315 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2316 lum->lmm_stripe_offset ==
2317 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2318 lum->lmm_stripe_offset);
2320 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2321 objects[0].l_ost_idx);
2322 seperator = is_dir ? " " : "\n";
2325 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
2326 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2327 if (verbose & ~VERBOSE_POOL)
2328 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
2330 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2333 if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
2334 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2337 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2338 struct lov_user_ost_data_v1 *objects,
2339 char *path, int is_dir, int obdindex,
2340 int depth, int header, int raw)
2342 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2345 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2346 if (obdindex == objects[i].l_ost_idx) {
2354 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
2355 depth, raw, pool_name);
2357 if (!is_dir && (header & VERBOSE_OBJID) &&
2358 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2360 llapi_printf(LLAPI_MSG_NORMAL,
2361 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2363 for (i = 0; i < lum->lmm_stripe_count; i++) {
2364 int idx = objects[i].l_ost_idx;
2365 long long oid = ostid_id(&objects[i].l_ost_oi);
2366 long long gr = ostid_seq(&objects[i].l_ost_oi);
2367 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) {
2369 sprintf(fmt, "%s%s%s\n",
2370 "\t%6u\t%14llu\t%#13llx\t",
2371 (fid_seq_is_rsvd(gr) ||
2372 fid_seq_is_mdt0(gr)) ?
2373 "%14llu" : "%#14llx", "%s");
2374 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2376 obdindex == idx ? " *" : "");
2380 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2384 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2385 char *path, int obdindex, int depth, int verbose)
2387 struct lmv_user_mds_data *objects = lum->lum_objects;
2388 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2389 int i, obdstripe = 0;
2390 char *separator = "";
2392 if (obdindex != OBD_NOT_FOUND) {
2393 for (i = 0; i < lum->lum_stripe_count; i++) {
2394 if (obdindex == objects[i].lum_mds) {
2395 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix,
2405 /* show all information default */
2407 if (lum->lum_magic == LMV_USER_MAGIC)
2408 verbose = VERBOSE_POOL | VERBOSE_COUNT | VERBOSE_OFFSET;
2410 verbose = VERBOSE_OBJID;
2413 if (depth && path && ((verbose != VERBOSE_OBJID)))
2414 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2416 if (verbose & VERBOSE_COUNT) {
2417 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2418 if (verbose & ~VERBOSE_COUNT)
2419 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2420 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2421 (int)lum->lum_stripe_count);
2425 if (verbose & VERBOSE_OFFSET) {
2426 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2427 if (verbose & ~VERBOSE_OFFSET)
2428 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2429 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2430 (int)lum->lum_stripe_offset);
2434 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2435 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2436 if (obdstripe == 1 && lum->lum_stripe_count > 0)
2437 llapi_printf(LLAPI_MSG_NORMAL,
2438 "mdtidx\t\t FID[seq:oid:ver]\n");
2439 for (i = 0; i < lum->lum_stripe_count; i++) {
2440 int idx = objects[i].lum_mds;
2441 struct lu_fid *fid = &objects[i].lum_fid;
2442 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2443 llapi_printf(LLAPI_MSG_NORMAL,
2444 "%6u\t\t "DFID"\t\t%s\n",
2446 obdindex == idx ? " *" : "");
2451 if ((verbose & VERBOSE_POOL) && (pool_name[0] != '\0')) {
2452 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2453 if (verbose & ~VERBOSE_POOL)
2454 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2456 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2460 if (!(verbose & VERBOSE_OBJID))
2461 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2464 void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
2468 if (param->get_lmv || param->get_default_lmv)
2469 magic = (__u32)param->fp_lmv_md->lum_magic;
2471 magic = *(__u32 *)¶m->lmd->lmd_lmm; /* lum->lmm_magic */
2474 case LOV_USER_MAGIC_V1:
2475 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
2476 param->lmd->lmd_lmm.lmm_objects,
2478 param->obdindex, param->fp_max_depth,
2479 param->verbose, param->raw);
2481 case LOV_USER_MAGIC_V3: {
2482 char pool_name[LOV_MAXPOOLNAME + 1];
2483 struct lov_user_ost_data_v1 *objects;
2484 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2486 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
2487 pool_name[LOV_MAXPOOLNAME] = '\0';
2488 objects = lmmv3->lmm_objects;
2489 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
2490 objects, path, is_dir,
2491 param->obdindex, param->fp_max_depth,
2492 param->verbose, param->raw);
2496 case LMV_USER_MAGIC: {
2497 char pool_name[LOV_MAXPOOLNAME + 1];
2498 struct lmv_user_md *lum;
2500 lum = (struct lmv_user_md *)param->fp_lmv_md;
2501 strncpy(pool_name, lum->lum_pool_name, LOV_MAXPOOLNAME);
2502 lmv_dump_user_lmm(lum, pool_name, path,
2503 param->obdindex, param->fp_max_depth,
2508 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2509 "(expecting one of %#x %#x %#x %#x)\n",
2510 *(__u32 *)¶m->lmd->lmd_lmm,
2511 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2512 LMV_USER_MAGIC, LMV_MAGIC_V1);
2517 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2523 fname = strrchr(path, '/');
2525 /* It should be a file (or other non-directory) */
2526 if (fname == NULL) {
2527 dname = (char *)malloc(2);
2531 fname = (char *)path;
2533 dname = (char *)malloc(fname - path + 1);
2536 strncpy(dname, path, fname - path);
2537 dname[fname - path] = '\0';
2541 fd = open(dname, O_RDONLY);
2548 strcpy((char *)lum, fname);
2549 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2552 if (close(fd) == -1 && rc == 0)
2559 int llapi_file_lookup(int dirfd, const char *name)
2561 struct obd_ioctl_data data = { 0 };
2566 if (dirfd < 0 || name == NULL)
2569 data.ioc_version = OBD_IOCTL_VERSION;
2570 data.ioc_len = sizeof(data);
2571 data.ioc_inlbuf1 = (char *)name;
2572 data.ioc_inllen1 = strlen(name) + 1;
2574 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2576 llapi_error(LLAPI_MSG_ERROR, rc,
2577 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2582 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2588 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2589 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2591 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2592 * The table below gives the answers for the specified parameters (value and
2593 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2594 * --------------------------------------
2595 * 1 | file > limit; sign > 0 | -1 / -1 |
2596 * 2 | file = limit; sign > 0 | -1 / -1 |
2597 * 3 | file < limit; sign > 0 | ? / 1 |
2598 * 4 | file > limit; sign = 0 | -1 / -1 |
2599 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2600 * 6 | file < limit; sign = 0 | ? / -1 |
2601 * 7 | file > limit; sign < 0 | 1 / 1 |
2602 * 8 | file = limit; sign < 0 | ? / -1 |
2603 * 9 | file < limit; sign < 0 | ? / -1 |
2604 * --------------------------------------
2605 * Note: 5th actually means that the value is within the interval
2606 * (limit - margin, limit]. */
2607 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2608 int sign, int negopt, unsigned long long margin,
2614 /* Drop the fraction of margin (of days). */
2615 if (file + margin <= limit)
2617 } else if (sign == 0) {
2618 if (file <= limit && file + margin > limit)
2620 else if (file + margin <= limit)
2622 } else if (sign < 0) {
2629 return negopt ? ~ret + 1 : ret;
2632 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2633 * Return -1 or 1 if file timestamp does not or does match the given criteria
2634 * correspondingly. Return 0 if the MDS time is being checked and there are
2635 * attributes on OSTs and it is not yet clear if the timespamp matches.
2637 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2638 * updated timestamps. */
2639 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2644 /* Check if file is accepted. */
2645 if (param->fp_atime) {
2646 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
2647 param->fp_asign, param->fp_exclude_atime,
2654 if (param->fp_mtime) {
2655 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
2656 param->fp_msign, param->fp_exclude_mtime,
2661 /* If the previous check matches, but this one is not yet clear,
2662 * we should return 0 to do an RPC on OSTs. */
2667 if (param->fp_ctime) {
2668 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
2669 param->fp_csign, param->fp_exclude_ctime,
2674 /* If the previous check matches, but this one is not yet clear,
2675 * we should return 0 to do an RPC on OSTs. */
2684 * Check whether the stripes matches the indexes user provided
2688 static int check_obd_match(struct find_param *param)
2690 lstat_t *st = ¶m->lmd->lmd_st;
2691 struct lov_user_ost_data_v1 *lmm_objects;
2694 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
2697 if (!S_ISREG(st->st_mode))
2700 /* Only those files should be accepted, which have a
2701 * stripe on the specified OST. */
2702 if (!param->lmd->lmd_lmm.lmm_stripe_count)
2705 if (param->lmd->lmd_lmm.lmm_magic ==
2706 LOV_USER_MAGIC_V3) {
2707 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2709 lmm_objects = lmmv3->lmm_objects;
2710 } else if (param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2711 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
2713 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2714 __func__, param->lmd->lmd_lmm.lmm_magic);
2718 for (i = 0; i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
2719 for (j = 0; j < param->num_obds; j++) {
2720 if (param->obdindexes[j] ==
2721 lmm_objects[i].l_ost_idx) {
2722 if (param->exclude_obd)
2729 if (param->exclude_obd)
2734 static int check_mdt_match(struct find_param *param)
2738 if (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND)
2741 /* FIXME: For striped dir, we should get stripe information and check */
2742 for (i = 0; i < param->num_mdts; i++) {
2743 if (param->mdtindexes[i] == param->file_mdtindex)
2744 return !param->exclude_mdt;
2747 if (param->exclude_mdt)
2753 * Check whether the obd is active or not, if it is
2754 * not active, just print the object affected by this
2757 static int print_failed_tgt(struct find_param *param, char *path, int type)
2759 struct obd_statfs stat_buf;
2760 struct obd_uuid uuid_buf;
2763 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2765 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2766 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2767 ret = llapi_obd_statfs(path, type,
2768 param->obdindex, &stat_buf,
2771 llapi_printf(LLAPI_MSG_NORMAL,
2772 "obd_uuid: %s failed %s ",
2773 param->obduuid->uuid,
2779 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
2780 void *data, struct dirent64 *de)
2782 struct find_param *param = (struct find_param *)data;
2783 DIR *dir = dirp == NULL ? NULL : *dirp;
2784 int decision = 1; /* 1 is accepted; -1 is rejected. */
2785 lstat_t *st = ¶m->lmd->lmd_st;
2787 int checked_type = 0;
2790 LASSERT(parent != NULL || dir != NULL);
2792 if (param->have_fileinfo == 0)
2793 param->lmd->lmd_lmm.lmm_stripe_count = 0;
2795 /* If a regular expression is presented, make the initial decision */
2796 if (param->pattern != NULL) {
2797 char *fname = strrchr(path, '/');
2798 fname = (fname == NULL ? path : fname + 1);
2799 ret = fnmatch(param->pattern, fname, 0);
2800 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
2801 (ret == 0 && param->exclude_pattern))
2805 /* See if we can check the file type from the dirent. */
2806 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
2809 if (DTTOIF(de->d_type) == param->fp_type) {
2810 if (param->fp_exclude_type)
2813 if (!param->fp_exclude_type)
2820 /* Request MDS for the stat info if some of these parameters need
2821 * to be compared. */
2822 if (param->obduuid || param->mdtuuid ||
2823 param->fp_check_uid || param->fp_check_gid ||
2824 param->fp_atime || param->fp_mtime || param->fp_ctime ||
2825 param->check_pool || param->check_size ||
2826 param->check_stripecount || param->check_stripesize ||
2827 param->check_layout)
2830 if (param->fp_type != 0 && checked_type == 0)
2833 if (param->have_fileinfo == 0 && decision == 0) {
2834 ret = get_lmd_info(path, parent, dir, param->lmd,
2838 ret = llapi_file_fget_mdtidx(dirfd(dir),
2839 ¶m->file_mdtindex);
2844 ret = lstat_f(path, &tmp_st);
2847 llapi_error(LLAPI_MSG_ERROR, ret,
2848 "error: %s: lstat failed"
2849 "for %s", __func__, path);
2852 if (S_ISREG(tmp_st.st_mode)) {
2853 fd = open(path, O_RDONLY);
2855 ret = llapi_file_fget_mdtidx(fd,
2856 ¶m->file_mdtindex);
2862 /* For special inode, it assumes to
2863 * reside on the same MDT with the
2866 ret = llapi_file_fget_mdtidx(fd,
2867 ¶m->file_mdtindex);
2880 if (param->fp_type && !checked_type) {
2881 if ((st->st_mode & S_IFMT) == param->fp_type) {
2882 if (param->fp_exclude_type)
2885 if (!param->fp_exclude_type)
2891 if (param->obduuid || param->mdtuuid) {
2892 if (lustre_fs && param->got_uuids &&
2893 param->fp_dev != st->st_dev) {
2894 /* A lustre/lustre mount point is crossed. */
2895 param->got_uuids = 0;
2896 param->obds_printed = 0;
2897 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2900 if (lustre_fs && !param->got_uuids) {
2901 ret = setup_target_indexes(dir ? dir : parent, path,
2906 param->fp_dev = st->st_dev;
2907 } else if (!lustre_fs && param->got_uuids) {
2908 /* A lustre/non-lustre mount point is crossed. */
2909 param->got_uuids = 0;
2910 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2914 if (param->check_stripesize) {
2915 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_size,
2917 param->stripesize_sign,
2918 param->exclude_stripesize,
2919 param->stripesize_units, 0);
2924 if (param->check_stripecount) {
2925 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_count,
2927 param->stripecount_sign,
2928 param->exclude_stripecount, 1, 0);
2933 if (param->check_layout) {
2936 found = (param->lmd->lmd_lmm.lmm_pattern & param->layout);
2937 if ((param->lmd->lmd_lmm.lmm_pattern == 0xFFFFFFFF) ||
2938 (found && param->exclude_layout) ||
2939 (!found && !param->exclude_layout)) {
2945 /* If an OBD UUID is specified but none matches, skip this file. */
2946 if ((param->obduuid && param->obdindex == OBD_NOT_FOUND) ||
2947 (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND))
2950 /* If a OST or MDT UUID is given, and some OST matches,
2952 if (param->obdindex != OBD_NOT_FOUND ||
2953 param->mdtindex != OBD_NOT_FOUND) {
2954 if (param->obduuid) {
2955 if (check_obd_match(param)) {
2956 /* If no mdtuuid is given, we are done.
2957 * Otherwise, fall through to the mdtuuid
2959 if (!param->mdtuuid)
2965 if (param->mdtuuid) {
2966 if (check_mdt_match(param))
2972 if (param->fp_check_uid) {
2973 if (st->st_uid == param->fp_uid) {
2974 if (param->fp_exclude_uid)
2977 if (!param->fp_exclude_uid)
2982 if (param->fp_check_gid) {
2983 if (st->st_gid == param->fp_gid) {
2984 if (param->fp_exclude_gid)
2987 if (!param->fp_exclude_gid)
2992 if (param->check_pool) {
2993 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2995 /* empty requested pool is taken as no pool search => V1 */
2996 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
2997 (param->poolname[0] == '\0')) ||
2998 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2999 (strncmp(lmmv3->lmm_pool_name,
3000 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
3001 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3002 (strcmp(param->poolname, "*") == 0))) {
3003 if (param->exclude_pool)
3006 if (!param->exclude_pool)
3011 /* Check the time on mds. */
3013 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
3016 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
3017 param->lmd->lmd_lmm.lmm_stripe_count)
3019 decision = find_time_check(st, param, for_mds);
3024 /* If file still fits the request, ask ost for updated info.
3025 The regular stat is almost of the same speed as some new
3026 'glimpse-size-ioctl'. */
3028 if (param->check_size && S_ISREG(st->st_mode) &&
3029 param->lmd->lmd_lmm.lmm_stripe_count)
3033 /* For regular files with the stripe the decision may have not
3034 * been taken yet if *time or size is to be checked. */
3035 LASSERT((S_ISREG(st->st_mode) &&
3036 param->lmd->lmd_lmm.lmm_stripe_count) ||
3037 param->mdtindex != OBD_NOT_FOUND);
3039 if (param->obdindex != OBD_NOT_FOUND)
3040 print_failed_tgt(param, path, LL_STATFS_LOV);
3042 if (param->mdtindex != OBD_NOT_FOUND)
3043 print_failed_tgt(param, path, LL_STATFS_LMV);
3046 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
3047 (void *)param->lmd);
3048 } else if (parent) {
3049 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
3050 (void *)param->lmd);
3054 if (errno == ENOENT) {
3055 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
3056 "warning: %s: %s does not exist",
3061 llapi_error(LLAPI_MSG_ERROR, ret,
3062 "%s: IOC_LOV_GETINFO on %s failed",
3068 /* Check the time on osc. */
3069 decision = find_time_check(st, param, 0);
3076 if (param->check_size)
3077 decision = find_value_cmp(st->st_size, param->size,
3078 param->size_sign, param->exclude_size,
3079 param->size_units, 0);
3081 if (decision != -1) {
3082 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
3084 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
3086 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3090 /* Do not get down anymore? */
3091 if (param->fp_depth == param->fp_max_depth)
3099 static int cb_mv_init(char *path, DIR *parent, DIR **dirp,
3100 void *param_data, struct dirent64 *de)
3102 struct find_param *param = (struct find_param *)param_data;
3104 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3106 struct obd_ioctl_data data = { 0 };
3111 LASSERT(parent != NULL || dirp != NULL);
3115 if (parent == NULL) {
3116 dir = opendir_parent(path);
3120 llapi_error(LLAPI_MSG_ERROR, ret,
3121 "can not open %s\n", path);
3128 filename = basename(path);
3129 data.ioc_inlbuf1 = (char *)filename;
3130 data.ioc_inllen1 = strlen(filename) + 1;
3131 data.ioc_inlbuf2 = (char *)¶m->mdtindex;
3132 data.ioc_inllen2 = sizeof(param->mdtindex);
3133 ret = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3135 llapi_error(LLAPI_MSG_ERROR, ret,
3136 "llapi_obd_statfs: error packing ioctl data");
3140 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
3143 fprintf(stderr, "%s migrate failed %d\n", path, ret);
3145 } else if (param->verbose & VERBOSE_DETAIL) {
3146 fprintf(stdout, "migrate %s to MDT%d\n", path, param->mdtindex);
3151 /* If the directory is being migration, we need
3152 * close the directory after migration,
3153 * so the old directory cache will be cleanup
3154 * on the client side, and re-open to get the
3155 * new directory handle */
3156 *dirp = opendir(path);
3159 llapi_error(LLAPI_MSG_ERROR, ret,
3160 "%s: Failed to open '%s'", __func__, path);
3171 int llapi_mv(char *path, struct find_param *param)
3173 return param_callback(path, cb_mv_init, cb_common_fini, param);
3176 int llapi_find(char *path, struct find_param *param)
3178 return param_callback(path, cb_find_init, cb_common_fini, param);
3182 * Get MDT number that the file/directory inode referenced
3183 * by the open fd resides on.
3184 * Return 0 and mdtidx on success, or -ve errno.
3186 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
3188 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
3193 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
3194 struct dirent64 *de)
3196 struct find_param *param = (struct find_param *)data;
3197 DIR *d = dirp == NULL ? NULL : *dirp;
3201 LASSERT(parent != NULL || d != NULL);
3204 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3205 } else if (parent) {
3208 fd = open(path, O_RDONLY);
3210 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3218 if (ret == -ENODATA) {
3219 if (!param->obduuid)
3220 llapi_printf(LLAPI_MSG_NORMAL,
3221 "%s has no stripe info\n", path);
3223 } else if (ret == -ENOENT) {
3224 llapi_error(LLAPI_MSG_WARN, ret,
3225 "warning: %s: %s does not exist",
3228 } else if (ret == -ENOTTY) {
3229 llapi_error(LLAPI_MSG_ERROR, ret,
3230 "%s: '%s' not on a Lustre fs?",
3233 llapi_error(LLAPI_MSG_ERROR, ret,
3234 "error: %s: LL_IOC_GET_MDTIDX failed for %s",
3240 /* The 'LASSERT(parent != NULL || d != NULL);' guarantees
3241 * that either 'd' or 'parent' is not null.
3242 * So in all cases llapi_file_fget_mdtidx() is called,
3243 * thus initializing 'mdtidx'. */
3244 if (param->quiet || !(param->verbose & VERBOSE_DETAIL))
3245 /* coverity[uninit_use_in_call] */
3246 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3248 /* coverity[uninit_use_in_call] */
3249 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3253 /* Do not get down anymore? */
3254 if (param->fp_depth == param->fp_max_depth)
3262 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
3263 struct dirent64 *de)
3265 struct find_param *param = (struct find_param *)data;
3266 DIR *d = dirp == NULL ? NULL : *dirp;
3269 LASSERT(parent != NULL || d != NULL);
3271 if (param->obduuid) {
3273 ret = setup_obd_uuid(d ? d : parent, path, param);
3279 if (param->get_lmv || param->get_default_lmv) {
3280 ret = cb_get_dirstripe(path, d, param);
3282 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
3283 (void *)¶m->lmd->lmd_lmm);
3286 } else if (parent) {
3287 char *fname = strrchr(path, '/');
3288 fname = (fname == NULL ? path : fname + 1);
3290 if (param->get_lmv) {
3291 llapi_printf(LLAPI_MSG_NORMAL,
3292 "%s get dirstripe information for file\n",
3297 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
3299 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
3300 (void *)¶m->lmd->lmd_lmm);
3304 if (errno == ENODATA && d != NULL) {
3305 /* We need to "fake" the "use the default" values
3306 * since the lmm struct is zeroed out at this point.
3307 * The magic needs to be set in order to satisfy
3308 * a check later on in the code path.
3309 * The object_seq needs to be set for the "(Default)"
3310 * prefix to be displayed. */
3311 if (param->get_default_lmv) {
3312 struct lmv_user_md *lum = param->fp_lmv_md;
3314 lum->lum_magic = LMV_USER_MAGIC;
3315 lum->lum_stripe_count = 0;
3316 lum->lum_stripe_offset = -1;
3318 } else if (param->get_lmv) {
3319 struct lmv_user_md *lum = param->fp_lmv_md;
3322 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3325 lum->lum_magic = LMV_MAGIC_V1;
3326 lum->lum_stripe_count = 0;
3327 lum->lum_stripe_offset = mdtidx;
3330 struct lov_user_md *lmm = ¶m->lmd->lmd_lmm;
3331 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3333 ostid_set_seq(&lmm->lmm_oi,
3334 FID_SEQ_LOV_DEFAULT);
3335 lmm->lmm_stripe_count = 0;
3336 lmm->lmm_stripe_size = 0;
3337 lmm->lmm_stripe_offset = -1;
3340 } else if (errno == ENODATA && parent != NULL) {
3341 if (!param->obduuid && !param->mdtuuid)
3342 llapi_printf(LLAPI_MSG_NORMAL,
3343 "%s has no stripe info\n", path);
3345 } else if (errno == ENOENT) {
3346 llapi_error(LLAPI_MSG_WARN, -ENOENT,
3347 "warning: %s: %s does not exist",
3350 } else if (errno == ENOTTY) {
3352 llapi_error(LLAPI_MSG_ERROR, ret,
3353 "%s: '%s' not on a Lustre fs?",
3358 llapi_error(LLAPI_MSG_ERROR, ret,
3359 "error: %s: %s failed for %s",
3360 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
3361 "IOC_MDC_GETFILESTRIPE", path);
3368 if (!(param->verbose & VERBOSE_MDTINDEX))
3369 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
3372 /* Do not get down anymore? */
3373 if (param->fp_depth == param->fp_max_depth)
3381 int llapi_getstripe(char *path, struct find_param *param)
3383 return param_callback(path, (param->verbose & VERBOSE_MDTINDEX) ?
3384 cb_get_mdt_index : cb_getstripe,
3385 cb_common_fini, param);
3388 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
3389 struct obd_statfs *stat_buf,
3390 struct obd_uuid *uuid_buf)
3393 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3395 struct obd_ioctl_data data = { 0 };
3398 data.ioc_inlbuf1 = (char *)&type;
3399 data.ioc_inllen1 = sizeof(__u32);
3400 data.ioc_inlbuf2 = (char *)&index;
3401 data.ioc_inllen2 = sizeof(__u32);
3402 data.ioc_pbuf1 = (char *)stat_buf;
3403 data.ioc_plen1 = sizeof(struct obd_statfs);
3404 data.ioc_pbuf2 = (char *)uuid_buf;
3405 data.ioc_plen2 = sizeof(struct obd_uuid);
3407 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3409 llapi_error(LLAPI_MSG_ERROR, rc,
3410 "llapi_obd_statfs: error packing ioctl data");
3414 fd = open(path, O_RDONLY);
3415 if (errno == EISDIR)
3416 fd = open(path, O_DIRECTORY | O_RDONLY);
3419 rc = errno ? -errno : -EBADF;
3420 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
3422 /* If we can't even open a file on the filesystem (e.g. with
3423 * -ESHUTDOWN), force caller to exit or it will loop forever. */
3426 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
3428 rc = errno ? -errno : -EINVAL;
3434 #define MAX_STRING_SIZE 128
3436 int llapi_ping(char *obd_type, char *obd_name)
3438 char path[MAX_STRING_SIZE];
3442 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
3443 obd_type, obd_name);
3445 fd = open(path, O_WRONLY);
3448 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
3452 /* The purpose is to send a byte as a ping, whatever this byte is. */
3453 /* coverity[uninit_use_in_call] */
3454 rc = write(fd, buf, 1);
3464 int llapi_target_iterate(int type_num, char **obd_type,
3465 void *args, llapi_cb_t cb)
3467 char buf[MAX_STRING_SIZE];
3468 FILE *fp = fopen(DEVICES_LIST, "r");
3473 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
3477 while (fgets(buf, sizeof(buf), fp) != NULL) {
3478 char *obd_type_name = NULL;
3479 char *obd_name = NULL;
3480 char *obd_uuid = NULL;
3482 struct obd_statfs osfs_buffer;
3484 while(bufp[0] == ' ')
3487 for(i = 0; i < 3; i++) {
3488 obd_type_name = strsep(&bufp, " ");
3490 obd_name = strsep(&bufp, " ");
3491 obd_uuid = strsep(&bufp, " ");
3493 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
3495 for (i = 0; i < type_num; i++) {
3496 if (strcmp(obd_type_name, obd_type[i]) != 0)
3499 cb(obd_type_name, obd_name, obd_uuid, args);
3506 static void do_target_check(char *obd_type_name, char *obd_name,
3507 char *obd_uuid, void *args)
3511 rc = llapi_ping(obd_type_name, obd_name);
3512 if (rc == ENOTCONN) {
3513 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
3515 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
3517 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
3521 int llapi_target_check(int type_num, char **obd_type, char *dir)
3523 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
3526 #undef MAX_STRING_SIZE
3528 /* Is this a lustre fs? */
3529 int llapi_is_lustre_mnttype(const char *type)
3531 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
3534 /* Is this a lustre client fs? */
3535 int llapi_is_lustre_mnt(struct mntent *mnt)
3537 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
3538 strstr(mnt->mnt_fsname, ":/") != NULL);
3541 int llapi_quotacheck(char *mnt, int check_type)
3546 root = opendir(mnt);
3549 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3553 rc = ioctl(dirfd(root), OBD_IOC_QUOTACHECK, check_type);
3561 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
3567 root = opendir(mnt);
3570 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3575 rc = ioctl(dirfd(root), OBD_IOC_POLL_QUOTACHECK, qchk);
3579 if (poll_intvl < 30)
3587 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3592 root = opendir(mnt);
3595 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3599 rc = ioctl(dirfd(root), OBD_IOC_QUOTACTL, qctl);
3607 static int cb_quotachown(char *path, DIR *parent, DIR **dirp, void *data,
3608 struct dirent64 *de)
3610 struct find_param *param = (struct find_param *)data;
3611 DIR *d = dirp == NULL ? NULL : *dirp;
3615 LASSERT(parent != NULL || d != NULL);
3617 rc = get_lmd_info(path, parent, d, param->lmd, param->lumlen);
3619 if (rc == -ENODATA) {
3620 if (!param->obduuid && !param->quiet)
3621 llapi_error(LLAPI_MSG_ERROR, -ENODATA,
3622 "%s has no stripe info", path);
3624 } else if (rc == -ENOENT) {
3630 st = ¶m->lmd->lmd_st;
3632 /* libc chown() will do extra check, and if the real owner is
3633 * the same as the ones to set, it won't fall into kernel, so
3634 * invoke syscall directly. */
3635 rc = syscall(SYS_chown, path, -1, -1);
3637 llapi_error(LLAPI_MSG_ERROR, errno,
3638 "error: chown %s", path);
3640 rc = chmod(path, st->st_mode);
3643 llapi_error(LLAPI_MSG_ERROR, rc, "error: chmod %s (%hu)",
3650 int llapi_quotachown(char *path, int flag)
3652 struct find_param param;
3654 memset(¶m, 0, sizeof(param));
3655 param.recursive = 1;
3659 return param_callback(path, cb_quotachown, NULL, ¶m);
3665 #include <sys/wait.h>
3669 static int rmtacl_notify(int ops)
3673 int found = 0, fd = 0, rc = 0;
3675 fp = setmntent(MOUNTED, "r");
3678 llapi_error(LLAPI_MSG_ERROR, rc,
3679 "error setmntent(%s)", MOUNTED);
3684 mnt = getmntent(fp);
3688 if (!llapi_is_lustre_mnt(mnt))
3691 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
3694 llapi_error(LLAPI_MSG_ERROR, rc,
3695 "Can't open '%s'\n", mnt->mnt_dir);
3699 rc = ioctl(fd, LL_IOC_RMTACL, ops);
3703 llapi_error(LLAPI_MSG_ERROR, rc,
3704 "ioctl RMTACL on '%s' err %d\n",
3714 return ((rc != 0) ? rc : found);
3717 static char *next_token(char *p, int div)
3723 while (*p && *p != ':' && !isspace(*p))
3726 while (*p == ':' || isspace(*p))
3729 return *p ? p : NULL;
3732 static int rmtacl_name2id(char *name, int is_user)
3737 pw = getpwnam(name);
3741 return (int)(pw->pw_uid);
3745 gr = getgrnam(name);
3749 return (int)(gr->gr_gid);
3753 static int isodigit(int c)
3755 return (c >= '0' && c <= '7') ? 1 : 0;
3759 * Whether the name is just digits string (uid/gid) already or not.
3764 static int str_is_id(char *str)
3771 if (*str == 'x' || *str == 'X') { /* for Hex. */
3772 if (!isxdigit(*(++str)))
3775 while (isxdigit(*(++str)));
3776 } else if (isodigit(*str)) { /* for Oct. */
3777 while (isodigit(*(++str)));
3779 } else if (isdigit(*str)) { /* for Dec. */
3780 while (isdigit(*(++str)));
3783 return (*str == 0) ? 1 : 0;
3793 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
3795 static rmtacl_name_t rmtacl_namelist[] = {
3796 { RMTACL_OPTNAME("user:"), 1, 0 },
3797 { RMTACL_OPTNAME("group:"), 0, 0 },
3798 { RMTACL_OPTNAME("default:user:"), 1, 0 },
3799 { RMTACL_OPTNAME("default:group:"), 0, 0 },
3800 /* for --tabular option */
3801 { RMTACL_OPTNAME("user"), 1, 1 },
3802 { RMTACL_OPTNAME("group"), 0, 1 },
3806 static int rgetfacl_output(char *str)
3808 char *start = NULL, *end = NULL;
3809 int is_user = 0, n, id;
3816 for (rn = rmtacl_namelist; rn->name; rn++) {
3817 if(strncmp(str, rn->name, rn->length) == 0) {
3818 if (!rn->next_token)
3819 start = str + rn->length;
3821 start = next_token(str + rn->length, 0);
3822 is_user = rn->is_user;
3827 end = next_token(start, 1);
3828 if (end == NULL || start == end) {
3829 n = printf("%s", str);
3835 id = rmtacl_name2id(start, is_user);
3836 if (id == INVALID_ID) {
3837 if (str_is_id(start)) {
3839 n = printf("%s", str);
3842 } else if ((id == NOBODY_UID && is_user) ||
3843 (id == NOBODY_GID && !is_user)) {
3845 n = printf("%s", str);
3849 n = printf("%s%d%s", str, id, end);
3854 static int child_status(int status)
3856 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
3859 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
3862 int fd[2], status, rc;
3869 llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe\n");
3876 llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork\n");
3881 /* child process redirects its output. */
3884 if (dup2(fd[1], 1) < 0) {
3886 llapi_error(LLAPI_MSG_ERROR, rc,
3887 "Can't dup2 %d\n", fd[1]);
3897 status = rmtacl_notify(ops);
3901 exit(execvp(argv[0], argv));
3904 /* the following is parent process */
3905 fp = fdopen(fd[0], "r");
3908 llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed\n", fd[0]);
3914 while (fgets(buf, PIPE_BUF, fp) != NULL) {
3915 if (output_func(buf) < 0)
3916 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
3922 if (waitpid(pid, &status, 0) < 0) {
3924 llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed\n", pid);
3928 return child_status(status);
3931 int llapi_lsetfacl(int argc, char *argv[])
3933 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
3936 int llapi_lgetfacl(int argc, char *argv[])
3938 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
3941 int llapi_rsetfacl(int argc, char *argv[])
3943 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
3946 int llapi_rgetfacl(int argc, char *argv[])
3948 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
3951 int llapi_cp(int argc, char *argv[])
3955 rc = rmtacl_notify(RMT_RSETFACL);
3959 exit(execvp(argv[0], argv));
3962 int llapi_ls(int argc, char *argv[])
3966 rc = rmtacl_notify(RMT_LGETFACL);
3970 exit(execvp(argv[0], argv));
3973 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
3974 * format must have %s%s, buf must be > 16
3975 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
3976 * then buf = "lustre-MDT0000"
3978 static int get_mdtname(char *name, char *format, char *buf)
3980 char suffix[]="-MDT0000";
3981 int len = strlen(name);
3983 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
3984 name[len - 5] = '\0';
3989 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
3992 /* Not enough room to add suffix */
3993 llapi_err_noerrno(LLAPI_MSG_ERROR,
3994 "MDT name too long |%s|", name);
3999 return sprintf(buf, format, name, suffix);
4002 /** ioctl on filsystem root, with mdtindex sent as data
4003 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
4004 * \param mdtidxp pointer to integer within data to be filled in with the
4005 * mdt index (0 if no mdt is specified). NULL won't be filled.
4007 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
4015 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
4016 Open root and parse mdt index. */
4017 if (mdtname[0] == '/') {
4019 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
4020 (char *)mdtname, -1);
4022 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
4024 ptr = fsname + strlen(fsname) - 8;
4026 index = strtol(ptr + 4, NULL, 10);
4027 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
4031 llapi_err_noerrno(LLAPI_MSG_ERROR,
4032 "Can't open %s: %d\n", mdtname, rc);
4039 rc = ioctl(fd, opc, data);
4044 if (rc && want_error)
4045 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
4051 /****** Changelog API ********/
4053 static int changelog_ioctl(const char *mdtname, int opc, int id,
4054 long long recno, int flags)
4056 struct ioc_changelog data;
4060 data.icc_recno = recno;
4061 data.icc_flags = flags;
4062 idx = (int *)(&data.icc_mdtindex);
4064 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
4067 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
4068 struct changelog_private {
4071 lustre_kernelcomm kuc;
4074 /** Start reading from a changelog
4075 * @param priv Opaque private control structure
4076 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
4077 * @param device Report changes recorded on this MDT
4078 * @param startrec Report changes beginning with this record number
4079 * (just call llapi_changelog_fini when done; don't need an endrec)
4081 int llapi_changelog_start(void **priv, int flags, const char *device,
4084 struct changelog_private *cp;
4087 /* Set up the receiver control struct */
4088 cp = calloc(1, sizeof(*cp));
4092 cp->magic = CHANGELOG_PRIV_MAGIC;
4095 /* Set up the receiver */
4096 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */);
4102 /* Tell the kernel to start sending */
4103 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
4105 /* Only the kernel reference keeps the write side open */
4106 close(cp->kuc.lk_wfd);
4107 cp->kuc.lk_wfd = LK_NOFD;
4109 /* frees and clears priv */
4110 llapi_changelog_fini(priv);
4121 /** Finish reading from a changelog */
4122 int llapi_changelog_fini(void **priv)
4124 struct changelog_private *cp = (struct changelog_private *)*priv;
4126 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4129 libcfs_ukuc_stop(&cp->kuc);
4135 /** Convert a changelog_rec to changelog_ext_rec, in this way client can treat
4136 * all records in the format of changelog_ext_rec, this can make record
4139 static inline int changelog_extend_rec(struct changelog_ext_rec *ext)
4141 if (!CHANGELOG_REC_EXTENDED(ext)) {
4142 struct changelog_rec *rec = (struct changelog_rec *)ext;
4144 memmove(ext->cr_name, rec->cr_name, rec->cr_namelen);
4145 fid_zero(&ext->cr_sfid);
4146 fid_zero(&ext->cr_spfid);
4153 /** Read the next changelog entry
4154 * @param priv Opaque private control structure
4155 * @param rech Changelog record handle; record will be allocated here
4156 * @return 0 valid message received; rec is set
4160 int llapi_changelog_recv(void *priv, struct changelog_ext_rec **rech)
4162 struct changelog_private *cp = (struct changelog_private *)priv;
4163 struct kuc_hdr *kuch;
4166 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4170 kuch = malloc(KUC_CHANGELOG_MSG_MAXSIZE);
4175 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
4176 KUC_CHANGELOG_MSG_MAXSIZE,
4177 KUC_TRANSPORT_CHANGELOG);
4181 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
4182 ((kuch->kuc_msgtype != CL_RECORD) &&
4183 (kuch->kuc_msgtype != CL_EOF))) {
4184 llapi_err_noerrno(LLAPI_MSG_ERROR,
4185 "Unknown changelog message type %d:%d\n",
4186 kuch->kuc_transport, kuch->kuc_msgtype);
4191 if (kuch->kuc_msgtype == CL_EOF) {
4192 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
4201 /* Our message is a changelog_ext_rec. Use pointer math to skip
4202 * kuch_hdr and point directly to the message payload.
4204 *rech = (struct changelog_ext_rec *)(kuch + 1);
4205 changelog_extend_rec(*rech);
4215 /** Release the changelog record when done with it. */
4216 int llapi_changelog_free(struct changelog_ext_rec **rech)
4219 /* We allocated memory starting at the kuc_hdr, but passed
4220 * the consumer a pointer to the payload.
4221 * Use pointer math to get back to the header.
4223 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
4230 int llapi_changelog_clear(const char *mdtname, const char *idstr,
4236 llapi_err_noerrno(LLAPI_MSG_ERROR,
4237 "can't purge negative records\n");
4241 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
4242 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
4243 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
4244 llapi_err_noerrno(LLAPI_MSG_ERROR,
4245 "expecting id of the form '"
4246 CHANGELOG_USER_PREFIX
4247 "<num>'; got '%s'\n", idstr);
4251 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
4254 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
4255 int buflen, long long *recno, int *linkno)
4258 struct getinfo_fid2path *gf;
4261 while (*fidstr == '[')
4264 sscanf(fidstr, SFID, RFID(&fid));
4265 if (!fid_is_sane(&fid)) {
4266 llapi_err_noerrno(LLAPI_MSG_ERROR,
4267 "bad FID format [%s], should be "DFID"\n",
4268 fidstr, (__u64)1, 2, 0);
4272 gf = malloc(sizeof(*gf) + buflen);
4276 gf->gf_recno = *recno;
4277 gf->gf_linkno = *linkno;
4278 gf->gf_pathlen = buflen;
4280 /* Take path or fsname */
4281 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
4284 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
4286 memcpy(buf, gf->gf_path, gf->gf_pathlen);
4287 if (buf[0] == '\0') { /* ROOT path */
4291 *recno = gf->gf_recno;
4292 *linkno = gf->gf_linkno;
4299 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
4302 struct lustre_mdt_attrs *lma;
4306 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
4308 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
4311 lma = (struct lustre_mdt_attrs *)buf;
4312 fid_le_to_cpu(fid, &lma->lma_self_fid);
4316 int llapi_fd2fid(const int fd, lustre_fid *fid)
4320 memset(fid, 0, sizeof(*fid));
4322 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
4323 if (rc == -EINVAL || rc == -ENOTTY)
4324 rc = fid_from_lma(NULL, fd, fid);
4329 int llapi_path2fid(const char *path, lustre_fid *fid)
4333 memset(fid, 0, sizeof(*fid));
4334 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4336 if (errno == ELOOP || errno == ENXIO)
4337 return fid_from_lma(path, -1, fid);
4341 rc = llapi_fd2fid(fd, fid);
4342 if (rc == -EINVAL || rc == -ENOTTY)
4343 rc = fid_from_lma(path, -1, fid);
4349 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4354 root = opendir(mnt);
4357 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4361 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
4364 llapi_error(LLAPI_MSG_ERROR, rc,
4365 "ioctl on %s for getting connect flags failed", mnt);
4371 int llapi_get_version(char *buffer, int buffer_size,
4376 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
4378 fd = open(OBD_DEV_PATH, O_RDONLY);
4382 memset(buffer, 0, buffer_size);
4383 data->ioc_version = OBD_IOCTL_VERSION;
4384 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
4385 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
4386 data->ioc_len = obd_ioctl_packlen(data);
4388 rc = ioctl(fd, OBD_GET_VERSION, buffer);
4395 *version = data->ioc_bulk;
4400 * Get a 64-bit value representing the version of file data pointed by fd.
4402 * Each write or truncate, flushed on OST, will change this value. You can use
4403 * this value to verify if file data was modified. This only checks the file
4404 * data, not metadata.
4406 * \param flags 0: no flush pages, usually used it the process has already
4408 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
4410 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
4411 * pages from clients.
4413 * \retval 0 on success.
4414 * \retval -errno on error.
4416 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4419 struct ioc_data_version idv;
4421 idv.idv_flags = flags;
4423 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4427 *data_version = idv.idv_version;
4433 * Create a volatile file and open it for write:
4434 * - file is created as a standard file in the directory
4435 * - file does not appears in directory and directory mtime does not change
4436 * - file is removed at close
4437 * - file modes are rw-------, if user wants another one it must use fchmod()
4438 * \param directory Directory where the file is created
4439 * \param idx MDT index on which the file is created
4440 * \param open_flags Standard open flags
4442 * \retval 0 on success.
4443 * \retval -errno on error.
4445 int llapi_create_volatile_idx(char *directory, int idx, int open_flags)
4447 char file_path[PATH_MAX];
4448 char filename[PATH_MAX];
4453 fd = open("/dev/urandom", O_RDONLY);
4455 llapi_error(LLAPI_MSG_ERROR, errno,
4456 "Cannot open /dev/urandom\n");
4459 rc = read(fd, &random, sizeof(random));
4461 if (rc < sizeof(random)) {
4462 llapi_error(LLAPI_MSG_ERROR, errno,
4463 "cannot read %zu bytes from /dev/urandom",
4468 snprintf(filename, sizeof(filename),
4469 LUSTRE_VOLATILE_HDR"::%.4X", random);
4471 snprintf(filename, sizeof(filename),
4472 LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, random);
4474 rc = snprintf(file_path, sizeof(file_path),
4475 "%s/%s", directory, filename);
4476 if (rc >= sizeof(file_path))
4479 fd = open(file_path, O_RDWR | O_CREAT | open_flags, S_IRUSR | S_IWUSR);
4481 llapi_error(LLAPI_MSG_ERROR, errno,
4482 "Cannot create volatile file %s in %s\n",
4483 filename + LUSTRE_VOLATILE_HDR_LEN,
4491 * Swap the layouts between 2 file descriptors
4492 * the 2 files must be open in write
4493 * first fd received the ioctl, second fd is passed as arg
4494 * this is assymetric but avoid use of root path for ioctl
4496 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4498 struct lustre_swap_layouts lsl;
4501 srandom(time(NULL));
4503 lsl.sl_flags = flags;
4504 lsl.sl_gid = random();
4507 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4514 * Swap the layouts between 2 files
4515 * the 2 files are open in write
4517 int llapi_swap_layouts(const char *path1, const char *path2,
4518 __u64 dv1, __u64 dv2, __u64 flags)
4522 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4525 llapi_error(LLAPI_MSG_ERROR, rc,
4526 "error: cannot open '%s' for write", path1);
4530 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4533 llapi_error(LLAPI_MSG_ERROR, rc,
4534 "error: cannot open '%s' for write", path2);
4538 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4540 llapi_error(LLAPI_MSG_ERROR, rc,
4541 "error: cannot swap layout between '%s' and '%s'\n",