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>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <sys/ioctl.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
38 #include <linux/lustre_lib.h>
39 #include <linux/lustre_idl.h>
40 #include <linux/lustre_dlm.h>
42 #include <libxml/xmlmemory.h>
43 #include <libxml/parser.h>
49 #include <netinet/in.h>
53 #include <asm/page.h> /* needed for PAGE_SIZE - rread */
56 #include <linux/list.h>
67 # define MAX_SHMEM_COUNT 1024
68 static long long *shared_counters;
69 static long long counter_snapshot[2][MAX_SHMEM_COUNT];
70 struct timeval prev_time;
73 static int jt_newdev(int argc, char **argv);
74 static int jt_attach(int argc, char **argv);
75 static int jt_setup(int argc, char **argv);
79 uint64_t conn_addr = -1;
87 #define IOCINIT(data) \
89 memset(&data, 0, sizeof(data)); \
90 data.ioc_version = OBD_IOCTL_VERSION; \
91 data.ioc_addr = conn_addr; \
92 data.ioc_cookie = conn_cookie; \
93 data.ioc_len = sizeof(data); \
95 fprintf(stderr, "No device open, use device\n"); \
101 pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L",
120 0, 0, # struct list_head
126 char *obdo_print(struct obdo *obd)
130 sprintf(buf, "id: %Ld\ngrp: %Ld\natime: %Ld\nmtime: %Ld\nctime: %Ld\n"
131 "size: %Ld\nblocks: %Ld\nblksize: %d\nmode: %o\nuid: %d\n"
132 "gid: %d\nflags: %x\nobdflags: %x\nnlink: %d,\nvalid %x\n",
144 obd->o_flags, obd->o_obdflags, obd->o_nlink, obd->o_valid);
148 static char *cmdname(char *func)
150 static char buf[512];
153 sprintf(buf, "%s-%d", func, thread);
160 int getfd(char *func)
163 fd = open("/dev/obd", O_RDWR);
165 fprintf(stderr, "error: %s: opening /dev/obd: %s\n",
166 cmdname(func), strerror(errno));
172 #define difftime(a, b) \
173 ((double)(a)->tv_sec - (b)->tv_sec + \
174 ((double)((a)->tv_usec - (b)->tv_usec) / 1000000))
176 static int be_verbose(int verbose, struct timeval *next_time,
177 int num, int *next_num, int num_total)
184 if (next_time != NULL)
185 gettimeofday(&now, NULL);
187 /* A positive verbosity means to print every X iterations */
189 (next_num == NULL || num >= *next_num || num >= num_total)) {
190 *next_num += verbose;
192 next_time->tv_sec = now.tv_sec - verbose;
193 next_time->tv_usec = now.tv_usec;
198 /* A negative verbosity means to print at most each X seconds */
199 if (verbose < 0 && next_time != NULL && difftime(&now, next_time) >= 0) {
200 next_time->tv_sec = now.tv_sec - verbose;
201 next_time->tv_usec = now.tv_usec;
210 static int get_verbose(const char *arg)
214 if (!arg || arg[0] == 'v')
216 else if (arg[0] == 's' || arg[0] == 'q')
219 verbose = (int)strtoul(arg, NULL, 0);
222 printf("Print status every %d seconds\n", -verbose);
223 else if (verbose == 1)
224 printf("Print status every operation\n");
225 else if (verbose > 1)
226 printf("Print status every %d operations\n", verbose);
231 static int do_disconnect(char *func, int verbose)
233 struct obd_ioctl_data data;
240 rc = ioctl(fd, OBD_IOC_DISCONNECT, &data);
242 fprintf(stderr, "error: %s: %x %s\n", cmdname(func),
243 OBD_IOC_DISCONNECT, strerror(errno));
246 printf("%s: disconnected conn %Lx\n", cmdname(func),
255 static void shmem_setup(void)
257 int shmid = shmget(IPC_PRIVATE, sizeof(counter_snapshot[0]), 0600);
260 fprintf(stderr, "Can't create shared memory counters: %s\n",
265 shared_counters = (long long *)shmat(shmid, NULL, 0);
267 if (shared_counters == (long long *)(-1)) {
268 fprintf(stderr, "Can't attach shared memory counters: %s\n",
270 shared_counters = NULL;
275 static inline void shmem_reset(void)
277 if (shared_counters == NULL)
280 memset(shared_counters, 0, sizeof(counter_snapshot[0]));
281 memset(counter_snapshot, 0, sizeof(counter_snapshot));
282 gettimeofday(&prev_time, NULL);
285 static inline void shmem_bump(void)
287 if (shared_counters == NULL || thread <= 0 || thread > MAX_SHMEM_COUNT)
290 shared_counters[thread - 1]++;
293 static void shmem_snap(int n)
295 struct timeval this_time;
301 if (shared_counters == NULL || n > MAX_SHMEM_COUNT)
304 memcpy(counter_snapshot[1], counter_snapshot[0],
305 n * sizeof(counter_snapshot[0][0]));
306 memcpy(counter_snapshot[0], shared_counters,
307 n * sizeof(counter_snapshot[0][0]));
308 gettimeofday(&this_time, NULL);
310 for (i = 0; i < n; i++) {
311 long long this_count =
312 counter_snapshot[0][i] - counter_snapshot[1][i];
314 if (this_count != 0) {
320 secs = (this_time.tv_sec + this_time.tv_usec / 1000000.0) -
321 (prev_time.tv_sec + prev_time.tv_usec / 1000000.0);
323 printf("%d/%d Total: %f/second\n", non_zero, n, total / secs);
325 prev_time = this_time;
328 #define SHMEM_SETUP() shmem_setup()
329 #define SHMEM_RESET() shmem_reset()
330 #define SHMEM_BUMP() shmem_bump()
331 #define SHMEM_SNAP(n) shmem_snap(n)
333 #define SHMEM_SETUP()
334 #define SHMEM_RESET()
336 #define SHMEM_SNAP(n)
339 extern command_t cmdlist[];
341 static int xml_command(char *cmd, ...)
350 while (((arg = va_arg(args, char *)) != NULL) && (i < 8)) {
358 for (j = 0; j < i; j++)
359 printf("%s ", cmds[j]);
363 return Parser_execarg(i, cmds, cmdlist);
367 static network_t *xml_network(xmlDocPtr doc, xmlNodePtr root)
369 xmlNodePtr cur = root->xmlChildrenNode;
372 if ((net = (network_t *) calloc(1, sizeof(network_t))) == NULL) {
373 printf("error: unable to malloc network_t\n");
377 net->type = xmlGetProp(root, "type");
378 if (net->type == NULL) {
379 printf("error: type attrib required (tcp, elan, myrinet)\n");
384 while (cur != NULL) {
385 if (!xmlStrcmp(cur->name, "server"))
386 net->server = xmlNodeGetContent(cur);
388 if (!xmlStrcmp(cur->name, "port"))
389 net->port = atoi(xmlNodeGetContent(cur));
394 if (net->server == NULL) {
395 printf("error: <server> tag required\n");
404 static int xml_mds(xmlDocPtr doc, xmlNodePtr root,
405 char *serv_name, char *serv_uuid)
407 xmlNodePtr cur = root->xmlChildrenNode;
408 char *fstype = NULL, *device = NULL;
411 printf("--- Setting up MDS ---\n");
412 while (cur != NULL) {
413 if (!xmlStrcmp(cur->name, "fstype"))
414 fstype = xmlNodeGetContent(cur);
416 if (!xmlStrcmp(cur->name, "device"))
417 device = xmlNodeGetContent(cur);
419 /* FIXME: Parse the network bits
420 * if (!xmlStrcmp(cur->name, "network")) {
421 * net = xml_network(doc, cur);
430 if ((fstype == NULL) || (device == NULL)) {
431 printf("error: <fstype> and <device> tags required\n");
435 if ((rc = xml_command("newdev", NULL)) != 0)
439 xml_command("attach", "mds", serv_name, serv_uuid, NULL)) != 0)
442 if ((rc = xml_command("setup", device, fstype, NULL)) != 0)
448 static int xml_obd(xmlDocPtr doc, xmlNodePtr root,
449 char *serv_name, char *serv_uuid)
451 char *obdtype, *format = NULL, *fstype = NULL, *device = NULL;
452 xmlNodePtr cur = root->xmlChildrenNode;
455 obdtype = xmlGetProp(root, "type");
456 printf("--- Setting up OBD ---\n");
458 while (cur != NULL) {
459 if (!xmlStrcmp(cur->name, "fstype"))
460 fstype = xmlNodeGetContent(cur);
462 if (!xmlStrcmp(cur->name, "device"))
463 device = xmlNodeGetContent(cur);
465 if (!xmlStrcmp(cur->name, "autoformat"))
466 format = xmlNodeGetContent(cur);
471 if ((obdtype == NULL) || (fstype == NULL) || (device == NULL)) {
472 printf("error: 'type' attrib and <fstype> "
473 "and <device> tags required\n");
477 /* FIXME: Building and configuring loopback devices should go here
478 * but is currently unsupported. You'll have to use the scripts
479 * for now until support is added, or specify a real device.
482 if ((rc = xml_command("newdev", NULL)) != 0)
485 if ((rc = xml_command("attach", obdtype,
486 serv_name, serv_uuid, NULL)) != 0)
489 if ((rc = xml_command("setup", device, fstype, NULL)) != 0)
495 static int xml_ost(xmlDocPtr doc, xmlNodePtr root,
496 char *serv_name, char *serv_uuid)
498 char *server_name = NULL, *server_uuid = NULL;
499 char *failover_name = NULL, *failover_uuid = NULL;
500 xmlNodePtr cur = root->xmlChildrenNode;
501 int server_num, failover_num, rc;
503 printf("--- Setting up OST ---\n");
504 while (cur != NULL) {
505 if (!xmlStrcmp(cur->name, "server_id")) {
506 server_num = atoi(xmlGetProp(cur, "num"));
507 server_name = xmlGetProp(cur, "name");
508 server_uuid = xmlGetProp(cur, "uuid");
511 /* FIXME: Properly handle multiple failover servers */
512 if (!xmlStrcmp(cur->name, "failover_id")) {
513 failover_num = atoi(xmlGetProp(cur, "num"));
514 failover_name = xmlGetProp(cur, "name");
515 failover_uuid = xmlGetProp(cur, "uuid");
521 if ((server_name == NULL) || (server_uuid == NULL)) {
522 printf("error: atleast the <server_id> tag is required\n");
526 if ((rc = xml_command("newdev", NULL)) != 0)
530 xml_command("attach", "ost", serv_name, serv_uuid, NULL)) != 0)
533 if ((rc = xml_command("setup", server_name, NULL)) != 0)
539 static int xml_osc(xmlDocPtr doc, xmlNodePtr root,
540 char *serv_name, char *serv_uuid)
542 char *ost_name = NULL, *ost_uuid = NULL;
543 xmlNodePtr cur = root->xmlChildrenNode;
546 printf("--- Setting up OSC ---\n");
547 while (cur != NULL) {
548 if (!xmlStrcmp(cur->name, "service_id")) {
549 ost_num = atoi(xmlGetProp(cur, "num"));
550 ost_name = xmlGetProp(cur, "name");
551 ost_uuid = xmlGetProp(cur, "uuid");
557 if ((ost_name == NULL) || (ost_uuid == NULL)) {
558 printf("error: atleast the <service_id> tag is required\n");
562 if ((rc = xml_command("newdev", NULL)) != 0)
566 xml_command("attach", "osc", serv_name, serv_uuid, NULL)) != 0)
569 if ((rc = xml_command("setup", ost_uuid, "localhost", NULL)) != 0)
575 static int xml_mdc(xmlDocPtr doc, xmlNodePtr root,
576 char *serv_name, char *serv_uuid)
578 char *mds_name = NULL, *mds_uuid = NULL;
579 xmlNodePtr cur = root->xmlChildrenNode;
582 printf("--- Setting up MDC ---\n");
583 while (cur != NULL) {
584 if (!xmlStrcmp(cur->name, "service_id")) {
585 mds_num = atoi(xmlGetProp(cur, "num"));
586 mds_name = xmlGetProp(cur, "name");
587 mds_uuid = xmlGetProp(cur, "uuid");
593 if ((mds_name == NULL) || (mds_uuid == NULL)) {
594 printf("error: atleast the <service_id> tag is required\n");
598 if ((rc = xml_command("newdev", NULL)) != 0)
602 xml_command("attach", "mdc", serv_name, serv_uuid, NULL)) != 0)
605 if ((rc = xml_command("setup", mds_uuid, "localhost", NULL)) != 0)
611 static int xml_lov(xmlDocPtr doc, xmlNodePtr root,
612 char *serv_name, char *serv_uuid)
614 printf("--- Setting up LOV ---\n");
618 static int xml_router(xmlDocPtr doc, xmlNodePtr root,
619 char *serv_name, char *serv_uuid)
621 printf("--- Setting up ROUTER ---\n");
625 static int xml_ldlm(xmlDocPtr doc, xmlNodePtr root,
626 char *serv_name, char *serv_uuid)
630 printf("--- Setting up LDLM ---\n");
631 if ((rc = xml_command("newdev", NULL)) != 0)
635 xml_command("attach", "ldlm", serv_name, serv_uuid, NULL)) != 0)
638 if ((rc = xml_command("setup", NULL)) != 0)
644 static int xml_service(xmlDocPtr doc, xmlNodePtr root,
645 int serv_num, char *serv_name, char *serv_uuid)
647 xmlNodePtr cur = root;
650 while (cur != NULL) {
651 name = xmlGetProp(cur, "name");
652 uuid = xmlGetProp(cur, "uuid");
654 if (xmlStrcmp(name, serv_name) || xmlStrcmp(uuid, serv_uuid)) {
659 if (!xmlStrcmp(cur->name, "mds"))
660 return xml_mds(doc, cur, name, uuid);
661 else if (!xmlStrcmp(cur->name, "obd"))
662 return xml_obd(doc, cur, name, uuid);
663 else if (!xmlStrcmp(cur->name, "ost"))
664 return xml_ost(doc, cur, name, uuid);
665 else if (!xmlStrcmp(cur->name, "osc"))
666 return xml_osc(doc, cur, name, uuid);
667 else if (!xmlStrcmp(cur->name, "mdc"))
668 return xml_mdc(doc, cur, name, uuid);
669 else if (!xmlStrcmp(cur->name, "lov"))
670 return xml_lov(doc, cur, name, uuid);
671 else if (!xmlStrcmp(cur->name, "router"))
672 return xml_router(doc, cur, name, uuid);
673 else if (!xmlStrcmp(cur->name, "ldlm"))
674 return xml_ldlm(doc, cur, name, uuid);
681 printf("error: No XML config branch for name=%s uuid=%s\n",
682 serv_name, serv_uuid);
686 static int xml_profile(xmlDocPtr doc, xmlNodePtr root,
687 int prof_num, char *prof_name, char *prof_uuid)
689 xmlNodePtr parent, cur = root;
690 char *name, *uuid, *srv_name, *srv_uuid;
693 while (cur != NULL) {
694 name = xmlGetProp(cur, "name");
695 uuid = xmlGetProp(cur, "uuid");
697 if (xmlStrcmp(cur->name, "profile") ||
698 xmlStrcmp(name, prof_name) || xmlStrcmp(uuid, prof_uuid)) {
703 /* FIXME: Doesn't understand mountpoints yet
704 * xml_mountpoint(doc, root, ...);
707 /* Setup each service in turn
708 * FIXME: Should be sorted by "num" attr, we shouldn't
709 * assume they're in order in the XML document.
712 cur = cur->xmlChildrenNode;
713 while (cur != NULL) {
714 if (!xmlStrcmp(cur->name, "service_id")) {
715 num = atoi(xmlGetProp(cur, "num"));
716 rc = xml_service(doc, root, num,
718 xmlGetProp(cur, "name"),
720 xmlGetProp(cur, "uuid"));
722 printf("error: service config\n");
736 static int xml_node(xmlDocPtr doc, xmlNodePtr root)
738 xmlNodePtr parent, cur = root;
742 /* Walk the node tags looking for ours */
743 while (cur != NULL) {
744 if (xmlStrcmp(cur->name, "node")) {
749 name = xmlGetProp(cur, "name");
753 uuid = xmlGetProp(cur, "uuid");
757 /* FIXME: Verify our NAME and UUID against /etc/lustre/id
758 * so we're sure we are who we think we are.
761 /* Setup each profile in turn
762 * FIXME: Should be sorted by "num" attr, we shouldn't
763 * assume they're in order in the XML document.
766 cur = cur->xmlChildrenNode;
767 while (cur != NULL) {
768 if (!xmlStrcmp(cur->name, "profile_id")) {
769 num = atoi(xmlGetProp(cur, "num"));
770 rc = xml_profile(doc, root, num,
771 xmlGetProp(cur, "name"),
772 xmlGetProp(cur, "uuid"));
786 static int do_xml(char *func, char *file)
792 doc = xmlParseFile(file);
794 fprintf(stderr, "error: Unable to parse XML\n");
798 cur = xmlDocGetRootElement(doc);
800 fprintf(stderr, "error: Empty XML\n");
805 if (xmlStrcmp(cur->name, (const xmlChar *)"lustre")) {
806 fprintf(stderr, "error: Root node != <lustre>\n");
811 /* FIXME: Validate the XML against the DTD here */
813 /* FIXME: Merge all the text nodes under each branch and
814 * prune empty nodes. Just to make the parsing more
815 * tolerant, the exact location of nested tags isn't
819 rc = xml_node(doc, cur->xmlChildrenNode);
825 static int do_device(char *func, int dev)
827 struct obd_ioctl_data data;
829 memset(&data, 0, sizeof(data));
836 if (obd_ioctl_pack(&data, &buf, max)) {
837 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(func));
841 return ioctl(fd, OBD_IOC_DEVICE, buf);
844 static int jt_device(int argc, char **argv)
846 do_disconnect(argv[0], 1);
849 fprintf(stderr, "usage: %s devno\n", cmdname(argv[0]));
853 rc = do_device(argv[0], strtoul(argv[1], NULL, 0));
856 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
857 strerror(rc = errno));
862 static int jt_connect(int argc, char **argv)
864 struct obd_ioctl_data data;
868 do_disconnect(argv[0], 1);
871 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
875 rc = ioctl(fd, OBD_IOC_CONNECT, &data);
877 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
878 OBD_IOC_CONNECT, strerror(rc = errno));
880 conn_addr = data.ioc_addr;
881 conn_cookie = data.ioc_cookie;
885 static int jt_disconnect(int argc, char **argv)
888 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
895 return do_disconnect(argv[0], 0);
898 static int jt__xml(int argc, char **argv)
901 fprintf(stderr, "usage: %s <xml file> <command [args ...]>\n",
906 return do_xml("xml", argv[1]);
909 static int jt__device(int argc, char **argv)
915 fprintf(stderr, "usage: %s devno <command [args ...]>\n",
920 rc = do_device("device", strtoul(argv[1], NULL, 0));
925 rc = jt_connect(1, arg2);
929 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
931 ret = do_disconnect(argv[0], 0);
938 static int jt__threads(int argc, char **argv)
940 int threads, next_thread;
946 "usage: %s numthreads verbose devno <cmd [args ...]>\n",
951 threads = strtoul(argv[1], NULL, 0);
953 verbose = get_verbose(argv[2]);
956 printf("%s: starting %d threads on device %s running %s\n",
957 argv[0], threads, argv[3], argv[4]);
961 for (i = 1, next_thread = verbose; i <= threads; i++) {
964 fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
965 strerror(rc = errno));
967 } else if (rc == 0) {
969 argv[2] = "--device";
970 return jt__device(argc - 2, argv + 2);
971 } else if (be_verbose(verbose, NULL, i, &next_thread, threads))
972 printf("%s: thread #%d (PID %d) started\n",
977 if (!thread) { /* parent process */
978 int live_threads = threads;
980 while (live_threads > 0) {
984 ret = waitpid(0, &status, verbose < 0 ? WNOHANG : 0);
995 fprintf(stderr, "error: %s: wait - %s\n",
996 argv[0], strerror(errno));
1001 * This is a hack. We _should_ be able to use
1002 * WIFEXITED(status) to see if there was an
1003 * error, but it appears to be broken and it
1004 * always returns 1 (OK). See wait(2).
1006 int err = WEXITSTATUS(status);
1009 "%s: PID %d had rc=%d\n",
1022 static int jt_detach(int argc, char **argv)
1024 struct obd_ioctl_data data;
1029 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1033 if (obd_ioctl_pack(&data, &buf, max)) {
1034 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1038 rc = ioctl(fd, OBD_IOC_DETACH, buf);
1040 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1041 strerror(rc = errno));
1046 static int jt_cleanup(int argc, char **argv)
1048 struct obd_ioctl_data data;
1053 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1057 rc = ioctl(fd, OBD_IOC_CLEANUP, &data);
1059 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1060 strerror(rc = errno));
1065 static int jt_newdev(int argc, char **argv)
1067 struct obd_ioctl_data data;
1075 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1079 rc = ioctl(fd, OBD_IOC_NEWDEV, &data);
1081 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1082 strerror(rc = errno));
1084 printf("Current device set to %d\n", data.ioc_dev);
1090 static int jt_list(int argc, char **argv)
1093 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buf;
1098 memset(buf, 0, sizeof(buf));
1099 data->ioc_version = OBD_IOCTL_VERSION;
1100 data->ioc_addr = conn_addr;
1101 data->ioc_cookie = conn_addr;
1102 data->ioc_len = sizeof(buf);
1103 data->ioc_inllen1 = sizeof(buf) - size_round(sizeof(*data));
1106 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1110 rc = ioctl(fd, OBD_IOC_LIST, data);
1112 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1113 strerror(rc = errno));
1115 printf("%s", data->ioc_bulk);
1121 static int jt_attach(int argc, char **argv)
1123 struct obd_ioctl_data data;
1127 if (argc != 2 && argc != 3 && argc != 4) {
1128 fprintf(stderr, "usage: %s type [name [uuid]]\n",
1133 data.ioc_inllen1 = strlen(argv[1]) + 1;
1134 data.ioc_inlbuf1 = argv[1];
1136 data.ioc_inllen2 = strlen(argv[2]) + 1;
1137 data.ioc_inlbuf2 = argv[2];
1141 data.ioc_inllen3 = strlen(argv[3]) + 1;
1142 data.ioc_inlbuf3 = argv[3];
1145 if (obd_ioctl_pack(&data, &buf, max)) {
1146 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1150 rc = ioctl(fd, OBD_IOC_ATTACH, buf);
1152 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
1153 OBD_IOC_ATTACH, strerror(rc = errno));
1154 else if (argc == 3) {
1156 if (strlen(argv[2]) > 128) {
1157 printf("Name too long to set environment\n");
1160 snprintf(name, 512, "LUSTRE_DEV_%s", argv[2]);
1161 rc = setenv(name, argv[1], 1);
1163 printf("error setting env variable %s\n", name);
1170 #define N2D_OFF 0x100 /* So we can tell between error codes and devices */
1172 static int do_name2dev(char *func, char *name)
1174 struct obd_ioctl_data data;
1181 data.ioc_inllen1 = strlen(name) + 1;
1182 data.ioc_inlbuf1 = name;
1184 if (obd_ioctl_pack(&data, &buf, max)) {
1185 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(func));
1188 rc = ioctl(fd, OBD_IOC_NAME2DEV, buf);
1190 fprintf(stderr, "error: %s: %s - %s\n", cmdname(func),
1191 name, strerror(rc = errno));
1195 memcpy((char *)(&data), buf, sizeof(data));
1197 return data.ioc_dev + N2D_OFF;
1200 static int jt_name2dev(int argc, char **argv)
1203 Parser_printhelp("name2dev");
1207 rc = do_name2dev(argv[0], argv[1]);
1208 if (rc >= N2D_OFF) {
1209 int dev = rc - N2D_OFF;
1210 rc = do_device(argv[0], dev);
1212 printf("%d\n", dev);
1217 static int jt_setup(int argc, char **argv)
1219 struct obd_ioctl_data data;
1224 fprintf(stderr, "usage: %s [device] [fstype]\n",
1231 if (argv[1][0] == '$') {
1232 rc = do_name2dev(argv[0], argv[1] + 1);
1233 if (rc >= N2D_OFF) {
1234 printf("%s is device %d\n", argv[1],
1236 data.ioc_dev = rc - N2D_OFF;
1239 data.ioc_dev = strtoul(argv[1], NULL, 0);
1240 data.ioc_inllen1 = strlen(argv[1]) + 1;
1241 data.ioc_inlbuf1 = argv[1];
1244 data.ioc_inllen2 = strlen(argv[2]) + 1;
1245 data.ioc_inlbuf2 = argv[2];
1248 if (obd_ioctl_pack(&data, &buf, max)) {
1249 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1252 rc = ioctl(fd, OBD_IOC_SETUP, buf);
1254 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1255 strerror(rc = errno));
1261 static int jt_create(int argc, char **argv)
1263 struct obd_ioctl_data data;
1264 struct timeval next_time;
1265 int count = 1, next_count;
1270 if (argc < 2 || argc > 4) {
1271 fprintf(stderr, "usage: %s num [mode] [verbose]\n",
1275 count = strtoul(argv[1], NULL, 0);
1278 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
1280 data.ioc_obdo1.o_mode = 0100644;
1281 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
1283 verbose = get_verbose(argv[3]);
1285 printf("%s: %d obdos\n", cmdname(argv[0]), count);
1286 gettimeofday(&next_time, NULL);
1287 next_time.tv_sec -= verbose;
1289 for (i = 1, next_count = verbose; i <= count; i++) {
1290 rc = ioctl(fd, OBD_IOC_CREATE, &data);
1293 fprintf(stderr, "error: %s: #%d - %s\n",
1294 cmdname(argv[0]), i, strerror(rc = errno));
1297 if (be_verbose(verbose, &next_time, i, &next_count, count))
1298 printf("%s: #%d is object id %Ld\n", cmdname(argv[0]),
1299 i, data.ioc_obdo1.o_id);
1304 static int jt_setattr(int argc, char **argv)
1306 struct obd_ioctl_data data;
1310 fprintf(stderr, "usage: %s id mode\n", cmdname(argv[0]));
1314 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
1315 data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], NULL, 0);
1316 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
1318 rc = ioctl(fd, OBD_IOC_SETATTR, &data);
1320 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1321 strerror(rc = errno));
1326 static int jt_destroy(int argc, char **argv)
1328 struct obd_ioctl_data data;
1332 fprintf(stderr, "usage: %s id\n", cmdname(argv[0]));
1336 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
1337 data.ioc_obdo1.o_mode = S_IFREG | 0644;
1339 rc = ioctl(fd, OBD_IOC_DESTROY, &data);
1341 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1342 strerror(rc = errno));
1347 static int jt_getattr(int argc, char **argv)
1349 struct obd_ioctl_data data;
1352 fprintf(stderr, "usage: %s id\n", cmdname(argv[0]));
1357 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
1358 /* to help obd filter */
1359 data.ioc_obdo1.o_mode = 0100644;
1360 data.ioc_obdo1.o_valid = 0xffffffff;
1361 printf("%s: object id %Ld\n", cmdname(argv[0]), data.ioc_obdo1.o_id);
1363 rc = ioctl(fd, OBD_IOC_GETATTR, &data);
1365 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1366 strerror(rc = errno));
1368 printf("%s: object id %Ld, mode %o\n", cmdname(argv[0]),
1369 data.ioc_obdo1.o_id, data.ioc_obdo1.o_mode);
1374 static int jt_test_getattr(int argc, char **argv)
1376 struct obd_ioctl_data data;
1377 struct timeval start, next_time;
1378 int i, count, next_count;
1381 if (argc != 2 && argc != 3) {
1382 fprintf(stderr, "usage: %s count [verbose]\n",
1388 count = strtoul(argv[1], NULL, 0);
1391 verbose = get_verbose(argv[2]);
1395 data.ioc_obdo1.o_valid = 0xffffffff;
1396 data.ioc_obdo1.o_id = 2;
1397 gettimeofday(&start, NULL);
1398 next_time.tv_sec = start.tv_sec - verbose;
1399 next_time.tv_usec = start.tv_usec;
1401 printf("%s: getting %d attrs (testing only): %s",
1402 cmdname(argv[0]), count, ctime(&start.tv_sec));
1404 for (i = 1, next_count = verbose; i <= count; i++) {
1405 rc = ioctl(fd, OBD_IOC_GETATTR, &data);
1408 fprintf(stderr, "error: %s: #%d - %s\n",
1409 cmdname(argv[0]), i, strerror(rc = errno));
1413 (verbose, &next_time, i, &next_count, count))
1414 printf("%s: got attr #%d\n", cmdname(argv[0]),
1423 gettimeofday(&end, NULL);
1425 diff = difftime(&end, &start);
1429 printf("%s: %d attrs in %.4gs (%.4g attr/s): %s",
1430 cmdname(argv[0]), i, diff, (double)i / diff,
1431 ctime(&end.tv_sec));
1436 static int jt_test_brw(int argc, char **argv)
1438 struct obd_ioctl_data data;
1439 struct timeval start, next_time;
1441 int pages = 1, obdos = 1, count, next_count;
1442 int verbose = 1, write = 0, rw;
1446 if (argc < 2 || argc > 6) {
1448 "usage: %s count [write [verbose [pages [obdos]]]]\n",
1453 count = strtoul(argv[1], NULL, 0);
1456 if (argv[2][0] == 'w' || argv[2][0] == '1')
1458 else if (argv[2][0] == 'r' || argv[2][0] == '0')
1461 verbose = get_verbose(argv[3]);
1465 pages = strtoul(argv[4], NULL, 0);
1467 obdos = strtoul(argv[5], NULL, 0);
1469 if (obdos != 1 && obdos != 2) {
1470 fprintf(stderr, "error: %s: only 1 or 2 obdos supported\n",
1475 len = pages * PAGE_SIZE;
1477 bulk = calloc(obdos, len);
1479 fprintf(stderr, "error: %s: no memory allocating %dx%d pages\n",
1480 cmdname(argv[0]), obdos, pages);
1484 data.ioc_obdo1.o_id = 2;
1485 data.ioc_count = len;
1486 data.ioc_offset = 0;
1487 data.ioc_plen1 = len;
1488 data.ioc_pbuf1 = bulk;
1490 data.ioc_obdo2.o_id = 3;
1491 data.ioc_plen2 = len;
1492 data.ioc_pbuf2 = bulk + len;
1495 gettimeofday(&start, NULL);
1496 next_time.tv_sec = start.tv_sec - verbose;
1497 next_time.tv_usec = start.tv_usec;
1500 printf("%s: %s %d (%dx%d pages) (testing only): %s",
1501 cmdname(argv[0]), write ? "writing" : "reading",
1502 count, obdos, pages, ctime(&start.tv_sec));
1505 * We will put in the start time (and loop count inside the loop)
1506 * at the beginning of each page so that we will be able to validate
1507 * (at some later time) whether the data actually made it or not.
1509 * XXX we do not currently use any of this memory in OBD_IOC_BRW_*
1510 * just to avoid the overhead of the copy_{to,from}_user. It
1511 * can be fixed if we ever need to send real data around.
1513 for (o = 0, b = bulk; o < obdos; o++)
1514 for (p = 0; p < pages; p++, b += PAGE_SIZE)
1515 memcpy(b, &start, sizeof(start));
1517 rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
1518 for (i = 1, next_count = verbose; i <= count; i++) {
1520 b = bulk + sizeof(struct timeval);
1521 for (o = 0; o < obdos; o++)
1522 for (p = 0; p < pages; p++, b += PAGE_SIZE)
1523 memcpy(b, &count, sizeof(count));
1526 rc = ioctl(fd, rw, &data);
1529 fprintf(stderr, "error: %s: #%d - %s on %s\n",
1530 cmdname(argv[0]), i, strerror(rc = errno),
1531 write ? "write" : "read");
1533 } else if (be_verbose
1534 (verbose, &next_time, i, &next_count, count))
1535 printf("%s: %s number %d\n", cmdname(argv[0]),
1536 write ? "write" : "read", i);
1545 gettimeofday(&end, NULL);
1547 diff = difftime(&end, &start);
1551 printf("%s: %s %dx%dx%d pages in %.4gs (%.4g pg/s): %s",
1552 cmdname(argv[0]), write ? "wrote" : "read",
1553 obdos, pages, i, diff,
1554 (double)obdos * i * pages / diff,
1555 ctime(&end.tv_sec));
1560 static int jt_lov_config(int argc, char **argv)
1562 struct obd_ioctl_data data;
1563 struct lov_desc desc;
1568 printf("WARNING: obdctl lovconfig NOT MAINTAINED\n");
1572 Parser_printhelp("lovconfig");
1576 if (strlen(argv[1]) > sizeof(uuid_t) - 1) {
1577 fprintf(stderr, "lov_config: no %dB memory for uuid's\n",
1582 memset(&desc, 0, sizeof(desc));
1583 strcpy(desc.ld_uuid, argv[1]);
1584 desc.ld_default_stripe_count = strtoul(argv[2], NULL, 0);
1585 desc.ld_default_stripe_size = strtoul(argv[3], NULL, 0);
1586 desc.ld_pattern = strtoul(argv[4], NULL, 0);
1587 desc.ld_tgt_count = argc - 5;
1590 size = sizeof(uuid_t) * desc.ld_tgt_count;
1591 uuidarray = malloc(size);
1593 fprintf(stderr, "lov_config: no %dB memory for uuid's\n", size);
1596 memset(uuidarray, 0, size);
1597 for (i = 5; i < argc; i++) {
1598 char *buf = (char *)(uuidarray + i - 5);
1599 if (strlen(argv[i]) >= sizeof(uuid_t)) {
1600 fprintf(stderr, "lov_config: arg %d (%s) too long\n",
1605 strcpy(buf, argv[i]);
1608 data.ioc_inllen1 = sizeof(desc);
1609 data.ioc_inlbuf1 = (char *)&desc;
1610 data.ioc_inllen2 = size;
1611 data.ioc_inlbuf2 = (char *)uuidarray;
1613 if (obd_ioctl_pack(&data, &buf, max)) {
1614 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1618 rc = ioctl(fd, OBD_IOC_LOV_CONFIG, buf);
1620 fprintf(stderr, "lov_config: error: %s: %s\n",
1621 cmdname(argv[0]), strerror(rc = errno));
1626 static int jt_test_ldlm(int argc, char **argv)
1628 struct obd_ioctl_data data;
1632 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1636 rc = ioctl(fd, IOC_LDLM_TEST, &data);
1638 fprintf(stderr, "error: %s: test failed: %s\n",
1639 cmdname(argv[0]), strerror(rc = errno));
1643 static int jt_dump_ldlm(int argc, char **argv)
1645 struct obd_ioctl_data data;
1649 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1653 rc = ioctl(fd, IOC_LDLM_DUMP, &data);
1655 fprintf(stderr, "error: %s failed: %s\n",
1656 cmdname(argv[0]), strerror(rc = errno));
1660 static int jt_newconn(int argc, char **argv)
1662 struct obd_ioctl_data data;
1666 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1670 rc = ioctl(fd, OBD_IOC_RECOVD_NEWCONN, &data);
1672 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1673 strerror(rc = errno));
1678 static int jt_quit(int argc, char **argv)
1680 Parser_quit(argc, argv);
1685 command_t cmdlist[] = {
1687 {"--xml", jt__xml, 0, "--xml <xml file> <command [args ...]>"},
1688 {"--device", jt__device, 0, "--device <devno> <command [args ...]>"},
1689 {"--threads", jt__threads, 0,
1690 "--threads <threads> <devno> <command [args ...]>"},
1692 /* Device configuration commands */
1693 {"lovconfig", jt_lov_config, 0, "configure lov data on MDS "
1694 "[usage: lovconfig lov-uuid stripecount, stripesize, pattern, UUID1, [UUID2, ...]"},
1695 {"list", jt_list, 0, "list the devices (no args)"},
1696 {"newdev", jt_newdev, 0, "set device to a new unused obd (no args)"},
1697 {"device", jt_device, 0, "set current device (args device_no name)"},
1698 {"name2dev", jt_name2dev, 0,
1699 "set device by name [usage: name2dev devname]"},
1700 {"attach", jt_attach, 0, "name the type of device (args: type data"},
1701 {"setup", jt_setup, 0, "setup device (args: <blkdev> [data]"},
1702 {"detach", jt_detach, 0, "detach the current device (arg: )"},
1703 {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
1705 /* Session commands */
1706 {"connect", jt_connect, 0, "connect - get a connection to device"},
1707 {"disconnect", jt_disconnect, 0,
1708 "disconnect - break connection to device"},
1710 /* Session operations */
1711 {"create", jt_create, 0, "create [count [mode [verbose]]]"},
1712 {"destroy", jt_destroy, 0, "destroy <id>"},
1713 {"getattr", jt_getattr, 0, "getattr <id>"},
1714 {"setattr", jt_setattr, 0, "setattr <id> <mode>"},
1715 {"newconn", jt_newconn, 0, "newconn [newuuid]"},
1716 {"test_getattr", jt_test_getattr, 0, "test_getattr <count> [verbose]"},
1717 {"test_brw", jt_test_brw, 0, "test_brw <count> [write [verbose]]"},
1718 {"test_ldlm", jt_test_ldlm, 0, "test lock manager (no args)"},
1719 {"dump_ldlm", jt_dump_ldlm, 0, "dump all lock manager state (no args)"},
1721 /* User interface commands */
1722 {"help", Parser_help, 0, "help"},
1723 {"exit", jt_quit, 0, "quit"},
1724 {"quit", jt_quit, 0, "quit"},
1729 static void signal_server(int sig)
1731 if (sig == SIGINT) {
1732 do_disconnect("sigint", 1);
1735 fprintf(stderr, "%s: got signal %d\n", cmdname("sigint"), sig);
1738 int main(int argc, char **argv)
1740 struct sigaction sigact;
1742 sigact.sa_handler = signal_server;
1743 sigfillset(&sigact.sa_mask);
1744 sigact.sa_flags = SA_RESTART;
1745 sigaction(SIGINT, &sigact, NULL);
1751 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1753 Parser_init("obdctl > ", cmdlist);
1754 rc = Parser_commands();
1757 do_disconnect(argv[0], 1);