1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Peter J. Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
7 * Author: Robert Read <rread@clusterfs.com>
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/syscall.h>
43 #ifdef HAVE_ASM_TYPES_H
44 #include <asm/types.h>
46 #ifdef HAVE_LINUX_UNISTD_H
47 #include <linux/unistd.h>
52 #include <liblustre.h>
53 #include <lnet/lnetctl.h>
55 #include <lustre_lib.h>
57 #include <lustre/liblustreapi.h>
59 static void err_msg(char *fmt, ...)
62 int tmp_errno = abs(errno);
65 vfprintf(stderr, fmt, args);
67 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
70 int llapi_file_open(const char *name, int flags, int mode,
71 unsigned long stripe_size, int stripe_offset,
72 int stripe_count, int stripe_pattern)
74 struct lov_user_md lum = { 0 };
79 fd = open(name, flags | O_LOV_DELAY_CREATE, mode);
80 if (fd < 0 && errno == EISDIR) {
81 fd = open(name, O_DIRECTORY | O_RDONLY);
87 err_msg("unable to open '%s'", name);
91 /* 64 KB is the largest common page size I'm aware of (on ia64), but
92 * check the local page size just in case. */
93 page_size = LOV_MIN_STRIPE_SIZE;
94 if (getpagesize() > page_size) {
95 page_size = getpagesize();
96 fprintf(stderr, "warning: your page size (%u) is larger than "
97 "expected (%u).\n", page_size, LOV_MIN_STRIPE_SIZE);
99 if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
100 errno = rc = -EINVAL;
101 err_msg("error: bad stripe_size %lu, must be an even "
102 "multiple of %d bytes", stripe_size, page_size);
105 if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
106 errno = rc = -EINVAL;
107 err_msg("error: bad stripe offset %d", stripe_offset);
110 if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
111 errno = rc = -EINVAL;
112 err_msg("error: bad stripe count %d", stripe_count);
115 if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){
116 errno = rc = -EINVAL;
117 err_msg("error: stripe_size %lu * stripe_count %u "
118 "exceeds 4GB", stripe_size, stripe_count);
122 /* Initialize IOCTL striping pattern structure */
123 lum.lmm_magic = LOV_USER_MAGIC;
124 lum.lmm_pattern = stripe_pattern;
125 lum.lmm_stripe_size = stripe_size;
126 lum.lmm_stripe_count = stripe_count;
127 lum.lmm_stripe_offset = stripe_offset;
129 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
130 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);
147 int llapi_file_create(const char *name, unsigned long stripe_size,
148 int stripe_offset, int stripe_count, int stripe_pattern)
152 fd = llapi_file_open(name, O_CREAT | O_WRONLY, 0644, stripe_size,
153 stripe_offset, stripe_count, stripe_pattern);
161 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d, void *data);
163 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
164 #define OBD_NOT_FOUND (-1)
166 static int common_param_init(struct find_param *param)
168 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
169 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
170 err_msg("error: allocation of %d bytes for ioctl",
171 sizeof(lstat_t) + param->lumlen);
175 param->got_uuids = 0;
176 param->obdindex = OBD_NOT_FOUND;
180 static void find_param_fini(struct find_param *param)
186 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
188 char lov_name[sizeof(struct obd_uuid)];
191 int rc = 0, index = 0;
193 /* Get the lov name */
194 rc = ioctl(fd, OBD_IOC_GETNAME, (void *) lov_name);
197 err_msg("error: can't get lov name.");
201 /* Now get the ost uuids from /proc */
202 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
204 fp = fopen(buf, "r");
207 err_msg("error: opening '%s'", buf);
211 while ((fgets(buf, sizeof(buf), fp) != NULL) && index < *ost_count) {
212 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) < 2)
223 static int setup_obd_uuids(DIR *dir, char *dname, struct find_param *param)
225 char uuid[sizeof(struct obd_uuid)];
230 /* Get the lov name */
231 rc = ioctl(dirfd(dir), OBD_IOC_GETNAME, (void *)uuid);
233 if (errno != ENOTTY) {
235 err_msg("error: can't get lov name: %s", dname);
242 param->got_uuids = 1;
244 /* Now get the ost uuids from /proc */
245 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
247 fp = fopen(buf, "r");
250 err_msg("error: opening '%s'", buf);
254 if (!param->obduuid && !param->quiet && !param->obds_printed)
257 while (fgets(buf, sizeof(buf), fp) != NULL) {
258 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
261 if (param->obduuid) {
262 if (strncmp(param->obduuid->uuid, uuid,
263 sizeof(uuid)) == 0) {
264 param->obdindex = index;
267 } else if (!param->quiet && !param->obds_printed) {
268 /* Print everything */
272 param->obds_printed = 1;
276 if (!param->quiet && param->obduuid &&
277 (param->obdindex == OBD_NOT_FOUND)) {
278 fprintf(stderr, "error: %s: unknown obduuid: %s\n",
279 __FUNCTION__, param->obduuid->uuid);
286 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
287 int obdindex, int quiet, int header, int body)
289 int i, obdstripe = 0;
291 if (obdindex != OBD_NOT_FOUND) {
292 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
293 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
294 printf("%s\n", path);
300 printf("%s\n", path);
304 /* if it's a directory */
306 if (obdstripe == 1) {
307 printf("default stripe_count: %d stripe_size: %u "
308 "stripe_offset: %d\n",
309 lum->lmm_stripe_count == (__u16)-1 ? -1 :
310 lum->lmm_stripe_count,
311 lum->lmm_stripe_size,
312 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
313 lum->lmm_stripe_offset);
318 if (header && (obdstripe == 1)) {
319 printf("lmm_magic: 0x%08X\n", lum->lmm_magic);
320 printf("lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
321 printf("lmm_object_id: "LPX64"\n", lum->lmm_object_id);
322 printf("lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
323 printf("lmm_stripe_size: %u\n", lum->lmm_stripe_size);
324 printf("lmm_stripe_pattern: %x\n", lum->lmm_pattern);
328 if ((!quiet) && (obdstripe == 1))
329 printf("\tobdidx\t\t objid\t\tobjid\t\t group\n");
331 for (i = 0; i < lum->lmm_stripe_count; i++) {
332 int idx = lum->lmm_objects[i].l_ost_idx;
333 long long oid = lum->lmm_objects[i].l_object_id;
334 long long gr = lum->lmm_objects[i].l_object_gr;
335 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
336 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
338 obdindex == idx ? " *" : "");
344 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
345 int is_dir, int obdindex, int quiet,
346 int header, int body)
348 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
349 int i, obdstripe = 0;
351 if (obdindex != OBD_NOT_FOUND) {
352 for (i = 0; i < lumj->lmm_stripe_count; i++) {
353 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
354 printf("%s\n", path);
360 printf("%s\n", path);
364 if (header && obdstripe == 1) {
365 printf("lmm_magic: 0x%08X\n", lumj->lmm_magic);
366 printf("lmm_object_gr: "LPX64"\n", lumj->lmm_object_gr);
367 printf("lmm_object_id: "LPX64"\n", lumj->lmm_object_id);
368 printf("lmm_stripe_count: %u\n", (int)lumj->lmm_stripe_count);
369 printf("lmm_stripe_size: %u\n", lumj->lmm_stripe_size);
370 printf("lmm_stripe_pattern: %x\n", lumj->lmm_pattern);
371 printf("lmm_extent_count: %x\n", lumj->lmm_extent_count);
375 unsigned long long start = -1, end = 0;
376 if (!quiet && obdstripe == 1)
377 printf("joined\tobdidx\t\t objid\t\tobjid\t\t group"
378 "\t\tstart\t\tend\n");
379 for (i = 0; i < lumj->lmm_stripe_count; i++) {
380 int idx = lumj->lmm_objects[i].l_ost_idx;
381 long long oid = lumj->lmm_objects[i].l_object_id;
382 long long gr = lumj->lmm_objects[i].l_object_gr;
383 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
384 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s",
386 obdindex == idx ? " *" : "");
387 if (start != lumj->lmm_objects[i].l_extent_start ||
388 end != lumj->lmm_objects[i].l_extent_end) {
389 start = lumj->lmm_objects[i].l_extent_start;
390 printf("\t%14llu", start);
391 end = lumj->lmm_objects[i].l_extent_end;
392 if (end == (unsigned long long)-1)
395 printf("\t\t%llu\n", end);
397 printf("\t\t\t\t\n");
404 void llapi_lov_dump_user_lmm(struct find_param *param,
405 char *path, int is_dir)
407 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
408 case LOV_USER_MAGIC_V1:
409 lov_dump_user_lmm_v1(¶m->lmd->lmd_lmm, path, is_dir,
410 param->obdindex, param->quiet,
412 (param->verbose || !param->obduuid));
414 case LOV_USER_MAGIC_JOIN:
415 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
416 param->obdindex, param->quiet,
418 (param->verbose || !param->obduuid));
421 printf("unknown lmm_magic: %#x (expecting %#x)\n",
422 *(__u32 *)¶m->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
427 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
433 fname = strrchr(path, '/');
435 /* It should be a file (or other non-directory) */
437 dname = (char *)malloc(2);
441 fname = (char *)path;
443 dname = (char *)malloc(fname - path + 1);
446 strncpy(dname, path, fname - path);
447 dname[fname - path] = '\0';
451 if ((fd = open(dname, O_RDONLY)) == -1) {
457 strcpy((char *)lum, fname);
458 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
461 if (close(fd) == -1 && rc == 0)
469 int llapi_file_lookup(int dirfd, const char *name)
471 struct obd_ioctl_data data = { 0 };
476 if (dirfd < 0 || name == NULL)
479 data.ioc_version = OBD_IOCTL_VERSION;
480 data.ioc_len = sizeof(data);
481 data.ioc_inlbuf1 = (char *)name;
482 data.ioc_inllen1 = strlen(name) + 1;
484 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
487 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d\n",
492 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
495 /* some 64bit libcs implement readdir64() by calling sys_getdents(). the
496 * kernel's sys_getdents() doesn't return d_type. */
497 unsigned char handle_dt_unknown(char *path)
501 fd = open(path, O_DIRECTORY|O_RDONLY);
503 if (errno == ENOTDIR)
504 return DT_REG; /* kind of a lie */
511 static DIR *opendir_parent(char *path)
517 fname = strrchr(path, '/');
523 parent = opendir(path);
528 static int llapi_semantic_traverse(char *path, int size, DIR *parent,
529 semantic_func_t sem_init,
530 semantic_func_t sem_fini, void *data)
532 struct dirent64 *dent;
540 if (!d && errno != ENOTDIR) {
541 fprintf(stderr, "%s: Failed to open '%s': %s.",
542 __FUNCTION__, path, strerror(errno));
544 } else if (!d && !parent) {
545 /* ENOTDIR. Open the parent dir. */
546 p = opendir_parent(path);
548 GOTO(out, ret = -EINVAL);
551 if (sem_init && (ret = sem_init(path, parent ?: p, d, data)))
557 while ((dent = readdir64(d)) != NULL) {
558 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
562 if ((len + dent->d_reclen + 2) > size) {
564 "error: %s: string buffer is too small\n",
569 strcat(path, dent->d_name);
571 if (dent->d_type == DT_UNKNOWN)
572 dent->d_type = handle_dt_unknown(path);
574 switch (dent->d_type) {
576 fprintf(stderr, "error: %s: '%s' is UNKNOWN type %d",
577 __FUNCTION__, dent->d_name, dent->d_type);
578 /* If we cared we could stat the file to determine
579 * type and continue on here, but we don't since we
580 * know d_type should be valid for lustre and this
581 * tool only makes sense for lustre filesystems. */
584 ret = llapi_semantic_traverse(path, size, d, sem_init,
592 ret = sem_init(path, d, NULL, data);
596 if (sem_fini && ret == 0)
597 sem_fini(path, d, NULL, data);
605 sem_fini(path, parent, d, data);
614 /* Check if the file time matches 1 of the given criteria (e.g. --atime +/-N).
615 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
617 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
618 * The table bolow gives the answers for the specified parameters (time and
619 * sign), 1st column is the answer for the MDS time, the 2nd is for the OST:
620 * --------------------------------------
621 * 1 | file > limit; sign > 0 | -1 / -1 |
622 * 2 | file = limit; sign > 0 | ? / 1 |
623 * 3 | file < limit; sign > 0 | ? / 1 |
624 * 4 | file > limit; sign = 0 | -1 / -1 |
625 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
626 * 6 | file < limit; sign = 0 | ? / -1 |
627 * 7 | file > limit; sign < 0 | 1 / 1 |
628 * 8 | file = limit; sign < 0 | ? / -1 |
629 * 9 | file < limit; sign < 0 | ? / -1 |
630 * --------------------------------------
631 * Note: 5th actually means that the file time stamp is within the interval
632 * (limit - 24hours, limit]. */
633 static int find_time_cmp(time_t file, time_t limit, int sign, int mds) {
640 if (file <= limit && file + 24 * 60 * 60 > limit)
642 if (file + 24 * 60 * 60 <= limit)
656 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
657 * Return -1 or 1 if file timestamp does not or does match the given criteria
658 * correspondingly. Return 0 if the MDS time is being checked and there are
659 * attributes on OSTs and it is not yet clear if the timespamp matches.
661 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
662 * updated timestamps. */
663 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
668 /* Check if file is accepted. */
670 ret = find_time_cmp(st->st_atime, param->atime,
678 ret = find_time_cmp(st->st_mtime, param->mtime,
683 /* If the previous check matches, but this one is not yet clear,
684 * we should return 0 to do an RPC on OSTs. */
690 ret = find_time_cmp(st->st_ctime, param->ctime,
695 /* If the previous check matches, but this one is not yet clear,
696 * we should return 0 to do an RPC on OSTs. */
704 static int cb_find_init(char *path, DIR *parent, DIR *dir, void *data)
706 struct find_param *param = (struct find_param *)data;
707 int decision = 1; /* 1 is accepted; -1 is rejected. */
708 lstat_t *st = ¶m->lmd->lmd_st;
712 LASSERT(parent != NULL || dir != NULL);
714 param->lmd->lmd_lmm.lmm_stripe_count = 0;
716 /* If a time or OST should be checked, the decision is not taken yet. */
717 if (param->atime || param->ctime || param->mtime || param->obduuid)
720 /* Request MDS for the stat info. */
721 if (!decision && dir) {
722 /* retrieve needed file info */
723 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
725 } else if (!decision && parent) {
726 char *fname = strrchr(path, '/');
727 fname = (fname == NULL ? path : fname + 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 a regular expression is presented, make the initial decision */
777 if (param->pattern != NULL) {
778 char *fname = strrchr(path, '/');
779 fname = (fname == NULL ? path : fname + 1);
780 ret = fnmatch(param->pattern, fname, 0);
781 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
782 (ret == 0 && param->exclude_pattern))
786 /* If an OBD UUID is specified but no one matches, skip this file. */
787 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
790 /* If a OST UUID is given, and some OST matches, check it here. */
791 if (decision != -1 && param->obdindex != OBD_NOT_FOUND) {
792 if (!S_ISREG(st->st_mode))
795 /* Only those files should be accepted, which have a
796 * stripe on 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 ? (S_ISREG(st->st_mode) &&
818 param->lmd->lmd_lmm.lmm_stripe_count)
820 decision = find_time_check(st, param, for_mds);
823 /* If file still fits the request, ask osd for updated info.
824 The regulat stat is almost of the same speed as some new
825 'glimpse-size-ioctl'. */
826 if (!decision && param->lmd->lmd_lmm.lmm_stripe_count &&
827 S_ISREG(st->st_mode)) {
828 if (param->obdindex != OBD_NOT_FOUND) {
829 /* Check whether the obd is active or not, if it is
830 * not active, just print the object affected by this
833 struct obd_statfs stat_buf;
834 struct obd_uuid uuid_buf;
836 memset(&stat_buf, 0, sizeof(struct obd_statfs));
837 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
838 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
839 param->obdindex, &stat_buf,
842 if (ret == -ENODATA || ret == -ENODEV
845 printf("obd_uuid: %s failed %s ",
846 param->obduuid->uuid, strerror(errno));
851 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
854 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
859 fprintf(stderr, "%s: IOC_LOV_GETINFO on %s failed: "
860 "%s.\n", __FUNCTION__, path, strerror(errno));
864 /* Check the time on osc. */
866 decision = find_time_check(st, param, 0);
870 if (decision != -1) {
879 /* Do not get down anymore? */
880 if (param->depth == param->maxdepth)
887 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data)
889 struct find_param *param = (struct find_param *)data;
894 int llapi_find(char *path, struct find_param *param)
897 int ret, len = strlen(path);
899 if (len > PATH_MAX) {
900 fprintf(stderr, "%s: Path name '%s' is too long.\n",
905 buf = (char *)malloc(PATH_MAX + 1);
909 ret = common_param_init(param);
917 strncpy(buf, path, PATH_MAX + 1);
918 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init,
919 cb_common_fini, param);
921 find_param_fini(param);
923 return ret < 0 ? ret : 0;
926 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data)
928 struct find_param *param = (struct find_param *)data;
931 LASSERT(parent != NULL || d != NULL);
934 if (!param->got_uuids) {
935 ret = setup_obd_uuids(d ? d : parent, path, param);
941 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
942 (void *)¶m->lmd->lmd_lmm);
944 char *fname = strrchr(path, '/');
945 fname = (fname == NULL ? path : fname + 1);
947 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
948 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
949 (void *)¶m->lmd->lmd_lmm);
953 if (errno == ENODATA) {
954 if (!param->obduuid && !param->quiet)
955 printf("%s has no stripe info\n", path);
957 } else if (errno == ENOTTY) {
958 fprintf(stderr, "%s: '%s' not on a Lustre fs?\n",
961 err_msg("error: %s: %s failed for %s", __FUNCTION__,
962 d ? "LL_IOC_LOV_GETSTRIPE" :
963 "IOC_MDC_GETFILESTRIPE", path);
969 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
971 /* Do not get down anymore? */
972 if (param->depth == param->maxdepth)
979 int llapi_getstripe(char *path, struct find_param *param)
982 int ret = 0, len = strlen(path);
984 if (len > PATH_MAX) {
985 fprintf(stderr, "%s: Path name '%s' is too long.\n",
990 buf = (char *)malloc(PATH_MAX + 1);
994 ret = common_param_init(param);
1002 strncpy(buf, path, PATH_MAX + 1);
1003 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe,
1004 cb_common_fini, param);
1005 find_param_fini(param);
1007 return ret < 0 ? ret : 0;
1010 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
1011 struct obd_statfs *stat_buf,
1012 struct obd_uuid *uuid_buf)
1015 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
1017 struct obd_ioctl_data data = { 0 };
1020 data.ioc_inlbuf1 = (char *)&type;
1021 data.ioc_inllen1 = sizeof(__u32);
1022 data.ioc_inlbuf2 = (char *)&index;
1023 data.ioc_inllen2 = sizeof(__u32);
1024 data.ioc_pbuf1 = (char *)stat_buf;
1025 data.ioc_plen1 = sizeof(struct obd_statfs);
1026 data.ioc_pbuf2 = (char *)uuid_buf;
1027 data.ioc_plen2 = sizeof(struct obd_uuid);
1029 if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
1030 fprintf(stderr, "llapi_obd_statfs: error packing ioctl data\n");
1034 fd = open(path, O_RDONLY);
1035 if (errno == EISDIR)
1036 fd = open(path, O_DIRECTORY | O_RDONLY);
1039 rc = errno ? -errno : -EBADF;
1040 err_msg("error: %s: opening '%s'", __FUNCTION__, path);
1043 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1045 rc = errno ? -errno : -EINVAL;
1051 #define MAX_STRING_SIZE 128
1052 #define DEVICES_LIST "/proc/fs/lustre/devices"
1054 int llapi_ping(char *obd_type, char *obd_name)
1056 char path[MAX_STRING_SIZE];
1060 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1061 obd_type, obd_name);
1063 fd = open(path, O_WRONLY);
1066 fprintf(stderr, "error opening %s: %s\n", path, strerror(errno));
1070 rc = write(fd, buf, 1);
1078 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1080 char buf[MAX_STRING_SIZE];
1081 FILE *fp = fopen(DEVICES_LIST, "r");
1086 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
1091 while (fgets(buf, sizeof(buf), fp) != NULL) {
1092 char *obd_type_name = NULL;
1093 char *obd_name = NULL;
1094 char *obd_uuid = NULL;
1095 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1096 char *bufl = rawbuf;
1098 struct obd_ioctl_data datal = { 0, };
1099 struct obd_statfs osfs_buffer;
1101 while(bufp[0] == ' ')
1104 for(i = 0; i < 3; i++) {
1105 obd_type_name = strsep(&bufp, " ");
1107 obd_name = strsep(&bufp, " ");
1108 obd_uuid = strsep(&bufp, " ");
1110 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1112 memset(bufl, 0, sizeof(rawbuf));
1113 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1114 datal.ioc_plen1 = sizeof(osfs_buffer);
1116 for (i = 0; i < type_num; i++) {
1117 if (strcmp(obd_type_name, obd_type[i]) != 0)
1120 cb(obd_type_name, obd_name, obd_uuid, args);
1127 static void do_target_check(char *obd_type_name, char *obd_name,
1128 char *obd_uuid, void *args)
1132 rc = llapi_ping(obd_type_name, obd_name);
1134 err_msg("error: check '%s'", obd_name);
1136 printf("%s active.\n", obd_name);
1140 int llapi_target_check(int type_num, char **obd_type, char *dir)
1142 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1145 #undef MAX_STRING_SIZE
1147 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1149 char raw[OBD_MAX_IOCTL_BUFFER];
1150 char out[LLOG_CHUNK_SIZE];
1152 struct obd_ioctl_data data = { 0 };
1157 sprintf(key, "%s", keyword);
1158 memset(raw, 0, sizeof(raw));
1159 memset(out, 0, sizeof(out));
1160 data.ioc_inlbuf1 = key;
1161 data.ioc_inllen1 = strlen(key) + 1;
1163 data.ioc_inlbuf2 = node_name;
1164 data.ioc_inllen2 = strlen(node_name) + 1;
1166 data.ioc_pbuf1 = out;
1167 data.ioc_plen1 = sizeof(out);
1168 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1172 root = opendir(dir);
1175 err_msg("open %s failed", dir);
1179 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1181 err_msg("ioctl OBD_IOC_CATINFO failed");
1183 fprintf(stdout, "%s", data.ioc_pbuf1);
1189 /* Is this a lustre fs? */
1190 int llapi_is_lustre_mnttype(const char *type)
1192 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1195 /* Is this a lustre client fs? */
1196 int llapi_is_lustre_mnt(struct mntent *mnt)
1198 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1199 strstr(mnt->mnt_fsname, ":/") != NULL);
1202 int llapi_quotacheck(char *mnt, int check_type)
1207 root = opendir(mnt);
1209 err_msg("open %s failed", mnt);
1213 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1219 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1225 root = opendir(mnt);
1227 err_msg("open %s failed", mnt);
1232 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1236 if (poll_intvl < 30)
1244 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1249 root = opendir(mnt);
1251 err_msg("open %s failed", mnt);
1255 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1261 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data)
1263 struct find_param *param = (struct find_param *)data;
1267 LASSERT(parent != NULL || d != NULL);
1270 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1271 (void *)param->lmd);
1272 } else if (parent) {
1273 char *fname = strrchr(path, '/');
1274 fname = (fname == NULL ? path : fname + 1);
1276 strncpy((char *)param->lmd, fname, param->lumlen);
1277 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1278 (void *)param->lmd);
1284 if (errno == ENODATA) {
1285 if (!param->obduuid && !param->quiet)
1286 fprintf(stderr, "%s has no stripe info\n",
1289 } else if (errno != EISDIR) {
1291 err_msg("%s ioctl failed for %s.",
1292 d ? "LL_IOC_MDC_GETINFO" :
1293 "IOC_MDC_GETFILEINFO", path);
1298 st = ¶m->lmd->lmd_st;
1300 /* libc chown() will do extra check, and if the real owner is
1301 * the same as the ones to set, it won't fall into kernel, so
1302 * invoke syscall directly. */
1303 rc = syscall(SYS_chown, path, -1, -1);
1305 err_msg("error: chown %s (%u,%u)", path);
1307 rc = chmod(path, st->st_mode);
1309 err_msg("error: chmod %s (%hu)", path, st->st_mode);
1314 int llapi_quotachown(char *path, int flag)
1316 struct find_param param;
1318 int ret = 0, len = strlen(path);
1320 if (len > PATH_MAX) {
1321 fprintf(stderr, "%s: Path name '%s' is too long.\n",
1322 __FUNCTION__, path);
1326 buf = (char *)malloc(PATH_MAX + 1);
1330 memset(¶m, 0, sizeof(param));
1331 param.recursive = 1;
1335 ret = common_param_init(¶m);
1339 strncpy(buf, path, PATH_MAX + 1);
1340 ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown,
1343 find_param_fini(¶m);
1348 int llapi_getfacl(char *fname, char *cmd)
1350 struct rmtacl_ioctl_data data;
1351 char out[RMTACL_SIZE_MAX] = "";
1355 data.cmd_len = strlen(cmd) + 1;
1357 data.res_len = sizeof(out);
1359 fd = open(fname, 0);
1361 err_msg("open %s failed", fname);
1365 rc = ioctl(fd, LL_IOC_GETFACL, &data);
1367 if (errno == EBADE) {
1368 fprintf(stderr, "Please use getfacl directly!\n");
1371 err_msg("getfacl %s failed", fname);
1379 int llapi_setfacl(char *fname, char *cmd)
1381 struct rmtacl_ioctl_data data;
1382 char out[RMTACL_SIZE_MAX] = "";
1386 data.cmd_len = strlen(cmd) + 1;
1388 data.res_len = sizeof(out);
1390 fd = open(fname, 0);
1392 err_msg("open %s failed", fname);
1396 rc = ioctl(fd, LL_IOC_SETFACL, &data);
1398 if (errno == EBADE) {
1399 fprintf(stderr, "Please use setfacl directly!\n");
1401 } else if (errno == EOPNOTSUPP) {
1402 fprintf(stderr, "setfacl: %s: %s\n", fname, strerror(errno));
1405 err_msg("setfacl %s failed", fname);