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>
57 #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 static void err_msg(char *fmt, ...)
102 int tmp_errno = abs(errno);
105 vfprintf(stderr, fmt, args);
107 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
110 int parse_size(char *optarg, unsigned long long *size,
111 unsigned long long *size_units)
115 *size = strtoul(optarg, &end, 0);
118 if ((*end == 'b') && *(end+1) == '\0' &&
119 (*size & (~0ULL << (64 - 9))) == 0) {
121 *size_units = 1 << 9;
122 } else if ((*end == 'k' || *end == 'K') &&
123 *(end+1) == '\0' && (*size &
124 (~0ULL << (64 - 10))) == 0) {
126 *size_units = 1 << 10;
127 } else if ((*end == 'm' || *end == 'M') &&
128 *(end+1) == '\0' && (*size &
129 (~0ULL << (64 - 20))) == 0) {
131 *size_units = 1 << 20;
132 } else if ((*end == 'g' || *end == 'G') &&
133 *(end+1) == '\0' && (*size &
134 (~0ULL << (64 - 30))) == 0) {
136 *size_units = 1 << 30;
137 } else if ((*end == 't' || *end == 'T') &&
138 *(end+1) == '\0' && (*size &
139 (~0ULL << (64 - 40))) == 0) {
141 *size_units = 1ULL << 40;
142 } else if ((*end == 'p' || *end == 'P') &&
143 *(end+1) == '\0' && (*size &
144 (~0ULL << (64 - 50))) == 0) {
146 *size_units = 1ULL << 50;
147 } else if ((*end == 'e' || *end == 'E') &&
148 *(end+1) == '\0' && (*size &
149 (~0ULL << (64 - 60))) == 0) {
151 *size_units = 1ULL << 60;
160 int llapi_file_open(const char *name, int flags, int mode,
161 unsigned long stripe_size, int stripe_offset,
162 int stripe_count, int stripe_pattern)
164 struct lov_user_md lum = { 0 };
169 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
170 if (fd < 0 && errno == EISDIR) {
171 fd = open(name, O_DIRECTORY | O_RDONLY);
177 err_msg("unable to open '%s'", name);
181 /* 64 KB is the largest common page size I'm aware of (on ia64), but
182 * check the local page size just in case. */
183 page_size = LOV_MIN_STRIPE_SIZE;
184 if (getpagesize() > page_size) {
185 page_size = getpagesize();
186 fprintf(stderr, "warning: your page size (%u) is larger than "
187 "expected (%u).\n", page_size, LOV_MIN_STRIPE_SIZE);
189 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
190 errno = rc = -EINVAL;
191 err_msg("error: bad stripe_size %lu, must be an even "
192 "multiple of %d bytes", stripe_size, page_size);
195 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
196 errno = rc = -EINVAL;
197 err_msg("error: bad stripe offset %d", stripe_offset);
200 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
201 errno = rc = -EINVAL;
202 err_msg("error: bad stripe count %d", stripe_count);
205 if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
206 errno = rc = -EINVAL;
207 err_msg("error: stripe_size %lu * stripe_count %u "
208 "exceeds 4GB", stripe_size, stripe_count);
212 /* Initialize IOCTL striping pattern structure */
213 lum.lmm_magic = LOV_USER_MAGIC;
214 lum.lmm_pattern = stripe_pattern;
215 lum.lmm_stripe_size = stripe_size;
216 lum.lmm_stripe_count = stripe_count;
217 lum.lmm_stripe_offset = stripe_offset;
219 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
220 char *errmsg = "stripe already set";
222 if (errno != EEXIST && errno != EALREADY)
223 errmsg = strerror(errno);
225 fprintf(stderr, "error on ioctl "LPX64" for '%s' (%d): %s\n",
226 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
237 int llapi_file_create(const char *name, unsigned long stripe_size,
238 int stripe_offset, int stripe_count, int stripe_pattern)
242 fd = llapi_file_open(name, O_CREAT | O_WRONLY, 0644, stripe_size,
243 stripe_offset, stripe_count, stripe_pattern);
251 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
252 void *data, struct dirent64 *de);
254 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
255 #define OBD_NOT_FOUND (-1)
257 static int common_param_init(struct find_param *param)
259 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
260 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
261 err_msg("error: allocation of %d bytes for ioctl",
262 sizeof(lstat_t) + param->lumlen);
266 param->got_uuids = 0;
267 param->obdindexes = NULL;
268 param->obdindex = OBD_NOT_FOUND;
272 static void find_param_fini(struct find_param *param)
274 if (param->obdindexes)
275 free(param->obdindexes);
282 * If uuidp is NULL, return the number of available obd uuids.
283 * If uuidp is non-NULL, then it will return the uuids of the obds. If
284 * there are more OSTs then allocated to uuidp, then an error is returned with
285 * the ost_count set to number of available obd uuids.
287 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
289 char lov_name[sizeof(struct obd_uuid)];
292 int rc = 0, index = 0;
294 /* Get the lov name */
295 rc = ioctl(fd, OBD_IOC_GETNAME, (void *) lov_name);
298 err_msg("error: can't get lov name.");
302 /* Now get the ost uuids from /proc */
303 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
305 fp = fopen(buf, "r");
308 err_msg("error: opening '%s'", buf);
312 while (fgets(buf, sizeof(buf), fp) != NULL) {
313 if (uuidp && (index < *ost_count)) {
314 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) <2)
322 if (uuidp && (index >= *ost_count))
329 /* Here, param->obduuid points to a single obduuid, the index of which is
330 * returned in param->obdindex */
331 static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
333 char uuid[sizeof(struct obd_uuid)];
338 /* Get the lov name */
339 rc = ioctl(dirfd(dir), OBD_IOC_GETNAME, (void *)uuid);
341 if (errno != ENOTTY) {
343 err_msg("error: can't get lov name: %s", dname);
350 param->got_uuids = 1;
352 /* Now get the ost uuids from /proc */
353 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
355 fp = fopen(buf, "r");
358 err_msg("error: opening '%s'", buf);
362 if (!param->obduuid && !param->quiet && !param->obds_printed)
365 while (fgets(buf, sizeof(buf), fp) != NULL) {
366 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
369 if (param->obduuid) {
370 if (strncmp(param->obduuid->uuid, uuid,
371 sizeof(uuid)) == 0) {
372 param->obdindex = index;
375 } else if (!param->quiet && !param->obds_printed) {
376 /* Print everything */
380 param->obds_printed = 1;
384 if (!param->quiet && param->obduuid &&
385 (param->obdindex == OBD_NOT_FOUND)) {
386 fprintf(stderr, "error: %s: unknown obduuid: %s\n",
387 __FUNCTION__, param->obduuid->uuid);
394 /* In this case, param->obduuid will be an array of obduuids and
395 * obd index for all these obduuids will be returned in
396 * param->obdindexes */
397 static int setup_obd_indexes(DIR *dir, struct find_param *param)
399 struct obd_uuid *uuids = NULL;
400 int obdcount = INIT_ALLOC_NUM_OSTS;
401 int ret, obd_valid = 0, obdnum, i;
403 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
404 sizeof(struct obd_uuid));
409 ret = llapi_lov_get_uuids(dirfd(dir), uuids,
412 struct obd_uuid *uuids_temp;
414 if (ret == -EOVERFLOW) {
415 uuids_temp = realloc(uuids, obdcount *
416 sizeof(struct obd_uuid));
417 if (uuids_temp != NULL)
418 goto retry_get_uuids;
423 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
427 param->obdindexes = malloc(param->num_obds * sizeof(param->obdindex));
428 if (param->obdindexes == NULL)
431 for (obdnum = 0; obdnum < param->num_obds; obdnum++) {
432 for (i = 0; i <= obdcount; i++) {
433 if (strcmp((char *)¶m->obduuid[obdnum].uuid,
434 (char *)&uuids[i]) == 0) {
435 param->obdindexes[obdnum] = i;
441 param->obdindexes[obdnum] = OBD_NOT_FOUND;
445 param->obdindex = OBD_NOT_FOUND;
447 param->obdindex = obd_valid;
449 param->got_uuids = 1;
454 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
455 int obdindex, int quiet, int header, int body)
457 int i, obdstripe = 0;
459 if (obdindex != OBD_NOT_FOUND) {
460 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
461 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
462 printf("%s\n", path);
468 printf("%s\n", path);
472 /* if it's a directory */
474 if (obdstripe == 1) {
475 printf("default stripe_count: %d stripe_size: %u "
476 "stripe_offset: %d\n",
477 lum->lmm_stripe_count == (__u16)-1 ? -1 :
478 lum->lmm_stripe_count,
479 lum->lmm_stripe_size,
480 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
481 lum->lmm_stripe_offset);
486 if (header && (obdstripe == 1)) {
487 printf("lmm_magic: 0x%08X\n", lum->lmm_magic);
488 printf("lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
489 printf("lmm_object_id: "LPX64"\n", lum->lmm_object_id);
490 printf("lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
491 printf("lmm_stripe_size: %u\n", lum->lmm_stripe_size);
492 printf("lmm_stripe_pattern: %x\n", lum->lmm_pattern);
496 if ((!quiet) && (obdstripe == 1))
497 printf("\tobdidx\t\t objid\t\tobjid\t\t group\n");
499 for (i = 0; i < lum->lmm_stripe_count; i++) {
500 int idx = lum->lmm_objects[i].l_ost_idx;
501 long long oid = lum->lmm_objects[i].l_object_id;
502 long long gr = lum->lmm_objects[i].l_object_gr;
503 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
504 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
506 obdindex == idx ? " *" : "");
512 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
513 int is_dir, int obdindex, int quiet,
514 int header, int body)
516 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
517 int i, obdstripe = 0;
519 if (obdindex != OBD_NOT_FOUND) {
520 for (i = 0; i < lumj->lmm_stripe_count; i++) {
521 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
522 printf("%s\n", path);
528 printf("%s\n", path);
532 if (header && obdstripe == 1) {
533 printf("lmm_magic: 0x%08X\n", lumj->lmm_magic);
534 printf("lmm_object_gr: "LPX64"\n", lumj->lmm_object_gr);
535 printf("lmm_object_id: "LPX64"\n", lumj->lmm_object_id);
536 printf("lmm_stripe_count: %u\n", (int)lumj->lmm_stripe_count);
537 printf("lmm_stripe_size: %u\n", lumj->lmm_stripe_size);
538 printf("lmm_stripe_pattern: %x\n", lumj->lmm_pattern);
539 printf("lmm_extent_count: %x\n", lumj->lmm_extent_count);
543 unsigned long long start = -1, end = 0;
544 if (!quiet && obdstripe == 1)
545 printf("joined\tobdidx\t\t objid\t\tobjid\t\t group"
546 "\t\tstart\t\tend\n");
547 for (i = 0; i < lumj->lmm_stripe_count; i++) {
548 int idx = lumj->lmm_objects[i].l_ost_idx;
549 long long oid = lumj->lmm_objects[i].l_object_id;
550 long long gr = lumj->lmm_objects[i].l_object_gr;
551 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
552 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s",
554 obdindex == idx ? " *" : "");
555 if (start != lumj->lmm_objects[i].l_extent_start ||
556 end != lumj->lmm_objects[i].l_extent_end) {
557 start = lumj->lmm_objects[i].l_extent_start;
558 printf("\t%14llu", start);
559 end = lumj->lmm_objects[i].l_extent_end;
560 if (end == (unsigned long long)-1)
563 printf("\t\t%llu\n", end);
565 printf("\t\t\t\t\n");
572 void llapi_lov_dump_user_lmm(struct find_param *param,
573 char *path, int is_dir)
575 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
576 case LOV_USER_MAGIC_V1:
577 lov_dump_user_lmm_v1(¶m->lmd->lmd_lmm, path, is_dir,
578 param->obdindex, param->quiet,
580 (param->verbose || !param->obduuid));
582 case LOV_USER_MAGIC_JOIN:
583 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
584 param->obdindex, param->quiet,
586 (param->verbose || !param->obduuid));
589 printf("unknown lmm_magic: %#x (expecting %#x)\n",
590 *(__u32 *)¶m->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
595 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
601 fname = strrchr(path, '/');
603 /* It should be a file (or other non-directory) */
605 dname = (char *)malloc(2);
609 fname = (char *)path;
611 dname = (char *)malloc(fname - path + 1);
614 strncpy(dname, path, fname - path);
615 dname[fname - path] = '\0';
619 if ((fd = open(dname, O_RDONLY)) == -1) {
625 strcpy((char *)lum, fname);
626 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
629 if (close(fd) == -1 && rc == 0)
637 int llapi_file_lookup(int dirfd, const char *name)
639 struct obd_ioctl_data data = { 0 };
644 if (dirfd < 0 || name == NULL)
647 data.ioc_version = OBD_IOCTL_VERSION;
648 data.ioc_len = sizeof(data);
649 data.ioc_inlbuf1 = (char *)name;
650 data.ioc_inllen1 = strlen(name) + 1;
652 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
655 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d\n",
660 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
663 int llapi_mds_getfileinfo(char *path, DIR *parent,
664 struct lov_user_mds_data *lmd)
666 lstat_t *st = &lmd->lmd_st;
667 char *fname = strrchr(path, '/');
673 fname = (fname == NULL ? path : fname + 1);
674 /* retrieve needed file info */
675 strncpy((char *)lmd, fname, lov_mds_md_size(MAX_LOV_UUID_COUNT));
676 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd);
679 if (errno == ENOTTY) {
680 /* ioctl is not supported, it is not a lustre fs.
681 * Do the regular lstat(2) instead. */
682 ret = lstat_f(path, st);
684 err_msg("error: %s: lstat failed for %s",
688 } else if (errno == ENOENT) {
689 err_msg("warning: %s: %s does not exist", __FUNCTION__,
693 err_msg("error: %s: IOC_MDC_GETFILEINFO failed for %s",
702 static DIR *opendir_parent(char *path)
708 fname = strrchr(path, '/');
714 parent = opendir(path);
719 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
720 semantic_func_t sem_init,
721 semantic_func_t sem_fini, void *data,
724 struct dirent64 *dent;
732 if (!d && errno != ENOTDIR) {
733 fprintf(stderr, "%s: Failed to open '%s': %s.",
734 __FUNCTION__, path, strerror(errno));
736 } else if (!d && !parent) {
737 /* ENOTDIR. Open the parent dir. */
738 p = opendir_parent(path);
740 GOTO(out, ret = -EINVAL);
743 if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
749 while ((dent = readdir64(d)) != NULL) {
750 ((struct find_param *)data)->have_fileinfo = 0;
752 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
756 if ((len + dent->d_reclen + 2) > size) {
758 "error: %s: string buffer is too small\n",
763 strcat(path, dent->d_name);
765 if (dent->d_type == DT_UNKNOWN) {
766 lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
768 ret = llapi_mds_getfileinfo(path, d,
769 ((struct find_param *)data)->lmd);
771 ((struct find_param *)data)->have_fileinfo = 1;
772 dent->d_type = llapi_filetype_dir_table[st->st_mode &
779 switch (dent->d_type) {
781 fprintf(stderr, "error: %s: '%s' is UNKNOWN type %d",
782 __FUNCTION__, dent->d_name, dent->d_type);
785 ret = llapi_semantic_traverse(path, size, d, sem_init,
786 sem_fini, data, dent);
793 ret = sem_init(path, d, NULL, data, dent);
797 if (sem_fini && ret == 0)
798 sem_fini(path, d, NULL, data, dent);
806 sem_fini(path, parent, d, data, de);
815 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
816 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
818 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
819 * The table bolow gives the answers for the specified parameters (value and
820 * sign), 1st column is the answer for the MDS value, the 2nd is for the OST:
821 * --------------------------------------
822 * 1 | file > limit; sign > 0 | -1 / -1 |
823 * 2 | file = limit; sign > 0 | ? / 1 |
824 * 3 | file < limit; sign > 0 | ? / 1 |
825 * 4 | file > limit; sign = 0 | -1 / -1 |
826 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
827 * 6 | file < limit; sign = 0 | ? / -1 |
828 * 7 | file > limit; sign < 0 | 1 / 1 |
829 * 8 | file = limit; sign < 0 | ? / -1 |
830 * 9 | file < limit; sign < 0 | ? / -1 |
831 * --------------------------------------
832 * Note: 5th actually means that the value is within the interval
833 * (limit - margin, limit]. */
834 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
835 unsigned long long margin, int mds)
843 if (file <= limit && file + margin > limit)
845 if (file + margin <= limit)
859 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
860 * Return -1 or 1 if file timestamp does not or does match the given criteria
861 * correspondingly. Return 0 if the MDS time is being checked and there are
862 * attributes on OSTs and it is not yet clear if the timespamp matches.
864 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
865 * updated timestamps. */
866 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
871 /* Check if file is accepted. */
873 ret = find_value_cmp(st->st_atime, param->atime,
874 param->asign, 24 * 60 * 60, mds);
881 ret = find_value_cmp(st->st_mtime, param->mtime,
882 param->msign, 24 * 60 * 60, mds);
886 /* If the previous check matches, but this one is not yet clear,
887 * we should return 0 to do an RPC on OSTs. */
893 ret = find_value_cmp(st->st_ctime, param->ctime,
894 param->csign, 24 * 60 * 60, mds);
898 /* If the previous check matches, but this one is not yet clear,
899 * we should return 0 to do an RPC on OSTs. */
907 static int cb_find_init(char *path, DIR *parent, DIR *dir,
908 void *data, struct dirent64 *de)
910 struct find_param *param = (struct find_param *)data;
911 int decision = 1; /* 1 is accepted; -1 is rejected. */
912 lstat_t *st = ¶m->lmd->lmd_st;
914 int checked_type = 0;
917 LASSERT(parent != NULL || dir != NULL);
919 param->lmd->lmd_lmm.lmm_stripe_count = 0;
921 /* If a regular expression is presented, make the initial decision */
922 if (param->pattern != NULL) {
923 char *fname = strrchr(path, '/');
924 fname = (fname == NULL ? path : fname + 1);
925 ret = fnmatch(param->pattern, fname, 0);
926 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
927 (ret == 0 && param->exclude_pattern))
931 /* See if we can check the file type from the dirent. */
932 if (param->type && de != NULL && de->d_type != DT_UNKNOWN &&
933 de->d_type < DT_MAX) {
935 if (llapi_dir_filetype_table[de->d_type] == param->type) {
936 if (param->exclude_type)
939 if (!param->exclude_type)
945 /* If a time or OST should be checked, the decision is not taken yet. */
946 if (param->atime || param->ctime || param->mtime || param->obduuid ||
951 /* Request MDS for the stat info. */
952 if (param->have_fileinfo == 0) {
954 /* retrieve needed file info */
955 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
958 char *fname = strrchr(path, '/');
959 fname = (fname == NULL ? path : fname + 1);
961 /* retrieve needed file info */
962 strncpy((char *)param->lmd, fname, param->lumlen);
963 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
969 if (errno == ENOTTY) {
970 /* ioctl is not supported, it is not a lustre fs.
971 * Do the regular lstat(2) instead. */
973 ret = lstat_f(path, st);
975 err_msg("error: %s: lstat failed for %s",
979 } else if (errno == ENOENT) {
980 err_msg("warning: %s: %s does not exist",
984 err_msg("error: %s: %s failed for %s", __FUNCTION__,
985 dir ? "LL_IOC_MDC_GETINFO" :
986 "IOC_MDC_GETFILEINFO", path);
991 if (param->type && !checked_type) {
992 if ((st->st_mode & S_IFMT) == param->type) {
993 if (param->exclude_type)
996 if (!param->exclude_type)
1002 if (param->obduuid) {
1003 if (lustre_fs && param->got_uuids &&
1004 param->st_dev != st->st_dev) {
1005 /* A lustre/lustre mount point is crossed. */
1006 param->got_uuids = 0;
1007 param->obds_printed = 0;
1008 param->obdindex = OBD_NOT_FOUND;
1011 if (lustre_fs && !param->got_uuids) {
1012 ret = setup_obd_indexes(dir ? dir : parent, param);
1016 param->st_dev = st->st_dev;
1017 } else if (!lustre_fs && param->got_uuids) {
1018 /* A lustre/non-lustre mount point is crossed. */
1019 param->got_uuids = 0;
1020 param->obdindex = OBD_NOT_FOUND;
1024 /* If an OBD UUID is specified but no one matches, skip this file. */
1025 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
1028 /* If a OST UUID is given, and some OST matches, check it here. */
1029 if (param->obdindex != OBD_NOT_FOUND) {
1030 if (!S_ISREG(st->st_mode))
1033 /* Only those files should be accepted, which have a
1034 * stripe on the specified OST. */
1035 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
1040 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
1041 for (j = 0; j < param->num_obds; j++) {
1042 if (param->obdindexes[j] ==
1043 param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
1048 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
1053 /* Check the time on mds. */
1057 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
1058 param->lmd->lmd_lmm.lmm_stripe_count)
1060 decision = find_time_check(st, param, for_mds);
1064 /* If file still fits the request, ask osd for updated info.
1065 The regulat stat is almost of the same speed as some new
1066 'glimpse-size-ioctl'. */
1067 if (!decision && S_ISREG(st->st_mode) &&
1068 (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) {
1069 if (param->obdindex != OBD_NOT_FOUND) {
1070 /* Check whether the obd is active or not, if it is
1071 * not active, just print the object affected by this
1074 struct obd_statfs stat_buf;
1075 struct obd_uuid uuid_buf;
1077 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1078 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1079 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
1080 param->obdindex, &stat_buf,
1083 if (ret == -ENODATA || ret == -ENODEV
1086 printf("obd_uuid: %s failed %s ",
1087 param->obduuid->uuid, strerror(errno));
1092 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
1093 (void *)param->lmd);
1094 } else if (parent) {
1095 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
1096 (void *)param->lmd);
1100 if (errno == ENOENT) {
1101 err_msg("warning: %s: %s does not exist",
1102 __FUNCTION__, path);
1105 fprintf(stderr, "%s: IOC_LOV_GETINFO on %s failed: "
1106 "%s.\n", __FUNCTION__, path, strerror(errno));
1111 /* Check the time on osc. */
1112 decision = find_time_check(st, param, 0);
1118 decision = find_value_cmp(st->st_size, param->size,
1119 param->size_sign, param->size_units,
1123 if (decision != -1) {
1132 /* Do not get down anymore? */
1133 if (param->depth == param->maxdepth)
1140 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
1141 struct dirent64 *de)
1143 struct find_param *param = (struct find_param *)data;
1148 int llapi_find(char *path, struct find_param *param)
1151 int ret, len = strlen(path);
1153 if (len > PATH_MAX) {
1154 fprintf(stderr, "%s: Path name '%s' is too long.\n",
1155 __FUNCTION__, path);
1159 buf = (char *)malloc(PATH_MAX + 1);
1163 ret = common_param_init(param);
1171 strncpy(buf, path, PATH_MAX + 1);
1172 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
1173 cb_common_fini, param, NULL);
1175 find_param_fini(param);
1177 return ret < 0 ? ret : 0;
1180 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
1181 struct dirent64 *de)
1183 struct find_param *param = (struct find_param *)data;
1186 LASSERT(parent != NULL || d != NULL);
1189 if (!param->got_uuids) {
1190 ret = setup_obd_uuid(d ? d : parent, path, param);
1196 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
1197 (void *)¶m->lmd->lmd_lmm);
1198 } else if (parent) {
1199 char *fname = strrchr(path, '/');
1200 fname = (fname == NULL ? path : fname + 1);
1202 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
1203 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
1204 (void *)¶m->lmd->lmd_lmm);
1208 if (errno == ENODATA) {
1209 if (!param->obduuid && !param->quiet)
1210 printf("%s has no stripe info\n", path);
1212 } else if (errno == ENOTTY) {
1213 fprintf(stderr, "%s: '%s' not on a Lustre fs?\n",
1214 __FUNCTION__, path);
1215 } else if (errno == ENOENT) {
1216 err_msg("warning: %s: %s does not exist",
1217 __FUNCTION__, path);
1220 err_msg("error: %s: %s failed for %s", __FUNCTION__,
1221 d ? "LL_IOC_LOV_GETSTRIPE" :
1222 "IOC_MDC_GETFILESTRIPE", path);
1228 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
1230 /* Do not get down anymore? */
1231 if (param->depth == param->maxdepth)
1238 int llapi_getstripe(char *path, struct find_param *param)
1241 int ret = 0, len = strlen(path);
1243 if (len > PATH_MAX) {
1244 fprintf(stderr, "%s: Path name '%s' is too long.\n",
1245 __FUNCTION__, path);
1249 buf = (char *)malloc(PATH_MAX + 1);
1253 ret = common_param_init(param);
1261 strncpy(buf, path, PATH_MAX + 1);
1262 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1263 cb_common_fini, param, NULL);
1264 find_param_fini(param);
1266 return ret < 0 ? ret : 0;
1269 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1270 struct obd_statfs *stat_buf,
1271 struct obd_uuid *uuid_buf)
1274 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1276 struct obd_ioctl_data data = { 0 };
1279 data.ioc_inlbuf1 = (char *)&type;
1280 data.ioc_inllen1 = sizeof(__u32);
1281 data.ioc_inlbuf2 = (char *)&index;
1282 data.ioc_inllen2 = sizeof(__u32);
1283 data.ioc_pbuf1 = (char *)stat_buf;
1284 data.ioc_plen1 = sizeof(struct obd_statfs);
1285 data.ioc_pbuf2 = (char *)uuid_buf;
1286 data.ioc_plen2 = sizeof(struct obd_uuid);
1288 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1289 fprintf(stderr, "llapi_obd_statfs: error packing ioctl data\n");
1293 fd = open(path, O_RDONLY);
1294 if (errno == EISDIR)
1295 fd = open(path, O_DIRECTORY | O_RDONLY);
1298 rc = errno ? -errno : -EBADF;
1299 err_msg("error: %s: opening '%s'", __FUNCTION__, path);
1302 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1304 rc = errno ? -errno : -EINVAL;
1310 #define MAX_STRING_SIZE 128
1311 #define DEVICES_LIST "/proc/fs/lustre/devices"
1313 int llapi_ping(char *obd_type, char *obd_name)
1315 char path[MAX_STRING_SIZE];
1319 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1320 obd_type, obd_name);
1322 fd = open(path, O_WRONLY);
1325 fprintf(stderr, "error opening %s: %s\n", path, strerror(errno));
1329 rc = write(fd, buf, 1);
1337 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1339 char buf[MAX_STRING_SIZE];
1340 FILE *fp = fopen(DEVICES_LIST, "r");
1345 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
1350 while (fgets(buf, sizeof(buf), fp) != NULL) {
1351 char *obd_type_name = NULL;
1352 char *obd_name = NULL;
1353 char *obd_uuid = NULL;
1354 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1355 char *bufl = rawbuf;
1357 struct obd_ioctl_data datal = { 0, };
1358 struct obd_statfs osfs_buffer;
1360 while(bufp[0] == ' ')
1363 for(i = 0; i < 3; i++) {
1364 obd_type_name = strsep(&bufp, " ");
1366 obd_name = strsep(&bufp, " ");
1367 obd_uuid = strsep(&bufp, " ");
1369 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1371 memset(bufl, 0, sizeof(rawbuf));
1372 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1373 datal.ioc_plen1 = sizeof(osfs_buffer);
1375 for (i = 0; i < type_num; i++) {
1376 if (strcmp(obd_type_name, obd_type[i]) != 0)
1379 cb(obd_type_name, obd_name, obd_uuid, args);
1386 static void do_target_check(char *obd_type_name, char *obd_name,
1387 char *obd_uuid, void *args)
1391 rc = llapi_ping(obd_type_name, obd_name);
1393 err_msg("error: check '%s'", obd_name);
1395 printf("%s active.\n", obd_name);
1399 int llapi_target_check(int type_num, char **obd_type, char *dir)
1401 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1404 #undef MAX_STRING_SIZE
1406 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1408 char raw[OBD_MAX_IOCTL_BUFFER];
1409 char out[LLOG_CHUNK_SIZE];
1411 struct obd_ioctl_data data = { 0 };
1416 sprintf(key, "%s", keyword);
1417 memset(raw, 0, sizeof(raw));
1418 memset(out, 0, sizeof(out));
1419 data.ioc_inlbuf1 = key;
1420 data.ioc_inllen1 = strlen(key) + 1;
1422 data.ioc_inlbuf2 = node_name;
1423 data.ioc_inllen2 = strlen(node_name) + 1;
1425 data.ioc_pbuf1 = out;
1426 data.ioc_plen1 = sizeof(out);
1427 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1431 root = opendir(dir);
1434 err_msg("open %s failed", dir);
1438 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1440 err_msg("ioctl OBD_IOC_CATINFO failed");
1442 fprintf(stdout, "%s", data.ioc_pbuf1);
1448 /* Is this a lustre fs? */
1449 int llapi_is_lustre_mnttype(const char *type)
1451 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1454 /* Is this a lustre client fs? */
1455 int llapi_is_lustre_mnt(struct mntent *mnt)
1457 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1458 strstr(mnt->mnt_fsname, ":/") != NULL);
1461 int llapi_quotacheck(char *mnt, int check_type)
1466 root = opendir(mnt);
1468 err_msg("open %s failed", mnt);
1472 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1478 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1484 root = opendir(mnt);
1486 err_msg("open %s failed", mnt);
1491 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1495 if (poll_intvl < 30)
1503 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1508 root = opendir(mnt);
1510 err_msg("open %s failed", mnt);
1514 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1520 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
1521 struct dirent64 *de)
1523 struct find_param *param = (struct find_param *)data;
1527 LASSERT(parent != NULL || d != NULL);
1530 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1531 (void *)param->lmd);
1532 } else if (parent) {
1533 char *fname = strrchr(path, '/');
1534 fname = (fname == NULL ? path : fname + 1);
1536 strncpy((char *)param->lmd, fname, param->lumlen);
1537 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1538 (void *)param->lmd);
1544 if (errno == ENODATA) {
1545 if (!param->obduuid && !param->quiet)
1546 fprintf(stderr, "%s has no stripe info\n",
1549 } else if (errno == ENOENT) {
1550 err_msg("warning: %s: %s does not exist",
1551 __FUNCTION__, path);
1553 } else if (errno != EISDIR) {
1555 err_msg("%s ioctl failed for %s.",
1556 d ? "LL_IOC_MDC_GETINFO" :
1557 "IOC_MDC_GETFILEINFO", path);
1562 st = ¶m->lmd->lmd_st;
1564 /* libc chown() will do extra check, and if the real owner is
1565 * the same as the ones to set, it won't fall into kernel, so
1566 * invoke syscall directly. */
1567 rc = syscall(SYS_chown, path, -1, -1);
1569 err_msg("error: chown %s (%u,%u)", path);
1571 rc = chmod(path, st->st_mode);
1573 err_msg("error: chmod %s (%hu)", path, st->st_mode);
1578 int llapi_quotachown(char *path, int flag)
1580 struct find_param param;
1582 int ret = 0, len = strlen(path);
1584 if (len > PATH_MAX) {
1585 fprintf(stderr, "%s: Path name '%s' is too long.\n",
1586 __FUNCTION__, path);
1590 buf = (char *)malloc(PATH_MAX + 1);
1594 memset(¶m, 0, sizeof(param));
1595 param.recursive = 1;
1599 ret = common_param_init(¶m);
1603 strncpy(buf, path, PATH_MAX + 1);
1604 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
1605 NULL, ¶m, NULL);
1607 find_param_fini(¶m);
1612 int llapi_getfacl(char *fname, char *cmd)
1614 struct rmtacl_ioctl_data data;
1615 char out[RMTACL_SIZE_MAX] = "";
1619 data.cmd_len = strlen(cmd) + 1;
1621 data.res_len = sizeof(out);
1623 fd = open(fname, 0);
1625 err_msg("open %s failed", fname);
1629 rc = ioctl(fd, LL_IOC_GETFACL, &data);
1631 if (errno == EBADE) {
1632 fprintf(stderr, "Please use getfacl directly!\n");
1635 err_msg("getfacl %s failed", fname);
1643 int llapi_setfacl(char *fname, char *cmd)
1645 struct rmtacl_ioctl_data data;
1646 char out[RMTACL_SIZE_MAX] = "";
1650 data.cmd_len = strlen(cmd) + 1;
1652 data.res_len = sizeof(out);
1654 fd = open(fname, 0);
1656 err_msg("open %s failed", fname);
1660 rc = ioctl(fd, LL_IOC_SETFACL, &data);
1662 if (errno == EBADE) {
1663 fprintf(stderr, "Please use setfacl directly!\n");
1665 } else if (errno == EOPNOTSUPP) {
1666 fprintf(stderr, "setfacl: %s: %s\n", fname, strerror(errno));
1669 err_msg("setfacl %s failed", fname);