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;
169 /* XXX Max obds per lov currently hardcoded to 1000 in lov/lov_obd.c */
170 #define MAX_LOV_UUID_COUNT 1000
171 #define OBD_NOT_FOUND (-1)
173 static int prepare_find(struct find_param *param)
175 param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT);
176 if ((param->lum = malloc(param->lumlen)) == NULL) {
177 err_msg("unable to allocate %d bytes of memory for ioctl",
182 param->got_uuids = 0;
183 param->obdindex = OBD_NOT_FOUND;
188 static void cleanup_find(struct find_param *param)
191 free(param->obduuid);
196 int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, __u32 *obdgens,
199 struct obd_ioctl_data data = { 0, };
200 struct lov_desc desc = { 0, };
202 int max_ost_count, rc;
204 max_ost_count = (OBD_MAX_IOCTL_BUFFER - size_round(sizeof(data)) -
205 size_round(sizeof(desc))) /
206 (sizeof(*uuidp) + sizeof(*obdgens));
207 if (max_ost_count > *ost_count)
208 max_ost_count = *ost_count;
210 data.ioc_inllen1 = sizeof(desc);
211 data.ioc_inlbuf1 = (char *)&desc;
212 data.ioc_inllen2 = size_round(max_ost_count * sizeof(*uuidp));
213 data.ioc_inlbuf2 = (char *)uuidp;
214 data.ioc_inllen3 = size_round(max_ost_count * sizeof(*obdgens));
215 data.ioc_inlbuf3 = (char *)obdgens;
217 desc.ld_tgt_count = max_ost_count;
219 if (obd_ioctl_pack(&data, &buf, OBD_MAX_IOCTL_BUFFER)) {
220 fprintf(stderr, "internal buffer error packing\n");
225 rc = ioctl(fd, OBD_IOC_LOV_GET_CONFIG, buf);
227 err_msg("error getting LOV config");
232 if (obd_ioctl_unpack(&data, buf, OBD_MAX_IOCTL_BUFFER)) {
233 fprintf(stderr, "invalid reply from ioctl");
238 *ost_count = desc.ld_tgt_count;
245 static int setup_obd_uuids(DIR *dir, char *dname, struct find_param *param)
247 struct obd_uuid uuids[1024], *uuidp;
248 __u32 obdgens[1024], *genp;
253 param->got_uuids = 1;
255 rc = llapi_lov_get_uuids(dirfd(dir), uuids, obdgens, &obdcount);
257 return (param->obduuid ? rc : 0);
262 if (param->obduuid) {
263 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
264 if (strncmp(param->obduuid->uuid, uuidp->uuid,
265 sizeof(*uuidp)) == 0) {
270 if (param->obdindex == OBD_NOT_FOUND) {
271 printf("unknown obduuid: %s\n", param->obduuid->uuid);
274 } else if (!param->quiet) {
276 printf("OBDS:\tobdidx\t\tobdgen\t\t obduuid\n");
279 for (i = 0; i < obdcount; i++, uuidp++, genp++) {
280 if (obd_uuid_empty(uuidp))
282 printf("\t%6u\t%14u\t\t %s\n", i, *genp, uuidp->uuid);
288 void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *dname, char *fname,
289 int obdindex, int quiet, int header, int body)
291 int i, obdstripe = 0;
293 if (obdindex != OBD_NOT_FOUND) {
294 for (i = 0; i < lum->lmm_stripe_count; i++) {
295 if (obdindex == lum->lmm_objects[i].l_ost_idx) {
296 printf("%s/%s\n", dname, fname);
302 printf("%s/%s\n", dname, fname);
306 /* if it's a directory */
307 if (*fname == '\0') {
308 if (header && (obdstripe == 1)) {
309 printf("count: %d, size: %d, offset: %d\n\n",
310 lum->lmm_stripe_count, lum->lmm_stripe_size,
311 (short int)lum->lmm_stripe_offset);
316 if (header && (obdstripe == 1)) {
317 printf("lmm_magic: 0x%08X\n", lum->lmm_magic);
318 printf("lmm_object_gr: "LPX64"\n", lum->lmm_object_gr);
319 printf("lmm_object_id: "LPX64"\n", lum->lmm_object_id);
320 printf("lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count);
321 printf("lmm_stripe_size: %u\n", lum->lmm_stripe_size);
322 printf("lmm_stripe_pattern: %x\n", lum->lmm_pattern);
326 if ((!quiet) && (obdstripe == 1))
327 printf("\tobdidx\t\t obdgen\t\t objid\t\tobjid\t\t group\n");
329 for (i = 0; i < lum->lmm_stripe_count; i++) {
330 int idx = lum->lmm_objects[i].l_ost_idx;
331 long long oid = lum->lmm_objects[i].l_object_id;
332 int gen = lum->lmm_objects[i].l_ost_gen;
333 long long gr = lum->lmm_objects[i].l_object_gr;
334 if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
335 printf("\t%6u\t%14u\t%14llu\t%#13llx\t%14lld%s\n",
336 idx, gen, oid, oid, gr,
337 obdindex == idx ? " *" : "");
343 void llapi_lov_dump_user_lmm(struct find_param *param, char *dname, char *fname)
345 switch(*(__u32 *)param->lum) { /* lum->lmm_magic */
346 case LOV_USER_MAGIC_V1:
347 lov_dump_user_lmm_v1(param->lum, dname, fname, param->obdindex,
348 param->quiet, param->verbose,
349 (param->verbose || !param->obduuid));
352 printf("unknown lmm_magic: 0x%08X\n", *(__u32 *)param->lum);
357 int llapi_file_get_stripe(char *path, struct lov_user_md *lum)
362 fname = strrchr(path, '/');
364 /* It should be a file (or other non-directory) */
366 dname = (char *)malloc(2);
372 dname = (char *)malloc(fname - path + 1);
375 strncpy(dname, path, fname - path);
376 dname[fname - path + 1] = '\0';
380 if ((fd = open(dname, O_RDONLY)) == -1) {
385 strncpy((char *)lum, fname, sizeof(*lum));
386 if (ioctl(fd, IOC_MDC_GETSTRIPE, (void *)lum) == -1) {
400 /* short term backwards compat only */
401 int op_get_file_stripe(char *path, struct lov_user_md *lum)
403 return llapi_file_get_stripe(path, lum);
406 static int process_file(DIR *dir, char *dname, char *fname,
407 struct find_param *param)
411 strncpy((char *)param->lum, fname, param->lumlen);
413 rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, (void *)param->lum);
415 if (errno == ENODATA) {
416 if (!param->obduuid && !param->quiet)
418 "%s/%s has no stripe info\n",
421 } else if (errno == EISDIR) {
422 fprintf(stderr, "process_file on directory %s/%s!\n",
424 /* add fname to directory list; */
427 err_msg("IOC_MDC_GETSTRIPE ioctl failed");
433 llapi_lov_dump_user_lmm(param, dname, fname);
438 /* some 64bit libcs implement readdir64() by calling sys_getdents(). the
439 * kernel's sys_getdents() doesn't return d_type. */
440 unsigned char handle_dt_unknown(char *parent, char *entry)
442 char path[PATH_MAX + 1];
445 ret = snprintf(path, PATH_MAX, "%s/%s", parent, entry);
449 fd = open(path, O_DIRECTORY|O_RDONLY);
451 if (errno == ENOTDIR)
452 return DT_REG; /* kind of a lie */
459 static int process_dir(DIR *dir, char *dname, struct find_param *param)
461 struct dirent64 *dirp;
466 if (!param->got_uuids) {
467 rc = setup_obd_uuids(dir, dname, param);
472 /* retrieve dir's stripe info */
473 strncpy((char *)param->lum, dname, param->lumlen);
474 rc = ioctl(dirfd(dir), LL_IOC_LOV_GETSTRIPE, (void *)param->lum);
476 if (errno == ENODATA) {
477 if (!param->obduuid && param->verbose)
478 printf("%s/%s has no stripe info\n", dname, "");
481 err_msg("IOC_MDC_GETSTRIPE ioctl failed");
485 llapi_lov_dump_user_lmm(param, dname, "");
488 /* Handle the contents of the directory */
489 while ((dirp = readdir64(dir)) != NULL) {
490 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
493 if (dirp->d_type == DT_UNKNOWN)
494 dirp->d_type = handle_dt_unknown(dname, dirp->d_name);
496 switch (dirp->d_type) {
498 err_msg("\"%s\" is UNKNOWN type %d", dirp->d_name,
500 /* If we cared we could stat the file to determine
501 * type and continue on here, but we don't since we
502 * know d_type should be valid for lustre and this
503 * tool only makes sense for lustre filesystems. */
507 if (!param->recursive)
511 strcat(path, dirp->d_name);
512 subdir = opendir(path);
513 if (subdir == NULL) {
514 err_msg("\"%.40s\" opendir failed", path);
517 rc = process_dir(subdir, path, param);
523 rc = process_file(dir, dname, dirp->d_name, param);
535 static int process_path(char *path, struct find_param *param)
541 fname = strrchr(path, '/');
542 if (fname != NULL && fname[1] == '\0') {
543 /* Trailing '/', it must be a dir */
547 err_msg("\"%.40s\" opendir failed", path);
550 rc = process_dir(dir, path, param);
553 } else if ((dir = opendir(path)) != NULL) {
554 /* No trailing '/', but it is still a dir */
555 rc = process_dir(dir, path, param);
558 /* It must be a file (or other non-directory) */
567 dir = opendir(dname);
569 err_msg("\"%.40s\" opendir failed", dname);
572 if (!param->got_uuids)
573 rc = setup_obd_uuids(dir, dname, param);
575 rc = process_file(dir, dname, fname, param);
583 int llapi_find(char *path, struct obd_uuid *obduuid, int recursive,
584 int verbose, int quiet)
586 struct find_param param;
589 memset(¶m, 0, sizeof(param));
590 param.recursive = recursive;
591 param.verbose = verbose;
594 param.obduuid = malloc(sizeof(*obduuid));
595 if (param.obduuid == NULL) {
599 memcpy(param.obduuid, obduuid, sizeof(*obduuid));
602 ret = prepare_find(¶m);
606 process_path(path, ¶m);
608 cleanup_find(¶m);
612 #define MAX_STRING_SIZE 128
613 #define DEVICES_LIST "/proc/fs/lustre/devices"
615 int llapi_target_check(int type_num, char **obd_type, char *dir)
617 char buf[MAX_STRING_SIZE];
618 FILE *fp = fopen(DEVICES_LIST, "r");
623 fprintf(stderr, "error: %s opening "DEVICES_LIST"\n",
624 strerror(rc = errno));
628 while (fgets(buf, sizeof(buf), fp) != NULL) {
629 char *obd_type_name = NULL;
630 char *obd_name = NULL;
631 char rawbuf[OBD_MAX_IOCTL_BUFFER];
634 int max = sizeof(rawbuf);
635 struct obd_ioctl_data datal;
636 struct obd_statfs osfs_buffer;
638 while(bufp[0] == ' ')
641 for(i = 0; i < 3; i++) {
642 obd_type_name = strsep(&bufp, " ");
644 obd_name = strsep(&bufp, " ");
646 memset(&osfs_buffer, 0, sizeof (osfs_buffer));
648 memset(bufl, 0, sizeof(rawbuf));
649 datal.ioc_pbuf1 = (char *)&osfs_buffer;
650 datal.ioc_plen1 = sizeof(osfs_buffer);
652 for (i = 0; i < type_num; i++)
653 if (strcmp(obd_type_name, obd_type[i]) == 0) {
654 datal.ioc_inlbuf1 = obd_name;
655 datal.ioc_inllen1 = strlen(obd_name) + 1;
657 obd_ioctl_pack(&datal,&bufl,max);
659 rc = ioctl(dirfd(opendir(dir)), OBD_IOC_PING,
663 fprintf(stderr, "error: check %s: %s\n",
664 obd_name, strerror(rc = errno));
666 printf("%s active.\n",obd_name);
675 #undef MAX_STRING_SIZE
677 int llapi_catinfo(char *dir, char *keyword, char *node_name)
679 char raw[OBD_MAX_IOCTL_BUFFER];
680 char out[LLOG_CHUNK_SIZE];
682 struct obd_ioctl_data data;
687 sprintf(key, "%s", keyword);
688 memset(raw, 0, sizeof(buf));
689 memset(out, 0, sizeof(out));
690 data.ioc_inlbuf1 = key;
691 data.ioc_inllen1 = strlen(key) + 1;
693 data.ioc_inlbuf2 = node_name;
694 data.ioc_inllen2 = strlen(node_name) + 1;
696 data.ioc_pbuf1 = out;
697 data.ioc_plen1 = sizeof(out);
698 rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
704 err_msg("open %s failed", dir);
708 rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
710 err_msg("ioctl OBD_IOC_CATINFO failed");
712 fprintf(stdout, "%s", data.ioc_pbuf1);
718 int llapi_is_lustre_mnttype(char *type)
720 return (strcmp(type,"lustre") == 0 || strcmp(type,"lustre_lite") == 0);