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 () { &_IOC(0, ord(\'f\'), 5, 0);}' unless
27 defined(&OBD_IOC_CLEANUP);
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);
75 my ($device, $filesystem, $file);
76 # startup options (I'll replace these when I have some to replace with)
77 GetOptions("f!" => \$file, "device=s" => \$device, "fs=s" => $filesystem) || die "Getoptions";
79 # genuine new simulated OBD device
80 $device = "/dev/obd0" unless $device;
81 # object store in the ext2 formatted block device
82 $filesystem = "/dev/loop0" unless $filesystem;
86 # get a console for the app
93 ('create' => {func => "Create", doc => "create: creates a new inode"},
94 'attach' => {func => "Attach", doc => "format type [adapter bus tid lun]"},
95 'format' => {func => "Format", doc => "format type adapter bus tid lun size"},
96 'partition' => {func => "Partition", doc => "partition type adapter bus tid lun partition size"},
97 'setup' => {func => "Setup", doc => "setup: link the ext2 partition (default /dev/loop0) to this obddev"},
98 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
99 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
100 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
101 'destroy' => {func => "Destroy", doc => "setup: destroys an inode"},
102 'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
103 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
104 'read' => {func => "Read", doc => "read <inode> <count> [offset]"},
105 'fsread' => {func => "Read2", doc => "read <inode> <count> [offset]"},
106 'write' => {func => "Write", doc => "write <inode> <offset> <text>"},
107 'setattr' => {func => "Setattr", doc => "setattr <inode> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"},
108 'getattr' => {func => "Getattr", doc => "getattr <inode>: displays inode object attributes"},
109 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num inodes."},
110 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
111 'help' => {func => \&Help, doc => "help: this message"},
112 'quit' => {func => \&Quit, doc => "see \"exit\""},
113 'exit' => {func => \&Quit, doc => "see \"quit\""}
117 # setup completion function
119 my @jcm_cmd_list = keys %commands;
121 #------------------------------------------------------------------------------
122 # Open the device, as we need an FD for the ioctl
123 sysopen(DEV_OBD, $device, 0) || die "Cannot open $device";
125 if (!defined($::st = stat($filesystem))) {
126 die "Unable to stat $filesystem.\n";
138 $term = new Term::ReadLine 'obdcontrol ';
139 $attribs = $term->Attribs;
140 $attribs->{attempted_completion_function} = \&completeme;
141 $term->ornaments('md,me,,'); # bold face prompt
143 # make sure stdout is not buffered
144 STDOUT->autoflush(1);
146 # Get on with the show
150 #------------------------------------------------------------------------------
152 my ($text, $line, $start, $end) = @_;
153 if (substr($line, 0, $start) =~ /^\s*$/) {
154 $attribs->{completion_word} = \@jcm_cmd_list;
155 return $term->completion_matches($text,
156 $attribs->{'list_completion_function'});
163 my @completions = completeme($given, $given, 0, length($given));
164 if ($#completions == 0) {
165 $name = shift @completions;
171 # start making requests
174 $line = $term->readline("obdcontrol > ");
182 my @arg = split(' ', $line);
183 my $word = shift @arg;
189 $cmd = find_command($word);
192 printf STDERR "$word: No such command, or not unique.\n";
196 if ($cmd eq "help" || $cmd eq "exit" || $cmd eq "quit") {
197 return (&{$commands{$cmd}->{func}}(@arg));
201 return (&{$commands{$cmd}->{func}}(@arg));
211 if ($type eq "obdscsi" ) {
216 $data = pack("iiiii", $adapter, $bus, $tid, $lun, $size);
220 my $packed = pack("ipip", length($type), $type, $datalen, $data);
222 my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
225 print STDERR "ioctl failed: $!\n";
226 } elsif ($rc eq "0 but true") {
227 print "Finished (success)\n";
229 print "ioctl returned error code $rc.\n";
236 my $data = pack("i", $size);
239 my $packed = pack("ip", $datalen, $data);
240 my $rc = ioctl(DEV_OBD, &OBD_IOC_FORMATOBD, $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";
255 my $data = pack("ii", $partno, $size);
258 my $packed = pack("ip", $datalen, $data);
259 my $rc = ioctl(DEV_OBD, &OBD_IOC_PARTITION, $packed);
262 print STDERR "ioctl failed: $!\n";
263 } elsif ($rc eq "0 but true") {
264 print "Finished (success)\n";
266 print "ioctl returned error code $rc.\n";
276 $type = "sim_obd" unless $type;
278 if ( $type eq "sim_obd" ) {
280 $dev = $::st->rdev() unless $dev;
281 $data = pack("i", $dev);
285 my $packed = pack("ip", $datalen, $data);
286 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP_OBDDEV, $packed);
289 print STDERR "ioctl failed: $!\n";
290 } elsif ($rc eq "0 but true") {
291 print "Finished (success)\n";
293 print "ioctl returned error code $rc.\n";
299 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP, $err);
302 print STDERR "ioctl failed: $!\n";
303 } elsif ($rc eq "0 but true") {
304 print "Finished (success)\n";
307 print "ioctl returned error code $rc.\n";
316 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
317 $id = unpack("I", $packed);
320 print STDERR "ioctl failed: $!\n";
321 } elsif ($rc eq "0 but true") {
323 print "Client ID : $id\n";
324 print "Finished (success)\n";
326 print "ioctl returned error code $rc.\n";
338 print "syntax: disconnect [client ID]\n";
339 print "When client ID is not given, the last valid client ID to be returned by a\n";
340 print "connect command this session is used; there is no such ID.\n";
344 my $packed = pack("L", $id);
345 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
348 print STDERR "ioctl failed: $!\n";
349 } elsif ($rc eq "0 but true") {
350 $::client_id = undef;
351 print "Finished (success)\n";
353 print "ioctl returned error code $rc.\n";
363 if (defined($quiet) && !($quiet eq "quiet")) {
364 print "syntax: create [number of objects [quiet]]\n";
368 my $packed = pack("IL", $::client_id, $prealloc);
369 if (!defined($arg) || scalar($arg) < 2) {
370 print "Creating 1 object...\n";
371 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
372 if (!defined($quiet)) {
373 my $ino = unpack("L", $packed);
374 print "Created object #$ino.\n";
379 print "Creating " . scalar($arg) . " objects...\n";
380 for ($i = 0; $i < scalar($arg); $i++) {
381 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
382 my $ino = unpack("L", $packed);
383 if (!($rc eq "0 but true")) {
385 $packed = pack("IL", $::client_id, $prealloc);
386 } elsif (!defined($quiet)) {
387 $packed = pack("IL", $::client_id, $prealloc);
388 print "Created object #$ino.\n";
394 print STDERR "ioctl failed: $!\n";
395 } elsif ($rc eq "0 but true") {
396 print "Finished (success)\n";
398 print "ioctl returned error code $rc.\n";
404 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
407 print STDERR "ioctl failed: $!\n";
408 } elsif ($rc eq "0 but true") {
409 print "Finished (success)\n";
411 print "ioctl returned error code $rc.\n";
416 if (!defined($::client_id)) {
417 print "You must first ``connect''.\n";
423 if (!defined($arg) || scalar($arg) < 1) {
424 print "destroy requires the object number to destroy.\n";
428 print "Destroying object $arg...\n";
429 my $packed = pack("IL", $::client_id, $arg);
430 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
433 print STDERR "ioctl failed: $!\n";
434 } elsif ($rc eq "0 but true") {
435 print "Finished (success)\n";
437 print "ioctl returned error code $rc.\n";
442 if (!defined($::client_id)) {
443 print "You must first ``connect''.\n";
449 if (!defined($inode) || scalar($inode) < 1) {
450 print "invalid arguments; type \"help getattr\" for a synopsis\n";
455 my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
457 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
460 print STDERR "ioctl failed: $!\n";
461 } elsif ($rc eq "0 but true") {
462 my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
463 ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
464 unpack("ISssx2lLLLI", $packed);
466 printf("Inode: %d Mode: %o\n", $inode, $mode);
467 printf("User: %6d Group: %6d Size: %d\n", $uid, $gid, $size);
468 printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
469 printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
470 printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
471 printf("flags: %08x\n", $flags);
472 print "Finished (success)\n";
474 print "ioctl returned error code $rc.\n";
479 if (!defined($::client_id)) {
480 print "You must first ``connect''.\n";
486 my $mode = oct(shift);
500 if (defined($size)) {
501 $valid |= &ATTR_SIZE;
503 if (defined($atime)) {
504 $valid |= &ATTR_ATIME;
506 if (defined($mtime)) {
507 $valid |= &ATTR_MTIME;
509 if (defined($ctime)) {
510 $valid |= &ATTR_CTIME;
512 if (defined($mode)) {
513 $valid |= &ATTR_MODE;
516 if (!defined($inode) || scalar($inode) < 1) {
517 print "invalid arguments; type \"help setattr\" for a synopsis\n";
522 # unsigned int ia_valid; (32)
523 # umode_t ia_mode; (16)
526 # -- 16 bit alignment here! --
527 # off_t ia_size; (32)
528 # time_t ia_atime; (32)
529 # time_t ia_mtime; (32)
530 # time_t ia_ctime; (32)
531 # unsigned int ia_attr_flags; (32)
534 printf "valid is %x, mode is %o\n", $valid, $mode;
535 my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
536 $uid, $gid, $size, $atime, $mtime, $ctime, 0);
537 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
540 print STDERR "ioctl failed: $!\n";
541 } elsif ($rc eq "0 but true") {
542 print "Finished (success)\n";
544 print "ioctl returned error code $rc.\n";
549 if (!defined($::client_id)) {
550 print "You must first ``connect''.\n";
558 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
559 $count < 1 || (defined($offset) && $offset < 0)) {
560 print "invalid arguments; type \"help read\" for a synopsis\n";
564 if (!defined($offset)) {
568 print("Reading $count bytes starting at byte $offset from object " .
571 # "allocate" a large enough buffer
572 my $buf = sprintf("%${count}s", " ");
573 die "suck" if (length($buf) != $count);
575 # the perl we're using doesn't support pack type Q, and offset is 64 bits
576 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
578 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
580 $retval = unpack("l", $packed);
583 print STDERR "ioctl failed: $!\n";
584 } elsif ($rc eq "0 but true") {
586 print substr($buf, 0, $retval);
587 print "\nRead $retval of an attempted $count bytes.\n";
588 print "Finished (success)\n";
590 print "Finished (error $retval)\n";
593 print "ioctl returned error code $rc.\n";
598 if (!defined($::client_id)) {
599 print "You must first ``connect''.\n";
607 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
608 $count < 1 || (defined($offset) && $offset < 0)) {
609 print "invalid arguments; type \"help read\" for a synopsis\n";
613 if (!defined($offset)) {
617 print("Reading $count bytes starting at byte $offset from object " .
620 # "allocate" a large enough buffer
621 my $buf = sprintf("%${count}s", " ");
622 die "suck" if (length($buf) != $count);
624 # the perl we're using doesn't support pack type Q, and offset is 64 bits
625 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
627 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed);
629 $retval = unpack("l", $packed);
632 print STDERR "ioctl failed: $!\n";
633 } elsif ($rc eq "0 but true") {
635 print substr($buf, 0, $retval);
636 print "\nRead $retval of an attempted $count bytes.\n";
637 print "Finished (success)\n";
639 print "Finished (error $retval)\n";
642 print "ioctl returned error code $rc.\n";
647 if (!defined($::client_id)) {
648 print "You must first ``connect''.\n";
654 my $text = join(' ', @_);
655 my $count = length($text);
657 if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
658 scalar($offset) < 0) {
659 print "invalid arguments; type \"help write\" for a synopsis\n";
663 if (!defined($text)) {
668 print("Writing $count bytes starting at byte $offset to object " .
671 # the perl we're using doesn't support pack type Q
672 my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, $offset, 0);
673 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
675 $retval = unpack("l", $packed);
678 print STDERR "ioctl failed: $!\n";
679 } elsif ($rc eq "0 but true") {
681 print "\nWrote $retval of an attempted $count bytes.\n";
682 print "Finished (success)\n";
684 print "Finished (error $retval)\n";
687 print "ioctl returned error code $rc.\n";
694 if (!defined($::client_id)) {
695 print "You must first ``connect''.\n";
699 if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
703 print "Preallocating $arg inodes...\n";
704 my $packed = pack("LLx128", $::client_id, $arg);
705 # client id, alloc, inodes[32]
707 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
710 print STDERR "ioctl failed: $!\n";
711 } elsif ($rc eq "0 but true") {
712 my $alloc = unpack("x4L", $packed);
713 my @inodes = unpack("x8L32", $packed);
716 print "Got $alloc inodes: ";
717 foreach $i (@inodes) {
720 print "\nFinished (success)\n";
722 print "ioctl returned error code $rc.\n";
727 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
730 print STDERR "ioctl failed: $!\n";
731 } elsif ($rc eq "0 but true") {
732 print "Finished (success)\n";
734 print "ioctl returned error code $rc.\n";
739 if (!defined($::client_id)) {
740 print "You must first ``connect''.\n";
752 # __kernel_fsid_t f_fsid; (64 bits)
757 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
760 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
763 print STDERR "ioctl failed: $!\n";
764 } elsif ($rc eq "0 but true") {
765 # skip both the conn_id and the fs_type in the buffer
766 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
767 unpack("x4x4LLLLLL", $packed);
768 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
769 "$bfree free ($bavail available).\n");
770 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
771 print "Finished (success)\n";
773 print "ioctl returned error code $rc.\n";
780 if ( !$arg || !$commands{$arg} ) {
781 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
783 print "Usage: " . $commands{$arg}->{doc} . "\n";
789 print "Disconnecting active session ($::client_id)...";
790 Disconnect($::client_id);