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);
164 * size_units is unchanged if no specifier used
166 int parse_size(char *optarg, unsigned long long *size,
167 unsigned long long *size_units, int bytes_spec)
171 *size = strtoull(optarg, &end, 0);
174 if ((*end == 'b') && *(end+1) == '\0' &&
175 (*size & (~0ULL << (64 - 9))) == 0 &&
178 *size_units = 1 << 9;
179 } else if ((*end == 'b') && *(end+1) == '\0' &&
182 } else if ((*end == 'k' || *end == 'K') &&
183 *(end+1) == '\0' && (*size &
184 (~0ULL << (64 - 10))) == 0) {
186 *size_units = 1 << 10;
187 } else if ((*end == 'm' || *end == 'M') &&
188 *(end+1) == '\0' && (*size &
189 (~0ULL << (64 - 20))) == 0) {
191 *size_units = 1 << 20;
192 } else if ((*end == 'g' || *end == 'G') &&
193 *(end+1) == '\0' && (*size &
194 (~0ULL << (64 - 30))) == 0) {
196 *size_units = 1 << 30;
197 } else if ((*end == 't' || *end == 'T') &&
198 *(end+1) == '\0' && (*size &
199 (~0ULL << (64 - 40))) == 0) {
201 *size_units = 1ULL << 40;
202 } else if ((*end == 'p' || *end == 'P') &&
203 *(end+1) == '\0' && (*size &
204 (~0ULL << (64 - 50))) == 0) {
206 *size_units = 1ULL << 50;
207 } else if ((*end == 'e' || *end == 'E') &&
208 *(end+1) == '\0' && (*size &
209 (~0ULL << (64 - 60))) == 0) {
211 *size_units = 1ULL << 60;
220 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
221 int stripe_count, int stripe_pattern)
225 /* 64 KB is the largest common page size I'm aware of (on ia64), but
226 * check the local page size just in case. */
227 page_size = LOV_MIN_STRIPE_SIZE;
228 if (getpagesize() > page_size) {
229 page_size = getpagesize();
230 llapi_err_noerrno(LLAPI_MSG_WARN,
231 "warning: your page size (%u) is "
232 "larger than expected (%u)", page_size,
233 LOV_MIN_STRIPE_SIZE);
235 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
236 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
237 "must be an even multiple of %d bytes",
238 stripe_size, page_size);
241 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
243 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d",
247 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
249 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d",
253 if (stripe_size >= (1ULL << 32)){
255 llapi_err(LLAPI_MSG_ERROR, "warning: stripe size larger than 4G"
256 " is not currently supported and would wrap");
262 static int poolpath(char *fsname, char *pathname, char *pool_pathname);
264 int llapi_file_open_pool(const char *name, int flags, int mode,
265 unsigned long long stripe_size, int stripe_offset,
266 int stripe_count, int stripe_pattern, char *pool_name)
268 struct lov_user_md_v3 lum = { 0 };
271 char fsname[MAX_OBD_NAME + 1], *ptr;
273 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
274 if (fd < 0 && errno == EISDIR) {
275 fd = open(name, O_DIRECTORY | O_RDONLY);
281 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
285 if ((rc = llapi_stripe_limit_check(stripe_size, stripe_offset,
286 stripe_count, stripe_pattern)) != 0){
291 /* Initialize IOCTL striping pattern structure */
292 lum.lmm_magic = LOV_USER_MAGIC_V3;
293 lum.lmm_pattern = stripe_pattern;
294 lum.lmm_stripe_size = stripe_size;
295 lum.lmm_stripe_count = stripe_count;
296 lum.lmm_stripe_offset = stripe_offset;
298 /* in case user give the full pool name <fsname>.<poolname>, skip
300 if (pool_name != NULL) {
301 ptr = strchr(pool_name, '.');
303 strncpy(fsname, pool_name, ptr - pool_name);
305 /* if fsname matches a filesystem skip it
306 * if not keep the poolname as is */
307 if (poolpath(fsname, NULL, NULL) == 0)
310 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
312 /* If no pool is specified at all, use V1 request */
313 lum.lmm_magic = LOV_USER_MAGIC_V1;
316 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
317 char *errmsg = "stripe already set";
319 if (errno != EEXIST && errno != EALREADY)
320 errmsg = strerror(errno);
322 llapi_err_noerrno(LLAPI_MSG_ERROR,
323 "error on ioctl "LPX64" for '%s' (%d): %s",
324 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
335 int llapi_file_open(const char *name, int flags, int mode,
336 unsigned long long stripe_size, int stripe_offset,
337 int stripe_count, int stripe_pattern)
339 return llapi_file_open_pool(name, flags, mode, stripe_size,
340 stripe_offset, stripe_count,
341 stripe_pattern, NULL);
344 int llapi_file_create(const char *name, unsigned long long stripe_size,
345 int stripe_offset, int stripe_count, int stripe_pattern)
349 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
350 stripe_offset, stripe_count, stripe_pattern,
359 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
360 int stripe_offset, int stripe_count,
361 int stripe_pattern, char *pool_name)
365 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
366 stripe_offset, stripe_count, stripe_pattern,
375 static int print_pool_members(char *fs, char *pool_dir, char *pool_file)
377 char path[PATH_MAX + 1];
381 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fs, pool_file);
382 sprintf(path, "%s/%s", pool_dir, pool_file);
383 if ((fd = fopen(path, "r")) == NULL) {
384 llapi_err(LLAPI_MSG_ERROR, "Cannot open %s\n", path);
387 while (fgets(buf, sizeof(buf), fd) != NULL)
388 llapi_printf(LLAPI_MSG_NORMAL, buf);
395 * Find the fsname, the full path, and/or an open fd.
396 * Either the fsname or path must not be NULL
398 #define WANT_PATH 0x1
399 #define WANT_FSNAME 0x2
401 static int get_root_path(int want, char *fsname, int *outfd, char *path)
410 /* get the mount point */
411 fp = setmntent(MOUNTED, "r");
413 llapi_err(LLAPI_MSG_ERROR,
414 "setmntent(%s) failed: %s:", MOUNTED,
419 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
422 if (!llapi_is_lustre_mnt(&mnt))
425 ptr = strrchr(mnt.mnt_fsname, '/');
432 /* If path was specified and matches, store the fsname */
433 if ((want & WANT_FSNAME) && (strcmp(mnt.mnt_dir, path) == 0))
435 /* Else check the fsname for a match */
436 else if (strcmp(ptr, fsname) != 0)
441 if (want & WANT_PATH)
442 strcpy(path, mnt.mnt_dir);
443 if (want & WANT_FD) {
444 fd = open(mnt.mnt_dir,
445 O_RDONLY | O_DIRECTORY | O_NONBLOCK);
457 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
458 "can't find fs root for '%s': %d",
459 (want & WANT_PATH) ? fsname : path, rc);
463 int llapi_search_fsname(const char *pathname, char *fsname)
465 return get_root_path(WANT_FSNAME, fsname, NULL, (char *)pathname);
468 /* return the first file matching this pattern */
469 static int first_match(char *pattern, char *buffer)
473 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
476 if (glob_info.gl_pathc < 1) {
477 globfree(&glob_info);
481 strcpy(buffer, glob_info.gl_pathv[0]);
483 globfree(&glob_info);
488 * find the pool directory path under /proc
489 * (can be also used to test if a fsname is known)
491 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
494 char pattern[PATH_MAX + 1];
495 char buffer[PATH_MAX];
497 if (fsname == NULL) {
498 rc = get_root_path(WANT_FSNAME, buffer, NULL, pathname);
502 strcpy(pathname, fsname);
505 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
506 rc = first_match(pattern, buffer);
510 /* in fsname test mode, pool_pathname is NULL */
511 if (pool_pathname != NULL)
512 strcpy(pool_pathname, buffer);
517 int llapi_poollist(char *name)
521 char rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
525 /* is name a pathname ? */
526 ptr = strchr(name, '/');
528 /* only absolute pathname is supported */
531 if (!realpath(name, rname)) {
533 llapi_err(LLAPI_MSG_ERROR, "invalid path '%s'", name);
537 rc = poolpath(NULL, rname, pathname);
540 llapi_err(LLAPI_MSG_ERROR, "'%s' is not"
541 " a Lustre filesystem", name);
547 /* name is FSNAME[.POOLNAME] */
549 poolname = strchr(name, '.');
550 if (poolname != NULL) {
554 rc = poolpath(fsname, NULL, pathname);
558 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
563 if (poolname != NULL) {
564 rc = print_pool_members(fsname, pathname, poolname);
571 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
572 if ((dir = opendir(pathname)) == NULL) {
575 while ((pool = readdir(dir)) != NULL) {
576 if (!((pool->d_name[0] == '.') &&
577 (pool->d_name[1] == '\0')) &&
578 !((pool->d_name[0] == '.') &&
579 (pool->d_name[1] == '.') &&
580 (pool->d_name[2] == '\0')))
581 llapi_printf(LLAPI_MSG_NORMAL, " %s.%s\n",
582 fsname, pool->d_name);
589 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
590 void *data, cfs_dirent_t *de);
592 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
593 #define OBD_NOT_FOUND (-1)
595 static int common_param_init(struct find_param *param)
597 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
598 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
599 llapi_err(LLAPI_MSG_ERROR,
600 "error: allocation of %d bytes for ioctl",
601 sizeof(lstat_t) + param->lumlen);
605 param->got_uuids = 0;
606 param->obdindexes = NULL;
607 param->obdindex = OBD_NOT_FOUND;
611 static void find_param_fini(struct find_param *param)
613 if (param->obdindexes)
614 free(param->obdindexes);
620 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
622 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
625 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
630 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
634 fd = open(path, O_RDONLY);
637 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
641 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
649 * If uuidp is NULL, return the number of available obd uuids.
650 * If uuidp is non-NULL, then it will return the uuids of the obds. If
651 * there are more OSTs then allocated to uuidp, then an error is returned with
652 * the ost_count set to number of available obd uuids.
654 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
656 struct obd_uuid lov_name;
659 int rc = 0, index = 0;
661 /* Get the lov name */
662 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
666 /* Now get the ost uuids from /proc */
667 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
669 fp = fopen(buf, "r");
672 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
676 while (fgets(buf, sizeof(buf), fp) != NULL) {
677 if (uuidp && (index < *ost_count)) {
678 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
686 if (uuidp && (index >= *ost_count))
693 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
700 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
705 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
711 /* Here, param->obduuid points to a single obduuid, the index of which is
712 * returned in param->obdindex */
713 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
715 struct obd_uuid lov_uuid;
716 char uuid[sizeof(struct obd_uuid)];
721 /* Get the lov name */
722 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
724 if (errno != ENOTTY) {
726 llapi_err(LLAPI_MSG_ERROR,
727 "error: can't get lov name: %s", dname);
734 param->got_uuids = 1;
736 /* Now get the ost uuids from /proc */
737 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
739 fp = fopen(buf, "r");
742 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
746 if (!param->obduuid && !param->quiet && !param->obds_printed)
747 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
749 while (fgets(buf, sizeof(buf), fp) != NULL) {
750 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
753 if (param->obduuid) {
754 if (strncmp(param->obduuid->uuid, uuid,
755 sizeof(uuid)) == 0) {
756 param->obdindex = index;
759 } else if (!param->quiet && !param->obds_printed) {
760 /* Print everything */
761 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
764 param->obds_printed = 1;
768 if (!param->quiet && param->obduuid &&
769 (param->obdindex == OBD_NOT_FOUND)) {
770 llapi_err_noerrno(LLAPI_MSG_ERROR,
771 "error: %s: unknown obduuid: %s",
772 __FUNCTION__, param->obduuid->uuid);
779 /* In this case, param->obduuid will be an array of obduuids and
780 * obd index for all these obduuids will be returned in
781 * param->obdindexes */
782 static int setup_obd_indexes(DIR *dir, struct find_param *param)
784 struct obd_uuid *uuids = NULL;
785 int obdcount = INIT_ALLOC_NUM_OSTS;
786 int ret, obd_valid = 0, obdnum, i;
788 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
789 sizeof(struct obd_uuid));
794 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
797 struct obd_uuid *uuids_temp;
799 if (ret == -EOVERFLOW) {
800 uuids_temp = realloc(uuids, obdcount *
801 sizeof(struct obd_uuid));
802 if (uuids_temp != NULL)
803 goto retry_get_uuids;
808 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
812 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
813 if (param->obdindexes == NULL)
816 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
817 for (i = 0; i <= obdcount; i++) {
818 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
819 (char *)&uuids[i]) == 0) {
820 param->obdindexes[obdnum] = i;
826 param->obdindexes[obdnum] = OBD_NOT_FOUND;
830 param->obdindex = OBD_NOT_FOUND;
832 param->obdindex = obd_valid;
834 param->got_uuids = 1;
839 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
840 struct lov_user_ost_data_v1 *objects,
841 char *path, int is_dir,
842 int obdindex, int quiet, int header, int body)
844 int i, obdstripe = 0;
846 if (obdindex != OBD_NOT_FOUND) {
847 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
848 if (obdindex == objects[i].l_ost_idx) {
849 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
855 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
859 /* if it's a directory */
861 if (obdstripe == 1) {
862 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
863 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
864 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
866 llapi_printf(LLAPI_MSG_NORMAL,
867 "stripe_count: %d stripe_size: %u "
868 "stripe_offset: %d%s%s\n",
869 lum->lmm_stripe_count == (__u16)-1 ? -1 :
870 lum->lmm_stripe_count,
871 lum->lmm_stripe_size,
872 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
873 lum->lmm_stripe_offset,
874 pool_name != NULL ? " pool: " : "",
875 pool_name != NULL ? pool_name : "");
880 if (header && (obdstripe == 1)) {
881 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
883 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
885 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
887 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
888 (int)lum->lmm_stripe_count);
889 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
890 lum->lmm_stripe_size);
891 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
893 if (pool_name != NULL)
894 llapi_printf(LLAPI_MSG_NORMAL,
895 "lmm_pool_name: %s\n", pool_name);
899 if ((!quiet) && (obdstripe == 1))
900 llapi_printf(LLAPI_MSG_NORMAL,
901 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
903 for (i = 0; i < lum->lmm_stripe_count; i++) {
904 int idx = objects[i].l_ost_idx;
905 long long oid = objects[i].l_object_id;
906 long long gr = objects[i].l_object_gr;
907 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
908 llapi_printf(LLAPI_MSG_NORMAL,
909 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
911 obdindex == idx ? " *" : "");
913 llapi_printf(LLAPI_MSG_NORMAL, "\n");
917 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
918 int is_dir, int obdindex, int quiet,
919 int header, int body)
921 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
922 int i, obdstripe = 0;
924 if (obdindex != OBD_NOT_FOUND) {
925 for (i = 0; i < lumj->lmm_stripe_count; i++) {
926 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
927 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
933 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
937 if (header && obdstripe == 1) {
938 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
940 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
941 lumj->lmm_object_gr);
942 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
943 lumj->lmm_object_id);
944 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
945 (int)lumj->lmm_stripe_count);
946 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
947 lumj->lmm_stripe_size);
948 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
950 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
951 lumj->lmm_extent_count);
955 unsigned long long start = -1, end = 0;
956 if (!quiet && obdstripe == 1)
957 llapi_printf(LLAPI_MSG_NORMAL,
958 "joined\tobdidx\t\t objid\t\tobjid\t\t "
959 "group\t\tstart\t\tend\n");
960 for (i = 0; i < lumj->lmm_stripe_count; i++) {
961 int idx = lumj->lmm_objects[i].l_ost_idx;
962 long long oid = lumj->lmm_objects[i].l_object_id;
963 long long gr = lumj->lmm_objects[i].l_object_gr;
964 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
965 llapi_printf(LLAPI_MSG_NORMAL,
966 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
968 obdindex == idx ? " *" : "");
969 if (start != lumj->lmm_objects[i].l_extent_start ||
970 end != lumj->lmm_objects[i].l_extent_end) {
971 start = lumj->lmm_objects[i].l_extent_start;
972 llapi_printf(LLAPI_MSG_NORMAL,"\t%14llu",start);
973 end = lumj->lmm_objects[i].l_extent_end;
974 if (end == (unsigned long long)-1)
975 llapi_printf(LLAPI_MSG_NORMAL,
978 llapi_printf(LLAPI_MSG_NORMAL,
981 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
984 llapi_printf(LLAPI_MSG_NORMAL, "\n");
988 void llapi_lov_dump_user_lmm(struct find_param *param,
989 char *path, int is_dir)
991 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
992 case LOV_USER_MAGIC_V1:
993 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
994 param->lmd->lmd_lmm.lmm_objects,
996 param->obdindex, param->quiet,
998 (param->verbose || !param->obduuid));
1000 case LOV_USER_MAGIC_JOIN:
1001 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
1002 param->obdindex, param->quiet,
1004 (param->verbose || !param->obduuid));
1006 case LOV_USER_MAGIC_V3: {
1007 char pool_name[LOV_MAXPOOLNAME + 1];
1008 struct lov_user_ost_data_v1 *objects;
1009 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1011 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1012 pool_name[LOV_MAXPOOLNAME] = '\0';
1013 objects = lmmv3->lmm_objects;
1014 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
1015 objects, path, is_dir,
1016 param->obdindex, param->quiet,
1018 (param->verbose || !param->obduuid));
1022 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1023 "(expecting one of %#x %#x %#x)\n",
1024 *(__u32 *)¶m->lmd->lmd_lmm,
1025 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_JOIN,
1031 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1037 fname = strrchr(path, '/');
1039 /* It should be a file (or other non-directory) */
1040 if (fname == NULL) {
1041 dname = (char *)malloc(2);
1045 fname = (char *)path;
1047 dname = (char *)malloc(fname - path + 1);
1050 strncpy(dname, path, fname - path);
1051 dname[fname - path] = '\0';
1055 if ((fd = open(dname, O_RDONLY)) == -1) {
1061 strcpy((char *)lum, fname);
1062 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1065 if (close(fd) == -1 && rc == 0)
1073 int llapi_file_lookup(int dirfd, const char *name)
1075 struct obd_ioctl_data data = { 0 };
1080 if (dirfd < 0 || name == NULL)
1083 data.ioc_version = OBD_IOCTL_VERSION;
1084 data.ioc_len = sizeof(data);
1085 data.ioc_inlbuf1 = (char *)name;
1086 data.ioc_inllen1 = strlen(name) + 1;
1088 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1090 llapi_err(LLAPI_MSG_ERROR,
1091 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1096 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1099 int llapi_mds_getfileinfo(char *path, DIR *parent,
1100 struct lov_user_mds_data *lmd)
1102 lstat_t *st = &lmd->lmd_st;
1103 char *fname = strrchr(path, '/');
1109 fname = (fname == NULL ? path : fname + 1);
1110 /* retrieve needed file info */
1111 strncpy((char *)lmd, fname,
1112 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
1113 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1116 if (errno == ENOTTY) {
1117 /* ioctl is not supported, it is not a lustre fs.
1118 * Do the regular lstat(2) instead. */
1119 ret = lstat_f(path, st);
1121 llapi_err(LLAPI_MSG_ERROR,
1122 "error: %s: lstat failed for %s",
1123 __FUNCTION__, path);
1126 } else if (errno == ENOENT) {
1127 llapi_err(LLAPI_MSG_WARN,
1128 "warning: %s: %s does not exist",
1129 __FUNCTION__, path);
1132 llapi_err(LLAPI_MSG_ERROR,
1133 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1134 __FUNCTION__, path);
1142 static DIR *opendir_parent(char *path)
1148 fname = strrchr(path, '/');
1150 return opendir(".");
1154 parent = opendir(path);
1159 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1160 semantic_func_t sem_init,
1161 semantic_func_t sem_fini, void *data,
1172 if (!d && errno != ENOTDIR) {
1173 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
1174 __FUNCTION__, path);
1176 } else if (!d && !parent) {
1177 /* ENOTDIR. Open the parent dir. */
1178 p = opendir_parent(path);
1180 GOTO(out, ret = -EINVAL);
1183 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1189 while ((dent = readdir64(d)) != NULL) {
1190 ((struct find_param *)data)->have_fileinfo = 0;
1192 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1195 /* Don't traverse .lustre directory */
1196 if (!(strcmp(dent->d_name, dot_lustre_name)))
1200 if ((len + dent->d_reclen + 2) > size) {
1201 llapi_err(LLAPI_MSG_ERROR,
1202 "error: %s: string buffer is too small",
1207 strcat(path, dent->d_name);
1209 if (dent->d_type == DT_UNKNOWN) {
1210 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1212 ret = llapi_mds_getfileinfo(path, d,
1213 ((struct find_param *)data)->lmd);
1215 ((struct find_param *)data)->have_fileinfo = 1;
1217 llapi_filetype_dir_table[st->st_mode &
1224 switch (dent->d_type) {
1226 llapi_err(LLAPI_MSG_ERROR,
1227 "error: %s: '%s' is UNKNOWN type %d",
1228 __FUNCTION__, dent->d_name, dent->d_type);
1231 ret = llapi_semantic_traverse(path, size, d, sem_init,
1232 sem_fini, data, dent);
1239 ret = sem_init(path, d, NULL, data, dent);
1243 if (sem_fini && ret == 0)
1244 sem_fini(path, d, NULL, data, dent);
1252 sem_fini(path, parent, d, data, de);
1261 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1262 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1264 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1265 * The table below gives the answers for the specified parameters (value and
1266 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1267 * --------------------------------------
1268 * 1 | file > limit; sign > 0 | -1 / -1 |
1269 * 2 | file = limit; sign > 0 | ? / 1 |
1270 * 3 | file < limit; sign > 0 | ? / 1 |
1271 * 4 | file > limit; sign = 0 | -1 / -1 |
1272 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1273 * 6 | file < limit; sign = 0 | ? / -1 |
1274 * 7 | file > limit; sign < 0 | 1 / 1 |
1275 * 8 | file = limit; sign < 0 | ? / -1 |
1276 * 9 | file < limit; sign < 0 | ? / -1 |
1277 * --------------------------------------
1278 * Note: 5th actually means that the value is within the interval
1279 * (limit - margin, limit]. */
1280 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1281 unsigned long long margin, int mds)
1289 if (file <= limit && file + margin > limit)
1291 if (file + margin <= limit)
1292 return mds ? 0 : -1;
1305 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1306 * Return -1 or 1 if file timestamp does not or does match the given criteria
1307 * correspondingly. Return 0 if the MDS time is being checked and there are
1308 * attributes on OSTs and it is not yet clear if the timespamp matches.
1310 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1311 * updated timestamps. */
1312 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1317 /* Check if file is accepted. */
1319 ret = find_value_cmp(st->st_atime, param->atime,
1320 param->asign, 24 * 60 * 60, mds);
1327 ret = find_value_cmp(st->st_mtime, param->mtime,
1328 param->msign, 24 * 60 * 60, mds);
1332 /* If the previous check matches, but this one is not yet clear,
1333 * we should return 0 to do an RPC on OSTs. */
1339 ret = find_value_cmp(st->st_ctime, param->ctime,
1340 param->csign, 24 * 60 * 60, mds);
1344 /* If the previous check matches, but this one is not yet clear,
1345 * we should return 0 to do an RPC on OSTs. */
1353 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1354 void *data, cfs_dirent_t *de)
1356 struct find_param *param = (struct find_param *)data;
1357 int decision = 1; /* 1 is accepted; -1 is rejected. */
1358 lstat_t *st = ¶m->lmd->lmd_st;
1360 int checked_type = 0;
1363 LASSERT(parent != NULL || dir != NULL);
1365 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1367 /* If a regular expression is presented, make the initial decision */
1368 if (param->pattern != NULL) {
1369 char *fname = strrchr(path, '/');
1370 fname = (fname == NULL ? path : fname + 1);
1371 ret = fnmatch(param->pattern, fname, 0);
1372 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1373 (ret == 0 && param->exclude_pattern))
1377 /* See if we can check the file type from the dirent. */
1378 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1379 de->d_type < DT_MAX) {
1381 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1382 if (param->exclude_type)
1385 if (!param->exclude_type)
1391 /* If a time or OST should be checked, the decision is not taken yet. */
1392 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1397 /* Request MDS for the stat info. */
1398 if (param->have_fileinfo == 0) {
1400 /* retrieve needed file info */
1401 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1402 (void *)param->lmd);
1404 char *fname = strrchr(path, '/');
1405 fname = (fname == NULL ? path : fname + 1);
1407 /* retrieve needed file info */
1408 strncpy((char *)param->lmd, fname, param->lumlen);
1409 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1410 (void *)param->lmd);
1415 if (errno == ENOTTY) {
1416 /* ioctl is not supported, it is not a lustre fs.
1417 * Do the regular lstat(2) instead. */
1419 ret = lstat_f(path, st);
1421 llapi_err(LLAPI_MSG_ERROR,
1422 "error: %s: lstat failed for %s",
1423 __FUNCTION__, path);
1426 } else if (errno == ENOENT) {
1427 llapi_err(LLAPI_MSG_WARN,
1428 "warning: %s: %s does not exist",
1429 __FUNCTION__, path);
1432 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1433 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1434 "IOC_MDC_GETFILEINFO", path);
1439 if (param->type && !checked_type) {
1440 if ((st->st_mode & S_IFMT) == param->type) {
1441 if (param->exclude_type)
1444 if (!param->exclude_type)
1450 if (param->obduuid) {
1451 if (lustre_fs && param->got_uuids &&
1452 param->st_dev != st->st_dev) {
1453 /* A lustre/lustre mount point is crossed. */
1454 param->got_uuids = 0;
1455 param->obds_printed = 0;
1456 param->obdindex = OBD_NOT_FOUND;
1459 if (lustre_fs && !param->got_uuids) {
1460 ret = setup_obd_indexes(dir ? dir : parent, param);
1464 param->st_dev = st->st_dev;
1465 } else if (!lustre_fs && param->got_uuids) {
1466 /* A lustre/non-lustre mount point is crossed. */
1467 param->got_uuids = 0;
1468 param->obdindex = OBD_NOT_FOUND;
1472 /* If an OBD UUID is specified but no one matches, skip this file. */
1473 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1476 /* If a OST UUID is given, and some OST matches, check it here. */
1477 if (param->obdindex != OBD_NOT_FOUND) {
1478 if (!S_ISREG(st->st_mode))
1481 /* Only those files should be accepted, which have a
1482 * stripe on the specified OST. */
1483 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1487 struct lov_user_ost_data_v1 *lmm_objects;
1489 if (param->lmd->lmd_lmm.lmm_magic ==
1490 LOV_USER_MAGIC_V3) {
1491 struct lov_user_md_v3 *lmmv3 =
1492 (void *)¶m->lmd->lmd_lmm;
1494 lmm_objects = lmmv3->lmm_objects;
1496 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1500 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1501 for (j = 0; j < param->num_obds; j++) {
1502 if (param->obdindexes[j] ==
1503 lmm_objects[i].l_ost_idx)
1508 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1513 if (param->check_uid) {
1514 if (st->st_uid == param->uid) {
1515 if (param->exclude_uid)
1518 if (!param->exclude_uid)
1523 if (param->check_gid) {
1524 if (st->st_gid == param->gid) {
1525 if (param->exclude_gid)
1528 if (!param->exclude_gid)
1533 if (param->check_pool) {
1534 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1536 /* empty requested pool is taken as no pool search => V1 */
1537 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1538 (param->poolname[0] == '\0')) ||
1539 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1540 (strncmp(lmmv3->lmm_pool_name,
1541 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1542 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1543 (strcmp(param->poolname, "*") == 0))) {
1544 if (param->exclude_pool)
1547 if (!param->exclude_pool)
1552 /* Check the time on mds. */
1556 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1557 param->lmd->lmd_lmm.lmm_stripe_count)
1559 decision = find_time_check(st, param, for_mds);
1563 /* If file still fits the request, ask ost for updated info.
1564 The regular stat is almost of the same speed as some new
1565 'glimpse-size-ioctl'. */
1566 if (!decision && S_ISREG(st->st_mode) &&
1567 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1568 if (param->obdindex != OBD_NOT_FOUND) {
1569 /* Check whether the obd is active or not, if it is
1570 * not active, just print the object affected by this
1573 struct obd_statfs stat_buf;
1574 struct obd_uuid uuid_buf;
1576 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1577 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1578 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1579 param->obdindex, &stat_buf,
1582 if (ret == -ENODATA || ret == -ENODEV
1585 llapi_printf(LLAPI_MSG_NORMAL,
1586 "obd_uuid: %s failed %s ",
1587 param->obduuid->uuid,
1593 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1594 (void *)param->lmd);
1595 } else if (parent) {
1596 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1597 (void *)param->lmd);
1601 if (errno == ENOENT) {
1602 llapi_err(LLAPI_MSG_ERROR,
1603 "warning: %s: %s does not exist",
1604 __FUNCTION__, path);
1607 llapi_err(LLAPI_MSG_ERROR,
1608 "%s: IOC_LOV_GETINFO on %s failed",
1609 __FUNCTION__, path);
1614 /* Check the time on osc. */
1615 decision = find_time_check(st, param, 0);
1621 decision = find_value_cmp(st->st_size, param->size,
1622 param->size_sign, param->size_units,
1626 if (decision != -1) {
1627 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1629 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1631 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1635 /* Do not get down anymore? */
1636 if (param->depth == param->maxdepth)
1643 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1646 struct find_param *param = (struct find_param *)data;
1651 int llapi_find(char *path, struct find_param *param)
1654 int ret, len = strlen(path);
1656 if (len > PATH_MAX) {
1657 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1658 __FUNCTION__, path);
1662 buf = (char *)malloc(PATH_MAX + 1);
1666 ret = common_param_init(param);
1674 strncpy(buf, path, PATH_MAX + 1);
1675 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1676 cb_common_fini, param, NULL);
1678 find_param_fini(param);
1680 return ret < 0 ? ret : 0;
1683 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1686 struct find_param *param = (struct find_param *)data;
1689 LASSERT(parent != NULL || d != NULL);
1692 if (!param->got_uuids) {
1693 ret = setup_obd_uuid(d ? d : parent, path, param);
1699 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1700 (void *)¶m->lmd->lmd_lmm);
1701 } else if (parent) {
1702 char *fname = strrchr(path, '/');
1703 fname = (fname == NULL ? path : fname + 1);
1705 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1706 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1707 (void *)¶m->lmd->lmd_lmm);
1711 if (errno == ENODATA) {
1712 if (!param->obduuid && !param->quiet)
1713 llapi_printf(LLAPI_MSG_NORMAL,
1714 "%s has no stripe info\n", path);
1716 } else if (errno == ENOTTY) {
1717 llapi_err(LLAPI_MSG_ERROR,
1718 "%s: '%s' not on a Lustre fs?",
1719 __FUNCTION__, path);
1720 } else if (errno == ENOENT) {
1721 llapi_err(LLAPI_MSG_WARN,
1722 "warning: %s: %s does not exist",
1723 __FUNCTION__, path);
1726 llapi_err(LLAPI_MSG_ERROR,
1727 "error: %s: %s failed for %s",
1728 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1729 "IOC_MDC_GETFILESTRIPE", path);
1735 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1737 /* Do not get down anymore? */
1738 if (param->depth == param->maxdepth)
1745 int llapi_getstripe(char *path, struct find_param *param)
1748 int ret = 0, len = strlen(path);
1750 if (len > PATH_MAX) {
1751 llapi_err(LLAPI_MSG_ERROR,
1752 "%s: Path name '%s' is too long",
1753 __FUNCTION__, path);
1757 buf = (char *)malloc(PATH_MAX + 1);
1761 ret = common_param_init(param);
1769 strncpy(buf, path, PATH_MAX + 1);
1770 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1771 cb_common_fini, param, NULL);
1772 find_param_fini(param);
1774 return ret < 0 ? ret : 0;
1777 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1778 struct obd_statfs *stat_buf,
1779 struct obd_uuid *uuid_buf)
1782 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1784 struct obd_ioctl_data data = { 0 };
1787 data.ioc_inlbuf1 = (char *)&type;
1788 data.ioc_inllen1 = sizeof(__u32);
1789 data.ioc_inlbuf2 = (char *)&index;
1790 data.ioc_inllen2 = sizeof(__u32);
1791 data.ioc_pbuf1 = (char *)stat_buf;
1792 data.ioc_plen1 = sizeof(struct obd_statfs);
1793 data.ioc_pbuf2 = (char *)uuid_buf;
1794 data.ioc_plen2 = sizeof(struct obd_uuid);
1796 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1797 llapi_err(LLAPI_MSG_ERROR,
1798 "llapi_obd_statfs: error packing ioctl data");
1802 fd = open(path, O_RDONLY);
1803 if (errno == EISDIR)
1804 fd = open(path, O_DIRECTORY | O_RDONLY);
1807 rc = errno ? -errno : -EBADF;
1808 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
1809 __FUNCTION__, path);
1812 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1814 rc = errno ? -errno : -EINVAL;
1820 #define MAX_STRING_SIZE 128
1821 #define DEVICES_LIST "/proc/fs/lustre/devices"
1823 int llapi_ping(char *obd_type, char *obd_name)
1825 char path[MAX_STRING_SIZE];
1829 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1830 obd_type, obd_name);
1832 fd = open(path, O_WRONLY);
1835 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1839 rc = write(fd, buf, 1);
1847 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
1849 char buf[MAX_STRING_SIZE];
1850 FILE *fp = fopen(DEVICES_LIST, "r");
1855 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1859 while (fgets(buf, sizeof(buf), fp) != NULL) {
1860 char *obd_type_name = NULL;
1861 char *obd_name = NULL;
1862 char *obd_uuid = NULL;
1864 struct obd_ioctl_data datal = { 0, };
1865 struct obd_statfs osfs_buffer;
1867 while(bufp[0] == ' ')
1870 for(i = 0; i < 3; i++) {
1871 obd_type_name = strsep(&bufp, " ");
1873 obd_name = strsep(&bufp, " ");
1874 obd_uuid = strsep(&bufp, " ");
1876 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1878 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1879 datal.ioc_plen1 = sizeof(osfs_buffer);
1881 for (i = 0; i < type_num; i++) {
1882 if (strcmp(obd_type_name, obd_type[i]) != 0)
1885 cb(obd_type_name, obd_name, obd_uuid, args);
1892 static void do_target_check(char *obd_type_name, char *obd_name,
1893 char *obd_uuid, void *args)
1897 rc = llapi_ping(obd_type_name, obd_name);
1898 if (rc == ENOTCONN) {
1899 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
1901 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1903 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1907 int llapi_target_check(int type_num, char **obd_type, char *dir)
1909 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1912 #undef MAX_STRING_SIZE
1914 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1916 char raw[OBD_MAX_IOCTL_BUFFER];
1917 char out[LLOG_CHUNK_SIZE];
1919 struct obd_ioctl_data data = { 0 };
1924 sprintf(key, "%s", keyword);
1925 memset(raw, 0, sizeof(raw));
1926 memset(out, 0, sizeof(out));
1927 data.ioc_inlbuf1 = key;
1928 data.ioc_inllen1 = strlen(key) + 1;
1930 data.ioc_inlbuf2 = node_name;
1931 data.ioc_inllen2 = strlen(node_name) + 1;
1933 data.ioc_pbuf1 = out;
1934 data.ioc_plen1 = sizeof(out);
1935 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1939 root = opendir(dir);
1942 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1946 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1948 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1950 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1956 /* Is this a lustre fs? */
1957 int llapi_is_lustre_mnttype(const char *type)
1959 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1962 /* Is this a lustre client fs? */
1963 int llapi_is_lustre_mnt(struct mntent *mnt)
1965 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1966 strstr(mnt->mnt_fsname, ":/") != NULL);
1969 int llapi_quotacheck(char *mnt, int check_type)
1974 root = opendir(mnt);
1976 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1980 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1986 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1992 root = opendir(mnt);
1994 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1999 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2003 if (poll_intvl < 30)
2011 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2016 root = opendir(mnt);
2018 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2022 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2028 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2031 struct find_param *param = (struct find_param *)data;
2035 LASSERT(parent != NULL || d != NULL);
2038 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2039 (void *)param->lmd);
2040 } else if (parent) {
2041 char *fname = strrchr(path, '/');
2042 fname = (fname == NULL ? path : fname + 1);
2044 strncpy((char *)param->lmd, fname, param->lumlen);
2045 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2046 (void *)param->lmd);
2052 if (errno == ENODATA) {
2053 if (!param->obduuid && !param->quiet)
2054 llapi_err(LLAPI_MSG_ERROR,
2055 "%s has no stripe info", path);
2057 } else if (errno == ENOENT) {
2058 llapi_err(LLAPI_MSG_ERROR,
2059 "warning: %s: %s does not exist",
2060 __FUNCTION__, path);
2062 } else if (errno != EISDIR) {
2064 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2065 d ? "LL_IOC_MDC_GETINFO" :
2066 "IOC_MDC_GETFILEINFO", path);
2071 st = ¶m->lmd->lmd_st;
2073 /* libc chown() will do extra check, and if the real owner is
2074 * the same as the ones to set, it won't fall into kernel, so
2075 * invoke syscall directly. */
2076 rc = syscall(SYS_chown, path, -1, -1);
2078 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2080 rc = chmod(path, st->st_mode);
2082 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2088 int llapi_quotachown(char *path, int flag)
2090 struct find_param param;
2092 int ret = 0, len = strlen(path);
2094 if (len > PATH_MAX) {
2095 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
2096 __FUNCTION__, path);
2100 buf = (char *)malloc(PATH_MAX + 1);
2104 memset(¶m, 0, sizeof(param));
2105 param.recursive = 1;
2109 ret = common_param_init(¶m);
2113 strncpy(buf, path, PATH_MAX + 1);
2114 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
2115 NULL, ¶m, NULL);
2117 find_param_fini(¶m);
2125 #include <sys/wait.h>
2129 static int rmtacl_notify(int ops)
2133 int found = 0, fd, rc;
2135 fp = setmntent(MOUNTED, "r");
2137 perror("setmntent");
2142 mnt = getmntent(fp);
2146 if (!llapi_is_lustre_mnt(mnt))
2149 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2155 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2167 static char *next_token(char *p, int div)
2173 while (*p && *p != ':' && !isspace(*p))
2176 while (*p == ':' || isspace(*p))
2179 return *p ? p : NULL;
2182 static int rmtacl_name2id(char *name, int is_user)
2187 if ((pw = getpwnam(name)) == NULL)
2190 return (int)(pw->pw_uid);
2194 if ((gr = getgrnam(name)) == NULL)
2197 return (int)(gr->gr_gid);
2201 static int isodigit(int c)
2203 return (c >= '0' && c <= '7') ? 1 : 0;
2207 * Whether the name is just digits string (uid/gid) already or not.
2212 static int str_is_id(char *str)
2219 if (*str == 'x' || *str == 'X') { /* for Hex. */
2220 if (!isxdigit(*(++str)))
2223 while (isxdigit(*(++str)));
2224 } else if (isodigit(*str)) { /* for Oct. */
2225 while (isodigit(*(++str)));
2227 } else if (isdigit(*str)) { /* for Dec. */
2228 while (isdigit(*(++str)));
2231 return (*str == 0) ? 1 : 0;
2241 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2243 static rmtacl_name_t rmtacl_namelist[] = {
2244 { RMTACL_OPTNAME("user:"), 1, 0 },
2245 { RMTACL_OPTNAME("group:"), 0, 0 },
2246 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2247 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2248 /* for --tabular option */
2249 { RMTACL_OPTNAME("user"), 1, 1 },
2250 { RMTACL_OPTNAME("group"), 0, 1 },
2254 static int rgetfacl_output(char *str)
2256 char *start = NULL, *end = NULL;
2257 int is_user = 0, n, id;
2264 for (rn = rmtacl_namelist; rn->name; rn++) {
2265 if(strncmp(str, rn->name, rn->length) == 0) {
2266 if (!rn->next_token)
2267 start = str + rn->length;
2269 start = next_token(str + rn->length, 0);
2270 is_user = rn->is_user;
2275 end = next_token(start, 1);
2276 if (end == NULL || start == end) {
2277 n = printf("%s", str);
2283 id = rmtacl_name2id(start, is_user);
2284 if (id == INVALID_ID) {
2285 if (str_is_id(start)) {
2287 n = printf("%s", str);
2290 } else if ((id == NOBODY_UID && is_user) ||
2291 (id == NOBODY_GID && !is_user)) {
2293 n = printf("%s", str);
2297 n = printf("%s%d%s", str, id, end);
2302 static int child_status(int status)
2304 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2307 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2320 if ((pid = fork()) < 0) {
2326 /* child process redirects its output. */
2329 if (dup2(fd[1], 1) < 0) {
2340 status = rmtacl_notify(ops);
2344 exit(execvp(argv[0], argv));
2347 /* the following is parent process */
2348 if ((fp = fdopen(fd[0], "r")) == NULL) {
2355 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2356 if (output_func(buf) < 0)
2357 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2363 if (waitpid(pid, &status, 0) < 0) {
2368 return child_status(status);
2371 int llapi_lsetfacl(int argc, char *argv[])
2373 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2376 int llapi_lgetfacl(int argc, char *argv[])
2378 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2381 int llapi_rsetfacl(int argc, char *argv[])
2383 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2386 int llapi_rgetfacl(int argc, char *argv[])
2388 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2391 int llapi_cp(int argc, char *argv[])
2395 rc = rmtacl_notify(RMT_RSETFACL);
2399 exit(execvp(argv[0], argv));
2402 int llapi_ls(int argc, char *argv[])
2406 rc = rmtacl_notify(RMT_LGETFACL);
2410 exit(execvp(argv[0], argv));
2413 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
2414 * format must have %s%s, buf must be > 16
2416 static int get_mdtname(const char *name, char *format, char *buf)
2418 char suffix[]="-MDT0000";
2419 int len = strlen(name);
2422 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
2425 /* Not enough room to add suffix */
2426 llapi_err(LLAPI_MSG_ERROR, "MDT name too long |%s|\n",
2432 return sprintf(buf, format, name, suffix);
2436 /* Return a file descriptor to a readable changelog */
2437 int llapi_changelog_open(const char *device, long long startrec)
2443 if (device[0] == '/')
2444 rc = get_root_path(WANT_FSNAME, mdtname, NULL, (char *)device);
2446 strncpy(mdtname, device, sizeof(mdtname));
2448 /* Use either the mdd changelog (preferred) or a client mdc changelog */
2449 if (get_mdtname(mdtname,
2450 "/proc/fs/lustre/md[cd]/%s%s{,-mdc-*}/changelog",
2453 rc = first_match(path, path);
2457 if ((fd = open(path, O_RDONLY)) < 0) {
2458 llapi_err(LLAPI_MSG_ERROR, "error: can't open |%s|\n", path);
2462 rc = lseek(fd, (off_t)startrec, SEEK_SET);
2464 llapi_err(LLAPI_MSG_ERROR, "can't seek rc=%d\n", rc);
2471 int llapi_changelog_clear(const char *mdtname, const char *idstr,
2474 struct ioc_changelog_clear data;
2477 int id, fd, index, rc;
2480 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2481 "can't purge negative records\n");
2485 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
2486 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
2487 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
2488 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2489 "expecting id of the form '"CHANGELOG_USER_PREFIX
2490 "<num>'; got '%s'\n", idstr);
2494 /* Take path, fsname, or MDTNAME. Assume MDT0000 in the former cases */
2495 if (mdtname[0] == '/') {
2497 fd = open(mdtname, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
2498 rc = fd < 0 ? -errno : 0;
2500 if (get_mdtname(mdtname, "%s%s", fsname) < 0)
2502 ptr = fsname + strlen(fsname) - 8;
2504 index = strtol(ptr + 4, NULL, 10);
2505 rc = get_root_path(WANT_FD, fsname, &fd, NULL);
2508 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2509 "Can't open %s: %d\n", mdtname, rc);
2513 data.icc_mdtindex = index;
2515 data.icc_recno = endrec;
2516 rc = ioctl(fd, OBD_IOC_CHANGELOG_CLEAR, &data);
2518 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2524 int llapi_fid2path(char *device, char *fidstr, char *buf, int buflen,
2525 long long *recno, int *linkno)
2527 char path[PATH_MAX];
2529 struct getinfo_fid2path *gf;
2532 while (*fidstr == '[')
2535 sscanf(fidstr, "0x%llx:0x%x:0x%x", &(fid.f_seq), &(fid.f_oid),
2537 if (!fid_is_sane(&fid)) {
2538 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2539 "bad FID format [%s], should be "DFID"\n",
2540 fidstr, (__u64)1, 2, 0);
2544 /* Take path or fsname */
2545 if (device[0] == '/') {
2546 strcpy(path, device);
2548 rc = get_root_path(WANT_PATH, device, NULL, path);
2552 sprintf(path, "%s/%s/fid/%s", path, dot_lustre_name, fidstr);
2553 fd = open(path, O_RDONLY | O_NONBLOCK);
2557 gf = malloc(sizeof(*gf) + buflen);
2559 gf->gf_recno = *recno;
2560 gf->gf_linkno = *linkno;
2561 gf->gf_pathlen = buflen;
2562 rc = ioctl(fd, OBD_IOC_FID2PATH, gf);
2564 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2566 memcpy(buf, gf->gf_path, gf->gf_pathlen);
2567 *recno = gf->gf_recno;
2568 *linkno = gf->gf_linkno;
2576 int llapi_path2fid(const char *path, unsigned long long *seq,
2577 unsigned long *oid, unsigned long *ver)
2582 fd = open(path, O_RDONLY);
2586 rc = ioctl(fd, LL_IOC_PATH2FID, &fid);
2587 *seq = fid_seq(&fid);
2588 *oid = fid_oid(&fid);
2589 *ver = fid_ver(&fid);