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\'), 5, 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));
175 my $packed = pack("L", $::st->rdev());
176 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP_SUPER, $packed);
179 print STDERR "ioctl failed: $!\n";
180 } elsif ($rc eq "0 but true") {
181 print "Finished (success)\n";
183 print "ioctl returned error code $rc.\n";
189 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP_SUPER, $err);
192 print STDERR "ioctl failed: $!\n";
193 } elsif ($rc eq "0 but true") {
194 print "Finished (success)\n";
196 print "ioctl returned error code $rc.\n";
208 # unsigned int conn_id;
209 # unsigned long conn_ino;
210 # unsigned long conn_blocksize;
211 # unsigned char conn_blocksize_bits;
213 my $packed = pack("ILLC", $id, $ino, $bs, $bs_b);
214 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
215 ($id, $ino, $bs, $bs_b) = unpack("ILLC", $packed);
218 print STDERR "ioctl failed: $!\n";
219 } elsif ($rc eq "0 but true") {
221 print "Client ID : $id\n";
222 print "Root inode : $ino\n";
223 print "Blocksize : $bs\n";
224 print "Blocksize bits: $bs_b\n";
225 print "Finished (success)\n";
227 print "ioctl returned error code $rc.\n";
239 print "syntax: disconnect [client ID]\n";
240 print "When client ID is not given, the last valid client ID to be returned by a\n";
241 print "connect command this session is used; there is no such ID.\n";
245 my $packed = pack("L", $id);
246 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
249 print STDERR "ioctl failed: $!\n";
250 } elsif ($rc eq "0 but true") {
251 $::client_id = undef;
252 print "Finished (success)\n";
254 print "ioctl returned error code $rc.\n";
263 if (defined($quiet) && !($quiet eq "quiet")) {
264 print "syntax: create [number of objects [quiet]]\n";
268 my $packed = pack("I", $::client_id);
269 if (!defined($arg) || scalar($arg) < 2) {
270 print "Creating 1 object...\n";
271 my $packed = pack("I", 0);
272 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
273 if (!defined($quiet)) {
274 my $ino = unpack("L", $packed);
275 print "Created object #$ino.\n";
280 print "Creating " . scalar($arg) . " objects...\n";
281 for ($i = 0; $i < scalar($arg); $i++) {
282 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
283 my $ino = unpack("L", $packed);
284 if (!($rc eq "0 but true") || $packed == 0) {
286 } elsif (!defined($quiet)) {
287 print "Created object #$ino.\n";
293 print STDERR "ioctl failed: $!\n";
294 } elsif ($rc eq "0 but true") {
295 print "Finished (success)\n";
297 print "ioctl returned error code $rc.\n";
303 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
306 print STDERR "ioctl failed: $!\n";
307 } elsif ($rc eq "0 but true") {
308 print "Finished (success)\n";
310 print "ioctl returned error code $rc.\n";
315 if (!defined($::client_id)) {
316 print "You must first ``connect''.\n";
322 if (!defined($arg) || scalar($arg) < 1) {
323 print "destroy requires the object number to destroy.\n";
327 print "Destroying object $arg...\n";
328 my $packed = pack("IL", $::client_id, $arg);
329 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
332 print STDERR "ioctl failed: $!\n";
333 } elsif ($rc eq "0 but true") {
334 print "Finished (success)\n";
336 print "ioctl returned error code $rc.\n";
341 if (!defined($::client_id)) {
342 print "You must first ``connect''.\n";
348 if (!defined($inode) || scalar($inode) < 1) {
349 print "invalid arguments; type \"help getattr\" for a synopsis\n";
354 my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
356 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
359 print STDERR "ioctl failed: $!\n";
360 } elsif ($rc eq "0 but true") {
361 my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
362 ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
363 unpack("ISssx2lLLLI", $packed);
365 printf("Inode: %d Mode: %o\n", $inode, $mode);
366 printf("User: %6d Group: %6d Size: %d\n", $uid, $gid, $size);
367 printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
368 printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
369 printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
370 printf("flags: %08x\n", $flags);
371 print "Finished (success)\n";
373 print "ioctl returned error code $rc.\n";
378 if (!defined($::client_id)) {
379 print "You must first ``connect''.\n";
385 my $mode = oct(shift);
399 if (defined($size)) {
400 $valid |= &ATTR_SIZE;
402 if (defined($atime)) {
403 $valid |= &ATTR_ATIME;
405 if (defined($mtime)) {
406 $valid |= &ATTR_MTIME;
408 if (defined($ctime)) {
409 $valid |= &ATTR_CTIME;
411 if (defined($mode)) {
412 $valid |= &ATTR_MODE;
415 if (!defined($inode) || scalar($inode) < 1) {
416 print "invalid arguments; type \"help setattr\" for a synopsis\n";
421 # unsigned int ia_valid; (32)
422 # umode_t ia_mode; (16)
425 # -- 16 bit alignment here! --
426 # off_t ia_size; (32)
427 # time_t ia_atime; (32)
428 # time_t ia_mtime; (32)
429 # time_t ia_ctime; (32)
430 # unsigned int ia_attr_flags; (32)
433 printf "valid is %x, mode is %o\n", $valid, $mode;
434 my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
435 $uid, $gid, $size, $atime, $mtime, $ctime, 0);
436 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
439 print STDERR "ioctl failed: $!\n";
440 } elsif ($rc eq "0 but true") {
441 print "Finished (success)\n";
443 print "ioctl returned error code $rc.\n";
448 if (!defined($::client_id)) {
449 print "You must first ``connect''.\n";
457 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
458 $count < 1 || (defined($offset) && $offset < 0)) {
459 print "invalid arguments; type \"help read\" for a synopsis\n";
463 if (!defined($offset)) {
467 print("Reading $count bytes starting at byte $offset from object " .
470 # "allocate" a large enough buffer
471 my $buf = sprintf("%${count}s", " ");
472 die "suck" if (length($buf) != $count);
474 # the perl we're using doesn't support pack type Q, and offset is 64 bits
475 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, 0, $offset);
477 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
479 $retval = unpack("l", $packed);
482 print STDERR "ioctl failed: $!\n";
483 } elsif ($rc eq "0 but true") {
485 print substr($buf, 0, $retval);
486 print "\nRead $retval of an attempted $count bytes.\n";
487 print "Finished (success)\n";
489 print "Finished (error $retval)\n";
492 print "ioctl returned error code $rc.\n";
497 if (!defined($::client_id)) {
498 print "You must first ``connect''.\n";
504 my $text = join(' ', @_);
505 my $count = length($text);
507 if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
508 scalar($offset) < 0) {
509 print "invalid arguments; type \"help write\" for a synopsis\n";
513 if (!defined($text)) {
518 print("Writing $count bytes starting at byte $offset to object " .
521 # the perl we're using doesn't support pack type Q
522 my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, 0, $offset);
523 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
525 $retval = unpack("l", $packed);
528 print STDERR "ioctl failed: $!\n";
529 } elsif ($rc eq "0 but true") {
531 print "\nWrote $retval of an attempted $count bytes.\n";
532 print "Finished (success)\n";
534 print "Finished (error $retval)\n";
537 print "ioctl returned error code $rc.\n";
544 if (!defined($::client_id)) {
545 print "You must first ``connect''.\n";
549 if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
553 print "Preallocating $arg inodes...\n";
554 my $packed = pack("LLx128", $::client_id, $arg);
555 # client id, alloc, inodes[32]
557 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
560 print STDERR "ioctl failed: $!\n";
561 } elsif ($rc eq "0 but true") {
562 my $alloc = unpack("x4L", $packed);
563 my @inodes = unpack("x8L32", $packed);
566 print "Got $alloc inodes: ";
567 foreach $i (@inodes) {
570 print "\nFinished (success)\n";
572 print "ioctl returned error code $rc.\n";
577 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
580 print STDERR "ioctl failed: $!\n";
581 } elsif ($rc eq "0 but true") {
582 print "Finished (success)\n";
584 print "ioctl returned error code $rc.\n";
589 if (!defined($::client_id)) {
590 print "You must first ``connect''.\n";
602 # __kernel_fsid_t f_fsid; (64 bits)
607 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
610 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
613 print STDERR "ioctl failed: $!\n";
614 } elsif ($rc eq "0 but true") {
615 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
616 unpack("x4LLLLLL", $packed);
617 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
618 "$bfree free ($bavail available).\n");
619 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
620 print "Finished (success)\n";
622 print "ioctl returned error code $rc.\n";
629 if ( !$arg || !$commands{$arg} ) {
630 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
632 print "Usage: " . $commands{$arg}->{doc} . "\n";
638 print "Disconnecting active session ($::client_id)...";
639 Disconnect($::client_id);