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,
161 struct obd_uuid *obduuid;
163 struct lov_user_md *lum;
168 /* XXX Max obds per lov currently hardcoded to 1000 in lov/lov_obd.c */
169 #define MAX_LOV_UUID_COUNT 1000
170 #define OBD_NOT_FOUND (-1)
172 static int prepare_find(struct find_param *param)
174 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
175 if ((param->lum = malloc(param->lumlen)) == NULL) {
176 err_msg("unable to allocate %d bytes of memory for ioctl",
181 param->got_uuids = 0;
182 param->obdindex = OBD_NOT_FOUND;
187 static void cleanup_find(struct find_param *param)
190 free(param->obduuid);
195 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
197 struct obd_ioctl_data data = { 0, };
198 struct lov_desc desc = { 0, };
200 int max_ost_count, rc;
203 max_ost_count = (OBD_MAX_IOCTL_BUFFER - size_round(sizeof(data)) -
204 size_round(sizeof(desc))) /
205 (sizeof(*uuidp) + sizeof(*obdgens));
206 if (max_ost_count > *ost_count)
207 max_ost_count = *ost_count;
209 obdgens = malloc(size_round(max_ost_count * sizeof(*obdgens)));
211 err_msg("no memory for %d generation #'s", max_ost_count);
215 data.ioc_inllen1 = sizeof(desc);
216 data.ioc_inlbuf1 = (char *)&desc;
217 data.ioc_inllen2 = size_round(max_ost_count * sizeof(*uuidp));
218 data.ioc_inlbuf2 = (char *)uuidp;
219 data.ioc_inllen3 = size_round(max_ost_count * sizeof(*obdgens));
220 data.ioc_inlbuf3 = (char *)obdgens;
222 desc.ld_tgt_count = max_ost_count;
224 if (obd_ioctl_pack(&data, &buf, OBD_MAX_IOCTL_BUFFER)) {
225 fprintf(stderr, "internal buffer error packing\n");
230 rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
232 err_msg("error getting LOV config");
237 if (obd_ioctl_unpack(&data, buf, OBD_MAX_IOCTL_BUFFER)) {
238 fprintf(stderr, "invalid reply from ioctl");
243 *ost_count = desc.ld_tgt_count;
251 static int setup_obd_uuids(DIR *dir, char *dname, struct find_param *param)
253 struct obd_uuid uuids[1024], *uuidp;
257 param->got_uuids = 1;
259 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
261 return (param->obduuid ? rc : 0);
266 if (param->obduuid) {
267 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
268 if (strncmp(param->obduuid->uuid, uuidp->uuid,
269 sizeof(*uuidp)) == 0) {
274 if (param->obdindex == OBD_NOT_FOUND) {
275 printf("unknown obduuid: %s\n", param->obduuid->uuid);
278 } else if (!param->quiet) {
280 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++)
281 printf("%4d: %s\n", i, uuidp->uuid);
287 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *dname, char *fname,
288 int obdindex, int quiet, int header, int body)
290 int i, obdstripe = 0;
292 if (obdindex != OBD_NOT_FOUND) {
293 for (i = 0; i < lum->lmm_stripe_count; i++) {
294 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
295 printf("%s/%s\n", dname, fname);
301 printf("%s/%s\n", dname, fname);
305 /* if it's a directory */
306 if (*fname == '\0') {
307 if (header && (obdstripe == 1)) {
308 printf("count: %d, size: %d, offset: %d\n\n",
309 lum->lmm_stripe_count, lum->lmm_stripe_size,
310 (short int)lum->lmm_stripe_offset);
315 if (header && (obdstripe == 1)) {
316 printf("lmm_magic: 0x%08X\n", lum->lmm_magic);
317 printf("lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
318 printf("lmm_object_id: "LPX64"\n", lum->lmm_object_id);
319 printf("lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
320 printf("lmm_stripe_size: %u\n", lum->lmm_stripe_size);
321 printf("lmm_stripe_pattern: %x\n", lum->lmm_pattern);
325 if ((!quiet) && (obdstripe == 1))
326 printf("\tobdidx\t\t objid\t\tobjid\t\t group\n");
328 for (i = 0; i < lum->lmm_stripe_count; i++) {
329 int idx = lum->lmm_objects[i].l_ost_idx;
330 long long oid = lum->lmm_objects[i].l_object_id;
331 long long gr = lum->lmm_objects[i].l_object_gr;
332 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
333 printf("\t%6u\t%14llu\t%#13llx\t%14llu%s\n",
335 obdindex == idx ? " *" : "");
341 void llapi_lov_dump_user_lmm(struct find_param *param, char *dname, char *fname)
343 switch(*(__u32 *)param->lum) { /* lum->lmm_magic */
344 case LOV_USER_MAGIC_V1:
345 lov_dump_user_lmm_v1(param->lum, dname, fname, param->obdindex,
346 param->quiet, param->verbose,
347 (param->verbose || !param->obduuid));
350 printf("unknown lmm_magic: 0x%08X\n", *(__u32 *)param->lum);
355 int llapi_file_get_stripe(char *path, struct lov_user_md *lum)
360 fname = strrchr(path, '/');
362 /* It should be a file (or other non-directory) */
364 dname = (char *)malloc(2);
370 dname = (char *)malloc(fname - path + 1);
373 strncpy(dname, path, fname - path);
374 dname[fname - path + 1] = '\0';
378 if ((fd = open(dname, O_RDONLY)) == -1) {
383 strncpy((char *)lum, fname, sizeof(*lum));
384 if (ioctl(fd, IOC_MDC_GETSTRIPE, (void *)lum) == -1) {
398 /* short term backwards compat only */
399 int op_get_file_stripe(char *path, struct lov_user_md *lum)
401 return llapi_file_get_stripe(path, lum);
404 static int process_file(DIR *dir, char *dname, char *fname,
405 struct find_param *param)
409 strncpy((char *)param->lum, fname, param->lumlen);
411 rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, (void *)param->lum);
413 if (errno == ENODATA) {
414 if (!param->obduuid && !param->quiet)
416 "%s/%s has no stripe info\n",
419 } else if (errno == EISDIR) {
420 fprintf(stderr, "process_file on directory %s/%s!\n",
422 /* add fname to directory list; */
425 err_msg("IOC_MDC_GETSTRIPE ioctl failed");
431 llapi_lov_dump_user_lmm(param, dname, fname);
436 /* some 64bit libcs implement readdir64() by calling sys_getdents(). the
437 * kernel's sys_getdents() doesn't return d_type. */
438 unsigned char handle_dt_unknown(char *parent, char *entry)
440 char path[PATH_MAX + 1];
443 ret = snprintf(path, PATH_MAX, "%s/%s", parent, entry);
447 fd = open(path, O_DIRECTORY|O_RDONLY);
449 if (errno == ENOTDIR)
450 return DT_REG; /* kind of a lie */
457 static int process_dir(DIR *dir, char *dname, struct find_param *param)
459 struct dirent64 *dirp;
464 if (!param->got_uuids) {
465 rc = setup_obd_uuids(dir, dname, param);
470 /* retrieve dir's stripe info */
471 strncpy((char *)param->lum, dname, param->lumlen);
472 rc = ioctl(dirfd(dir), LL_IOC_LOV_GETSTRIPE, (void *)param->lum);
474 if (errno == ENODATA) {
475 if (!param->obduuid && param->verbose)
476 printf("%s/%s has no stripe info\n", dname, "");
479 err_msg("IOC_MDC_GETSTRIPE ioctl failed");
483 llapi_lov_dump_user_lmm(param, dname, "");
486 /* Handle the contents of the directory */
487 while ((dirp = readdir64(dir)) != NULL) {
488 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
491 if (dirp->d_type == DT_UNKNOWN)
492 dirp->d_type = handle_dt_unknown(dname, dirp->d_name);
494 switch (dirp->d_type) {
496 err_msg("\"%s\" is UNKNOWN type %d", dirp->d_name,
498 /* If we cared we could stat the file to determine
499 * type and continue on here, but we don't since we
500 * know d_type should be valid for lustre and this
501 * tool only makes sense for lustre filesystems. */
505 if (!param->recursive)
509 strcat(path, dirp->d_name);
510 subdir = opendir(path);
511 if (subdir == NULL) {
512 err_msg("\"%.40s\" opendir failed", path);
515 rc = process_dir(subdir, path, param);
521 rc = process_file(dir, dname, dirp->d_name, param);
533 static int process_path(char *path, struct find_param *param)
539 fname = strrchr(path, '/');
540 if (fname != NULL && fname[1] == '\0') {
541 /* Trailing '/', it must be a dir */
545 err_msg("\"%.40s\" opendir failed", path);
548 rc = process_dir(dir, path, param);
551 } else if ((dir = opendir(path)) != NULL) {
552 /* No trailing '/', but it is still a dir */
553 rc = process_dir(dir, path, param);
556 /* It must be a file (or other non-directory) */
565 dir = opendir(dname);
567 err_msg("\"%.40s\" opendir failed", dname);
570 if (!param->got_uuids)
571 rc = setup_obd_uuids(dir, dname, param);
573 rc = process_file(dir, dname, fname, param);
581 int llapi_find(char *path, struct obd_uuid *obduuid, int recursive,
582 int verbose, int quiet)
584 struct find_param param;
587 memset(¶m, 0, sizeof(param));
588 param.recursive = recursive;
589 param.verbose = verbose;
592 param.obduuid = malloc(sizeof(*obduuid));
593 if (param.obduuid == NULL) {
597 memcpy(param.obduuid, obduuid, sizeof(*obduuid));
600 ret = prepare_find(¶m);
604 process_path(path, ¶m);
606 cleanup_find(¶m);
610 #define MAX_STRING_SIZE 128
611 #define DEVICES_LIST "/proc/fs/lustre/devices"
613 int llapi_target_check(int type_num, char **obd_type, char *dir)
615 char buf[MAX_STRING_SIZE];
616 FILE *fp = fopen(DEVICES_LIST, "r");
621 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
622 strerror(rc = errno));
626 while (fgets(buf, sizeof(buf), fp) != NULL) {
627 char *obd_type_name = NULL;
628 char *obd_name = NULL;
629 char rawbuf[OBD_MAX_IOCTL_BUFFER];
632 int max = sizeof(rawbuf);
633 struct obd_ioctl_data datal;
634 struct obd_statfs osfs_buffer;
636 while(bufp[0] == ' ')
639 for(i = 0; i < 3; i++) {
640 obd_type_name = strsep(&bufp, " ");
642 obd_name = strsep(&bufp, " ");
644 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
646 memset(bufl, 0, sizeof(rawbuf));
647 datal.ioc_pbuf1 = (char *)&osfs_buffer;
648 datal.ioc_plen1 = sizeof(osfs_buffer);
650 for (i = 0; i < type_num; i++)
651 if (strcmp(obd_type_name, obd_type[i]) == 0) {
652 datal.ioc_inlbuf1 = obd_name;
653 datal.ioc_inllen1 = strlen(obd_name) + 1;
655 obd_ioctl_pack(&datal,&bufl,max);
657 rc = ioctl(dirfd(opendir(dir)), OBD_IOC_PING,
661 fprintf(stderr, "error: check %s: %s\n",
662 obd_name, strerror(rc = errno));
664 printf("%s active.\n",obd_name);
673 #undef MAX_STRING_SIZE
675 int llapi_catinfo(char *dir, char *keyword, char *node_name)
677 char raw[OBD_MAX_IOCTL_BUFFER];
678 char out[LLOG_CHUNK_SIZE];
680 struct obd_ioctl_data data;
685 sprintf(key, "%s", keyword);
686 memset(raw, 0, sizeof(buf));
687 memset(out, 0, sizeof(out));
688 data.ioc_inlbuf1 = key;
689 data.ioc_inllen1 = strlen(key) + 1;
691 data.ioc_inlbuf2 = node_name;
692 data.ioc_inllen2 = strlen(node_name) + 1;
694 data.ioc_pbuf1 = out;
695 data.ioc_plen1 = sizeof(out);
696 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
702 err_msg("open %s failed", dir);
706 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
708 err_msg("ioctl OBD_IOC_CATINFO failed");
710 fprintf(stdout, "%s", data.ioc_pbuf1);
716 int llapi_is_lustre_mnttype(char *type)
718 return (strcmp(type,"lustre") == 0 || strcmp(type,"lustre_lite") == 0);