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 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_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
255 llapi_err(LLAPI_MSG_ERROR, "error: stripe_size %lu * "
256 "stripe_count %u exceeds 4GB", stripe_size,
263 static int poolpath(char *fsname, char *pathname, char *pool_pathname);
265 int llapi_file_open_pool(const char *name, int flags, int mode,
266 unsigned long stripe_size, int stripe_offset,
267 int stripe_count, int stripe_pattern, char *pool_name)
269 struct lov_user_md_v3 lum = { 0 };
272 char fsname[MAX_OBD_NAME + 1], *ptr;
274 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
275 if (fd < 0 && errno == EISDIR) {
276 fd = open(name, O_DIRECTORY | O_RDONLY);
282 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
286 if ((rc = llapi_stripe_limit_check(stripe_size, stripe_offset,
287 stripe_count, stripe_pattern)) != 0){
292 /* Initialize IOCTL striping pattern structure */
293 lum.lmm_magic = LOV_USER_MAGIC_V3;
294 lum.lmm_pattern = stripe_pattern;
295 lum.lmm_stripe_size = stripe_size;
296 lum.lmm_stripe_count = stripe_count;
297 lum.lmm_stripe_offset = stripe_offset;
299 /* in case user give the full pool name <fsname>.<poolname>, skip
301 if (pool_name != NULL) {
302 ptr = strchr(pool_name, '.');
304 strncpy(fsname, pool_name, ptr - pool_name);
306 /* if fsname matches a filesystem skip it
307 * if not keep the poolname as is */
308 if (poolpath(fsname, NULL, NULL) == 0)
311 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
313 /* If no pool is specified at all, use V1 request */
314 lum.lmm_magic = LOV_USER_MAGIC_V1;
317 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
318 char *errmsg = "stripe already set";
320 if (errno != EEXIST && errno != EALREADY)
321 errmsg = strerror(errno);
323 llapi_err_noerrno(LLAPI_MSG_ERROR,
324 "error on ioctl "LPX64" for '%s' (%d): %s",
325 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
336 int llapi_file_open(const char *name, int flags, int mode,
337 unsigned long stripe_size, int stripe_offset,
338 int stripe_count, int stripe_pattern)
340 return llapi_file_open_pool(name, flags, mode, stripe_size,
341 stripe_offset, stripe_count,
342 stripe_pattern, NULL);
345 int llapi_file_create(const char *name, unsigned long stripe_size,
346 int stripe_offset, int stripe_count, int stripe_pattern)
350 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
351 stripe_offset, stripe_count, stripe_pattern,
360 int llapi_file_create_pool(const char *name, unsigned long stripe_size,
361 int stripe_offset, int stripe_count,
362 int stripe_pattern, char *pool_name)
366 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
367 stripe_offset, stripe_count, stripe_pattern,
376 static int print_pool_members(char *fs, char *pool_dir, char *pool_file)
378 char path[PATH_MAX + 1];
382 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fs, pool_file);
383 sprintf(path, "%s/%s", pool_dir, pool_file);
384 if ((fd = fopen(path, "r")) == NULL) {
385 llapi_err(LLAPI_MSG_ERROR, "Cannot open %s\n", path);
388 while (fgets(buf, sizeof(buf), fd) != NULL)
389 llapi_printf(LLAPI_MSG_NORMAL, buf);
396 * Find the fsname, the full path, and/or an open fd.
397 * Either the fsname or path must not be NULL
399 #define WANT_PATH 0x1
400 #define WANT_FSNAME 0x2
402 static int get_root_path(int want, char *fsname, int *outfd, char *path)
411 /* get the mount point */
412 fp = setmntent(MOUNTED, "r");
414 llapi_err(LLAPI_MSG_ERROR,
415 "setmntent(%s) failed: %s:", MOUNTED,
420 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
423 if (!llapi_is_lustre_mnt(&mnt))
426 ptr = strrchr(mnt.mnt_fsname, '/');
433 /* If path was specified and matches, store the fsname */
434 if ((want & WANT_FSNAME) && (strcmp(mnt.mnt_dir, path) == 0))
436 /* Else check the fsname for a match */
437 else if (strcmp(ptr, fsname) != 0)
442 if (want & WANT_PATH)
443 strcpy(path, mnt.mnt_dir);
444 if (want & WANT_FD) {
445 fd = open(mnt.mnt_dir,
446 O_RDONLY | O_DIRECTORY | O_NONBLOCK);
458 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
459 "can't find fs root for '%s': %d",
460 (want & WANT_PATH) ? fsname : path, rc);
464 int llapi_search_fsname(const char *pathname, char *fsname)
466 return get_root_path(WANT_FSNAME, fsname, NULL, (char *)pathname);
469 /* return the first file matching this pattern */
470 static int first_match(char *pattern, char *buffer)
474 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
477 if (glob_info.gl_pathc < 1) {
478 globfree(&glob_info);
482 strcpy(buffer, glob_info.gl_pathv[0]);
484 globfree(&glob_info);
489 * find the pool directory path under /proc
490 * (can be also used to test if a fsname is known)
492 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
495 char pattern[PATH_MAX + 1];
496 char buffer[PATH_MAX];
498 if (fsname == NULL) {
499 rc = get_root_path(WANT_FSNAME, buffer, NULL, pathname);
503 strcpy(pathname, fsname);
506 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
507 rc = first_match(pattern, buffer);
511 /* in fsname test mode, pool_pathname is NULL */
512 if (pool_pathname != NULL)
513 strcpy(pool_pathname, buffer);
518 int llapi_poollist(char *name)
522 char rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
526 /* is name a pathname ? */
527 ptr = strchr(name, '/');
529 /* only absolute pathname is supported */
532 if (!realpath(name, rname)) {
534 llapi_err(LLAPI_MSG_ERROR, "invalid path '%s'", name);
538 rc = poolpath(NULL, rname, pathname);
541 llapi_err(LLAPI_MSG_ERROR, "'%s' is not"
542 " a Lustre filesystem", name);
548 /* name is FSNAME[.POOLNAME] */
550 poolname = strchr(name, '.');
551 if (poolname != NULL) {
555 rc = poolpath(fsname, NULL, pathname);
559 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
564 if (poolname != NULL) {
565 rc = print_pool_members(fsname, pathname, poolname);
572 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
573 if ((dir = opendir(pathname)) == NULL) {
576 while ((pool = readdir(dir)) != NULL) {
577 if (!((pool->d_name[0] == '.') &&
578 (pool->d_name[1] == '\0')) &&
579 !((pool->d_name[0] == '.') &&
580 (pool->d_name[1] == '.') &&
581 (pool->d_name[2] == '\0')))
582 llapi_printf(LLAPI_MSG_NORMAL, " %s.%s\n",
583 fsname, pool->d_name);
590 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
591 void *data, cfs_dirent_t *de);
593 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
594 #define OBD_NOT_FOUND (-1)
596 static int common_param_init(struct find_param *param)
598 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
599 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
600 llapi_err(LLAPI_MSG_ERROR,
601 "error: allocation of %d bytes for ioctl",
602 sizeof(lstat_t) + param->lumlen);
606 param->got_uuids = 0;
607 param->obdindexes = NULL;
608 param->obdindex = OBD_NOT_FOUND;
612 static void find_param_fini(struct find_param *param)
614 if (param->obdindexes)
615 free(param->obdindexes);
621 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
623 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
626 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
631 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
635 fd = open(path, O_RDONLY);
638 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
642 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
650 * If uuidp is NULL, return the number of available obd uuids.
651 * If uuidp is non-NULL, then it will return the uuids of the obds. If
652 * there are more OSTs then allocated to uuidp, then an error is returned with
653 * the ost_count set to number of available obd uuids.
655 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
657 struct obd_uuid lov_name;
660 int rc = 0, index = 0;
662 /* Get the lov name */
663 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
667 /* Now get the ost uuids from /proc */
668 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
670 fp = fopen(buf, "r");
673 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
677 while (fgets(buf, sizeof(buf), fp) != NULL) {
678 if (uuidp && (index < *ost_count)) {
679 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
687 if (uuidp && (index >= *ost_count))
694 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
701 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
706 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
712 /* Here, param->obduuid points to a single obduuid, the index of which is
713 * returned in param->obdindex */
714 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
716 struct obd_uuid lov_uuid;
717 char uuid[sizeof(struct obd_uuid)];
722 /* Get the lov name */
723 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
725 if (errno != ENOTTY) {
727 llapi_err(LLAPI_MSG_ERROR,
728 "error: can't get lov name: %s", dname);
735 param->got_uuids = 1;
737 /* Now get the ost uuids from /proc */
738 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
740 fp = fopen(buf, "r");
743 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
747 if (!param->obduuid && !param->quiet && !param->obds_printed)
748 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
750 while (fgets(buf, sizeof(buf), fp) != NULL) {
751 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
754 if (param->obduuid) {
755 if (strncmp(param->obduuid->uuid, uuid,
756 sizeof(uuid)) == 0) {
757 param->obdindex = index;
760 } else if (!param->quiet && !param->obds_printed) {
761 /* Print everything */
762 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
765 param->obds_printed = 1;
769 if (!param->quiet && param->obduuid &&
770 (param->obdindex == OBD_NOT_FOUND)) {
771 llapi_err_noerrno(LLAPI_MSG_ERROR,
772 "error: %s: unknown obduuid: %s",
773 __FUNCTION__, param->obduuid->uuid);
780 /* In this case, param->obduuid will be an array of obduuids and
781 * obd index for all these obduuids will be returned in
782 * param->obdindexes */
783 static int setup_obd_indexes(DIR *dir, struct find_param *param)
785 struct obd_uuid *uuids = NULL;
786 int obdcount = INIT_ALLOC_NUM_OSTS;
787 int ret, obd_valid = 0, obdnum, i;
789 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
790 sizeof(struct obd_uuid));
795 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
798 struct obd_uuid *uuids_temp;
800 if (ret == -EOVERFLOW) {
801 uuids_temp = realloc(uuids, obdcount *
802 sizeof(struct obd_uuid));
803 if (uuids_temp != NULL)
804 goto retry_get_uuids;
809 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
813 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
814 if (param->obdindexes == NULL)
817 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
818 for (i = 0; i <= obdcount; i++) {
819 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
820 (char *)&uuids[i]) == 0) {
821 param->obdindexes[obdnum] = i;
827 param->obdindexes[obdnum] = OBD_NOT_FOUND;
831 param->obdindex = OBD_NOT_FOUND;
833 param->obdindex = obd_valid;
835 param->got_uuids = 1;
840 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
841 struct lov_user_ost_data_v1 *objects,
842 char *path, int is_dir,
843 int obdindex, int quiet, int header, int body)
845 int i, obdstripe = 0;
847 if (obdindex != OBD_NOT_FOUND) {
848 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
849 if (obdindex == objects[i].l_ost_idx) {
850 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
856 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
860 /* if it's a directory */
862 if (obdstripe == 1) {
863 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
864 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
865 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
867 llapi_printf(LLAPI_MSG_NORMAL,
868 "stripe_count: %d stripe_size: %u "
869 "stripe_offset: %d%s%s\n",
870 lum->lmm_stripe_count == (__u16)-1 ? -1 :
871 lum->lmm_stripe_count,
872 lum->lmm_stripe_size,
873 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
874 lum->lmm_stripe_offset,
875 pool_name != NULL ? " pool: " : "",
876 pool_name != NULL ? pool_name : "");
881 if (header && (obdstripe == 1)) {
882 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
884 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
886 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
888 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
889 (int)lum->lmm_stripe_count);
890 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
891 lum->lmm_stripe_size);
892 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
894 if (pool_name != NULL)
895 llapi_printf(LLAPI_MSG_NORMAL,
896 "lmm_pool_name: %s\n", pool_name);
900 if ((!quiet) && (obdstripe == 1))
901 llapi_printf(LLAPI_MSG_NORMAL,
902 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
904 for (i = 0; i < lum->lmm_stripe_count; i++) {
905 int idx = objects[i].l_ost_idx;
906 long long oid = objects[i].l_object_id;
907 long long gr = objects[i].l_object_gr;
908 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
909 llapi_printf(LLAPI_MSG_NORMAL,
910 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
912 obdindex == idx ? " *" : "");
914 llapi_printf(LLAPI_MSG_NORMAL, "\n");
918 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
919 int is_dir, int obdindex, int quiet,
920 int header, int body)
922 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
923 int i, obdstripe = 0;
925 if (obdindex != OBD_NOT_FOUND) {
926 for (i = 0; i < lumj->lmm_stripe_count; i++) {
927 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
928 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
934 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
938 if (header && obdstripe == 1) {
939 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
941 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
942 lumj->lmm_object_gr);
943 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
944 lumj->lmm_object_id);
945 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
946 (int)lumj->lmm_stripe_count);
947 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
948 lumj->lmm_stripe_size);
949 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
951 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
952 lumj->lmm_extent_count);
956 unsigned long long start = -1, end = 0;
957 if (!quiet && obdstripe == 1)
958 llapi_printf(LLAPI_MSG_NORMAL,
959 "joined\tobdidx\t\t objid\t\tobjid\t\t "
960 "group\t\tstart\t\tend\n");
961 for (i = 0; i < lumj->lmm_stripe_count; i++) {
962 int idx = lumj->lmm_objects[i].l_ost_idx;
963 long long oid = lumj->lmm_objects[i].l_object_id;
964 long long gr = lumj->lmm_objects[i].l_object_gr;
965 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
966 llapi_printf(LLAPI_MSG_NORMAL,
967 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
969 obdindex == idx ? " *" : "");
970 if (start != lumj->lmm_objects[i].l_extent_start ||
971 end != lumj->lmm_objects[i].l_extent_end) {
972 start = lumj->lmm_objects[i].l_extent_start;
973 llapi_printf(LLAPI_MSG_NORMAL,"\t%14llu",start);
974 end = lumj->lmm_objects[i].l_extent_end;
975 if (end == (unsigned long long)-1)
976 llapi_printf(LLAPI_MSG_NORMAL,
979 llapi_printf(LLAPI_MSG_NORMAL,
982 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
985 llapi_printf(LLAPI_MSG_NORMAL, "\n");
989 void llapi_lov_dump_user_lmm(struct find_param *param,
990 char *path, int is_dir)
992 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
993 case LOV_USER_MAGIC_V1:
994 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
995 param->lmd->lmd_lmm.lmm_objects,
997 param->obdindex, param->quiet,
999 (param->verbose || !param->obduuid));
1001 case LOV_USER_MAGIC_JOIN:
1002 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
1003 param->obdindex, param->quiet,
1005 (param->verbose || !param->obduuid));
1007 case LOV_USER_MAGIC_V3: {
1008 char pool_name[LOV_MAXPOOLNAME + 1];
1009 struct lov_user_ost_data_v1 *objects;
1010 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1012 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1013 pool_name[LOV_MAXPOOLNAME] = '\0';
1014 objects = lmmv3->lmm_objects;
1015 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
1016 objects, path, is_dir,
1017 param->obdindex, param->quiet,
1019 (param->verbose || !param->obduuid));
1023 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1024 "(expecting one of %#x %#x %#x)\n",
1025 *(__u32 *)¶m->lmd->lmd_lmm,
1026 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_JOIN,
1032 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1038 fname = strrchr(path, '/');
1040 /* It should be a file (or other non-directory) */
1041 if (fname == NULL) {
1042 dname = (char *)malloc(2);
1046 fname = (char *)path;
1048 dname = (char *)malloc(fname - path + 1);
1051 strncpy(dname, path, fname - path);
1052 dname[fname - path] = '\0';
1056 if ((fd = open(dname, O_RDONLY)) == -1) {
1062 strcpy((char *)lum, fname);
1063 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1066 if (close(fd) == -1 && rc == 0)
1074 int llapi_file_lookup(int dirfd, const char *name)
1076 struct obd_ioctl_data data = { 0 };
1081 if (dirfd < 0 || name == NULL)
1084 data.ioc_version = OBD_IOCTL_VERSION;
1085 data.ioc_len = sizeof(data);
1086 data.ioc_inlbuf1 = (char *)name;
1087 data.ioc_inllen1 = strlen(name) + 1;
1089 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1091 llapi_err(LLAPI_MSG_ERROR,
1092 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1097 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1100 int llapi_mds_getfileinfo(char *path, DIR *parent,
1101 struct lov_user_mds_data *lmd)
1103 lstat_t *st = &lmd->lmd_st;
1104 char *fname = strrchr(path, '/');
1110 fname = (fname == NULL ? path : fname + 1);
1111 /* retrieve needed file info */
1112 strncpy((char *)lmd, fname,
1113 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
1114 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1117 if (errno == ENOTTY) {
1118 /* ioctl is not supported, it is not a lustre fs.
1119 * Do the regular lstat(2) instead. */
1120 ret = lstat_f(path, st);
1122 llapi_err(LLAPI_MSG_ERROR,
1123 "error: %s: lstat failed for %s",
1124 __FUNCTION__, path);
1127 } else if (errno == ENOENT) {
1128 llapi_err(LLAPI_MSG_WARN,
1129 "warning: %s: %s does not exist",
1130 __FUNCTION__, path);
1133 llapi_err(LLAPI_MSG_ERROR,
1134 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1135 __FUNCTION__, path);
1143 static DIR *opendir_parent(char *path)
1149 fname = strrchr(path, '/');
1151 return opendir(".");
1155 parent = opendir(path);
1160 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1161 semantic_func_t sem_init,
1162 semantic_func_t sem_fini, void *data,
1173 if (!d && errno != ENOTDIR) {
1174 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
1175 __FUNCTION__, path);
1177 } else if (!d && !parent) {
1178 /* ENOTDIR. Open the parent dir. */
1179 p = opendir_parent(path);
1181 GOTO(out, ret = -EINVAL);
1184 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1190 while ((dent = readdir64(d)) != NULL) {
1191 ((struct find_param *)data)->have_fileinfo = 0;
1193 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1196 /* Don't traverse .lustre directory */
1197 if (!(strcmp(dent->d_name, dot_lustre_name)))
1201 if ((len + dent->d_reclen + 2) > size) {
1202 llapi_err(LLAPI_MSG_ERROR,
1203 "error: %s: string buffer is too small",
1208 strcat(path, dent->d_name);
1210 if (dent->d_type == DT_UNKNOWN) {
1211 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1213 ret = llapi_mds_getfileinfo(path, d,
1214 ((struct find_param *)data)->lmd);
1216 ((struct find_param *)data)->have_fileinfo = 1;
1218 llapi_filetype_dir_table[st->st_mode &
1225 switch (dent->d_type) {
1227 llapi_err(LLAPI_MSG_ERROR,
1228 "error: %s: '%s' is UNKNOWN type %d",
1229 __FUNCTION__, dent->d_name, dent->d_type);
1232 ret = llapi_semantic_traverse(path, size, d, sem_init,
1233 sem_fini, data, dent);
1240 ret = sem_init(path, d, NULL, data, dent);
1244 if (sem_fini && ret == 0)
1245 sem_fini(path, d, NULL, data, dent);
1253 sem_fini(path, parent, d, data, de);
1262 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1263 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1265 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1266 * The table below gives the answers for the specified parameters (value and
1267 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1268 * --------------------------------------
1269 * 1 | file > limit; sign > 0 | -1 / -1 |
1270 * 2 | file = limit; sign > 0 | ? / 1 |
1271 * 3 | file < limit; sign > 0 | ? / 1 |
1272 * 4 | file > limit; sign = 0 | -1 / -1 |
1273 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1274 * 6 | file < limit; sign = 0 | ? / -1 |
1275 * 7 | file > limit; sign < 0 | 1 / 1 |
1276 * 8 | file = limit; sign < 0 | ? / -1 |
1277 * 9 | file < limit; sign < 0 | ? / -1 |
1278 * --------------------------------------
1279 * Note: 5th actually means that the value is within the interval
1280 * (limit - margin, limit]. */
1281 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1282 unsigned long long margin, int mds)
1290 if (file <= limit && file + margin > limit)
1292 if (file + margin <= limit)
1293 return mds ? 0 : -1;
1306 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1307 * Return -1 or 1 if file timestamp does not or does match the given criteria
1308 * correspondingly. Return 0 if the MDS time is being checked and there are
1309 * attributes on OSTs and it is not yet clear if the timespamp matches.
1311 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1312 * updated timestamps. */
1313 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1318 /* Check if file is accepted. */
1320 ret = find_value_cmp(st->st_atime, param->atime,
1321 param->asign, 24 * 60 * 60, mds);
1328 ret = find_value_cmp(st->st_mtime, param->mtime,
1329 param->msign, 24 * 60 * 60, mds);
1333 /* If the previous check matches, but this one is not yet clear,
1334 * we should return 0 to do an RPC on OSTs. */
1340 ret = find_value_cmp(st->st_ctime, param->ctime,
1341 param->csign, 24 * 60 * 60, mds);
1345 /* If the previous check matches, but this one is not yet clear,
1346 * we should return 0 to do an RPC on OSTs. */
1354 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1355 void *data, cfs_dirent_t *de)
1357 struct find_param *param = (struct find_param *)data;
1358 int decision = 1; /* 1 is accepted; -1 is rejected. */
1359 lstat_t *st = ¶m->lmd->lmd_st;
1361 int checked_type = 0;
1364 LASSERT(parent != NULL || dir != NULL);
1366 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1368 /* If a regular expression is presented, make the initial decision */
1369 if (param->pattern != NULL) {
1370 char *fname = strrchr(path, '/');
1371 fname = (fname == NULL ? path : fname + 1);
1372 ret = fnmatch(param->pattern, fname, 0);
1373 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1374 (ret == 0 && param->exclude_pattern))
1378 /* See if we can check the file type from the dirent. */
1379 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1380 de->d_type < DT_MAX) {
1382 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1383 if (param->exclude_type)
1386 if (!param->exclude_type)
1392 /* If a time or OST should be checked, the decision is not taken yet. */
1393 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1398 /* Request MDS for the stat info. */
1399 if (param->have_fileinfo == 0) {
1401 /* retrieve needed file info */
1402 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1403 (void *)param->lmd);
1405 char *fname = strrchr(path, '/');
1406 fname = (fname == NULL ? path : fname + 1);
1408 /* retrieve needed file info */
1409 strncpy((char *)param->lmd, fname, param->lumlen);
1410 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1411 (void *)param->lmd);
1416 if (errno == ENOTTY) {
1417 /* ioctl is not supported, it is not a lustre fs.
1418 * Do the regular lstat(2) instead. */
1420 ret = lstat_f(path, st);
1422 llapi_err(LLAPI_MSG_ERROR,
1423 "error: %s: lstat failed for %s",
1424 __FUNCTION__, path);
1427 } else if (errno == ENOENT) {
1428 llapi_err(LLAPI_MSG_WARN,
1429 "warning: %s: %s does not exist",
1430 __FUNCTION__, path);
1433 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1434 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1435 "IOC_MDC_GETFILEINFO", path);
1440 if (param->type && !checked_type) {
1441 if ((st->st_mode & S_IFMT) == param->type) {
1442 if (param->exclude_type)
1445 if (!param->exclude_type)
1451 if (param->obduuid) {
1452 if (lustre_fs && param->got_uuids &&
1453 param->st_dev != st->st_dev) {
1454 /* A lustre/lustre mount point is crossed. */
1455 param->got_uuids = 0;
1456 param->obds_printed = 0;
1457 param->obdindex = OBD_NOT_FOUND;
1460 if (lustre_fs && !param->got_uuids) {
1461 ret = setup_obd_indexes(dir ? dir : parent, param);
1465 param->st_dev = st->st_dev;
1466 } else if (!lustre_fs && param->got_uuids) {
1467 /* A lustre/non-lustre mount point is crossed. */
1468 param->got_uuids = 0;
1469 param->obdindex = OBD_NOT_FOUND;
1473 /* If an OBD UUID is specified but no one matches, skip this file. */
1474 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1477 /* If a OST UUID is given, and some OST matches, check it here. */
1478 if (param->obdindex != OBD_NOT_FOUND) {
1479 if (!S_ISREG(st->st_mode))
1482 /* Only those files should be accepted, which have a
1483 * stripe on the specified OST. */
1484 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1488 struct lov_user_ost_data_v1 *lmm_objects;
1490 if (param->lmd->lmd_lmm.lmm_magic ==
1491 LOV_USER_MAGIC_V3) {
1492 struct lov_user_md_v3 *lmmv3 =
1493 (void *)¶m->lmd->lmd_lmm;
1495 lmm_objects = lmmv3->lmm_objects;
1497 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1501 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1502 for (j = 0; j < param->num_obds; j++) {
1503 if (param->obdindexes[j] ==
1504 lmm_objects[i].l_ost_idx)
1509 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1514 if (param->check_uid) {
1515 if (st->st_uid == param->uid) {
1516 if (param->exclude_uid)
1519 if (!param->exclude_uid)
1524 if (param->check_gid) {
1525 if (st->st_gid == param->gid) {
1526 if (param->exclude_gid)
1529 if (!param->exclude_gid)
1534 if (param->check_pool) {
1535 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1537 /* empty requested pool is taken as no pool search => V1 */
1538 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1539 (param->poolname[0] == '\0')) ||
1540 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1541 (strncmp(lmmv3->lmm_pool_name,
1542 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1543 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1544 (strcmp(param->poolname, "*") == 0))) {
1545 if (param->exclude_pool)
1548 if (!param->exclude_pool)
1553 /* Check the time on mds. */
1557 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1558 param->lmd->lmd_lmm.lmm_stripe_count)
1560 decision = find_time_check(st, param, for_mds);
1564 /* If file still fits the request, ask ost for updated info.
1565 The regular stat is almost of the same speed as some new
1566 'glimpse-size-ioctl'. */
1567 if (!decision && S_ISREG(st->st_mode) &&
1568 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1569 if (param->obdindex != OBD_NOT_FOUND) {
1570 /* Check whether the obd is active or not, if it is
1571 * not active, just print the object affected by this
1574 struct obd_statfs stat_buf;
1575 struct obd_uuid uuid_buf;
1577 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1578 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1579 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1580 param->obdindex, &stat_buf,
1583 if (ret == -ENODATA || ret == -ENODEV
1586 llapi_printf(LLAPI_MSG_NORMAL,
1587 "obd_uuid: %s failed %s ",
1588 param->obduuid->uuid,
1594 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1595 (void *)param->lmd);
1596 } else if (parent) {
1597 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1598 (void *)param->lmd);
1602 if (errno == ENOENT) {
1603 llapi_err(LLAPI_MSG_ERROR,
1604 "warning: %s: %s does not exist",
1605 __FUNCTION__, path);
1608 llapi_err(LLAPI_MSG_ERROR,
1609 "%s: IOC_LOV_GETINFO on %s failed",
1610 __FUNCTION__, path);
1615 /* Check the time on osc. */
1616 decision = find_time_check(st, param, 0);
1622 decision = find_value_cmp(st->st_size, param->size,
1623 param->size_sign, param->size_units,
1627 if (decision != -1) {
1628 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1630 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1632 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1636 /* Do not get down anymore? */
1637 if (param->depth == param->maxdepth)
1644 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1647 struct find_param *param = (struct find_param *)data;
1652 int llapi_find(char *path, struct find_param *param)
1655 int ret, len = strlen(path);
1657 if (len > PATH_MAX) {
1658 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1659 __FUNCTION__, path);
1663 buf = (char *)malloc(PATH_MAX + 1);
1667 ret = common_param_init(param);
1675 strncpy(buf, path, PATH_MAX + 1);
1676 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1677 cb_common_fini, param, NULL);
1679 find_param_fini(param);
1681 return ret < 0 ? ret : 0;
1684 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1687 struct find_param *param = (struct find_param *)data;
1690 LASSERT(parent != NULL || d != NULL);
1693 if (!param->got_uuids) {
1694 ret = setup_obd_uuid(d ? d : parent, path, param);
1700 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1701 (void *)¶m->lmd->lmd_lmm);
1702 } else if (parent) {
1703 char *fname = strrchr(path, '/');
1704 fname = (fname == NULL ? path : fname + 1);
1706 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1707 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1708 (void *)¶m->lmd->lmd_lmm);
1712 if (errno == ENODATA) {
1713 if (!param->obduuid && !param->quiet)
1714 llapi_printf(LLAPI_MSG_NORMAL,
1715 "%s has no stripe info\n", path);
1717 } else if (errno == ENOTTY) {
1718 llapi_err(LLAPI_MSG_ERROR,
1719 "%s: '%s' not on a Lustre fs?",
1720 __FUNCTION__, path);
1721 } else if (errno == ENOENT) {
1722 llapi_err(LLAPI_MSG_WARN,
1723 "warning: %s: %s does not exist",
1724 __FUNCTION__, path);
1727 llapi_err(LLAPI_MSG_ERROR,
1728 "error: %s: %s failed for %s",
1729 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1730 "IOC_MDC_GETFILESTRIPE", path);
1736 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1738 /* Do not get down anymore? */
1739 if (param->depth == param->maxdepth)
1746 int llapi_getstripe(char *path, struct find_param *param)
1749 int ret = 0, len = strlen(path);
1751 if (len > PATH_MAX) {
1752 llapi_err(LLAPI_MSG_ERROR,
1753 "%s: Path name '%s' is too long",
1754 __FUNCTION__, path);
1758 buf = (char *)malloc(PATH_MAX + 1);
1762 ret = common_param_init(param);
1770 strncpy(buf, path, PATH_MAX + 1);
1771 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1772 cb_common_fini, param, NULL);
1773 find_param_fini(param);
1775 return ret < 0 ? ret : 0;
1778 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1779 struct obd_statfs *stat_buf,
1780 struct obd_uuid *uuid_buf)
1783 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1785 struct obd_ioctl_data data = { 0 };
1788 data.ioc_inlbuf1 = (char *)&type;
1789 data.ioc_inllen1 = sizeof(__u32);
1790 data.ioc_inlbuf2 = (char *)&index;
1791 data.ioc_inllen2 = sizeof(__u32);
1792 data.ioc_pbuf1 = (char *)stat_buf;
1793 data.ioc_plen1 = sizeof(struct obd_statfs);
1794 data.ioc_pbuf2 = (char *)uuid_buf;
1795 data.ioc_plen2 = sizeof(struct obd_uuid);
1797 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1798 llapi_err(LLAPI_MSG_ERROR,
1799 "llapi_obd_statfs: error packing ioctl data");
1803 fd = open(path, O_RDONLY);
1804 if (errno == EISDIR)
1805 fd = open(path, O_DIRECTORY | O_RDONLY);
1808 rc = errno ? -errno : -EBADF;
1809 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
1810 __FUNCTION__, path);
1813 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1815 rc = errno ? -errno : -EINVAL;
1821 #define MAX_STRING_SIZE 128
1822 #define DEVICES_LIST "/proc/fs/lustre/devices"
1824 int llapi_ping(char *obd_type, char *obd_name)
1826 char path[MAX_STRING_SIZE];
1830 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1831 obd_type, obd_name);
1833 fd = open(path, O_WRONLY);
1836 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1840 rc = write(fd, buf, 1);
1848 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
1850 char buf[MAX_STRING_SIZE];
1851 FILE *fp = fopen(DEVICES_LIST, "r");
1856 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1860 while (fgets(buf, sizeof(buf), fp) != NULL) {
1861 char *obd_type_name = NULL;
1862 char *obd_name = NULL;
1863 char *obd_uuid = NULL;
1865 struct obd_ioctl_data datal = { 0, };
1866 struct obd_statfs osfs_buffer;
1868 while(bufp[0] == ' ')
1871 for(i = 0; i < 3; i++) {
1872 obd_type_name = strsep(&bufp, " ");
1874 obd_name = strsep(&bufp, " ");
1875 obd_uuid = strsep(&bufp, " ");
1877 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1879 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1880 datal.ioc_plen1 = sizeof(osfs_buffer);
1882 for (i = 0; i < type_num; i++) {
1883 if (strcmp(obd_type_name, obd_type[i]) != 0)
1886 cb(obd_type_name, obd_name, obd_uuid, args);
1893 static void do_target_check(char *obd_type_name, char *obd_name,
1894 char *obd_uuid, void *args)
1898 rc = llapi_ping(obd_type_name, obd_name);
1899 if (rc == ENOTCONN) {
1900 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
1902 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1904 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1908 int llapi_target_check(int type_num, char **obd_type, char *dir)
1910 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1913 #undef MAX_STRING_SIZE
1915 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1917 char raw[OBD_MAX_IOCTL_BUFFER];
1918 char out[LLOG_CHUNK_SIZE];
1920 struct obd_ioctl_data data = { 0 };
1925 sprintf(key, "%s", keyword);
1926 memset(raw, 0, sizeof(raw));
1927 memset(out, 0, sizeof(out));
1928 data.ioc_inlbuf1 = key;
1929 data.ioc_inllen1 = strlen(key) + 1;
1931 data.ioc_inlbuf2 = node_name;
1932 data.ioc_inllen2 = strlen(node_name) + 1;
1934 data.ioc_pbuf1 = out;
1935 data.ioc_plen1 = sizeof(out);
1936 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1940 root = opendir(dir);
1943 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1947 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1949 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1951 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1957 /* Is this a lustre fs? */
1958 int llapi_is_lustre_mnttype(const char *type)
1960 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1963 /* Is this a lustre client fs? */
1964 int llapi_is_lustre_mnt(struct mntent *mnt)
1966 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1967 strstr(mnt->mnt_fsname, ":/") != NULL);
1970 int llapi_quotacheck(char *mnt, int check_type)
1975 root = opendir(mnt);
1977 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1981 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1987 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1993 root = opendir(mnt);
1995 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2000 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2004 if (poll_intvl < 30)
2012 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2017 root = opendir(mnt);
2019 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2023 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2029 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2032 struct find_param *param = (struct find_param *)data;
2036 LASSERT(parent != NULL || d != NULL);
2039 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2040 (void *)param->lmd);
2041 } else if (parent) {
2042 char *fname = strrchr(path, '/');
2043 fname = (fname == NULL ? path : fname + 1);
2045 strncpy((char *)param->lmd, fname, param->lumlen);
2046 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2047 (void *)param->lmd);
2053 if (errno == ENODATA) {
2054 if (!param->obduuid && !param->quiet)
2055 llapi_err(LLAPI_MSG_ERROR,
2056 "%s has no stripe info", path);
2058 } else if (errno == ENOENT) {
2059 llapi_err(LLAPI_MSG_ERROR,
2060 "warning: %s: %s does not exist",
2061 __FUNCTION__, path);
2063 } else if (errno != EISDIR) {
2065 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2066 d ? "LL_IOC_MDC_GETINFO" :
2067 "IOC_MDC_GETFILEINFO", path);
2072 st = ¶m->lmd->lmd_st;
2074 /* libc chown() will do extra check, and if the real owner is
2075 * the same as the ones to set, it won't fall into kernel, so
2076 * invoke syscall directly. */
2077 rc = syscall(SYS_chown, path, -1, -1);
2079 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2081 rc = chmod(path, st->st_mode);
2083 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2089 int llapi_quotachown(char *path, int flag)
2091 struct find_param param;
2093 int ret = 0, len = strlen(path);
2095 if (len > PATH_MAX) {
2096 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
2097 __FUNCTION__, path);
2101 buf = (char *)malloc(PATH_MAX + 1);
2105 memset(¶m, 0, sizeof(param));
2106 param.recursive = 1;
2110 ret = common_param_init(¶m);
2114 strncpy(buf, path, PATH_MAX + 1);
2115 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
2116 NULL, ¶m, NULL);
2118 find_param_fini(¶m);
2126 #include <sys/wait.h>
2130 static int rmtacl_notify(int ops)
2134 int found = 0, fd, rc;
2136 fp = setmntent(MOUNTED, "r");
2138 perror("setmntent");
2143 mnt = getmntent(fp);
2147 if (!llapi_is_lustre_mnt(mnt))
2150 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2156 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2168 static char *next_token(char *p, int div)
2174 while (*p && *p != ':' && !isspace(*p))
2177 while (*p == ':' || isspace(*p))
2180 return *p ? p : NULL;
2183 static int rmtacl_name2id(char *name, int is_user)
2188 if ((pw = getpwnam(name)) == NULL)
2191 return (int)(pw->pw_uid);
2195 if ((gr = getgrnam(name)) == NULL)
2198 return (int)(gr->gr_gid);
2202 static int isodigit(int c)
2204 return (c >= '0' && c <= '7') ? 1 : 0;
2208 * Whether the name is just digits string (uid/gid) already or not.
2213 static int str_is_id(char *str)
2220 if (*str == 'x' || *str == 'X') { /* for Hex. */
2221 if (!isxdigit(*(++str)))
2224 while (isxdigit(*(++str)));
2225 } else if (isodigit(*str)) { /* for Oct. */
2226 while (isodigit(*(++str)));
2228 } else if (isdigit(*str)) { /* for Dec. */
2229 while (isdigit(*(++str)));
2232 return (*str == 0) ? 1 : 0;
2242 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2244 static rmtacl_name_t rmtacl_namelist[] = {
2245 { RMTACL_OPTNAME("user:"), 1, 0 },
2246 { RMTACL_OPTNAME("group:"), 0, 0 },
2247 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2248 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2249 /* for --tabular option */
2250 { RMTACL_OPTNAME("user"), 1, 1 },
2251 { RMTACL_OPTNAME("group"), 0, 1 },
2255 static int rgetfacl_output(char *str)
2257 char *start = NULL, *end = NULL;
2258 int is_user = 0, n, id;
2265 for (rn = rmtacl_namelist; rn->name; rn++) {
2266 if(strncmp(str, rn->name, rn->length) == 0) {
2267 if (!rn->next_token)
2268 start = str + rn->length;
2270 start = next_token(str + rn->length, 0);
2271 is_user = rn->is_user;
2276 end = next_token(start, 1);
2277 if (end == NULL || start == end) {
2278 n = printf("%s", str);
2284 id = rmtacl_name2id(start, is_user);
2285 if (id == INVALID_ID) {
2286 if (str_is_id(start)) {
2288 n = printf("%s", str);
2291 } else if ((id == NOBODY_UID && is_user) ||
2292 (id == NOBODY_GID && !is_user)) {
2294 n = printf("%s", str);
2298 n = printf("%s%d%s", str, id, end);
2303 static int child_status(int status)
2305 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2308 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2321 if ((pid = fork()) < 0) {
2327 /* child process redirects its output. */
2330 if (dup2(fd[1], 1) < 0) {
2341 status = rmtacl_notify(ops);
2345 exit(execvp(argv[0], argv));
2348 /* the following is parent process */
2349 if ((fp = fdopen(fd[0], "r")) == NULL) {
2356 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2357 if (output_func(buf) < 0)
2358 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2364 if (waitpid(pid, &status, 0) < 0) {
2369 return child_status(status);
2372 int llapi_lsetfacl(int argc, char *argv[])
2374 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2377 int llapi_lgetfacl(int argc, char *argv[])
2379 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2382 int llapi_rsetfacl(int argc, char *argv[])
2384 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2387 int llapi_rgetfacl(int argc, char *argv[])
2389 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2392 int llapi_cp(int argc, char *argv[])
2396 rc = rmtacl_notify(RMT_RSETFACL);
2400 exit(execvp(argv[0], argv));
2403 int llapi_ls(int argc, char *argv[])
2407 rc = rmtacl_notify(RMT_LGETFACL);
2411 exit(execvp(argv[0], argv));
2414 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
2415 * format must have %s%s, buf must be > 16
2417 static int get_mdtname(const char *name, char *format, char *buf)
2419 char suffix[]="-MDT0000";
2420 int len = strlen(name);
2423 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
2426 /* Not enough room to add suffix */
2427 llapi_err(LLAPI_MSG_ERROR, "MDT name too long |%s|\n",
2433 return sprintf(buf, format, name, suffix);
2437 /* Return a file descriptor to a readable changelog */
2438 int llapi_changelog_open(const char *device, long long startrec)
2444 if (device[0] == '/')
2445 rc = get_root_path(WANT_FSNAME, mdtname, NULL, (char *)device);
2447 strncpy(mdtname, device, sizeof(mdtname));
2449 /* Use either the mdd changelog (preferred) or a client mdc changelog */
2450 if (get_mdtname(mdtname,
2451 "/proc/fs/lustre/md[cd]/%s%s{,-mdc-*}/changelog",
2454 rc = first_match(path, path);
2458 if ((fd = open(path, O_RDONLY)) < 0) {
2459 llapi_err(LLAPI_MSG_ERROR, "error: can't open |%s|\n", path);
2463 rc = lseek(fd, (off_t)startrec, SEEK_SET);
2465 llapi_err(LLAPI_MSG_ERROR, "can't seek rc=%d\n", rc);
2472 int llapi_changelog_clear(const char *mdtname, const char *idstr,
2475 struct ioc_changelog_clear data;
2478 int id, fd, index, rc;
2481 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2482 "can't purge negative records\n");
2486 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
2487 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
2488 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
2489 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2490 "expecting id of the form '"CHANGELOG_USER_PREFIX
2491 "<num>'; got '%s'\n", idstr);
2495 /* Take path, fsname, or MDTNAME. Assume MDT0000 in the former cases */
2496 if (mdtname[0] == '/') {
2498 fd = open(mdtname, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
2499 rc = fd < 0 ? -errno : 0;
2501 if (get_mdtname(mdtname, "%s%s", fsname) < 0)
2503 ptr = fsname + strlen(fsname) - 8;
2505 index = strtol(ptr + 4, NULL, 10);
2506 rc = get_root_path(WANT_FD, fsname, &fd, NULL);
2509 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2510 "Can't open %s: %d\n", mdtname, rc);
2514 data.icc_mdtindex = index;
2516 data.icc_recno = endrec;
2517 rc = ioctl(fd, OBD_IOC_CHANGELOG_CLEAR, &data);
2519 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2525 int llapi_fid2path(char *device, char *fidstr, char *buf, int buflen,
2526 long long *recno, int *linkno)
2528 char path[PATH_MAX];
2530 struct getinfo_fid2path *gf;
2533 while (*fidstr == '[')
2536 sscanf(fidstr, "0x%llx:0x%x:0x%x", &(fid.f_seq), &(fid.f_oid),
2538 if (!fid_is_sane(&fid)) {
2539 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2540 "bad FID format [%s], should be "DFID"\n",
2541 fidstr, (__u64)1, 2, 0);
2545 /* Take path or fsname */
2546 if (device[0] == '/') {
2547 strcpy(path, device);
2549 rc = get_root_path(WANT_PATH, device, NULL, path);
2553 sprintf(path, "%s/%s/fid/%s", path, dot_lustre_name, fidstr);
2554 fd = open(path, O_RDONLY | O_NONBLOCK);
2558 gf = malloc(sizeof(*gf) + buflen);
2560 gf->gf_recno = *recno;
2561 gf->gf_linkno = *linkno;
2562 gf->gf_pathlen = buflen;
2563 rc = ioctl(fd, OBD_IOC_FID2PATH, gf);
2565 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2567 memcpy(buf, gf->gf_path, gf->gf_pathlen);
2568 *recno = gf->gf_recno;
2569 *linkno = gf->gf_linkno;
2577 int llapi_path2fid(const char *path, unsigned long long *seq,
2578 unsigned long *oid, unsigned long *ver)
2583 fd = open(path, O_RDONLY);
2587 rc = ioctl(fd, LL_IOC_PATH2FID, &fid);
2588 *seq = fid_seq(&fid);
2589 *oid = fid_oid(&fid);
2590 *ver = fid_ver(&fid);