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>
34 #include <linux/lustre_lib.h>
35 #include <linux/lustre_idl.h>
40 #include <netinet/in.h>
43 #include <linux/module.h>
46 #include <linux/list.h>
58 #define IOCINIT(data) \
60 memset(&data, 0, sizeof(data)); \
61 data.ioc_version = OBD_IOCTL_VERSION; \
62 data.ioc_conn1 = connid; \
63 data.ioc_len = sizeof(data); \
65 fprintf(stderr, "No device open, use device\n"); \
71 pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L",
90 0, 0, # struct list_head
96 char * obdo_print(struct obdo *obd)
100 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",
119 static int do_disconnect()
121 struct obd_ioctl_data data;
129 rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
131 printf("Device: %x %s\n", OBD_IOC_DISCONNECT, strerror(errno));
134 printf("Disconnected connid %d\n", connid);
141 static int jt_device(int argc, char **argv)
143 struct obd_ioctl_data data;
148 memset(&data, 0, sizeof(data));
150 fprintf(stderr, "Usage: %s devno\n", argv[0]);
154 data.ioc_dev = atoi(argv[1]);
156 if (obd_ioctl_pack(&data, &buf, max)) {
157 printf("invalid ioctl\n");
162 fd = open("/dev/obd", O_RDWR);
164 printf("Opening /dev/obd: %s\n", strerror(errno));
168 rc = ioctl(fd, OBD_IOC_DEVICE , buf);
170 printf("Device: %x %s\n", OBD_IOC_DEVICE, strerror(errno));
177 static int jt_connect(int argc, char **argv)
179 struct obd_ioctl_data data;
187 fprintf(stderr, "Usage: %s\n", argv[0]);
191 rc = ioctl(fd, OBD_IOC_CONNECT , &data);
193 printf("Device: %x %s\n", OBD_IOC_CONNECT, strerror(errno));
196 connid = data.ioc_conn1;
201 static int jt_disconnect(int argc, char **argv)
203 struct obd_ioctl_data data;
209 fprintf(stderr, "Usage: %s\n", argv[0]);
213 rc = ioctl(fd, OBD_IOC_DISCONNECT , &data);
215 printf("Device: %x %s\n", OBD_IOC_DISCONNECT, strerror(errno));
224 static int jt_detach(int argc, char **argv)
226 struct obd_ioctl_data data;
232 fprintf(stderr, "Usage: %s\n", argv[0]);
236 if (obd_ioctl_pack(&data, &buf, max)) {
237 printf("invalid ioctl\n");
241 rc = ioctl(fd, OBD_IOC_DETACH , buf);
243 printf("Detach: %s\n", strerror(errno));
249 static int jt_cleanup(int argc, char **argv)
251 struct obd_ioctl_data data;
257 fprintf(stderr, "Usage: %s\n", argv[0]);
261 rc = ioctl(fd, OBD_IOC_CLEANUP , &data);
263 printf("Detach: %s\n", strerror(errno));
269 static int jt_attach(int argc, char **argv)
271 struct obd_ioctl_data data;
276 if ( argc != 2 && argc != 3 ) {
277 fprintf(stderr, "Usage: %s type [data]\n", argv[0]);
281 data.ioc_inllen1 = strlen(argv[1]) + 1;
282 data.ioc_inlbuf1 = argv[1];
284 data.ioc_inllen2 = strlen(argv[2]) + 1;
285 data.ioc_inlbuf2 = argv[2];
288 printf("attach len %d addr %p type %s data %s\n", data.ioc_len, buf,
289 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
291 if (obd_ioctl_pack(&data, &buf, max)) {
292 printf("invalid ioctl\n");
295 printf("attach len %d addr %p raw %p type %s data %s and %s\n",
296 data.ioc_len, buf, rawbuf,
297 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2), &buf[516]);
299 rc = ioctl(fd, OBD_IOC_ATTACH , buf);
301 printf("Attach: %x %s\n", OBD_IOC_ATTACH, strerror(errno));
307 static int jt_setup(int argc, char **argv)
309 struct obd_ioctl_data data;
315 fprintf(stderr, "Usage: %s [device] [fstype]\n", argv[0]);
320 data.ioc_inllen1 = strlen(argv[1]) + 1;
321 data.ioc_inlbuf1 = argv[1];
322 data.ioc_dev = strtoul(argv[1], NULL, 0);
327 data.ioc_inllen2 = strlen(argv[2]) + 1;
328 data.ioc_inlbuf2 = argv[2];
331 printf("setup len %d addr %p device %s type %s\n", data.ioc_len, buf,
332 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
334 if (obd_ioctl_pack(&data, &buf, max)) {
335 printf("invalid ioctl\n");
338 printf("setup len %d addr %p raw %p device %s type %s\n",
339 data.ioc_len, buf, rawbuf,
340 MKSTR(data.ioc_inlbuf1), MKSTR(data.ioc_inlbuf2));
342 rc = ioctl(fd, OBD_IOC_SETUP , buf);
344 printf("setup: %x %s\n", OBD_IOC_SETUP, strerror(errno));
351 static int jt_create(int argc, char **argv)
353 struct obd_ioctl_data data;
361 num = strtoul(argv[1], NULL, 0);
363 printf("usage %s num [mode] [silent]\n", argv[0]);
367 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
369 data.ioc_obdo1.o_mode = 0100644;
371 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
374 silent = strtoul(argv[3], NULL, 0);
377 printf("Creating %d obdos\n", num);
379 for (i = 0 ; i<num ; i++) {
380 rc = ioctl(fd, OBD_IOC_CREATE , &data);
382 printf("Create: %x %s\n", OBD_IOC_CREATE,
386 printf("created obdo %Ld\n", data.ioc_obdo1.o_id);
391 static int jt_setattr(int argc, char **argv)
393 struct obd_ioctl_data data;
398 printf("usage: %s id mode\n", argv[0]);
402 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
403 data.ioc_obdo1.o_mode = strtoul(argv[2], NULL, 0);
404 data.ioc_obdo1.o_valid = OBD_MD_FLMODE;
406 rc = ioctl(fd, OBD_IOC_SETATTR , &data);
408 printf("setattr: %x %s\n", OBD_IOC_SETATTR, strerror(errno));
413 static int jt_destroy(int argc, char **argv)
415 struct obd_ioctl_data data;
420 printf("usage: %s id\n", argv[0]);
423 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
425 rc = ioctl(fd, OBD_IOC_DESTROY , &data);
427 printf("setattr: %x %s\n", OBD_IOC_DESTROY, strerror(errno));
432 static int jt_getattr(int argc, char **argv)
434 struct obd_ioctl_data data;
439 data.ioc_obdo1.o_id = strtoul(argv[1], NULL, 0);
440 data.ioc_obdo1.o_valid = 0xffffffff;
441 printf("getting attr for %Ld\n", data.ioc_obdo1.o_id);
443 printf("usage: %s id\n", argv[0]);
447 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
449 printf("Error: %s\n", strerror(rc));
451 printf("attr obdo %Ld, mode %o\n", data.ioc_obdo1.o_id,
452 data.ioc_obdo1.o_mode);
457 static int jt_test_getattr(int argc, char **argv)
459 struct obd_ioctl_data data;
460 struct timeval start;
466 if (argc == 2 || argc == 3) {
467 count = strtoul(argv[1], NULL, 0);
470 silent = strtoul(argv[2], NULL, 0);
471 data.ioc_obdo1.o_valid = 0xffffffff;
472 data.ioc_obdo1.o_id = 2;
473 gettimeofday(&start, NULL);
474 printf("getting %d attrs (testing only): %s", count,
475 ctime(&start.tv_sec));
477 printf("usage: %s count [silent]\n", argv[0]);
481 for (i = 0 ; i < count; i++) {
482 rc = ioctl(fd, OBD_IOC_GETATTR , &data);
484 printf("Error: %s on getattr #%d\n", strerror(rc), i);
486 } else if (!silent) {
487 printf("attr number %d\n", i);
493 gettimeofday(&end, NULL);
495 printf("got attrs successfully %d times (%g/sec): %s", i,
496 (double)i / ((double)(end.tv_sec - start.tv_sec) +
497 (double)(end.tv_usec - start.tv_usec) /
498 1000000), ctime(&end.tv_sec));
503 static int jt_test_brw(int argc, char **argv)
505 struct obd_ioctl_data data;
506 struct timeval start;
508 int pages = 4, count;
509 int silent = 0, write = 0, rw;
513 if (argc >= 2 && argc <= 5) {
516 count = strtoul(argv[1], NULL, 0);
519 write = strtoul(argv[2], NULL, 0);
521 silent = strtoul(argv[3], NULL, 0);
523 pages = strtoul(argv[4], NULL, 0);
525 len = pages * PAGE_SIZE;
527 bulk = malloc(2 * len);
529 printf("%s: out of memory allocating 2x%d pages\n",
534 data.ioc_conn2 = connid;
535 data.ioc_obdo1.o_id = data.ioc_obdo2.o_id = 2;
536 data.ioc_count = len;
538 data.ioc_plen1 = data.ioc_plen2 = len;
539 data.ioc_pbuf1 = bulk;
540 data.ioc_pbuf2 = bulk + len;
542 gettimeofday(&start, NULL);
543 printf("%s %d (2x%d pages) (testing only): %s",
544 write ? "writing" : "reading", count, pages,
545 ctime(&start.tv_sec));
547 printf("usage: %s count [write [silent [pages]]]\n", argv[0]);
551 rw = write ? OBD_IOC_BRW_WRITE : OBD_IOC_BRW_READ;
552 for (i = 0 ; i < count; i++) {
553 rc = ioctl(fd, rw, &data);
555 printf("Error: %s on %s #%d\n", strerror(rc),
556 write ? "write" : "read", i);
558 } else if (!silent) {
559 printf("%s number %d\n", write ? "write" : "read", i);
568 gettimeofday(&end, NULL);
570 printf("%s 2x%d pages successfully %d times (%g/sec): %s",
571 write ? "wrote" : "read", pages, i,
572 2.0 * i * pages / ((double)(end.tv_sec - start.tv_sec) +
573 (double)(end.tv_usec - start.tv_usec) /
574 1000000), ctime(&end.tv_sec));
580 {"device", jt_device, 0, "set current device (args device no)"},
581 {"attach", jt_attach, 0, "name the typed of device (args: type data"},
582 {"setup", jt_setup, 0, "setup device (args: blkdev, data"},
583 {"detach", jt_detach, 0, "detach the current device (arg: )"},
584 {"cleanup", jt_cleanup, 0, "cleanup the current device (arg: )"},
585 {"create", jt_create, 0, "create [count [mode [silent]]]"},
586 {"destroy", jt_destroy, 0, "destroy id"},
587 {"getattr", jt_getattr, 0, "getattr id"},
588 {"setattr", jt_setattr, 0, "setattr id mode"},
589 {"connect", jt_connect, 0, "connect - get a connection to device"},
590 {"disconnect", jt_disconnect, 0,
591 "disconnect - break connection to device"},
592 {"test_getattr", jt_test_getattr, 0, "test_getattr count [silent]"},
593 {"test_brw", jt_test_brw, 0, "test_brw count [write [silent]]"},
594 {"help", Parser_help, 0, "help"},
595 {"exit", Parser_quit, 0, "quit"},
596 {"quit", Parser_quit, 0, "quit"},
601 static void signal_server(int sig)
604 fprintf(stderr,"\nDisconnecting any existing connections...\n");
610 int main(int argc, char **argv)
612 struct sigaction sigact;
615 sigact.sa_handler = signal_server;
616 sigfillset(&sigact.sa_mask);
617 sigact.sa_flags = SA_RESTART;
618 sigaction(SIGINT, &sigact, NULL);
622 rc = Parser_execarg(argc - 1, &argv[1], list);
624 Parser_init("obdctl > ", list);