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);
137 fprintf(stderr, "error on ioctl "LPX64" for '%s' (%d): %s\n",
138 (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg);
149 int llapi_file_create(const char *name, unsigned long stripe_size,
150 int stripe_offset, int stripe_count, int stripe_pattern)
154 fd = llapi_file_open(name, O_CREAT | O_WRONLY, 0644, stripe_size,
155 stripe_offset, stripe_count, stripe_pattern);
163 typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d, void *data);
165 #define MAX_LOV_UUID_COUNT max(LOV_MAX_STRIPE_COUNT, 1000)
166 #define OBD_NOT_FOUND (-1)
168 static int common_param_init(struct find_param *param)
170 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
171 if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
172 err_msg("error: allocation of %d bytes for ioctl",
173 sizeof(lstat_t) + param->lumlen);
177 param->got_uuids = 0;
178 param->obdindex = OBD_NOT_FOUND;
182 static void find_param_fini(struct find_param *param)
188 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
190 char lov_name[sizeof(struct obd_uuid)];
193 int rc = 0, index = 0;
195 /* Get the lov name */
196 rc = ioctl(fd, OBD_IOC_GETNAME, (void *) lov_name);
199 err_msg("error: can't get lov name.");
203 /* Now get the ost uuids from /proc */
204 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
206 fp = fopen(buf, "r");
209 err_msg("error: opening '%s'", buf);
213 while ((fgets(buf, sizeof(buf), fp) != NULL) && index < *ost_count) {
214 if (sscanf(buf, "%d: %s", &index, uuidp[index].uuid) < 2)
225 static int setup_obd_uuids(DIR *dir, char *dname, struct find_param *param)
227 char uuid[sizeof(struct obd_uuid)];
232 /* Get the lov name */
233 rc = ioctl(dirfd(dir), OBD_IOC_GETNAME, (void *)uuid);
235 if (errno != ENOTTY) {
237 err_msg("error: can't get lov name: %s", dname);
244 param->got_uuids = 1;
246 /* Now get the ost uuids from /proc */
247 snprintf(buf, sizeof(buf), "/proc/fs/lustre/lov/%s/target_obd",
249 fp = fopen(buf, "r");
252 err_msg("error: opening '%s'", buf);
256 if (!param->obduuid && !param->quiet && !param->obds_printed)
259 while (fgets(buf, sizeof(buf), fp) != NULL) {
260 if (sscanf(buf, "%d: %s", &index, uuid) < 2)
263 if (param->obduuid) {
264 if (strncmp(param->obduuid->uuid, uuid,
265 sizeof(uuid)) == 0) {
266 param->obdindex = index;
269 } else if (!param->quiet && !param->obds_printed) {
270 /* Print everything */
274 param->obds_printed = 1;
278 if (!param->quiet && param->obduuid &&
279 (param->obdindex == OBD_NOT_FOUND)) {
280 fprintf(stderr, "error: %s: unknown obduuid: %s\n",
281 __FUNCTION__, param->obduuid->uuid);
288 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir,
289 int obdindex, int quiet, int header, int body)
291 int i, obdstripe = 0;
293 if (obdindex != OBD_NOT_FOUND) {
294 for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) {
295 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
296 printf("%s\n", path);
302 printf("%s\n", path);
306 /* if it's a directory */
308 if (obdstripe == 1) {
309 printf("default stripe_count: %d stripe_size: %u "
310 "stripe_offset: %d\n",
311 lum->lmm_stripe_count == (__u16)-1 ? -1 :
312 lum->lmm_stripe_count,
313 lum->lmm_stripe_size,
314 lum->lmm_stripe_offset == (__u16)-1 ? -1 :
315 lum->lmm_stripe_offset);
320 if (header && (obdstripe == 1)) {
321 printf("lmm_magic: 0x%08X\n", lum->lmm_magic);
322 printf("lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
323 printf("lmm_object_id: "LPX64"\n", lum->lmm_object_id);
324 printf("lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
325 printf("lmm_stripe_size: %u\n", lum->lmm_stripe_size);
326 printf("lmm_stripe_pattern: %x\n", lum->lmm_pattern);
330 if ((!quiet) && (obdstripe == 1))
331 printf("\tobdidx\t\t objid\t\tobjid\t\t group\n");
333 for (i = 0; i < lum->lmm_stripe_count; i++) {
334 int idx = lum->lmm_objects[i].l_ost_idx;
335 long long oid = lum->lmm_objects[i].l_object_id;
336 long long gr = lum->lmm_objects[i].l_object_gr;
337 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
338 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
340 obdindex == idx ? " *" : "");
346 void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path,
347 int is_dir, int obdindex, int quiet,
348 int header, int body)
350 struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum;
351 int i, obdstripe = 0;
353 if (obdindex != OBD_NOT_FOUND) {
354 for (i = 0; i < lumj->lmm_stripe_count; i++) {
355 if (obdindex == lumj->lmm_objects[i].l_ost_idx) {
356 printf("%s\n", path);
362 printf("%s\n", path);
366 if (header && obdstripe == 1) {
367 printf("lmm_magic: 0x%08X\n", lumj->lmm_magic);
368 printf("lmm_object_gr: "LPX64"\n", lumj->lmm_object_gr);
369 printf("lmm_object_id: "LPX64"\n", lumj->lmm_object_id);
370 printf("lmm_stripe_count: %u\n", (int)lumj->lmm_stripe_count);
371 printf("lmm_stripe_size: %u\n", lumj->lmm_stripe_size);
372 printf("lmm_stripe_pattern: %x\n", lumj->lmm_pattern);
373 printf("lmm_extent_count: %x\n", lumj->lmm_extent_count);
377 unsigned long long start = -1, end = 0;
378 if (!quiet && obdstripe == 1)
379 printf("joined\tobdidx\t\t objid\t\tobjid\t\t group"
380 "\t\tstart\t\tend\n");
381 for (i = 0; i < lumj->lmm_stripe_count; i++) {
382 int idx = lumj->lmm_objects[i].l_ost_idx;
383 long long oid = lumj->lmm_objects[i].l_object_id;
384 long long gr = lumj->lmm_objects[i].l_object_gr;
385 if (obdindex == OBD_NOT_FOUND || obdindex == idx)
386 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s",
388 obdindex == idx ? " *" : "");
389 if (start != lumj->lmm_objects[i].l_extent_start ||
390 end != lumj->lmm_objects[i].l_extent_end) {
391 start = lumj->lmm_objects[i].l_extent_start;
392 printf("\t%14llu", start);
393 end = lumj->lmm_objects[i].l_extent_end;
394 if (end == (unsigned long long)-1)
397 printf("\t\t%llu\n", end);
399 printf("\t\t\t\t\n");
406 void llapi_lov_dump_user_lmm(struct find_param *param,
407 char *path, int is_dir)
409 switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */
410 case LOV_USER_MAGIC_V1:
411 lov_dump_user_lmm_v1(¶m->lmd->lmd_lmm, path, is_dir,
412 param->obdindex, param->quiet,
414 (param->verbose || !param->obduuid));
416 case LOV_USER_MAGIC_JOIN:
417 lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir,
418 param->obdindex, param->quiet,
420 (param->verbose || !param->obduuid));
423 printf("unknown lmm_magic: %#x (expecting %#x)\n",
424 *(__u32 *)¶m->lmd->lmd_lmm, LOV_USER_MAGIC_V1);
429 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
435 fname = strrchr(path, '/');
437 /* It should be a file (or other non-directory) */
439 dname = (char *)malloc(2);
443 fname = (char *)path;
445 dname = (char *)malloc(fname - path + 1);
448 strncpy(dname, path, fname - path);
449 dname[fname - path] = '\0';
453 if ((fd = open(dname, O_RDONLY)) == -1) {
459 strcpy((char *)lum, fname);
460 if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
463 if (close(fd) == -1 && rc == 0)
471 int llapi_file_lookup(int dirfd, const char *name)
473 struct obd_ioctl_data data = { 0 };
478 if (dirfd < 0 || name == NULL)
481 data.ioc_version = OBD_IOCTL_VERSION;
482 data.ioc_len = sizeof(data);
483 data.ioc_inlbuf1 = (char *)name;
484 data.ioc_inllen1 = strlen(name) + 1;
486 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
489 "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d\n",
494 return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
497 /* some 64bit libcs implement readdir64() by calling sys_getdents(). the
498 * kernel's sys_getdents() doesn't return d_type. */
499 unsigned char handle_dt_unknown(char *path)
503 fd = open(path, O_DIRECTORY|O_RDONLY);
505 if (errno == ENOTDIR)
506 return DT_REG; /* kind of a lie */
513 static DIR *opendir_parent(char *path)
519 fname = strrchr(path, '/');
525 parent = opendir(path);
530 static int llapi_semantic_traverse(char *path, DIR *parent,
531 semantic_func_t sem_init,
532 semantic_func_t sem_fini, void *data)
534 struct dirent64 *dent;
542 if (!d && errno != ENOTDIR) {
543 fprintf(stderr, "%s: Failed to open '%s': %s.",
544 __FUNCTION__, path, strerror(errno));
546 } else if (!d && !parent) {
547 /* ENOTDIR. Open the parent dir. */
548 p = opendir_parent(path);
550 GOTO(out, ret = -EINVAL);
553 if (sem_init && (ret = sem_init(path, parent ?: p, d, data)))
559 while ((dent = readdir64(d)) != NULL) {
560 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
565 strcat(path, dent->d_name);
567 if (dent->d_type == DT_UNKNOWN)
568 dent->d_type = handle_dt_unknown(path);
570 switch (dent->d_type) {
572 fprintf(stderr, "error: %s: '%s' is UNKNOWN type %d",
573 __FUNCTION__, dent->d_name, dent->d_type);
574 /* If we cared we could stat the file to determine
575 * type and continue on here, but we don't since we
576 * know d_type should be valid for lustre and this
577 * tool only makes sense for lustre filesystems. */
580 ret = llapi_semantic_traverse(path, d, sem_init,
588 ret = sem_init(path, d, NULL, data);
592 if (sem_fini && ret == 0)
593 sem_fini(path, d, NULL, data);
601 sem_fini(path, parent, d, data);
610 /* Check if the file time matches 1 of the given criteria (e.g. --atime +/-N).
611 * @mds indicates if this is MDS timestamps and there are attributes on OSTs.
613 * The result is -1 if it does not match, 0 if not yet clear, 1 if matches.
614 * The table bolow gives the answers for the specified parameters (time and
615 * sign), 1st column is the answer for the MDS time, the 2nd is for the OST:
616 * --------------------------------------
617 * 1 | file > limit; sign > 0 | -1 / -1 |
618 * 2 | file = limit; sign > 0 | ? / 1 |
619 * 3 | file < limit; sign > 0 | ? / 1 |
620 * 4 | file > limit; sign = 0 | -1 / -1 |
621 * 5 | file = limit; sign = 0 | ? / 1 | <- (see the Note below)
622 * 6 | file < limit; sign = 0 | ? / -1 |
623 * 7 | file > limit; sign < 0 | 1 / 1 |
624 * 8 | file = limit; sign < 0 | ? / -1 |
625 * 9 | file < limit; sign < 0 | ? / -1 |
626 * --------------------------------------
627 * Note: 5th actually means that the file time stamp is within the interval
628 * (limit - 24hours, limit]. */
629 static int find_time_cmp(time_t file, time_t limit, int sign, int mds) {
636 if (file <= limit && file + 24 * 60 * 60 > limit)
638 if (file + 24 * 60 * 60 <= limit)
652 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
653 * Return -1 or 1 if file timestamp does not or does match the given criteria
654 * correspondingly. Return 0 if the MDS time is being checked and there are
655 * attributes on OSTs and it is not yet clear if the timespamp matches.
657 * If 0 is returned, we need to do another RPC to the OSTs to obtain the
658 * updated timestamps. */
659 static int find_time_check(lstat_t *st, struct find_param *param, int mds)
664 /* Check if file is accepted. */
666 ret = find_time_cmp(st->st_atime, param->atime,
674 ret = find_time_cmp(st->st_mtime, param->mtime,
679 /* If the previous check matches, but this one is not yet clear,
680 * we should return 0 to do an RPC on OSTs. */
686 ret = find_time_cmp(st->st_ctime, param->ctime,
691 /* If the previous check matches, but this one is not yet clear,
692 * we should return 0 to do an RPC on OSTs. */
700 static int cb_find_init(char *path, DIR *parent, DIR *dir, void *data)
702 struct find_param *param = (struct find_param *)data;
703 int decision = 1; /* 1 is accepted; -1 is rejected. */
704 lstat_t *st = ¶m->lmd->lmd_st;
708 LASSERT(parent != NULL || dir != NULL);
710 param->lmd->lmd_lmm.lmm_stripe_count = 0;
712 /* If a time or OST should be checked, the decision is not taken yet. */
713 if (param->atime || param->ctime || param->mtime || param->obduuid)
716 /* Request MDS for the stat info. */
717 if (!decision && dir) {
718 /* retrieve needed file info */
719 ret = ioctl(dirfd(dir), LL_IOC_MDC_GETINFO,
721 } else if (!decision && parent) {
722 char *fname = strrchr(path, '/');
723 fname = (fname == NULL ? path : fname + 1);
725 /* retrieve needed file info */
726 strncpy((char *)param->lmd, fname, param->lumlen);
727 ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
732 if (errno == ENOTTY) {
733 /* ioctl is not supported, it is not a lustre fs.
734 * Do the regular lstat(2) instead. */
736 ret = lstat_f(path, st);
738 err_msg("error: %s: lstat failed for %s",
743 err_msg("error: %s: %s failed for %s", __FUNCTION__,
744 dir ? "LL_IOC_MDC_GETINFO" :
745 "IOC_MDC_GETFILEINFO", path);
751 if (param->obduuid) {
752 if (lustre_fs && param->got_uuids &&
753 param->st_dev != st->st_dev) {
754 /* A lustre/lustre mount point is crossed. */
755 param->got_uuids = 0;
756 param->obds_printed = 0;
757 param->obdindex = OBD_NOT_FOUND;
760 if (lustre_fs && !param->got_uuids) {
761 ret = setup_obd_uuids(dir ? dir : parent, path, param);
764 param->st_dev = st->st_dev;
765 } else if (!lustre_fs && param->got_uuids) {
766 /* A lustre/non-lustre mount point is crossed. */
767 param->got_uuids = 0;
768 param->obdindex = OBD_NOT_FOUND;
772 /* If a regular expression is presented, make the initial decision */
773 if (param->pattern != NULL) {
774 char *fname = strrchr(path, '/');
775 fname = (fname == NULL ? path : fname + 1);
776 ret = fnmatch(param->pattern, fname, 0);
777 if ((ret == FNM_NOMATCH && !param->exclude_pattern) ||
778 (ret == 0 && param->exclude_pattern))
782 /* If an OBD UUID is specified but no one matches, skip this file. */
783 if (param->obduuid && param->obdindex == OBD_NOT_FOUND)
786 /* If a OST UUID is given, and some OST matches, check it here. */
787 if (decision != -1 && param->obdindex != OBD_NOT_FOUND &&
788 S_ISREG(st->st_mode)) {
789 /* Only those files should be accepted, which have a strip on
790 * the specified OST. */
791 if (!param->lmd->lmd_lmm.lmm_stripe_count) {
796 i < param->lmd->lmd_lmm.lmm_stripe_count; i++) {
797 if (param->obdindex ==
798 param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx)
802 if (i == param->lmd->lmd_lmm.lmm_stripe_count)
807 /* Check the time on mds. */
811 for_mds = lustre_fs ? (S_ISREG(st->st_mode) &&
812 param->lmd->lmd_lmm.lmm_stripe_count)
814 decision = find_time_check(st, param, for_mds);
817 /* If file still fits the request, ask osd for updated info.
818 The regulat stat is almost of the same speed as some new
819 'glimpse-size-ioctl'. */
820 if (!decision && param->lmd->lmd_lmm.lmm_stripe_count &&
821 S_ISREG(st->st_mode)) {
822 if (param->obdindex != OBD_NOT_FOUND) {
823 /* Check whether the obd is active or not, if it is
824 * not active, just print the object affected by this
827 struct obd_statfs stat_buf;
828 struct obd_uuid uuid_buf;
830 memset(&stat_buf, 0, sizeof(struct obd_statfs));
831 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
832 ret = llapi_obd_statfs(path, LL_STATFS_LOV,
833 param->obdindex, &stat_buf,
836 if (ret == -ENODATA || ret == -ENODEV
839 printf("obd_uuid: %s failed %s ",
840 param->obduuid->uuid, strerror(errno));
845 ret = ioctl(dirfd(dir), IOC_LOV_GETINFO,
848 ret = ioctl(dirfd(parent), IOC_LOV_GETINFO,
853 fprintf(stderr, "%s: IOC_LOV_GETINFO on %s failed: "
854 "%s.\n", __FUNCTION__, path, strerror(errno));
858 /* Check the time on osc. */
860 decision = find_time_check(st, param, 0);
864 if (decision != -1) {
872 /* Do not get down anymore? */
873 if (param->depth == param->maxdepth)
880 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data)
882 struct find_param *param = (struct find_param *)data;
887 int llapi_find(char *path, struct find_param *param)
889 char buf[PATH_MAX + 1];
892 ret = common_param_init(param);
897 strncpy(buf, path, strlen(path));
898 buf[strlen(path)] = '\0';
900 ret = llapi_semantic_traverse(buf, NULL, cb_find_init,
901 cb_common_fini, param);
903 find_param_fini(param);
904 return ret < 0 ? ret : 0;
907 static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data)
909 struct find_param *param = (struct find_param *)data;
912 LASSERT(parent != NULL || d != NULL);
915 if (!param->got_uuids) {
916 ret = setup_obd_uuids(d ? d : parent, path, param);
922 ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
923 (void *)¶m->lmd->lmd_lmm);
925 char *fname = strrchr(path, '/');
926 fname = (fname == NULL ? path : fname + 1);
928 strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen);
929 ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
930 (void *)¶m->lmd->lmd_lmm);
934 if (errno == ENODATA) {
935 if (!param->obduuid && !param->quiet)
936 printf("%s has no stripe info\n", path);
938 } else if (errno == ENOTTY) {
939 fprintf(stderr, "%s: '%s' not on a Lustre fs?\n",
942 err_msg("error: %s: %s failed for %s", __FUNCTION__,
943 d ? "LL_IOC_LOV_GETSTRIPE" :
944 "IOC_MDC_GETFILESTRIPE", path);
950 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
952 /* Do not get down anymore? */
953 if (param->depth == param->maxdepth)
960 int llapi_getstripe(char *path, struct find_param *param)
964 ret = common_param_init(param);
969 ret = llapi_semantic_traverse(path, NULL, cb_getstripe,
970 cb_common_fini, param);
971 find_param_fini(param);
972 return ret < 0 ? ret : 0;
975 int llapi_obd_statfs(char *path, __u32 type, __u32 index,
976 struct obd_statfs *stat_buf,
977 struct obd_uuid *uuid_buf)
980 char raw[OBD_MAX_IOCTL_BUFFER] = {'\0'};
982 struct obd_ioctl_data data = { 0 };
985 data.ioc_inlbuf1 = (char *)&type;
986 data.ioc_inllen1 = sizeof(__u32);
987 data.ioc_inlbuf2 = (char *)&index;
988 data.ioc_inllen2 = sizeof(__u32);
989 data.ioc_pbuf1 = (char *)stat_buf;
990 data.ioc_plen1 = sizeof(struct obd_statfs);
991 data.ioc_pbuf2 = (char *)uuid_buf;
992 data.ioc_plen2 = sizeof(struct obd_uuid);
994 if (obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) {
995 fprintf(stderr, "llapi_obd_statfs: error packing ioctl data\n");
999 fd = open(path, O_RDONLY);
1000 if (errno == EISDIR)
1001 fd = open(path, O_DIRECTORY | O_RDONLY);
1004 rc = errno ? -errno : -EBADF;
1005 err_msg("error: %s: opening '%s'", __FUNCTION__, path);
1008 rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
1010 rc = errno ? -errno : -EINVAL;
1016 #define MAX_STRING_SIZE 128
1017 #define DEVICES_LIST "/proc/fs/lustre/devices"
1019 int llapi_ping(char *obd_type, char *obd_name)
1021 char path[MAX_STRING_SIZE];
1025 snprintf(path, MAX_STRING_SIZE, "/proc/fs/lustre/%s/%s/ping",
1026 obd_type, obd_name);
1028 fd = open(path, O_WRONLY);
1031 fprintf(stderr, "error opening %s: %s\n", path, strerror(errno));
1035 rc = write(fd, buf, 1);
1043 int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb)
1045 char buf[MAX_STRING_SIZE];
1046 FILE *fp = fopen(DEVICES_LIST, "r");
1051 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
1056 while (fgets(buf, sizeof(buf), fp) != NULL) {
1057 char *obd_type_name = NULL;
1058 char *obd_name = NULL;
1059 char *obd_uuid = NULL;
1060 char rawbuf[OBD_MAX_IOCTL_BUFFER];
1061 char *bufl = rawbuf;
1063 struct obd_ioctl_data datal = { 0, };
1064 struct obd_statfs osfs_buffer;
1066 while(bufp[0] == ' ')
1069 for(i = 0; i < 3; i++) {
1070 obd_type_name = strsep(&bufp, " ");
1072 obd_name = strsep(&bufp, " ");
1073 obd_uuid = strsep(&bufp, " ");
1075 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
1077 memset(bufl, 0, sizeof(rawbuf));
1078 datal.ioc_pbuf1 = (char *)&osfs_buffer;
1079 datal.ioc_plen1 = sizeof(osfs_buffer);
1081 for (i = 0; i < type_num; i++) {
1082 if (strcmp(obd_type_name, obd_type[i]) != 0)
1085 cb(obd_type_name, obd_name, obd_uuid, args);
1092 static void do_target_check(char *obd_type_name, char *obd_name,
1093 char *obd_uuid, void *args)
1097 rc = llapi_ping(obd_type_name, obd_name);
1099 err_msg("error: check '%s'", obd_name);
1101 printf("%s active.\n", obd_name);
1105 int llapi_target_check(int type_num, char **obd_type, char *dir)
1107 return llapi_target_iterate(type_num, obd_type, NULL, do_target_check);
1110 #undef MAX_STRING_SIZE
1112 int llapi_catinfo(char *dir, char *keyword, char *node_name)
1114 char raw[OBD_MAX_IOCTL_BUFFER];
1115 char out[LLOG_CHUNK_SIZE];
1117 struct obd_ioctl_data data = { 0 };
1122 sprintf(key, "%s", keyword);
1123 memset(raw, 0, sizeof(raw));
1124 memset(out, 0, sizeof(out));
1125 data.ioc_inlbuf1 = key;
1126 data.ioc_inllen1 = strlen(key) + 1;
1128 data.ioc_inlbuf2 = node_name;
1129 data.ioc_inllen2 = strlen(node_name) + 1;
1131 data.ioc_pbuf1 = out;
1132 data.ioc_plen1 = sizeof(out);
1133 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
1137 root = opendir(dir);
1140 err_msg("open %s failed", dir);
1144 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
1146 err_msg("ioctl OBD_IOC_CATINFO failed");
1148 fprintf(stdout, "%s", data.ioc_pbuf1);
1154 /* Is this a lustre fs? */
1155 int llapi_is_lustre_mnttype(const char *type)
1157 return (strcmp(type, "lustre") == 0 || strcmp(type,"lustre_lite") == 0);
1160 /* Is this a lustre client fs? */
1161 int llapi_is_lustre_mnt(struct mntent *mnt)
1163 return (llapi_is_lustre_mnttype(mnt->mnt_type) &&
1164 strstr(mnt->mnt_fsname, ":/") != NULL);
1167 int llapi_quotacheck(char *mnt, int check_type)
1172 root = opendir(mnt);
1174 err_msg("open %s failed", mnt);
1178 rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
1184 int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
1190 root = opendir(mnt);
1192 err_msg("open %s failed", mnt);
1197 rc = ioctl(dirfd(root), LL_IOC_POLL_QUOTACHECK, qchk);
1201 if (poll_intvl < 30)
1209 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
1214 root = opendir(mnt);
1216 err_msg("open %s failed", mnt);
1220 rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
1226 static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data)
1228 struct find_param *param = (struct find_param *)data;
1232 LASSERT(parent != NULL || d != NULL);
1235 rc = ioctl(dirfd(d), LL_IOC_MDC_GETINFO,
1236 (void *)param->lmd);
1237 } else if (parent) {
1238 char *fname = strrchr(path, '/');
1239 fname = (fname == NULL ? path : fname + 1);
1241 strncpy((char *)param->lmd, fname, param->lumlen);
1242 rc = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO,
1243 (void *)param->lmd);
1249 if (errno == ENODATA) {
1250 if (!param->obduuid && !param->quiet)
1251 fprintf(stderr, "%s has no stripe info\n",
1254 } else if (errno != EISDIR) {
1256 err_msg("%s ioctl failed for %s.",
1257 d ? "LL_IOC_MDC_GETINFO" :
1258 "IOC_MDC_GETFILEINFO", path);
1263 st = ¶m->lmd->lmd_st;
1265 /* libc chown() will do extra check, and if the real owner is
1266 * the same as the ones to set, it won't fall into kernel, so
1267 * invoke syscall directly. */
1268 rc = syscall(SYS_chown, path, -1, -1);
1270 err_msg("error: chown %s (%u,%u)", path);
1272 rc = chmod(path, st->st_mode);
1274 err_msg("error: chmod %s (%hu)", path, st->st_mode);
1279 int llapi_quotachown(char *path, int flag)
1281 struct find_param param;
1284 memset(¶m, 0, sizeof(param));
1285 param.recursive = 1;
1289 ret = common_param_init(¶m);
1293 ret = llapi_semantic_traverse(path, NULL, cb_quotachown,
1296 find_param_fini(¶m);
1300 int llapi_getfacl(char *fname, char *cmd)
1302 struct rmtacl_ioctl_data data;
1303 char out[RMTACL_SIZE_MAX] = "";
1307 data.cmd_len = strlen(cmd) + 1;
1309 data.res_len = sizeof(out);
1311 fd = open(fname, 0);
1313 err_msg("open %s failed", fname);
1317 rc = ioctl(fd, LL_IOC_GETFACL, &data);
1319 if (errno == EBADE) {
1320 fprintf(stderr, "Please use getfacl directly!\n");
1323 err_msg("getfacl %s failed", fname);
1331 int llapi_setfacl(char *fname, char *cmd)
1333 struct rmtacl_ioctl_data data;
1334 char out[RMTACL_SIZE_MAX] = "";
1338 data.cmd_len = strlen(cmd) + 1;
1340 data.res_len = sizeof(out);
1342 fd = open(fname, 0);
1344 err_msg("open %s failed", fname);
1348 rc = ioctl(fd, LL_IOC_SETFACL, &data);
1350 if (errno == EBADE) {
1351 fprintf(stderr, "Please use setfacl directly!\n");
1353 } else if (errno == EOPNOTSUPP) {
1354 fprintf(stderr, "setfacl: %s: %s\n", fname, strerror(errno));
1357 err_msg("setfacl %s failed", fname);