1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/liblustreapi.c
38 * Author: Peter J. Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
40 * Author: Robert Read <rread@clusterfs.com>
52 #include <sys/ioctl.h>
59 #include <sys/types.h>
60 #include <sys/syscall.h>
63 #ifdef HAVE_LINUX_UNISTD_H
64 #include <linux/unistd.h>
69 #include <liblustre.h>
70 #include <lnet/lnetctl.h>
72 #include <lustre_lib.h>
74 #include <lustre/liblustreapi.h>
76 static unsigned llapi_dir_filetype_table[] = {
85 #if defined(DT_DOOR) && defined(S_IFDOOR)
90 #if defined(DT_DOOR) && defined(S_IFDOOR)
91 static const int DT_MAX = DT_DOOR;
93 static const int DT_MAX = DT_SOCK;
96 static unsigned llapi_filetype_dir_table[] = {
105 #if defined(DT_DOOR) && defined(S_IFDOOR)
110 #if defined(DT_DOOR) && defined(S_IFDOOR)
111 static const int S_IFMAX = DT_DOOR;
113 static const int S_IFMAX = DT_SOCK;
116 /* liblustreapi message level */
117 static int llapi_msg_level = LLAPI_MSG_MAX;
119 void llapi_msg_set_level(int level)
121 /* ensure level is in the good range */
122 if (level < LLAPI_MSG_OFF)
123 llapi_msg_level = LLAPI_MSG_OFF;
124 else if (level > LLAPI_MSG_MAX)
125 llapi_msg_level = LLAPI_MSG_MAX;
127 llapi_msg_level = level;
130 void llapi_err(int level, char *fmt, ...)
133 int tmp_errno = abs(errno);
135 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
139 vfprintf(stderr, fmt, args);
142 if (level & LLAPI_MSG_NO_ERRNO)
143 fprintf(stderr, "\n");
145 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
148 #define llapi_err_noerrno(level, fmt, a...) \
149 llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
151 void llapi_printf(int level, char *fmt, ...)
155 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
159 vfprintf(stdout, fmt, args);
164 * size_units is unchanged if no specifier used
166 int parse_size(char *optarg, unsigned long long *size,
167 unsigned long long *size_units, int bytes_spec)
171 *size = strtoull(optarg, &end, 0);
174 if ((*end == 'b') && *(end+1) == '\0' &&
175 (*size & (~0ULL << (64 - 9))) == 0 &&
178 *size_units = 1 << 9;
179 } else if ((*end == 'b') && *(end+1) == '\0' &&
182 } else if ((*end == 'k' || *end == 'K') &&
183 *(end+1) == '\0' && (*size &
184 (~0ULL << (64 - 10))) == 0) {
186 *size_units = 1 << 10;
187 } else if ((*end == 'm' || *end == 'M') &&
188 *(end+1) == '\0' && (*size &
189 (~0ULL << (64 - 20))) == 0) {
191 *size_units = 1 << 20;
192 } else if ((*end == 'g' || *end == 'G') &&
193 *(end+1) == '\0' && (*size &
194 (~0ULL << (64 - 30))) == 0) {
196 *size_units = 1 << 30;
197 } else if ((*end == 't' || *end == 'T') &&
198 *(end+1) == '\0' && (*size &
199 (~0ULL << (64 - 40))) == 0) {
201 *size_units = 1ULL << 40;
202 } else if ((*end == 'p' || *end == 'P') &&
203 *(end+1) == '\0' && (*size &
204 (~0ULL << (64 - 50))) == 0) {
206 *size_units = 1ULL << 50;
207 } else if ((*end == 'e' || *end == 'E') &&
208 *(end+1) == '\0' && (*size &
209 (~0ULL << (64 - 60))) == 0) {
211 *size_units = 1ULL << 60;
220 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
221 int stripe_count, int stripe_pattern)
225 /* 64 KB is the largest common page size I'm aware of (on ia64), but
226 * check the local page size just in case. */
227 page_size = LOV_MIN_STRIPE_SIZE;
228 if (getpagesize() > page_size) {
229 page_size = getpagesize();
230 llapi_err_noerrno(LLAPI_MSG_WARN,
231 "warning: your page size (%u) is "
232 "larger than expected (%u)", page_size,
233 LOV_MIN_STRIPE_SIZE);
235 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
236 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
237 "must be an even multiple of %d bytes",
238 stripe_size, page_size);
241 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
243 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d",
247 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
249 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d",
253 if (stripe_size >= (1ULL << 32)){
255 llapi_err(LLAPI_MSG_ERROR, "warning: stripe size larger than 4G"
256 " is not currently supported and would wrap");
262 static int poolpath(char *fsname, char *pathname, char *pool_pathname);
264 int llapi_file_open_pool(const char *name, int flags, int mode,
265 unsigned long long stripe_size, int stripe_offset,
266 int stripe_count, int stripe_pattern, char *pool_name)
268 struct lov_user_md_v3 lum = { 0 };
271 char fsname[MAX_OBD_NAME + 1], *ptr;
273 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
274 if (fd < 0 && errno == EISDIR) {
275 fd = open(name, O_DIRECTORY | O_RDONLY);
281 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
285 if ((rc = llapi_stripe_limit_check(stripe_size, stripe_offset,
286 stripe_count, stripe_pattern)) != 0){
291 /* Initialize IOCTL striping pattern structure */
292 lum.lmm_magic = LOV_USER_MAGIC_V3;
293 lum.lmm_pattern = stripe_pattern;
294 lum.lmm_stripe_size = stripe_size;
295 lum.lmm_stripe_count = stripe_count;
296 lum.lmm_stripe_offset = stripe_offset;
298 /* in case user give the full pool name <fsname>.<poolname>, skip
300 if (pool_name != NULL) {
301 ptr = strchr(pool_name, '.');
303 strncpy(fsname, pool_name, ptr - pool_name);
305 /* if fsname matches a filesystem skip it
306 * if not keep the poolname as is */
307 if (poolpath(fsname, NULL, NULL) == 0)
310 strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME);
312 /* If no pool is specified at all, use V1 request */
313 lum.lmm_magic = LOV_USER_MAGIC_V1;
316 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
317 char *errmsg = "stripe already set";
319 if (errno != EEXIST && errno != EALREADY)
320 errmsg = strerror(errno);
322 llapi_err_noerrno(LLAPI_MSG_ERROR,
323 "error on ioctl "LPX64" for '%s' (%d): %s",
324 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg);
335 int llapi_file_open(const char *name, int flags, int mode,
336 unsigned long long stripe_size, int stripe_offset,
337 int stripe_count, int stripe_pattern)
339 return llapi_file_open_pool(name, flags, mode, stripe_size,
340 stripe_offset, stripe_count,
341 stripe_pattern, NULL);
344 int llapi_file_create(const char *name, unsigned long long stripe_size,
345 int stripe_offset, int stripe_count, int stripe_pattern)
349 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
350 stripe_offset, stripe_count, stripe_pattern,
359 int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
360 int stripe_offset, int stripe_count,
361 int stripe_pattern, char *pool_name)
365 fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size,
366 stripe_offset, stripe_count, stripe_pattern,
375 static int print_pool_members(char *fs, char *pool_dir, char *pool_file)
377 char path[PATH_MAX + 1];
381 llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fs, pool_file);
382 sprintf(path, "%s/%s", pool_dir, pool_file);
383 if ((fd = fopen(path, "r")) == NULL) {
384 llapi_err(LLAPI_MSG_ERROR, "Cannot open %s\n", path);
387 while (fgets(buf, sizeof(buf), fd) != NULL)
388 llapi_printf(LLAPI_MSG_NORMAL, buf);
395 * Find the fsname, the full path, and/or an open fd.
396 * Either the fsname or path must not be NULL
398 #define WANT_PATH 0x1
399 #define WANT_FSNAME 0x2
401 static int get_root_path(int want, char *fsname, int *outfd, char *path)
410 /* get the mount point */
411 fp = setmntent(MOUNTED, "r");
413 llapi_err(LLAPI_MSG_ERROR,
414 "setmntent(%s) failed: %s:", MOUNTED,
419 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
422 if (!llapi_is_lustre_mnt(&mnt))
425 ptr = strrchr(mnt.mnt_fsname, '/');
432 /* If path was specified and matches, store the fsname */
433 if ((want & WANT_FSNAME) && (strcmp(mnt.mnt_dir, path) == 0))
435 /* Else check the fsname for a match */
436 else if (strcmp(ptr, fsname) != 0)
441 if (want & WANT_PATH)
442 strcpy(path, mnt.mnt_dir);
443 if (want & WANT_FD) {
444 fd = open(mnt.mnt_dir,
445 O_RDONLY | O_DIRECTORY | O_NONBLOCK);
457 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
458 "can't find fs root for '%s': %d",
459 (want & WANT_PATH) ? fsname : path, rc);
463 int llapi_search_fsname(const char *pathname, char *fsname)
465 return get_root_path(WANT_FSNAME, fsname, NULL, (char *)pathname);
468 /* return the first file matching this pattern */
469 static int first_match(char *pattern, char *buffer)
473 if (glob(pattern, GLOB_BRACE, NULL, &glob_info))
476 if (glob_info.gl_pathc < 1) {
477 globfree(&glob_info);
481 strcpy(buffer, glob_info.gl_pathv[0]);
483 globfree(&glob_info);
488 * find the pool directory path under /proc
489 * (can be also used to test if a fsname is known)
491 static int poolpath(char *fsname, char *pathname, char *pool_pathname)
494 char pattern[PATH_MAX + 1];
495 char buffer[PATH_MAX];
497 if (fsname == NULL) {
498 rc = get_root_path(WANT_FSNAME, buffer, NULL, pathname);
502 strcpy(pathname, fsname);
505 snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname);
506 rc = first_match(pattern, buffer);
510 /* in fsname test mode, pool_pathname is NULL */
511 if (pool_pathname != NULL)
512 strcpy(pool_pathname, buffer);
517 int llapi_poollist(char *name)
521 char rname[PATH_MAX + 1], pathname[PATH_MAX + 1];
525 /* is name a pathname ? */
526 ptr = strchr(name, '/');
528 /* only absolute pathname is supported */
531 if (!realpath(name, rname)) {
533 llapi_err(LLAPI_MSG_ERROR, "invalid path '%s'", name);
537 rc = poolpath(NULL, rname, pathname);
540 llapi_err(LLAPI_MSG_ERROR, "'%s' is not"
541 " a Lustre filesystem", name);
547 /* name is FSNAME[.POOLNAME] */
549 poolname = strchr(name, '.');
550 if (poolname != NULL) {
554 rc = poolpath(fsname, NULL, pathname);
558 llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
563 if (poolname != NULL) {
564 rc = print_pool_members(fsname, pathname, poolname);
571 llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
572 if ((dir = opendir(pathname)) == NULL) {
575 while ((pool = readdir(dir)) != NULL) {
576 if (!((pool->d_name[0] == '.') &&
577 (pool->d_name[1] == '\0')) &&
578 !((pool->d_name[0] == '.') &&
579 (pool->d_name[1] == '.') &&
580 (pool->d_name[2] == '\0')))
581 llapi_printf(LLAPI_MSG_NORMAL, " %s.%s\n",
582 fsname, pool->d_name);
589 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
590 void *data, cfs_dirent_t *de);
592 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
593 #define OBD_NOT_FOUND (-1)
595 static int common_param_init(struct find_param *param)
597 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
598 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
599 llapi_err(LLAPI_MSG_ERROR,
600 "error: allocation of %d bytes for ioctl",
601 sizeof(lstat_t) + param->lumlen);
605 param->got_uuids = 0;
606 param->obdindexes = NULL;
607 param->obdindex = OBD_NOT_FOUND;
611 static void find_param_fini(struct find_param *param)
613 if (param->obdindexes)
614 free(param->obdindexes);
620 int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
622 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
625 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
630 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
634 fd = open(path, O_RDONLY);
637 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
641 rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
649 * If uuidp is NULL, return the number of available obd uuids.
650 * If uuidp is non-NULL, then it will return the uuids of the obds. If
651 * there are more OSTs then allocated to uuidp, then an error is returned with
652 * the ost_count set to number of available obd uuids.
654 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
656 struct obd_uuid lov_name;
659 int rc = 0, index = 0;
661 /* Get the lov name */
662 rc = llapi_file_fget_lov_uuid(fd, &lov_name);
666 /* Now get the ost uuids from /proc */
667 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
669 fp = fopen(buf, "r");
672 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
676 while (fgets(buf, sizeof(buf), fp) != NULL) {
677 if (uuidp && (index < *ost_count)) {
678 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
686 if (uuidp && (index >= *ost_count))
693 int llapi_get_obd_count(char *mnt, int *count, int is_mdt)
700 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
705 rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count);
711 /* Here, param->obduuid points to a single obduuid, the index of which is
712 * returned in param->obdindex */
713 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
715 struct obd_uuid lov_uuid;
716 char uuid[sizeof(struct obd_uuid)];
721 /* Get the lov name */
722 rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
724 if (errno != ENOTTY) {
726 llapi_err(LLAPI_MSG_ERROR,
727 "error: can't get lov name: %s", dname);
734 param->got_uuids = 1;
736 /* Now get the ost uuids from /proc */
737 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
739 fp = fopen(buf, "r");
742 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
746 if (!param->obduuid && !param->quiet && !param->obds_printed)
747 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
749 while (fgets(buf, sizeof(buf), fp) != NULL) {
750 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
753 if (param->obduuid) {
754 if (strncmp(param->obduuid->uuid, uuid,
755 sizeof(uuid)) == 0) {
756 param->obdindex = index;
759 } else if (!param->quiet && !param->obds_printed) {
760 /* Print everything */
761 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
764 param->obds_printed = 1;
768 if (!param->quiet && param->obduuid &&
769 (param->obdindex == OBD_NOT_FOUND)) {
770 llapi_err_noerrno(LLAPI_MSG_ERROR,
771 "error: %s: unknown obduuid: %s",
772 __FUNCTION__, param->obduuid->uuid);
779 /* In this case, param->obduuid will be an array of obduuids and
780 * obd index for all these obduuids will be returned in
781 * param->obdindexes */
782 static int setup_obd_indexes(DIR *dir, struct find_param *param)
784 struct obd_uuid *uuids = NULL;
785 int obdcount = INIT_ALLOC_NUM_OSTS;
786 int ret, obd_valid = 0, obdnum, i;
788 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
789 sizeof(struct obd_uuid));
794 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
797 struct obd_uuid *uuids_temp;
799 if (ret == -EOVERFLOW) {
800 uuids_temp = realloc(uuids, obdcount *
801 sizeof(struct obd_uuid));
802 if (uuids_temp != NULL)
803 goto retry_get_uuids;
808 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
812 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
813 if (param->obdindexes == NULL)
816 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
817 for (i = 0; i <= obdcount; i++) {
818 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
819 (char *)&uuids[i]) == 0) {
820 param->obdindexes[obdnum] = i;
826 param->obdindexes[obdnum] = OBD_NOT_FOUND;
830 param->obdindex = OBD_NOT_FOUND;
832 param->obdindex = obd_valid;
834 param->got_uuids = 1;
839 static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
840 int is_dir, int verbose, int quiet,
843 char *prefix = is_dir ? "" : "lmm_";
844 char nl = is_dir ? ' ' : '\n';
847 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
849 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
850 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
852 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
854 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
858 if (verbose & VERBOSE_COUNT) {
860 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ",
862 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
863 (int)lum->lmm_stripe_count, nl);
866 if (verbose & VERBOSE_SIZE) {
868 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ",
870 llapi_printf(LLAPI_MSG_NORMAL, "%u%c", lum->lmm_stripe_size,
874 if ((verbose & VERBOSE_DETAIL) && !is_dir) {
875 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c",
876 lum->lmm_pattern, nl);
879 if (verbose & VERBOSE_OFFSET) {
881 llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ",
883 llapi_printf(LLAPI_MSG_NORMAL, "%u%c",
884 lum->lmm_objects[0].l_ost_idx, nl);
887 if ((verbose & VERBOSE_POOL) && (pool_name != NULL))
888 llapi_printf(LLAPI_MSG_NORMAL, "pool: %s%c", pool_name, nl);
891 llapi_printf(LLAPI_MSG_NORMAL, "\n");
894 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
895 struct lov_user_ost_data_v1 *objects,
896 char *path, int is_dir,
897 int obdindex, int quiet, int header, int body)
899 int i, obdstripe = 0;
901 if (obdindex != OBD_NOT_FOUND) {
902 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
903 if (obdindex == objects[i].l_ost_idx) {
904 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
911 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
915 /* if it's a directory */
917 if (obdstripe == 1) {
918 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
919 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
920 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
922 /* maintain original behavior */
924 header |= VERBOSE_ALL;
925 lov_dump_user_lmm_header(lum, path, is_dir, header,
931 if (header && (obdstripe == 1))
932 lov_dump_user_lmm_header(lum, NULL, is_dir, header, quiet,
936 if ((!quiet) && (obdstripe == 1))
937 llapi_printf(LLAPI_MSG_NORMAL,
938 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
940 for (i = 0; i < lum->lmm_stripe_count; i++) {
941 int idx = objects[i].l_ost_idx;
942 long long oid = objects[i].l_object_id;
943 long long gr = objects[i].l_object_gr;
944 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
945 llapi_printf(LLAPI_MSG_NORMAL,
946 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
948 obdindex == idx ? " *" : "");
950 llapi_printf(LLAPI_MSG_NORMAL, "\n");
954 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
955 int is_dir, int obdindex, int quiet,
956 int header, int body)
958 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
959 int i, obdstripe = 0;
961 if (obdindex != OBD_NOT_FOUND) {
962 for (i = 0; i < lumj->lmm_stripe_count; i++) {
963 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
964 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
971 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
975 if (header && obdstripe == 1) {
976 lov_dump_user_lmm_header(lum, NULL, 0, header, quiet, NULL);
978 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
979 lumj->lmm_extent_count);
983 unsigned long long start = -1, end = 0;
984 if (!quiet && obdstripe == 1)
985 llapi_printf(LLAPI_MSG_NORMAL,
986 "joined\tobdidx\t\t objid\t\tobjid\t\t "
987 "group\t\tstart\t\tend\n");
988 for (i = 0; i < lumj->lmm_stripe_count; i++) {
989 int idx = lumj->lmm_objects[i].l_ost_idx;
990 long long oid = lumj->lmm_objects[i].l_object_id;
991 long long gr = lumj->lmm_objects[i].l_object_gr;
992 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
993 llapi_printf(LLAPI_MSG_NORMAL,
994 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
996 obdindex == idx ? " *" : "");
997 if (start != lumj->lmm_objects[i].l_extent_start ||
998 end != lumj->lmm_objects[i].l_extent_end) {
999 start = lumj->lmm_objects[i].l_extent_start;
1000 llapi_printf(LLAPI_MSG_NORMAL,"\t%14llu",start);
1001 end = lumj->lmm_objects[i].l_extent_end;
1002 if (end == (unsigned long long)-1)
1003 llapi_printf(LLAPI_MSG_NORMAL,
1006 llapi_printf(LLAPI_MSG_NORMAL,
1009 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
1012 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1016 void llapi_lov_dump_user_lmm(struct find_param *param,
1017 char *path, int is_dir)
1019 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
1020 case LOV_USER_MAGIC_V1:
1021 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL,
1022 param->lmd->lmd_lmm.lmm_objects,
1024 param->obdindex, param->quiet,
1026 (param->verbose || !param->obduuid));
1028 case LOV_USER_MAGIC_JOIN:
1029 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
1030 param->obdindex, param->quiet,
1032 (param->verbose || !param->obduuid));
1034 case LOV_USER_MAGIC_V3: {
1035 char pool_name[LOV_MAXPOOLNAME + 1];
1036 struct lov_user_ost_data_v1 *objects;
1037 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1039 strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME);
1040 pool_name[LOV_MAXPOOLNAME] = '\0';
1041 objects = lmmv3->lmm_objects;
1042 lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name,
1043 objects, path, is_dir,
1044 param->obdindex, param->quiet,
1046 (param->verbose || !param->obduuid));
1050 llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x "
1051 "(expecting one of %#x %#x %#x)\n",
1052 *(__u32 *)¶m->lmd->lmd_lmm,
1053 LOV_USER_MAGIC_V1, LOV_USER_MAGIC_JOIN,
1059 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
1065 fname = strrchr(path, '/');
1067 /* It should be a file (or other non-directory) */
1068 if (fname == NULL) {
1069 dname = (char *)malloc(2);
1073 fname = (char *)path;
1075 dname = (char *)malloc(fname - path + 1);
1078 strncpy(dname, path, fname - path);
1079 dname[fname - path] = '\0';
1083 if ((fd = open(dname, O_RDONLY)) == -1) {
1089 strcpy((char *)lum, fname);
1090 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
1093 if (close(fd) == -1 && rc == 0)
1101 int llapi_file_lookup(int dirfd, const char *name)
1103 struct obd_ioctl_data data = { 0 };
1108 if (dirfd < 0 || name == NULL)
1111 data.ioc_version = OBD_IOCTL_VERSION;
1112 data.ioc_len = sizeof(data);
1113 data.ioc_inlbuf1 = (char *)name;
1114 data.ioc_inllen1 = strlen(name) + 1;
1116 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
1118 llapi_err(LLAPI_MSG_ERROR,
1119 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
1124 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
1127 int llapi_mds_getfileinfo(char *path, DIR *parent,
1128 struct lov_user_mds_data *lmd)
1130 lstat_t *st = &lmd->lmd_st;
1131 char *fname = strrchr(path, '/');
1137 fname = (fname == NULL ? path : fname + 1);
1138 /* retrieve needed file info */
1139 strncpy((char *)lmd, fname,
1140 lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC));
1141 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
1144 if (errno == ENOTTY) {
1145 /* ioctl is not supported, it is not a lustre fs.
1146 * Do the regular lstat(2) instead. */
1147 ret = lstat_f(path, st);
1149 llapi_err(LLAPI_MSG_ERROR,
1150 "error: %s: lstat failed for %s",
1151 __FUNCTION__, path);
1154 } else if (errno == ENOENT) {
1155 llapi_err(LLAPI_MSG_WARN,
1156 "warning: %s: %s does not exist",
1157 __FUNCTION__, path);
1160 llapi_err(LLAPI_MSG_ERROR,
1161 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
1162 __FUNCTION__, path);
1170 static DIR *opendir_parent(char *path)
1176 fname = strrchr(path, '/');
1178 return opendir(".");
1182 parent = opendir(path);
1187 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
1188 semantic_func_t sem_init,
1189 semantic_func_t sem_fini, void *data,
1200 if (!d && errno != ENOTDIR) {
1201 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
1202 __FUNCTION__, path);
1204 } else if (!d && !parent) {
1205 /* ENOTDIR. Open the parent dir. */
1206 p = opendir_parent(path);
1208 GOTO(out, ret = -EINVAL);
1211 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
1217 while ((dent = readdir64(d)) != NULL) {
1218 ((struct find_param *)data)->have_fileinfo = 0;
1220 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1223 /* Don't traverse .lustre directory */
1224 if (!(strcmp(dent->d_name, dot_lustre_name)))
1228 if ((len + dent->d_reclen + 2) > size) {
1229 llapi_err(LLAPI_MSG_ERROR,
1230 "error: %s: string buffer is too small",
1235 strcat(path, dent->d_name);
1237 if (dent->d_type == DT_UNKNOWN) {
1238 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
1240 ret = llapi_mds_getfileinfo(path, d,
1241 ((struct find_param *)data)->lmd);
1243 ((struct find_param *)data)->have_fileinfo = 1;
1245 llapi_filetype_dir_table[st->st_mode &
1252 switch (dent->d_type) {
1254 llapi_err(LLAPI_MSG_ERROR,
1255 "error: %s: '%s' is UNKNOWN type %d",
1256 __FUNCTION__, dent->d_name, dent->d_type);
1259 ret = llapi_semantic_traverse(path, size, d, sem_init,
1260 sem_fini, data, dent);
1267 ret = sem_init(path, d, NULL, data, dent);
1271 if (sem_fini && ret == 0)
1272 sem_fini(path, d, NULL, data, dent);
1280 sem_fini(path, parent, d, data, de);
1289 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
1290 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
1292 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
1293 * The table below gives the answers for the specified parameters (value and
1294 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
1295 * --------------------------------------
1296 * 1 | file > limit; sign > 0 | -1 / -1 |
1297 * 2 | file = limit; sign > 0 | ? / 1 |
1298 * 3 | file < limit; sign > 0 | ? / 1 |
1299 * 4 | file > limit; sign = 0 | -1 / -1 |
1300 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
1301 * 6 | file < limit; sign = 0 | ? / -1 |
1302 * 7 | file > limit; sign < 0 | 1 / 1 |
1303 * 8 | file = limit; sign < 0 | ? / -1 |
1304 * 9 | file < limit; sign < 0 | ? / -1 |
1305 * --------------------------------------
1306 * Note: 5th actually means that the value is within the interval
1307 * (limit - margin, limit]. */
1308 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
1309 unsigned long long margin, int mds)
1317 if (file <= limit && file + margin > limit)
1319 if (file + margin <= limit)
1320 return mds ? 0 : -1;
1333 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
1334 * Return -1 or 1 if file timestamp does not or does match the given criteria
1335 * correspondingly. Return 0 if the MDS time is being checked and there are
1336 * attributes on OSTs and it is not yet clear if the timespamp matches.
1338 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
1339 * updated timestamps. */
1340 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
1345 /* Check if file is accepted. */
1347 ret = find_value_cmp(st->st_atime, param->atime,
1348 param->asign, 24 * 60 * 60, mds);
1355 ret = find_value_cmp(st->st_mtime, param->mtime,
1356 param->msign, 24 * 60 * 60, mds);
1360 /* If the previous check matches, but this one is not yet clear,
1361 * we should return 0 to do an RPC on OSTs. */
1367 ret = find_value_cmp(st->st_ctime, param->ctime,
1368 param->csign, 24 * 60 * 60, mds);
1372 /* If the previous check matches, but this one is not yet clear,
1373 * we should return 0 to do an RPC on OSTs. */
1381 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1382 void *data, cfs_dirent_t *de)
1384 struct find_param *param = (struct find_param *)data;
1385 int decision = 1; /* 1 is accepted; -1 is rejected. */
1386 lstat_t *st = ¶m->lmd->lmd_st;
1388 int checked_type = 0;
1391 LASSERT(parent != NULL || dir != NULL);
1393 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1395 /* If a regular expression is presented, make the initial decision */
1396 if (param->pattern != NULL) {
1397 char *fname = strrchr(path, '/');
1398 fname = (fname == NULL ? path : fname + 1);
1399 ret = fnmatch(param->pattern, fname, 0);
1400 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1401 (ret == 0 && param->exclude_pattern))
1405 /* See if we can check the file type from the dirent. */
1406 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1407 de->d_type < DT_MAX) {
1409 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1410 if (param->exclude_type)
1413 if (!param->exclude_type)
1419 /* If a time or OST should be checked, the decision is not taken yet. */
1420 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1425 /* Request MDS for the stat info. */
1426 if (param->have_fileinfo == 0) {
1428 /* retrieve needed file info */
1429 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1430 (void *)param->lmd);
1432 char *fname = strrchr(path, '/');
1433 fname = (fname == NULL ? path : fname + 1);
1435 /* retrieve needed file info */
1436 strncpy((char *)param->lmd, fname, param->lumlen);
1437 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1438 (void *)param->lmd);
1443 if (errno == ENOTTY) {
1444 /* ioctl is not supported, it is not a lustre fs.
1445 * Do the regular lstat(2) instead. */
1447 ret = lstat_f(path, st);
1449 llapi_err(LLAPI_MSG_ERROR,
1450 "error: %s: lstat failed for %s",
1451 __FUNCTION__, path);
1454 } else if (errno == ENOENT) {
1455 llapi_err(LLAPI_MSG_WARN,
1456 "warning: %s: %s does not exist",
1457 __FUNCTION__, path);
1460 llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
1461 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1462 "IOC_MDC_GETFILEINFO", path);
1467 if (param->type && !checked_type) {
1468 if ((st->st_mode & S_IFMT) == param->type) {
1469 if (param->exclude_type)
1472 if (!param->exclude_type)
1478 if (param->obduuid) {
1479 if (lustre_fs && param->got_uuids &&
1480 param->st_dev != st->st_dev) {
1481 /* A lustre/lustre mount point is crossed. */
1482 param->got_uuids = 0;
1483 param->obds_printed = 0;
1484 param->obdindex = OBD_NOT_FOUND;
1487 if (lustre_fs && !param->got_uuids) {
1488 ret = setup_obd_indexes(dir ? dir : parent, param);
1492 param->st_dev = st->st_dev;
1493 } else if (!lustre_fs && param->got_uuids) {
1494 /* A lustre/non-lustre mount point is crossed. */
1495 param->got_uuids = 0;
1496 param->obdindex = OBD_NOT_FOUND;
1500 /* If an OBD UUID is specified but no one matches, skip this file. */
1501 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1504 /* If a OST UUID is given, and some OST matches, check it here. */
1505 if (param->obdindex != OBD_NOT_FOUND) {
1506 if (!S_ISREG(st->st_mode))
1509 /* Only those files should be accepted, which have a
1510 * stripe on the specified OST. */
1511 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1515 struct lov_user_ost_data_v1 *lmm_objects;
1517 if (param->lmd->lmd_lmm.lmm_magic ==
1518 LOV_USER_MAGIC_V3) {
1519 struct lov_user_md_v3 *lmmv3 =
1520 (void *)¶m->lmd->lmd_lmm;
1522 lmm_objects = lmmv3->lmm_objects;
1524 lmm_objects = param->lmd->lmd_lmm.lmm_objects;
1528 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1529 for (j = 0; j < param->num_obds; j++) {
1530 if (param->obdindexes[j] ==
1531 lmm_objects[i].l_ost_idx)
1536 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1541 if (param->check_uid) {
1542 if (st->st_uid == param->uid) {
1543 if (param->exclude_uid)
1546 if (!param->exclude_uid)
1551 if (param->check_gid) {
1552 if (st->st_gid == param->gid) {
1553 if (param->exclude_gid)
1556 if (!param->exclude_gid)
1561 if (param->check_pool) {
1562 struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm;
1564 /* empty requested pool is taken as no pool search => V1 */
1565 if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) &&
1566 (param->poolname[0] == '\0')) ||
1567 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1568 (strncmp(lmmv3->lmm_pool_name,
1569 param->poolname, LOV_MAXPOOLNAME) == 0)) ||
1570 ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) &&
1571 (strcmp(param->poolname, "*") == 0))) {
1572 if (param->exclude_pool)
1575 if (!param->exclude_pool)
1580 /* Check the time on mds. */
1584 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1585 param->lmd->lmd_lmm.lmm_stripe_count)
1587 decision = find_time_check(st, param, for_mds);
1591 /* If file still fits the request, ask ost for updated info.
1592 The regular stat is almost of the same speed as some new
1593 'glimpse-size-ioctl'. */
1594 if (!decision && S_ISREG(st->st_mode) &&
1595 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1596 if (param->obdindex != OBD_NOT_FOUND) {
1597 /* Check whether the obd is active or not, if it is
1598 * not active, just print the object affected by this
1601 struct obd_statfs stat_buf;
1602 struct obd_uuid uuid_buf;
1604 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1605 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1606 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1607 param->obdindex, &stat_buf,
1610 if (ret == -ENODATA || ret == -ENODEV
1613 llapi_printf(LLAPI_MSG_NORMAL,
1614 "obd_uuid: %s failed %s ",
1615 param->obduuid->uuid,
1621 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1622 (void *)param->lmd);
1623 } else if (parent) {
1624 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1625 (void *)param->lmd);
1629 if (errno == ENOENT) {
1630 llapi_err(LLAPI_MSG_ERROR,
1631 "warning: %s: %s does not exist",
1632 __FUNCTION__, path);
1635 llapi_err(LLAPI_MSG_ERROR,
1636 "%s: IOC_LOV_GETINFO on %s failed",
1637 __FUNCTION__, path);
1642 /* Check the time on osc. */
1643 decision = find_time_check(st, param, 0);
1649 decision = find_value_cmp(st->st_size, param->size,
1650 param->size_sign, param->size_units,
1654 if (decision != -1) {
1655 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1657 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1659 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1663 /* Do not get down anymore? */
1664 if (param->depth == param->maxdepth)
1671 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1674 struct find_param *param = (struct find_param *)data;
1679 int llapi_find(char *path, struct find_param *param)
1682 int ret, len = strlen(path);
1684 if (len > PATH_MAX) {
1685 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1686 __FUNCTION__, path);
1690 buf = (char *)malloc(PATH_MAX + 1);
1694 ret = common_param_init(param);
1702 strncpy(buf, path, PATH_MAX + 1);
1703 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1704 cb_common_fini, param, NULL);
1706 find_param_fini(param);
1708 return ret < 0 ? ret : 0;
1711 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1714 struct find_param *param = (struct find_param *)data;
1717 LASSERT(parent != NULL || d != NULL);
1720 if (!param->got_uuids) {
1721 ret = setup_obd_uuid(d ? d : parent, path, param);
1727 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1728 (void *)¶m->lmd->lmd_lmm);
1729 } else if (parent) {
1730 char *fname = strrchr(path, '/');
1731 fname = (fname == NULL ? path : fname + 1);
1733 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1734 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1735 (void *)¶m->lmd->lmd_lmm);
1739 if (errno == ENODATA) {
1740 if (!param->obduuid && !param->quiet)
1741 llapi_printf(LLAPI_MSG_NORMAL,
1742 "%s has no stripe info\n", path);
1744 } else if (errno == ENOTTY) {
1745 llapi_err(LLAPI_MSG_ERROR,
1746 "%s: '%s' not on a Lustre fs?",
1747 __FUNCTION__, path);
1748 } else if (errno == ENOENT) {
1749 llapi_err(LLAPI_MSG_WARN,
1750 "warning: %s: %s does not exist",
1751 __FUNCTION__, path);
1754 llapi_err(LLAPI_MSG_ERROR,
1755 "error: %s: %s failed for %s",
1756 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1757 "IOC_MDC_GETFILESTRIPE", path);
1763 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1765 /* Do not get down anymore? */
1766 if (param->depth == param->maxdepth)
1773 int llapi_getstripe(char *path, struct find_param *param)
1776 int ret = 0, len = strlen(path);
1778 if (len > PATH_MAX) {
1779 llapi_err(LLAPI_MSG_ERROR,
1780 "%s: Path name '%s' is too long",
1781 __FUNCTION__, path);
1785 buf = (char *)malloc(PATH_MAX + 1);
1789 ret = common_param_init(param);
1797 strncpy(buf, path, PATH_MAX + 1);
1798 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1799 cb_common_fini, param, NULL);
1800 find_param_fini(param);
1802 return ret < 0 ? ret : 0;
1805 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1806 struct obd_statfs *stat_buf,
1807 struct obd_uuid *uuid_buf)
1810 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1812 struct obd_ioctl_data data = { 0 };
1815 data.ioc_inlbuf1 = (char *)&type;
1816 data.ioc_inllen1 = sizeof(__u32);
1817 data.ioc_inlbuf2 = (char *)&index;
1818 data.ioc_inllen2 = sizeof(__u32);
1819 data.ioc_pbuf1 = (char *)stat_buf;
1820 data.ioc_plen1 = sizeof(struct obd_statfs);
1821 data.ioc_pbuf2 = (char *)uuid_buf;
1822 data.ioc_plen2 = sizeof(struct obd_uuid);
1824 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1825 llapi_err(LLAPI_MSG_ERROR,
1826 "llapi_obd_statfs: error packing ioctl data");
1830 fd = open(path, O_RDONLY);
1831 if (errno == EISDIR)
1832 fd = open(path, O_DIRECTORY | O_RDONLY);
1835 rc = errno ? -errno : -EBADF;
1836 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
1837 __FUNCTION__, path);
1840 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1842 rc = errno ? -errno : -EINVAL;
1848 #define MAX_STRING_SIZE 128
1849 #define DEVICES_LIST "/proc/fs/lustre/devices"
1851 int llapi_ping(char *obd_type, char *obd_name)
1853 char path[MAX_STRING_SIZE];
1857 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1858 obd_type, obd_name);
1860 fd = open(path, O_WRONLY);
1863 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1867 rc = write(fd, buf, 1);
1875 int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
1877 char buf[MAX_STRING_SIZE];
1878 FILE *fp = fopen(DEVICES_LIST, "r");
1883 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1887 while (fgets(buf, sizeof(buf), fp) != NULL) {
1888 char *obd_type_name = NULL;
1889 char *obd_name = NULL;
1890 char *obd_uuid = NULL;
1892 struct obd_ioctl_data datal = { 0, };
1893 struct obd_statfs osfs_buffer;
1895 while(bufp[0] == ' ')
1898 for(i = 0; i < 3; i++) {
1899 obd_type_name = strsep(&bufp, " ");
1901 obd_name = strsep(&bufp, " ");
1902 obd_uuid = strsep(&bufp, " ");
1904 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1906 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1907 datal.ioc_plen1 = sizeof(osfs_buffer);
1909 for (i = 0; i < type_num; i++) {
1910 if (strcmp(obd_type_name, obd_type[i]) != 0)
1913 cb(obd_type_name, obd_name, obd_uuid, args);
1920 static void do_target_check(char *obd_type_name, char *obd_name,
1921 char *obd_uuid, void *args)
1925 rc = llapi_ping(obd_type_name, obd_name);
1926 if (rc == ENOTCONN) {
1927 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
1929 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1931 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1935 int llapi_target_check(int type_num, char **obd_type, char *dir)
1937 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1940 #undef MAX_STRING_SIZE
1942 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1944 char raw[OBD_MAX_IOCTL_BUFFER];
1945 char out[LLOG_CHUNK_SIZE];
1947 struct obd_ioctl_data data = { 0 };
1952 sprintf(key, "%s", keyword);
1953 memset(raw, 0, sizeof(raw));
1954 memset(out, 0, sizeof(out));
1955 data.ioc_inlbuf1 = key;
1956 data.ioc_inllen1 = strlen(key) + 1;
1958 data.ioc_inlbuf2 = node_name;
1959 data.ioc_inllen2 = strlen(node_name) + 1;
1961 data.ioc_pbuf1 = out;
1962 data.ioc_plen1 = sizeof(out);
1963 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1967 root = opendir(dir);
1970 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1974 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1976 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1978 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1984 /* Is this a lustre fs? */
1985 int llapi_is_lustre_mnttype(const char *type)
1987 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1990 /* Is this a lustre client fs? */
1991 int llapi_is_lustre_mnt(struct mntent *mnt)
1993 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1994 strstr(mnt->mnt_fsname, ":/") != NULL);
1997 int llapi_quotacheck(char *mnt, int check_type)
2002 root = opendir(mnt);
2004 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2008 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
2014 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
2020 root = opendir(mnt);
2022 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2027 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
2031 if (poll_intvl < 30)
2039 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
2044 root = opendir(mnt);
2046 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
2050 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
2056 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
2059 struct find_param *param = (struct find_param *)data;
2063 LASSERT(parent != NULL || d != NULL);
2066 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
2067 (void *)param->lmd);
2068 } else if (parent) {
2069 char *fname = strrchr(path, '/');
2070 fname = (fname == NULL ? path : fname + 1);
2072 strncpy((char *)param->lmd, fname, param->lumlen);
2073 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
2074 (void *)param->lmd);
2080 if (errno == ENODATA) {
2081 if (!param->obduuid && !param->quiet)
2082 llapi_err(LLAPI_MSG_ERROR,
2083 "%s has no stripe info", path);
2085 } else if (errno == ENOENT) {
2086 llapi_err(LLAPI_MSG_ERROR,
2087 "warning: %s: %s does not exist",
2088 __FUNCTION__, path);
2090 } else if (errno != EISDIR) {
2092 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
2093 d ? "LL_IOC_MDC_GETINFO" :
2094 "IOC_MDC_GETFILEINFO", path);
2099 st = ¶m->lmd->lmd_st;
2101 /* libc chown() will do extra check, and if the real owner is
2102 * the same as the ones to set, it won't fall into kernel, so
2103 * invoke syscall directly. */
2104 rc = syscall(SYS_chown, path, -1, -1);
2106 llapi_err(LLAPI_MSG_ERROR,"error: chown %s (%u,%u)", path);
2108 rc = chmod(path, st->st_mode);
2110 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
2116 int llapi_quotachown(char *path, int flag)
2118 struct find_param param;
2120 int ret = 0, len = strlen(path);
2122 if (len > PATH_MAX) {
2123 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
2124 __FUNCTION__, path);
2128 buf = (char *)malloc(PATH_MAX + 1);
2132 memset(¶m, 0, sizeof(param));
2133 param.recursive = 1;
2137 ret = common_param_init(¶m);
2141 strncpy(buf, path, PATH_MAX + 1);
2142 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
2143 NULL, ¶m, NULL);
2145 find_param_fini(¶m);
2153 #include <sys/wait.h>
2157 static int rmtacl_notify(int ops)
2161 int found = 0, fd, rc;
2163 fp = setmntent(MOUNTED, "r");
2165 perror("setmntent");
2170 mnt = getmntent(fp);
2174 if (!llapi_is_lustre_mnt(mnt))
2177 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
2183 rc = ioctl(fd, LL_IOC_RMTACL, ops);
2195 static char *next_token(char *p, int div)
2201 while (*p && *p != ':' && !isspace(*p))
2204 while (*p == ':' || isspace(*p))
2207 return *p ? p : NULL;
2210 static int rmtacl_name2id(char *name, int is_user)
2215 if ((pw = getpwnam(name)) == NULL)
2218 return (int)(pw->pw_uid);
2222 if ((gr = getgrnam(name)) == NULL)
2225 return (int)(gr->gr_gid);
2229 static int isodigit(int c)
2231 return (c >= '0' && c <= '7') ? 1 : 0;
2235 * Whether the name is just digits string (uid/gid) already or not.
2240 static int str_is_id(char *str)
2247 if (*str == 'x' || *str == 'X') { /* for Hex. */
2248 if (!isxdigit(*(++str)))
2251 while (isxdigit(*(++str)));
2252 } else if (isodigit(*str)) { /* for Oct. */
2253 while (isodigit(*(++str)));
2255 } else if (isdigit(*str)) { /* for Dec. */
2256 while (isdigit(*(++str)));
2259 return (*str == 0) ? 1 : 0;
2269 #define RMTACL_OPTNAME(name) name, sizeof(name) - 1
2271 static rmtacl_name_t rmtacl_namelist[] = {
2272 { RMTACL_OPTNAME("user:"), 1, 0 },
2273 { RMTACL_OPTNAME("group:"), 0, 0 },
2274 { RMTACL_OPTNAME("default:user:"), 1, 0 },
2275 { RMTACL_OPTNAME("default:group:"), 0, 0 },
2276 /* for --tabular option */
2277 { RMTACL_OPTNAME("user"), 1, 1 },
2278 { RMTACL_OPTNAME("group"), 0, 1 },
2282 static int rgetfacl_output(char *str)
2284 char *start = NULL, *end = NULL;
2285 int is_user = 0, n, id;
2292 for (rn = rmtacl_namelist; rn->name; rn++) {
2293 if(strncmp(str, rn->name, rn->length) == 0) {
2294 if (!rn->next_token)
2295 start = str + rn->length;
2297 start = next_token(str + rn->length, 0);
2298 is_user = rn->is_user;
2303 end = next_token(start, 1);
2304 if (end == NULL || start == end) {
2305 n = printf("%s", str);
2311 id = rmtacl_name2id(start, is_user);
2312 if (id == INVALID_ID) {
2313 if (str_is_id(start)) {
2315 n = printf("%s", str);
2318 } else if ((id == NOBODY_UID && is_user) ||
2319 (id == NOBODY_GID && !is_user)) {
2321 n = printf("%s", str);
2325 n = printf("%s%d%s", str, id, end);
2330 static int child_status(int status)
2332 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
2335 static int do_rmtacl(int argc, char *argv[], int ops, int (output_func)(char *))
2348 if ((pid = fork()) < 0) {
2354 /* child process redirects its output. */
2357 if (dup2(fd[1], 1) < 0) {
2368 status = rmtacl_notify(ops);
2372 exit(execvp(argv[0], argv));
2375 /* the following is parent process */
2376 if ((fp = fdopen(fd[0], "r")) == NULL) {
2383 while (fgets(buf, PIPE_BUF, fp) != NULL) {
2384 if (output_func(buf) < 0)
2385 fprintf(stderr, "WARNING: unexpected error!\n[%s]\n",
2391 if (waitpid(pid, &status, 0) < 0) {
2396 return child_status(status);
2399 int llapi_lsetfacl(int argc, char *argv[])
2401 return do_rmtacl(argc, argv, RMT_LSETFACL, NULL);
2404 int llapi_lgetfacl(int argc, char *argv[])
2406 return do_rmtacl(argc, argv, RMT_LGETFACL, NULL);
2409 int llapi_rsetfacl(int argc, char *argv[])
2411 return do_rmtacl(argc, argv, RMT_RSETFACL, NULL);
2414 int llapi_rgetfacl(int argc, char *argv[])
2416 return do_rmtacl(argc, argv, RMT_RGETFACL, rgetfacl_output);
2419 int llapi_cp(int argc, char *argv[])
2423 rc = rmtacl_notify(RMT_RSETFACL);
2427 exit(execvp(argv[0], argv));
2430 int llapi_ls(int argc, char *argv[])
2434 rc = rmtacl_notify(RMT_LGETFACL);
2438 exit(execvp(argv[0], argv));
2441 /* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed.
2442 * format must have %s%s, buf must be > 16
2444 static int get_mdtname(const char *name, char *format, char *buf)
2446 char suffix[]="-MDT0000";
2447 int len = strlen(name);
2450 if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) {
2453 /* Not enough room to add suffix */
2454 llapi_err(LLAPI_MSG_ERROR, "MDT name too long |%s|\n",
2460 return sprintf(buf, format, name, suffix);
2464 /* Return a file descriptor to a readable changelog */
2465 int llapi_changelog_open(const char *device, long long startrec)
2471 if (device[0] == '/')
2472 rc = get_root_path(WANT_FSNAME, mdtname, NULL, (char *)device);
2474 strncpy(mdtname, device, sizeof(mdtname));
2476 /* Use either the mdd changelog (preferred) or a client mdc changelog */
2477 if (get_mdtname(mdtname,
2478 "/proc/fs/lustre/md[cd]/%s%s{,-mdc-*}/changelog",
2481 rc = first_match(path, path);
2485 if ((fd = open(path, O_RDONLY)) < 0) {
2486 llapi_err(LLAPI_MSG_ERROR, "error: can't open |%s|\n", path);
2490 rc = lseek(fd, (off_t)startrec, SEEK_SET);
2492 llapi_err(LLAPI_MSG_ERROR, "can't seek rc=%d\n", rc);
2499 int llapi_changelog_clear(const char *mdtname, const char *idstr,
2502 struct ioc_changelog_clear data;
2505 int id, fd, index, rc;
2508 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2509 "can't purge negative records\n");
2513 id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10);
2514 if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX,
2515 strlen(CHANGELOG_USER_PREFIX)) != 0)) {
2516 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2517 "expecting id of the form '"CHANGELOG_USER_PREFIX
2518 "<num>'; got '%s'\n", idstr);
2522 /* Take path, fsname, or MDTNAME. Assume MDT0000 in the former cases */
2523 if (mdtname[0] == '/') {
2525 fd = open(mdtname, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
2526 rc = fd < 0 ? -errno : 0;
2528 if (get_mdtname(mdtname, "%s%s", fsname) < 0)
2530 ptr = fsname + strlen(fsname) - 8;
2532 index = strtol(ptr + 4, NULL, 10);
2533 rc = get_root_path(WANT_FD, fsname, &fd, NULL);
2536 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2537 "Can't open %s: %d\n", mdtname, rc);
2541 data.icc_mdtindex = index;
2543 data.icc_recno = endrec;
2544 rc = ioctl(fd, OBD_IOC_CHANGELOG_CLEAR, &data);
2546 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2552 int llapi_fid2path(const char *device, const char *fidstr, char *buf,
2553 int buflen, long long *recno, int *linkno)
2555 char path[PATH_MAX];
2557 struct getinfo_fid2path *gf;
2560 while (*fidstr == '[')
2563 sscanf(fidstr, SFID, RFID(&fid));
2564 if (!fid_is_sane(&fid)) {
2565 llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
2566 "bad FID format [%s], should be "DFID"\n",
2567 fidstr, (__u64)1, 2, 0);
2571 /* Take path or fsname */
2572 if (device[0] == '/') {
2573 strcpy(path, device);
2575 rc = get_root_path(WANT_PATH, (char *)device, NULL, path);
2579 sprintf(path, "%s/%s/fid/%s", path, dot_lustre_name, fidstr);
2580 fd = open(path, O_RDONLY | O_NONBLOCK);
2584 gf = malloc(sizeof(*gf) + buflen);
2586 gf->gf_recno = *recno;
2587 gf->gf_linkno = *linkno;
2588 gf->gf_pathlen = buflen;
2589 rc = ioctl(fd, OBD_IOC_FID2PATH, gf);
2591 llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
2593 memcpy(buf, gf->gf_path, gf->gf_pathlen);
2594 *recno = gf->gf_recno;
2595 *linkno = gf->gf_linkno;
2603 int llapi_path2fid(const char *path, lustre_fid *fid)
2607 fd = open(path, O_RDONLY);
2611 rc = ioctl(fd, LL_IOC_PATH2FID, fid);