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 <lnet/lnetctl.h>
54 #include <liblustre.h>
56 #include <lustre_lib.h>
58 #include <lustre/liblustreapi.h>
60 static void err_msg(char *fmt, ...)
63 int tmp_errno = abs(errno);
66 vfprintf(stderr, fmt, args);
68 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
71 int llapi_file_open(const char *name, int flags, int mode,
72 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, flags | O_LOV_DELAY_CREATE, mode);
81 if (fd < 0 && 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);
136 fprintf(stderr, "error on ioctl "LPX64" for '%s' (%d): %s\n",
137 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
148 int llapi_file_create(const char *name, unsigned long stripe_size,
149 int stripe_offset, int stripe_count, int stripe_pattern)
153 fd = llapi_file_open(name, O_CREAT | O_WRONLY, 0644, stripe_size,
154 stripe_offset, stripe_count, stripe_pattern);
162 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d, void *data);
164 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
165 #define OBD_NOT_FOUND (-1)
167 static int common_param_init(struct find_param *param)
169 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
170 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
171 err_msg("error: allocation of %d bytes for ioctl",
172 sizeof(lstat_t) + param->lumlen);
176 param->got_uuids = 0;
177 param->obdindex = OBD_NOT_FOUND;
181 static void find_param_fini(struct find_param *param)
187 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
189 char lov_name[sizeof(struct obd_uuid)];
192 int rc = 0, index = 0;
194 /* Get the lov name */
195 rc = ioctl(fd, OBD_IOC_GETNAME, (void *) lov_name);
198 err_msg("error: can't get lov name.");
202 /* Now get the ost uuids from /proc */
203 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
205 fp = fopen(buf, "r");
208 err_msg("error: opening '%s'", buf);
212 while ((fgets(buf, sizeof(buf), fp) != NULL) && index < *ost_count) {
213 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) < 2)
224 static int setup_obd_uuids(DIR *dir, char *dname, struct find_param *param)
226 char uuid[sizeof(struct obd_uuid)];
231 /* Get the lov name */
232 rc = ioctl(dirfd(dir), OBD_IOC_GETNAME, (void *)uuid);
234 if (errno != ENOTTY) {
236 err_msg("error: can't get lov name: %s", dname);
243 param->got_uuids = 1;
245 /* Now get the ost uuids from /proc */
246 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
248 fp = fopen(buf, "r");
251 err_msg("error: opening '%s'", buf);
255 if (!param->obduuid && !param->quiet && !param->obds_printed)
258 while (fgets(buf, sizeof(buf), fp) != NULL) {
259 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
262 if (param->obduuid) {
263 if (strncmp(param->obduuid->uuid, uuid,
264 sizeof(uuid)) == 0) {
265 param->obdindex = index;
268 } else if (!param->quiet && !param->obds_printed) {
269 /* Print everything */
273 param->obds_printed = 1;
277 if (!param->quiet && param->obduuid &&
278 (param->obdindex == OBD_NOT_FOUND)) {
279 fprintf(stderr, "error: %s: unknown obduuid: %s\n",
280 __FUNCTION__, param->obduuid->uuid);
287 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
288 int obdindex, int quiet, int header, int body)
290 int i, obdstripe = 0;
292 if (obdindex != OBD_NOT_FOUND) {
293 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
294 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
295 printf("%s\n", path);
301 printf("%s\n", path);
305 /* if it's a directory */
307 if (obdstripe == 1) {
308 printf("default stripe_count: %d stripe_size: %u "
309 "stripe_offset: %d\n",
310 lum->lmm_stripe_count == (__u16)-1 ? -1 :
311 lum->lmm_stripe_count,
312 lum->lmm_stripe_size,
313 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
314 lum->lmm_stripe_offset);
319 if (header && (obdstripe == 1)) {
320 printf("lmm_magic: 0x%08X\n", lum->lmm_magic);
321 printf("lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
322 printf("lmm_object_id: "LPX64"\n", lum->lmm_object_id);
323 printf("lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
324 printf("lmm_stripe_size: %u\n", lum->lmm_stripe_size);
325 printf("lmm_stripe_pattern: %x\n", lum->lmm_pattern);
329 if ((!quiet) && (obdstripe == 1))
330 printf("\tobdidx\t\t objid\t\tobjid\t\t group\n");
332 for (i = 0; i < lum->lmm_stripe_count; i++) {
333 int idx = lum->lmm_objects[i].l_ost_idx;
334 long long oid = lum->lmm_objects[i].l_object_id;
335 long long gr = lum->lmm_objects[i].l_object_gr;
336 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
337 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
339 obdindex == idx ? " *" : "");
345 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
346 int is_dir, int obdindex, int quiet,
347 int header, int body)
349 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
350 int i, obdstripe = 0;
352 if (obdindex != OBD_NOT_FOUND) {
353 for (i = 0; i < lumj->lmm_stripe_count; i++) {
354 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
355 printf("%s\n", path);
361 printf("%s\n", path);
365 if (header && obdstripe == 1) {
366 printf("lmm_magic: 0x%08X\n", lumj->lmm_magic);
367 printf("lmm_object_gr: "LPX64"\n", lumj->lmm_object_gr);
368 printf("lmm_object_id: "LPX64"\n", lumj->lmm_object_id);
369 printf("lmm_stripe_count: %u\n", (int)lumj->lmm_stripe_count);
370 printf("lmm_stripe_size: %u\n", lumj->lmm_stripe_size);
371 printf("lmm_stripe_pattern: %x\n", lumj->lmm_pattern);
372 printf("lmm_extent_count: %x\n", lumj->lmm_extent_count);
376 unsigned long long start = -1, end = 0;
377 if (!quiet && obdstripe == 1)
378 printf("joined\tobdidx\t\t objid\t\tobjid\t\t group"
379 "\t\tstart\t\tend\n");
380 for (i = 0; i < lumj->lmm_stripe_count; i++) {
381 int idx = lumj->lmm_objects[i].l_ost_idx;
382 long long oid = lumj->lmm_objects[i].l_object_id;
383 long long gr = lumj->lmm_objects[i].l_object_gr;
384 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
385 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s",
387 obdindex == idx ? " *" : "");
388 if (start != lumj->lmm_objects[i].l_extent_start ||
389 end != lumj->lmm_objects[i].l_extent_end) {
390 start = lumj->lmm_objects[i].l_extent_start;
391 printf("\t%14llu", start);
392 end = lumj->lmm_objects[i].l_extent_end;
393 if (end == (unsigned long long)-1)
396 printf("\t\t%llu\n", end);
398 printf("\t\t\t\t\n");
405 void llapi_lov_dump_user_lmm(struct find_param *param,
406 char *path, int is_dir)
408 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
409 case LOV_USER_MAGIC_V1:
410 lov_dump_user_lmm_v1(¶m->lmd->lmd_lmm, path, is_dir,
411 param->obdindex, param->quiet,
413 (param->verbose || !param->obduuid));
415 case LOV_USER_MAGIC_JOIN:
416 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
417 param->obdindex, param->quiet,
419 (param->verbose || !param->obduuid));
422 printf("unknown lmm_magic: %#x (expecting %#x)\n",
423 *(__u32 *)¶m->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
428 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
434 fname = strrchr(path, '/');
436 /* It should be a file (or other non-directory) */
438 dname = (char *)malloc(2);
442 fname = (char *)path;
444 dname = (char *)malloc(fname - path + 1);
447 strncpy(dname, path, fname - path);
448 dname[fname - path] = '\0';
452 if ((fd = open(dname, O_RDONLY)) == -1) {
458 strcpy((char *)lum, fname);
459 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
462 if (close(fd) == -1 && rc == 0)
470 int llapi_file_lookup(int dirfd, const char *name)
472 struct obd_ioctl_data data = { 0 };
477 if (dirfd < 0 || name == NULL)
480 data.ioc_version = OBD_IOCTL_VERSION;
481 data.ioc_len = sizeof(data);
482 data.ioc_inlbuf1 = (char *)name;
483 data.ioc_inllen1 = strlen(name) + 1;
485 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
488 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d\n",
493 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
496 /* some 64bit libcs implement readdir64() by calling sys_getdents(). the
497 * kernel's sys_getdents() doesn't return d_type. */
498 unsigned char handle_dt_unknown(char *path)
502 fd = open(path, O_DIRECTORY|O_RDONLY);
504 if (errno == ENOTDIR)
505 return DT_REG; /* kind of a lie */
512 static DIR *opendir_parent(char *path)
518 fname = strrchr(path, '/');
524 parent = opendir(path);
529 static int llapi_semantic_traverse(char *path, DIR *parent,
530 semantic_func_t sem_init,
531 semantic_func_t sem_fini, void *data)
533 struct dirent64 *dent;
541 if (!d && errno != ENOTDIR) {
542 fprintf(stderr, "%s: Failed to open '%s': %s.",
543 __FUNCTION__, path, strerror(errno));
545 } else if (!d && !parent) {
546 /* ENOTDIR. Open the parent dir. */
547 p = opendir_parent(path);
549 GOTO(out, ret = -EINVAL);
552 if (sem_init && (ret = sem_init(path, parent ?: p, d, data)))
558 while ((dent = readdir64(d)) != NULL) {
559 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
564 strcat(path, dent->d_name);
566 if (dent->d_type == DT_UNKNOWN)
567 dent->d_type = handle_dt_unknown(path);
569 switch (dent->d_type) {
571 fprintf(stderr, "error: %s: '%s' is UNKNOWN type %d",
572 __FUNCTION__, dent->d_name, dent->d_type);
573 /* If we cared we could stat the file to determine
574 * type and continue on here, but we don't since we
575 * know d_type should be valid for lustre and this
576 * tool only makes sense for lustre filesystems. */
579 ret = llapi_semantic_traverse(path, d, sem_init,
587 ret = sem_init(path, d, NULL, data);
591 if (sem_fini && ret == 0)
592 sem_fini(path, d, NULL, data);
600 sem_fini(path, parent, d, data);
609 /* Check if the file time matches 1 of the given criteria (e.g. --atime +/-N).
610 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
612 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
613 * The table bolow gives the answers for the specified parameters (time and
614 * sign), 1st column is the answer for the MDS time, the 2nd is for the OST:
615 * --------------------------------------
616 * 1 | file > limit; sign > 0 | -1 / -1 |
617 * 2 | file = limit; sign > 0 | ? / 1 |
618 * 3 | file < limit; sign > 0 | ? / 1 |
619 * 4 | file > limit; sign = 0 | -1 / -1 |
620 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
621 * 6 | file < limit; sign = 0 | ? / -1 |
622 * 7 | file > limit; sign < 0 | 1 / 1 |
623 * 8 | file = limit; sign < 0 | ? / -1 |
624 * 9 | file < limit; sign < 0 | ? / -1 |
625 * --------------------------------------
626 * Note: 5th actually means that the file time stamp is within the interval
627 * (limit - 24hours, limit]. */
628 static int find_time_cmp(time_t file, time_t limit, int sign, int mds) {
635 if (file <= limit && file + 24 * 60 * 60 > limit)
637 if (file + 24 * 60 * 60 <= limit)
651 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
652 * Return -1 or 1 if file timestamp does not or does match the given criteria
653 * correspondingly. Return 0 if the MDS time is being checked and there are
654 * attributes on OSTs and it is not yet clear if the timespamp matches.
656 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
657 * updated timestamps. */
658 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
663 /* Check if file is accepted. */
665 ret = find_time_cmp(st->st_atime, param->atime,
673 ret = find_time_cmp(st->st_mtime, param->mtime,
678 /* If the previous check matches, but this one is not yet clear,
679 * we should return 0 to do an RPC on OSTs. */
685 ret = find_time_cmp(st->st_ctime, param->ctime,
690 /* If the previous check matches, but this one is not yet clear,
691 * we should return 0 to do an RPC on OSTs. */
699 static int cb_find_init(char *path, DIR *parent, DIR *dir, void *data)
701 struct find_param *param = (struct find_param *)data;
702 int decision = 1; /* 1 is accepted; -1 is rejected. */
703 lstat_t *st = ¶m->lmd->lmd_st;
707 LASSERT(parent != NULL || dir != NULL);
709 param->lmd->lmd_lmm.lmm_stripe_count = 0;
711 /* If a time or OST should be checked, the decision is not taken yet. */
712 if (param->atime || param->ctime || param->mtime || param->obduuid)
715 /* Request MDS for the stat info. */
716 if (!decision && dir) {
717 /* retrieve needed file info */
718 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
720 } else if (!decision && parent) {
721 char *fname = strrchr(path, '/');
722 fname = (fname == NULL ? path : fname + 1);
724 /* retrieve needed file info */
725 strncpy((char *)param->lmd, fname, param->lumlen);
726 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
731 if (errno == ENOTTY) {
732 /* ioctl is not supported, it is not a lustre fs.
733 * Do the regular lstat(2) instead. */
735 ret = lstat_f(path, st);
737 err_msg("error: %s: lstat failed for %s",
742 err_msg("error: %s: %s failed for %s", __FUNCTION__,
743 dir ? "LL_IOC_MDC_GETINFO" :
744 "IOC_MDC_GETFILEINFO", path);
750 if (param->obduuid) {
751 if (lustre_fs && param->got_uuids &&
752 param->st_dev != st->st_dev) {
753 /* A lustre/lustre mount point is crossed. */
754 param->got_uuids = 0;
755 param->obds_printed = 0;
756 param->obdindex = OBD_NOT_FOUND;
759 if (lustre_fs && !param->got_uuids) {
760 ret = setup_obd_uuids(dir ? dir : parent, path, param);
763 param->st_dev = st->st_dev;
764 } else if (!lustre_fs && param->got_uuids) {
765 /* A lustre/non-lustre mount point is crossed. */
766 param->got_uuids = 0;
767 param->obdindex = OBD_NOT_FOUND;
771 /* If a regular expression is presented, make the initial decision */
772 if (param->pattern != NULL) {
773 char *fname = strrchr(path, '/');
774 fname = (fname == NULL ? path : fname + 1);
775 ret = fnmatch(param->pattern, fname, 0);
776 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
777 (ret == 0 && param->exclude_pattern))
781 /* If an OBD UUID is specified but no one matches, skip this file. */
782 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
785 /* If a OST UUID is given, and some OST matches, check it here. */
786 if (decision != -1 && param->obdindex != OBD_NOT_FOUND) {
787 if (!S_ISREG(st->st_mode))
790 /* Only those files should be accepted, which have a
791 * stripe on the specified OST. */
792 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
797 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
798 if (param->obdindex ==
799 param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
803 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
808 /* Check the time on mds. */
812 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
813 param->lmd->lmd_lmm.lmm_stripe_count)
815 decision = find_time_check(st, param, for_mds);
818 /* If file still fits the request, ask osd for updated info.
819 The regulat stat is almost of the same speed as some new
820 'glimpse-size-ioctl'. */
821 if (!decision && param->lmd->lmd_lmm.lmm_stripe_count &&
822 S_ISREG(st->st_mode)) {
823 if (param->obdindex != OBD_NOT_FOUND) {
824 /* Check whether the obd is active or not, if it is
825 * not active, just print the object affected by this
828 struct obd_statfs stat_buf;
829 struct obd_uuid uuid_buf;
831 memset(&stat_buf, 0, sizeof(struct obd_statfs));
832 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
833 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
834 param->obdindex, &stat_buf,
837 if (ret == -ENODATA || ret == -ENODEV
840 printf("obd_uuid: %s failed %s ",
841 param->obduuid->uuid, strerror(errno));
846 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
849 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
854 fprintf(stderr, "%s: IOC_LOV_GETINFO on %s failed: "
855 "%s.\n", __FUNCTION__, path, strerror(errno));
859 /* Check the time on osc. */
861 decision = find_time_check(st, param, 0);
865 if (decision != -1) {
874 /* Do not get down anymore? */
875 if (param->depth == param->maxdepth)
882 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data)
884 struct find_param *param = (struct find_param *)data;
889 int llapi_find(char *path, struct find_param *param)
891 char buf[PATH_MAX + 1];
894 ret = common_param_init(param);
899 strncpy(buf, path, strlen(path));
900 buf[strlen(path)] = '\0';
902 ret = llapi_semantic_traverse(buf, NULL, cb_find_init,
903 cb_common_fini, param);
905 find_param_fini(param);
906 return ret < 0 ? ret : 0;
909 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data)
911 struct find_param *param = (struct find_param *)data;
914 LASSERT(parent != NULL || d != NULL);
917 if (!param->got_uuids) {
918 ret = setup_obd_uuids(d ? d : parent, path, param);
924 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
925 (void *)¶m->lmd->lmd_lmm);
927 char *fname = strrchr(path, '/');
928 fname = (fname == NULL ? path : fname + 1);
930 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
931 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
932 (void *)¶m->lmd->lmd_lmm);
936 if (errno == ENODATA) {
937 if (!param->obduuid && !param->quiet)
938 printf("%s has no stripe info\n", path);
940 } else if (errno == ENOTTY) {
941 fprintf(stderr, "%s: '%s' not on a Lustre fs?\n",
944 err_msg("error: %s: %s failed for %s", __FUNCTION__,
945 d ? "LL_IOC_LOV_GETSTRIPE" :
946 "IOC_MDC_GETFILESTRIPE", path);
952 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
954 /* Do not get down anymore? */
955 if (param->depth == param->maxdepth)
962 int llapi_getstripe(char *path, struct find_param *param)
966 ret = common_param_init(param);
971 ret = llapi_semantic_traverse(path, NULL, cb_getstripe,
972 cb_common_fini, param);
973 find_param_fini(param);
974 return ret < 0 ? ret : 0;
977 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
978 struct obd_statfs *stat_buf,
979 struct obd_uuid *uuid_buf)
982 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
984 struct obd_ioctl_data data = { 0 };
987 data.ioc_inlbuf1 = (char *)&type;
988 data.ioc_inllen1 = sizeof(__u32);
989 data.ioc_inlbuf2 = (char *)&index;
990 data.ioc_inllen2 = sizeof(__u32);
991 data.ioc_pbuf1 = (char *)stat_buf;
992 data.ioc_plen1 = sizeof(struct obd_statfs);
993 data.ioc_pbuf2 = (char *)uuid_buf;
994 data.ioc_plen2 = sizeof(struct obd_uuid);
996 if (obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) {
997 fprintf(stderr, "llapi_obd_statfs: error packing ioctl data\n");
1001 fd = open(path, O_RDONLY);
1002 if (errno == EISDIR)
1003 fd = open(path, O_DIRECTORY | O_RDONLY);
1006 rc = errno ? -errno : -EBADF;
1007 err_msg("error: %s: opening '%s'", __FUNCTION__, path);
1010 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1012 rc = errno ? -errno : -EINVAL;
1018 #define MAX_STRING_SIZE 128
1019 #define DEVICES_LIST "/proc/fs/lustre/devices"
1021 int llapi_ping(char *obd_type, char *obd_name)
1023 char path[MAX_STRING_SIZE];
1027 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1028 obd_type, obd_name);
1030 fd = open(path, O_WRONLY);
1033 fprintf(stderr, "error opening %s: %s\n", path, strerror(errno));
1037 rc = write(fd, buf, 1);
1045 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1047 char buf[MAX_STRING_SIZE];
1048 FILE *fp = fopen(DEVICES_LIST, "r");
1053 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
1058 while (fgets(buf, sizeof(buf), fp) != NULL) {
1059 char *obd_type_name = NULL;
1060 char *obd_name = NULL;
1061 char *obd_uuid = NULL;
1062 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1063 char *bufl = rawbuf;
1065 struct obd_ioctl_data datal = { 0, };
1066 struct obd_statfs osfs_buffer;
1068 while(bufp[0] == ' ')
1071 for(i = 0; i < 3; i++) {
1072 obd_type_name = strsep(&bufp, " ");
1074 obd_name = strsep(&bufp, " ");
1075 obd_uuid = strsep(&bufp, " ");
1077 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1079 memset(bufl, 0, sizeof(rawbuf));
1080 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1081 datal.ioc_plen1 = sizeof(osfs_buffer);
1083 for (i = 0; i < type_num; i++) {
1084 if (strcmp(obd_type_name, obd_type[i]) != 0)
1087 cb(obd_type_name, obd_name, obd_uuid, args);
1094 static void do_target_check(char *obd_type_name, char *obd_name,
1095 char *obd_uuid, void *args)
1099 rc = llapi_ping(obd_type_name, obd_name);
1101 err_msg("error: check '%s'", obd_name);
1103 printf("%s active.\n", obd_name);
1107 int llapi_target_check(int type_num, char **obd_type, char *dir)
1109 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1112 #undef MAX_STRING_SIZE
1114 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1116 char raw[OBD_MAX_IOCTL_BUFFER];
1117 char out[LLOG_CHUNK_SIZE];
1119 struct obd_ioctl_data data = { 0 };
1124 sprintf(key, "%s", keyword);
1125 memset(raw, 0, sizeof(raw));
1126 memset(out, 0, sizeof(out));
1127 data.ioc_inlbuf1 = key;
1128 data.ioc_inllen1 = strlen(key) + 1;
1130 data.ioc_inlbuf2 = node_name;
1131 data.ioc_inllen2 = strlen(node_name) + 1;
1133 data.ioc_pbuf1 = out;
1134 data.ioc_plen1 = sizeof(out);
1135 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1139 root = opendir(dir);
1142 err_msg("open %s failed", dir);
1146 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1148 err_msg("ioctl OBD_IOC_CATINFO failed");
1150 fprintf(stdout, "%s", data.ioc_pbuf1);
1156 /* Is this a lustre fs? */
1157 int llapi_is_lustre_mnttype(const char *type)
1159 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1162 /* Is this a lustre client fs? */
1163 int llapi_is_lustre_mnt(struct mntent *mnt)
1165 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1166 strstr(mnt->mnt_fsname, ":/") != NULL);
1169 int llapi_quotacheck(char *mnt, int check_type)
1174 root = opendir(mnt);
1176 err_msg("open %s failed", mnt);
1180 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1186 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1192 root = opendir(mnt);
1194 err_msg("open %s failed", mnt);
1199 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1203 if (poll_intvl < 30)
1211 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1216 root = opendir(mnt);
1218 err_msg("open %s failed", mnt);
1222 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1228 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data)
1230 struct find_param *param = (struct find_param *)data;
1234 LASSERT(parent != NULL || d != NULL);
1237 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1238 (void *)param->lmd);
1239 } else if (parent) {
1240 char *fname = strrchr(path, '/');
1241 fname = (fname == NULL ? path : fname + 1);
1243 strncpy((char *)param->lmd, fname, param->lumlen);
1244 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1245 (void *)param->lmd);
1251 if (errno == ENODATA) {
1252 if (!param->obduuid && !param->quiet)
1253 fprintf(stderr, "%s has no stripe info\n",
1256 } else if (errno != EISDIR) {
1258 err_msg("%s ioctl failed for %s.",
1259 d ? "LL_IOC_MDC_GETINFO" :
1260 "IOC_MDC_GETFILEINFO", path);
1265 st = ¶m->lmd->lmd_st;
1267 /* libc chown() will do extra check, and if the real owner is
1268 * the same as the ones to set, it won't fall into kernel, so
1269 * invoke syscall directly. */
1270 rc = syscall(SYS_chown, path, -1, -1);
1272 err_msg("error: chown %s (%u,%u)", path);
1274 rc = chmod(path, st->st_mode);
1276 err_msg("error: chmod %s (%hu)", path, st->st_mode);
1281 int llapi_quotachown(char *path, int flag)
1283 struct find_param param;
1286 memset(¶m, 0, sizeof(param));
1287 param.recursive = 1;
1291 ret = common_param_init(¶m);
1295 ret = llapi_semantic_traverse(path, NULL, cb_quotachown,
1298 find_param_fini(¶m);
1302 int llapi_getfacl(char *fname, char *cmd)
1304 struct rmtacl_ioctl_data data;
1305 char out[RMTACL_SIZE_MAX] = "";
1309 data.cmd_len = strlen(cmd) + 1;
1311 data.res_len = sizeof(out);
1313 fd = open(fname, 0);
1315 err_msg("open %s failed", fname);
1319 rc = ioctl(fd, LL_IOC_GETFACL, &data);
1321 if (errno == EBADE) {
1322 fprintf(stderr, "Please use getfacl directly!\n");
1325 err_msg("getfacl %s failed", fname);
1333 int llapi_setfacl(char *fname, char *cmd)
1335 struct rmtacl_ioctl_data data;
1336 char out[RMTACL_SIZE_MAX] = "";
1340 data.cmd_len = strlen(cmd) + 1;
1342 data.res_len = sizeof(out);
1344 fd = open(fname, 0);
1346 err_msg("open %s failed", fname);
1350 rc = ioctl(fd, LL_IOC_SETFACL, &data);
1352 if (errno == EBADE) {
1353 fprintf(stderr, "Please use setfacl directly!\n");
1355 } else if (errno == EOPNOTSUPP) {
1356 fprintf(stderr, "setfacl: %s: %s\n", fname, strerror(errno));
1359 err_msg("setfacl %s failed", fname);