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 = 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);
427 rc = ioctl(fd, OBD_IOC_DESTROY , &data);
429 printf("setattr: %x %s\n", OBD_IOC_DESTROY, strerror(errno));
434 static int jt_getattr(int argc, char **argv)
436 struct obd_ioctl_data data;
441 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
442 data.ioc_obdo1.o_valid = 0xffffffff;
443 printf("getting attr for %Ld\n", data.ioc_obdo1.o_id);
445 fprintf(stderr, "usage: %s id\n", argv[0]);
449 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
451 printf("Error: %s\n", strerror(rc));
453 printf("attr obdo %Ld, mode %o\n", data.ioc_obdo1.o_id,
454 data.ioc_obdo1.o_mode);
459 static int jt_test_getattr(int argc, char **argv)
461 struct obd_ioctl_data data;
462 struct timeval start;
468 if (argc == 2 || argc == 3) {
469 count = strtoul(argv[1], NULL, 0);
472 silent = strtoul(argv[2], NULL, 0);
473 data.ioc_obdo1.o_valid = 0xffffffff;
474 data.ioc_obdo1.o_id = 2;
475 gettimeofday(&start, NULL);
476 printf("getting %d attrs (testing only): %s", count,
477 ctime(&start.tv_sec));
479 fprintf(stderr, "usage: %s count [silent]\n", argv[0]);
483 for (i = 0 ; i < count; i++) {
484 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
486 printf("Error: %s on getattr #%d\n", strerror(rc), i);
488 } else if (!silent) {
489 printf("attr number %d\n", i);
495 gettimeofday(&end, NULL);
497 printf("got attrs successfully %d times (%g/sec): %s", i,
498 (double)i / ((double)(end.tv_sec - start.tv_sec) +
499 (double)(end.tv_usec - start.tv_usec) /
500 1000000), ctime(&end.tv_sec));
505 static int jt_test_brw(int argc, char **argv)
507 struct obd_ioctl_data data;
508 struct timeval start;
510 int pages = 4, count;
511 int silent = 0, write = 0, rw;
515 if (argc >= 2 && argc <= 5) {
518 count = strtoul(argv[1], NULL, 0);
521 write = strtoul(argv[2], NULL, 0);
523 silent = strtoul(argv[3], NULL, 0);
525 pages = strtoul(argv[4], NULL, 0);
527 len = pages * PAGE_SIZE;
529 bulk = malloc(2 * len);
532 "%s: out of memory allocating 2x%d pages\n",
537 data.ioc_conn2 = connid;
538 data.ioc_obdo1.o_id = data.ioc_obdo2.o_id = 2;
539 data.ioc_count = len;
541 data.ioc_plen1 = data.ioc_plen2 = len;
542 data.ioc_pbuf1 = bulk;
543 data.ioc_pbuf2 = bulk + len;
545 gettimeofday(&start, NULL);
546 printf("%s %d (2x%d pages) (testing only): %s",
547 write ? "writing" : "reading", count, pages,
548 ctime(&start.tv_sec));
550 printf("usage: %s count [write [silent [pages]]]\n", argv[0]);
554 rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
555 for (i = 0 ; i < count; i++) {
556 rc = ioctl(fd, rw, &data);
558 printf("Error: %s on %s #%d\n", strerror(rc),
559 write ? "write" : "read", i);
561 } else if (!silent) {
562 printf("%s number %d\n", write ? "write" : "read", i);
571 gettimeofday(&end, NULL);
573 printf("%s 2x%d pages successfully %d times (%g/sec): %s",
574 write ? "wrote" : "read", pages, i,
575 2.0 * i * pages / ((double)(end.tv_sec - start.tv_sec) +
576 (double)(end.tv_usec - start.tv_usec) /
577 1000000), ctime(&end.tv_sec));
582 static int jt_test_ldlm(int argc, char **argv)
584 struct obd_ioctl_data data;
589 fprintf(stderr, "usage: %s\n", argv[0]);
593 rc = ioctl(fd, IOC_LDLM_TEST, &data);
595 fprintf(stderr, "LDLM test failed: %s\n", strerror(errno));
600 {"device", jt_device, 0, "set current device (args device no)"},
601 {"attach", jt_attach, 0, "name the typed of device (args: type data"},
602 {"setup", jt_setup, 0, "setup device (args: blkdev, data"},
603 {"detach", jt_detach, 0, "detach the current device (arg: )"},
604 {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
605 {"create", jt_create, 0, "create [count [mode [silent]]]"},
606 {"destroy", jt_destroy, 0, "destroy id"},
607 {"getattr", jt_getattr, 0, "getattr id"},
608 {"setattr", jt_setattr, 0, "setattr id mode"},
609 {"connect", jt_connect, 0, "connect - get a connection to device"},
610 {"disconnect", jt_disconnect, 0,
611 "disconnect - break connection to device"},
612 {"test_getattr", jt_test_getattr, 0, "test_getattr count [silent]"},
613 {"test_brw", jt_test_brw, 0, "test_brw count [write [silent]]"},
614 {"test_ldlm", jt_test_ldlm, 0, "test lock manager (no args)"},
615 {"help", Parser_help, 0, "help"},
616 {"exit", Parser_quit, 0, "quit"},
617 {"quit", Parser_quit, 0, "quit"},
622 static void signal_server(int sig)
625 fprintf(stderr,"\nDisconnecting any existing connections...\n");
631 int main(int argc, char **argv)
633 struct sigaction sigact;
636 sigact.sa_handler = signal_server;
637 sigfillset(&sigact.sa_mask);
638 sigact.sa_flags = SA_RESTART;
639 sigaction(SIGINT, &sigact, NULL);
643 rc = Parser_execarg(argc - 1, &argv[1], list);
645 Parser_init("obdctl > ", list);