4 # This code is issued under the GNU General Public License.
5 # See the file COPYING in this distribution
7 # Copyright (C) 1998, Stelias Computing
9 # Modified for InterMezzo from Gordian's HSM bcache device/jcm module
10 # Copyright (C) 1999, Carnegie Mellon University
12 # Derived from InterMezzo's incontrol, modified for OBD's
13 # Copyright (C) 1999, Stelias Computing
18 BEGIN { require "asm/errno.ph" };
19 BEGIN { require "asm/ioctl.ph" };
21 # p2ph generated invalid macros for ioctl stuff, so I override some of it here
22 eval 'sub OBD_IOC_CREATE () { &_IOC(2, ord(\'f\'), 3, 4);}' unless
23 defined(&OBD_IOC_CREATE);
24 eval 'sub OBD_IOC_SETUP_OBDDEV () { &_IOC(1, ord(\'f\'), 4, 4);}' unless
25 defined(&OBD_IOC_SETUP_OBDDEV);
26 eval 'sub OBD_IOC_CLEANUP_OBDDEV () { &_IOC(0, ord(\'f\'), 5, 0);}' unless
27 defined(&OBD_IOC_CLEANUP_OBDDEV);
28 eval 'sub OBD_IOC_DESTROY () { &_IOC(1, ord(\'f\'), 6, 4);}' unless
29 defined(&OBD_IOC_DESTROY);
30 eval 'sub OBD_IOC_PREALLOCATE () { &_IOC(3, ord(\'f\'), 7, 4);}' unless
31 defined(&OBD_IOC_PREALLOCATE);
32 eval 'sub OBD_IOC_DEC_USE_COUNT () { &_IOC(0, ord(\'f\'), 8, 0);}' unless
33 defined(&OBD_IOC_DEC_USE_COUNT);
34 eval 'sub OBD_IOC_SETATTR () { &_IOC(1, ord(\'f\'), 9, 4);}' unless
35 defined(&OBD_IOC_SETATTR);
36 eval 'sub OBD_IOC_GETATTR () { &_IOC(2, ord(\'f\'), 10, 4);}' unless
37 defined(&OBD_IOC_GETATTR);
38 eval 'sub OBD_IOC_READ () { &_IOC(3, ord(\'f\'), 11, 4);}' unless
39 defined(&OBD_IOC_READ);
40 eval 'sub OBD_IOC_WRITE () { &_IOC(3, ord(\'f\'), 12, 4);}' unless
41 defined(&OBD_IOC_WRITE);
42 eval 'sub OBD_IOC_CONNECT () { &_IOC(2, ord(\'f\'), 13, 4);}' unless
43 defined(&OBD_IOC_CONNECT);
44 eval 'sub OBD_IOC_DISCONNECT () { &_IOC(1, ord(\'f\'), 14, 4);}' unless
45 defined(&OBD_IOC_DISCONNECT);
46 eval 'sub OBD_IOC_STATFS () { &_IOC(3, ord(\'f\'), 15, 4);}' unless
47 defined(&OBD_IOC_STATFS);
48 eval 'sub OBD_IOC_SYNC () { &_IOC(2, ord(\'f\'), 16, 4);}' unless
49 defined(&OBD_IOC_SYNC);
50 eval 'sub OBD_IOC_READ2 () { &_IOC(3, ord(\'f\'), 17, 4);}' unless
51 defined(&OBD_IOC_READ2);
52 eval 'sub OBD_IOC_FORMATOBD () { &_IOC(3, ord(\'f\'), 18, 4);}' unless
53 defined(&OBD_IOC_FORMATOBD);
54 eval 'sub OBD_IOC_PARTITION () { &_IOC(3, ord(\'f\'), 19, 4);}' unless
55 defined(&OBD_IOC_PARTITION);
56 eval 'sub OBD_IOC_ATTACH () { &_IOC(3, ord(\'f\'), 20, 4);}' unless
57 defined(&OBD_IOC_ATTACH);
59 eval 'sub ATTR_MODE () {1;}' unless defined(&ATTR_MODE);
60 eval 'sub ATTR_UID () {2;}' unless defined(&ATTR_UID);
61 eval 'sub ATTR_GID () {4;}' unless defined(&ATTR_GID);
62 eval 'sub ATTR_SIZE () {8;}' unless defined(&ATTR_SIZE);
63 eval 'sub ATTR_ATIME () {16;}' unless defined(&ATTR_ATIME);
64 eval 'sub ATTR_MTIME () {32;}' unless defined(&ATTR_MTIME);
65 eval 'sub ATTR_CTIME () {64;}' unless defined(&ATTR_CTIME);
74 my ($device, $filesystem);
75 # startup options (I'll replace these when I have some to replace with)
76 GetOptions("device=s" => \$device, "fs=s" => $filesystem) || die "Getoptions";
78 # genuine new simulated OBD device
79 $device = "/dev/obd" unless $device;
80 # object store in the ext2 formatted block device
81 $filesystem = "/dev/loop0" unless $filesystem;
83 # get a console for the app
84 my $term = new Term::ReadLine 'obdcontrol ';
85 my $attribs = $term->Attribs;
86 $term->ornaments('md,me,,'); # bold face prompt
88 # make sure stdout is not buffered
96 ('create' => {func => "Create", doc => "create: creates a new inode"},
97 'attach' => {func => "Attach", doc => "format type [adapter bus tid lun]"},
98 'format' => {func => "Format", doc => "format type adapter bus tid lun size"},
99 'partition' => {func => "Partition", doc => "partition type adapter bus tid lun partition size"},
100 'setup' => {func => "Setup", doc => "setup: link the ext2 partition (default /dev/loop0) to this obddev"},
101 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
102 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
103 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
104 'destroy' => {func => "Destroy", doc => "setup: destroys an inode"},
105 'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
106 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
107 'read' => {func => "Read", doc => "read <inode> <count> [offset]"},
108 'fsread' => {func => "Read2", doc => "read <inode> <count> [offset]"},
109 'write' => {func => "Write", doc => "write <inode> <offset> <text>"},
110 'setattr' => {func => "Setattr", doc => "setattr <inode> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"},
111 'getattr' => {func => "Getattr", doc => "getattr <inode>: displays inode object attributes"},
112 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num inodes."},
113 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
114 'help' => {func => \&Help, doc => "help: this message"},
115 'quit' => {func => \&Quit, doc => "see \"exit\""},
116 'exit' => {func => \&Quit, doc => "see \"quit\""}
120 # setup completion function
122 my @jcm_cmd_list = keys %commands;
124 $attribs->{attempted_completion_function} = \&completeme;
125 #------------------------------------------------------------------------------
126 # Open the device, as we need an FD for the ioctl
127 sysopen(DEV_OBD, $device, 0) || die "Cannot open $device";
129 if (!defined($::st = stat($filesystem))) {
130 die "Unable to stat $filesystem.\n";
133 # Get on with the show
136 #------------------------------------------------------------------------------
138 my ($text, $line, $start, $end) = @_;
139 if (substr($line, 0, $start) =~ /^\s*$/) {
140 $attribs->{completion_word} = \@jcm_cmd_list;
141 return $term->completion_matches($text,
142 $attribs->{'list_completion_function'});
149 my @completions = completeme($given, $given, 0, length($given));
150 if ($#completions == 0) {
151 $name = shift @completions;
157 # start making requests
160 $line = $term->readline("obdcontrol > ");
168 my @arg = split(' ', $line);
169 my $word = shift @arg;
171 my $cmd = find_command($word);
173 printf STDERR "$word: No such command, or not unique.\n";
177 if ($cmd eq "help" || $cmd eq "exit" || $cmd eq "quit") {
178 return (&{$commands{$cmd}->{func}}(@arg));
182 return (&{$commands{$cmd}->{func}}(@arg));
192 if ($type eq "obdscsi" ) {
197 $data = pack("iiiii", $adapter, $bus, $tid, $lun, $size);
201 my $packed = pack("ipip", length($type), $type, $datalen, $data);
203 my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
206 print STDERR "ioctl failed: $!\n";
207 } elsif ($rc eq "0 but true") {
208 print "Finished (success)\n";
210 print "ioctl returned error code $rc.\n";
217 my $data = pack("i", $size);
220 my $packed = pack("ip", $datalen, $data);
221 my $rc = ioctl(DEV_OBD, &OBD_IOC_FORMATOBD, $packed);
224 print STDERR "ioctl failed: $!\n";
225 } elsif ($rc eq "0 but true") {
226 print "Finished (success)\n";
228 print "ioctl returned error code $rc.\n";
236 my $data = pack("ii", $partno, $size);
239 my $packed = pack("ip", $datalen, $data);
240 my $rc = ioctl(DEV_OBD, &OBD_IOC_PARTITION, $packed);
243 print STDERR "ioctl failed: $!\n";
244 } elsif ($rc eq "0 but true") {
245 print "Finished (success)\n";
247 print "ioctl returned error code $rc.\n";
257 $type = "sim_obd" unless $type;
259 if ( $type eq "sim_obd" ) {
261 $dev = $::st->rdev() unless $dev;
262 $data = pack("i", $dev);
266 my $packed = pack("ip", $datalen, $data);
267 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP_OBDDEV, $packed);
270 print STDERR "ioctl failed: $!\n";
271 } elsif ($rc eq "0 but true") {
272 print "Finished (success)\n";
274 print "ioctl returned error code $rc.\n";
280 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP_OBDDEV, $err);
283 print STDERR "ioctl failed: $!\n";
284 } elsif ($rc eq "0 but true") {
285 print "Finished (success)\n";
287 print "ioctl returned error code $rc.\n";
296 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
297 $id = unpack("I", $packed);
300 print STDERR "ioctl failed: $!\n";
301 } elsif ($rc eq "0 but true") {
303 print "Client ID : $id\n";
304 print "Finished (success)\n";
306 print "ioctl returned error code $rc.\n";
318 print "syntax: disconnect [client ID]\n";
319 print "When client ID is not given, the last valid client ID to be returned by a\n";
320 print "connect command this session is used; there is no such ID.\n";
324 my $packed = pack("L", $id);
325 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
328 print STDERR "ioctl failed: $!\n";
329 } elsif ($rc eq "0 but true") {
330 $::client_id = undef;
331 print "Finished (success)\n";
333 print "ioctl returned error code $rc.\n";
342 if (defined($quiet) && !($quiet eq "quiet")) {
343 print "syntax: create [number of objects [quiet]]\n";
347 my $packed = pack("I", $::client_id);
348 if (!defined($arg) || scalar($arg) < 2) {
349 print "Creating 1 object...\n";
350 my $packed = pack("I", 0);
351 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
352 if (!defined($quiet)) {
353 my $ino = unpack("L", $packed);
354 print "Created object #$ino.\n";
359 print "Creating " . scalar($arg) . " objects...\n";
360 for ($i = 0; $i < scalar($arg); $i++) {
361 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
362 my $ino = unpack("L", $packed);
363 if (!($rc eq "0 but true") || $packed == 0) {
365 } elsif (!defined($quiet)) {
366 print "Created object #$ino.\n";
372 print STDERR "ioctl failed: $!\n";
373 } elsif ($rc eq "0 but true") {
374 print "Finished (success)\n";
376 print "ioctl returned error code $rc.\n";
382 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
385 print STDERR "ioctl failed: $!\n";
386 } elsif ($rc eq "0 but true") {
387 print "Finished (success)\n";
389 print "ioctl returned error code $rc.\n";
394 if (!defined($::client_id)) {
395 print "You must first ``connect''.\n";
401 if (!defined($arg) || scalar($arg) < 1) {
402 print "destroy requires the object number to destroy.\n";
406 print "Destroying object $arg...\n";
407 my $packed = pack("IL", $::client_id, $arg);
408 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
411 print STDERR "ioctl failed: $!\n";
412 } elsif ($rc eq "0 but true") {
413 print "Finished (success)\n";
415 print "ioctl returned error code $rc.\n";
420 if (!defined($::client_id)) {
421 print "You must first ``connect''.\n";
427 if (!defined($inode) || scalar($inode) < 1) {
428 print "invalid arguments; type \"help getattr\" for a synopsis\n";
433 my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
435 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
438 print STDERR "ioctl failed: $!\n";
439 } elsif ($rc eq "0 but true") {
440 my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
441 ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
442 unpack("ISssx2lLLLI", $packed);
444 printf("Inode: %d Mode: %o\n", $inode, $mode);
445 printf("User: %6d Group: %6d Size: %d\n", $uid, $gid, $size);
446 printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
447 printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
448 printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
449 printf("flags: %08x\n", $flags);
450 print "Finished (success)\n";
452 print "ioctl returned error code $rc.\n";
457 if (!defined($::client_id)) {
458 print "You must first ``connect''.\n";
464 my $mode = oct(shift);
478 if (defined($size)) {
479 $valid |= &ATTR_SIZE;
481 if (defined($atime)) {
482 $valid |= &ATTR_ATIME;
484 if (defined($mtime)) {
485 $valid |= &ATTR_MTIME;
487 if (defined($ctime)) {
488 $valid |= &ATTR_CTIME;
490 if (defined($mode)) {
491 $valid |= &ATTR_MODE;
494 if (!defined($inode) || scalar($inode) < 1) {
495 print "invalid arguments; type \"help setattr\" for a synopsis\n";
500 # unsigned int ia_valid; (32)
501 # umode_t ia_mode; (16)
504 # -- 16 bit alignment here! --
505 # off_t ia_size; (32)
506 # time_t ia_atime; (32)
507 # time_t ia_mtime; (32)
508 # time_t ia_ctime; (32)
509 # unsigned int ia_attr_flags; (32)
512 printf "valid is %x, mode is %o\n", $valid, $mode;
513 my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
514 $uid, $gid, $size, $atime, $mtime, $ctime, 0);
515 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
518 print STDERR "ioctl failed: $!\n";
519 } elsif ($rc eq "0 but true") {
520 print "Finished (success)\n";
522 print "ioctl returned error code $rc.\n";
527 if (!defined($::client_id)) {
528 print "You must first ``connect''.\n";
536 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
537 $count < 1 || (defined($offset) && $offset < 0)) {
538 print "invalid arguments; type \"help read\" for a synopsis\n";
542 if (!defined($offset)) {
546 print("Reading $count bytes starting at byte $offset from object " .
549 # "allocate" a large enough buffer
550 my $buf = sprintf("%${count}s", " ");
551 die "suck" if (length($buf) != $count);
553 # the perl we're using doesn't support pack type Q, and offset is 64 bits
554 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
556 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
558 $retval = unpack("l", $packed);
561 print STDERR "ioctl failed: $!\n";
562 } elsif ($rc eq "0 but true") {
564 print substr($buf, 0, $retval);
565 print "\nRead $retval of an attempted $count bytes.\n";
566 print "Finished (success)\n";
568 print "Finished (error $retval)\n";
571 print "ioctl returned error code $rc.\n";
576 if (!defined($::client_id)) {
577 print "You must first ``connect''.\n";
585 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
586 $count < 1 || (defined($offset) && $offset < 0)) {
587 print "invalid arguments; type \"help read\" for a synopsis\n";
591 if (!defined($offset)) {
595 print("Reading $count bytes starting at byte $offset from object " .
598 # "allocate" a large enough buffer
599 my $buf = sprintf("%${count}s", " ");
600 die "suck" if (length($buf) != $count);
602 # the perl we're using doesn't support pack type Q, and offset is 64 bits
603 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
605 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed);
607 $retval = unpack("l", $packed);
610 print STDERR "ioctl failed: $!\n";
611 } elsif ($rc eq "0 but true") {
613 print substr($buf, 0, $retval);
614 print "\nRead $retval of an attempted $count bytes.\n";
615 print "Finished (success)\n";
617 print "Finished (error $retval)\n";
620 print "ioctl returned error code $rc.\n";
625 if (!defined($::client_id)) {
626 print "You must first ``connect''.\n";
632 my $text = join(' ', @_);
633 my $count = length($text);
635 if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
636 scalar($offset) < 0) {
637 print "invalid arguments; type \"help write\" for a synopsis\n";
641 if (!defined($text)) {
646 print("Writing $count bytes starting at byte $offset to object " .
649 # the perl we're using doesn't support pack type Q
650 my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, $offset, 0);
651 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
653 $retval = unpack("l", $packed);
656 print STDERR "ioctl failed: $!\n";
657 } elsif ($rc eq "0 but true") {
659 print "\nWrote $retval of an attempted $count bytes.\n";
660 print "Finished (success)\n";
662 print "Finished (error $retval)\n";
665 print "ioctl returned error code $rc.\n";
672 if (!defined($::client_id)) {
673 print "You must first ``connect''.\n";
677 if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
681 print "Preallocating $arg inodes...\n";
682 my $packed = pack("LLx128", $::client_id, $arg);
683 # client id, alloc, inodes[32]
685 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
688 print STDERR "ioctl failed: $!\n";
689 } elsif ($rc eq "0 but true") {
690 my $alloc = unpack("x4L", $packed);
691 my @inodes = unpack("x8L32", $packed);
694 print "Got $alloc inodes: ";
695 foreach $i (@inodes) {
698 print "\nFinished (success)\n";
700 print "ioctl returned error code $rc.\n";
705 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
708 print STDERR "ioctl failed: $!\n";
709 } elsif ($rc eq "0 but true") {
710 print "Finished (success)\n";
712 print "ioctl returned error code $rc.\n";
717 if (!defined($::client_id)) {
718 print "You must first ``connect''.\n";
730 # __kernel_fsid_t f_fsid; (64 bits)
735 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
738 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
741 print STDERR "ioctl failed: $!\n";
742 } elsif ($rc eq "0 but true") {
743 # skip both the conn_id and the fs_type in the buffer
744 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
745 unpack("x4x4LLLLLL", $packed);
746 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
747 "$bfree free ($bavail available).\n");
748 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
749 print "Finished (success)\n";
751 print "ioctl returned error code $rc.\n";
758 if ( !$arg || !$commands{$arg} ) {
759 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
761 print "Usage: " . $commands{$arg}->{doc} . "\n";
767 print "Disconnecting active session ($::client_id)...";
768 Disconnect($::client_id);