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_SUPER () { &_IOC(1, ord(\'f\'), 4, 4);}' unless
25 defined(&OBD_IOC_SETUP_SUPER);
26 eval 'sub OBD_IOC_CLEANUP_SUPER () { &_IOC(0, ord(\'f\'), 5, 0);}' unless
27 defined(&OBD_IOC_CLEANUP_SUPER);
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);
51 eval 'sub ATTR_MODE () {1;}' unless defined(&ATTR_MODE);
52 eval 'sub ATTR_UID () {2;}' unless defined(&ATTR_UID);
53 eval 'sub ATTR_GID () {4;}' unless defined(&ATTR_GID);
54 eval 'sub ATTR_SIZE () {8;}' unless defined(&ATTR_SIZE);
55 eval 'sub ATTR_ATIME () {16;}' unless defined(&ATTR_ATIME);
56 eval 'sub ATTR_MTIME () {32;}' unless defined(&ATTR_MTIME);
57 eval 'sub ATTR_CTIME () {64;}' unless defined(&ATTR_CTIME);
66 my ($device, $filesystem);
67 # startup options (I'll replace these when I have some to replace with)
68 GetOptions("device=s" => \$device, "fs=s" => $filesystem) || die "Getoptions";
70 # genuine new simulated OBD device
71 $device = "/dev/obd" unless $device;
72 # object store in the ext2 formatted block device
73 $filesystem = "/dev/loop0" unless $filesystem;
75 # get a console for the app
76 my $term = new Term::ReadLine 'obdcontrol ';
77 my $attribs = $term->Attribs;
78 $term->ornaments('md,me,,'); # bold face prompt
80 # make sure stdout is not buffered
88 ('create' => {func => "Create", doc => "create: creates a new inode"},
89 'setup' => {func => "Setup", doc => "setup: link the ext2 partition (default /dev/loop0) to this obddev"},
90 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
91 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
92 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
93 'destroy' => {func => "Destroy", doc => "setup: destroys an inode"},
94 'cleanup' => {func => "Cleanup", doc => "detach the superblock from this minor obd dev"},
95 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
96 'read' => {func => "Read", doc => "read <inode> <count> [offset]"},
97 'write' => {func => "Write", doc => "write <inode> <offset> <text>"},
98 'setattr' => {func => "Setattr", doc => "setattr <inode> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"},
99 'getattr' => {func => "Getattr", doc => "getattr <inode>: displays inode object attributes"},
100 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num inodes."},
101 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
102 'help' => {func => \&Help, doc => "help: this message"},
103 'quit' => {func => \&Quit, doc => "see \"exit\""},
104 'exit' => {func => \&Quit, doc => "see \"quit\""}
108 # setup completion function
110 my @jcm_cmd_list = keys %commands;
112 $attribs->{attempted_completion_function} = \&completeme;
113 #------------------------------------------------------------------------------
114 # Open the device, as we need an FD for the ioctl
115 sysopen(DEV_OBD, $device, 0) || die "Cannot open $device";
117 if (!defined($::st = stat($filesystem))) {
118 die "Unable to stat $filesystem.\n";
121 # Get on with the show
124 #------------------------------------------------------------------------------
126 my ($text, $line, $start, $end) = @_;
127 if (substr($line, 0, $start) =~ /^\s*$/) {
128 $attribs->{completion_word} = \@jcm_cmd_list;
129 return $term->completion_matches($text,
130 $attribs->{'list_completion_function'});
137 my @completions = completeme($given, $given, 0, length($given));
138 if ($#completions == 0) {
139 $name = shift @completions;
145 # start making requests
148 $line = $term->readline("obdcontrol > ");
156 my @arg = split(' ', $line);
157 my $word = shift @arg;
159 my $cmd = find_command($word);
161 printf STDERR "$word: No such command, or not unique.\n";
165 if ($cmd eq "help" || $cmd eq "exit" || $cmd eq "quit") {
166 return (&{$commands{$cmd}->{func}}(@arg));
170 return (&{$commands{$cmd}->{func}}(@arg));
176 my $packed = pack("La24", $::st->rdev(), "sim_obd");
177 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP_SUPER, $packed);
180 print STDERR "ioctl failed: $!\n";
181 } elsif ($rc eq "0 but true") {
182 print "Finished (success)\n";
184 print "ioctl returned error code $rc.\n";
190 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP_SUPER, $err);
193 print STDERR "ioctl failed: $!\n";
194 } elsif ($rc eq "0 but true") {
195 print "Finished (success)\n";
197 print "ioctl returned error code $rc.\n";
209 # unsigned int conn_id;
210 # unsigned long conn_ino;
211 # unsigned long conn_blocksize;
212 # unsigned char conn_blocksize_bits;
214 my $packed = pack("ILLC", $id, $ino, $bs, $bs_b);
215 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
216 ($id, $ino, $bs, $bs_b) = unpack("ILLC", $packed);
219 print STDERR "ioctl failed: $!\n";
220 } elsif ($rc eq "0 but true") {
222 print "Client ID : $id\n";
223 print "Root inode : $ino\n";
224 print "Blocksize : $bs\n";
225 print "Blocksize bits: $bs_b\n";
226 print "Finished (success)\n";
228 print "ioctl returned error code $rc.\n";
240 print "syntax: disconnect [client ID]\n";
241 print "When client ID is not given, the last valid client ID to be returned by a\n";
242 print "connect command this session is used; there is no such ID.\n";
246 my $packed = pack("L", $id);
247 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
250 print STDERR "ioctl failed: $!\n";
251 } elsif ($rc eq "0 but true") {
252 $::client_id = undef;
253 print "Finished (success)\n";
255 print "ioctl returned error code $rc.\n";
264 if (defined($quiet) && !($quiet eq "quiet")) {
265 print "syntax: create [number of objects [quiet]]\n";
269 my $packed = pack("I", $::client_id);
270 if (!defined($arg) || scalar($arg) < 2) {
271 print "Creating 1 object...\n";
272 my $packed = pack("I", 0);
273 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
274 if (!defined($quiet)) {
275 my $ino = unpack("L", $packed);
276 print "Created object #$ino.\n";
281 print "Creating " . scalar($arg) . " objects...\n";
282 for ($i = 0; $i < scalar($arg); $i++) {
283 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
284 my $ino = unpack("L", $packed);
285 if (!($rc eq "0 but true") || $packed == 0) {
287 } elsif (!defined($quiet)) {
288 print "Created object #$ino.\n";
294 print STDERR "ioctl failed: $!\n";
295 } elsif ($rc eq "0 but true") {
296 print "Finished (success)\n";
298 print "ioctl returned error code $rc.\n";
304 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
307 print STDERR "ioctl failed: $!\n";
308 } elsif ($rc eq "0 but true") {
309 print "Finished (success)\n";
311 print "ioctl returned error code $rc.\n";
316 if (!defined($::client_id)) {
317 print "You must first ``connect''.\n";
323 if (!defined($arg) || scalar($arg) < 1) {
324 print "destroy requires the object number to destroy.\n";
328 print "Destroying object $arg...\n";
329 my $packed = pack("IL", $::client_id, $arg);
330 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
333 print STDERR "ioctl failed: $!\n";
334 } elsif ($rc eq "0 but true") {
335 print "Finished (success)\n";
337 print "ioctl returned error code $rc.\n";
342 if (!defined($::client_id)) {
343 print "You must first ``connect''.\n";
349 if (!defined($inode) || scalar($inode) < 1) {
350 print "invalid arguments; type \"help getattr\" for a synopsis\n";
355 my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
357 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
360 print STDERR "ioctl failed: $!\n";
361 } elsif ($rc eq "0 but true") {
362 my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
363 ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
364 unpack("ISssx2lLLLI", $packed);
366 printf("Inode: %d Mode: %o\n", $inode, $mode);
367 printf("User: %6d Group: %6d Size: %d\n", $uid, $gid, $size);
368 printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
369 printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
370 printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
371 printf("flags: %08x\n", $flags);
372 print "Finished (success)\n";
374 print "ioctl returned error code $rc.\n";
379 if (!defined($::client_id)) {
380 print "You must first ``connect''.\n";
386 my $mode = oct(shift);
400 if (defined($size)) {
401 $valid |= &ATTR_SIZE;
403 if (defined($atime)) {
404 $valid |= &ATTR_ATIME;
406 if (defined($mtime)) {
407 $valid |= &ATTR_MTIME;
409 if (defined($ctime)) {
410 $valid |= &ATTR_CTIME;
412 if (defined($mode)) {
413 $valid |= &ATTR_MODE;
416 if (!defined($inode) || scalar($inode) < 1) {
417 print "invalid arguments; type \"help setattr\" for a synopsis\n";
422 # unsigned int ia_valid; (32)
423 # umode_t ia_mode; (16)
426 # -- 16 bit alignment here! --
427 # off_t ia_size; (32)
428 # time_t ia_atime; (32)
429 # time_t ia_mtime; (32)
430 # time_t ia_ctime; (32)
431 # unsigned int ia_attr_flags; (32)
434 printf "valid is %x, mode is %o\n", $valid, $mode;
435 my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
436 $uid, $gid, $size, $atime, $mtime, $ctime, 0);
437 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
440 print STDERR "ioctl failed: $!\n";
441 } elsif ($rc eq "0 but true") {
442 print "Finished (success)\n";
444 print "ioctl returned error code $rc.\n";
449 if (!defined($::client_id)) {
450 print "You must first ``connect''.\n";
458 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
459 $count < 1 || (defined($offset) && $offset < 0)) {
460 print "invalid arguments; type \"help read\" for a synopsis\n";
464 if (!defined($offset)) {
468 print("Reading $count bytes starting at byte $offset from object " .
471 # "allocate" a large enough buffer
472 my $buf = sprintf("%${count}s", " ");
473 die "suck" if (length($buf) != $count);
475 # the perl we're using doesn't support pack type Q, and offset is 64 bits
476 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
478 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
480 $retval = unpack("l", $packed);
483 print STDERR "ioctl failed: $!\n";
484 } elsif ($rc eq "0 but true") {
486 print substr($buf, 0, $retval);
487 print "\nRead $retval of an attempted $count bytes.\n";
488 print "Finished (success)\n";
490 print "Finished (error $retval)\n";
493 print "ioctl returned error code $rc.\n";
498 if (!defined($::client_id)) {
499 print "You must first ``connect''.\n";
505 my $text = join(' ', @_);
506 my $count = length($text);
508 if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
509 scalar($offset) < 0) {
510 print "invalid arguments; type \"help write\" for a synopsis\n";
514 if (!defined($text)) {
519 print("Writing $count bytes starting at byte $offset to object " .
522 # the perl we're using doesn't support pack type Q
523 my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, $offset, 0);
524 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
526 $retval = unpack("l", $packed);
529 print STDERR "ioctl failed: $!\n";
530 } elsif ($rc eq "0 but true") {
532 print "\nWrote $retval of an attempted $count bytes.\n";
533 print "Finished (success)\n";
535 print "Finished (error $retval)\n";
538 print "ioctl returned error code $rc.\n";
545 if (!defined($::client_id)) {
546 print "You must first ``connect''.\n";
550 if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
554 print "Preallocating $arg inodes...\n";
555 my $packed = pack("LLx128", $::client_id, $arg);
556 # client id, alloc, inodes[32]
558 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
561 print STDERR "ioctl failed: $!\n";
562 } elsif ($rc eq "0 but true") {
563 my $alloc = unpack("x4L", $packed);
564 my @inodes = unpack("x8L32", $packed);
567 print "Got $alloc inodes: ";
568 foreach $i (@inodes) {
571 print "\nFinished (success)\n";
573 print "ioctl returned error code $rc.\n";
578 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
581 print STDERR "ioctl failed: $!\n";
582 } elsif ($rc eq "0 but true") {
583 print "Finished (success)\n";
585 print "ioctl returned error code $rc.\n";
590 if (!defined($::client_id)) {
591 print "You must first ``connect''.\n";
603 # __kernel_fsid_t f_fsid; (64 bits)
608 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
611 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
614 print STDERR "ioctl failed: $!\n";
615 } elsif ($rc eq "0 but true") {
616 # skip both the conn_id and the fs_type in the buffer
617 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
618 unpack("x4x4LLLLLL", $packed);
619 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
620 "$bfree free ($bavail available).\n");
621 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
622 print "Finished (success)\n";
624 print "ioctl returned error code $rc.\n";
631 if ( !$arg || !$commands{$arg} ) {
632 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
634 print "Usage: " . $commands{$arg}->{doc} . "\n";
640 print "Disconnecting active session ($::client_id)...";
641 Disconnect($::client_id);