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.
34 #include <sys/ioctl.h>
41 #include <sys/types.h>
42 #include <sys/syscall.h>
44 #ifdef HAVE_ASM_TYPES_H
45 #include <asm/types.h>
47 #ifdef HAVE_LINUX_UNISTD_H
48 #include <linux/unistd.h>
53 #include <lnet/lnetctl.h>
55 #include <liblustre.h>
57 #include <lustre_lib.h>
59 #include <lustre/liblustreapi.h>
61 static void err_msg(char *fmt, ...)
64 int tmp_errno = abs(errno);
67 vfprintf(stderr, fmt, args);
69 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
72 int llapi_file_create(const char *name, unsigned long stripe_size, int stripe_offset,
73 int stripe_count, int stripe_pattern)
75 struct lov_user_md lum = { 0 };
80 fd = open(name, O_CREAT | O_RDWR | O_LOV_DELAY_CREATE, 0644);
81 if (errno == EISDIR) {
82 fd = open(name, O_DIRECTORY | O_RDONLY);
88 err_msg("unable to open '%s'", name);
92 /* 64 KB is the largest common page size I'm aware of (on ia64), but
93 * check the local page size just in case. */
94 page_size = LOV_MIN_STRIPE_SIZE;
95 if (getpagesize() > page_size) {
96 page_size = getpagesize();
97 fprintf(stderr, "warning: your page size (%u) is larger than "
98 "expected (%u).\n", page_size, LOV_MIN_STRIPE_SIZE);
100 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
101 errno = rc = -EINVAL;
102 err_msg("error: bad stripe_size %lu, must be an even "
103 "multiple of %d bytes", stripe_size, page_size);
106 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
107 errno = rc = -EINVAL;
108 err_msg("error: bad stripe offset %d", stripe_offset);
111 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
112 errno = rc = -EINVAL;
113 err_msg("error: bad stripe count %d", stripe_count);
116 if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
117 errno = rc = -EINVAL;
118 err_msg("error: stripe_size %lu * stripe_count %u "
119 "exceeds 4GB", stripe_size, stripe_count);
123 /* Initialize IOCTL striping pattern structure */
124 lum.lmm_magic = LOV_USER_MAGIC;
125 lum.lmm_pattern = stripe_pattern;
126 lum.lmm_stripe_size = stripe_size;
127 lum.lmm_stripe_count = stripe_count;
128 lum.lmm_stripe_offset = stripe_offset;
130 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
131 char *errmsg = "stripe already set";
133 if (errno != EEXIST && errno != EALREADY)
134 errmsg = strerror(errno);
137 fprintf(stderr, "error on ioctl "LPX64" for '%s' (%d): %s\n",
138 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
144 err_msg("error on close for '%s' (%d)", name, fd);
149 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d, void *data);
151 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
152 #define OBD_NOT_FOUND (-1)
154 static int common_param_init(struct find_param *param)
156 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
157 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
158 err_msg("error: allocation of %d bytes for ioctl",
159 sizeof(lstat_t) + param->lumlen);
163 param->got_uuids = 0;
164 param->obdindex = OBD_NOT_FOUND;
168 static void find_param_fini(struct find_param *param)
174 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
176 char lov_name[sizeof(struct obd_uuid)];
179 int rc = 0, index = 0;
181 /* Get the lov name */
182 rc = ioctl(fd, OBD_IOC_GETNAME, (void *) lov_name);
185 err_msg("error: can't get lov name.");
189 /* Now get the ost uuids from /proc */
190 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
192 fp = fopen(buf, "r");
195 err_msg("error: opening '%s'", buf);
199 while ((fgets(buf, sizeof(buf), fp) != NULL) && index < *ost_count) {
200 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) < 2)
211 static int setup_obd_uuids(DIR *dir, char *dname, struct find_param *param)
213 char uuid[sizeof(struct obd_uuid)];
218 /* Get the lov name */
219 rc = ioctl(dirfd(dir), OBD_IOC_GETNAME, (void *)uuid);
221 if (errno != ENOTTY) {
223 err_msg("error: can't get lov name: %s", dname);
230 param->got_uuids = 1;
232 /* Now get the ost uuids from /proc */
233 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
235 fp = fopen(buf, "r");
238 err_msg("error: opening '%s'", buf);
242 if (!param->obduuid && !param->quiet && !param->obds_printed)
245 while (fgets(buf, sizeof(buf), fp) != NULL) {
246 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
249 if (param->obduuid) {
250 if (strncmp(param->obduuid->uuid, uuid,
251 sizeof(uuid)) == 0) {
252 param->obdindex = index;
255 } else if (!param->quiet && !param->obds_printed) {
256 /* Print everything */
260 param->obds_printed = 1;
264 if (!param->quiet && param->obduuid &&
265 (param->obdindex == OBD_NOT_FOUND)) {
266 fprintf(stderr, "error: %s: unknown obduuid: %s\n",
267 __FUNCTION__, param->obduuid->uuid);
274 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
275 int obdindex, int quiet, int header, int body)
277 int i, obdstripe = 0;
279 if (obdindex != OBD_NOT_FOUND) {
280 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
281 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
282 printf("%s\n", path);
288 printf("%s\n", path);
292 /* if it's a directory */
294 if (obdstripe == 1) {
295 printf("default stripe_count: %d stripe_size: %u "
296 "stripe_offset: %d\n",
297 lum->lmm_stripe_count == (__u16)-1 ? -1 :
298 lum->lmm_stripe_count,
299 lum->lmm_stripe_size,
300 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
301 lum->lmm_stripe_offset);
306 if (header && (obdstripe == 1)) {
307 printf("lmm_magic: 0x%08X\n", lum->lmm_magic);
308 printf("lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
309 printf("lmm_object_id: "LPX64"\n", lum->lmm_object_id);
310 printf("lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
311 printf("lmm_stripe_size: %u\n", lum->lmm_stripe_size);
312 printf("lmm_stripe_pattern: %x\n", lum->lmm_pattern);
316 if ((!quiet) && (obdstripe == 1))
317 printf("\tobdidx\t\t objid\t\tobjid\t\t group\n");
319 for (i = 0; i < lum->lmm_stripe_count; i++) {
320 int idx = lum->lmm_objects[i].l_ost_idx;
321 long long oid = lum->lmm_objects[i].l_object_id;
322 long long gr = lum->lmm_objects[i].l_object_gr;
323 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
324 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
326 obdindex == idx ? " *" : "");
332 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
333 int is_dir, int obdindex, int quiet,
334 int header, int body)
336 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
337 int i, obdstripe = 0;
339 if (obdindex != OBD_NOT_FOUND) {
340 for (i = 0; i < lumj->lmm_stripe_count; i++) {
341 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
342 printf("%s\n", path);
348 printf("%s\n", path);
352 if (header && obdstripe == 1) {
353 printf("lmm_magic: 0x%08X\n", lumj->lmm_magic);
354 printf("lmm_object_gr: "LPX64"\n", lumj->lmm_object_gr);
355 printf("lmm_object_id: "LPX64"\n", lumj->lmm_object_id);
356 printf("lmm_stripe_count: %u\n", (int)lumj->lmm_stripe_count);
357 printf("lmm_stripe_size: %u\n", lumj->lmm_stripe_size);
358 printf("lmm_stripe_pattern: %x\n", lumj->lmm_pattern);
359 printf("lmm_extent_count: %x\n", lumj->lmm_extent_count);
363 unsigned long long start = -1, end = 0;
364 if (!quiet && obdstripe == 1)
365 printf("joined\tobdidx\t\t objid\t\tobjid\t\t group"
366 "\t\tstart\t\tend\n");
367 for (i = 0; i < lumj->lmm_stripe_count; i++) {
368 int idx = lumj->lmm_objects[i].l_ost_idx;
369 long long oid = lumj->lmm_objects[i].l_object_id;
370 long long gr = lumj->lmm_objects[i].l_object_gr;
371 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
372 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s",
374 obdindex == idx ? " *" : "");
375 if (start != lumj->lmm_objects[i].l_extent_start ||
376 end != lumj->lmm_objects[i].l_extent_end) {
377 start = lumj->lmm_objects[i].l_extent_start;
378 printf("\t%14llu", start);
379 end = lumj->lmm_objects[i].l_extent_end;
380 if (end == (unsigned long long)-1)
383 printf("\t\t%llu\n", end);
385 printf("\t\t\t\t\n");
392 void llapi_lov_dump_user_lmm(struct find_param *param,
393 char *path, int is_dir)
395 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
396 case LOV_USER_MAGIC_V1:
397 lov_dump_user_lmm_v1(¶m->lmd->lmd_lmm, path, is_dir,
398 param->obdindex, param->quiet,
400 (param->verbose || !param->obduuid));
402 case LOV_USER_MAGIC_JOIN:
403 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
404 param->obdindex, param->quiet,
406 (param->verbose || !param->obduuid));
409 printf("unknown lmm_magic: %#x (expecting %#x)\n",
410 *(__u32 *)¶m->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
415 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
421 fname = strrchr(path, '/');
423 /* It should be a file (or other non-directory) */
425 dname = (char *)malloc(2);
429 fname = (char *)path;
431 dname = (char *)malloc(fname - path + 1);
434 strncpy(dname, path, fname - path);
435 dname[fname - path] = '\0';
439 if ((fd = open(dname, O_RDONLY)) == -1) {
445 strcpy((char *)lum, fname);
446 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
449 if (close(fd) == -1 && rc == 0)
457 int llapi_file_lookup(int dirfd, const char *name)
459 struct obd_ioctl_data data = { 0 };
464 if (dirfd < 0 || name == NULL)
467 data.ioc_version = OBD_IOCTL_VERSION;
468 data.ioc_len = sizeof(data);
469 data.ioc_inlbuf1 = (char *)name;
470 data.ioc_inllen1 = strlen(name) + 1;
472 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
475 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d\n",
480 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
483 /* some 64bit libcs implement readdir64() by calling sys_getdents(). the
484 * kernel's sys_getdents() doesn't return d_type. */
485 unsigned char handle_dt_unknown(char *path)
489 fd = open(path, O_DIRECTORY|O_RDONLY);
491 if (errno == ENOTDIR)
492 return DT_REG; /* kind of a lie */
499 static DIR *opendir_parent(char *path)
505 fname = strrchr(path, '/');
511 parent = opendir(path);
516 static int llapi_semantic_traverse(char *path, DIR *parent,
517 semantic_func_t sem_init,
518 semantic_func_t sem_fini, void *data)
520 struct dirent64 *dent;
528 if (!d && errno != ENOTDIR) {
529 fprintf(stderr, "%s: Failed to open '%s': %s.",
530 __FUNCTION__, path, strerror(errno));
532 } else if (!d && !parent) {
533 /* ENOTDIR. Open the parent dir. */
534 p = opendir_parent(path);
536 GOTO(out, ret = -EINVAL);
539 if (sem_init && (ret = sem_init(path, parent ?: p, d, data)))
545 while ((dent = readdir64(d)) != NULL) {
546 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
551 strcat(path, dent->d_name);
553 if (dent->d_type == DT_UNKNOWN)
554 dent->d_type = handle_dt_unknown(path);
556 switch (dent->d_type) {
558 fprintf(stderr, "error: %s: '%s' is UNKNOWN type %d",
559 __FUNCTION__, dent->d_name, dent->d_type);
560 /* If we cared we could stat the file to determine
561 * type and continue on here, but we don't since we
562 * know d_type should be valid for lustre and this
563 * tool only makes sense for lustre filesystems. */
566 ret = llapi_semantic_traverse(path, d, sem_init,
574 ret = sem_init(path, d, NULL, data);
578 if (sem_fini && ret == 0)
579 sem_fini(path, d, NULL, data);
587 sem_fini(path, parent, d, data);
596 /* Check if the file time matches 1 of the given criteria (e.g. --atime +/-N).
597 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
599 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
600 * The table bolow gives the answers for the specified parameters (time and
601 * sign), 1st column is the answer for the MDS time, the 2nd is for the OST:
602 * --------------------------------------
603 * 1 | file > limit; sign > 0 | -1 / -1 |
604 * 2 | file = limit; sign > 0 | ? / 1 |
605 * 3 | file < limit; sign > 0 | ? / 1 |
606 * 4 | file > limit; sign = 0 | -1 / -1 |
607 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
608 * 6 | file < limit; sign = 0 | ? / -1 |
609 * 7 | file > limit; sign < 0 | 1 / 1 |
610 * 8 | file = limit; sign < 0 | ? / -1 |
611 * 9 | file < limit; sign < 0 | ? / -1 |
612 * --------------------------------------
613 * Note: 5th actually means that the file time stamp is within the interval
614 * (limit - 24hours, limit]. */
615 static int find_time_cmp(time_t file, time_t limit, int sign, int mds) {
622 if (file <= limit && file + 24 * 60 * 60 > limit)
624 if (file + 24 * 60 * 60 <= limit)
638 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
639 * Return -1 or 1 if file timestamp does not or does match the given criteria
640 * correspondingly. Return 0 if the MDS time is being checked and there are
641 * attributes on OSTs and it is not yet clear if the timespamp matches.
643 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
644 * updated timestamps. */
645 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
650 /* Check if file is accepted. */
652 ret = find_time_cmp(st->st_atime, param->atime,
660 ret = find_time_cmp(st->st_mtime, param->mtime,
665 /* If the previous check matches, but this one is not yet clear,
666 * we should return 0 to do an RPC on OSTs. */
672 ret = find_time_cmp(st->st_ctime, param->ctime,
677 /* If the previous check matches, but this one is not yet clear,
678 * we should return 0 to do an RPC on OSTs. */
686 static int cb_find_init(char *path, DIR *parent, DIR *dir, void *data)
688 struct find_param *param = (struct find_param *)data;
689 int decision = 1; /* 1 is accepted; -1 is rejected. */
690 lstat_t *st = ¶m->lmd->lmd_st;
694 LASSERT(parent != NULL || dir != NULL);
696 param->lmd->lmd_lmm.lmm_stripe_count = 0;
698 /* If a time or OST should be checked, the decision is not taken yet. */
699 if (param->atime || param->ctime || param->mtime || param->obduuid)
702 /* Request MDS for the stat info. */
703 if (!decision && dir) {
704 /* retrieve needed file info */
705 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
707 } else if (!decision && parent) {
708 char *fname = strrchr(path, '/');
709 fname = (fname == NULL ? path : fname + 1);
711 /* retrieve needed file info */
712 strncpy((char *)param->lmd, fname, param->lumlen);
713 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
718 if (errno == ENOTTY) {
719 /* ioctl is not supported, it is not a lustre fs.
720 * Do the regular lstat(2) instead. */
722 ret = lstat_f(path, st);
724 err_msg("error: %s: lstat failed for %s",
729 err_msg("error: %s: %s failed for %s", __FUNCTION__,
730 dir ? "LL_IOC_MDC_GETINFO" :
731 "IOC_MDC_GETFILEINFO", path);
737 if (param->obduuid) {
738 if (lustre_fs && param->got_uuids &&
739 param->st_dev != st->st_dev) {
740 /* A lustre/lustre mount point is crossed. */
741 param->got_uuids = 0;
742 param->obds_printed = 0;
743 param->obdindex = OBD_NOT_FOUND;
746 if (lustre_fs && !param->got_uuids) {
747 ret = setup_obd_uuids(dir ? dir : parent, path, param);
750 param->st_dev = st->st_dev;
751 } else if (!lustre_fs && param->got_uuids) {
752 /* A lustre/non-lustre mount point is crossed. */
753 param->got_uuids = 0;
754 param->obdindex = OBD_NOT_FOUND;
758 /* If a regular expression is presented, make the initial decision */
759 if (param->pattern != NULL) {
760 char *fname = strrchr(path, '/');
761 fname = (fname == NULL ? path : fname + 1);
762 ret = fnmatch(param->pattern, fname, 0);
763 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
764 (ret == 0 && param->exclude_pattern))
768 /* If an OBD UUID is specified but no one matches, skip this file. */
769 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
772 /* If a OST UUID is given, and some OST matches, check it here. */
773 if (decision != -1 && param->obdindex != OBD_NOT_FOUND &&
774 S_ISREG(st->st_mode)) {
775 /* Only those files should be accepted, which have a strip on
776 * the specified OST. */
777 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
782 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
783 if (param->obdindex ==
784 param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
788 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
793 /* Check the time on mds. */
797 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
798 param->lmd->lmd_lmm.lmm_stripe_count)
800 decision = find_time_check(st, param, for_mds);
803 /* If file still fits the request, ask osd for updated info.
804 The regulat stat is almost of the same speed as some new
805 'glimpse-size-ioctl'. */
806 if (!decision && param->lmd->lmd_lmm.lmm_stripe_count &&
807 S_ISREG(st->st_mode)) {
808 if (param->obdindex != OBD_NOT_FOUND) {
809 /* Check whether the obd is active or not, if it is
810 * not active, just print the object affected by this
813 struct obd_statfs stat_buf;
814 struct obd_uuid uuid_buf;
816 memset(&stat_buf, 0, sizeof(struct obd_statfs));
817 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
818 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
819 param->obdindex, &stat_buf,
822 if (ret == -ENODATA || ret == -ENODEV
825 printf("obd_uuid: %s failed %s ",
826 param->obduuid->uuid, strerror(errno));
831 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
834 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
839 fprintf(stderr, "%s: IOC_LOV_GETINFO on %s failed: "
840 "%s.\n", __FUNCTION__, path, strerror(errno));
844 /* Check the time on osc. */
846 decision = find_time_check(st, param, 0);
850 if (decision != -1) {
858 /* Do not get down anymore? */
859 if (param->depth == param->maxdepth)
866 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data)
868 struct find_param *param = (struct find_param *)data;
873 int llapi_find(char *path, struct find_param *param)
875 char buf[PATH_MAX + 1];
878 ret = common_param_init(param);
883 strncpy(buf, path, strlen(path));
884 buf[strlen(path)] = '\0';
886 ret = llapi_semantic_traverse(buf, NULL, cb_find_init,
887 cb_common_fini, param);
889 find_param_fini(param);
890 return ret < 0 ? ret : 0;
893 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data)
895 struct find_param *param = (struct find_param *)data;
898 LASSERT(parent != NULL || d != NULL);
901 if (!param->got_uuids) {
902 ret = setup_obd_uuids(d ? d : parent, path, param);
908 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
909 (void *)¶m->lmd->lmd_lmm);
911 char *fname = strrchr(path, '/');
912 fname = (fname == NULL ? path : fname + 1);
914 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
915 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
916 (void *)¶m->lmd->lmd_lmm);
920 if (errno == ENODATA) {
921 if (!param->obduuid && !param->quiet)
922 printf("%s has no stripe info\n", path);
924 } else if (errno == ENOTTY) {
925 fprintf(stderr, "%s: '%s' not on a Lustre fs?\n",
928 err_msg("error: %s: %s failed for %s", __FUNCTION__,
929 d ? "LL_IOC_LOV_GETSTRIPE" :
930 "IOC_MDC_GETFILESTRIPE", path);
936 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
938 /* Do not get down anymore? */
939 if (param->depth == param->maxdepth)
946 int llapi_getstripe(char *path, struct find_param *param)
950 ret = common_param_init(param);
955 ret = llapi_semantic_traverse(path, NULL, cb_getstripe,
956 cb_common_fini, param);
957 find_param_fini(param);
958 return ret < 0 ? ret : 0;
961 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
962 struct obd_statfs *stat_buf,
963 struct obd_uuid *uuid_buf)
966 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
968 struct obd_ioctl_data data = { 0 };
971 data.ioc_inlbuf1 = (char *)&type;
972 data.ioc_inllen1 = sizeof(__u32);
973 data.ioc_inlbuf2 = (char *)&index;
974 data.ioc_inllen2 = sizeof(__u32);
975 data.ioc_pbuf1 = (char *)stat_buf;
976 data.ioc_plen1 = sizeof(struct obd_statfs);
977 data.ioc_pbuf2 = (char *)uuid_buf;
978 data.ioc_plen2 = sizeof(struct obd_uuid);
980 if (obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) {
981 fprintf(stderr, "llapi_obd_statfs: error packing ioctl data\n");
985 fd = open(path, O_RDONLY);
987 fd = open(path, O_DIRECTORY | O_RDONLY);
990 rc = errno ? -errno : -EBADF;
991 err_msg("error: %s: opening '%s'", __FUNCTION__, path);
994 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
996 rc = errno ? -errno : -EINVAL;
1002 #define MAX_STRING_SIZE 128
1003 #define DEVICES_LIST "/proc/fs/lustre/devices"
1005 int llapi_ping(char *obd_type, char *obd_name)
1007 char path[MAX_STRING_SIZE];
1011 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1012 obd_type, obd_name);
1014 fd = open(path, O_WRONLY);
1017 fprintf(stderr, "error opening %s: %s\n", path, strerror(errno));
1021 rc = write(fd, buf, 1);
1029 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1031 char buf[MAX_STRING_SIZE];
1032 FILE *fp = fopen(DEVICES_LIST, "r");
1037 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
1042 while (fgets(buf, sizeof(buf), fp) != NULL) {
1043 char *obd_type_name = NULL;
1044 char *obd_name = NULL;
1045 char *obd_uuid = NULL;
1046 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1047 char *bufl = rawbuf;
1049 struct obd_ioctl_data datal = { 0, };
1050 struct obd_statfs osfs_buffer;
1052 while(bufp[0] == ' ')
1055 for(i = 0; i < 3; i++) {
1056 obd_type_name = strsep(&bufp, " ");
1058 obd_name = strsep(&bufp, " ");
1059 obd_uuid = strsep(&bufp, " ");
1061 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1063 memset(bufl, 0, sizeof(rawbuf));
1064 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1065 datal.ioc_plen1 = sizeof(osfs_buffer);
1067 for (i = 0; i < type_num; i++) {
1068 if (strcmp(obd_type_name, obd_type[i]) != 0)
1071 cb(obd_type_name, obd_name, obd_uuid, args);
1078 static void do_target_check(char *obd_type_name, char *obd_name,
1079 char *obd_uuid, void *args)
1083 rc = llapi_ping(obd_type_name, obd_name);
1085 err_msg("error: check '%s'", obd_name);
1087 printf("%s active.\n", obd_name);
1091 int llapi_target_check(int type_num, char **obd_type, char *dir)
1093 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1096 #undef MAX_STRING_SIZE
1098 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1100 char raw[OBD_MAX_IOCTL_BUFFER];
1101 char out[LLOG_CHUNK_SIZE];
1103 struct obd_ioctl_data data = { 0 };
1108 sprintf(key, "%s", keyword);
1109 memset(raw, 0, sizeof(raw));
1110 memset(out, 0, sizeof(out));
1111 data.ioc_inlbuf1 = key;
1112 data.ioc_inllen1 = strlen(key) + 1;
1114 data.ioc_inlbuf2 = node_name;
1115 data.ioc_inllen2 = strlen(node_name) + 1;
1117 data.ioc_pbuf1 = out;
1118 data.ioc_plen1 = sizeof(out);
1119 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1123 root = opendir(dir);
1126 err_msg("open %s failed", dir);
1130 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1132 err_msg("ioctl OBD_IOC_CATINFO failed");
1134 fprintf(stdout, "%s", data.ioc_pbuf1);
1140 /* Is this a lustre fs? */
1141 int llapi_is_lustre_mnttype(const char *type)
1143 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1146 /* Is this a lustre client fs? */
1147 int llapi_is_lustre_mnt(struct mntent *mnt)
1149 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1150 strstr(mnt->mnt_fsname, ":/") != NULL);
1153 int llapi_quotacheck(char *mnt, int check_type)
1158 root = opendir(mnt);
1160 err_msg("open %s failed", mnt);
1164 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1170 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1176 root = opendir(mnt);
1178 err_msg("open %s failed", mnt);
1183 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1187 if (poll_intvl < 30)
1195 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1200 root = opendir(mnt);
1202 err_msg("open %s failed", mnt);
1206 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1212 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data)
1214 struct find_param *param = (struct find_param *)data;
1218 LASSERT(parent != NULL || d != NULL);
1221 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1222 (void *)param->lmd);
1223 } else if (parent) {
1224 char *fname = strrchr(path, '/');
1225 fname = (fname == NULL ? path : fname + 1);
1227 strncpy((char *)param->lmd, fname, param->lumlen);
1228 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1229 (void *)param->lmd);
1235 if (errno == ENODATA) {
1236 if (!param->obduuid && !param->quiet)
1237 fprintf(stderr, "%s has no stripe info\n",
1240 } else if (errno != EISDIR) {
1242 err_msg("%s ioctl failed for %s.",
1243 d ? "LL_IOC_MDC_GETINFO" :
1244 "IOC_MDC_GETFILEINFO", path);
1249 st = ¶m->lmd->lmd_st;
1251 /* libc chown() will do extra check, and if the real owner is
1252 * the same as the ones to set, it won't fall into kernel, so
1253 * invoke syscall directly. */
1254 rc = syscall(SYS_chown, path, -1, -1);
1256 err_msg("error: chown %s (%u,%u)", path);
1258 rc = chmod(path, st->st_mode);
1260 err_msg("error: chmod %s (%hu)", path, st->st_mode);
1265 int llapi_quotachown(char *path, int flag)
1267 struct find_param param;
1270 memset(¶m, 0, sizeof(param));
1271 param.recursive = 1;
1275 ret = common_param_init(¶m);
1279 ret = llapi_semantic_traverse(path, NULL, cb_quotachown,
1282 find_param_fini(¶m);
1286 int llapi_getfacl(char *fname, char *cmd)
1288 struct rmtacl_ioctl_data data;
1289 char out[RMTACL_SIZE_MAX] = "";
1293 data.cmd_len = strlen(cmd) + 1;
1295 data.res_len = sizeof(out);
1297 fd = open(fname, 0);
1299 err_msg("open %s failed", fname);
1303 rc = ioctl(fd, LL_IOC_GETFACL, &data);
1305 if (errno == EBADE) {
1306 fprintf(stderr, "Please use getfacl directly!\n");
1309 err_msg("getfacl %s failed", fname);
1317 int llapi_setfacl(char *fname, char *cmd)
1319 struct rmtacl_ioctl_data data;
1320 char out[RMTACL_SIZE_MAX] = "";
1324 data.cmd_len = strlen(cmd) + 1;
1326 data.res_len = sizeof(out);
1328 fd = open(fname, 0);
1330 err_msg("open %s failed", fname);
1334 rc = ioctl(fd, LL_IOC_SETFACL, &data);
1336 if (errno == EBADE) {
1337 fprintf(stderr, "Please use setfacl directly!\n");
1339 } else if (errno == EOPNOTSUPP) {
1340 fprintf(stderr, "setfacl: %s: %s\n", fname, strerror(errno));
1343 err_msg("setfacl %s failed", fname);