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>
62 #define IOCINIT(data) \
64 memset(&data, 0, sizeof(data)); \
65 data.ioc_version = OBD_IOCTL_VERSION; \
66 data.ioc_conn1 = connid; \
67 data.ioc_len = sizeof(data); \
69 fprintf(stderr, "No device open, use device\n"); \
75 pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L",
94 0, 0, # struct list_head
100 char * obdo_print(struct obdo *obd)
104 sprintf(buf, "id: %Ld\ngrp: %Ld\natime: %Ld\nmtime: %Ld\nctime: %Ld\n"
105 "size: %Ld\nblocks: %Ld\nblksize: %d\nmode: %o\nuid: %d\n"
106 "gid: %d\nflags: %x\nobdflags: %x\nnlink: %d,\nvalid %x\n",
125 static char *cmdname(char *func)
127 static char buf[512];
130 sprintf(buf, "%s-%d", func, thread);
137 #define difftime(a, b) \
138 ((double)(a)->tv_sec - (b)->tv_sec + \
139 ((double)((a)->tv_usec - (b)->tv_usec) / 1000000))
141 static int be_verbose(int verbose, struct timeval *next_time,
142 int num, int *next_num, int num_total)
149 if (next_time != NULL)
150 gettimeofday(&now, NULL);
152 /* A positive verbosity means to print every X iterations */
154 (next_num == NULL || num >= *next_num || num >= num_total)) {
155 *next_num += verbose;
157 next_time->tv_sec = now.tv_sec - verbose;
158 next_time->tv_usec = now.tv_usec;
163 /* A negative verbosity means to print at most each X seconds */
164 if (verbose < 0 && next_time != NULL && difftime(&now, next_time) >= 0){
165 next_time->tv_sec = now.tv_sec - verbose;
166 next_time->tv_usec = now.tv_usec;
175 static int get_verbose(const char *arg)
179 if (!arg || arg[0] == 'v')
181 else if (arg[0] == 's' || arg[0] == 'q')
184 verbose = strtoul(arg, NULL, 0);
189 static int do_disconnect(char *func, int verbose)
191 struct obd_ioctl_data data;
199 rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
201 fprintf(stderr, "error: %s: %x %s\n", cmdname(func),
202 OBD_IOC_DISCONNECT, strerror(errno));
205 printf("%s: disconnected connid %d\n", cmdname(func),
213 extern command_t cmdlist[];
215 static int jt_device(int argc, char **argv)
217 struct obd_ioctl_data data;
220 do_disconnect(argv[0], 1);
222 memset(&data, 0, sizeof(data));
224 fprintf(stderr, "usage: %s devno\n", cmdname(argv[0]));
228 data.ioc_dev = strtoul(argv[1], NULL, 0);
230 if (obd_ioctl_pack(&data, &buf, max)) {
231 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
236 fd = open("/dev/obd", O_RDWR);
238 fprintf(stderr, "error: %s: opening /dev/obd: %s\n",
239 cmdname(argv[0]), strerror(errno));
243 rc = ioctl(fd, OBD_IOC_DEVICE , buf);
245 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
246 OBD_IOC_DEVICE, strerror(rc = errno));
251 static int jt_connect(int argc, char **argv)
253 struct obd_ioctl_data data;
258 do_disconnect(argv[0], 1);
261 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
265 rc = ioctl(fd, OBD_IOC_CONNECT , &data);
267 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
268 OBD_IOC_CONNECT, strerror(rc = errno));
270 connid = data.ioc_conn1;
275 static int jt_disconnect(int argc, char **argv)
278 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
282 return do_disconnect(argv[0], 0);
285 static int jt__device(int argc, char **argv)
291 fprintf(stderr, "usage: %s devno <command [args ...]>\n",
299 rc = jt_device(2, arg2);
304 rc = jt_connect(1, arg2);
308 rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
310 ret = do_disconnect(argv[0], 0);
317 static int jt__threads(int argc, char **argv)
319 int threads, next_thread;
326 "usage: %s numthreads verbose devno <cmd [args ...]>\n",
331 threads = strtoul(argv[1], NULL, 0);
333 verbose = get_verbose(argv[2]);
335 printf("%s: starting %d threads on device %s running %s\n",
336 argv[0], threads, argv[3], argv[4]);
338 for (i = 1, next_thread = verbose; i <= threads; i++) {
341 fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
342 strerror(rc = errno));
344 } else if (rc == 0) {
346 argv[2] = "--device";
347 return jt__device(argc - 2, argv + 2);
348 } else if (be_verbose(verbose, NULL, i, &next_thread, threads))
349 printf("%s: thread #%d (PID %d) started\n",
354 if (!thread) { /* parent process */
356 printf("%s: started %d threads\n\n", argv[0], i - 1);
360 for (j = 1; j < i; j++) {
362 int ret = wait(&status);
365 fprintf(stderr, "error: %s: wait - %s\n",
366 argv[0], strerror(errno));
371 * This is a hack. We _should_ be able to use
372 * WIFEXITED(status) to see if there was an
373 * error, but it appears to be broken and it
374 * always returns 1 (OK). See wait(2).
376 int err = WEXITSTATUS(status);
379 "%s: PID %d had rc=%d\n",
390 static int jt_detach(int argc, char **argv)
392 struct obd_ioctl_data data;
398 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
402 if (obd_ioctl_pack(&data, &buf, max)) {
403 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
407 rc = ioctl(fd, OBD_IOC_DETACH , buf);
409 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
415 static int jt_cleanup(int argc, char **argv)
417 struct obd_ioctl_data data;
423 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
427 rc = ioctl(fd, OBD_IOC_CLEANUP , &data);
429 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
435 static int jt_attach(int argc, char **argv)
437 struct obd_ioctl_data data;
442 if (argc != 2 && argc != 3) {
443 fprintf(stderr, "usage: %s type [name [uuid]]\n", cmdname(argv[0]));
447 data.ioc_inllen1 = strlen(argv[1]) + 1;
448 data.ioc_inlbuf1 = argv[1];
450 data.ioc_inllen2 = strlen(argv[2]) + 1;
451 data.ioc_inlbuf2 = argv[2];
454 printf("%s: len %d addr %p type %s data %s\n",
455 cmdname(argv[0]), data.ioc_len, buf,
456 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
458 if (obd_ioctl_pack(&data, &buf, max)) {
459 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
462 printf("%s: len %d addr %p raw %p type %s data %s and %s\n",
463 cmdname(argv[0]), data.ioc_len, buf, rawbuf,
464 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2), &buf[516]);
466 rc = ioctl(fd, OBD_IOC_ATTACH , buf);
468 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
469 OBD_IOC_ATTACH, strerror(rc = errno));
470 else if (argc == 3) {
472 if (strlen(argv[2]) > 128) {
473 printf("Name too long to set environment\n");
476 snprintf(name, 512, "LUSTRE-dev-%s", argv[2]);
477 rc = setenv(name, argv[1], 1);
479 printf("error setting env variable %s\n", name);
486 static int jt_name2dev(int argc, char **argv)
488 struct obd_ioctl_data data;
492 fd = open("/dev/obd", O_RDWR);
494 fprintf(stderr, "error: %s: opening /dev/obd: %s\n",
495 cmdname(argv[0]), strerror(errno));
502 fprintf(stderr, "usage: %s name\n", cmdname(argv[0]));
506 data.ioc_inllen1 = strlen(argv[1]) + 1;
507 data.ioc_inlbuf1 = argv[1];
510 if (obd_ioctl_pack(&data, &buf, max)) {
511 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
514 rc = ioctl(fd, OBD_IOC_NAME2DEV , buf);
516 fprintf(stderr, "error: %s: %x %s\n", cmdname(argv[0]),
517 OBD_IOC_NAME2DEV, strerror(rc = errno));
518 memcpy((char *)(&data), buf, sizeof(data));
519 printf("%d\n", data.ioc_dev);
523 static int jt_setup(int argc, char **argv)
525 struct obd_ioctl_data data;
531 fprintf(stderr, "usage: %s [device] [fstype]\n",
537 data.ioc_inllen1 = strlen(argv[1]) + 1;
538 data.ioc_inlbuf1 = argv[1];
539 data.ioc_dev = strtoul(argv[1], NULL, 0);
544 data.ioc_inllen2 = strlen(argv[2]) + 1;
545 data.ioc_inlbuf2 = argv[2];
548 printf("%s: len %d addr %p device %s type %s\n",
549 cmdname(argv[0]), data.ioc_len, buf,
550 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
552 if (obd_ioctl_pack(&data, &buf, max)) {
553 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
556 printf("%s: len %d addr %p raw %p device %s type %s\n",
557 cmdname(argv[0]), data.ioc_len, buf, rawbuf,
558 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
560 rc = ioctl(fd, OBD_IOC_SETUP , buf);
562 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
563 strerror(rc = errno));
569 static int jt_create(int argc, char **argv)
571 struct obd_ioctl_data data;
572 struct timeval next_time;
573 int count = 1, next_count;
579 if (argc < 2 || argc > 4) {
580 fprintf(stderr, "usage: %s num [mode] [verbose]\n",
584 count = strtoul(argv[1], NULL, 0);
587 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
589 data.ioc_obdo1.o_mode = 0100644;
590 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
592 verbose = get_verbose(argv[3]);
594 printf("%s: %d obdos\n", cmdname(argv[0]), count);
595 gettimeofday(&next_time, NULL);
596 next_time.tv_sec -= verbose;
598 for (i = 1, next_count = verbose; i <= count ; i++) {
599 rc = ioctl(fd, OBD_IOC_CREATE , &data);
601 fprintf(stderr, "error: %s: #%d - %s\n",
602 cmdname(argv[0]), i, strerror(rc = errno));
605 if (be_verbose(verbose, &next_time, i, &next_count, count))
606 printf("%s: #%d is object id %Ld\n", cmdname(argv[0]),
607 i, data.ioc_obdo1.o_id);
612 static int jt_setattr(int argc, char **argv)
614 struct obd_ioctl_data data;
619 fprintf(stderr, "usage: %s id mode\n", cmdname(argv[0]));
623 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
624 data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], NULL, 0);
625 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
627 rc = ioctl(fd, OBD_IOC_SETATTR , &data);
629 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
630 strerror(rc = errno));
635 static int jt_destroy(int argc, char **argv)
637 struct obd_ioctl_data data;
642 fprintf(stderr, "usage: %s id\n", cmdname(argv[0]));
646 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
647 data.ioc_obdo1.o_mode = S_IFREG|0644;
649 rc = ioctl(fd, OBD_IOC_DESTROY , &data);
651 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
652 strerror(rc = errno));
657 static int jt_getattr(int argc, char **argv)
659 struct obd_ioctl_data data;
663 fprintf(stderr, "usage: %s id\n", cmdname(argv[0]));
668 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
669 /* to help obd filter */
670 data.ioc_obdo1.o_mode = 0100644;
671 data.ioc_obdo1.o_valid = 0xffffffff;
672 printf("%s: object id %Ld\n", cmdname(argv[0]), data.ioc_obdo1.o_id);
674 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
676 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
679 printf("%s: object id %Ld, mode %o\n", cmdname(argv[0]),
680 data.ioc_obdo1.o_id, data.ioc_obdo1.o_mode);
685 static int jt_test_getattr(int argc, char **argv)
687 struct obd_ioctl_data data;
688 struct timeval start, next_time;
689 int i, count, next_count;
693 if (argc != 2 && argc != 3) {
694 fprintf(stderr, "usage: %s count [verbose]\n",cmdname(argv[0]));
699 count = strtoul(argv[1], NULL, 0);
701 verbose = get_verbose(argv[2]);
703 data.ioc_obdo1.o_valid = 0xffffffff;
704 data.ioc_obdo1.o_id = 2;
705 gettimeofday(&start, NULL);
706 next_time.tv_sec = start.tv_sec - verbose;
707 next_time.tv_usec = start.tv_usec;
708 printf("%s: getting %d attrs (testing only): %s", cmdname(argv[0]),
709 count, ctime(&start.tv_sec));
711 for (i = 1, next_count = verbose; i <= count; i++) {
712 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
714 fprintf(stderr, "error: %s: #%d - %s\n",
715 cmdname(argv[0]), i, strerror(rc = errno));
717 } else if (be_verbose(verbose, &next_time, i,&next_count,count))
718 printf("%s: got attr #%d\n", cmdname(argv[0]), i);
725 gettimeofday(&end, NULL);
727 diff = difftime(&end, &start);
730 printf("%s: %d attrs in %.4gs (%.4g attr/s): %s",
731 cmdname(argv[0]), i, diff, (double)i / diff,
737 static int jt_test_brw(int argc, char **argv)
739 struct obd_ioctl_data data;
740 struct timeval start, next_time;
742 int pages = 1, obdos = 1, count, next_count;
743 int verbose = 1, write = 0, rw;
748 if (argc < 2 || argc > 6) {
750 "usage: %s count [write [verbose [pages [obdos]]]]\n",
755 count = strtoul(argv[1], NULL, 0);
758 if (argv[2][0] == 'w' || argv[2][0] == '1')
760 else if (argv[2][0] == 'r' || argv[2][0] == '0')
763 verbose = get_verbose(argv[3]);
767 pages = strtoul(argv[4], NULL, 0);
769 obdos = strtoul(argv[5], NULL, 0);
771 if (obdos != 1 && obdos != 2) {
772 fprintf(stderr, "error: %s: only 1 or 2 obdos supported\n",
777 len = pages * PAGE_SIZE;
779 bulk = calloc(obdos, len);
781 fprintf(stderr, "error: %s: no memory allocating %dx%d pages\n",
782 cmdname(argv[0]), obdos, pages);
786 data.ioc_conn2 = connid;
787 data.ioc_obdo1.o_id = 2;
788 data.ioc_count = len;
790 data.ioc_plen1 = len;
791 data.ioc_pbuf1 = bulk;
793 data.ioc_obdo2.o_id = 2;
794 data.ioc_plen2 = len;
795 data.ioc_pbuf2 = bulk + len;
798 gettimeofday(&start, NULL);
799 next_time.tv_sec = start.tv_sec - verbose;
800 next_time.tv_usec = start.tv_usec;
802 printf("%s: %s %d (%dx%d pages) (testing only): %s",
803 cmdname(argv[0]), write ? "writing" : "reading",
804 count, obdos, pages, ctime(&start.tv_sec));
807 * We will put in the start time (and loop count inside the loop)
808 * at the beginning of each page so that we will be able to validate
809 * (at some later time) whether the data actually made it or not.
811 for (o = 0, b = bulk; o < obdos; o++)
812 for (p = 0; p < pages; p++, b += PAGE_SIZE)
813 memcpy(b, &start, sizeof(start));
815 rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
816 for (i = 1, next_count = verbose; i <= count; i++) {
818 b = bulk + sizeof(struct timeval);
819 for (o = 0; o < obdos; o++)
820 for (p = 0; p < pages; p++, b += PAGE_SIZE)
821 memcpy(b, &count, sizeof(count));
824 rc = ioctl(fd, rw, &data);
826 fprintf(stderr, "error: %s: #%d - %s on %s\n",
827 cmdname(argv[0]), i, strerror(rc = errno),
828 write ? "write" : "read");
830 } else if (be_verbose(verbose, &next_time, i,&next_count,count))
831 printf("%s: %s number %d\n", cmdname(argv[0]),
832 write ? "write" : "read", i);
841 gettimeofday(&end, NULL);
843 diff = difftime(&end, &start);
846 printf("%s: %s %dx%dx%d pages in %.4gs (%.4g pg/s): %s",
847 cmdname(argv[0]), write ? "wrote" : "read", obdos,
848 pages, i, diff, (double)obdos * i * pages / diff,
854 static int jt_test_ldlm(int argc, char **argv)
856 struct obd_ioctl_data data;
861 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
865 rc = ioctl(fd, IOC_LDLM_TEST, &data);
867 fprintf(stderr, "error: %s: test failed: %s\n",
868 cmdname(argv[0]), strerror(rc = errno));
872 command_t cmdlist[] = {
874 {"--device", jt__device, 0, "--device <devno> <command [args ...]>"},
875 {"--threads", jt__threads, 0,
876 "--threads <threads> <devno> <command [args ...]>"},
878 /* Device configuration commands */
879 {"device", jt_device, 0, "set current device (args device_no name)"},
880 {"name2dev", jt_name2dev, 0, "set device by name (args name)"},
881 {"attach", jt_attach, 0, "name the type of device (args: type data"},
882 {"setup", jt_setup, 0, "setup device (args: <blkdev> [data]"},
883 {"detach", jt_detach, 0, "detach the current device (arg: )"},
884 {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
886 /* Session commands */
887 {"connect", jt_connect, 0, "connect - get a connection to device"},
888 {"disconnect", jt_disconnect, 0,
889 "disconnect - break connection to device"},
891 /* Session operations */
892 {"create", jt_create, 0, "create [count [mode [verbose]]]"},
893 {"destroy", jt_destroy, 0, "destroy <id>"},
894 {"getattr", jt_getattr, 0, "getattr <id>"},
895 {"setattr", jt_setattr, 0, "setattr <id> <mode>"},
896 {"test_getattr", jt_test_getattr, 0, "test_getattr <count> [verbose]"},
897 {"test_brw", jt_test_brw, 0, "test_brw <count> [write [verbose]]"},
898 {"test_ldlm", jt_test_ldlm, 0, "test lock manager (no args)"},
900 /* User interface commands */
901 {"help", Parser_help, 0, "help"},
902 {"exit", Parser_quit, 0, "quit"},
903 {"quit", Parser_quit, 0, "quit"},
908 static void signal_server(int sig)
911 do_disconnect("sigint", 1);
914 fprintf(stderr, "%s: got signal %d\n", cmdname("sigint"), sig);
918 int main(int argc, char **argv)
920 struct sigaction sigact;
923 sigact.sa_handler = signal_server;
924 sigfillset(&sigact.sa_mask);
925 sigact.sa_flags = SA_RESTART;
926 sigaction(SIGINT, &sigact, NULL);
930 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
932 Parser_init("obdctl > ", cmdlist);
936 do_disconnect(argv[0], 1);