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 [data]\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));
474 static int jt_setup(int argc, char **argv)
476 struct obd_ioctl_data data;
482 fprintf(stderr, "usage: %s [device] [fstype]\n",
488 data.ioc_inllen1 = strlen(argv[1]) + 1;
489 data.ioc_inlbuf1 = argv[1];
490 data.ioc_dev = strtoul(argv[1], NULL, 0);
495 data.ioc_inllen2 = strlen(argv[2]) + 1;
496 data.ioc_inlbuf2 = argv[2];
499 printf("%s: len %d addr %p device %s type %s\n",
500 cmdname(argv[0]), data.ioc_len, buf,
501 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
503 if (obd_ioctl_pack(&data, &buf, max)) {
504 fprintf(stderr, "error: %s: invalid ioctl\n", cmdname(argv[0]));
507 printf("%s: len %d addr %p raw %p device %s type %s\n",
508 cmdname(argv[0]), data.ioc_len, buf, rawbuf,
509 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
511 rc = ioctl(fd, OBD_IOC_SETUP , buf);
513 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
514 strerror(rc = errno));
520 static int jt_create(int argc, char **argv)
522 struct obd_ioctl_data data;
523 struct timeval next_time;
524 int count = 1, next_count;
530 if (argc < 2 || argc > 4) {
531 fprintf(stderr, "usage: %s num [mode] [verbose]\n",
535 count = strtoul(argv[1], NULL, 0);
538 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
540 data.ioc_obdo1.o_mode = 0100644;
541 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
543 verbose = get_verbose(argv[3]);
545 printf("%s: %d obdos\n", cmdname(argv[0]), count);
546 gettimeofday(&next_time, NULL);
547 next_time.tv_sec -= verbose;
549 for (i = 1, next_count = verbose; i <= count ; i++) {
550 rc = ioctl(fd, OBD_IOC_CREATE , &data);
552 fprintf(stderr, "error: %s: #%d - %s\n",
553 cmdname(argv[0]), i, strerror(rc = errno));
556 if (be_verbose(verbose, &next_time, i, &next_count, count))
557 printf("%s: #%d is object id %Ld\n", cmdname(argv[0]),
558 i, data.ioc_obdo1.o_id);
563 static int jt_setattr(int argc, char **argv)
565 struct obd_ioctl_data data;
570 fprintf(stderr, "usage: %s id mode\n", cmdname(argv[0]));
574 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
575 data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], NULL, 0);
576 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
578 rc = ioctl(fd, OBD_IOC_SETATTR , &data);
580 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
581 strerror(rc = errno));
586 static int jt_destroy(int argc, char **argv)
588 struct obd_ioctl_data data;
593 fprintf(stderr, "usage: %s id\n", cmdname(argv[0]));
597 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
598 data.ioc_obdo1.o_mode = S_IFREG|0644;
600 rc = ioctl(fd, OBD_IOC_DESTROY , &data);
602 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
603 strerror(rc = errno));
608 static int jt_getattr(int argc, char **argv)
610 struct obd_ioctl_data data;
614 fprintf(stderr, "usage: %s id\n", cmdname(argv[0]));
619 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
620 /* to help obd filter */
621 data.ioc_obdo1.o_mode = 0100644;
622 data.ioc_obdo1.o_valid = 0xffffffff;
623 printf("%s: object id %Ld\n", cmdname(argv[0]), data.ioc_obdo1.o_id);
625 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
627 fprintf(stderr, "error: %s: %s\n", cmdname(argv[0]),
630 printf("%s: object id %Ld, mode %o\n", cmdname(argv[0]),
631 data.ioc_obdo1.o_id, data.ioc_obdo1.o_mode);
636 static int jt_test_getattr(int argc, char **argv)
638 struct obd_ioctl_data data;
639 struct timeval start, next_time;
640 int i, count, next_count;
644 if (argc != 2 && argc != 3) {
645 fprintf(stderr, "usage: %s count [verbose]\n",cmdname(argv[0]));
650 count = strtoul(argv[1], NULL, 0);
652 verbose = get_verbose(argv[2]);
654 data.ioc_obdo1.o_valid = 0xffffffff;
655 data.ioc_obdo1.o_id = 2;
656 gettimeofday(&start, NULL);
657 next_time.tv_sec = start.tv_sec - verbose;
658 next_time.tv_usec = start.tv_usec;
659 printf("%s: getting %d attrs (testing only): %s", cmdname(argv[0]),
660 count, ctime(&start.tv_sec));
662 for (i = 1, next_count = verbose; i <= count; i++) {
663 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
665 fprintf(stderr, "error: %s: #%d - %s\n",
666 cmdname(argv[0]), i, strerror(rc = errno));
668 } else if (be_verbose(verbose, &next_time, i,&next_count,count))
669 printf("%s: got attr #%d\n", cmdname(argv[0]), i);
676 gettimeofday(&end, NULL);
678 diff = difftime(&end, &start);
681 printf("%s: %d attrs in %.4gs (%.4g attr/s): %s",
682 cmdname(argv[0]), i, diff, (double)i / diff,
688 static int jt_test_brw(int argc, char **argv)
690 struct obd_ioctl_data data;
691 struct timeval start, next_time;
693 int pages = 1, obdos = 1, count, next_count;
694 int verbose = 1, write = 0, rw;
699 if (argc < 2 || argc > 6) {
701 "usage: %s count [write [verbose [pages [obdos]]]]\n",
706 count = strtoul(argv[1], NULL, 0);
709 if (argv[2][0] == 'w' || argv[2][0] == '1')
711 else if (argv[2][0] == 'r' || argv[2][0] == '0')
714 verbose = get_verbose(argv[3]);
718 pages = strtoul(argv[4], NULL, 0);
720 obdos = strtoul(argv[5], NULL, 0);
722 if (obdos != 1 && obdos != 2) {
723 fprintf(stderr, "error: %s: only 1 or 2 obdos supported\n",
728 len = pages * PAGE_SIZE;
730 bulk = calloc(obdos, len);
732 fprintf(stderr, "error: %s: no memory allocating %dx%d pages\n",
733 cmdname(argv[0]), obdos, pages);
737 data.ioc_conn2 = connid;
738 data.ioc_obdo1.o_id = 2;
739 data.ioc_count = len;
741 data.ioc_plen1 = len;
742 data.ioc_pbuf1 = bulk;
744 data.ioc_obdo2.o_id = 2;
745 data.ioc_plen2 = len;
746 data.ioc_pbuf2 = bulk + len;
749 gettimeofday(&start, NULL);
750 next_time.tv_sec = start.tv_sec - verbose;
751 next_time.tv_usec = start.tv_usec;
753 printf("%s: %s %d (%dx%d pages) (testing only): %s",
754 cmdname(argv[0]), write ? "writing" : "reading",
755 count, obdos, pages, ctime(&start.tv_sec));
758 * We will put in the start time (and loop count inside the loop)
759 * at the beginning of each page so that we will be able to validate
760 * (at some later time) whether the data actually made it or not.
762 for (o = 0, b = bulk; o < obdos; o++)
763 for (p = 0; p < pages; p++, b += PAGE_SIZE)
764 memcpy(b, &start, sizeof(start));
766 rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
767 for (i = 1, next_count = verbose; i <= count; i++) {
769 b = bulk + sizeof(struct timeval);
770 for (o = 0; o < obdos; o++)
771 for (p = 0; p < pages; p++, b += PAGE_SIZE)
772 memcpy(b, &count, sizeof(count));
775 rc = ioctl(fd, rw, &data);
777 fprintf(stderr, "error: %s: #%d - %s on %s\n",
778 cmdname(argv[0]), i, strerror(rc = errno),
779 write ? "write" : "read");
781 } else if (be_verbose(verbose, &next_time, i,&next_count,count))
782 printf("%s: %s number %d\n", cmdname(argv[0]),
783 write ? "write" : "read", i);
792 gettimeofday(&end, NULL);
794 diff = difftime(&end, &start);
797 printf("%s: %s %dx%dx%d pages in %.4gs (%.4g pg/s): %s",
798 cmdname(argv[0]), write ? "wrote" : "read", obdos,
799 pages, i, diff, (double)obdos * i * pages / diff,
805 static int jt_test_ldlm(int argc, char **argv)
807 struct obd_ioctl_data data;
812 fprintf(stderr, "usage: %s\n", cmdname(argv[0]));
816 rc = ioctl(fd, IOC_LDLM_TEST, &data);
818 fprintf(stderr, "error: %s: test failed: %s\n",
819 cmdname(argv[0]), strerror(rc = errno));
823 command_t cmdlist[] = {
825 {"--device", jt__device, 0, "--device <devno> <command [args ...]>"},
826 {"--threads", jt__threads, 0,
827 "--threads <threads> <devno> <command [args ...]>"},
829 /* Device configuration commands */
830 {"device", jt_device, 0, "set current device (args device no)"},
831 {"attach", jt_attach, 0, "name the type of device (args: type data"},
832 {"setup", jt_setup, 0, "setup device (args: <blkdev> [data]"},
833 {"detach", jt_detach, 0, "detach the current device (arg: )"},
834 {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
836 /* Session commands */
837 {"connect", jt_connect, 0, "connect - get a connection to device"},
838 {"disconnect", jt_disconnect, 0,
839 "disconnect - break connection to device"},
841 /* Session operations */
842 {"create", jt_create, 0, "create [count [mode [verbose]]]"},
843 {"destroy", jt_destroy, 0, "destroy <id>"},
844 {"getattr", jt_getattr, 0, "getattr <id>"},
845 {"setattr", jt_setattr, 0, "setattr <id> <mode>"},
846 {"test_getattr", jt_test_getattr, 0, "test_getattr <count> [verbose]"},
847 {"test_brw", jt_test_brw, 0, "test_brw <count> [write [verbose]]"},
848 {"test_ldlm", jt_test_ldlm, 0, "test lock manager (no args)"},
850 /* User interface commands */
851 {"help", Parser_help, 0, "help"},
852 {"exit", Parser_quit, 0, "quit"},
853 {"quit", Parser_quit, 0, "quit"},
858 static void signal_server(int sig)
861 do_disconnect("sigint", 1);
864 fprintf(stderr, "%s: got signal %d\n", cmdname("sigint"), sig);
868 int main(int argc, char **argv)
870 struct sigaction sigact;
873 sigact.sa_handler = signal_server;
874 sigfillset(&sigact.sa_mask);
875 sigact.sa_flags = SA_RESTART;
876 sigaction(SIGINT, &sigact, NULL);
880 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
882 Parser_init("obdctl > ", cmdlist);
886 do_disconnect(argv[0], 1);