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_READ2 () { &_IOC(3, ord(\'f\'), 17, 4);}' unless
41 defined(&OBD_IOC_READ2);
42 eval 'sub OBD_IOC_WRITE () { &_IOC(3, ord(\'f\'), 12, 4);}' unless
43 defined(&OBD_IOC_WRITE);
44 eval 'sub OBD_IOC_CONNECT () { &_IOC(2, ord(\'f\'), 13, 4);}' unless
45 defined(&OBD_IOC_CONNECT);
46 eval 'sub OBD_IOC_DISCONNECT () { &_IOC(1, ord(\'f\'), 14, 4);}' unless
47 defined(&OBD_IOC_DISCONNECT);
48 eval 'sub OBD_IOC_STATFS () { &_IOC(3, ord(\'f\'), 15, 4);}' unless
49 defined(&OBD_IOC_STATFS);
50 eval 'sub OBD_IOC_SYNC () { &_IOC(2, ord(\'f\'), 16, 4);}' unless
51 defined(&OBD_IOC_SYNC);
53 eval 'sub ATTR_MODE () {1;}' unless defined(&ATTR_MODE);
54 eval 'sub ATTR_UID () {2;}' unless defined(&ATTR_UID);
55 eval 'sub ATTR_GID () {4;}' unless defined(&ATTR_GID);
56 eval 'sub ATTR_SIZE () {8;}' unless defined(&ATTR_SIZE);
57 eval 'sub ATTR_ATIME () {16;}' unless defined(&ATTR_ATIME);
58 eval 'sub ATTR_MTIME () {32;}' unless defined(&ATTR_MTIME);
59 eval 'sub ATTR_CTIME () {64;}' unless defined(&ATTR_CTIME);
68 my ($device, $filesystem);
69 # startup options (I'll replace these when I have some to replace with)
70 GetOptions("device=s" => \$device, "fs=s" => $filesystem) || die "Getoptions";
72 # genuine new simulated OBD device
73 $device = "/dev/obd" unless $device;
74 # object store in the ext2 formatted block device
75 $filesystem = "/dev/loop0" unless $filesystem;
77 # get a console for the app
78 my $term = new Term::ReadLine 'obdcontrol ';
79 my $attribs = $term->Attribs;
80 $term->ornaments('md,me,,'); # bold face prompt
82 # make sure stdout is not buffered
90 ('create' => {func => "Create", doc => "create: creates a new inode"},
91 'setup' => {func => "Setup", doc => "setup: link the ext2 partition (default /dev/loop0) to this obddev"},
92 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
93 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
94 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
95 'destroy' => {func => "Destroy", doc => "setup: destroys an inode"},
96 'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
97 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
98 'read' => {func => "Read", doc => "read <inode> <count> [offset]"},
99 'fsread' => {func => "Read2", doc => "read <inode> <count> [offset]"},
100 'write' => {func => "Write", doc => "write <inode> <offset> <text>"},
101 'setattr' => {func => "Setattr", doc => "setattr <inode> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"},
102 'getattr' => {func => "Getattr", doc => "getattr <inode>: displays inode object attributes"},
103 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num inodes."},
104 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
105 'help' => {func => \&Help, doc => "help: this message"},
106 'quit' => {func => \&Quit, doc => "see \"exit\""},
107 'exit' => {func => \&Quit, doc => "see \"quit\""}
111 # setup completion function
113 my @jcm_cmd_list = keys %commands;
115 $attribs->{attempted_completion_function} = \&completeme;
116 #------------------------------------------------------------------------------
117 # Open the device, as we need an FD for the ioctl
118 sysopen(DEV_OBD, $device, 0) || die "Cannot open $device";
120 if (!defined($::st = stat($filesystem))) {
121 die "Unable to stat $filesystem.\n";
124 # Get on with the show
127 #------------------------------------------------------------------------------
129 my ($text, $line, $start, $end) = @_;
130 if (substr($line, 0, $start) =~ /^\s*$/) {
131 $attribs->{completion_word} = \@jcm_cmd_list;
132 return $term->completion_matches($text,
133 $attribs->{'list_completion_function'});
140 my @completions = completeme($given, $given, 0, length($given));
141 if ($#completions == 0) {
142 $name = shift @completions;
148 # start making requests
151 $line = $term->readline("obdcontrol > ");
159 my @arg = split(' ', $line);
160 my $word = shift @arg;
162 my $cmd = find_command($word);
164 printf STDERR "$word: No such command, or not unique.\n";
168 if ($cmd eq "help" || $cmd eq "exit" || $cmd eq "quit") {
169 return (&{$commands{$cmd}->{func}}(@arg));
173 return (&{$commands{$cmd}->{func}}(@arg));
179 my $packed = pack("La24", $::st->rdev(), "sim_obd");
180 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP_OBDDEV, $packed);
183 print STDERR "ioctl failed: $!\n";
184 } elsif ($rc eq "0 but true") {
185 print "Finished (success)\n";
187 print "ioctl returned error code $rc.\n";
193 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP_OBDDEV, $err);
196 print STDERR "ioctl failed: $!\n";
197 } elsif ($rc eq "0 but true") {
198 print "Finished (success)\n";
200 print "ioctl returned error code $rc.\n";
212 # unsigned int conn_id;
213 # unsigned long conn_ino;
214 # unsigned long conn_blocksize;
215 # unsigned char conn_blocksize_bits;
217 my $packed = pack("ILLC", $id, $ino, $bs, $bs_b);
218 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
219 ($id, $ino, $bs, $bs_b) = unpack("ILLC", $packed);
222 print STDERR "ioctl failed: $!\n";
223 } elsif ($rc eq "0 but true") {
225 print "Client ID : $id\n";
226 print "Root inode : $ino\n";
227 print "Blocksize : $bs\n";
228 print "Blocksize bits: $bs_b\n";
229 print "Finished (success)\n";
231 print "ioctl returned error code $rc.\n";
243 print "syntax: disconnect [client ID]\n";
244 print "When client ID is not given, the last valid client ID to be returned by a\n";
245 print "connect command this session is used; there is no such ID.\n";
249 my $packed = pack("L", $id);
250 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
253 print STDERR "ioctl failed: $!\n";
254 } elsif ($rc eq "0 but true") {
255 $::client_id = undef;
256 print "Finished (success)\n";
258 print "ioctl returned error code $rc.\n";
267 if (defined($quiet) && !($quiet eq "quiet")) {
268 print "syntax: create [number of objects [quiet]]\n";
272 my $packed = pack("I", $::client_id);
273 if (!defined($arg) || scalar($arg) < 2) {
274 print "Creating 1 object...\n";
275 my $packed = pack("I", 0);
276 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
277 if (!defined($quiet)) {
278 my $ino = unpack("L", $packed);
279 print "Created object #$ino.\n";
284 print "Creating " . scalar($arg) . " objects...\n";
285 for ($i = 0; $i < scalar($arg); $i++) {
286 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
287 my $ino = unpack("L", $packed);
288 if (!($rc eq "0 but true") || $packed == 0) {
290 } elsif (!defined($quiet)) {
291 print "Created object #$ino.\n";
297 print STDERR "ioctl failed: $!\n";
298 } elsif ($rc eq "0 but true") {
299 print "Finished (success)\n";
301 print "ioctl returned error code $rc.\n";
307 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
310 print STDERR "ioctl failed: $!\n";
311 } elsif ($rc eq "0 but true") {
312 print "Finished (success)\n";
314 print "ioctl returned error code $rc.\n";
319 if (!defined($::client_id)) {
320 print "You must first ``connect''.\n";
326 if (!defined($arg) || scalar($arg) < 1) {
327 print "destroy requires the object number to destroy.\n";
331 print "Destroying object $arg...\n";
332 my $packed = pack("IL", $::client_id, $arg);
333 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
336 print STDERR "ioctl failed: $!\n";
337 } elsif ($rc eq "0 but true") {
338 print "Finished (success)\n";
340 print "ioctl returned error code $rc.\n";
345 if (!defined($::client_id)) {
346 print "You must first ``connect''.\n";
352 if (!defined($inode) || scalar($inode) < 1) {
353 print "invalid arguments; type \"help getattr\" for a synopsis\n";
358 my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
360 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
363 print STDERR "ioctl failed: $!\n";
364 } elsif ($rc eq "0 but true") {
365 my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
366 ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
367 unpack("ISssx2lLLLI", $packed);
369 printf("Inode: %d Mode: %o\n", $inode, $mode);
370 printf("User: %6d Group: %6d Size: %d\n", $uid, $gid, $size);
371 printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
372 printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
373 printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
374 printf("flags: %08x\n", $flags);
375 print "Finished (success)\n";
377 print "ioctl returned error code $rc.\n";
382 if (!defined($::client_id)) {
383 print "You must first ``connect''.\n";
389 my $mode = oct(shift);
403 if (defined($size)) {
404 $valid |= &ATTR_SIZE;
406 if (defined($atime)) {
407 $valid |= &ATTR_ATIME;
409 if (defined($mtime)) {
410 $valid |= &ATTR_MTIME;
412 if (defined($ctime)) {
413 $valid |= &ATTR_CTIME;
415 if (defined($mode)) {
416 $valid |= &ATTR_MODE;
419 if (!defined($inode) || scalar($inode) < 1) {
420 print "invalid arguments; type \"help setattr\" for a synopsis\n";
425 # unsigned int ia_valid; (32)
426 # umode_t ia_mode; (16)
429 # -- 16 bit alignment here! --
430 # off_t ia_size; (32)
431 # time_t ia_atime; (32)
432 # time_t ia_mtime; (32)
433 # time_t ia_ctime; (32)
434 # unsigned int ia_attr_flags; (32)
437 printf "valid is %x, mode is %o\n", $valid, $mode;
438 my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
439 $uid, $gid, $size, $atime, $mtime, $ctime, 0);
440 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
443 print STDERR "ioctl failed: $!\n";
444 } elsif ($rc eq "0 but true") {
445 print "Finished (success)\n";
447 print "ioctl returned error code $rc.\n";
452 if (!defined($::client_id)) {
453 print "You must first ``connect''.\n";
461 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
462 $count < 1 || (defined($offset) && $offset < 0)) {
463 print "invalid arguments; type \"help read\" for a synopsis\n";
467 if (!defined($offset)) {
471 print("Reading $count bytes starting at byte $offset from object " .
474 # "allocate" a large enough buffer
475 my $buf = sprintf("%${count}s", " ");
476 die "suck" if (length($buf) != $count);
478 # the perl we're using doesn't support pack type Q, and offset is 64 bits
479 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
481 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
483 $retval = unpack("l", $packed);
486 print STDERR "ioctl failed: $!\n";
487 } elsif ($rc eq "0 but true") {
489 print substr($buf, 0, $retval);
490 print "\nRead $retval of an attempted $count bytes.\n";
491 print "Finished (success)\n";
493 print "Finished (error $retval)\n";
496 print "ioctl returned error code $rc.\n";
501 if (!defined($::client_id)) {
502 print "You must first ``connect''.\n";
510 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
511 $count < 1 || (defined($offset) && $offset < 0)) {
512 print "invalid arguments; type \"help read\" for a synopsis\n";
516 if (!defined($offset)) {
520 print("Reading $count bytes starting at byte $offset from object " .
523 # "allocate" a large enough buffer
524 my $buf = sprintf("%${count}s", " ");
525 die "suck" if (length($buf) != $count);
527 # the perl we're using doesn't support pack type Q, and offset is 64 bits
528 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
530 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed);
532 $retval = unpack("l", $packed);
535 print STDERR "ioctl failed: $!\n";
536 } elsif ($rc eq "0 but true") {
538 print substr($buf, 0, $retval);
539 print "\nRead $retval of an attempted $count bytes.\n";
540 print "Finished (success)\n";
542 print "Finished (error $retval)\n";
545 print "ioctl returned error code $rc.\n";
550 if (!defined($::client_id)) {
551 print "You must first ``connect''.\n";
557 my $text = join(' ', @_);
558 my $count = length($text);
560 if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
561 scalar($offset) < 0) {
562 print "invalid arguments; type \"help write\" for a synopsis\n";
566 if (!defined($text)) {
571 print("Writing $count bytes starting at byte $offset to object " .
574 # the perl we're using doesn't support pack type Q
575 my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, $offset, 0);
576 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
578 $retval = unpack("l", $packed);
581 print STDERR "ioctl failed: $!\n";
582 } elsif ($rc eq "0 but true") {
584 print "\nWrote $retval of an attempted $count bytes.\n";
585 print "Finished (success)\n";
587 print "Finished (error $retval)\n";
590 print "ioctl returned error code $rc.\n";
597 if (!defined($::client_id)) {
598 print "You must first ``connect''.\n";
602 if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
606 print "Preallocating $arg inodes...\n";
607 my $packed = pack("LLx128", $::client_id, $arg);
608 # client id, alloc, inodes[32]
610 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
613 print STDERR "ioctl failed: $!\n";
614 } elsif ($rc eq "0 but true") {
615 my $alloc = unpack("x4L", $packed);
616 my @inodes = unpack("x8L32", $packed);
619 print "Got $alloc inodes: ";
620 foreach $i (@inodes) {
623 print "\nFinished (success)\n";
625 print "ioctl returned error code $rc.\n";
630 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
633 print STDERR "ioctl failed: $!\n";
634 } elsif ($rc eq "0 but true") {
635 print "Finished (success)\n";
637 print "ioctl returned error code $rc.\n";
642 if (!defined($::client_id)) {
643 print "You must first ``connect''.\n";
655 # __kernel_fsid_t f_fsid; (64 bits)
660 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
663 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
666 print STDERR "ioctl failed: $!\n";
667 } elsif ($rc eq "0 but true") {
668 # skip both the conn_id and the fs_type in the buffer
669 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
670 unpack("x4x4LLLLLL", $packed);
671 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
672 "$bfree free ($bavail available).\n");
673 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
674 print "Finished (success)\n";
676 print "ioctl returned error code $rc.\n";
683 if ( !$arg || !$commands{$arg} ) {
684 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
686 print "Usage: " . $commands{$arg}->{doc} . "\n";
692 print "Disconnecting active session ($::client_id)...";
693 Disconnect($::client_id);