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>
70 #include <liblustre.h>
71 #include <lnet/lnetctl.h>
73 #include <lustre_lib.h>
75 #include <lustre/liblustreapi.h>
77 static unsigned llapi_dir_filetype_table[] = {
86 #if defined(DT_DOOR) && defined(S_IFDOOR)
91 #if defined(DT_DOOR) && defined(S_IFDOOR)
92 static const int DT_MAX = DT_DOOR;
94 static const int DT_MAX = DT_SOCK;
97 static unsigned llapi_filetype_dir_table[] = {
106 #if defined(DT_DOOR) && defined(S_IFDOOR)
111 #if defined(DT_DOOR) && defined(S_IFDOOR)
112 static const int S_IFMAX = DT_DOOR;
114 static const int S_IFMAX = DT_SOCK;
117 /* liblustreapi message level */
118 static int llapi_msg_level = LLAPI_MSG_MAX;
120 void llapi_msg_set_level(int level)
122 /* ensure level is in the good range */
123 if (level < LLAPI_MSG_OFF)
124 llapi_msg_level = LLAPI_MSG_OFF;
125 else if (level > LLAPI_MSG_MAX)
126 llapi_msg_level = LLAPI_MSG_MAX;
128 llapi_msg_level = level;
131 void llapi_err(int level, char *fmt, ...)
134 int tmp_errno = abs(errno);
136 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
140 vfprintf(stderr, fmt, args);
143 if (level & LLAPI_MSG_NO_ERRNO)
144 fprintf(stderr, "\n");
146 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
149 #define llapi_err_noerrno(level, fmt, a...) \
150 llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
152 void llapi_printf(int level, char *fmt, ...)
156 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
160 vfprintf(stdout, fmt, args);
165 * size_units is unchanged if no specifier used
167 int parse_size(char *optarg, unsigned long long *size,
168 unsigned long long *size_units, int bytes_spec)
172 *size = strtoull(optarg, &end, 0);
175 if ((*end == 'b') && *(end+1) == '\0' &&
176 (*size & (~0ULL << (64 - 9))) == 0 &&
179 *size_units = 1 << 9;
180 } else if ((*end == 'b') && *(end+1) == '\0' &&
183 } else if ((*end == 'k' || *end == 'K') &&
184 *(end+1) == '\0' && (*size &
185 (~0ULL << (64 - 10))) == 0) {
187 *size_units = 1 << 10;
188 } else if ((*end == 'm' || *end == 'M') &&
189 *(end+1) == '\0' && (*size &
190 (~0ULL << (64 - 20))) == 0) {
192 *size_units = 1 << 20;
193 } else if ((*end == 'g' || *end == 'G') &&
194 *(end+1) == '\0' && (*size &
195 (~0ULL << (64 - 30))) == 0) {
197 *size_units = 1 << 30;
198 } else if ((*end == 't' || *end == 'T') &&
199 *(end+1) == '\0' && (*size &
200 (~0ULL << (64 - 40))) == 0) {
202 *size_units = 1ULL << 40;
203 } else if ((*end == 'p' || *end == 'P') &&
204 *(end+1) == '\0' && (*size &
205 (~0ULL << (64 - 50))) == 0) {
207 *size_units = 1ULL << 50;
208 } else if ((*end == 'e' || *end == 'E') &&
209 *(end+1) == '\0' && (*size &
210 (~0ULL << (64 - 60))) == 0) {
212 *size_units = 1ULL << 60;
221 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
222 int stripe_count, int stripe_pattern)
226 /* 64 KB is the largest common page size I'm aware of (on ia64), but
227 * check the local page size just in case. */
228 page_size = LOV_MIN_STRIPE_SIZE;
229 if (getpagesize() > page_size) {
230 page_size = getpagesize();
231 llapi_err_noerrno(LLAPI_MSG_WARN,
232 "warning: your page size (%u) is "
233 "larger than expected (%u)", page_size,
234 LOV_MIN_STRIPE_SIZE);
236 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
237 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
238 "must be an even multiple of %d bytes",
239 stripe_size, page_size);
242 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
244 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d",
248 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
250 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d",
254 if (stripe_size >= (1ULL << 32)){
256 llapi_err(LLAPI_MSG_ERROR, "warning: stripe size larger than 4G"
257 " is not currently supported and would wrap");
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 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 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 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 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 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
841 int is_dir, int verbose, int quiet,
844 char *prefix = is_dir ? "" : "lmm_";
845 char nl = is_dir ? ' ' : '\n';
848 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
850 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
851 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
853 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
855 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
859 if (verbose & VERBOSE_COUNT) {
861 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
863 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
864 (int)lum->lmm_stripe_count, nl);
867 if (verbose & VERBOSE_SIZE) {
869 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
871 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", lum->lmm_stripe_size,
875 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
876 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
877 lum->lmm_pattern, nl);
880 if (verbose & VERBOSE_OFFSET) {
882 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
884 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
885 lum->lmm_objects[0].l_ost_idx, nl);
888 if ((verbose & VERBOSE_POOL) && (pool_name != NULL))
889 llapi_printf(LLAPI_MSG_NORMAL, "pool: %s%c", pool_name, nl);
892 llapi_printf(LLAPI_MSG_NORMAL, "\n");
895 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
896 struct lov_user_ost_data_v1 *objects,
897 char *path, int is_dir,
898 int obdindex, int quiet, int header, int body)
900 int i, obdstripe = 0;
902 if (obdindex != OBD_NOT_FOUND) {
903 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
904 if (obdindex == objects[i].l_ost_idx) {
905 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
912 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
916 /* if it's a directory */
918 if (obdstripe == 1) {
919 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
920 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
921 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
923 /* maintain original behavior */
925 header |= VERBOSE_ALL;
926 lov_dump_user_lmm_header(lum, path, is_dir, header,
932 if (header && (obdstripe == 1))
933 lov_dump_user_lmm_header(lum, NULL, is_dir, header, quiet,
937 if ((!quiet) && (obdstripe == 1))
938 llapi_printf(LLAPI_MSG_NORMAL,
939 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
941 for (i = 0; i < lum->lmm_stripe_count; i++) {
942 int idx = objects[i].l_ost_idx;
943 long long oid = objects[i].l_object_id;
944 long long gr = objects[i].l_object_gr;
945 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
946 llapi_printf(LLAPI_MSG_NORMAL,
947 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
949 obdindex == idx ? " *" : "");
951 llapi_printf(LLAPI_MSG_NORMAL, "\n");
955 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
956 int is_dir, int obdindex, int quiet,
957 int header, int body)
959 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
960 int i, obdstripe = 0;
962 if (obdindex != OBD_NOT_FOUND) {
963 for (i = 0; i < lumj->lmm_stripe_count; i++) {
964 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
965 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
972 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
976 if (header && obdstripe == 1) {
977 lov_dump_user_lmm_header(lum, NULL, 0, header, quiet, NULL);
979 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
980 lumj->lmm_extent_count);
984 unsigned long long start = -1, end = 0;
985 if (!quiet && obdstripe == 1)
986 llapi_printf(LLAPI_MSG_NORMAL,
987 "joined\tobdidx\t\t objid\t\tobjid\t\t "
988 "group\t\tstart\t\tend\n");
989 for (i = 0; i < lumj->lmm_stripe_count; i++) {
990 int idx = lumj->lmm_objects[i].l_ost_idx;
991 long long oid = lumj->lmm_objects[i].l_object_id;
992 long long gr = lumj->lmm_objects[i].l_object_gr;
993 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
994 llapi_printf(LLAPI_MSG_NORMAL,
995 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
997 obdindex == idx ? " *" : "");
998 if (start != lumj->lmm_objects[i].l_extent_start ||
999 end != lumj->lmm_objects[i].l_extent_end) {
1000 start = lumj->lmm_objects[i].l_extent_start;
1001 llapi_printf(LLAPI_MSG_NORMAL,"\t%14llu",start);
1002 end = lumj->lmm_objects[i].l_extent_end;
1003 if (end == (unsigned long long)-1)
1004 llapi_printf(LLAPI_MSG_NORMAL,
1007 llapi_printf(LLAPI_MSG_NORMAL,
1010 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
1013 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1017 void llapi_lov_dump_user_lmm(struct find_param *param,
1018 char *path, int is_dir)
1020 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
1021 case LOV_USER_MAGIC_V1:
1022 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
1023 param->lmd->lmd_lmm.lmm_objects,
1025 param->obdindex, param->quiet,
1027 (param->verbose || !param->obduuid));
1029 case LOV_USER_MAGIC_JOIN:
1030 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
1031 param->obdindex, param->quiet,
1033 (param->verbose || !param->obduuid));
1035 case LOV_USER_MAGIC_V3: {
1036 char pool_name[LOV_MAXPOOLNAME + 1];
1037 struct lov_user_ost_data_v1 *objects;
1038 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1040 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1041 pool_name[LOV_MAXPOOLNAME] = '\0';
1042 objects = lmmv3->lmm_objects;
1043 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
1044 objects, path, is_dir,
1045 param->obdindex, param->quiet,
1047 (param->verbose || !param->obduuid));
1051 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1052 "(expecting one of %#x %#x %#x)\n",
1053 *(__u32 *)¶m->lmd->lmd_lmm,
1054 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_JOIN,
1060 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1066 fname = strrchr(path, '/');
1068 /* It should be a file (or other non-directory) */
1069 if (fname == NULL) {
1070 dname = (char *)malloc(2);
1074 fname = (char *)path;
1076 dname = (char *)malloc(fname - path + 1);
1079 strncpy(dname, path, fname - path);
1080 dname[fname - path] = '\0';
1084 if ((fd = open(dname, O_RDONLY)) == -1) {
1090 strcpy((char *)lum, fname);
1091 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1094 if (close(fd) == -1 && rc == 0)
1102 int llapi_file_lookup(int dirfd, const char *name)
1104 struct obd_ioctl_data data = { 0 };
1109 if (dirfd < 0 || name == NULL)
1112 data.ioc_version = OBD_IOCTL_VERSION;
1113 data.ioc_len = sizeof(data);
1114 data.ioc_inlbuf1 = (char *)name;
1115 data.ioc_inllen1 = strlen(name) + 1;
1117 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1119 llapi_err(LLAPI_MSG_ERROR,
1120 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1125 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1128 int llapi_mds_getfileinfo(char *path, DIR *parent,
1129 struct lov_user_mds_data *lmd)
1131 lstat_t *st = &lmd->lmd_st;
1132 char *fname = strrchr(path, '/');
1138 fname = (fname == NULL ? path : fname + 1);
1139 /* retrieve needed file info */
1140 strncpy((char *)lmd, fname,
1141 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
1142 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1145 if (errno == ENOTTY) {
1146 /* ioctl is not supported, it is not a lustre fs.
1147 * Do the regular lstat(2) instead. */
1148 ret = lstat_f(path, st);
1150 llapi_err(LLAPI_MSG_ERROR,
1151 "error: %s: lstat failed for %s",
1152 __FUNCTION__, path);
1155 } else if (errno == ENOENT) {
1156 llapi_err(LLAPI_MSG_WARN,
1157 "warning: %s: %s does not exist",
1158 __FUNCTION__, path);
1161 llapi_err(LLAPI_MSG_ERROR,
1162 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1163 __FUNCTION__, path);
1171 static DIR *opendir_parent(char *path)
1177 fname = strrchr(path, '/');
1179 return opendir(".");
1183 parent = opendir(path);
1188 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1189 semantic_func_t sem_init,
1190 semantic_func_t sem_fini, void *data,
1201 if (!d && errno != ENOTDIR) {
1202 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
1203 __FUNCTION__, path);
1205 } else if (!d && !parent) {
1206 /* ENOTDIR. Open the parent dir. */
1207 p = opendir_parent(path);
1209 GOTO(out, ret = -EINVAL);
1212 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1218 while ((dent = readdir64(d)) != NULL) {
1219 ((struct find_param *)data)->have_fileinfo = 0;
1221 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1224 /* Don't traverse .lustre directory */
1225 if (!(strcmp(dent->d_name, dot_lustre_name)))
1229 if ((len + dent->d_reclen + 2) > size) {
1230 llapi_err(LLAPI_MSG_ERROR,
1231 "error: %s: string buffer is too small",
1236 strcat(path, dent->d_name);
1238 if (dent->d_type == DT_UNKNOWN) {
1239 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1241 ret = llapi_mds_getfileinfo(path, d,
1242 ((struct find_param *)data)->lmd);
1244 ((struct find_param *)data)->have_fileinfo = 1;
1246 llapi_filetype_dir_table[st->st_mode &
1253 switch (dent->d_type) {
1255 llapi_err(LLAPI_MSG_ERROR,
1256 "error: %s: '%s' is UNKNOWN type %d",
1257 __FUNCTION__, dent->d_name, dent->d_type);
1260 ret = llapi_semantic_traverse(path, size, d, sem_init,
1261 sem_fini, data, dent);
1268 ret = sem_init(path, d, NULL, data, dent);
1272 if (sem_fini && ret == 0)
1273 sem_fini(path, d, NULL, data, dent);
1281 sem_fini(path, parent, d, data, de);
1290 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1291 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1293 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1294 * The table below gives the answers for the specified parameters (value and
1295 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1296 * --------------------------------------
1297 * 1 | file > limit; sign > 0 | -1 / -1 |
1298 * 2 | file = limit; sign > 0 | ? / 1 |
1299 * 3 | file < limit; sign > 0 | ? / 1 |
1300 * 4 | file > limit; sign = 0 | -1 / -1 |
1301 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1302 * 6 | file < limit; sign = 0 | ? / -1 |
1303 * 7 | file > limit; sign < 0 | 1 / 1 |
1304 * 8 | file = limit; sign < 0 | ? / -1 |
1305 * 9 | file < limit; sign < 0 | ? / -1 |
1306 * --------------------------------------
1307 * Note: 5th actually means that the value is within the interval
1308 * (limit - margin, limit]. */
1309 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1310 unsigned long long margin, int mds)
1318 if (file <= limit && file + margin > limit)
1320 if (file + margin <= limit)
1321 return mds ? 0 : -1;
1334 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1335 * Return -1 or 1 if file timestamp does not or does match the given criteria
1336 * correspondingly. Return 0 if the MDS time is being checked and there are
1337 * attributes on OSTs and it is not yet clear if the timespamp matches.
1339 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1340 * updated timestamps. */
1341 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1346 /* Check if file is accepted. */
1348 ret = find_value_cmp(st->st_atime, param->atime,
1349 param->asign, 24 * 60 * 60, mds);
1356 ret = find_value_cmp(st->st_mtime, param->mtime,
1357 param->msign, 24 * 60 * 60, mds);
1361 /* If the previous check matches, but this one is not yet clear,
1362 * we should return 0 to do an RPC on OSTs. */
1368 ret = find_value_cmp(st->st_ctime, param->ctime,
1369 param->csign, 24 * 60 * 60, mds);
1373 /* If the previous check matches, but this one is not yet clear,
1374 * we should return 0 to do an RPC on OSTs. */
1382 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1383 void *data, cfs_dirent_t *de)
1385 struct find_param *param = (struct find_param *)data;
1386 int decision = 1; /* 1 is accepted; -1 is rejected. */
1387 lstat_t *st = ¶m->lmd->lmd_st;
1389 int checked_type = 0;
1392 LASSERT(parent != NULL || dir != NULL);
1394 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1396 /* If a regular expression is presented, make the initial decision */
1397 if (param->pattern != NULL) {
1398 char *fname = strrchr(path, '/');
1399 fname = (fname == NULL ? path : fname + 1);
1400 ret = fnmatch(param->pattern, fname, 0);
1401 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1402 (ret == 0 && param->exclude_pattern))
1406 /* See if we can check the file type from the dirent. */
1407 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1408 de->d_type < DT_MAX) {
1410 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1411 if (param->exclude_type)
1414 if (!param->exclude_type)
1420 /* If a time or OST should be checked, the decision is not taken yet. */
1421 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1426 /* Request MDS for the stat info. */
1427 if (param->have_fileinfo == 0) {
1429 /* retrieve needed file info */
1430 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1431 (void *)param->lmd);
1433 char *fname = strrchr(path, '/');
1434 fname = (fname == NULL ? path : fname + 1);
1436 /* retrieve needed file info */
1437 strncpy((char *)param->lmd, fname, param->lumlen);
1438 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1439 (void *)param->lmd);
1444 if (errno == ENOTTY) {
1445 /* ioctl is not supported, it is not a lustre fs.
1446 * Do the regular lstat(2) instead. */
1448 ret = lstat_f(path, st);
1450 llapi_err(LLAPI_MSG_ERROR,
1451 "error: %s: lstat failed for %s",
1452 __FUNCTION__, path);
1455 } else if (errno == ENOENT) {
1456 llapi_err(LLAPI_MSG_WARN,
1457 "warning: %s: %s does not exist",
1458 __FUNCTION__, path);
1461 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1462 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1463 "IOC_MDC_GETFILEINFO", path);
1468 if (param->type && !checked_type) {
1469 if ((st->st_mode & S_IFMT) == param->type) {
1470 if (param->exclude_type)
1473 if (!param->exclude_type)
1479 if (param->obduuid) {
1480 if (lustre_fs && param->got_uuids &&
1481 param->st_dev != st->st_dev) {
1482 /* A lustre/lustre mount point is crossed. */
1483 param->got_uuids = 0;
1484 param->obds_printed = 0;
1485 param->obdindex = OBD_NOT_FOUND;
1488 if (lustre_fs && !param->got_uuids) {
1489 ret = setup_obd_indexes(dir ? dir : parent, param);
1493 param->st_dev = st->st_dev;
1494 } else if (!lustre_fs && param->got_uuids) {
1495 /* A lustre/non-lustre mount point is crossed. */
1496 param->got_uuids = 0;
1497 param->obdindex = OBD_NOT_FOUND;
1501 /* If an OBD UUID is specified but no one matches, skip this file. */
1502 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1505 /* If a OST UUID is given, and some OST matches, check it here. */
1506 if (param->obdindex != OBD_NOT_FOUND) {
1507 if (!S_ISREG(st->st_mode))
1510 /* Only those files should be accepted, which have a
1511 * stripe on the specified OST. */
1512 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1516 struct lov_user_ost_data_v1 *lmm_objects;
1518 if (param->lmd->lmd_lmm.lmm_magic ==
1519 LOV_USER_MAGIC_V3) {
1520 struct lov_user_md_v3 *lmmv3 =
1521 (void *)¶m->lmd->lmd_lmm;
1523 lmm_objects = lmmv3->lmm_objects;
1525 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1529 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1530 for (j = 0; j < param->num_obds; j++) {
1531 if (param->obdindexes[j] ==
1532 lmm_objects[i].l_ost_idx)
1537 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1542 if (param->check_uid) {
1543 if (st->st_uid == param->uid) {
1544 if (param->exclude_uid)
1547 if (!param->exclude_uid)
1552 if (param->check_gid) {
1553 if (st->st_gid == param->gid) {
1554 if (param->exclude_gid)
1557 if (!param->exclude_gid)
1562 if (param->check_pool) {
1563 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1565 /* empty requested pool is taken as no pool search => V1 */
1566 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1567 (param->poolname[0] == '\0')) ||
1568 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1569 (strncmp(lmmv3->lmm_pool_name,
1570 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1571 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1572 (strcmp(param->poolname, "*") == 0))) {
1573 if (param->exclude_pool)
1576 if (!param->exclude_pool)
1581 /* Check the time on mds. */
1585 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1586 param->lmd->lmd_lmm.lmm_stripe_count)
1588 decision = find_time_check(st, param, for_mds);
1592 /* If file still fits the request, ask ost for updated info.
1593 The regular stat is almost of the same speed as some new
1594 'glimpse-size-ioctl'. */
1595 if (!decision && S_ISREG(st->st_mode) &&
1596 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1597 if (param->obdindex != OBD_NOT_FOUND) {
1598 /* Check whether the obd is active or not, if it is
1599 * not active, just print the object affected by this
1602 struct obd_statfs stat_buf;
1603 struct obd_uuid uuid_buf;
1605 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1606 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1607 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1608 param->obdindex, &stat_buf,
1611 if (ret == -ENODATA || ret == -ENODEV
1614 llapi_printf(LLAPI_MSG_NORMAL,
1615 "obd_uuid: %s failed %s ",
1616 param->obduuid->uuid,
1622 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1623 (void *)param->lmd);
1624 } else if (parent) {
1625 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1626 (void *)param->lmd);
1630 if (errno == ENOENT) {
1631 llapi_err(LLAPI_MSG_ERROR,
1632 "warning: %s: %s does not exist",
1633 __FUNCTION__, path);
1636 llapi_err(LLAPI_MSG_ERROR,
1637 "%s: IOC_LOV_GETINFO on %s failed",
1638 __FUNCTION__, path);
1643 /* Check the time on osc. */
1644 decision = find_time_check(st, param, 0);
1650 decision = find_value_cmp(st->st_size, param->size,
1651 param->size_sign, param->size_units,
1655 if (decision != -1) {
1656 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1658 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1660 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1664 /* Do not get down anymore? */
1665 if (param->depth == param->maxdepth)
1672 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1675 struct find_param *param = (struct find_param *)data;
1680 int llapi_find(char *path, struct find_param *param)
1683 int ret, len = strlen(path);
1685 if (len > PATH_MAX) {
1686 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1687 __FUNCTION__, path);
1691 buf = (char *)malloc(PATH_MAX + 1);
1695 ret = common_param_init(param);
1703 strncpy(buf, path, PATH_MAX + 1);
1704 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1705 cb_common_fini, param, NULL);
1707 find_param_fini(param);
1709 return ret < 0 ? ret : 0;
1712 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1715 struct find_param *param = (struct find_param *)data;
1718 LASSERT(parent != NULL || d != NULL);
1721 if (!param->got_uuids) {
1722 ret = setup_obd_uuid(d ? d : parent, path, param);
1728 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1729 (void *)¶m->lmd->lmd_lmm);
1730 } else if (parent) {
1731 char *fname = strrchr(path, '/');
1732 fname = (fname == NULL ? path : fname + 1);
1734 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1735 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1736 (void *)¶m->lmd->lmd_lmm);
1740 if (errno == ENODATA) {
1741 if (!param->obduuid && !param->quiet)
1742 llapi_printf(LLAPI_MSG_NORMAL,
1743 "%s has no stripe info\n", path);
1745 } else if (errno == ENOTTY) {
1746 llapi_err(LLAPI_MSG_ERROR,
1747 "%s: '%s' not on a Lustre fs?",
1748 __FUNCTION__, path);
1749 } else if (errno == ENOENT) {
1750 llapi_err(LLAPI_MSG_WARN,
1751 "warning: %s: %s does not exist",
1752 __FUNCTION__, path);
1755 llapi_err(LLAPI_MSG_ERROR,
1756 "error: %s: %s failed for %s",
1757 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1758 "IOC_MDC_GETFILESTRIPE", path);
1764 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1766 /* Do not get down anymore? */
1767 if (param->depth == param->maxdepth)
1774 int llapi_getstripe(char *path, struct find_param *param)
1777 int ret = 0, len = strlen(path);
1779 if (len > PATH_MAX) {
1780 llapi_err(LLAPI_MSG_ERROR,
1781 "%s: Path name '%s' is too long",
1782 __FUNCTION__, path);
1786 buf = (char *)malloc(PATH_MAX + 1);
1790 ret = common_param_init(param);
1798 strncpy(buf, path, PATH_MAX + 1);
1799 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1800 cb_common_fini, param, NULL);
1801 find_param_fini(param);
1803 return ret < 0 ? ret : 0;
1806 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1807 struct obd_statfs *stat_buf,
1808 struct obd_uuid *uuid_buf)
1811 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1813 struct obd_ioctl_data data = { 0 };
1816 data.ioc_inlbuf1 = (char *)&type;
1817 data.ioc_inllen1 = sizeof(__u32);
1818 data.ioc_inlbuf2 = (char *)&index;
1819 data.ioc_inllen2 = sizeof(__u32);
1820 data.ioc_pbuf1 = (char *)stat_buf;
1821 data.ioc_plen1 = sizeof(struct obd_statfs);
1822 data.ioc_pbuf2 = (char *)uuid_buf;
1823 data.ioc_plen2 = sizeof(struct obd_uuid);
1825 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1826 llapi_err(LLAPI_MSG_ERROR,
1827 "llapi_obd_statfs: error packing ioctl data");
1831 fd = open(path, O_RDONLY);
1832 if (errno == EISDIR)
1833 fd = open(path, O_DIRECTORY | O_RDONLY);
1836 rc = errno ? -errno : -EBADF;
1837 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
1838 __FUNCTION__, path);
1841 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1843 rc = errno ? -errno : -EINVAL;
1849 #define MAX_STRING_SIZE 128
1850 #define DEVICES_LIST "/proc/fs/lustre/devices"
1852 int llapi_ping(char *obd_type, char *obd_name)
1854 char path[MAX_STRING_SIZE];
1858 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1859 obd_type, obd_name);
1861 fd = open(path, O_WRONLY);
1864 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1868 rc = write(fd, buf, 1);
1876 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
1878 char buf[MAX_STRING_SIZE];
1879 FILE *fp = fopen(DEVICES_LIST, "r");
1884 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1888 while (fgets(buf, sizeof(buf), fp) != NULL) {
1889 char *obd_type_name = NULL;
1890 char *obd_name = NULL;
1891 char *obd_uuid = NULL;
1893 struct obd_ioctl_data datal = { 0, };
1894 struct obd_statfs osfs_buffer;
1896 while(bufp[0] == ' ')
1899 for(i = 0; i < 3; i++) {
1900 obd_type_name = strsep(&bufp, " ");
1902 obd_name = strsep(&bufp, " ");
1903 obd_uuid = strsep(&bufp, " ");
1905 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1907 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1908 datal.ioc_plen1 = sizeof(osfs_buffer);
1910 for (i = 0; i < type_num; i++) {
1911 if (strcmp(obd_type_name, obd_type[i]) != 0)
1914 cb(obd_type_name, obd_name, obd_uuid, args);
1921 static void do_target_check(char *obd_type_name, char *obd_name,
1922 char *obd_uuid, void *args)
1926 rc = llapi_ping(obd_type_name, obd_name);
1927 if (rc == ENOTCONN) {
1928 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
1930 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1932 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1936 int llapi_target_check(int type_num, char **obd_type, char *dir)
1938 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1941 #undef MAX_STRING_SIZE
1943 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1945 char raw[OBD_MAX_IOCTL_BUFFER];
1946 char out[LLOG_CHUNK_SIZE];
1948 struct obd_ioctl_data data = { 0 };
1953 sprintf(key, "%s", keyword);
1954 memset(raw, 0, sizeof(raw));
1955 memset(out, 0, sizeof(out));
1956 data.ioc_inlbuf1 = key;
1957 data.ioc_inllen1 = strlen(key) + 1;
1959 data.ioc_inlbuf2 = node_name;
1960 data.ioc_inllen2 = strlen(node_name) + 1;
1962 data.ioc_pbuf1 = out;
1963 data.ioc_plen1 = sizeof(out);
1964 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1968 root = opendir(dir);
1971 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1975 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1977 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1979 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1985 /* Is this a lustre fs? */
1986 int llapi_is_lustre_mnttype(const char *type)
1988 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1991 /* Is this a lustre client fs? */
1992 int llapi_is_lustre_mnt(struct mntent *mnt)
1994 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1995 strstr(mnt->mnt_fsname, ":/") != NULL);
1998 int llapi_quotacheck(char *mnt, int check_type)
2003 root = opendir(mnt);
2005 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2009 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2015 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2021 root = opendir(mnt);
2023 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2028 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2032 if (poll_intvl < 30)
2040 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2045 root = opendir(mnt);
2047 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2051 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2057 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2060 struct find_param *param = (struct find_param *)data;
2064 LASSERT(parent != NULL || d != NULL);
2067 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2068 (void *)param->lmd);
2069 } else if (parent) {
2070 char *fname = strrchr(path, '/');
2071 fname = (fname == NULL ? path : fname + 1);
2073 strncpy((char *)param->lmd, fname, param->lumlen);
2074 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2075 (void *)param->lmd);
2081 if (errno == ENODATA) {
2082 if (!param->obduuid && !param->quiet)
2083 llapi_err(LLAPI_MSG_ERROR,
2084 "%s has no stripe info", path);
2086 } else if (errno == ENOENT) {
2087 llapi_err(LLAPI_MSG_ERROR,
2088 "warning: %s: %s does not exist",
2089 __FUNCTION__, path);
2091 } else if (errno != EISDIR) {
2093 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2094 d ? "LL_IOC_MDC_GETINFO" :
2095 "IOC_MDC_GETFILEINFO", path);
2100 st = ¶m->lmd->lmd_st;
2102 /* libc chown() will do extra check, and if the real owner is
2103 * the same as the ones to set, it won't fall into kernel, so
2104 * invoke syscall directly. */
2105 rc = syscall(SYS_chown, path, -1, -1);
2107 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2109 rc = chmod(path, st->st_mode);
2111 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2117 int llapi_quotachown(char *path, int flag)
2119 struct find_param param;
2121 int ret = 0, len = strlen(path);
2123 if (len > PATH_MAX) {
2124 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
2125 __FUNCTION__, path);
2129 buf = (char *)malloc(PATH_MAX + 1);
2133 memset(¶m, 0, sizeof(param));
2134 param.recursive = 1;
2138 ret = common_param_init(¶m);
2142 strncpy(buf, path, PATH_MAX + 1);
2143 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
2144 NULL, ¶m, NULL);
2146 find_param_fini(¶m);
2154 #include <sys/wait.h>
2158 static int rmtacl_notify(int ops)
2162 int found = 0, fd, rc;
2164 fp = setmntent(MOUNTED, "r");
2166 perror("setmntent");
2171 mnt = getmntent(fp);
2175 if (!llapi_is_lustre_mnt(mnt))
2178 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2184 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2196 static char *next_token(char *p, int div)
2202 while (*p && *p != ':' && !isspace(*p))
2205 while (*p == ':' || isspace(*p))
2208 return *p ? p : NULL;
2211 static int rmtacl_name2id(char *name, int is_user)
2216 if ((pw = getpwnam(name)) == NULL)
2219 return (int)(pw->pw_uid);
2223 if ((gr = getgrnam(name)) == NULL)
2226 return (int)(gr->gr_gid);
2230 static int isodigit(int c)
2232 return (c >= '0' && c <= '7') ? 1 : 0;
2236 * Whether the name is just digits string (uid/gid) already or not.
2241 static int str_is_id(char *str)
2248 if (*str == 'x' || *str == 'X') { /* for Hex. */
2249 if (!isxdigit(*(++str)))
2252 while (isxdigit(*(++str)));
2253 } else if (isodigit(*str)) { /* for Oct. */
2254 while (isodigit(*(++str)));
2256 } else if (isdigit(*str)) { /* for Dec. */
2257 while (isdigit(*(++str)));
2260 return (*str == 0) ? 1 : 0;
2270 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2272 static rmtacl_name_t rmtacl_namelist[] = {
2273 { RMTACL_OPTNAME("user:"), 1, 0 },
2274 { RMTACL_OPTNAME("group:"), 0, 0 },
2275 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2276 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2277 /* for --tabular option */
2278 { RMTACL_OPTNAME("user"), 1, 1 },
2279 { RMTACL_OPTNAME("group"), 0, 1 },
2283 static int rgetfacl_output(char *str)
2285 char *start = NULL, *end = NULL;
2286 int is_user = 0, n, id;
2293 for (rn = rmtacl_namelist; rn->name; rn++) {
2294 if(strncmp(str, rn->name, rn->length) == 0) {
2295 if (!rn->next_token)
2296 start = str + rn->length;
2298 start = next_token(str + rn->length, 0);
2299 is_user = rn->is_user;
2304 end = next_token(start, 1);
2305 if (end == NULL || start == end) {
2306 n = printf("%s", str);
2312 id = rmtacl_name2id(start, is_user);
2313 if (id == INVALID_ID) {
2314 if (str_is_id(start)) {
2316 n = printf("%s", str);
2319 } else if ((id == NOBODY_UID && is_user) ||
2320 (id == NOBODY_GID && !is_user)) {
2322 n = printf("%s", str);
2326 n = printf("%s%d%s", str, id, end);
2331 static int child_status(int status)
2333 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2336 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2349 if ((pid = fork()) < 0) {
2355 /* child process redirects its output. */
2358 if (dup2(fd[1], 1) < 0) {
2369 status = rmtacl_notify(ops);
2373 exit(execvp(argv[0], argv));
2376 /* the following is parent process */
2377 if ((fp = fdopen(fd[0], "r")) == NULL) {
2384 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2385 if (output_func(buf) < 0)
2386 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2392 if (waitpid(pid, &status, 0) < 0) {
2397 return child_status(status);
2400 int llapi_lsetfacl(int argc, char *argv[])
2402 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2405 int llapi_lgetfacl(int argc, char *argv[])
2407 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2410 int llapi_rsetfacl(int argc, char *argv[])
2412 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2415 int llapi_rgetfacl(int argc, char *argv[])
2417 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2420 int llapi_cp(int argc, char *argv[])
2424 rc = rmtacl_notify(RMT_RSETFACL);
2428 exit(execvp(argv[0], argv));
2431 int llapi_ls(int argc, char *argv[])
2435 rc = rmtacl_notify(RMT_LGETFACL);
2439 exit(execvp(argv[0], argv));
2442 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
2443 * format must have %s%s, buf must be > 16
2445 static int get_mdtname(char *name, char *format, char *buf)
2447 char suffix[]="-MDT0000";
2448 int len = strlen(name);
2450 if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) {
2451 name[len - 5] = '\0';
2456 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
2459 /* Not enough room to add suffix */
2460 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2461 "MDT name too long |%s|", name);
2466 return sprintf(buf, format, name, suffix);
2469 /****** Changelog API ********/
2470 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
2471 struct changelog_private {
2477 /** Start reading from a changelog
2478 * @param priv Opaque private control structure
2479 * @param flags Start flags (e.g. follow)
2480 * @param device Report changes recorded on this MDT
2481 * @param startrec Report changes beginning with this record number
2483 int llapi_changelog_start(void **priv, int flags, const char *device,
2486 struct changelog_private *cp;
2491 if (device[0] == '/')
2492 rc = get_root_path(WANT_FSNAME, mdtname, NULL, (char *)device);
2494 strncpy(mdtname, device, sizeof(mdtname));
2496 /* Use either the mdd changelog (preferred) or a client mdc changelog */
2497 if (get_mdtname(mdtname,
2498 "/proc/fs/lustre/md[cd]/%s%s{,-mdc-*}/changelog",
2501 rc = first_match(path, path);
2505 if ((fd = open(path, O_RDONLY)) < 0) {
2506 llapi_err(LLAPI_MSG_ERROR, "error: can't open |%s|\n", path);
2510 rc = lseek(fd, (off_t)startrec, SEEK_SET);
2512 llapi_err(LLAPI_MSG_ERROR, "can't seek rc=%d\n", rc);
2516 cp = malloc(sizeof(*cp));
2522 cp->magic = CHANGELOG_PRIV_MAGIC;
2530 /** Finish reading from a changelog */
2531 int llapi_changelog_fini(void **priv)
2533 struct changelog_private *cp = (struct changelog_private *)*priv;
2535 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2544 static int pollwait(int fd) {
2545 struct pollfd pfds[1];
2549 pfds[0].events = POLLIN;
2550 rc = poll(pfds, 1, -1);
2551 return rc < 0 ? -errno : rc;
2554 /** Read the next changelog entry
2555 * @param priv Opaque private control structure
2556 * @param rech Changelog record handle; record will be allocated here
2557 * @return 0 valid message received; rec is set
2561 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
2563 struct changelog_private *cp = (struct changelog_private *)priv;
2564 struct changelog_rec rec, *recp;
2567 if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC))
2573 /* Read in the rec to get the namelen */
2574 rc = read(cp->fd, &rec, sizeof(rec));
2578 if (cp->flags && CHANGELOG_FLAG_FOLLOW) {
2579 rc = pollwait(cp->fd);
2587 recp = malloc(sizeof(rec) + rec.cr_namelen);
2590 memcpy(recp, &rec, sizeof(rec));
2591 rc = read(cp->fd, recp->cr_name, rec.cr_namelen);
2594 llapi_err(LLAPI_MSG_ERROR, "Can't read entire filename");
2602 /** Release the changelog record when done with it. */
2603 int llapi_changelog_free(struct changelog_rec **rech)
2611 int llapi_changelog_clear(const char *mdtname, const char *idstr,
2614 struct ioc_changelog_clear data;
2617 int id, fd, index, rc;
2620 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2621 "can't purge negative records\n");
2625 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
2626 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
2627 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
2628 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2629 "expecting id of the form '"CHANGELOG_USER_PREFIX
2630 "<num>'; got '%s'\n", idstr);
2634 /* Take path, fsname, or MDTNAME. Assume MDT0000 in the former cases */
2635 if (mdtname[0] == '/') {
2637 fd = open(mdtname, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
2638 rc = fd < 0 ? -errno : 0;
2640 if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0)
2642 ptr = fsname + strlen(fsname) - 8;
2644 index = strtol(ptr + 4, NULL, 10);
2645 rc = get_root_path(WANT_FD, fsname, &fd, NULL);
2648 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2649 "Can't open %s: %d\n", mdtname, rc);
2653 data.icc_mdtindex = index;
2655 data.icc_recno = endrec;
2656 rc = ioctl(fd, OBD_IOC_CHANGELOG_CLEAR, &data);
2658 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2664 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
2665 int buflen, long long *recno, int *linkno)
2667 char path[PATH_MAX];
2669 struct getinfo_fid2path *gf;
2672 while (*fidstr == '[')
2675 sscanf(fidstr, SFID, RFID(&fid));
2676 if (!fid_is_sane(&fid)) {
2677 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2678 "bad FID format [%s], should be "DFID"\n",
2679 fidstr, (__u64)1, 2, 0);
2683 /* Take path or fsname */
2684 if (device[0] == '/') {
2685 strcpy(path, device);
2687 rc = get_root_path(WANT_PATH, (char *)device, NULL, path);
2691 sprintf(path, "%s/%s/fid/%s", path, dot_lustre_name, fidstr);
2692 fd = open(path, O_RDONLY | O_NONBLOCK);
2696 gf = malloc(sizeof(*gf) + buflen);
2698 gf->gf_recno = *recno;
2699 gf->gf_linkno = *linkno;
2700 gf->gf_pathlen = buflen;
2701 rc = ioctl(fd, OBD_IOC_FID2PATH, gf);
2703 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2705 memcpy(buf, gf->gf_path, gf->gf_pathlen);
2706 *recno = gf->gf_recno;
2707 *linkno = gf->gf_linkno;
2715 int llapi_path2fid(const char *path, lustre_fid *fid)
2719 fd = open(path, O_RDONLY);
2723 rc = ioctl(fd, LL_IOC_PATH2FID, fid);
2729 /****** HSM Copytool API ********/
2730 #define CT_PRIV_MAGIC 0xC0BE2001
2731 struct copytool_private {
2734 int archive_num_count;
2735 int archive_nums[0];
2738 #include <libcfs/libcfs.h>
2740 /** Register a copytool
2741 * @param priv Opaque private control structure
2742 * @param flags Open flags, currently unused (e.g. O_NONBLOCK)
2743 * @param archive_nums Which archive numbers this copytool is responsible for
2745 int llapi_copytool_start(void **priv, int flags, int archive_num_count,
2748 struct copytool_private *ct;
2751 if (archive_num_count > 0 && archive_nums == NULL) {
2752 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2753 "NULL archive numbers");
2757 ct = malloc(sizeof(*ct) +
2758 archive_num_count * sizeof(ct->archive_nums[0]));
2762 ct->magic = CT_PRIV_MAGIC;
2763 ct->archive_num_count = archive_num_count;
2764 if (ct->archive_num_count > 0)
2765 memcpy(ct->archive_nums, archive_nums, archive_num_count *
2766 sizeof(ct->archive_nums[0]));
2768 rc = libcfs_ulnl_start(&ct->lnl, LNL_GRP_HSM);
2780 /** Deregister a copytool */
2781 int llapi_copytool_fini(void **priv)
2783 struct copytool_private *ct = (struct copytool_private *)*priv;
2785 if (!ct || (ct->magic != CT_PRIV_MAGIC))
2788 libcfs_ulnl_stop(&ct->lnl);
2794 /** Wait for the next hsm_action_list
2795 * @param priv Opaque private control structure
2796 * @param halh Action list handle, will be allocated here
2797 * @param msgsize Number of bytes in the message, will be set here
2798 * @return 0 valid message received; halh and msgsize are set
2801 int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
2803 struct copytool_private *ct = (struct copytool_private *)priv;
2804 struct lnl_hdr *lnlh;
2805 struct hsm_action_list *hal;
2808 if (!ct || (ct->magic != CT_PRIV_MAGIC))
2810 if (halh == NULL || msgsize == NULL)
2813 rc = libcfs_ulnl_msg_get(&ct->lnl, HAL_MAXSIZE,
2814 LNL_TRANSPORT_HSM, &lnlh);
2818 /* Handle generic messages */
2819 if (lnlh->lnl_transport == LNL_TRANSPORT_GENERIC &&
2820 lnlh->lnl_msgtype == LNL_MSG_SHUTDOWN) {
2825 if (lnlh->lnl_transport != LNL_TRANSPORT_HSM ||
2826 lnlh->lnl_msgtype != HMT_ACTION_LIST) {
2827 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2828 "Unknown HSM message type %d:%d\n",
2829 lnlh->lnl_transport, lnlh->lnl_msgtype);
2834 /* Our message is an hsm_action_list */
2836 hal = (struct hsm_action_list *)(lnlh + 1);
2838 /* Check that we have registered for this archive # */
2839 for (rc = 0; rc < ct->archive_num_count; rc++) {
2840 if (hal->hal_archive_num == ct->archive_nums[rc])
2843 if (rc >= ct->archive_num_count) {
2844 CDEBUG(D_INFO, "This copytool does not service archive #%d, "
2845 "ignoring this request.\n", hal->hal_archive_num);
2851 *msgsize = lnlh->lnl_msglen - sizeof(*lnlh);
2855 libcfs_ulnl_msg_free(&lnlh);
2861 /** Release the action list when done with it. */
2862 int llapi_copytool_free(struct hsm_action_list **hal)
2865 struct lnl_hdr *lnlh = (struct lnl_hdr *)*hal - 1;
2866 libcfs_ulnl_msg_free(&lnlh);