4 BEGIN { require "asm/errno.ph" };
5 BEGIN { require "asm/ioctl.ph" };
7 # p2ph generated invalid macros for ioctl stuff, so I override some of it here
8 eval 'sub OBD_IOC_CREATE () { &_IOC(2, ord(\'f\'), 3, 4);}' unless
9 defined(&OBD_IOC_CREATE);
10 eval 'sub OBD_IOC_SETUP () { &_IOC(1, ord(\'f\'), 4, 4);}' unless
11 defined(&OBD_IOC_SETUP);
12 eval 'sub OBD_IOC_SYNC () { &_IOC(2, ord(\'f\'), 5, 4);}' unless
13 defined(&OBD_IOC_SYNC);
14 eval 'sub OBD_IOC_DESTROY () { &_IOC(1, ord(\'f\'), 6, 4);}' unless
15 defined(&OBD_IOC_DESTROY);
16 eval 'sub OBD_IOC_PREALLOCATE () { &_IOC(3, ord(\'f\'), 7, 4);}' unless
17 defined(&OBD_IOC_PREALLOCATE);
18 eval 'sub OBD_IOC_DEC_USE_COUNT () { &_IOC(0, ord(\'f\'), 8, 0);}' unless
19 defined(&OBD_IOC_DEC_USE_COUNT);
20 eval 'sub OBD_IOC_SETATTR () { &_IOC(1, ord(\'f\'), 9, 4);}' unless
21 defined(&OBD_IOC_SETATTR);
22 eval 'sub OBD_IOC_GETATTR () { &_IOC(2, ord(\'f\'), 10, 4);}' unless
23 defined(&OBD_IOC_GETATTR);
24 eval 'sub OBD_IOC_READ () { &_IOC(3, ord(\'f\'), 11, 4);}' unless
25 defined(&OBD_IOC_READ);
26 eval 'sub OBD_IOC_WRITE () { &_IOC(3, ord(\'f\'), 12, 4);}' unless
27 defined(&OBD_IOC_WRITE);
28 eval 'sub OBD_IOC_CONNECT () { &_IOC(2, ord(\'f\'), 13, 4);}' unless
29 defined(&OBD_IOC_CONNECT);
30 eval 'sub OBD_IOC_DISCONNECT () { &_IOC(1, ord(\'f\'), 14, 4);}' unless
31 defined(&OBD_IOC_DISCONNECT);
32 eval 'sub OBD_IOC_STATFS () { &_IOC(3, ord(\'f\'), 15, 4);}' unless
33 defined(&OBD_IOC_STATFS);
35 eval 'sub ATTR_MODE () {1;}' unless defined(&ATTR_MODE);
36 eval 'sub ATTR_UID () {2;}' unless defined(&ATTR_UID);
37 eval 'sub ATTR_GID () {4;}' unless defined(&ATTR_GID);
38 eval 'sub ATTR_SIZE () {8;}' unless defined(&ATTR_SIZE);
39 eval 'sub ATTR_ATIME () {16;}' unless defined(&ATTR_ATIME);
40 eval 'sub ATTR_MTIME () {32;}' unless defined(&ATTR_MTIME);
41 eval 'sub ATTR_CTIME () {64;}' unless defined(&ATTR_CTIME);
50 my ($device, $filesystem);
51 # startup options (I'll replace these when I have some to replace with)
52 GetOptions("device=s" => \$device, "fs=s" => $filesystem) || die "Getoptions";
54 $device = "/dev/obd" unless $device;
55 $filesystem = "/dev/loop0" unless $filesystem;
57 # get a console for the app
58 my $term = new Term::ReadLine 'obdcontrol ';
59 my $attribs = $term->Attribs;
60 $term->ornaments('md,me,,'); # bold face prompt
62 # make sure stdout is not buffered
70 ('create' => {func => "Create", doc => "create: creates a new inode"},
71 'setup' => {func => "Setup", doc => "setup: initializes the environment"},
72 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
73 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
74 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
75 'destroy' => {func => "Destroy", doc => "setup: destroys an inode"},
76 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
77 'read' => {func => "Read", doc => "read <inode> <count> [offset]"},
78 'write' => {func => "Write", doc => "write <inode> <offset> <text>"},
79 'setattr' => {func => "Setattr", doc => "setattr [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"},
80 'getattr' => {func => "Getattr", doc => "getattr [inode]: displays inode object attributes"},
81 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num inodes."},
82 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
83 'help' => {func => \&Help, doc => "help: this message"},
84 'quit' => {func => \&Quit, doc => "see \"exit\""},
85 'exit' => {func => \&Quit, doc => "see \"quit\""}
89 # setup completion function
91 my @jcm_cmd_list = keys %commands;
93 $attribs->{attempted_completion_function} = \&completeme;
94 #------------------------------------------------------------------------------
95 # Open the device, as we need an FD for the ioctl
96 sysopen(DEV_OBD, $device, 0);
98 if (!defined($::st = stat($filesystem))) {
99 die "Unable to stat $filesystem.\n";
102 # Get on with the show
105 #------------------------------------------------------------------------------
107 my ($text, $line, $start, $end) = @_;
108 if (substr($line, 0, $start) =~ /^\s*$/) {
109 $attribs->{completion_word} = \@jcm_cmd_list;
110 return $term->completion_matches($text,
111 $attribs->{'list_completion_function'});
118 my @completions = completeme($given, $given, 0, length($given));
119 if ($#completions == 0) {
120 $name = shift @completions;
126 # start making requests
129 $line = $term->readline("obdcontrol > ");
137 my @arg = split(' ', $line);
138 my $word = shift @arg;
140 my $cmd = find_command($word);
142 printf STDERR "$word: No such command, or not unique.\n";
146 if ($cmd eq "help" || $cmd eq "exit" || $cmd eq "quit") {
147 return (&{$commands{$cmd}->{func}}(@arg));
151 return (&{$commands{$cmd}->{func}}(@arg));
156 my $packed = pack("L", $::st->rdev());
157 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP, $packed);
160 print STDERR "ioctl failed: $!\n";
161 } elsif ($rc eq "0 but true") {
162 print "Finished (success)\n";
164 print "ioctl returned error code $rc.\n";
175 # unsigned int conn_id;
176 # unsigned long conn_ino;
177 # unsigned long conn_blocksize;
178 # unsigned char conn_blocksize_bits;
180 my $packed = pack("ILLC", $id, $ino, $bs, $bs_b);
181 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
182 ($id, $ino, $bs, $bs_b) = unpack("ILLC", $packed);
185 print STDERR "ioctl failed: $!\n";
186 } elsif ($rc eq "0 but true") {
188 print "Client ID : $id\n";
189 print "Root inode : $ino\n";
190 print "Blocksize : $bs\n";
191 print "Blocksize bits: $bs_b\n";
192 print "Finished (success)\n";
194 print "ioctl returned error code $rc.\n";
206 print "syntax: disconnect [client ID]\n";
207 print "When client ID is not given, the last valid client ID to be returned by a\n";
208 print "connect command this session is used; there is no such ID.\n";
212 my $packed = pack("L", $id);
213 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
216 print STDERR "ioctl failed: $!\n";
217 } elsif ($rc eq "0 but true") {
218 $::client_id = undef;
219 print "Finished (success)\n";
221 print "ioctl returned error code $rc.\n";
230 if (defined($quiet) && !($quiet eq "quiet")) {
231 print "syntax: create [number of objects [quiet]]\n";
235 my $packed = pack("I", $::client_id);
236 if (!defined($arg) || scalar($arg) < 2) {
237 print "Creating 1 object...\n";
238 my $packed = pack("I", 0);
239 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
240 if (!defined($quiet)) {
241 my $ino = unpack("L", $packed);
242 print "Created object #$ino.\n";
247 print "Creating " . scalar($arg) . " objects...\n";
248 for ($i = 0; $i < scalar($arg); $i++) {
249 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
250 my $ino = unpack("L", $packed);
251 if (!($rc eq "0 but true") || $packed == 0) {
253 } elsif (!defined($quiet)) {
254 print "Created object #$ino.\n";
260 print STDERR "ioctl failed: $!\n";
261 } elsif ($rc eq "0 but true") {
262 print "Finished (success)\n";
264 print "ioctl returned error code $rc.\n";
270 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
273 print STDERR "ioctl failed: $!\n";
274 } elsif ($rc eq "0 but true") {
275 print "Finished (success)\n";
277 print "ioctl returned error code $rc.\n";
288 if (!defined($arg) || scalar($arg) < 1) {
289 print "destroy requires the object number to destroy.\n";
293 print "Destroying object $arg...\n";
294 my $packed = pack("IL", $::client_id, $arg);
295 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
298 print STDERR "ioctl failed: $!\n";
299 } elsif ($rc eq "0 but true") {
300 print "Finished (success)\n";
302 print "ioctl returned error code $rc.\n";
307 if (!defined($::client_id)) {
308 print "You must first ``connect''.\n";
314 if (!defined($inode) || scalar($inode) < 1) {
315 print "invalid arguments; type \"help getattr\" for a synopsis\n";
320 my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
322 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
325 print STDERR "ioctl failed: $!\n";
326 } elsif ($rc eq "0 but true") {
327 my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
328 ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
329 unpack("ISssx2lLLLI", $packed);
331 printf("Inode: %d Mode: %04d\n", $inode, $mode);
332 printf("User: %6d Group: %6d Size: %d\n", $uid, $gid, $size);
333 printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
334 printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
335 printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
336 printf("flags: %08x\n", $flags);
337 print "Finished (success)\n";
339 print "ioctl returned error code $rc.\n";
344 if (!defined($::client_id)) {
345 print "You must first ``connect''.\n";
365 if (defined($size)) {
366 $valid |= &ATTR_SIZE;
368 if (defined($atime)) {
369 $valid |= &ATTR_ATIME;
371 if (defined($mtime)) {
372 $valid |= &ATTR_MTIME;
374 if (defined($ctime)) {
375 $valid |= &ATTR_CTIME;
377 if (defined($mode)) {
378 $valid |= &ATTR_MODE;
381 if (!defined($inode) || scalar($inode) < 1) {
382 print "invalid arguments; type \"help setattr\" for a synopsis\n";
387 # unsigned int ia_valid; (32)
388 # umode_t ia_mode; (16)
391 # -- 16 bit alignment here! --
392 # off_t ia_size; (32)
393 # time_t ia_atime; (32)
394 # time_t ia_mtime; (32)
395 # time_t ia_ctime; (32)
396 # unsigned int ia_attr_flags; (32)
398 my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
399 $uid, $gid, $size, $atime, $mtime, $ctime, 0);
400 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
403 print STDERR "ioctl failed: $!\n";
404 } elsif ($rc eq "0 but true") {
405 print "Finished (success)\n";
407 print "ioctl returned error code $rc.\n";
412 if (!defined($::client_id)) {
413 print "You must first ``connect''.\n";
421 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
422 $count < 1 || (defined($offset) && $offset < 0)) {
423 print "invalid arguments; type \"help read\" for a synopsis\n";
427 if (!defined($offset)) {
431 print("Reading $count bytes starting at byte $offset from object " .
434 # "allocate" a large enough buffer
435 my $buf = sprintf("%${count}s", " ");
436 die "suck" if (length($buf) != $count);
438 # the perl we're using doesn't support pack type Q, and offset is 64 bits
439 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, 0, $offset);
441 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
443 $retval = unpack("L", $packed);
446 print STDERR "ioctl failed: $!\n";
447 } elsif ($rc eq "0 but true") {
449 print substr($buf, 0, $retval);
450 print "\nRead $retval of an attempted $count bytes.\n";
451 print "Finished (success)\n";
453 print "Finished (error $retval)\n";
456 print "ioctl returned error code $rc.\n";
461 if (!defined($::client_id)) {
462 print "You must first ``connect''.\n";
468 my $text = join(' ', @_);
469 my $count = length($text);
471 if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
472 scalar($offset) < 0) {
473 print "invalid arguments; type \"help write\" for a synopsis\n";
477 if (!defined($text)) {
482 print("Writing $count bytes starting at byte $offset to object " .
485 # the perl we're using doesn't support pack type Q
486 my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, 0, $offset);
487 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
489 $retval = unpack("L", $packed);
492 print STDERR "ioctl failed: $!\n";
493 } elsif ($rc eq "0 but true") {
495 print "\nWrote $retval of an attempted $count bytes.\n";
496 print "Finished (success)\n";
498 print "Finished (error $retval)\n";
501 print "ioctl returned error code $rc.\n";
508 if (!defined($::client_id)) {
509 print "You must first ``connect''.\n";
513 if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
517 print "Preallocating $arg inodes...\n";
518 my $packed = pack("LLx128", $::client_id, $arg);
519 # client id, alloc, inodes[32]
521 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
524 print STDERR "ioctl failed: $!\n";
525 } elsif ($rc eq "0 but true") {
526 my $alloc = unpack("x4L", $packed);
527 my @inodes = unpack("x8L32", $packed);
530 print "Got $alloc inodes: ";
531 foreach $i (@inodes) {
534 print "\nFinished (success)\n";
536 print "ioctl returned error code $rc.\n";
541 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
544 print STDERR "ioctl failed: $!\n";
545 } elsif ($rc eq "0 but true") {
546 print "Finished (success)\n";
548 print "ioctl returned error code $rc.\n";
553 if (!defined($::client_id)) {
554 print "You must first ``connect''.\n";
566 # __kernel_fsid_t f_fsid; (64 bits)
571 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
574 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
577 print STDERR "ioctl failed: $!\n";
578 } elsif ($rc eq "0 but true") {
579 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
580 unpack("x4LLLLLL", $packed);
581 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
582 "$bfree free ($bavail available).\n");
583 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
584 print "Finished (success)\n";
586 print "ioctl returned error code $rc.\n";
593 if ( !$arg || !$commands{$arg} ) {
594 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
596 print "Usage: " . $commands{$arg}->{doc} . "\n";
602 print "Disconnecting active session ($::client_id)...";
603 Disconnect($::client_id);