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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
63 #ifdef HAVE_LINUX_UNISTD_H
64 #include <linux/unistd.h>
69 #include <libcfs/libcfsutil.h> /* l_ioctl */
70 #include <liblustre.h>
71 #include <lnet/lnetctl.h>
73 #include <lustre_lib.h>
75 #include <lustre/liblustreapi.h>
77 static unsigned llapi_dir_filetype_table[] = {
86 #if defined(DT_DOOR) && defined(S_IFDOOR)
91 #if defined(DT_DOOR) && defined(S_IFDOOR)
92 static const int DT_MAX = DT_DOOR;
94 static const int DT_MAX = DT_SOCK;
97 static unsigned llapi_filetype_dir_table[] = {
106 #if defined(DT_DOOR) && defined(S_IFDOOR)
111 #if defined(DT_DOOR) && defined(S_IFDOOR)
112 static const int S_IFMAX = DT_DOOR;
114 static const int S_IFMAX = DT_SOCK;
117 /* liblustreapi message level */
118 static int llapi_msg_level = LLAPI_MSG_MAX;
120 void llapi_msg_set_level(int level)
122 /* ensure level is in the good range */
123 if (level < LLAPI_MSG_OFF)
124 llapi_msg_level = LLAPI_MSG_OFF;
125 else if (level > LLAPI_MSG_MAX)
126 llapi_msg_level = LLAPI_MSG_MAX;
128 llapi_msg_level = level;
131 void llapi_err(int level, char *fmt, ...)
134 int tmp_errno = abs(errno);
136 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
140 vfprintf(stderr, fmt, args);
143 if (level & LLAPI_MSG_NO_ERRNO)
144 fprintf(stderr, "\n");
146 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
149 #define llapi_err_noerrno(level, fmt, a...) \
150 llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
152 void llapi_printf(int level, char *fmt, ...)
156 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
160 vfprintf(stdout, fmt, args);
165 * size_units is unchanged if no specifier used
167 int parse_size(char *optarg, unsigned long long *size,
168 unsigned long long *size_units, int bytes_spec)
172 *size = strtoull(optarg, &end, 0);
175 if ((*end == 'b') && *(end+1) == '\0' &&
176 (*size & (~0ULL << (64 - 9))) == 0 &&
179 *size_units = 1 << 9;
180 } else if ((*end == 'b') && *(end+1) == '\0' &&
183 } else if ((*end == 'k' || *end == 'K') &&
184 *(end+1) == '\0' && (*size &
185 (~0ULL << (64 - 10))) == 0) {
187 *size_units = 1 << 10;
188 } else if ((*end == 'm' || *end == 'M') &&
189 *(end+1) == '\0' && (*size &
190 (~0ULL << (64 - 20))) == 0) {
192 *size_units = 1 << 20;
193 } else if ((*end == 'g' || *end == 'G') &&
194 *(end+1) == '\0' && (*size &
195 (~0ULL << (64 - 30))) == 0) {
197 *size_units = 1 << 30;
198 } else if ((*end == 't' || *end == 'T') &&
199 *(end+1) == '\0' && (*size &
200 (~0ULL << (64 - 40))) == 0) {
202 *size_units = 1ULL << 40;
203 } else if ((*end == 'p' || *end == 'P') &&
204 *(end+1) == '\0' && (*size &
205 (~0ULL << (64 - 50))) == 0) {
207 *size_units = 1ULL << 50;
208 } else if ((*end == 'e' || *end == 'E') &&
209 *(end+1) == '\0' && (*size &
210 (~0ULL << (64 - 60))) == 0) {
212 *size_units = 1ULL << 60;
221 int llapi_stripe_limit_check(unsigned long stripe_size, int stripe_offset,
222 int stripe_count, int stripe_pattern)
226 /* 64 KB is the largest common page size I'm aware of (on ia64), but
227 * check the local page size just in case. */
228 page_size = LOV_MIN_STRIPE_SIZE;
229 if (getpagesize() > page_size) {
230 page_size = getpagesize();
231 llapi_err_noerrno(LLAPI_MSG_WARN,
232 "warning: your page size (%u) is "
233 "larger than expected (%u)", page_size,
234 LOV_MIN_STRIPE_SIZE);
236 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
237 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
238 "must be an even multiple of %d bytes",
239 stripe_size, page_size);
242 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
244 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d",
248 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
250 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d",
254 if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
256 llapi_err(LLAPI_MSG_ERROR, "error: stripe_size %lu * "
257 "stripe_count %u exceeds 4GB", stripe_size,
264 static int poolpath(char *fsname, char *pathname, char *pool_pathname);
266 int llapi_file_open_pool(const char *name, int flags, int mode,
267 unsigned long stripe_size, int stripe_offset,
268 int stripe_count, int stripe_pattern, char *pool_name)
270 struct lov_user_md_v3 lum = { 0 };
273 char fsname[MAX_OBD_NAME + 1], *ptr;
275 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
276 if (fd < 0 && errno == EISDIR) {
277 fd = open(name, O_DIRECTORY | O_RDONLY);
283 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
287 if ((rc = llapi_stripe_limit_check(stripe_size, stripe_offset,
288 stripe_count, stripe_pattern)) != 0){
293 /* Initialize IOCTL striping pattern structure */
294 lum.lmm_magic = LOV_USER_MAGIC_V3;
295 lum.lmm_pattern = stripe_pattern;
296 lum.lmm_stripe_size = stripe_size;
297 lum.lmm_stripe_count = stripe_count;
298 lum.lmm_stripe_offset = stripe_offset;
300 /* in case user give the full pool name <fsname>.<poolname>, skip
302 if (pool_name != NULL) {
303 ptr = strchr(pool_name, '.');
305 strncpy(fsname, pool_name, ptr - pool_name);
306 fsname[ptr - pool_name] = '\0';
307 /* if fsname matches a filesystem skip it
308 * if not keep the poolname as is */
309 if (poolpath(fsname, NULL, NULL) == 0)
312 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
314 /* If no pool is specified at all, use V1 request */
315 lum.lmm_magic = LOV_USER_MAGIC_V1;
318 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
319 char *errmsg = "stripe already set";
321 if (errno != EEXIST && errno != EALREADY)
322 errmsg = strerror(errno);
324 llapi_err_noerrno(LLAPI_MSG_ERROR,
325 "error on ioctl "LPX64" for '%s' (%d): %s",
326 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
337 int llapi_file_open(const char *name, int flags, int mode,
338 unsigned long stripe_size, int stripe_offset,
339 int stripe_count, int stripe_pattern)
341 return llapi_file_open_pool(name, flags, mode, stripe_size,
342 stripe_offset, stripe_count,
343 stripe_pattern, NULL);
346 int llapi_file_create(const char *name, unsigned long stripe_size,
347 int stripe_offset, int stripe_count, int stripe_pattern)
351 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
352 stripe_offset, stripe_count, stripe_pattern,
361 int llapi_file_create_pool(const char *name, unsigned long stripe_size,
362 int stripe_offset, int stripe_count,
363 int stripe_pattern, char *pool_name)
367 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
368 stripe_offset, stripe_count, stripe_pattern,
378 static int print_pool_members(char *fs, char *pool_dir, char *pool_file)
380 char path[PATH_MAX + 1];
384 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fs, pool_file);
385 sprintf(path, "%s/%s", pool_dir, pool_file);
386 if ((fd = fopen(path, "r")) == NULL) {
387 llapi_err(LLAPI_MSG_ERROR, "Cannot open %s\n", path);
390 while (fgets(buf, sizeof(buf), fd) != NULL)
391 llapi_printf(LLAPI_MSG_NORMAL, buf);
398 * search lustre fsname from pathname
401 static int search_fsname(char *pathname, char *fsname)
405 struct mntent *mnt = NULL;
407 /* get the mount point */
408 fp = setmntent(MOUNTED, "r");
410 llapi_err(LLAPI_MSG_ERROR,
411 "setmntent(%s) failed: %s:", MOUNTED,
416 while ((feof(fp) == 0) && ferror(fp) == 0) {
417 if (llapi_is_lustre_mnt(mnt)) {
418 /* search by pathname */
419 if (strncmp(mnt->mnt_dir, pathname,
420 max(strlen(pathname),
421 strlen(mnt->mnt_dir))) == 0) {
422 ptr = strchr(mnt->mnt_fsname, '/');
437 * find the pool directory path under /proc
438 * (can be also used to test if a fsname is known)
440 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
444 char pattern[PATH_MAX + 1];
445 char buffer[PATH_MAX];
447 if (fsname == NULL) {
448 rc = search_fsname(pathname, buffer);
452 strcpy(pathname, fsname);
455 snprintf(pattern, PATH_MAX,
456 "/proc/fs/lustre/lov/%s-*/pools",
458 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
462 if (glob_info.gl_pathc == 0) {
463 globfree(&glob_info);
467 /* in fsname test mode, pool_pathname is NULL */
468 if (pool_pathname != NULL)
469 strcpy(pool_pathname, glob_info.gl_pathv[0]);
474 int llapi_poollist(char *name)
478 char rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
482 /* is name a pathname ? */
483 ptr = strchr(name, '/');
485 /* only absolute pathname is supported */
488 if (!realpath(name, rname)) {
490 llapi_err(LLAPI_MSG_ERROR,
491 "llapi_poollist: invalid path '%s'",
496 rc = poolpath(NULL, rname, pathname);
499 llapi_err(LLAPI_MSG_ERROR,
500 "llapi_poollist: '%s' is not"
501 " a Lustre filesystem",
508 /* name is FSNAME[.POOLNAME] */
510 poolname = strchr(name, '.');
511 if (poolname != NULL) {
515 rc = poolpath(fsname, NULL, pathname);
518 llapi_err(LLAPI_MSG_ERROR,
519 "llapi_poollist: Lustre filesystem '%s'"
526 llapi_err(LLAPI_MSG_ERROR,
527 "llapi_poollist: Lustre filesystem '%s' not found",
532 if (poolname != NULL) {
533 rc = print_pool_members(fsname, pathname, poolname);
540 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
541 if ((dir = opendir(pathname)) == NULL) {
544 while ((pool = readdir(dir)) != NULL) {
545 if (!((pool->d_name[0] == '.') &&
546 (pool->d_name[1] == '\0')) &&
547 !((pool->d_name[0] == '.') &&
548 (pool->d_name[1] == '.') &&
549 (pool->d_name[2] == '\0')))
550 llapi_printf(LLAPI_MSG_NORMAL, " %s.%s\n",
551 fsname, pool->d_name);
558 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
559 void *data, cfs_dirent_t *de);
561 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
562 #define OBD_NOT_FOUND (-1)
564 static int common_param_init(struct find_param *param)
566 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
567 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
568 llapi_err(LLAPI_MSG_ERROR,
569 "error: allocation of %d bytes for ioctl",
570 sizeof(lstat_t) + param->lumlen);
574 param->got_uuids = 0;
575 param->obdindexes = NULL;
576 param->obdindex = OBD_NOT_FOUND;
580 static void find_param_fini(struct find_param *param)
582 if (param->obdindexes)
583 free(param->obdindexes);
589 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
591 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
594 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
599 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
603 fd = open(path, O_RDONLY);
606 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
610 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
618 * If uuidp is NULL, return the number of available obd uuids.
619 * If uuidp is non-NULL, then it will return the uuids of the obds. If
620 * there are more OSTs then allocated to uuidp, then an error is returned with
621 * the ost_count set to number of available obd uuids.
623 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
625 struct obd_uuid lov_name;
628 int rc = 0, index = 0;
630 /* Get the lov name */
631 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
635 /* Now get the ost uuids from /proc */
636 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
638 fp = fopen(buf, "r");
641 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
645 while (fgets(buf, sizeof(buf), fp) != NULL) {
646 if (uuidp && (index < *ost_count)) {
647 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
655 if (uuidp && (index >= *ost_count))
662 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
669 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
674 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
680 /* Here, param->obduuid points to a single obduuid, the index of which is
681 * returned in param->obdindex */
682 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
684 struct obd_uuid lov_uuid;
685 char uuid[sizeof(struct obd_uuid)];
690 /* Get the lov name */
691 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
693 if (errno != ENOTTY) {
695 llapi_err(LLAPI_MSG_ERROR,
696 "error: can't get lov name: %s", dname);
703 param->got_uuids = 1;
705 /* Now get the ost uuids from /proc */
706 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
708 fp = fopen(buf, "r");
711 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
715 if (!param->obduuid && !param->quiet && !param->obds_printed)
716 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
718 while (fgets(buf, sizeof(buf), fp) != NULL) {
719 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
722 if (param->obduuid) {
723 if (strncmp(param->obduuid->uuid, uuid,
724 sizeof(uuid)) == 0) {
725 param->obdindex = index;
728 } else if (!param->quiet && !param->obds_printed) {
729 /* Print everything */
730 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
733 param->obds_printed = 1;
737 if (!param->quiet && param->obduuid &&
738 (param->obdindex == OBD_NOT_FOUND)) {
739 llapi_err_noerrno(LLAPI_MSG_ERROR,
740 "error: %s: unknown obduuid: %s",
741 __FUNCTION__, param->obduuid->uuid);
748 /* In this case, param->obduuid will be an array of obduuids and
749 * obd index for all these obduuids will be returned in
750 * param->obdindexes */
751 static int setup_obd_indexes(DIR *dir, struct find_param *param)
753 struct obd_uuid *uuids = NULL;
754 int obdcount = INIT_ALLOC_NUM_OSTS;
755 int ret, obd_valid = 0, obdnum, i;
757 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
758 sizeof(struct obd_uuid));
763 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
766 struct obd_uuid *uuids_temp;
768 if (ret == -EOVERFLOW) {
769 uuids_temp = realloc(uuids, obdcount *
770 sizeof(struct obd_uuid));
771 if (uuids_temp != NULL)
772 goto retry_get_uuids;
777 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
781 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
782 if (param->obdindexes == NULL)
785 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
786 for (i = 0; i <= obdcount; i++) {
787 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
788 (char *)&uuids[i]) == 0) {
789 param->obdindexes[obdnum] = i;
795 param->obdindexes[obdnum] = OBD_NOT_FOUND;
799 param->obdindex = OBD_NOT_FOUND;
801 param->obdindex = obd_valid;
803 param->got_uuids = 1;
808 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
809 struct lov_user_ost_data_v1 *objects,
810 char *path, int is_dir,
811 int obdindex, int quiet, int header, int body)
813 int i, obdstripe = 0;
815 if (obdindex != OBD_NOT_FOUND) {
816 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
817 if (obdindex == objects[i].l_ost_idx) {
818 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
824 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
828 /* if it's a directory */
830 if (obdstripe == 1) {
831 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
832 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
833 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
835 llapi_printf(LLAPI_MSG_NORMAL,
836 "stripe_count: %d stripe_size: %u "
837 "stripe_offset: %d%s%s\n",
838 lum->lmm_stripe_count == (__u16)-1 ? -1 :
839 lum->lmm_stripe_count,
840 lum->lmm_stripe_size,
841 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
842 lum->lmm_stripe_offset,
843 pool_name != NULL ? " pool: " : "",
844 pool_name != NULL ? pool_name : "");
849 if (header && (obdstripe == 1)) {
850 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
852 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
854 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
856 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
857 (int)lum->lmm_stripe_count);
858 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
859 lum->lmm_stripe_size);
860 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
862 if (pool_name != NULL)
863 llapi_printf(LLAPI_MSG_NORMAL,
864 "lmm_pool_name: %s\n", pool_name);
868 if ((!quiet) && (obdstripe == 1))
869 llapi_printf(LLAPI_MSG_NORMAL,
870 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
872 for (i = 0; i < lum->lmm_stripe_count; i++) {
873 int idx = objects[i].l_ost_idx;
874 long long oid = objects[i].l_object_id;
875 long long gr = objects[i].l_object_gr;
876 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
877 llapi_printf(LLAPI_MSG_NORMAL,
878 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
880 obdindex == idx ? " *" : "");
882 llapi_printf(LLAPI_MSG_NORMAL, "\n");
886 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
887 int is_dir, int obdindex, int quiet,
888 int header, int body)
890 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
891 int i, obdstripe = 0;
893 if (obdindex != OBD_NOT_FOUND) {
894 for (i = 0; i < lumj->lmm_stripe_count; i++) {
895 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
896 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
902 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
906 if (header && obdstripe == 1) {
907 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
909 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
910 lumj->lmm_object_gr);
911 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
912 lumj->lmm_object_id);
913 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
914 (int)lumj->lmm_stripe_count);
915 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
916 lumj->lmm_stripe_size);
917 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
919 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
920 lumj->lmm_extent_count);
924 unsigned long long start = -1, end = 0;
925 if (!quiet && obdstripe == 1)
926 llapi_printf(LLAPI_MSG_NORMAL,
927 "joined\tobdidx\t\t objid\t\tobjid\t\t "
928 "group\t\tstart\t\tend\n");
929 for (i = 0; i < lumj->lmm_stripe_count; i++) {
930 int idx = lumj->lmm_objects[i].l_ost_idx;
931 long long oid = lumj->lmm_objects[i].l_object_id;
932 long long gr = lumj->lmm_objects[i].l_object_gr;
933 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
934 llapi_printf(LLAPI_MSG_NORMAL,
935 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
937 obdindex == idx ? " *" : "");
938 if (start != lumj->lmm_objects[i].l_extent_start ||
939 end != lumj->lmm_objects[i].l_extent_end) {
940 start = lumj->lmm_objects[i].l_extent_start;
941 llapi_printf(LLAPI_MSG_NORMAL,"\t%14llu",start);
942 end = lumj->lmm_objects[i].l_extent_end;
943 if (end == (unsigned long long)-1)
944 llapi_printf(LLAPI_MSG_NORMAL,
947 llapi_printf(LLAPI_MSG_NORMAL,
950 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
953 llapi_printf(LLAPI_MSG_NORMAL, "\n");
957 void llapi_lov_dump_user_lmm(struct find_param *param,
958 char *path, int is_dir)
960 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
961 case LOV_USER_MAGIC_V1:
962 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
963 param->lmd->lmd_lmm.lmm_objects,
965 param->obdindex, param->quiet,
967 (param->verbose || !param->obduuid));
969 case LOV_USER_MAGIC_JOIN:
970 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
971 param->obdindex, param->quiet,
973 (param->verbose || !param->obduuid));
975 case LOV_USER_MAGIC_V3: {
976 char pool_name[LOV_MAXPOOLNAME + 1];
977 struct lov_user_ost_data_v1 *objects;
978 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
980 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
981 pool_name[LOV_MAXPOOLNAME] = '\0';
982 objects = lmmv3->lmm_objects;
983 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
984 objects, path, is_dir,
985 param->obdindex, param->quiet,
987 (param->verbose || !param->obduuid));
991 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
992 "(expecting one of %#x %#x %#x)\n",
993 *(__u32 *)¶m->lmd->lmd_lmm,
994 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_JOIN,
1000 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1006 fname = strrchr(path, '/');
1008 /* It should be a file (or other non-directory) */
1009 if (fname == NULL) {
1010 dname = (char *)malloc(2);
1014 fname = (char *)path;
1016 dname = (char *)malloc(fname - path + 1);
1019 strncpy(dname, path, fname - path);
1020 dname[fname - path] = '\0';
1024 if ((fd = open(dname, O_RDONLY)) == -1) {
1030 strcpy((char *)lum, fname);
1031 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1034 if (close(fd) == -1 && rc == 0)
1042 int llapi_file_lookup(int dirfd, const char *name)
1044 struct obd_ioctl_data data = { 0 };
1049 if (dirfd < 0 || name == NULL)
1052 data.ioc_version = OBD_IOCTL_VERSION;
1053 data.ioc_len = sizeof(data);
1054 data.ioc_inlbuf1 = (char *)name;
1055 data.ioc_inllen1 = strlen(name) + 1;
1057 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1059 llapi_err(LLAPI_MSG_ERROR,
1060 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1065 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1068 int llapi_mds_getfileinfo(char *path, DIR *parent,
1069 struct lov_user_mds_data *lmd)
1071 lstat_t *st = &lmd->lmd_st;
1072 char *fname = strrchr(path, '/');
1078 fname = (fname == NULL ? path : fname + 1);
1079 /* retrieve needed file info */
1080 strncpy((char *)lmd, fname,
1081 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
1082 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1085 if (errno == ENOTTY) {
1086 /* ioctl is not supported, it is not a lustre fs.
1087 * Do the regular lstat(2) instead. */
1088 ret = lstat_f(path, st);
1090 llapi_err(LLAPI_MSG_ERROR,
1091 "error: %s: lstat failed for %s",
1092 __FUNCTION__, path);
1095 } else if (errno == ENOENT) {
1096 llapi_err(LLAPI_MSG_WARN,
1097 "warning: %s: %s does not exist",
1098 __FUNCTION__, path);
1101 llapi_err(LLAPI_MSG_ERROR,
1102 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1103 __FUNCTION__, path);
1111 static DIR *opendir_parent(char *path)
1117 fname = strrchr(path, '/');
1119 return opendir(".");
1123 parent = opendir(path);
1128 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1129 semantic_func_t sem_init,
1130 semantic_func_t sem_fini, void *data,
1141 if (!d && errno != ENOTDIR) {
1142 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
1143 __FUNCTION__, path);
1145 } else if (!d && !parent) {
1146 /* ENOTDIR. Open the parent dir. */
1147 p = opendir_parent(path);
1149 GOTO(out, ret = -EINVAL);
1152 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1158 while ((dent = readdir64(d)) != NULL) {
1159 ((struct find_param *)data)->have_fileinfo = 0;
1161 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1165 if ((len + dent->d_reclen + 2) > size) {
1166 llapi_err(LLAPI_MSG_ERROR,
1167 "error: %s: string buffer is too small",
1172 strcat(path, dent->d_name);
1174 if (dent->d_type == DT_UNKNOWN) {
1175 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1177 ret = llapi_mds_getfileinfo(path, d,
1178 ((struct find_param *)data)->lmd);
1180 ((struct find_param *)data)->have_fileinfo = 1;
1182 llapi_filetype_dir_table[st->st_mode &
1189 switch (dent->d_type) {
1191 llapi_err(LLAPI_MSG_ERROR,
1192 "error: %s: '%s' is UNKNOWN type %d",
1193 __FUNCTION__, dent->d_name, dent->d_type);
1196 ret = llapi_semantic_traverse(path, size, d, sem_init,
1197 sem_fini, data, dent);
1204 ret = sem_init(path, d, NULL, data, dent);
1208 if (sem_fini && ret == 0)
1209 sem_fini(path, d, NULL, data, dent);
1217 sem_fini(path, parent, d, data, de);
1226 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1227 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1229 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1230 * The table below gives the answers for the specified parameters (value and
1231 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1232 * --------------------------------------
1233 * 1 | file > limit; sign > 0 | -1 / -1 |
1234 * 2 | file = limit; sign > 0 | ? / 1 |
1235 * 3 | file < limit; sign > 0 | ? / 1 |
1236 * 4 | file > limit; sign = 0 | -1 / -1 |
1237 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1238 * 6 | file < limit; sign = 0 | ? / -1 |
1239 * 7 | file > limit; sign < 0 | 1 / 1 |
1240 * 8 | file = limit; sign < 0 | ? / -1 |
1241 * 9 | file < limit; sign < 0 | ? / -1 |
1242 * --------------------------------------
1243 * Note: 5th actually means that the value is within the interval
1244 * (limit - margin, limit]. */
1245 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1246 unsigned long long margin, int mds)
1254 if (file <= limit && file + margin > limit)
1256 if (file + margin <= limit)
1257 return mds ? 0 : -1;
1270 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1271 * Return -1 or 1 if file timestamp does not or does match the given criteria
1272 * correspondingly. Return 0 if the MDS time is being checked and there are
1273 * attributes on OSTs and it is not yet clear if the timespamp matches.
1275 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1276 * updated timestamps. */
1277 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1282 /* Check if file is accepted. */
1284 ret = find_value_cmp(st->st_atime, param->atime,
1285 param->asign, 24 * 60 * 60, mds);
1292 ret = find_value_cmp(st->st_mtime, param->mtime,
1293 param->msign, 24 * 60 * 60, mds);
1297 /* If the previous check matches, but this one is not yet clear,
1298 * we should return 0 to do an RPC on OSTs. */
1304 ret = find_value_cmp(st->st_ctime, param->ctime,
1305 param->csign, 24 * 60 * 60, mds);
1309 /* If the previous check matches, but this one is not yet clear,
1310 * we should return 0 to do an RPC on OSTs. */
1318 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1319 void *data, cfs_dirent_t *de)
1321 struct find_param *param = (struct find_param *)data;
1322 int decision = 1; /* 1 is accepted; -1 is rejected. */
1323 lstat_t *st = ¶m->lmd->lmd_st;
1325 int checked_type = 0;
1328 LASSERT(parent != NULL || dir != NULL);
1330 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1332 /* If a regular expression is presented, make the initial decision */
1333 if (param->pattern != NULL) {
1334 char *fname = strrchr(path, '/');
1335 fname = (fname == NULL ? path : fname + 1);
1336 ret = fnmatch(param->pattern, fname, 0);
1337 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1338 (ret == 0 && param->exclude_pattern))
1342 /* See if we can check the file type from the dirent. */
1343 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1344 de->d_type < DT_MAX) {
1346 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1347 if (param->exclude_type)
1350 if (!param->exclude_type)
1356 /* If a time or OST should be checked, the decision is not taken yet. */
1357 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1362 /* Request MDS for the stat info. */
1363 if (param->have_fileinfo == 0) {
1365 /* retrieve needed file info */
1366 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1367 (void *)param->lmd);
1369 char *fname = strrchr(path, '/');
1370 fname = (fname == NULL ? path : fname + 1);
1372 /* retrieve needed file info */
1373 strncpy((char *)param->lmd, fname, param->lumlen);
1374 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1375 (void *)param->lmd);
1380 if (errno == ENOTTY) {
1381 /* ioctl is not supported, it is not a lustre fs.
1382 * Do the regular lstat(2) instead. */
1384 ret = lstat_f(path, st);
1386 llapi_err(LLAPI_MSG_ERROR,
1387 "error: %s: lstat failed for %s",
1388 __FUNCTION__, path);
1391 } else if (errno == ENOENT) {
1392 llapi_err(LLAPI_MSG_WARN,
1393 "warning: %s: %s does not exist",
1394 __FUNCTION__, path);
1397 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1398 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1399 "IOC_MDC_GETFILEINFO", path);
1404 if (param->type && !checked_type) {
1405 if ((st->st_mode & S_IFMT) == param->type) {
1406 if (param->exclude_type)
1409 if (!param->exclude_type)
1415 if (param->obduuid) {
1416 if (lustre_fs && param->got_uuids &&
1417 param->st_dev != st->st_dev) {
1418 /* A lustre/lustre mount point is crossed. */
1419 param->got_uuids = 0;
1420 param->obds_printed = 0;
1421 param->obdindex = OBD_NOT_FOUND;
1424 if (lustre_fs && !param->got_uuids) {
1425 ret = setup_obd_indexes(dir ? dir : parent, param);
1429 param->st_dev = st->st_dev;
1430 } else if (!lustre_fs && param->got_uuids) {
1431 /* A lustre/non-lustre mount point is crossed. */
1432 param->got_uuids = 0;
1433 param->obdindex = OBD_NOT_FOUND;
1437 /* If an OBD UUID is specified but no one matches, skip this file. */
1438 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1441 /* If a OST UUID is given, and some OST matches, check it here. */
1442 if (param->obdindex != OBD_NOT_FOUND) {
1443 if (!S_ISREG(st->st_mode))
1446 /* Only those files should be accepted, which have a
1447 * stripe on the specified OST. */
1448 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1452 struct lov_user_ost_data_v1 *lmm_objects;
1454 if (param->lmd->lmd_lmm.lmm_magic ==
1455 LOV_USER_MAGIC_V3) {
1456 struct lov_user_md_v3 *lmmv3 =
1457 (void *)¶m->lmd->lmd_lmm;
1459 lmm_objects = lmmv3->lmm_objects;
1461 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1465 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1466 for (j = 0; j < param->num_obds; j++) {
1467 if (param->obdindexes[j] ==
1468 lmm_objects[i].l_ost_idx)
1473 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1478 if (param->check_uid) {
1479 if (st->st_uid == param->uid) {
1480 if (param->exclude_uid)
1483 if (!param->exclude_uid)
1488 if (param->check_gid) {
1489 if (st->st_gid == param->gid) {
1490 if (param->exclude_gid)
1493 if (!param->exclude_gid)
1498 if (param->check_pool) {
1499 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1501 /* empty requested pool is taken as no pool search => V1 */
1502 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1503 (param->poolname[0] == '\0')) ||
1504 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1505 (strncmp(lmmv3->lmm_pool_name,
1506 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1507 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1508 (strcmp(param->poolname, "*") == 0))) {
1509 if (param->exclude_pool)
1512 if (!param->exclude_pool)
1517 /* Check the time on mds. */
1521 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1522 param->lmd->lmd_lmm.lmm_stripe_count)
1524 decision = find_time_check(st, param, for_mds);
1528 /* If file still fits the request, ask osd for updated info.
1529 The regulat stat is almost of the same speed as some new
1530 'glimpse-size-ioctl'. */
1531 if (!decision && S_ISREG(st->st_mode) &&
1532 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1533 if (param->obdindex != OBD_NOT_FOUND) {
1534 /* Check whether the obd is active or not, if it is
1535 * not active, just print the object affected by this
1538 struct obd_statfs stat_buf;
1539 struct obd_uuid uuid_buf;
1541 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1542 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1543 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1544 param->obdindex, &stat_buf,
1547 if (ret == -ENODATA || ret == -ENODEV
1550 llapi_printf(LLAPI_MSG_NORMAL,
1551 "obd_uuid: %s failed %s ",
1552 param->obduuid->uuid,
1558 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1559 (void *)param->lmd);
1560 } else if (parent) {
1561 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1562 (void *)param->lmd);
1566 if (errno == ENOENT) {
1567 llapi_err(LLAPI_MSG_ERROR,
1568 "warning: %s: %s does not exist",
1569 __FUNCTION__, path);
1572 llapi_err(LLAPI_MSG_ERROR,
1573 "%s: IOC_LOV_GETINFO on %s failed",
1574 __FUNCTION__, path);
1579 /* Check the time on osc. */
1580 decision = find_time_check(st, param, 0);
1586 decision = find_value_cmp(st->st_size, param->size,
1587 param->size_sign, param->size_units,
1591 if (decision != -1) {
1592 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1594 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1596 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1600 /* Do not get down anymore? */
1601 if (param->depth == param->maxdepth)
1608 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1611 struct find_param *param = (struct find_param *)data;
1616 int llapi_find(char *path, struct find_param *param)
1619 int ret, len = strlen(path);
1621 if (len > PATH_MAX) {
1622 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1623 __FUNCTION__, path);
1627 buf = (char *)malloc(PATH_MAX + 1);
1631 ret = common_param_init(param);
1639 strncpy(buf, path, PATH_MAX + 1);
1640 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1641 cb_common_fini, param, NULL);
1643 find_param_fini(param);
1645 return ret < 0 ? ret : 0;
1648 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1651 struct find_param *param = (struct find_param *)data;
1654 LASSERT(parent != NULL || d != NULL);
1657 if (!param->got_uuids) {
1658 ret = setup_obd_uuid(d ? d : parent, path, param);
1664 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1665 (void *)¶m->lmd->lmd_lmm);
1666 } else if (parent) {
1667 char *fname = strrchr(path, '/');
1668 fname = (fname == NULL ? path : fname + 1);
1670 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1671 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1672 (void *)¶m->lmd->lmd_lmm);
1676 if (errno == ENODATA) {
1677 if (!param->obduuid && !param->quiet)
1678 llapi_printf(LLAPI_MSG_NORMAL,
1679 "%s has no stripe info\n", path);
1681 } else if (errno == ENOTTY) {
1682 llapi_err(LLAPI_MSG_ERROR,
1683 "%s: '%s' not on a Lustre fs?",
1684 __FUNCTION__, path);
1685 } else if (errno == ENOENT) {
1686 llapi_err(LLAPI_MSG_WARN,
1687 "warning: %s: %s does not exist",
1688 __FUNCTION__, path);
1691 llapi_err(LLAPI_MSG_ERROR,
1692 "error: %s: %s failed for %s",
1693 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1694 "IOC_MDC_GETFILESTRIPE", path);
1700 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1702 /* Do not get down anymore? */
1703 if (param->depth == param->maxdepth)
1710 int llapi_getstripe(char *path, struct find_param *param)
1713 int ret = 0, len = strlen(path);
1715 if (len > PATH_MAX) {
1716 llapi_err(LLAPI_MSG_ERROR,
1717 "%s: Path name '%s' is too long",
1718 __FUNCTION__, path);
1722 buf = (char *)malloc(PATH_MAX + 1);
1726 ret = common_param_init(param);
1734 strncpy(buf, path, PATH_MAX + 1);
1735 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1736 cb_common_fini, param, NULL);
1737 find_param_fini(param);
1739 return ret < 0 ? ret : 0;
1742 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1743 struct obd_statfs *stat_buf,
1744 struct obd_uuid *uuid_buf)
1747 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1749 struct obd_ioctl_data data = { 0 };
1752 data.ioc_inlbuf1 = (char *)&type;
1753 data.ioc_inllen1 = sizeof(__u32);
1754 data.ioc_inlbuf2 = (char *)&index;
1755 data.ioc_inllen2 = sizeof(__u32);
1756 data.ioc_pbuf1 = (char *)stat_buf;
1757 data.ioc_plen1 = sizeof(struct obd_statfs);
1758 data.ioc_pbuf2 = (char *)uuid_buf;
1759 data.ioc_plen2 = sizeof(struct obd_uuid);
1761 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1762 llapi_err(LLAPI_MSG_ERROR,
1763 "llapi_obd_statfs: error packing ioctl data");
1767 fd = open(path, O_RDONLY);
1768 if (errno == EISDIR)
1769 fd = open(path, O_DIRECTORY | O_RDONLY);
1772 rc = errno ? -errno : -EBADF;
1773 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
1774 __FUNCTION__, path);
1777 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1779 rc = errno ? -errno : -EINVAL;
1785 #define MAX_STRING_SIZE 128
1786 #define DEVICES_LIST "/proc/fs/lustre/devices"
1788 int llapi_ping(char *obd_type, char *obd_name)
1790 char path[MAX_STRING_SIZE];
1794 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1795 obd_type, obd_name);
1797 fd = open(path, O_WRONLY);
1800 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1804 rc = write(fd, buf, 1);
1812 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
1814 char buf[MAX_STRING_SIZE];
1815 FILE *fp = fopen(DEVICES_LIST, "r");
1820 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1824 while (fgets(buf, sizeof(buf), fp) != NULL) {
1825 char *obd_type_name = NULL;
1826 char *obd_name = NULL;
1827 char *obd_uuid = NULL;
1829 struct obd_ioctl_data datal = { 0, };
1830 struct obd_statfs osfs_buffer;
1832 while(bufp[0] == ' ')
1835 for(i = 0; i < 3; i++) {
1836 obd_type_name = strsep(&bufp, " ");
1838 obd_name = strsep(&bufp, " ");
1839 obd_uuid = strsep(&bufp, " ");
1841 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1843 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1844 datal.ioc_plen1 = sizeof(osfs_buffer);
1846 for (i = 0; i < type_num; i++) {
1847 if (strcmp(obd_type_name, obd_type[i]) != 0)
1850 cb(obd_type_name, obd_name, obd_uuid, args);
1857 static void do_target_check(char *obd_type_name, char *obd_name,
1858 char *obd_uuid, void *args)
1862 rc = llapi_ping(obd_type_name, obd_name);
1863 if (rc == ENOTCONN) {
1864 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
1866 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1868 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1872 int llapi_target_check(int type_num, char **obd_type, char *dir)
1874 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1877 #undef MAX_STRING_SIZE
1879 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1881 char raw[OBD_MAX_IOCTL_BUFFER];
1882 char out[LLOG_CHUNK_SIZE];
1884 struct obd_ioctl_data data = { 0 };
1889 sprintf(key, "%s", keyword);
1890 memset(raw, 0, sizeof(raw));
1891 memset(out, 0, sizeof(out));
1892 data.ioc_inlbuf1 = key;
1893 data.ioc_inllen1 = strlen(key) + 1;
1895 data.ioc_inlbuf2 = node_name;
1896 data.ioc_inllen2 = strlen(node_name) + 1;
1898 data.ioc_pbuf1 = out;
1899 data.ioc_plen1 = sizeof(out);
1900 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1904 root = opendir(dir);
1907 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1911 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1913 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1915 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1921 /* Is this a lustre fs? */
1922 int llapi_is_lustre_mnttype(const char *type)
1924 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1927 /* Is this a lustre client fs? */
1928 int llapi_is_lustre_mnt(struct mntent *mnt)
1930 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1931 strstr(mnt->mnt_fsname, ":/") != NULL);
1934 int llapi_quotacheck(char *mnt, int check_type)
1939 root = opendir(mnt);
1941 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1945 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1951 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1957 root = opendir(mnt);
1959 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1964 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1968 if (poll_intvl < 30)
1976 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1981 root = opendir(mnt);
1983 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1987 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1993 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
1996 struct find_param *param = (struct find_param *)data;
2000 LASSERT(parent != NULL || d != NULL);
2003 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2004 (void *)param->lmd);
2005 } else if (parent) {
2006 char *fname = strrchr(path, '/');
2007 fname = (fname == NULL ? path : fname + 1);
2009 strncpy((char *)param->lmd, fname, param->lumlen);
2010 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2011 (void *)param->lmd);
2017 if (errno == ENODATA) {
2018 if (!param->obduuid && !param->quiet)
2019 llapi_err(LLAPI_MSG_ERROR,
2020 "%s has no stripe info", path);
2022 } else if (errno == ENOENT) {
2023 llapi_err(LLAPI_MSG_ERROR,
2024 "warning: %s: %s does not exist",
2025 __FUNCTION__, path);
2027 } else if (errno != EISDIR) {
2029 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2030 d ? "LL_IOC_MDC_GETINFO" :
2031 "IOC_MDC_GETFILEINFO", path);
2036 st = ¶m->lmd->lmd_st;
2038 /* libc chown() will do extra check, and if the real owner is
2039 * the same as the ones to set, it won't fall into kernel, so
2040 * invoke syscall directly. */
2041 rc = syscall(SYS_chown, path, -1, -1);
2043 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2045 rc = chmod(path, st->st_mode);
2047 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2053 int llapi_quotachown(char *path, int flag)
2055 struct find_param param;
2057 int ret = 0, len = strlen(path);
2059 if (len > PATH_MAX) {
2060 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
2061 __FUNCTION__, path);
2065 buf = (char *)malloc(PATH_MAX + 1);
2069 memset(¶m, 0, sizeof(param));
2070 param.recursive = 1;
2074 ret = common_param_init(¶m);
2078 strncpy(buf, path, PATH_MAX + 1);
2079 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
2080 NULL, ¶m, NULL);
2082 find_param_fini(¶m);
2090 #include <sys/wait.h>
2094 static int rmtacl_notify(int ops)
2098 int found = 0, fd, rc;
2100 fp = setmntent(MOUNTED, "r");
2102 perror("setmntent");
2107 mnt = getmntent(fp);
2111 if (!llapi_is_lustre_mnt(mnt))
2114 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2120 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2132 static char *next_token(char *p, int div)
2138 while (*p && *p != ':' && !isspace(*p))
2141 while (*p == ':' || isspace(*p))
2144 return *p ? p : NULL;
2147 static int rmtacl_name2id(char *name, int is_user)
2152 if ((pw = getpwnam(name)) == NULL)
2155 return (int)(pw->pw_uid);
2159 if ((gr = getgrnam(name)) == NULL)
2162 return (int)(gr->gr_gid);
2166 static int isodigit(int c)
2168 return (c >= '0' && c <= '7') ? 1 : 0;
2172 * Whether the name is just digits string (uid/gid) already or not.
2177 static int str_is_id(char *str)
2184 if (*str == 'x' || *str == 'X') { /* for Hex. */
2185 if (!isxdigit(*(++str)))
2188 while (isxdigit(*(++str)));
2189 } else if (isodigit(*str)) { /* for Oct. */
2190 while (isodigit(*(++str)));
2192 } else if (isdigit(*str)) { /* for Dec. */
2193 while (isdigit(*(++str)));
2196 return (*str == 0) ? 1 : 0;
2206 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2208 static rmtacl_name_t rmtacl_namelist[] = {
2209 { RMTACL_OPTNAME("user:"), 1, 0 },
2210 { RMTACL_OPTNAME("group:"), 0, 0 },
2211 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2212 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2213 /* for --tabular option */
2214 { RMTACL_OPTNAME("user"), 1, 1 },
2215 { RMTACL_OPTNAME("group"), 0, 1 },
2219 static int rgetfacl_output(char *str)
2221 char *start = NULL, *end = NULL;
2222 int is_user = 0, n, id;
2229 for (rn = rmtacl_namelist; rn->name; rn++) {
2230 if(strncmp(str, rn->name, rn->length) == 0) {
2231 if (!rn->next_token)
2232 start = str + rn->length;
2234 start = next_token(str + rn->length, 0);
2235 is_user = rn->is_user;
2240 end = next_token(start, 1);
2241 if (end == NULL || start == end) {
2242 n = printf("%s", str);
2248 id = rmtacl_name2id(start, is_user);
2249 if (id == INVALID_ID) {
2250 if (str_is_id(start)) {
2252 n = printf("%s", str);
2255 } else if ((id == NOBODY_UID && is_user) ||
2256 (id == NOBODY_GID && !is_user)) {
2258 n = printf("%s", str);
2262 n = printf("%s%d%s", str, id, end);
2267 static int child_status(int status)
2269 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2272 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2285 if ((pid = fork()) < 0) {
2291 /* child process redirects its output. */
2294 if (dup2(fd[1], 1) < 0) {
2305 status = rmtacl_notify(ops);
2309 exit(execvp(argv[0], argv));
2312 /* the following is parent process */
2313 if ((fp = fdopen(fd[0], "r")) == NULL) {
2320 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2321 if (output_func(buf) < 0)
2322 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2328 if (waitpid(pid, &status, 0) < 0) {
2333 return child_status(status);
2336 int llapi_lsetfacl(int argc, char *argv[])
2338 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2341 int llapi_lgetfacl(int argc, char *argv[])
2343 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2346 int llapi_rsetfacl(int argc, char *argv[])
2348 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2351 int llapi_rgetfacl(int argc, char *argv[])
2353 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2356 int llapi_cp(int argc, char *argv[])
2360 rc = rmtacl_notify(RMT_RSETFACL);
2364 exit(execvp(argv[0], argv));
2367 int llapi_ls(int argc, char *argv[])
2371 rc = rmtacl_notify(RMT_LGETFACL);
2375 exit(execvp(argv[0], argv));
2378 /* format must have %s%s, buf must be > 16 */
2379 static int get_mdtname(const char *name, char *format, char *buf)
2381 char suffix[]="-MDT0000";
2382 int len = strlen(name);
2385 llapi_err(LLAPI_MSG_ERROR, "bad MDT name |%s|\n", name);
2389 if ((len > 8) && (strncmp(name + len - 8, "-MDT", 4) == 0))
2392 return sprintf(buf, format, name, suffix);
2395 #define CHANGELOG_FILE "/proc/fs/lustre/mdd/%s%s/changelog"
2397 /* return a file desc to readable changelog */
2398 int llapi_changelog_open(const char *mdtname, long long startrec)
2403 if (get_mdtname(mdtname, CHANGELOG_FILE, path) <0)
2406 if ((fd = open(path, O_RDONLY)) < 0) {
2407 llapi_err(LLAPI_MSG_ERROR, "error: can't open |%s|\n", path);
2411 rc = lseek(fd, (off_t)startrec, SEEK_SET);
2413 llapi_err(LLAPI_MSG_ERROR, "can't seek rc=%d\n", rc);
2420 int llapi_changelog_clear(const char *mdtname, long long endrec)
2427 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2428 "can't purge negative records\n");
2432 if (get_mdtname(mdtname, CHANGELOG_FILE, path) <0)
2435 if ((fd = open(path, O_WRONLY)) < 0) {
2436 llapi_err(LLAPI_MSG_ERROR, "error: can't open |%s|\n", path);
2440 snprintf(val, sizeof(val), "%llu", endrec);
2441 len = write(fd, val, strlen(val));
2443 if (len != strlen(val)) {
2444 llapi_err(LLAPI_MSG_ERROR, "purge err\n");
2451 static int dev_ioctl(struct obd_ioctl_data *data, int dev, int cmd)
2454 static char rawbuf[8192];
2455 static char *buf = rawbuf;
2457 data->ioc_dev = dev;
2458 memset(buf, 0, sizeof(rawbuf));
2460 if ((rc = obd_ioctl_pack(data, &buf, sizeof(rawbuf)))) {
2461 llapi_err(LLAPI_MSG_ERROR,
2462 "error: ioctl pack (%d) failed: rc %d", cmd, rc);
2466 rc = l_ioctl(OBD_DEV_ID, cmd, buf);
2468 /* ioctl returns -1 with errno set */
2473 if (obd_ioctl_unpack(data, buf, sizeof(rawbuf))) {
2474 llapi_err(LLAPI_MSG_ERROR,
2475 "error: invalid reply\n");
2481 /* should we just grep it from proc? */
2482 static int dev_name2dev(char *name)
2484 struct obd_ioctl_data data;
2487 memset(&data, 0, sizeof(data));
2488 data.ioc_inllen1 = strlen(name) + 1;
2489 data.ioc_inlbuf1 = name;
2490 rc = dev_ioctl(&data, -1, OBD_IOC_NAME2DEV);
2493 llapi_err(LLAPI_MSG_ERROR, "Device %s not found %d\n", name,rc);
2496 return data.ioc_dev;
2499 int llapi_fid2path(char *device, char *fidstr, char *buf, int buflen,
2500 __u64 recno, int *linkno)
2503 struct obd_ioctl_data data;
2506 while (*fidstr == '[')
2509 sscanf(fidstr, "0x%llx:0x%x:0x%x", &(fid.f_seq), &(fid.f_oid),
2511 if (!fid_is_sane(&fid)) {
2512 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2513 "bad FID format [%s], should be "DFID"\n",
2514 fidstr, (__u64)1, 2, 0);
2518 dev = dev_name2dev(device);
2522 memset(&data, 0, sizeof(data));
2523 data.ioc_inlbuf1 = (char *)&fid;
2524 data.ioc_inllen1 = sizeof(fid);
2525 data.ioc_inlbuf2 = (char *)&recno;
2526 data.ioc_inllen2 = sizeof(__u64);
2527 data.ioc_inlbuf3 = (char *)linkno;
2528 data.ioc_inllen3 = sizeof(int);
2529 data.ioc_plen1 = buflen;
2530 data.ioc_pbuf1 = buf;
2531 rc = dev_ioctl(&data, dev, OBD_IOC_FID2PATH);
2536 int llapi_path2fid(const char *path, unsigned long long *seq,
2537 unsigned long *oid, unsigned long *ver)
2542 fd = open(path, O_RDONLY);
2546 rc = ioctl(fd, LL_IOC_PATH2FID, &fid);
2547 *seq = fid_seq(&fid);
2548 *oid = fid_oid(&fid);
2549 *ver = fid_ver(&fid);