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, Whamcloud, Inc.
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>
74 #include <lustre_lib.h>
76 #include <lustre/liblustreapi.h>
78 static unsigned llapi_dir_filetype_table[] = {
87 #if defined(DT_DOOR) && defined(S_IFDOOR)
92 #if defined(DT_DOOR) && defined(S_IFDOOR)
93 static const int DT_MAX = DT_DOOR;
95 static const int DT_MAX = DT_SOCK;
98 static unsigned llapi_filetype_dir_table[] = {
107 #if defined(DT_DOOR) && defined(S_IFDOOR)
112 #if defined(DT_DOOR) && defined(S_IFDOOR)
113 static const int S_IFMAX = DT_DOOR;
115 static const int S_IFMAX = DT_SOCK;
118 /* liblustreapi message level */
119 static int llapi_msg_level = LLAPI_MSG_MAX;
121 void llapi_msg_set_level(int level)
123 /* ensure level is in the good range */
124 if (level < LLAPI_MSG_OFF)
125 llapi_msg_level = LLAPI_MSG_OFF;
126 else if (level > LLAPI_MSG_MAX)
127 llapi_msg_level = LLAPI_MSG_MAX;
129 llapi_msg_level = level;
132 /* llapi_error will preserve errno */
133 void llapi_error(int level, int _rc, char *fmt, ...)
136 int tmp_errno = errno;
137 /* to protect using errno as _rc argument */
140 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
144 vfprintf(stderr, fmt, args);
147 if (level & LLAPI_MSG_NO_ERRNO)
148 fprintf(stderr, "\n");
150 fprintf(stderr, ": %s (%d)\n", strerror(rc), rc);
154 /* llapi_printf will preserve errno */
155 void llapi_printf(int level, char *fmt, ...)
158 int tmp_errno = errno;
160 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
164 vfprintf(stdout, fmt, args);
170 * size_units is to be initialized (or zeroed) by caller.
172 int parse_size(char *optarg, unsigned long long *size,
173 unsigned long long *size_units, int bytes_spec)
177 if (strncmp(optarg, "-", 1) == 0)
180 if (*size_units == 0)
183 *size = strtoull(optarg, &end, 0);
186 if ((*end == 'b') && *(end + 1) == '\0' &&
187 (*size & (~0ULL << (64 - 9))) == 0 &&
189 *size_units = 1 << 9;
190 } else if ((*end == 'b') &&
191 *(end + 1) == '\0' &&
194 } else if ((*end == 'k' || *end == 'K') &&
195 *(end + 1) == '\0' &&
196 (*size & (~0ULL << (64 - 10))) == 0) {
197 *size_units = 1 << 10;
198 } else if ((*end == 'm' || *end == 'M') &&
199 *(end + 1) == '\0' &&
200 (*size & (~0ULL << (64 - 20))) == 0) {
201 *size_units = 1 << 20;
202 } else if ((*end == 'g' || *end == 'G') &&
203 *(end + 1) == '\0' &&
204 (*size & (~0ULL << (64 - 30))) == 0) {
205 *size_units = 1 << 30;
206 } else if ((*end == 't' || *end == 'T') &&
207 *(end + 1) == '\0' &&
208 (*size & (~0ULL << (64 - 40))) == 0) {
209 *size_units = 1ULL << 40;
210 } else if ((*end == 'p' || *end == 'P') &&
211 *(end + 1) == '\0' &&
212 (*size & (~0ULL << (64 - 50))) == 0) {
213 *size_units = 1ULL << 50;
214 } else if ((*end == 'e' || *end == 'E') &&
215 *(end + 1) == '\0' &&
216 (*size & (~0ULL << (64 - 60))) == 0) {
217 *size_units = 1ULL << 60;
222 *size *= *size_units;
226 /* XXX: llapi_xxx() functions return negative values upon failure */
228 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
229 int stripe_count, int stripe_pattern)
233 /* 64 KB is the largest common page size I'm aware of (on ia64), but
234 * check the local page size just in case. */
235 page_size = LOV_MIN_STRIPE_SIZE;
236 if (getpagesize() > page_size) {
237 page_size = getpagesize();
238 llapi_err_noerrno(LLAPI_MSG_WARN,
239 "warning: your page size (%u) is "
240 "larger than expected (%u)", page_size,
241 LOV_MIN_STRIPE_SIZE);
243 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
245 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %lu, "
246 "must be an even multiple of %d bytes",
247 stripe_size, page_size);
250 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
252 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
256 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
258 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
262 if (stripe_size >= (1ULL << 32)){
264 llapi_error(LLAPI_MSG_ERROR, rc,
265 "warning: stripe size larger than 4G "
266 "is not currently supported and would wrap");
272 /* return the first file matching this pattern */
273 static int first_match(char *pattern, char *buffer)
277 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
280 if (glob_info.gl_pathc < 1) {
281 globfree(&glob_info);
285 strcpy(buffer, glob_info.gl_pathv[0]);
287 globfree(&glob_info);
291 static int find_target_obdpath(char *fsname, char *path)
294 char pattern[PATH_MAX + 1];
297 snprintf(pattern, PATH_MAX,
298 "/proc/fs/lustre/lov/%s-*/target_obd",
300 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
301 if (rc == GLOB_NOMATCH)
306 strcpy(path, glob_info.gl_pathv[0]);
307 globfree(&glob_info);
311 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
314 char pattern[PATH_MAX + 1];
317 snprintf(pattern, PATH_MAX,
318 "/proc/fs/lustre/lov/%s-*/pools/%s",
320 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
321 /* If no pools, make sure the lov is available */
322 if ((rc == GLOB_NOMATCH) &&
323 (find_target_obdpath(fsname, poolpath) == -ENODEV))
328 strcpy(poolpath, glob_info.gl_pathv[0]);
329 globfree(&glob_info);
334 * return a parameter string for a specific device type or mountpoint
336 * \param param_path the path to the file containing parameter data
337 * \param result buffer for parameter value string
338 * \param result_size size of buffer for return value
340 * The \param param_path is appended to /proc/{fs,sys}/{lnet,lustre} to
341 * complete the absolute path to the file containing the parameter data
342 * the user is requesting. If that file exist then the data is read from
343 * the file and placed into the \param result buffer that is passed by
344 * the user. Data is only copied up to the \param result_size to prevent
345 * overflow of the array.
347 * Return 0 for success, with a NUL-terminated string in \param result.
348 * Return -ve value for error.
350 static int get_param(const char *param_path, char *result,
351 unsigned int result_size)
353 char file[PATH_MAX + 1], pattern[PATH_MAX + 1], buf[result_size];
357 snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
359 rc = first_match(pattern, file);
363 fp = fopen(file, "r");
365 while (fgets(buf, result_size, fp) != NULL)
374 #define DEVICES_LIST "/proc/fs/lustre/devices"
377 * return a parameter string for a specific device type or mountpoint
379 * \param fsname Lustre filesystem name (optional)
380 * \param file_path path to file in filesystem (optional, if fsname unset)
381 * \param obd_type Lustre OBD device type
382 * \param param_name parameter name to fetch
383 * \param value return buffer for parameter value string
384 * \param val_len size of buffer for return value
386 * If fsname is specified then the parameter will be from that filesystem
387 * (if it exists). If file_path is given and it is in a mounted Lustre
388 * filesystem, then the parameter will be otherwise the value may be
389 * from any mounted filesystem (if there is more than one).
391 * If "obd_type" matches a Lustre device then the first matching device
392 * (as with "lctl dl", constrained by \param fsname or \param mount_path)
393 * will be used to provide the return value, otherwise the first such
394 * device found will be used.
396 * Return 0 for success, with a NUL-terminated string in \param buffer.
397 * Return -ve value for error.
399 static int get_param_obdvar(const char *fsname, const char *file_path,
400 const char *obd_type, const char *param_name,
401 char *value, unsigned int val_len)
403 char devices[PATH_MAX + 1], dev[PATH_MAX + 1] = "*", fs[PATH_MAX + 1];
404 FILE *fp = fopen(DEVICES_LIST, "r");
407 if (!fsname && file_path) {
408 rc = llapi_search_fsname(file_path, fs);
410 llapi_error(LLAPI_MSG_ERROR, rc,
411 "'%s' is not on a Lustre filesystem",
421 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
425 while (fgets(devices, sizeof(devices), fp) != NULL) {
426 char *bufp = devices, *tmp;
428 while (bufp[0] == ' ')
431 tmp = strstr(bufp, obd_type);
433 tmp += strlen(obd_type) + 1;
437 tmp = strchr(dev, ' ');
443 if (dev[0] == '*' && strlen(fs))
444 snprintf(dev, PATH_MAX, "%s-*", fs);
445 snprintf(devices, PATH_MAX, "%s/%s/%s", obd_type, dev, param_name);
447 return get_param(devices, value, val_len);
450 static int get_mds_md_size(char *path)
452 int lumlen = lov_mds_md_size(LOV_MAX_STRIPE_COUNT, LOV_MAGIC_V3);
455 /* Now get the maxea from llite proc */
456 if (!get_param_obdvar(NULL, path, "llite", "max_easize",
463 * if pool is NULL, search ostname in target_obd
464 * if pool is not NULL:
465 * if pool not found returns errno < 0
466 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
467 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
469 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
472 char buffer[PATH_MAX + 1];
476 len = strlen(ostname);
478 if (poolname == NULL)
479 rc = find_target_obdpath(fsname, buffer);
481 rc = find_poolpath(fsname, poolname, buffer);
485 fd = fopen(buffer, "r");
489 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
490 if (poolname == NULL) {
492 /* Search for an ostname in the list of OSTs
493 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
494 ptr = strchr(buffer, ' ');
496 (strncmp(ptr + 1, ostname, len) == 0)) {
501 /* Search for an ostname in a pool,
502 (or an existing non-empty pool if no ostname) */
503 if ((ostname == NULL) ||
504 (strncmp(buffer, ostname, len) == 0)) {
514 int llapi_file_open_pool(const char *name, int flags, int mode,
515 unsigned long long stripe_size, int stripe_offset,
516 int stripe_count, int stripe_pattern, char *pool_name)
518 struct lov_user_md_v3 lum = { 0 };
522 /* Make sure we have a good pool */
523 if (pool_name != NULL) {
524 char fsname[MAX_OBD_NAME + 1], *ptr;
526 rc = llapi_search_fsname(name, fsname);
528 llapi_error(LLAPI_MSG_ERROR, rc,
529 "'%s' is not on a Lustre filesystem",
534 /* in case user gives the full pool name <fsname>.<poolname>,
535 * strip the fsname */
536 ptr = strchr(pool_name, '.');
539 if (strcmp(pool_name, fsname) != 0) {
541 llapi_err_noerrno(LLAPI_MSG_ERROR,
542 "Pool '%s' is not on filesystem '%s'",
549 /* Make sure the pool exists and is non-empty */
550 rc = llapi_search_ost(fsname, pool_name, NULL);
552 llapi_err_noerrno(LLAPI_MSG_ERROR,
553 "pool '%s.%s' %s", fsname, pool_name,
554 rc == 0 ? "has no OSTs" : "does not exist");
559 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
560 if (fd < 0 && errno == EISDIR) {
561 fd = open(name, O_DIRECTORY | O_RDONLY);
567 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
571 rc = llapi_stripe_limit_check(stripe_size, stripe_offset, stripe_count,
576 /* Initialize IOCTL striping pattern structure */
577 lum.lmm_magic = LOV_USER_MAGIC_V3;
578 lum.lmm_pattern = stripe_pattern;
579 lum.lmm_stripe_size = stripe_size;
580 lum.lmm_stripe_count = stripe_count;
581 lum.lmm_stripe_offset = stripe_offset;
582 if (pool_name != NULL) {
583 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
585 /* If no pool is specified at all, use V1 request */
586 lum.lmm_magic = LOV_USER_MAGIC_V1;
589 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
590 char *errmsg = "stripe already set";
592 if (errno != EEXIST && errno != EALREADY)
593 errmsg = strerror(errno);
595 llapi_err_noerrno(LLAPI_MSG_ERROR,
596 "error on ioctl "LPX64" for '%s' (%d): %s",
597 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
608 int llapi_file_open(const char *name, int flags, int mode,
609 unsigned long long stripe_size, int stripe_offset,
610 int stripe_count, int stripe_pattern)
612 return llapi_file_open_pool(name, flags, mode, stripe_size,
613 stripe_offset, stripe_count,
614 stripe_pattern, NULL);
617 int llapi_file_create(const char *name, unsigned long long stripe_size,
618 int stripe_offset, int stripe_count, int stripe_pattern)
622 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
623 stripe_offset, stripe_count, stripe_pattern,
632 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
633 int stripe_offset, int stripe_count,
634 int stripe_pattern, char *pool_name)
638 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
639 stripe_offset, stripe_count, stripe_pattern,
649 * Find the fsname, the full path, and/or an open fd.
650 * Either the fsname or path must not be NULL
652 #define WANT_PATH 0x1
653 #define WANT_FSNAME 0x2
655 #define WANT_INDEX 0x8
656 #define WANT_ERROR 0x10
657 static int get_root_path(int want, char *fsname, int *outfd, char *path,
661 char buf[PATH_MAX], mntdir[PATH_MAX];
664 int idx = 0, len = 0, mntlen, fd;
667 /* get the mount point */
668 fp = setmntent(MOUNTED, "r");
671 llapi_error(LLAPI_MSG_ERROR, rc,
672 "setmntent(%s) failed", MOUNTED);
676 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
679 if (!llapi_is_lustre_mnt(&mnt))
682 if ((want & WANT_INDEX) && (idx++ != index))
685 mntlen = strlen(mnt.mnt_dir);
686 ptr = strrchr(mnt.mnt_fsname, '/');
693 /* Check the fsname for a match, if given */
694 if (!(want & WANT_FSNAME) && fsname != NULL &&
695 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
698 /* If the path isn't set return the first one we find */
699 if (path == NULL || strlen(path) == 0) {
700 strcpy(mntdir, mnt.mnt_dir);
701 if ((want & WANT_FSNAME) && fsname != NULL)
705 /* Otherwise find the longest matching path */
706 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
707 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
708 strcpy(mntdir, mnt.mnt_dir);
710 if ((want & WANT_FSNAME) && fsname != NULL)
719 if ((want & WANT_PATH) && path != NULL)
720 strcpy(path, mntdir);
721 if (want & WANT_FD) {
722 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
725 llapi_error(LLAPI_MSG_ERROR, rc,
726 "error opening '%s'", mntdir);
732 } else if (want & WANT_ERROR)
733 llapi_err_noerrno(LLAPI_MSG_ERROR,
734 "can't find fs root for '%s': %d",
735 (want & WANT_PATH) ? fsname : path, rc);
740 * search lustre mounts
742 * Calling this function will return to the user the mount point, mntdir, and
743 * the file system name, fsname, if the user passed a buffer to this routine.
745 * The user inputs are pathname and index. If the pathname is supplied then
746 * the value of the index will be ignored. The pathname will return data if
747 * the pathname is located on a lustre mount. Index is used to pick which
748 * mount point you want in the case of multiple mounted lustre file systems.
749 * See function lfs_osts in lfs.c for a example of the index use.
751 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
754 int want = WANT_PATH, idx = -1;
756 if (!pathname || pathname[0] == '\0') {
760 strcpy(mntdir, pathname);
764 return get_root_path(want, fsname, NULL, mntdir, idx);
767 /* Given a path, find the corresponding Lustre fsname */
768 int llapi_search_fsname(const char *pathname, char *fsname)
773 path = realpath(pathname, NULL);
775 char buf[PATH_MAX + 1], *ptr;
778 if (pathname[0] != '/') {
779 /* Need an absolute path, but realpath() only works for
780 * pathnames that actually exist. We go through the
781 * extra hurdle of dirname(getcwd() + pathname) in
782 * case the relative pathname contains ".." in it. */
783 if (getcwd(buf, sizeof(buf) - 1) == NULL)
787 strncat(buf, pathname, sizeof(buf) - strlen(buf));
788 path = realpath(buf, NULL);
790 ptr = strrchr(buf, '/');
794 path = realpath(buf, NULL);
797 llapi_error(LLAPI_MSG_ERROR, rc,
798 "pathname '%s' cannot expand",
804 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
809 int llapi_getname(const char *path, char *buf, size_t size)
811 struct obd_uuid uuid_buf;
812 char *uuid = uuid_buf.uuid;
815 memset(&uuid_buf, 0, sizeof(uuid_buf));
816 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
820 /* We want to turn lustre-clilov-ffff88002738bc00 into
821 * lustre-ffff88002738bc00. */
823 nr = snprintf(buf, size, "%.*s-%s",
824 (int) (strlen(uuid) - 24), uuid,
825 uuid + strlen(uuid) - 16);
835 * find the pool directory path under /proc
836 * (can be also used to test if a fsname is known)
838 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
841 char pattern[PATH_MAX + 1];
842 char buffer[PATH_MAX];
844 if (fsname == NULL) {
845 rc = llapi_search_fsname(pathname, buffer);
849 strcpy(pathname, fsname);
852 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
853 rc = first_match(pattern, buffer);
857 /* in fsname test mode, pool_pathname is NULL */
858 if (pool_pathname != NULL)
859 strcpy(pool_pathname, buffer);
865 * Get the list of pool members.
866 * \param poolname string of format \<fsname\>.\<poolname\>
867 * \param members caller-allocated array of char*
868 * \param list_size size of the members array
869 * \param buffer caller-allocated buffer for storing OST names
870 * \param buffer_size size of the buffer
872 * \return number of members retrieved for this pool
873 * \retval -error failure
875 int llapi_get_poolmembers(const char *poolname, char **members,
876 int list_size, char *buffer, int buffer_size)
878 char fsname[PATH_MAX + 1];
880 char pathname[PATH_MAX + 1];
881 char path[PATH_MAX + 1];
888 /* name is FSNAME.POOLNAME */
889 if (strlen(poolname) > PATH_MAX)
891 strcpy(fsname, poolname);
892 pool = strchr(fsname, '.');
899 rc = poolpath(fsname, NULL, pathname);
901 llapi_error(LLAPI_MSG_ERROR, rc,
902 "Lustre filesystem '%s' not found",
907 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
908 sprintf(path, "%s/%s", pathname, pool);
909 fd = fopen(path, "r");
912 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
917 while (fgets(buf, sizeof(buf), fd) != NULL) {
918 if (nb_entries >= list_size) {
923 tmp = strchr(buf, '\n');
926 if (used + strlen(buf) + 1 > buffer_size) {
931 strcpy(buffer + used, buf);
932 members[nb_entries] = buffer + used;
933 used += strlen(buf) + 1;
943 * Get the list of pools in a filesystem.
944 * \param name filesystem name or path
945 * \param poollist caller-allocated array of char*
946 * \param list_size size of the poollist array
947 * \param buffer caller-allocated buffer for storing pool names
948 * \param buffer_size size of the buffer
950 * \return number of pools retrieved for this filesystem
951 * \retval -error failure
953 int llapi_get_poollist(const char *name, char **poollist, int list_size,
954 char *buffer, int buffer_size)
956 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
960 struct dirent *cookie = NULL;
962 unsigned int nb_entries = 0;
963 unsigned int used = 0;
966 /* initilize output array */
967 for (i = 0; i < list_size; i++)
970 /* is name a pathname ? */
971 ptr = strchr(name, '/');
973 /* only absolute pathname is supported */
977 if (!realpath(name, rname)) {
979 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
984 rc = poolpath(NULL, rname, pathname);
986 llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
987 " a Lustre filesystem", name);
990 strcpy(fsname, rname);
993 strcpy(fsname, name);
994 rc = poolpath(fsname, NULL, pathname);
997 llapi_error(LLAPI_MSG_ERROR, rc,
998 "Lustre filesystem '%s' not found", name);
1002 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1003 dir = opendir(pathname);
1006 llapi_error(LLAPI_MSG_ERROR, rc,
1007 "Could not open pool list for '%s'",
1013 rc = readdir_r(dir, &pool, &cookie);
1017 llapi_error(LLAPI_MSG_ERROR, rc,
1018 "Error reading pool list for '%s'", name);
1020 } else if ((rc == 0) && (cookie == NULL)) {
1021 /* end of directory */
1025 /* ignore . and .. */
1026 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1029 /* check output bounds */
1030 if (nb_entries >= list_size)
1033 /* +2 for '.' and final '\0' */
1034 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1038 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1039 poollist[nb_entries] = buffer + used;
1040 used += strlen(pool.d_name) + strlen(fsname) + 2;
1048 /* wrapper for lfs.c and obd.c */
1049 int llapi_poollist(const char *name)
1051 /* list of pool names (assume that pool count is smaller
1053 char **list, *buffer = NULL, *path = NULL, *fsname = NULL;
1054 int obdcount, bufsize, rc, nb, i;
1055 char *poolname = NULL, *tmp = NULL, data[16];
1057 if (name[0] != '/') {
1058 fsname = strdup(name);
1059 poolname = strchr(fsname, '.');
1063 path = (char *) name;
1066 rc = get_param_obdvar(fsname, path, "lov", "numobd",
1067 data, sizeof(data));
1070 obdcount = atoi(data);
1072 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1073 * and also an array to store the pointers for all that
1074 * allocated space. */
1076 bufsize = sizeof(struct obd_uuid) * obdcount;
1077 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1078 if (buffer == NULL) {
1082 list = (char **) (buffer + bufsize);
1085 /* name is a path or fsname */
1086 nb = llapi_get_poollist(name, list, obdcount,
1089 /* name is a pool name (<fsname>.<poolname>) */
1090 nb = llapi_get_poolmembers(name, list, obdcount,
1094 if (nb == -EOVERFLOW) {
1097 goto retry_get_pools;
1100 for (i = 0; i < nb; i++)
1101 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1102 rc = (nb < 0 ? nb : 0);
1111 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
1112 void *data, cfs_dirent_t *de);
1114 #define OBD_NOT_FOUND (-1)
1116 static int common_param_init(struct find_param *param, char *path)
1118 param->lumlen = get_mds_md_size(path);
1119 param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
1120 if (param->lmd == NULL) {
1121 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1122 "error: allocation of %d bytes for ioctl",
1123 sizeof(lstat_t) + param->lumlen);
1127 param->got_uuids = 0;
1128 param->obdindexes = NULL;
1129 param->obdindex = OBD_NOT_FOUND;
1133 static void find_param_fini(struct find_param *param)
1135 if (param->obdindexes)
1136 free(param->obdindexes);
1142 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1145 struct find_param *param = (struct find_param *)data;
1150 /* set errno upon failure */
1151 static DIR *opendir_parent(char *path)
1157 fname = strrchr(path, '/');
1159 return opendir(".");
1163 parent = opendir(path);
1168 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1169 struct lov_user_mds_data *lmd, int lumlen)
1171 lstat_t *st = &lmd->lmd_st;
1174 if (parent == NULL && dir == NULL)
1178 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1179 } else if (parent) {
1180 char *fname = strrchr(path, '/');
1182 fname = (fname == NULL ? path : fname + 1);
1183 /* retrieve needed file info */
1184 strncpy((char *)lmd, fname, lumlen);
1185 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1191 if (errno == ENOTTY) {
1192 /* ioctl is not supported, it is not a lustre fs.
1193 * Do the regular lstat(2) instead. */
1194 ret = lstat_f(path, st);
1197 llapi_error(LLAPI_MSG_ERROR, ret,
1198 "error: %s: lstat failed for %s",
1201 } else if (errno == ENOENT) {
1203 llapi_error(LLAPI_MSG_WARN, ret,
1204 "warning: %s: %s does not exist",
1206 } else if (errno != EISDIR) {
1208 llapi_error(LLAPI_MSG_ERROR, ret,
1209 "%s ioctl failed for %s.",
1210 dir ? "LL_IOC_MDC_GETINFO" :
1211 "IOC_MDC_GETFILEINFO", path);
1214 llapi_error(LLAPI_MSG_ERROR, ret,
1215 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1222 int llapi_mds_getfileinfo(char *path, DIR *parent,
1223 struct lov_user_mds_data *lmd)
1225 int lumlen = get_mds_md_size(path);
1227 return get_lmd_info(path, parent, NULL, lmd, lumlen);
1230 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1231 semantic_func_t sem_init,
1232 semantic_func_t sem_fini, void *data,
1243 if (!d && errno != ENOTDIR) {
1245 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1248 } else if (!d && !parent) {
1249 /* ENOTDIR. Open the parent dir. */
1250 p = opendir_parent(path);
1252 GOTO(out, ret = -errno);
1255 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1261 while ((dent = readdir64(d)) != NULL) {
1262 ((struct find_param *)data)->have_fileinfo = 0;
1264 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1267 /* Don't traverse .lustre directory */
1268 if (!(strcmp(dent->d_name, dot_lustre_name)))
1272 if ((len + dent->d_reclen + 2) > size) {
1273 llapi_err_noerrno(LLAPI_MSG_ERROR,
1274 "error: %s: string buffer is too small",
1279 strcat(path, dent->d_name);
1281 if (dent->d_type == DT_UNKNOWN) {
1282 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1284 ret = llapi_mds_getfileinfo(path, d,
1285 ((struct find_param *)data)->lmd);
1287 ((struct find_param *)data)->have_fileinfo = 1;
1289 llapi_filetype_dir_table[st->st_mode &
1296 switch (dent->d_type) {
1298 llapi_err_noerrno(LLAPI_MSG_ERROR,
1299 "error: %s: '%s' is UNKNOWN type %d",
1300 __func__, dent->d_name, dent->d_type);
1303 ret = llapi_semantic_traverse(path, size, d, sem_init,
1304 sem_fini, data, dent);
1311 ret = sem_init(path, d, NULL, data, dent);
1315 if (sem_fini && ret == 0)
1316 sem_fini(path, d, NULL, data, dent);
1324 sem_fini(path, parent, d, data, de);
1333 static int param_callback(char *path, semantic_func_t sem_init,
1334 semantic_func_t sem_fini, struct find_param *param)
1336 int ret, len = strlen(path);
1339 if (len > PATH_MAX) {
1341 llapi_error(LLAPI_MSG_ERROR, ret,
1342 "Path name '%s' is too long", path);
1346 buf = (char *)malloc(PATH_MAX + 1);
1350 strncpy(buf, path, PATH_MAX + 1);
1351 ret = common_param_init(param, buf);
1356 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1357 sem_fini, param, NULL);
1359 find_param_fini(param);
1361 return ret < 0 ? ret : 0;
1364 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1366 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1369 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1374 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1376 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1379 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1384 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1388 fd = open(path, O_RDONLY);
1391 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1395 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1406 * If uuidp is NULL, return the number of available obd uuids.
1407 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1408 * there are more OSTs then allocated to uuidp, then an error is returned with
1409 * the ost_count set to number of available obd uuids.
1411 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1412 int *ost_count, enum tgt_type type)
1414 struct obd_uuid name;
1417 int rc = 0, index = 0;
1419 /* Get the lov name */
1420 if (type == LOV_TYPE) {
1421 rc = llapi_file_fget_lov_uuid(fd, &name);
1425 rc = llapi_file_fget_lmv_uuid(fd, &name);
1430 /* Now get the ost uuids from /proc */
1431 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1432 type == LOV_TYPE ? "lov" : "lmv", name.uuid);
1433 fp = fopen(buf, "r");
1436 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1440 while (fgets(buf, sizeof(buf), fp) != NULL) {
1441 if (uuidp && (index < *ost_count)) {
1442 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
1450 if (uuidp && (index > *ost_count))
1457 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1459 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1462 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1467 root = opendir(mnt);
1470 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1475 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1483 /* Check if user specified value matches a real uuid. Ignore _UUID,
1484 * -osc-4ba41334, other trailing gunk in comparison.
1485 * @param real_uuid ends in "_UUID"
1486 * @param search_uuid may or may not end in "_UUID"
1488 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1490 int cmplen = strlen(real_uuid);
1491 int searchlen = strlen(search_uuid);
1493 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1495 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1498 /* The UUIDs may legitimately be different lengths, if
1499 * the system was upgraded from an older version. */
1500 if (cmplen != searchlen)
1503 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1506 /* Here, param->obduuid points to a single obduuid, the index of which is
1507 * returned in param->obdindex */
1508 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1510 struct obd_uuid obd_uuid;
1511 char uuid[sizeof(struct obd_uuid)];
1516 if (param->got_uuids)
1519 /* Get the lov/lmv name */
1521 rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
1523 rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
1525 if (rc != -ENOTTY) {
1526 llapi_error(LLAPI_MSG_ERROR, rc,
1527 "error: can't get lov name: %s", dname);
1534 param->got_uuids = 1;
1536 /* Now get the ost uuids from /proc */
1537 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1538 param->get_lmv ? "lmv" : "lov", obd_uuid.uuid);
1539 fp = fopen(buf, "r");
1542 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1546 if (!param->obduuid && !param->quiet && !param->obds_printed)
1547 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1548 param->get_lmv ? "MDTS" : "OBDS:");
1550 while (fgets(buf, sizeof(buf), fp) != NULL) {
1551 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
1554 if (param->obduuid) {
1555 if (llapi_uuid_match(uuid, param->obduuid->uuid)) {
1556 param->obdindex = index;
1559 } else if (!param->quiet && !param->obds_printed) {
1560 /* Print everything */
1561 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1564 param->obds_printed = 1;
1568 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1569 llapi_err_noerrno(LLAPI_MSG_ERROR,
1570 "error: %s: unknown obduuid: %s",
1571 __func__, param->obduuid->uuid);
1578 /* In this case, param->obduuid will be an array of obduuids and
1579 * obd index for all these obduuids will be returned in
1580 * param->obdindexes */
1581 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1582 int num_obds, int **obdindexes, int *obdindex,
1585 int ret, obdcount, obd_valid = 0, obdnum, i;
1586 struct obd_uuid *uuids = NULL;
1590 ret = get_param_obdvar(NULL, path, type == LOV_TYPE ? "lov" : "lmv",
1591 "numobd", buf, sizeof(buf));
1595 obdcount = atoi(buf);
1596 uuids = (struct obd_uuid *)malloc(obdcount *
1597 sizeof(struct obd_uuid));
1602 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1604 struct obd_uuid *uuids_temp;
1606 if (ret == -EOVERFLOW) {
1607 uuids_temp = realloc(uuids, obdcount *
1608 sizeof(struct obd_uuid));
1609 if (uuids_temp != NULL)
1610 goto retry_get_uuids;
1615 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
1619 indexes = malloc(num_obds * sizeof(*obdindex));
1620 if (indexes == NULL) {
1625 for (obdnum = 0; obdnum < num_obds; obdnum++) {
1628 /* The user may have specified a simple index */
1629 i = strtol(obduuids[obdnum].uuid, &end, 0);
1630 if (end && *end == '\0' && i < obdcount) {
1631 indexes[obdnum] = i;
1634 for (i = 0; i < obdcount; i++) {
1635 if (llapi_uuid_match(uuids[i].uuid,
1636 obduuids[obdnum].uuid)) {
1637 indexes[obdnum] = i;
1643 if (i >= obdcount) {
1644 indexes[obdnum] = OBD_NOT_FOUND;
1645 llapi_err_noerrno(LLAPI_MSG_ERROR,
1646 "error: %s: unknown obduuid: %s",
1647 __func__, obduuids[obdnum].uuid);
1653 *obdindex = OBD_NOT_FOUND;
1655 *obdindex = obd_valid;
1657 *obdindexes = indexes;
1665 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
1669 if (param->mdtuuid) {
1670 ret = setup_indexes(dir, path, param->mdtuuid, param->num_mdts,
1671 ¶m->mdtindexes, ¶m->mdtindex, LMV_TYPE);
1675 if (param->obduuid) {
1676 ret = setup_indexes(dir, path, param->obduuid, param->num_obds,
1677 ¶m->obdindexes, ¶m->obdindex, LOV_TYPE);
1681 param->got_uuids = 1;
1685 int llapi_ostlist(char *path, struct find_param *param)
1690 dir = opendir(path);
1694 ret = setup_obd_uuid(dir, path, param);
1701 * Given a filesystem name, or a pathname of a file on a lustre filesystem,
1702 * tries to determine the path to the filesystem's clilov directory under /proc
1704 * fsname is limited to MTI_NAME_MAXLEN in lustre_idl.h
1705 * The NUL terminator is compensated by the additional "%s" bytes. */
1706 #define LOV_LEN (sizeof("/proc/fs/lustre/lov/%s-clilov-*") + MTI_NAME_MAXLEN)
1707 static int clilovpath(const char *fsname, const char *const pathname,
1711 char pattern[LOV_LEN];
1712 char buffer[PATH_MAX + 1];
1714 if (fsname == NULL) {
1715 rc = llapi_search_fsname(pathname, buffer);
1721 snprintf(pattern, sizeof(pattern), "/proc/fs/lustre/lov/%s-clilov-*",
1724 rc = first_match(pattern, buffer);
1728 strncpy(clilovpath, buffer, sizeof(buffer));
1734 * Given the path to a stripe attribute proc file, tries to open and
1735 * read the attribute and return the value using the attr parameter
1737 static int sattr_read_attr(const char *const fpath,
1742 char line[PATH_MAX + 1];
1745 f = fopen(fpath, "r");
1748 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open '%s'", fpath);
1752 if (fgets(line, sizeof(line), f) != NULL) {
1755 llapi_error(LLAPI_MSG_ERROR, errno, "Cannot read from '%s'", fpath);
1764 * Tries to determine the default stripe attributes for a given filesystem. The
1765 * filesystem to check should be specified by fsname, or will be determined
1768 static int sattr_get_defaults(const char *const fsname,
1769 const char *const pathname,
1770 unsigned int *scount,
1771 unsigned int *ssize,
1772 unsigned int *soffset)
1775 char dpath[PATH_MAX + 1];
1776 char fpath[PATH_MAX + 1];
1778 rc = clilovpath(fsname, pathname, dpath);
1783 snprintf(fpath, PATH_MAX, "%s/stripecount", dpath);
1784 rc = sattr_read_attr(fpath, scount);
1790 snprintf(fpath, PATH_MAX, "%s/stripesize", dpath);
1791 rc = sattr_read_attr(fpath, ssize);
1797 snprintf(fpath, PATH_MAX, "%s/stripeoffset", dpath);
1798 rc = sattr_read_attr(fpath, soffset);
1807 * Tries to gather the default stripe attributes for a given filesystem. If
1808 * the attributes can be determined, they are cached for easy retreival the
1809 * next time they are needed. Only a single filesystem's attributes are
1812 static int sattr_cache_get_defaults(const char *const fsname,
1813 const char *const pathname,
1814 unsigned int *scount,
1815 unsigned int *ssize,
1816 unsigned int *soffset)
1819 char fsname[PATH_MAX + 1];
1820 unsigned int stripecount;
1821 unsigned int stripesize;
1822 unsigned int stripeoffset;
1828 char fsname_buf[PATH_MAX + 1];
1829 unsigned int tmp[3];
1831 if (fsname == NULL) {
1832 rc = llapi_search_fsname(pathname, fsname_buf);
1836 strncpy(fsname_buf, fsname, PATH_MAX);
1839 if (strncmp(fsname_buf, cache.fsname, PATH_MAX) != 0) {
1841 * Ensure all 3 sattrs (count, size, and offset) are
1842 * successfully retrieved and stored in tmp before writing to
1845 rc = sattr_get_defaults(fsname_buf, NULL, &tmp[0], &tmp[1],
1850 cache.stripecount = tmp[0];
1851 cache.stripesize = tmp[1];
1852 cache.stripeoffset = tmp[2];
1853 strncpy(cache.fsname, fsname_buf, PATH_MAX);
1857 *scount = cache.stripecount;
1859 *ssize = cache.stripesize;
1861 *soffset = cache.stripeoffset;
1866 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
1867 struct lov_user_ost_data_v1 *objects,
1868 int is_dir, int verbose, int depth,
1869 int raw, char *pool_name)
1871 char *prefix = is_dir ? "" : "lmm_";
1872 char nl = is_dir ? ' ' : '\n';
1875 if (is_dir && lum->lmm_object_seq == FID_SEQ_LOV_DEFAULT) {
1876 lum->lmm_object_seq = FID_SEQ_OST_MDT0;
1877 if (verbose & VERBOSE_DETAIL)
1878 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
1881 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
1882 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1884 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1885 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
1887 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
1888 lum->lmm_object_seq);
1889 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
1890 lum->lmm_object_id);
1893 if (verbose & VERBOSE_COUNT) {
1894 if (verbose & ~VERBOSE_COUNT)
1895 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
1898 if (!raw && lum->lmm_stripe_count == 0) {
1899 unsigned int scount;
1900 rc = sattr_cache_get_defaults(NULL, path,
1904 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1907 llapi_error(LLAPI_MSG_ERROR, rc,
1908 "Cannot determine default"
1911 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1912 lum->lmm_stripe_count ==
1913 (typeof(lum->lmm_stripe_count))(-1)
1914 ? -1 : lum->lmm_stripe_count, nl);
1917 llapi_printf(LLAPI_MSG_NORMAL, "%hd%c",
1918 (__s16)lum->lmm_stripe_count, nl);
1922 if (verbose & VERBOSE_SIZE) {
1923 if (verbose & ~VERBOSE_SIZE)
1924 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
1926 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
1928 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
1931 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", ssize,
1934 llapi_error(LLAPI_MSG_ERROR, rc,
1935 "Cannot determine default"
1938 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1939 lum->lmm_stripe_size, nl);
1943 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1944 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
1945 lum->lmm_pattern, nl);
1948 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
1949 if (verbose & ~VERBOSE_GENERATION)
1950 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
1952 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1953 (int)lum->u.lum_layout_gen, nl);
1956 if (verbose & VERBOSE_OFFSET) {
1957 if (verbose & ~VERBOSE_OFFSET)
1958 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
1961 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1962 lum->lmm_stripe_offset ==
1963 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
1964 lum->lmm_stripe_offset, nl);
1966 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1967 objects[0].l_ost_idx, nl);
1970 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
1971 if (verbose & ~VERBOSE_POOL)
1972 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
1974 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", pool_name, nl);
1977 if (is_dir && (verbose != VERBOSE_OBJID))
1978 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1981 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
1982 struct lov_user_ost_data_v1 *objects,
1983 char *path, int is_dir, int obdindex,
1984 int depth, int header, int raw)
1986 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
1989 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
1990 if (obdindex == objects[i].l_ost_idx) {
1998 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
1999 depth, raw, pool_name);
2001 if (!is_dir && (header & VERBOSE_OBJID)) {
2003 llapi_printf(LLAPI_MSG_NORMAL,
2004 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
2006 for (i = 0; i < lum->lmm_stripe_count; i++) {
2007 int idx = objects[i].l_ost_idx;
2008 long long oid = objects[i].l_object_id;
2009 long long gr = objects[i].l_object_seq;
2010 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2011 llapi_printf(LLAPI_MSG_NORMAL,
2012 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
2014 obdindex == idx ? " *" : "");
2016 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2020 void llapi_lov_dump_user_lmm(struct find_param *param,
2021 char *path, int is_dir)
2023 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
2024 case LOV_USER_MAGIC_V1:
2025 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
2026 param->lmd->lmd_lmm.lmm_objects,
2028 param->obdindex, param->maxdepth,
2029 param->verbose, param->raw);
2031 case LOV_USER_MAGIC_V3: {
2032 char pool_name[LOV_MAXPOOLNAME + 1];
2033 struct lov_user_ost_data_v1 *objects;
2034 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2036 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
2037 pool_name[LOV_MAXPOOLNAME] = '\0';
2038 objects = lmmv3->lmm_objects;
2039 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
2040 objects, path, is_dir,
2041 param->obdindex, param->maxdepth,
2042 param->verbose, param->raw);
2046 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2047 "(expecting one of %#x %#x %#x)\n",
2048 *(__u32 *)¶m->lmd->lmd_lmm,
2049 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3);
2054 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2060 fname = strrchr(path, '/');
2062 /* It should be a file (or other non-directory) */
2063 if (fname == NULL) {
2064 dname = (char *)malloc(2);
2068 fname = (char *)path;
2070 dname = (char *)malloc(fname - path + 1);
2073 strncpy(dname, path, fname - path);
2074 dname[fname - path] = '\0';
2078 fd = open(dname, O_RDONLY);
2085 strcpy((char *)lum, fname);
2086 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2089 if (close(fd) == -1 && rc == 0)
2096 int llapi_file_lookup(int dirfd, const char *name)
2098 struct obd_ioctl_data data = { 0 };
2103 if (dirfd < 0 || name == NULL)
2106 data.ioc_version = OBD_IOCTL_VERSION;
2107 data.ioc_len = sizeof(data);
2108 data.ioc_inlbuf1 = (char *)name;
2109 data.ioc_inllen1 = strlen(name) + 1;
2111 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2113 llapi_error(LLAPI_MSG_ERROR, rc,
2114 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2119 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2125 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2126 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2128 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2129 * The table below gives the answers for the specified parameters (value and
2130 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2131 * --------------------------------------
2132 * 1 | file > limit; sign > 0 | -1 / -1 |
2133 * 2 | file = limit; sign > 0 | -1 / -1 |
2134 * 3 | file < limit; sign > 0 | ? / 1 |
2135 * 4 | file > limit; sign = 0 | -1 / -1 |
2136 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2137 * 6 | file < limit; sign = 0 | ? / -1 |
2138 * 7 | file > limit; sign < 0 | 1 / 1 |
2139 * 8 | file = limit; sign < 0 | ? / -1 |
2140 * 9 | file < limit; sign < 0 | ? / -1 |
2141 * --------------------------------------
2142 * Note: 5th actually means that the value is within the interval
2143 * (limit - margin, limit]. */
2144 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2145 int sign, int negopt, unsigned long long margin,
2151 /* Drop the fraction of margin (of days). */
2152 if (file + margin <= limit)
2154 } else if (sign == 0) {
2155 if (file <= limit && file + margin > limit)
2157 else if (file + margin <= limit)
2159 } else if (sign < 0) {
2166 return negopt ? ~ret + 1 : ret;
2169 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2170 * Return -1 or 1 if file timestamp does not or does match the given criteria
2171 * correspondingly. Return 0 if the MDS time is being checked and there are
2172 * attributes on OSTs and it is not yet clear if the timespamp matches.
2174 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2175 * updated timestamps. */
2176 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2181 /* Check if file is accepted. */
2183 ret = find_value_cmp(st->st_atime, param->atime,
2184 param->asign, param->exclude_atime,
2192 ret = find_value_cmp(st->st_mtime, param->mtime,
2193 param->msign, param->exclude_mtime,
2198 /* If the previous check matches, but this one is not yet clear,
2199 * we should return 0 to do an RPC on OSTs. */
2205 ret = find_value_cmp(st->st_ctime, param->ctime,
2206 param->csign, param->exclude_ctime,
2211 /* If the previous check matches, but this one is not yet clear,
2212 * we should return 0 to do an RPC on OSTs. */
2221 * Check whether the stripes matches the indexes user provided
2225 static int check_obd_match(struct find_param *param)
2227 lstat_t *st = ¶m->lmd->lmd_st;
2228 struct lov_user_ost_data_v1 *lmm_objects;
2231 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
2234 if (!S_ISREG(st->st_mode))
2237 /* Only those files should be accepted, which have a
2238 * stripe on the specified OST. */
2239 if (!param->lmd->lmd_lmm.lmm_stripe_count)
2242 if (param->lmd->lmd_lmm.lmm_magic ==
2243 LOV_USER_MAGIC_V3) {
2244 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2246 lmm_objects = lmmv3->lmm_objects;
2247 } else if (param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2248 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
2250 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2251 __func__, param->lmd->lmd_lmm.lmm_magic);
2255 for (i = 0; i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
2256 for (j = 0; j < param->num_obds; j++) {
2257 if (param->obdindexes[j] ==
2258 lmm_objects[i].l_ost_idx) {
2259 if (param->exclude_obd)
2266 if (param->exclude_obd)
2271 static int check_mdt_match(struct find_param *param)
2275 if (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND)
2278 /* FIXME: For striped dir, we should get stripe information and check */
2279 for (i = 0; i < param->num_mdts; i++) {
2280 if (param->mdtindexes[i] == param->file_mdtindex)
2281 if (param->exclude_mdt)
2286 if (param->exclude_mdt)
2292 * Check whether the obd is active or not, if it is
2293 * not active, just print the object affected by this
2296 static int print_failed_tgt(struct find_param *param, char *path, int type)
2298 struct obd_statfs stat_buf;
2299 struct obd_uuid uuid_buf;
2302 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2304 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2305 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2306 ret = llapi_obd_statfs(path, type,
2307 param->obdindex, &stat_buf,
2310 llapi_printf(LLAPI_MSG_NORMAL,
2311 "obd_uuid: %s failed %s ",
2312 param->obduuid->uuid,
2318 static int cb_find_init(char *path, DIR *parent, DIR *dir,
2319 void *data, cfs_dirent_t *de)
2321 struct find_param *param = (struct find_param *)data;
2322 int decision = 1; /* 1 is accepted; -1 is rejected. */
2323 lstat_t *st = ¶m->lmd->lmd_st;
2325 int checked_type = 0;
2328 LASSERT(parent != NULL || dir != NULL);
2330 if (param->have_fileinfo == 0)
2331 param->lmd->lmd_lmm.lmm_stripe_count = 0;
2333 /* If a regular expression is presented, make the initial decision */
2334 if (param->pattern != NULL) {
2335 char *fname = strrchr(path, '/');
2336 fname = (fname == NULL ? path : fname + 1);
2337 ret = fnmatch(param->pattern, fname, 0);
2338 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
2339 (ret == 0 && param->exclude_pattern))
2343 /* See if we can check the file type from the dirent. */
2344 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
2345 de->d_type < DT_MAX) {
2347 if (llapi_dir_filetype_table[de->d_type] == param->type) {
2348 if (param->exclude_type)
2351 if (!param->exclude_type)
2358 /* Request MDS for the stat info if some of these parameters need
2359 * to be compared. */
2360 if (param->obduuid || param->mdtuuid || param->check_uid ||
2361 param->check_gid || param->check_pool || param->atime ||
2362 param->ctime || param->mtime || param->check_size ||
2363 param->check_stripecount || param->check_stripesize)
2366 if (param->type && checked_type == 0)
2369 if (param->have_fileinfo == 0 && decision == 0) {
2370 ret = get_lmd_info(path, parent, dir, param->lmd,
2374 ret = llapi_file_fget_mdtidx(dirfd(dir),
2375 ¶m->file_mdtindex);
2380 ret = lstat_f(path, &tmp_st);
2383 llapi_error(LLAPI_MSG_ERROR, ret,
2384 "error: %s: lstat failed"
2385 "for %s", __func__, path);
2388 if (S_ISREG(tmp_st.st_mode)) {
2389 fd = open(path, O_RDONLY);
2391 ret = llapi_file_fget_mdtidx(fd,
2392 ¶m->file_mdtindex);
2398 /* For special inode, it assumes to
2399 * reside on the same MDT with the
2402 ret = llapi_file_fget_mdtidx(fd,
2403 ¶m->file_mdtindex);
2416 if (param->type && !checked_type) {
2417 if ((st->st_mode & S_IFMT) == param->type) {
2418 if (param->exclude_type)
2421 if (!param->exclude_type)
2427 if (param->obduuid || param->mdtuuid) {
2428 if (lustre_fs && param->got_uuids &&
2429 param->st_dev != st->st_dev) {
2430 /* A lustre/lustre mount point is crossed. */
2431 param->got_uuids = 0;
2432 param->obds_printed = 0;
2433 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2436 if (lustre_fs && !param->got_uuids) {
2437 ret = setup_target_indexes(dir ? dir : parent, path,
2442 param->st_dev = st->st_dev;
2443 } else if (!lustre_fs && param->got_uuids) {
2444 /* A lustre/non-lustre mount point is crossed. */
2445 param->got_uuids = 0;
2446 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2450 if (param->check_stripesize) {
2451 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_size,
2453 param->stripesize_sign,
2454 param->exclude_stripesize,
2455 param->stripesize_units, 0);
2460 if (param->check_stripecount) {
2461 decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_count,
2463 param->stripecount_sign,
2464 param->exclude_stripecount, 1, 0);
2469 /* If an OBD UUID is specified but none matches, skip this file. */
2470 if ((param->obduuid && param->obdindex == OBD_NOT_FOUND) ||
2471 (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND))
2474 /* If a OST or MDT UUID is given, and some OST matches,
2476 if (param->obdindex != OBD_NOT_FOUND ||
2477 param->mdtindex != OBD_NOT_FOUND) {
2478 if (param->obduuid) {
2479 if (check_obd_match(param)) {
2480 /* If no mdtuuid is given, we are done.
2481 * Otherwise, fall through to the mdtuuid
2483 if (!param->mdtuuid)
2489 if (param->mdtuuid) {
2490 if (check_mdt_match(param))
2496 if (param->check_uid) {
2497 if (st->st_uid == param->uid) {
2498 if (param->exclude_uid)
2501 if (!param->exclude_uid)
2506 if (param->check_gid) {
2507 if (st->st_gid == param->gid) {
2508 if (param->exclude_gid)
2511 if (!param->exclude_gid)
2516 if (param->check_pool) {
2517 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2519 /* empty requested pool is taken as no pool search => V1 */
2520 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
2521 (param->poolname[0] == '\0')) ||
2522 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2523 (strncmp(lmmv3->lmm_pool_name,
2524 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
2525 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2526 (strcmp(param->poolname, "*") == 0))) {
2527 if (param->exclude_pool)
2530 if (!param->exclude_pool)
2535 /* Check the time on mds. */
2537 if (param->atime || param->ctime || param->mtime) {
2540 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
2541 param->lmd->lmd_lmm.lmm_stripe_count)
2543 decision = find_time_check(st, param, for_mds);
2548 /* If file still fits the request, ask ost for updated info.
2549 The regular stat is almost of the same speed as some new
2550 'glimpse-size-ioctl'. */
2552 if (param->check_size && S_ISREG(st->st_mode) &&
2553 param->lmd->lmd_lmm.lmm_stripe_count)
2557 /* For regular files with the stripe the decision may have not
2558 * been taken yet if *time or size is to be checked. */
2559 LASSERT((S_ISREG(st->st_mode) &&
2560 param->lmd->lmd_lmm.lmm_stripe_count) ||
2561 param->mdtindex != OBD_NOT_FOUND);
2563 if (param->obdindex != OBD_NOT_FOUND)
2564 print_failed_tgt(param, path, LL_STATFS_LOV);
2566 if (param->mdtindex != OBD_NOT_FOUND)
2567 print_failed_tgt(param, path, LL_STATFS_LMV);
2570 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
2571 (void *)param->lmd);
2572 } else if (parent) {
2573 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
2574 (void *)param->lmd);
2578 if (errno == ENOENT) {
2579 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
2580 "warning: %s: %s does not exist",
2585 llapi_error(LLAPI_MSG_ERROR, ret,
2586 "%s: IOC_LOV_GETINFO on %s failed",
2592 /* Check the time on osc. */
2593 decision = find_time_check(st, param, 0);
2600 if (param->check_size)
2601 decision = find_value_cmp(st->st_size, param->size,
2602 param->size_sign, param->exclude_size,
2603 param->size_units, 0);
2605 if (decision != -1) {
2606 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
2608 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
2610 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2614 /* Do not get down anymore? */
2615 if (param->depth == param->maxdepth)
2622 int llapi_find(char *path, struct find_param *param)
2624 return param_callback(path, cb_find_init, cb_common_fini, param);
2628 * Get MDT number that the file/directory inode referenced
2629 * by the open fd resides on.
2630 * Return 0 and mdtidx on success, or -ve errno.
2632 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
2634 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
2639 static int cb_get_mdt_index(char *path, DIR *parent, DIR *d, void *data,
2642 struct find_param *param = (struct find_param *)data;
2646 LASSERT(parent != NULL || d != NULL);
2649 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
2650 } else if (parent) {
2653 fd = open(path, O_RDONLY);
2655 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
2663 if (ret == -ENODATA) {
2664 if (!param->obduuid)
2665 llapi_printf(LLAPI_MSG_NORMAL,
2666 "%s has no stripe info\n", path);
2668 } else if (ret == -ENOENT) {
2669 llapi_error(LLAPI_MSG_WARN, ret,
2670 "warning: %s: %s does not exist",
2673 } else if (ret == -ENOTTY) {
2674 llapi_error(LLAPI_MSG_ERROR, ret,
2675 "%s: '%s' not on a Lustre fs?",
2678 llapi_error(LLAPI_MSG_ERROR, ret,
2679 "error: %s: LL_IOC_GET_MDTIDX failed for %s",
2685 if (param->quiet || !(param->verbose & VERBOSE_DETAIL))
2686 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
2688 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
2692 /* Do not get down anymore? */
2693 if (param->depth == param->maxdepth)
2700 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
2703 struct find_param *param = (struct find_param *)data;
2706 LASSERT(parent != NULL || d != NULL);
2708 if (param->obduuid) {
2710 ret = setup_obd_uuid(d ? d : parent, path, param);
2716 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
2717 (void *)¶m->lmd->lmd_lmm);
2718 } else if (parent) {
2719 char *fname = strrchr(path, '/');
2720 fname = (fname == NULL ? path : fname + 1);
2722 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
2724 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
2725 (void *)¶m->lmd->lmd_lmm);
2729 if (errno == ENODATA && d != NULL) {
2730 /* We need to "fake" the "use the default" values
2731 * since the lmm struct is zeroed out at this point.
2732 * The magic needs to be set in order to satisfy
2733 * a check later on in the code path.
2734 * The object_seq needs to be set for the "(Default)"
2735 * prefix to be displayed. */
2736 struct lov_user_md *lmm = ¶m->lmd->lmd_lmm;
2737 lmm->lmm_magic = LOV_MAGIC_V1;
2739 lmm->lmm_object_seq = FID_SEQ_LOV_DEFAULT;
2740 lmm->lmm_stripe_count = 0;
2741 lmm->lmm_stripe_size = 0;
2742 lmm->lmm_stripe_offset = -1;
2745 } else if (errno == ENODATA && parent != NULL) {
2746 if (!param->obduuid)
2747 llapi_printf(LLAPI_MSG_NORMAL,
2748 "%s has no stripe info\n", path);
2750 } else if (errno == ENOENT) {
2751 llapi_error(LLAPI_MSG_WARN, -ENOENT,
2752 "warning: %s: %s does not exist",
2755 } else if (errno == ENOTTY) {
2757 llapi_error(LLAPI_MSG_ERROR, ret,
2758 "%s: '%s' not on a Lustre fs?",
2762 llapi_error(LLAPI_MSG_ERROR, ret,
2763 "error: %s: %s failed for %s",
2764 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
2765 "IOC_MDC_GETFILESTRIPE", path);
2772 if (!(param->verbose & VERBOSE_MDTINDEX))
2773 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
2776 /* Do not get down anymore? */
2777 if (param->depth == param->maxdepth)
2784 int llapi_getstripe(char *path, struct find_param *param)
2786 return param_callback(path, (param->verbose & VERBOSE_MDTINDEX) ?
2787 cb_get_mdt_index : cb_getstripe,
2788 cb_common_fini, param);
2791 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
2792 struct obd_statfs *stat_buf,
2793 struct obd_uuid *uuid_buf)
2796 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
2798 struct obd_ioctl_data data = { 0 };
2801 data.ioc_inlbuf1 = (char *)&type;
2802 data.ioc_inllen1 = sizeof(__u32);
2803 data.ioc_inlbuf2 = (char *)&index;
2804 data.ioc_inllen2 = sizeof(__u32);
2805 data.ioc_pbuf1 = (char *)stat_buf;
2806 data.ioc_plen1 = sizeof(struct obd_statfs);
2807 data.ioc_pbuf2 = (char *)uuid_buf;
2808 data.ioc_plen2 = sizeof(struct obd_uuid);
2810 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
2812 llapi_error(LLAPI_MSG_ERROR, rc,
2813 "llapi_obd_statfs: error packing ioctl data");
2817 fd = open(path, O_RDONLY);
2818 if (errno == EISDIR)
2819 fd = open(path, O_DIRECTORY | O_RDONLY);
2822 rc = errno ? -errno : -EBADF;
2823 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
2827 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
2829 rc = errno ? -errno : -EINVAL;
2835 #define MAX_STRING_SIZE 128
2837 int llapi_ping(char *obd_type, char *obd_name)
2839 char path[MAX_STRING_SIZE];
2843 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
2844 obd_type, obd_name);
2846 fd = open(path, O_WRONLY);
2849 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2853 rc = write(fd, buf, 1);
2863 int llapi_target_iterate(int type_num, char **obd_type,
2864 void *args, llapi_cb_t cb)
2866 char buf[MAX_STRING_SIZE];
2867 FILE *fp = fopen(DEVICES_LIST, "r");
2872 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
2876 while (fgets(buf, sizeof(buf), fp) != NULL) {
2877 char *obd_type_name = NULL;
2878 char *obd_name = NULL;
2879 char *obd_uuid = NULL;
2881 struct obd_statfs osfs_buffer;
2883 while(bufp[0] == ' ')
2886 for(i = 0; i < 3; i++) {
2887 obd_type_name = strsep(&bufp, " ");
2889 obd_name = strsep(&bufp, " ");
2890 obd_uuid = strsep(&bufp, " ");
2892 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
2894 for (i = 0; i < type_num; i++) {
2895 if (strcmp(obd_type_name, obd_type[i]) != 0)
2898 cb(obd_type_name, obd_name, obd_uuid, args);
2905 static void do_target_check(char *obd_type_name, char *obd_name,
2906 char *obd_uuid, void *args)
2910 rc = llapi_ping(obd_type_name, obd_name);
2911 if (rc == ENOTCONN) {
2912 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
2914 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
2916 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
2920 int llapi_target_check(int type_num, char **obd_type, char *dir)
2922 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
2925 #undef MAX_STRING_SIZE
2927 int llapi_catinfo(char *dir, char *keyword, char *node_name)
2929 char raw[OBD_MAX_IOCTL_BUFFER];
2930 char out[LLOG_CHUNK_SIZE];
2932 struct obd_ioctl_data data = { 0 };
2937 sprintf(key, "%s", keyword);
2938 memset(raw, 0, sizeof(raw));
2939 memset(out, 0, sizeof(out));
2940 data.ioc_inlbuf1 = key;
2941 data.ioc_inllen1 = strlen(key) + 1;
2943 data.ioc_inlbuf2 = node_name;
2944 data.ioc_inllen2 = strlen(node_name) + 1;
2946 data.ioc_pbuf1 = out;
2947 data.ioc_plen1 = sizeof(out);
2948 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
2952 root = opendir(dir);
2955 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", dir);
2959 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
2962 llapi_error(LLAPI_MSG_ERROR, rc,
2963 "ioctl OBD_IOC_CATINFO failed");
2965 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
2972 /* Is this a lustre fs? */
2973 int llapi_is_lustre_mnttype(const char *type)
2975 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
2978 /* Is this a lustre client fs? */
2979 int llapi_is_lustre_mnt(struct mntent *mnt)
2981 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
2982 strstr(mnt->mnt_fsname, ":/") != NULL);
2985 int llapi_quotacheck(char *mnt, int check_type)
2990 root = opendir(mnt);
2993 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2997 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
3005 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
3011 root = opendir(mnt);
3014 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3019 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
3023 if (poll_intvl < 30)
3031 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3036 root = opendir(mnt);
3039 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3043 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
3051 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
3054 struct find_param *param = (struct find_param *)data;
3058 LASSERT(parent != NULL || d != NULL);
3060 rc = get_lmd_info(path, parent, d, param->lmd, param->lumlen);
3062 if (rc == -ENODATA) {
3063 if (!param->obduuid && !param->quiet)
3064 llapi_error(LLAPI_MSG_ERROR, -ENODATA,
3065 "%s has no stripe info", path);
3067 } else if (rc == -ENOENT) {
3073 st = ¶m->lmd->lmd_st;
3075 /* libc chown() will do extra check, and if the real owner is
3076 * the same as the ones to set, it won't fall into kernel, so
3077 * invoke syscall directly. */
3078 rc = syscall(SYS_chown, path, -1, -1);
3080 llapi_error(LLAPI_MSG_ERROR, errno,
3081 "error: chown %s", path);
3083 rc = chmod(path, st->st_mode);
3086 llapi_error(LLAPI_MSG_ERROR, rc, "error: chmod %s (%hu)",
3093 int llapi_quotachown(char *path, int flag)
3095 struct find_param param;
3097 memset(¶m, 0, sizeof(param));
3098 param.recursive = 1;
3102 return param_callback(path, cb_quotachown, NULL, ¶m);
3108 #include <sys/wait.h>
3112 static int rmtacl_notify(int ops)
3116 int found = 0, fd, rc;
3118 fp = setmntent(MOUNTED, "r");
3121 llapi_error(LLAPI_MSG_ERROR, rc,
3122 "error setmntent(%s)", MOUNTED);
3127 mnt = getmntent(fp);
3131 if (!llapi_is_lustre_mnt(mnt))
3134 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
3137 llapi_error(LLAPI_MSG_ERROR, rc,
3138 "Can't open '%s'\n", mnt->mnt_dir);
3142 rc = ioctl(fd, LL_IOC_RMTACL, ops);
3145 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d\n", fd);
3155 static char *next_token(char *p, int div)
3161 while (*p && *p != ':' && !isspace(*p))
3164 while (*p == ':' || isspace(*p))
3167 return *p ? p : NULL;
3170 static int rmtacl_name2id(char *name, int is_user)
3175 pw = getpwnam(name);
3179 return (int)(pw->pw_uid);
3183 gr = getgrnam(name);
3187 return (int)(gr->gr_gid);
3191 static int isodigit(int c)
3193 return (c >= '0' && c <= '7') ? 1 : 0;
3197 * Whether the name is just digits string (uid/gid) already or not.
3202 static int str_is_id(char *str)
3209 if (*str == 'x' || *str == 'X') { /* for Hex. */
3210 if (!isxdigit(*(++str)))
3213 while (isxdigit(*(++str)));
3214 } else if (isodigit(*str)) { /* for Oct. */
3215 while (isodigit(*(++str)));
3217 } else if (isdigit(*str)) { /* for Dec. */
3218 while (isdigit(*(++str)));
3221 return (*str == 0) ? 1 : 0;
3231 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
3233 static rmtacl_name_t rmtacl_namelist[] = {
3234 { RMTACL_OPTNAME("user:"), 1, 0 },
3235 { RMTACL_OPTNAME("group:"), 0, 0 },
3236 { RMTACL_OPTNAME("default:user:"), 1, 0 },
3237 { RMTACL_OPTNAME("default:group:"), 0, 0 },
3238 /* for --tabular option */
3239 { RMTACL_OPTNAME("user"), 1, 1 },
3240 { RMTACL_OPTNAME("group"), 0, 1 },
3244 static int rgetfacl_output(char *str)
3246 char *start = NULL, *end = NULL;
3247 int is_user = 0, n, id;
3254 for (rn = rmtacl_namelist; rn->name; rn++) {
3255 if(strncmp(str, rn->name, rn->length) == 0) {
3256 if (!rn->next_token)
3257 start = str + rn->length;
3259 start = next_token(str + rn->length, 0);
3260 is_user = rn->is_user;
3265 end = next_token(start, 1);
3266 if (end == NULL || start == end) {
3267 n = printf("%s", str);
3273 id = rmtacl_name2id(start, is_user);
3274 if (id == INVALID_ID) {
3275 if (str_is_id(start)) {
3277 n = printf("%s", str);
3280 } else if ((id == NOBODY_UID && is_user) ||
3281 (id == NOBODY_GID && !is_user)) {
3283 n = printf("%s", str);
3287 n = printf("%s%d%s", str, id, end);
3292 static int child_status(int status)
3294 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
3297 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
3300 int fd[2], status, rc;
3307 llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe\n");
3314 llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork\n");
3319 /* child process redirects its output. */
3322 if (dup2(fd[1], 1) < 0) {
3324 llapi_error(LLAPI_MSG_ERROR, rc,
3325 "Can't dup2 %d\n", fd[1]);
3335 status = rmtacl_notify(ops);
3339 exit(execvp(argv[0], argv));
3342 /* the following is parent process */
3343 fp = fdopen(fd[0], "r");
3346 llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed\n", fd[0]);
3352 while (fgets(buf, PIPE_BUF, fp) != NULL) {
3353 if (output_func(buf) < 0)
3354 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
3360 if (waitpid(pid, &status, 0) < 0) {
3362 llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed\n", pid);
3366 return child_status(status);
3369 int llapi_lsetfacl(int argc, char *argv[])
3371 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
3374 int llapi_lgetfacl(int argc, char *argv[])
3376 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
3379 int llapi_rsetfacl(int argc, char *argv[])
3381 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
3384 int llapi_rgetfacl(int argc, char *argv[])
3386 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
3389 int llapi_cp(int argc, char *argv[])
3393 rc = rmtacl_notify(RMT_RSETFACL);
3397 exit(execvp(argv[0], argv));
3400 int llapi_ls(int argc, char *argv[])
3404 rc = rmtacl_notify(RMT_LGETFACL);
3408 exit(execvp(argv[0], argv));
3411 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
3412 * format must have %s%s, buf must be > 16
3413 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
3414 * then buf = "lustre-MDT0000"
3416 static int get_mdtname(char *name, char *format, char *buf)
3418 char suffix[]="-MDT0000";
3419 int len = strlen(name);
3421 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
3422 name[len - 5] = '\0';
3427 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
3430 /* Not enough room to add suffix */
3431 llapi_err_noerrno(LLAPI_MSG_ERROR,
3432 "MDT name too long |%s|", name);
3437 return sprintf(buf, format, name, suffix);
3440 /** ioctl on filsystem root, with mdtindex sent as data
3441 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
3442 * \param mdtidxp pointer to integer within data to be filled in with the
3443 * mdt index (0 if no mdt is specified). NULL won't be filled.
3445 static int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
3452 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
3453 Open root and parse mdt index. */
3454 if (mdtname[0] == '/') {
3456 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
3457 (char *)mdtname, -1);
3459 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
3461 ptr = fsname + strlen(fsname) - 8;
3463 index = strtol(ptr + 4, NULL, 10);
3464 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
3468 llapi_err_noerrno(LLAPI_MSG_ERROR,
3469 "Can't open %s: %d\n", mdtname, rc);
3476 rc = ioctl(fd, opc, data);
3481 if (rc && want_error)
3482 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
3488 /****** Changelog API ********/
3490 static int changelog_ioctl(const char *mdtname, int opc, int id,
3491 long long recno, int flags)
3493 struct ioc_changelog data;
3497 data.icc_recno = recno;
3498 data.icc_flags = flags;
3499 idx = (int *)(&data.icc_mdtindex);
3501 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
3504 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
3505 struct changelog_private {
3508 lustre_kernelcomm kuc;
3511 /** Start reading from a changelog
3512 * @param priv Opaque private control structure
3513 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
3514 * @param device Report changes recorded on this MDT
3515 * @param startrec Report changes beginning with this record number
3516 * (just call llapi_changelog_fini when done; don't need an endrec)
3518 int llapi_changelog_start(void **priv, int flags, const char *device,
3521 struct changelog_private *cp;
3524 /* Set up the receiver control struct */
3525 cp = calloc(1, sizeof(*cp));
3529 cp->magic = CHANGELOG_PRIV_MAGIC;
3532 /* Set up the receiver */
3533 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */);
3539 /* Tell the kernel to start sending */
3540 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
3542 /* Only the kernel reference keeps the write side open */
3543 close(cp->kuc.lk_wfd);
3546 /* frees and clears priv */
3547 llapi_changelog_fini(priv);
3558 /** Finish reading from a changelog */
3559 int llapi_changelog_fini(void **priv)
3561 struct changelog_private *cp = (struct changelog_private *)*priv;
3563 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3566 libcfs_ukuc_stop(&cp->kuc);
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_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_rec. Use pointer math to skip
3621 * kuch_hdr and point directly to the message payload.
3623 *rech = (struct changelog_rec *)(kuch + 1);
3633 /** Release the changelog record when done with it. */
3634 int llapi_changelog_free(struct changelog_rec **rech)
3637 /* We allocated memory starting at the kuc_hdr, but passed
3638 * the consumer a pointer to the payload.
3639 * Use pointer math to get back to the header.
3641 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
3648 int llapi_changelog_clear(const char *mdtname, const char *idstr,
3654 llapi_err_noerrno(LLAPI_MSG_ERROR,
3655 "can't purge negative records\n");
3659 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
3660 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
3661 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
3662 llapi_err_noerrno(LLAPI_MSG_ERROR,
3663 "expecting id of the form '"
3664 CHANGELOG_USER_PREFIX
3665 "<num>'; got '%s'\n", idstr);
3669 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
3672 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
3673 int buflen, long long *recno, int *linkno)
3676 struct getinfo_fid2path *gf;
3679 while (*fidstr == '[')
3682 sscanf(fidstr, SFID, RFID(&fid));
3683 if (!fid_is_sane(&fid)) {
3684 llapi_err_noerrno(LLAPI_MSG_ERROR,
3685 "bad FID format [%s], should be "DFID"\n",
3686 fidstr, (__u64)1, 2, 0);
3690 gf = malloc(sizeof(*gf) + buflen);
3694 gf->gf_recno = *recno;
3695 gf->gf_linkno = *linkno;
3696 gf->gf_pathlen = buflen;
3698 /* Take path or fsname */
3699 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
3702 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
3704 memcpy(buf, gf->gf_path, gf->gf_pathlen);
3705 *recno = gf->gf_recno;
3706 *linkno = gf->gf_linkno;
3713 static int path2fid_from_lma(const char *path, lustre_fid *fid)
3716 struct lustre_mdt_attrs *lma;
3719 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
3722 lma = (struct lustre_mdt_attrs *)buf;
3723 fid_le_to_cpu(fid, &lma->lma_self_fid);
3727 int llapi_path2fid(const char *path, lustre_fid *fid)
3731 memset(fid, 0, sizeof(*fid));
3732 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
3734 if (errno == ELOOP || errno == ENXIO)
3735 return path2fid_from_lma(path, fid);
3739 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
3740 if (rc == -EINVAL || rc == -ENOTTY)
3741 rc = path2fid_from_lma(path, fid);
3747 /****** HSM Copytool API ********/
3748 #define CT_PRIV_MAGIC 0xC0BE2001
3749 struct copytool_private {
3752 lustre_kernelcomm kuc;
3756 #include <libcfs/libcfs.h>
3758 /** Register a copytool
3759 * @param[out] priv Opaque private control structure
3760 * @param fsname Lustre filesystem
3761 * @param flags Open flags, currently unused (e.g. O_NONBLOCK)
3762 * @param archive_count
3763 * @param archives Which archive numbers this copytool is responsible for
3765 int llapi_copytool_start(void **priv, char *fsname, int flags,
3766 int archive_count, int *archives)
3768 struct copytool_private *ct;
3771 if (archive_count > 0 && archives == NULL) {
3772 llapi_err_noerrno(LLAPI_MSG_ERROR,
3773 "NULL archive numbers");
3777 ct = calloc(1, sizeof(*ct));
3781 ct->fsname = malloc(strlen(fsname) + 1);
3782 if (ct->fsname == NULL) {
3786 strcpy(ct->fsname, fsname);
3787 ct->magic = CT_PRIV_MAGIC;
3789 for (rc = 0; rc < archive_count; rc++) {
3790 if (archives[rc] > sizeof(ct->archives)) {
3791 llapi_err_noerrno(LLAPI_MSG_ERROR,
3792 "Maximum of %d archives supported",
3793 sizeof(ct->archives));
3796 ct->archives |= 1 << archives[rc];
3798 /* special case: if no archives specified, default to archive #0. */
3799 if (ct->archives == 0)
3802 rc = libcfs_ukuc_start(&ct->kuc, KUC_GRP_HSM);
3806 /* Storing archive(s) in lk_data; see mdc_ioc_hsm_ct_start */
3807 ct->kuc.lk_data = ct->archives;
3808 rc = root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL,
3810 /* Only the kernel reference keeps the write side open */
3811 close(ct->kuc.lk_wfd);
3826 /** Deregister a copytool */
3827 int llapi_copytool_fini(void **priv)
3829 struct copytool_private *ct = (struct copytool_private *)*priv;
3831 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3834 /* Tell the kernel to stop sending us messages */
3835 ct->kuc.lk_flags = LK_FLG_STOP;
3836 root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL, 0);
3838 /* Shut down the kernelcomms */
3839 libcfs_ukuc_stop(&ct->kuc);
3847 /** Wait for the next hsm_action_list
3848 * @param priv Opaque private control structure
3849 * @param halh Action list handle, will be allocated here
3850 * @param msgsize Number of bytes in the message, will be set here
3851 * @return 0 valid message received; halh and msgsize are set
3854 int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
3856 struct copytool_private *ct = (struct copytool_private *)priv;
3857 struct kuc_hdr *kuch;
3858 struct hsm_action_list *hal;
3861 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3863 if (halh == NULL || msgsize == NULL)
3866 kuch = malloc(HAL_MAXSIZE + sizeof(*kuch));
3870 rc = libcfs_ukuc_msg_get(&ct->kuc, (char *)kuch,
3871 HAL_MAXSIZE + sizeof(*kuch),
3876 /* Handle generic messages */
3877 if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
3878 kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
3883 if (kuch->kuc_transport != KUC_TRANSPORT_HSM ||
3884 kuch->kuc_msgtype != HMT_ACTION_LIST) {
3885 llapi_err_noerrno(LLAPI_MSG_ERROR,
3886 "Unknown HSM message type %d:%d\n",
3887 kuch->kuc_transport, kuch->kuc_msgtype);
3892 /* Our message is a hsm_action_list. Use pointer math to skip
3893 * kuch_hdr and point directly to the message payload.
3895 hal = (struct hsm_action_list *)(kuch + 1);
3897 /* Check that we have registered for this archive # */
3898 if (((1 << hal->hal_archive_num) & ct->archives) == 0) {
3899 llapi_err_noerrno(LLAPI_MSG_INFO,
3900 "Ignoring request for archive #%d (bitmask %#x)\n",
3901 hal->hal_archive_num, ct->archives);
3907 *msgsize = kuch->kuc_msglen - sizeof(*kuch);
3917 /** Release the action list when done with it. */
3918 int llapi_copytool_free(struct hsm_action_list **hal)
3920 /* Reuse the llapi_changelog_free function */
3921 return llapi_changelog_free((struct changelog_rec **)hal);
3924 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
3929 root = opendir(mnt);
3932 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3936 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
3939 llapi_error(LLAPI_MSG_ERROR, rc,
3940 "ioctl on %s for getting connect flags failed", mnt);
3946 int llapi_get_version(char *buffer, int buffer_size,
3951 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
3953 fd = open(OBD_DEV_PATH, O_RDONLY);
3957 memset(buffer, 0, buffer_size);
3958 data->ioc_version = OBD_IOCTL_VERSION;
3959 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
3960 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
3961 data->ioc_len = obd_ioctl_packlen(data);
3963 rc = ioctl(fd, OBD_GET_VERSION, buffer);
3970 *version = data->ioc_bulk;
3975 * Get a 64-bit value representing the version of file data pointed by fd.
3977 * Each write or truncate, flushed on OST, will change this value. You can use
3978 * this value to verify if file data was modified. This only checks the file
3979 * data, not metadata.
3981 * \param flags If set to LL_DV_NOFLUSH, the data version will be read
3982 * directly from OST without regard to possible dirty cache on
3985 * \retval 0 on success.
3986 * \retval -errno on error.
3988 int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
3991 struct ioc_data_version idv;
3993 idv.idv_flags = flags;
3995 rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
3999 *data_version = idv.idv_version;