4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/liblustreapi.c
38 * Author: Peter J. Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
40 * Author: Robert Read <rread@clusterfs.com>
52 #include <sys/ioctl.h>
59 #include <sys/statfs.h>
60 #include <sys/types.h>
61 #include <sys/syscall.h>
62 #include <sys/xattr.h>
65 #include <libgen.h> /* for dirname() */
66 #ifdef HAVE_LINUX_UNISTD_H
67 #include <linux/unistd.h>
73 #include <libcfs/libcfs.h>
74 #include <lnet/lnetctl.h>
75 #include <lustre/lustreapi.h>
76 #include <lustre_ioctl.h>
77 #include "lustreapi_internal.h"
79 static int llapi_msg_level = LLAPI_MSG_MAX;
81 void llapi_msg_set_level(int level)
83 /* ensure level is in the good range */
84 if (level < LLAPI_MSG_OFF)
85 llapi_msg_level = LLAPI_MSG_OFF;
86 else if (level > LLAPI_MSG_MAX)
87 llapi_msg_level = LLAPI_MSG_MAX;
89 llapi_msg_level = level;
92 int llapi_msg_get_level(void)
94 return llapi_msg_level;
97 static void error_callback_default(enum llapi_message_level level, int err,
98 const char *fmt, va_list ap)
100 vfprintf(stderr, fmt, ap);
101 if (level & LLAPI_MSG_NO_ERRNO)
102 fprintf(stderr, "\n");
104 fprintf(stderr, ": %s (%d)\n", strerror(err), err);
107 static void info_callback_default(enum llapi_message_level level, int err,
108 const char *fmt, va_list ap)
110 vfprintf(stdout, fmt, ap);
113 static llapi_log_callback_t llapi_error_callback = error_callback_default;
114 static llapi_log_callback_t llapi_info_callback = info_callback_default;
117 /* llapi_error will preserve errno */
118 void llapi_error(enum llapi_message_level level, int err, const char *fmt, ...)
121 int tmp_errno = errno;
123 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
127 llapi_error_callback(level, abs(err), fmt, args);
132 /* llapi_printf will preserve errno */
133 void llapi_printf(enum llapi_message_level level, const char *fmt, ...)
136 int tmp_errno = errno;
138 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
142 llapi_info_callback(level, 0, fmt, args);
148 * Set a custom error logging function. Passing in NULL will reset the logging
149 * callback to its default value.
151 * This function returns the value of the old callback.
153 llapi_log_callback_t llapi_error_callback_set(llapi_log_callback_t cb)
155 llapi_log_callback_t old = llapi_error_callback;
158 llapi_error_callback = cb;
160 llapi_error_callback = error_callback_default;
166 * Set a custom info logging function. Passing in NULL will reset the logging
167 * callback to its default value.
169 * This function returns the value of the old callback.
171 llapi_log_callback_t llapi_info_callback_set(llapi_log_callback_t cb)
173 llapi_log_callback_t old = llapi_info_callback;
176 llapi_info_callback = cb;
178 llapi_info_callback = info_callback_default;
184 * size_units is to be initialized (or zeroed) by caller.
186 int llapi_parse_size(const char *optarg, unsigned long long *size,
187 unsigned long long *size_units, int bytes_spec)
191 if (strncmp(optarg, "-", 1) == 0)
194 if (*size_units == 0)
197 *size = strtoull(optarg, &end, 0);
200 if ((*end == 'b') && *(end + 1) == '\0' &&
201 (*size & (~0ULL << (64 - 9))) == 0 &&
203 *size_units = 1 << 9;
204 } else if ((*end == 'b') &&
205 *(end + 1) == '\0' &&
208 } else if ((*end == 'k' || *end == 'K') &&
209 *(end + 1) == '\0' &&
210 (*size & (~0ULL << (64 - 10))) == 0) {
211 *size_units = 1 << 10;
212 } else if ((*end == 'm' || *end == 'M') &&
213 *(end + 1) == '\0' &&
214 (*size & (~0ULL << (64 - 20))) == 0) {
215 *size_units = 1 << 20;
216 } else if ((*end == 'g' || *end == 'G') &&
217 *(end + 1) == '\0' &&
218 (*size & (~0ULL << (64 - 30))) == 0) {
219 *size_units = 1 << 30;
220 } else if ((*end == 't' || *end == 'T') &&
221 *(end + 1) == '\0' &&
222 (*size & (~0ULL << (64 - 40))) == 0) {
223 *size_units = 1ULL << 40;
224 } else if ((*end == 'p' || *end == 'P') &&
225 *(end + 1) == '\0' &&
226 (*size & (~0ULL << (64 - 50))) == 0) {
227 *size_units = 1ULL << 50;
228 } else if ((*end == 'e' || *end == 'E') &&
229 *(end + 1) == '\0' &&
230 (*size & (~0ULL << (64 - 60))) == 0) {
231 *size_units = 1ULL << 60;
236 *size *= *size_units;
240 /* XXX: llapi_xxx() functions return negative values upon failure */
242 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
243 int stripe_count, int stripe_pattern)
247 /* 64 KB is the largest common page size I'm aware of (on ia64), but
248 * check the local page size just in case. */
249 page_size = LOV_MIN_STRIPE_SIZE;
250 if (getpagesize() > page_size) {
251 page_size = getpagesize();
252 llapi_err_noerrno(LLAPI_MSG_WARN,
253 "warning: your page size (%u) is "
254 "larger than expected (%u)", page_size,
255 LOV_MIN_STRIPE_SIZE);
257 if (!llapi_stripe_size_is_aligned(stripe_size)) {
259 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
260 "must be an even multiple of %d bytes",
261 stripe_size, page_size);
264 if (!llapi_stripe_offset_is_valid(stripe_offset)) {
266 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
270 if (!llapi_stripe_count_is_valid(stripe_count)) {
272 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
276 if (llapi_stripe_size_is_too_big(stripe_size)) {
278 llapi_error(LLAPI_MSG_ERROR, rc,
279 "warning: stripe size 4G or larger "
280 "is not currently supported and would wrap");
286 /* return the first file matching this pattern */
287 static int first_match(char *pattern, char *buffer)
291 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
294 if (glob_info.gl_pathc < 1) {
295 globfree(&glob_info);
299 strcpy(buffer, glob_info.gl_pathv[0]);
301 globfree(&glob_info);
305 static int find_target_obdpath(char *fsname, char *path)
308 char pattern[PATH_MAX + 1];
311 snprintf(pattern, PATH_MAX,
312 "/proc/fs/lustre/lov/%s-*/target_obd",
314 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
315 if (rc == GLOB_NOMATCH)
320 strcpy(path, glob_info.gl_pathv[0]);
321 globfree(&glob_info);
325 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
328 char pattern[PATH_MAX + 1];
331 snprintf(pattern, PATH_MAX,
332 "/proc/fs/lustre/lov/%s-*/pools/%s",
334 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
335 /* If no pools, make sure the lov is available */
336 if ((rc == GLOB_NOMATCH) &&
337 (find_target_obdpath(fsname, poolpath) == -ENODEV))
342 strcpy(poolpath, glob_info.gl_pathv[0]);
343 globfree(&glob_info);
348 * Trim a trailing newline from a string, if it exists.
350 int llapi_chomp_string(char *buf)
366 * return a parameter string for a specific device type or mountpoint
368 * \param param_path the path to the file containing parameter data
369 * \param result buffer for parameter value string
370 * \param result_size size of buffer for return value
372 * The \param param_path is appended to /proc/{fs,sys}/{lnet,lustre} to
373 * complete the absolute path to the file containing the parameter data
374 * the user is requesting. If that file exist then the data is read from
375 * the file and placed into the \param result buffer that is passed by
376 * the user. Data is only copied up to the \param result_size to prevent
377 * overflow of the array.
379 * Return 0 for success, with a NUL-terminated string in \param result.
380 * Return -ve value for error.
382 int get_param(const char *param_path, char *result,
383 unsigned int result_size)
385 char file[PATH_MAX + 1], pattern[PATH_MAX + 1], buf[result_size];
389 snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
391 rc = first_match(pattern, file);
392 if (rc != 0 || result == NULL)
395 fp = fopen(file, "r");
397 while (fgets(buf, result_size, fp) != NULL)
406 #define DEVICES_LIST "/proc/fs/lustre/devices"
409 * return a parameter string for a specific device type or mountpoint
411 * \param fsname Lustre filesystem name (optional)
412 * \param file_path path to file in filesystem (optional, if fsname unset)
413 * \param obd_type Lustre OBD device type
414 * \param param_name parameter name to fetch
415 * \param value return buffer for parameter value string
416 * \param val_len size of buffer for return value
418 * If fsname is specified then the parameter will be from that filesystem
419 * (if it exists). If file_path is given and it is in a mounted Lustre
420 * filesystem, then the parameter will be otherwise the value may be
421 * from any mounted filesystem (if there is more than one).
423 * If "obd_type" matches a Lustre device then the first matching device
424 * (as with "lctl dl", constrained by \param fsname or \param mount_path)
425 * will be used to provide the return value, otherwise the first such
426 * device found will be used.
428 * Return 0 for success, with a NUL-terminated string in \param buffer.
429 * Return -ve value for error.
431 static int get_param_obdvar(const char *fsname, const char *file_path,
432 const char *obd_type, const char *param_name,
433 char *value, unsigned int val_len)
435 char devices[PATH_MAX];
436 char dev[PATH_MAX] = "*";
441 fp = fopen(DEVICES_LIST, "r");
444 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
448 if (fsname == NULL && file_path != NULL) {
449 rc = llapi_search_fsname(file_path, fs);
451 llapi_error(LLAPI_MSG_ERROR, rc,
452 "'%s' is not on a Lustre filesystem",
456 } else if (fsname != NULL) {
457 rc = strlcpy(fs, fsname, sizeof(fs));
458 if (rc >= sizeof(fs)) {
464 while (fgets(devices, sizeof(devices) - 1, fp) != NULL) {
465 char *bufp = devices, *tmp;
467 devices[sizeof(devices) - 1] = '\0';
468 while (bufp[0] == ' ')
471 tmp = strstr(bufp, obd_type);
473 tmp += strlen(obd_type) + 1;
476 rc = strlcpy(dev, tmp, sizeof(dev));
477 if (rc >= sizeof(dev)) {
482 tmp = strchr(dev, ' ');
489 if (dev[0] == '*' && strlen(fs)) {
490 rc = snprintf(dev, sizeof(dev), "%s-*", fs);
491 if (rc >= sizeof(dev)) {
496 rc = snprintf(devices, sizeof(devices), "%s/%s/%s", obd_type, dev,
498 if (rc >= sizeof(devices)) {
504 return get_param(devices, value, val_len);
512 * TYPE one of llite, lmv, lov.
513 * /proc/fs/lustre/TYPE/INST the directory of interest.
515 static int get_param_cli(const char *type, const char *inst,
516 const char *param, char *buf, size_t buf_size)
518 char param_path[PATH_MAX + 1];
519 FILE *param_file = NULL;
522 snprintf(param_path, sizeof(param_path),
523 "/proc/fs/lustre/%s/%s/%s", type, inst, param);
525 param_file = fopen(param_path, "r");
526 if (param_file == NULL) {
531 if (fgets(buf, buf_size, param_file) == NULL) {
538 if (param_file != NULL)
544 static int get_param_llite(const char *path,
545 const char *param, char *buf, size_t buf_size)
550 rc = llapi_getname(path, inst, sizeof(inst));
554 return get_param_cli("llite", inst, param, buf, buf_size);
557 static int get_param_lov(const char *path,
558 const char *param, char *buf, size_t buf_size)
560 struct obd_uuid uuid;
563 rc = llapi_file_get_lov_uuid(path, &uuid);
567 return get_param_cli("lov", uuid.uuid, param, buf, buf_size);
570 static int get_param_lmv(const char *path,
571 const char *param, char *buf, size_t buf_size)
573 struct obd_uuid uuid;
576 rc = llapi_file_get_lmv_uuid(path, &uuid);
580 return get_param_cli("lmv", uuid.uuid, param, buf, buf_size);
583 static int get_mds_md_size(const char *path)
585 int md_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
589 /* Get the max ea size from llite proc. */
590 rc = get_param_llite(path, "max_easize", buf, sizeof(buf));
602 int llapi_get_agent_uuid(char *path, char *buf, size_t bufsize)
604 return get_param_lmv(path, "uuid", buf, bufsize);
608 * if pool is NULL, search ostname in target_obd
609 * if pool is not NULL:
610 * if pool not found returns errno < 0
611 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
612 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
614 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
617 char buffer[PATH_MAX + 1];
621 len = strlen(ostname);
623 if (poolname == NULL) {
627 rc = find_target_obdpath(fsname, buffer);
629 rc = find_poolpath(fsname, poolname, buffer);
634 fd = fopen(buffer, "r");
638 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
639 if (poolname == NULL) {
641 /* Search for an ostname in the list of OSTs
642 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
643 ptr = strchr(buffer, ' ');
645 (strncmp(ptr + 1, ostname, len) == 0)) {
650 /* Search for an ostname in a pool,
651 (or an existing non-empty pool if no ostname) */
652 if ((ostname == NULL) ||
653 (strncmp(buffer, ostname, len) == 0)) {
663 int llapi_file_open_pool(const char *name, int flags, int mode,
664 unsigned long long stripe_size, int stripe_offset,
665 int stripe_count, int stripe_pattern, char *pool_name)
667 struct lov_user_md_v3 lum = { 0 };
670 /* Make sure we have a good pool */
671 if (pool_name != NULL) {
672 char fsname[MAX_OBD_NAME + 1], *ptr;
674 rc = llapi_search_fsname(name, fsname);
676 llapi_error(LLAPI_MSG_ERROR, rc,
677 "'%s' is not on a Lustre filesystem",
682 /* in case user gives the full pool name <fsname>.<poolname>,
683 * strip the fsname */
684 ptr = strchr(pool_name, '.');
687 if (strcmp(pool_name, fsname) != 0) {
689 llapi_err_noerrno(LLAPI_MSG_ERROR,
690 "Pool '%s' is not on filesystem '%s'",
697 /* Make sure the pool exists and is non-empty */
698 rc = llapi_search_ost(fsname, pool_name, NULL);
700 llapi_err_noerrno(LLAPI_MSG_ERROR,
701 "pool '%s.%s' %s", fsname, pool_name,
702 rc == 0 ? "has no OSTs" : "does not exist");
708 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
710 if (errno == EISDIR && !(flags & O_DIRECTORY)) {
711 flags = O_DIRECTORY | O_RDONLY;
718 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
722 rc = llapi_stripe_limit_check(stripe_size, stripe_offset, stripe_count,
727 /* Initialize IOCTL striping pattern structure */
728 lum.lmm_magic = LOV_USER_MAGIC_V3;
729 lum.lmm_pattern = stripe_pattern;
730 lum.lmm_stripe_size = stripe_size;
731 lum.lmm_stripe_count = stripe_count;
732 lum.lmm_stripe_offset = stripe_offset;
733 if (pool_name != NULL) {
734 strlcpy(lum.lmm_pool_name, pool_name,
735 sizeof(lum.lmm_pool_name));
737 /* If no pool is specified at all, use V1 request */
738 lum.lmm_magic = LOV_USER_MAGIC_V1;
741 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
742 char *errmsg = "stripe already set";
744 if (errno != EEXIST && errno != EALREADY)
745 errmsg = strerror(errno);
747 llapi_err_noerrno(LLAPI_MSG_ERROR,
748 "error on ioctl "LPX64" for '%s' (%d): %s",
749 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
760 int llapi_file_open(const char *name, int flags, int mode,
761 unsigned long long stripe_size, int stripe_offset,
762 int stripe_count, int stripe_pattern)
764 return llapi_file_open_pool(name, flags, mode, stripe_size,
765 stripe_offset, stripe_count,
766 stripe_pattern, NULL);
769 int llapi_file_create(const char *name, unsigned long long stripe_size,
770 int stripe_offset, int stripe_count, int stripe_pattern)
774 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
775 stripe_offset, stripe_count, stripe_pattern,
784 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
785 int stripe_offset, int stripe_count,
786 int stripe_pattern, char *pool_name)
790 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
791 stripe_offset, stripe_count, stripe_pattern,
800 int llapi_dir_set_default_lmv_stripe(const char *name, int stripe_offset,
801 int stripe_count, int stripe_pattern,
802 const char *pool_name)
804 struct lmv_user_md lum = { 0 };
808 lum.lum_magic = LMV_USER_MAGIC;
809 lum.lum_stripe_offset = stripe_offset;
810 lum.lum_stripe_count = stripe_count;
811 lum.lum_hash_type = stripe_pattern;
812 if (pool_name != NULL) {
813 if (strlen(pool_name) >= sizeof(lum.lum_pool_name)) {
814 llapi_err_noerrno(LLAPI_MSG_ERROR,
815 "error LL_IOC_LMV_SET_DEFAULT_STRIPE '%s'"
816 ": too large pool name: %s", name, pool_name);
819 strncpy(lum.lum_pool_name, pool_name,
820 sizeof(lum.lum_pool_name));
823 fd = open(name, O_DIRECTORY | O_RDONLY);
826 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
830 rc = ioctl(fd, LL_IOC_LMV_SET_DEFAULT_STRIPE, &lum);
832 char *errmsg = "stripe already set";
834 if (errno != EEXIST && errno != EALREADY)
835 errmsg = strerror(errno);
837 llapi_err_noerrno(LLAPI_MSG_ERROR,
838 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
845 int llapi_dir_create_pool(const char *name, int mode, int stripe_offset,
846 int stripe_count, int stripe_pattern,
847 const char *pool_name)
849 struct lmv_user_md lmu = { 0 };
850 struct obd_ioctl_data data = { 0 };
853 char *dirpath = NULL;
854 char *namepath = NULL;
860 dirpath = strdup(name);
861 namepath = strdup(name);
862 if (!dirpath || !namepath)
865 lmu.lum_magic = LMV_USER_MAGIC;
866 lmu.lum_stripe_offset = stripe_offset;
867 lmu.lum_stripe_count = stripe_count;
868 lmu.lum_hash_type = stripe_pattern;
869 if (pool_name != NULL) {
870 if (strlen(pool_name) >= LOV_MAXPOOLNAME) {
871 llapi_err_noerrno(LLAPI_MSG_ERROR,
872 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
873 "pool name: %s", name, pool_name);
877 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
880 filename = basename(namepath);
881 dir = dirname(dirpath);
883 data.ioc_inlbuf1 = (char *)filename;
884 data.ioc_inllen1 = strlen(filename) + 1;
885 data.ioc_inlbuf2 = (char *)&lmu;
886 data.ioc_inllen2 = sizeof(struct lmv_user_md);
887 data.ioc_type = mode;
888 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
890 llapi_error(LLAPI_MSG_ERROR, rc,
891 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
896 fd = open(dir, O_DIRECTORY | O_RDONLY);
899 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
903 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
904 char *errmsg = "stripe already set";
906 if (errno != EEXIST && errno != EALREADY)
907 errmsg = strerror(errno);
909 llapi_err_noerrno(LLAPI_MSG_ERROR,
910 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
920 int llapi_direntry_remove(char *dname)
922 char *dirpath = NULL;
923 char *namepath = NULL;
929 dirpath = strdup(dname);
930 namepath = strdup(dname);
931 if (!dirpath || !namepath)
934 filename = basename(namepath);
936 dir = dirname(dirpath);
938 fd = open(dir, O_DIRECTORY | O_RDONLY);
941 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
946 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
947 char *errmsg = strerror(errno);
948 llapi_err_noerrno(LLAPI_MSG_ERROR,
949 "error on ioctl "LPX64" for '%s' (%d): %s",
950 (__u64)LL_IOC_LMV_SETSTRIPE, filename,
962 * Find the fsname, the full path, and/or an open fd.
963 * Either the fsname or path must not be NULL
965 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
968 char buf[PATH_MAX], mntdir[PATH_MAX];
971 int idx = 0, len = 0, mntlen, fd;
974 /* get the mount point */
975 fp = setmntent(MOUNTED, "r");
978 llapi_error(LLAPI_MSG_ERROR, rc,
979 "setmntent(%s) failed", MOUNTED);
983 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
986 if (!llapi_is_lustre_mnt(&mnt))
989 if ((want & WANT_INDEX) && (idx++ != index))
992 mntlen = strlen(mnt.mnt_dir);
993 ptr = strrchr(mnt.mnt_fsname, '/');
994 /* thanks to the call to llapi_is_lustre_mnt() above,
995 * we are sure that mnt.mnt_fsname contains ":/",
996 * so ptr should never be NULL */
1001 /* Check the fsname for a match, if given */
1002 if (!(want & WANT_FSNAME) && fsname != NULL &&
1003 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
1006 /* If the path isn't set return the first one we find */
1007 if (path == NULL || strlen(path) == 0) {
1008 strcpy(mntdir, mnt.mnt_dir);
1009 if ((want & WANT_FSNAME) && fsname != NULL)
1010 strcpy(fsname, ptr);
1013 /* Otherwise find the longest matching path */
1014 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
1015 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
1016 strcpy(mntdir, mnt.mnt_dir);
1018 if ((want & WANT_FSNAME) && fsname != NULL)
1019 strcpy(fsname, ptr);
1027 if ((want & WANT_PATH) && path != NULL)
1028 strcpy(path, mntdir);
1029 if (want & WANT_FD) {
1030 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1033 llapi_error(LLAPI_MSG_ERROR, rc,
1034 "error opening '%s'", mntdir);
1040 } else if (want & WANT_ERROR)
1041 llapi_err_noerrno(LLAPI_MSG_ERROR,
1042 "can't find fs root for '%s': %d",
1043 (want & WANT_PATH) ? fsname : path, rc);
1048 * search lustre mounts
1050 * Calling this function will return to the user the mount point, mntdir, and
1051 * the file system name, fsname, if the user passed a buffer to this routine.
1053 * The user inputs are pathname and index. If the pathname is supplied then
1054 * the value of the index will be ignored. The pathname will return data if
1055 * the pathname is located on a lustre mount. Index is used to pick which
1056 * mount point you want in the case of multiple mounted lustre file systems.
1057 * See function lfs_osts in lfs.c for a example of the index use.
1059 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
1062 int want = WANT_PATH, idx = -1;
1064 if (!pathname || pathname[0] == '\0') {
1068 strcpy(mntdir, pathname);
1071 want |= WANT_FSNAME;
1072 return get_root_path(want, fsname, NULL, mntdir, idx);
1075 /* Given a path, find the corresponding Lustre fsname */
1076 int llapi_search_fsname(const char *pathname, char *fsname)
1081 path = realpath(pathname, NULL);
1083 char buf[PATH_MAX], *ptr;
1086 if (pathname[0] != '/') {
1087 /* Need an absolute path, but realpath() only works for
1088 * pathnames that actually exist. We go through the
1089 * extra hurdle of dirname(getcwd() + pathname) in
1090 * case the relative pathname contains ".." in it. */
1091 if (getcwd(buf, sizeof(buf) - 2) == NULL)
1093 rc = strlcat(buf, "/", sizeof(buf));
1094 if (rc >= sizeof(buf))
1097 rc = strlcat(buf, pathname, sizeof(buf));
1098 if (rc >= sizeof(buf))
1100 path = realpath(buf, NULL);
1102 ptr = strrchr(buf, '/');
1106 path = realpath(buf, NULL);
1109 llapi_error(LLAPI_MSG_ERROR, rc,
1110 "pathname '%s' cannot expand",
1116 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1121 int llapi_search_rootpath(char *pathname, const char *fsname)
1123 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1126 int llapi_getname(const char *path, char *buf, size_t size)
1128 struct obd_uuid uuid_buf;
1129 char *uuid = uuid_buf.uuid;
1132 memset(&uuid_buf, 0, sizeof(uuid_buf));
1133 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1137 /* We want to turn lustre-clilov-ffff88002738bc00 into
1138 * lustre-ffff88002738bc00. */
1140 nr = snprintf(buf, size, "%.*s-%s",
1141 (int) (strlen(uuid) - 24), uuid,
1142 uuid + strlen(uuid) - 16);
1152 * find the pool directory path under /proc
1153 * (can be also used to test if a fsname is known)
1155 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
1158 char pattern[PATH_MAX + 1];
1159 char buffer[PATH_MAX];
1161 if (fsname == NULL) {
1162 rc = llapi_search_fsname(pathname, buffer);
1166 strcpy(pathname, fsname);
1169 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
1170 rc = first_match(pattern, buffer);
1174 /* in fsname test mode, pool_pathname is NULL */
1175 if (pool_pathname != NULL)
1176 strcpy(pool_pathname, buffer);
1182 * Get the list of pool members.
1183 * \param poolname string of format \<fsname\>.\<poolname\>
1184 * \param members caller-allocated array of char*
1185 * \param list_size size of the members array
1186 * \param buffer caller-allocated buffer for storing OST names
1187 * \param buffer_size size of the buffer
1189 * \return number of members retrieved for this pool
1190 * \retval -error failure
1192 int llapi_get_poolmembers(const char *poolname, char **members,
1193 int list_size, char *buffer, int buffer_size)
1195 char fsname[PATH_MAX];
1197 char pathname[PATH_MAX];
1198 char path[PATH_MAX];
1205 /* name is FSNAME.POOLNAME */
1206 if (strlen(poolname) >= sizeof(fsname))
1208 strlcpy(fsname, poolname, sizeof(fsname));
1209 pool = strchr(fsname, '.');
1216 rc = poolpath(fsname, NULL, pathname);
1218 llapi_error(LLAPI_MSG_ERROR, rc,
1219 "Lustre filesystem '%s' not found",
1224 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1225 rc = snprintf(path, sizeof(path), "%s/%s", pathname, pool);
1226 if (rc >= sizeof(path))
1228 fd = fopen(path, "r");
1231 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
1236 while (fgets(buf, sizeof(buf), fd) != NULL) {
1237 if (nb_entries >= list_size) {
1241 buf[sizeof(buf) - 1] = '\0';
1243 tmp = strchr(buf, '\n');
1246 if (used + strlen(buf) + 1 > buffer_size) {
1251 strcpy(buffer + used, buf);
1252 members[nb_entries] = buffer + used;
1253 used += strlen(buf) + 1;
1263 * Get the list of pools in a filesystem.
1264 * \param name filesystem name or path
1265 * \param poollist caller-allocated array of char*
1266 * \param list_size size of the poollist array
1267 * \param buffer caller-allocated buffer for storing pool names
1268 * \param buffer_size size of the buffer
1270 * \return number of pools retrieved for this filesystem
1271 * \retval -error failure
1273 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1274 char *buffer, int buffer_size)
1276 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
1280 struct dirent *cookie = NULL;
1282 unsigned int nb_entries = 0;
1283 unsigned int used = 0;
1286 /* initilize output array */
1287 for (i = 0; i < list_size; i++)
1290 /* is name a pathname ? */
1291 ptr = strchr(name, '/');
1293 /* only absolute pathname is supported */
1297 if (!realpath(name, rname)) {
1299 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1304 rc = poolpath(NULL, rname, pathname);
1306 llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
1307 " a Lustre filesystem", name);
1310 if (strlen(rname) > sizeof(fsname)-1)
1312 strncpy(fsname, rname, sizeof(fsname));
1314 /* name is FSNAME */
1315 if (strlen(name) > sizeof(fsname)-1)
1317 strncpy(fsname, name, sizeof(fsname));
1318 rc = poolpath(fsname, NULL, pathname);
1321 llapi_error(LLAPI_MSG_ERROR, rc,
1322 "Lustre filesystem '%s' not found", name);
1326 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1327 dir = opendir(pathname);
1330 llapi_error(LLAPI_MSG_ERROR, rc,
1331 "Could not open pool list for '%s'",
1337 rc = readdir_r(dir, &pool, &cookie);
1341 llapi_error(LLAPI_MSG_ERROR, rc,
1342 "Error reading pool list for '%s'", name);
1344 } else if ((rc == 0) && (cookie == NULL)) {
1345 /* end of directory */
1349 /* ignore . and .. */
1350 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1353 /* check output bounds */
1354 if (nb_entries >= list_size) {
1359 /* +2 for '.' and final '\0' */
1360 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1366 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1367 poollist[nb_entries] = buffer + used;
1368 used += strlen(pool.d_name) + strlen(fsname) + 2;
1374 return ((rc != 0) ? rc : nb_entries);
1377 /* wrapper for lfs.c and obd.c */
1378 int llapi_poollist(const char *name)
1380 /* list of pool names (assume that pool count is smaller
1382 char **list, *buffer = NULL, *path = NULL, *fsname = NULL;
1383 int obdcount, bufsize, rc, nb, i;
1384 char *poolname = NULL, *tmp = NULL, data[16];
1389 if (name[0] != '/') {
1390 fsname = strdup(name);
1394 poolname = strchr(fsname, '.');
1398 path = (char *) name;
1401 rc = get_param_obdvar(fsname, path, "lov", "numobd",
1402 data, sizeof(data));
1405 obdcount = atoi(data);
1407 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1408 * and also an array to store the pointers for all that
1409 * allocated space. */
1411 bufsize = sizeof(struct obd_uuid) * obdcount;
1412 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1413 if (buffer == NULL) {
1417 list = (char **) (buffer + bufsize);
1420 /* name is a path or fsname */
1421 nb = llapi_get_poollist(name, list, obdcount,
1424 /* name is a pool name (<fsname>.<poolname>) */
1425 nb = llapi_get_poolmembers(name, list, obdcount,
1429 if (nb == -EOVERFLOW) {
1432 goto retry_get_pools;
1435 for (i = 0; i < nb; i++)
1436 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1437 rc = (nb < 0 ? nb : 0);
1446 typedef int (semantic_func_t)(char *path, DIR *parent, DIR **d,
1447 void *data, struct dirent64 *de);
1449 #define OBD_NOT_FOUND (-1)
1451 static int common_param_init(struct find_param *param, char *path)
1453 int lumlen = get_mds_md_size(path);
1455 if (lumlen < PATH_MAX + 1)
1456 lumlen = PATH_MAX + 1;
1458 param->lumlen = lumlen;
1459 param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
1460 if (param->lmd == NULL) {
1461 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1462 "error: allocation of %zu bytes for ioctl",
1463 sizeof(lstat_t) + param->lumlen);
1467 param->fp_lmv_count = 256;
1468 param->fp_lmv_md = malloc(lmv_user_md_size(256, LMV_MAGIC_V1));
1469 if (param->fp_lmv_md == NULL) {
1470 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1471 "error: allocation of %d bytes for ioctl",
1472 lmv_user_md_size(256, LMV_MAGIC_V1));
1476 param->got_uuids = 0;
1477 param->obdindexes = NULL;
1478 param->obdindex = OBD_NOT_FOUND;
1479 if (!param->migrate)
1480 param->mdtindex = OBD_NOT_FOUND;
1484 static void find_param_fini(struct find_param *param)
1486 if (param->obdindexes)
1487 free(param->obdindexes);
1492 if (param->fp_lmv_md)
1493 free(param->fp_lmv_md);
1496 static int cb_common_fini(char *path, DIR *parent, DIR **dirp, void *data,
1497 struct dirent64 *de)
1499 struct find_param *param = data;
1505 /* set errno upon failure */
1506 static DIR *opendir_parent(char *path)
1512 fname = strrchr(path, '/');
1514 return opendir(".");
1518 parent = opendir(path);
1523 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1525 struct lmv_user_md *lmv = (struct lmv_user_md *)param->fp_lmv_md;
1528 lmv->lum_stripe_count = param->fp_lmv_count;
1529 if (param->get_default_lmv)
1530 lmv->lum_magic = LMV_USER_MAGIC;
1532 lmv->lum_magic = LMV_MAGIC_V1;
1533 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, lmv);
1538 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1539 struct lov_user_mds_data *lmd, int lumlen)
1541 lstat_t *st = &lmd->lmd_st;
1544 if (parent == NULL && dir == NULL)
1548 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1549 } else if (parent) {
1550 char *fname = strrchr(path, '/');
1552 fname = (fname == NULL ? path : fname + 1);
1553 /* retrieve needed file info */
1554 strlcpy((char *)lmd, fname, lumlen);
1555 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1559 if (errno == ENOTTY) {
1560 /* ioctl is not supported, it is not a lustre fs.
1561 * Do the regular lstat(2) instead. */
1562 ret = lstat_f(path, st);
1565 llapi_error(LLAPI_MSG_ERROR, ret,
1566 "error: %s: lstat failed for %s",
1569 } else if (errno == ENOENT) {
1571 llapi_error(LLAPI_MSG_WARN, ret,
1572 "warning: %s: %s does not exist",
1574 } else if (errno != EISDIR) {
1576 llapi_error(LLAPI_MSG_ERROR, ret,
1577 "%s ioctl failed for %s.",
1578 dir ? "LL_IOC_MDC_GETINFO" :
1579 "IOC_MDC_GETFILEINFO", path);
1582 llapi_error(LLAPI_MSG_ERROR, ret,
1583 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1590 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1591 semantic_func_t sem_init,
1592 semantic_func_t sem_fini, void *data,
1593 struct dirent64 *de)
1595 struct find_param *param = (struct find_param *)data;
1596 struct dirent64 *dent;
1604 if (!d && errno != ENOTDIR) {
1606 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1609 } else if (!d && !parent) {
1610 /* ENOTDIR. Open the parent dir. */
1611 p = opendir_parent(path);
1618 if (sem_init && (ret = sem_init(path, parent ?: p, &d, data, de)))
1624 while ((dent = readdir64(d)) != NULL) {
1627 param->have_fileinfo = 0;
1629 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1632 /* Don't traverse .lustre directory */
1633 if (!(strcmp(dent->d_name, dot_lustre_name)))
1637 if ((len + dent->d_reclen + 2) > size) {
1638 llapi_err_noerrno(LLAPI_MSG_ERROR,
1639 "error: %s: string buffer is too small",
1644 strcat(path, dent->d_name);
1646 if (dent->d_type == DT_UNKNOWN) {
1647 lstat_t *st = ¶m->lmd->lmd_st;
1649 rc = get_lmd_info(path, d, NULL, param->lmd,
1652 dent->d_type = IFTODT(st->st_mode);
1659 switch (dent->d_type) {
1661 llapi_err_noerrno(LLAPI_MSG_ERROR,
1662 "error: %s: '%s' is UNKNOWN type %d",
1663 __func__, dent->d_name, dent->d_type);
1666 rc = llapi_semantic_traverse(path, size, d, sem_init,
1667 sem_fini, data, dent);
1668 if (rc != 0 && ret == 0)
1674 rc = sem_init(path, d, NULL, data, dent);
1675 if (rc < 0 && ret == 0)
1678 if (sem_fini && rc == 0)
1679 sem_fini(path, d, NULL, data, dent);
1687 sem_fini(path, parent, &d, data, de);
1696 static int param_callback(char *path, semantic_func_t sem_init,
1697 semantic_func_t sem_fini, struct find_param *param)
1699 int ret, len = strlen(path);
1702 if (len > PATH_MAX) {
1704 llapi_error(LLAPI_MSG_ERROR, ret,
1705 "Path name '%s' is too long", path);
1709 buf = (char *)malloc(PATH_MAX + 1);
1713 strlcpy(buf, path, PATH_MAX + 1);
1714 ret = common_param_init(param, buf);
1718 param->fp_depth = 0;
1720 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1721 sem_fini, param, NULL);
1723 find_param_fini(param);
1725 return ret < 0 ? ret : 0;
1728 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1730 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1733 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1738 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1740 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1743 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1748 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1752 fd = open(path, O_RDONLY);
1755 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1759 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1765 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1769 fd = open(path, O_RDONLY);
1772 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1776 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1787 * If uuidp is NULL, return the number of available obd uuids.
1788 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1789 * there are more OSTs then allocated to uuidp, then an error is returned with
1790 * the ost_count set to number of available obd uuids.
1792 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1793 int *ost_count, enum tgt_type type)
1795 struct obd_uuid name;
1799 int rc = 0, index = 0;
1801 /* Get the lov name */
1802 if (type == LOV_TYPE) {
1803 rc = llapi_file_fget_lov_uuid(fd, &name);
1807 rc = llapi_file_fget_lmv_uuid(fd, &name);
1812 /* Now get the ost uuids from /proc */
1813 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1814 type == LOV_TYPE ? "lov" : "lmv", name.uuid);
1815 fp = fopen(buf, "r");
1818 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1822 snprintf(format, sizeof(format),
1823 "%%d: %%%zus", sizeof(uuidp[0].uuid) - 1);
1824 while (fgets(buf, sizeof(buf), fp) != NULL) {
1825 if (uuidp && (index < *ost_count)) {
1826 if (sscanf(buf, format, &index, uuidp[index].uuid) < 2)
1834 if (uuidp && (index > *ost_count))
1841 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1843 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1846 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1851 root = opendir(mnt);
1854 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1859 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1867 /* Check if user specified value matches a real uuid. Ignore _UUID,
1868 * -osc-4ba41334, other trailing gunk in comparison.
1869 * @param real_uuid ends in "_UUID"
1870 * @param search_uuid may or may not end in "_UUID"
1872 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1874 int cmplen = strlen(real_uuid);
1875 int searchlen = strlen(search_uuid);
1877 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1879 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1882 /* The UUIDs may legitimately be different lengths, if
1883 * the system was upgraded from an older version. */
1884 if (cmplen != searchlen)
1887 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1890 /* Here, param->obduuid points to a single obduuid, the index of which is
1891 * returned in param->obdindex */
1892 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1894 struct obd_uuid obd_uuid;
1900 if (param->got_uuids)
1903 /* Get the lov/lmv name */
1905 rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
1907 rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
1909 if (rc != -ENOTTY) {
1910 llapi_error(LLAPI_MSG_ERROR, rc,
1911 "error: can't get lov name: %s", dname);
1918 param->got_uuids = 1;
1920 /* Now get the ost uuids from /proc */
1921 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1922 param->get_lmv ? "lmv" : "lov", obd_uuid.uuid);
1923 fp = fopen(buf, "r");
1926 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1930 if (!param->obduuid && !param->quiet && !param->obds_printed)
1931 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1932 param->get_lmv ? "MDTS" : "OBDS:");
1934 snprintf(format, sizeof(format),
1935 "%%d: %%%zus", sizeof(obd_uuid.uuid) - 1);
1936 while (fgets(buf, sizeof(buf), fp) != NULL) {
1939 if (sscanf(buf, format, &index, obd_uuid.uuid) < 2)
1942 if (param->obduuid) {
1943 if (llapi_uuid_match(obd_uuid.uuid,
1944 param->obduuid->uuid)) {
1945 param->obdindex = index;
1948 } else if (!param->quiet && !param->obds_printed) {
1949 /* Print everything */
1950 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1953 param->obds_printed = 1;
1957 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1958 llapi_err_noerrno(LLAPI_MSG_ERROR,
1959 "error: %s: unknown obduuid: %s",
1960 __func__, param->obduuid->uuid);
1967 /* In this case, param->obduuid will be an array of obduuids and
1968 * obd index for all these obduuids will be returned in
1969 * param->obdindexes */
1970 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1971 int num_obds, int **obdindexes, int *obdindex,
1974 int ret, obdcount, obd_valid = 0, obdnum;
1976 struct obd_uuid *uuids = NULL;
1980 if (type == LOV_TYPE)
1981 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
1983 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
1987 obdcount = atoi(buf);
1988 uuids = (struct obd_uuid *)malloc(obdcount *
1989 sizeof(struct obd_uuid));
1994 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1996 struct obd_uuid *uuids_temp;
1998 if (ret == -EOVERFLOW) {
1999 uuids_temp = realloc(uuids, obdcount *
2000 sizeof(struct obd_uuid));
2001 if (uuids_temp != NULL) {
2003 goto retry_get_uuids;
2009 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
2013 indexes = malloc(num_obds * sizeof(*obdindex));
2014 if (indexes == NULL) {
2019 for (obdnum = 0; obdnum < num_obds; obdnum++) {
2022 /* The user may have specified a simple index */
2023 i = strtol(obduuids[obdnum].uuid, &end, 0);
2024 if (end && *end == '\0' && i < obdcount) {
2025 indexes[obdnum] = i;
2028 for (i = 0; i < obdcount; i++) {
2029 if (llapi_uuid_match(uuids[i].uuid,
2030 obduuids[obdnum].uuid)) {
2031 indexes[obdnum] = i;
2037 if (i >= obdcount) {
2038 indexes[obdnum] = OBD_NOT_FOUND;
2039 llapi_err_noerrno(LLAPI_MSG_ERROR,
2040 "error: %s: unknown obduuid: %s",
2041 __func__, obduuids[obdnum].uuid);
2047 *obdindex = OBD_NOT_FOUND;
2049 *obdindex = obd_valid;
2051 *obdindexes = indexes;
2059 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
2063 if (param->mdtuuid) {
2064 ret = setup_indexes(dir, path, param->mdtuuid, param->num_mdts,
2065 ¶m->mdtindexes, ¶m->mdtindex, LMV_TYPE);
2069 if (param->obduuid) {
2070 ret = setup_indexes(dir, path, param->obduuid, param->num_obds,
2071 ¶m->obdindexes, ¶m->obdindex, LOV_TYPE);
2075 param->got_uuids = 1;
2079 int llapi_ostlist(char *path, struct find_param *param)
2084 dir = opendir(path);
2088 ret = setup_obd_uuid(dir, path, param);
2095 * Given a filesystem name, or a pathname of a file on a lustre filesystem,
2096 * tries to determine the path to the filesystem's clilov directory under /proc
2098 * fsname is limited to MTI_NAME_MAXLEN in lustre_idl.h
2099 * The NUL terminator is compensated by the additional "%s" bytes. */
2100 #define LOV_LEN (sizeof("/proc/fs/lustre/lov/%s-clilov-*") + MTI_NAME_MAXLEN)
2101 static int clilovpath(const char *fsname, const char *const pathname,
2105 char pattern[LOV_LEN];
2106 char buffer[PATH_MAX + 1];
2108 if (fsname == NULL) {
2109 rc = llapi_search_fsname(pathname, buffer);
2115 snprintf(pattern, sizeof(pattern), "/proc/fs/lustre/lov/%s-clilov-*",
2118 rc = first_match(pattern, buffer);
2122 strlcpy(clilovpath, buffer, sizeof(buffer));
2128 * Given the path to a stripe attribute proc file, tries to open and
2129 * read the attribute and return the value using the attr parameter
2131 static int sattr_read_attr(const char *const fpath,
2136 char line[PATH_MAX + 1];
2139 f = fopen(fpath, "r");
2142 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open '%s'", fpath);
2146 if (fgets(line, sizeof(line), f) != NULL) {
2149 llapi_error(LLAPI_MSG_ERROR, errno, "Cannot read from '%s'", fpath);
2158 * Tries to determine the default stripe attributes for a given filesystem. The
2159 * filesystem to check should be specified by fsname, or will be determined
2162 static int sattr_get_defaults(const char *const fsname,
2163 const char *const pathname,
2164 unsigned int *scount,
2165 unsigned int *ssize,
2166 unsigned int *soffset)
2169 char dpath[PATH_MAX + 1];
2170 char fpath[PATH_MAX + 1];
2172 rc = clilovpath(fsname, pathname, dpath);
2177 snprintf(fpath, PATH_MAX, "%s/stripecount", dpath);
2178 rc = sattr_read_attr(fpath, scount);
2184 snprintf(fpath, PATH_MAX, "%s/stripesize", dpath);
2185 rc = sattr_read_attr(fpath, ssize);
2191 snprintf(fpath, PATH_MAX, "%s/stripeoffset", dpath);
2192 rc = sattr_read_attr(fpath, soffset);
2201 * Tries to gather the default stripe attributes for a given filesystem. If
2202 * the attributes can be determined, they are cached for easy retreival the
2203 * next time they are needed. Only a single filesystem's attributes are
2206 static int sattr_cache_get_defaults(const char *const fsname,
2207 const char *const pathname,
2208 unsigned int *scount,
2209 unsigned int *ssize,
2210 unsigned int *soffset)
2213 char fsname[PATH_MAX + 1];
2214 unsigned int stripecount;
2215 unsigned int stripesize;
2216 unsigned int stripeoffset;
2222 char fsname_buf[PATH_MAX + 1];
2223 unsigned int tmp[3];
2225 if (fsname == NULL) {
2226 rc = llapi_search_fsname(pathname, fsname_buf);
2230 strlcpy(fsname_buf, fsname, sizeof(fsname_buf));
2233 if (strncmp(fsname_buf, cache.fsname, sizeof(fsname_buf) - 1) != 0) {
2235 * Ensure all 3 sattrs (count, size, and offset) are
2236 * successfully retrieved and stored in tmp before writing to
2239 rc = sattr_get_defaults(fsname_buf, NULL, &tmp[0], &tmp[1],
2244 cache.stripecount = tmp[0];
2245 cache.stripesize = tmp[1];
2246 cache.stripeoffset = tmp[2];
2247 strlcpy(cache.fsname, fsname_buf, sizeof(cache.fsname));
2251 *scount = cache.stripecount;
2253 *ssize = cache.stripesize;
2255 *soffset = cache.stripeoffset;
2260 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2261 struct lov_user_ost_data_v1 *objects,
2262 int is_dir, int verbose, int depth,
2263 int raw, char *pool_name)
2265 char *prefix = is_dir ? "" : "lmm_";
2266 char *seperator = "";
2269 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2270 lmm_oi_set_seq(&lum->lmm_oi, 0);
2271 if (verbose & VERBOSE_DETAIL)
2272 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
2275 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2276 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2278 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2279 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
2281 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
2282 lmm_oi_seq(&lum->lmm_oi));
2283 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
2284 lmm_oi_id(&lum->lmm_oi));
2287 if (verbose & VERBOSE_COUNT) {
2288 if (verbose & ~VERBOSE_COUNT)
2289 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
2292 if (!raw && lum->lmm_stripe_count == 0) {
2293 unsigned int scount;
2294 rc = sattr_cache_get_defaults(NULL, path,
2298 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2301 llapi_error(LLAPI_MSG_ERROR, rc,
2302 "Cannot determine default"
2305 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2306 lum->lmm_stripe_count ==
2307 (typeof(lum->lmm_stripe_count))(-1)
2308 ? -1 : lum->lmm_stripe_count);
2311 llapi_printf(LLAPI_MSG_NORMAL, "%hd",
2312 (__s16)lum->lmm_stripe_count);
2314 seperator = is_dir ? " " : "\n";
2317 if (verbose & VERBOSE_SIZE) {
2318 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2319 if (verbose & ~VERBOSE_SIZE)
2320 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
2322 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
2324 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2327 llapi_printf(LLAPI_MSG_NORMAL, "%u", ssize);
2329 llapi_error(LLAPI_MSG_ERROR, rc,
2330 "Cannot determine default"
2333 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2334 lum->lmm_stripe_size);
2336 seperator = is_dir ? " " : "\n";
2339 if ((verbose & VERBOSE_LAYOUT) && !is_dir) {
2340 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2341 if (verbose & ~VERBOSE_LAYOUT)
2342 llapi_printf(LLAPI_MSG_NORMAL, "%spattern: ",
2344 llapi_printf(LLAPI_MSG_NORMAL, "%.x", lum->lmm_pattern);
2348 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2349 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2350 if (verbose & ~VERBOSE_GENERATION)
2351 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
2353 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2354 (int)lum->lmm_layout_gen);
2358 if (verbose & VERBOSE_OFFSET) {
2359 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2360 if (verbose & ~VERBOSE_OFFSET)
2361 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
2364 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2365 lum->lmm_stripe_offset ==
2366 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2367 lum->lmm_stripe_offset);
2369 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2370 objects[0].l_ost_idx);
2371 seperator = is_dir ? " " : "\n";
2374 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
2375 llapi_printf(LLAPI_MSG_NORMAL, "%s", seperator);
2376 if (verbose & ~VERBOSE_POOL)
2377 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
2379 llapi_printf(LLAPI_MSG_NORMAL, "%s", pool_name);
2382 if (!is_dir || (is_dir && (verbose != VERBOSE_OBJID)))
2383 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2386 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2387 struct lov_user_ost_data_v1 *objects,
2388 char *path, int is_dir, int obdindex,
2389 int depth, int header, int raw)
2391 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2394 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2395 if (obdindex == objects[i].l_ost_idx) {
2403 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
2404 depth, raw, pool_name);
2406 if (!is_dir && (header & VERBOSE_OBJID) &&
2407 !(lum->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
2409 llapi_printf(LLAPI_MSG_NORMAL,
2410 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2412 for (i = 0; i < lum->lmm_stripe_count; i++) {
2413 int idx = objects[i].l_ost_idx;
2414 long long oid = ostid_id(&objects[i].l_ost_oi);
2415 long long gr = ostid_seq(&objects[i].l_ost_oi);
2416 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) {
2418 sprintf(fmt, "%s%s%s\n",
2419 "\t%6u\t%14llu\t%#13llx\t",
2420 (fid_seq_is_rsvd(gr) ||
2421 fid_seq_is_mdt0(gr)) ?
2422 "%14llu" : "%#14llx", "%s");
2423 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2425 obdindex == idx ? " *" : "");
2429 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2433 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2434 char *path, int obdindex, int depth, int verbose)
2436 struct lmv_user_mds_data *objects = lum->lum_objects;
2437 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2438 int i, obdstripe = 0;
2439 char *separator = "";
2441 if (obdindex != OBD_NOT_FOUND) {
2442 if (lum->lum_stripe_count == 0) {
2443 if (obdindex == lum->lum_stripe_offset)
2446 for (i = 0; i < lum->lum_stripe_count; i++) {
2447 if (obdindex == objects[i].lum_mds) {
2448 llapi_printf(LLAPI_MSG_NORMAL,
2463 /* show all information default */
2465 if (lum->lum_magic == LMV_USER_MAGIC)
2466 verbose = VERBOSE_POOL | VERBOSE_COUNT | VERBOSE_OFFSET;
2468 verbose = VERBOSE_OBJID;
2471 if (depth && path && ((verbose != VERBOSE_OBJID)))
2472 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix, path);
2474 if (verbose & VERBOSE_COUNT) {
2475 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2476 if (verbose & ~VERBOSE_COUNT)
2477 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2478 llapi_printf(LLAPI_MSG_NORMAL, "%u",
2479 (int)lum->lum_stripe_count);
2480 if (verbose & VERBOSE_OFFSET)
2486 if (verbose & VERBOSE_OFFSET) {
2487 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2488 if (verbose & ~VERBOSE_OFFSET)
2489 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2490 llapi_printf(LLAPI_MSG_NORMAL, "%d",
2491 (int)lum->lum_stripe_offset);
2495 if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
2496 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2497 if (obdstripe == 1 && lum->lum_stripe_count > 0)
2498 llapi_printf(LLAPI_MSG_NORMAL,
2499 "mdtidx\t\t FID[seq:oid:ver]\n");
2500 for (i = 0; i < lum->lum_stripe_count; i++) {
2501 int idx = objects[i].lum_mds;
2502 struct lu_fid *fid = &objects[i].lum_fid;
2503 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2504 llapi_printf(LLAPI_MSG_NORMAL,
2505 "%6u\t\t "DFID"\t\t%s\n",
2507 obdindex == idx ? " *" : "");
2512 if ((verbose & VERBOSE_POOL) && (pool_name[0] != '\0')) {
2513 llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
2514 if (verbose & ~VERBOSE_POOL)
2515 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2517 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2521 if (!(verbose & VERBOSE_OBJID) || lum->lum_magic == LMV_USER_MAGIC)
2522 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2525 void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
2529 if (param->get_lmv || param->get_default_lmv)
2530 magic = (__u32)param->fp_lmv_md->lum_magic;
2532 magic = *(__u32 *)¶m->lmd->lmd_lmm; /* lum->lmm_magic */
2535 case LOV_USER_MAGIC_V1:
2536 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
2537 param->lmd->lmd_lmm.lmm_objects,
2539 param->obdindex, param->fp_max_depth,
2540 param->verbose, param->raw);
2542 case LOV_USER_MAGIC_V3: {
2543 char pool_name[LOV_MAXPOOLNAME + 1];
2544 struct lov_user_ost_data_v1 *objects;
2545 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2547 strlcpy(pool_name, lmmv3->lmm_pool_name, sizeof(pool_name));
2548 objects = lmmv3->lmm_objects;
2549 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
2550 objects, path, is_dir,
2551 param->obdindex, param->fp_max_depth,
2552 param->verbose, param->raw);
2556 case LMV_USER_MAGIC: {
2557 char pool_name[LOV_MAXPOOLNAME + 1];
2558 struct lmv_user_md *lum;
2560 lum = (struct lmv_user_md *)param->fp_lmv_md;
2561 strlcpy(pool_name, lum->lum_pool_name, sizeof(pool_name));
2562 lmv_dump_user_lmm(lum, pool_name, path,
2563 param->obdindex, param->fp_max_depth,
2568 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2569 "(expecting one of %#x %#x %#x %#x)\n",
2570 *(__u32 *)¶m->lmd->lmd_lmm,
2571 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2572 LMV_USER_MAGIC, LMV_MAGIC_V1);
2577 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2583 fname = strrchr(path, '/');
2585 /* It should be a file (or other non-directory) */
2586 if (fname == NULL) {
2587 dname = (char *)malloc(2);
2591 fname = (char *)path;
2593 dname = (char *)malloc(fname - path + 1);
2596 strncpy(dname, path, fname - path);
2597 dname[fname - path] = '\0';
2601 fd = open(dname, O_RDONLY);
2608 strcpy((char *)lum, fname);
2609 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2612 if (close(fd) == -1 && rc == 0)
2619 int llapi_file_lookup(int dirfd, const char *name)
2621 struct obd_ioctl_data data = { 0 };
2626 if (dirfd < 0 || name == NULL)
2629 data.ioc_version = OBD_IOCTL_VERSION;
2630 data.ioc_len = sizeof(data);
2631 data.ioc_inlbuf1 = (char *)name;
2632 data.ioc_inllen1 = strlen(name) + 1;
2634 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2636 llapi_error(LLAPI_MSG_ERROR, rc,
2637 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2642 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2648 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2649 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2651 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2652 * The table below gives the answers for the specified parameters (value and
2653 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2654 * --------------------------------------
2655 * 1 | file > limit; sign > 0 | -1 / -1 |
2656 * 2 | file = limit; sign > 0 | -1 / -1 |
2657 * 3 | file < limit; sign > 0 | ? / 1 |
2658 * 4 | file > limit; sign = 0 | -1 / -1 |
2659 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2660 * 6 | file < limit; sign = 0 | ? / -1 |
2661 * 7 | file > limit; sign < 0 | 1 / 1 |
2662 * 8 | file = limit; sign < 0 | ? / -1 |
2663 * 9 | file < limit; sign < 0 | ? / -1 |
2664 * --------------------------------------
2665 * Note: 5th actually means that the value is within the interval
2666 * (limit - margin, limit]. */
2667 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2668 int sign, int negopt, unsigned long long margin,
2674 /* Drop the fraction of margin (of days). */
2675 if (file + margin <= limit)
2677 } else if (sign == 0) {
2678 if (file <= limit && file + margin > limit)
2680 else if (file + margin <= limit)
2682 } else if (sign < 0) {
2689 return negopt ? ~ret + 1 : ret;
2692 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2693 * Return -1 or 1 if file timestamp does not or does match the given criteria
2694 * correspondingly. Return 0 if the MDS time is being checked and there are
2695 * attributes on OSTs and it is not yet clear if the timespamp matches.
2697 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2698 * updated timestamps. */
2699 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2704 /* Check if file is accepted. */
2705 if (param->fp_atime) {
2706 rc2 = find_value_cmp(st->st_atime, param->fp_atime,
2707 param->fp_asign, param->fp_exclude_atime,
2714 if (param->fp_mtime) {
2715 rc2 = find_value_cmp(st->st_mtime, param->fp_mtime,
2716 param->fp_msign, param->fp_exclude_mtime,
2721 /* If the previous check matches, but this one is not yet clear,
2722 * we should return 0 to do an RPC on OSTs. */
2727 if (param->fp_ctime) {
2728 rc2 = find_value_cmp(st->st_ctime, param->fp_ctime,
2729 param->fp_csign, param->fp_exclude_ctime,
2734 /* If the previous check matches, but this one is not yet clear,
2735 * we should return 0 to do an RPC on OSTs. */
2744 * Check whether the stripes matches the indexes user provided
2748 static int check_obd_match(struct find_param *param)
2750 lstat_t *st = ¶m->lmd->lmd_st;
2751 struct lov_user_ost_data_v1 *lmm_objects;
2754 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
2757 if (!S_ISREG(st->st_mode))
2760 /* Only those files should be accepted, which have a
2761 * stripe on the specified OST. */
2762 if (!param->lmd->lmd_lmm.lmm_stripe_count)
2765 if (param->lmd->lmd_lmm.lmm_magic ==
2766 LOV_USER_MAGIC_V3) {
2767 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2769 lmm_objects = lmmv3->lmm_objects;
2770 } else if (param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2771 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
2773 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2774 __func__, param->lmd->lmd_lmm.lmm_magic);
2778 for (i = 0; i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
2779 for (j = 0; j < param->num_obds; j++) {
2780 if (param->obdindexes[j] ==
2781 lmm_objects[i].l_ost_idx) {
2782 if (param->exclude_obd)
2789 if (param->exclude_obd)
2794 static int check_mdt_match(struct find_param *param)
2798 if (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND)
2801 /* FIXME: For striped dir, we should get stripe information and check */
2802 for (i = 0; i < param->num_mdts; i++) {
2803 if (param->mdtindexes[i] == param->file_mdtindex)
2804 return !param->exclude_mdt;
2807 if (param->exclude_mdt)
2813 * Check whether the obd is active or not, if it is
2814 * not active, just print the object affected by this
2817 static int print_failed_tgt(struct find_param *param, char *path, int type)
2819 struct obd_statfs stat_buf;
2820 struct obd_uuid uuid_buf;
2823 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2825 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2826 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2827 ret = llapi_obd_statfs(path, type,
2828 param->obdindex, &stat_buf,
2831 llapi_printf(LLAPI_MSG_NORMAL,
2832 "obd_uuid: %s failed %s ",
2833 param->obduuid->uuid,
2839 static int cb_find_init(char *path, DIR *parent, DIR **dirp,
2840 void *data, struct dirent64 *de)
2842 struct find_param *param = (struct find_param *)data;
2843 DIR *dir = dirp == NULL ? NULL : *dirp;
2844 int decision = 1; /* 1 is accepted; -1 is rejected. */
2845 lstat_t *st = ¶m->lmd->lmd_st;
2847 int checked_type = 0;
2850 LASSERT(parent != NULL || dir != NULL);
2852 if (param->have_fileinfo == 0)
2853 param->lmd->lmd_lmm.lmm_stripe_count = 0;
2855 /* If a regular expression is presented, make the initial decision */
2856 if (param->pattern != NULL) {
2857 char *fname = strrchr(path, '/');
2858 fname = (fname == NULL ? path : fname + 1);
2859 ret = fnmatch(param->pattern, fname, 0);
2860 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
2861 (ret == 0 && param->exclude_pattern))
2865 /* See if we can check the file type from the dirent. */
2866 if (param->fp_type != 0 && de != NULL && de->d_type != DT_UNKNOWN) {
2869 if (DTTOIF(de->d_type) == param->fp_type) {
2870 if (param->fp_exclude_type)
2873 if (!param->fp_exclude_type)
2880 /* Request MDS for the stat info if some of these parameters need
2881 * to be compared. */
2882 if (param->obduuid || param->mdtuuid ||
2883 param->fp_check_uid || param->fp_check_gid ||
2884 param->fp_atime || param->fp_mtime || param->fp_ctime ||
2885 param->check_pool || param->check_size ||
2886 param->check_stripecount || param->check_stripesize ||
2887 param->check_layout)
2890 if (param->fp_type != 0 && checked_type == 0)
2893 if (param->have_fileinfo == 0 && decision == 0) {
2894 ret = get_lmd_info(path, parent, dir, param->lmd,
2898 ret = llapi_file_fget_mdtidx(dirfd(dir),
2899 ¶m->file_mdtindex);
2904 ret = lstat_f(path, &tmp_st);
2907 llapi_error(LLAPI_MSG_ERROR, ret,
2908 "error: %s: lstat failed"
2909 "for %s", __func__, path);
2912 if (S_ISREG(tmp_st.st_mode)) {
2913 fd = open(path, O_RDONLY);
2915 ret = llapi_file_fget_mdtidx(fd,
2916 ¶m->file_mdtindex);
2922 /* For special inode, it assumes to
2923 * reside on the same MDT with the
2926 ret = llapi_file_fget_mdtidx(fd,
2927 ¶m->file_mdtindex);
2940 if (param->fp_type && !checked_type) {
2941 if ((st->st_mode & S_IFMT) == param->fp_type) {
2942 if (param->fp_exclude_type)
2945 if (!param->fp_exclude_type)
2951 if (param->obduuid || param->mdtuuid) {
2952 if (lustre_fs && param->got_uuids &&
2953 param->fp_dev != st->st_dev) {
2954 /* A lustre/lustre mount point is crossed. */
2955 param->got_uuids = 0;
2956 param->obds_printed = 0;
2957 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2960 if (lustre_fs && !param->got_uuids) {
2961 ret = setup_target_indexes(dir ? dir : parent, path,
2966 param->fp_dev = st->st_dev;
2967 } else if (!lustre_fs && param->got_uuids) {
2968 /* A lustre/non-lustre mount point is crossed. */
2969 param->got_uuids = 0;
2970 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2974 if (param->check_stripesize) {
2975 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_size,
2977 param->stripesize_sign,
2978 param->exclude_stripesize,
2979 param->stripesize_units, 0);
2984 if (param->check_stripecount) {
2985 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_count,
2987 param->stripecount_sign,
2988 param->exclude_stripecount, 1, 0);
2993 if (param->check_layout) {
2996 found = (param->lmd->lmd_lmm.lmm_pattern & param->layout);
2997 if ((param->lmd->lmd_lmm.lmm_pattern == 0xFFFFFFFF) ||
2998 (found && param->exclude_layout) ||
2999 (!found && !param->exclude_layout)) {
3005 /* If an OBD UUID is specified but none matches, skip this file. */
3006 if ((param->obduuid && param->obdindex == OBD_NOT_FOUND) ||
3007 (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND))
3010 /* If a OST or MDT UUID is given, and some OST matches,
3012 if (param->obdindex != OBD_NOT_FOUND ||
3013 param->mdtindex != OBD_NOT_FOUND) {
3014 if (param->obduuid) {
3015 if (check_obd_match(param)) {
3016 /* If no mdtuuid is given, we are done.
3017 * Otherwise, fall through to the mdtuuid
3019 if (!param->mdtuuid)
3025 if (param->mdtuuid) {
3026 if (check_mdt_match(param))
3032 if (param->fp_check_uid) {
3033 if (st->st_uid == param->fp_uid) {
3034 if (param->fp_exclude_uid)
3037 if (!param->fp_exclude_uid)
3042 if (param->fp_check_gid) {
3043 if (st->st_gid == param->fp_gid) {
3044 if (param->fp_exclude_gid)
3047 if (!param->fp_exclude_gid)
3052 if (param->check_pool) {
3053 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
3055 /* empty requested pool is taken as no pool search => V1 */
3056 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
3057 (param->poolname[0] == '\0')) ||
3058 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3059 (strncmp(lmmv3->lmm_pool_name,
3060 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
3061 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
3062 (strcmp(param->poolname, "*") == 0))) {
3063 if (param->exclude_pool)
3066 if (!param->exclude_pool)
3071 /* Check the time on mds. */
3073 if (param->fp_atime || param->fp_mtime || param->fp_ctime) {
3076 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
3077 param->lmd->lmd_lmm.lmm_stripe_count)
3079 decision = find_time_check(st, param, for_mds);
3084 /* If file still fits the request, ask ost for updated info.
3085 The regular stat is almost of the same speed as some new
3086 'glimpse-size-ioctl'. */
3088 if (param->check_size && S_ISREG(st->st_mode) &&
3089 param->lmd->lmd_lmm.lmm_stripe_count)
3093 /* For regular files with the stripe the decision may have not
3094 * been taken yet if *time or size is to be checked. */
3095 LASSERT((S_ISREG(st->st_mode) &&
3096 param->lmd->lmd_lmm.lmm_stripe_count) ||
3097 param->mdtindex != OBD_NOT_FOUND);
3099 if (param->obdindex != OBD_NOT_FOUND)
3100 print_failed_tgt(param, path, LL_STATFS_LOV);
3102 if (param->mdtindex != OBD_NOT_FOUND)
3103 print_failed_tgt(param, path, LL_STATFS_LMV);
3106 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
3107 (void *)param->lmd);
3108 } else if (parent) {
3109 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
3110 (void *)param->lmd);
3114 if (errno == ENOENT) {
3115 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
3116 "warning: %s: %s does not exist",
3121 llapi_error(LLAPI_MSG_ERROR, ret,
3122 "%s: IOC_LOV_GETINFO on %s failed",
3128 /* Check the time on osc. */
3129 decision = find_time_check(st, param, 0);
3136 if (param->check_size)
3137 decision = find_value_cmp(st->st_size, param->size,
3138 param->size_sign, param->exclude_size,
3139 param->size_units, 0);
3141 if (decision != -1) {
3142 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
3144 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
3146 llapi_printf(LLAPI_MSG_NORMAL, "\n");
3150 /* Do not get down anymore? */
3151 if (param->fp_depth == param->fp_max_depth)
3159 static int cb_mv_init(char *path, DIR *parent, DIR **dirp,
3160 void *param_data, struct dirent64 *de)
3162 struct find_param *param = (struct find_param *)param_data;
3164 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3166 struct obd_ioctl_data data = { 0 };
3171 LASSERT(parent != NULL || dirp != NULL);
3175 if (parent == NULL) {
3176 dir = opendir_parent(path);
3180 llapi_error(LLAPI_MSG_ERROR, ret,
3181 "can not open %s", path);
3188 filename = basename(path);
3189 data.ioc_inlbuf1 = (char *)filename;
3190 data.ioc_inllen1 = strlen(filename) + 1;
3191 data.ioc_inlbuf2 = (char *)¶m->mdtindex;
3192 data.ioc_inllen2 = sizeof(param->mdtindex);
3193 ret = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3195 llapi_error(LLAPI_MSG_ERROR, ret,
3196 "llapi_obd_statfs: error packing ioctl data");
3200 ret = ioctl(fd, LL_IOC_MIGRATE, rawbuf);
3203 fprintf(stderr, "%s migrate failed %d\n", path, ret);
3205 } else if (param->verbose & VERBOSE_DETAIL) {
3206 fprintf(stdout, "migrate %s to MDT%d\n", path, param->mdtindex);
3211 /* If the directory is being migration, we need
3212 * close the directory after migration,
3213 * so the old directory cache will be cleanup
3214 * on the client side, and re-open to get the
3215 * new directory handle */
3216 *dirp = opendir(path);
3219 llapi_error(LLAPI_MSG_ERROR, ret,
3220 "%s: Failed to open '%s'", __func__, path);
3231 int llapi_mv(char *path, struct find_param *param)
3233 return param_callback(path, cb_mv_init, cb_common_fini, param);
3236 int llapi_find(char *path, struct find_param *param)
3238 return param_callback(path, cb_find_init, cb_common_fini, param);
3242 * Get MDT number that the file/directory inode referenced
3243 * by the open fd resides on.
3244 * Return 0 and mdtidx on success, or -ve errno.
3246 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
3248 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
3253 static int cb_get_mdt_index(char *path, DIR *parent, DIR **dirp, void *data,
3254 struct dirent64 *de)
3256 struct find_param *param = (struct find_param *)data;
3257 DIR *d = dirp == NULL ? NULL : *dirp;
3261 LASSERT(parent != NULL || d != NULL);
3264 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3265 } else if (parent) {
3268 fd = open(path, O_RDONLY);
3270 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3278 if (ret == -ENODATA) {
3279 if (!param->obduuid)
3280 llapi_printf(LLAPI_MSG_NORMAL,
3281 "%s has no stripe info\n", path);
3283 } else if (ret == -ENOENT) {
3284 llapi_error(LLAPI_MSG_WARN, ret,
3285 "warning: %s: %s does not exist",
3288 } else if (ret == -ENOTTY) {
3289 llapi_error(LLAPI_MSG_ERROR, ret,
3290 "%s: '%s' not on a Lustre fs?",
3293 llapi_error(LLAPI_MSG_ERROR, ret,
3294 "error: %s: LL_IOC_GET_MDTIDX failed for %s",
3300 /* The 'LASSERT(parent != NULL || d != NULL);' guarantees
3301 * that either 'd' or 'parent' is not null.
3302 * So in all cases llapi_file_fget_mdtidx() is called,
3303 * thus initializing 'mdtidx'. */
3304 if (param->quiet || !(param->verbose & VERBOSE_DETAIL))
3305 /* coverity[uninit_use_in_call] */
3306 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3308 /* coverity[uninit_use_in_call] */
3309 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3313 /* Do not get down anymore? */
3314 if (param->fp_depth == param->fp_max_depth)
3322 static int cb_getstripe(char *path, DIR *parent, DIR **dirp, void *data,
3323 struct dirent64 *de)
3325 struct find_param *param = (struct find_param *)data;
3326 DIR *d = dirp == NULL ? NULL : *dirp;
3329 LASSERT(parent != NULL || d != NULL);
3331 if (param->obduuid) {
3333 ret = setup_obd_uuid(d ? d : parent, path, param);
3339 if (param->get_lmv || param->get_default_lmv) {
3340 ret = cb_get_dirstripe(path, d, param);
3342 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
3343 (void *)¶m->lmd->lmd_lmm);
3346 } else if (parent && !param->get_lmv && !param->get_default_lmv) {
3347 char *fname = strrchr(path, '/');
3348 fname = (fname == NULL ? path : fname + 1);
3350 strlcpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
3352 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
3353 (void *)¶m->lmd->lmd_lmm);
3359 if (errno == ENODATA && d != NULL) {
3360 /* We need to "fake" the "use the default" values
3361 * since the lmm struct is zeroed out at this point.
3362 * The magic needs to be set in order to satisfy
3363 * a check later on in the code path.
3364 * The object_seq needs to be set for the "(Default)"
3365 * prefix to be displayed. */
3366 if (param->get_default_lmv) {
3367 struct lmv_user_md *lum = param->fp_lmv_md;
3369 lum->lum_magic = LMV_USER_MAGIC;
3370 lum->lum_stripe_count = 0;
3371 lum->lum_stripe_offset = -1;
3373 } else if (param->get_lmv) {
3374 struct lmv_user_md *lum = param->fp_lmv_md;
3377 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
3380 lum->lum_magic = LMV_MAGIC_V1;
3381 lum->lum_stripe_count = 0;
3382 lum->lum_stripe_offset = mdtidx;
3385 struct lov_user_md *lmm = ¶m->lmd->lmd_lmm;
3386 lmm->lmm_magic = LOV_USER_MAGIC_V1;
3388 ostid_set_seq(&lmm->lmm_oi,
3389 FID_SEQ_LOV_DEFAULT);
3390 lmm->lmm_stripe_count = 0;
3391 lmm->lmm_stripe_size = 0;
3392 lmm->lmm_stripe_offset = -1;
3395 } else if (errno == ENODATA && parent != NULL) {
3396 if (!param->obduuid && !param->mdtuuid)
3397 llapi_printf(LLAPI_MSG_NORMAL,
3398 "%s has no stripe info\n", path);
3400 } else if (errno == ENOENT) {
3401 llapi_error(LLAPI_MSG_WARN, -ENOENT,
3402 "warning: %s: %s does not exist",
3405 } else if (errno == ENOTTY) {
3407 llapi_error(LLAPI_MSG_ERROR, ret,
3408 "%s: '%s' not on a Lustre fs?",
3413 llapi_error(LLAPI_MSG_ERROR, ret,
3414 "error: %s: %s failed for %s",
3415 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
3416 "IOC_MDC_GETFILESTRIPE", path);
3423 if (!(param->verbose & VERBOSE_MDTINDEX))
3424 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
3427 /* Do not get down anymore? */
3428 if (param->fp_depth == param->fp_max_depth)
3436 int llapi_getstripe(char *path, struct find_param *param)
3438 return param_callback(path, (param->verbose & VERBOSE_MDTINDEX) ?
3439 cb_get_mdt_index : cb_getstripe,
3440 cb_common_fini, param);
3443 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
3444 struct obd_statfs *stat_buf,
3445 struct obd_uuid *uuid_buf)
3448 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3450 struct obd_ioctl_data data = { 0 };
3453 data.ioc_inlbuf1 = (char *)&type;
3454 data.ioc_inllen1 = sizeof(__u32);
3455 data.ioc_inlbuf2 = (char *)&index;
3456 data.ioc_inllen2 = sizeof(__u32);
3457 data.ioc_pbuf1 = (char *)stat_buf;
3458 data.ioc_plen1 = sizeof(struct obd_statfs);
3459 data.ioc_pbuf2 = (char *)uuid_buf;
3460 data.ioc_plen2 = sizeof(struct obd_uuid);
3462 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3464 llapi_error(LLAPI_MSG_ERROR, rc,
3465 "llapi_obd_statfs: error packing ioctl data");
3469 fd = open(path, O_RDONLY);
3470 if (errno == EISDIR)
3471 fd = open(path, O_DIRECTORY | O_RDONLY);
3474 rc = errno ? -errno : -EBADF;
3475 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
3477 /* If we can't even open a file on the filesystem (e.g. with
3478 * -ESHUTDOWN), force caller to exit or it will loop forever. */
3481 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
3483 rc = errno ? -errno : -EINVAL;
3489 #define MAX_STRING_SIZE 128
3491 int llapi_ping(char *obd_type, char *obd_name)
3493 char path[MAX_STRING_SIZE];
3497 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
3498 obd_type, obd_name);
3500 fd = open(path, O_WRONLY);
3503 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
3507 /* The purpose is to send a byte as a ping, whatever this byte is. */
3508 /* coverity[uninit_use_in_call] */
3509 rc = write(fd, buf, 1);
3519 int llapi_target_iterate(int type_num, char **obd_type,
3520 void *args, llapi_cb_t cb)
3522 char buf[MAX_STRING_SIZE];
3523 FILE *fp = fopen(DEVICES_LIST, "r");
3528 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
3532 while (fgets(buf, sizeof(buf), fp) != NULL) {
3533 char *obd_type_name = NULL;
3534 char *obd_name = NULL;
3535 char *obd_uuid = NULL;
3537 struct obd_statfs osfs_buffer;
3539 while(bufp[0] == ' ')
3542 for(i = 0; i < 3; i++) {
3543 obd_type_name = strsep(&bufp, " ");
3545 obd_name = strsep(&bufp, " ");
3546 obd_uuid = strsep(&bufp, " ");
3548 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
3550 for (i = 0; i < type_num; i++) {
3551 if (strcmp(obd_type_name, obd_type[i]) != 0)
3554 cb(obd_type_name, obd_name, obd_uuid, args);
3561 static void do_target_check(char *obd_type_name, char *obd_name,
3562 char *obd_uuid, void *args)
3566 rc = llapi_ping(obd_type_name, obd_name);
3567 if (rc == ENOTCONN) {
3568 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
3570 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
3572 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
3576 int llapi_target_check(int type_num, char **obd_type, char *dir)
3578 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
3581 #undef MAX_STRING_SIZE
3583 /* Is this a lustre fs? */
3584 int llapi_is_lustre_mnttype(const char *type)
3586 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
3589 /* Is this a lustre client fs? */
3590 int llapi_is_lustre_mnt(struct mntent *mnt)
3592 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
3593 strstr(mnt->mnt_fsname, ":/") != NULL);
3596 int llapi_quotacheck(char *mnt, int check_type)
3601 root = opendir(mnt);
3604 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3608 rc = ioctl(dirfd(root), OBD_IOC_QUOTACHECK, check_type);
3616 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
3622 root = opendir(mnt);
3625 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3630 rc = ioctl(dirfd(root), OBD_IOC_POLL_QUOTACHECK, qchk);
3634 if (poll_intvl < 30)
3642 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3647 root = opendir(mnt);
3650 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3654 rc = ioctl(dirfd(root), OBD_IOC_QUOTACTL, qctl);
3662 static int cb_quotachown(char *path, DIR *parent, DIR **dirp, void *data,
3663 struct dirent64 *de)
3665 struct find_param *param = (struct find_param *)data;
3666 DIR *d = dirp == NULL ? NULL : *dirp;
3670 LASSERT(parent != NULL || d != NULL);
3672 rc = get_lmd_info(path, parent, d, param->lmd, param->lumlen);
3674 if (rc == -ENODATA) {
3675 if (!param->obduuid && !param->quiet)
3676 llapi_error(LLAPI_MSG_ERROR, -ENODATA,
3677 "%s has no stripe info", path);
3679 } else if (rc == -ENOENT) {
3685 st = ¶m->lmd->lmd_st;
3687 /* libc chown() will do extra check, and if the real owner is
3688 * the same as the ones to set, it won't fall into kernel, so
3689 * invoke syscall directly. */
3690 rc = syscall(SYS_chown, path, -1, -1);
3692 llapi_error(LLAPI_MSG_ERROR, errno,
3693 "error: chown %s", path);
3695 rc = chmod(path, st->st_mode);
3698 llapi_error(LLAPI_MSG_ERROR, rc, "error: chmod %s (%hu)",
3705 int llapi_quotachown(char *path, int flag)
3707 struct find_param param;
3709 memset(¶m, 0, sizeof(param));
3710 param.recursive = 1;
3714 return param_callback(path, cb_quotachown, NULL, ¶m);
3720 #include <sys/wait.h>
3724 static int rmtacl_notify(int ops)
3728 int found = 0, fd = 0, rc = 0;
3730 fp = setmntent(MOUNTED, "r");
3733 llapi_error(LLAPI_MSG_ERROR, rc,
3734 "error setmntent(%s)", MOUNTED);
3739 mnt = getmntent(fp);
3743 if (!llapi_is_lustre_mnt(mnt))
3746 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
3749 llapi_error(LLAPI_MSG_ERROR, rc,
3750 "Can't open '%s'", mnt->mnt_dir);
3754 rc = ioctl(fd, LL_IOC_RMTACL, ops);
3758 llapi_error(LLAPI_MSG_ERROR, rc,
3759 "ioctl RMTACL on '%s' err %d",
3769 return ((rc != 0) ? rc : found);
3772 static char *next_token(char *p, int div)
3778 while (*p && *p != ':' && !isspace(*p))
3781 while (*p == ':' || isspace(*p))
3784 return *p ? p : NULL;
3787 static int rmtacl_name2id(char *name, int is_user)
3792 pw = getpwnam(name);
3796 return (int)(pw->pw_uid);
3800 gr = getgrnam(name);
3804 return (int)(gr->gr_gid);
3808 static int isodigit(int c)
3810 return (c >= '0' && c <= '7') ? 1 : 0;
3814 * Whether the name is just digits string (uid/gid) already or not.
3819 static int str_is_id(char *str)
3826 if (*str == 'x' || *str == 'X') { /* for Hex. */
3827 if (!isxdigit(*(++str)))
3830 while (isxdigit(*(++str)));
3831 } else if (isodigit(*str)) { /* for Oct. */
3832 while (isodigit(*(++str)));
3834 } else if (isdigit(*str)) { /* for Dec. */
3835 while (isdigit(*(++str)));
3838 return (*str == 0) ? 1 : 0;
3848 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
3850 static rmtacl_name_t rmtacl_namelist[] = {
3851 { RMTACL_OPTNAME("user:"), 1, 0 },
3852 { RMTACL_OPTNAME("group:"), 0, 0 },
3853 { RMTACL_OPTNAME("default:user:"), 1, 0 },
3854 { RMTACL_OPTNAME("default:group:"), 0, 0 },
3855 /* for --tabular option */
3856 { RMTACL_OPTNAME("user"), 1, 1 },
3857 { RMTACL_OPTNAME("group"), 0, 1 },
3861 static int rgetfacl_output(char *str)
3863 char *start = NULL, *end = NULL;
3864 int is_user = 0, n, id;
3871 for (rn = rmtacl_namelist; rn->name; rn++) {
3872 if(strncmp(str, rn->name, rn->length) == 0) {
3873 if (!rn->next_token)
3874 start = str + rn->length;
3876 start = next_token(str + rn->length, 0);
3877 is_user = rn->is_user;
3882 end = next_token(start, 1);
3883 if (end == NULL || start == end) {
3884 n = printf("%s", str);
3890 id = rmtacl_name2id(start, is_user);
3891 if (id == INVALID_ID) {
3892 if (str_is_id(start)) {
3894 n = printf("%s", str);
3897 } else if ((id == NOBODY_UID && is_user) ||
3898 (id == NOBODY_GID && !is_user)) {
3900 n = printf("%s", str);
3904 n = printf("%s%d%s", str, id, end);
3909 static int child_status(int status)
3911 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
3914 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
3917 int fd[2], status, rc;
3924 llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe");
3931 llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork");
3936 /* child process redirects its output. */
3939 if (dup2(fd[1], 1) < 0) {
3941 llapi_error(LLAPI_MSG_ERROR, rc,
3942 "Can't dup2 %d", fd[1]);
3952 status = rmtacl_notify(ops);
3956 exit(execvp(argv[0], argv));
3959 /* the following is parent process */
3960 fp = fdopen(fd[0], "r");
3963 llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed", fd[0]);
3969 while (fgets(buf, PIPE_BUF, fp) != NULL) {
3970 if (output_func(buf) < 0)
3971 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
3977 if (waitpid(pid, &status, 0) < 0) {
3979 llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed", pid);
3983 return child_status(status);
3986 int llapi_lsetfacl(int argc, char *argv[])
3988 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
3991 int llapi_lgetfacl(int argc, char *argv[])
3993 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
3996 int llapi_rsetfacl(int argc, char *argv[])
3998 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
4001 int llapi_rgetfacl(int argc, char *argv[])
4003 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
4006 int llapi_cp(int argc, char *argv[])
4010 rc = rmtacl_notify(RMT_RSETFACL);
4014 exit(execvp(argv[0], argv));
4017 int llapi_ls(int argc, char *argv[])
4021 rc = rmtacl_notify(RMT_LGETFACL);
4025 exit(execvp(argv[0], argv));
4028 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
4029 * format must have %s%s, buf must be > 16
4030 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
4031 * then buf = "lustre-MDT0000"
4033 static int get_mdtname(char *name, char *format, char *buf)
4035 char suffix[]="-MDT0000";
4036 int len = strlen(name);
4038 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
4039 name[len - 5] = '\0';
4044 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
4047 /* Not enough room to add suffix */
4048 llapi_err_noerrno(LLAPI_MSG_ERROR,
4049 "MDT name too long |%s|", name);
4054 return sprintf(buf, format, name, suffix);
4057 /** ioctl on filsystem root, with mdtindex sent as data
4058 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
4059 * \param mdtidxp pointer to integer within data to be filled in with the
4060 * mdt index (0 if no mdt is specified). NULL won't be filled.
4062 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
4070 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
4071 Open root and parse mdt index. */
4072 if (mdtname[0] == '/') {
4074 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
4075 (char *)mdtname, -1);
4077 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
4079 ptr = fsname + strlen(fsname) - 8;
4081 index = strtol(ptr + 4, NULL, 10);
4082 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
4086 llapi_err_noerrno(LLAPI_MSG_ERROR,
4087 "Can't open %s: %d\n", mdtname, rc);
4094 rc = ioctl(fd, opc, data);
4099 if (rc && want_error)
4100 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
4106 /****** Changelog API ********/
4108 static int changelog_ioctl(const char *mdtname, int opc, int id,
4109 long long recno, int flags)
4111 struct ioc_changelog data;
4115 data.icc_recno = recno;
4116 data.icc_flags = flags;
4117 idx = (int *)(&data.icc_mdtindex);
4119 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
4122 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
4123 struct changelog_private {
4126 lustre_kernelcomm kuc;
4129 /** Start reading from a changelog
4130 * @param priv Opaque private control structure
4131 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
4132 * @param device Report changes recorded on this MDT
4133 * @param startrec Report changes beginning with this record number
4134 * (just call llapi_changelog_fini when done; don't need an endrec)
4136 int llapi_changelog_start(void **priv, int flags, const char *device,
4139 struct changelog_private *cp;
4142 /* Set up the receiver control struct */
4143 cp = calloc(1, sizeof(*cp));
4147 cp->magic = CHANGELOG_PRIV_MAGIC;
4150 /* Set up the receiver */
4151 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */, 0);
4157 /* Tell the kernel to start sending */
4158 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
4160 /* Only the kernel reference keeps the write side open */
4161 close(cp->kuc.lk_wfd);
4162 cp->kuc.lk_wfd = LK_NOFD;
4164 /* frees and clears priv */
4165 llapi_changelog_fini(priv);
4176 /** Finish reading from a changelog */
4177 int llapi_changelog_fini(void **priv)
4179 struct changelog_private *cp = (struct changelog_private *)*priv;
4181 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4184 libcfs_ukuc_stop(&cp->kuc);
4190 /** Convert a changelog_rec to changelog_ext_rec, in this way client can treat
4191 * all records in the format of changelog_ext_rec, this can make record
4194 static inline int changelog_extend_rec(struct changelog_ext_rec *ext)
4196 if (!CHANGELOG_REC_EXTENDED(ext)) {
4197 struct changelog_rec *rec = (struct changelog_rec *)ext;
4199 memmove(ext->cr_name, rec->cr_name, rec->cr_namelen);
4200 fid_zero(&ext->cr_sfid);
4201 fid_zero(&ext->cr_spfid);
4208 /** Read the next changelog entry
4209 * @param priv Opaque private control structure
4210 * @param rech Changelog record handle; record will be allocated here
4211 * @return 0 valid message received; rec is set
4215 int llapi_changelog_recv(void *priv, struct changelog_ext_rec **rech)
4217 struct changelog_private *cp = (struct changelog_private *)priv;
4218 struct kuc_hdr *kuch;
4221 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
4225 kuch = malloc(KUC_CHANGELOG_MSG_MAXSIZE);
4230 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
4231 KUC_CHANGELOG_MSG_MAXSIZE,
4232 KUC_TRANSPORT_CHANGELOG);
4236 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
4237 ((kuch->kuc_msgtype != CL_RECORD) &&
4238 (kuch->kuc_msgtype != CL_EOF))) {
4239 llapi_err_noerrno(LLAPI_MSG_ERROR,
4240 "Unknown changelog message type %d:%d\n",
4241 kuch->kuc_transport, kuch->kuc_msgtype);
4246 if (kuch->kuc_msgtype == CL_EOF) {
4247 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
4256 /* Our message is a changelog_ext_rec. Use pointer math to skip
4257 * kuch_hdr and point directly to the message payload.
4259 *rech = (struct changelog_ext_rec *)(kuch + 1);
4260 changelog_extend_rec(*rech);
4270 /** Release the changelog record when done with it. */
4271 int llapi_changelog_free(struct changelog_ext_rec **rech)
4274 /* We allocated memory starting at the kuc_hdr, but passed
4275 * the consumer a pointer to the payload.
4276 * Use pointer math to get back to the header.
4278 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
4285 int llapi_changelog_clear(const char *mdtname, const char *idstr,
4291 llapi_err_noerrno(LLAPI_MSG_ERROR,
4292 "can't purge negative records\n");
4296 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
4297 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
4298 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
4299 llapi_err_noerrno(LLAPI_MSG_ERROR,
4300 "expecting id of the form '"
4301 CHANGELOG_USER_PREFIX
4302 "<num>'; got '%s'\n", idstr);
4306 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
4309 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
4310 int buflen, long long *recno, int *linkno)
4313 struct getinfo_fid2path *gf;
4316 while (*fidstr == '[')
4319 sscanf(fidstr, SFID, RFID(&fid));
4320 if (!fid_is_sane(&fid)) {
4321 llapi_err_noerrno(LLAPI_MSG_ERROR,
4322 "bad FID format [%s], should be "DFID"\n",
4323 fidstr, (__u64)1, 2, 0);
4327 gf = malloc(sizeof(*gf) + buflen);
4331 gf->gf_recno = *recno;
4332 gf->gf_linkno = *linkno;
4333 gf->gf_pathlen = buflen;
4335 /* Take path or fsname */
4336 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
4339 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
4341 memcpy(buf, gf->gf_path, gf->gf_pathlen);
4342 if (buf[0] == '\0') { /* ROOT path */
4346 *recno = gf->gf_recno;
4347 *linkno = gf->gf_linkno;
4354 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
4357 struct lustre_mdt_attrs *lma;
4361 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
4363 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
4366 lma = (struct lustre_mdt_attrs *)buf;
4367 fid_le_to_cpu(fid, &lma->lma_self_fid);
4371 int llapi_get_mdt_index_by_fid(int fd, const lustre_fid *fid,
4376 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
4385 int llapi_fd2fid(const int fd, lustre_fid *fid)
4389 memset(fid, 0, sizeof(*fid));
4391 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
4392 if (rc == -EINVAL || rc == -ENOTTY)
4393 rc = fid_from_lma(NULL, fd, fid);
4398 int llapi_path2fid(const char *path, lustre_fid *fid)
4402 memset(fid, 0, sizeof(*fid));
4403 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4405 if (errno == ELOOP || errno == ENXIO)
4406 return fid_from_lma(path, -1, fid);
4410 rc = llapi_fd2fid(fd, fid);
4411 if (rc == -EINVAL || rc == -ENOTTY)
4412 rc = fid_from_lma(path, -1, fid);
4418 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4423 root = opendir(mnt);
4426 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4430 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
4433 llapi_error(LLAPI_MSG_ERROR, rc,
4434 "ioctl on %s for getting connect flags failed", mnt);
4440 int llapi_get_version(char *buffer, int buffer_size,
4445 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
4447 fd = open(OBD_DEV_PATH, O_RDONLY);
4451 memset(buffer, 0, buffer_size);
4452 data->ioc_version = OBD_IOCTL_VERSION;
4453 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
4454 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
4455 data->ioc_len = obd_ioctl_packlen(data);
4457 rc = ioctl(fd, OBD_GET_VERSION, buffer);
4464 *version = data->ioc_bulk;
4469 * Get a 64-bit value representing the version of file data pointed by fd.
4471 * Each write or truncate, flushed on OST, will change this value. You can use
4472 * this value to verify if file data was modified. This only checks the file
4473 * data, not metadata.
4475 * \param flags 0: no flush pages, usually used it the process has already
4477 * LL_DV_RD_FLUSH: OSTs will take LCK_PR to flush dirty pages
4479 * LL_DV_WR_FLUSH: OSTs will take LCK_PW to flush all caching
4480 * pages from clients.
4482 * \retval 0 on success.
4483 * \retval -errno on error.
4485 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4488 struct ioc_data_version idv;
4490 idv.idv_flags = flags;
4492 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4496 *data_version = idv.idv_version;
4502 * Create a file without any name open it for read/write
4504 * - file is created as if it were a standard file in the given \a directory
4505 * - file does not appear in \a directory and mtime does not change because
4506 * the filename is handled specially by the Lustre MDS.
4507 * - file is removed at final close
4508 * - file modes are rw------- since it doesn't make sense to have a read-only
4509 * or write-only file that cannot be opened again.
4510 * - if user wants another mode it must use fchmod() on the open file, no
4511 * security problems arise because it cannot be opened by another process.
4513 * \param[in] directory directory from which to inherit layout/MDT idx
4514 * \param[in] idx MDT index on which the file is created,
4515 * \a idx == -1 means no specific MDT is requested
4516 * \param[in] open_flags standard open(2) flags
4518 * \retval 0 on success.
4519 * \retval -errno on error.
4521 int llapi_create_volatile_idx(char *directory, int idx, int open_flags)
4523 char file_path[PATH_MAX];
4524 char filename[PATH_MAX];
4529 fd = open("/dev/urandom", O_RDONLY);
4531 llapi_error(LLAPI_MSG_ERROR, errno,
4532 "Cannot open /dev/urandom");
4535 rc = read(fd, &random, sizeof(random));
4537 if (rc < sizeof(random)) {
4538 llapi_error(LLAPI_MSG_ERROR, errno,
4539 "cannot read %zu bytes from /dev/urandom",
4544 snprintf(filename, sizeof(filename),
4545 LUSTRE_VOLATILE_HDR"::%.4X", random);
4547 snprintf(filename, sizeof(filename),
4548 LUSTRE_VOLATILE_HDR":%.4X:%.4X", idx, random);
4550 rc = snprintf(file_path, sizeof(file_path),
4551 "%s/%s", directory, filename);
4552 if (rc >= sizeof(file_path))
4555 fd = open(file_path, O_RDWR | O_CREAT | open_flags, S_IRUSR | S_IWUSR);
4557 llapi_error(LLAPI_MSG_ERROR, errno,
4558 "Cannot create volatile file '%s' in '%s'",
4559 filename + LUSTRE_VOLATILE_HDR_LEN,
4563 /* unlink file in case this wasn't a Lustre filesystem, and the
4564 * magic volatile filename wasn't handled as intended. The effect
4572 * Swap the layouts between 2 file descriptors
4573 * the 2 files must be open in write
4574 * first fd received the ioctl, second fd is passed as arg
4575 * this is assymetric but avoid use of root path for ioctl
4577 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4579 struct lustre_swap_layouts lsl;
4582 srandom(time(NULL));
4584 lsl.sl_flags = flags;
4585 lsl.sl_gid = random();
4588 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4595 * Swap the layouts between 2 files
4596 * the 2 files are open in write
4598 int llapi_swap_layouts(const char *path1, const char *path2,
4599 __u64 dv1, __u64 dv2, __u64 flags)
4603 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4606 llapi_error(LLAPI_MSG_ERROR, rc,
4607 "error: cannot open '%s' for write", path1);
4611 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4614 llapi_error(LLAPI_MSG_ERROR, rc,
4615 "error: cannot open '%s' for write", path2);
4619 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4621 llapi_error(LLAPI_MSG_ERROR, rc,
4622 "error: cannot swap layout between '%s' and '%s'",
4633 * Attempt to open a file with Lustre file identifier \a fid
4634 * and return an open file descriptor.
4636 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
4637 * \param[in] fid Lustre file identifier of file to open
4638 * \param[in] flags open() flags
4640 * \retval non-negative file descriptor on successful open
4641 * \retval -1 if an error occurred
4643 int llapi_open_by_fid(const char *lustre_dir, const lustre_fid *fid, int flags)
4645 char mntdir[PATH_MAX];
4646 char path[PATH_MAX];
4649 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
4653 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
4654 return open(path, flags);
4660 * \param fd File to lock.
4661 * \param gid Group Identifier.
4663 * \retval 0 on success.
4664 * \retval -errno on failure.
4666 int llapi_group_lock(int fd, int gid)
4670 rc = ioctl(fd, LL_IOC_GROUP_LOCK, gid);
4673 llapi_error(LLAPI_MSG_ERROR, rc, "cannot get group lock");
4681 * \param fd File to unlock.
4682 * \param gid Group Identifier.
4684 * \retval 0 on success.
4685 * \retval -errno on failure.
4687 int llapi_group_unlock(int fd, int gid)
4691 rc = ioctl(fd, LL_IOC_GROUP_UNLOCK, gid);
4694 llapi_error(LLAPI_MSG_ERROR, rc, "cannot put group lock");