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>
54 #include <linux/list.h>
60 static int jt_newdev(int argc, char **argv);
61 static int jt_attach(int argc, char **argv);
62 static int jt_setup(int argc, char **argv);
66 uint64_t conn_addr = -1;
74 #define IOCINIT(data) \
76 memset(&data, 0, sizeof(data)); \
77 data.ioc_version = OBD_IOCTL_VERSION; \
78 data.ioc_addr = conn_addr; \
79 data.ioc_cookie = conn_cookie; \
80 data.ioc_len = sizeof(data); \
82 fprintf(stderr, "No device open, use device\n"); \
88 pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L",
107 0, 0, # struct list_head
113 char * obdo_print(struct obdo *obd)
117 sprintf(buf, "id: %Ld\ngrp: %Ld\natime: %Ld\nmtime: %Ld\nctime: %Ld\n"
118 "size: %Ld\nblocks: %Ld\nblksize: %d\nmode: %o\nuid: %d\n"
119 "gid: %d\nflags: %x\nobdflags: %x\nnlink: %d,\nvalid %x\n",
138 static char *cmdname(char *func)
140 static char buf[512];
143 sprintf(buf, "%s-%d", func, thread);
150 int getfd(char *func)
153 fd = open("/dev/obd", O_RDWR);
155 fprintf(stderr, "error: %s: opening /dev/obd: %s\n",
156 cmdname(func), strerror(errno));
162 #define difftime(a, b) \
163 ((double)(a)->tv_sec - (b)->tv_sec + \
164 ((double)((a)->tv_usec - (b)->tv_usec) / 1000000))
166 static int be_verbose(int verbose, struct timeval *next_time,
167 int num, int *next_num, int num_total)
174 if (next_time != NULL)
175 gettimeofday(&now, NULL);
177 /* A positive verbosity means to print every X iterations */
179 (next_num == NULL || num >= *next_num || num >= num_total)) {
180 *next_num += verbose;
182 next_time->tv_sec = now.tv_sec - verbose;
183 next_time->tv_usec = now.tv_usec;
188 /* A negative verbosity means to print at most each X seconds */
189 if (verbose < 0 && next_time != NULL && difftime(&now, next_time) >= 0){
190 next_time->tv_sec = now.tv_sec - verbose;
191 next_time->tv_usec = now.tv_usec;
200 static int get_verbose(const char *arg)
204 if (!arg || arg[0] == 'v')
206 else if (arg[0] == 's' || arg[0] == 'q')
209 verbose = (int) strtoul(arg, NULL, 0);
212 printf("Print status every %d seconds\n", -verbose);
213 else if (verbose == 1)
214 printf("Print status every operation\n");
215 else if (verbose > 1)
216 printf("Print status every %d operations\n", verbose);
221 static int do_disconnect(char *func, int verbose)
223 struct obd_ioctl_data data;
230 rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
232 fprintf(stderr, "error: %s: %x %s\n", cmdname(func),
233 OBD_IOC_DISCONNECT, strerror(errno));
236 printf("%s: disconnected conn %Lx\n", cmdname(func),
244 extern command_t cmdlist[];
246 static int xml_command(char *cmd, ...) {
254 while (((arg = va_arg(args, char *)) != NULL) && (i < 8)) {
262 for (j = 0; j < i; j++)
263 printf("%s ", cmds[j]);
267 return Parser_execarg(i, cmds, cmdlist);
270 static network_t *xml_network(xmlDocPtr doc, xmlNodePtr root) {
271 xmlNodePtr cur = root->xmlChildrenNode;
274 if ((net = (network_t *)calloc(1, sizeof(network_t))) == NULL) {
275 printf("error: unable to malloc network_t\n");
279 net->type = xmlGetProp(root, "type");
280 if (net->type == NULL) {
281 printf("error: type attrib required (tcp, elan, myrinet)\n");
286 while (cur != NULL) {
287 if (!xmlStrcmp(cur->name, "server"))
288 net->server = xmlNodeGetContent(cur);
290 if (!xmlStrcmp(cur->name, "port"))
291 net->port = atoi(xmlNodeGetContent(cur));
296 if (net->server == NULL) {
297 printf("error: <server> tag required\n");
305 static int xml_mds(xmlDocPtr doc, xmlNodePtr root,
306 char *serv_name, char *serv_uuid) {
307 xmlNodePtr cur = root->xmlChildrenNode;
308 char *fstype = NULL, *device = NULL;
311 printf("--- Setting up MDS ---\n");
312 while (cur != NULL) {
313 if (!xmlStrcmp(cur->name, "fstype"))
314 fstype = xmlNodeGetContent(cur);
316 if (!xmlStrcmp(cur->name, "device"))
317 device = xmlNodeGetContent(cur);
319 /* FIXME: Parse the network bits
320 * if (!xmlStrcmp(cur->name, "network")) {
321 * net = xml_network(doc, cur);
330 if ((fstype == NULL) || (device == NULL)) {
331 printf("error: <fstype> and <device> tags required\n");
335 if ((rc = xml_command("newdev", NULL)) != 0)
338 if ((rc = xml_command("attach","mds",serv_name,serv_uuid,NULL)) != 0)
341 if ((rc = xml_command("setup", device, fstype, NULL)) != 0)
347 static int xml_obd(xmlDocPtr doc, xmlNodePtr root,
348 char *serv_name, char *serv_uuid) {
349 char *obdtype, *format = NULL, *fstype = NULL, *device = NULL;
350 xmlNodePtr cur = root->xmlChildrenNode;
353 obdtype = xmlGetProp(root, "type");
354 printf("--- Setting up OBD ---\n");
356 while (cur != NULL) {
357 if (!xmlStrcmp(cur->name, "fstype"))
358 fstype = xmlNodeGetContent(cur);
360 if (!xmlStrcmp(cur->name, "device"))
361 device = xmlNodeGetContent(cur);
363 if (!xmlStrcmp(cur->name, "autoformat"))
364 format = xmlNodeGetContent(cur);
369 if ((obdtype == NULL) || (fstype == NULL) || (device == NULL)) {
370 printf("error: 'type' attrib and <fstype> "
371 "and <device> tags required\n");
375 /* FIXME: Building and configuring loopback devices should go here
376 * but is currently unsupported. You'll have to use the scripts
377 * for now until support is added, or specify a real device.
380 if ((rc = xml_command("newdev", NULL)) != 0)
383 if ((rc = xml_command("attach", obdtype,
384 serv_name,serv_uuid, NULL)) != 0)
387 if ((rc = xml_command("setup", device, fstype, NULL)) != 0)
393 static int xml_ost(xmlDocPtr doc, xmlNodePtr root,
394 char *serv_name, char *serv_uuid) {
395 char *server_name = NULL, *server_uuid = NULL;
396 char *failover_name = NULL, *failover_uuid = NULL;
397 xmlNodePtr cur = root->xmlChildrenNode;
398 int server_num, failover_num, rc;
400 printf("--- Setting up OST ---\n");
401 while (cur != NULL) {
402 if (!xmlStrcmp(cur->name, "server_id")) {
403 server_num = atoi(xmlGetProp(cur, "num"));
404 server_name = xmlGetProp(cur, "name");
405 server_uuid = xmlGetProp(cur, "uuid");
408 /* FIXME: Properly handle multiple failover servers */
409 if (!xmlStrcmp(cur->name, "failover_id")) {
410 failover_num = atoi(xmlGetProp(cur, "num"));
411 failover_name = xmlGetProp(cur, "name");
412 failover_uuid = xmlGetProp(cur, "uuid");
418 if ((server_name == NULL) || (server_uuid == NULL)) {
419 printf("error: atleast the <server_id> tag is required\n");
423 if ((rc = xml_command("newdev", NULL)) != 0)
426 if ((rc = xml_command("attach","ost",serv_name,serv_uuid,NULL)) != 0)
429 if ((rc = xml_command("setup", server_name, NULL)) != 0)
435 static int xml_osc(xmlDocPtr doc, xmlNodePtr root,
436 char *serv_name, char *serv_uuid) {
437 char *ost_name = NULL, *ost_uuid = NULL;
438 xmlNodePtr cur = root->xmlChildrenNode;
441 printf("--- Setting up OSC ---\n");
442 while (cur != NULL) {
443 if (!xmlStrcmp(cur->name, "service_id")) {
444 ost_num = atoi(xmlGetProp(cur, "num"));
445 ost_name = xmlGetProp(cur, "name");
446 ost_uuid = xmlGetProp(cur, "uuid");
452 if ((ost_name == NULL) || (ost_uuid == NULL)) {
453 printf("error: atleast the <service_id> tag is required\n");
457 if ((rc = xml_command("newdev", NULL)) != 0)
460 if ((rc = xml_command("attach","osc",serv_name,serv_uuid,NULL)) != 0)
463 if ((rc = xml_command("setup", ost_uuid, "localhost", NULL)) != 0)
469 static int xml_mdc(xmlDocPtr doc, xmlNodePtr root,
470 char *serv_name, char *serv_uuid) {
471 char *mds_name = NULL, *mds_uuid = NULL;
472 xmlNodePtr cur = root->xmlChildrenNode;
475 printf("--- Setting up MDC ---\n");
476 while (cur != NULL) {
477 if (!xmlStrcmp(cur->name, "service_id")) {
478 mds_num = atoi(xmlGetProp(cur, "num"));
479 mds_name = xmlGetProp(cur, "name");
480 mds_uuid = xmlGetProp(cur, "uuid");
486 if ((mds_name == NULL) || (mds_uuid == NULL)) {
487 printf("error: atleast the <service_id> tag is required\n");
491 if ((rc = xml_command("newdev", NULL)) != 0)
494 if ((rc = xml_command("attach","mdc",serv_name,serv_uuid,NULL)) != 0)
497 if ((rc = xml_command("setup", mds_uuid, "localhost", NULL)) != 0)
503 static int xml_lov(xmlDocPtr doc, xmlNodePtr root,
504 char *serv_name, char *serv_uuid) {
505 printf("--- Setting up LOV ---\n");
509 static int xml_router(xmlDocPtr doc, xmlNodePtr root,
510 char *serv_name, char *serv_uuid) {
511 printf("--- Setting up ROUTER ---\n");
515 static int xml_ldlm(xmlDocPtr doc, xmlNodePtr root,
516 char *serv_name, char *serv_uuid) {
519 printf("--- Setting up LDLM ---\n");
520 if ((rc = xml_command("newdev", NULL)) != 0)
523 if ((rc = xml_command("attach","ldlm",serv_name,serv_uuid,NULL)) != 0)
526 if ((rc = xml_command("setup", NULL)) != 0)
532 static int xml_service(xmlDocPtr doc, xmlNodePtr root,
533 int serv_num, char *serv_name, char *serv_uuid) {
534 xmlNodePtr cur = root;
537 while (cur != NULL) {
538 name = xmlGetProp(cur, "name");
539 uuid = xmlGetProp(cur, "uuid");
541 if (xmlStrcmp(name, serv_name) ||
542 xmlStrcmp(uuid, serv_uuid)) {
547 if (!xmlStrcmp(cur->name, "mds"))
548 return xml_mds(doc, cur, name, uuid);
549 else if (!xmlStrcmp(cur->name, "obd"))
550 return xml_obd(doc, cur, name, uuid);
551 else if (!xmlStrcmp(cur->name, "ost"))
552 return xml_ost(doc, cur, name, uuid);
553 else if (!xmlStrcmp(cur->name, "osc"))
554 return xml_osc(doc, cur, name, uuid);
555 else if (!xmlStrcmp(cur->name, "mdc"))
556 return xml_mdc(doc, cur, name, uuid);
557 else if (!xmlStrcmp(cur->name, "lov"))
558 return xml_lov(doc, cur, name, uuid);
559 else if (!xmlStrcmp(cur->name, "router"))
560 return xml_router(doc, cur, name, uuid);
561 else if (!xmlStrcmp(cur->name, "ldlm"))
562 return xml_ldlm(doc, cur, name, uuid);
569 printf("error: No XML config branch for name=%s uuid=%s\n",
570 serv_name, serv_uuid);
574 static int xml_profile(xmlDocPtr doc, xmlNodePtr root,
575 int prof_num, char *prof_name, char *prof_uuid) {
576 xmlNodePtr parent, cur = root;
577 char *name, *uuid, *srv_name, *srv_uuid;
580 while (cur != NULL) {
581 name = xmlGetProp(cur, "name");
582 uuid = xmlGetProp(cur, "uuid");
584 if (xmlStrcmp(cur->name, "profile") ||
585 xmlStrcmp(name, prof_name) ||
586 xmlStrcmp(uuid, prof_uuid)) {
591 /* FIXME: Doesn't understand mountpoints yet
592 * xml_mountpoint(doc, root, ...);
595 /* Setup each service in turn
596 * FIXME: Should be sorted by "num" attr, we shouldn't
597 * assume they're in order in the XML document.
600 cur = cur->xmlChildrenNode;
601 while (cur != NULL) {
602 if (!xmlStrcmp(cur->name, "service_id")) {
603 num = atoi(xmlGetProp(cur, "num"));
604 rc = xml_service(doc, root, num,
605 srv_name = xmlGetProp(cur, "name"),
606 srv_uuid = xmlGetProp(cur, "uuid"));
608 printf("error: service config\n");
622 static int xml_node(xmlDocPtr doc, xmlNodePtr root) {
623 xmlNodePtr parent, cur = root;
627 /* Walk the node tags looking for ours */
628 while (cur != NULL) {
629 if (xmlStrcmp(cur->name, "node")) {
634 name = xmlGetProp(cur, "name");
638 uuid = xmlGetProp(cur, "uuid");
642 /* FIXME: Verify our NAME and UUID against /etc/lustre/id
643 * so we're sure we are who we think we are.
646 /* Setup each profile in turn
647 * FIXME: Should be sorted by "num" attr, we shouldn't
648 * assume they're in order in the XML document.
651 cur = cur->xmlChildrenNode;
652 while (cur != NULL) {
653 if (!xmlStrcmp(cur->name, "profile_id")) {
654 num = atoi(xmlGetProp(cur, "num"));
655 rc = xml_profile(doc, root, num,
656 xmlGetProp(cur, "name"),
657 xmlGetProp(cur, "uuid"));
671 static int do_xml(char *func, char *file)
677 doc = xmlParseFile(file);
679 fprintf(stderr, "error: Unable to parse XML\n");
683 cur = xmlDocGetRootElement(doc);
685 fprintf(stderr, "error: Empty XML\n");
690 if (xmlStrcmp(cur->name, (const xmlChar *)"lustre")) {
691 fprintf(stderr, "error: Root node != <lustre>\n");
696 /* FIXME: Validate the XML against the DTD here */
698 /* FIXME: Merge all the text nodes under each branch and
699 * prune empty nodes. Just to make the parsing more
700 * tolerant, the exact location of nested tags isn't
704 rc = xml_node(doc, cur->xmlChildrenNode);
710 static int do_device(char *func, int dev)
712 struct obd_ioctl_data data;
714 memset(&data, 0, sizeof(data));
721 if (obd_ioctl_pack(&data, &buf, max)) {
722 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(func));
726 return ioctl(fd, OBD_IOC_DEVICE , buf);
729 static int jt_device(int argc, char **argv)
731 do_disconnect(argv[0], 1);
734 fprintf(stderr, "usage: %s devno\n", cmdname(argv[0]));
738 rc = do_device(argv[0], strtoul(argv[1], NULL, 0));
741 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
742 strerror(rc = errno));
747 static int jt_connect(int argc, char **argv)
749 struct obd_ioctl_data data;
753 do_disconnect(argv[0], 1);
756 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
760 rc = ioctl(fd, OBD_IOC_CONNECT , &data);
762 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
763 OBD_IOC_CONNECT, strerror(rc = errno));
765 conn_addr = data.ioc_addr;
766 conn_cookie = data.ioc_cookie;
770 static int jt_disconnect(int argc, char **argv)
773 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
780 return do_disconnect(argv[0], 0);
783 static int jt__xml(int argc, char **argv)
786 fprintf(stderr, "usage: %s <xml file> <command [args ...]>\n",
791 return do_xml("xml", argv[1]);
794 static int jt__device(int argc, char **argv)
800 fprintf(stderr, "usage: %s devno <command [args ...]>\n",
805 rc = do_device("device", strtoul(argv[1], NULL, 0));
810 rc = jt_connect(1, arg2);
814 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
816 ret = do_disconnect(argv[0], 0);
823 static int jt__threads(int argc, char **argv)
825 int threads, next_thread;
831 "usage: %s numthreads verbose devno <cmd [args ...]>\n",
836 threads = strtoul(argv[1], NULL, 0);
838 verbose = get_verbose(argv[2]);
840 printf("%s: starting %d threads on device %s running %s\n",
841 argv[0], threads, argv[3], argv[4]);
843 for (i = 1, next_thread = verbose; i <= threads; i++) {
846 fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
847 strerror(rc = errno));
849 } else if (rc == 0) {
851 argv[2] = "--device";
852 return jt__device(argc - 2, argv + 2);
853 } else if (be_verbose(verbose, NULL, i, &next_thread, threads))
854 printf("%s: thread #%d (PID %d) started\n",
859 if (!thread) { /* parent process */
861 printf("%s: started %d threads\n\n", argv[0], i - 1);
865 for (j = 1; j < i; j++) {
867 int ret = wait(&status);
870 fprintf(stderr, "error: %s: wait - %s\n",
871 argv[0], strerror(errno));
876 * This is a hack. We _should_ be able to use
877 * WIFEXITED(status) to see if there was an
878 * error, but it appears to be broken and it
879 * always returns 1 (OK). See wait(2).
881 int err = WEXITSTATUS(status);
884 "%s: PID %d had rc=%d\n",
895 static int jt_detach(int argc, char **argv)
897 struct obd_ioctl_data data;
902 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
906 if (obd_ioctl_pack(&data, &buf, max)) {
907 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
911 rc = ioctl(fd, OBD_IOC_DETACH , buf);
913 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
919 static int jt_cleanup(int argc, char **argv)
921 struct obd_ioctl_data data;
926 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
930 rc = ioctl(fd, OBD_IOC_CLEANUP , &data);
932 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
938 static int jt_newdev(int argc, char **argv)
940 struct obd_ioctl_data data;
948 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
952 rc = ioctl(fd, OBD_IOC_NEWDEV , &data);
954 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
957 printf("Current device set to %d\n", data.ioc_dev);
963 static int jt_list(int argc, char **argv)
966 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buf;
971 memset(buf, 0, sizeof(buf));
972 data->ioc_version = OBD_IOCTL_VERSION;
973 data->ioc_addr = conn_addr;
974 data->ioc_cookie = conn_addr;
975 data->ioc_len = sizeof(buf);
976 data->ioc_inllen1 = sizeof(buf) - size_round(sizeof(*data));
979 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
983 rc = ioctl(fd, OBD_IOC_LIST , data);
985 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
988 printf("%s", data->ioc_bulk);
994 static int jt_attach(int argc, char **argv)
996 struct obd_ioctl_data data;
1000 if (argc != 2 && argc != 3 && argc != 4) {
1001 fprintf(stderr, "usage: %s type [name [uuid]]\n",
1006 data.ioc_inllen1 = strlen(argv[1]) + 1;
1007 data.ioc_inlbuf1 = argv[1];
1009 data.ioc_inllen2 = strlen(argv[2]) + 1;
1010 data.ioc_inlbuf2 = argv[2];
1014 data.ioc_inllen3 = strlen(argv[3]) + 1;
1015 data.ioc_inlbuf3 = argv[3];
1018 if (obd_ioctl_pack(&data, &buf, max)) {
1019 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1023 rc = ioctl(fd, OBD_IOC_ATTACH , buf);
1025 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
1026 OBD_IOC_ATTACH, strerror(rc = errno));
1027 else if (argc == 3) {
1029 if (strlen(argv[2]) > 128) {
1030 printf("Name too long to set environment\n");
1033 snprintf(name, 512, "LUSTRE_DEV_%s", argv[2]);
1034 rc = setenv(name, argv[1], 1);
1036 printf("error setting env variable %s\n", name);
1043 #define N2D_OFF 0x100 /* So we can tell between error codes and devices */
1045 static int do_name2dev(char *func, char *name)
1047 struct obd_ioctl_data data;
1054 data.ioc_inllen1 = strlen(name) + 1;
1055 data.ioc_inlbuf1 = name;
1057 if (obd_ioctl_pack(&data, &buf, max)) {
1058 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(func));
1061 rc = ioctl(fd, OBD_IOC_NAME2DEV , buf);
1063 fprintf(stderr, "error: %s: %s - %s\n", cmdname(func),
1064 name, strerror(rc = errno));
1068 memcpy((char *)(&data), buf, sizeof(data));
1070 return data.ioc_dev + N2D_OFF;
1073 static int jt_name2dev(int argc, char **argv)
1076 fprintf(stderr, "usage: %s name\n", cmdname(argv[0]));
1080 rc = do_name2dev(argv[0], argv[1]);
1081 if (rc >= N2D_OFF) {
1082 int dev = rc - N2D_OFF;
1083 rc = do_device(argv[0], dev);
1085 printf("%d\n", dev);
1090 static int jt_setup(int argc, char **argv)
1092 struct obd_ioctl_data data;
1097 fprintf(stderr, "usage: %s [device] [fstype]\n",
1104 if (argv[1][0] == '$') {
1105 rc = do_name2dev(argv[0], argv[1] + 1);
1106 if (rc >= N2D_OFF) {
1107 printf("%s is device %d\n", argv[1],
1109 data.ioc_dev = rc - N2D_OFF;
1112 data.ioc_dev = strtoul(argv[1], NULL, 0);
1113 data.ioc_inllen1 = strlen(argv[1]) + 1;
1114 data.ioc_inlbuf1 = argv[1];
1117 data.ioc_inllen2 = strlen(argv[2]) + 1;
1118 data.ioc_inlbuf2 = argv[2];
1121 if (obd_ioctl_pack(&data, &buf, max)) {
1122 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
1125 rc = ioctl(fd, OBD_IOC_SETUP , buf);
1127 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1128 strerror(rc = errno));
1134 static int jt_create(int argc, char **argv)
1136 struct obd_ioctl_data data;
1137 struct timeval next_time;
1138 int count = 1, next_count;
1143 if (argc < 2 || argc > 4) {
1144 fprintf(stderr, "usage: %s num [mode] [verbose]\n",
1148 count = strtoul(argv[1], NULL, 0);
1151 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
1153 data.ioc_obdo1.o_mode = 0100644;
1154 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
1156 verbose = get_verbose(argv[3]);
1158 printf("%s: %d obdos\n", cmdname(argv[0]), count);
1159 gettimeofday(&next_time, NULL);
1160 next_time.tv_sec -= verbose;
1162 for (i = 1, next_count = verbose; i <= count ; i++) {
1163 rc = ioctl(fd, OBD_IOC_CREATE , &data);
1165 fprintf(stderr, "error: %s: #%d - %s\n",
1166 cmdname(argv[0]), i, strerror(rc = errno));
1169 if (be_verbose(verbose, &next_time, i, &next_count, count))
1170 printf("%s: #%d is object id %Ld\n", cmdname(argv[0]),
1171 i, data.ioc_obdo1.o_id);
1176 static int jt_setattr(int argc, char **argv)
1178 struct obd_ioctl_data data;
1182 fprintf(stderr, "usage: %s id mode\n", cmdname(argv[0]));
1186 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
1187 data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], NULL, 0);
1188 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
1190 rc = ioctl(fd, OBD_IOC_SETATTR , &data);
1192 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1193 strerror(rc = errno));
1198 static int jt_destroy(int argc, char **argv)
1200 struct obd_ioctl_data data;
1204 fprintf(stderr, "usage: %s id\n", cmdname(argv[0]));
1208 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
1209 data.ioc_obdo1.o_mode = S_IFREG|0644;
1211 rc = ioctl(fd, OBD_IOC_DESTROY , &data);
1213 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1214 strerror(rc = errno));
1219 static int jt_getattr(int argc, char **argv)
1221 struct obd_ioctl_data data;
1224 fprintf(stderr, "usage: %s id\n", cmdname(argv[0]));
1229 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
1230 /* to help obd filter */
1231 data.ioc_obdo1.o_mode = 0100644;
1232 data.ioc_obdo1.o_valid = 0xffffffff;
1233 printf("%s: object id %Ld\n", cmdname(argv[0]), data.ioc_obdo1.o_id);
1235 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
1237 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1238 strerror(rc=errno));
1240 printf("%s: object id %Ld, mode %o\n", cmdname(argv[0]),
1241 data.ioc_obdo1.o_id, data.ioc_obdo1.o_mode);
1246 static int jt_test_getattr(int argc, char **argv)
1248 struct obd_ioctl_data data;
1249 struct timeval start, next_time;
1250 int i, count, next_count;
1253 if (argc != 2 && argc != 3) {
1254 fprintf(stderr, "usage: %s count [verbose]\n",cmdname(argv[0]));
1259 count = strtoul(argv[1], NULL, 0);
1262 verbose = get_verbose(argv[2]);
1266 data.ioc_obdo1.o_valid = 0xffffffff;
1267 data.ioc_obdo1.o_id = 2;
1268 gettimeofday(&start, NULL);
1269 next_time.tv_sec = start.tv_sec - verbose;
1270 next_time.tv_usec = start.tv_usec;
1271 printf("%s: getting %d attrs (testing only): %s", cmdname(argv[0]),
1272 count, ctime(&start.tv_sec));
1274 for (i = 1, next_count = verbose; i <= count; i++) {
1275 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
1277 fprintf(stderr, "error: %s: #%d - %s\n",
1278 cmdname(argv[0]), i, strerror(rc = errno));
1281 if (be_verbose(verbose, &next_time, i,&next_count,count))
1282 printf("%s: got attr #%d\n", cmdname(argv[0]), i);
1290 gettimeofday(&end, NULL);
1292 diff = difftime(&end, &start);
1295 printf("%s: %d attrs in %.4gs (%.4g attr/s): %s",
1296 cmdname(argv[0]), i, diff, (double)i / diff,
1297 ctime(&end.tv_sec));
1302 static int jt_test_brw(int argc, char **argv)
1304 struct obd_ioctl_data data;
1305 struct timeval start, next_time;
1307 int pages = 1, obdos = 1, count, next_count;
1308 int verbose = 1, write = 0, rw;
1312 if (argc < 2 || argc > 6) {
1314 "usage: %s count [write [verbose [pages [obdos]]]]\n",
1319 count = strtoul(argv[1], NULL, 0);
1322 if (argv[2][0] == 'w' || argv[2][0] == '1')
1324 else if (argv[2][0] == 'r' || argv[2][0] == '0')
1327 verbose = get_verbose(argv[3]);
1331 pages = strtoul(argv[4], NULL, 0);
1333 obdos = strtoul(argv[5], NULL, 0);
1335 if (obdos != 1 && obdos != 2) {
1336 fprintf(stderr, "error: %s: only 1 or 2 obdos supported\n",
1341 len = pages * PAGE_SIZE;
1343 bulk = calloc(obdos, len);
1345 fprintf(stderr, "error: %s: no memory allocating %dx%d pages\n",
1346 cmdname(argv[0]), obdos, pages);
1350 data.ioc_obdo1.o_id = 2;
1351 data.ioc_count = len;
1352 data.ioc_offset = 0;
1353 data.ioc_plen1 = len;
1354 data.ioc_pbuf1 = bulk;
1356 data.ioc_obdo2.o_id = 3;
1357 data.ioc_plen2 = len;
1358 data.ioc_pbuf2 = bulk + len;
1361 gettimeofday(&start, NULL);
1362 next_time.tv_sec = start.tv_sec - verbose;
1363 next_time.tv_usec = start.tv_usec;
1365 printf("%s: %s %d (%dx%d pages) (testing only): %s",
1366 cmdname(argv[0]), write ? "writing" : "reading",
1367 count, obdos, pages, ctime(&start.tv_sec));
1370 * We will put in the start time (and loop count inside the loop)
1371 * at the beginning of each page so that we will be able to validate
1372 * (at some later time) whether the data actually made it or not.
1374 * XXX we do not currently use any of this memory in OBD_IOC_BRW_*
1375 * just to avoid the overhead of the copy_{to,from}_user. It
1376 * can be fixed if we ever need to send real data around.
1378 for (o = 0, b = bulk; o < obdos; o++)
1379 for (p = 0; p < pages; p++, b += PAGE_SIZE)
1380 memcpy(b, &start, sizeof(start));
1382 rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
1383 for (i = 1, next_count = verbose; i <= count; i++) {
1385 b = bulk + sizeof(struct timeval);
1386 for (o = 0; o < obdos; o++)
1387 for (p = 0; p < pages; p++, b += PAGE_SIZE)
1388 memcpy(b, &count, sizeof(count));
1391 rc = ioctl(fd, rw, &data);
1393 fprintf(stderr, "error: %s: #%d - %s on %s\n",
1394 cmdname(argv[0]), i, strerror(rc = errno),
1395 write ? "write" : "read");
1397 } else if (be_verbose(verbose, &next_time, i,&next_count,count))
1398 printf("%s: %s number %d\n", cmdname(argv[0]),
1399 write ? "write" : "read", i);
1408 gettimeofday(&end, NULL);
1410 diff = difftime(&end, &start);
1413 printf("%s: %s %dx%dx%d pages in %.4gs (%.4g pg/s): %s",
1414 cmdname(argv[0]), write ? "wrote" : "read", obdos,
1415 pages, i, diff, (double)obdos * i * pages / diff,
1416 ctime(&end.tv_sec));
1421 static int jt_test_ldlm(int argc, char **argv)
1423 struct obd_ioctl_data data;
1427 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1431 rc = ioctl(fd, IOC_LDLM_TEST, &data);
1433 fprintf(stderr, "error: %s: test failed: %s\n",
1434 cmdname(argv[0]), strerror(rc = errno));
1438 static int jt_newconn(int argc, char **argv)
1440 struct obd_ioctl_data data;
1444 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
1448 rc = ioctl(fd, OBD_IOC_RECOVD_NEWCONN , &data);
1450 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
1451 strerror(rc = errno));
1456 static int jt_quit(int argc, char **argv)
1458 Parser_quit(argc, argv);
1463 command_t cmdlist[] = {
1465 {"--xml", jt__xml, 0, "--xml <xml file> <command [args ...]>"},
1466 {"--device", jt__device, 0, "--device <devno> <command [args ...]>"},
1467 {"--threads", jt__threads, 0,
1468 "--threads <threads> <devno> <command [args ...]>"},
1470 /* Device configuration commands */
1471 {"list", jt_list, 0, "list the devices (no args)"},
1472 {"newdev", jt_newdev, 0, "set device to a new unused obd (no args)"},
1473 {"device", jt_device, 0, "set current device (args device_no name)"},
1474 {"name2dev", jt_name2dev, 0, "set device by name (args name)"},
1475 {"attach", jt_attach, 0, "name the type of device (args: type data"},
1476 {"setup", jt_setup, 0, "setup device (args: <blkdev> [data]"},
1477 {"detach", jt_detach, 0, "detach the current device (arg: )"},
1478 {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
1480 /* Session commands */
1481 {"connect", jt_connect, 0, "connect - get a connection to device"},
1482 {"disconnect", jt_disconnect, 0,
1483 "disconnect - break connection to device"},
1485 /* Session operations */
1486 {"create", jt_create, 0, "create [count [mode [verbose]]]"},
1487 {"destroy", jt_destroy, 0, "destroy <id>"},
1488 {"getattr", jt_getattr, 0, "getattr <id>"},
1489 {"setattr", jt_setattr, 0, "setattr <id> <mode>"},
1490 {"newconn", jt_newconn, 0, "newconn [newuuid]"},
1491 {"test_getattr", jt_test_getattr, 0, "test_getattr <count> [verbose]"},
1492 {"test_brw", jt_test_brw, 0, "test_brw <count> [write [verbose]]"},
1493 {"test_ldlm", jt_test_ldlm, 0, "test lock manager (no args)"},
1495 /* User interface commands */
1496 {"help", Parser_help, 0, "help"},
1497 {"exit", jt_quit, 0, "quit"},
1498 {"quit", jt_quit, 0, "quit"},
1503 static void signal_server(int sig)
1505 if (sig == SIGINT) {
1506 do_disconnect("sigint", 1);
1509 fprintf(stderr, "%s: got signal %d\n", cmdname("sigint"), sig);
1513 int main(int argc, char **argv)
1515 struct sigaction sigact;
1517 sigact.sa_handler = signal_server;
1518 sigfillset(&sigact.sa_mask);
1519 sigact.sa_flags = SA_RESTART;
1520 sigaction(SIGINT, &sigact, NULL);
1524 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1526 Parser_init("obdctl > ", cmdlist);
1527 rc = Parser_commands();
1530 do_disconnect(argv[0], 1);