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_detach(int argc, char **argv)
231 struct obd_ioctl_data data;
237 fprintf(stderr, "usage: %s\n", argv[0]);
241 if (obd_ioctl_pack(&data, &buf, max)) {
242 fprintf(stderr, "error: %s: invalid ioctl\n", argv[0]);
246 rc = ioctl(fd, OBD_IOC_DETACH , buf);
248 fprintf(stderr, "error: %s: %s\n", argv[0], strerror(rc=errno));
253 static int jt_cleanup(int argc, char **argv)
255 struct obd_ioctl_data data;
261 fprintf(stderr, "usage: %s\n", argv[0]);
265 rc = ioctl(fd, OBD_IOC_CLEANUP , &data);
267 fprintf(stderr, "error: %s: %s\n", argv[0], strerror(rc=errno));
272 static int jt_attach(int argc, char **argv)
274 struct obd_ioctl_data data;
279 if (argc != 2 && argc != 3) {
280 fprintf(stderr, "usage: %s type [data]\n", argv[0]);
284 data.ioc_inllen1 = strlen(argv[1]) + 1;
285 data.ioc_inlbuf1 = argv[1];
287 data.ioc_inllen2 = strlen(argv[2]) + 1;
288 data.ioc_inlbuf2 = argv[2];
291 printf("attach len %d addr %p type %s data %s\n", data.ioc_len, buf,
292 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
294 if (obd_ioctl_pack(&data, &buf, max)) {
295 fprintf(stderr, "error: %s: invalid ioctl\n", argv[0]);
298 printf("attach len %d addr %p raw %p type %s data %s and %s\n",
299 data.ioc_len, buf, rawbuf,
300 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2), &buf[516]);
302 rc = ioctl(fd, OBD_IOC_ATTACH , buf);
304 fprintf(stderr, "error: %s: %x %s\n", argv[0],
305 OBD_IOC_ATTACH, strerror(rc = errno));
310 static int jt_setup(int argc, char **argv)
312 struct obd_ioctl_data data;
318 fprintf(stderr, "usage: %s [device] [fstype]\n", argv[0]);
323 data.ioc_inllen1 = strlen(argv[1]) + 1;
324 data.ioc_inlbuf1 = argv[1];
325 data.ioc_dev = strtoul(argv[1], NULL, 0);
330 data.ioc_inllen2 = strlen(argv[2]) + 1;
331 data.ioc_inlbuf2 = argv[2];
334 printf("setup len %d addr %p device %s type %s\n", data.ioc_len, buf,
335 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
337 if (obd_ioctl_pack(&data, &buf, max)) {
338 fprintf(stderr, "error: %s: invalid ioctl\n", argv[0]);
341 printf("setup len %d addr %p raw %p device %s type %s\n",
342 data.ioc_len, buf, rawbuf,
343 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
345 rc = ioctl(fd, OBD_IOC_SETUP , buf);
347 fprintf(stderr, "error: %s: %x %s\n", argv[0],
348 OBD_IOC_SETUP, strerror(rc = errno));
354 static int jt_create(int argc, char **argv)
356 struct obd_ioctl_data data;
363 if (argc < 2 || argc > 4) {
364 fprintf(stderr, "usage: %s num [mode] [silent]\n", argv[0]);
367 num = strtoul(argv[1], NULL, 0);
370 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
372 data.ioc_obdo1.o_mode = 0100644;
373 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
376 silent = strtoul(argv[3], NULL, 0);
378 printf("Creating %d obdos\n", num);
380 for (i = 0 ; i < num ; i++) {
381 rc = ioctl(fd, OBD_IOC_CREATE , &data);
383 fprintf(stderr, "error: %s: #%d - %x %s\n", argv[0],
384 i, OBD_IOC_CREATE, strerror(rc = errno));
388 printf("created obdo %Ld\n", data.ioc_obdo1.o_id);
393 static int jt_setattr(int argc, char **argv)
395 struct obd_ioctl_data data;
400 fprintf(stderr, "usage: %s id mode\n", argv[0]);
404 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
405 data.ioc_obdo1.o_mode = S_IFREG | strtoul(argv[2], NULL, 0);
406 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
408 rc = ioctl(fd, OBD_IOC_SETATTR , &data);
410 fprintf(stderr, "error: %s: %x %s\n", argv[0],
411 OBD_IOC_SETATTR, strerror(rc = errno));
416 static int jt_destroy(int argc, char **argv)
418 struct obd_ioctl_data data;
423 fprintf(stderr, "usage: %s id\n", argv[0]);
427 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
428 data.ioc_obdo1.o_mode = S_IFREG|0644;
430 rc = ioctl(fd, OBD_IOC_DESTROY , &data);
432 fprintf(stderr, "error: %s: %x %s\n", argv[0],
433 OBD_IOC_DESTROY, strerror(rc = errno));
438 static int jt_getattr(int argc, char **argv)
440 struct obd_ioctl_data data;
444 fprintf(stderr, "usage: %s id\n", argv[0]);
449 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
450 /* to help obd filter */
451 data.ioc_obdo1.o_mode = 0100644;
452 data.ioc_obdo1.o_valid = 0xffffffff;
453 printf("getting attr for %Ld\n", data.ioc_obdo1.o_id);
455 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
457 fprintf(stderr, "error: %s: %s\n", argv[0], strerror(rc=errno));
459 printf("attr obdo %Ld, mode %o\n", data.ioc_obdo1.o_id,
460 data.ioc_obdo1.o_mode);
465 static int jt_test_getattr(int argc, char **argv)
467 struct obd_ioctl_data data;
468 struct timeval start;
473 if (argc != 2 && argc != 3) {
474 fprintf(stderr, "usage: %s count [silent]\n", argv[0]);
479 count = strtoul(argv[1], NULL, 0);
482 if (argv[2][0] == 's' || argv[2][0] == 'q')
485 silent = strtoul(argv[2], NULL, 0);
487 data.ioc_obdo1.o_valid = 0xffffffff;
488 data.ioc_obdo1.o_id = 2;
489 gettimeofday(&start, NULL);
490 printf("getting %d attrs (testing only): %s", count,
491 ctime(&start.tv_sec));
493 for (i = 0 ; i < count; i++) {
494 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
496 fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
497 strerror(rc = errno));
499 } else if (!silent) {
500 printf("attr number %d\n", i);
507 gettimeofday(&end, NULL);
509 diff = ((double)(end.tv_sec - start.tv_sec) +
510 (double)(end.tv_usec - start.tv_usec) / 1000000);
512 printf("got %d attrs in %gs (%g attr/s): %s", i,
513 diff, (double)i / diff, ctime(&end.tv_sec));
518 static int jt_test_brw(int argc, char **argv)
520 struct obd_ioctl_data data;
521 struct timeval start;
523 int pages = 1, obdos = 1, count;
524 int silent = 0, write = 0, rw;
529 if (argc < 2 || argc > 6) {
530 fprintf(stderr, "usage: \n"
531 "%s count [write [silent [pages [obdos]]]]\n", argv[0]);
535 count = strtoul(argv[1], NULL, 0);
538 if (argv[2][0] == 'w')
540 else if (argv[2][0] == 'r')
543 write = strtoul(argv[2], NULL, 0);
546 if (argv[3][0] == 's' || argv[3][0] == 'q')
549 silent = strtoul(argv[3], NULL, 0);
552 pages = strtoul(argv[4], NULL, 0);
554 obdos = strtoul(argv[5], NULL, 0);
556 if (obdos != 1 && obdos != 2) {
557 fprintf(stderr, "error: %s: only 1 or 2 obdos supported\n",
562 len = pages * PAGE_SIZE;
564 bulk = calloc(obdos, len);
566 fprintf(stderr, "error: %s: no memory allocating %dx%d pages\n",
567 argv[0], obdos, pages);
571 data.ioc_conn2 = connid;
572 data.ioc_obdo1.o_id = 2;
573 data.ioc_count = len;
575 data.ioc_plen1 = len;
576 data.ioc_pbuf1 = bulk;
578 data.ioc_obdo2.o_id = 2;
579 data.ioc_plen2 = len;
580 data.ioc_pbuf2 = bulk + len;
583 gettimeofday(&start, NULL);
584 printf("%s %d (%dx%d pages) (testing only): %s",
585 write ? "writing" : "reading", count, obdos, pages,
586 ctime(&start.tv_sec));
589 * We will put in the start time (and loop count inside the loop)
590 * at the beginning of each page so that we will be able to validate
591 * (at some later time) whether the data actually made it or not.
593 for (o = 0, b = bulk; o < obdos; o++)
594 for (p = 0; p < pages; p++, b += PAGE_SIZE)
595 memcpy(b, &start, sizeof(start));
597 rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
598 for (i = 0 ; i < count; i++) {
600 b = bulk + sizeof(struct timeval);
601 for (o = 0; o < obdos; o++)
602 for (p = 0; p < pages; p++, b += PAGE_SIZE)
603 memcpy(b, &count, sizeof(count));
606 rc = ioctl(fd, rw, &data);
608 fprintf(stderr, "error: %s: #%d - %s on %s\n", argv[0],
609 i, strerror(rc = errno),
610 write ? "write" : "read");
613 printf("%s number %d\n", write ? "write" : "read", i);
622 gettimeofday(&end, NULL);
624 diff = ((double)(end.tv_sec - start.tv_sec) +
625 (double)(end.tv_usec - start.tv_usec) / 1000000);
627 printf("%s %dx%dx%d pages in %gs (%g pg/s): %s",
628 write ? "wrote" : "read", obdos, pages, i, diff,
629 (double)obdos * i * pages / diff, ctime(&end.tv_sec));
634 static int jt_test_ldlm(int argc, char **argv)
636 struct obd_ioctl_data data;
641 fprintf(stderr, "usage: %s\n", argv[0]);
645 rc = ioctl(fd, IOC_LDLM_TEST, &data);
647 fprintf(stderr, "error: %s: test failed: %s\n", argv[0],
648 strerror(rc = errno));
652 command_t cmdlist[] = {
653 {"device", jt_device, 0, "set current device (args device no)"},
654 {"attach", jt_attach, 0, "name the type of device (args: type data"},
655 {"setup", jt_setup, 0, "setup device (args: <blkdev> [data]"},
656 {"detach", jt_detach, 0, "detach the current device (arg: )"},
657 {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
658 {"create", jt_create, 0, "create [count [mode [silent]]]"},
659 {"destroy", jt_destroy, 0, "destroy <id>"},
660 {"getattr", jt_getattr, 0, "getattr <id>"},
661 {"setattr", jt_setattr, 0, "setattr <id> <mode>"},
662 {"connect", jt_connect, 0, "connect - get a connection to device"},
663 {"disconnect", jt_disconnect, 0,
664 "disconnect - break connection to device"},
665 {"test_getattr", jt_test_getattr, 0, "test_getattr <count> [silent]"},
666 {"test_brw", jt_test_brw, 0, "test_brw <count> [write [silent]]"},
667 {"test_ldlm", jt_test_ldlm, 0, "test lock manager (no args)"},
668 {"help", Parser_help, 0, "help"},
669 {"exit", Parser_quit, 0, "quit"},
670 {"quit", Parser_quit, 0, "quit"},
675 static void signal_server(int sig)
678 fprintf(stderr,"\nDisconnecting any existing connections...\n");
679 do_disconnect("sigint");
684 int main(int argc, char **argv)
686 struct sigaction sigact;
689 sigact.sa_handler = signal_server;
690 sigfillset(&sigact.sa_mask);
691 sigact.sa_flags = SA_RESTART;
692 sigaction(SIGINT, &sigact, NULL);
696 rc = Parser_execarg(argc - 1, &argv[1], cmdlist);
698 Parser_init("obdctl > ", cmdlist);
702 do_disconnect(argv[0]);