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",
151 static char *cmdname(char *func)
153 static char buf[512];
156 sprintf(buf, "%s-%d", func, thread);
163 int getfd(char *func)
166 fd = open("/dev/obd", O_RDWR);
168 fprintf(stderr, "error: %s: opening /dev/obd: %s\n",
169 cmdname(func), strerror(errno));
175 #define difftime(a, b) \
176 ((double)(a)->tv_sec - (b)->tv_sec + \
177 ((double)((a)->tv_usec - (b)->tv_usec) / 1000000))
179 static int be_verbose(int verbose, struct timeval *next_time,
180 int num, int *next_num, int num_total)
187 if (next_time != NULL)
188 gettimeofday(&now, NULL);
190 /* A positive verbosity means to print every X iterations */
192 (next_num == NULL || num >= *next_num || num >= num_total)) {
193 *next_num += verbose;
195 next_time->tv_sec = now.tv_sec - verbose;
196 next_time->tv_usec = now.tv_usec;
201 /* A negative verbosity means to print at most each X seconds */
202 if (verbose < 0 && next_time != NULL && difftime(&now, next_time) >= 0){
203 next_time->tv_sec = now.tv_sec - verbose;
204 next_time->tv_usec = now.tv_usec;
213 static int get_verbose(const char *arg)
217 if (!arg || arg[0] == 'v')
219 else if (arg[0] == 's' || arg[0] == 'q')
222 verbose = (int) strtoul(arg, NULL, 0);
225 printf("Print status every %d seconds\n", -verbose);
226 else if (verbose == 1)
227 printf("Print status every operation\n");
228 else if (verbose > 1)
229 printf("Print status every %d operations\n", verbose);
234 static int do_disconnect(char *func, int verbose)
236 struct obd_ioctl_data data;
243 rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
245 fprintf(stderr, "error: %s: %x %s\n", cmdname(func),
246 OBD_IOC_DISCONNECT, strerror(errno));
249 printf("%s: disconnected conn %Lx\n", cmdname(func),
261 int shmid = shmget (IPC_PRIVATE, sizeof (counter_snapshot[0]), 0600);
265 fprintf (stderr, "Can't create shared memory counters: %s\n", strerror (errno));
269 shared_counters = (long long *)shmat (shmid, NULL, 0);
271 if (shared_counters == (long long *)(-1))
273 fprintf (stderr, "Can't attach shared memory counters: %s\n", strerror (errno));
274 shared_counters = NULL;
282 if (shared_counters == NULL)
285 memset (shared_counters, 0, sizeof (counter_snapshot[0]));
286 memset (counter_snapshot, 0, sizeof (counter_snapshot));
287 gettimeofday (&prev_time, NULL);
293 if (shared_counters == NULL ||
294 thread <= 0 || thread > MAX_SHMEM_COUNT)
297 shared_counters[thread - 1]++;
303 struct timeval this_time;
309 if (shared_counters == NULL || n > MAX_SHMEM_COUNT)
312 memcpy (counter_snapshot[1], counter_snapshot[0], n * sizeof (counter_snapshot[0][0]));
313 memcpy (counter_snapshot[0], shared_counters, n * sizeof (counter_snapshot[0][0]));
314 gettimeofday (&this_time, NULL);
316 for (i = 0; i < n; i++)
318 long long this_count = counter_snapshot[0][i] - counter_snapshot[1][i];
327 secs = (this_time.tv_sec + this_time.tv_usec/1000000.0) -
328 (prev_time.tv_sec + prev_time.tv_usec/1000000.0);
330 printf ("%d/%d Total: %f/second\n", non_zero, n, total / secs);
332 prev_time = this_time;
335 #define SHMEM_SETUP() shmem_setup()
336 #define SHMEM_RESET() shmem_reset()
337 #define SHMEM_BUMP() shmem_bump()
338 #define SHMEM_SNAP(n) shmem_snap(n)
340 #define SHMEM_SETUP()
341 #define SHMEM_RESET()
343 #define SHMEM_SNAP(n)
346 extern command_t cmdlist[];
348 static int xml_command(char *cmd, ...) {
356 while (((arg = va_arg(args, char *)) != NULL) && (i < 8)) {
364 for (j = 0; j < i; j++)
365 printf("%s ", cmds[j]);
369 return Parser_execarg(i, cmds, cmdlist);
373 static network_t *xml_network(xmlDocPtr doc, xmlNodePtr root) {
374 xmlNodePtr cur = root->xmlChildrenNode;
377 if ((net = (network_t *)calloc(1, sizeof(network_t))) == NULL) {
378 printf("error: unable to malloc network_t\n");
382 net->type = xmlGetProp(root, "type");
383 if (net->type == NULL) {
384 printf("error: type attrib required (tcp, elan, myrinet)\n");
389 while (cur != NULL) {
390 if (!xmlStrcmp(cur->name, "server"))
391 net->server = xmlNodeGetContent(cur);
393 if (!xmlStrcmp(cur->name, "port"))
394 net->port = atoi(xmlNodeGetContent(cur));
399 if (net->server == NULL) {
400 printf("error: <server> tag required\n");
409 static int xml_mds(xmlDocPtr doc, xmlNodePtr root,
410 char *serv_name, char *serv_uuid) {
411 xmlNodePtr cur = root->xmlChildrenNode;
412 char *fstype = NULL, *device = NULL;
415 printf("--- Setting up MDS ---\n");
416 while (cur != NULL) {
417 if (!xmlStrcmp(cur->name, "fstype"))
418 fstype = xmlNodeGetContent(cur);
420 if (!xmlStrcmp(cur->name, "device"))
421 device = xmlNodeGetContent(cur);
423 /* FIXME: Parse the network bits
424 * if (!xmlStrcmp(cur->name, "network")) {
425 * net = xml_network(doc, cur);
434 if ((fstype == NULL) || (device == NULL)) {
435 printf("error: <fstype> and <device> tags required\n");
439 if ((rc = xml_command("newdev", NULL)) != 0)
442 if ((rc = xml_command("attach","mds",serv_name,serv_uuid,NULL)) != 0)
445 if ((rc = xml_command("setup", device, fstype, NULL)) != 0)
451 static int xml_obd(xmlDocPtr doc, xmlNodePtr root,
452 char *serv_name, char *serv_uuid) {
453 char *obdtype, *format = NULL, *fstype = NULL, *device = NULL;
454 xmlNodePtr cur = root->xmlChildrenNode;
457 obdtype = xmlGetProp(root, "type");
458 printf("--- Setting up OBD ---\n");
460 while (cur != NULL) {
461 if (!xmlStrcmp(cur->name, "fstype"))
462 fstype = xmlNodeGetContent(cur);
464 if (!xmlStrcmp(cur->name, "device"))
465 device = xmlNodeGetContent(cur);
467 if (!xmlStrcmp(cur->name, "autoformat"))
468 format = xmlNodeGetContent(cur);
473 if ((obdtype == NULL) || (fstype == NULL) || (device == NULL)) {
474 printf("error: 'type' attrib and <fstype> "
475 "and <device> tags required\n");
479 /* FIXME: Building and configuring loopback devices should go here
480 * but is currently unsupported. You'll have to use the scripts
481 * for now until support is added, or specify a real device.
484 if ((rc = xml_command("newdev", NULL)) != 0)
487 if ((rc = xml_command("attach", obdtype,
488 serv_name,serv_uuid, NULL)) != 0)
491 if ((rc = xml_command("setup", device, fstype, NULL)) != 0)
497 static int xml_ost(xmlDocPtr doc, xmlNodePtr root,
498 char *serv_name, char *serv_uuid) {
499 char *server_name = NULL, *server_uuid = NULL;
500 char *failover_name = NULL, *failover_uuid = NULL;
501 xmlNodePtr cur = root->xmlChildrenNode;
502 int server_num, failover_num, rc;
504 printf("--- Setting up OST ---\n");
505 while (cur != NULL) {
506 if (!xmlStrcmp(cur->name, "server_id")) {
507 server_num = atoi(xmlGetProp(cur, "num"));
508 server_name = xmlGetProp(cur, "name");
509 server_uuid = xmlGetProp(cur, "uuid");
512 /* FIXME: Properly handle multiple failover servers */
513 if (!xmlStrcmp(cur->name, "failover_id")) {
514 failover_num = atoi(xmlGetProp(cur, "num"));
515 failover_name = xmlGetProp(cur, "name");
516 failover_uuid = xmlGetProp(cur, "uuid");
522 if ((server_name == NULL) || (server_uuid == NULL)) {
523 printf("error: atleast the <server_id> tag is required\n");
527 if ((rc = xml_command("newdev", NULL)) != 0)
530 if ((rc = 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) {
541 char *ost_name = NULL, *ost_uuid = NULL;
542 xmlNodePtr cur = root->xmlChildrenNode;
545 printf("--- Setting up OSC ---\n");
546 while (cur != NULL) {
547 if (!xmlStrcmp(cur->name, "service_id")) {
548 ost_num = atoi(xmlGetProp(cur, "num"));
549 ost_name = xmlGetProp(cur, "name");
550 ost_uuid = xmlGetProp(cur, "uuid");
556 if ((ost_name == NULL) || (ost_uuid == NULL)) {
557 printf("error: atleast the <service_id> tag is required\n");
561 if ((rc = xml_command("newdev", NULL)) != 0)
564 if ((rc = xml_command("attach","osc",serv_name,serv_uuid,NULL)) != 0)
567 if ((rc = xml_command("setup", ost_uuid, "localhost", NULL)) != 0)
573 static int xml_mdc(xmlDocPtr doc, xmlNodePtr root,
574 char *serv_name, char *serv_uuid) {
575 char *mds_name = NULL, *mds_uuid = NULL;
576 xmlNodePtr cur = root->xmlChildrenNode;
579 printf("--- Setting up MDC ---\n");
580 while (cur != NULL) {
581 if (!xmlStrcmp(cur->name, "service_id")) {
582 mds_num = atoi(xmlGetProp(cur, "num"));
583 mds_name = xmlGetProp(cur, "name");
584 mds_uuid = xmlGetProp(cur, "uuid");
590 if ((mds_name == NULL) || (mds_uuid == NULL)) {
591 printf("error: atleast the <service_id> tag is required\n");
595 if ((rc = xml_command("newdev", NULL)) != 0)
598 if ((rc = xml_command("attach","mdc",serv_name,serv_uuid,NULL)) != 0)
601 if ((rc = xml_command("setup", mds_uuid, "localhost", NULL)) != 0)
607 static int xml_lov(xmlDocPtr doc, xmlNodePtr root,
608 char *serv_name, char *serv_uuid) {
609 printf("--- Setting up LOV ---\n");
613 static int xml_router(xmlDocPtr doc, xmlNodePtr root,
614 char *serv_name, char *serv_uuid) {
615 printf("--- Setting up ROUTER ---\n");
619 static int xml_ldlm(xmlDocPtr doc, xmlNodePtr root,
620 char *serv_name, char *serv_uuid) {
623 printf("--- Setting up LDLM ---\n");
624 if ((rc = xml_command("newdev", NULL)) != 0)
627 if ((rc = xml_command("attach","ldlm",serv_name,serv_uuid,NULL)) != 0)
630 if ((rc = xml_command("setup", NULL)) != 0)
636 static int xml_service(xmlDocPtr doc, xmlNodePtr root,
637 int serv_num, char *serv_name, char *serv_uuid) {
638 xmlNodePtr cur = root;
641 while (cur != NULL) {
642 name = xmlGetProp(cur, "name");
643 uuid = xmlGetProp(cur, "uuid");
645 if (xmlStrcmp(name, serv_name) ||
646 xmlStrcmp(uuid, serv_uuid)) {
651 if (!xmlStrcmp(cur->name, "mds"))
652 return xml_mds(doc, cur, name, uuid);
653 else if (!xmlStrcmp(cur->name, "obd"))
654 return xml_obd(doc, cur, name, uuid);
655 else if (!xmlStrcmp(cur->name, "ost"))
656 return xml_ost(doc, cur, name, uuid);
657 else if (!xmlStrcmp(cur->name, "osc"))
658 return xml_osc(doc, cur, name, uuid);
659 else if (!xmlStrcmp(cur->name, "mdc"))
660 return xml_mdc(doc, cur, name, uuid);
661 else if (!xmlStrcmp(cur->name, "lov"))
662 return xml_lov(doc, cur, name, uuid);
663 else if (!xmlStrcmp(cur->name, "router"))
664 return xml_router(doc, cur, name, uuid);
665 else if (!xmlStrcmp(cur->name, "ldlm"))
666 return xml_ldlm(doc, cur, name, uuid);
673 printf("error: No XML config branch for name=%s uuid=%s\n",
674 serv_name, serv_uuid);
678 static int xml_profile(xmlDocPtr doc, xmlNodePtr root,
679 int prof_num, char *prof_name, char *prof_uuid) {
680 xmlNodePtr parent, cur = root;
681 char *name, *uuid, *srv_name, *srv_uuid;
684 while (cur != NULL) {
685 name = xmlGetProp(cur, "name");
686 uuid = xmlGetProp(cur, "uuid");
688 if (xmlStrcmp(cur->name, "profile") ||
689 xmlStrcmp(name, prof_name) ||
690 xmlStrcmp(uuid, prof_uuid)) {
695 /* FIXME: Doesn't understand mountpoints yet
696 * xml_mountpoint(doc, root, ...);
699 /* Setup each service in turn
700 * FIXME: Should be sorted by "num" attr, we shouldn't
701 * assume they're in order in the XML document.
704 cur = cur->xmlChildrenNode;
705 while (cur != NULL) {
706 if (!xmlStrcmp(cur->name, "service_id")) {
707 num = atoi(xmlGetProp(cur, "num"));
708 rc = xml_service(doc, root, num,
709 srv_name = xmlGetProp(cur, "name"),
710 srv_uuid = xmlGetProp(cur, "uuid"));
712 printf("error: service config\n");
726 static int xml_node(xmlDocPtr doc, xmlNodePtr root) {
727 xmlNodePtr parent, cur = root;
731 /* Walk the node tags looking for ours */
732 while (cur != NULL) {
733 if (xmlStrcmp(cur->name, "node")) {
738 name = xmlGetProp(cur, "name");
742 uuid = xmlGetProp(cur, "uuid");
746 /* FIXME: Verify our NAME and UUID against /etc/lustre/id
747 * so we're sure we are who we think we are.
750 /* Setup each profile in turn
751 * FIXME: Should be sorted by "num" attr, we shouldn't
752 * assume they're in order in the XML document.
755 cur = cur->xmlChildrenNode;
756 while (cur != NULL) {
757 if (!xmlStrcmp(cur->name, "profile_id")) {
758 num = atoi(xmlGetProp(cur, "num"));
759 rc = xml_profile(doc, root, num,
760 xmlGetProp(cur, "name"),
761 xmlGetProp(cur, "uuid"));
775 static int do_xml(char *func, char *file)
781 doc = xmlParseFile(file);
783 fprintf(stderr, "error: Unable to parse XML\n");
787 cur = xmlDocGetRootElement(doc);
789 fprintf(stderr, "error: Empty XML\n");
794 if (xmlStrcmp(cur->name, (const xmlChar *)"lustre")) {
795 fprintf(stderr, "error: Root node != <lustre>\n");
800 /* FIXME: Validate the XML against the DTD here */
802 /* FIXME: Merge all the text nodes under each branch and
803 * prune empty nodes. Just to make the parsing more
804 * tolerant, the exact location of nested tags isn't
808 rc = xml_node(doc, cur->xmlChildrenNode);
814 static int do_device(char *func, int dev)
816 struct obd_ioctl_data data;
818 memset(&data, 0, sizeof(data));
825 if (obd_ioctl_pack(&data, &buf, max)) {
826 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(func));
830 return ioctl(fd, OBD_IOC_DEVICE , buf);
833 static int jt_device(int argc, char **argv)
835 do_disconnect(argv[0], 1);
838 fprintf(stderr, "usage: %s devno\n", cmdname(argv[0]));
842 rc = do_device(argv[0], strtoul(argv[1], NULL, 0));
845 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
846 strerror(rc = errno));
851 static int jt_connect(int argc, char **argv)
853 struct obd_ioctl_data data;
857 do_disconnect(argv[0], 1);
860 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
864 rc = ioctl(fd, OBD_IOC_CONNECT , &data);
866 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
867 OBD_IOC_CONNECT, strerror(rc = errno));
869 conn_addr = data.ioc_addr;
870 conn_cookie = data.ioc_cookie;
874 static int jt_disconnect(int argc, char **argv)
877 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
884 return do_disconnect(argv[0], 0);
887 static int jt__xml(int argc, char **argv)
890 fprintf(stderr, "usage: %s <xml file> <command [args ...]>\n",
895 return do_xml("xml", argv[1]);
898 static int jt__device(int argc, char **argv)
904 fprintf(stderr, "usage: %s devno <command [args ...]>\n",
909 rc = do_device("device", strtoul(argv[1], NULL, 0));
914 rc = jt_connect(1, arg2);
918 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
920 ret = do_disconnect(argv[0], 0);
927 static int jt__threads(int argc, char **argv)
929 int threads, next_thread;
935 "usage: %s numthreads verbose devno <cmd [args ...]>\n",
940 threads = strtoul(argv[1], NULL, 0);
942 verbose = get_verbose(argv[2]);
945 printf("%s: starting %d threads on device %s running %s\n",
946 argv[0], threads, argv[3], argv[4]);
950 for (i = 1, next_thread = verbose; i <= threads; i++) {
953 fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
954 strerror(rc = errno));
956 } else if (rc == 0) {
958 argv[2] = "--device";
959 return jt__device(argc - 2, argv + 2);
960 } else if (be_verbose(verbose, NULL, i, &next_thread, threads))
961 printf("%s: thread #%d (PID %d) started\n",
966 if (!thread) { /* parent process */
967 int live_threads = threads;
969 while (live_threads > 0)
974 ret = waitpid (0, &status, verbose < 0 ? WNOHANG : 0);
981 SHMEM_SNAP (threads);
986 fprintf(stderr, "error: %s: wait - %s\n",
987 argv[0], strerror(errno));
992 * This is a hack. We _should_ be able to use
993 * WIFEXITED(status) to see if there was an
994 * error, but it appears to be broken and it
995 * always returns 1 (OK). See wait(2).
997 int err = WEXITSTATUS(status);
1000 "%s: PID %d had rc=%d\n",
1013 static int jt_detach(int argc, char **argv)
1015 struct obd_ioctl_data data;
1020 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1024 if (obd_ioctl_pack(&data, &buf, max)) {
1025 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1029 rc = ioctl(fd, OBD_IOC_DETACH , buf);
1031 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1032 strerror(rc=errno));
1037 static int jt_cleanup(int argc, char **argv)
1039 struct obd_ioctl_data data;
1044 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1048 rc = ioctl(fd, OBD_IOC_CLEANUP , &data);
1050 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1051 strerror(rc=errno));
1056 static int jt_newdev(int argc, char **argv)
1058 struct obd_ioctl_data data;
1066 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1070 rc = ioctl(fd, OBD_IOC_NEWDEV , &data);
1072 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1073 strerror(rc=errno));
1075 printf("Current device set to %d\n", data.ioc_dev);
1081 static int jt_list(int argc, char **argv)
1084 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buf;
1089 memset(buf, 0, sizeof(buf));
1090 data->ioc_version = OBD_IOCTL_VERSION;
1091 data->ioc_addr = conn_addr;
1092 data->ioc_cookie = conn_addr;
1093 data->ioc_len = sizeof(buf);
1094 data->ioc_inllen1 = sizeof(buf) - size_round(sizeof(*data));
1097 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1101 rc = ioctl(fd, OBD_IOC_LIST , data);
1103 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1104 strerror(rc=errno));
1106 printf("%s", data->ioc_bulk);
1112 static int jt_attach(int argc, char **argv)
1114 struct obd_ioctl_data data;
1118 if (argc != 2 && argc != 3 && argc != 4) {
1119 fprintf(stderr, "usage: %s type [name [uuid]]\n",
1124 data.ioc_inllen1 = strlen(argv[1]) + 1;
1125 data.ioc_inlbuf1 = argv[1];
1127 data.ioc_inllen2 = strlen(argv[2]) + 1;
1128 data.ioc_inlbuf2 = argv[2];
1132 data.ioc_inllen3 = strlen(argv[3]) + 1;
1133 data.ioc_inlbuf3 = argv[3];
1136 if (obd_ioctl_pack(&data, &buf, max)) {
1137 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1141 rc = ioctl(fd, OBD_IOC_ATTACH , buf);
1143 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
1144 OBD_IOC_ATTACH, strerror(rc = errno));
1145 else if (argc == 3) {
1147 if (strlen(argv[2]) > 128) {
1148 printf("Name too long to set environment\n");
1151 snprintf(name, 512, "LUSTRE_DEV_%s", argv[2]);
1152 rc = setenv(name, argv[1], 1);
1154 printf("error setting env variable %s\n", name);
1161 #define N2D_OFF 0x100 /* So we can tell between error codes and devices */
1163 static int do_name2dev(char *func, char *name)
1165 struct obd_ioctl_data data;
1172 data.ioc_inllen1 = strlen(name) + 1;
1173 data.ioc_inlbuf1 = name;
1175 if (obd_ioctl_pack(&data, &buf, max)) {
1176 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(func));
1179 rc = ioctl(fd, OBD_IOC_NAME2DEV , buf);
1181 fprintf(stderr, "error: %s: %s - %s\n", cmdname(func),
1182 name, strerror(rc = errno));
1186 memcpy((char *)(&data), buf, sizeof(data));
1188 return data.ioc_dev + N2D_OFF;
1191 static int jt_name2dev(int argc, char **argv)
1194 Parser_printhelp("name2dev");
1198 rc = do_name2dev(argv[0], argv[1]);
1199 if (rc >= N2D_OFF) {
1200 int dev = rc - N2D_OFF;
1201 rc = do_device(argv[0], dev);
1203 printf("%d\n", dev);
1208 static int jt_setup(int argc, char **argv)
1210 struct obd_ioctl_data data;
1215 fprintf(stderr, "usage: %s [device] [fstype]\n",
1222 if (argv[1][0] == '$') {
1223 rc = do_name2dev(argv[0], argv[1] + 1);
1224 if (rc >= N2D_OFF) {
1225 printf("%s is device %d\n", argv[1],
1227 data.ioc_dev = rc - N2D_OFF;
1230 data.ioc_dev = strtoul(argv[1], NULL, 0);
1231 data.ioc_inllen1 = strlen(argv[1]) + 1;
1232 data.ioc_inlbuf1 = argv[1];
1235 data.ioc_inllen2 = strlen(argv[2]) + 1;
1236 data.ioc_inlbuf2 = argv[2];
1239 if (obd_ioctl_pack(&data, &buf, max)) {
1240 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1243 rc = ioctl(fd, OBD_IOC_SETUP , buf);
1245 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1246 strerror(rc = errno));
1252 static int jt_create(int argc, char **argv)
1254 struct obd_ioctl_data data;
1255 struct timeval next_time;
1256 int count = 1, next_count;
1261 if (argc < 2 || argc > 4) {
1262 fprintf(stderr, "usage: %s num [mode] [verbose]\n",
1266 count = strtoul(argv[1], NULL, 0);
1269 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
1271 data.ioc_obdo1.o_mode = 0100644;
1272 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
1274 verbose = get_verbose(argv[3]);
1276 printf("%s: %d obdos\n", cmdname(argv[0]), count);
1277 gettimeofday(&next_time, NULL);
1278 next_time.tv_sec -= verbose;
1280 for (i = 1, next_count = verbose; i <= count ; i++) {
1281 rc = ioctl(fd, OBD_IOC_CREATE , &data);
1284 fprintf(stderr, "error: %s: #%d - %s\n",
1285 cmdname(argv[0]), i, strerror(rc = errno));
1288 if (be_verbose(verbose, &next_time, i, &next_count, count))
1289 printf("%s: #%d is object id %Ld\n", cmdname(argv[0]),
1290 i, data.ioc_obdo1.o_id);
1295 static int jt_setattr(int argc, char **argv)
1297 struct obd_ioctl_data data;
1301 fprintf(stderr, "usage: %s id mode\n", cmdname(argv[0]));
1305 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
1306 data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], NULL, 0);
1307 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
1309 rc = ioctl(fd, OBD_IOC_SETATTR , &data);
1311 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1312 strerror(rc = errno));
1317 static int jt_destroy(int argc, char **argv)
1319 struct obd_ioctl_data data;
1323 fprintf(stderr, "usage: %s id\n", cmdname(argv[0]));
1327 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
1328 data.ioc_obdo1.o_mode = S_IFREG|0644;
1330 rc = ioctl(fd, OBD_IOC_DESTROY , &data);
1332 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1333 strerror(rc = errno));
1338 static int jt_getattr(int argc, char **argv)
1340 struct obd_ioctl_data data;
1343 fprintf(stderr, "usage: %s id\n", cmdname(argv[0]));
1348 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
1349 /* to help obd filter */
1350 data.ioc_obdo1.o_mode = 0100644;
1351 data.ioc_obdo1.o_valid = 0xffffffff;
1352 printf("%s: object id %Ld\n", cmdname(argv[0]), data.ioc_obdo1.o_id);
1354 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
1356 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1357 strerror(rc=errno));
1359 printf("%s: object id %Ld, mode %o\n", cmdname(argv[0]),
1360 data.ioc_obdo1.o_id, data.ioc_obdo1.o_mode);
1365 static int jt_test_getattr(int argc, char **argv)
1367 struct obd_ioctl_data data;
1368 struct timeval start, next_time;
1369 int i, count, next_count;
1372 if (argc != 2 && argc != 3) {
1373 fprintf(stderr, "usage: %s count [verbose]\n",cmdname(argv[0]));
1378 count = strtoul(argv[1], NULL, 0);
1381 verbose = get_verbose(argv[2]);
1385 data.ioc_obdo1.o_valid = 0xffffffff;
1386 data.ioc_obdo1.o_id = 2;
1387 gettimeofday(&start, NULL);
1388 next_time.tv_sec = start.tv_sec - verbose;
1389 next_time.tv_usec = start.tv_usec;
1391 printf("%s: getting %d attrs (testing only): %s", cmdname(argv[0]),
1392 count, ctime(&start.tv_sec));
1394 for (i = 1, next_count = verbose; i <= count; i++) {
1395 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
1398 fprintf(stderr, "error: %s: #%d - %s\n",
1399 cmdname(argv[0]), i, strerror(rc = errno));
1402 if (be_verbose(verbose, &next_time, i,&next_count,count))
1403 printf("%s: got attr #%d\n", cmdname(argv[0]), i);
1411 gettimeofday(&end, NULL);
1413 diff = difftime(&end, &start);
1417 printf("%s: %d attrs in %.4gs (%.4g attr/s): %s",
1418 cmdname(argv[0]), i, diff, (double)i / diff,
1419 ctime(&end.tv_sec));
1424 static int jt_test_brw(int argc, char **argv)
1426 struct obd_ioctl_data data;
1427 struct timeval start, next_time;
1429 int pages = 1, obdos = 1, count, next_count;
1430 int verbose = 1, write = 0, rw;
1434 if (argc < 2 || argc > 6) {
1436 "usage: %s count [write [verbose [pages [obdos]]]]\n",
1441 count = strtoul(argv[1], NULL, 0);
1444 if (argv[2][0] == 'w' || argv[2][0] == '1')
1446 else if (argv[2][0] == 'r' || argv[2][0] == '0')
1449 verbose = get_verbose(argv[3]);
1453 pages = strtoul(argv[4], NULL, 0);
1455 obdos = strtoul(argv[5], NULL, 0);
1457 if (obdos != 1 && obdos != 2) {
1458 fprintf(stderr, "error: %s: only 1 or 2 obdos supported\n",
1463 len = pages * PAGE_SIZE;
1465 bulk = calloc(obdos, len);
1467 fprintf(stderr, "error: %s: no memory allocating %dx%d pages\n",
1468 cmdname(argv[0]), obdos, pages);
1472 data.ioc_obdo1.o_id = 2;
1473 data.ioc_count = len;
1474 data.ioc_offset = 0;
1475 data.ioc_plen1 = len;
1476 data.ioc_pbuf1 = bulk;
1478 data.ioc_obdo2.o_id = 3;
1479 data.ioc_plen2 = len;
1480 data.ioc_pbuf2 = bulk + len;
1483 gettimeofday(&start, NULL);
1484 next_time.tv_sec = start.tv_sec - verbose;
1485 next_time.tv_usec = start.tv_usec;
1488 printf("%s: %s %d (%dx%d pages) (testing only): %s",
1489 cmdname(argv[0]), write ? "writing" : "reading",
1490 count, obdos, pages, ctime(&start.tv_sec));
1493 * We will put in the start time (and loop count inside the loop)
1494 * at the beginning of each page so that we will be able to validate
1495 * (at some later time) whether the data actually made it or not.
1497 * XXX we do not currently use any of this memory in OBD_IOC_BRW_*
1498 * just to avoid the overhead of the copy_{to,from}_user. It
1499 * can be fixed if we ever need to send real data around.
1501 for (o = 0, b = bulk; o < obdos; o++)
1502 for (p = 0; p < pages; p++, b += PAGE_SIZE)
1503 memcpy(b, &start, sizeof(start));
1505 rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
1506 for (i = 1, next_count = verbose; i <= count; i++) {
1508 b = bulk + sizeof(struct timeval);
1509 for (o = 0; o < obdos; o++)
1510 for (p = 0; p < pages; p++, b += PAGE_SIZE)
1511 memcpy(b, &count, sizeof(count));
1514 rc = ioctl(fd, rw, &data);
1517 fprintf(stderr, "error: %s: #%d - %s on %s\n",
1518 cmdname(argv[0]), i, strerror(rc = errno),
1519 write ? "write" : "read");
1521 } else if (be_verbose(verbose, &next_time, i,&next_count,count))
1522 printf("%s: %s number %d\n", cmdname(argv[0]),
1523 write ? "write" : "read", i);
1532 gettimeofday(&end, NULL);
1534 diff = difftime(&end, &start);
1538 printf("%s: %s %dx%dx%d pages in %.4gs (%.4g pg/s): %s",
1539 cmdname(argv[0]), write ? "wrote" : "read", obdos,
1540 pages, i, diff, (double)obdos * i * pages / diff,
1541 ctime(&end.tv_sec));
1546 static int jt_lov_config(int argc, char **argv)
1548 struct obd_ioctl_data data;
1549 struct lov_desc desc;
1554 printf("WARNING: obdctl lovconfig NOT MAINTAINED\n");
1558 Parser_printhelp("lovconfig");
1562 if (strlen(argv[1]) > sizeof(uuid_t) - 1) {
1563 fprintf(stderr, "lov_config: no %dB memory for uuid's\n",
1568 memset(&desc, 0, sizeof(desc));
1569 strcpy(desc.ld_uuid, argv[1]);
1570 desc.ld_default_stripe_count = strtoul(argv[2], NULL, 0);
1571 desc.ld_default_stripe_size = strtoul(argv[3], NULL, 0);
1572 desc.ld_pattern = strtoul(argv[4], NULL, 0);
1573 desc.ld_tgt_count = argc - 5;
1576 size = sizeof(uuid_t) * desc.ld_tgt_count;
1577 uuidarray = malloc(size);
1579 fprintf(stderr, "lov_config: no %dB memory for uuid's\n",
1583 memset(uuidarray, 0, size);
1584 for (i=5 ; i < argc ; i++) {
1585 char *buf = (char *) (uuidarray + i -5 );
1586 if (strlen(argv[i]) >= sizeof(uuid_t)) {
1587 fprintf(stderr, "lov_config: arg %d (%s) too long\n",
1592 strcpy(buf, argv[i]);
1595 data.ioc_inllen1 = sizeof(desc);
1596 data.ioc_inlbuf1 = (char *)&desc;
1597 data.ioc_inllen2 = size;
1598 data.ioc_inlbuf2 = (char *)uuidarray;
1600 if (obd_ioctl_pack(&data, &buf, max)) {
1601 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1605 rc = ioctl(fd, OBD_IOC_LOV_CONFIG , buf);
1607 fprintf(stderr, "lov_config: error: %s: %s\n",
1608 cmdname(argv[0]),strerror(rc = errno));
1614 static int jt_test_ldlm(int argc, char **argv)
1616 struct obd_ioctl_data data;
1620 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1624 rc = ioctl(fd, IOC_LDLM_TEST, &data);
1626 fprintf(stderr, "error: %s: test failed: %s\n",
1627 cmdname(argv[0]), strerror(rc = errno));
1631 static int jt_newconn(int argc, char **argv)
1633 struct obd_ioctl_data data;
1637 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1641 rc = ioctl(fd, OBD_IOC_RECOVD_NEWCONN , &data);
1643 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1644 strerror(rc = errno));
1649 static int jt_quit(int argc, char **argv)
1651 Parser_quit(argc, argv);
1656 command_t cmdlist[] = {
1658 {"--xml", jt__xml, 0, "--xml <xml file> <command [args ...]>"},
1659 {"--device", jt__device, 0, "--device <devno> <command [args ...]>"},
1660 {"--threads", jt__threads, 0,
1661 "--threads <threads> <devno> <command [args ...]>"},
1663 /* Device configuration commands */
1664 {"lovconfig", jt_lov_config, 0, "configure lov data on MDS "
1665 "[usage: lovconfig lov-uuid stripecount, stripesize, pattern, UUID1, [UUID2, ...]"},
1666 {"list", jt_list, 0, "list the devices (no args)"},
1667 {"newdev", jt_newdev, 0, "set device to a new unused obd (no args)"},
1668 {"device", jt_device, 0, "set current device (args device_no name)"},
1669 {"name2dev", jt_name2dev, 0, "set device by name [usage: name2dev devname]"},
1670 {"attach", jt_attach, 0, "name the type of device (args: type data"},
1671 {"setup", jt_setup, 0, "setup device (args: <blkdev> [data]"},
1672 {"detach", jt_detach, 0, "detach the current device (arg: )"},
1673 {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
1675 /* Session commands */
1676 {"connect", jt_connect, 0, "connect - get a connection to device"},
1677 {"disconnect", jt_disconnect, 0,
1678 "disconnect - break connection to device"},
1680 /* Session operations */
1681 {"create", jt_create, 0, "create [count [mode [verbose]]]"},
1682 {"destroy", jt_destroy, 0, "destroy <id>"},
1683 {"getattr", jt_getattr, 0, "getattr <id>"},
1684 {"setattr", jt_setattr, 0, "setattr <id> <mode>"},
1685 {"newconn", jt_newconn, 0, "newconn [newuuid]"},
1686 {"test_getattr", jt_test_getattr, 0, "test_getattr <count> [verbose]"},
1687 {"test_brw", jt_test_brw, 0, "test_brw <count> [write [verbose]]"},
1688 {"test_ldlm", jt_test_ldlm, 0, "test lock manager (no args)"},
1690 /* User interface commands */
1691 {"help", Parser_help, 0, "help"},
1692 {"exit", jt_quit, 0, "quit"},
1693 {"quit", jt_quit, 0, "quit"},
1698 static void signal_server(int sig)
1700 if (sig == SIGINT) {
1701 do_disconnect("sigint", 1);
1704 fprintf(stderr, "%s: got signal %d\n", cmdname("sigint"), sig);
1708 int main(int argc, char **argv)
1710 struct sigaction sigact;
1712 sigact.sa_handler = signal_server;
1713 sigfillset(&sigact.sa_mask);
1714 sigact.sa_flags = SA_RESTART;
1715 sigaction(SIGINT, &sigact, NULL);
1717 setlinebuf (stdout);
1721 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1723 Parser_init("obdctl > ", cmdlist);
1724 rc = Parser_commands();
1727 do_disconnect(argv[0], 1);