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 <linux/types.h>
43 #include <linux/unistd.h>
45 #include <liblustre.h>
46 #include <linux/obd.h>
47 #include <linux/lustre_lib.h>
48 #include <lustre/lustre_user.h>
49 #include <linux/obd_lov.h>
51 #include <portals/ptlctl.h>
53 static void err_msg(char *fmt, ...)
56 int tmp_errno = errno;
59 vfprintf(stderr, fmt, args);
61 fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
64 int llapi_file_create(char *name, long stripe_size, int stripe_offset,
65 int stripe_count, int stripe_pattern)
67 struct lov_user_md lum = { 0 };
70 /* Initialize IOCTL striping pattern structure */
71 lum.lmm_magic = LOV_USER_MAGIC;
72 lum.lmm_pattern = stripe_pattern;
73 lum.lmm_stripe_size = stripe_size;
74 lum.lmm_stripe_count = stripe_count;
75 lum.lmm_stripe_offset = stripe_offset;
77 fd = open(name, O_CREAT | O_RDWR | O_LOV_DELAY_CREATE, 0644);
79 fd = open(name, O_DIRECTORY | O_RDONLY);
82 err_msg("unable to open '%s'",name);
87 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) {
88 char *errmsg = "stripe already set";
89 if (errno != EEXIST && errno != EALREADY)
90 errmsg = strerror(errno);
92 fprintf(stderr, "error on ioctl for '%s' (%d): %s\n",
97 err_msg("error on close for '%s' (%d)", name, fd);
104 int op_create_dir(char *name, int stripe_count)
106 struct ll_user_mkdir_stripe lums = { 0 };
111 dirname = rindex(name, '/');
119 lums.lums_nstripes = stripe_count;
120 lums.lums_namelen = strlen(dirname);
121 lums.lums_name = dirname;
122 /* XXX: Probably users might want to specify permissions as well? */
123 lums.lums_mode = 0755;
125 fd = open(name, O_RDONLY|O_DIRECTORY);
129 err_msg("unable to open '%s'",name);
134 if (ioctl(fd, LL_IOC_MDC_MKDIRSTRIPE, &lums)) {
135 char *errmsg = strerror(errno);
137 fprintf(stderr, "error on ioctl for '%s' (%d): %s\n",
142 err_msg("error on close for '%s' (%d)", name, fd);
149 /* short term backwards compat only */
150 int op_create_file(char *name, long stripe_size, int stripe_offset,
153 return llapi_file_create(name, stripe_size, stripe_offset,
162 struct obd_uuid *obduuid;
164 struct lov_user_md *data;
170 /* XXX Max obds per lov currently hardcoded to 1000 in lov/lov_obd.c */
171 #define MAX_LOV_UUID_COUNT 1000
172 #define OBD_NOT_FOUND (-1)
174 static int prepare_find(struct find_param *param)
176 if (param->showfid) {
177 param->datalen = PATH_MAX + 1;
178 if ((param->data = malloc(param->datalen)) == NULL) {
179 err_msg("unable to allocate %d bytes of memory for ioctl",
184 param->datalen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
185 if ((param->data = malloc(param->datalen)) == NULL) {
186 err_msg("unable to allocate %d bytes of memory for ioctl",
191 param->got_uuids = 0;
192 param->obdindex = OBD_NOT_FOUND;
196 static void cleanup_find(struct find_param *param)
199 free(param->obduuid);
204 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp,
205 __u32 *obdgens, int *ost_count)
207 struct obd_ioctl_data data = { 0, };
208 struct lov_desc desc = { 0, };
210 int max_ost_count, rc;
212 max_ost_count = (OBD_MAX_IOCTL_BUFFER - size_round(sizeof(data)) -
213 size_round(sizeof(desc))) /
214 (sizeof(*uuidp) + sizeof(*obdgens));
215 if (max_ost_count > *ost_count)
216 max_ost_count = *ost_count;
218 data.ioc_inllen1 = sizeof(desc);
219 data.ioc_inlbuf1 = (char *)&desc;
220 data.ioc_inllen2 = size_round(max_ost_count * sizeof(*uuidp));
221 data.ioc_inlbuf2 = (char *)uuidp;
222 data.ioc_inllen3 = size_round(max_ost_count * sizeof(*obdgens));
223 data.ioc_inlbuf3 = (char *)obdgens;
225 desc.ld_tgt_count = max_ost_count;
227 if (obd_ioctl_pack(&data, &buf, OBD_MAX_IOCTL_BUFFER)) {
228 fprintf(stderr, "internal buffer error packing\n");
233 rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
235 err_msg("error getting LOV config");
240 if (obd_ioctl_unpack(&data, buf, OBD_MAX_IOCTL_BUFFER)) {
241 fprintf(stderr, "invalid reply from ioctl");
246 *ost_count = desc.ld_tgt_count;
253 static int setup_obd_uuids(DIR *dir, char *dname, struct find_param *param)
255 struct obd_uuid uuids[1024], *uuidp;
256 __u32 obdgens[1024], *genp;
260 param->got_uuids = 1;
262 rc = llapi_lov_get_uuids(dirfd(dir), uuids, obdgens, &obdcount);
264 return (param->obduuid ? rc : 0);
269 if (param->obduuid) {
270 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
271 if (strncmp(param->obduuid->uuid, uuidp->uuid,
272 sizeof(*uuidp)) == 0) {
277 if (param->obdindex == OBD_NOT_FOUND) {
278 printf("unknown obduuid: %s\n", param->obduuid->uuid);
281 } else if (!param->quiet) {
283 printf("OBDS:\tobdidx\t\tobdgen\t\t obduuid\n");
286 for (i = 0; i < obdcount; i++, uuidp++, genp++) {
287 if (obd_uuid_empty(uuidp))
289 printf("\t%6u\t%14u\t\t %s\n", i, *genp, uuidp->uuid);
295 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *dname, char *fname,
296 int obdindex, int quiet, int header, int body)
298 int i, obdstripe = 0;
300 if (obdindex != OBD_NOT_FOUND) {
301 for (i = 0; i < lum->lmm_stripe_count; i++) {
302 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
303 printf("%s/%s\n", dname, fname);
309 printf("%s/%s\n", dname, fname);
313 /* if it's a directory */
314 if (*fname == '\0') {
315 if (header && (obdstripe == 1)) {
316 printf("count: %d, size: %d, offset: %d\n\n",
317 lum->lmm_stripe_count, lum->lmm_stripe_size,
318 (short int)lum->lmm_stripe_offset);
323 if (header && (obdstripe == 1)) {
324 printf("lmm_magic: 0x%08X\n", lum->lmm_magic);
325 printf("lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
326 printf("lmm_object_id: "LPX64"\n", lum->lmm_object_id);
327 printf("lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
328 printf("lmm_stripe_size: %u\n", lum->lmm_stripe_size);
329 printf("lmm_stripe_pattern: %x\n", lum->lmm_pattern);
333 if ((!quiet) && (obdstripe == 1))
334 printf("\tobdidx\t\t obdgen\t\t objid\t\tobjid\t\t group\n");
336 for (i = 0; i < lum->lmm_stripe_count; i++) {
337 int idx = lum->lmm_objects[i].l_ost_idx;
338 long long oid = lum->lmm_objects[i].l_object_id;
339 int gen = lum->lmm_objects[i].l_ost_gen;
340 long long gr = lum->lmm_objects[i].l_object_gr;
341 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
342 printf("\t%6u\t%14u\t%14llu\t%#13llx\t%14lld%s\n",
343 idx, gen, oid, oid, gr,
344 obdindex == idx ? " *" : "");
350 void llapi_lov_dump_user_lmm(struct find_param *param, char *dname, char *fname)
352 switch(*(__u32 *)param->data) { /* lum->lmm_magic */
353 case LOV_USER_MAGIC_V1:
354 lov_dump_user_lmm_v1(param->data, dname, fname, param->obdindex,
355 param->quiet, param->verbose,
356 (param->verbose || !param->obduuid));
359 printf("unknown lmm_magic: 0x%08X\n", *(__u32 *)param->data);
364 void llapi_dump_fid(struct find_param *param, char *dname, char *fname)
368 char path[PATH_MAX + 1];
370 n = snprintf(path, PATH_MAX, "%s/%s",
378 printf("%s:%*s"DLID4"\n", path, n, "",
379 OLID4((struct lustre_id *)param->data));
383 int llapi_file_get_stripe(char *path, struct lov_user_md *lum)
388 fname = strrchr(path, '/');
390 /* It should be a file (or other non-directory) */
392 dname = (char *)malloc(2);
398 dname = (char *)malloc(fname - path + 1);
401 strncpy(dname, path, fname - path);
402 dname[fname - path] = '\0';
406 if ((fd = open(dname, O_RDONLY)) == -1) {
411 strncpy((char *)lum, fname, sizeof(*lum));
412 if (ioctl(fd, IOC_MDC_GETSTRIPE, (void *)lum) == -1) {
426 int llapi_file_get_fid(char *path, void *data)
431 fname = strrchr(path, '/');
434 dname = (char *)malloc(2);
440 dname = (char *)malloc(fname - path + 1);
443 strncpy(dname, path, fname - path);
444 dname[fname - path] = '\0';
451 if ((fd = open(dname, O_RDONLY)) == -1) {
456 strncpy((char *)data, fname, strlen(fname));
457 if (ioctl(fd, IOC_MDC_SHOWFID, (void *)data) == -1) {
470 /* short term backwards compat only */
471 int op_get_file_stripe(char *path, struct lov_user_md *lum)
473 return llapi_file_get_stripe(path, lum);
476 static int process_file(DIR *dir, char *dname, char *fname,
477 struct find_param *param)
481 if (param->showfid) {
482 char path[PATH_MAX + 1];
484 snprintf(path, PATH_MAX, "%s/%s", dname, fname);
485 rc = llapi_file_get_fid(path, (void *)param->data);
487 err_msg("IOC_MDC_SHOWFID ioctl failed");
490 llapi_dump_fid(param, dname, fname);
492 strncpy((char *)param->data, fname, param->datalen);
493 rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, (void *)param->data);
495 if (errno == ENODATA) {
496 if (!param->obduuid && !param->quiet)
498 "%s/%s has no stripe info\n",
501 } else if (errno == EISDIR) {
502 fprintf(stderr, "process_file on directory %s/%s!\n",
504 /* add fname to directory list; */
507 err_msg("IOC_MDC_GETSTRIPE ioctl failed");
512 llapi_lov_dump_user_lmm(param, dname, fname);
517 /* some 64bit libcs implement readdir64() by calling sys_getdents(). the
518 * kernel's sys_getdents() doesn't return d_type. */
519 unsigned char handle_dt_unknown(char *parent, char *entry)
521 char path[PATH_MAX + 1];
524 ret = snprintf(path, PATH_MAX, "%s/%s", parent, entry);
528 fd = open(path, O_DIRECTORY|O_RDONLY);
530 if (errno == ENOTDIR)
531 return DT_REG; /* kind of a lie */
538 static int process_dir(DIR *dir, char *dname, struct find_param *param)
540 char path[PATH_MAX + 1];
541 struct dirent64 *dirp;
545 /* retrieve dir's stripe info */
546 if (param->showfid) {
547 rc = llapi_file_get_fid(dname, (void *)param->data);
549 err_msg("IOC_MDC_SHOWFID ioctl failed");
552 llapi_dump_fid(param, dname, "");
554 if (!param->got_uuids) {
555 rc = setup_obd_uuids(dir, dname, param);
560 strncpy((char *)param->data, dname, param->datalen);
561 rc = ioctl(dirfd(dir), LL_IOC_LOV_GETSTRIPE, (void *)param->data);
563 if (errno == ENODATA) {
564 if (!param->obduuid && param->verbose)
565 printf("%s/%s has no stripe info\n", dname, "");
568 err_msg("IOC_MDC_GETSTRIPE ioctl failed");
572 llapi_lov_dump_user_lmm(param, dname, "");
576 /* Handle the contents of the directory */
577 while ((dirp = readdir64(dir)) != NULL) {
578 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
581 if (dirp->d_type == DT_UNKNOWN)
582 dirp->d_type = handle_dt_unknown(dname, dirp->d_name);
584 switch (dirp->d_type) {
586 err_msg("\"%s\" is UNKNOWN type %d", dirp->d_name,
588 /* If we cared we could stat the file to determine
589 * type and continue on here, but we don't since we
590 * know d_type should be valid for lustre and this
591 * tool only makes sense for lustre filesystems. */
594 if (!param->recursive)
598 strcat(path, dirp->d_name);
599 subdir = opendir(path);
600 if (subdir == NULL) {
601 err_msg("\"%.40s\" opendir failed", path);
604 rc = process_dir(subdir, path, param);
610 rc = process_file(dir, dname, dirp->d_name, param);
622 static int process_path(char *path, struct find_param *param)
628 fname = strrchr(path, '/');
629 if (fname != NULL && fname[1] == '\0') {
630 /* Trailing '/', it must be a dir */
634 err_msg("\"%.40s\" opendir failed", path);
637 rc = process_dir(dir, path, param);
640 } else if ((dir = opendir(path)) != NULL) {
641 /* No trailing '/', but it is still a dir */
642 rc = process_dir(dir, path, param);
645 /* It must be a file (or other non-directory) */
654 dir = opendir(dname);
656 err_msg("\"%.40s\" opendir failed", dname);
659 if (!param->showfid) {
660 if (!param->got_uuids)
661 rc = setup_obd_uuids(dir, dname, param);
664 rc = process_file(dir, dname, fname, param);
672 int llapi_find(char *path, struct obd_uuid *obduuid, int recursive,
673 int verbose, int quiet, int showfid)
675 struct find_param param;
678 memset(¶m, 0, sizeof(param));
679 param.recursive = recursive;
680 param.showfid = showfid;
681 param.verbose = verbose;
684 param.obduuid = malloc(sizeof(*obduuid));
685 if (param.obduuid == NULL) {
689 memcpy(param.obduuid, obduuid, sizeof(*obduuid));
692 ret = prepare_find(¶m);
696 process_path(path, ¶m);
698 cleanup_find(¶m);
702 #define MAX_STRING_SIZE 128
703 #define DEVICES_LIST "/proc/fs/lustre/devices"
705 int llapi_target_check(int type_num, char **obd_type, char *dir)
707 char buf[MAX_STRING_SIZE];
708 FILE *fp = fopen(DEVICES_LIST, "r");
713 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
714 strerror(rc = errno));
718 while (fgets(buf, sizeof(buf), fp) != NULL) {
719 char *obd_type_name = NULL;
720 char *obd_name = NULL;
721 char rawbuf[OBD_MAX_IOCTL_BUFFER];
724 int max = sizeof(rawbuf);
725 struct obd_ioctl_data datal;
726 struct obd_statfs osfs_buffer;
728 while(bufp[0] == ' ')
731 for(i = 0; i < 3; i++) {
732 obd_type_name = strsep(&bufp, " ");
734 obd_name = strsep(&bufp, " ");
736 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
738 memset(bufl, 0, sizeof(rawbuf));
739 datal.ioc_pbuf1 = (char *)&osfs_buffer;
740 datal.ioc_plen1 = sizeof(osfs_buffer);
742 for (i = 0; i < type_num; i++)
743 if (strcmp(obd_type_name, obd_type[i]) == 0) {
744 datal.ioc_inlbuf1 = obd_name;
745 datal.ioc_inllen1 = strlen(obd_name) + 1;
747 obd_ioctl_pack(&datal,&bufl,max);
749 rc = ioctl(dirfd(opendir(dir)), OBD_IOC_PING,
753 fprintf(stderr, "error: check %s: %s\n",
754 obd_name, strerror(rc = errno));
756 printf("%s active.\n",obd_name);
765 #undef MAX_STRING_SIZE
767 int llapi_catinfo(char *dir, char *keyword, char *node_name)
769 char raw[OBD_MAX_IOCTL_BUFFER];
770 char out[LLOG_CHUNK_SIZE];
772 struct obd_ioctl_data data;
777 sprintf(key, "%s", keyword);
778 memset(raw, 0, sizeof(buf));
779 memset(out, 0, sizeof(out));
780 data.ioc_inlbuf1 = key;
781 data.ioc_inllen1 = strlen(key) + 1;
783 data.ioc_inlbuf2 = node_name;
784 data.ioc_inllen2 = strlen(node_name) + 1;
786 data.ioc_pbuf1 = out;
787 data.ioc_plen1 = sizeof(out);
788 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
794 err_msg("open %s failed", dir);
798 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
800 err_msg("ioctl OBD_IOC_CATINFO failed");
802 fprintf(stdout, "%s", data.ioc_pbuf1);
808 int llapi_is_lustre_mnttype(char *type)
810 return (strcmp(type,"lustre") == 0 || strcmp(type,"lustre_lite") == 0);