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>
43 #ifdef HAVE_ASM_TYPES_H
44 #include <asm/types.h>
46 #ifdef HAVE_LINUX_UNISTD_H
47 #include <linux/unistd.h>
52 #include <lnet/lnetctl.h>
54 #include <liblustre.h>
56 #include <lustre_lib.h>
57 #include <lustre/liblustreapi.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, 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);
87 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 > 2048) {
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 > ~0UL) {
117 errno = rc = -EINVAL;
118 err_msg("error: stripe_size %ld * stripe_count %d "
119 "exceeds %lu bytes", ~0UL);
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";
132 if (errno != EEXIST && errno != EALREADY)
133 errmsg = strerror(errno);
135 fprintf(stderr, "error on ioctl "LPX64" for '%s' (%d): %s\n",
136 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
141 err_msg("error on close for '%s' (%d)", name, fd);
148 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d, void *data);
150 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
151 #define OBD_NOT_FOUND (-1)
153 static int common_param_init(struct find_param *param)
155 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
156 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
157 err_msg("error: allocation of %d bytes for ioctl",
158 sizeof(lstat_t) + param->lumlen);
162 param->got_uuids = 0;
163 param->obdindex = OBD_NOT_FOUND;
167 static void find_param_fini(struct find_param *param)
173 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
175 struct obd_ioctl_data data = { 0, };
176 struct lov_desc desc = { 0, };
178 int max_ost_count, rc;
181 max_ost_count = (OBD_MAX_IOCTL_BUFFER - size_round(sizeof(data)) -
182 size_round(sizeof(desc))) /
183 (sizeof(*uuidp) + sizeof(*obdgens));
184 if (max_ost_count > *ost_count)
185 max_ost_count = *ost_count;
187 obdgens = malloc(size_round(max_ost_count * sizeof(*obdgens)));
189 err_msg("error: %d generation #'s", max_ost_count);
193 data.ioc_inllen1 = sizeof(desc);
194 data.ioc_inlbuf1 = (char *)&desc;
195 data.ioc_inllen2 = size_round(max_ost_count * sizeof(*uuidp));
196 data.ioc_inlbuf2 = (char *)uuidp;
197 data.ioc_inllen3 = size_round(max_ost_count * sizeof(*obdgens));
198 data.ioc_inlbuf3 = (char *)obdgens;
200 desc.ld_tgt_count = max_ost_count;
202 if (obd_ioctl_pack(&data, &buf, OBD_MAX_IOCTL_BUFFER)) {
203 fprintf(stderr, "error: %s: internal packing error\n",
209 rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
211 err_msg("error: %s: getting LOV config", __FUNCTION__);
216 if (obd_ioctl_unpack(&data, buf, OBD_MAX_IOCTL_BUFFER)) {
218 err_msg("error: %s: internal ioctl unpack", __FUNCTION__);
222 *ost_count = desc.ld_tgt_count;
230 static int setup_obd_uuids(DIR *dir, char *dname, struct find_param *param)
232 char uuid[sizeof(struct obd_uuid)];
237 /* Get the lov name */
238 rc = ioctl(dirfd(dir), OBD_IOC_GETNAME, (void *)uuid);
240 if (errno != ENOTTY) {
241 err_msg("error: can't get lov name: %s", dname);
249 param->got_uuids = 1;
251 /* Now get the ost uuids from /proc */
252 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
254 fp = fopen(buf, "r");
256 err_msg("error: opening '%s'", buf);
260 if (!param->obduuid && !param->quiet && !param->obds_printed)
263 while (fgets(buf, sizeof(buf), fp) != NULL) {
264 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
267 if (param->obduuid) {
268 if (strncmp(param->obduuid->uuid, uuid,
269 sizeof(uuid)) == 0) {
270 param->obdindex = index;
273 } else if (!param->quiet && !param->obds_printed) {
274 /* Print everything */
278 param->obds_printed = 1;
282 if (!param->quiet && param->obduuid &&
283 (param->obdindex == OBD_NOT_FOUND)) {
284 fprintf(stderr, "error: %s: unknown obduuid: %s\n",
285 __FUNCTION__, param->obduuid->uuid);
292 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
293 int obdindex, int quiet, int header, int body)
295 int i, obdstripe = 0;
297 if (obdindex != OBD_NOT_FOUND) {
298 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
299 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
300 printf("%s\n", path);
306 printf("%s\n", path);
310 /* if it's a directory */
312 if (obdstripe == 1) {
313 printf("default stripe_count: %d stripe_size: %u "
314 "stripe_offset: %d\n",
315 lum->lmm_stripe_count == (__u16)-1 ? -1 :
316 lum->lmm_stripe_count,
317 lum->lmm_stripe_size,
318 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
319 lum->lmm_stripe_offset);
324 if (header && (obdstripe == 1)) {
325 printf("lmm_magic: 0x%08X\n", lum->lmm_magic);
326 printf("lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
327 printf("lmm_object_id: "LPX64"\n", lum->lmm_object_id);
328 printf("lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
329 printf("lmm_stripe_size: %u\n", lum->lmm_stripe_size);
330 printf("lmm_stripe_pattern: %x\n", lum->lmm_pattern);
334 if ((!quiet) && (obdstripe == 1))
335 printf("\tobdidx\t\t objid\t\tobjid\t\t group\n");
337 for (i = 0; i < lum->lmm_stripe_count; i++) {
338 int idx = lum->lmm_objects[i].l_ost_idx;
339 long long oid = lum->lmm_objects[i].l_object_id;
340 long long gr = lum->lmm_objects[i].l_object_gr;
341 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
342 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
344 obdindex == idx ? " *" : "");
350 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
351 int is_dir, int obdindex, int quiet,
352 int header, int body)
354 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
355 int i, obdstripe = 0;
357 if (obdindex != OBD_NOT_FOUND) {
358 for (i = 0; i < lumj->lmm_stripe_count; i++) {
359 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
360 printf("%s\n", path);
366 printf("%s\n", path);
370 if (header && obdstripe == 1) {
371 printf("lmm_magic: 0x%08X\n", lumj->lmm_magic);
372 printf("lmm_object_gr: "LPX64"\n", lumj->lmm_object_gr);
373 printf("lmm_object_id: "LPX64"\n", lumj->lmm_object_id);
374 printf("lmm_stripe_count: %u\n", (int)lumj->lmm_stripe_count);
375 printf("lmm_stripe_size: %u\n", lumj->lmm_stripe_size);
376 printf("lmm_stripe_pattern: %x\n", lumj->lmm_pattern);
377 printf("lmm_extent_count: %x\n", lumj->lmm_extent_count);
381 unsigned long long start = -1, end = 0;
382 if (!quiet && obdstripe == 1)
383 printf("joined\tobdidx\t\t objid\t\tobjid\t\t group"
384 "\t\tstart\t\tend\n");
385 for (i = 0; i < lumj->lmm_stripe_count; i++) {
386 int idx = lumj->lmm_objects[i].l_ost_idx;
387 long long oid = lumj->lmm_objects[i].l_object_id;
388 long long gr = lumj->lmm_objects[i].l_object_gr;
389 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
390 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s",
392 obdindex == idx ? " *" : "");
393 if (start != lumj->lmm_objects[i].l_extent_start ||
394 end != lumj->lmm_objects[i].l_extent_end) {
395 start = lumj->lmm_objects[i].l_extent_start;
396 printf("\t%14llu", start);
397 end = lumj->lmm_objects[i].l_extent_end;
398 if (end == (unsigned long long)-1)
401 printf("\t\t%llu\n", end);
403 printf("\t\t\t\t\n");
410 void llapi_lov_dump_user_lmm(struct find_param *param,
411 char *path, int is_dir)
413 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
414 case LOV_USER_MAGIC_V1:
415 lov_dump_user_lmm_v1(¶m->lmd->lmd_lmm, path, is_dir,
416 param->obdindex, param->quiet,
418 (param->verbose || !param->obduuid));
420 case LOV_USER_MAGIC_JOIN:
421 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
422 param->obdindex, param->quiet,
424 (param->verbose || !param->obduuid));
427 printf("unknown lmm_magic: %#x (expecting %#x)\n",
428 *(__u32 *)¶m->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
433 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
438 fname = strrchr(path, '/');
440 /* It should be a file (or other non-directory) */
442 dname = (char *)malloc(2);
446 fname = (char *)path;
448 dname = (char *)malloc(fname - path + 1);
451 strncpy(dname, path, fname - path);
452 dname[fname - path] = '\0';
456 if ((fd = open(dname, O_RDONLY)) == -1) {
461 strcpy((char *)lum, fname);
462 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1) {
476 int llapi_file_lookup(int dirfd, const char *name)
478 struct obd_ioctl_data data = { 0 };
483 if (dirfd < 0 || name == NULL)
486 data.ioc_version = OBD_IOCTL_VERSION;
487 data.ioc_len = sizeof(data);
488 data.ioc_inlbuf1 = (char *)name;
489 data.ioc_inllen1 = strlen(name) + 1;
491 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
494 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d\n",
499 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
502 /* some 64bit libcs implement readdir64() by calling sys_getdents(). the
503 * kernel's sys_getdents() doesn't return d_type. */
504 unsigned char handle_dt_unknown(char *path)
508 fd = open(path, O_DIRECTORY|O_RDONLY);
510 if (errno == ENOTDIR)
511 return DT_REG; /* kind of a lie */
518 static DIR *opendir_parent(char *path)
524 fname = strrchr(path, '/');
530 parent = opendir(path);
535 static int llapi_semantic_traverse(char *path, DIR *parent,
536 semantic_func_t sem_init,
537 semantic_func_t sem_fini, void *data)
539 struct dirent64 *dent;
547 if (!d && errno != ENOTDIR) {
548 fprintf(stderr, "%s: Failed to open '%s': %s.",
549 __FUNCTION__, path, strerror(errno));
551 } else if (!d && !parent) {
552 /* ENOTDIR. Open the parent dir. */
553 p = opendir_parent(path);
555 GOTO(out, ret = -EINVAL);
558 if (sem_init && (ret = sem_init(path, parent ?: p, d, data)))
564 while ((dent = readdir64(d)) != NULL) {
565 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
570 strcat(path, dent->d_name);
572 if (dent->d_type == DT_UNKNOWN)
573 dent->d_type = handle_dt_unknown(path);
575 switch (dent->d_type) {
577 fprintf(stderr, "error: %s: '%s' is UNKNOWN type %d",
578 __FUNCTION__, dent->d_name, dent->d_type);
579 /* If we cared we could stat the file to determine
580 * type and continue on here, but we don't since we
581 * know d_type should be valid for lustre and this
582 * tool only makes sense for lustre filesystems. */
585 ret = llapi_semantic_traverse(path, d, sem_init,
593 ret = sem_init(path, d, NULL, data);
597 if (sem_fini && ret == 0)
598 sem_fini(path, d, NULL, data);
606 sem_fini(path, parent, d, data);
615 /* Check if the file time matches 1 of the given criteria (e.g. --atime +/-N).
616 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
618 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
619 * The table bolow gives the answers for the specified parameters (time and
620 * sign), 1st column is the answer for the MDS time, the 2nd is for the OST:
621 * --------------------------------------
622 * 1 | file > limit; sign > 0 | -1 / -1 |
623 * 2 | file = limit; sign > 0 | ? / 1 |
624 * 3 | file < limit; sign > 0 | ? / 1 |
625 * 4 | file > limit; sign = 0 | -1 / -1 |
626 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
627 * 6 | file < limit; sign = 0 | ? / -1 |
628 * 7 | file > limit; sign < 0 | 1 / 1 |
629 * 8 | file = limit; sign < 0 | ? / -1 |
630 * 9 | file < limit; sign < 0 | ? / -1 |
631 * --------------------------------------
632 * Note: 5th actually means that the file time stamp is within the interval
633 * (limit - 24hours, limit]. */
634 static int find_time_cmp(time_t file, time_t limit, int sign, int mds) {
641 if (file <= limit && file + 24 * 60 * 60 > limit)
643 if (file + 24 * 60 * 60 <= limit)
657 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
658 * Return -1 or 1 if file timestamp does not or does match the given criteria
659 * correspondingly. Return 0 if the MDS time is being checked and there are
660 * attributes on OSTs and it is not yet clear if the timespamp matches.
662 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
663 * updated timestamps. */
664 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
669 /* Check if file is accepted. */
671 ret = find_time_cmp(st->st_atime, param->atime,
679 ret = find_time_cmp(st->st_mtime, param->mtime,
684 /* If the previous check matches, but this one is not yet clear,
685 * we should return 0 to do an RPC on OSTs. */
691 ret = find_time_cmp(st->st_ctime, param->ctime,
696 /* If the previous check matches, but this one is not yet clear,
697 * we should return 0 to do an RPC on OSTs. */
705 static int cb_find_init(char *path, DIR *parent, DIR *dir, void *data)
707 struct find_param *param = (struct find_param *)data;
708 int decision = 1; /* 1 is accepted; -1 is rejected. */
709 lstat_t *st = ¶m->lmd->lmd_st;
713 LASSERT(parent != NULL || dir != NULL);
715 param->lmd->lmd_lmm.lmm_stripe_count = 0;
717 /* If a time or OST should be checked, the decision is not taken yet. */
718 if (param->atime || param->ctime || param->mtime || param->obduuid)
721 /* Request MDS for the stat info. */
722 if (!decision && dir) {
723 /* retrieve needed file info */
724 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
726 } else if (!decision && parent) {
727 char *fname = strrchr(path, '/') + 1;
729 /* retrieve needed file info */
730 strncpy((char *)param->lmd, fname, param->lumlen);
731 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
736 if (errno == ENOTTY) {
737 /* ioctl is not supported, it is not a lustre fs.
738 * Do the regular lstat(2) instead. */
740 ret = lstat_f(path, st);
742 err_msg("error: %s: lstat failed for %s",
747 err_msg("error: %s: %s failed for %s", __FUNCTION__,
748 dir ? "LL_IOC_MDC_GETINFO" :
749 "IOC_MDC_GETFILEINFO", path);
755 if (param->obduuid) {
756 if (lustre_fs && param->got_uuids &&
757 param->st_dev != st->st_dev) {
758 /* A lustre/lustre mount point is crossed. */
759 param->got_uuids = 0;
760 param->obds_printed = 0;
761 param->obdindex = OBD_NOT_FOUND;
764 if (lustre_fs && !param->got_uuids) {
765 ret = setup_obd_uuids(dir ? dir : parent, path, param);
768 param->st_dev = st->st_dev;
769 } else if (!lustre_fs && param->got_uuids) {
770 /* A lustre/non-lustre mount point is crossed. */
771 param->got_uuids = 0;
772 param->obdindex = OBD_NOT_FOUND;
776 /* If an OBD UUID is specified but no one matches, skip this file. */
777 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
780 /* If a OST UUID is given, and some OST matches, check it here. */
781 if (decision != -1 && param->obdindex != OBD_NOT_FOUND) {
782 /* Only those files should be accepted, which have a strip on
783 * the specified OST. */
784 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
789 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
790 if (param->obdindex ==
791 param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
795 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
800 /* Check the time on mds. */
804 for_mds = lustre_fs ? param->lmd->lmd_lmm.lmm_stripe_count : 0;
805 decision = find_time_check(st, param, for_mds);
808 /* If file still fits the request, ask osd for updated info.
809 The regulat stat is almost of the same speed as some new
810 'glimpse-size-ioctl'. */
811 if (!decision && param->lmd->lmd_lmm.lmm_stripe_count) {
813 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
816 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
821 fprintf(stderr, "%s: IOC_LOV_GETINFO on %s failed: "
822 "%s.\n", __FUNCTION__, path, strerror(errno));
826 /* Check the time on osc. */
828 decision = find_time_check(st, param, 0);
831 if (decision != -1) {
839 /* Do not get down anymore? */
840 if (param->depth == param->maxdepth)
847 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data)
849 struct find_param *param = (struct find_param *)data;
854 int llapi_find(char *path, struct find_param *param)
856 char buf[PATH_MAX + 1];
859 ret = common_param_init(param);
864 strncpy(buf, path, strlen(path));
865 buf[strlen(path)] = '\0';
867 ret = llapi_semantic_traverse(buf, NULL, cb_find_init,
868 cb_common_fini, param);
870 find_param_fini(param);
871 return ret < 0 ? ret : 0;
874 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data)
876 struct find_param *param = (struct find_param *)data;
879 LASSERT(parent != NULL || d != NULL);
882 if (!param->got_uuids) {
883 ret = setup_obd_uuids(d ? d : parent, path, param);
889 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
890 (void *)¶m->lmd->lmd_lmm);
892 char *fname = strrchr(path, '/') + 1;
894 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
895 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
896 (void *)¶m->lmd->lmd_lmm);
900 if (errno == ENODATA) {
901 if (!param->obduuid && !param->quiet)
902 printf("%s has no stripe info\n", path);
904 } else if (errno == ENOTTY) {
905 fprintf(stderr, "%s: '%s' not on a Lustre fs?\n",
908 err_msg("error: %s: %s failed for %s", __FUNCTION__,
909 d ? "LL_IOC_LOV_GETSTRIPE" :
910 "IOC_MDC_GETFILESTRIPE", path);
916 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
918 /* Do not get down anymore? */
919 if (param->depth == param->maxdepth)
926 int llapi_getstripe(char *path, struct find_param *param)
930 ret = common_param_init(param);
935 ret = llapi_semantic_traverse(path, NULL, cb_getstripe,
936 cb_common_fini, param);
937 find_param_fini(param);
938 return ret < 0 ? ret : 0;
941 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
942 struct obd_statfs *stat_buf,
943 struct obd_uuid *uuid_buf)
946 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
948 struct obd_ioctl_data data;
951 data.ioc_inlbuf1 = (char *)&type;
952 data.ioc_inllen1 = sizeof(__u32);
953 data.ioc_inlbuf2 = (char *)&index;
954 data.ioc_inllen2 = sizeof(__u32);
955 data.ioc_pbuf1 = (char *)stat_buf;
956 data.ioc_plen1 = sizeof(struct obd_statfs);
957 data.ioc_pbuf2 = (char *)uuid_buf;
958 data.ioc_plen2 = sizeof(struct obd_uuid);
960 if (obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) {
961 fprintf(stderr, "llapi_obd_statfs: error packing ioctl data\n");
965 fd = open(path, O_RDONLY);
967 fd = open(path, O_DIRECTORY | O_RDONLY);
971 err_msg("error: %s: opening '%s'", __FUNCTION__, path);
974 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
982 #define MAX_STRING_SIZE 128
983 #define DEVICES_LIST "/proc/fs/lustre/devices"
985 int llapi_ping(char *obd_type, char *obd_name)
987 char path[MAX_STRING_SIZE];
991 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
994 fd = open(path, O_WRONLY);
996 fprintf(stderr, "error opening %s: %s\n", path, strerror(errno));
1000 rc = write(fd, buf, 1);
1008 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1010 char buf[MAX_STRING_SIZE];
1011 FILE *fp = fopen(DEVICES_LIST, "r");
1015 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
1016 strerror(rc = errno));
1020 while (fgets(buf, sizeof(buf), fp) != NULL) {
1021 char *obd_type_name = NULL;
1022 char *obd_name = NULL;
1023 char *obd_uuid = NULL;
1024 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1025 char *bufl = rawbuf;
1027 struct obd_ioctl_data datal = { 0, };
1028 struct obd_statfs osfs_buffer;
1030 while(bufp[0] == ' ')
1033 for(i = 0; i < 3; i++) {
1034 obd_type_name = strsep(&bufp, " ");
1036 obd_name = strsep(&bufp, " ");
1037 obd_uuid = strsep(&bufp, " ");
1039 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1041 memset(bufl, 0, sizeof(rawbuf));
1042 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1043 datal.ioc_plen1 = sizeof(osfs_buffer);
1045 for (i = 0; i < type_num; i++) {
1046 if (strcmp(obd_type_name, obd_type[i]) != 0)
1049 cb(obd_type_name, obd_name, obd_uuid, args);
1056 static void do_target_check(char *obd_type_name, char *obd_name,
1057 char *obd_uuid, void *args)
1061 rc = llapi_ping(obd_type_name, obd_name);
1063 err_msg("error: check '%s'", obd_name);
1065 printf("%s active.\n", obd_name);
1069 int llapi_target_check(int type_num, char **obd_type, char *dir)
1071 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1074 #undef MAX_STRING_SIZE
1076 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1078 char raw[OBD_MAX_IOCTL_BUFFER];
1079 char out[LLOG_CHUNK_SIZE];
1081 struct obd_ioctl_data data;
1086 sprintf(key, "%s", keyword);
1087 memset(raw, 0, sizeof(raw));
1088 memset(out, 0, sizeof(out));
1089 data.ioc_inlbuf1 = key;
1090 data.ioc_inllen1 = strlen(key) + 1;
1092 data.ioc_inlbuf2 = node_name;
1093 data.ioc_inllen2 = strlen(node_name) + 1;
1095 data.ioc_pbuf1 = out;
1096 data.ioc_plen1 = sizeof(out);
1097 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1101 root = opendir(dir);
1103 err_msg("open %s failed", dir);
1107 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1109 err_msg("ioctl OBD_IOC_CATINFO failed");
1111 fprintf(stdout, "%s", data.ioc_pbuf1);
1117 /* Is this a lustre client fs? */
1118 int llapi_is_lustre_mnttype(struct mntent *mnt)
1120 char *type = mnt->mnt_type;
1121 return ((strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0)
1122 && (strstr(mnt->mnt_fsname, ":/") != NULL));
1125 int llapi_quotacheck(char *mnt, int check_type)
1130 root = opendir(mnt);
1132 err_msg("open %s failed", mnt);
1136 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1142 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1148 root = opendir(mnt);
1150 err_msg("open %s failed", mnt);
1155 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1159 if (poll_intvl < 30)
1167 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1172 root = opendir(mnt);
1174 err_msg("open %s failed", mnt);
1178 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1184 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data)
1186 struct find_param *param = (struct find_param *)data;
1190 LASSERT(parent != NULL || d != NULL);
1193 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1194 (void *)param->lmd);
1195 } else if (parent) {
1196 char *fname = strrchr(path, '/') + 1;
1198 strncpy((char *)param->lmd, fname, param->lumlen);
1199 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1200 (void *)param->lmd);
1206 if (errno == ENODATA) {
1207 if (!param->obduuid && !param->quiet)
1208 fprintf(stderr, "%s has no stripe info\n",
1211 } else if (errno != EISDIR) {
1212 err_msg("%s ioctl failed for %s.",
1213 d ? "LL_IOC_MDC_GETINFO" :
1214 "IOC_MDC_GETFILEINFO", path);
1220 st = ¶m->lmd->lmd_st;
1222 /* libc chown() will do extra check, and if the real owner is
1223 * the same as the ones to set, it won't fall into kernel, so
1224 * invoke syscall directly. */
1225 rc = syscall(SYS_chown, path, st->st_uid, st->st_gid);
1227 err_msg("error: chown %s (%u,%u)",
1228 path, st->st_uid, st->st_gid);
1232 int llapi_quotachown(char *path, int flag)
1234 struct find_param param;
1237 memset(¶m, 0, sizeof(param));
1238 param.recursive = 1;
1242 ret = common_param_init(¶m);
1246 ret = llapi_semantic_traverse(path, NULL, cb_quotachown,
1249 find_param_fini(¶m);