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, 2012, 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 #ifdef HAVE_LINUX_UNISTD_H
65 #include <linux/unistd.h>
71 #include <liblustre.h>
72 #include <lnet/lnetctl.h>
75 #include <lustre/lustreapi.h>
77 static unsigned llapi_dir_filetype_table[] = {
86 #if defined(DT_DOOR) && defined(S_IFDOOR)
91 #if defined(DT_DOOR) && defined(S_IFDOOR)
92 static const int DT_MAX = DT_DOOR;
94 static const int DT_MAX = DT_SOCK;
97 static unsigned llapi_filetype_dir_table[] = {
106 #if defined(DT_DOOR) && defined(S_IFDOOR)
111 #if defined(DT_DOOR) && defined(S_IFDOOR)
112 static const int S_IFMAX = DT_DOOR;
114 static const int S_IFMAX = DT_SOCK;
117 /* liblustreapi message level */
118 static int llapi_msg_level = LLAPI_MSG_MAX;
120 void llapi_msg_set_level(int level)
122 /* ensure level is in the good range */
123 if (level < LLAPI_MSG_OFF)
124 llapi_msg_level = LLAPI_MSG_OFF;
125 else if (level > LLAPI_MSG_MAX)
126 llapi_msg_level = LLAPI_MSG_MAX;
128 llapi_msg_level = level;
131 /* llapi_error will preserve errno */
132 void llapi_error(int level, int _rc, char *fmt, ...)
135 int tmp_errno = errno;
136 /* to protect using errno as _rc argument */
139 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
143 vfprintf(stderr, fmt, args);
146 if (level & LLAPI_MSG_NO_ERRNO)
147 fprintf(stderr, "\n");
149 fprintf(stderr, ": %s (%d)\n", strerror(rc), rc);
153 /* llapi_printf will preserve errno */
154 void llapi_printf(int level, char *fmt, ...)
157 int tmp_errno = errno;
159 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
163 vfprintf(stdout, fmt, args);
169 * size_units is to be initialized (or zeroed) by caller.
171 int parse_size(char *optarg, unsigned long long *size,
172 unsigned long long *size_units, int bytes_spec)
176 if (strncmp(optarg, "-", 1) == 0)
179 if (*size_units == 0)
182 *size = strtoull(optarg, &end, 0);
185 if ((*end == 'b') && *(end + 1) == '\0' &&
186 (*size & (~0ULL << (64 - 9))) == 0 &&
188 *size_units = 1 << 9;
189 } else if ((*end == 'b') &&
190 *(end + 1) == '\0' &&
193 } else if ((*end == 'k' || *end == 'K') &&
194 *(end + 1) == '\0' &&
195 (*size & (~0ULL << (64 - 10))) == 0) {
196 *size_units = 1 << 10;
197 } else if ((*end == 'm' || *end == 'M') &&
198 *(end + 1) == '\0' &&
199 (*size & (~0ULL << (64 - 20))) == 0) {
200 *size_units = 1 << 20;
201 } else if ((*end == 'g' || *end == 'G') &&
202 *(end + 1) == '\0' &&
203 (*size & (~0ULL << (64 - 30))) == 0) {
204 *size_units = 1 << 30;
205 } else if ((*end == 't' || *end == 'T') &&
206 *(end + 1) == '\0' &&
207 (*size & (~0ULL << (64 - 40))) == 0) {
208 *size_units = 1ULL << 40;
209 } else if ((*end == 'p' || *end == 'P') &&
210 *(end + 1) == '\0' &&
211 (*size & (~0ULL << (64 - 50))) == 0) {
212 *size_units = 1ULL << 50;
213 } else if ((*end == 'e' || *end == 'E') &&
214 *(end + 1) == '\0' &&
215 (*size & (~0ULL << (64 - 60))) == 0) {
216 *size_units = 1ULL << 60;
221 *size *= *size_units;
225 /* XXX: llapi_xxx() functions return negative values upon failure */
227 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
228 int stripe_count, int stripe_pattern)
232 /* 64 KB is the largest common page size I'm aware of (on ia64), but
233 * check the local page size just in case. */
234 page_size = LOV_MIN_STRIPE_SIZE;
235 if (getpagesize() > page_size) {
236 page_size = getpagesize();
237 llapi_err_noerrno(LLAPI_MSG_WARN,
238 "warning: your page size (%u) is "
239 "larger than expected (%u)", page_size,
240 LOV_MIN_STRIPE_SIZE);
242 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
244 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %lu, "
245 "must be an even multiple of %d bytes",
246 stripe_size, page_size);
249 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
251 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
255 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
257 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
261 if (stripe_size >= (1ULL << 32)) {
263 llapi_error(LLAPI_MSG_ERROR, rc,
264 "warning: stripe size 4G or larger "
265 "is not currently supported and would wrap");
271 /* return the first file matching this pattern */
272 static int first_match(char *pattern, char *buffer)
276 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
279 if (glob_info.gl_pathc < 1) {
280 globfree(&glob_info);
284 strcpy(buffer, glob_info.gl_pathv[0]);
286 globfree(&glob_info);
290 static int find_target_obdpath(char *fsname, char *path)
293 char pattern[PATH_MAX + 1];
296 snprintf(pattern, PATH_MAX,
297 "/proc/fs/lustre/lov/%s-*/target_obd",
299 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
300 if (rc == GLOB_NOMATCH)
305 strcpy(path, glob_info.gl_pathv[0]);
306 globfree(&glob_info);
310 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
313 char pattern[PATH_MAX + 1];
316 snprintf(pattern, PATH_MAX,
317 "/proc/fs/lustre/lov/%s-*/pools/%s",
319 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
320 /* If no pools, make sure the lov is available */
321 if ((rc == GLOB_NOMATCH) &&
322 (find_target_obdpath(fsname, poolpath) == -ENODEV))
327 strcpy(poolpath, glob_info.gl_pathv[0]);
328 globfree(&glob_info);
333 * return a parameter string for a specific device type or mountpoint
335 * \param param_path the path to the file containing parameter data
336 * \param result buffer for parameter value string
337 * \param result_size size of buffer for return value
339 * The \param param_path is appended to /proc/{fs,sys}/{lnet,lustre} to
340 * complete the absolute path to the file containing the parameter data
341 * the user is requesting. If that file exist then the data is read from
342 * the file and placed into the \param result buffer that is passed by
343 * the user. Data is only copied up to the \param result_size to prevent
344 * overflow of the array.
346 * Return 0 for success, with a NUL-terminated string in \param result.
347 * Return -ve value for error.
349 static int get_param(const char *param_path, char *result,
350 unsigned int result_size)
352 char file[PATH_MAX + 1], pattern[PATH_MAX + 1], buf[result_size];
356 snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
358 rc = first_match(pattern, file);
362 fp = fopen(file, "r");
364 while (fgets(buf, result_size, fp) != NULL)
373 #define DEVICES_LIST "/proc/fs/lustre/devices"
376 * return a parameter string for a specific device type or mountpoint
378 * \param fsname Lustre filesystem name (optional)
379 * \param file_path path to file in filesystem (optional, if fsname unset)
380 * \param obd_type Lustre OBD device type
381 * \param param_name parameter name to fetch
382 * \param value return buffer for parameter value string
383 * \param val_len size of buffer for return value
385 * If fsname is specified then the parameter will be from that filesystem
386 * (if it exists). If file_path is given and it is in a mounted Lustre
387 * filesystem, then the parameter will be otherwise the value may be
388 * from any mounted filesystem (if there is more than one).
390 * If "obd_type" matches a Lustre device then the first matching device
391 * (as with "lctl dl", constrained by \param fsname or \param mount_path)
392 * will be used to provide the return value, otherwise the first such
393 * device found will be used.
395 * Return 0 for success, with a NUL-terminated string in \param buffer.
396 * Return -ve value for error.
398 static int get_param_obdvar(const char *fsname, const char *file_path,
399 const char *obd_type, const char *param_name,
400 char *value, unsigned int val_len)
402 char devices[PATH_MAX + 1], dev[PATH_MAX + 1] = "*", fs[PATH_MAX + 1];
403 FILE *fp = fopen(DEVICES_LIST, "r");
406 if (!fsname && file_path) {
407 rc = llapi_search_fsname(file_path, fs);
409 llapi_error(LLAPI_MSG_ERROR, rc,
410 "'%s' is not on a Lustre filesystem",
422 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
426 while (fgets(devices, sizeof(devices), fp) != NULL) {
427 char *bufp = devices, *tmp;
429 while (bufp[0] == ' ')
432 tmp = strstr(bufp, obd_type);
434 tmp += strlen(obd_type) + 1;
438 tmp = strchr(dev, ' ');
444 if (dev[0] == '*' && strlen(fs))
445 snprintf(dev, PATH_MAX, "%s-*", fs);
446 snprintf(devices, PATH_MAX, "%s/%s/%s", obd_type, dev, param_name);
448 return get_param(devices, value, val_len);
451 static int get_mds_md_size(char *path)
453 int lumlen = lov_mds_md_size(LOV_MAX_STRIPE_COUNT, LOV_MAGIC_V3);
456 /* Now get the maxea from llite proc */
457 if (!get_param_obdvar(NULL, path, "llite", "max_easize",
464 * if pool is NULL, search ostname in target_obd
465 * if pool is not NULL:
466 * if pool not found returns errno < 0
467 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
468 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
470 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
473 char buffer[PATH_MAX + 1];
477 len = strlen(ostname);
479 if (poolname == NULL)
480 rc = find_target_obdpath(fsname, buffer);
482 rc = find_poolpath(fsname, poolname, buffer);
486 fd = fopen(buffer, "r");
490 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
491 if (poolname == NULL) {
493 /* Search for an ostname in the list of OSTs
494 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
495 ptr = strchr(buffer, ' ');
497 (strncmp(ptr + 1, ostname, len) == 0)) {
502 /* Search for an ostname in a pool,
503 (or an existing non-empty pool if no ostname) */
504 if ((ostname == NULL) ||
505 (strncmp(buffer, ostname, len) == 0)) {
515 int llapi_file_open_pool(const char *name, int flags, int mode,
516 unsigned long long stripe_size, int stripe_offset,
517 int stripe_count, int stripe_pattern, char *pool_name)
519 struct lov_user_md_v3 lum = { 0 };
523 /* Make sure we have a good pool */
524 if (pool_name != NULL) {
525 char fsname[MAX_OBD_NAME + 1], *ptr;
527 rc = llapi_search_fsname(name, fsname);
529 llapi_error(LLAPI_MSG_ERROR, rc,
530 "'%s' is not on a Lustre filesystem",
535 /* in case user gives the full pool name <fsname>.<poolname>,
536 * strip the fsname */
537 ptr = strchr(pool_name, '.');
540 if (strcmp(pool_name, fsname) != 0) {
542 llapi_err_noerrno(LLAPI_MSG_ERROR,
543 "Pool '%s' is not on filesystem '%s'",
550 /* Make sure the pool exists and is non-empty */
551 rc = llapi_search_ost(fsname, pool_name, NULL);
553 llapi_err_noerrno(LLAPI_MSG_ERROR,
554 "pool '%s.%s' %s", fsname, pool_name,
555 rc == 0 ? "has no OSTs" : "does not exist");
560 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
561 if (fd < 0 && errno == EISDIR) {
562 fd = open(name, O_DIRECTORY | O_RDONLY);
568 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
572 rc = llapi_stripe_limit_check(stripe_size, stripe_offset, stripe_count,
577 /* Initialize IOCTL striping pattern structure */
578 lum.lmm_magic = LOV_USER_MAGIC_V3;
579 lum.lmm_pattern = stripe_pattern;
580 lum.lmm_stripe_size = stripe_size;
581 lum.lmm_stripe_count = stripe_count;
582 lum.lmm_stripe_offset = stripe_offset;
583 if (pool_name != NULL) {
584 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
586 /* If no pool is specified at all, use V1 request */
587 lum.lmm_magic = LOV_USER_MAGIC_V1;
590 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
591 char *errmsg = "stripe already set";
593 if (errno != EEXIST && errno != EALREADY)
594 errmsg = strerror(errno);
596 llapi_err_noerrno(LLAPI_MSG_ERROR,
597 "error on ioctl "LPX64" for '%s' (%d): %s",
598 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
609 int llapi_file_open(const char *name, int flags, int mode,
610 unsigned long long stripe_size, int stripe_offset,
611 int stripe_count, int stripe_pattern)
613 return llapi_file_open_pool(name, flags, mode, stripe_size,
614 stripe_offset, stripe_count,
615 stripe_pattern, NULL);
618 int llapi_file_create(const char *name, unsigned long long stripe_size,
619 int stripe_offset, int stripe_count, int stripe_pattern)
623 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
624 stripe_offset, stripe_count, stripe_pattern,
633 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
634 int stripe_offset, int stripe_count,
635 int stripe_pattern, char *pool_name)
639 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
640 stripe_offset, stripe_count, stripe_pattern,
650 * Find the fsname, the full path, and/or an open fd.
651 * Either the fsname or path must not be NULL
653 #define WANT_PATH 0x1
654 #define WANT_FSNAME 0x2
656 #define WANT_INDEX 0x8
657 #define WANT_ERROR 0x10
658 static int get_root_path(int want, char *fsname, int *outfd, char *path,
662 char buf[PATH_MAX], mntdir[PATH_MAX];
665 int idx = 0, len = 0, mntlen, fd;
668 /* get the mount point */
669 fp = setmntent(MOUNTED, "r");
672 llapi_error(LLAPI_MSG_ERROR, rc,
673 "setmntent(%s) failed", MOUNTED);
677 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
680 if (!llapi_is_lustre_mnt(&mnt))
683 if ((want & WANT_INDEX) && (idx++ != index))
686 mntlen = strlen(mnt.mnt_dir);
687 ptr = strrchr(mnt.mnt_fsname, '/');
694 /* Check the fsname for a match, if given */
695 if (!(want & WANT_FSNAME) && fsname != NULL &&
696 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
699 /* If the path isn't set return the first one we find */
700 if (path == NULL || strlen(path) == 0) {
701 strcpy(mntdir, mnt.mnt_dir);
702 if ((want & WANT_FSNAME) && fsname != NULL)
706 /* Otherwise find the longest matching path */
707 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
708 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
709 strcpy(mntdir, mnt.mnt_dir);
711 if ((want & WANT_FSNAME) && fsname != NULL)
720 if ((want & WANT_PATH) && path != NULL)
721 strcpy(path, mntdir);
722 if (want & WANT_FD) {
723 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
726 llapi_error(LLAPI_MSG_ERROR, rc,
727 "error opening '%s'", mntdir);
733 } else if (want & WANT_ERROR)
734 llapi_err_noerrno(LLAPI_MSG_ERROR,
735 "can't find fs root for '%s': %d",
736 (want & WANT_PATH) ? fsname : path, rc);
741 * search lustre mounts
743 * Calling this function will return to the user the mount point, mntdir, and
744 * the file system name, fsname, if the user passed a buffer to this routine.
746 * The user inputs are pathname and index. If the pathname is supplied then
747 * the value of the index will be ignored. The pathname will return data if
748 * the pathname is located on a lustre mount. Index is used to pick which
749 * mount point you want in the case of multiple mounted lustre file systems.
750 * See function lfs_osts in lfs.c for a example of the index use.
752 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
755 int want = WANT_PATH, idx = -1;
757 if (!pathname || pathname[0] == '\0') {
761 strcpy(mntdir, pathname);
765 return get_root_path(want, fsname, NULL, mntdir, idx);
768 /* Given a path, find the corresponding Lustre fsname */
769 int llapi_search_fsname(const char *pathname, char *fsname)
774 path = realpath(pathname, NULL);
776 char buf[PATH_MAX + 1], *ptr;
779 if (pathname[0] != '/') {
780 /* Need an absolute path, but realpath() only works for
781 * pathnames that actually exist. We go through the
782 * extra hurdle of dirname(getcwd() + pathname) in
783 * case the relative pathname contains ".." in it. */
784 if (getcwd(buf, sizeof(buf) - 1) == NULL)
788 strncat(buf, pathname, sizeof(buf) - strlen(buf));
789 path = realpath(buf, NULL);
791 ptr = strrchr(buf, '/');
795 path = realpath(buf, NULL);
798 llapi_error(LLAPI_MSG_ERROR, rc,
799 "pathname '%s' cannot expand",
805 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
810 int llapi_getname(const char *path, char *buf, size_t size)
812 struct obd_uuid uuid_buf;
813 char *uuid = uuid_buf.uuid;
816 memset(&uuid_buf, 0, sizeof(uuid_buf));
817 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
821 /* We want to turn lustre-clilov-ffff88002738bc00 into
822 * lustre-ffff88002738bc00. */
824 nr = snprintf(buf, size, "%.*s-%s",
825 (int) (strlen(uuid) - 24), uuid,
826 uuid + strlen(uuid) - 16);
836 * find the pool directory path under /proc
837 * (can be also used to test if a fsname is known)
839 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
842 char pattern[PATH_MAX + 1];
843 char buffer[PATH_MAX];
845 if (fsname == NULL) {
846 rc = llapi_search_fsname(pathname, buffer);
850 strcpy(pathname, fsname);
853 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
854 rc = first_match(pattern, buffer);
858 /* in fsname test mode, pool_pathname is NULL */
859 if (pool_pathname != NULL)
860 strcpy(pool_pathname, buffer);
866 * Get the list of pool members.
867 * \param poolname string of format \<fsname\>.\<poolname\>
868 * \param members caller-allocated array of char*
869 * \param list_size size of the members array
870 * \param buffer caller-allocated buffer for storing OST names
871 * \param buffer_size size of the buffer
873 * \return number of members retrieved for this pool
874 * \retval -error failure
876 int llapi_get_poolmembers(const char *poolname, char **members,
877 int list_size, char *buffer, int buffer_size)
879 char fsname[PATH_MAX + 1];
881 char pathname[PATH_MAX + 1];
882 char path[PATH_MAX + 1];
889 /* name is FSNAME.POOLNAME */
890 if (strlen(poolname) > PATH_MAX)
892 strcpy(fsname, poolname);
893 pool = strchr(fsname, '.');
900 rc = poolpath(fsname, NULL, pathname);
902 llapi_error(LLAPI_MSG_ERROR, rc,
903 "Lustre filesystem '%s' not found",
908 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
909 sprintf(path, "%s/%s", pathname, pool);
910 fd = fopen(path, "r");
913 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
918 while (fgets(buf, sizeof(buf), fd) != NULL) {
919 if (nb_entries >= list_size) {
924 tmp = strchr(buf, '\n');
927 if (used + strlen(buf) + 1 > buffer_size) {
932 strcpy(buffer + used, buf);
933 members[nb_entries] = buffer + used;
934 used += strlen(buf) + 1;
944 * Get the list of pools in a filesystem.
945 * \param name filesystem name or path
946 * \param poollist caller-allocated array of char*
947 * \param list_size size of the poollist array
948 * \param buffer caller-allocated buffer for storing pool names
949 * \param buffer_size size of the buffer
951 * \return number of pools retrieved for this filesystem
952 * \retval -error failure
954 int llapi_get_poollist(const char *name, char **poollist, int list_size,
955 char *buffer, int buffer_size)
957 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
961 struct dirent *cookie = NULL;
963 unsigned int nb_entries = 0;
964 unsigned int used = 0;
967 /* initilize output array */
968 for (i = 0; i < list_size; i++)
971 /* is name a pathname ? */
972 ptr = strchr(name, '/');
974 /* only absolute pathname is supported */
978 if (!realpath(name, rname)) {
980 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
985 rc = poolpath(NULL, rname, pathname);
987 llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
988 " a Lustre filesystem", name);
991 strcpy(fsname, rname);
994 strcpy(fsname, name);
995 rc = poolpath(fsname, NULL, pathname);
998 llapi_error(LLAPI_MSG_ERROR, rc,
999 "Lustre filesystem '%s' not found", name);
1003 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1004 dir = opendir(pathname);
1007 llapi_error(LLAPI_MSG_ERROR, rc,
1008 "Could not open pool list for '%s'",
1014 rc = readdir_r(dir, &pool, &cookie);
1018 llapi_error(LLAPI_MSG_ERROR, rc,
1019 "Error reading pool list for '%s'", name);
1021 } else if ((rc == 0) && (cookie == NULL)) {
1022 /* end of directory */
1026 /* ignore . and .. */
1027 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1030 /* check output bounds */
1031 if (nb_entries >= list_size) {
1036 /* +2 for '.' and final '\0' */
1037 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1043 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1044 poollist[nb_entries] = buffer + used;
1045 used += strlen(pool.d_name) + strlen(fsname) + 2;
1051 return ((rc != 0) ? rc : nb_entries);
1054 /* wrapper for lfs.c and obd.c */
1055 int llapi_poollist(const char *name)
1057 /* list of pool names (assume that pool count is smaller
1059 char **list, *buffer = NULL, *path = NULL, *fsname = NULL;
1060 int obdcount, bufsize, rc, nb, i;
1061 char *poolname = NULL, *tmp = NULL, data[16];
1063 if (name[0] != '/') {
1064 fsname = strdup(name);
1065 poolname = strchr(fsname, '.');
1069 path = (char *) name;
1072 rc = get_param_obdvar(fsname, path, "lov", "numobd",
1073 data, sizeof(data));
1076 obdcount = atoi(data);
1078 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1079 * and also an array to store the pointers for all that
1080 * allocated space. */
1082 bufsize = sizeof(struct obd_uuid) * obdcount;
1083 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1084 if (buffer == NULL) {
1088 list = (char **) (buffer + bufsize);
1091 /* name is a path or fsname */
1092 nb = llapi_get_poollist(name, list, obdcount,
1095 /* name is a pool name (<fsname>.<poolname>) */
1096 nb = llapi_get_poolmembers(name, list, obdcount,
1100 if (nb == -EOVERFLOW) {
1103 goto retry_get_pools;
1106 for (i = 0; i < nb; i++)
1107 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1108 rc = (nb < 0 ? nb : 0);
1117 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
1118 void *data, cfs_dirent_t *de);
1120 #define OBD_NOT_FOUND (-1)
1122 static int common_param_init(struct find_param *param, char *path)
1124 param->lumlen = get_mds_md_size(path);
1125 param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
1126 if (param->lmd == NULL) {
1127 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1128 "error: allocation of %d bytes for ioctl",
1129 sizeof(lstat_t) + param->lumlen);
1133 param->got_uuids = 0;
1134 param->obdindexes = NULL;
1135 param->obdindex = OBD_NOT_FOUND;
1136 param->mdtindex = OBD_NOT_FOUND;
1140 static void find_param_fini(struct find_param *param)
1142 if (param->obdindexes)
1143 free(param->obdindexes);
1149 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1152 struct find_param *param = (struct find_param *)data;
1157 /* set errno upon failure */
1158 static DIR *opendir_parent(char *path)
1164 fname = strrchr(path, '/');
1166 return opendir(".");
1170 parent = opendir(path);
1175 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1176 struct lov_user_mds_data *lmd, int lumlen)
1178 lstat_t *st = &lmd->lmd_st;
1181 if (parent == NULL && dir == NULL)
1185 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1186 } else if (parent) {
1187 char *fname = strrchr(path, '/');
1189 fname = (fname == NULL ? path : fname + 1);
1190 /* retrieve needed file info */
1191 strncpy((char *)lmd, fname, lumlen);
1192 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1198 if (errno == ENOTTY) {
1199 /* ioctl is not supported, it is not a lustre fs.
1200 * Do the regular lstat(2) instead. */
1201 ret = lstat_f(path, st);
1204 llapi_error(LLAPI_MSG_ERROR, ret,
1205 "error: %s: lstat failed for %s",
1208 } else if (errno == ENOENT) {
1210 llapi_error(LLAPI_MSG_WARN, ret,
1211 "warning: %s: %s does not exist",
1213 } else if (errno != EISDIR) {
1215 llapi_error(LLAPI_MSG_ERROR, ret,
1216 "%s ioctl failed for %s.",
1217 dir ? "LL_IOC_MDC_GETINFO" :
1218 "IOC_MDC_GETFILEINFO", path);
1221 llapi_error(LLAPI_MSG_ERROR, ret,
1222 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1229 int llapi_mds_getfileinfo(char *path, DIR *parent,
1230 struct lov_user_mds_data *lmd)
1232 int lumlen = get_mds_md_size(path);
1234 return get_lmd_info(path, parent, NULL, lmd, lumlen);
1237 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1238 semantic_func_t sem_init,
1239 semantic_func_t sem_fini, void *data,
1250 if (!d && errno != ENOTDIR) {
1252 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1255 } else if (!d && !parent) {
1256 /* ENOTDIR. Open the parent dir. */
1257 p = opendir_parent(path);
1259 GOTO(out, ret = -errno);
1262 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1268 while ((dent = readdir64(d)) != NULL) {
1269 ((struct find_param *)data)->have_fileinfo = 0;
1271 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1274 /* Don't traverse .lustre directory */
1275 if (!(strcmp(dent->d_name, dot_lustre_name)))
1279 if ((len + dent->d_reclen + 2) > size) {
1280 llapi_err_noerrno(LLAPI_MSG_ERROR,
1281 "error: %s: string buffer is too small",
1286 strcat(path, dent->d_name);
1288 if (dent->d_type == DT_UNKNOWN) {
1289 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1291 ret = llapi_mds_getfileinfo(path, d,
1292 ((struct find_param *)data)->lmd);
1294 ((struct find_param *)data)->have_fileinfo = 1;
1296 llapi_filetype_dir_table[st->st_mode &
1303 switch (dent->d_type) {
1305 llapi_err_noerrno(LLAPI_MSG_ERROR,
1306 "error: %s: '%s' is UNKNOWN type %d",
1307 __func__, dent->d_name, dent->d_type);
1310 ret = llapi_semantic_traverse(path, size, d, sem_init,
1311 sem_fini, data, dent);
1318 ret = sem_init(path, d, NULL, data, dent);
1322 if (sem_fini && ret == 0)
1323 sem_fini(path, d, NULL, data, dent);
1331 sem_fini(path, parent, d, data, de);
1340 static int param_callback(char *path, semantic_func_t sem_init,
1341 semantic_func_t sem_fini, struct find_param *param)
1343 int ret, len = strlen(path);
1346 if (len > PATH_MAX) {
1348 llapi_error(LLAPI_MSG_ERROR, ret,
1349 "Path name '%s' is too long", path);
1353 buf = (char *)malloc(PATH_MAX + 1);
1357 strncpy(buf, path, PATH_MAX + 1);
1358 ret = common_param_init(param, buf);
1363 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1364 sem_fini, param, NULL);
1366 find_param_fini(param);
1368 return ret < 0 ? ret : 0;
1371 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1373 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1376 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1381 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1383 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1386 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1391 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1395 fd = open(path, O_RDONLY);
1398 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1402 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1413 * If uuidp is NULL, return the number of available obd uuids.
1414 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1415 * there are more OSTs then allocated to uuidp, then an error is returned with
1416 * the ost_count set to number of available obd uuids.
1418 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1419 int *ost_count, enum tgt_type type)
1421 struct obd_uuid name;
1424 int rc = 0, index = 0;
1426 /* Get the lov name */
1427 if (type == LOV_TYPE) {
1428 rc = llapi_file_fget_lov_uuid(fd, &name);
1432 rc = llapi_file_fget_lmv_uuid(fd, &name);
1437 /* Now get the ost uuids from /proc */
1438 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1439 type == LOV_TYPE ? "lov" : "lmv", name.uuid);
1440 fp = fopen(buf, "r");
1443 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1447 while (fgets(buf, sizeof(buf), fp) != NULL) {
1448 if (uuidp && (index < *ost_count)) {
1449 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
1457 if (uuidp && (index > *ost_count))
1464 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1466 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1469 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1474 root = opendir(mnt);
1477 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1482 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1490 /* Check if user specified value matches a real uuid. Ignore _UUID,
1491 * -osc-4ba41334, other trailing gunk in comparison.
1492 * @param real_uuid ends in "_UUID"
1493 * @param search_uuid may or may not end in "_UUID"
1495 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1497 int cmplen = strlen(real_uuid);
1498 int searchlen = strlen(search_uuid);
1500 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1502 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1505 /* The UUIDs may legitimately be different lengths, if
1506 * the system was upgraded from an older version. */
1507 if (cmplen != searchlen)
1510 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1513 /* Here, param->obduuid points to a single obduuid, the index of which is
1514 * returned in param->obdindex */
1515 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1517 struct obd_uuid obd_uuid;
1518 char uuid[sizeof(struct obd_uuid)];
1523 if (param->got_uuids)
1526 /* Get the lov/lmv name */
1528 rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
1530 rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
1532 if (rc != -ENOTTY) {
1533 llapi_error(LLAPI_MSG_ERROR, rc,
1534 "error: can't get lov name: %s", dname);
1541 param->got_uuids = 1;
1543 /* Now get the ost uuids from /proc */
1544 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1545 param->get_lmv ? "lmv" : "lov", obd_uuid.uuid);
1546 fp = fopen(buf, "r");
1549 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1553 if (!param->obduuid && !param->quiet && !param->obds_printed)
1554 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1555 param->get_lmv ? "MDTS" : "OBDS:");
1557 while (fgets(buf, sizeof(buf), fp) != NULL) {
1558 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
1561 if (param->obduuid) {
1562 if (llapi_uuid_match(uuid, param->obduuid->uuid)) {
1563 param->obdindex = index;
1566 } else if (!param->quiet && !param->obds_printed) {
1567 /* Print everything */
1568 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1571 param->obds_printed = 1;
1575 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1576 llapi_err_noerrno(LLAPI_MSG_ERROR,
1577 "error: %s: unknown obduuid: %s",
1578 __func__, param->obduuid->uuid);
1585 /* In this case, param->obduuid will be an array of obduuids and
1586 * obd index for all these obduuids will be returned in
1587 * param->obdindexes */
1588 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1589 int num_obds, int **obdindexes, int *obdindex,
1592 int ret, obdcount, obd_valid = 0, obdnum, i;
1593 struct obd_uuid *uuids = NULL;
1597 ret = get_param_obdvar(NULL, path, type == LOV_TYPE ? "lov" : "lmv",
1598 "numobd", buf, sizeof(buf));
1602 obdcount = atoi(buf);
1603 uuids = (struct obd_uuid *)malloc(obdcount *
1604 sizeof(struct obd_uuid));
1609 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1611 struct obd_uuid *uuids_temp;
1613 if (ret == -EOVERFLOW) {
1614 uuids_temp = realloc(uuids, obdcount *
1615 sizeof(struct obd_uuid));
1616 if (uuids_temp != NULL) {
1618 goto retry_get_uuids;
1624 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
1628 indexes = malloc(num_obds * sizeof(*obdindex));
1629 if (indexes == NULL) {
1634 for (obdnum = 0; obdnum < num_obds; obdnum++) {
1637 /* The user may have specified a simple index */
1638 i = strtol(obduuids[obdnum].uuid, &end, 0);
1639 if (end && *end == '\0' && i < obdcount) {
1640 indexes[obdnum] = i;
1643 for (i = 0; i < obdcount; i++) {
1644 if (llapi_uuid_match(uuids[i].uuid,
1645 obduuids[obdnum].uuid)) {
1646 indexes[obdnum] = i;
1652 if (i >= obdcount) {
1653 indexes[obdnum] = OBD_NOT_FOUND;
1654 llapi_err_noerrno(LLAPI_MSG_ERROR,
1655 "error: %s: unknown obduuid: %s",
1656 __func__, obduuids[obdnum].uuid);
1662 *obdindex = OBD_NOT_FOUND;
1664 *obdindex = obd_valid;
1666 *obdindexes = indexes;
1674 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
1678 if (param->mdtuuid) {
1679 ret = setup_indexes(dir, path, param->mdtuuid, param->num_mdts,
1680 ¶m->mdtindexes, ¶m->mdtindex, LMV_TYPE);
1684 if (param->obduuid) {
1685 ret = setup_indexes(dir, path, param->obduuid, param->num_obds,
1686 ¶m->obdindexes, ¶m->obdindex, LOV_TYPE);
1690 param->got_uuids = 1;
1694 int llapi_ostlist(char *path, struct find_param *param)
1699 dir = opendir(path);
1703 ret = setup_obd_uuid(dir, path, param);
1710 * Given a filesystem name, or a pathname of a file on a lustre filesystem,
1711 * tries to determine the path to the filesystem's clilov directory under /proc
1713 * fsname is limited to MTI_NAME_MAXLEN in lustre_idl.h
1714 * The NUL terminator is compensated by the additional "%s" bytes. */
1715 #define LOV_LEN (sizeof("/proc/fs/lustre/lov/%s-clilov-*") + MTI_NAME_MAXLEN)
1716 static int clilovpath(const char *fsname, const char *const pathname,
1720 char pattern[LOV_LEN];
1721 char buffer[PATH_MAX + 1];
1723 if (fsname == NULL) {
1724 rc = llapi_search_fsname(pathname, buffer);
1730 snprintf(pattern, sizeof(pattern), "/proc/fs/lustre/lov/%s-clilov-*",
1733 rc = first_match(pattern, buffer);
1737 strncpy(clilovpath, buffer, sizeof(buffer));
1743 * Given the path to a stripe attribute proc file, tries to open and
1744 * read the attribute and return the value using the attr parameter
1746 static int sattr_read_attr(const char *const fpath,
1751 char line[PATH_MAX + 1];
1754 f = fopen(fpath, "r");
1757 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open '%s'", fpath);
1761 if (fgets(line, sizeof(line), f) != NULL) {
1764 llapi_error(LLAPI_MSG_ERROR, errno, "Cannot read from '%s'", fpath);
1773 * Tries to determine the default stripe attributes for a given filesystem. The
1774 * filesystem to check should be specified by fsname, or will be determined
1777 static int sattr_get_defaults(const char *const fsname,
1778 const char *const pathname,
1779 unsigned int *scount,
1780 unsigned int *ssize,
1781 unsigned int *soffset)
1784 char dpath[PATH_MAX + 1];
1785 char fpath[PATH_MAX + 1];
1787 rc = clilovpath(fsname, pathname, dpath);
1792 snprintf(fpath, PATH_MAX, "%s/stripecount", dpath);
1793 rc = sattr_read_attr(fpath, scount);
1799 snprintf(fpath, PATH_MAX, "%s/stripesize", dpath);
1800 rc = sattr_read_attr(fpath, ssize);
1806 snprintf(fpath, PATH_MAX, "%s/stripeoffset", dpath);
1807 rc = sattr_read_attr(fpath, soffset);
1816 * Tries to gather the default stripe attributes for a given filesystem. If
1817 * the attributes can be determined, they are cached for easy retreival the
1818 * next time they are needed. Only a single filesystem's attributes are
1821 static int sattr_cache_get_defaults(const char *const fsname,
1822 const char *const pathname,
1823 unsigned int *scount,
1824 unsigned int *ssize,
1825 unsigned int *soffset)
1828 char fsname[PATH_MAX + 1];
1829 unsigned int stripecount;
1830 unsigned int stripesize;
1831 unsigned int stripeoffset;
1837 char fsname_buf[PATH_MAX + 1];
1838 unsigned int tmp[3];
1840 if (fsname == NULL) {
1841 rc = llapi_search_fsname(pathname, fsname_buf);
1845 strncpy(fsname_buf, fsname, PATH_MAX);
1848 if (strncmp(fsname_buf, cache.fsname, PATH_MAX) != 0) {
1850 * Ensure all 3 sattrs (count, size, and offset) are
1851 * successfully retrieved and stored in tmp before writing to
1854 rc = sattr_get_defaults(fsname_buf, NULL, &tmp[0], &tmp[1],
1859 cache.stripecount = tmp[0];
1860 cache.stripesize = tmp[1];
1861 cache.stripeoffset = tmp[2];
1862 strncpy(cache.fsname, fsname_buf, PATH_MAX);
1866 *scount = cache.stripecount;
1868 *ssize = cache.stripesize;
1870 *soffset = cache.stripeoffset;
1875 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
1876 struct lov_user_ost_data_v1 *objects,
1877 int is_dir, int verbose, int depth,
1878 int raw, char *pool_name)
1880 char *prefix = is_dir ? "" : "lmm_";
1881 char nl = is_dir ? ' ' : '\n';
1884 if (is_dir && lum->lmm_object_seq == FID_SEQ_LOV_DEFAULT) {
1885 lum->lmm_object_seq = FID_SEQ_OST_MDT0;
1886 if (verbose & VERBOSE_DETAIL)
1887 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
1890 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
1891 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1893 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1894 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
1896 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
1897 lum->lmm_object_seq);
1898 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
1899 lum->lmm_object_id);
1902 if (verbose & VERBOSE_COUNT) {
1903 if (verbose & ~VERBOSE_COUNT)
1904 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
1907 if (!raw && lum->lmm_stripe_count == 0) {
1908 unsigned int scount;
1909 rc = sattr_cache_get_defaults(NULL, path,
1913 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1916 llapi_error(LLAPI_MSG_ERROR, rc,
1917 "Cannot determine default"
1920 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1921 lum->lmm_stripe_count ==
1922 (typeof(lum->lmm_stripe_count))(-1)
1923 ? -1 : lum->lmm_stripe_count, nl);
1926 llapi_printf(LLAPI_MSG_NORMAL, "%hd%c",
1927 (__s16)lum->lmm_stripe_count, nl);
1931 if (verbose & VERBOSE_SIZE) {
1932 if (verbose & ~VERBOSE_SIZE)
1933 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
1935 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
1937 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
1940 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", ssize,
1943 llapi_error(LLAPI_MSG_ERROR, rc,
1944 "Cannot determine default"
1947 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1948 lum->lmm_stripe_size, nl);
1952 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1953 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
1954 lum->lmm_pattern, nl);
1957 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
1958 if (verbose & ~VERBOSE_GENERATION)
1959 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
1961 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1962 (int)lum->u.lum_layout_gen, nl);
1965 if (verbose & VERBOSE_OFFSET) {
1966 if (verbose & ~VERBOSE_OFFSET)
1967 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
1970 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1971 lum->lmm_stripe_offset ==
1972 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
1973 lum->lmm_stripe_offset, nl);
1975 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1976 objects[0].l_ost_idx, nl);
1979 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
1980 if (verbose & ~VERBOSE_POOL)
1981 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
1983 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", pool_name, nl);
1986 if (is_dir && (verbose != VERBOSE_OBJID))
1987 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1990 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
1991 struct lov_user_ost_data_v1 *objects,
1992 char *path, int is_dir, int obdindex,
1993 int depth, int header, int raw)
1995 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
1998 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
1999 if (obdindex == objects[i].l_ost_idx) {
2007 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
2008 depth, raw, pool_name);
2010 if (!is_dir && (header & VERBOSE_OBJID)) {
2012 llapi_printf(LLAPI_MSG_NORMAL,
2013 "\tobdidx\t\t objid\t\t objid\t\t group\n");
2015 for (i = 0; i < lum->lmm_stripe_count; i++) {
2016 int idx = objects[i].l_ost_idx;
2017 long long oid = objects[i].l_object_id;
2018 long long gr = objects[i].l_object_seq;
2019 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) {
2021 sprintf(fmt, "%s%s%s\n",
2022 "\t%6u\t%14llu\t%#13llx\t",
2023 (fid_seq_is_rsvd(gr) ||
2024 fid_seq_is_mdt0(gr)) ?
2025 "%14llu" : "%#14llx", "%s");
2026 llapi_printf(LLAPI_MSG_NORMAL, fmt, idx, oid,
2028 obdindex == idx ? " *" : "");
2032 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2036 void llapi_lov_dump_user_lmm(struct find_param *param,
2037 char *path, int is_dir)
2039 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
2040 case LOV_USER_MAGIC_V1:
2041 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
2042 param->lmd->lmd_lmm.lmm_objects,
2044 param->obdindex, param->maxdepth,
2045 param->verbose, param->raw);
2047 case LOV_USER_MAGIC_V3: {
2048 char pool_name[LOV_MAXPOOLNAME + 1];
2049 struct lov_user_ost_data_v1 *objects;
2050 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2052 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
2053 pool_name[LOV_MAXPOOLNAME] = '\0';
2054 objects = lmmv3->lmm_objects;
2055 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
2056 objects, path, is_dir,
2057 param->obdindex, param->maxdepth,
2058 param->verbose, param->raw);
2062 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2063 "(expecting one of %#x %#x %#x)\n",
2064 *(__u32 *)¶m->lmd->lmd_lmm,
2065 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3);
2070 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2076 fname = strrchr(path, '/');
2078 /* It should be a file (or other non-directory) */
2079 if (fname == NULL) {
2080 dname = (char *)malloc(2);
2084 fname = (char *)path;
2086 dname = (char *)malloc(fname - path + 1);
2089 strncpy(dname, path, fname - path);
2090 dname[fname - path] = '\0';
2094 fd = open(dname, O_RDONLY);
2101 strcpy((char *)lum, fname);
2102 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2105 if (close(fd) == -1 && rc == 0)
2112 int llapi_file_lookup(int dirfd, const char *name)
2114 struct obd_ioctl_data data = { 0 };
2119 if (dirfd < 0 || name == NULL)
2122 data.ioc_version = OBD_IOCTL_VERSION;
2123 data.ioc_len = sizeof(data);
2124 data.ioc_inlbuf1 = (char *)name;
2125 data.ioc_inllen1 = strlen(name) + 1;
2127 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2129 llapi_error(LLAPI_MSG_ERROR, rc,
2130 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2135 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2141 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2142 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2144 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2145 * The table below gives the answers for the specified parameters (value and
2146 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2147 * --------------------------------------
2148 * 1 | file > limit; sign > 0 | -1 / -1 |
2149 * 2 | file = limit; sign > 0 | -1 / -1 |
2150 * 3 | file < limit; sign > 0 | ? / 1 |
2151 * 4 | file > limit; sign = 0 | -1 / -1 |
2152 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2153 * 6 | file < limit; sign = 0 | ? / -1 |
2154 * 7 | file > limit; sign < 0 | 1 / 1 |
2155 * 8 | file = limit; sign < 0 | ? / -1 |
2156 * 9 | file < limit; sign < 0 | ? / -1 |
2157 * --------------------------------------
2158 * Note: 5th actually means that the value is within the interval
2159 * (limit - margin, limit]. */
2160 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2161 int sign, int negopt, unsigned long long margin,
2167 /* Drop the fraction of margin (of days). */
2168 if (file + margin <= limit)
2170 } else if (sign == 0) {
2171 if (file <= limit && file + margin > limit)
2173 else if (file + margin <= limit)
2175 } else if (sign < 0) {
2182 return negopt ? ~ret + 1 : ret;
2185 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2186 * Return -1 or 1 if file timestamp does not or does match the given criteria
2187 * correspondingly. Return 0 if the MDS time is being checked and there are
2188 * attributes on OSTs and it is not yet clear if the timespamp matches.
2190 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2191 * updated timestamps. */
2192 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2197 /* Check if file is accepted. */
2199 ret = find_value_cmp(st->st_atime, param->atime,
2200 param->asign, param->exclude_atime,
2208 ret = find_value_cmp(st->st_mtime, param->mtime,
2209 param->msign, param->exclude_mtime,
2214 /* If the previous check matches, but this one is not yet clear,
2215 * we should return 0 to do an RPC on OSTs. */
2221 ret = find_value_cmp(st->st_ctime, param->ctime,
2222 param->csign, param->exclude_ctime,
2227 /* If the previous check matches, but this one is not yet clear,
2228 * we should return 0 to do an RPC on OSTs. */
2237 * Check whether the stripes matches the indexes user provided
2241 static int check_obd_match(struct find_param *param)
2243 lstat_t *st = ¶m->lmd->lmd_st;
2244 struct lov_user_ost_data_v1 *lmm_objects;
2247 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
2250 if (!S_ISREG(st->st_mode))
2253 /* Only those files should be accepted, which have a
2254 * stripe on the specified OST. */
2255 if (!param->lmd->lmd_lmm.lmm_stripe_count)
2258 if (param->lmd->lmd_lmm.lmm_magic ==
2259 LOV_USER_MAGIC_V3) {
2260 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2262 lmm_objects = lmmv3->lmm_objects;
2263 } else if (param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2264 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
2266 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2267 __func__, param->lmd->lmd_lmm.lmm_magic);
2271 for (i = 0; i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
2272 for (j = 0; j < param->num_obds; j++) {
2273 if (param->obdindexes[j] ==
2274 lmm_objects[i].l_ost_idx) {
2275 if (param->exclude_obd)
2282 if (param->exclude_obd)
2287 static int check_mdt_match(struct find_param *param)
2291 if (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND)
2294 /* FIXME: For striped dir, we should get stripe information and check */
2295 for (i = 0; i < param->num_mdts; i++) {
2296 if (param->mdtindexes[i] == param->file_mdtindex)
2297 if (param->exclude_mdt)
2302 if (param->exclude_mdt)
2308 * Check whether the obd is active or not, if it is
2309 * not active, just print the object affected by this
2312 static int print_failed_tgt(struct find_param *param, char *path, int type)
2314 struct obd_statfs stat_buf;
2315 struct obd_uuid uuid_buf;
2318 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2320 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2321 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2322 ret = llapi_obd_statfs(path, type,
2323 param->obdindex, &stat_buf,
2326 llapi_printf(LLAPI_MSG_NORMAL,
2327 "obd_uuid: %s failed %s ",
2328 param->obduuid->uuid,
2334 static int cb_find_init(char *path, DIR *parent, DIR *dir,
2335 void *data, cfs_dirent_t *de)
2337 struct find_param *param = (struct find_param *)data;
2338 int decision = 1; /* 1 is accepted; -1 is rejected. */
2339 lstat_t *st = ¶m->lmd->lmd_st;
2341 int checked_type = 0;
2344 LASSERT(parent != NULL || dir != NULL);
2346 if (param->have_fileinfo == 0)
2347 param->lmd->lmd_lmm.lmm_stripe_count = 0;
2349 /* If a regular expression is presented, make the initial decision */
2350 if (param->pattern != NULL) {
2351 char *fname = strrchr(path, '/');
2352 fname = (fname == NULL ? path : fname + 1);
2353 ret = fnmatch(param->pattern, fname, 0);
2354 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
2355 (ret == 0 && param->exclude_pattern))
2359 /* See if we can check the file type from the dirent. */
2360 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
2361 de->d_type < DT_MAX) {
2363 if (llapi_dir_filetype_table[de->d_type] == param->type) {
2364 if (param->exclude_type)
2367 if (!param->exclude_type)
2374 /* Request MDS for the stat info if some of these parameters need
2375 * to be compared. */
2376 if (param->obduuid || param->mdtuuid || param->check_uid ||
2377 param->check_gid || param->check_pool || param->atime ||
2378 param->ctime || param->mtime || param->check_size ||
2379 param->check_stripecount || param->check_stripesize)
2382 if (param->type && checked_type == 0)
2385 if (param->have_fileinfo == 0 && decision == 0) {
2386 ret = get_lmd_info(path, parent, dir, param->lmd,
2390 ret = llapi_file_fget_mdtidx(dirfd(dir),
2391 ¶m->file_mdtindex);
2396 ret = lstat_f(path, &tmp_st);
2399 llapi_error(LLAPI_MSG_ERROR, ret,
2400 "error: %s: lstat failed"
2401 "for %s", __func__, path);
2404 if (S_ISREG(tmp_st.st_mode)) {
2405 fd = open(path, O_RDONLY);
2407 ret = llapi_file_fget_mdtidx(fd,
2408 ¶m->file_mdtindex);
2414 /* For special inode, it assumes to
2415 * reside on the same MDT with the
2418 ret = llapi_file_fget_mdtidx(fd,
2419 ¶m->file_mdtindex);
2432 if (param->type && !checked_type) {
2433 if ((st->st_mode & S_IFMT) == param->type) {
2434 if (param->exclude_type)
2437 if (!param->exclude_type)
2443 if (param->obduuid || param->mdtuuid) {
2444 if (lustre_fs && param->got_uuids &&
2445 param->st_dev != st->st_dev) {
2446 /* A lustre/lustre mount point is crossed. */
2447 param->got_uuids = 0;
2448 param->obds_printed = 0;
2449 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2452 if (lustre_fs && !param->got_uuids) {
2453 ret = setup_target_indexes(dir ? dir : parent, path,
2458 param->st_dev = st->st_dev;
2459 } else if (!lustre_fs && param->got_uuids) {
2460 /* A lustre/non-lustre mount point is crossed. */
2461 param->got_uuids = 0;
2462 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2466 if (param->check_stripesize) {
2467 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_size,
2469 param->stripesize_sign,
2470 param->exclude_stripesize,
2471 param->stripesize_units, 0);
2476 if (param->check_stripecount) {
2477 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_count,
2479 param->stripecount_sign,
2480 param->exclude_stripecount, 1, 0);
2485 /* If an OBD UUID is specified but none matches, skip this file. */
2486 if ((param->obduuid && param->obdindex == OBD_NOT_FOUND) ||
2487 (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND))
2490 /* If a OST or MDT UUID is given, and some OST matches,
2492 if (param->obdindex != OBD_NOT_FOUND ||
2493 param->mdtindex != OBD_NOT_FOUND) {
2494 if (param->obduuid) {
2495 if (check_obd_match(param)) {
2496 /* If no mdtuuid is given, we are done.
2497 * Otherwise, fall through to the mdtuuid
2499 if (!param->mdtuuid)
2505 if (param->mdtuuid) {
2506 if (check_mdt_match(param))
2512 if (param->check_uid) {
2513 if (st->st_uid == param->uid) {
2514 if (param->exclude_uid)
2517 if (!param->exclude_uid)
2522 if (param->check_gid) {
2523 if (st->st_gid == param->gid) {
2524 if (param->exclude_gid)
2527 if (!param->exclude_gid)
2532 if (param->check_pool) {
2533 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2535 /* empty requested pool is taken as no pool search => V1 */
2536 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
2537 (param->poolname[0] == '\0')) ||
2538 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2539 (strncmp(lmmv3->lmm_pool_name,
2540 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
2541 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2542 (strcmp(param->poolname, "*") == 0))) {
2543 if (param->exclude_pool)
2546 if (!param->exclude_pool)
2551 /* Check the time on mds. */
2553 if (param->atime || param->ctime || param->mtime) {
2556 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
2557 param->lmd->lmd_lmm.lmm_stripe_count)
2559 decision = find_time_check(st, param, for_mds);
2564 /* If file still fits the request, ask ost for updated info.
2565 The regular stat is almost of the same speed as some new
2566 'glimpse-size-ioctl'. */
2568 if (param->check_size && S_ISREG(st->st_mode) &&
2569 param->lmd->lmd_lmm.lmm_stripe_count)
2573 /* For regular files with the stripe the decision may have not
2574 * been taken yet if *time or size is to be checked. */
2575 LASSERT((S_ISREG(st->st_mode) &&
2576 param->lmd->lmd_lmm.lmm_stripe_count) ||
2577 param->mdtindex != OBD_NOT_FOUND);
2579 if (param->obdindex != OBD_NOT_FOUND)
2580 print_failed_tgt(param, path, LL_STATFS_LOV);
2582 if (param->mdtindex != OBD_NOT_FOUND)
2583 print_failed_tgt(param, path, LL_STATFS_LMV);
2586 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
2587 (void *)param->lmd);
2588 } else if (parent) {
2589 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
2590 (void *)param->lmd);
2594 if (errno == ENOENT) {
2595 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
2596 "warning: %s: %s does not exist",
2601 llapi_error(LLAPI_MSG_ERROR, ret,
2602 "%s: IOC_LOV_GETINFO on %s failed",
2608 /* Check the time on osc. */
2609 decision = find_time_check(st, param, 0);
2616 if (param->check_size)
2617 decision = find_value_cmp(st->st_size, param->size,
2618 param->size_sign, param->exclude_size,
2619 param->size_units, 0);
2621 if (decision != -1) {
2622 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
2624 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
2626 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2630 /* Do not get down anymore? */
2631 if (param->depth == param->maxdepth)
2638 int llapi_find(char *path, struct find_param *param)
2640 return param_callback(path, cb_find_init, cb_common_fini, param);
2644 * Get MDT number that the file/directory inode referenced
2645 * by the open fd resides on.
2646 * Return 0 and mdtidx on success, or -ve errno.
2648 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
2650 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
2655 static int cb_get_mdt_index(char *path, DIR *parent, DIR *d, void *data,
2658 struct find_param *param = (struct find_param *)data;
2662 LASSERT(parent != NULL || d != NULL);
2665 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
2666 } else if (parent) {
2669 fd = open(path, O_RDONLY);
2671 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
2679 if (ret == -ENODATA) {
2680 if (!param->obduuid)
2681 llapi_printf(LLAPI_MSG_NORMAL,
2682 "%s has no stripe info\n", path);
2684 } else if (ret == -ENOENT) {
2685 llapi_error(LLAPI_MSG_WARN, ret,
2686 "warning: %s: %s does not exist",
2689 } else if (ret == -ENOTTY) {
2690 llapi_error(LLAPI_MSG_ERROR, ret,
2691 "%s: '%s' not on a Lustre fs?",
2694 llapi_error(LLAPI_MSG_ERROR, ret,
2695 "error: %s: LL_IOC_GET_MDTIDX failed for %s",
2701 /* The 'LASSERT(parent != NULL || d != NULL);' guarantees
2702 * that either 'd' or 'parent' is not null.
2703 * So in all cases llapi_file_fget_mdtidx() is called,
2704 * thus initializing 'mdtidx'. */
2705 if (param->quiet || !(param->verbose & VERBOSE_DETAIL))
2706 /* coverity[uninit_use_in_call] */
2707 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
2709 /* coverity[uninit_use_in_call] */
2710 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
2714 /* Do not get down anymore? */
2715 if (param->depth == param->maxdepth)
2722 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
2725 struct find_param *param = (struct find_param *)data;
2728 LASSERT(parent != NULL || d != NULL);
2730 if (param->obduuid) {
2732 ret = setup_obd_uuid(d ? d : parent, path, param);
2738 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
2739 (void *)¶m->lmd->lmd_lmm);
2740 } else if (parent) {
2741 char *fname = strrchr(path, '/');
2742 fname = (fname == NULL ? path : fname + 1);
2744 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
2746 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
2747 (void *)¶m->lmd->lmd_lmm);
2751 if (errno == ENODATA && d != NULL) {
2752 /* We need to "fake" the "use the default" values
2753 * since the lmm struct is zeroed out at this point.
2754 * The magic needs to be set in order to satisfy
2755 * a check later on in the code path.
2756 * The object_seq needs to be set for the "(Default)"
2757 * prefix to be displayed. */
2758 struct lov_user_md *lmm = ¶m->lmd->lmd_lmm;
2759 lmm->lmm_magic = LOV_MAGIC_V1;
2761 lmm->lmm_object_seq = FID_SEQ_LOV_DEFAULT;
2762 lmm->lmm_stripe_count = 0;
2763 lmm->lmm_stripe_size = 0;
2764 lmm->lmm_stripe_offset = -1;
2767 } else if (errno == ENODATA && parent != NULL) {
2768 if (!param->obduuid)
2769 llapi_printf(LLAPI_MSG_NORMAL,
2770 "%s has no stripe info\n", path);
2772 } else if (errno == ENOENT) {
2773 llapi_error(LLAPI_MSG_WARN, -ENOENT,
2774 "warning: %s: %s does not exist",
2777 } else if (errno == ENOTTY) {
2779 llapi_error(LLAPI_MSG_ERROR, ret,
2780 "%s: '%s' not on a Lustre fs?",
2784 llapi_error(LLAPI_MSG_ERROR, ret,
2785 "error: %s: %s failed for %s",
2786 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
2787 "IOC_MDC_GETFILESTRIPE", path);
2794 if (!(param->verbose & VERBOSE_MDTINDEX))
2795 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
2798 /* Do not get down anymore? */
2799 if (param->depth == param->maxdepth)
2806 int llapi_getstripe(char *path, struct find_param *param)
2808 return param_callback(path, (param->verbose & VERBOSE_MDTINDEX) ?
2809 cb_get_mdt_index : cb_getstripe,
2810 cb_common_fini, param);
2813 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
2814 struct obd_statfs *stat_buf,
2815 struct obd_uuid *uuid_buf)
2818 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
2820 struct obd_ioctl_data data = { 0 };
2823 data.ioc_inlbuf1 = (char *)&type;
2824 data.ioc_inllen1 = sizeof(__u32);
2825 data.ioc_inlbuf2 = (char *)&index;
2826 data.ioc_inllen2 = sizeof(__u32);
2827 data.ioc_pbuf1 = (char *)stat_buf;
2828 data.ioc_plen1 = sizeof(struct obd_statfs);
2829 data.ioc_pbuf2 = (char *)uuid_buf;
2830 data.ioc_plen2 = sizeof(struct obd_uuid);
2832 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
2834 llapi_error(LLAPI_MSG_ERROR, rc,
2835 "llapi_obd_statfs: error packing ioctl data");
2839 fd = open(path, O_RDONLY);
2840 if (errno == EISDIR)
2841 fd = open(path, O_DIRECTORY | O_RDONLY);
2844 rc = errno ? -errno : -EBADF;
2845 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
2849 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
2851 rc = errno ? -errno : -EINVAL;
2857 #define MAX_STRING_SIZE 128
2859 int llapi_ping(char *obd_type, char *obd_name)
2861 char path[MAX_STRING_SIZE];
2865 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
2866 obd_type, obd_name);
2868 fd = open(path, O_WRONLY);
2871 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2875 /* The purpose is to send a byte as a ping, whatever this byte is. */
2876 /* coverity[uninit_use_in_call] */
2877 rc = write(fd, buf, 1);
2887 int llapi_target_iterate(int type_num, char **obd_type,
2888 void *args, llapi_cb_t cb)
2890 char buf[MAX_STRING_SIZE];
2891 FILE *fp = fopen(DEVICES_LIST, "r");
2896 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
2900 while (fgets(buf, sizeof(buf), fp) != NULL) {
2901 char *obd_type_name = NULL;
2902 char *obd_name = NULL;
2903 char *obd_uuid = NULL;
2905 struct obd_statfs osfs_buffer;
2907 while(bufp[0] == ' ')
2910 for(i = 0; i < 3; i++) {
2911 obd_type_name = strsep(&bufp, " ");
2913 obd_name = strsep(&bufp, " ");
2914 obd_uuid = strsep(&bufp, " ");
2916 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
2918 for (i = 0; i < type_num; i++) {
2919 if (strcmp(obd_type_name, obd_type[i]) != 0)
2922 cb(obd_type_name, obd_name, obd_uuid, args);
2929 static void do_target_check(char *obd_type_name, char *obd_name,
2930 char *obd_uuid, void *args)
2934 rc = llapi_ping(obd_type_name, obd_name);
2935 if (rc == ENOTCONN) {
2936 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
2938 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
2940 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
2944 int llapi_target_check(int type_num, char **obd_type, char *dir)
2946 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
2949 #undef MAX_STRING_SIZE
2951 /* Is this a lustre fs? */
2952 int llapi_is_lustre_mnttype(const char *type)
2954 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
2957 /* Is this a lustre client fs? */
2958 int llapi_is_lustre_mnt(struct mntent *mnt)
2960 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
2961 strstr(mnt->mnt_fsname, ":/") != NULL);
2964 int llapi_quotacheck(char *mnt, int check_type)
2969 root = opendir(mnt);
2972 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2976 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2984 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2990 root = opendir(mnt);
2993 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2998 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
3002 if (poll_intvl < 30)
3010 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3015 root = opendir(mnt);
3018 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3022 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
3030 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
3033 struct find_param *param = (struct find_param *)data;
3037 LASSERT(parent != NULL || d != NULL);
3039 rc = get_lmd_info(path, parent, d, param->lmd, param->lumlen);
3041 if (rc == -ENODATA) {
3042 if (!param->obduuid && !param->quiet)
3043 llapi_error(LLAPI_MSG_ERROR, -ENODATA,
3044 "%s has no stripe info", path);
3046 } else if (rc == -ENOENT) {
3052 st = ¶m->lmd->lmd_st;
3054 /* libc chown() will do extra check, and if the real owner is
3055 * the same as the ones to set, it won't fall into kernel, so
3056 * invoke syscall directly. */
3057 rc = syscall(SYS_chown, path, -1, -1);
3059 llapi_error(LLAPI_MSG_ERROR, errno,
3060 "error: chown %s", path);
3062 rc = chmod(path, st->st_mode);
3065 llapi_error(LLAPI_MSG_ERROR, rc, "error: chmod %s (%hu)",
3072 int llapi_quotachown(char *path, int flag)
3074 struct find_param param;
3076 memset(¶m, 0, sizeof(param));
3077 param.recursive = 1;
3081 return param_callback(path, cb_quotachown, NULL, ¶m);
3087 #include <sys/wait.h>
3091 static int rmtacl_notify(int ops)
3095 int found = 0, fd = 0, rc = 0;
3097 fp = setmntent(MOUNTED, "r");
3100 llapi_error(LLAPI_MSG_ERROR, rc,
3101 "error setmntent(%s)", MOUNTED);
3106 mnt = getmntent(fp);
3110 if (!llapi_is_lustre_mnt(mnt))
3113 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
3116 llapi_error(LLAPI_MSG_ERROR, rc,
3117 "Can't open '%s'\n", mnt->mnt_dir);
3121 rc = ioctl(fd, LL_IOC_RMTACL, ops);
3125 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d\n", fd);
3134 return ((rc != 0) ? rc : found);
3137 static char *next_token(char *p, int div)
3143 while (*p && *p != ':' && !isspace(*p))
3146 while (*p == ':' || isspace(*p))
3149 return *p ? p : NULL;
3152 static int rmtacl_name2id(char *name, int is_user)
3157 pw = getpwnam(name);
3161 return (int)(pw->pw_uid);
3165 gr = getgrnam(name);
3169 return (int)(gr->gr_gid);
3173 static int isodigit(int c)
3175 return (c >= '0' && c <= '7') ? 1 : 0;
3179 * Whether the name is just digits string (uid/gid) already or not.
3184 static int str_is_id(char *str)
3191 if (*str == 'x' || *str == 'X') { /* for Hex. */
3192 if (!isxdigit(*(++str)))
3195 while (isxdigit(*(++str)));
3196 } else if (isodigit(*str)) { /* for Oct. */
3197 while (isodigit(*(++str)));
3199 } else if (isdigit(*str)) { /* for Dec. */
3200 while (isdigit(*(++str)));
3203 return (*str == 0) ? 1 : 0;
3213 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
3215 static rmtacl_name_t rmtacl_namelist[] = {
3216 { RMTACL_OPTNAME("user:"), 1, 0 },
3217 { RMTACL_OPTNAME("group:"), 0, 0 },
3218 { RMTACL_OPTNAME("default:user:"), 1, 0 },
3219 { RMTACL_OPTNAME("default:group:"), 0, 0 },
3220 /* for --tabular option */
3221 { RMTACL_OPTNAME("user"), 1, 1 },
3222 { RMTACL_OPTNAME("group"), 0, 1 },
3226 static int rgetfacl_output(char *str)
3228 char *start = NULL, *end = NULL;
3229 int is_user = 0, n, id;
3236 for (rn = rmtacl_namelist; rn->name; rn++) {
3237 if(strncmp(str, rn->name, rn->length) == 0) {
3238 if (!rn->next_token)
3239 start = str + rn->length;
3241 start = next_token(str + rn->length, 0);
3242 is_user = rn->is_user;
3247 end = next_token(start, 1);
3248 if (end == NULL || start == end) {
3249 n = printf("%s", str);
3255 id = rmtacl_name2id(start, is_user);
3256 if (id == INVALID_ID) {
3257 if (str_is_id(start)) {
3259 n = printf("%s", str);
3262 } else if ((id == NOBODY_UID && is_user) ||
3263 (id == NOBODY_GID && !is_user)) {
3265 n = printf("%s", str);
3269 n = printf("%s%d%s", str, id, end);
3274 static int child_status(int status)
3276 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
3279 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
3282 int fd[2], status, rc;
3289 llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe\n");
3296 llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork\n");
3301 /* child process redirects its output. */
3304 if (dup2(fd[1], 1) < 0) {
3306 llapi_error(LLAPI_MSG_ERROR, rc,
3307 "Can't dup2 %d\n", fd[1]);
3317 status = rmtacl_notify(ops);
3321 exit(execvp(argv[0], argv));
3324 /* the following is parent process */
3325 fp = fdopen(fd[0], "r");
3328 llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed\n", fd[0]);
3334 while (fgets(buf, PIPE_BUF, fp) != NULL) {
3335 if (output_func(buf) < 0)
3336 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
3342 if (waitpid(pid, &status, 0) < 0) {
3344 llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed\n", pid);
3348 return child_status(status);
3351 int llapi_lsetfacl(int argc, char *argv[])
3353 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
3356 int llapi_lgetfacl(int argc, char *argv[])
3358 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
3361 int llapi_rsetfacl(int argc, char *argv[])
3363 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
3366 int llapi_rgetfacl(int argc, char *argv[])
3368 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
3371 int llapi_cp(int argc, char *argv[])
3375 rc = rmtacl_notify(RMT_RSETFACL);
3379 exit(execvp(argv[0], argv));
3382 int llapi_ls(int argc, char *argv[])
3386 rc = rmtacl_notify(RMT_LGETFACL);
3390 exit(execvp(argv[0], argv));
3393 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
3394 * format must have %s%s, buf must be > 16
3395 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
3396 * then buf = "lustre-MDT0000"
3398 static int get_mdtname(char *name, char *format, char *buf)
3400 char suffix[]="-MDT0000";
3401 int len = strlen(name);
3403 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
3404 name[len - 5] = '\0';
3409 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
3412 /* Not enough room to add suffix */
3413 llapi_err_noerrno(LLAPI_MSG_ERROR,
3414 "MDT name too long |%s|", name);
3419 return sprintf(buf, format, name, suffix);
3422 /** ioctl on filsystem root, with mdtindex sent as data
3423 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
3424 * \param mdtidxp pointer to integer within data to be filled in with the
3425 * mdt index (0 if no mdt is specified). NULL won't be filled.
3427 static int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
3434 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
3435 Open root and parse mdt index. */
3436 if (mdtname[0] == '/') {
3438 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
3439 (char *)mdtname, -1);
3441 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
3443 ptr = fsname + strlen(fsname) - 8;
3445 index = strtol(ptr + 4, NULL, 10);
3446 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
3450 llapi_err_noerrno(LLAPI_MSG_ERROR,
3451 "Can't open %s: %d\n", mdtname, rc);
3458 rc = ioctl(fd, opc, data);
3463 if (rc && want_error)
3464 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
3470 /****** Changelog API ********/
3472 static int changelog_ioctl(const char *mdtname, int opc, int id,
3473 long long recno, int flags)
3475 struct ioc_changelog data;
3479 data.icc_recno = recno;
3480 data.icc_flags = flags;
3481 idx = (int *)(&data.icc_mdtindex);
3483 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
3486 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
3487 struct changelog_private {
3490 lustre_kernelcomm kuc;
3493 /** Start reading from a changelog
3494 * @param priv Opaque private control structure
3495 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
3496 * @param device Report changes recorded on this MDT
3497 * @param startrec Report changes beginning with this record number
3498 * (just call llapi_changelog_fini when done; don't need an endrec)
3500 int llapi_changelog_start(void **priv, int flags, const char *device,
3503 struct changelog_private *cp;
3506 /* Set up the receiver control struct */
3507 cp = calloc(1, sizeof(*cp));
3511 cp->magic = CHANGELOG_PRIV_MAGIC;
3514 /* Set up the receiver */
3515 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */);
3521 /* Tell the kernel to start sending */
3522 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
3524 /* Only the kernel reference keeps the write side open */
3525 close(cp->kuc.lk_wfd);
3528 /* frees and clears priv */
3529 llapi_changelog_fini(priv);
3540 /** Finish reading from a changelog */
3541 int llapi_changelog_fini(void **priv)
3543 struct changelog_private *cp = (struct changelog_private *)*priv;
3545 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3548 libcfs_ukuc_stop(&cp->kuc);
3554 /** Convert a changelog_rec to changelog_ext_rec, in this way client can treat
3555 * all records in the format of changelog_ext_rec, this can make record
3558 static inline int changelog_extend_rec(struct changelog_ext_rec *ext)
3560 if (!CHANGELOG_REC_EXTENDED(ext)) {
3561 struct changelog_rec *rec = (struct changelog_rec *)ext;
3563 memmove(ext->cr_name, rec->cr_name, rec->cr_namelen);
3564 fid_zero(&ext->cr_sfid);
3565 fid_zero(&ext->cr_spfid);
3572 /** Read the next changelog entry
3573 * @param priv Opaque private control structure
3574 * @param rech Changelog record handle; record will be allocated here
3575 * @return 0 valid message received; rec is set
3579 int llapi_changelog_recv(void *priv, struct changelog_ext_rec **rech)
3581 struct changelog_private *cp = (struct changelog_private *)priv;
3582 struct kuc_hdr *kuch;
3585 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3589 kuch = malloc(CR_MAXSIZE + sizeof(*kuch));
3594 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
3595 CR_MAXSIZE + sizeof(*kuch),
3596 KUC_TRANSPORT_CHANGELOG);
3600 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
3601 ((kuch->kuc_msgtype != CL_RECORD) &&
3602 (kuch->kuc_msgtype != CL_EOF))) {
3603 llapi_err_noerrno(LLAPI_MSG_ERROR,
3604 "Unknown changelog message type %d:%d\n",
3605 kuch->kuc_transport, kuch->kuc_msgtype);
3610 if (kuch->kuc_msgtype == CL_EOF) {
3611 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
3620 /* Our message is a changelog_ext_rec. Use pointer math to skip
3621 * kuch_hdr and point directly to the message payload.
3623 *rech = (struct changelog_ext_rec *)(kuch + 1);
3624 changelog_extend_rec(*rech);
3634 /** Release the changelog record when done with it. */
3635 int llapi_changelog_free(struct changelog_ext_rec **rech)
3638 /* We allocated memory starting at the kuc_hdr, but passed
3639 * the consumer a pointer to the payload.
3640 * Use pointer math to get back to the header.
3642 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
3649 int llapi_changelog_clear(const char *mdtname, const char *idstr,
3655 llapi_err_noerrno(LLAPI_MSG_ERROR,
3656 "can't purge negative records\n");
3660 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
3661 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
3662 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
3663 llapi_err_noerrno(LLAPI_MSG_ERROR,
3664 "expecting id of the form '"
3665 CHANGELOG_USER_PREFIX
3666 "<num>'; got '%s'\n", idstr);
3670 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
3673 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
3674 int buflen, long long *recno, int *linkno)
3677 struct getinfo_fid2path *gf;
3680 while (*fidstr == '[')
3683 sscanf(fidstr, SFID, RFID(&fid));
3684 if (!fid_is_sane(&fid)) {
3685 llapi_err_noerrno(LLAPI_MSG_ERROR,
3686 "bad FID format [%s], should be "DFID"\n",
3687 fidstr, (__u64)1, 2, 0);
3691 gf = malloc(sizeof(*gf) + buflen);
3695 gf->gf_recno = *recno;
3696 gf->gf_linkno = *linkno;
3697 gf->gf_pathlen = buflen;
3699 /* Take path or fsname */
3700 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
3703 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
3705 memcpy(buf, gf->gf_path, gf->gf_pathlen);
3706 *recno = gf->gf_recno;
3707 *linkno = gf->gf_linkno;
3714 static int fid_from_lma(const char *path, const int fd, lustre_fid *fid)
3717 struct lustre_mdt_attrs *lma;
3721 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
3723 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
3726 lma = (struct lustre_mdt_attrs *)buf;
3727 fid_le_to_cpu(fid, &lma->lma_self_fid);
3731 int llapi_fd2fid(const int fd, lustre_fid *fid)
3735 memset(fid, 0, sizeof(*fid));
3737 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
3738 if (rc == -EINVAL || rc == -ENOTTY)
3739 rc = fid_from_lma(NULL, fd, fid);
3744 int llapi_path2fid(const char *path, lustre_fid *fid)
3748 memset(fid, 0, sizeof(*fid));
3749 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
3751 if (errno == ELOOP || errno == ENXIO)
3752 return fid_from_lma(path, -1, fid);
3756 rc = llapi_fd2fid(fd, fid);
3757 if (rc == -EINVAL || rc == -ENOTTY)
3758 rc = fid_from_lma(path, -1, fid);
3764 /****** HSM Copytool API ********/
3765 #define CT_PRIV_MAGIC 0xC0BE2001
3766 struct copytool_private {
3769 lustre_kernelcomm kuc;
3773 #include <libcfs/libcfs.h>
3775 /** Register a copytool
3776 * @param[out] priv Opaque private control structure
3777 * @param fsname Lustre filesystem
3778 * @param flags Open flags, currently unused (e.g. O_NONBLOCK)
3779 * @param archive_count
3780 * @param archives Which archive numbers this copytool is responsible for
3782 int llapi_copytool_start(void **priv, char *fsname, int flags,
3783 int archive_count, int *archives)
3785 struct copytool_private *ct;
3788 if (archive_count > 0 && archives == NULL) {
3789 llapi_err_noerrno(LLAPI_MSG_ERROR,
3790 "NULL archive numbers");
3794 ct = calloc(1, sizeof(*ct));
3798 ct->fsname = malloc(strlen(fsname) + 1);
3799 if (ct->fsname == NULL) {
3803 strcpy(ct->fsname, fsname);
3804 ct->magic = CT_PRIV_MAGIC;
3806 for (rc = 0; rc < archive_count; rc++) {
3807 if (archives[rc] > sizeof(ct->archives)) {
3808 llapi_err_noerrno(LLAPI_MSG_ERROR,
3809 "Maximum of %d archives supported",
3810 sizeof(ct->archives));
3813 ct->archives |= 1 << archives[rc];
3815 /* special case: if no archives specified, default to archive #0. */
3816 if (ct->archives == 0)
3819 rc = libcfs_ukuc_start(&ct->kuc, KUC_GRP_HSM);
3823 /* Storing archive(s) in lk_data; see mdc_ioc_hsm_ct_start */
3824 ct->kuc.lk_data = ct->archives;
3825 rc = root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL,
3827 /* Only the kernel reference keeps the write side open */
3828 close(ct->kuc.lk_wfd);
3843 /** Deregister a copytool */
3844 int llapi_copytool_fini(void **priv)
3846 struct copytool_private *ct = (struct copytool_private *)*priv;
3848 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3851 /* Tell the kernel to stop sending us messages */
3852 ct->kuc.lk_flags = LK_FLG_STOP;
3853 root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL, 0);
3855 /* Shut down the kernelcomms */
3856 libcfs_ukuc_stop(&ct->kuc);
3864 /** Wait for the next hsm_action_list
3865 * @param priv Opaque private control structure
3866 * @param halh Action list handle, will be allocated here
3867 * @param msgsize Number of bytes in the message, will be set here
3868 * @return 0 valid message received; halh and msgsize are set
3871 int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
3873 struct copytool_private *ct = (struct copytool_private *)priv;
3874 struct kuc_hdr *kuch;
3875 struct hsm_action_list *hal;
3878 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3880 if (halh == NULL || msgsize == NULL)
3883 kuch = malloc(HAL_MAXSIZE + sizeof(*kuch));
3887 rc = libcfs_ukuc_msg_get(&ct->kuc, (char *)kuch,
3888 HAL_MAXSIZE + sizeof(*kuch),
3893 /* Handle generic messages */
3894 if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
3895 kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
3900 if (kuch->kuc_transport != KUC_TRANSPORT_HSM ||
3901 kuch->kuc_msgtype != HMT_ACTION_LIST) {
3902 llapi_err_noerrno(LLAPI_MSG_ERROR,
3903 "Unknown HSM message type %d:%d\n",
3904 kuch->kuc_transport, kuch->kuc_msgtype);
3909 /* Our message is a hsm_action_list. Use pointer math to skip
3910 * kuch_hdr and point directly to the message payload.
3912 hal = (struct hsm_action_list *)(kuch + 1);
3914 /* Check that we have registered for this archive # */
3915 if (((1 << hal->hal_archive_num) & ct->archives) == 0) {
3916 llapi_err_noerrno(LLAPI_MSG_INFO,
3917 "Ignoring request for archive #%d (bitmask %#x)\n",
3918 hal->hal_archive_num, ct->archives);
3924 *msgsize = kuch->kuc_msglen - sizeof(*kuch);
3934 /** Release the action list when done with it. */
3935 int llapi_copytool_free(struct hsm_action_list **hal)
3937 /* Reuse the llapi_changelog_free function */
3938 return llapi_changelog_free((struct changelog_ext_rec **)hal);
3941 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
3946 root = opendir(mnt);
3949 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3953 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
3956 llapi_error(LLAPI_MSG_ERROR, rc,
3957 "ioctl on %s for getting connect flags failed", mnt);
3963 int llapi_get_version(char *buffer, int buffer_size,
3968 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
3970 fd = open(OBD_DEV_PATH, O_RDONLY);
3974 memset(buffer, 0, buffer_size);
3975 data->ioc_version = OBD_IOCTL_VERSION;
3976 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
3977 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
3978 data->ioc_len = obd_ioctl_packlen(data);
3980 rc = ioctl(fd, OBD_GET_VERSION, buffer);
3987 *version = data->ioc_bulk;
3992 * Get a 64-bit value representing the version of file data pointed by fd.
3994 * Each write or truncate, flushed on OST, will change this value. You can use
3995 * this value to verify if file data was modified. This only checks the file
3996 * data, not metadata.
3998 * \param flags If set to LL_DV_NOFLUSH, the data version will be read
3999 * directly from OST without regard to possible dirty cache on
4002 * \retval 0 on success.
4003 * \retval -errno on error.
4005 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
4008 struct ioc_data_version idv;
4010 idv.idv_flags = flags;
4012 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
4016 *data_version = idv.idv_version;
4022 * Create a volatile file and open it for write:
4023 * - file is created as a standard file in the directory
4024 * - file does not appears in directory and directory mtime does not change
4025 * - file is removed at close
4026 * - file modes are rw-------, if user wants another one it must use fchmod()
4027 * \param directory Directory where the file is created
4028 * \param idx MDT index on which the file is created
4029 * \param flags Std open flags
4031 * \retval 0 on success.
4032 * \retval -errno on error.
4034 int llapi_create_volatile_idx(char *directory, int idx, int mode)
4036 char file_path[PATH_MAX];
4037 char filename[PATH_MAX];
4042 fd = open("/dev/urandom", O_RDONLY);
4044 llapi_error(LLAPI_MSG_ERROR, errno,
4045 "Cannot open /dev/urandom\n");
4048 rc = read(fd, &random, sizeof(random));
4050 if (rc < sizeof(random)) {
4051 llapi_error(LLAPI_MSG_ERROR, errno,
4052 "Cannot read %d bytes from /dev/urandom\n",
4057 sprintf(filename, LUSTRE_VOLATILE_HDR"::%.4X", random);
4059 sprintf(filename, LUSTRE_VOLATILE_IDX"%.4X", 0, random);
4061 sprintf(file_path, "%s/%s", directory, filename);
4063 fd = open(file_path, O_RDWR|O_CREAT|mode, S_IRUSR|S_IWUSR);
4065 llapi_error(LLAPI_MSG_ERROR, errno,
4066 "Cannot create volatile file %s in %s\n",
4067 filename + LUSTRE_VOLATILE_HDR_LEN,