1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/utils/liblustreapi.c
41 * Author: Peter J. Braam <braam@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Robert Read <rread@clusterfs.com>
55 #include <sys/ioctl.h>
62 #include <sys/types.h>
63 #include <sys/syscall.h>
64 #include <sys/xattr.h>
67 #ifdef HAVE_LINUX_UNISTD_H
68 #include <linux/unistd.h>
74 #include <liblustre.h>
75 #include <lnet/lnetctl.h>
77 #include <lustre_lib.h>
79 #include <lustre/liblustreapi.h>
81 static unsigned llapi_dir_filetype_table[] = {
90 #if defined(DT_DOOR) && defined(S_IFDOOR)
95 #if defined(DT_DOOR) && defined(S_IFDOOR)
96 static const int DT_MAX = DT_DOOR;
98 static const int DT_MAX = DT_SOCK;
101 static unsigned llapi_filetype_dir_table[] = {
110 #if defined(DT_DOOR) && defined(S_IFDOOR)
115 #if defined(DT_DOOR) && defined(S_IFDOOR)
116 static const int S_IFMAX = DT_DOOR;
118 static const int S_IFMAX = DT_SOCK;
121 /* liblustreapi message level */
122 static int llapi_msg_level = LLAPI_MSG_MAX;
124 void llapi_msg_set_level(int level)
126 /* ensure level is in the good range */
127 if (level < LLAPI_MSG_OFF)
128 llapi_msg_level = LLAPI_MSG_OFF;
129 else if (level > LLAPI_MSG_MAX)
130 llapi_msg_level = LLAPI_MSG_MAX;
132 llapi_msg_level = level;
135 /* llapi_error will preserve errno */
136 void llapi_error(int level, int _rc, char *fmt, ...)
139 int tmp_errno = errno;
140 /* to protect using errno as _rc argument */
143 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
147 vfprintf(stderr, fmt, args);
150 if (level & LLAPI_MSG_NO_ERRNO)
151 fprintf(stderr, "\n");
153 fprintf(stderr, ": %s (%d)\n", strerror(rc), rc);
157 /* llapi_printf will preserve errno */
158 void llapi_printf(int level, char *fmt, ...)
161 int tmp_errno = errno;
163 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
167 vfprintf(stdout, fmt, args);
173 * size_units is to be initialized (or zeroed) by caller.
175 int parse_size(char *optarg, unsigned long long *size,
176 unsigned long long *size_units, int bytes_spec)
180 if (strncmp(optarg, "-", 1) == 0)
183 if (*size_units == 0)
186 *size = strtoull(optarg, &end, 0);
189 if ((*end == 'b') && *(end+1) == '\0' &&
190 (*size & (~0ULL << (64 - 9))) == 0 &&
192 *size_units = 1 << 9;
193 } else if ((*end == 'b') && *(end+1) == '\0' &&
196 } else if ((*end == 'k' || *end == 'K') &&
197 *(end+1) == '\0' && (*size &
198 (~0ULL << (64 - 10))) == 0) {
199 *size_units = 1 << 10;
200 } else if ((*end == 'm' || *end == 'M') &&
201 *(end+1) == '\0' && (*size &
202 (~0ULL << (64 - 20))) == 0) {
203 *size_units = 1 << 20;
204 } else if ((*end == 'g' || *end == 'G') &&
205 *(end+1) == '\0' && (*size &
206 (~0ULL << (64 - 30))) == 0) {
207 *size_units = 1 << 30;
208 } else if ((*end == 't' || *end == 'T') &&
209 *(end+1) == '\0' && (*size &
210 (~0ULL << (64 - 40))) == 0) {
211 *size_units = 1ULL << 40;
212 } else if ((*end == 'p' || *end == 'P') &&
213 *(end+1) == '\0' && (*size &
214 (~0ULL << (64 - 50))) == 0) {
215 *size_units = 1ULL << 50;
216 } else if ((*end == 'e' || *end == 'E') &&
217 *(end+1) == '\0' && (*size &
218 (~0ULL << (64 - 60))) == 0) {
219 *size_units = 1ULL << 60;
224 *size *= *size_units;
228 /* XXX: llapi_xxx() functions return negative values upon failure */
230 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
231 int stripe_count, int stripe_pattern)
235 /* 64 KB is the largest common page size I'm aware of (on ia64), but
236 * check the local page size just in case. */
237 page_size = LOV_MIN_STRIPE_SIZE;
238 if (getpagesize() > page_size) {
239 page_size = getpagesize();
240 llapi_err_noerrno(LLAPI_MSG_WARN,
241 "warning: your page size (%u) is "
242 "larger than expected (%u)", page_size,
243 LOV_MIN_STRIPE_SIZE);
245 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
247 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %lu, "
248 "must be an even multiple of %d bytes",
249 stripe_size, page_size);
252 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
254 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
258 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
260 llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
264 if (stripe_size >= (1ULL << 32)){
266 llapi_error(LLAPI_MSG_ERROR, rc,
267 "warning: stripe size larger than 4G "
268 "is not currently supported and would wrap");
274 /* return the first file matching this pattern */
275 static int first_match(char *pattern, char *buffer)
279 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
282 if (glob_info.gl_pathc < 1) {
283 globfree(&glob_info);
287 strcpy(buffer, glob_info.gl_pathv[0]);
289 globfree(&glob_info);
293 static int find_target_obdpath(char *fsname, char *path)
296 char pattern[PATH_MAX + 1];
299 snprintf(pattern, PATH_MAX,
300 "/proc/fs/lustre/lov/%s-*/target_obd",
302 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
303 if (rc == GLOB_NOMATCH)
308 strcpy(path, glob_info.gl_pathv[0]);
309 globfree(&glob_info);
313 static int find_poolpath(char *fsname, char *poolname, char *poolpath)
316 char pattern[PATH_MAX + 1];
319 snprintf(pattern, PATH_MAX,
320 "/proc/fs/lustre/lov/%s-*/pools/%s",
322 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
323 /* If no pools, make sure the lov is available */
324 if ((rc == GLOB_NOMATCH) &&
325 (find_target_obdpath(fsname, poolpath) == -ENODEV))
330 strcpy(poolpath, glob_info.gl_pathv[0]);
331 globfree(&glob_info);
336 * return a parameter string for a specific device type or mountpoint
338 * \param param_path the path to the file containing parameter data
339 * \param result buffer for parameter value string
340 * \param result_size size of buffer for return value
342 * The \param param_path is appended to /proc/{fs,sys}/{lnet,lustre} to
343 * complete the absolute path to the file containing the parameter data
344 * the user is requesting. If that file exist then the data is read from
345 * the file and placed into the \param result buffer that is passed by
346 * the user. Data is only copied up to the \param result_size to prevent
347 * overflow of the array.
349 * Return 0 for success, with a NUL-terminated string in \param result.
350 * Return -ve value for error.
352 static int get_param(const char *param_path, char *result,
353 unsigned int result_size)
355 char file[PATH_MAX + 1], pattern[PATH_MAX + 1], buf[result_size];
359 snprintf(pattern, PATH_MAX, "/proc/{fs,sys}/{lnet,lustre}/%s",
361 rc = first_match(pattern, file);
365 fp = fopen(file, "r");
367 while (fgets(buf, result_size, fp) != NULL)
376 #define DEVICES_LIST "/proc/fs/lustre/devices"
379 * return a parameter string for a specific device type or mountpoint
381 * \param fsname Lustre filesystem name (optional)
382 * \param file_path path to file in filesystem (optional, if fsname unset)
383 * \param obd_type Lustre OBD device type
384 * \param param_name parameter name to fetch
385 * \param value return buffer for parameter value string
386 * \param val_len size of buffer for return value
388 * If fsname is specified then the parameter will be from that filesystem
389 * (if it exists). If file_path is given and it is in a mounted Lustre
390 * filesystem, then the parameter will be otherwise the value may be
391 * from any mounted filesystem (if there is more than one).
393 * If "obd_type" matches a Lustre device then the first matching device
394 * (as with "lctl dl", constrained by \param fsname or \param mount_path)
395 * will be used to provide the return value, otherwise the first such
396 * device found will be used.
398 * Return 0 for success, with a NUL-terminated string in \param buffer.
399 * Return -ve value for error.
401 static int get_param_obdvar(const char *fsname, const char *file_path,
402 const char *obd_type, const char *param_name,
403 char *value, unsigned int val_len)
405 char devices[PATH_MAX + 1], dev[PATH_MAX + 1] = "*", fs[PATH_MAX + 1];
406 FILE *fp = fopen(DEVICES_LIST, "r");
409 if (!fsname && file_path) {
410 rc = llapi_search_fsname(file_path, fs);
412 llapi_error(LLAPI_MSG_ERROR, rc,
413 "'%s' is not on a Lustre filesystem",
423 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
427 while (fgets(devices, sizeof(devices), fp) != NULL) {
428 char *bufp = devices, *tmp;
430 while (bufp[0] == ' ')
433 tmp = strstr(bufp, obd_type);
435 tmp += strlen(obd_type) + 1;
439 tmp = strchr(dev, ' ');
445 if (dev[0] == '*' && strlen(fs))
446 snprintf(dev, PATH_MAX, "%s-*", fs);
447 snprintf(devices, PATH_MAX, "%s/%s/%s", obd_type, dev, param_name);
449 return get_param(devices, value, val_len);
452 static int get_mds_md_size(char *path)
454 int lumlen = lov_mds_md_size(LOV_MAX_STRIPE_COUNT, LOV_MAGIC_V3);
457 /* Now get the maxea from llite proc */
458 if (!get_param_obdvar(NULL, path, "llite", "max_easize",
465 * if pool is NULL, search ostname in target_obd
466 * if pool is not NULL:
467 * if pool not found returns errno < 0
468 * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty
469 * if ostname is not NULL, returns 1 if OST is in pool and 0 if not
471 int llapi_search_ost(char *fsname, char *poolname, char *ostname)
474 char buffer[PATH_MAX + 1];
478 len = strlen(ostname);
480 if (poolname == NULL)
481 rc = find_target_obdpath(fsname, buffer);
483 rc = find_poolpath(fsname, poolname, buffer);
487 fd = fopen(buffer, "r");
491 while (fgets(buffer, sizeof(buffer), fd) != NULL) {
492 if (poolname == NULL) {
494 /* Search for an ostname in the list of OSTs
495 Line format is IDX: fsname-OSTxxxx_UUID STATUS */
496 ptr = strchr(buffer, ' ');
498 (strncmp(ptr + 1, ostname, len) == 0)) {
503 /* Search for an ostname in a pool,
504 (or an existing non-empty pool if no ostname) */
505 if ((ostname == NULL) ||
506 (strncmp(buffer, ostname, len) == 0)) {
516 int llapi_file_open_pool(const char *name, int flags, int mode,
517 unsigned long long stripe_size, int stripe_offset,
518 int stripe_count, int stripe_pattern, char *pool_name)
520 struct lov_user_md_v3 lum = { 0 };
524 /* Make sure we have a good pool */
525 if (pool_name != NULL) {
526 char fsname[MAX_OBD_NAME + 1], *ptr;
528 rc = llapi_search_fsname(name, fsname);
530 llapi_error(LLAPI_MSG_ERROR, rc,
531 "'%s' is not on a Lustre filesystem",
536 /* in case user gives the full pool name <fsname>.<poolname>,
537 * strip the fsname */
538 ptr = strchr(pool_name, '.');
541 if (strcmp(pool_name, fsname) != 0) {
543 llapi_err_noerrno(LLAPI_MSG_ERROR,
544 "Pool '%s' is not on filesystem '%s'",
551 /* Make sure the pool exists and is non-empty */
552 rc = llapi_search_ost(fsname, pool_name, NULL);
554 llapi_err_noerrno(LLAPI_MSG_ERROR,
555 "pool '%s.%s' %s", fsname, pool_name,
556 rc == 0 ? "has no OSTs" : "does not exist");
561 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
562 if (fd < 0 && errno == EISDIR) {
563 fd = open(name, O_DIRECTORY | O_RDONLY);
569 llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
573 rc = llapi_stripe_limit_check(stripe_size, stripe_offset, stripe_count,
578 /* Initialize IOCTL striping pattern structure */
579 lum.lmm_magic = LOV_USER_MAGIC_V3;
580 lum.lmm_pattern = stripe_pattern;
581 lum.lmm_stripe_size = stripe_size;
582 lum.lmm_stripe_count = stripe_count;
583 lum.lmm_stripe_offset = stripe_offset;
584 if (pool_name != NULL) {
585 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
587 /* If no pool is specified at all, use V1 request */
588 lum.lmm_magic = LOV_USER_MAGIC_V1;
591 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
592 char *errmsg = "stripe already set";
594 if (errno != EEXIST && errno != EALREADY)
595 errmsg = strerror(errno);
597 llapi_err_noerrno(LLAPI_MSG_ERROR,
598 "error on ioctl "LPX64" for '%s' (%d): %s",
599 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
610 int llapi_file_open(const char *name, int flags, int mode,
611 unsigned long long stripe_size, int stripe_offset,
612 int stripe_count, int stripe_pattern)
614 return llapi_file_open_pool(name, flags, mode, stripe_size,
615 stripe_offset, stripe_count,
616 stripe_pattern, NULL);
619 int llapi_file_create(const char *name, unsigned long long stripe_size,
620 int stripe_offset, int stripe_count, int stripe_pattern)
624 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
625 stripe_offset, stripe_count, stripe_pattern,
634 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
635 int stripe_offset, int stripe_count,
636 int stripe_pattern, char *pool_name)
640 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
641 stripe_offset, stripe_count, stripe_pattern,
651 * Find the fsname, the full path, and/or an open fd.
652 * Either the fsname or path must not be NULL
654 #define WANT_PATH 0x1
655 #define WANT_FSNAME 0x2
657 #define WANT_INDEX 0x8
658 #define WANT_ERROR 0x10
659 static int get_root_path(int want, char *fsname, int *outfd, char *path,
663 char buf[PATH_MAX], mntdir[PATH_MAX];
666 int idx = 0, len = 0, mntlen, fd;
669 /* get the mount point */
670 fp = setmntent(MOUNTED, "r");
673 llapi_error(LLAPI_MSG_ERROR, rc,
674 "setmntent(%s) failed", MOUNTED);
678 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
681 if (!llapi_is_lustre_mnt(&mnt))
684 if ((want & WANT_INDEX) && (idx++ != index))
687 mntlen = strlen(mnt.mnt_dir);
688 ptr = strrchr(mnt.mnt_fsname, '/');
695 /* Check the fsname for a match, if given */
696 if (!(want & WANT_FSNAME) && fsname != NULL &&
697 (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0))
700 /* If the path isn't set return the first one we find */
701 if (path == NULL || strlen(path) == 0) {
702 strcpy(mntdir, mnt.mnt_dir);
703 if ((want & WANT_FSNAME) && fsname != NULL)
707 /* Otherwise find the longest matching path */
708 } else if ((strlen(path) >= mntlen) && (mntlen >= len) &&
709 (strncmp(mnt.mnt_dir, path, mntlen) == 0)) {
710 strcpy(mntdir, mnt.mnt_dir);
712 if ((want & WANT_FSNAME) && fsname != NULL)
721 if ((want & WANT_PATH) && path != NULL)
722 strcpy(path, mntdir);
723 if (want & WANT_FD) {
724 fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
727 llapi_error(LLAPI_MSG_ERROR, rc,
728 "error opening '%s'", mntdir);
734 } else if (want & WANT_ERROR)
735 llapi_err_noerrno(LLAPI_MSG_ERROR,
736 "can't find fs root for '%s': %d",
737 (want & WANT_PATH) ? fsname : path, rc);
742 * search lustre mounts
744 * Calling this function will return to the user the mount point, mntdir, and
745 * the file system name, fsname, if the user passed a buffer to this routine.
747 * The user inputs are pathname and index. If the pathname is supplied then
748 * the value of the index will be ignored. The pathname will return data if
749 * the pathname is located on a lustre mount. Index is used to pick which
750 * mount point you want in the case of multiple mounted lustre file systems.
751 * See function lfs_osts in lfs.c for a example of the index use.
753 int llapi_search_mounts(const char *pathname, int index, char *mntdir,
756 int want = WANT_PATH, idx = -1;
758 if (!pathname || pathname[0] == '\0') {
762 strcpy(mntdir, pathname);
766 return get_root_path(want, fsname, NULL, mntdir, idx);
769 /* Given a path, find the corresponding Lustre fsname */
770 int llapi_search_fsname(const char *pathname, char *fsname)
775 path = realpath(pathname, NULL);
777 char buf[PATH_MAX + 1], *ptr;
780 if (pathname[0] != '/') {
781 /* Need an absolute path, but realpath() only works for
782 * pathnames that actually exist. We go through the
783 * extra hurdle of dirname(getcwd() + pathname) in
784 * case the relative pathname contains ".." in it. */
785 if (getcwd(buf, sizeof(buf) - 1) == NULL)
789 strncat(buf, pathname, sizeof(buf) - strlen(buf));
790 path = realpath(buf, NULL);
792 ptr = strrchr(buf, '/');
796 path = realpath(buf, NULL);
799 llapi_error(LLAPI_MSG_ERROR, rc,
800 "pathname '%s' cannot expand",
806 rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
811 int llapi_getname(const char *path, char *buf, size_t size)
813 struct obd_uuid uuid_buf;
814 char *uuid = uuid_buf.uuid;
817 memset(&uuid_buf, 0, sizeof(uuid_buf));
818 rc = llapi_file_get_lov_uuid(path, &uuid_buf);
822 /* We want to turn lustre-clilov-ffff88002738bc00 into
823 * lustre-ffff88002738bc00. */
825 nr = snprintf(buf, size, "%.*s-%s",
826 (int) (strlen(uuid) - 24), uuid,
827 uuid + strlen(uuid) - 16);
837 * find the pool directory path under /proc
838 * (can be also used to test if a fsname is known)
840 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
843 char pattern[PATH_MAX + 1];
844 char buffer[PATH_MAX];
846 if (fsname == NULL) {
847 rc = llapi_search_fsname(pathname, buffer);
851 strcpy(pathname, fsname);
854 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
855 rc = first_match(pattern, buffer);
859 /* in fsname test mode, pool_pathname is NULL */
860 if (pool_pathname != NULL)
861 strcpy(pool_pathname, buffer);
867 * Get the list of pool members.
868 * \param poolname string of format \<fsname\>.\<poolname\>
869 * \param members caller-allocated array of char*
870 * \param list_size size of the members array
871 * \param buffer caller-allocated buffer for storing OST names
872 * \param buffer_size size of the buffer
874 * \return number of members retrieved for this pool
875 * \retval -error failure
877 int llapi_get_poolmembers(const char *poolname, char **members,
878 int list_size, char *buffer, int buffer_size)
880 char fsname[PATH_MAX + 1];
882 char pathname[PATH_MAX + 1];
883 char path[PATH_MAX + 1];
890 /* name is FSNAME.POOLNAME */
891 if (strlen(poolname) > PATH_MAX)
893 strcpy(fsname, poolname);
894 pool = strchr(fsname, '.');
901 rc = poolpath(fsname, NULL, pathname);
903 llapi_error(LLAPI_MSG_ERROR, rc,
904 "Lustre filesystem '%s' not found",
909 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
910 sprintf(path, "%s/%s", pathname, pool);
911 fd = fopen(path, "r");
914 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
919 while (fgets(buf, sizeof(buf), fd) != NULL) {
920 if (nb_entries >= list_size) {
925 tmp = strchr(buf, '\n');
928 if (used + strlen(buf) + 1 > buffer_size) {
933 strcpy(buffer + used, buf);
934 members[nb_entries] = buffer + used;
935 used += strlen(buf) + 1;
945 * Get the list of pools in a filesystem.
946 * \param name filesystem name or path
947 * \param poollist caller-allocated array of char*
948 * \param list_size size of the poollist array
949 * \param buffer caller-allocated buffer for storing pool names
950 * \param buffer_size size of the buffer
952 * \return number of pools retrieved for this filesystem
953 * \retval -error failure
955 int llapi_get_poollist(const char *name, char **poollist, int list_size,
956 char *buffer, int buffer_size)
958 char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
962 struct dirent *cookie = NULL;
964 unsigned int nb_entries = 0;
965 unsigned int used = 0;
968 /* initilize output array */
969 for (i = 0; i < list_size; i++)
972 /* is name a pathname ? */
973 ptr = strchr(name, '/');
975 /* only absolute pathname is supported */
979 if (!realpath(name, rname)) {
981 llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
986 rc = poolpath(NULL, rname, pathname);
988 llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
989 " a Lustre filesystem", name);
992 strcpy(fsname, rname);
995 strcpy(fsname, name);
996 rc = poolpath(fsname, NULL, pathname);
999 llapi_error(LLAPI_MSG_ERROR, rc,
1000 "Lustre filesystem '%s' not found", name);
1004 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
1005 dir = opendir(pathname);
1008 llapi_error(LLAPI_MSG_ERROR, rc,
1009 "Could not open pool list for '%s'",
1015 rc = readdir_r(dir, &pool, &cookie);
1019 llapi_error(LLAPI_MSG_ERROR, rc,
1020 "Error reading pool list for '%s'", name);
1022 } else if ((rc == 0) && (cookie == NULL)) {
1023 /* end of directory */
1027 /* ignore . and .. */
1028 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
1031 /* check output bounds */
1032 if (nb_entries >= list_size)
1035 /* +2 for '.' and final '\0' */
1036 if (used + strlen(pool.d_name) + strlen(fsname) + 2
1040 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
1041 poollist[nb_entries] = buffer + used;
1042 used += strlen(pool.d_name) + strlen(fsname) + 2;
1050 /* wrapper for lfs.c and obd.c */
1051 int llapi_poollist(const char *name)
1053 /* list of pool names (assume that pool count is smaller
1055 char **list, *buffer = NULL, *path = NULL, *fsname = NULL;
1056 int obdcount, bufsize, rc, nb, i;
1057 char *poolname = NULL, *tmp = NULL, data[16];
1059 if (name[0] != '/') {
1060 fsname = strdup(name);
1061 poolname = strchr(fsname, '.');
1065 path = (char *) name;
1068 rc = get_param_obdvar(fsname, path, "lov", "numobd",
1069 data, sizeof(data));
1072 obdcount = atoi(data);
1074 /* Allocate space for each fsname-OST0000_UUID, 1 per OST,
1075 * and also an array to store the pointers for all that
1076 * allocated space. */
1078 bufsize = sizeof(struct obd_uuid) * obdcount;
1079 buffer = realloc(tmp, bufsize + sizeof(*list) * obdcount);
1080 if (buffer == NULL) {
1084 list = (char **) (buffer + bufsize);
1087 /* name is a path or fsname */
1088 nb = llapi_get_poollist(name, list, obdcount,
1091 /* name is a pool name (<fsname>.<poolname>) */
1092 nb = llapi_get_poolmembers(name, list, obdcount,
1096 if (nb == -EOVERFLOW) {
1099 goto retry_get_pools;
1102 for (i = 0; i < nb; i++)
1103 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]);
1104 rc = (nb < 0 ? nb : 0);
1113 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
1114 void *data, cfs_dirent_t *de);
1116 #define OBD_NOT_FOUND (-1)
1118 static int common_param_init(struct find_param *param, char *path)
1120 param->lumlen = get_mds_md_size(path);
1121 param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
1122 if (param->lmd == NULL) {
1123 llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
1124 "error: allocation of %d bytes for ioctl",
1125 sizeof(lstat_t) + param->lumlen);
1129 param->got_uuids = 0;
1130 param->obdindexes = NULL;
1131 param->obdindex = OBD_NOT_FOUND;
1135 static void find_param_fini(struct find_param *param)
1137 if (param->obdindexes)
1138 free(param->obdindexes);
1144 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1147 struct find_param *param = (struct find_param *)data;
1152 /* set errno upon failure */
1153 static DIR *opendir_parent(char *path)
1159 fname = strrchr(path, '/');
1161 return opendir(".");
1165 parent = opendir(path);
1170 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
1171 struct lov_user_mds_data *lmd, int lumlen)
1173 lstat_t *st = &lmd->lmd_st;
1176 if (parent == NULL && dir == NULL)
1180 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO, (void *)lmd);
1181 } else if (parent) {
1182 char *fname = strrchr(path, '/');
1184 fname = (fname == NULL ? path : fname + 1);
1185 /* retrieve needed file info */
1186 strncpy((char *)lmd, fname, lumlen);
1187 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1193 if (errno == ENOTTY) {
1194 /* ioctl is not supported, it is not a lustre fs.
1195 * Do the regular lstat(2) instead. */
1196 ret = lstat_f(path, st);
1199 llapi_error(LLAPI_MSG_ERROR, ret,
1200 "error: %s: lstat failed for %s",
1203 } else if (errno == ENOENT) {
1205 llapi_error(LLAPI_MSG_WARN, ret,
1206 "warning: %s: %s does not exist",
1208 } else if (errno != EISDIR) {
1210 llapi_error(LLAPI_MSG_ERROR, ret,
1211 "%s ioctl failed for %s.",
1212 dir ? "LL_IOC_MDC_GETINFO" :
1213 "IOC_MDC_GETFILEINFO", path);
1216 llapi_error(LLAPI_MSG_ERROR, ret,
1217 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1224 int llapi_mds_getfileinfo(char *path, DIR *parent,
1225 struct lov_user_mds_data *lmd)
1227 int lumlen = get_mds_md_size(path);
1229 return get_lmd_info(path, parent, NULL, lmd, lumlen);
1232 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1233 semantic_func_t sem_init,
1234 semantic_func_t sem_fini, void *data,
1245 if (!d && errno != ENOTDIR) {
1247 llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
1250 } else if (!d && !parent) {
1251 /* ENOTDIR. Open the parent dir. */
1252 p = opendir_parent(path);
1254 GOTO(out, ret = -errno);
1257 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1263 while ((dent = readdir64(d)) != NULL) {
1264 ((struct find_param *)data)->have_fileinfo = 0;
1266 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1269 /* Don't traverse .lustre directory */
1270 if (!(strcmp(dent->d_name, dot_lustre_name)))
1274 if ((len + dent->d_reclen + 2) > size) {
1275 llapi_err_noerrno(LLAPI_MSG_ERROR,
1276 "error: %s: string buffer is too small",
1281 strcat(path, dent->d_name);
1283 if (dent->d_type == DT_UNKNOWN) {
1284 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1286 ret = llapi_mds_getfileinfo(path, d,
1287 ((struct find_param *)data)->lmd);
1289 ((struct find_param *)data)->have_fileinfo = 1;
1291 llapi_filetype_dir_table[st->st_mode &
1298 switch (dent->d_type) {
1300 llapi_err_noerrno(LLAPI_MSG_ERROR,
1301 "error: %s: '%s' is UNKNOWN type %d",
1302 __func__, dent->d_name, dent->d_type);
1305 ret = llapi_semantic_traverse(path, size, d, sem_init,
1306 sem_fini, data, dent);
1313 ret = sem_init(path, d, NULL, data, dent);
1317 if (sem_fini && ret == 0)
1318 sem_fini(path, d, NULL, data, dent);
1326 sem_fini(path, parent, d, data, de);
1335 static int param_callback(char *path, semantic_func_t sem_init,
1336 semantic_func_t sem_fini, struct find_param *param)
1338 int ret, len = strlen(path);
1341 if (len > PATH_MAX) {
1343 llapi_error(LLAPI_MSG_ERROR, ret,
1344 "Path name '%s' is too long", path);
1348 buf = (char *)malloc(PATH_MAX + 1);
1352 strncpy(buf, path, PATH_MAX + 1);
1353 ret = common_param_init(param, buf);
1358 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init,
1359 sem_fini, param, NULL);
1361 find_param_fini(param);
1363 return ret < 0 ? ret : 0;
1366 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
1368 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
1371 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
1376 int llapi_file_fget_lmv_uuid(int fd, struct obd_uuid *lov_name)
1378 int rc = ioctl(fd, OBD_IOC_GETMDNAME, lov_name);
1381 llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lmv name.");
1386 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
1390 fd = open(path, O_RDONLY);
1393 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
1397 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
1408 * If uuidp is NULL, return the number of available obd uuids.
1409 * If uuidp is non-NULL, then it will return the uuids of the obds. If
1410 * there are more OSTs then allocated to uuidp, then an error is returned with
1411 * the ost_count set to number of available obd uuids.
1413 static int llapi_get_target_uuids(int fd, struct obd_uuid *uuidp,
1414 int *ost_count, enum tgt_type type)
1416 struct obd_uuid name;
1419 int rc = 0, index = 0;
1421 /* Get the lov name */
1422 if (type == LOV_TYPE) {
1423 rc = llapi_file_fget_lov_uuid(fd, &name);
1427 rc = llapi_file_fget_lmv_uuid(fd, &name);
1432 /* Now get the ost uuids from /proc */
1433 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1434 type == LOV_TYPE ? "lov" : "lmv", name.uuid);
1435 fp = fopen(buf, "r");
1438 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1442 while (fgets(buf, sizeof(buf), fp) != NULL) {
1443 if (uuidp && (index < *ost_count)) {
1444 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
1452 if (uuidp && (index > *ost_count))
1459 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
1461 return llapi_get_target_uuids(fd, uuidp, ost_count, LOV_TYPE);
1464 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
1469 root = opendir(mnt);
1472 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
1477 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
1485 /* Check if user specified value matches a real uuid. Ignore _UUID,
1486 * -osc-4ba41334, other trailing gunk in comparison.
1487 * @param real_uuid ends in "_UUID"
1488 * @param search_uuid may or may not end in "_UUID"
1490 int llapi_uuid_match(char *real_uuid, char *search_uuid)
1492 int cmplen = strlen(real_uuid);
1493 int searchlen = strlen(search_uuid);
1495 if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0)
1497 if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0)
1500 /* The UUIDs may legitimately be different lengths, if
1501 * the system was upgraded from an older version. */
1502 if (cmplen != searchlen)
1505 return (strncmp(search_uuid, real_uuid, cmplen) == 0);
1508 /* Here, param->obduuid points to a single obduuid, the index of which is
1509 * returned in param->obdindex */
1510 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
1512 struct obd_uuid obd_uuid;
1513 char uuid[sizeof(struct obd_uuid)];
1518 if (param->got_uuids)
1521 /* Get the lov/lmv name */
1523 rc = llapi_file_fget_lmv_uuid(dirfd(dir), &obd_uuid);
1525 rc = llapi_file_fget_lov_uuid(dirfd(dir), &obd_uuid);
1527 if (rc != -ENOTTY) {
1528 llapi_error(LLAPI_MSG_ERROR, rc,
1529 "error: can't get lov name: %s", dname);
1536 param->got_uuids = 1;
1538 /* Now get the ost uuids from /proc */
1539 snprintf(buf, sizeof(buf), "/proc/fs/lustre/%s/%s/target_obd",
1540 param->get_lmv ? "lmv" : "lov", obd_uuid.uuid);
1541 fp = fopen(buf, "r");
1544 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
1548 if (!param->obduuid && !param->quiet && !param->obds_printed)
1549 llapi_printf(LLAPI_MSG_NORMAL, "%s:\n",
1550 param->get_lmv ? "MDTS" : "OBDS:");
1552 while (fgets(buf, sizeof(buf), fp) != NULL) {
1553 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
1556 if (param->obduuid) {
1557 if (llapi_uuid_match(uuid, param->obduuid->uuid)) {
1558 param->obdindex = index;
1561 } else if (!param->quiet && !param->obds_printed) {
1562 /* Print everything */
1563 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
1566 param->obds_printed = 1;
1570 if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) {
1571 llapi_err_noerrno(LLAPI_MSG_ERROR,
1572 "error: %s: unknown obduuid: %s",
1573 __func__, param->obduuid->uuid);
1580 /* In this case, param->obduuid will be an array of obduuids and
1581 * obd index for all these obduuids will be returned in
1582 * param->obdindexes */
1583 static int setup_indexes(DIR *dir, char *path, struct obd_uuid *obduuids,
1584 int num_obds, int **obdindexes, int *obdindex,
1587 int ret, obdcount, obd_valid = 0, obdnum, i;
1588 struct obd_uuid *uuids = NULL;
1592 ret = get_param_obdvar(NULL, path, type == LOV_TYPE ? "lov" : "lmv",
1593 "numobd", buf, sizeof(buf));
1597 obdcount = atoi(buf);
1598 uuids = (struct obd_uuid *)malloc(obdcount *
1599 sizeof(struct obd_uuid));
1604 ret = llapi_get_target_uuids(dirfd(dir), uuids, &obdcount, type);
1606 struct obd_uuid *uuids_temp;
1608 if (ret == -EOVERFLOW) {
1609 uuids_temp = realloc(uuids, obdcount *
1610 sizeof(struct obd_uuid));
1611 if (uuids_temp != NULL)
1612 goto retry_get_uuids;
1617 llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
1621 indexes = malloc(num_obds * sizeof(*obdindex));
1622 if (indexes == NULL) {
1627 for (obdnum = 0; obdnum < num_obds; obdnum++) {
1628 for (i = 0; i < obdcount; i++) {
1629 if (llapi_uuid_match(uuids[i].uuid,
1630 obduuids[obdnum].uuid)) {
1631 indexes[obdnum] = i;
1636 if (i >= obdcount) {
1637 indexes[obdnum] = OBD_NOT_FOUND;
1638 llapi_err_noerrno(LLAPI_MSG_ERROR,
1639 "error: %s: unknown obduuid: %s",
1640 __func__, obduuids[obdnum].uuid);
1646 *obdindex = OBD_NOT_FOUND;
1648 *obdindex = obd_valid;
1650 *obdindexes = indexes;
1658 static int setup_target_indexes(DIR *dir, char *path, struct find_param *param)
1662 if (param->mdtuuid) {
1663 ret = setup_indexes(dir, path, param->mdtuuid, param->num_mdts,
1664 ¶m->mdtindexes, ¶m->mdtindex, LMV_TYPE);
1668 if (param->obduuid) {
1669 ret = setup_indexes(dir, path, param->obduuid, param->num_obds,
1670 ¶m->obdindexes, ¶m->obdindex, LOV_TYPE);
1674 param->got_uuids = 1;
1678 int llapi_ostlist(char *path, struct find_param *param)
1683 dir = opendir(path);
1687 ret = setup_obd_uuid(dir, path, param);
1694 * Given a filesystem name, or a pathname of a file on a lustre filesystem,
1695 * tries to determine the path to the filesystem's clilov directory under /proc
1697 * fsname is limited to MTI_NAME_MAXLEN in lustre_idl.h
1698 * The NUL terminator is compensated by the additional "%s" bytes. */
1699 #define LOV_LEN (sizeof("/proc/fs/lustre/lov/%s-clilov-*") + MTI_NAME_MAXLEN)
1700 static int clilovpath(const char *fsname, const char *const pathname,
1704 char pattern[LOV_LEN];
1705 char buffer[PATH_MAX + 1];
1707 if (fsname == NULL) {
1708 rc = llapi_search_fsname(pathname, buffer);
1714 snprintf(pattern, sizeof(pattern), "/proc/fs/lustre/lov/%s-clilov-*",
1717 rc = first_match(pattern, buffer);
1721 strncpy(clilovpath, buffer, sizeof(buffer));
1727 * Given the path to a stripe attribute proc file, tries to open and
1728 * read the attribute and return the value using the attr parameter
1730 static int sattr_read_attr(const char *const fpath,
1735 char line[PATH_MAX + 1];
1738 f = fopen(fpath, "r");
1741 llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open '%s'", fpath);
1745 if (fgets(line, sizeof(line), f) != NULL) {
1748 llapi_error(LLAPI_MSG_ERROR, errno, "Cannot read from '%s'", fpath);
1757 * Tries to determine the default stripe attributes for a given filesystem. The
1758 * filesystem to check should be specified by fsname, or will be determined
1761 static int sattr_get_defaults(const char *const fsname,
1762 const char *const pathname,
1763 unsigned int *scount,
1764 unsigned int *ssize,
1765 unsigned int *soffset)
1768 char dpath[PATH_MAX + 1];
1769 char fpath[PATH_MAX + 1];
1771 rc = clilovpath(fsname, pathname, dpath);
1776 snprintf(fpath, PATH_MAX, "%s/stripecount", dpath);
1777 rc = sattr_read_attr(fpath, scount);
1783 snprintf(fpath, PATH_MAX, "%s/stripesize", dpath);
1784 rc = sattr_read_attr(fpath, ssize);
1790 snprintf(fpath, PATH_MAX, "%s/stripeoffset", dpath);
1791 rc = sattr_read_attr(fpath, soffset);
1800 * Tries to gather the default stripe attributes for a given filesystem. If
1801 * the attributes can be determined, they are cached for easy retreival the
1802 * next time they are needed. Only a single filesystem's attributes are
1805 static int sattr_cache_get_defaults(const char *const fsname,
1806 const char *const pathname,
1807 unsigned int *scount,
1808 unsigned int *ssize,
1809 unsigned int *soffset)
1812 char fsname[PATH_MAX + 1];
1813 unsigned int stripecount;
1814 unsigned int stripesize;
1815 unsigned int stripeoffset;
1821 char fsname_buf[PATH_MAX + 1];
1822 unsigned int tmp[3];
1824 if (fsname == NULL) {
1825 rc = llapi_search_fsname(pathname, fsname_buf);
1829 strncpy(fsname_buf, fsname, PATH_MAX);
1832 if (strncmp(fsname_buf, cache.fsname, PATH_MAX) != 0) {
1834 * Ensure all 3 sattrs (count, size, and offset) are
1835 * successfully retrieved and stored in tmp before writing to
1838 rc = sattr_get_defaults(fsname_buf, NULL, &tmp[0], &tmp[1],
1843 cache.stripecount = tmp[0];
1844 cache.stripesize = tmp[1];
1845 cache.stripeoffset = tmp[2];
1846 strncpy(cache.fsname, fsname_buf, PATH_MAX);
1850 *scount = cache.stripecount;
1852 *ssize = cache.stripesize;
1854 *soffset = cache.stripeoffset;
1859 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
1860 struct lov_user_ost_data_v1 *objects,
1861 int is_dir, int verbose, int depth,
1862 int raw, char *pool_name)
1864 char *prefix = is_dir ? "" : "lmm_";
1865 char nl = is_dir ? ' ' : '\n';
1868 if (is_dir && lum->lmm_object_seq == FID_SEQ_LOV_DEFAULT) {
1869 lum->lmm_object_seq = FID_SEQ_OST_MDT0;
1870 if (verbose & VERBOSE_DETAIL)
1871 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
1874 if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir))
1875 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
1877 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1878 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
1880 llapi_printf(LLAPI_MSG_NORMAL, "lmm_seq: "LPX64"\n",
1881 lum->lmm_object_seq);
1882 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
1883 lum->lmm_object_id);
1886 if (verbose & VERBOSE_COUNT) {
1887 if (verbose & ~VERBOSE_COUNT)
1888 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
1891 if (!raw && lum->lmm_stripe_count == 0) {
1892 unsigned int scount;
1893 rc = sattr_cache_get_defaults(NULL, path,
1897 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1900 llapi_error(LLAPI_MSG_ERROR, rc,
1901 "Cannot determine default"
1904 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1905 lum->lmm_stripe_count ==
1906 (typeof(lum->lmm_stripe_count))(-1)
1907 ? -1 : lum->lmm_stripe_count, nl);
1910 llapi_printf(LLAPI_MSG_NORMAL, "%hd%c",
1911 (__s16)lum->lmm_stripe_count, nl);
1915 if (verbose & VERBOSE_SIZE) {
1916 if (verbose & ~VERBOSE_SIZE)
1917 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
1919 if (is_dir && !raw && lum->lmm_stripe_size == 0) {
1921 rc = sattr_cache_get_defaults(NULL, path, NULL, &ssize,
1924 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", ssize,
1927 llapi_error(LLAPI_MSG_ERROR, rc,
1928 "Cannot determine default"
1931 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1932 lum->lmm_stripe_size, nl);
1936 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
1937 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
1938 lum->lmm_pattern, nl);
1941 if ((verbose & VERBOSE_GENERATION) && !is_dir) {
1942 if (verbose & ~VERBOSE_GENERATION)
1943 llapi_printf(LLAPI_MSG_NORMAL, "%slayout_gen: ",
1945 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1946 (int)lum->u.lum_layout_gen, nl);
1949 if (verbose & VERBOSE_OFFSET) {
1950 if (verbose & ~VERBOSE_OFFSET)
1951 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
1954 llapi_printf(LLAPI_MSG_NORMAL, "%d%c",
1955 lum->lmm_stripe_offset ==
1956 (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
1957 lum->lmm_stripe_offset, nl);
1959 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
1960 objects[0].l_ost_idx, nl);
1963 if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) {
1964 if (verbose & ~VERBOSE_POOL)
1965 llapi_printf(LLAPI_MSG_NORMAL, "%spool: ",
1967 llapi_printf(LLAPI_MSG_NORMAL, "%s%c", pool_name, nl);
1970 if (is_dir && (verbose != VERBOSE_OBJID))
1971 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1974 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
1975 struct lov_user_ost_data_v1 *objects,
1976 char *path, int is_dir,
1977 int obdindex, int depth, int header, int raw)
1979 int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
1982 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
1983 if (obdindex == objects[i].l_ost_idx) {
1991 lov_dump_user_lmm_header(lum, path, objects, is_dir, header,
1992 depth, raw, pool_name);
1994 if (!is_dir && (header & VERBOSE_OBJID)) {
1996 llapi_printf(LLAPI_MSG_NORMAL,
1997 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
1999 for (i = 0; i < lum->lmm_stripe_count; i++) {
2000 int idx = objects[i].l_ost_idx;
2001 long long oid = objects[i].l_object_id;
2002 long long gr = objects[i].l_object_seq;
2003 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
2004 llapi_printf(LLAPI_MSG_NORMAL,
2005 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
2007 obdindex == idx ? " *" : "");
2009 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2013 void llapi_lov_dump_user_lmm(struct find_param *param,
2014 char *path, int is_dir)
2016 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
2017 case LOV_USER_MAGIC_V1:
2018 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
2019 param->lmd->lmd_lmm.lmm_objects,
2021 param->obdindex, param->maxdepth,
2022 param->verbose, param->raw);
2024 case LOV_USER_MAGIC_V3: {
2025 char pool_name[LOV_MAXPOOLNAME + 1];
2026 struct lov_user_ost_data_v1 *objects;
2027 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2029 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
2030 pool_name[LOV_MAXPOOLNAME] = '\0';
2031 objects = lmmv3->lmm_objects;
2032 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
2033 objects, path, is_dir,
2034 param->obdindex, param->maxdepth,
2035 param->verbose, param->raw);
2039 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
2040 "(expecting one of %#x %#x %#x)\n",
2041 *(__u32 *)¶m->lmd->lmd_lmm,
2042 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3);
2047 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
2053 fname = strrchr(path, '/');
2055 /* It should be a file (or other non-directory) */
2056 if (fname == NULL) {
2057 dname = (char *)malloc(2);
2061 fname = (char *)path;
2063 dname = (char *)malloc(fname - path + 1);
2066 strncpy(dname, path, fname - path);
2067 dname[fname - path] = '\0';
2071 fd = open(dname, O_RDONLY);
2078 strcpy((char *)lum, fname);
2079 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
2082 if (close(fd) == -1 && rc == 0)
2089 int llapi_file_lookup(int dirfd, const char *name)
2091 struct obd_ioctl_data data = { 0 };
2096 if (dirfd < 0 || name == NULL)
2099 data.ioc_version = OBD_IOCTL_VERSION;
2100 data.ioc_len = sizeof(data);
2101 data.ioc_inlbuf1 = (char *)name;
2102 data.ioc_inllen1 = strlen(name) + 1;
2104 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
2106 llapi_error(LLAPI_MSG_ERROR, rc,
2107 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
2112 rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
2118 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
2119 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
2121 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
2122 * The table below gives the answers for the specified parameters (value and
2123 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
2124 * --------------------------------------
2125 * 1 | file > limit; sign > 0 | -1 / -1 |
2126 * 2 | file = limit; sign > 0 | -1 / -1 |
2127 * 3 | file < limit; sign > 0 | ? / 1 |
2128 * 4 | file > limit; sign = 0 | -1 / -1 |
2129 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
2130 * 6 | file < limit; sign = 0 | ? / -1 |
2131 * 7 | file > limit; sign < 0 | 1 / 1 |
2132 * 8 | file = limit; sign < 0 | ? / -1 |
2133 * 9 | file < limit; sign < 0 | ? / -1 |
2134 * --------------------------------------
2135 * Note: 5th actually means that the value is within the interval
2136 * (limit - margin, limit]. */
2137 static int find_value_cmp(unsigned long long file, unsigned long long limit,
2138 int sign, int negopt, unsigned long long margin,
2144 /* Drop the fraction of margin (of days). */
2145 if (file + margin <= limit)
2147 } else if (sign == 0) {
2148 if (file <= limit && file + margin > limit)
2150 else if (file + margin <= limit)
2152 } else if (sign < 0) {
2159 return negopt ? ~ret + 1 : ret;
2162 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
2163 * Return -1 or 1 if file timestamp does not or does match the given criteria
2164 * correspondingly. Return 0 if the MDS time is being checked and there are
2165 * attributes on OSTs and it is not yet clear if the timespamp matches.
2167 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
2168 * updated timestamps. */
2169 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
2174 /* Check if file is accepted. */
2176 ret = find_value_cmp(st->st_atime, param->atime,
2177 param->asign, param->exclude_atime,
2185 ret = find_value_cmp(st->st_mtime, param->mtime,
2186 param->msign, param->exclude_mtime,
2191 /* If the previous check matches, but this one is not yet clear,
2192 * we should return 0 to do an RPC on OSTs. */
2198 ret = find_value_cmp(st->st_ctime, param->ctime,
2199 param->csign, param->exclude_ctime,
2204 /* If the previous check matches, but this one is not yet clear,
2205 * we should return 0 to do an RPC on OSTs. */
2214 * Check whether the stripes matches the indexes user provided
2218 static int check_obd_match(struct find_param *param)
2220 lstat_t *st = ¶m->lmd->lmd_st;
2221 struct lov_user_ost_data_v1 *lmm_objects;
2224 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
2227 if (!S_ISREG(st->st_mode))
2230 /* Only those files should be accepted, which have a
2231 * stripe on the specified OST. */
2232 if (!param->lmd->lmd_lmm.lmm_stripe_count)
2235 if (param->lmd->lmd_lmm.lmm_magic ==
2236 LOV_USER_MAGIC_V3) {
2237 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2239 lmm_objects = lmmv3->lmm_objects;
2240 } else if (param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) {
2241 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
2243 llapi_err_noerrno(LLAPI_MSG_ERROR, "%s:Unknown magic: 0x%08X\n",
2244 __func__, param->lmd->lmd_lmm.lmm_magic);
2248 for (i = 0; i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
2249 for (j = 0; j < param->num_obds; j++) {
2250 if (param->obdindexes[j] ==
2251 lmm_objects[i].l_ost_idx) {
2252 if (param->exclude_obd)
2259 if (param->exclude_obd)
2264 static int check_mdt_match(struct find_param *param)
2268 if (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND)
2271 /* FIXME: For striped dir, we should get stripe information and check */
2272 for (i = 0; i < param->num_mdts; i++) {
2273 if (param->mdtindexes[i] == param->file_mdtindex)
2274 if (param->exclude_mdt)
2279 if (param->exclude_mdt)
2285 * Check whether the obd is active or not, if it is
2286 * not active, just print the object affected by this
2289 static int print_failed_tgt(struct find_param *param, char *path, int type)
2291 struct obd_statfs stat_buf;
2292 struct obd_uuid uuid_buf;
2295 LASSERT(type == LL_STATFS_LOV || type == LL_STATFS_LMV);
2297 memset(&stat_buf, 0, sizeof(struct obd_statfs));
2298 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
2299 ret = llapi_obd_statfs(path, type,
2300 param->obdindex, &stat_buf,
2303 llapi_printf(LLAPI_MSG_NORMAL,
2304 "obd_uuid: %s failed %s ",
2305 param->obduuid->uuid,
2311 static int cb_find_init(char *path, DIR *parent, DIR *dir,
2312 void *data, cfs_dirent_t *de)
2314 struct find_param *param = (struct find_param *)data;
2315 int decision = 1; /* 1 is accepted; -1 is rejected. */
2316 lstat_t *st = ¶m->lmd->lmd_st;
2318 int checked_type = 0;
2321 LASSERT(parent != NULL || dir != NULL);
2323 if (param->have_fileinfo == 0)
2324 param->lmd->lmd_lmm.lmm_stripe_count = 0;
2326 /* If a regular expression is presented, make the initial decision */
2327 if (param->pattern != NULL) {
2328 char *fname = strrchr(path, '/');
2329 fname = (fname == NULL ? path : fname + 1);
2330 ret = fnmatch(param->pattern, fname, 0);
2331 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
2332 (ret == 0 && param->exclude_pattern))
2336 /* See if we can check the file type from the dirent. */
2337 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
2338 de->d_type < DT_MAX) {
2340 if (llapi_dir_filetype_table[de->d_type] == param->type) {
2341 if (param->exclude_type)
2344 if (!param->exclude_type)
2351 /* Request MDS for the stat info if some of these parameters need
2352 * to be compared. */
2353 if (param->obduuid || param->mdtuuid || param->check_uid ||
2354 param->check_gid || param->check_pool || param->atime ||
2355 param->ctime || param->mtime || param->check_size)
2358 if (param->type && checked_type == 0)
2361 if (param->have_fileinfo == 0 && decision == 0) {
2362 ret = get_lmd_info(path, parent, dir, param->lmd,
2366 ret = llapi_file_fget_mdtidx(dirfd(dir),
2367 ¶m->file_mdtindex);
2372 ret = lstat_f(path, &tmp_st);
2375 llapi_error(LLAPI_MSG_ERROR, ret,
2376 "error: %s: lstat failed"
2377 "for %s", __func__, path);
2380 if (S_ISREG(tmp_st.st_mode)) {
2381 fd = open(path, O_RDONLY);
2383 ret = llapi_file_fget_mdtidx(fd,
2384 ¶m->file_mdtindex);
2390 /* For special inode, it assumes to
2391 * reside on the same MDT with the
2394 ret = llapi_file_fget_mdtidx(fd,
2395 ¶m->file_mdtindex);
2408 if (param->type && !checked_type) {
2409 if ((st->st_mode & S_IFMT) == param->type) {
2410 if (param->exclude_type)
2413 if (!param->exclude_type)
2419 if (param->obduuid || param->mdtuuid) {
2420 if (lustre_fs && param->got_uuids &&
2421 param->st_dev != st->st_dev) {
2422 /* A lustre/lustre mount point is crossed. */
2423 param->got_uuids = 0;
2424 param->obds_printed = 0;
2425 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2428 if (lustre_fs && !param->got_uuids) {
2429 ret = setup_target_indexes(dir ? dir : parent, path,
2434 param->st_dev = st->st_dev;
2435 } else if (!lustre_fs && param->got_uuids) {
2436 /* A lustre/non-lustre mount point is crossed. */
2437 param->got_uuids = 0;
2438 param->obdindex = param->mdtindex = OBD_NOT_FOUND;
2442 if ((param->obduuid && param->obdindex == OBD_NOT_FOUND) ||
2443 (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND))
2446 /* If a OST or MDT UUID is given, and some OST matches,
2448 if (param->obdindex != OBD_NOT_FOUND ||
2449 param->mdtindex != OBD_NOT_FOUND) {
2450 if (param->obduuid) {
2451 if (check_obd_match(param)) {
2452 /* If no mdtuuid is given, we are done.
2453 * Otherwise, fall through to the mdtuuid
2455 if (!param->mdtuuid)
2461 if (param->mdtuuid) {
2462 if (check_mdt_match(param))
2468 if (param->check_uid) {
2469 if (st->st_uid == param->uid) {
2470 if (param->exclude_uid)
2473 if (!param->exclude_uid)
2478 if (param->check_gid) {
2479 if (st->st_gid == param->gid) {
2480 if (param->exclude_gid)
2483 if (!param->exclude_gid)
2488 if (param->check_pool) {
2489 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
2491 /* empty requested pool is taken as no pool search => V1 */
2492 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
2493 (param->poolname[0] == '\0')) ||
2494 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2495 (strncmp(lmmv3->lmm_pool_name,
2496 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
2497 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
2498 (strcmp(param->poolname, "*") == 0))) {
2499 if (param->exclude_pool)
2502 if (!param->exclude_pool)
2507 /* Check the time on mds. */
2509 if (param->atime || param->ctime || param->mtime) {
2512 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
2513 param->lmd->lmd_lmm.lmm_stripe_count)
2515 decision = find_time_check(st, param, for_mds);
2520 /* If file still fits the request, ask ost for updated info.
2521 The regular stat is almost of the same speed as some new
2522 'glimpse-size-ioctl'. */
2524 if (param->check_size && S_ISREG(st->st_mode) &&
2525 param->lmd->lmd_lmm.lmm_stripe_count)
2529 /* For regular files with the stripe the decision may have not
2530 * been taken yet if *time or size is to be checked. */
2531 LASSERT((S_ISREG(st->st_mode) &&
2532 param->lmd->lmd_lmm.lmm_stripe_count) ||
2533 param->mdtindex != OBD_NOT_FOUND);
2535 if (param->obdindex != OBD_NOT_FOUND)
2536 print_failed_tgt(param, path, LL_STATFS_LOV);
2538 if (param->mdtindex != OBD_NOT_FOUND)
2539 print_failed_tgt(param, path, LL_STATFS_LMV);
2542 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
2543 (void *)param->lmd);
2544 } else if (parent) {
2545 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
2546 (void *)param->lmd);
2550 if (errno == ENOENT) {
2551 llapi_error(LLAPI_MSG_ERROR, -ENOENT,
2552 "warning: %s: %s does not exist",
2557 llapi_error(LLAPI_MSG_ERROR, ret,
2558 "%s: IOC_LOV_GETINFO on %s failed",
2564 /* Check the time on osc. */
2565 decision = find_time_check(st, param, 0);
2572 if (param->check_size)
2573 decision = find_value_cmp(st->st_size, param->size,
2574 param->size_sign, param->exclude_size,
2575 param->size_units, 0);
2577 if (decision != -1) {
2578 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
2580 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
2582 llapi_printf(LLAPI_MSG_NORMAL, "\n");
2586 /* Do not get down anymore? */
2587 if (param->depth == param->maxdepth)
2594 int llapi_find(char *path, struct find_param *param)
2596 return param_callback(path, cb_find_init, cb_common_fini, param);
2600 * Get MDT number that the file/directory inode referenced
2601 * by the open fd resides on.
2602 * Return 0 and mdtidx on success, or -ve errno.
2604 int llapi_file_fget_mdtidx(int fd, int *mdtidx)
2606 if (ioctl(fd, LL_IOC_GET_MDTIDX, mdtidx) < 0)
2611 static int cb_get_mdt_index(char *path, DIR *parent, DIR *d, void *data,
2614 struct find_param *param = (struct find_param *)data;
2618 LASSERT(parent != NULL || d != NULL);
2621 ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx);
2622 } else if (parent) {
2625 fd = open(path, O_RDONLY);
2627 ret = llapi_file_fget_mdtidx(fd, &mdtidx);
2635 if (ret == -ENODATA) {
2636 if (!param->obduuid)
2637 llapi_printf(LLAPI_MSG_NORMAL,
2638 "%s has no stripe info\n", path);
2640 } else if (ret == -ENOENT) {
2641 llapi_error(LLAPI_MSG_WARN, ret,
2642 "warning: %s: %s does not exist",
2645 } else if (ret == -ENOTTY) {
2646 llapi_error(LLAPI_MSG_ERROR, ret,
2647 "%s: '%s' not on a Lustre fs?",
2650 llapi_error(LLAPI_MSG_ERROR, ret,
2651 "error: %s: LL_IOC_GET_MDTIDX failed for %s",
2657 if (param->quiet || !(param->verbose & VERBOSE_DETAIL))
2658 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
2660 llapi_printf(LLAPI_MSG_NORMAL, "%s MDT index: %d\n",
2664 /* Do not get down anymore? */
2665 if (param->depth == param->maxdepth)
2672 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
2675 struct find_param *param = (struct find_param *)data;
2678 LASSERT(parent != NULL || d != NULL);
2680 if (param->obduuid) {
2682 ret = setup_obd_uuid(d ? d : parent, path, param);
2688 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
2689 (void *)¶m->lmd->lmd_lmm);
2690 } else if (parent) {
2691 char *fname = strrchr(path, '/');
2692 fname = (fname == NULL ? path : fname + 1);
2694 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
2696 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
2697 (void *)¶m->lmd->lmd_lmm);
2701 if (errno == ENODATA && d != NULL) {
2702 /* We need to "fake" the "use the default" values
2703 * since the lmm struct is zeroed out at this point.
2704 * The magic needs to be set in order to satisfy
2705 * a check later on in the code path.
2706 * The object_seq needs to be set for the "(Default)"
2707 * prefix to be displayed. */
2708 struct lov_user_md *lmm = ¶m->lmd->lmd_lmm;
2709 lmm->lmm_magic = LOV_MAGIC_V1;
2711 lmm->lmm_object_seq = FID_SEQ_LOV_DEFAULT;
2712 lmm->lmm_stripe_count = 0;
2713 lmm->lmm_stripe_size = 0;
2714 lmm->lmm_stripe_offset = -1;
2717 } else if (errno == ENODATA && parent != NULL) {
2718 if (!param->obduuid)
2719 llapi_printf(LLAPI_MSG_NORMAL,
2720 "%s has no stripe info\n", path);
2722 } else if (errno == ENOENT) {
2723 llapi_error(LLAPI_MSG_WARN, -ENOENT,
2724 "warning: %s: %s does not exist",
2727 } else if (errno == ENOTTY) {
2729 llapi_error(LLAPI_MSG_ERROR, ret,
2730 "%s: '%s' not on a Lustre fs?",
2734 llapi_error(LLAPI_MSG_ERROR, ret,
2735 "error: %s: %s failed for %s",
2736 __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
2737 "IOC_MDC_GETFILESTRIPE", path);
2744 if (!param->get_mdt_index)
2745 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
2748 /* Do not get down anymore? */
2749 if (param->depth == param->maxdepth)
2756 int llapi_getstripe(char *path, struct find_param *param)
2758 return param_callback(path, param->get_mdt_index ?
2759 cb_get_mdt_index : cb_getstripe,
2760 cb_common_fini, param);
2763 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
2764 struct obd_statfs *stat_buf,
2765 struct obd_uuid *uuid_buf)
2768 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
2770 struct obd_ioctl_data data = { 0 };
2773 data.ioc_inlbuf1 = (char *)&type;
2774 data.ioc_inllen1 = sizeof(__u32);
2775 data.ioc_inlbuf2 = (char *)&index;
2776 data.ioc_inllen2 = sizeof(__u32);
2777 data.ioc_pbuf1 = (char *)stat_buf;
2778 data.ioc_plen1 = sizeof(struct obd_statfs);
2779 data.ioc_pbuf2 = (char *)uuid_buf;
2780 data.ioc_plen2 = sizeof(struct obd_uuid);
2782 rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
2784 llapi_error(LLAPI_MSG_ERROR, rc,
2785 "llapi_obd_statfs: error packing ioctl data");
2789 fd = open(path, O_RDONLY);
2790 if (errno == EISDIR)
2791 fd = open(path, O_DIRECTORY | O_RDONLY);
2794 rc = errno ? -errno : -EBADF;
2795 llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
2799 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
2801 rc = errno ? -errno : -EINVAL;
2807 #define MAX_STRING_SIZE 128
2809 int llapi_ping(char *obd_type, char *obd_name)
2811 char path[MAX_STRING_SIZE];
2815 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
2816 obd_type, obd_name);
2818 fd = open(path, O_WRONLY);
2821 llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
2825 rc = write(fd, buf, 1);
2835 int llapi_target_iterate(int type_num, char **obd_type,
2836 void *args, llapi_cb_t cb)
2838 char buf[MAX_STRING_SIZE];
2839 FILE *fp = fopen(DEVICES_LIST, "r");
2844 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
2848 while (fgets(buf, sizeof(buf), fp) != NULL) {
2849 char *obd_type_name = NULL;
2850 char *obd_name = NULL;
2851 char *obd_uuid = NULL;
2853 struct obd_statfs osfs_buffer;
2855 while(bufp[0] == ' ')
2858 for(i = 0; i < 3; i++) {
2859 obd_type_name = strsep(&bufp, " ");
2861 obd_name = strsep(&bufp, " ");
2862 obd_uuid = strsep(&bufp, " ");
2864 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
2866 for (i = 0; i < type_num; i++) {
2867 if (strcmp(obd_type_name, obd_type[i]) != 0)
2870 cb(obd_type_name, obd_name, obd_uuid, args);
2877 static void do_target_check(char *obd_type_name, char *obd_name,
2878 char *obd_uuid, void *args)
2882 rc = llapi_ping(obd_type_name, obd_name);
2883 if (rc == ENOTCONN) {
2884 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
2886 llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
2888 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
2892 int llapi_target_check(int type_num, char **obd_type, char *dir)
2894 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
2897 #undef MAX_STRING_SIZE
2899 int llapi_catinfo(char *dir, char *keyword, char *node_name)
2901 char raw[OBD_MAX_IOCTL_BUFFER];
2902 char out[LLOG_CHUNK_SIZE];
2904 struct obd_ioctl_data data = { 0 };
2909 sprintf(key, "%s", keyword);
2910 memset(raw, 0, sizeof(raw));
2911 memset(out, 0, sizeof(out));
2912 data.ioc_inlbuf1 = key;
2913 data.ioc_inllen1 = strlen(key) + 1;
2915 data.ioc_inlbuf2 = node_name;
2916 data.ioc_inllen2 = strlen(node_name) + 1;
2918 data.ioc_pbuf1 = out;
2919 data.ioc_plen1 = sizeof(out);
2920 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
2924 root = opendir(dir);
2927 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", dir);
2931 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
2934 llapi_error(LLAPI_MSG_ERROR, rc,
2935 "ioctl OBD_IOC_CATINFO failed");
2937 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
2944 /* Is this a lustre fs? */
2945 int llapi_is_lustre_mnttype(const char *type)
2947 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
2950 /* Is this a lustre client fs? */
2951 int llapi_is_lustre_mnt(struct mntent *mnt)
2953 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
2954 strstr(mnt->mnt_fsname, ":/") != NULL);
2957 int llapi_quotacheck(char *mnt, int check_type)
2962 root = opendir(mnt);
2965 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2969 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2977 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2983 root = opendir(mnt);
2986 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
2991 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2995 if (poll_intvl < 30)
3003 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
3008 root = opendir(mnt);
3011 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3015 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
3023 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
3026 struct find_param *param = (struct find_param *)data;
3030 LASSERT(parent != NULL || d != NULL);
3032 rc = get_lmd_info(path, parent, d, param->lmd, param->lumlen);
3034 if (rc == -ENODATA) {
3035 if (!param->obduuid && !param->quiet)
3036 llapi_error(LLAPI_MSG_ERROR, -ENODATA,
3037 "%s has no stripe info", path);
3039 } else if (rc == -ENOENT) {
3045 st = ¶m->lmd->lmd_st;
3047 /* libc chown() will do extra check, and if the real owner is
3048 * the same as the ones to set, it won't fall into kernel, so
3049 * invoke syscall directly. */
3050 rc = syscall(SYS_chown, path, -1, -1);
3052 llapi_error(LLAPI_MSG_ERROR, errno,
3053 "error: chown %s", path);
3055 rc = chmod(path, st->st_mode);
3058 llapi_error(LLAPI_MSG_ERROR, rc, "error: chmod %s (%hu)",
3065 int llapi_quotachown(char *path, int flag)
3067 struct find_param param;
3069 memset(¶m, 0, sizeof(param));
3070 param.recursive = 1;
3074 return param_callback(path, cb_quotachown, NULL, ¶m);
3080 #include <sys/wait.h>
3084 static int rmtacl_notify(int ops)
3088 int found = 0, fd, rc;
3090 fp = setmntent(MOUNTED, "r");
3093 llapi_error(LLAPI_MSG_ERROR, rc,
3094 "error setmntent(%s)", MOUNTED);
3099 mnt = getmntent(fp);
3103 if (!llapi_is_lustre_mnt(mnt))
3106 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
3109 llapi_error(LLAPI_MSG_ERROR, rc,
3110 "Can't open '%s'\n", mnt->mnt_dir);
3114 rc = ioctl(fd, LL_IOC_RMTACL, ops);
3117 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d\n", fd);
3127 static char *next_token(char *p, int div)
3133 while (*p && *p != ':' && !isspace(*p))
3136 while (*p == ':' || isspace(*p))
3139 return *p ? p : NULL;
3142 static int rmtacl_name2id(char *name, int is_user)
3147 pw = getpwnam(name);
3151 return (int)(pw->pw_uid);
3155 gr = getgrnam(name);
3159 return (int)(gr->gr_gid);
3163 static int isodigit(int c)
3165 return (c >= '0' && c <= '7') ? 1 : 0;
3169 * Whether the name is just digits string (uid/gid) already or not.
3174 static int str_is_id(char *str)
3181 if (*str == 'x' || *str == 'X') { /* for Hex. */
3182 if (!isxdigit(*(++str)))
3185 while (isxdigit(*(++str)));
3186 } else if (isodigit(*str)) { /* for Oct. */
3187 while (isodigit(*(++str)));
3189 } else if (isdigit(*str)) { /* for Dec. */
3190 while (isdigit(*(++str)));
3193 return (*str == 0) ? 1 : 0;
3203 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
3205 static rmtacl_name_t rmtacl_namelist[] = {
3206 { RMTACL_OPTNAME("user:"), 1, 0 },
3207 { RMTACL_OPTNAME("group:"), 0, 0 },
3208 { RMTACL_OPTNAME("default:user:"), 1, 0 },
3209 { RMTACL_OPTNAME("default:group:"), 0, 0 },
3210 /* for --tabular option */
3211 { RMTACL_OPTNAME("user"), 1, 1 },
3212 { RMTACL_OPTNAME("group"), 0, 1 },
3216 static int rgetfacl_output(char *str)
3218 char *start = NULL, *end = NULL;
3219 int is_user = 0, n, id;
3226 for (rn = rmtacl_namelist; rn->name; rn++) {
3227 if(strncmp(str, rn->name, rn->length) == 0) {
3228 if (!rn->next_token)
3229 start = str + rn->length;
3231 start = next_token(str + rn->length, 0);
3232 is_user = rn->is_user;
3237 end = next_token(start, 1);
3238 if (end == NULL || start == end) {
3239 n = printf("%s", str);
3245 id = rmtacl_name2id(start, is_user);
3246 if (id == INVALID_ID) {
3247 if (str_is_id(start)) {
3249 n = printf("%s", str);
3252 } else if ((id == NOBODY_UID && is_user) ||
3253 (id == NOBODY_GID && !is_user)) {
3255 n = printf("%s", str);
3259 n = printf("%s%d%s", str, id, end);
3264 static int child_status(int status)
3266 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
3269 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
3272 int fd[2], status, rc;
3279 llapi_error(LLAPI_MSG_ERROR, rc, "Can't create pipe\n");
3286 llapi_error(LLAPI_MSG_ERROR, rc, "Can't fork\n");
3291 /* child process redirects its output. */
3294 if (dup2(fd[1], 1) < 0) {
3296 llapi_error(LLAPI_MSG_ERROR, rc,
3297 "Can't dup2 %d\n", fd[1]);
3307 status = rmtacl_notify(ops);
3311 exit(execvp(argv[0], argv));
3314 /* the following is parent process */
3315 fp = fdopen(fd[0], "r");
3318 llapi_error(LLAPI_MSG_ERROR, rc, "fdopen %d failed\n", fd[0]);
3324 while (fgets(buf, PIPE_BUF, fp) != NULL) {
3325 if (output_func(buf) < 0)
3326 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
3332 if (waitpid(pid, &status, 0) < 0) {
3334 llapi_error(LLAPI_MSG_ERROR, rc, "waitpid %d failed\n", pid);
3338 return child_status(status);
3341 int llapi_lsetfacl(int argc, char *argv[])
3343 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
3346 int llapi_lgetfacl(int argc, char *argv[])
3348 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
3351 int llapi_rsetfacl(int argc, char *argv[])
3353 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
3356 int llapi_rgetfacl(int argc, char *argv[])
3358 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
3361 int llapi_cp(int argc, char *argv[])
3365 rc = rmtacl_notify(RMT_RSETFACL);
3369 exit(execvp(argv[0], argv));
3372 int llapi_ls(int argc, char *argv[])
3376 rc = rmtacl_notify(RMT_LGETFACL);
3380 exit(execvp(argv[0], argv));
3383 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
3384 * format must have %s%s, buf must be > 16
3385 * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID"
3386 * then buf = "lustre-MDT0000"
3388 static int get_mdtname(char *name, char *format, char *buf)
3390 char suffix[]="-MDT0000";
3391 int len = strlen(name);
3393 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
3394 name[len - 5] = '\0';
3399 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
3402 /* Not enough room to add suffix */
3403 llapi_err_noerrno(LLAPI_MSG_ERROR,
3404 "MDT name too long |%s|", name);
3409 return sprintf(buf, format, name, suffix);
3412 /** ioctl on filsystem root, with mdtindex sent as data
3413 * \param mdtname path, fsname, or mdtname (lutre-MDT0004)
3414 * \param mdtidxp pointer to integer within data to be filled in with the
3415 * mdt index (0 if no mdt is specified). NULL won't be filled.
3417 static int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp,
3424 /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases.
3425 Open root and parse mdt index. */
3426 if (mdtname[0] == '/') {
3428 rc = get_root_path(WANT_FD | want_error, NULL, &fd,
3429 (char *)mdtname, -1);
3431 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
3433 ptr = fsname + strlen(fsname) - 8;
3435 index = strtol(ptr + 4, NULL, 10);
3436 rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1);
3440 llapi_err_noerrno(LLAPI_MSG_ERROR,
3441 "Can't open %s: %d\n", mdtname, rc);
3448 rc = ioctl(fd, opc, data);
3453 if (rc && want_error)
3454 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d err %d", opc, rc);
3460 /****** Changelog API ********/
3462 static int changelog_ioctl(const char *mdtname, int opc, int id,
3463 long long recno, int flags)
3465 struct ioc_changelog data;
3469 data.icc_recno = recno;
3470 data.icc_flags = flags;
3471 idx = (int *)(&data.icc_mdtindex);
3473 return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR);
3476 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
3477 struct changelog_private {
3480 lustre_kernelcomm kuc;
3483 /** Start reading from a changelog
3484 * @param priv Opaque private control structure
3485 * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK)
3486 * @param device Report changes recorded on this MDT
3487 * @param startrec Report changes beginning with this record number
3488 * (just call llapi_changelog_fini when done; don't need an endrec)
3490 int llapi_changelog_start(void **priv, int flags, const char *device,
3493 struct changelog_private *cp;
3496 /* Set up the receiver control struct */
3497 cp = calloc(1, sizeof(*cp));
3501 cp->magic = CHANGELOG_PRIV_MAGIC;
3504 /* Set up the receiver */
3505 rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */);
3511 /* Tell the kernel to start sending */
3512 rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd,
3514 /* Only the kernel reference keeps the write side open */
3515 close(cp->kuc.lk_wfd);
3518 /* frees and clears priv */
3519 llapi_changelog_fini(priv);
3530 /** Finish reading from a changelog */
3531 int llapi_changelog_fini(void **priv)
3533 struct changelog_private *cp = (struct changelog_private *)*priv;
3535 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3538 libcfs_ukuc_stop(&cp->kuc);
3544 /** Read the next changelog entry
3545 * @param priv Opaque private control structure
3546 * @param rech Changelog record handle; record will be allocated here
3547 * @return 0 valid message received; rec is set
3551 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
3553 struct changelog_private *cp = (struct changelog_private *)priv;
3554 struct kuc_hdr *kuch;
3557 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
3561 kuch = malloc(CR_MAXSIZE + sizeof(*kuch));
3566 rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
3567 CR_MAXSIZE + sizeof(*kuch),
3568 KUC_TRANSPORT_CHANGELOG);
3572 if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
3573 ((kuch->kuc_msgtype != CL_RECORD) &&
3574 (kuch->kuc_msgtype != CL_EOF))) {
3575 llapi_err_noerrno(LLAPI_MSG_ERROR,
3576 "Unknown changelog message type %d:%d\n",
3577 kuch->kuc_transport, kuch->kuc_msgtype);
3582 if (kuch->kuc_msgtype == CL_EOF) {
3583 if (cp->flags & CHANGELOG_FLAG_FOLLOW) {
3592 /* Our message is a changelog_rec. Use pointer math to skip
3593 * kuch_hdr and point directly to the message payload.
3595 *rech = (struct changelog_rec *)(kuch + 1);
3605 /** Release the changelog record when done with it. */
3606 int llapi_changelog_free(struct changelog_rec **rech)
3609 /* We allocated memory starting at the kuc_hdr, but passed
3610 * the consumer a pointer to the payload.
3611 * Use pointer math to get back to the header.
3613 struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
3620 int llapi_changelog_clear(const char *mdtname, const char *idstr,
3626 llapi_err_noerrno(LLAPI_MSG_ERROR,
3627 "can't purge negative records\n");
3631 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
3632 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
3633 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
3634 llapi_err_noerrno(LLAPI_MSG_ERROR,
3635 "expecting id of the form '"
3636 CHANGELOG_USER_PREFIX
3637 "<num>'; got '%s'\n", idstr);
3641 return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0);
3644 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
3645 int buflen, long long *recno, int *linkno)
3648 struct getinfo_fid2path *gf;
3651 while (*fidstr == '[')
3654 sscanf(fidstr, SFID, RFID(&fid));
3655 if (!fid_is_sane(&fid)) {
3656 llapi_err_noerrno(LLAPI_MSG_ERROR,
3657 "bad FID format [%s], should be "DFID"\n",
3658 fidstr, (__u64)1, 2, 0);
3662 gf = malloc(sizeof(*gf) + buflen);
3666 gf->gf_recno = *recno;
3667 gf->gf_linkno = *linkno;
3668 gf->gf_pathlen = buflen;
3670 /* Take path or fsname */
3671 rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
3674 llapi_error(LLAPI_MSG_ERROR, rc, "ioctl err %d", rc);
3676 memcpy(buf, gf->gf_path, gf->gf_pathlen);
3677 *recno = gf->gf_recno;
3678 *linkno = gf->gf_linkno;
3685 static int path2fid_from_lma(const char *path, lustre_fid *fid)
3688 struct lustre_mdt_attrs *lma;
3691 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
3694 lma = (struct lustre_mdt_attrs *)buf;
3695 fid_le_to_cpu(fid, &lma->lma_self_fid);
3699 int llapi_path2fid(const char *path, lustre_fid *fid)
3703 memset(fid, 0, sizeof(*fid));
3704 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
3706 if (errno == ELOOP) /* symbolic link */
3707 return path2fid_from_lma(path, fid);
3711 rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
3712 if (rc == -EINVAL) /* char special device */
3713 rc = path2fid_from_lma(path, fid);
3719 /****** HSM Copytool API ********/
3720 #define CT_PRIV_MAGIC 0xC0BE2001
3721 struct copytool_private {
3724 lustre_kernelcomm kuc;
3728 #include <libcfs/libcfs.h>
3730 /** Register a copytool
3731 * @param[out] priv Opaque private control structure
3732 * @param fsname Lustre filesystem
3733 * @param flags Open flags, currently unused (e.g. O_NONBLOCK)
3734 * @param archive_count
3735 * @param archives Which archive numbers this copytool is responsible for
3737 int llapi_copytool_start(void **priv, char *fsname, int flags,
3738 int archive_count, int *archives)
3740 struct copytool_private *ct;
3743 if (archive_count > 0 && archives == NULL) {
3744 llapi_err_noerrno(LLAPI_MSG_ERROR,
3745 "NULL archive numbers");
3749 ct = calloc(1, sizeof(*ct));
3753 ct->fsname = malloc(strlen(fsname) + 1);
3754 if (ct->fsname == NULL) {
3758 strcpy(ct->fsname, fsname);
3759 ct->magic = CT_PRIV_MAGIC;
3761 for (rc = 0; rc < archive_count; rc++) {
3762 if (archives[rc] > sizeof(ct->archives)) {
3763 llapi_err_noerrno(LLAPI_MSG_ERROR,
3764 "Maximum of %d archives supported",
3765 sizeof(ct->archives));
3768 ct->archives |= 1 << archives[rc];
3770 /* special case: if no archives specified, default to archive #0. */
3771 if (ct->archives == 0)
3774 rc = libcfs_ukuc_start(&ct->kuc, KUC_GRP_HSM);
3778 /* Storing archive(s) in lk_data; see mdc_ioc_hsm_ct_start */
3779 ct->kuc.lk_data = ct->archives;
3780 rc = root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL,
3782 /* Only the kernel reference keeps the write side open */
3783 close(ct->kuc.lk_wfd);
3798 /** Deregister a copytool */
3799 int llapi_copytool_fini(void **priv)
3801 struct copytool_private *ct = (struct copytool_private *)*priv;
3803 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3806 /* Tell the kernel to stop sending us messages */
3807 ct->kuc.lk_flags = LK_FLG_STOP;
3808 root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL, 0);
3810 /* Shut down the kernelcomms */
3811 libcfs_ukuc_stop(&ct->kuc);
3819 /** Wait for the next hsm_action_list
3820 * @param priv Opaque private control structure
3821 * @param halh Action list handle, will be allocated here
3822 * @param msgsize Number of bytes in the message, will be set here
3823 * @return 0 valid message received; halh and msgsize are set
3826 int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
3828 struct copytool_private *ct = (struct copytool_private *)priv;
3829 struct kuc_hdr *kuch;
3830 struct hsm_action_list *hal;
3833 if (!ct || (ct->magic != CT_PRIV_MAGIC))
3835 if (halh == NULL || msgsize == NULL)
3838 kuch = malloc(HAL_MAXSIZE + sizeof(*kuch));
3842 rc = libcfs_ukuc_msg_get(&ct->kuc, (char *)kuch,
3843 HAL_MAXSIZE + sizeof(*kuch),
3848 /* Handle generic messages */
3849 if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
3850 kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
3855 if (kuch->kuc_transport != KUC_TRANSPORT_HSM ||
3856 kuch->kuc_msgtype != HMT_ACTION_LIST) {
3857 llapi_err_noerrno(LLAPI_MSG_ERROR,
3858 "Unknown HSM message type %d:%d\n",
3859 kuch->kuc_transport, kuch->kuc_msgtype);
3864 /* Our message is a hsm_action_list. Use pointer math to skip
3865 * kuch_hdr and point directly to the message payload.
3867 hal = (struct hsm_action_list *)(kuch + 1);
3869 /* Check that we have registered for this archive # */
3870 if (((1 << hal->hal_archive_num) & ct->archives) == 0) {
3871 llapi_err_noerrno(LLAPI_MSG_INFO,
3872 "Ignoring request for archive #%d (bitmask %#x)\n",
3873 hal->hal_archive_num, ct->archives);
3879 *msgsize = kuch->kuc_msglen - sizeof(*kuch);
3889 /** Release the action list when done with it. */
3890 int llapi_copytool_free(struct hsm_action_list **hal)
3892 /* Reuse the llapi_changelog_free function */
3893 return llapi_changelog_free((struct changelog_rec **)hal);
3896 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
3901 root = opendir(mnt);
3904 llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
3908 rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
3911 llapi_error(LLAPI_MSG_ERROR, rc,
3912 "ioctl on %s for getting connect flags failed", mnt);
3918 int llapi_get_version(char *buffer, int buffer_size,
3923 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buffer;
3925 fd = open(OBD_DEV_PATH, O_RDONLY);
3929 memset(buffer, 0, buffer_size);
3930 data->ioc_version = OBD_IOCTL_VERSION;
3931 data->ioc_inllen1 = buffer_size - cfs_size_round(sizeof(*data));
3932 data->ioc_inlbuf1 = buffer + cfs_size_round(sizeof(*data));
3933 data->ioc_len = obd_ioctl_packlen(data);
3935 rc = ioctl(fd, OBD_GET_VERSION, buffer);
3942 *version = data->ioc_bulk;