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>
58 #include <lustre/liblustreapi.h>
60 #include <lustre/liblustreapi.h>
62 static void err_msg(char *fmt, ...)
65 int tmp_errno = abs(errno);
68 vfprintf(stderr, fmt, args);
70 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
73 int llapi_file_create(const char *name, unsigned long stripe_size,
74 int stripe_offset, int stripe_count, int stripe_pattern)
76 struct lov_user_md lum = { 0 };
81 fd = open(name, O_CREAT | O_RDWR | O_LOV_DELAY_CREATE, 0644);
82 if (errno == EISDIR) {
83 fd = open(name, O_DIRECTORY | O_RDONLY);
89 err_msg("unable to open '%s'",name);
93 /* 64 KB is the largest common page size I'm aware of (on ia64), but
94 * check the local page size just in case. */
95 page_size = LOV_MIN_STRIPE_SIZE;
96 if (getpagesize() > page_size) {
97 page_size = getpagesize();
98 fprintf(stderr, "warning: your page size (%u) is larger than "
99 "expected (%u).\n", page_size, LOV_MIN_STRIPE_SIZE);
101 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
102 err_msg("error: bad stripe_size %lu, must be an even "
103 "multiple of %d bytes", stripe_size, page_size);
104 errno = rc = -EINVAL;
107 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
108 err_msg("error: bad stripe offset %d", stripe_offset);
109 errno = rc = -EINVAL;
112 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
113 err_msg("error: bad stripe count %d", stripe_count);
114 errno = rc = -EINVAL;
117 if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
118 err_msg("error: stripe_size %lu * stripe_count %u "
119 "exceeds 4GB", stripe_size, stripe_count);
120 errno = rc = -EINVAL;
124 /* Initialize IOCTL striping pattern structure */
125 lum.lmm_magic = LOV_USER_MAGIC;
126 lum.lmm_pattern = stripe_pattern;
127 lum.lmm_stripe_size = stripe_size;
128 lum.lmm_stripe_count = stripe_count;
129 lum.lmm_stripe_offset = stripe_offset;
131 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
132 char *errmsg = "stripe already set";
134 if (errno != EEXIST && errno != EALREADY)
135 errmsg = strerror(errno);
138 fprintf(stderr, "error on ioctl "LPX64" for '%s' (%d): %s\n",
139 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
145 err_msg("error on close for '%s' (%d)", name, fd);
150 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d, void *data);
152 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
153 #define OBD_NOT_FOUND (-1)
155 static int common_param_init(struct find_param *param)
157 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
158 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
159 err_msg("error: allocation of %d bytes for ioctl",
160 sizeof(lstat_t) + param->lumlen);
164 param->got_uuids = 0;
165 param->obdindex = OBD_NOT_FOUND;
169 static void find_param_fini(struct find_param *param)
175 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
177 struct obd_ioctl_data data = { 0, };
178 struct lov_desc desc = { 0, };
180 int max_ost_count, rc;
183 max_ost_count = (OBD_MAX_IOCTL_BUFFER - size_round(sizeof(data)) -
184 size_round(sizeof(desc))) /
185 (sizeof(*uuidp) + sizeof(*obdgens));
186 if (max_ost_count > *ost_count)
187 max_ost_count = *ost_count;
189 obdgens = malloc(size_round(max_ost_count * sizeof(*obdgens)));
191 err_msg("error: %d generation #'s", max_ost_count);
195 data.ioc_inllen1 = sizeof(desc);
196 data.ioc_inlbuf1 = (char *)&desc;
197 data.ioc_inllen2 = size_round(max_ost_count * sizeof(*uuidp));
198 data.ioc_inlbuf2 = (char *)uuidp;
199 data.ioc_inllen3 = size_round(max_ost_count * sizeof(*obdgens));
200 data.ioc_inlbuf3 = (char *)obdgens;
202 desc.ld_tgt_count = max_ost_count;
204 if (obd_ioctl_pack(&data, &buf, OBD_MAX_IOCTL_BUFFER)) {
205 fprintf(stderr, "error: %s: internal packing error\n",
211 rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
214 err_msg("error: %s: getting LOV config", __FUNCTION__);
218 if (obd_ioctl_unpack(&data, buf, OBD_MAX_IOCTL_BUFFER)) {
220 err_msg("error: %s: internal ioctl unpack", __FUNCTION__);
224 *ost_count = desc.ld_tgt_count;
232 static int setup_obd_uuids(DIR *dir, char *dname, struct find_param *param)
234 char uuid[sizeof(struct obd_uuid)];
239 /* Get the lov name */
240 rc = ioctl(dirfd(dir), OBD_IOC_GETNAME, (void *)uuid);
242 if (errno != ENOTTY) {
244 err_msg("error: can't get lov name: %s", dname);
251 param->got_uuids = 1;
253 /* Now get the ost uuids from /proc */
254 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
256 fp = fopen(buf, "r");
259 err_msg("error: opening '%s'", buf);
263 if (!param->obduuid && !param->quiet && !param->obds_printed)
266 while (fgets(buf, sizeof(buf), fp) != NULL) {
267 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
270 if (param->obduuid) {
271 if (strncmp(param->obduuid->uuid, uuid,
272 sizeof(uuid)) == 0) {
273 param->obdindex = index;
276 } else if (!param->quiet && !param->obds_printed) {
277 /* Print everything */
281 param->obds_printed = 1;
285 if (!param->quiet && param->obduuid &&
286 (param->obdindex == OBD_NOT_FOUND)) {
287 fprintf(stderr, "error: %s: unknown obduuid: %s\n",
288 __FUNCTION__, param->obduuid->uuid);
295 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
296 int obdindex, int quiet, int header, int body)
298 int i, obdstripe = 0;
300 if (obdindex != OBD_NOT_FOUND) {
301 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
302 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
303 printf("%s\n", path);
309 printf("%s\n", path);
313 /* if it's a directory */
315 if (obdstripe == 1) {
316 printf("default stripe_count: %d stripe_size: %u "
317 "stripe_offset: %d\n",
318 lum->lmm_stripe_count == (__u16)-1 ? -1 :
319 lum->lmm_stripe_count,
320 lum->lmm_stripe_size,
321 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
322 lum->lmm_stripe_offset);
327 if (header && (obdstripe == 1)) {
328 printf("lmm_magic: 0x%08X\n", lum->lmm_magic);
329 printf("lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
330 printf("lmm_object_id: "LPX64"\n", lum->lmm_object_id);
331 printf("lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
332 printf("lmm_stripe_size: %u\n", lum->lmm_stripe_size);
333 printf("lmm_stripe_pattern: %x\n", lum->lmm_pattern);
337 if ((!quiet) && (obdstripe == 1))
338 printf("\tobdidx\t\t objid\t\tobjid\t\t group\n");
340 for (i = 0; i < lum->lmm_stripe_count; i++) {
341 int idx = lum->lmm_objects[i].l_ost_idx;
342 long long oid = lum->lmm_objects[i].l_object_id;
343 long long gr = lum->lmm_objects[i].l_object_gr;
344 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
345 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
347 obdindex == idx ? " *" : "");
353 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
354 int is_dir, int obdindex, int quiet,
355 int header, int body)
357 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
358 int i, obdstripe = 0;
360 if (obdindex != OBD_NOT_FOUND) {
361 for (i = 0; i < lumj->lmm_stripe_count; i++) {
362 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
363 printf("%s\n", path);
369 printf("%s\n", path);
373 if (header && obdstripe == 1) {
374 printf("lmm_magic: 0x%08X\n", lumj->lmm_magic);
375 printf("lmm_object_gr: "LPX64"\n", lumj->lmm_object_gr);
376 printf("lmm_object_id: "LPX64"\n", lumj->lmm_object_id);
377 printf("lmm_stripe_count: %u\n", (int)lumj->lmm_stripe_count);
378 printf("lmm_stripe_size: %u\n", lumj->lmm_stripe_size);
379 printf("lmm_stripe_pattern: %x\n", lumj->lmm_pattern);
380 printf("lmm_extent_count: %x\n", lumj->lmm_extent_count);
384 unsigned long long start = -1, end = 0;
385 if (!quiet && obdstripe == 1)
386 printf("joined\tobdidx\t\t objid\t\tobjid\t\t group"
387 "\t\tstart\t\tend\n");
388 for (i = 0; i < lumj->lmm_stripe_count; i++) {
389 int idx = lumj->lmm_objects[i].l_ost_idx;
390 long long oid = lumj->lmm_objects[i].l_object_id;
391 long long gr = lumj->lmm_objects[i].l_object_gr;
392 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
393 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s",
395 obdindex == idx ? " *" : "");
396 if (start != lumj->lmm_objects[i].l_extent_start ||
397 end != lumj->lmm_objects[i].l_extent_end) {
398 start = lumj->lmm_objects[i].l_extent_start;
399 printf("\t%14llu", start);
400 end = lumj->lmm_objects[i].l_extent_end;
401 if (end == (unsigned long long)-1)
404 printf("\t\t%llu\n", end);
406 printf("\t\t\t\t\n");
413 void llapi_lov_dump_user_lmm(struct find_param *param,
414 char *path, int is_dir)
416 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
417 case LOV_USER_MAGIC_V1:
418 lov_dump_user_lmm_v1(¶m->lmd->lmd_lmm, path, is_dir,
419 param->obdindex, param->quiet,
421 (param->verbose || !param->obduuid));
423 case LOV_USER_MAGIC_JOIN:
424 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
425 param->obdindex, param->quiet,
427 (param->verbose || !param->obduuid));
430 printf("unknown lmm_magic: %#x (expecting %#x)\n",
431 *(__u32 *)¶m->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
436 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
442 fname = strrchr(path, '/');
444 /* It should be a file (or other non-directory) */
446 dname = (char *)malloc(2);
450 fname = (char *)path;
452 dname = (char *)malloc(fname - path + 1);
455 strncpy(dname, path, fname - path);
456 dname[fname - path] = '\0';
460 if ((fd = open(dname, O_RDONLY)) == -1) {
466 strcpy((char *)lum, fname);
467 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
470 if (close(fd) == -1 && rc == 0)
478 int llapi_file_lookup(int dirfd, const char *name)
480 struct obd_ioctl_data data = { 0 };
485 if (dirfd < 0 || name == NULL)
488 data.ioc_version = OBD_IOCTL_VERSION;
489 data.ioc_len = sizeof(data);
490 data.ioc_inlbuf1 = (char *)name;
491 data.ioc_inllen1 = strlen(name) + 1;
493 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
496 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d\n",
501 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
504 /* some 64bit libcs implement readdir64() by calling sys_getdents(). the
505 * kernel's sys_getdents() doesn't return d_type. */
506 unsigned char handle_dt_unknown(char *path)
510 fd = open(path, O_DIRECTORY|O_RDONLY);
512 if (errno == ENOTDIR)
513 return DT_REG; /* kind of a lie */
520 static DIR *opendir_parent(char *path)
526 fname = strrchr(path, '/');
532 parent = opendir(path);
537 static int llapi_semantic_traverse(char *path, DIR *parent,
538 semantic_func_t sem_init,
539 semantic_func_t sem_fini, void *data)
541 struct dirent64 *dent;
549 if (!d && errno != ENOTDIR) {
550 fprintf(stderr, "%s: Failed to open '%s': %s.",
551 __FUNCTION__, path, strerror(errno));
553 } else if (!d && !parent) {
554 /* ENOTDIR. Open the parent dir. */
555 p = opendir_parent(path);
557 GOTO(out, ret = -EINVAL);
560 if (sem_init && (ret = sem_init(path, parent ?: p, d, data)))
566 while ((dent = readdir64(d)) != NULL) {
567 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
572 strcat(path, dent->d_name);
574 if (dent->d_type == DT_UNKNOWN)
575 dent->d_type = handle_dt_unknown(path);
577 switch (dent->d_type) {
579 fprintf(stderr, "error: %s: '%s' is UNKNOWN type %d",
580 __FUNCTION__, dent->d_name, dent->d_type);
581 /* If we cared we could stat the file to determine
582 * type and continue on here, but we don't since we
583 * know d_type should be valid for lustre and this
584 * tool only makes sense for lustre filesystems. */
587 ret = llapi_semantic_traverse(path, d, sem_init,
595 ret = sem_init(path, d, NULL, data);
599 if (sem_fini && ret == 0)
600 sem_fini(path, d, NULL, data);
608 sem_fini(path, parent, d, data);
617 /* Check if the file time matches 1 of the given criteria (e.g. --atime +/-N).
618 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
620 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
621 * The table bolow gives the answers for the specified parameters (time and
622 * sign), 1st column is the answer for the MDS time, the 2nd is for the OST:
623 * --------------------------------------
624 * 1 | file > limit; sign > 0 | -1 / -1 |
625 * 2 | file = limit; sign > 0 | ? / 1 |
626 * 3 | file < limit; sign > 0 | ? / 1 |
627 * 4 | file > limit; sign = 0 | -1 / -1 |
628 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
629 * 6 | file < limit; sign = 0 | ? / -1 |
630 * 7 | file > limit; sign < 0 | 1 / 1 |
631 * 8 | file = limit; sign < 0 | ? / -1 |
632 * 9 | file < limit; sign < 0 | ? / -1 |
633 * --------------------------------------
634 * Note: 5th actually means that the file time stamp is within the interval
635 * (limit - 24hours, limit]. */
636 static int find_time_cmp(time_t file, time_t limit, int sign, int mds) {
643 if (file <= limit && file + 24 * 60 * 60 > limit)
645 if (file + 24 * 60 * 60 <= limit)
659 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
660 * Return -1 or 1 if file timestamp does not or does match the given criteria
661 * correspondingly. Return 0 if the MDS time is being checked and there are
662 * attributes on OSTs and it is not yet clear if the timespamp matches.
664 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
665 * updated timestamps. */
666 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
671 /* Check if file is accepted. */
673 ret = find_time_cmp(st->st_atime, param->atime,
681 ret = find_time_cmp(st->st_mtime, param->mtime,
686 /* If the previous check matches, but this one is not yet clear,
687 * we should return 0 to do an RPC on OSTs. */
693 ret = find_time_cmp(st->st_ctime, param->ctime,
698 /* If the previous check matches, but this one is not yet clear,
699 * we should return 0 to do an RPC on OSTs. */
707 static int cb_find_init(char *path, DIR *parent, DIR *dir, void *data)
709 struct find_param *param = (struct find_param *)data;
710 int decision = 1; /* 1 is accepted; -1 is rejected. */
711 lstat_t *st = ¶m->lmd->lmd_st;
715 LASSERT(parent != NULL || dir != NULL);
717 param->lmd->lmd_lmm.lmm_stripe_count = 0;
719 /* If a time or OST should be checked, the decision is not taken yet. */
720 if (param->atime || param->ctime || param->mtime || param->obduuid)
723 /* Request MDS for the stat info. */
724 if (!decision && dir) {
725 /* retrieve needed file info */
726 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
728 } else if (!decision && parent) {
729 char *fname = strrchr(path, '/');
730 fname = (fname == NULL ? path : fname + 1);
732 /* retrieve needed file info */
733 strncpy((char *)param->lmd, fname, param->lumlen);
734 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
739 if (errno == ENOTTY) {
740 /* ioctl is not supported, it is not a lustre fs.
741 * Do the regular lstat(2) instead. */
743 ret = lstat_f(path, st);
745 err_msg("error: %s: lstat failed for %s",
750 err_msg("error: %s: %s failed for %s", __FUNCTION__,
751 dir ? "LL_IOC_MDC_GETINFO" :
752 "IOC_MDC_GETFILEINFO", path);
758 if (param->obduuid) {
759 if (lustre_fs && param->got_uuids &&
760 param->st_dev != st->st_dev) {
761 /* A lustre/lustre mount point is crossed. */
762 param->got_uuids = 0;
763 param->obds_printed = 0;
764 param->obdindex = OBD_NOT_FOUND;
767 if (lustre_fs && !param->got_uuids) {
768 ret = setup_obd_uuids(dir ? dir : parent, path, param);
771 param->st_dev = st->st_dev;
772 } else if (!lustre_fs && param->got_uuids) {
773 /* A lustre/non-lustre mount point is crossed. */
774 param->got_uuids = 0;
775 param->obdindex = OBD_NOT_FOUND;
779 /* If a regular expression is presented, make the initial decision */
780 if (param->pattern != NULL) {
781 char *fname = strrchr(path, '/');
782 fname = (fname == NULL ? path : fname + 1);
783 ret = fnmatch(param->pattern, fname, 0);
784 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
785 (ret == 0 && param->exclude_pattern))
789 /* If an OBD UUID is specified but no one matches, skip this file. */
790 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
793 /* If a OST UUID is given, and some OST matches, check it here. */
794 if (decision != -1 && param->obdindex != OBD_NOT_FOUND) {
795 /* Only those files should be accepted, which have a strip on
796 * the specified OST. */
797 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
802 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
803 if (param->obdindex ==
804 param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
808 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
813 /* Check the time on mds. */
817 for_mds = lustre_fs ? param->lmd->lmd_lmm.lmm_stripe_count : 0;
818 decision = find_time_check(st, param, for_mds);
821 /* If file still fits the request, ask osd for updated info.
822 The regulat stat is almost of the same speed as some new
823 'glimpse-size-ioctl'. */
824 if (!decision && param->lmd->lmd_lmm.lmm_stripe_count) {
826 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
829 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
834 fprintf(stderr, "%s: IOC_LOV_GETINFO on %s failed: "
835 "%s.\n", __FUNCTION__, path, strerror(errno));
839 /* Check the time on osc. */
841 decision = find_time_check(st, param, 0);
844 if (decision != -1) {
852 /* Do not get down anymore? */
853 if (param->depth == param->maxdepth)
860 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data)
862 struct find_param *param = (struct find_param *)data;
867 int llapi_find(char *path, struct find_param *param)
869 char buf[PATH_MAX + 1];
872 ret = common_param_init(param);
877 strncpy(buf, path, strlen(path));
878 buf[strlen(path)] = '\0';
880 ret = llapi_semantic_traverse(buf, NULL, cb_find_init,
881 cb_common_fini, param);
883 find_param_fini(param);
884 return ret < 0 ? ret : 0;
887 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data)
889 struct find_param *param = (struct find_param *)data;
892 LASSERT(parent != NULL || d != NULL);
895 if (!param->got_uuids) {
896 ret = setup_obd_uuids(d ? d : parent, path, param);
902 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
903 (void *)¶m->lmd->lmd_lmm);
905 char *fname = strrchr(path, '/');
906 fname = (fname == NULL ? path : fname + 1);
908 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
909 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
910 (void *)¶m->lmd->lmd_lmm);
914 if (errno == ENODATA) {
915 if (!param->obduuid && !param->quiet)
916 printf("%s has no stripe info\n", path);
918 } else if (errno == ENOTTY) {
919 fprintf(stderr, "%s: '%s' not on a Lustre fs?\n",
922 err_msg("error: %s: %s failed for %s", __FUNCTION__,
923 d ? "LL_IOC_LOV_GETSTRIPE" :
924 "IOC_MDC_GETFILESTRIPE", path);
930 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
932 /* Do not get down anymore? */
933 if (param->depth == param->maxdepth)
940 int llapi_getstripe(char *path, struct find_param *param)
944 ret = common_param_init(param);
949 ret = llapi_semantic_traverse(path, NULL, cb_getstripe,
950 cb_common_fini, param);
951 find_param_fini(param);
952 return ret < 0 ? ret : 0;
955 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
956 struct obd_statfs *stat_buf,
957 struct obd_uuid *uuid_buf)
960 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
962 struct obd_ioctl_data data = { 0 };
965 data.ioc_inlbuf1 = (char *)&type;
966 data.ioc_inllen1 = sizeof(__u32);
967 data.ioc_inlbuf2 = (char *)&index;
968 data.ioc_inllen2 = sizeof(__u32);
969 data.ioc_pbuf1 = (char *)stat_buf;
970 data.ioc_plen1 = sizeof(struct obd_statfs);
971 data.ioc_pbuf2 = (char *)uuid_buf;
972 data.ioc_plen2 = sizeof(struct obd_uuid);
974 if (obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) {
975 fprintf(stderr, "llapi_obd_statfs: error packing ioctl data\n");
979 fd = open(path, O_RDONLY);
981 fd = open(path, O_DIRECTORY | O_RDONLY);
984 rc = errno ? -errno : -EBADF;
985 err_msg("error: %s: opening '%s'", __FUNCTION__, path);
988 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
990 rc = errno ? -errno : -EINVAL;
996 #define MAX_STRING_SIZE 128
997 #define DEVICES_LIST "/proc/fs/lustre/devices"
999 int llapi_ping(char *obd_type, char *obd_name)
1001 char path[MAX_STRING_SIZE];
1005 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1006 obd_type, obd_name);
1008 fd = open(path, O_WRONLY);
1011 fprintf(stderr, "error opening %s: %s\n", path, strerror(errno));
1015 rc = write(fd, buf, 1);
1023 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1025 char buf[MAX_STRING_SIZE];
1026 FILE *fp = fopen(DEVICES_LIST, "r");
1031 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
1036 while (fgets(buf, sizeof(buf), fp) != NULL) {
1037 char *obd_type_name = NULL;
1038 char *obd_name = NULL;
1039 char *obd_uuid = NULL;
1040 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1041 char *bufl = rawbuf;
1043 struct obd_ioctl_data datal = { 0, };
1044 struct obd_statfs osfs_buffer;
1046 while(bufp[0] == ' ')
1049 for(i = 0; i < 3; i++) {
1050 obd_type_name = strsep(&bufp, " ");
1052 obd_name = strsep(&bufp, " ");
1053 obd_uuid = strsep(&bufp, " ");
1055 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1057 memset(bufl, 0, sizeof(rawbuf));
1058 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1059 datal.ioc_plen1 = sizeof(osfs_buffer);
1061 for (i = 0; i < type_num; i++) {
1062 if (strcmp(obd_type_name, obd_type[i]) != 0)
1065 cb(obd_type_name, obd_name, obd_uuid, args);
1072 static void do_target_check(char *obd_type_name, char *obd_name,
1073 char *obd_uuid, void *args)
1077 rc = llapi_ping(obd_type_name, obd_name);
1079 err_msg("error: check '%s'", obd_name);
1081 printf("%s active.\n", obd_name);
1085 int llapi_target_check(int type_num, char **obd_type, char *dir)
1087 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1090 #undef MAX_STRING_SIZE
1092 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1094 char raw[OBD_MAX_IOCTL_BUFFER];
1095 char out[LLOG_CHUNK_SIZE];
1097 struct obd_ioctl_data data = { 0 };
1102 sprintf(key, "%s", keyword);
1103 memset(raw, 0, sizeof(raw));
1104 memset(out, 0, sizeof(out));
1105 data.ioc_inlbuf1 = key;
1106 data.ioc_inllen1 = strlen(key) + 1;
1108 data.ioc_inlbuf2 = node_name;
1109 data.ioc_inllen2 = strlen(node_name) + 1;
1111 data.ioc_pbuf1 = out;
1112 data.ioc_plen1 = sizeof(out);
1113 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1117 root = opendir(dir);
1120 err_msg("open %s failed", dir);
1124 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1126 err_msg("ioctl OBD_IOC_CATINFO failed");
1128 fprintf(stdout, "%s", data.ioc_pbuf1);
1134 /* Is this a lustre client fs? */
1135 int llapi_is_lustre_mnttype(struct mntent *mnt)
1137 char *type = mnt->mnt_type;
1138 return ((strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0)
1139 && (strstr(mnt->mnt_fsname, ":/") != NULL));
1142 int llapi_quotacheck(char *mnt, int check_type)
1147 root = opendir(mnt);
1149 err_msg("open %s failed", mnt);
1153 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1159 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1165 root = opendir(mnt);
1167 err_msg("open %s failed", mnt);
1172 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1176 if (poll_intvl < 30)
1184 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1189 root = opendir(mnt);
1191 err_msg("open %s failed", mnt);
1195 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1201 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data)
1203 struct find_param *param = (struct find_param *)data;
1207 LASSERT(parent != NULL || d != NULL);
1210 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1211 (void *)param->lmd);
1212 } else if (parent) {
1213 char *fname = strrchr(path, '/');
1214 fname = (fname == NULL ? path : fname + 1);
1216 strncpy((char *)param->lmd, fname, param->lumlen);
1217 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1218 (void *)param->lmd);
1224 if (errno == ENODATA) {
1225 if (!param->obduuid && !param->quiet)
1226 fprintf(stderr, "%s has no stripe info\n",
1229 } else if (errno != EISDIR) {
1231 err_msg("%s ioctl failed for %s.",
1232 d ? "LL_IOC_MDC_GETINFO" :
1233 "IOC_MDC_GETFILEINFO", path);
1238 st = ¶m->lmd->lmd_st;
1240 /* libc chown() will do extra check, and if the real owner is
1241 * the same as the ones to set, it won't fall into kernel, so
1242 * invoke syscall directly. */
1243 rc = syscall(SYS_chown, path, st->st_uid, st->st_gid);
1245 err_msg("error: chown %s (%u,%u)",
1246 path, st->st_uid, st->st_gid);
1250 int llapi_quotachown(char *path, int flag)
1252 struct find_param param;
1255 memset(¶m, 0, sizeof(param));
1256 param.recursive = 1;
1260 ret = common_param_init(¶m);
1264 ret = llapi_semantic_traverse(path, NULL, cb_quotachown,
1267 find_param_fini(¶m);