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\nsize: %Ld\nblocks: %Ld\nblksize: %d\nmode: %o\nuid: %d\ngid: %d\nflags: %x\nobdflags: %x\nnlink: %d,\nvalid %x\n",
121 static int do_disconnect()
123 struct obd_ioctl_data data;
131 rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
133 printf("Device: %x %s\n", OBD_IOC_DISCONNECT, strerror(errno));
136 printf("Disconnected connid %d\n", connid);
143 static int jt_device(int argc, char **argv)
145 struct obd_ioctl_data data;
150 memset(&data, 0, sizeof(data));
152 fprintf(stderr, "Usage: %s devno\n", argv[0]);
156 data.ioc_dev = atoi(argv[1]);
158 if (obd_ioctl_pack(&data, &buf, max)) {
159 printf("invalid ioctl\n");
164 fd = open("/dev/obd", O_RDWR);
166 printf("Opening /dev/obd: %s\n", strerror(errno));
170 rc = ioctl(fd, OBD_IOC_DEVICE , buf);
172 printf("Device: %x %s\n", OBD_IOC_DEVICE, strerror(errno));
179 static int jt_connect(int argc, char **argv)
181 struct obd_ioctl_data data;
189 fprintf(stderr, "Usage: %s\n", argv[0]);
193 rc = ioctl(fd, OBD_IOC_CONNECT , &data);
195 printf("Device: %x %s\n", OBD_IOC_CONNECT, strerror(errno));
198 connid = data.ioc_conn1;
203 static int jt_disconnect(int argc, char **argv)
205 struct obd_ioctl_data data;
211 fprintf(stderr, "Usage: %s\n", argv[0]);
215 rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
217 printf("Device: %x %s\n", OBD_IOC_DISCONNECT, strerror(errno));
226 static int jt_detach(int argc, char **argv)
228 struct obd_ioctl_data data;
234 fprintf(stderr, "Usage: %s\n", argv[0]);
238 if (obd_ioctl_pack(&data, &buf, max)) {
239 printf("invalid ioctl\n");
243 rc = ioctl(fd, OBD_IOC_DETACH , buf);
245 printf("Detach: %s\n", strerror(errno));
251 static int jt_cleanup(int argc, char **argv)
253 struct obd_ioctl_data data;
259 fprintf(stderr, "Usage: %s\n", argv[0]);
263 rc = ioctl(fd, OBD_IOC_CLEANUP , &data);
265 printf("Detach: %s\n", strerror(errno));
271 static int jt_attach(int argc, char **argv)
273 struct obd_ioctl_data data;
278 if ( argc != 2 && argc != 3 ) {
279 fprintf(stderr, "Usage: %s type [data]\n", argv[0]);
283 data.ioc_inllen1 = strlen(argv[1]) + 1;
284 data.ioc_inlbuf1 = argv[1];
286 data.ioc_inllen2 = strlen(argv[2]) + 1;
287 data.ioc_inlbuf2 = argv[2];
290 printf("attach len %d addr %p type %s data %s\n", data.ioc_len, buf,
291 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
293 if (obd_ioctl_pack(&data, &buf, max)) {
294 printf("invalid ioctl\n");
297 printf("attach len %d addr %p raw %p type %s data %s and %s\n",
298 data.ioc_len, buf, rawbuf,
299 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2), &buf[516]);
301 rc = ioctl(fd, OBD_IOC_ATTACH , buf);
303 printf("Attach: %x %s\n", OBD_IOC_ATTACH, strerror(errno));
309 static int jt_setup(int argc, char **argv)
311 struct obd_ioctl_data data;
317 fprintf(stderr, "Usage: %s [device] [fstype]\n", argv[0]);
322 data.ioc_inllen1 = strlen(argv[1]) + 1;
323 data.ioc_inlbuf1 = argv[1];
324 data.ioc_dev = strtoul(argv[1], NULL, 0);
329 data.ioc_inllen2 = strlen(argv[2]) + 1;
330 data.ioc_inlbuf2 = argv[2];
333 printf("setup len %d addr %p device %s type %s\n", data.ioc_len, buf,
334 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
336 if (obd_ioctl_pack(&data, &buf, max)) {
337 printf("invalid ioctl\n");
340 printf("setup len %d addr %p raw %p device %s type %s\n",
341 data.ioc_len, buf, rawbuf,
342 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
344 rc = ioctl(fd, OBD_IOC_SETUP , buf);
346 printf("setup: %x %s\n", OBD_IOC_SETUP, strerror(errno));
353 static int jt_create(int argc, char **argv)
355 struct obd_ioctl_data data;
363 num = strtoul(argv[1], NULL, 0);
365 printf("usage %s num [mode] [silent]\n", argv[0]);
369 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
371 data.ioc_obdo1.o_mode = 0100644;
373 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
376 silent = strtoul(argv[3], NULL, 0);
379 printf("Creating %d obdos\n", num);
381 for (i = 0 ; i<num ; i++) {
382 rc = ioctl(fd, OBD_IOC_CREATE , &data);
384 printf("Create: %x %s\n", OBD_IOC_CREATE,
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 printf("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 printf("setattr: %x %s\n", OBD_IOC_SETATTR, strerror(errno));
415 static int jt_destroy(int argc, char **argv)
417 struct obd_ioctl_data data;
422 printf("usage: %s id\n", argv[0]);
425 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
426 data.ioc_obdo1.o_mode = S_IFREG|0644;
428 rc = ioctl(fd, OBD_IOC_DESTROY , &data);
430 printf("setattr: %x %s\n", OBD_IOC_DESTROY, strerror(errno));
435 static int jt_getattr(int argc, char **argv)
437 struct obd_ioctl_data data;
442 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
443 /* to help obd filter */
444 data.ioc_obdo1.o_mode = 0100644;
445 data.ioc_obdo1.o_valid = 0xffffffff;
446 printf("getting attr for %Ld\n", data.ioc_obdo1.o_id);
448 fprintf(stderr, "usage: %s id\n", argv[0]);
452 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
454 printf("Error: %s\n", strerror(rc));
456 printf("attr obdo %Ld, mode %o\n", data.ioc_obdo1.o_id,
457 data.ioc_obdo1.o_mode);
462 static int jt_test_getattr(int argc, char **argv)
464 struct obd_ioctl_data data;
465 struct timeval start;
471 if (argc == 2 || argc == 3) {
472 count = strtoul(argv[1], NULL, 0);
475 silent = strtoul(argv[2], NULL, 0);
476 data.ioc_obdo1.o_valid = 0xffffffff;
477 data.ioc_obdo1.o_id = 2;
478 gettimeofday(&start, NULL);
479 printf("getting %d attrs (testing only): %s", count,
480 ctime(&start.tv_sec));
482 fprintf(stderr, "usage: %s count [silent]\n", argv[0]);
486 for (i = 0 ; i < count; i++) {
487 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
489 printf("Error: %s on getattr #%d\n", strerror(rc), i);
491 } else if (!silent) {
492 printf("attr number %d\n", i);
498 gettimeofday(&end, NULL);
500 printf("got attrs successfully %d times (%g/sec): %s", i,
501 (double)i / ((double)(end.tv_sec - start.tv_sec) +
502 (double)(end.tv_usec - start.tv_usec) /
503 1000000), ctime(&end.tv_sec));
508 static int jt_test_brw(int argc, char **argv)
510 struct obd_ioctl_data data;
511 struct timeval start;
513 int pages = 1, obdos = 1, count;
514 int silent = 0, write = 0, rw;
518 if (argc >= 2 && argc <= 6) {
521 count = strtoul(argv[1], NULL, 0);
524 if (argv[2][0] == 'w')
526 else if (argv[2][0] == 'r')
529 write = strtoul(argv[2], NULL, 0);
532 silent = strtoul(argv[3], NULL, 0);
534 pages = strtoul(argv[4], NULL, 0);
536 obdos = strtoul(argv[5], NULL, 0);
538 if (obdos != 1 && obdos != 2) {
539 fprintf(stderr, "%s: only 1 or 2 obdos supported\n",
544 len = pages * PAGE_SIZE;
546 bulk = malloc(obdos * len);
549 "%s: out of memory allocating %dx%d pages\n",
550 argv[0], obdos, pages);
554 data.ioc_conn2 = connid;
555 data.ioc_obdo1.o_id = 2;
556 data.ioc_count = len;
558 data.ioc_plen1 = len;
559 data.ioc_pbuf1 = bulk;
561 data.ioc_obdo2.o_id = 2;
562 data.ioc_plen2 = len;
563 data.ioc_pbuf2 = bulk + len;
566 gettimeofday(&start, NULL);
567 printf("%s %d (%dx%d pages) (testing only): %s",
568 write ? "writing" : "reading", count, obdos, pages,
569 ctime(&start.tv_sec));
571 printf("usage: %s count [write [silent [pages [obdos]]]]\n",
576 rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
577 for (i = 0 ; i < count; i++) {
578 rc = ioctl(fd, rw, &data);
580 printf("Error: %s on %s #%d\n", strerror(rc),
581 write ? "write" : "read", i);
583 } else if (!silent) {
584 printf("%s number %d\n", write ? "write" : "read", i);
593 gettimeofday(&end, NULL);
595 printf("%s %dx%d pages successfully %d times (%g/sec): %s",
596 write ? "wrote" : "read", obdos, pages, i,
597 (double)obdos * i * pages /
598 ((double)(end.tv_sec - start.tv_sec) +
599 (double)(end.tv_usec - start.tv_usec) /
600 1000000), ctime(&end.tv_sec));
605 static int jt_test_ldlm(int argc, char **argv)
607 struct obd_ioctl_data data;
612 fprintf(stderr, "usage: %s\n", argv[0]);
616 rc = ioctl(fd, IOC_LDLM_TEST, &data);
618 fprintf(stderr, "LDLM test failed: %s\n", strerror(errno));
623 {"device", jt_device, 0, "set current device (args device no)"},
624 {"attach", jt_attach, 0, "name the typed of device (args: type data"},
625 {"setup", jt_setup, 0, "setup device (args: blkdev, data"},
626 {"detach", jt_detach, 0, "detach the current device (arg: )"},
627 {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
628 {"create", jt_create, 0, "create [count [mode [silent]]]"},
629 {"destroy", jt_destroy, 0, "destroy id"},
630 {"getattr", jt_getattr, 0, "getattr id"},
631 {"setattr", jt_setattr, 0, "setattr id mode"},
632 {"connect", jt_connect, 0, "connect - get a connection to device"},
633 {"disconnect", jt_disconnect, 0,
634 "disconnect - break connection to device"},
635 {"test_getattr", jt_test_getattr, 0, "test_getattr count [silent]"},
636 {"test_brw", jt_test_brw, 0, "test_brw count [write [silent]]"},
637 {"test_ldlm", jt_test_ldlm, 0, "test lock manager (no args)"},
638 {"help", Parser_help, 0, "help"},
639 {"exit", Parser_quit, 0, "quit"},
640 {"quit", Parser_quit, 0, "quit"},
645 static void signal_server(int sig)
648 fprintf(stderr,"\nDisconnecting any existing connections...\n");
654 int main(int argc, char **argv)
656 struct sigaction sigact;
659 sigact.sa_handler = signal_server;
660 sigfillset(&sigact.sa_mask);
661 sigact.sa_flags = SA_RESTART;
662 sigaction(SIGINT, &sigact, NULL);
666 rc = Parser_execarg(argc - 1, &argv[1], list);
668 Parser_init("obdctl > ", list);