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>
35 #include <linux/lustre_lib.h>
36 #include <linux/lustre_idl.h>
37 #include <linux/lustre_dlm.h>
43 #include <netinet/in.h>
48 #include <linux/list.h>
60 #define IOCINIT(data) \
62 memset(&data, 0, sizeof(data)); \
63 data.ioc_version = OBD_IOCTL_VERSION; \
64 data.ioc_conn1 = connid; \
65 data.ioc_len = sizeof(data); \
67 fprintf(stderr, "No device open, use device\n"); \
73 pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L",
92 0, 0, # struct list_head
98 char * obdo_print(struct obdo *obd)
102 sprintf(buf, "id: %Ld\ngrp: %Ld\natime: %Ld\nmtime: %Ld\nctime: %Ld\n"
103 "size: %Ld\nblocks: %Ld\nblksize: %d\nmode: %o\nuid: %d\n"
104 "gid: %d\nflags: %x\nobdflags: %x\nnlink: %d,\nvalid %x\n",
123 static int do_disconnect(const char *func)
125 struct obd_ioctl_data data;
133 rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
135 fprintf(stderr, "error: %s: %x %s\n", func,
136 OBD_IOC_DISCONNECT, strerror(errno));
138 printf("Disconnected connid %d\n", connid);
145 extern command_t cmdlist[];
147 static int jt_device(int argc, char **argv)
149 struct obd_ioctl_data data;
152 do_disconnect(argv[0]);
154 memset(&data, 0, sizeof(data));
156 fprintf(stderr, "usage: %s devno\n", argv[0]);
160 data.ioc_dev = atoi(argv[1]);
162 if (obd_ioctl_pack(&data, &buf, max)) {
163 fprintf(stderr, "error: %s: invalid ioctl\n", argv[0]);
168 fd = open("/dev/obd", O_RDWR);
170 fprintf(stderr, "error: %s: opening /dev/obd: %s\n", argv[0],
175 rc = ioctl(fd, OBD_IOC_DEVICE , buf);
177 fprintf(stderr, "error: %s: %x %s\n", argv[0],
178 OBD_IOC_DEVICE, strerror(rc = errno));
183 static int jt_connect(int argc, char **argv)
185 struct obd_ioctl_data data;
190 do_disconnect(argv[0]);
193 fprintf(stderr, "usage: %s\n", argv[0]);
197 rc = ioctl(fd, OBD_IOC_CONNECT , &data);
199 fprintf(stderr, "error: %s: %x %s\n", argv[0],
200 OBD_IOC_CONNECT, strerror(rc = errno));
202 connid = data.ioc_conn1;
207 static int jt_disconnect(int argc, char **argv)
209 struct obd_ioctl_data data;
215 fprintf(stderr, "usage: %s\n", argv[0]);
219 rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
221 fprintf(stderr, "error: %s: %x %s\n", argv[0],
222 OBD_IOC_DISCONNECT, strerror(rc = errno));
229 static int jt__device(int argc, char **argv)
235 fprintf(stderr, "usage: %s devno <command [args ...]>\n",
243 rc = jt_device(2, arg2);
248 rc = jt_connect(1, arg2);
252 rc = Parser_execarg(argc - 2, &argv[2], cmdlist);
255 arg2[0] = "disconnect";
257 rc = jt_disconnect(1, arg2);
263 static int jt_detach(int argc, char **argv)
265 struct obd_ioctl_data data;
271 fprintf(stderr, "usage: %s\n", argv[0]);
275 if (obd_ioctl_pack(&data, &buf, max)) {
276 fprintf(stderr, "error: %s: invalid ioctl\n", argv[0]);
280 rc = ioctl(fd, OBD_IOC_DETACH , buf);
282 fprintf(stderr, "error: %s: %s\n", argv[0], strerror(rc=errno));
287 static int jt_cleanup(int argc, char **argv)
289 struct obd_ioctl_data data;
295 fprintf(stderr, "usage: %s\n", argv[0]);
299 rc = ioctl(fd, OBD_IOC_CLEANUP , &data);
301 fprintf(stderr, "error: %s: %s\n", argv[0], strerror(rc=errno));
306 static int jt_attach(int argc, char **argv)
308 struct obd_ioctl_data data;
313 if (argc != 2 && argc != 3) {
314 fprintf(stderr, "usage: %s type [data]\n", argv[0]);
318 data.ioc_inllen1 = strlen(argv[1]) + 1;
319 data.ioc_inlbuf1 = argv[1];
321 data.ioc_inllen2 = strlen(argv[2]) + 1;
322 data.ioc_inlbuf2 = argv[2];
325 printf("attach len %d addr %p type %s data %s\n", data.ioc_len, buf,
326 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
328 if (obd_ioctl_pack(&data, &buf, max)) {
329 fprintf(stderr, "error: %s: invalid ioctl\n", argv[0]);
332 printf("attach len %d addr %p raw %p type %s data %s and %s\n",
333 data.ioc_len, buf, rawbuf,
334 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2), &buf[516]);
336 rc = ioctl(fd, OBD_IOC_ATTACH , buf);
338 fprintf(stderr, "error: %s: %x %s\n", argv[0],
339 OBD_IOC_ATTACH, strerror(rc = errno));
344 static int jt_setup(int argc, char **argv)
346 struct obd_ioctl_data data;
352 fprintf(stderr, "usage: %s [device] [fstype]\n", argv[0]);
357 data.ioc_inllen1 = strlen(argv[1]) + 1;
358 data.ioc_inlbuf1 = argv[1];
359 data.ioc_dev = strtoul(argv[1], NULL, 0);
364 data.ioc_inllen2 = strlen(argv[2]) + 1;
365 data.ioc_inlbuf2 = argv[2];
368 printf("setup len %d addr %p device %s type %s\n", data.ioc_len, buf,
369 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
371 if (obd_ioctl_pack(&data, &buf, max)) {
372 fprintf(stderr, "error: %s: invalid ioctl\n", argv[0]);
375 printf("setup len %d addr %p raw %p device %s type %s\n",
376 data.ioc_len, buf, rawbuf,
377 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
379 rc = ioctl(fd, OBD_IOC_SETUP , buf);
381 fprintf(stderr, "error: %s: %x %s\n", argv[0],
382 OBD_IOC_SETUP, strerror(rc = errno));
388 static int jt_create(int argc, char **argv)
390 struct obd_ioctl_data data;
397 if (argc < 2 || argc > 4) {
398 fprintf(stderr, "usage: %s num [mode] [silent]\n", argv[0]);
401 num = strtoul(argv[1], NULL, 0);
404 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
406 data.ioc_obdo1.o_mode = 0100644;
407 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
410 silent = strtoul(argv[3], NULL, 0);
412 printf("Creating %d obdos\n", num);
414 for (i = 0 ; i < num ; i++) {
415 rc = ioctl(fd, OBD_IOC_CREATE , &data);
417 fprintf(stderr, "error: %s: #%d - %x %s\n", argv[0],
418 i, OBD_IOC_CREATE, strerror(rc = errno));
422 printf("created obdo %Ld\n", data.ioc_obdo1.o_id);
427 static int jt_setattr(int argc, char **argv)
429 struct obd_ioctl_data data;
434 fprintf(stderr, "usage: %s id mode\n", argv[0]);
438 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
439 data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], NULL, 0);
440 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
442 rc = ioctl(fd, OBD_IOC_SETATTR , &data);
444 fprintf(stderr, "error: %s: %x %s\n", argv[0],
445 OBD_IOC_SETATTR, strerror(rc = errno));
450 static int jt_destroy(int argc, char **argv)
452 struct obd_ioctl_data data;
457 fprintf(stderr, "usage: %s id\n", argv[0]);
461 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
462 data.ioc_obdo1.o_mode = S_IFREG|0644;
464 rc = ioctl(fd, OBD_IOC_DESTROY , &data);
466 fprintf(stderr, "error: %s: %x %s\n", argv[0],
467 OBD_IOC_DESTROY, strerror(rc = errno));
472 static int jt_getattr(int argc, char **argv)
474 struct obd_ioctl_data data;
478 fprintf(stderr, "usage: %s id\n", argv[0]);
483 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
484 /* to help obd filter */
485 data.ioc_obdo1.o_mode = 0100644;
486 data.ioc_obdo1.o_valid = 0xffffffff;
487 printf("getting attr for %Ld\n", data.ioc_obdo1.o_id);
489 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
491 fprintf(stderr, "error: %s: %s\n", argv[0], strerror(rc=errno));
493 printf("attr obdo %Ld, mode %o\n", data.ioc_obdo1.o_id,
494 data.ioc_obdo1.o_mode);
499 static int jt_test_getattr(int argc, char **argv)
501 struct obd_ioctl_data data;
502 struct timeval start;
507 if (argc != 2 && argc != 3) {
508 fprintf(stderr, "usage: %s count [silent]\n", argv[0]);
513 count = strtoul(argv[1], NULL, 0);
516 if (argv[2][0] == 's' || argv[2][0] == 'q')
519 silent = strtoul(argv[2], NULL, 0);
521 data.ioc_obdo1.o_valid = 0xffffffff;
522 data.ioc_obdo1.o_id = 2;
523 gettimeofday(&start, NULL);
524 printf("getting %d attrs (testing only): %s", count,
525 ctime(&start.tv_sec));
527 for (i = 0 ; i < count; i++) {
528 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
530 fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
531 strerror(rc = errno));
533 } else if (!silent) {
534 printf("attr number %d\n", i);
541 gettimeofday(&end, NULL);
543 diff = ((double)(end.tv_sec - start.tv_sec) +
544 (double)(end.tv_usec - start.tv_usec) / 1000000);
546 printf("got %d attrs in %gs (%g attr/s): %s", i,
547 diff, (double)i / diff, ctime(&end.tv_sec));
552 static int jt_test_brw(int argc, char **argv)
554 struct obd_ioctl_data data;
555 struct timeval start;
557 int pages = 1, obdos = 1, count;
558 int silent = 0, write = 0, rw;
563 if (argc < 2 || argc > 6) {
564 fprintf(stderr, "usage: \n"
565 "%s count [write [silent [pages [obdos]]]]\n", argv[0]);
569 count = strtoul(argv[1], NULL, 0);
572 if (argv[2][0] == 'w')
574 else if (argv[2][0] == 'r')
577 write = strtoul(argv[2], NULL, 0);
580 if (argv[3][0] == 's' || argv[3][0] == 'q')
583 silent = strtoul(argv[3], NULL, 0);
586 pages = strtoul(argv[4], NULL, 0);
588 obdos = strtoul(argv[5], NULL, 0);
590 if (obdos != 1 && obdos != 2) {
591 fprintf(stderr, "error: %s: only 1 or 2 obdos supported\n",
596 len = pages * PAGE_SIZE;
598 bulk = calloc(obdos, len);
600 fprintf(stderr, "error: %s: no memory allocating %dx%d pages\n",
601 argv[0], obdos, pages);
605 data.ioc_conn2 = connid;
606 data.ioc_obdo1.o_id = 2;
607 data.ioc_count = len;
609 data.ioc_plen1 = len;
610 data.ioc_pbuf1 = bulk;
612 data.ioc_obdo2.o_id = 2;
613 data.ioc_plen2 = len;
614 data.ioc_pbuf2 = bulk + len;
617 gettimeofday(&start, NULL);
618 printf("%s %d (%dx%d pages) (testing only): %s",
619 write ? "writing" : "reading", count, obdos, pages,
620 ctime(&start.tv_sec));
623 * We will put in the start time (and loop count inside the loop)
624 * at the beginning of each page so that we will be able to validate
625 * (at some later time) whether the data actually made it or not.
627 for (o = 0, b = bulk; o < obdos; o++)
628 for (p = 0; p < pages; p++, b += PAGE_SIZE)
629 memcpy(b, &start, sizeof(start));
631 rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
632 for (i = 0 ; i < count; i++) {
634 b = bulk + sizeof(struct timeval);
635 for (o = 0; o < obdos; o++)
636 for (p = 0; p < pages; p++, b += PAGE_SIZE)
637 memcpy(b, &count, sizeof(count));
640 rc = ioctl(fd, rw, &data);
642 fprintf(stderr, "error: %s: #%d - %s on %s\n", argv[0],
643 i, strerror(rc = errno),
644 write ? "write" : "read");
647 printf("%s number %d\n", write ? "write" : "read", i);
656 gettimeofday(&end, NULL);
658 diff = ((double)(end.tv_sec - start.tv_sec) +
659 (double)(end.tv_usec - start.tv_usec) / 1000000);
661 printf("%s %dx%dx%d pages in %gs (%g pg/s): %s",
662 write ? "wrote" : "read", obdos, pages, i, diff,
663 (double)obdos * i * pages / diff, ctime(&end.tv_sec));
668 static int jt_test_ldlm(int argc, char **argv)
670 struct obd_ioctl_data data;
675 fprintf(stderr, "usage: %s\n", argv[0]);
679 rc = ioctl(fd, IOC_LDLM_TEST, &data);
681 fprintf(stderr, "error: %s: test failed: %s\n", argv[0],
682 strerror(rc = errno));
686 command_t cmdlist[] = {
687 {"--device", jt__device, 0, "device <devno> <command [args ...]>"},
688 {"device", jt_device, 0, "set current device (args device no)"},
689 {"attach", jt_attach, 0, "name the type of device (args: type data"},
690 {"setup", jt_setup, 0, "setup device (args: <blkdev> [data]"},
691 {"detach", jt_detach, 0, "detach the current device (arg: )"},
692 {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
693 {"create", jt_create, 0, "create [count [mode [silent]]]"},
694 {"destroy", jt_destroy, 0, "destroy <id>"},
695 {"getattr", jt_getattr, 0, "getattr <id>"},
696 {"setattr", jt_setattr, 0, "setattr <id> <mode>"},
697 {"connect", jt_connect, 0, "connect - get a connection to device"},
698 {"disconnect", jt_disconnect, 0,
699 "disconnect - break connection to device"},
700 {"test_getattr", jt_test_getattr, 0, "test_getattr <count> [silent]"},
701 {"test_brw", jt_test_brw, 0, "test_brw <count> [write [silent]]"},
702 {"test_ldlm", jt_test_ldlm, 0, "test lock manager (no args)"},
703 {"help", Parser_help, 0, "help"},
704 {"exit", Parser_quit, 0, "quit"},
705 {"quit", Parser_quit, 0, "quit"},
710 static void signal_server(int sig)
713 fprintf(stderr,"\nDisconnecting any existing connections...\n");
714 do_disconnect("sigint");
719 int main(int argc, char **argv)
721 struct sigaction sigact;
724 sigact.sa_handler = signal_server;
725 sigfillset(&sigact.sa_mask);
726 sigact.sa_flags = SA_RESTART;
727 sigaction(SIGINT, &sigact, NULL);
731 rc = Parser_execarg(argc - 1, &argv[1], cmdlist);
733 Parser_init("obdctl > ", cmdlist);
737 do_disconnect(argv[0]);