1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Peter J. Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
7 * Author: Robert Read <rread@clusterfs.com>
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/syscall.h>
43 #ifdef HAVE_ASM_TYPES_H
44 #include <asm/types.h>
46 #ifdef HAVE_LINUX_UNISTD_H
47 #include <linux/unistd.h>
52 #include <liblustre.h>
53 #include <lnet/lnetctl.h>
55 #include <lustre_lib.h>
56 #include <lustre/liblustreapi.h>
59 static unsigned llapi_dir_filetype_table[] = {
68 #if defined(DT_DOOR) && defined(S_IFDOOR)
73 #if defined(DT_DOOR) && defined(S_IFDOOR)
74 static const int DT_MAX = DT_DOOR;
76 static const int DT_MAX = DT_SOCK;
79 static unsigned llapi_filetype_dir_table[] = {
88 #if defined(DT_DOOR) && defined(S_IFDOOR)
93 #if defined(DT_DOOR) && defined(S_IFDOOR)
94 static const int S_IFMAX = DT_DOOR;
96 static const int S_IFMAX = DT_SOCK;
99 /* liblustreapi message level */
100 static int llapi_msg_level = LLAPI_MSG_MAX;
102 void llapi_msg_set_level(int level)
104 /* ensure level is in the good range */
105 if (level < LLAPI_MSG_OFF)
106 llapi_msg_level = LLAPI_MSG_OFF;
107 else if (level > LLAPI_MSG_MAX)
108 llapi_msg_level = LLAPI_MSG_MAX;
110 llapi_msg_level = level;
113 void llapi_err(int level, char *fmt, ...)
116 int tmp_errno = abs(errno);
118 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
122 vfprintf(stderr, fmt, args);
125 if (level & LLAPI_MSG_NO_ERRNO)
126 fprintf(stderr, "\n");
128 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
131 #define llapi_err_noerrno(level, fmt, a...) \
132 llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
134 void llapi_printf(int level, char *fmt, ...)
138 if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
142 vfprintf(stdout, fmt, args);
146 /* size_units is unchanged if no specifier used */
147 int parse_size(char *optarg, unsigned long long *size,
148 unsigned long long *size_units, int bytes_spec)
152 *size = strtoull(optarg, &end, 0);
155 if ((*end == 'b') && *(end+1) == '\0' &&
156 (*size & (~0ULL << (64 - 9))) == 0 &&
159 *size_units = 1 << 9;
160 } else if ((*end == 'b') && *(end+1) == '\0' &&
163 } else if ((*end == 'k' || *end == 'K') &&
164 *(end+1) == '\0' && (*size &
165 (~0ULL << (64 - 10))) == 0) {
167 *size_units = 1 << 10;
168 } else if ((*end == 'm' || *end == 'M') &&
169 *(end+1) == '\0' && (*size &
170 (~0ULL << (64 - 20))) == 0) {
172 *size_units = 1 << 20;
173 } else if ((*end == 'g' || *end == 'G') &&
174 *(end+1) == '\0' && (*size &
175 (~0ULL << (64 - 30))) == 0) {
177 *size_units = 1 << 30;
178 } else if ((*end == 't' || *end == 'T') &&
179 *(end+1) == '\0' && (*size &
180 (~0ULL << (64 - 40))) == 0) {
182 *size_units = 1ULL << 40;
183 } else if ((*end == 'p' || *end == 'P') &&
184 *(end+1) == '\0' && (*size &
185 (~0ULL << (64 - 50))) == 0) {
187 *size_units = 1ULL << 50;
188 } else if ((*end == 'e' || *end == 'E') &&
189 *(end+1) == '\0' && (*size &
190 (~0ULL << (64 - 60))) == 0) {
192 *size_units = 1ULL << 60;
201 int llapi_file_open(const char *name, int flags, int mode,
202 unsigned long stripe_size, int stripe_offset,
203 int stripe_count, int stripe_pattern)
205 struct lov_user_md lum = { 0 };
210 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
211 if (fd < 0 && errno == EISDIR) {
212 fd = open(name, O_DIRECTORY | O_RDONLY);
218 llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
222 /* 64 KB is the largest common page size I'm aware of (on ia64), but
223 * check the local page size just in case. */
224 page_size = LOV_MIN_STRIPE_SIZE;
225 if (getpagesize() > page_size) {
226 page_size = getpagesize();
227 llapi_err_noerrno(LLAPI_MSG_WARN,
228 "warning: your page size (%u) is "
229 "larger than expected (%u)", page_size,
230 LOV_MIN_STRIPE_SIZE);
232 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
233 errno = rc = -EINVAL;
234 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
235 "must be an even multiple of %d bytes",
236 stripe_size, page_size);
239 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
240 errno = rc = -EINVAL;
241 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d",
245 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
246 errno = rc = -EINVAL;
247 llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d",
251 if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
252 errno = rc = -EINVAL;
253 llapi_err(LLAPI_MSG_ERROR, "error: stripe_size %lu * "
254 "stripe_count %u exceeds 4GB", stripe_size,
259 /* Initialize IOCTL striping pattern structure */
260 lum.lmm_magic = LOV_USER_MAGIC;
261 lum.lmm_pattern = stripe_pattern;
262 lum.lmm_stripe_size = stripe_size;
263 lum.lmm_stripe_count = stripe_count;
264 lum.lmm_stripe_offset = stripe_offset;
266 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
267 char *errmsg = "stripe already set";
269 if (errno != EEXIST && errno != EALREADY)
270 errmsg = strerror(errno);
272 llapi_err_noerrno(LLAPI_MSG_ERROR,
273 "error on ioctl "LPX64" for '%s' (%d): %s",
274 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
285 int llapi_file_create(const char *name, unsigned long stripe_size,
286 int stripe_offset, int stripe_count, int stripe_pattern)
290 fd = llapi_file_open(name, O_CREAT | O_WRONLY, 0644, stripe_size,
291 stripe_offset, stripe_count, stripe_pattern);
299 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
300 void *data, struct dirent64 *de);
302 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
303 #define OBD_NOT_FOUND (-1)
305 static int common_param_init(struct find_param *param)
307 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
308 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
309 llapi_err(LLAPI_MSG_ERROR,
310 "error: allocation of %d bytes for ioctl",
311 sizeof(lstat_t) + param->lumlen);
315 param->got_uuids = 0;
316 param->obdindexes = NULL;
317 param->obdindex = OBD_NOT_FOUND;
321 static void find_param_fini(struct find_param *param)
323 if (param->obdindexes)
324 free(param->obdindexes);
330 int llapi_file_get_lov_fuuid(int fd, struct obd_uuid *lov_name)
332 int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
335 llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
340 int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
344 fd = open(path, O_RDONLY);
347 llapi_err(LLAPI_MSG_ERROR, "error opening %s\n", path);
351 rc = llapi_file_get_lov_fuuid(fd, lov_uuid);
359 * If uuidp is NULL, return the number of available obd uuids.
360 * If uuidp is non-NULL, then it will return the uuids of the obds. If
361 * there are more OSTs then allocated to uuidp, then an error is returned with
362 * the ost_count set to number of available obd uuids.
364 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
366 struct obd_uuid lov_name;
369 int rc = 0, index = 0;
371 /* Get the lov name */
372 rc = llapi_file_get_lov_fuuid(fd, &lov_name);
376 /* Now get the ost uuids from /proc */
377 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
379 fp = fopen(buf, "r");
382 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
386 while (fgets(buf, sizeof(buf), fp) != NULL) {
387 if (uuidp && (index < *ost_count)) {
388 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
396 if (uuidp && (index >= *ost_count))
403 /* Here, param->obduuid points to a single obduuid, the index of which is
404 * returned in param->obdindex */
405 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
407 struct obd_uuid lov_uuid;
408 char uuid[sizeof(struct obd_uuid)];
413 /* Get the lov name */
414 rc = llapi_file_get_lov_fuuid(dirfd(dir), &lov_uuid);
416 if (errno != ENOTTY) {
418 llapi_err(LLAPI_MSG_ERROR,
419 "error: can't get lov name: %s", dname);
426 param->got_uuids = 1;
428 /* Now get the ost uuids from /proc */
429 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
431 fp = fopen(buf, "r");
434 llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
438 if (!param->obduuid && !param->quiet && !param->obds_printed)
439 llapi_printf(LLAPI_MSG_NORMAL, "OBDS:\n");
441 while (fgets(buf, sizeof(buf), fp) != NULL) {
442 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
445 if (param->obduuid) {
446 if (strncmp((char *)param->obduuid->uuid, uuid,
447 sizeof(uuid)) == 0) {
448 param->obdindex = index;
451 } else if (!param->quiet && !param->obds_printed) {
452 /* Print everything */
453 llapi_printf(LLAPI_MSG_NORMAL, "%s", buf);
456 param->obds_printed = 1;
460 if (!param->quiet && param->obduuid &&
461 (param->obdindex == OBD_NOT_FOUND)) {
462 llapi_err_noerrno(LLAPI_MSG_ERROR,
463 "error: %s: unknown obduuid: %s",
464 __FUNCTION__, param->obduuid->uuid);
471 /* In this case, param->obduuid will be an array of obduuids and
472 * obd index for all these obduuids will be returned in
473 * param->obdindexes */
474 static int setup_obd_indexes(DIR *dir, struct find_param *param)
476 struct obd_uuid *uuids = NULL;
477 int obdcount = INIT_ALLOC_NUM_OSTS;
478 int ret, obd_valid = 0, obdnum, i;
480 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
481 sizeof(struct obd_uuid));
486 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
489 struct obd_uuid *uuids_temp;
491 if (ret == -EOVERFLOW) {
492 uuids_temp = realloc(uuids, obdcount *
493 sizeof(struct obd_uuid));
494 if (uuids_temp != NULL)
495 goto retry_get_uuids;
500 llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
504 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
505 if (param->obdindexes == NULL)
508 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
509 for (i = 0; i <= obdcount; i++) {
510 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
511 (char *)&uuids[i]) == 0) {
512 param->obdindexes[obdnum] = i;
518 param->obdindexes[obdnum] = OBD_NOT_FOUND;
522 param->obdindex = OBD_NOT_FOUND;
524 param->obdindex = obd_valid;
526 param->got_uuids = 1;
531 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
532 int obdindex, int quiet, int header, int body)
534 int i, obdstripe = 0;
536 if (obdindex != OBD_NOT_FOUND) {
537 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
538 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
539 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
545 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
549 /* if it's a directory */
551 if (obdstripe == 1) {
552 if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) {
553 llapi_printf(LLAPI_MSG_NORMAL, "(Default) ");
554 lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR;
556 llapi_printf(LLAPI_MSG_NORMAL,
557 "stripe_count: %d stripe_size: %u "
558 "stripe_offset: %d\n",
559 lum->lmm_stripe_count == (__u16)-1 ? -1 :
560 lum->lmm_stripe_count,
561 lum->lmm_stripe_size,
562 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
563 lum->lmm_stripe_offset);
568 if (header && (obdstripe == 1)) {
569 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
571 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
573 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
575 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
576 (int)lum->lmm_stripe_count);
577 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
578 lum->lmm_stripe_size);
579 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
584 if ((!quiet) && (obdstripe == 1))
585 llapi_printf(LLAPI_MSG_NORMAL,
586 "\tobdidx\t\t objid\t\tobjid\t\t group\n");
588 for (i = 0; i < lum->lmm_stripe_count; i++) {
589 int idx = lum->lmm_objects[i].l_ost_idx;
590 long long oid = lum->lmm_objects[i].l_object_id;
591 long long gr = lum->lmm_objects[i].l_object_gr;
592 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
593 llapi_printf(LLAPI_MSG_NORMAL,
594 "\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
596 obdindex == idx ? " *" : "");
598 llapi_printf(LLAPI_MSG_NORMAL, "\n");
602 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
603 int is_dir, int obdindex, int quiet,
604 int header, int body)
606 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
607 int i, obdstripe = 0;
609 if (obdindex != OBD_NOT_FOUND) {
610 for (i = 0; i < lumj->lmm_stripe_count; i++) {
611 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
612 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
618 llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
622 if (header && obdstripe == 1) {
623 llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n",
625 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n",
626 lumj->lmm_object_gr);
627 llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n",
628 lumj->lmm_object_id);
629 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n",
630 (int)lumj->lmm_stripe_count);
631 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n",
632 lumj->lmm_stripe_size);
633 llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n",
635 llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n",
636 lumj->lmm_extent_count);
640 unsigned long long start = -1, end = 0;
641 if (!quiet && obdstripe == 1)
642 llapi_printf(LLAPI_MSG_NORMAL,
643 "joined\tobdidx\t\t objid\t\tobjid\t\t group"
644 "\t\tstart\t\tend\n");
645 for (i = 0; i < lumj->lmm_stripe_count; i++) {
646 int idx = lumj->lmm_objects[i].l_ost_idx;
647 long long oid = lumj->lmm_objects[i].l_object_id;
648 long long gr = lumj->lmm_objects[i].l_object_gr;
649 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
650 llapi_printf(LLAPI_MSG_NORMAL,
651 "\t%6u\t%14llu\t%#13llx\t%14llu%s",
653 obdindex == idx ? " *" : "");
654 if (start != lumj->lmm_objects[i].l_extent_start ||
655 end != lumj->lmm_objects[i].l_extent_end) {
656 start = lumj->lmm_objects[i].l_extent_start;
657 llapi_printf(LLAPI_MSG_NORMAL, "\t%14llu", start);
658 end = lumj->lmm_objects[i].l_extent_end;
659 if (end == (unsigned long long)-1)
660 llapi_printf(LLAPI_MSG_NORMAL, "\t\tEOF\n");
662 llapi_printf(LLAPI_MSG_NORMAL, "\t\t%llu\n",
665 llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n");
668 llapi_printf(LLAPI_MSG_NORMAL, "\n");
672 void llapi_lov_dump_user_lmm(struct find_param *param,
673 char *path, int is_dir)
675 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
676 case LOV_USER_MAGIC_V1:
677 lov_dump_user_lmm_v1(¶m->lmd->lmd_lmm, path, is_dir,
678 param->obdindex, param->quiet,
680 (param->verbose || !param->obduuid));
682 case LOV_USER_MAGIC_JOIN:
683 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
684 param->obdindex, param->quiet,
686 (param->verbose || !param->obduuid));
689 llapi_printf(LLAPI_MSG_NORMAL,
690 "unknown lmm_magic: %#x (expecting %#x)\n",
691 *(__u32 *)¶m->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
696 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
702 fname = strrchr(path, '/');
704 /* It should be a file (or other non-directory) */
706 dname = (char *)malloc(2);
710 fname = (char *)path;
712 dname = (char *)malloc(fname - path + 1);
715 strncpy(dname, path, fname - path);
716 dname[fname - path] = '\0';
720 if ((fd = open(dname, O_RDONLY)) == -1) {
726 strcpy((char *)lum, fname);
727 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
730 if (close(fd) == -1 && rc == 0)
738 int llapi_file_lookup(int dirfd, const char *name)
740 struct obd_ioctl_data data = { 0 };
745 if (dirfd < 0 || name == NULL)
748 data.ioc_version = OBD_IOCTL_VERSION;
749 data.ioc_len = sizeof(data);
750 data.ioc_inlbuf1 = (char *)name;
751 data.ioc_inllen1 = strlen(name) + 1;
753 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
755 llapi_err(LLAPI_MSG_ERROR,
756 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
761 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
764 int llapi_mds_getfileinfo(char *path, DIR *parent,
765 struct lov_user_mds_data *lmd)
767 lstat_t *st = &lmd->lmd_st;
768 char *fname = strrchr(path, '/');
774 fname = (fname == NULL ? path : fname + 1);
775 /* retrieve needed file info */
776 strncpy((char *)lmd, fname, lov_mds_md_size(MAX_LOV_UUID_COUNT));
777 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
780 if (errno == ENOTTY) {
781 /* ioctl is not supported, it is not a lustre fs.
782 * Do the regular lstat(2) instead. */
783 ret = lstat_f(path, st);
785 llapi_err(LLAPI_MSG_ERROR,
786 "error: %s: lstat failed for %s",
790 } else if (errno == ENOENT) {
791 llapi_err(LLAPI_MSG_WARN,
792 "warning: %s: %s does not exist",
796 llapi_err(LLAPI_MSG_ERROR,
797 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
806 static DIR *opendir_parent(char *path)
812 fname = strrchr(path, '/');
818 parent = opendir(path);
823 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
824 semantic_func_t sem_init,
825 semantic_func_t sem_fini, void *data,
828 struct dirent64 *dent;
836 if (!d && errno != ENOTDIR) {
837 llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
840 } else if (!d && !parent) {
841 /* ENOTDIR. Open the parent dir. */
842 p = opendir_parent(path);
844 GOTO(out, ret = -EINVAL);
847 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
853 while ((dent = readdir64(d)) != NULL) {
854 ((struct find_param *)data)->have_fileinfo = 0;
856 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
860 if ((len + dent->d_reclen + 2) > size) {
861 llapi_err(LLAPI_MSG_ERROR,
862 "error: %s: string buffer is too small",
867 strcat(path, dent->d_name);
869 if (dent->d_type == DT_UNKNOWN) {
870 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
872 ret = llapi_mds_getfileinfo(path, d,
873 ((struct find_param *)data)->lmd);
875 ((struct find_param *)data)->have_fileinfo = 1;
876 dent->d_type = llapi_filetype_dir_table[st->st_mode &
883 switch (dent->d_type) {
885 llapi_err(LLAPI_MSG_ERROR,
886 "error: %s: '%s' is UNKNOWN type %d",
887 __FUNCTION__, dent->d_name, dent->d_type);
890 ret = llapi_semantic_traverse(path, size, d, sem_init,
891 sem_fini, data, dent);
898 ret = sem_init(path, d, NULL, data, dent);
902 if (sem_fini && ret == 0)
903 sem_fini(path, d, NULL, data, dent);
911 sem_fini(path, parent, d, data, de);
920 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
921 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
923 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
924 * The table below gives the answers for the specified parameters (value and
925 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
926 * --------------------------------------
927 * 1 | file > limit; sign > 0 | -1 / -1 |
928 * 2 | file = limit; sign > 0 | ? / 1 |
929 * 3 | file < limit; sign > 0 | ? / 1 |
930 * 4 | file > limit; sign = 0 | -1 / -1 |
931 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
932 * 6 | file < limit; sign = 0 | ? / -1 |
933 * 7 | file > limit; sign < 0 | 1 / 1 |
934 * 8 | file = limit; sign < 0 | ? / -1 |
935 * 9 | file < limit; sign < 0 | ? / -1 |
936 * --------------------------------------
937 * Note: 5th actually means that the value is within the interval
938 * (limit - margin, limit]. */
939 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
940 unsigned long long margin, int mds)
948 if (file <= limit && file + margin > limit)
950 if (file + margin <= limit)
964 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
965 * Return -1 or 1 if file timestamp does not or does match the given criteria
966 * correspondingly. Return 0 if the MDS time is being checked and there are
967 * attributes on OSTs and it is not yet clear if the timespamp matches.
969 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
970 * updated timestamps. */
971 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
976 /* Check if file is accepted. */
978 ret = find_value_cmp(st->st_atime, param->atime,
979 param->asign, 24 * 60 * 60, mds);
986 ret = find_value_cmp(st->st_mtime, param->mtime,
987 param->msign, 24 * 60 * 60, mds);
991 /* If the previous check matches, but this one is not yet clear,
992 * we should return 0 to do an RPC on OSTs. */
998 ret = find_value_cmp(st->st_ctime, param->ctime,
999 param->csign, 24 * 60 * 60, mds);
1003 /* If the previous check matches, but this one is not yet clear,
1004 * we should return 0 to do an RPC on OSTs. */
1012 static int cb_find_init(char *path, DIR *parent, DIR *dir,
1013 void *data, struct dirent64 *de)
1015 struct find_param *param = (struct find_param *)data;
1016 int decision = 1; /* 1 is accepted; -1 is rejected. */
1017 lstat_t *st = ¶m->lmd->lmd_st;
1019 int checked_type = 0;
1022 LASSERT(parent != NULL || dir != NULL);
1024 param->lmd->lmd_lmm.lmm_stripe_count = 0;
1026 /* If a regular expression is presented, make the initial decision */
1027 if (param->pattern != NULL) {
1028 char *fname = strrchr(path, '/');
1029 fname = (fname == NULL ? path : fname + 1);
1030 ret = fnmatch(param->pattern, fname, 0);
1031 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
1032 (ret == 0 && param->exclude_pattern))
1036 /* See if we can check the file type from the dirent. */
1037 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
1038 de->d_type <= DT_MAX) {
1040 if (llapi_dir_filetype_table[de->d_type] == param->type) {
1041 if (param->exclude_type)
1044 if (!param->exclude_type)
1050 /* If a time or OST should be checked, the decision is not taken yet. */
1051 if (param->atime || param->ctime || param->mtime || param->obduuid ||
1056 /* Request MDS for the stat info. */
1057 if (param->have_fileinfo == 0) {
1059 /* retrieve needed file info */
1060 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
1061 (void *)param->lmd);
1063 char *fname = strrchr(path, '/');
1064 fname = (fname == NULL ? path : fname + 1);
1066 /* retrieve needed file info */
1067 strncpy((char *)param->lmd, fname, param->lumlen);
1068 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1069 (void *)param->lmd);
1074 if (errno == ENOTTY) {
1075 /* ioctl is not supported, it is not a lustre fs.
1076 * Do the regular lstat(2) instead. */
1078 ret = lstat_f(path, st);
1080 llapi_err(LLAPI_MSG_ERROR,
1081 "error: %s: lstat failed for %s",
1082 __FUNCTION__, path);
1085 } else if (errno == ENOENT) {
1086 llapi_err(LLAPI_MSG_WARN,
1087 "warning: %s: %s does not exist",
1088 __FUNCTION__, path);
1091 llapi_err(LLAPI_MSG_ERROR, "error: %s: %s failed for %s",
1092 __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" :
1093 "IOC_MDC_GETFILEINFO", path);
1098 if (param->check_uid) {
1099 if (st->st_uid == param->uid) {
1100 if (param->exclude_uid)
1103 if (!param->exclude_uid)
1108 if (param->check_gid) {
1109 if (st->st_gid == param->gid) {
1110 if (param->exclude_gid)
1113 if (!param->exclude_gid)
1118 /* Check the time on mds. */
1122 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1123 param->lmd->lmd_lmm.lmm_stripe_count)
1125 decision = find_time_check(st, param, for_mds);
1130 if (param->type && !checked_type) {
1131 if ((st->st_mode & S_IFMT) == param->type) {
1132 if (param->exclude_type)
1135 if (!param->exclude_type)
1141 if (param->obduuid) {
1142 if (lustre_fs && param->got_uuids &&
1143 param->st_dev != st->st_dev) {
1144 /* A lustre/lustre mount point is crossed. */
1145 param->got_uuids = 0;
1146 param->obds_printed = 0;
1147 param->obdindex = OBD_NOT_FOUND;
1150 if (lustre_fs && !param->got_uuids) {
1151 ret = setup_obd_indexes(dir ? dir : parent, param);
1155 param->st_dev = st->st_dev;
1156 } else if (!lustre_fs && param->got_uuids) {
1157 /* A lustre/non-lustre mount point is crossed. */
1158 param->got_uuids = 0;
1159 param->obdindex = OBD_NOT_FOUND;
1163 /* If an OBD UUID is specified but no one matches, skip this file. */
1164 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1167 /* If a OST UUID is given, and some OST matches, check it here. */
1168 if (param->obdindex != OBD_NOT_FOUND) {
1169 if (!S_ISREG(st->st_mode))
1172 /* Only those files should be accepted, which have a
1173 * stripe on the specified OST. */
1174 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1179 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1180 for (j = 0; j < param->num_obds; j++) {
1181 if (param->obdindexes[j] ==
1182 param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
1187 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1194 /* If file still fits the request, ask osd for updated info.
1195 The regulat stat is almost of the same speed as some new
1196 'glimpse-size-ioctl'. */
1197 if (!decision && S_ISREG(st->st_mode) &&
1198 (param->lmd->lmd_lmm.lmm_stripe_count || param->size_check)) {
1200 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1201 (void *)param->lmd);
1202 } else if (parent) {
1203 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1204 (void *)param->lmd);
1208 if (errno == ENOENT) {
1209 llapi_err(LLAPI_MSG_ERROR,
1210 "warning: %s: %s does not exist",
1211 __FUNCTION__, path);
1214 llapi_err(LLAPI_MSG_ERROR,
1215 "%s: IOC_LOV_GETINFO on %s failed",
1216 __FUNCTION__, path);
1221 /* Check the time on osc. */
1222 decision = find_time_check(st, param, 0);
1227 if (param->size_check)
1228 decision = find_value_cmp(st->st_size, param->size,
1229 param->size_sign, param->size_units,
1232 if (decision != -1) {
1233 llapi_printf(LLAPI_MSG_NORMAL, "%s", path);
1235 llapi_printf(LLAPI_MSG_NORMAL, "%c", '\0');
1237 llapi_printf(LLAPI_MSG_NORMAL, "\n");
1241 /* Do not get down anymore? */
1242 if (param->depth == param->maxdepth)
1249 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1250 struct dirent64 *de)
1252 struct find_param *param = (struct find_param *)data;
1257 int llapi_find(char *path, struct find_param *param)
1260 int ret, len = strlen(path);
1262 if (len > PATH_MAX) {
1263 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1264 __FUNCTION__, path);
1268 buf = (char *)malloc(PATH_MAX + 1);
1272 ret = common_param_init(param);
1280 strncpy(buf, path, PATH_MAX + 1);
1281 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1282 cb_common_fini, param, NULL);
1284 find_param_fini(param);
1286 return ret < 0 ? ret : 0;
1289 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1290 struct dirent64 *de)
1292 struct find_param *param = (struct find_param *)data;
1295 LASSERT(parent != NULL || d != NULL);
1298 if (!param->got_uuids) {
1299 ret = setup_obd_uuid(d ? d : parent, path, param);
1305 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1306 (void *)¶m->lmd->lmd_lmm);
1307 } else if (parent) {
1308 char *fname = strrchr(path, '/');
1309 fname = (fname == NULL ? path : fname + 1);
1311 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1312 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1313 (void *)¶m->lmd->lmd_lmm);
1317 if (errno == ENODATA) {
1318 if (!param->obduuid && !param->quiet)
1319 llapi_printf(LLAPI_MSG_NORMAL,
1320 "%s has no stripe info\n", path);
1322 } else if (errno == ENOTTY) {
1323 llapi_err(LLAPI_MSG_ERROR,
1324 "%s: '%s' not on a Lustre fs?",
1325 __FUNCTION__, path);
1326 } else if (errno == ENOENT) {
1327 llapi_err(LLAPI_MSG_WARN,
1328 "warning: %s: %s does not exist",
1329 __FUNCTION__, path);
1332 llapi_err(LLAPI_MSG_ERROR,
1333 "error: %s: %s failed for %s",
1334 __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" :
1335 "IOC_MDC_GETFILESTRIPE", path);
1341 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1343 /* Do not get down anymore? */
1344 if (param->depth == param->maxdepth)
1351 int llapi_getstripe(char *path, struct find_param *param)
1354 int ret = 0, len = strlen(path);
1356 if (len > PATH_MAX) {
1357 llapi_err(LLAPI_MSG_ERROR,
1358 "%s: Path name '%s' is too long",
1359 __FUNCTION__, path);
1363 buf = (char *)malloc(PATH_MAX + 1);
1367 ret = common_param_init(param);
1375 strncpy(buf, path, PATH_MAX + 1);
1376 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1377 cb_common_fini, param, NULL);
1378 find_param_fini(param);
1380 return ret < 0 ? ret : 0;
1383 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1384 struct obd_statfs *stat_buf,
1385 struct obd_uuid *uuid_buf)
1388 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1390 struct obd_ioctl_data data = { 0 };
1393 data.ioc_inlbuf1 = (char *)&type;
1394 data.ioc_inllen1 = sizeof(__u32);
1395 data.ioc_inlbuf2 = (char *)&index;
1396 data.ioc_inllen2 = sizeof(__u32);
1397 data.ioc_pbuf1 = (char *)stat_buf;
1398 data.ioc_plen1 = sizeof(struct obd_statfs);
1399 data.ioc_pbuf2 = (char *)uuid_buf;
1400 data.ioc_plen2 = sizeof(struct obd_uuid);
1402 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1403 llapi_err(LLAPI_MSG_ERROR,
1404 "llapi_obd_statfs: error packing ioctl data");
1408 fd = open(path, O_RDONLY);
1409 if (errno == EISDIR)
1410 fd = open(path, O_DIRECTORY | O_RDONLY);
1413 rc = errno ? -errno : -EBADF;
1414 llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
1415 __FUNCTION__, path);
1418 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1420 rc = errno ? -errno : -EINVAL;
1426 #define MAX_STRING_SIZE 128
1427 #define DEVICES_LIST "/proc/fs/lustre/devices"
1429 int llapi_ping(char *obd_type, char *obd_name)
1431 char path[MAX_STRING_SIZE];
1435 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1436 obd_type, obd_name);
1438 fd = open(path, O_WRONLY);
1441 llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
1445 rc = write(fd, buf, 1);
1453 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1455 char buf[MAX_STRING_SIZE];
1456 FILE *fp = fopen(DEVICES_LIST, "r");
1461 llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
1465 while (fgets(buf, sizeof(buf), fp) != NULL) {
1466 char *obd_type_name = NULL;
1467 char *obd_name = NULL;
1468 char *obd_uuid = NULL;
1469 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1470 char *bufl = rawbuf;
1472 struct obd_ioctl_data datal = { 0, };
1473 struct obd_statfs osfs_buffer;
1475 while(bufp[0] == ' ')
1478 for(i = 0; i < 3; i++) {
1479 obd_type_name = strsep(&bufp, " ");
1481 obd_name = strsep(&bufp, " ");
1482 obd_uuid = strsep(&bufp, " ");
1484 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1486 memset(bufl, 0, sizeof(rawbuf));
1487 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1488 datal.ioc_plen1 = sizeof(osfs_buffer);
1490 for (i = 0; i < type_num; i++) {
1491 if (strcmp(obd_type_name, obd_type[i]) != 0)
1494 cb(obd_type_name, obd_name, obd_uuid, args);
1501 static void do_target_check(char *obd_type_name, char *obd_name,
1502 char *obd_uuid, void *args)
1506 rc = llapi_ping(obd_type_name, obd_name);
1508 llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
1510 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
1514 int llapi_target_check(int type_num, char **obd_type, char *dir)
1516 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1519 #undef MAX_STRING_SIZE
1521 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1523 char raw[OBD_MAX_IOCTL_BUFFER];
1524 char out[LLOG_CHUNK_SIZE];
1526 struct obd_ioctl_data data = { 0 };
1531 sprintf(key, "%s", keyword);
1532 memset(raw, 0, sizeof(raw));
1533 memset(out, 0, sizeof(out));
1534 data.ioc_inlbuf1 = key;
1535 data.ioc_inllen1 = strlen(key) + 1;
1537 data.ioc_inlbuf2 = node_name;
1538 data.ioc_inllen2 = strlen(node_name) + 1;
1540 data.ioc_pbuf1 = out;
1541 data.ioc_plen1 = sizeof(out);
1542 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1546 root = opendir(dir);
1549 llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
1553 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1555 llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
1557 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
1563 /* Is this a lustre fs? */
1564 int llapi_is_lustre_mnttype(const char *type)
1566 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1569 /* Is this a lustre client fs? */
1570 int llapi_is_lustre_mnt(struct mntent *mnt)
1572 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1573 strstr(mnt->mnt_fsname, ":/") != NULL);
1576 int llapi_quotacheck(char *mnt, int check_type)
1581 root = opendir(mnt);
1583 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1587 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1593 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1599 root = opendir(mnt);
1601 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1606 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1610 if (poll_intvl < 30)
1618 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1623 root = opendir(mnt);
1625 llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
1629 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1635 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
1636 struct dirent64 *de)
1638 struct find_param *param = (struct find_param *)data;
1642 LASSERT(parent != NULL || d != NULL);
1645 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1646 (void *)param->lmd);
1647 } else if (parent) {
1648 char *fname = strrchr(path, '/');
1649 fname = (fname == NULL ? path : fname + 1);
1651 strncpy((char *)param->lmd, fname, param->lumlen);
1652 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1653 (void *)param->lmd);
1659 if (errno == ENODATA) {
1660 if (!param->obduuid && !param->quiet)
1661 llapi_err(LLAPI_MSG_ERROR,
1662 "%s has no stripe info", path);
1664 } else if (errno == ENOENT) {
1665 llapi_err(LLAPI_MSG_ERROR,
1666 "warning: %s: %s does not exist",
1667 __FUNCTION__, path);
1669 } else if (errno != EISDIR) {
1671 llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
1672 d ? "LL_IOC_MDC_GETINFO" :
1673 "IOC_MDC_GETFILEINFO", path);
1678 st = ¶m->lmd->lmd_st;
1680 /* libc chown() will do extra check, and if the real owner is
1681 * the same as the ones to set, it won't fall into kernel, so
1682 * invoke syscall directly. */
1683 rc = syscall(SYS_chown, path, -1, -1);
1685 llapi_err(LLAPI_MSG_ERROR, "error: chown %s", path);
1687 rc = chmod(path, st->st_mode);
1689 llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
1695 int llapi_quotachown(char *path, int flag)
1697 struct find_param param;
1699 int ret = 0, len = strlen(path);
1701 if (len > PATH_MAX) {
1702 llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long",
1703 __FUNCTION__, path);
1707 buf = (char *)malloc(PATH_MAX + 1);
1711 memset(¶m, 0, sizeof(param));
1712 param.recursive = 1;
1716 ret = common_param_init(¶m);
1720 strncpy(buf, path, PATH_MAX + 1);
1721 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
1722 NULL, ¶m, NULL);
1724 find_param_fini(¶m);