4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/liblustreapi.c
38 * Author: Peter J. Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
40 * Author: Robert Read <rread@clusterfs.com>
52 #include <sys/ioctl.h>
59 #include <sys/types.h>
60 #include <sys/syscall.h>
61 #include <sys/xattr.h>
64 #include <libgen.h> /* for dirname() */
65 #ifdef HAVE_LINUX_UNISTD_H
66 #include <linux/unistd.h>
72 #include <liblustre.h>
73 #include <lnet/lnetctl.h>
76 #include <lustre/lustreapi.h>
77 #include "lustreapi_internal.h"
79 static unsigned llapi_dir_filetype_table[] = {
88 #if defined(DT_DOOR) && defined(S_IFDOOR)
93 #if defined(DT_DOOR) && defined(S_IFDOOR)
94 static const int DT_MAX = DT_DOOR;
96 static const int DT_MAX = DT_SOCK;
99 static unsigned llapi_filetype_dir_table[] = {
108 #if defined(DT_DOOR) && defined(S_IFDOOR)
113 #if defined(DT_DOOR) && defined(S_IFDOOR)
114 static const int S_IFMAX = DT_DOOR;
116 static const int S_IFMAX = DT_SOCK;
119 /* liblustreapi message level */
120 static int llapi_msg_level = LLAPI_MSG_MAX;
122 void llapi_msg_set_level(int level)
124 /* ensure level is in the good range */
125 if (level < LLAPI_MSG_OFF)
126 llapi_msg_level = LLAPI_MSG_OFF;
127 else if (level > LLAPI_MSG_MAX)
128 llapi_msg_level = LLAPI_MSG_MAX;
130 llapi_msg_level = level;
133 /* llapi_error will preserve errno */
134 void llapi_error(int level, int _rc, char *fmt, ...)
137 int tmp_errno = errno;
138 /* to protect using errno as _rc argument */
141 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
145 vfprintf(stderr, fmt, args);
148 if (level & LLAPI_MSG_NO_ERRNO)
149 fprintf(stderr, "\n");
151 fprintf(stderr, ": %s (%d)\n", strerror(rc), rc);
155 /* llapi_printf will preserve errno */
156 void llapi_printf(int level, char *fmt, ...)
159 int tmp_errno = errno;
161 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
165 vfprintf(stdout, fmt, args);
171 * size_units is to be initialized (or zeroed) by caller.
173 int parse_size(char *optarg, unsigned long long *size,
174 unsigned long long *size_units, int bytes_spec)
178 if (strncmp(optarg, "-", 1) == 0)
181 if (*size_units == 0)
184 *size = strtoull(optarg, &end, 0);
187 if ((*end == 'b') && *(end + 1) == '\0' &&
188 (*size & (~0ULL << (64 - 9))) == 0 &&
190 *size_units = 1 << 9;
191 } else if ((*end == 'b') &&
192 *(end + 1) == '\0' &&
195 } else if ((*end == 'k' || *end == 'K') &&
196 *(end + 1) == '\0' &&
197 (*size & (~0ULL << (64 - 10))) == 0) {
198 *size_units = 1 << 10;
199 } else if ((*end == 'm' || *end == 'M') &&
200 *(end + 1) == '\0' &&
201 (*size & (~0ULL << (64 - 20))) == 0) {
202 *size_units = 1 << 20;
203 } else if ((*end == 'g' || *end == 'G') &&
204 *(end + 1) == '\0' &&
205 (*size & (~0ULL << (64 - 30))) == 0) {
206 *size_units = 1 << 30;
207 } else if ((*end == 't' || *end == 'T') &&
208 *(end + 1) == '\0' &&
209 (*size & (~0ULL << (64 - 40))) == 0) {
210 *size_units = 1ULL << 40;
211 } else if ((*end == 'p' || *end == 'P') &&
212 *(end + 1) == '\0' &&
213 (*size & (~0ULL << (64 - 50))) == 0) {
214 *size_units = 1ULL << 50;
215 } else if ((*end == 'e' || *end == 'E') &&
216 *(end + 1) == '\0' &&
217 (*size & (~0ULL << (64 - 60))) == 0) {
218 *size_units = 1ULL << 60;
223 *size *= *size_units;
227 /* XXX: llapi_xxx() functions return negative values upon failure */
229 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
230 int stripe_count, int stripe_pattern)
234 /* 64 KB is the largest common page size I'm aware of (on ia64), but
235 * check the local page size just in case. */
236 page_size = LOV_MIN_STRIPE_SIZE;
237 if (getpagesize() > page_size) {
238 page_size = getpagesize();
239 llapi_err_noerrno(LLAPI_MSG_WARN,
240 "warning: your page size (%u) is "
241 "larger than expected (%u)", page_size,
242 LOV_MIN_STRIPE_SIZE);
244 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
246 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %lu, "
247 "must be an even multiple of %d bytes",
248 stripe_size, page_size);
251 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
253 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
257 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
259 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
263 if (stripe_size >= (1ULL << 32)) {
265 llapi_error(LLAPI_MSG_ERROR, rc,
266 "warning: stripe size 4G or larger "
267 "is not currently supported and would wrap");
273 /* return the first file matching this pattern */
274 static int first_match(char *pattern, char *buffer)
278 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
281 if (glob_info.gl_pathc < 1) {
282 globfree(&glob_info);
286 strcpy(buffer, glob_info.gl_pathv[0]);
288 globfree(&glob_info);
292 static int find_target_obdpath(char *fsname, char *path)
295 char pattern[PATH_MAX + 1];
298 snprintf(pattern, PATH_MAX,
299 "/proc/fs/lustre/lov/%s-*/target_obd",
301 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
302 if (rc == GLOB_NOMATCH)
307 strcpy(path, glob_info.gl_pathv[0]);
308 globfree(&glob_info);
312 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
315 char pattern[PATH_MAX + 1];
318 snprintf(pattern, PATH_MAX,
319 "/proc/fs/lustre/lov/%s-*/pools/%s",
321 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
322 /* If no pools, make sure the lov is available */
323 if ((rc == GLOB_NOMATCH) &&
324 (find_target_obdpath(fsname, poolpath) == -ENODEV))
329 strcpy(poolpath, glob_info.gl_pathv[0]);
330 globfree(&glob_info);
335 * return a parameter string for a specific device type or mountpoint
337 * \param param_path the path to the file containing parameter data
338 * \param result buffer for parameter value string
339 * \param result_size size of buffer for return value
341 * The \param param_path is appended to /proc/{fs,sys}/{lnet,lustre} to
342 * complete the absolute path to the file containing the parameter data
343 * the user is requesting. If that file exist then the data is read from
344 * the file and placed into the \param result buffer that is passed by
345 * the user. Data is only copied up to the \param result_size to prevent
346 * overflow of the array.
348 * Return 0 for success, with a NUL-terminated string in \param result.
349 * Return -ve value for error.
351 static int get_param(const char *param_path, char *result,
352 unsigned int result_size)
354 char file[PATH_MAX + 1], pattern[PATH_MAX + 1], buf[result_size];
358 snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
360 rc = first_match(pattern, file);
364 fp = fopen(file, "r");
366 while (fgets(buf, result_size, fp) != NULL)
375 #define DEVICES_LIST "/proc/fs/lustre/devices"
378 * return a parameter string for a specific device type or mountpoint
380 * \param fsname Lustre filesystem name (optional)
381 * \param file_path path to file in filesystem (optional, if fsname unset)
382 * \param obd_type Lustre OBD device type
383 * \param param_name parameter name to fetch
384 * \param value return buffer for parameter value string
385 * \param val_len size of buffer for return value
387 * If fsname is specified then the parameter will be from that filesystem
388 * (if it exists). If file_path is given and it is in a mounted Lustre
389 * filesystem, then the parameter will be otherwise the value may be
390 * from any mounted filesystem (if there is more than one).
392 * If "obd_type" matches a Lustre device then the first matching device
393 * (as with "lctl dl", constrained by \param fsname or \param mount_path)
394 * will be used to provide the return value, otherwise the first such
395 * device found will be used.
397 * Return 0 for success, with a NUL-terminated string in \param buffer.
398 * Return -ve value for error.
400 static int get_param_obdvar(const char *fsname, const char *file_path,
401 const char *obd_type, const char *param_name,
402 char *value, unsigned int val_len)
404 char devices[PATH_MAX + 1], dev[PATH_MAX + 1] = "*", fs[PATH_MAX + 1];
405 FILE *fp = fopen(DEVICES_LIST, "r");
408 if (!fsname && file_path) {
409 rc = llapi_search_fsname(file_path, fs);
411 llapi_error(LLAPI_MSG_ERROR, rc,
412 "'%s' is not on a Lustre filesystem",
424 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
428 while (fgets(devices, sizeof(devices), fp) != NULL) {
429 char *bufp = devices, *tmp;
431 while (bufp[0] == ' ')
434 tmp = strstr(bufp, obd_type);
436 tmp += strlen(obd_type) + 1;
440 tmp = strchr(dev, ' ');
446 if (dev[0] == '*' && strlen(fs))
447 snprintf(dev, PATH_MAX, "%s-*", fs);
448 snprintf(devices, PATH_MAX, "%s/%s/%s", obd_type, dev, param_name);
450 return get_param(devices, value, val_len);
454 * TYPE one of llite, lmv, lov.
455 * /proc/fs/lustre/TYPE/INST the directory of interest.
457 static int get_param_cli(const char *type, const char *inst,
458 const char *param, char *buf, size_t buf_size)
460 char param_path[PATH_MAX + 1];
461 FILE *param_file = NULL;
464 snprintf(param_path, sizeof(param_path),
465 "/proc/fs/lustre/%s/%s/%s", type, inst, param);
467 param_file = fopen(param_path, "r");
468 if (param_file == NULL) {
473 if (fgets(buf, buf_size, param_file) == NULL) {
480 if (param_file != NULL)
486 static int get_param_llite(const char *path,
487 const char *param, char *buf, size_t buf_size)
492 rc = llapi_getname(path, inst, sizeof(inst));
496 return get_param_cli("llite", inst, param, buf, buf_size);
499 static int get_param_lov(const char *path,
500 const char *param, char *buf, size_t buf_size)
502 struct obd_uuid uuid;
505 rc = llapi_file_get_lov_uuid(path, &uuid);
509 return get_param_cli("lov", uuid.uuid, param, buf, buf_size);
512 static int get_param_lmv(const char *path,
513 const char *param, char *buf, size_t buf_size)
515 struct obd_uuid uuid;
518 rc = llapi_file_get_lmv_uuid(path, &uuid);
522 return get_param_cli("lmv", uuid.uuid, param, buf, buf_size);
525 static int get_mds_md_size(const char *path)
527 int md_size = lov_mds_md_size(LOV_MAX_STRIPE_COUNT, LOV_MAGIC_V3);
531 /* Get the max ea size from llite proc. */
532 rc = get_param_llite(path, "max_easize", buf, sizeof(buf));
545 * if pool is NULL, search ostname in target_obd
546 * if pool is not NULL:
547 * if pool not found returns errno < 0
548 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
549 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
551 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
554 char buffer[PATH_MAX + 1];
558 len = strlen(ostname);
560 if (poolname == NULL)
561 rc = find_target_obdpath(fsname, buffer);
563 rc = find_poolpath(fsname, poolname, buffer);
567 fd = fopen(buffer, "r");
571 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
572 if (poolname == NULL) {
574 /* Search for an ostname in the list of OSTs
575 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
576 ptr = strchr(buffer, ' ');
578 (strncmp(ptr + 1, ostname, len) == 0)) {
583 /* Search for an ostname in a pool,
584 (or an existing non-empty pool if no ostname) */
585 if ((ostname == NULL) ||
586 (strncmp(buffer, ostname, len) == 0)) {
596 int llapi_file_open_pool(const char *name, int flags, int mode,
597 unsigned long long stripe_size, int stripe_offset,
598 int stripe_count, int stripe_pattern, char *pool_name)
600 struct lov_user_md_v3 lum = { 0 };
604 /* Make sure we have a good pool */
605 if (pool_name != NULL) {
606 char fsname[MAX_OBD_NAME + 1], *ptr;
608 rc = llapi_search_fsname(name, fsname);
610 llapi_error(LLAPI_MSG_ERROR, rc,
611 "'%s' is not on a Lustre filesystem",
616 /* in case user gives the full pool name <fsname>.<poolname>,
617 * strip the fsname */
618 ptr = strchr(pool_name, '.');
621 if (strcmp(pool_name, fsname) != 0) {
623 llapi_err_noerrno(LLAPI_MSG_ERROR,
624 "Pool '%s' is not on filesystem '%s'",
631 /* Make sure the pool exists and is non-empty */
632 rc = llapi_search_ost(fsname, pool_name, NULL);
634 llapi_err_noerrno(LLAPI_MSG_ERROR,
635 "pool '%s.%s' %s", fsname, pool_name,
636 rc == 0 ? "has no OSTs" : "does not exist");
641 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
642 if (fd < 0 && errno == EISDIR) {
643 fd = open(name, O_DIRECTORY | O_RDONLY);
649 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
653 rc = llapi_stripe_limit_check(stripe_size, stripe_offset, stripe_count,
658 /* Initialize IOCTL striping pattern structure */
659 lum.lmm_magic = LOV_USER_MAGIC_V3;
660 lum.lmm_pattern = stripe_pattern;
661 lum.lmm_stripe_size = stripe_size;
662 lum.lmm_stripe_count = stripe_count;
663 lum.lmm_stripe_offset = stripe_offset;
664 if (pool_name != NULL) {
665 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
667 /* If no pool is specified at all, use V1 request */
668 lum.lmm_magic = LOV_USER_MAGIC_V1;
671 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
672 char *errmsg = "stripe already set";
674 if (errno != EEXIST && errno != EALREADY)
675 errmsg = strerror(errno);
677 llapi_err_noerrno(LLAPI_MSG_ERROR,
678 "error on ioctl "LPX64" for '%s' (%d): %s",
679 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
690 int llapi_file_open(const char *name, int flags, int mode,
691 unsigned long long stripe_size, int stripe_offset,
692 int stripe_count, int stripe_pattern)
694 return llapi_file_open_pool(name, flags, mode, stripe_size,
695 stripe_offset, stripe_count,
696 stripe_pattern, NULL);
699 int llapi_file_create(const char *name, unsigned long long stripe_size,
700 int stripe_offset, int stripe_count, int stripe_pattern)
704 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
705 stripe_offset, stripe_count, stripe_pattern,
714 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
715 int stripe_offset, int stripe_count,
716 int stripe_pattern, char *pool_name)
720 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
721 stripe_offset, stripe_count, stripe_pattern,
731 * In DNE phase I, only stripe_offset will be used in this function.
732 * stripe_count, stripe_pattern and pool_name will be supported later.
734 int llapi_dir_create_pool(const char *name, int flags, int stripe_offset,
735 int stripe_count, int stripe_pattern, char *pool_name)
737 struct lmv_user_md lmu = { 0 };
738 struct obd_ioctl_data data = { 0 };
741 char *dirpath = NULL;
742 char *namepath = NULL;
748 dirpath = strdup(name);
749 namepath = strdup(name);
750 if (!dirpath || !namepath)
753 lmu.lum_magic = LMV_USER_MAGIC;
754 lmu.lum_stripe_offset = stripe_offset;
755 lmu.lum_stripe_count = stripe_count;
756 lmu.lum_hash_type = stripe_pattern;
757 if (pool_name != NULL) {
758 if (strlen(pool_name) >= LOV_MAXPOOLNAME) {
759 llapi_err_noerrno(LLAPI_MSG_ERROR,
760 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
761 "pool name: %s", name, pool_name);
762 GOTO(out, rc = -E2BIG);
764 memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
767 filename = basename(namepath);
768 lmu.lum_type = LMV_STRIPE_TYPE;
769 dir = dirname(dirpath);
771 data.ioc_inlbuf1 = (char *)filename;
772 data.ioc_inllen1 = strlen(filename) + 1;
773 data.ioc_inlbuf2 = (char *)&lmu;
774 data.ioc_inllen2 = sizeof(struct lmv_user_md);
775 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
777 llapi_error(LLAPI_MSG_ERROR, rc,
778 "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
783 fd = open(dir, O_DIRECTORY | O_RDONLY);
786 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
790 if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
791 char *errmsg = "stripe already set";
793 if (errno != EEXIST && errno != EALREADY)
794 errmsg = strerror(errno);
796 llapi_err_noerrno(LLAPI_MSG_ERROR,
797 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
807 int llapi_direntry_remove(char *dname)
809 char *dirpath = NULL;
810 char *namepath = NULL;
816 dirpath = strdup(dname);
817 namepath = strdup(dname);
818 if (!dirpath || !namepath)
821 filename = basename(namepath);
823 dir = dirname(dirpath);
825 fd = open(dir, O_DIRECTORY | O_RDONLY);
828 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
833 if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
834 char *errmsg = strerror(errno);
835 llapi_err_noerrno(LLAPI_MSG_ERROR,
836 "error on ioctl "LPX64" for '%s' (%d): %s",
837 (__u64)LL_IOC_LMV_SETSTRIPE, filename,
849 * Find the fsname, the full path, and/or an open fd.
850 * Either the fsname or path must not be NULL
852 int get_root_path(int want, char *fsname, int *outfd, char *path, int index)
855 char buf[PATH_MAX], mntdir[PATH_MAX];
858 int idx = 0, len = 0, mntlen, fd;
861 /* get the mount point */
862 fp = setmntent(MOUNTED, "r");
865 llapi_error(LLAPI_MSG_ERROR, rc,
866 "setmntent(%s) failed", MOUNTED);
870 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
873 if (!llapi_is_lustre_mnt(&mnt))
876 if ((want & WANT_INDEX) && (idx++ != index))
879 mntlen = strlen(mnt.mnt_dir);
880 ptr = strrchr(mnt.mnt_fsname, '/');
881 /* thanks to the call to llapi_is_lustre_mnt() above,
882 * we are sure that mnt.mnt_fsname contains ":/",
883 * so ptr should never be NULL */
888 /* Check the fsname for a match, if given */
889 if (!(want & WANT_FSNAME) && fsname != NULL &&
890 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
893 /* If the path isn't set return the first one we find */
894 if (path == NULL || strlen(path) == 0) {
895 strcpy(mntdir, mnt.mnt_dir);
896 if ((want & WANT_FSNAME) && fsname != NULL)
900 /* Otherwise find the longest matching path */
901 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
902 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
903 strcpy(mntdir, mnt.mnt_dir);
905 if ((want & WANT_FSNAME) && fsname != NULL)
914 if ((want & WANT_PATH) && path != NULL)
915 strcpy(path, mntdir);
916 if (want & WANT_FD) {
917 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
920 llapi_error(LLAPI_MSG_ERROR, rc,
921 "error opening '%s'", mntdir);
927 } else if (want & WANT_ERROR)
928 llapi_err_noerrno(LLAPI_MSG_ERROR,
929 "can't find fs root for '%s': %d",
930 (want & WANT_PATH) ? fsname : path, rc);
935 * search lustre mounts
937 * Calling this function will return to the user the mount point, mntdir, and
938 * the file system name, fsname, if the user passed a buffer to this routine.
940 * The user inputs are pathname and index. If the pathname is supplied then
941 * the value of the index will be ignored. The pathname will return data if
942 * the pathname is located on a lustre mount. Index is used to pick which
943 * mount point you want in the case of multiple mounted lustre file systems.
944 * See function lfs_osts in lfs.c for a example of the index use.
946 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
949 int want = WANT_PATH, idx = -1;
951 if (!pathname || pathname[0] == '\0') {
955 strcpy(mntdir, pathname);
959 return get_root_path(want, fsname, NULL, mntdir, idx);
962 /* Given a path, find the corresponding Lustre fsname */
963 int llapi_search_fsname(const char *pathname, char *fsname)
968 path = realpath(pathname, NULL);
970 char buf[PATH_MAX + 1], *ptr;
973 if (pathname[0] != '/') {
974 /* Need an absolute path, but realpath() only works for
975 * pathnames that actually exist. We go through the
976 * extra hurdle of dirname(getcwd() + pathname) in
977 * case the relative pathname contains ".." in it. */
978 if (getcwd(buf, sizeof(buf) - 1) == NULL)
982 strncat(buf, pathname, sizeof(buf) - strlen(buf));
983 path = realpath(buf, NULL);
985 ptr = strrchr(buf, '/');
989 path = realpath(buf, NULL);
992 llapi_error(LLAPI_MSG_ERROR, rc,
993 "pathname '%s' cannot expand",
999 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
1004 int llapi_search_rootpath(char *pathname, const char *fsname)
1006 return get_root_path(WANT_PATH, (char *)fsname, NULL, pathname, -1);
1009 int llapi_getname(const char *path, char *buf, size_t size)
1011 struct obd_uuid uuid_buf;
1012 char *uuid = uuid_buf.uuid;
1015 memset(&uuid_buf, 0, sizeof(uuid_buf));
1016 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
1020 /* We want to turn lustre-clilov-ffff88002738bc00 into
1021 * lustre-ffff88002738bc00. */
1023 nr = snprintf(buf, size, "%.*s-%s",
1024 (int) (strlen(uuid) - 24), uuid,
1025 uuid + strlen(uuid) - 16);
1035 * find the pool directory path under /proc
1036 * (can be also used to test if a fsname is known)
1038 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
1041 char pattern[PATH_MAX + 1];
1042 char buffer[PATH_MAX];
1044 if (fsname == NULL) {
1045 rc = llapi_search_fsname(pathname, buffer);
1049 strcpy(pathname, fsname);
1052 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
1053 rc = first_match(pattern, buffer);
1057 /* in fsname test mode, pool_pathname is NULL */
1058 if (pool_pathname != NULL)
1059 strcpy(pool_pathname, buffer);
1065 * Get the list of pool members.
1066 * \param poolname string of format \<fsname\>.\<poolname\>
1067 * \param members caller-allocated array of char*
1068 * \param list_size size of the members array
1069 * \param buffer caller-allocated buffer for storing OST names
1070 * \param buffer_size size of the buffer
1072 * \return number of members retrieved for this pool
1073 * \retval -error failure
1075 int llapi_get_poolmembers(const char *poolname, char **members,
1076 int list_size, char *buffer, int buffer_size)
1078 char fsname[PATH_MAX + 1];
1080 char pathname[PATH_MAX + 1];
1081 char path[PATH_MAX + 1];
1088 /* name is FSNAME.POOLNAME */
1089 if (strlen(poolname) > PATH_MAX)
1091 strcpy(fsname, poolname);
1092 pool = strchr(fsname, '.');
1099 rc = poolpath(fsname, NULL, pathname);
1101 llapi_error(LLAPI_MSG_ERROR, rc,
1102 "Lustre filesystem '%s' not found",
1107 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
1108 sprintf(path, "%s/%s", pathname, pool);
1109 fd = fopen(path, "r");
1112 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
1117 while (fgets(buf, sizeof(buf), fd) != NULL) {
1118 if (nb_entries >= list_size) {
1123 tmp = strchr(buf, '\n');
1126 if (used + strlen(buf) + 1 > buffer_size) {
1131 strcpy(buffer + used, buf);
1132 members[nb_entries] = buffer + used;
1133 used += strlen(buf) + 1;
1143 * Get the list of pools in a filesystem.
1144 * \param name filesystem name or path
1145 * \param poollist caller-allocated array of char*
1146 * \param list_size size of the poollist array
1147 * \param buffer caller-allocated buffer for storing pool names
1148 * \param buffer_size size of the buffer
1150 * \return number of pools retrieved for this filesystem
1151 * \retval -error failure
1153 int llapi_get_poollist(const char *name, char **poollist, int list_size,
1154 char *buffer, int buffer_size)
1156 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
1160 struct dirent *cookie = NULL;
1162 unsigned int nb_entries = 0;
1163 unsigned int used = 0;
1166 /* initilize output array */
1167 for (i = 0; i < list_size; i++)
1170 /* is name a pathname ? */
1171 ptr = strchr(name, '/');
1173 /* only absolute pathname is supported */
1177 if (!realpath(name, rname)) {
1179 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
1184 rc = poolpath(NULL, rname, pathname);
1186 llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
1187 " a Lustre filesystem", name);
1190 strcpy(fsname, rname);
1192 /* name is FSNAME */
1193 strcpy(fsname, name);
1194 rc = poolpath(fsname, NULL, pathname);
1197 llapi_error(LLAPI_MSG_ERROR, rc,
1198 "Lustre filesystem '%s' not found", name);
1202 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1203 dir = opendir(pathname);
1206 llapi_error(LLAPI_MSG_ERROR, rc,
1207 "Could not open pool list for '%s'",
1213 rc = readdir_r(dir, &pool, &cookie);
1217 llapi_error(LLAPI_MSG_ERROR, rc,
1218 "Error reading pool list for '%s'", name);
1220 } else if ((rc == 0) && (cookie == NULL)) {
1221 /* end of directory */
1225 /* ignore . and .. */
1226 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1229 /* check output bounds */
1230 if (nb_entries >= list_size) {
1235 /* +2 for '.' and final '\0' */
1236 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1242 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1243 poollist[nb_entries] = buffer + used;
1244 used += strlen(pool.d_name) + strlen(fsname) + 2;
1250 return ((rc != 0) ? rc : nb_entries);
1253 /* wrapper for lfs.c and obd.c */
1254 int llapi_poollist(const char *name)
1256 /* list of pool names (assume that pool count is smaller
1258 char **list, *buffer = NULL, *path = NULL, *fsname = NULL;
1259 int obdcount, bufsize, rc, nb, i;
1260 char *poolname = NULL, *tmp = NULL, data[16];
1262 if (name[0] != '/') {
1263 fsname = strdup(name);
1264 poolname = strchr(fsname, '.');
1268 path = (char *) name;
1271 rc = get_param_obdvar(fsname, path, "lov", "numobd",
1272 data, sizeof(data));
1275 obdcount = atoi(data);
1277 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1278 * and also an array to store the pointers for all that
1279 * allocated space. */
1281 bufsize = sizeof(struct obd_uuid) * obdcount;
1282 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1283 if (buffer == NULL) {
1287 list = (char **) (buffer + bufsize);
1290 /* name is a path or fsname */
1291 nb = llapi_get_poollist(name, list, obdcount,
1294 /* name is a pool name (<fsname>.<poolname>) */
1295 nb = llapi_get_poolmembers(name, list, obdcount,
1299 if (nb == -EOVERFLOW) {
1302 goto retry_get_pools;
1305 for (i = 0; i < nb; i++)
1306 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1307 rc = (nb < 0 ? nb : 0);
1316 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
1317 void *data, struct dirent64 *de);
1319 #define OBD_NOT_FOUND (-1)
1321 static int common_param_init(struct find_param *param, char *path)
1323 int lumlen = get_mds_md_size(path);
1325 if (lumlen < PATH_MAX + 1)
1326 lumlen = PATH_MAX + 1;
1328 param->lumlen = lumlen;
1329 param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
1330 if (param->lmd == NULL) {
1331 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1332 "error: allocation of %d bytes for ioctl",
1333 sizeof(lstat_t) + param->lumlen);
1337 param->fp_lmv_count = 256;
1338 param->fp_lmv_md = malloc(lmv_user_md_size(256, LMV_MAGIC_V1));
1339 if (param->fp_lmv_md == NULL) {
1340 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1341 "error: allocation of %d bytes for ioctl",
1342 lmv_user_md_size(256, LMV_MAGIC_V1));
1346 param->got_uuids = 0;
1347 param->obdindexes = NULL;
1348 param->obdindex = OBD_NOT_FOUND;
1349 param->mdtindex = OBD_NOT_FOUND;
1353 static void find_param_fini(struct find_param *param)
1355 if (param->obdindexes)
1356 free(param->obdindexes);
1361 if (param->fp_lmv_md)
1362 free(param->fp_lmv_md);
1365 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1366 struct dirent64 *de)
1368 struct find_param *param = (struct find_param *)data;
1373 /* set errno upon failure */
1374 static DIR *opendir_parent(char *path)
1380 fname = strrchr(path, '/');
1382 return opendir(".");
1386 parent = opendir(path);
1391 static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
1393 struct lmv_user_md *lmv = (struct lmv_user_md *)param->fp_lmv_md;
1396 lmv->lum_stripe_count = param->fp_lmv_count;
1397 lmv->lum_magic = LMV_MAGIC_V1;
1398 ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, lmv);
1402 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1403 struct lov_user_mds_data *lmd, int lumlen)
1405 lstat_t *st = &lmd->lmd_st;
1408 if (parent == NULL && dir == NULL)
1412 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1413 } else if (parent) {
1414 char *fname = strrchr(path, '/');
1416 fname = (fname == NULL ? path : fname + 1);
1417 /* retrieve needed file info */
1418 strncpy((char *)lmd, fname, lumlen);
1419 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1425 if (errno == ENOTTY) {
1426 /* ioctl is not supported, it is not a lustre fs.
1427 * Do the regular lstat(2) instead. */
1428 ret = lstat_f(path, st);
1431 llapi_error(LLAPI_MSG_ERROR, ret,
1432 "error: %s: lstat failed for %s",
1435 } else if (errno == ENOENT) {
1437 llapi_error(LLAPI_MSG_WARN, ret,
1438 "warning: %s: %s does not exist",
1440 } else if (errno != EISDIR) {
1442 llapi_error(LLAPI_MSG_ERROR, ret,
1443 "%s ioctl failed for %s.",
1444 dir ? "LL_IOC_MDC_GETINFO" :
1445 "IOC_MDC_GETFILEINFO", path);
1448 llapi_error(LLAPI_MSG_ERROR, ret,
1449 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1456 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1457 semantic_func_t sem_init,
1458 semantic_func_t sem_fini, void *data,
1459 struct dirent64 *de)
1461 struct find_param *param = (struct find_param *)data;
1462 struct dirent64 *dent;
1470 if (!d && errno != ENOTDIR) {
1472 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1475 } else if (!d && !parent) {
1476 /* ENOTDIR. Open the parent dir. */
1477 p = opendir_parent(path);
1479 GOTO(out, ret = -errno);
1482 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1485 if (!d || (param->get_lmv && !param->recursive))
1488 while ((dent = readdir64(d)) != NULL) {
1489 param->have_fileinfo = 0;
1491 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1494 /* Don't traverse .lustre directory */
1495 if (!(strcmp(dent->d_name, dot_lustre_name)))
1499 if ((len + dent->d_reclen + 2) > size) {
1500 llapi_err_noerrno(LLAPI_MSG_ERROR,
1501 "error: %s: string buffer is too small",
1506 strcat(path, dent->d_name);
1508 if (dent->d_type == DT_UNKNOWN) {
1509 lstat_t *st = ¶m->lmd->lmd_st;
1511 ret = get_lmd_info(path, d, NULL, param->lmd,
1515 llapi_filetype_dir_table[st->st_mode &
1521 switch (dent->d_type) {
1523 llapi_err_noerrno(LLAPI_MSG_ERROR,
1524 "error: %s: '%s' is UNKNOWN type %d",
1525 __func__, dent->d_name, dent->d_type);
1528 ret = llapi_semantic_traverse(path, size, d, sem_init,
1529 sem_fini, data, dent);
1536 ret = sem_init(path, d, NULL, data, dent);
1540 if (sem_fini && ret == 0)
1541 sem_fini(path, d, NULL, data, dent);
1549 sem_fini(path, parent, d, data, de);
1558 static int param_callback(char *path, semantic_func_t sem_init,
1559 semantic_func_t sem_fini, struct find_param *param)
1561 int ret, len = strlen(path);
1564 if (len > PATH_MAX) {
1566 llapi_error(LLAPI_MSG_ERROR, ret,
1567 "Path name '%s' is too long", path);
1571 buf = (char *)malloc(PATH_MAX + 1);
1575 strncpy(buf, path, PATH_MAX + 1);
1576 ret = common_param_init(param, buf);
1581 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1582 sem_fini, param, NULL);
1584 find_param_fini(param);
1586 return ret < 0 ? ret : 0;
1589 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1591 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1594 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1599 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1601 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1604 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1609 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1613 fd = open(path, O_RDONLY);
1616 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1620 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1626 int llapi_file_get_lmv_uuid(const char *path, struct obd_uuid *lov_uuid)
1630 fd = open(path, O_RDONLY);
1633 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1637 rc = llapi_file_fget_lmv_uuid(fd, lov_uuid);
1648 * If uuidp is NULL, return the number of available obd uuids.
1649 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1650 * there are more OSTs then allocated to uuidp, then an error is returned with
1651 * the ost_count set to number of available obd uuids.
1653 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1654 int *ost_count, enum tgt_type type)
1656 struct obd_uuid name;
1659 int rc = 0, index = 0;
1661 /* Get the lov name */
1662 if (type == LOV_TYPE) {
1663 rc = llapi_file_fget_lov_uuid(fd, &name);
1667 rc = llapi_file_fget_lmv_uuid(fd, &name);
1672 /* Now get the ost uuids from /proc */
1673 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1674 type == LOV_TYPE ? "lov" : "lmv", name.uuid);
1675 fp = fopen(buf, "r");
1678 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1682 while (fgets(buf, sizeof(buf), fp) != NULL) {
1683 if (uuidp && (index < *ost_count)) {
1684 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
1692 if (uuidp && (index > *ost_count))
1699 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1701 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1704 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1709 root = opendir(mnt);
1712 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1717 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1725 /* Check if user specified value matches a real uuid. Ignore _UUID,
1726 * -osc-4ba41334, other trailing gunk in comparison.
1727 * @param real_uuid ends in "_UUID"
1728 * @param search_uuid may or may not end in "_UUID"
1730 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1732 int cmplen = strlen(real_uuid);
1733 int searchlen = strlen(search_uuid);
1735 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1737 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1740 /* The UUIDs may legitimately be different lengths, if
1741 * the system was upgraded from an older version. */
1742 if (cmplen != searchlen)
1745 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1748 /* Here, param->obduuid points to a single obduuid, the index of which is
1749 * returned in param->obdindex */
1750 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1752 struct obd_uuid obd_uuid;
1753 char uuid[sizeof(struct obd_uuid)];
1758 if (param->got_uuids)
1761 /* Get the lov/lmv name */
1763 rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
1765 rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
1767 if (rc != -ENOTTY) {
1768 llapi_error(LLAPI_MSG_ERROR, rc,
1769 "error: can't get lov name: %s", dname);
1776 param->got_uuids = 1;
1778 /* Now get the ost uuids from /proc */
1779 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1780 param->get_lmv ? "lmv" : "lov", obd_uuid.uuid);
1781 fp = fopen(buf, "r");
1784 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1788 if (!param->obduuid && !param->quiet && !param->obds_printed)
1789 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1790 param->get_lmv ? "MDTS" : "OBDS:");
1792 while (fgets(buf, sizeof(buf), fp) != NULL) {
1793 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
1796 if (param->obduuid) {
1797 if (llapi_uuid_match(uuid, param->obduuid->uuid)) {
1798 param->obdindex = index;
1801 } else if (!param->quiet && !param->obds_printed) {
1802 /* Print everything */
1803 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1806 param->obds_printed = 1;
1810 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1811 llapi_err_noerrno(LLAPI_MSG_ERROR,
1812 "error: %s: unknown obduuid: %s",
1813 __func__, param->obduuid->uuid);
1820 /* In this case, param->obduuid will be an array of obduuids and
1821 * obd index for all these obduuids will be returned in
1822 * param->obdindexes */
1823 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1824 int num_obds, int **obdindexes, int *obdindex,
1827 int ret, obdcount, obd_valid = 0, obdnum, i;
1828 struct obd_uuid *uuids = NULL;
1832 if (type == LOV_TYPE)
1833 ret = get_param_lov(path, "numobd", buf, sizeof(buf));
1835 ret = get_param_lmv(path, "numobd", buf, sizeof(buf));
1839 obdcount = atoi(buf);
1840 uuids = (struct obd_uuid *)malloc(obdcount *
1841 sizeof(struct obd_uuid));
1846 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1848 struct obd_uuid *uuids_temp;
1850 if (ret == -EOVERFLOW) {
1851 uuids_temp = realloc(uuids, obdcount *
1852 sizeof(struct obd_uuid));
1853 if (uuids_temp != NULL) {
1855 goto retry_get_uuids;
1861 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
1865 indexes = malloc(num_obds * sizeof(*obdindex));
1866 if (indexes == NULL) {
1871 for (obdnum = 0; obdnum < num_obds; obdnum++) {
1874 /* The user may have specified a simple index */
1875 i = strtol(obduuids[obdnum].uuid, &end, 0);
1876 if (end && *end == '\0' && i < obdcount) {
1877 indexes[obdnum] = i;
1880 for (i = 0; i < obdcount; i++) {
1881 if (llapi_uuid_match(uuids[i].uuid,
1882 obduuids[obdnum].uuid)) {
1883 indexes[obdnum] = i;
1889 if (i >= obdcount) {
1890 indexes[obdnum] = OBD_NOT_FOUND;
1891 llapi_err_noerrno(LLAPI_MSG_ERROR,
1892 "error: %s: unknown obduuid: %s",
1893 __func__, obduuids[obdnum].uuid);
1899 *obdindex = OBD_NOT_FOUND;
1901 *obdindex = obd_valid;
1903 *obdindexes = indexes;
1911 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
1915 if (param->mdtuuid) {
1916 ret = setup_indexes(dir, path, param->mdtuuid, param->num_mdts,
1917 ¶m->mdtindexes, ¶m->mdtindex, LMV_TYPE);
1921 if (param->obduuid) {
1922 ret = setup_indexes(dir, path, param->obduuid, param->num_obds,
1923 ¶m->obdindexes, ¶m->obdindex, LOV_TYPE);
1927 param->got_uuids = 1;
1931 int llapi_ostlist(char *path, struct find_param *param)
1936 dir = opendir(path);
1940 ret = setup_obd_uuid(dir, path, param);
1947 * Given a filesystem name, or a pathname of a file on a lustre filesystem,
1948 * tries to determine the path to the filesystem's clilov directory under /proc
1950 * fsname is limited to MTI_NAME_MAXLEN in lustre_idl.h
1951 * The NUL terminator is compensated by the additional "%s" bytes. */
1952 #define LOV_LEN (sizeof("/proc/fs/lustre/lov/%s-clilov-*") + MTI_NAME_MAXLEN)
1953 static int clilovpath(const char *fsname, const char *const pathname,
1957 char pattern[LOV_LEN];
1958 char buffer[PATH_MAX + 1];
1960 if (fsname == NULL) {
1961 rc = llapi_search_fsname(pathname, buffer);
1967 snprintf(pattern, sizeof(pattern), "/proc/fs/lustre/lov/%s-clilov-*",
1970 rc = first_match(pattern, buffer);
1974 strncpy(clilovpath, buffer, sizeof(buffer));
1980 * Given the path to a stripe attribute proc file, tries to open and
1981 * read the attribute and return the value using the attr parameter
1983 static int sattr_read_attr(const char *const fpath,
1988 char line[PATH_MAX + 1];
1991 f = fopen(fpath, "r");
1994 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open '%s'", fpath);
1998 if (fgets(line, sizeof(line), f) != NULL) {
2001 llapi_error(LLAPI_MSG_ERROR, errno, "Cannot read from '%s'", fpath);
2010 * Tries to determine the default stripe attributes for a given filesystem. The
2011 * filesystem to check should be specified by fsname, or will be determined
2014 static int sattr_get_defaults(const char *const fsname,
2015 const char *const pathname,
2016 unsigned int *scount,
2017 unsigned int *ssize,
2018 unsigned int *soffset)
2021 char dpath[PATH_MAX + 1];
2022 char fpath[PATH_MAX + 1];
2024 rc = clilovpath(fsname, pathname, dpath);
2029 snprintf(fpath, PATH_MAX, "%s/stripecount", dpath);
2030 rc = sattr_read_attr(fpath, scount);
2036 snprintf(fpath, PATH_MAX, "%s/stripesize", dpath);
2037 rc = sattr_read_attr(fpath, ssize);
2043 snprintf(fpath, PATH_MAX, "%s/stripeoffset", dpath);
2044 rc = sattr_read_attr(fpath, soffset);
2053 * Tries to gather the default stripe attributes for a given filesystem. If
2054 * the attributes can be determined, they are cached for easy retreival the
2055 * next time they are needed. Only a single filesystem's attributes are
2058 static int sattr_cache_get_defaults(const char *const fsname,
2059 const char *const pathname,
2060 unsigned int *scount,
2061 unsigned int *ssize,
2062 unsigned int *soffset)
2065 char fsname[PATH_MAX + 1];
2066 unsigned int stripecount;
2067 unsigned int stripesize;
2068 unsigned int stripeoffset;
2074 char fsname_buf[PATH_MAX + 1];
2075 unsigned int tmp[3];
2077 if (fsname == NULL) {
2078 rc = llapi_search_fsname(pathname, fsname_buf);
2082 strncpy(fsname_buf, fsname, PATH_MAX);
2085 if (strncmp(fsname_buf, cache.fsname, PATH_MAX) != 0) {
2087 * Ensure all 3 sattrs (count, size, and offset) are
2088 * successfully retrieved and stored in tmp before writing to
2091 rc = sattr_get_defaults(fsname_buf, NULL, &tmp[0], &tmp[1],
2096 cache.stripecount = tmp[0];
2097 cache.stripesize = tmp[1];
2098 cache.stripeoffset = tmp[2];
2099 strncpy(cache.fsname, fsname_buf, PATH_MAX);
2103 *scount = cache.stripecount;
2105 *ssize = cache.stripesize;
2107 *soffset = cache.stripeoffset;
2112 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
2113 struct lov_user_ost_data_v1 *objects,
2114 int is_dir, int verbose, int depth,
2115 int raw, char *pool_name)
2117 char *prefix = is_dir ? "" : "lmm_";
2118 char nl = is_dir ? ' ' : '\n';
2121 if (is_dir && lmm_oi_seq(&lum->lmm_oi) == FID_SEQ_LOV_DEFAULT) {
2122 lmm_oi_set_seq(&lum->lmm_oi, 0);
2123 if (verbose & VERBOSE_DETAIL)
2124 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
2127 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
2128 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2130 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2131 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
2133 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
2134 lmm_oi_seq(&lum->lmm_oi));
2135 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
2136 lmm_oi_id(&lum->lmm_oi));
2139 if (verbose & VERBOSE_COUNT) {
2140 if (verbose & ~VERBOSE_COUNT)
2141 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
2144 if (!raw && lum->lmm_stripe_count == 0) {
2145 unsigned int scount;
2146 rc = sattr_cache_get_defaults(NULL, path,
2150 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
2153 llapi_error(LLAPI_MSG_ERROR, rc,
2154 "Cannot determine default"
2157 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
2158 lum->lmm_stripe_count ==
2159 (typeof(lum->lmm_stripe_count))(-1)
2160 ? -1 : lum->lmm_stripe_count, nl);
2163 llapi_printf(LLAPI_MSG_NORMAL, "%hd%c",
2164 (__s16)lum->lmm_stripe_count, nl);
2168 if (verbose & VERBOSE_SIZE) {
2169 if (verbose & ~VERBOSE_SIZE)
2170 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
2172 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
2174 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
2177 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", ssize,
2180 llapi_error(LLAPI_MSG_ERROR, rc,
2181 "Cannot determine default"
2184 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
2185 lum->lmm_stripe_size, nl);
2189 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
2190 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
2191 lum->lmm_pattern, nl);
2194 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
2195 if (verbose & ~VERBOSE_GENERATION)
2196 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
2198 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
2199 (int)lum->lmm_layout_gen, nl);
2202 if (verbose & VERBOSE_OFFSET) {
2203 if (verbose & ~VERBOSE_OFFSET)
2204 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
2207 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
2208 lum->lmm_stripe_offset ==
2209 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
2210 lum->lmm_stripe_offset, nl);
2212 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
2213 objects[0].l_ost_idx, nl);
2216 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
2217 if (verbose & ~VERBOSE_POOL)
2218 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
2220 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", pool_name, nl);
2223 if (is_dir && (verbose != VERBOSE_OBJID))
2224 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2227 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
2228 struct lov_user_ost_data_v1 *objects,
2229 char *path, int is_dir, int obdindex,
2230 int depth, int header, int raw)
2232 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
2235 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
2236 if (obdindex == objects[i].l_ost_idx) {
2244 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
2245 depth, raw, pool_name);
2247 if (!is_dir && (header & VERBOSE_OBJID)) {
2249 llapi_printf(LLAPI_MSG_NORMAL,
2250 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2252 for (i = 0; i < lum->lmm_stripe_count; i++) {
2253 int idx = objects[i].l_ost_idx;
2254 long long oid = ostid_id(&objects[i].l_ost_oi);
2255 long long gr = ostid_seq(&objects[i].l_ost_oi);
2256 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) {
2258 sprintf(fmt, "%s%s%s\n",
2259 "\t%6u\t%14llu\t%#13llx\t",
2260 (fid_seq_is_rsvd(gr) ||
2261 fid_seq_is_mdt0(gr)) ?
2262 "%14llu" : "%#14llx", "%s");
2263 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2265 obdindex == idx ? " *" : "");
2269 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2273 void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
2274 char *path, int obdindex, int depth, int verbose)
2276 struct lmv_user_mds_data *objects = lum->lum_objects;
2277 char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
2278 int i, obdstripe = 0;
2280 if (obdindex != OBD_NOT_FOUND) {
2281 for (i = 0; i < lum->lum_stripe_count; i++) {
2282 if (obdindex == objects[i].lum_mds) {
2283 llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix,
2293 /* show all information default */
2295 if (lum->lum_magic == LMV_USER_MAGIC)
2296 verbose = VERBOSE_POOL | VERBOSE_COUNT | VERBOSE_OFFSET;
2298 verbose = VERBOSE_OBJID;
2301 if (lum->lum_magic == LMV_USER_MAGIC)
2302 verbose &= ~VERBOSE_OBJID;
2304 if (depth && path && ((verbose != VERBOSE_OBJID)))
2305 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
2307 if (verbose & VERBOSE_COUNT) {
2308 if (verbose & ~VERBOSE_COUNT)
2309 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
2310 llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
2311 (int)lum->lum_stripe_count);
2314 if (verbose & VERBOSE_OFFSET) {
2315 if (verbose & ~VERBOSE_OFFSET)
2316 llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
2317 llapi_printf(LLAPI_MSG_NORMAL, "%d\n",
2318 (int)lum->lum_stripe_offset);
2321 if (verbose & VERBOSE_OBJID) {
2322 if ((obdstripe == 1))
2323 llapi_printf(LLAPI_MSG_NORMAL,
2324 "\tmdtidx\t\t FID[seq:oid:ver]\n");
2325 for (i = 0; i < lum->lum_stripe_count; i++) {
2326 int idx = objects[i].lum_mds;
2327 struct lu_fid *fid = &objects[i].lum_fid;
2328 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2329 llapi_printf(LLAPI_MSG_NORMAL,
2330 "\t%6u\t\t "DFID"\t\t%s\n",
2332 obdindex == idx ? " *" : "");
2337 if ((verbose & VERBOSE_POOL) && (pool_name[0] != '\0')) {
2338 if (verbose & ~VERBOSE_POOL)
2339 llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool: ",
2341 llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
2343 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2346 void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
2351 magic = (__u32)param->fp_lmv_md->lum_magic;
2353 magic = *(__u32 *)¶m->lmd->lmd_lmm; /* lum->lmm_magic */
2356 case LOV_USER_MAGIC_V1:
2357 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
2358 param->lmd->lmd_lmm.lmm_objects,
2360 param->obdindex, param->maxdepth,
2361 param->verbose, param->raw);
2363 case LOV_USER_MAGIC_V3: {
2364 char pool_name[LOV_MAXPOOLNAME + 1];
2365 struct lov_user_ost_data_v1 *objects;
2366 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2368 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
2369 pool_name[LOV_MAXPOOLNAME] = '\0';
2370 objects = lmmv3->lmm_objects;
2371 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
2372 objects, path, is_dir,
2373 param->obdindex, param->maxdepth,
2374 param->verbose, param->raw);
2378 case LMV_USER_MAGIC: {
2379 char pool_name[LOV_MAXPOOLNAME + 1];
2380 struct lmv_user_md *lum;
2382 lum = (struct lmv_user_md *)param->fp_lmv_md;
2383 strncpy(pool_name, lum->lum_pool_name, LOV_MAXPOOLNAME);
2384 lmv_dump_user_lmm(lum, pool_name, path,
2385 param->obdindex, param->maxdepth,
2390 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2391 "(expecting one of %#x %#x %#x %#x)\n",
2392 *(__u32 *)¶m->lmd->lmd_lmm,
2393 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
2394 LMV_USER_MAGIC, LMV_MAGIC_V1);
2399 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2405 fname = strrchr(path, '/');
2407 /* It should be a file (or other non-directory) */
2408 if (fname == NULL) {
2409 dname = (char *)malloc(2);
2413 fname = (char *)path;
2415 dname = (char *)malloc(fname - path + 1);
2418 strncpy(dname, path, fname - path);
2419 dname[fname - path] = '\0';
2423 fd = open(dname, O_RDONLY);
2430 strcpy((char *)lum, fname);
2431 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2434 if (close(fd) == -1 && rc == 0)
2441 int llapi_file_lookup(int dirfd, const char *name)
2443 struct obd_ioctl_data data = { 0 };
2448 if (dirfd < 0 || name == NULL)
2451 data.ioc_version = OBD_IOCTL_VERSION;
2452 data.ioc_len = sizeof(data);
2453 data.ioc_inlbuf1 = (char *)name;
2454 data.ioc_inllen1 = strlen(name) + 1;
2456 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2458 llapi_error(LLAPI_MSG_ERROR, rc,
2459 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2464 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2470 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2471 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2473 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2474 * The table below gives the answers for the specified parameters (value and
2475 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2476 * --------------------------------------
2477 * 1 | file > limit; sign > 0 | -1 / -1 |
2478 * 2 | file = limit; sign > 0 | -1 / -1 |
2479 * 3 | file < limit; sign > 0 | ? / 1 |
2480 * 4 | file > limit; sign = 0 | -1 / -1 |
2481 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2482 * 6 | file < limit; sign = 0 | ? / -1 |
2483 * 7 | file > limit; sign < 0 | 1 / 1 |
2484 * 8 | file = limit; sign < 0 | ? / -1 |
2485 * 9 | file < limit; sign < 0 | ? / -1 |
2486 * --------------------------------------
2487 * Note: 5th actually means that the value is within the interval
2488 * (limit - margin, limit]. */
2489 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2490 int sign, int negopt, unsigned long long margin,
2496 /* Drop the fraction of margin (of days). */
2497 if (file + margin <= limit)
2499 } else if (sign == 0) {
2500 if (file <= limit && file + margin > limit)
2502 else if (file + margin <= limit)
2504 } else if (sign < 0) {
2511 return negopt ? ~ret + 1 : ret;
2514 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2515 * Return -1 or 1 if file timestamp does not or does match the given criteria
2516 * correspondingly. Return 0 if the MDS time is being checked and there are
2517 * attributes on OSTs and it is not yet clear if the timespamp matches.
2519 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2520 * updated timestamps. */
2521 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2526 /* Check if file is accepted. */
2528 ret = find_value_cmp(st->st_atime, param->atime,
2529 param->asign, param->exclude_atime,
2537 ret = find_value_cmp(st->st_mtime, param->mtime,
2538 param->msign, param->exclude_mtime,
2543 /* If the previous check matches, but this one is not yet clear,
2544 * we should return 0 to do an RPC on OSTs. */
2550 ret = find_value_cmp(st->st_ctime, param->ctime,
2551 param->csign, param->exclude_ctime,
2556 /* If the previous check matches, but this one is not yet clear,
2557 * we should return 0 to do an RPC on OSTs. */
2566 * Check whether the stripes matches the indexes user provided
2570 static int check_obd_match(struct find_param *param)
2572 lstat_t *st = ¶m->lmd->lmd_st;
2573 struct lov_user_ost_data_v1 *lmm_objects;
2576 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
2579 if (!S_ISREG(st->st_mode))
2582 /* Only those files should be accepted, which have a
2583 * stripe on the specified OST. */
2584 if (!param->lmd->lmd_lmm.lmm_stripe_count)
2587 if (param->lmd->lmd_lmm.lmm_magic ==
2588 LOV_USER_MAGIC_V3) {
2589 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2591 lmm_objects = lmmv3->lmm_objects;
2592 } else if (param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2593 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
2595 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2596 __func__, param->lmd->lmd_lmm.lmm_magic);
2600 for (i = 0; i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
2601 for (j = 0; j < param->num_obds; j++) {
2602 if (param->obdindexes[j] ==
2603 lmm_objects[i].l_ost_idx) {
2604 if (param->exclude_obd)
2611 if (param->exclude_obd)
2616 static int check_mdt_match(struct find_param *param)
2620 if (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND)
2623 /* FIXME: For striped dir, we should get stripe information and check */
2624 for (i = 0; i < param->num_mdts; i++) {
2625 if (param->mdtindexes[i] == param->file_mdtindex)
2626 if (param->exclude_mdt)
2631 if (param->exclude_mdt)
2637 * Check whether the obd is active or not, if it is
2638 * not active, just print the object affected by this
2641 static int print_failed_tgt(struct find_param *param, char *path, int type)
2643 struct obd_statfs stat_buf;
2644 struct obd_uuid uuid_buf;
2647 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2649 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2650 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2651 ret = llapi_obd_statfs(path, type,
2652 param->obdindex, &stat_buf,
2655 llapi_printf(LLAPI_MSG_NORMAL,
2656 "obd_uuid: %s failed %s ",
2657 param->obduuid->uuid,
2663 static int cb_find_init(char *path, DIR *parent, DIR *dir,
2664 void *data, struct dirent64 *de)
2666 struct find_param *param = (struct find_param *)data;
2667 int decision = 1; /* 1 is accepted; -1 is rejected. */
2668 lstat_t *st = ¶m->lmd->lmd_st;
2670 int checked_type = 0;
2673 LASSERT(parent != NULL || dir != NULL);
2675 if (param->have_fileinfo == 0)
2676 param->lmd->lmd_lmm.lmm_stripe_count = 0;
2678 /* If a regular expression is presented, make the initial decision */
2679 if (param->pattern != NULL) {
2680 char *fname = strrchr(path, '/');
2681 fname = (fname == NULL ? path : fname + 1);
2682 ret = fnmatch(param->pattern, fname, 0);
2683 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
2684 (ret == 0 && param->exclude_pattern))
2688 /* See if we can check the file type from the dirent. */
2689 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
2690 de->d_type < DT_MAX) {
2692 if (llapi_dir_filetype_table[de->d_type] == param->type) {
2693 if (param->exclude_type)
2696 if (!param->exclude_type)
2703 /* Request MDS for the stat info if some of these parameters need
2704 * to be compared. */
2705 if (param->obduuid || param->mdtuuid || param->check_uid ||
2706 param->check_gid || param->check_pool || param->atime ||
2707 param->ctime || param->mtime || param->check_size ||
2708 param->check_stripecount || param->check_stripesize)
2711 if (param->type && checked_type == 0)
2714 if (param->have_fileinfo == 0 && decision == 0) {
2715 ret = get_lmd_info(path, parent, dir, param->lmd,
2719 ret = llapi_file_fget_mdtidx(dirfd(dir),
2720 ¶m->file_mdtindex);
2725 ret = lstat_f(path, &tmp_st);
2728 llapi_error(LLAPI_MSG_ERROR, ret,
2729 "error: %s: lstat failed"
2730 "for %s", __func__, path);
2733 if (S_ISREG(tmp_st.st_mode)) {
2734 fd = open(path, O_RDONLY);
2736 ret = llapi_file_fget_mdtidx(fd,
2737 ¶m->file_mdtindex);
2743 /* For special inode, it assumes to
2744 * reside on the same MDT with the
2747 ret = llapi_file_fget_mdtidx(fd,
2748 ¶m->file_mdtindex);
2761 if (param->type && !checked_type) {
2762 if ((st->st_mode & S_IFMT) == param->type) {
2763 if (param->exclude_type)
2766 if (!param->exclude_type)
2772 if (param->obduuid || param->mdtuuid) {
2773 if (lustre_fs && param->got_uuids &&
2774 param->st_dev != st->st_dev) {
2775 /* A lustre/lustre mount point is crossed. */
2776 param->got_uuids = 0;
2777 param->obds_printed = 0;
2778 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2781 if (lustre_fs && !param->got_uuids) {
2782 ret = setup_target_indexes(dir ? dir : parent, path,
2787 param->st_dev = st->st_dev;
2788 } else if (!lustre_fs && param->got_uuids) {
2789 /* A lustre/non-lustre mount point is crossed. */
2790 param->got_uuids = 0;
2791 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2795 if (param->check_stripesize) {
2796 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_size,
2798 param->stripesize_sign,
2799 param->exclude_stripesize,
2800 param->stripesize_units, 0);
2805 if (param->check_stripecount) {
2806 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_count,
2808 param->stripecount_sign,
2809 param->exclude_stripecount, 1, 0);
2814 /* If an OBD UUID is specified but none matches, skip this file. */
2815 if ((param->obduuid && param->obdindex == OBD_NOT_FOUND) ||
2816 (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND))
2819 /* If a OST or MDT UUID is given, and some OST matches,
2821 if (param->obdindex != OBD_NOT_FOUND ||
2822 param->mdtindex != OBD_NOT_FOUND) {
2823 if (param->obduuid) {
2824 if (check_obd_match(param)) {
2825 /* If no mdtuuid is given, we are done.
2826 * Otherwise, fall through to the mdtuuid
2828 if (!param->mdtuuid)
2834 if (param->mdtuuid) {
2835 if (check_mdt_match(param))
2841 if (param->check_uid) {
2842 if (st->st_uid == param->uid) {
2843 if (param->exclude_uid)
2846 if (!param->exclude_uid)
2851 if (param->check_gid) {
2852 if (st->st_gid == param->gid) {
2853 if (param->exclude_gid)
2856 if (!param->exclude_gid)
2861 if (param->check_pool) {
2862 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2864 /* empty requested pool is taken as no pool search => V1 */
2865 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
2866 (param->poolname[0] == '\0')) ||
2867 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2868 (strncmp(lmmv3->lmm_pool_name,
2869 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
2870 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2871 (strcmp(param->poolname, "*") == 0))) {
2872 if (param->exclude_pool)
2875 if (!param->exclude_pool)
2880 /* Check the time on mds. */
2882 if (param->atime || param->ctime || param->mtime) {
2885 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
2886 param->lmd->lmd_lmm.lmm_stripe_count)
2888 decision = find_time_check(st, param, for_mds);
2893 /* If file still fits the request, ask ost for updated info.
2894 The regular stat is almost of the same speed as some new
2895 'glimpse-size-ioctl'. */
2897 if (param->check_size && S_ISREG(st->st_mode) &&
2898 param->lmd->lmd_lmm.lmm_stripe_count)
2902 /* For regular files with the stripe the decision may have not
2903 * been taken yet if *time or size is to be checked. */
2904 LASSERT((S_ISREG(st->st_mode) &&
2905 param->lmd->lmd_lmm.lmm_stripe_count) ||
2906 param->mdtindex != OBD_NOT_FOUND);
2908 if (param->obdindex != OBD_NOT_FOUND)
2909 print_failed_tgt(param, path, LL_STATFS_LOV);
2911 if (param->mdtindex != OBD_NOT_FOUND)
2912 print_failed_tgt(param, path, LL_STATFS_LMV);
2915 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
2916 (void *)param->lmd);
2917 } else if (parent) {
2918 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
2919 (void *)param->lmd);
2923 if (errno == ENOENT) {
2924 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
2925 "warning: %s: %s does not exist",
2930 llapi_error(LLAPI_MSG_ERROR, ret,
2931 "%s: IOC_LOV_GETINFO on %s failed",
2937 /* Check the time on osc. */
2938 decision = find_time_check(st, param, 0);
2945 if (param->check_size)
2946 decision = find_value_cmp(st->st_size, param->size,
2947 param->size_sign, param->exclude_size,
2948 param->size_units, 0);
2950 if (decision != -1) {
2951 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
2953 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
2955 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2959 /* Do not get down anymore? */
2960 if (param->depth == param->maxdepth)
2967 int llapi_find(char *path, struct find_param *param)
2969 return param_callback(path, cb_find_init, cb_common_fini, param);
2973 * Get MDT number that the file/directory inode referenced
2974 * by the open fd resides on.
2975 * Return 0 and mdtidx on success, or -ve errno.
2977 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
2979 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
2984 static int cb_get_mdt_index(char *path, DIR *parent, DIR *d, void *data,
2985 struct dirent64 *de)
2987 struct find_param *param = (struct find_param *)data;
2991 LASSERT(parent != NULL || d != NULL);
2994 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
2995 } else if (parent) {
2998 fd = open(path, O_RDONLY);
3000 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
3008 if (ret == -ENODATA) {
3009 if (!param->obduuid)
3010 llapi_printf(LLAPI_MSG_NORMAL,
3011 "%s has no stripe info\n", path);
3013 } else if (ret == -ENOENT) {
3014 llapi_error(LLAPI_MSG_WARN, ret,
3015 "warning: %s: %s does not exist",
3018 } else if (ret == -ENOTTY) {
3019 llapi_error(LLAPI_MSG_ERROR, ret,
3020 "%s: '%s' not on a Lustre fs?",
3023 llapi_error(LLAPI_MSG_ERROR, ret,
3024 "error: %s: LL_IOC_GET_MDTIDX failed for %s",
3030 /* The 'LASSERT(parent != NULL || d != NULL);' guarantees
3031 * that either 'd' or 'parent' is not null.
3032 * So in all cases llapi_file_fget_mdtidx() is called,
3033 * thus initializing 'mdtidx'. */
3034 if (param->quiet || !(param->verbose & VERBOSE_DETAIL))
3035 /* coverity[uninit_use_in_call] */
3036 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
3038 /* coverity[uninit_use_in_call] */
3039 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
3043 /* Do not get down anymore? */
3044 if (param->depth == param->maxdepth)
3051 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
3052 struct dirent64 *de)
3054 struct find_param *param = (struct find_param *)data;
3057 LASSERT(parent != NULL || d != NULL);
3059 if (param->obduuid) {
3061 ret = setup_obd_uuid(d ? d : parent, path, param);
3067 if (param->get_lmv) {
3068 ret = cb_get_dirstripe(path, d, param);
3070 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
3071 (void *)¶m->lmd->lmd_lmm);
3074 } else if (parent) {
3075 char *fname = strrchr(path, '/');
3076 fname = (fname == NULL ? path : fname + 1);
3078 if (param->get_lmv) {
3079 llapi_printf(LLAPI_MSG_NORMAL,
3080 "%s get dirstripe information for file\n",
3085 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
3087 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
3088 (void *)¶m->lmd->lmd_lmm);
3092 if (errno == ENODATA && d != NULL) {
3093 /* We need to "fake" the "use the default" values
3094 * since the lmm struct is zeroed out at this point.
3095 * The magic needs to be set in order to satisfy
3096 * a check later on in the code path.
3097 * The object_seq needs to be set for the "(Default)"
3098 * prefix to be displayed. */
3099 struct lov_user_md *lmm = ¶m->lmd->lmd_lmm;
3100 lmm->lmm_magic = LOV_MAGIC_V1;
3102 ostid_set_seq(&lmm->lmm_oi,
3103 FID_SEQ_LOV_DEFAULT);
3104 lmm->lmm_stripe_count = 0;
3105 lmm->lmm_stripe_size = 0;
3106 lmm->lmm_stripe_offset = -1;
3108 } else if (errno == ENODATA && parent != NULL) {
3109 if (!param->obduuid && !param->mdtuuid)
3110 llapi_printf(LLAPI_MSG_NORMAL,
3111 "%s has no stripe info\n", path);
3113 } else if (errno == ENOENT) {
3114 llapi_error(LLAPI_MSG_WARN, -ENOENT,
3115 "warning: %s: %s does not exist",
3118 } else if (errno == ENOTTY) {
3120 llapi_error(LLAPI_MSG_ERROR, ret,
3121 "%s: '%s' not on a Lustre fs?",
3125 llapi_error(LLAPI_MSG_ERROR, ret,
3126 "error: %s: %s failed for %s",
3127 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
3128 "IOC_MDC_GETFILESTRIPE", path);
3135 if (!(param->verbose & VERBOSE_MDTINDEX))
3136 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
3139 /* Do not get down anymore? */
3140 if (param->depth == param->maxdepth)
3147 int llapi_getstripe(char *path, struct find_param *param)
3149 return param_callback(path, (param->verbose & VERBOSE_MDTINDEX) ?
3150 cb_get_mdt_index : cb_getstripe,
3151 cb_common_fini, param);
3154 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
3155 struct obd_statfs *stat_buf,
3156 struct obd_uuid *uuid_buf)
3159 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
3161 struct obd_ioctl_data data = { 0 };
3164 data.ioc_inlbuf1 = (char *)&type;
3165 data.ioc_inllen1 = sizeof(__u32);
3166 data.ioc_inlbuf2 = (char *)&index;
3167 data.ioc_inllen2 = sizeof(__u32);
3168 data.ioc_pbuf1 = (char *)stat_buf;
3169 data.ioc_plen1 = sizeof(struct obd_statfs);
3170 data.ioc_pbuf2 = (char *)uuid_buf;
3171 data.ioc_plen2 = sizeof(struct obd_uuid);
3173 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
3175 llapi_error(LLAPI_MSG_ERROR, rc,
3176 "llapi_obd_statfs: error packing ioctl data");
3180 fd = open(path, O_RDONLY);
3181 if (errno == EISDIR)
3182 fd = open(path, O_DIRECTORY | O_RDONLY);
3185 rc = errno ? -errno : -EBADF;
3186 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
3190 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
3192 rc = errno ? -errno : -EINVAL;
3198 #define MAX_STRING_SIZE 128
3200 int llapi_ping(char *obd_type, char *obd_name)
3202 char path[MAX_STRING_SIZE];
3206 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
3207 obd_type, obd_name);
3209 fd = open(path, O_WRONLY);
3212 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
3216 /* The purpose is to send a byte as a ping, whatever this byte is. */
3217 /* coverity[uninit_use_in_call] */
3218 rc = write(fd, buf, 1);
3228 int llapi_target_iterate(int type_num, char **obd_type,
3229 void *args, llapi_cb_t cb)
3231 char buf[MAX_STRING_SIZE];
3232 FILE *fp = fopen(DEVICES_LIST, "r");
3237 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
3241 while (fgets(buf, sizeof(buf), fp) != NULL) {
3242 char *obd_type_name = NULL;
3243 char *obd_name = NULL;
3244 char *obd_uuid = NULL;
3246 struct obd_statfs osfs_buffer;
3248 while(bufp[0] == ' ')
3251 for(i = 0; i < 3; i++) {
3252 obd_type_name = strsep(&bufp, " ");
3254 obd_name = strsep(&bufp, " ");
3255 obd_uuid = strsep(&bufp, " ");
3257 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
3259 for (i = 0; i < type_num; i++) {
3260 if (strcmp(obd_type_name, obd_type[i]) != 0)
3263 cb(obd_type_name, obd_name, obd_uuid, args);
3270 static void do_target_check(char *obd_type_name, char *obd_name,
3271 char *obd_uuid, void *args)
3275 rc = llapi_ping(obd_type_name, obd_name);
3276 if (rc == ENOTCONN) {
3277 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
3279 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
3281 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
3285 int llapi_target_check(int type_num, char **obd_type, char *dir)
3287 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
3290 #undef MAX_STRING_SIZE
3292 /* Is this a lustre fs? */
3293 int llapi_is_lustre_mnttype(const char *type)
3295 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
3298 /* Is this a lustre client fs? */
3299 int llapi_is_lustre_mnt(struct mntent *mnt)
3301 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
3302 strstr(mnt->mnt_fsname, ":/") != NULL);
3305 int llapi_quotacheck(char *mnt, int check_type)
3310 root = opendir(mnt);
3313 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3317 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
3325 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
3331 root = opendir(mnt);
3334 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3339 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
3343 if (poll_intvl < 30)
3351 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3356 root = opendir(mnt);
3359 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3363 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
3371 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
3372 struct dirent64 *de)
3374 struct find_param *param = (struct find_param *)data;
3378 LASSERT(parent != NULL || d != NULL);
3380 rc = get_lmd_info(path, parent, d, param->lmd, param->lumlen);
3382 if (rc == -ENODATA) {
3383 if (!param->obduuid && !param->quiet)
3384 llapi_error(LLAPI_MSG_ERROR, -ENODATA,
3385 "%s has no stripe info", path);
3387 } else if (rc == -ENOENT) {
3393 st = ¶m->lmd->lmd_st;
3395 /* libc chown() will do extra check, and if the real owner is
3396 * the same as the ones to set, it won't fall into kernel, so
3397 * invoke syscall directly. */
3398 rc = syscall(SYS_chown, path, -1, -1);
3400 llapi_error(LLAPI_MSG_ERROR, errno,
3401 "error: chown %s", path);
3403 rc = chmod(path, st->st_mode);
3406 llapi_error(LLAPI_MSG_ERROR, rc, "error: chmod %s (%hu)",
3413 int llapi_quotachown(char *path, int flag)
3415 struct find_param param;
3417 memset(¶m, 0, sizeof(param));
3418 param.recursive = 1;
3422 return param_callback(path, cb_quotachown, NULL, ¶m);
3428 #include <sys/wait.h>
3432 static int rmtacl_notify(int ops)
3436 int found = 0, fd = 0, rc = 0;
3438 fp = setmntent(MOUNTED, "r");
3441 llapi_error(LLAPI_MSG_ERROR, rc,
3442 "error setmntent(%s)", MOUNTED);
3447 mnt = getmntent(fp);
3451 if (!llapi_is_lustre_mnt(mnt))
3454 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
3457 llapi_error(LLAPI_MSG_ERROR, rc,
3458 "Can't open '%s'\n", mnt->mnt_dir);
3462 rc = ioctl(fd, LL_IOC_RMTACL, ops);
3466 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d\n", fd);
3475 return ((rc != 0) ? rc : found);
3478 static char *next_token(char *p, int div)
3484 while (*p && *p != ':' && !isspace(*p))
3487 while (*p == ':' || isspace(*p))
3490 return *p ? p : NULL;
3493 static int rmtacl_name2id(char *name, int is_user)
3498 pw = getpwnam(name);
3502 return (int)(pw->pw_uid);
3506 gr = getgrnam(name);
3510 return (int)(gr->gr_gid);
3514 static int isodigit(int c)
3516 return (c >= '0' && c <= '7') ? 1 : 0;
3520 * Whether the name is just digits string (uid/gid) already or not.
3525 static int str_is_id(char *str)
3532 if (*str == 'x' || *str == 'X') { /* for Hex. */
3533 if (!isxdigit(*(++str)))
3536 while (isxdigit(*(++str)));
3537 } else if (isodigit(*str)) { /* for Oct. */
3538 while (isodigit(*(++str)));
3540 } else if (isdigit(*str)) { /* for Dec. */
3541 while (isdigit(*(++str)));
3544 return (*str == 0) ? 1 : 0;
3554 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
3556 static rmtacl_name_t rmtacl_namelist[] = {
3557 { RMTACL_OPTNAME("user:"), 1, 0 },
3558 { RMTACL_OPTNAME("group:"), 0, 0 },
3559 { RMTACL_OPTNAME("default:user:"), 1, 0 },
3560 { RMTACL_OPTNAME("default:group:"), 0, 0 },
3561 /* for --tabular option */
3562 { RMTACL_OPTNAME("user"), 1, 1 },
3563 { RMTACL_OPTNAME("group"), 0, 1 },
3567 static int rgetfacl_output(char *str)
3569 char *start = NULL, *end = NULL;
3570 int is_user = 0, n, id;
3577 for (rn = rmtacl_namelist; rn->name; rn++) {
3578 if(strncmp(str, rn->name, rn->length) == 0) {
3579 if (!rn->next_token)
3580 start = str + rn->length;
3582 start = next_token(str + rn->length, 0);
3583 is_user = rn->is_user;
3588 end = next_token(start, 1);
3589 if (end == NULL || start == end) {
3590 n = printf("%s", str);
3596 id = rmtacl_name2id(start, is_user);
3597 if (id == INVALID_ID) {
3598 if (str_is_id(start)) {
3600 n = printf("%s", str);
3603 } else if ((id == NOBODY_UID && is_user) ||
3604 (id == NOBODY_GID && !is_user)) {
3606 n = printf("%s", str);
3610 n = printf("%s%d%s", str, id, end);
3615 static int child_status(int status)
3617 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
3620 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
3623 int fd[2], status, rc;
3630 llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe\n");
3637 llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork\n");
3642 /* child process redirects its output. */
3645 if (dup2(fd[1], 1) < 0) {
3647 llapi_error(LLAPI_MSG_ERROR, rc,
3648 "Can't dup2 %d\n", fd[1]);
3658 status = rmtacl_notify(ops);
3662 exit(execvp(argv[0], argv));
3665 /* the following is parent process */
3666 fp = fdopen(fd[0], "r");
3669 llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed\n", fd[0]);
3675 while (fgets(buf, PIPE_BUF, fp) != NULL) {
3676 if (output_func(buf) < 0)
3677 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
3683 if (waitpid(pid, &status, 0) < 0) {
3685 llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed\n", pid);
3689 return child_status(status);
3692 int llapi_lsetfacl(int argc, char *argv[])
3694 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
3697 int llapi_lgetfacl(int argc, char *argv[])
3699 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
3702 int llapi_rsetfacl(int argc, char *argv[])
3704 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
3707 int llapi_rgetfacl(int argc, char *argv[])
3709 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
3712 int llapi_cp(int argc, char *argv[])
3716 rc = rmtacl_notify(RMT_RSETFACL);
3720 exit(execvp(argv[0], argv));
3723 int llapi_ls(int argc, char *argv[])
3727 rc = rmtacl_notify(RMT_LGETFACL);
3731 exit(execvp(argv[0], argv));
3734 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
3735 * format must have %s%s, buf must be > 16
3736 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
3737 * then buf = "lustre-MDT0000"
3739 static int get_mdtname(char *name, char *format, char *buf)
3741 char suffix[]="-MDT0000";
3742 int len = strlen(name);
3744 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
3745 name[len - 5] = '\0';
3750 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
3753 /* Not enough room to add suffix */
3754 llapi_err_noerrno(LLAPI_MSG_ERROR,
3755 "MDT name too long |%s|", name);
3760 return sprintf(buf, format, name, suffix);
3763 /** ioctl on filsystem root, with mdtindex sent as data
3764 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
3765 * \param mdtidxp pointer to integer within data to be filled in with the
3766 * mdt index (0 if no mdt is specified). NULL won't be filled.
3768 int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
3775 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
3776 Open root and parse mdt index. */
3777 if (mdtname[0] == '/') {
3779 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
3780 (char *)mdtname, -1);
3782 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
3784 ptr = fsname + strlen(fsname) - 8;
3786 index = strtol(ptr + 4, NULL, 10);
3787 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
3791 llapi_err_noerrno(LLAPI_MSG_ERROR,
3792 "Can't open %s: %d\n", mdtname, rc);
3799 rc = ioctl(fd, opc, data);
3804 if (rc && want_error)
3805 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
3811 /****** Changelog API ********/
3813 static int changelog_ioctl(const char *mdtname, int opc, int id,
3814 long long recno, int flags)
3816 struct ioc_changelog data;
3820 data.icc_recno = recno;
3821 data.icc_flags = flags;
3822 idx = (int *)(&data.icc_mdtindex);
3824 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
3827 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
3828 struct changelog_private {
3831 lustre_kernelcomm kuc;
3834 /** Start reading from a changelog
3835 * @param priv Opaque private control structure
3836 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
3837 * @param device Report changes recorded on this MDT
3838 * @param startrec Report changes beginning with this record number
3839 * (just call llapi_changelog_fini when done; don't need an endrec)
3841 int llapi_changelog_start(void **priv, int flags, const char *device,
3844 struct changelog_private *cp;
3847 /* Set up the receiver control struct */
3848 cp = calloc(1, sizeof(*cp));
3852 cp->magic = CHANGELOG_PRIV_MAGIC;
3855 /* Set up the receiver */
3856 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */);
3862 /* Tell the kernel to start sending */
3863 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
3865 /* Only the kernel reference keeps the write side open */
3866 close(cp->kuc.lk_wfd);
3869 /* frees and clears priv */
3870 llapi_changelog_fini(priv);
3881 /** Finish reading from a changelog */
3882 int llapi_changelog_fini(void **priv)
3884 struct changelog_private *cp = (struct changelog_private *)*priv;
3886 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3889 libcfs_ukuc_stop(&cp->kuc);
3895 /** Convert a changelog_rec to changelog_ext_rec, in this way client can treat
3896 * all records in the format of changelog_ext_rec, this can make record
3899 static inline int changelog_extend_rec(struct changelog_ext_rec *ext)
3901 if (!CHANGELOG_REC_EXTENDED(ext)) {
3902 struct changelog_rec *rec = (struct changelog_rec *)ext;
3904 memmove(ext->cr_name, rec->cr_name, rec->cr_namelen);
3905 fid_zero(&ext->cr_sfid);
3906 fid_zero(&ext->cr_spfid);
3913 /** Read the next changelog entry
3914 * @param priv Opaque private control structure
3915 * @param rech Changelog record handle; record will be allocated here
3916 * @return 0 valid message received; rec is set
3920 int llapi_changelog_recv(void *priv, struct changelog_ext_rec **rech)
3922 struct changelog_private *cp = (struct changelog_private *)priv;
3923 struct kuc_hdr *kuch;
3926 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3930 kuch = malloc(CR_MAXSIZE + sizeof(*kuch));
3935 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
3936 CR_MAXSIZE + sizeof(*kuch),
3937 KUC_TRANSPORT_CHANGELOG);
3941 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
3942 ((kuch->kuc_msgtype != CL_RECORD) &&
3943 (kuch->kuc_msgtype != CL_EOF))) {
3944 llapi_err_noerrno(LLAPI_MSG_ERROR,
3945 "Unknown changelog message type %d:%d\n",
3946 kuch->kuc_transport, kuch->kuc_msgtype);
3951 if (kuch->kuc_msgtype == CL_EOF) {
3952 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
3961 /* Our message is a changelog_ext_rec. Use pointer math to skip
3962 * kuch_hdr and point directly to the message payload.
3964 *rech = (struct changelog_ext_rec *)(kuch + 1);
3965 changelog_extend_rec(*rech);
3975 /** Release the changelog record when done with it. */
3976 int llapi_changelog_free(struct changelog_ext_rec **rech)
3979 /* We allocated memory starting at the kuc_hdr, but passed
3980 * the consumer a pointer to the payload.
3981 * Use pointer math to get back to the header.
3983 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
3990 int llapi_changelog_clear(const char *mdtname, const char *idstr,
3996 llapi_err_noerrno(LLAPI_MSG_ERROR,
3997 "can't purge negative records\n");
4001 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
4002 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
4003 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
4004 llapi_err_noerrno(LLAPI_MSG_ERROR,
4005 "expecting id of the form '"
4006 CHANGELOG_USER_PREFIX
4007 "<num>'; got '%s'\n", idstr);
4011 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
4014 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
4015 int buflen, long long *recno, int *linkno)
4018 struct getinfo_fid2path *gf;
4021 while (*fidstr == '[')
4024 sscanf(fidstr, SFID, RFID(&fid));
4025 if (!fid_is_sane(&fid)) {
4026 llapi_err_noerrno(LLAPI_MSG_ERROR,
4027 "bad FID format [%s], should be "DFID"\n",
4028 fidstr, (__u64)1, 2, 0);
4032 gf = malloc(sizeof(*gf) + buflen);
4036 gf->gf_recno = *recno;
4037 gf->gf_linkno = *linkno;
4038 gf->gf_pathlen = buflen;
4040 /* Take path or fsname */
4041 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
4044 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
4046 memcpy(buf, gf->gf_path, gf->gf_pathlen);
4047 *recno = gf->gf_recno;
4048 *linkno = gf->gf_linkno;
4055 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
4058 struct lustre_mdt_attrs *lma;
4062 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
4064 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
4067 lma = (struct lustre_mdt_attrs *)buf;
4068 fid_le_to_cpu(fid, &lma->lma_self_fid);
4072 int llapi_fd2fid(const int fd, lustre_fid *fid)
4076 memset(fid, 0, sizeof(*fid));
4078 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
4079 if (rc == -EINVAL || rc == -ENOTTY)
4080 rc = fid_from_lma(NULL, fd, fid);
4085 int llapi_path2fid(const char *path, lustre_fid *fid)
4089 memset(fid, 0, sizeof(*fid));
4090 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
4092 if (errno == ELOOP || errno == ENXIO)
4093 return fid_from_lma(path, -1, fid);
4097 rc = llapi_fd2fid(fd, fid);
4098 if (rc == -EINVAL || rc == -ENOTTY)
4099 rc = fid_from_lma(path, -1, fid);
4105 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
4110 root = opendir(mnt);
4113 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
4117 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
4120 llapi_error(LLAPI_MSG_ERROR, rc,
4121 "ioctl on %s for getting connect flags failed", mnt);
4127 int llapi_get_version(char *buffer, int buffer_size,
4132 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
4134 fd = open(OBD_DEV_PATH, O_RDONLY);
4138 memset(buffer, 0, buffer_size);
4139 data->ioc_version = OBD_IOCTL_VERSION;
4140 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
4141 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
4142 data->ioc_len = obd_ioctl_packlen(data);
4144 rc = ioctl(fd, OBD_GET_VERSION, buffer);
4151 *version = data->ioc_bulk;
4156 * Get a 64-bit value representing the version of file data pointed by fd.
4158 * Each write or truncate, flushed on OST, will change this value. You can use
4159 * this value to verify if file data was modified. This only checks the file
4160 * data, not metadata.
4162 * \param flags If set to LL_DV_NOFLUSH, the data version will be read
4163 * directly from OST without regard to possible dirty cache on
4166 * \retval 0 on success.
4167 * \retval -errno on error.
4169 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4172 struct ioc_data_version idv;
4174 idv.idv_flags = flags;
4176 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4180 *data_version = idv.idv_version;
4186 * Create a volatile file and open it for write:
4187 * - file is created as a standard file in the directory
4188 * - file does not appears in directory and directory mtime does not change
4189 * - file is removed at close
4190 * - file modes are rw-------, if user wants another one it must use fchmod()
4191 * \param directory Directory where the file is created
4192 * \param idx MDT index on which the file is created
4193 * \param flags Std open flags
4195 * \retval 0 on success.
4196 * \retval -errno on error.
4198 int llapi_create_volatile_idx(char *directory, int idx, int mode)
4200 char file_path[PATH_MAX];
4201 char filename[PATH_MAX];
4206 fd = open("/dev/urandom", O_RDONLY);
4208 llapi_error(LLAPI_MSG_ERROR, errno,
4209 "Cannot open /dev/urandom\n");
4212 rc = read(fd, &random, sizeof(random));
4214 if (rc < sizeof(random)) {
4215 llapi_error(LLAPI_MSG_ERROR, errno,
4216 "Cannot read %d bytes from /dev/urandom\n",
4221 sprintf(filename, LUSTRE_VOLATILE_HDR"::%.4X", random);
4223 sprintf(filename, LUSTRE_VOLATILE_IDX"%.4X", 0, random);
4225 sprintf(file_path, "%s/%s", directory, filename);
4227 fd = open(file_path, O_RDWR|O_CREAT|mode, S_IRUSR|S_IWUSR);
4229 llapi_error(LLAPI_MSG_ERROR, errno,
4230 "Cannot create volatile file %s in %s\n",
4231 filename + LUSTRE_VOLATILE_HDR_LEN,
4239 * Swap the layouts between 2 file descriptors
4240 * the 2 files must be open in write
4241 * first fd received the ioctl, second fd is passed as arg
4242 * this is assymetric but avoid use of root path for ioctl
4244 int llapi_fswap_layouts(int fd1, int fd2, __u64 dv1, __u64 dv2, __u64 flags)
4246 struct lustre_swap_layouts lsl;
4249 srandom(time(NULL));
4251 lsl.sl_flags = flags;
4252 lsl.sl_gid = random();
4255 rc = ioctl(fd1, LL_IOC_LOV_SWAP_LAYOUTS, &lsl);
4262 * Swap the layouts between 2 files
4263 * the 2 files are open in write
4265 int llapi_swap_layouts(const char *path1, const char *path2,
4266 __u64 dv1, __u64 dv2, __u64 flags)
4270 fd1 = open(path1, O_WRONLY | O_LOV_DELAY_CREATE);
4272 llapi_error(LLAPI_MSG_ERROR, -errno,
4273 "error: cannot open for write %s",
4278 fd2 = open(path2, O_WRONLY | O_LOV_DELAY_CREATE);
4280 llapi_error(LLAPI_MSG_ERROR, -errno,
4281 "error: cannot open for write %s",
4287 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2, flags);
4289 llapi_error(LLAPI_MSG_ERROR, rc,
4290 "error: cannot swap layouts between %s and %s\n",