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, '/');
435 /* return the first file matching this pattern */
436 static int first_match(char *pattern, char *buffer)
440 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
443 if (glob_info.gl_pathc < 1) {
444 globfree(&glob_info);
448 strcpy(buffer, glob_info.gl_pathv[0]);
450 globfree(&glob_info);
455 * find the pool directory path under /proc
456 * (can be also used to test if a fsname is known)
458 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
461 char pattern[PATH_MAX + 1];
462 char buffer[PATH_MAX];
464 if (fsname == NULL) {
465 rc = search_fsname(pathname, buffer);
469 strcpy(pathname, fsname);
472 snprintf(pattern, PATH_MAX,
473 "/proc/fs/lustre/lov/%s-*/pools",
475 rc = first_match(pattern, buffer);
479 /* in fsname test mode, pool_pathname is NULL */
480 if (pool_pathname != NULL)
481 strcpy(pool_pathname, buffer);
486 int llapi_poollist(char *name)
490 char rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
494 /* is name a pathname ? */
495 ptr = strchr(name, '/');
497 /* only absolute pathname is supported */
500 if (!realpath(name, rname)) {
502 llapi_err(LLAPI_MSG_ERROR,
503 "llapi_poollist: invalid path '%s'",
508 rc = poolpath(NULL, rname, pathname);
511 llapi_err(LLAPI_MSG_ERROR,
512 "llapi_poollist: '%s' is not"
513 " a Lustre filesystem",
520 /* name is FSNAME[.POOLNAME] */
522 poolname = strchr(name, '.');
523 if (poolname != NULL) {
527 rc = poolpath(fsname, NULL, pathname);
530 llapi_err(LLAPI_MSG_ERROR,
531 "llapi_poollist: Lustre filesystem '%s'"
538 llapi_err(LLAPI_MSG_ERROR,
539 "llapi_poollist: Lustre filesystem '%s' not found",
544 if (poolname != NULL) {
545 rc = print_pool_members(fsname, pathname, poolname);
552 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
553 if ((dir = opendir(pathname)) == NULL) {
556 while ((pool = readdir(dir)) != NULL) {
557 if (!((pool->d_name[0] == '.') &&
558 (pool->d_name[1] == '\0')) &&
559 !((pool->d_name[0] == '.') &&
560 (pool->d_name[1] == '.') &&
561 (pool->d_name[2] == '\0')))
562 llapi_printf(LLAPI_MSG_NORMAL, " %s.%s\n",
563 fsname, pool->d_name);
570 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
571 void *data, cfs_dirent_t *de);
573 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
574 #define OBD_NOT_FOUND (-1)
576 static int common_param_init(struct find_param *param)
578 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
579 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
580 llapi_err(LLAPI_MSG_ERROR,
581 "error: allocation of %d bytes for ioctl",
582 sizeof(lstat_t) + param->lumlen);
586 param->got_uuids = 0;
587 param->obdindexes = NULL;
588 param->obdindex = OBD_NOT_FOUND;
592 static void find_param_fini(struct find_param *param)
594 if (param->obdindexes)
595 free(param->obdindexes);
601 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
603 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
606 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
611 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
615 fd = open(path, O_RDONLY);
618 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
622 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
630 * If uuidp is NULL, return the number of available obd uuids.
631 * If uuidp is non-NULL, then it will return the uuids of the obds. If
632 * there are more OSTs then allocated to uuidp, then an error is returned with
633 * the ost_count set to number of available obd uuids.
635 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
637 struct obd_uuid lov_name;
640 int rc = 0, index = 0;
642 /* Get the lov name */
643 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
647 /* Now get the ost uuids from /proc */
648 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
650 fp = fopen(buf, "r");
653 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
657 while (fgets(buf, sizeof(buf), fp) != NULL) {
658 if (uuidp && (index < *ost_count)) {
659 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
667 if (uuidp && (index >= *ost_count))
674 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
681 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
686 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
692 /* Here, param->obduuid points to a single obduuid, the index of which is
693 * returned in param->obdindex */
694 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
696 struct obd_uuid lov_uuid;
697 char uuid[sizeof(struct obd_uuid)];
702 /* Get the lov name */
703 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
705 if (errno != ENOTTY) {
707 llapi_err(LLAPI_MSG_ERROR,
708 "error: can't get lov name: %s", dname);
715 param->got_uuids = 1;
717 /* Now get the ost uuids from /proc */
718 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
720 fp = fopen(buf, "r");
723 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
727 if (!param->obduuid && !param->quiet && !param->obds_printed)
728 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
730 while (fgets(buf, sizeof(buf), fp) != NULL) {
731 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
734 if (param->obduuid) {
735 if (strncmp(param->obduuid->uuid, uuid,
736 sizeof(uuid)) == 0) {
737 param->obdindex = index;
740 } else if (!param->quiet && !param->obds_printed) {
741 /* Print everything */
742 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
745 param->obds_printed = 1;
749 if (!param->quiet && param->obduuid &&
750 (param->obdindex == OBD_NOT_FOUND)) {
751 llapi_err_noerrno(LLAPI_MSG_ERROR,
752 "error: %s: unknown obduuid: %s",
753 __FUNCTION__, param->obduuid->uuid);
760 /* In this case, param->obduuid will be an array of obduuids and
761 * obd index for all these obduuids will be returned in
762 * param->obdindexes */
763 static int setup_obd_indexes(DIR *dir, struct find_param *param)
765 struct obd_uuid *uuids = NULL;
766 int obdcount = INIT_ALLOC_NUM_OSTS;
767 int ret, obd_valid = 0, obdnum, i;
769 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
770 sizeof(struct obd_uuid));
775 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
778 struct obd_uuid *uuids_temp;
780 if (ret == -EOVERFLOW) {
781 uuids_temp = realloc(uuids, obdcount *
782 sizeof(struct obd_uuid));
783 if (uuids_temp != NULL)
784 goto retry_get_uuids;
789 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
793 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
794 if (param->obdindexes == NULL)
797 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
798 for (i = 0; i <= obdcount; i++) {
799 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
800 (char *)&uuids[i]) == 0) {
801 param->obdindexes[obdnum] = i;
807 param->obdindexes[obdnum] = OBD_NOT_FOUND;
811 param->obdindex = OBD_NOT_FOUND;
813 param->obdindex = obd_valid;
815 param->got_uuids = 1;
820 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
821 struct lov_user_ost_data_v1 *objects,
822 char *path, int is_dir,
823 int obdindex, int quiet, int header, int body)
825 int i, obdstripe = 0;
827 if (obdindex != OBD_NOT_FOUND) {
828 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
829 if (obdindex == objects[i].l_ost_idx) {
830 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
836 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
840 /* if it's a directory */
842 if (obdstripe == 1) {
843 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
844 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
845 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
847 llapi_printf(LLAPI_MSG_NORMAL,
848 "stripe_count: %d stripe_size: %u "
849 "stripe_offset: %d%s%s\n",
850 lum->lmm_stripe_count == (__u16)-1 ? -1 :
851 lum->lmm_stripe_count,
852 lum->lmm_stripe_size,
853 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
854 lum->lmm_stripe_offset,
855 pool_name != NULL ? " pool: " : "",
856 pool_name != NULL ? pool_name : "");
861 if (header && (obdstripe == 1)) {
862 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
864 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
866 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
868 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
869 (int)lum->lmm_stripe_count);
870 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
871 lum->lmm_stripe_size);
872 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
874 if (pool_name != NULL)
875 llapi_printf(LLAPI_MSG_NORMAL,
876 "lmm_pool_name: %s\n", pool_name);
880 if ((!quiet) && (obdstripe == 1))
881 llapi_printf(LLAPI_MSG_NORMAL,
882 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
884 for (i = 0; i < lum->lmm_stripe_count; i++) {
885 int idx = objects[i].l_ost_idx;
886 long long oid = objects[i].l_object_id;
887 long long gr = objects[i].l_object_gr;
888 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
889 llapi_printf(LLAPI_MSG_NORMAL,
890 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
892 obdindex == idx ? " *" : "");
894 llapi_printf(LLAPI_MSG_NORMAL, "\n");
898 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
899 int is_dir, int obdindex, int quiet,
900 int header, int body)
902 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
903 int i, obdstripe = 0;
905 if (obdindex != OBD_NOT_FOUND) {
906 for (i = 0; i < lumj->lmm_stripe_count; i++) {
907 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
908 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
914 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
918 if (header && obdstripe == 1) {
919 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
921 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
922 lumj->lmm_object_gr);
923 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
924 lumj->lmm_object_id);
925 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
926 (int)lumj->lmm_stripe_count);
927 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
928 lumj->lmm_stripe_size);
929 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
931 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
932 lumj->lmm_extent_count);
936 unsigned long long start = -1, end = 0;
937 if (!quiet && obdstripe == 1)
938 llapi_printf(LLAPI_MSG_NORMAL,
939 "joined\tobdidx\t\t objid\t\tobjid\t\t "
940 "group\t\tstart\t\tend\n");
941 for (i = 0; i < lumj->lmm_stripe_count; i++) {
942 int idx = lumj->lmm_objects[i].l_ost_idx;
943 long long oid = lumj->lmm_objects[i].l_object_id;
944 long long gr = lumj->lmm_objects[i].l_object_gr;
945 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
946 llapi_printf(LLAPI_MSG_NORMAL,
947 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
949 obdindex == idx ? " *" : "");
950 if (start != lumj->lmm_objects[i].l_extent_start ||
951 end != lumj->lmm_objects[i].l_extent_end) {
952 start = lumj->lmm_objects[i].l_extent_start;
953 llapi_printf(LLAPI_MSG_NORMAL,"\t%14llu",start);
954 end = lumj->lmm_objects[i].l_extent_end;
955 if (end == (unsigned long long)-1)
956 llapi_printf(LLAPI_MSG_NORMAL,
959 llapi_printf(LLAPI_MSG_NORMAL,
962 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
965 llapi_printf(LLAPI_MSG_NORMAL, "\n");
969 void llapi_lov_dump_user_lmm(struct find_param *param,
970 char *path, int is_dir)
972 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
973 case LOV_USER_MAGIC_V1:
974 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
975 param->lmd->lmd_lmm.lmm_objects,
977 param->obdindex, param->quiet,
979 (param->verbose || !param->obduuid));
981 case LOV_USER_MAGIC_JOIN:
982 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
983 param->obdindex, param->quiet,
985 (param->verbose || !param->obduuid));
987 case LOV_USER_MAGIC_V3: {
988 char pool_name[LOV_MAXPOOLNAME + 1];
989 struct lov_user_ost_data_v1 *objects;
990 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
992 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
993 pool_name[LOV_MAXPOOLNAME] = '\0';
994 objects = lmmv3->lmm_objects;
995 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
996 objects, path, is_dir,
997 param->obdindex, param->quiet,
999 (param->verbose || !param->obduuid));
1003 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1004 "(expecting one of %#x %#x %#x)\n",
1005 *(__u32 *)¶m->lmd->lmd_lmm,
1006 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_JOIN,
1012 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1018 fname = strrchr(path, '/');
1020 /* It should be a file (or other non-directory) */
1021 if (fname == NULL) {
1022 dname = (char *)malloc(2);
1026 fname = (char *)path;
1028 dname = (char *)malloc(fname - path + 1);
1031 strncpy(dname, path, fname - path);
1032 dname[fname - path] = '\0';
1036 if ((fd = open(dname, O_RDONLY)) == -1) {
1042 strcpy((char *)lum, fname);
1043 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1046 if (close(fd) == -1 && rc == 0)
1054 int llapi_file_lookup(int dirfd, const char *name)
1056 struct obd_ioctl_data data = { 0 };
1061 if (dirfd < 0 || name == NULL)
1064 data.ioc_version = OBD_IOCTL_VERSION;
1065 data.ioc_len = sizeof(data);
1066 data.ioc_inlbuf1 = (char *)name;
1067 data.ioc_inllen1 = strlen(name) + 1;
1069 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1071 llapi_err(LLAPI_MSG_ERROR,
1072 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1077 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1080 int llapi_mds_getfileinfo(char *path, DIR *parent,
1081 struct lov_user_mds_data *lmd)
1083 lstat_t *st = &lmd->lmd_st;
1084 char *fname = strrchr(path, '/');
1090 fname = (fname == NULL ? path : fname + 1);
1091 /* retrieve needed file info */
1092 strncpy((char *)lmd, fname,
1093 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
1094 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1097 if (errno == ENOTTY) {
1098 /* ioctl is not supported, it is not a lustre fs.
1099 * Do the regular lstat(2) instead. */
1100 ret = lstat_f(path, st);
1102 llapi_err(LLAPI_MSG_ERROR,
1103 "error: %s: lstat failed for %s",
1104 __FUNCTION__, path);
1107 } else if (errno == ENOENT) {
1108 llapi_err(LLAPI_MSG_WARN,
1109 "warning: %s: %s does not exist",
1110 __FUNCTION__, path);
1113 llapi_err(LLAPI_MSG_ERROR,
1114 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1115 __FUNCTION__, path);
1123 static DIR *opendir_parent(char *path)
1129 fname = strrchr(path, '/');
1131 return opendir(".");
1135 parent = opendir(path);
1140 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1141 semantic_func_t sem_init,
1142 semantic_func_t sem_fini, void *data,
1153 if (!d && errno != ENOTDIR) {
1154 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
1155 __FUNCTION__, path);
1157 } else if (!d && !parent) {
1158 /* ENOTDIR. Open the parent dir. */
1159 p = opendir_parent(path);
1161 GOTO(out, ret = -EINVAL);
1164 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1170 while ((dent = readdir64(d)) != NULL) {
1171 ((struct find_param *)data)->have_fileinfo = 0;
1173 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1176 /* Don't traverse .lustre directory */
1177 if (!(strcmp(dent->d_name, mdd_dot_lustre_name)))
1181 if ((len + dent->d_reclen + 2) > size) {
1182 llapi_err(LLAPI_MSG_ERROR,
1183 "error: %s: string buffer is too small",
1188 strcat(path, dent->d_name);
1190 if (dent->d_type == DT_UNKNOWN) {
1191 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1193 ret = llapi_mds_getfileinfo(path, d,
1194 ((struct find_param *)data)->lmd);
1196 ((struct find_param *)data)->have_fileinfo = 1;
1198 llapi_filetype_dir_table[st->st_mode &
1205 switch (dent->d_type) {
1207 llapi_err(LLAPI_MSG_ERROR,
1208 "error: %s: '%s' is UNKNOWN type %d",
1209 __FUNCTION__, dent->d_name, dent->d_type);
1212 ret = llapi_semantic_traverse(path, size, d, sem_init,
1213 sem_fini, data, dent);
1220 ret = sem_init(path, d, NULL, data, dent);
1224 if (sem_fini && ret == 0)
1225 sem_fini(path, d, NULL, data, dent);
1233 sem_fini(path, parent, d, data, de);
1242 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1243 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1245 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1246 * The table below gives the answers for the specified parameters (value and
1247 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1248 * --------------------------------------
1249 * 1 | file > limit; sign > 0 | -1 / -1 |
1250 * 2 | file = limit; sign > 0 | ? / 1 |
1251 * 3 | file < limit; sign > 0 | ? / 1 |
1252 * 4 | file > limit; sign = 0 | -1 / -1 |
1253 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1254 * 6 | file < limit; sign = 0 | ? / -1 |
1255 * 7 | file > limit; sign < 0 | 1 / 1 |
1256 * 8 | file = limit; sign < 0 | ? / -1 |
1257 * 9 | file < limit; sign < 0 | ? / -1 |
1258 * --------------------------------------
1259 * Note: 5th actually means that the value is within the interval
1260 * (limit - margin, limit]. */
1261 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1262 unsigned long long margin, int mds)
1270 if (file <= limit && file + margin > limit)
1272 if (file + margin <= limit)
1273 return mds ? 0 : -1;
1286 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1287 * Return -1 or 1 if file timestamp does not or does match the given criteria
1288 * correspondingly. Return 0 if the MDS time is being checked and there are
1289 * attributes on OSTs and it is not yet clear if the timespamp matches.
1291 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1292 * updated timestamps. */
1293 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1298 /* Check if file is accepted. */
1300 ret = find_value_cmp(st->st_atime, param->atime,
1301 param->asign, 24 * 60 * 60, mds);
1308 ret = find_value_cmp(st->st_mtime, param->mtime,
1309 param->msign, 24 * 60 * 60, mds);
1313 /* If the previous check matches, but this one is not yet clear,
1314 * we should return 0 to do an RPC on OSTs. */
1320 ret = find_value_cmp(st->st_ctime, param->ctime,
1321 param->csign, 24 * 60 * 60, mds);
1325 /* If the previous check matches, but this one is not yet clear,
1326 * we should return 0 to do an RPC on OSTs. */
1334 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1335 void *data, cfs_dirent_t *de)
1337 struct find_param *param = (struct find_param *)data;
1338 int decision = 1; /* 1 is accepted; -1 is rejected. */
1339 lstat_t *st = ¶m->lmd->lmd_st;
1341 int checked_type = 0;
1344 LASSERT(parent != NULL || dir != NULL);
1346 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1348 /* If a regular expression is presented, make the initial decision */
1349 if (param->pattern != NULL) {
1350 char *fname = strrchr(path, '/');
1351 fname = (fname == NULL ? path : fname + 1);
1352 ret = fnmatch(param->pattern, fname, 0);
1353 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1354 (ret == 0 && param->exclude_pattern))
1358 /* See if we can check the file type from the dirent. */
1359 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1360 de->d_type < DT_MAX) {
1362 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1363 if (param->exclude_type)
1366 if (!param->exclude_type)
1372 /* If a time or OST should be checked, the decision is not taken yet. */
1373 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1378 /* Request MDS for the stat info. */
1379 if (param->have_fileinfo == 0) {
1381 /* retrieve needed file info */
1382 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1383 (void *)param->lmd);
1385 char *fname = strrchr(path, '/');
1386 fname = (fname == NULL ? path : fname + 1);
1388 /* retrieve needed file info */
1389 strncpy((char *)param->lmd, fname, param->lumlen);
1390 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1391 (void *)param->lmd);
1396 if (errno == ENOTTY) {
1397 /* ioctl is not supported, it is not a lustre fs.
1398 * Do the regular lstat(2) instead. */
1400 ret = lstat_f(path, st);
1402 llapi_err(LLAPI_MSG_ERROR,
1403 "error: %s: lstat failed for %s",
1404 __FUNCTION__, path);
1407 } else if (errno == ENOENT) {
1408 llapi_err(LLAPI_MSG_WARN,
1409 "warning: %s: %s does not exist",
1410 __FUNCTION__, path);
1413 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1414 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1415 "IOC_MDC_GETFILEINFO", path);
1420 if (param->type && !checked_type) {
1421 if ((st->st_mode & S_IFMT) == param->type) {
1422 if (param->exclude_type)
1425 if (!param->exclude_type)
1431 if (param->obduuid) {
1432 if (lustre_fs && param->got_uuids &&
1433 param->st_dev != st->st_dev) {
1434 /* A lustre/lustre mount point is crossed. */
1435 param->got_uuids = 0;
1436 param->obds_printed = 0;
1437 param->obdindex = OBD_NOT_FOUND;
1440 if (lustre_fs && !param->got_uuids) {
1441 ret = setup_obd_indexes(dir ? dir : parent, param);
1445 param->st_dev = st->st_dev;
1446 } else if (!lustre_fs && param->got_uuids) {
1447 /* A lustre/non-lustre mount point is crossed. */
1448 param->got_uuids = 0;
1449 param->obdindex = OBD_NOT_FOUND;
1453 /* If an OBD UUID is specified but no one matches, skip this file. */
1454 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1457 /* If a OST UUID is given, and some OST matches, check it here. */
1458 if (param->obdindex != OBD_NOT_FOUND) {
1459 if (!S_ISREG(st->st_mode))
1462 /* Only those files should be accepted, which have a
1463 * stripe on the specified OST. */
1464 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1468 struct lov_user_ost_data_v1 *lmm_objects;
1470 if (param->lmd->lmd_lmm.lmm_magic ==
1471 LOV_USER_MAGIC_V3) {
1472 struct lov_user_md_v3 *lmmv3 =
1473 (void *)¶m->lmd->lmd_lmm;
1475 lmm_objects = lmmv3->lmm_objects;
1477 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1481 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1482 for (j = 0; j < param->num_obds; j++) {
1483 if (param->obdindexes[j] ==
1484 lmm_objects[i].l_ost_idx)
1489 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1494 if (param->check_uid) {
1495 if (st->st_uid == param->uid) {
1496 if (param->exclude_uid)
1499 if (!param->exclude_uid)
1504 if (param->check_gid) {
1505 if (st->st_gid == param->gid) {
1506 if (param->exclude_gid)
1509 if (!param->exclude_gid)
1514 if (param->check_pool) {
1515 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1517 /* empty requested pool is taken as no pool search => V1 */
1518 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1519 (param->poolname[0] == '\0')) ||
1520 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1521 (strncmp(lmmv3->lmm_pool_name,
1522 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1523 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1524 (strcmp(param->poolname, "*") == 0))) {
1525 if (param->exclude_pool)
1528 if (!param->exclude_pool)
1533 /* Check the time on mds. */
1537 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1538 param->lmd->lmd_lmm.lmm_stripe_count)
1540 decision = find_time_check(st, param, for_mds);
1544 /* If file still fits the request, ask ost for updated info.
1545 The regular stat is almost of the same speed as some new
1546 'glimpse-size-ioctl'. */
1547 if (!decision && S_ISREG(st->st_mode) &&
1548 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1549 if (param->obdindex != OBD_NOT_FOUND) {
1550 /* Check whether the obd is active or not, if it is
1551 * not active, just print the object affected by this
1554 struct obd_statfs stat_buf;
1555 struct obd_uuid uuid_buf;
1557 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1558 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1559 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1560 param->obdindex, &stat_buf,
1563 if (ret == -ENODATA || ret == -ENODEV
1566 llapi_printf(LLAPI_MSG_NORMAL,
1567 "obd_uuid: %s failed %s ",
1568 param->obduuid->uuid,
1574 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1575 (void *)param->lmd);
1576 } else if (parent) {
1577 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1578 (void *)param->lmd);
1582 if (errno == ENOENT) {
1583 llapi_err(LLAPI_MSG_ERROR,
1584 "warning: %s: %s does not exist",
1585 __FUNCTION__, path);
1588 llapi_err(LLAPI_MSG_ERROR,
1589 "%s: IOC_LOV_GETINFO on %s failed",
1590 __FUNCTION__, path);
1595 /* Check the time on osc. */
1596 decision = find_time_check(st, param, 0);
1602 decision = find_value_cmp(st->st_size, param->size,
1603 param->size_sign, param->size_units,
1607 if (decision != -1) {
1608 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1610 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1612 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1616 /* Do not get down anymore? */
1617 if (param->depth == param->maxdepth)
1624 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1627 struct find_param *param = (struct find_param *)data;
1632 int llapi_find(char *path, struct find_param *param)
1635 int ret, len = strlen(path);
1637 if (len > PATH_MAX) {
1638 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1639 __FUNCTION__, path);
1643 buf = (char *)malloc(PATH_MAX + 1);
1647 ret = common_param_init(param);
1655 strncpy(buf, path, PATH_MAX + 1);
1656 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1657 cb_common_fini, param, NULL);
1659 find_param_fini(param);
1661 return ret < 0 ? ret : 0;
1664 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1667 struct find_param *param = (struct find_param *)data;
1670 LASSERT(parent != NULL || d != NULL);
1673 if (!param->got_uuids) {
1674 ret = setup_obd_uuid(d ? d : parent, path, param);
1680 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1681 (void *)¶m->lmd->lmd_lmm);
1682 } else if (parent) {
1683 char *fname = strrchr(path, '/');
1684 fname = (fname == NULL ? path : fname + 1);
1686 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1687 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1688 (void *)¶m->lmd->lmd_lmm);
1692 if (errno == ENODATA) {
1693 if (!param->obduuid && !param->quiet)
1694 llapi_printf(LLAPI_MSG_NORMAL,
1695 "%s has no stripe info\n", path);
1697 } else if (errno == ENOTTY) {
1698 llapi_err(LLAPI_MSG_ERROR,
1699 "%s: '%s' not on a Lustre fs?",
1700 __FUNCTION__, path);
1701 } else if (errno == ENOENT) {
1702 llapi_err(LLAPI_MSG_WARN,
1703 "warning: %s: %s does not exist",
1704 __FUNCTION__, path);
1707 llapi_err(LLAPI_MSG_ERROR,
1708 "error: %s: %s failed for %s",
1709 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1710 "IOC_MDC_GETFILESTRIPE", path);
1716 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1718 /* Do not get down anymore? */
1719 if (param->depth == param->maxdepth)
1726 int llapi_getstripe(char *path, struct find_param *param)
1729 int ret = 0, len = strlen(path);
1731 if (len > PATH_MAX) {
1732 llapi_err(LLAPI_MSG_ERROR,
1733 "%s: Path name '%s' is too long",
1734 __FUNCTION__, path);
1738 buf = (char *)malloc(PATH_MAX + 1);
1742 ret = common_param_init(param);
1750 strncpy(buf, path, PATH_MAX + 1);
1751 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1752 cb_common_fini, param, NULL);
1753 find_param_fini(param);
1755 return ret < 0 ? ret : 0;
1758 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1759 struct obd_statfs *stat_buf,
1760 struct obd_uuid *uuid_buf)
1763 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1765 struct obd_ioctl_data data = { 0 };
1768 data.ioc_inlbuf1 = (char *)&type;
1769 data.ioc_inllen1 = sizeof(__u32);
1770 data.ioc_inlbuf2 = (char *)&index;
1771 data.ioc_inllen2 = sizeof(__u32);
1772 data.ioc_pbuf1 = (char *)stat_buf;
1773 data.ioc_plen1 = sizeof(struct obd_statfs);
1774 data.ioc_pbuf2 = (char *)uuid_buf;
1775 data.ioc_plen2 = sizeof(struct obd_uuid);
1777 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1778 llapi_err(LLAPI_MSG_ERROR,
1779 "llapi_obd_statfs: error packing ioctl data");
1783 fd = open(path, O_RDONLY);
1784 if (errno == EISDIR)
1785 fd = open(path, O_DIRECTORY | O_RDONLY);
1788 rc = errno ? -errno : -EBADF;
1789 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
1790 __FUNCTION__, path);
1793 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1795 rc = errno ? -errno : -EINVAL;
1801 #define MAX_STRING_SIZE 128
1802 #define DEVICES_LIST "/proc/fs/lustre/devices"
1804 int llapi_ping(char *obd_type, char *obd_name)
1806 char path[MAX_STRING_SIZE];
1810 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1811 obd_type, obd_name);
1813 fd = open(path, O_WRONLY);
1816 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1820 rc = write(fd, buf, 1);
1828 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
1830 char buf[MAX_STRING_SIZE];
1831 FILE *fp = fopen(DEVICES_LIST, "r");
1836 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1840 while (fgets(buf, sizeof(buf), fp) != NULL) {
1841 char *obd_type_name = NULL;
1842 char *obd_name = NULL;
1843 char *obd_uuid = NULL;
1845 struct obd_ioctl_data datal = { 0, };
1846 struct obd_statfs osfs_buffer;
1848 while(bufp[0] == ' ')
1851 for(i = 0; i < 3; i++) {
1852 obd_type_name = strsep(&bufp, " ");
1854 obd_name = strsep(&bufp, " ");
1855 obd_uuid = strsep(&bufp, " ");
1857 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1859 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1860 datal.ioc_plen1 = sizeof(osfs_buffer);
1862 for (i = 0; i < type_num; i++) {
1863 if (strcmp(obd_type_name, obd_type[i]) != 0)
1866 cb(obd_type_name, obd_name, obd_uuid, args);
1873 static void do_target_check(char *obd_type_name, char *obd_name,
1874 char *obd_uuid, void *args)
1878 rc = llapi_ping(obd_type_name, obd_name);
1879 if (rc == ENOTCONN) {
1880 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
1882 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1884 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1888 int llapi_target_check(int type_num, char **obd_type, char *dir)
1890 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1893 #undef MAX_STRING_SIZE
1895 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1897 char raw[OBD_MAX_IOCTL_BUFFER];
1898 char out[LLOG_CHUNK_SIZE];
1900 struct obd_ioctl_data data = { 0 };
1905 sprintf(key, "%s", keyword);
1906 memset(raw, 0, sizeof(raw));
1907 memset(out, 0, sizeof(out));
1908 data.ioc_inlbuf1 = key;
1909 data.ioc_inllen1 = strlen(key) + 1;
1911 data.ioc_inlbuf2 = node_name;
1912 data.ioc_inllen2 = strlen(node_name) + 1;
1914 data.ioc_pbuf1 = out;
1915 data.ioc_plen1 = sizeof(out);
1916 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1920 root = opendir(dir);
1923 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1927 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1929 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1931 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1937 /* Is this a lustre fs? */
1938 int llapi_is_lustre_mnttype(const char *type)
1940 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1943 /* Is this a lustre client fs? */
1944 int llapi_is_lustre_mnt(struct mntent *mnt)
1946 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1947 strstr(mnt->mnt_fsname, ":/") != NULL);
1950 int llapi_quotacheck(char *mnt, int check_type)
1955 root = opendir(mnt);
1957 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1961 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1967 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1973 root = opendir(mnt);
1975 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1980 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1984 if (poll_intvl < 30)
1992 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1997 root = opendir(mnt);
1999 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2003 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2009 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2012 struct find_param *param = (struct find_param *)data;
2016 LASSERT(parent != NULL || d != NULL);
2019 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2020 (void *)param->lmd);
2021 } else if (parent) {
2022 char *fname = strrchr(path, '/');
2023 fname = (fname == NULL ? path : fname + 1);
2025 strncpy((char *)param->lmd, fname, param->lumlen);
2026 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2027 (void *)param->lmd);
2033 if (errno == ENODATA) {
2034 if (!param->obduuid && !param->quiet)
2035 llapi_err(LLAPI_MSG_ERROR,
2036 "%s has no stripe info", path);
2038 } else if (errno == ENOENT) {
2039 llapi_err(LLAPI_MSG_ERROR,
2040 "warning: %s: %s does not exist",
2041 __FUNCTION__, path);
2043 } else if (errno != EISDIR) {
2045 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2046 d ? "LL_IOC_MDC_GETINFO" :
2047 "IOC_MDC_GETFILEINFO", path);
2052 st = ¶m->lmd->lmd_st;
2054 /* libc chown() will do extra check, and if the real owner is
2055 * the same as the ones to set, it won't fall into kernel, so
2056 * invoke syscall directly. */
2057 rc = syscall(SYS_chown, path, -1, -1);
2059 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2061 rc = chmod(path, st->st_mode);
2063 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2069 int llapi_quotachown(char *path, int flag)
2071 struct find_param param;
2073 int ret = 0, len = strlen(path);
2075 if (len > PATH_MAX) {
2076 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
2077 __FUNCTION__, path);
2081 buf = (char *)malloc(PATH_MAX + 1);
2085 memset(¶m, 0, sizeof(param));
2086 param.recursive = 1;
2090 ret = common_param_init(¶m);
2094 strncpy(buf, path, PATH_MAX + 1);
2095 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
2096 NULL, ¶m, NULL);
2098 find_param_fini(¶m);
2106 #include <sys/wait.h>
2110 static int rmtacl_notify(int ops)
2114 int found = 0, fd, rc;
2116 fp = setmntent(MOUNTED, "r");
2118 perror("setmntent");
2123 mnt = getmntent(fp);
2127 if (!llapi_is_lustre_mnt(mnt))
2130 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2136 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2148 static char *next_token(char *p, int div)
2154 while (*p && *p != ':' && !isspace(*p))
2157 while (*p == ':' || isspace(*p))
2160 return *p ? p : NULL;
2163 static int rmtacl_name2id(char *name, int is_user)
2168 if ((pw = getpwnam(name)) == NULL)
2171 return (int)(pw->pw_uid);
2175 if ((gr = getgrnam(name)) == NULL)
2178 return (int)(gr->gr_gid);
2182 static int isodigit(int c)
2184 return (c >= '0' && c <= '7') ? 1 : 0;
2188 * Whether the name is just digits string (uid/gid) already or not.
2193 static int str_is_id(char *str)
2200 if (*str == 'x' || *str == 'X') { /* for Hex. */
2201 if (!isxdigit(*(++str)))
2204 while (isxdigit(*(++str)));
2205 } else if (isodigit(*str)) { /* for Oct. */
2206 while (isodigit(*(++str)));
2208 } else if (isdigit(*str)) { /* for Dec. */
2209 while (isdigit(*(++str)));
2212 return (*str == 0) ? 1 : 0;
2222 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2224 static rmtacl_name_t rmtacl_namelist[] = {
2225 { RMTACL_OPTNAME("user:"), 1, 0 },
2226 { RMTACL_OPTNAME("group:"), 0, 0 },
2227 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2228 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2229 /* for --tabular option */
2230 { RMTACL_OPTNAME("user"), 1, 1 },
2231 { RMTACL_OPTNAME("group"), 0, 1 },
2235 static int rgetfacl_output(char *str)
2237 char *start = NULL, *end = NULL;
2238 int is_user = 0, n, id;
2245 for (rn = rmtacl_namelist; rn->name; rn++) {
2246 if(strncmp(str, rn->name, rn->length) == 0) {
2247 if (!rn->next_token)
2248 start = str + rn->length;
2250 start = next_token(str + rn->length, 0);
2251 is_user = rn->is_user;
2256 end = next_token(start, 1);
2257 if (end == NULL || start == end) {
2258 n = printf("%s", str);
2264 id = rmtacl_name2id(start, is_user);
2265 if (id == INVALID_ID) {
2266 if (str_is_id(start)) {
2268 n = printf("%s", str);
2271 } else if ((id == NOBODY_UID && is_user) ||
2272 (id == NOBODY_GID && !is_user)) {
2274 n = printf("%s", str);
2278 n = printf("%s%d%s", str, id, end);
2283 static int child_status(int status)
2285 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2288 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2301 if ((pid = fork()) < 0) {
2307 /* child process redirects its output. */
2310 if (dup2(fd[1], 1) < 0) {
2321 status = rmtacl_notify(ops);
2325 exit(execvp(argv[0], argv));
2328 /* the following is parent process */
2329 if ((fp = fdopen(fd[0], "r")) == NULL) {
2336 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2337 if (output_func(buf) < 0)
2338 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2344 if (waitpid(pid, &status, 0) < 0) {
2349 return child_status(status);
2352 int llapi_lsetfacl(int argc, char *argv[])
2354 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2357 int llapi_lgetfacl(int argc, char *argv[])
2359 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2362 int llapi_rsetfacl(int argc, char *argv[])
2364 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2367 int llapi_rgetfacl(int argc, char *argv[])
2369 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2372 int llapi_cp(int argc, char *argv[])
2376 rc = rmtacl_notify(RMT_RSETFACL);
2380 exit(execvp(argv[0], argv));
2383 int llapi_ls(int argc, char *argv[])
2387 rc = rmtacl_notify(RMT_LGETFACL);
2391 exit(execvp(argv[0], argv));
2394 /* format must have %s%s, buf must be > 16 */
2395 static int get_mdtname(const char *name, char *format, char *buf)
2397 char suffix[]="-MDT0000";
2398 int len = strlen(name);
2401 llapi_err(LLAPI_MSG_ERROR, "bad MDT name |%s|\n", name);
2405 if ((len > 8) && (strncmp(name + len - 8, "-MDT", 4) == 0))
2408 return sprintf(buf, format, name, suffix);
2412 /* Return a file descriptor to a readable changelog */
2413 int llapi_changelog_open(const char *mdtname, long long startrec)
2418 /* Use either the mdd changelog (preferred) or a client mdc changelog */
2419 if (get_mdtname(mdtname,
2420 "/proc/fs/lustre/md[cd]/%s%s{,-mdc-*}/changelog",
2423 rc = first_match(path, path);
2427 if ((fd = open(path, O_RDONLY)) < 0) {
2428 llapi_err(LLAPI_MSG_ERROR, "error: can't open |%s|\n", path);
2432 rc = lseek(fd, (off_t)startrec, SEEK_SET);
2434 llapi_err(LLAPI_MSG_ERROR, "can't seek rc=%d\n", rc);
2441 static int dev_ioctl(struct obd_ioctl_data *data, int dev, int cmd)
2443 static char rawbuf[8192];
2444 static char *buf = rawbuf;
2447 data->ioc_dev = dev;
2448 memset(buf, 0, sizeof(rawbuf));
2450 if ((rc = obd_ioctl_pack(data, &buf, sizeof(rawbuf)))) {
2451 llapi_err(LLAPI_MSG_ERROR,
2452 "error: ioctl pack (%d) failed: rc %d", cmd, rc);
2456 rc = l_ioctl(OBD_DEV_ID, cmd, buf);
2458 /* ioctl returns -1 with errno set */
2463 if (obd_ioctl_unpack(data, buf, sizeof(rawbuf))) {
2464 llapi_err(LLAPI_MSG_ERROR,
2465 "error: invalid reply\n");
2471 static int dev_name2dev(char *name)
2473 struct obd_ioctl_data data;
2476 memset(&data, 0, sizeof(data));
2477 data.ioc_inllen1 = strlen(name) + 1;
2478 data.ioc_inlbuf1 = name;
2480 rc = dev_ioctl(&data, -1, OBD_IOC_NAME2DEV);
2482 llapi_err(LLAPI_MSG_ERROR, "Device %s not found %d\n", name,rc);
2485 return data.ioc_dev;
2488 /* We need the full mdc name, and we shouldn't just grep from proc... */
2489 static void do_get_mdcname(char *obd_type_name, char *obd_name,
2490 char *obd_uuid, void *name)
2492 if (strncmp(obd_name, (char *)name, strlen((char *)name)) == 0)
2493 strcpy((char *)name, obd_name);
2496 static int get_mdcdev(const char *mdtname)
2498 char name[MAX_OBD_NAME];
2499 char *type[] = { "mdc" };
2502 strcpy(name, mdtname);
2503 rc = llapi_target_iterate(1, type, (void *)name, do_get_mdcname);
2504 rc = rc < 0 ? : -rc;
2506 llapi_err(LLAPI_MSG_ERROR, "Device %s not found %d\n", name,rc);
2509 return dev_name2dev(name);
2512 int llapi_changelog_clear(const char *mdtname, const char *idstr,
2515 struct obd_ioctl_data data;
2519 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2520 "can't purge negative records\n");
2524 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
2525 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
2526 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
2527 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2528 "expecting id of the form '"CHANGELOG_USER_PREFIX
2529 "<num>'; got '%s'\n", idstr);
2533 dev = get_mdcdev(mdtname);
2535 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2536 "can't find mdc for '%s'\n", mdtname);
2540 memset(&data, 0, sizeof(data));
2541 data.ioc_u32_1 = id;
2542 data.ioc_u64_1 = endrec;
2543 rc = dev_ioctl(&data, dev, OBD_IOC_CHANGELOG_CLEAR);
2545 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2546 "ioctl err %d", rc);
2551 int llapi_fid2path(char *device, char *fidstr, char *buf, int buflen,
2552 long long *recno, int *linkno)
2555 struct obd_ioctl_data data;
2559 while (*fidstr == '[')
2562 sscanf(fidstr, "0x%llx:0x%x:0x%x", &(fid.f_seq), &(fid.f_oid),
2564 if (!fid_is_sane(&fid)) {
2565 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2566 "bad FID format [%s], should be "DFID"\n",
2567 fidstr, (__u64)1, 2, 0);
2571 rc = get_mdtname(device, "%s%s", buffer);
2575 dev = dev_name2dev(buffer);
2579 memset(&data, 0, sizeof(data));
2580 data.ioc_inlbuf1 = (char *)&fid;
2581 data.ioc_inllen1 = sizeof(fid);
2582 data.ioc_inlbuf2 = (char *)recno;
2583 data.ioc_inllen2 = sizeof(__u64);
2584 data.ioc_inlbuf3 = (char *)linkno;
2585 data.ioc_inllen3 = sizeof(int);
2586 data.ioc_plen1 = buflen;
2587 data.ioc_pbuf1 = buf;
2588 rc = dev_ioctl(&data, dev, OBD_IOC_FID2PATH);
2593 int llapi_path2fid(const char *path, unsigned long long *seq,
2594 unsigned long *oid, unsigned long *ver)
2599 fd = open(path, O_RDONLY);
2603 rc = ioctl(fd, LL_IOC_PATH2FID, &fid);
2604 *seq = fid_seq(&fid);
2605 *oid = fid_oid(&fid);
2606 *ver = fid_ver(&fid);