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 <liblustre.h>
70 #include <lnet/lnetctl.h>
72 #include <lustre_lib.h>
74 #include <lustre/liblustreapi.h>
76 static unsigned llapi_dir_filetype_table[] = {
85 #if defined(DT_DOOR) && defined(S_IFDOOR)
90 #if defined(DT_DOOR) && defined(S_IFDOOR)
91 static const int DT_MAX = DT_DOOR;
93 static const int DT_MAX = DT_SOCK;
96 static unsigned llapi_filetype_dir_table[] = {
105 #if defined(DT_DOOR) && defined(S_IFDOOR)
110 #if defined(DT_DOOR) && defined(S_IFDOOR)
111 static const int S_IFMAX = DT_DOOR;
113 static const int S_IFMAX = DT_SOCK;
116 /* liblustreapi message level */
117 static int llapi_msg_level = LLAPI_MSG_MAX;
119 void llapi_msg_set_level(int level)
121 /* ensure level is in the good range */
122 if (level < LLAPI_MSG_OFF)
123 llapi_msg_level = LLAPI_MSG_OFF;
124 else if (level > LLAPI_MSG_MAX)
125 llapi_msg_level = LLAPI_MSG_MAX;
127 llapi_msg_level = level;
130 void llapi_err(int level, char *fmt, ...)
133 int tmp_errno = abs(errno);
135 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
139 vfprintf(stderr, fmt, args);
142 if (level & LLAPI_MSG_NO_ERRNO)
143 fprintf(stderr, "\n");
145 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
148 #define llapi_err_noerrno(level, fmt, a...) \
149 llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
151 void llapi_printf(int level, char *fmt, ...)
155 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
159 vfprintf(stdout, fmt, args);
163 int parse_size(char *optarg, unsigned long long *size,
164 unsigned long long *size_units)
168 *size = strtoul(optarg, &end, 0);
171 if ((*end == 'b') && *(end+1) == '\0' &&
172 (*size & (~0ULL << (64 - 9))) == 0) {
174 *size_units = 1 << 9;
175 } else if ((*end == 'k' || *end == 'K') &&
176 *(end+1) == '\0' && (*size &
177 (~0ULL << (64 - 10))) == 0) {
179 *size_units = 1 << 10;
180 } else if ((*end == 'm' || *end == 'M') &&
181 *(end+1) == '\0' && (*size &
182 (~0ULL << (64 - 20))) == 0) {
184 *size_units = 1 << 20;
185 } else if ((*end == 'g' || *end == 'G') &&
186 *(end+1) == '\0' && (*size &
187 (~0ULL << (64 - 30))) == 0) {
189 *size_units = 1 << 30;
190 } else if ((*end == 't' || *end == 'T') &&
191 *(end+1) == '\0' && (*size &
192 (~0ULL << (64 - 40))) == 0) {
194 *size_units = 1ULL << 40;
195 } else if ((*end == 'p' || *end == 'P') &&
196 *(end+1) == '\0' && (*size &
197 (~0ULL << (64 - 50))) == 0) {
199 *size_units = 1ULL << 50;
200 } else if ((*end == 'e' || *end == 'E') &&
201 *(end+1) == '\0' && (*size &
202 (~0ULL << (64 - 60))) == 0) {
204 *size_units = 1ULL << 60;
213 int llapi_stripe_limit_check(unsigned long stripe_size, int stripe_offset,
214 int stripe_count, int stripe_pattern)
218 /* 64 KB is the largest common page size I'm aware of (on ia64), but
219 * check the local page size just in case. */
220 page_size = LOV_MIN_STRIPE_SIZE;
221 if (getpagesize() > page_size) {
222 page_size = getpagesize();
223 llapi_err_noerrno(LLAPI_MSG_WARN,
224 "warning: your page size (%u) is "
225 "larger than expected (%u)", page_size,
226 LOV_MIN_STRIPE_SIZE);
228 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
229 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
230 "must be an even multiple of %d bytes",
231 stripe_size, page_size);
234 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
236 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d",
240 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
242 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d",
246 if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
248 llapi_err(LLAPI_MSG_ERROR, "error: stripe_size %lu * "
249 "stripe_count %u exceeds 4GB", stripe_size,
256 static int poolpath(char *fsname, char *pathname, char *pool_pathname);
258 int llapi_file_open_pool(const char *name, int flags, int mode,
259 unsigned long stripe_size, int stripe_offset,
260 int stripe_count, int stripe_pattern, char *pool_name)
262 struct lov_user_md_v3 lum = { 0 };
265 char fsname[MAX_OBD_NAME + 1], *ptr;
267 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
268 if (fd < 0 && errno == EISDIR) {
269 fd = open(name, O_DIRECTORY | O_RDONLY);
275 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
279 if ((rc = llapi_stripe_limit_check(stripe_size, stripe_offset,
280 stripe_count, stripe_pattern)) != 0){
285 /* Initialize IOCTL striping pattern structure */
286 lum.lmm_magic = LOV_USER_MAGIC_V3;
287 lum.lmm_pattern = stripe_pattern;
288 lum.lmm_stripe_size = stripe_size;
289 lum.lmm_stripe_count = stripe_count;
290 lum.lmm_stripe_offset = stripe_offset;
292 /* in case user give the full pool name <fsname>.<poolname>, skip
294 if (pool_name != NULL) {
295 ptr = strchr(pool_name, '.');
297 strncpy(fsname, pool_name, ptr - pool_name);
298 fsname[ptr - pool_name] = '\0';
299 /* if fsname matches a filesystem skip it
300 * if not keep the poolname as is */
301 if (poolpath(fsname, NULL, NULL) == 0)
304 strncpy(lum.lmm_pool_name, pool_name, MAXPOOLNAME);
306 /* If no pool is specified at all, use V1 request */
307 lum.lmm_magic = LOV_USER_MAGIC_V1;
310 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
311 char *errmsg = "stripe already set";
313 if (errno != EEXIST && errno != EALREADY)
314 errmsg = strerror(errno);
316 llapi_err_noerrno(LLAPI_MSG_ERROR,
317 "error on ioctl "LPX64" for '%s' (%d): %s",
318 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
329 int llapi_file_open(const char *name, int flags, int mode,
330 unsigned long stripe_size, int stripe_offset,
331 int stripe_count, int stripe_pattern)
333 return llapi_file_open_pool(name, flags, mode, stripe_size,
334 stripe_offset, stripe_count,
335 stripe_pattern, NULL);
338 int llapi_file_create(const char *name, unsigned long stripe_size,
339 int stripe_offset, int stripe_count, int stripe_pattern)
343 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
344 stripe_offset, stripe_count, stripe_pattern,
353 int llapi_file_create_pool(const char *name, unsigned long stripe_size,
354 int stripe_offset, int stripe_count,
355 int stripe_pattern, char *pool_name)
359 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
360 stripe_offset, stripe_count, stripe_pattern,
370 static int print_pool_members(char *fs, char *pool_dir, char *pool_file)
372 char path[PATH_MAX + 1];
376 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fs, pool_file);
377 sprintf(path, "%s/%s", pool_dir, pool_file);
378 if ((fd = fopen(path, "r")) == NULL) {
379 llapi_err(LLAPI_MSG_ERROR, "Cannot open %s\n", path);
382 while (fgets(buf, sizeof(buf), fd) != NULL)
383 llapi_printf(LLAPI_MSG_NORMAL, buf);
390 * search lustre fsname from pathname
393 static int search_fsname(char *pathname, char *fsname)
397 struct mntent *mnt = NULL;
399 /* get the mount point */
400 fp = setmntent(MOUNTED, "r");
402 llapi_err(LLAPI_MSG_ERROR,
403 "setmntent(%s) failed: %s:", MOUNTED,
408 while ((feof(fp) == 0) && ferror(fp) == 0) {
409 if (llapi_is_lustre_mnt(mnt)) {
410 /* search by pathname */
411 if (strncmp(mnt->mnt_dir, pathname,
412 strlen(mnt->mnt_dir)) == 0) {
413 ptr = strchr(mnt->mnt_fsname, '/');
429 * find the pool directory path under /proc
430 * (can be also used to test if a fsname is known)
432 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
436 char pattern[PATH_MAX + 1];
437 char buffer[PATH_MAX];
439 if (fsname == NULL) {
440 rc = search_fsname(pathname, buffer);
444 strcpy(pathname, fsname);
447 snprintf(pattern, PATH_MAX,
448 "/proc/fs/lustre/lov/%s-*/pools",
450 rc = glob(pattern, GLOB_BRACE, NULL, &glob_info);
454 if (glob_info.gl_pathc == 0) {
455 globfree(&glob_info);
459 /* in fsname test mode, pool_pathname is NULL */
460 if (pool_pathname != NULL)
461 strcpy(pool_pathname, glob_info.gl_pathv[0]);
466 int llapi_poollist(char *name)
470 char rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
474 /* is name a pathname ? */
475 ptr = strchr(name, '/');
477 /* only absolute pathname is supported */
480 if (!realpath(name, rname)) {
482 llapi_err(LLAPI_MSG_ERROR,
483 "llapi_poollist: invalid path '%s'",
488 rc = poolpath(NULL, rname, pathname);
491 llapi_err(LLAPI_MSG_ERROR,
492 "llapi_poollist: '%s' is not"
493 " a Lustre filesystem",
500 /* name is FSNAME[.POOLNAME] */
502 poolname = strchr(name, '.');
503 if (poolname != NULL) {
507 rc = poolpath(fsname, NULL, pathname);
510 llapi_err(LLAPI_MSG_ERROR,
511 "llapi_poollist: Lustre filesystem '%s'"
518 llapi_err(LLAPI_MSG_ERROR,
519 "llapi_poollist: Lustre filesystem '%s' not found",
524 if (poolname != NULL) {
525 rc = print_pool_members(fsname, pathname, poolname);
532 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
533 if ((dir = opendir(pathname)) == NULL) {
536 while ((pool = readdir(dir)) != NULL) {
537 if (!((pool->d_name[0] == '.') &&
538 (pool->d_name[1] == '\0')) &&
539 !((pool->d_name[0] == '.') &&
540 (pool->d_name[1] == '.') &&
541 (pool->d_name[2] == '\0')))
542 llapi_printf(LLAPI_MSG_NORMAL, " %s.%s\n", fsname, pool->d_name);
549 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
550 void *data, cfs_dirent_t *de);
552 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
553 #define OBD_NOT_FOUND (-1)
555 static int common_param_init(struct find_param *param)
557 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
558 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
559 llapi_err(LLAPI_MSG_ERROR,
560 "error: allocation of %d bytes for ioctl",
561 sizeof(lstat_t) + param->lumlen);
565 param->got_uuids = 0;
566 param->obdindexes = NULL;
567 param->obdindex = OBD_NOT_FOUND;
571 static void find_param_fini(struct find_param *param)
573 if (param->obdindexes)
574 free(param->obdindexes);
580 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
582 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
585 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
590 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
594 fd = open(path, O_RDONLY);
597 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
601 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
609 * If uuidp is NULL, return the number of available obd uuids.
610 * If uuidp is non-NULL, then it will return the uuids of the obds. If
611 * there are more OSTs then allocated to uuidp, then an error is returned with
612 * the ost_count set to number of available obd uuids.
614 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
616 struct obd_uuid lov_name;
619 int rc = 0, index = 0;
621 /* Get the lov name */
622 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
626 /* Now get the ost uuids from /proc */
627 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
629 fp = fopen(buf, "r");
632 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
636 while (fgets(buf, sizeof(buf), fp) != NULL) {
637 if (uuidp && (index < *ost_count)) {
638 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
646 if (uuidp && (index >= *ost_count))
653 /* Here, param->obduuid points to a single obduuid, the index of which is
654 * returned in param->obdindex */
655 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
657 struct obd_uuid lov_uuid;
658 char uuid[sizeof(struct obd_uuid)];
663 /* Get the lov name */
664 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
666 if (errno != ENOTTY) {
668 llapi_err(LLAPI_MSG_ERROR,
669 "error: can't get lov name: %s", dname);
676 param->got_uuids = 1;
678 /* Now get the ost uuids from /proc */
679 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
681 fp = fopen(buf, "r");
684 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
688 if (!param->obduuid && !param->quiet && !param->obds_printed)
689 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
691 while (fgets(buf, sizeof(buf), fp) != NULL) {
692 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
695 if (param->obduuid) {
696 if (strncmp(param->obduuid->uuid, uuid,
697 sizeof(uuid)) == 0) {
698 param->obdindex = index;
701 } else if (!param->quiet && !param->obds_printed) {
702 /* Print everything */
703 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
706 param->obds_printed = 1;
710 if (!param->quiet && param->obduuid &&
711 (param->obdindex == OBD_NOT_FOUND)) {
712 llapi_err_noerrno(LLAPI_MSG_ERROR,
713 "error: %s: unknown obduuid: %s",
714 __FUNCTION__, param->obduuid->uuid);
721 /* In this case, param->obduuid will be an array of obduuids and
722 * obd index for all these obduuids will be returned in
723 * param->obdindexes */
724 static int setup_obd_indexes(DIR *dir, struct find_param *param)
726 struct obd_uuid *uuids = NULL;
727 int obdcount = INIT_ALLOC_NUM_OSTS;
728 int ret, obd_valid = 0, obdnum, i;
730 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
731 sizeof(struct obd_uuid));
736 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
739 struct obd_uuid *uuids_temp;
741 if (ret == -EOVERFLOW) {
742 uuids_temp = realloc(uuids, obdcount *
743 sizeof(struct obd_uuid));
744 if (uuids_temp != NULL)
745 goto retry_get_uuids;
750 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
754 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
755 if (param->obdindexes == NULL)
758 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
759 for (i = 0; i <= obdcount; i++) {
760 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
761 (char *)&uuids[i]) == 0) {
762 param->obdindexes[obdnum] = i;
768 param->obdindexes[obdnum] = OBD_NOT_FOUND;
772 param->obdindex = OBD_NOT_FOUND;
774 param->obdindex = obd_valid;
776 param->got_uuids = 1;
781 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
782 struct lov_user_ost_data_v1 *objects,
783 char *path, int is_dir,
784 int obdindex, int quiet, int header, int body)
786 int i, obdstripe = 0;
788 if (obdindex != OBD_NOT_FOUND) {
789 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
790 if (obdindex == objects[i].l_ost_idx) {
791 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
797 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
801 /* if it's a directory */
803 if (obdstripe == 1) {
804 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
805 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
806 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
808 llapi_printf(LLAPI_MSG_NORMAL,
809 "stripe_count: %d stripe_size: %u "
810 "stripe_offset: %d%s%s\n",
811 lum->lmm_stripe_count == (__u16)-1 ? -1 :
812 lum->lmm_stripe_count,
813 lum->lmm_stripe_size,
814 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
815 lum->lmm_stripe_offset,
816 pool_name != NULL ? " pool: " : "",
817 pool_name != NULL ? pool_name : "");
822 if (header && (obdstripe == 1)) {
823 llapi_printf(LLAPI_MSG_NORMAL,
824 "lmm_magic: 0x%08X\n", lum->lmm_magic);
825 llapi_printf(LLAPI_MSG_NORMAL,
826 "lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
827 llapi_printf(LLAPI_MSG_NORMAL,
828 "lmm_object_id: "LPX64"\n", lum->lmm_object_id);
829 llapi_printf(LLAPI_MSG_NORMAL,
830 "lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
831 llapi_printf(LLAPI_MSG_NORMAL,
832 "lmm_stripe_size: %u\n", lum->lmm_stripe_size);
833 llapi_printf(LLAPI_MSG_NORMAL,
834 "lmm_stripe_pattern: %x\n", lum->lmm_pattern);
835 if (pool_name != NULL)
836 llapi_printf(LLAPI_MSG_NORMAL,
837 "lmm_pool_name: %s\n", pool_name);
841 if ((!quiet) && (obdstripe == 1))
842 llapi_printf(LLAPI_MSG_NORMAL,
843 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
845 for (i = 0; i < lum->lmm_stripe_count; i++) {
846 int idx = objects[i].l_ost_idx;
847 long long oid = objects[i].l_object_id;
848 long long gr = objects[i].l_object_gr;
849 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
850 llapi_printf(LLAPI_MSG_NORMAL,
851 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
853 obdindex == idx ? " *" : "");
855 llapi_printf(LLAPI_MSG_NORMAL, "\n");
859 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
860 int is_dir, int obdindex, int quiet,
861 int header, int body)
863 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
864 int i, obdstripe = 0;
866 if (obdindex != OBD_NOT_FOUND) {
867 for (i = 0; i < lumj->lmm_stripe_count; i++) {
868 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
869 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
875 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
879 if (header && obdstripe == 1) {
880 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
882 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
883 lumj->lmm_object_gr);
884 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
885 lumj->lmm_object_id);
886 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
887 (int)lumj->lmm_stripe_count);
888 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
889 lumj->lmm_stripe_size);
890 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
892 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
893 lumj->lmm_extent_count);
897 unsigned long long start = -1, end = 0;
898 if (!quiet && obdstripe == 1)
899 llapi_printf(LLAPI_MSG_NORMAL,
900 "joined\tobdidx\t\t objid\t\tobjid\t\t group"
901 "\t\tstart\t\tend\n");
902 for (i = 0; i < lumj->lmm_stripe_count; i++) {
903 int idx = lumj->lmm_objects[i].l_ost_idx;
904 long long oid = lumj->lmm_objects[i].l_object_id;
905 long long gr = lumj->lmm_objects[i].l_object_gr;
906 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
907 llapi_printf(LLAPI_MSG_NORMAL,
908 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
910 obdindex == idx ? " *" : "");
911 if (start != lumj->lmm_objects[i].l_extent_start ||
912 end != lumj->lmm_objects[i].l_extent_end) {
913 start = lumj->lmm_objects[i].l_extent_start;
914 llapi_printf(LLAPI_MSG_NORMAL, "\t%14llu", start);
915 end = lumj->lmm_objects[i].l_extent_end;
916 if (end == (unsigned long long)-1)
917 llapi_printf(LLAPI_MSG_NORMAL,
920 llapi_printf(LLAPI_MSG_NORMAL,
923 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
926 llapi_printf(LLAPI_MSG_NORMAL, "\n");
930 void llapi_lov_dump_user_lmm(struct find_param *param,
931 char *path, int is_dir)
933 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
934 case LOV_USER_MAGIC_V1:
935 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
936 param->lmd->lmd_lmm.lmm_objects,
938 param->obdindex, param->quiet,
940 (param->verbose || !param->obduuid));
942 case LOV_USER_MAGIC_JOIN:
943 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
944 param->obdindex, param->quiet,
946 (param->verbose || !param->obduuid));
948 case LOV_USER_MAGIC_V3: {
949 char pool_name[MAXPOOLNAME + 1];
950 struct lov_user_ost_data_v1 *objects;
953 ((struct lov_user_md_v3 *)(¶m->lmd->lmd_lmm))->lmm_pool_name,
955 pool_name[MAXPOOLNAME] = '\0';
956 objects = ((struct lov_user_md_v3 *)(¶m->lmd->lmd_lmm))->lmm_objects;
957 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
958 objects, path, is_dir,
959 param->obdindex, param->quiet,
961 (param->verbose || !param->obduuid));
965 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
966 "(expecting one of %#x %#x %#x)\n",
967 *(__u32 *)¶m->lmd->lmd_lmm,
968 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_JOIN,
974 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
980 fname = strrchr(path, '/');
982 /* It should be a file (or other non-directory) */
984 dname = (char *)malloc(2);
988 fname = (char *)path;
990 dname = (char *)malloc(fname - path + 1);
993 strncpy(dname, path, fname - path);
994 dname[fname - path] = '\0';
998 if ((fd = open(dname, O_RDONLY)) == -1) {
1004 strcpy((char *)lum, fname);
1005 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1008 if (close(fd) == -1 && rc == 0)
1016 int llapi_file_lookup(int dirfd, const char *name)
1018 struct obd_ioctl_data data = { 0 };
1023 if (dirfd < 0 || name == NULL)
1026 data.ioc_version = OBD_IOCTL_VERSION;
1027 data.ioc_len = sizeof(data);
1028 data.ioc_inlbuf1 = (char *)name;
1029 data.ioc_inllen1 = strlen(name) + 1;
1031 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1033 llapi_err(LLAPI_MSG_ERROR,
1034 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1039 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1042 int llapi_mds_getfileinfo(char *path, DIR *parent,
1043 struct lov_user_mds_data *lmd)
1045 lstat_t *st = &lmd->lmd_st;
1046 char *fname = strrchr(path, '/');
1052 fname = (fname == NULL ? path : fname + 1);
1053 /* retrieve needed file info */
1054 strncpy((char *)lmd, fname,
1055 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
1056 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1059 if (errno == ENOTTY) {
1060 /* ioctl is not supported, it is not a lustre fs.
1061 * Do the regular lstat(2) instead. */
1062 ret = lstat_f(path, st);
1064 llapi_err(LLAPI_MSG_ERROR,
1065 "error: %s: lstat failed for %s",
1066 __FUNCTION__, path);
1069 } else if (errno == ENOENT) {
1070 llapi_err(LLAPI_MSG_WARN,
1071 "warning: %s: %s does not exist",
1072 __FUNCTION__, path);
1075 llapi_err(LLAPI_MSG_ERROR,
1076 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1077 __FUNCTION__, path);
1085 static DIR *opendir_parent(char *path)
1091 fname = strrchr(path, '/');
1093 return opendir(".");
1097 parent = opendir(path);
1102 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1103 semantic_func_t sem_init,
1104 semantic_func_t sem_fini, void *data,
1115 if (!d && errno != ENOTDIR) {
1116 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
1117 __FUNCTION__, path);
1119 } else if (!d && !parent) {
1120 /* ENOTDIR. Open the parent dir. */
1121 p = opendir_parent(path);
1123 GOTO(out, ret = -EINVAL);
1126 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1132 while ((dent = readdir64(d)) != NULL) {
1133 ((struct find_param *)data)->have_fileinfo = 0;
1135 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1139 if ((len + dent->d_reclen + 2) > size) {
1140 llapi_err(LLAPI_MSG_ERROR,
1141 "error: %s: string buffer is too small",
1146 strcat(path, dent->d_name);
1148 if (dent->d_type == DT_UNKNOWN) {
1149 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1151 ret = llapi_mds_getfileinfo(path, d,
1152 ((struct find_param *)data)->lmd);
1154 ((struct find_param *)data)->have_fileinfo = 1;
1155 dent->d_type = llapi_filetype_dir_table[st->st_mode &
1162 switch (dent->d_type) {
1164 llapi_err(LLAPI_MSG_ERROR,
1165 "error: %s: '%s' is UNKNOWN type %d",
1166 __FUNCTION__, dent->d_name, dent->d_type);
1169 ret = llapi_semantic_traverse(path, size, d, sem_init,
1170 sem_fini, data, dent);
1177 ret = sem_init(path, d, NULL, data, dent);
1181 if (sem_fini && ret == 0)
1182 sem_fini(path, d, NULL, data, dent);
1190 sem_fini(path, parent, d, data, de);
1199 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1200 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1202 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1203 * The table bolow gives the answers for the specified parameters (value and
1204 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1205 * --------------------------------------
1206 * 1 | file > limit; sign > 0 | -1 / -1 |
1207 * 2 | file = limit; sign > 0 | ? / 1 |
1208 * 3 | file < limit; sign > 0 | ? / 1 |
1209 * 4 | file > limit; sign = 0 | -1 / -1 |
1210 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1211 * 6 | file < limit; sign = 0 | ? / -1 |
1212 * 7 | file > limit; sign < 0 | 1 / 1 |
1213 * 8 | file = limit; sign < 0 | ? / -1 |
1214 * 9 | file < limit; sign < 0 | ? / -1 |
1215 * --------------------------------------
1216 * Note: 5th actually means that the value is within the interval
1217 * (limit - margin, limit]. */
1218 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1219 unsigned long long margin, int mds)
1227 if (file <= limit && file + margin > limit)
1229 if (file + margin <= limit)
1230 return mds ? 0 : -1;
1243 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1244 * Return -1 or 1 if file timestamp does not or does match the given criteria
1245 * correspondingly. Return 0 if the MDS time is being checked and there are
1246 * attributes on OSTs and it is not yet clear if the timespamp matches.
1248 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1249 * updated timestamps. */
1250 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1255 /* Check if file is accepted. */
1257 ret = find_value_cmp(st->st_atime, param->atime,
1258 param->asign, 24 * 60 * 60, mds);
1265 ret = find_value_cmp(st->st_mtime, param->mtime,
1266 param->msign, 24 * 60 * 60, mds);
1270 /* If the previous check matches, but this one is not yet clear,
1271 * we should return 0 to do an RPC on OSTs. */
1277 ret = find_value_cmp(st->st_ctime, param->ctime,
1278 param->csign, 24 * 60 * 60, mds);
1282 /* If the previous check matches, but this one is not yet clear,
1283 * we should return 0 to do an RPC on OSTs. */
1291 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1292 void *data, cfs_dirent_t *de)
1294 struct find_param *param = (struct find_param *)data;
1295 int decision = 1; /* 1 is accepted; -1 is rejected. */
1296 lstat_t *st = ¶m->lmd->lmd_st;
1298 int checked_type = 0;
1301 LASSERT(parent != NULL || dir != NULL);
1303 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1305 /* If a regular expression is presented, make the initial decision */
1306 if (param->pattern != NULL) {
1307 char *fname = strrchr(path, '/');
1308 fname = (fname == NULL ? path : fname + 1);
1309 ret = fnmatch(param->pattern, fname, 0);
1310 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1311 (ret == 0 && param->exclude_pattern))
1315 /* See if we can check the file type from the dirent. */
1316 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1317 de->d_type < DT_MAX) {
1319 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1320 if (param->exclude_type)
1323 if (!param->exclude_type)
1329 /* If a time or OST should be checked, the decision is not taken yet. */
1330 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1335 /* Request MDS for the stat info. */
1336 if (param->have_fileinfo == 0) {
1338 /* retrieve needed file info */
1339 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1340 (void *)param->lmd);
1342 char *fname = strrchr(path, '/');
1343 fname = (fname == NULL ? path : fname + 1);
1345 /* retrieve needed file info */
1346 strncpy((char *)param->lmd, fname, param->lumlen);
1347 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1348 (void *)param->lmd);
1353 if (errno == ENOTTY) {
1354 /* ioctl is not supported, it is not a lustre fs.
1355 * Do the regular lstat(2) instead. */
1357 ret = lstat_f(path, st);
1359 llapi_err(LLAPI_MSG_ERROR,
1360 "error: %s: lstat failed for %s",
1361 __FUNCTION__, path);
1364 } else if (errno == ENOENT) {
1365 llapi_err(LLAPI_MSG_WARN,
1366 "warning: %s: %s does not exist",
1367 __FUNCTION__, path);
1370 llapi_err(LLAPI_MSG_ERROR, "error: %s: %s failed for %s",
1371 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1372 "IOC_MDC_GETFILEINFO", path);
1377 if (param->type && !checked_type) {
1378 if ((st->st_mode & S_IFMT) == param->type) {
1379 if (param->exclude_type)
1382 if (!param->exclude_type)
1388 if (param->obduuid) {
1389 if (lustre_fs && param->got_uuids &&
1390 param->st_dev != st->st_dev) {
1391 /* A lustre/lustre mount point is crossed. */
1392 param->got_uuids = 0;
1393 param->obds_printed = 0;
1394 param->obdindex = OBD_NOT_FOUND;
1397 if (lustre_fs && !param->got_uuids) {
1398 ret = setup_obd_indexes(dir ? dir : parent, param);
1402 param->st_dev = st->st_dev;
1403 } else if (!lustre_fs && param->got_uuids) {
1404 /* A lustre/non-lustre mount point is crossed. */
1405 param->got_uuids = 0;
1406 param->obdindex = OBD_NOT_FOUND;
1410 /* If an OBD UUID is specified but no one matches, skip this file. */
1411 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1414 /* If a OST UUID is given, and some OST matches, check it here. */
1415 if (param->obdindex != OBD_NOT_FOUND) {
1416 if (!S_ISREG(st->st_mode))
1419 /* Only those files should be accepted, which have a
1420 * stripe on the specified OST. */
1421 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1425 struct lov_user_ost_data_v1 *lmm_objects;
1427 if (param->lmd->lmd_lmm.lmm_magic ==
1428 LOV_USER_MAGIC_V3) {
1430 ((struct lov_user_md_v3 *)(&(param->lmd->lmd_lmm)))->lmm_objects;
1432 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1436 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1437 for (j = 0; j < param->num_obds; j++) {
1438 if (param->obdindexes[j] ==
1439 lmm_objects[i].l_ost_idx)
1444 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1449 if (param->check_uid) {
1450 if (st->st_uid == param->uid) {
1451 if (param->exclude_uid)
1454 if (!param->exclude_uid)
1459 if (param->check_gid) {
1460 if (st->st_gid == param->gid) {
1461 if (param->exclude_gid)
1464 if (!param->exclude_gid)
1469 if (param->check_pool) {
1470 /* empty requested pool is taken as no pool search => V1 */
1471 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1472 (param->poolname[0] == '\0')) ||
1473 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1474 (strncmp(((struct lov_user_md_v3 *)(&(param->lmd->lmd_lmm)))->lmm_pool_name,
1475 param->poolname, MAXPOOLNAME) == 0)) ||
1476 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1477 (strcmp(param->poolname, "*") == 0))) {
1478 if (param->exclude_pool)
1481 if (!param->exclude_pool)
1486 /* Check the time on mds. */
1490 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1491 param->lmd->lmd_lmm.lmm_stripe_count)
1493 decision = find_time_check(st, param, for_mds);
1497 /* If file still fits the request, ask osd for updated info.
1498 The regulat stat is almost of the same speed as some new
1499 'glimpse-size-ioctl'. */
1500 if (!decision && S_ISREG(st->st_mode) &&
1501 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1502 if (param->obdindex != OBD_NOT_FOUND) {
1503 /* Check whether the obd is active or not, if it is
1504 * not active, just print the object affected by this
1507 struct obd_statfs stat_buf;
1508 struct obd_uuid uuid_buf;
1510 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1511 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1512 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1513 param->obdindex, &stat_buf,
1516 if (ret == -ENODATA || ret == -ENODEV
1519 llapi_printf(LLAPI_MSG_NORMAL,
1520 "obd_uuid: %s failed %s ",
1521 param->obduuid->uuid,
1527 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1528 (void *)param->lmd);
1529 } else if (parent) {
1530 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1531 (void *)param->lmd);
1535 if (errno == ENOENT) {
1536 llapi_err(LLAPI_MSG_ERROR,
1537 "warning: %s: %s does not exist",
1538 __FUNCTION__, path);
1541 llapi_err(LLAPI_MSG_ERROR,
1542 "%s: IOC_LOV_GETINFO on %s failed",
1543 __FUNCTION__, path);
1548 /* Check the time on osc. */
1549 decision = find_time_check(st, param, 0);
1555 decision = find_value_cmp(st->st_size, param->size,
1556 param->size_sign, param->size_units,
1560 if (decision != -1) {
1561 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1563 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1565 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1569 /* Do not get down anymore? */
1570 if (param->depth == param->maxdepth)
1577 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1580 struct find_param *param = (struct find_param *)data;
1585 int llapi_find(char *path, struct find_param *param)
1588 int ret, len = strlen(path);
1590 if (len > PATH_MAX) {
1591 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1592 __FUNCTION__, path);
1596 buf = (char *)malloc(PATH_MAX + 1);
1600 ret = common_param_init(param);
1608 strncpy(buf, path, PATH_MAX + 1);
1609 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1610 cb_common_fini, param, NULL);
1612 find_param_fini(param);
1614 return ret < 0 ? ret : 0;
1617 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1620 struct find_param *param = (struct find_param *)data;
1623 LASSERT(parent != NULL || d != NULL);
1626 if (!param->got_uuids) {
1627 ret = setup_obd_uuid(d ? d : parent, path, param);
1633 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1634 (void *)¶m->lmd->lmd_lmm);
1635 } else if (parent) {
1636 char *fname = strrchr(path, '/');
1637 fname = (fname == NULL ? path : fname + 1);
1639 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1640 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1641 (void *)¶m->lmd->lmd_lmm);
1645 if (errno == ENODATA) {
1646 if (!param->obduuid && !param->quiet)
1647 llapi_printf(LLAPI_MSG_NORMAL,
1648 "%s has no stripe info\n", path);
1650 } else if (errno == ENOTTY) {
1651 llapi_err(LLAPI_MSG_ERROR,
1652 "%s: '%s' not on a Lustre fs?",
1653 __FUNCTION__, path);
1654 } else if (errno == ENOENT) {
1655 llapi_err(LLAPI_MSG_WARN,
1656 "warning: %s: %s does not exist",
1657 __FUNCTION__, path);
1660 llapi_err(LLAPI_MSG_ERROR,
1661 "error: %s: %s failed for %s",
1662 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1663 "IOC_MDC_GETFILESTRIPE", path);
1669 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1671 /* Do not get down anymore? */
1672 if (param->depth == param->maxdepth)
1679 int llapi_getstripe(char *path, struct find_param *param)
1682 int ret = 0, len = strlen(path);
1684 if (len > PATH_MAX) {
1685 llapi_err(LLAPI_MSG_ERROR,
1686 "%s: Path name '%s' is too long",
1687 __FUNCTION__, path);
1691 buf = (char *)malloc(PATH_MAX + 1);
1695 ret = common_param_init(param);
1703 strncpy(buf, path, PATH_MAX + 1);
1704 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1705 cb_common_fini, param, NULL);
1706 find_param_fini(param);
1708 return ret < 0 ? ret : 0;
1711 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1712 struct obd_statfs *stat_buf,
1713 struct obd_uuid *uuid_buf)
1716 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1718 struct obd_ioctl_data data = { 0 };
1721 data.ioc_inlbuf1 = (char *)&type;
1722 data.ioc_inllen1 = sizeof(__u32);
1723 data.ioc_inlbuf2 = (char *)&index;
1724 data.ioc_inllen2 = sizeof(__u32);
1725 data.ioc_pbuf1 = (char *)stat_buf;
1726 data.ioc_plen1 = sizeof(struct obd_statfs);
1727 data.ioc_pbuf2 = (char *)uuid_buf;
1728 data.ioc_plen2 = sizeof(struct obd_uuid);
1730 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1731 llapi_err(LLAPI_MSG_ERROR,
1732 "llapi_obd_statfs: error packing ioctl data");
1736 fd = open(path, O_RDONLY);
1737 if (errno == EISDIR)
1738 fd = open(path, O_DIRECTORY | O_RDONLY);
1741 rc = errno ? -errno : -EBADF;
1742 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
1743 __FUNCTION__, path);
1746 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1748 rc = errno ? -errno : -EINVAL;
1754 #define MAX_STRING_SIZE 128
1755 #define DEVICES_LIST "/proc/fs/lustre/devices"
1757 int llapi_ping(char *obd_type, char *obd_name)
1759 char path[MAX_STRING_SIZE];
1763 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1764 obd_type, obd_name);
1766 fd = open(path, O_WRONLY);
1769 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1773 rc = write(fd, buf, 1);
1781 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1783 char buf[MAX_STRING_SIZE];
1784 FILE *fp = fopen(DEVICES_LIST, "r");
1789 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1793 while (fgets(buf, sizeof(buf), fp) != NULL) {
1794 char *obd_type_name = NULL;
1795 char *obd_name = NULL;
1796 char *obd_uuid = NULL;
1797 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1798 char *bufl = rawbuf;
1800 struct obd_ioctl_data datal = { 0, };
1801 struct obd_statfs osfs_buffer;
1803 while(bufp[0] == ' ')
1806 for(i = 0; i < 3; i++) {
1807 obd_type_name = strsep(&bufp, " ");
1809 obd_name = strsep(&bufp, " ");
1810 obd_uuid = strsep(&bufp, " ");
1812 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1814 memset(bufl, 0, sizeof(rawbuf));
1815 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1816 datal.ioc_plen1 = sizeof(osfs_buffer);
1818 for (i = 0; i < type_num; i++) {
1819 if (strcmp(obd_type_name, obd_type[i]) != 0)
1822 cb(obd_type_name, obd_name, obd_uuid, args);
1829 static void do_target_check(char *obd_type_name, char *obd_name,
1830 char *obd_uuid, void *args)
1834 rc = llapi_ping(obd_type_name, obd_name);
1835 if (rc == ENOTCONN) {
1836 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
1838 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1840 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1844 int llapi_target_check(int type_num, char **obd_type, char *dir)
1846 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1849 #undef MAX_STRING_SIZE
1851 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1853 char raw[OBD_MAX_IOCTL_BUFFER];
1854 char out[LLOG_CHUNK_SIZE];
1856 struct obd_ioctl_data data = { 0 };
1861 sprintf(key, "%s", keyword);
1862 memset(raw, 0, sizeof(raw));
1863 memset(out, 0, sizeof(out));
1864 data.ioc_inlbuf1 = key;
1865 data.ioc_inllen1 = strlen(key) + 1;
1867 data.ioc_inlbuf2 = node_name;
1868 data.ioc_inllen2 = strlen(node_name) + 1;
1870 data.ioc_pbuf1 = out;
1871 data.ioc_plen1 = sizeof(out);
1872 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1876 root = opendir(dir);
1879 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1883 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1885 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1887 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1893 /* Is this a lustre fs? */
1894 int llapi_is_lustre_mnttype(const char *type)
1896 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1899 /* Is this a lustre client fs? */
1900 int llapi_is_lustre_mnt(struct mntent *mnt)
1902 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1903 strstr(mnt->mnt_fsname, ":/") != NULL);
1906 int llapi_quotacheck(char *mnt, int check_type)
1911 root = opendir(mnt);
1913 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1917 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1923 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1929 root = opendir(mnt);
1931 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1936 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1940 if (poll_intvl < 30)
1948 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1953 root = opendir(mnt);
1955 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1959 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1965 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
1968 struct find_param *param = (struct find_param *)data;
1972 LASSERT(parent != NULL || d != NULL);
1975 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1976 (void *)param->lmd);
1977 } else if (parent) {
1978 char *fname = strrchr(path, '/');
1979 fname = (fname == NULL ? path : fname + 1);
1981 strncpy((char *)param->lmd, fname, param->lumlen);
1982 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1983 (void *)param->lmd);
1989 if (errno == ENODATA) {
1990 if (!param->obduuid && !param->quiet)
1991 llapi_err(LLAPI_MSG_ERROR,
1992 "%s has no stripe info", path);
1994 } else if (errno == ENOENT) {
1995 llapi_err(LLAPI_MSG_ERROR,
1996 "warning: %s: %s does not exist",
1997 __FUNCTION__, path);
1999 } else if (errno != EISDIR) {
2001 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2002 d ? "LL_IOC_MDC_GETINFO" :
2003 "IOC_MDC_GETFILEINFO", path);
2008 st = ¶m->lmd->lmd_st;
2010 /* libc chown() will do extra check, and if the real owner is
2011 * the same as the ones to set, it won't fall into kernel, so
2012 * invoke syscall directly. */
2013 rc = syscall(SYS_chown, path, -1, -1);
2015 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2017 rc = chmod(path, st->st_mode);
2019 llapi_err(LLAPI_MSG_ERROR,"error: chmod %s (%hu)", path, st->st_mode);
2024 int llapi_quotachown(char *path, int flag)
2026 struct find_param param;
2028 int ret = 0, len = strlen(path);
2030 if (len > PATH_MAX) {
2031 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
2032 __FUNCTION__, path);
2036 buf = (char *)malloc(PATH_MAX + 1);
2040 memset(¶m, 0, sizeof(param));
2041 param.recursive = 1;
2045 ret = common_param_init(¶m);
2049 strncpy(buf, path, PATH_MAX + 1);
2050 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
2051 NULL, ¶m, NULL);
2053 find_param_fini(¶m);
2061 #include <sys/wait.h>
2065 static int rmtacl_notify(int ops)
2069 int found = 0, fd, rc;
2071 fp = setmntent(MOUNTED, "r");
2073 perror("setmntent");
2078 mnt = getmntent(fp);
2082 if (!llapi_is_lustre_mnt(mnt))
2085 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2091 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2103 static char *next_token(char *p, int div)
2109 while (*p && *p != ':' && !isspace(*p))
2112 while (*p == ':' || isspace(*p))
2115 return *p ? p : NULL;
2118 static int rmtacl_name2id(char *name, int is_user)
2123 if ((pw = getpwnam(name)) == NULL)
2126 return (int)(pw->pw_uid);
2130 if ((gr = getgrnam(name)) == NULL)
2133 return (int)(gr->gr_gid);
2137 static int isodigit(int c)
2139 return (c >= '0' && c <= '7') ? 1 : 0;
2143 * Whether the name is just digits string (uid/gid) already or not.
2148 static int str_is_id(char *str)
2155 if (*str == 'x' || *str == 'X') { /* for Hex. */
2156 if (!isxdigit(*(++str)))
2159 while (isxdigit(*(++str)));
2160 } else if (isodigit(*str)) { /* for Oct. */
2161 while (isodigit(*(++str)));
2163 } else if (isdigit(*str)) { /* for Dec. */
2164 while (isdigit(*(++str)));
2167 return (*str == 0) ? 1 : 0;
2177 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2179 static rmtacl_name_t rmtacl_namelist[] = {
2180 { RMTACL_OPTNAME("user:"), 1, 0 },
2181 { RMTACL_OPTNAME("group:"), 0, 0 },
2182 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2183 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2184 /* for --tabular option */
2185 { RMTACL_OPTNAME("user"), 1, 1 },
2186 { RMTACL_OPTNAME("group"), 0, 1 },
2190 static int rgetfacl_output(char *str)
2192 char *start = NULL, *end = NULL;
2193 int is_user = 0, n, id;
2200 for (rn = rmtacl_namelist; rn->name; rn++) {
2201 if(strncmp(str, rn->name, rn->length) == 0) {
2202 if (!rn->next_token)
2203 start = str + rn->length;
2205 start = next_token(str + rn->length, 0);
2206 is_user = rn->is_user;
2211 end = next_token(start, 1);
2212 if (end == NULL || start == end) {
2213 n = printf("%s", str);
2219 id = rmtacl_name2id(start, is_user);
2220 if (id == INVALID_ID) {
2221 if (str_is_id(start)) {
2223 n = printf("%s", str);
2226 } else if ((id == NOBODY_UID && is_user) ||
2227 (id == NOBODY_GID && !is_user)) {
2229 n = printf("%s", str);
2233 n = printf("%s%d%s", str, id, end);
2238 static int child_status(int status)
2240 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2243 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2256 if ((pid = fork()) < 0) {
2262 /* child process redirects its output. */
2265 if (dup2(fd[1], 1) < 0) {
2276 status = rmtacl_notify(ops);
2280 exit(execvp(argv[0], argv));
2283 /* the following is parent process */
2284 if ((fp = fdopen(fd[0], "r")) == NULL) {
2291 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2292 if (output_func(buf) < 0)
2293 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2299 if (waitpid(pid, &status, 0) < 0) {
2304 return child_status(status);
2307 int llapi_lsetfacl(int argc, char *argv[])
2309 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2312 int llapi_lgetfacl(int argc, char *argv[])
2314 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2317 int llapi_rsetfacl(int argc, char *argv[])
2319 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2322 int llapi_rgetfacl(int argc, char *argv[])
2324 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2327 int llapi_cp(int argc, char *argv[])
2331 rc = rmtacl_notify(RMT_RSETFACL);
2335 exit(execvp(argv[0], argv));
2338 int llapi_ls(int argc, char *argv[])
2342 rc = rmtacl_notify(RMT_LGETFACL);
2346 exit(execvp(argv[0], argv));