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.
26 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
36 #include <linux/lustre_lib.h>
37 #include <linux/lustre_idl.h>
38 #include <linux/lustre_dlm.h>
44 #include <netinet/in.h>
49 #include <linux/list.h>
63 #define IOCINIT(data) \
65 memset(&data, 0, sizeof(data)); \
66 data.ioc_version = OBD_IOCTL_VERSION; \
67 data.ioc_conn1 = connid; \
68 data.ioc_len = sizeof(data); \
70 fprintf(stderr, "No device open, use device\n"); \
76 pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L",
95 0, 0, # struct list_head
101 char * obdo_print(struct obdo *obd)
105 sprintf(buf, "id: %Ld\ngrp: %Ld\natime: %Ld\nmtime: %Ld\nctime: %Ld\n"
106 "size: %Ld\nblocks: %Ld\nblksize: %d\nmode: %o\nuid: %d\n"
107 "gid: %d\nflags: %x\nobdflags: %x\nnlink: %d,\nvalid %x\n",
126 static char *cmdname(char *func)
128 static char buf[512];
131 sprintf(buf, "%s-%d", func, thread);
138 int getfd(char *func)
141 fd = open("/dev/obd", O_RDWR);
143 fprintf(stderr, "error: %s: opening /dev/obd: %s\n",
144 cmdname(func), strerror(errno));
151 #define difftime(a, b) \
152 ((double)(a)->tv_sec - (b)->tv_sec + \
153 ((double)((a)->tv_usec - (b)->tv_usec) / 1000000))
156 #define difftime(a, b) (((a)->tv_sec - (b)->tv_sec) + \
157 (((a)->tv_usec - (b)->tv_usec) / 1000000))
159 static int be_verbose(int verbose, struct timeval *next_time,
160 int num, int *next_num, int num_total)
167 if (next_time != NULL)
168 gettimeofday(&now, NULL);
170 /* A positive verbosity means to print every X iterations */
172 (next_num == NULL || num >= *next_num || num >= num_total)) {
173 *next_num += verbose;
175 next_time->tv_sec = now.tv_sec - verbose;
176 next_time->tv_usec = now.tv_usec;
181 /* A negative verbosity means to print at most each X seconds */
182 if (verbose < 0 && next_time != NULL && difftime(&now, next_time) >= 0){
183 next_time->tv_sec = now.tv_sec - verbose;
184 next_time->tv_usec = now.tv_usec;
193 static int get_verbose(const char *arg)
197 if (!arg || arg[0] == 'v')
199 else if (arg[0] == 's' || arg[0] == 'q')
202 verbose = (int) strtoul(arg, NULL, 0);
205 printf("Print status every %d seconds\n", -verbose);
206 else if (verbose == 1)
207 printf("Print status every operation\n");
208 else if (verbose > 1)
209 printf("Print status every %d operations\n", verbose);
214 static int do_disconnect(char *func, int verbose)
216 struct obd_ioctl_data data;
223 rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
225 fprintf(stderr, "error: %s: %x %s\n", cmdname(func),
226 OBD_IOC_DISCONNECT, strerror(errno));
229 printf("%s: disconnected connid %d\n", cmdname(func),
237 extern command_t cmdlist[];
239 static int do_device(char *func, int dev)
241 struct obd_ioctl_data data;
243 memset(&data, 0, sizeof(data));
250 if (obd_ioctl_pack(&data, &buf, max)) {
251 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(func));
255 return ioctl(fd, OBD_IOC_DEVICE , buf);
258 static int jt_device(int argc, char **argv)
260 do_disconnect(argv[0], 1);
263 fprintf(stderr, "usage: %s devno\n", cmdname(argv[0]));
267 rc = do_device(argv[0], strtoul(argv[1], NULL, 0));
270 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
271 strerror(rc = errno));
276 static int jt_connect(int argc, char **argv)
278 struct obd_ioctl_data data;
282 do_disconnect(argv[0], 1);
285 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
289 rc = ioctl(fd, OBD_IOC_CONNECT , &data);
291 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
292 OBD_IOC_CONNECT, strerror(rc = errno));
294 connid = data.ioc_conn1;
299 static int jt_disconnect(int argc, char **argv)
302 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
306 return do_disconnect(argv[0], 0);
309 static int jt__device(int argc, char **argv)
315 fprintf(stderr, "usage: %s devno <command [args ...]>\n",
320 rc = do_device("device", strtoul(argv[1], NULL, 0));
325 rc = jt_connect(1, arg2);
329 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
331 ret = do_disconnect(argv[0], 0);
338 static int jt__threads(int argc, char **argv)
340 int threads, next_thread;
346 "usage: %s numthreads verbose devno <cmd [args ...]>\n",
351 threads = strtoul(argv[1], NULL, 0);
353 verbose = get_verbose(argv[2]);
355 printf("%s: starting %d threads on device %s running %s\n",
356 argv[0], threads, argv[3], argv[4]);
358 for (i = 1, next_thread = verbose; i <= threads; i++) {
361 fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
362 strerror(rc = errno));
364 } else if (rc == 0) {
366 argv[2] = "--device";
367 return jt__device(argc - 2, argv + 2);
368 } else if (be_verbose(verbose, NULL, i, &next_thread, threads))
369 printf("%s: thread #%d (PID %d) started\n",
374 if (!thread) { /* parent process */
376 printf("%s: started %d threads\n\n", argv[0], i - 1);
380 for (j = 1; j < i; j++) {
382 int ret = wait(&status);
385 fprintf(stderr, "error: %s: wait - %s\n",
386 argv[0], strerror(errno));
391 * This is a hack. We _should_ be able to use
392 * WIFEXITED(status) to see if there was an
393 * error, but it appears to be broken and it
394 * always returns 1 (OK). See wait(2).
396 int err = WEXITSTATUS(status);
399 "%s: PID %d had rc=%d\n",
410 static int jt_detach(int argc, char **argv)
412 struct obd_ioctl_data data;
417 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
421 if (obd_ioctl_pack(&data, &buf, max)) {
422 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
426 rc = ioctl(fd, OBD_IOC_DETACH , buf);
428 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
434 static int jt_cleanup(int argc, char **argv)
436 struct obd_ioctl_data data;
441 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
445 rc = ioctl(fd, OBD_IOC_CLEANUP , &data);
447 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
453 static int jt_newdev(int argc, char **argv)
455 struct obd_ioctl_data data;
463 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
467 rc = ioctl(fd, OBD_IOC_NEWDEV , &data);
469 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
472 printf("Current device set to %d\n", data.ioc_dev);
478 static int jt_attach(int argc, char **argv)
480 struct obd_ioctl_data data;
484 if (argc != 2 && argc != 3) {
485 fprintf(stderr, "usage: %s type [name [uuid]]\n",
490 data.ioc_inllen1 = strlen(argv[1]) + 1;
491 data.ioc_inlbuf1 = argv[1];
493 data.ioc_inllen2 = strlen(argv[2]) + 1;
494 data.ioc_inlbuf2 = argv[2];
497 if (obd_ioctl_pack(&data, &buf, max)) {
498 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
502 rc = ioctl(fd, OBD_IOC_ATTACH , buf);
504 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
505 OBD_IOC_ATTACH, strerror(rc = errno));
506 else if (argc == 3) {
508 if (strlen(argv[2]) > 128) {
509 printf("Name too long to set environment\n");
512 snprintf(name, 512, "LUSTRE_DEV_%s", argv[2]);
513 rc = setenv(name, argv[1], 1);
515 printf("error setting env variable %s\n", name);
522 #define N2D_OFF 0x100 /* So we can tell between error codes and devices */
524 static int do_name2dev(char *func, char *name)
526 struct obd_ioctl_data data;
533 data.ioc_inllen1 = strlen(name) + 1;
534 data.ioc_inlbuf1 = name;
536 if (obd_ioctl_pack(&data, &buf, max)) {
537 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(func));
540 rc = ioctl(fd, OBD_IOC_NAME2DEV , buf);
542 fprintf(stderr, "error: %s: %s - %s\n", cmdname(func),
543 name, strerror(rc = errno));
547 memcpy((char *)(&data), buf, sizeof(data));
549 return data.ioc_dev + N2D_OFF;
552 static int jt_name2dev(int argc, char **argv)
555 fprintf(stderr, "usage: %s name\n", cmdname(argv[0]));
559 rc = do_name2dev(argv[0], argv[1]);
561 int dev = rc - N2D_OFF;
562 rc = do_device(argv[0], dev);
569 static int jt_setup(int argc, char **argv)
571 struct obd_ioctl_data data;
576 fprintf(stderr, "usage: %s [device] [fstype]\n",
583 if (argv[1][0] == '$') {
584 rc = do_name2dev(argv[0], argv[1] + 1);
586 printf("%s is device %d\n", argv[1],
588 data.ioc_dev = rc - N2D_OFF;
591 data.ioc_dev = strtoul(argv[1], NULL, 0);
592 data.ioc_inllen1 = strlen(argv[1]) + 1;
593 data.ioc_inlbuf1 = argv[1];
596 data.ioc_inllen2 = strlen(argv[2]) + 1;
597 data.ioc_inlbuf2 = argv[2];
600 if (obd_ioctl_pack(&data, &buf, max)) {
601 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
604 rc = ioctl(fd, OBD_IOC_SETUP , buf);
606 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
607 strerror(rc = errno));
613 static int jt_create(int argc, char **argv)
615 struct obd_ioctl_data data;
616 struct timeval next_time;
617 int count = 1, next_count;
622 if (argc < 2 || argc > 4) {
623 fprintf(stderr, "usage: %s num [mode] [verbose]\n",
627 count = strtoul(argv[1], NULL, 0);
630 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
632 data.ioc_obdo1.o_mode = 0100644;
633 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
635 verbose = get_verbose(argv[3]);
637 printf("%s: %d obdos\n", cmdname(argv[0]), count);
638 gettimeofday(&next_time, NULL);
639 next_time.tv_sec -= verbose;
641 for (i = 1, next_count = verbose; i <= count ; i++) {
642 rc = ioctl(fd, OBD_IOC_CREATE , &data);
644 fprintf(stderr, "error: %s: #%d - %s\n",
645 cmdname(argv[0]), i, strerror(rc = errno));
648 if (be_verbose(verbose, &next_time, i, &next_count, count))
649 printf("%s: #%d is object id %Ld\n", cmdname(argv[0]),
650 i, data.ioc_obdo1.o_id);
655 static int jt_setattr(int argc, char **argv)
657 struct obd_ioctl_data data;
661 fprintf(stderr, "usage: %s id mode\n", cmdname(argv[0]));
665 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
666 data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], NULL, 0);
667 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
669 rc = ioctl(fd, OBD_IOC_SETATTR , &data);
671 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
672 strerror(rc = errno));
677 static int jt_destroy(int argc, char **argv)
679 struct obd_ioctl_data data;
683 fprintf(stderr, "usage: %s id\n", cmdname(argv[0]));
687 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
688 data.ioc_obdo1.o_mode = S_IFREG|0644;
690 rc = ioctl(fd, OBD_IOC_DESTROY , &data);
692 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
693 strerror(rc = errno));
698 static int jt_getattr(int argc, char **argv)
700 struct obd_ioctl_data data;
703 fprintf(stderr, "usage: %s id\n", cmdname(argv[0]));
708 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
709 /* to help obd filter */
710 data.ioc_obdo1.o_mode = 0100644;
711 data.ioc_obdo1.o_valid = 0xffffffff;
712 printf("%s: object id %Ld\n", cmdname(argv[0]), data.ioc_obdo1.o_id);
714 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
716 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
719 printf("%s: object id %Ld, mode %o\n", cmdname(argv[0]),
720 data.ioc_obdo1.o_id, data.ioc_obdo1.o_mode);
725 static int jt_test_getattr(int argc, char **argv)
727 struct obd_ioctl_data data;
728 struct timeval start, next_time;
729 int i, count, next_count;
732 if (argc != 2 && argc != 3) {
733 fprintf(stderr, "usage: %s count [verbose]\n",cmdname(argv[0]));
738 count = strtoul(argv[1], NULL, 0);
741 verbose = get_verbose(argv[2]);
745 data.ioc_obdo1.o_valid = 0xffffffff;
746 data.ioc_obdo1.o_id = 2;
747 gettimeofday(&start, NULL);
748 next_time.tv_sec = start.tv_sec - verbose;
749 next_time.tv_usec = start.tv_usec;
750 printf("%s: getting %d attrs (testing only): %s", cmdname(argv[0]),
751 count, ctime(&start.tv_sec));
753 for (i = 1, next_count = verbose; i <= count; i++) {
754 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
756 fprintf(stderr, "error: %s: #%d - %s\n",
757 cmdname(argv[0]), i, strerror(rc = errno));
760 if (be_verbose(verbose, &next_time, i,&next_count,count))
761 printf("%s: got attr #%d\n", cmdname(argv[0]), i);
769 gettimeofday(&end, NULL);
771 diff = difftime(&end, &start);
774 printf("%s: %d attrs in %.4gs (%.4g attr/s): %s",
775 cmdname(argv[0]), i, diff, (double)i / diff,
781 static int jt_test_brw(int argc, char **argv)
783 struct obd_ioctl_data data;
784 struct timeval start, next_time;
786 int pages = 1, obdos = 1, count, next_count;
787 int verbose = 1, write = 0, rw;
791 if (argc < 2 || argc > 6) {
793 "usage: %s count [write [verbose [pages [obdos]]]]\n",
798 count = strtoul(argv[1], NULL, 0);
801 if (argv[2][0] == 'w' || argv[2][0] == '1')
803 else if (argv[2][0] == 'r' || argv[2][0] == '0')
806 verbose = get_verbose(argv[3]);
810 pages = strtoul(argv[4], NULL, 0);
812 obdos = strtoul(argv[5], NULL, 0);
814 if (obdos != 1 && obdos != 2) {
815 fprintf(stderr, "error: %s: only 1 or 2 obdos supported\n",
820 len = pages * PAGE_SIZE;
822 bulk = calloc(obdos, len);
824 fprintf(stderr, "error: %s: no memory allocating %dx%d pages\n",
825 cmdname(argv[0]), obdos, pages);
829 data.ioc_conn2 = connid;
830 data.ioc_obdo1.o_id = 2;
831 data.ioc_count = len;
833 data.ioc_plen1 = len;
834 data.ioc_pbuf1 = bulk;
836 data.ioc_obdo2.o_id = 3;
837 data.ioc_plen2 = len;
838 data.ioc_pbuf2 = bulk + len;
841 gettimeofday(&start, NULL);
842 next_time.tv_sec = start.tv_sec - verbose;
843 next_time.tv_usec = start.tv_usec;
845 printf("%s: %s %d (%dx%d pages) (testing only): %s",
846 cmdname(argv[0]), write ? "writing" : "reading",
847 count, obdos, pages, ctime(&start.tv_sec));
850 * We will put in the start time (and loop count inside the loop)
851 * at the beginning of each page so that we will be able to validate
852 * (at some later time) whether the data actually made it or not.
854 * XXX we do not currently use any of this memory in OBD_IOC_BRW_*
855 * just to avoid the overhead of the copy_{to,from}_user. It
856 * can be fixed if we ever need to send real data around.
858 for (o = 0, b = bulk; o < obdos; o++)
859 for (p = 0; p < pages; p++, b += PAGE_SIZE)
860 memcpy(b, &start, sizeof(start));
862 rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
863 for (i = 1, next_count = verbose; i <= count; i++) {
865 b = bulk + sizeof(struct timeval);
866 for (o = 0; o < obdos; o++)
867 for (p = 0; p < pages; p++, b += PAGE_SIZE)
868 memcpy(b, &count, sizeof(count));
871 rc = ioctl(fd, rw, &data);
873 fprintf(stderr, "error: %s: #%d - %s on %s\n",
874 cmdname(argv[0]), i, strerror(rc = errno),
875 write ? "write" : "read");
877 } else if (be_verbose(verbose, &next_time, i,&next_count,count))
878 printf("%s: %s number %d\n", cmdname(argv[0]),
879 write ? "write" : "read", i);
888 gettimeofday(&end, NULL);
890 diff = difftime(&end, &start);
893 printf("%s: %s %dx%dx%d pages in %.4gs (%.4g pg/s): %s",
894 cmdname(argv[0]), write ? "wrote" : "read", obdos,
895 pages, i, diff, (double)obdos * i * pages / diff,
901 static int jt_test_ldlm(int argc, char **argv)
903 struct obd_ioctl_data data;
907 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
911 rc = ioctl(fd, IOC_LDLM_TEST, &data);
913 fprintf(stderr, "error: %s: test failed: %s\n",
914 cmdname(argv[0]), strerror(rc = errno));
918 static int jt_newconn(int argc, char **argv)
920 struct obd_ioctl_data data;
924 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
928 rc = ioctl(fd, OBD_IOC_RECOVD_NEWCONN , &data);
930 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
931 strerror(rc = errno));
936 static int jt_quit(int argc, char **argv)
938 Parser_quit(argc, argv);
943 command_t cmdlist[] = {
945 {"--device", jt__device, 0, "--device <devno> <command [args ...]>"},
946 {"--threads", jt__threads, 0,
947 "--threads <threads> <devno> <command [args ...]>"},
949 /* Device configuration commands */
950 {"newdev", jt_newdev, 0, "set device to a new unused obd (no args)"},
951 {"device", jt_device, 0, "set current device (args device_no name)"},
952 {"name2dev", jt_name2dev, 0, "set device by name (args name)"},
953 {"attach", jt_attach, 0, "name the type of device (args: type data"},
954 {"setup", jt_setup, 0, "setup device (args: <blkdev> [data]"},
955 {"detach", jt_detach, 0, "detach the current device (arg: )"},
956 {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
958 /* Session commands */
959 {"connect", jt_connect, 0, "connect - get a connection to device"},
960 {"disconnect", jt_disconnect, 0,
961 "disconnect - break connection to device"},
963 /* Session operations */
964 {"create", jt_create, 0, "create [count [mode [verbose]]]"},
965 {"destroy", jt_destroy, 0, "destroy <id>"},
966 {"getattr", jt_getattr, 0, "getattr <id>"},
967 {"setattr", jt_setattr, 0, "setattr <id> <mode>"},
968 {"newconn", jt_newconn, 0, "newconn [newuuid]"},
969 {"test_getattr", jt_test_getattr, 0, "test_getattr <count> [verbose]"},
970 {"test_brw", jt_test_brw, 0, "test_brw <count> [write [verbose]]"},
971 {"test_ldlm", jt_test_ldlm, 0, "test lock manager (no args)"},
973 /* User interface commands */
974 {"help", Parser_help, 0, "help"},
975 {"exit", jt_quit, 0, "quit"},
976 {"quit", jt_quit, 0, "quit"},
981 static void signal_server(int sig)
984 do_disconnect("sigint", 1);
987 fprintf(stderr, "%s: got signal %d\n", cmdname("sigint"), sig);
991 int main(int argc, char **argv)
993 struct sigaction sigact;
995 sigact.sa_handler = signal_server;
996 sigfillset(&sigact.sa_mask);
997 sigact.sa_flags = SA_RESTART;
998 sigaction(SIGINT, &sigact, NULL);
1002 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1004 Parser_init("obdctl > ", cmdlist);
1005 rc = Parser_commands();
1008 do_disconnect(argv[0], 1);