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);
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 * Resolve lustre fsname from pathname
400 static int search_fsname(char *pathname, char *fsname)
404 struct mntent *mnt = NULL;
406 /* get the mount point */
407 fp = setmntent(MOUNTED, "r");
409 llapi_err(LLAPI_MSG_ERROR,
410 "setmntent(%s) failed: %s:", MOUNTED,
415 while ((feof(fp) == 0) && ferror(fp) == 0) {
416 if (llapi_is_lustre_mnt(mnt)) {
417 /* search by pathname */
418 if (strncmp(mnt->mnt_dir, pathname,
419 max(strlen(pathname),
420 strlen(mnt->mnt_dir))) == 0) {
421 ptr = strchr(mnt->mnt_fsname, '/');
436 * Return an open fd to the named Lustre fs root
438 static int get_root_fd(char *fsname)
442 struct mntent *mnt = NULL;
445 /* get the mount point */
446 fp = setmntent(MOUNTED, "r");
448 llapi_err(LLAPI_MSG_ERROR,
449 "setmntent(%s) failed: %s:", MOUNTED,
454 while ((feof(fp) == 0) && ferror(fp) == 0) {
455 if (llapi_is_lustre_mnt(mnt)) {
456 ptr = strrchr(mnt->mnt_fsname, '/');
461 if (strcmp(ptr, fsname) == 0) {
462 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
477 /* return the first file matching this pattern */
478 static int first_match(char *pattern, char *buffer)
482 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
485 if (glob_info.gl_pathc < 1) {
486 globfree(&glob_info);
490 strcpy(buffer, glob_info.gl_pathv[0]);
492 globfree(&glob_info);
497 * find the pool directory path under /proc
498 * (can be also used to test if a fsname is known)
500 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
503 char pattern[PATH_MAX + 1];
504 char buffer[PATH_MAX];
506 if (fsname == NULL) {
507 rc = search_fsname(pathname, buffer);
511 strcpy(pathname, fsname);
514 snprintf(pattern, PATH_MAX,
515 "/proc/fs/lustre/lov/%s-*/pools",
517 rc = first_match(pattern, buffer);
521 /* in fsname test mode, pool_pathname is NULL */
522 if (pool_pathname != NULL)
523 strcpy(pool_pathname, buffer);
528 int llapi_poollist(char *name)
532 char rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
536 /* is name a pathname ? */
537 ptr = strchr(name, '/');
539 /* only absolute pathname is supported */
542 if (!realpath(name, rname)) {
544 llapi_err(LLAPI_MSG_ERROR,
545 "llapi_poollist: invalid path '%s'",
550 rc = poolpath(NULL, rname, pathname);
553 llapi_err(LLAPI_MSG_ERROR,
554 "llapi_poollist: '%s' is not"
555 " a Lustre filesystem",
562 /* name is FSNAME[.POOLNAME] */
564 poolname = strchr(name, '.');
565 if (poolname != NULL) {
569 rc = poolpath(fsname, NULL, pathname);
572 llapi_err(LLAPI_MSG_ERROR,
573 "llapi_poollist: Lustre filesystem '%s'"
580 llapi_err(LLAPI_MSG_ERROR,
581 "llapi_poollist: Lustre filesystem '%s' not found",
586 if (poolname != NULL) {
587 rc = print_pool_members(fsname, pathname, poolname);
594 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
595 if ((dir = opendir(pathname)) == NULL) {
598 while ((pool = readdir(dir)) != NULL) {
599 if (!((pool->d_name[0] == '.') &&
600 (pool->d_name[1] == '\0')) &&
601 !((pool->d_name[0] == '.') &&
602 (pool->d_name[1] == '.') &&
603 (pool->d_name[2] == '\0')))
604 llapi_printf(LLAPI_MSG_NORMAL, " %s.%s\n",
605 fsname, pool->d_name);
612 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
613 void *data, cfs_dirent_t *de);
615 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
616 #define OBD_NOT_FOUND (-1)
618 static int common_param_init(struct find_param *param)
620 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
621 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
622 llapi_err(LLAPI_MSG_ERROR,
623 "error: allocation of %d bytes for ioctl",
624 sizeof(lstat_t) + param->lumlen);
628 param->got_uuids = 0;
629 param->obdindexes = NULL;
630 param->obdindex = OBD_NOT_FOUND;
634 static void find_param_fini(struct find_param *param)
636 if (param->obdindexes)
637 free(param->obdindexes);
643 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
645 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
648 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
653 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
657 fd = open(path, O_RDONLY);
660 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
664 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
672 * If uuidp is NULL, return the number of available obd uuids.
673 * If uuidp is non-NULL, then it will return the uuids of the obds. If
674 * there are more OSTs then allocated to uuidp, then an error is returned with
675 * the ost_count set to number of available obd uuids.
677 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
679 struct obd_uuid lov_name;
682 int rc = 0, index = 0;
684 /* Get the lov name */
685 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
689 /* Now get the ost uuids from /proc */
690 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
692 fp = fopen(buf, "r");
695 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
699 while (fgets(buf, sizeof(buf), fp) != NULL) {
700 if (uuidp && (index < *ost_count)) {
701 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
709 if (uuidp && (index >= *ost_count))
716 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
723 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
728 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
734 /* Here, param->obduuid points to a single obduuid, the index of which is
735 * returned in param->obdindex */
736 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
738 struct obd_uuid lov_uuid;
739 char uuid[sizeof(struct obd_uuid)];
744 /* Get the lov name */
745 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
747 if (errno != ENOTTY) {
749 llapi_err(LLAPI_MSG_ERROR,
750 "error: can't get lov name: %s", dname);
757 param->got_uuids = 1;
759 /* Now get the ost uuids from /proc */
760 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
762 fp = fopen(buf, "r");
765 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
769 if (!param->obduuid && !param->quiet && !param->obds_printed)
770 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
772 while (fgets(buf, sizeof(buf), fp) != NULL) {
773 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
776 if (param->obduuid) {
777 if (strncmp(param->obduuid->uuid, uuid,
778 sizeof(uuid)) == 0) {
779 param->obdindex = index;
782 } else if (!param->quiet && !param->obds_printed) {
783 /* Print everything */
784 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
787 param->obds_printed = 1;
791 if (!param->quiet && param->obduuid &&
792 (param->obdindex == OBD_NOT_FOUND)) {
793 llapi_err_noerrno(LLAPI_MSG_ERROR,
794 "error: %s: unknown obduuid: %s",
795 __FUNCTION__, param->obduuid->uuid);
802 /* In this case, param->obduuid will be an array of obduuids and
803 * obd index for all these obduuids will be returned in
804 * param->obdindexes */
805 static int setup_obd_indexes(DIR *dir, struct find_param *param)
807 struct obd_uuid *uuids = NULL;
808 int obdcount = INIT_ALLOC_NUM_OSTS;
809 int ret, obd_valid = 0, obdnum, i;
811 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
812 sizeof(struct obd_uuid));
817 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
820 struct obd_uuid *uuids_temp;
822 if (ret == -EOVERFLOW) {
823 uuids_temp = realloc(uuids, obdcount *
824 sizeof(struct obd_uuid));
825 if (uuids_temp != NULL)
826 goto retry_get_uuids;
831 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
835 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
836 if (param->obdindexes == NULL)
839 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
840 for (i = 0; i <= obdcount; i++) {
841 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
842 (char *)&uuids[i]) == 0) {
843 param->obdindexes[obdnum] = i;
849 param->obdindexes[obdnum] = OBD_NOT_FOUND;
853 param->obdindex = OBD_NOT_FOUND;
855 param->obdindex = obd_valid;
857 param->got_uuids = 1;
862 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
863 struct lov_user_ost_data_v1 *objects,
864 char *path, int is_dir,
865 int obdindex, int quiet, int header, int body)
867 int i, obdstripe = 0;
869 if (obdindex != OBD_NOT_FOUND) {
870 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
871 if (obdindex == objects[i].l_ost_idx) {
872 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
878 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
882 /* if it's a directory */
884 if (obdstripe == 1) {
885 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
886 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
887 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
889 llapi_printf(LLAPI_MSG_NORMAL,
890 "stripe_count: %d stripe_size: %u "
891 "stripe_offset: %d%s%s\n",
892 lum->lmm_stripe_count == (__u16)-1 ? -1 :
893 lum->lmm_stripe_count,
894 lum->lmm_stripe_size,
895 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
896 lum->lmm_stripe_offset,
897 pool_name != NULL ? " pool: " : "",
898 pool_name != NULL ? pool_name : "");
903 if (header && (obdstripe == 1)) {
904 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
906 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
908 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
910 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
911 (int)lum->lmm_stripe_count);
912 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
913 lum->lmm_stripe_size);
914 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
916 if (pool_name != NULL)
917 llapi_printf(LLAPI_MSG_NORMAL,
918 "lmm_pool_name: %s\n", pool_name);
922 if ((!quiet) && (obdstripe == 1))
923 llapi_printf(LLAPI_MSG_NORMAL,
924 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
926 for (i = 0; i < lum->lmm_stripe_count; i++) {
927 int idx = objects[i].l_ost_idx;
928 long long oid = objects[i].l_object_id;
929 long long gr = objects[i].l_object_gr;
930 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
931 llapi_printf(LLAPI_MSG_NORMAL,
932 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
934 obdindex == idx ? " *" : "");
936 llapi_printf(LLAPI_MSG_NORMAL, "\n");
940 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
941 int is_dir, int obdindex, int quiet,
942 int header, int body)
944 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
945 int i, obdstripe = 0;
947 if (obdindex != OBD_NOT_FOUND) {
948 for (i = 0; i < lumj->lmm_stripe_count; i++) {
949 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
950 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
956 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
960 if (header && obdstripe == 1) {
961 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
963 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
964 lumj->lmm_object_gr);
965 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
966 lumj->lmm_object_id);
967 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
968 (int)lumj->lmm_stripe_count);
969 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
970 lumj->lmm_stripe_size);
971 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
973 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
974 lumj->lmm_extent_count);
978 unsigned long long start = -1, end = 0;
979 if (!quiet && obdstripe == 1)
980 llapi_printf(LLAPI_MSG_NORMAL,
981 "joined\tobdidx\t\t objid\t\tobjid\t\t "
982 "group\t\tstart\t\tend\n");
983 for (i = 0; i < lumj->lmm_stripe_count; i++) {
984 int idx = lumj->lmm_objects[i].l_ost_idx;
985 long long oid = lumj->lmm_objects[i].l_object_id;
986 long long gr = lumj->lmm_objects[i].l_object_gr;
987 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
988 llapi_printf(LLAPI_MSG_NORMAL,
989 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
991 obdindex == idx ? " *" : "");
992 if (start != lumj->lmm_objects[i].l_extent_start ||
993 end != lumj->lmm_objects[i].l_extent_end) {
994 start = lumj->lmm_objects[i].l_extent_start;
995 llapi_printf(LLAPI_MSG_NORMAL,"\t%14llu",start);
996 end = lumj->lmm_objects[i].l_extent_end;
997 if (end == (unsigned long long)-1)
998 llapi_printf(LLAPI_MSG_NORMAL,
1001 llapi_printf(LLAPI_MSG_NORMAL,
1004 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
1007 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1011 void llapi_lov_dump_user_lmm(struct find_param *param,
1012 char *path, int is_dir)
1014 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
1015 case LOV_USER_MAGIC_V1:
1016 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
1017 param->lmd->lmd_lmm.lmm_objects,
1019 param->obdindex, param->quiet,
1021 (param->verbose || !param->obduuid));
1023 case LOV_USER_MAGIC_JOIN:
1024 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
1025 param->obdindex, param->quiet,
1027 (param->verbose || !param->obduuid));
1029 case LOV_USER_MAGIC_V3: {
1030 char pool_name[LOV_MAXPOOLNAME + 1];
1031 struct lov_user_ost_data_v1 *objects;
1032 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1034 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1035 pool_name[LOV_MAXPOOLNAME] = '\0';
1036 objects = lmmv3->lmm_objects;
1037 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
1038 objects, path, is_dir,
1039 param->obdindex, param->quiet,
1041 (param->verbose || !param->obduuid));
1045 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1046 "(expecting one of %#x %#x %#x)\n",
1047 *(__u32 *)¶m->lmd->lmd_lmm,
1048 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_JOIN,
1054 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1060 fname = strrchr(path, '/');
1062 /* It should be a file (or other non-directory) */
1063 if (fname == NULL) {
1064 dname = (char *)malloc(2);
1068 fname = (char *)path;
1070 dname = (char *)malloc(fname - path + 1);
1073 strncpy(dname, path, fname - path);
1074 dname[fname - path] = '\0';
1078 if ((fd = open(dname, O_RDONLY)) == -1) {
1084 strcpy((char *)lum, fname);
1085 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1088 if (close(fd) == -1 && rc == 0)
1096 int llapi_file_lookup(int dirfd, const char *name)
1098 struct obd_ioctl_data data = { 0 };
1103 if (dirfd < 0 || name == NULL)
1106 data.ioc_version = OBD_IOCTL_VERSION;
1107 data.ioc_len = sizeof(data);
1108 data.ioc_inlbuf1 = (char *)name;
1109 data.ioc_inllen1 = strlen(name) + 1;
1111 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1113 llapi_err(LLAPI_MSG_ERROR,
1114 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1119 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1122 int llapi_mds_getfileinfo(char *path, DIR *parent,
1123 struct lov_user_mds_data *lmd)
1125 lstat_t *st = &lmd->lmd_st;
1126 char *fname = strrchr(path, '/');
1132 fname = (fname == NULL ? path : fname + 1);
1133 /* retrieve needed file info */
1134 strncpy((char *)lmd, fname,
1135 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
1136 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1139 if (errno == ENOTTY) {
1140 /* ioctl is not supported, it is not a lustre fs.
1141 * Do the regular lstat(2) instead. */
1142 ret = lstat_f(path, st);
1144 llapi_err(LLAPI_MSG_ERROR,
1145 "error: %s: lstat failed for %s",
1146 __FUNCTION__, path);
1149 } else if (errno == ENOENT) {
1150 llapi_err(LLAPI_MSG_WARN,
1151 "warning: %s: %s does not exist",
1152 __FUNCTION__, path);
1155 llapi_err(LLAPI_MSG_ERROR,
1156 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1157 __FUNCTION__, path);
1165 static DIR *opendir_parent(char *path)
1171 fname = strrchr(path, '/');
1173 return opendir(".");
1177 parent = opendir(path);
1182 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1183 semantic_func_t sem_init,
1184 semantic_func_t sem_fini, void *data,
1195 if (!d && errno != ENOTDIR) {
1196 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
1197 __FUNCTION__, path);
1199 } else if (!d && !parent) {
1200 /* ENOTDIR. Open the parent dir. */
1201 p = opendir_parent(path);
1203 GOTO(out, ret = -EINVAL);
1206 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1212 while ((dent = readdir64(d)) != NULL) {
1213 ((struct find_param *)data)->have_fileinfo = 0;
1215 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1218 /* Don't traverse .lustre directory */
1219 if (!(strcmp(dent->d_name, mdd_dot_lustre_name)))
1223 if ((len + dent->d_reclen + 2) > size) {
1224 llapi_err(LLAPI_MSG_ERROR,
1225 "error: %s: string buffer is too small",
1230 strcat(path, dent->d_name);
1232 if (dent->d_type == DT_UNKNOWN) {
1233 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1235 ret = llapi_mds_getfileinfo(path, d,
1236 ((struct find_param *)data)->lmd);
1238 ((struct find_param *)data)->have_fileinfo = 1;
1240 llapi_filetype_dir_table[st->st_mode &
1247 switch (dent->d_type) {
1249 llapi_err(LLAPI_MSG_ERROR,
1250 "error: %s: '%s' is UNKNOWN type %d",
1251 __FUNCTION__, dent->d_name, dent->d_type);
1254 ret = llapi_semantic_traverse(path, size, d, sem_init,
1255 sem_fini, data, dent);
1262 ret = sem_init(path, d, NULL, data, dent);
1266 if (sem_fini && ret == 0)
1267 sem_fini(path, d, NULL, data, dent);
1275 sem_fini(path, parent, d, data, de);
1284 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1285 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1287 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1288 * The table below gives the answers for the specified parameters (value and
1289 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1290 * --------------------------------------
1291 * 1 | file > limit; sign > 0 | -1 / -1 |
1292 * 2 | file = limit; sign > 0 | ? / 1 |
1293 * 3 | file < limit; sign > 0 | ? / 1 |
1294 * 4 | file > limit; sign = 0 | -1 / -1 |
1295 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1296 * 6 | file < limit; sign = 0 | ? / -1 |
1297 * 7 | file > limit; sign < 0 | 1 / 1 |
1298 * 8 | file = limit; sign < 0 | ? / -1 |
1299 * 9 | file < limit; sign < 0 | ? / -1 |
1300 * --------------------------------------
1301 * Note: 5th actually means that the value is within the interval
1302 * (limit - margin, limit]. */
1303 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1304 unsigned long long margin, int mds)
1312 if (file <= limit && file + margin > limit)
1314 if (file + margin <= limit)
1315 return mds ? 0 : -1;
1328 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1329 * Return -1 or 1 if file timestamp does not or does match the given criteria
1330 * correspondingly. Return 0 if the MDS time is being checked and there are
1331 * attributes on OSTs and it is not yet clear if the timespamp matches.
1333 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1334 * updated timestamps. */
1335 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1340 /* Check if file is accepted. */
1342 ret = find_value_cmp(st->st_atime, param->atime,
1343 param->asign, 24 * 60 * 60, mds);
1350 ret = find_value_cmp(st->st_mtime, param->mtime,
1351 param->msign, 24 * 60 * 60, mds);
1355 /* If the previous check matches, but this one is not yet clear,
1356 * we should return 0 to do an RPC on OSTs. */
1362 ret = find_value_cmp(st->st_ctime, param->ctime,
1363 param->csign, 24 * 60 * 60, mds);
1367 /* If the previous check matches, but this one is not yet clear,
1368 * we should return 0 to do an RPC on OSTs. */
1376 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1377 void *data, cfs_dirent_t *de)
1379 struct find_param *param = (struct find_param *)data;
1380 int decision = 1; /* 1 is accepted; -1 is rejected. */
1381 lstat_t *st = ¶m->lmd->lmd_st;
1383 int checked_type = 0;
1386 LASSERT(parent != NULL || dir != NULL);
1388 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1390 /* If a regular expression is presented, make the initial decision */
1391 if (param->pattern != NULL) {
1392 char *fname = strrchr(path, '/');
1393 fname = (fname == NULL ? path : fname + 1);
1394 ret = fnmatch(param->pattern, fname, 0);
1395 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1396 (ret == 0 && param->exclude_pattern))
1400 /* See if we can check the file type from the dirent. */
1401 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1402 de->d_type < DT_MAX) {
1404 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1405 if (param->exclude_type)
1408 if (!param->exclude_type)
1414 /* If a time or OST should be checked, the decision is not taken yet. */
1415 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1420 /* Request MDS for the stat info. */
1421 if (param->have_fileinfo == 0) {
1423 /* retrieve needed file info */
1424 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1425 (void *)param->lmd);
1427 char *fname = strrchr(path, '/');
1428 fname = (fname == NULL ? path : fname + 1);
1430 /* retrieve needed file info */
1431 strncpy((char *)param->lmd, fname, param->lumlen);
1432 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1433 (void *)param->lmd);
1438 if (errno == ENOTTY) {
1439 /* ioctl is not supported, it is not a lustre fs.
1440 * Do the regular lstat(2) instead. */
1442 ret = lstat_f(path, st);
1444 llapi_err(LLAPI_MSG_ERROR,
1445 "error: %s: lstat failed for %s",
1446 __FUNCTION__, path);
1449 } else if (errno == ENOENT) {
1450 llapi_err(LLAPI_MSG_WARN,
1451 "warning: %s: %s does not exist",
1452 __FUNCTION__, path);
1455 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1456 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1457 "IOC_MDC_GETFILEINFO", path);
1462 if (param->type && !checked_type) {
1463 if ((st->st_mode & S_IFMT) == param->type) {
1464 if (param->exclude_type)
1467 if (!param->exclude_type)
1473 if (param->obduuid) {
1474 if (lustre_fs && param->got_uuids &&
1475 param->st_dev != st->st_dev) {
1476 /* A lustre/lustre mount point is crossed. */
1477 param->got_uuids = 0;
1478 param->obds_printed = 0;
1479 param->obdindex = OBD_NOT_FOUND;
1482 if (lustre_fs && !param->got_uuids) {
1483 ret = setup_obd_indexes(dir ? dir : parent, param);
1487 param->st_dev = st->st_dev;
1488 } else if (!lustre_fs && param->got_uuids) {
1489 /* A lustre/non-lustre mount point is crossed. */
1490 param->got_uuids = 0;
1491 param->obdindex = OBD_NOT_FOUND;
1495 /* If an OBD UUID is specified but no one matches, skip this file. */
1496 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1499 /* If a OST UUID is given, and some OST matches, check it here. */
1500 if (param->obdindex != OBD_NOT_FOUND) {
1501 if (!S_ISREG(st->st_mode))
1504 /* Only those files should be accepted, which have a
1505 * stripe on the specified OST. */
1506 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1510 struct lov_user_ost_data_v1 *lmm_objects;
1512 if (param->lmd->lmd_lmm.lmm_magic ==
1513 LOV_USER_MAGIC_V3) {
1514 struct lov_user_md_v3 *lmmv3 =
1515 (void *)¶m->lmd->lmd_lmm;
1517 lmm_objects = lmmv3->lmm_objects;
1519 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1523 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1524 for (j = 0; j < param->num_obds; j++) {
1525 if (param->obdindexes[j] ==
1526 lmm_objects[i].l_ost_idx)
1531 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1536 if (param->check_uid) {
1537 if (st->st_uid == param->uid) {
1538 if (param->exclude_uid)
1541 if (!param->exclude_uid)
1546 if (param->check_gid) {
1547 if (st->st_gid == param->gid) {
1548 if (param->exclude_gid)
1551 if (!param->exclude_gid)
1556 if (param->check_pool) {
1557 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1559 /* empty requested pool is taken as no pool search => V1 */
1560 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1561 (param->poolname[0] == '\0')) ||
1562 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1563 (strncmp(lmmv3->lmm_pool_name,
1564 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1565 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1566 (strcmp(param->poolname, "*") == 0))) {
1567 if (param->exclude_pool)
1570 if (!param->exclude_pool)
1575 /* Check the time on mds. */
1579 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1580 param->lmd->lmd_lmm.lmm_stripe_count)
1582 decision = find_time_check(st, param, for_mds);
1586 /* If file still fits the request, ask ost for updated info.
1587 The regular stat is almost of the same speed as some new
1588 'glimpse-size-ioctl'. */
1589 if (!decision && S_ISREG(st->st_mode) &&
1590 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1591 if (param->obdindex != OBD_NOT_FOUND) {
1592 /* Check whether the obd is active or not, if it is
1593 * not active, just print the object affected by this
1596 struct obd_statfs stat_buf;
1597 struct obd_uuid uuid_buf;
1599 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1600 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1601 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1602 param->obdindex, &stat_buf,
1605 if (ret == -ENODATA || ret == -ENODEV
1608 llapi_printf(LLAPI_MSG_NORMAL,
1609 "obd_uuid: %s failed %s ",
1610 param->obduuid->uuid,
1616 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1617 (void *)param->lmd);
1618 } else if (parent) {
1619 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1620 (void *)param->lmd);
1624 if (errno == ENOENT) {
1625 llapi_err(LLAPI_MSG_ERROR,
1626 "warning: %s: %s does not exist",
1627 __FUNCTION__, path);
1630 llapi_err(LLAPI_MSG_ERROR,
1631 "%s: IOC_LOV_GETINFO on %s failed",
1632 __FUNCTION__, path);
1637 /* Check the time on osc. */
1638 decision = find_time_check(st, param, 0);
1644 decision = find_value_cmp(st->st_size, param->size,
1645 param->size_sign, param->size_units,
1649 if (decision != -1) {
1650 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1652 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1654 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1658 /* Do not get down anymore? */
1659 if (param->depth == param->maxdepth)
1666 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1669 struct find_param *param = (struct find_param *)data;
1674 int llapi_find(char *path, struct find_param *param)
1677 int ret, len = strlen(path);
1679 if (len > PATH_MAX) {
1680 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1681 __FUNCTION__, path);
1685 buf = (char *)malloc(PATH_MAX + 1);
1689 ret = common_param_init(param);
1697 strncpy(buf, path, PATH_MAX + 1);
1698 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1699 cb_common_fini, param, NULL);
1701 find_param_fini(param);
1703 return ret < 0 ? ret : 0;
1706 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1709 struct find_param *param = (struct find_param *)data;
1712 LASSERT(parent != NULL || d != NULL);
1715 if (!param->got_uuids) {
1716 ret = setup_obd_uuid(d ? d : parent, path, param);
1722 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1723 (void *)¶m->lmd->lmd_lmm);
1724 } else if (parent) {
1725 char *fname = strrchr(path, '/');
1726 fname = (fname == NULL ? path : fname + 1);
1728 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1729 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1730 (void *)¶m->lmd->lmd_lmm);
1734 if (errno == ENODATA) {
1735 if (!param->obduuid && !param->quiet)
1736 llapi_printf(LLAPI_MSG_NORMAL,
1737 "%s has no stripe info\n", path);
1739 } else if (errno == ENOTTY) {
1740 llapi_err(LLAPI_MSG_ERROR,
1741 "%s: '%s' not on a Lustre fs?",
1742 __FUNCTION__, path);
1743 } else if (errno == ENOENT) {
1744 llapi_err(LLAPI_MSG_WARN,
1745 "warning: %s: %s does not exist",
1746 __FUNCTION__, path);
1749 llapi_err(LLAPI_MSG_ERROR,
1750 "error: %s: %s failed for %s",
1751 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1752 "IOC_MDC_GETFILESTRIPE", path);
1758 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1760 /* Do not get down anymore? */
1761 if (param->depth == param->maxdepth)
1768 int llapi_getstripe(char *path, struct find_param *param)
1771 int ret = 0, len = strlen(path);
1773 if (len > PATH_MAX) {
1774 llapi_err(LLAPI_MSG_ERROR,
1775 "%s: Path name '%s' is too long",
1776 __FUNCTION__, path);
1780 buf = (char *)malloc(PATH_MAX + 1);
1784 ret = common_param_init(param);
1792 strncpy(buf, path, PATH_MAX + 1);
1793 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1794 cb_common_fini, param, NULL);
1795 find_param_fini(param);
1797 return ret < 0 ? ret : 0;
1800 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1801 struct obd_statfs *stat_buf,
1802 struct obd_uuid *uuid_buf)
1805 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1807 struct obd_ioctl_data data = { 0 };
1810 data.ioc_inlbuf1 = (char *)&type;
1811 data.ioc_inllen1 = sizeof(__u32);
1812 data.ioc_inlbuf2 = (char *)&index;
1813 data.ioc_inllen2 = sizeof(__u32);
1814 data.ioc_pbuf1 = (char *)stat_buf;
1815 data.ioc_plen1 = sizeof(struct obd_statfs);
1816 data.ioc_pbuf2 = (char *)uuid_buf;
1817 data.ioc_plen2 = sizeof(struct obd_uuid);
1819 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1820 llapi_err(LLAPI_MSG_ERROR,
1821 "llapi_obd_statfs: error packing ioctl data");
1825 fd = open(path, O_RDONLY);
1826 if (errno == EISDIR)
1827 fd = open(path, O_DIRECTORY | O_RDONLY);
1830 rc = errno ? -errno : -EBADF;
1831 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
1832 __FUNCTION__, path);
1835 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1837 rc = errno ? -errno : -EINVAL;
1843 #define MAX_STRING_SIZE 128
1844 #define DEVICES_LIST "/proc/fs/lustre/devices"
1846 int llapi_ping(char *obd_type, char *obd_name)
1848 char path[MAX_STRING_SIZE];
1852 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1853 obd_type, obd_name);
1855 fd = open(path, O_WRONLY);
1858 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1862 rc = write(fd, buf, 1);
1870 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
1872 char buf[MAX_STRING_SIZE];
1873 FILE *fp = fopen(DEVICES_LIST, "r");
1878 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1882 while (fgets(buf, sizeof(buf), fp) != NULL) {
1883 char *obd_type_name = NULL;
1884 char *obd_name = NULL;
1885 char *obd_uuid = NULL;
1887 struct obd_ioctl_data datal = { 0, };
1888 struct obd_statfs osfs_buffer;
1890 while(bufp[0] == ' ')
1893 for(i = 0; i < 3; i++) {
1894 obd_type_name = strsep(&bufp, " ");
1896 obd_name = strsep(&bufp, " ");
1897 obd_uuid = strsep(&bufp, " ");
1899 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1901 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1902 datal.ioc_plen1 = sizeof(osfs_buffer);
1904 for (i = 0; i < type_num; i++) {
1905 if (strcmp(obd_type_name, obd_type[i]) != 0)
1908 cb(obd_type_name, obd_name, obd_uuid, args);
1915 static void do_target_check(char *obd_type_name, char *obd_name,
1916 char *obd_uuid, void *args)
1920 rc = llapi_ping(obd_type_name, obd_name);
1921 if (rc == ENOTCONN) {
1922 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
1924 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1926 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1930 int llapi_target_check(int type_num, char **obd_type, char *dir)
1932 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1935 #undef MAX_STRING_SIZE
1937 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1939 char raw[OBD_MAX_IOCTL_BUFFER];
1940 char out[LLOG_CHUNK_SIZE];
1942 struct obd_ioctl_data data = { 0 };
1947 sprintf(key, "%s", keyword);
1948 memset(raw, 0, sizeof(raw));
1949 memset(out, 0, sizeof(out));
1950 data.ioc_inlbuf1 = key;
1951 data.ioc_inllen1 = strlen(key) + 1;
1953 data.ioc_inlbuf2 = node_name;
1954 data.ioc_inllen2 = strlen(node_name) + 1;
1956 data.ioc_pbuf1 = out;
1957 data.ioc_plen1 = sizeof(out);
1958 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1962 root = opendir(dir);
1965 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1969 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1971 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1973 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1979 /* Is this a lustre fs? */
1980 int llapi_is_lustre_mnttype(const char *type)
1982 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1985 /* Is this a lustre client fs? */
1986 int llapi_is_lustre_mnt(struct mntent *mnt)
1988 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1989 strstr(mnt->mnt_fsname, ":/") != NULL);
1992 int llapi_quotacheck(char *mnt, int check_type)
1997 root = opendir(mnt);
1999 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2003 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2009 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2015 root = opendir(mnt);
2017 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2022 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2026 if (poll_intvl < 30)
2034 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2039 root = opendir(mnt);
2041 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2045 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2051 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2054 struct find_param *param = (struct find_param *)data;
2058 LASSERT(parent != NULL || d != NULL);
2061 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2062 (void *)param->lmd);
2063 } else if (parent) {
2064 char *fname = strrchr(path, '/');
2065 fname = (fname == NULL ? path : fname + 1);
2067 strncpy((char *)param->lmd, fname, param->lumlen);
2068 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2069 (void *)param->lmd);
2075 if (errno == ENODATA) {
2076 if (!param->obduuid && !param->quiet)
2077 llapi_err(LLAPI_MSG_ERROR,
2078 "%s has no stripe info", path);
2080 } else if (errno == ENOENT) {
2081 llapi_err(LLAPI_MSG_ERROR,
2082 "warning: %s: %s does not exist",
2083 __FUNCTION__, path);
2085 } else if (errno != EISDIR) {
2087 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2088 d ? "LL_IOC_MDC_GETINFO" :
2089 "IOC_MDC_GETFILEINFO", path);
2094 st = ¶m->lmd->lmd_st;
2096 /* libc chown() will do extra check, and if the real owner is
2097 * the same as the ones to set, it won't fall into kernel, so
2098 * invoke syscall directly. */
2099 rc = syscall(SYS_chown, path, -1, -1);
2101 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2103 rc = chmod(path, st->st_mode);
2105 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2111 int llapi_quotachown(char *path, int flag)
2113 struct find_param param;
2115 int ret = 0, len = strlen(path);
2117 if (len > PATH_MAX) {
2118 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
2119 __FUNCTION__, path);
2123 buf = (char *)malloc(PATH_MAX + 1);
2127 memset(¶m, 0, sizeof(param));
2128 param.recursive = 1;
2132 ret = common_param_init(¶m);
2136 strncpy(buf, path, PATH_MAX + 1);
2137 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
2138 NULL, ¶m, NULL);
2140 find_param_fini(¶m);
2148 #include <sys/wait.h>
2152 static int rmtacl_notify(int ops)
2156 int found = 0, fd, rc;
2158 fp = setmntent(MOUNTED, "r");
2160 perror("setmntent");
2165 mnt = getmntent(fp);
2169 if (!llapi_is_lustre_mnt(mnt))
2172 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2178 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2190 static char *next_token(char *p, int div)
2196 while (*p && *p != ':' && !isspace(*p))
2199 while (*p == ':' || isspace(*p))
2202 return *p ? p : NULL;
2205 static int rmtacl_name2id(char *name, int is_user)
2210 if ((pw = getpwnam(name)) == NULL)
2213 return (int)(pw->pw_uid);
2217 if ((gr = getgrnam(name)) == NULL)
2220 return (int)(gr->gr_gid);
2224 static int isodigit(int c)
2226 return (c >= '0' && c <= '7') ? 1 : 0;
2230 * Whether the name is just digits string (uid/gid) already or not.
2235 static int str_is_id(char *str)
2242 if (*str == 'x' || *str == 'X') { /* for Hex. */
2243 if (!isxdigit(*(++str)))
2246 while (isxdigit(*(++str)));
2247 } else if (isodigit(*str)) { /* for Oct. */
2248 while (isodigit(*(++str)));
2250 } else if (isdigit(*str)) { /* for Dec. */
2251 while (isdigit(*(++str)));
2254 return (*str == 0) ? 1 : 0;
2264 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2266 static rmtacl_name_t rmtacl_namelist[] = {
2267 { RMTACL_OPTNAME("user:"), 1, 0 },
2268 { RMTACL_OPTNAME("group:"), 0, 0 },
2269 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2270 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2271 /* for --tabular option */
2272 { RMTACL_OPTNAME("user"), 1, 1 },
2273 { RMTACL_OPTNAME("group"), 0, 1 },
2277 static int rgetfacl_output(char *str)
2279 char *start = NULL, *end = NULL;
2280 int is_user = 0, n, id;
2287 for (rn = rmtacl_namelist; rn->name; rn++) {
2288 if(strncmp(str, rn->name, rn->length) == 0) {
2289 if (!rn->next_token)
2290 start = str + rn->length;
2292 start = next_token(str + rn->length, 0);
2293 is_user = rn->is_user;
2298 end = next_token(start, 1);
2299 if (end == NULL || start == end) {
2300 n = printf("%s", str);
2306 id = rmtacl_name2id(start, is_user);
2307 if (id == INVALID_ID) {
2308 if (str_is_id(start)) {
2310 n = printf("%s", str);
2313 } else if ((id == NOBODY_UID && is_user) ||
2314 (id == NOBODY_GID && !is_user)) {
2316 n = printf("%s", str);
2320 n = printf("%s%d%s", str, id, end);
2325 static int child_status(int status)
2327 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2330 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2343 if ((pid = fork()) < 0) {
2349 /* child process redirects its output. */
2352 if (dup2(fd[1], 1) < 0) {
2363 status = rmtacl_notify(ops);
2367 exit(execvp(argv[0], argv));
2370 /* the following is parent process */
2371 if ((fp = fdopen(fd[0], "r")) == NULL) {
2378 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2379 if (output_func(buf) < 0)
2380 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2386 if (waitpid(pid, &status, 0) < 0) {
2391 return child_status(status);
2394 int llapi_lsetfacl(int argc, char *argv[])
2396 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2399 int llapi_lgetfacl(int argc, char *argv[])
2401 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2404 int llapi_rsetfacl(int argc, char *argv[])
2406 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2409 int llapi_rgetfacl(int argc, char *argv[])
2411 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2414 int llapi_cp(int argc, char *argv[])
2418 rc = rmtacl_notify(RMT_RSETFACL);
2422 exit(execvp(argv[0], argv));
2425 int llapi_ls(int argc, char *argv[])
2429 rc = rmtacl_notify(RMT_LGETFACL);
2433 exit(execvp(argv[0], argv));
2436 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
2437 * format must have %s%s, buf must be > 16
2439 static int get_mdtname(const char *name, char *format, char *buf)
2441 char suffix[]="-MDT0000";
2442 int len = strlen(name);
2445 llapi_err(LLAPI_MSG_ERROR, "bad MDT name |%s|\n", name);
2449 if ((len > 8) && (strncmp(name + len - 8, "-MDT", 4) == 0))
2452 return sprintf(buf, format, name, suffix);
2456 /* Return a file descriptor to a readable changelog */
2457 int llapi_changelog_open(const char *mdtname, long long startrec)
2462 /* Use either the mdd changelog (preferred) or a client mdc changelog */
2463 if (get_mdtname(mdtname,
2464 "/proc/fs/lustre/md[cd]/%s%s{,-mdc-*}/changelog",
2467 rc = first_match(path, path);
2471 if ((fd = open(path, O_RDONLY)) < 0) {
2472 llapi_err(LLAPI_MSG_ERROR, "error: can't open |%s|\n", path);
2476 rc = lseek(fd, (off_t)startrec, SEEK_SET);
2478 llapi_err(LLAPI_MSG_ERROR, "can't seek rc=%d\n", rc);
2485 int llapi_changelog_clear(const char *mdtname, const char *idstr,
2488 struct ioc_changelog_clear data;
2490 int id, fd, index, rc;
2493 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2494 "can't purge negative records\n");
2498 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
2499 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
2500 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
2501 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2502 "expecting id of the form '"CHANGELOG_USER_PREFIX
2503 "<num>'; got '%s'\n", idstr);
2507 ptr = strstr(mdtname, "-MDT");
2510 index = strtol(ptr + 1, NULL, 10);
2513 fd = get_root_fd((char *)mdtname);
2515 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2516 "can't open fs root for '%s': %d", mdtname, fd);
2520 data.icc_mdtindex = index;
2522 data.icc_recno = endrec;
2523 rc = ioctl(fd, OBD_IOC_CHANGELOG_CLEAR, &data);
2525 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2531 static int dev_ioctl(struct obd_ioctl_data *data, int dev, int cmd)
2533 static char rawbuf[8192];
2534 static char *buf = rawbuf;
2537 data->ioc_dev = dev;
2538 memset(buf, 0, sizeof(rawbuf));
2540 if ((rc = obd_ioctl_pack(data, &buf, sizeof(rawbuf)))) {
2541 llapi_err(LLAPI_MSG_ERROR,
2542 "error: ioctl pack (%d) failed: rc %d", cmd, rc);
2546 rc = l_ioctl(OBD_DEV_ID, cmd, buf);
2548 /* ioctl returns -1 with errno set */
2553 if (obd_ioctl_unpack(data, buf, sizeof(rawbuf))) {
2554 llapi_err(LLAPI_MSG_ERROR,
2555 "error: invalid reply\n");
2561 static int dev_name2dev(char *name)
2563 struct obd_ioctl_data data;
2566 memset(&data, 0, sizeof(data));
2567 data.ioc_inllen1 = strlen(name) + 1;
2568 data.ioc_inlbuf1 = name;
2570 rc = dev_ioctl(&data, -1, OBD_IOC_NAME2DEV);
2574 return data.ioc_dev;
2578 static void do_get_mdcname(char *obd_type_name, char *obd_name,
2579 char *obd_uuid, void *name)
2581 if (strncmp(obd_name, (char *)name, strlen((char *)name)) == 0)
2582 strcpy((char *)name, obd_name);
2585 static int get_mdcdev(const char *mdtname)
2587 char name[MAX_OBD_NAME];
2588 char *type[] = { "mdc" };
2591 strcpy(name, mdtname);
2592 rc = llapi_target_iterate(1, type, (void *)name, do_get_mdcname);
2593 rc = rc < 0 ? : -rc;
2595 llapi_err(LLAPI_MSG_ERROR, "Device %s not found %d\n", name,rc);
2598 rc = dev_name2dev(name);
2600 llapi_err(LLAPI_MSG_ERROR, "Device %s not found %d\n", name,rc);
2604 int llapi_fid2path(char *device, char *fidstr, char *buf, int buflen,
2605 long long *recno, int *linkno)
2608 struct obd_ioctl_data data;
2612 while (*fidstr == '[')
2615 sscanf(fidstr, "0x%llx:0x%x:0x%x", &(fid.f_seq), &(fid.f_oid),
2617 if (!fid_is_sane(&fid)) {
2618 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2619 "bad FID format [%s], should be "DFID"\n",
2620 fidstr, (__u64)1, 2, 0);
2624 /* If the node is an MDS, issue the ioctl to the MDT . If not,
2625 issue it to the MDC. */
2626 rc = get_mdtname(device, "%s%s", mdtname);
2629 dev = dev_name2dev(mdtname);
2631 dev = get_mdcdev(mdtname);
2633 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2634 "can't find mdc for '%s'\n", mdtname);
2639 memset(&data, 0, sizeof(data));
2640 data.ioc_inlbuf1 = (char *)&fid;
2641 data.ioc_inllen1 = sizeof(fid);
2642 data.ioc_inlbuf2 = (char *)recno;
2643 data.ioc_inllen2 = sizeof(__u64);
2644 data.ioc_inlbuf3 = (char *)linkno;
2645 data.ioc_inllen3 = sizeof(int);
2646 data.ioc_plen1 = buflen;
2647 data.ioc_pbuf1 = buf;
2648 rc = dev_ioctl(&data, dev, OBD_IOC_FID2PATH);
2653 int llapi_path2fid(const char *path, unsigned long long *seq,
2654 unsigned long *oid, unsigned long *ver)
2659 fd = open(path, O_RDONLY);
2663 rc = ioctl(fd, LL_IOC_PATH2FID, &fid);
2664 *seq = fid_seq(&fid);
2665 *oid = fid_oid(&fid);
2666 *ver = fid_ver(&fid);