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 () { &_IOC(1, ord(\'f\'), 4, 4);}' unless
25 defined(&OBD_IOC_SETUP);
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);
58 eval 'sub OBD_IOC_DETACH () { &_IOC(3, ord(\'f\'), 21, 4);}' unless
59 defined(&OBD_IOC_DETACH);
60 eval 'sub OBD_IOC_COPY () { &_IOC(3, ord(\'f\'), 22, 4);}' unless
61 defined(&OBD_IOC_COPY);
62 eval 'sub OBD_IOC_MIGR () { &_IOC(3, ord(\'f\'), 23, 4);}' unless
63 defined(&OBD_IOC_MIGR);
64 eval 'sub OBD_SNAP_SETTABLE () { &_IOC(3, ord(\'f\'), 40, 4);}' unless
65 defined(&OBD_SNAP_SETTABLE);
66 eval 'sub OBD_SNAP_PRINTTABLE () { &_IOC(3, ord(\'f\'), 41, 4);}' unless
67 defined(&OBD_SNAP_PRINTTABLE);
68 eval 'sub OBD_SNAP_DELETE() { &_IOC(3, ord(\'f\'), 42, 4);}' unless
69 defined(&OBD_SNAP_DELETE);
70 eval 'sub OBD_SNAP_RESTORE() { &_IOC(3, ord(\'f\'), 43, 4);}' unless
71 defined(&OBD_SNAP_RESTORE);
73 eval 'sub OBD_EXT2_RUNIT () { &_IOC(3, ord(\'f\'), 61, 4);}' unless
74 defined(&OBD_EXT2_RUNIT);
76 eval 'sub ATTR_MODE () {1;}' unless defined(&ATTR_MODE);
77 eval 'sub ATTR_UID () {2;}' unless defined(&ATTR_UID);
78 eval 'sub ATTR_GID () {4;}' unless defined(&ATTR_GID);
79 eval 'sub ATTR_SIZE () {8;}' unless defined(&ATTR_SIZE);
80 eval 'sub ATTR_ATIME () {16;}' unless defined(&ATTR_ATIME);
81 eval 'sub ATTR_MTIME () {32;}' unless defined(&ATTR_MTIME);
82 eval 'sub ATTR_CTIME () {64;}' unless defined(&ATTR_CTIME);
94 GetOptions("f!" => \$file, "device=s" => \$::device, ) || die "Getoptions";
97 # get a console for the app
104 ('device' => {func => "Device", doc => "device <dev>: open another OBD device"},
105 'create' => {func => "Create", doc => "create: creates a new inode"},
106 'attach' => {func => "Attach", doc => "attach { ext2_obd | snap_obd snapdev snapidx tableno}"}, # | scsi_obd adapter bus tid lun |
107 'detach' => {func => "Detach", doc => "detach this device"},
108 'testext2iterator' => {func => "TestExt2Iterator", doc => "test ext2 iterator function"},
109 'snapset' => {func => "SnapSetTable", doc => "snapset tableno file" },
110 'snapprint' => {func => "SnapPrint", doc => "snapprint tableno"},
111 'snapdelete' => {func => "SnapDelete", doc => "snapdelete: delete connected snap obd"},
112 'snaprestore' => {func => "SnapRestore", doc => "snaprestore: restore connected old snap to current"},
113 'snaptable' => {func => "SnapShotTable", doc => "snaptable: build a snapshot table (interactive)"},
114 'copy' => {func => "Copy", doc => "copy srcid tgtid"},
115 'migrate' => {func => "Migrate", doc => "migrate srcid tgtid"},
116 'format' => {func => "Format", doc => "format type adapter bus tid lun size"},
117 'partition' => {func => "Partition", doc => "partition type adapter bus tid lun partition size"},
118 'setup' => {func => "Setup", doc => "setup [type]: link this OBD device to the underlying device (default type ext2_obd)"},
119 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
120 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
121 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
122 'destroy' => {func => "Destroy", doc => "destroy <inode>: destroys an inode"},
123 'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
124 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
125 'read' => {func => "Read", doc => "read <inode> <count> [offset]"},
126 'fsread' => {func => "Read2", doc => "read <inode> <count> [offset]"},
127 'write' => {func => "Write", doc => "write <inode> <offset> <text>"},
128 'setattr' => {func => "Setattr", doc => "setattr <inode> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"},
129 'getattr' => {func => "Getattr", doc => "getattr <inode>: displays inode object attributes"},
130 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num inodes."},
131 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
132 'help' => {func => \&Help, doc => "help: this message"},
133 'quit' => {func => \&Quit, doc => "see \"exit\""},
134 'exit' => {func => \&Quit, doc => "see \"quit\""}
138 # setup completion function
140 my @jcm_cmd_list = keys %commands;
155 return $term->readline(@_);
168 $term = new Term::ReadLine 'obdcontrol ';
169 $attribs = $term->Attribs;
170 $attribs->{attempted_completion_function} = \&completeme;
171 $term->ornaments('md,me,,'); # bold face prompt
173 # make sure stdout is not buffered
174 STDOUT->autoflush(1);
177 # Get on with the show
181 #------------------------------------------------------------------------------
183 my ($text, $line, $start, $end) = @_;
184 if (substr($line, 0, $start) =~ /^\s*$/) {
185 $attribs->{completion_word} = \@jcm_cmd_list;
186 return $term->completion_matches($text,
187 $attribs->{'list_completion_function'});
194 my @completions = completeme($given, $given, 0, length($given));
195 if ($#completions == 0) {
196 $name = shift @completions;
202 # start making requests
205 $line = $term->readline("obdcontrol > ");
213 my @arg = split(' ', $line);
214 my $word = shift @arg;
220 $cmd = find_command($word);
223 printf STDERR "$word: No such command, or not unique.\n";
227 if ($cmd eq "help" || $cmd eq "exit" || $cmd eq "quit") {
228 return (&{$commands{$cmd}->{func}}(@arg));
232 return (&{$commands{$cmd}->{func}}(@arg));
236 # select the OBD device we talk to
241 print "Disconnecting active session ($::client_id)...";
242 Disconnect($::client_id);
245 $device = "/dev/obd0";
248 # Open the device, as we need an FD for the ioctl
249 sysopen(DEV_OBD, $device, 0) || die "Cannot open $device";
250 print "Device now $device\n";
262 print "error: missing type\n";
264 print "usage: attach {ext2_obd | snap_obd}\n"; # XXX add scsi_obd later
268 if ($type eq "scsi_obd" ) {
274 $data = pack("iiii", $adapter, $bus, $tid, $lun);
276 } elsif ($type eq "snap_obd" ) {
281 $data = pack("iii", $snapdev, $snapidx, $tableno);
283 } elsif ($type eq "ext2_obd") {
285 # $basedev = "/dev/loop0" unless $basedev;
287 if (!defined($::st = stat($basedev))) {
288 die "Unable to stat $basedev.\n";
291 print "error: unknown attach type $type\n";
295 my $len = length($type);
296 my $cl = length($data);
298 print "type $type (len $len), datalen $datalen ($cl)\n";
299 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
301 my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
304 print STDERR "ioctl failed: $!\n";
305 } elsif ($rc eq "0 but true") {
306 print "Finished (success)\n";
308 print "ioctl returned error code $rc.\n";
315 my $rc = ioctl(DEV_OBD, &OBD_IOC_DETACH, $data);
318 print STDERR "ioctl failed: $!\n";
319 } elsif ($rc eq "0 but true") {
320 print "Finished (success)\n";
322 print "ioctl returned error code $rc.\n";
327 sub TestExt2Iterator {
328 if (!defined($::client_id)) {
329 print "You must first ``connect''.\n";
334 my $type = "ext2_obd";
336 $data = pack("i", 4711); # bogus data
339 my $len = length($type);
340 my $cl = length($data);
341 my $add = pack("p", $data);
342 print "type $type (len $len), datalen $datalen ($cl)\n";
343 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
345 my $rc = ioctl(DEV_OBD, &OBD_EXT2_RUNIT, $packed);
348 print STDERR "ioctl failed: $!\n";
349 } elsif ($rc eq "0 but true") {
350 print "Finished (success)\n";
352 print "ioctl returned error code $rc.\n";
358 if (!defined($::client_id)) {
359 print "You must first ``connect''.\n";
364 my $type = "snap_obd";
366 $data = pack("i", 4711); # bogus data
369 my $len = length($type);
370 my $cl = length($data);
371 my $add = pack("p", $data);
372 print "type $type (len $len), datalen $datalen ($cl)\n";
373 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
375 # XXX We need to fix this up so that after the objects in this snapshot
376 # are deleted, the snapshot itself is also removed from the table.
377 my $rc = ioctl(DEV_OBD, &OBD_SNAP_DELETE, $packed);
380 print STDERR "ioctl failed: $!\n";
381 } elsif ($rc eq "0 but true") {
382 print "Finished (success)\n";
384 print "ioctl returned error code $rc.\n";
389 # XXX the parameter to snaprestore SHOULD be the snapshot number of the
390 # snapshot to be restored, and this routine should handle all of the
391 # removal of intervening snapshots and changing the snaptable to have
392 # the "restored" snapshot as current and move the previous current.
394 if (!defined($::client_id)) {
395 print "You must first ``connect''.\n";
400 my $type = "snap_obd";
401 my $prevcurrent = shift;
403 $data = pack("i", $prevcurrent); # where the previous current snapshot now lives
406 my $len = length($type);
407 my $cl = length($data);
408 my $add = pack("p", $data);
409 print "type $type (len $len), datalen $datalen ($cl)\n";
410 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
412 my $rc = ioctl(DEV_OBD, &OBD_SNAP_RESTORE, $packed);
415 print STDERR "ioctl failed: $!\n";
416 } elsif ($rc eq "0 but true") {
417 print "Finished (success)\n";
419 print "ioctl returned error code $rc.\n";
427 my $type = "snap_obd";
428 my $snaptableno = shift;
430 $data = pack("i", $snaptableno);
433 my $len = length($type);
434 my $cl = length($data);
435 my $add = pack("p", $data);
436 print "type $type (len $len), datalen $datalen ($cl)\n";
437 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
439 my $rc = ioctl(DEV_OBD, &OBD_SNAP_PRINTTABLE, $packed);
442 print STDERR "ioctl failed: $!\n";
443 } elsif ($rc eq "0 but true") {
444 print "Finished (success)\n";
446 print "ioctl returned error code $rc.\n";
452 my $type = "snap_obd";
453 my $snaptableno = shift;
461 print "No such file $file\n";
464 $table = ReadSnapShotTable($file);
466 $snapcount = keys %{$table};
467 print "Snapcount $snapcount\n";
469 if ( ! defined $table->{0} ) {
470 print "No current snapshot in table! First make one\n";
473 $data = pack("ii", $snaptableno, $snapcount);
475 foreach my $time (sort keys %{$table}) {
476 $data .= pack("Ii", $time, $table->{$time});
480 my $len = length($type);
481 my $cl = length($data);
482 my $add = pack("p", $data);
483 print "type $type (len $len), datalen $datalen ($cl)\n";
484 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
486 my $rc = ioctl(DEV_OBD, &OBD_SNAP_SETTABLE, $packed);
489 print STDERR "ioctl failed: $!\n";
490 } elsif ($rc eq "0 but true") {
491 print "Finished (success)\n";
493 print "ioctl returned error code $rc.\n";
500 my $file = &readl("enter file name: ");
504 my $table = ReadSnapShotTable($file);
507 PrintSnapShotTable($table);
508 my $action = &readl("Add, Delete or Quit [adq]: ");
509 goto done if ($action =~ "^q.*" );
510 goto add if ($action =~ "^a.*");
511 goto del if ($action =~ "^d.*");
515 my $idx = &readl("enter index where you want this snapshot: ");
516 my $time = &readl("enter time or 'now' or 'current': ");
517 my $oldtime = SnapFindTimeFromIdx($idx, $table);
518 if (defined $oldtime) {
519 print "This already exists, first clean up\n";
523 if ( $time eq 'now' ) {
525 } elsif ( $time eq 'current' ) {
528 $table->{$time} = $idx;
532 $didx = &readl("Enter index to delete: ");
533 my $deltime = SnapFindTimeFromIdx($didx, $table);
534 delete $table->{$deltime} if defined $deltime;
538 my $ok = &readl("OK with new table? [Yn]: ");
539 unless ( $ok eq "no" ) {
540 WriteSnapShotTable($file, $table);
544 sub SnapFindTimeFromIdx {
548 foreach my $time ( keys %{$table} ) {
549 if ( $table->{$time} == $idx ) {
556 sub PrintSnapShotTable {
560 foreach $time ( sort keys %{$table} ) {
561 my $stime = localtime($time);
565 printf "Time: %s -- Index %d\n", $stime, $table->{$time};
569 sub ReadSnapShotTable {
576 my ($time, $index) = split ;
577 $table->{$time} = $index;
581 PrintSnapShotTable($table);
586 sub WriteSnapShotTable {
591 foreach my $time ( sort keys %{$table} ) {
592 print FH "$time $table->{$time}\n";
601 my $data = pack("III", $::client_id, $srcid, $tgtid);
604 my $packed = pack("ip", $datalen, $data);
605 my $rc = ioctl(DEV_OBD, &OBD_IOC_COPY, $packed);
608 print STDERR "ioctl failed: $!\n";
609 } elsif ($rc eq "0 but true") {
610 print "Finished (success)\n";
612 print "ioctl returned error code $rc.\n";
620 my $data = pack("III", $::client_id, $srcid, $tgtid);
623 my $packed = pack("ip", $datalen, $data);
624 my $rc = ioctl(DEV_OBD, &OBD_IOC_MIGR, $packed);
627 print STDERR "ioctl failed: $!\n";
628 } elsif ($rc eq "0 but true") {
629 print "Finished (success)\n";
631 print "ioctl returned error code $rc.\n";
639 my $data = pack("i", $size);
642 my $packed = pack("ip", $datalen, $data);
643 my $rc = ioctl(DEV_OBD, &OBD_IOC_FORMATOBD, $packed);
646 print STDERR "ioctl failed: $!\n";
647 } elsif ($rc eq "0 but true") {
648 print "Finished (success)\n";
650 print "ioctl returned error code $rc.\n";
658 my $data = pack("ii", $partno, $size);
661 my $packed = pack("ip", $datalen, $data);
662 my $rc = ioctl(DEV_OBD, &OBD_IOC_PARTITION, $packed);
665 print STDERR "ioctl failed: $!\n";
666 } elsif ($rc eq "0 but true") {
667 print "Finished (success)\n";
669 print "ioctl returned error code $rc.\n";
679 $type = "ext2_obd" unless $type;
681 if ( $type eq "ext2_obd" ) {
683 $dev = $::st->rdev() unless $dev;
684 $data = pack("i", $dev);
688 my $packed = pack("ip", $datalen, $data);
689 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP, $packed);
692 print STDERR "ioctl failed: $!\n";
693 } elsif ($rc eq "0 but true") {
694 print "Finished (success)\n";
696 print "ioctl returned error code $rc.\n";
702 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP, $err);
705 print STDERR "ioctl failed: $!\n";
706 } elsif ($rc eq "0 but true") {
707 print "Finished (success)\n";
710 print "ioctl returned error code $rc.\n";
719 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
720 $id = unpack("I", $packed);
723 print STDERR "ioctl failed: $!\n";
724 } elsif ($rc eq "0 but true") {
726 print "Client ID : $id\n";
727 print "Finished (success)\n";
729 print "ioctl returned error code $rc.\n";
741 print "syntax: disconnect [client ID]\n";
742 print "When client ID is not given, the last valid client ID to be returned by a\n";
743 print "connect command this session is used; there is no such ID.\n";
747 my $packed = pack("L", $id);
748 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
751 print STDERR "ioctl failed: $!\n";
752 } elsif ($rc eq "0 but true") {
753 $::client_id = undef;
754 print "Finished (success)\n";
756 print "ioctl returned error code $rc.\n";
766 if (defined($quiet) && $quiet ne "quiet") {
767 print "syntax: create [number of objects [quiet]]\n";
771 my $packed = pack("IL", $::client_id, $prealloc);
772 if (!defined($arg) || scalar($arg) < 2) {
773 print "Creating 1 object...\n";
774 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
775 if (!defined($quiet)) {
776 my $ino = unpack("L", $packed);
777 print "Created object #$ino.\n";
782 print "Creating " . scalar($arg) . " objects...\n";
783 for ($i = 0; $i < scalar($arg); $i++) {
784 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
785 my $ino = unpack("L", $packed);
786 if ($rc ne "0 but true") {
788 $packed = pack("IL", $::client_id, $prealloc);
789 } elsif (!defined($quiet)) {
790 $packed = pack("IL", $::client_id, $prealloc);
791 print "Created object #$ino.\n";
797 print STDERR "ioctl failed: $!\n";
798 } elsif ($rc eq "0 but true") {
799 print "Finished (success)\n";
801 print "ioctl returned error code $rc.\n";
807 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
810 print STDERR "ioctl failed: $!\n";
811 } elsif ($rc eq "0 but true") {
812 print "Finished (success)\n";
814 print "ioctl returned error code $rc.\n";
819 if (!defined($::client_id)) {
820 print "You must first ``connect''.\n";
826 if (!defined($arg) || scalar($arg) < 1) {
827 print "destroy requires the object number to destroy.\n";
831 print "Destroying object $arg...\n";
832 my $packed = pack("IL", $::client_id, $arg);
833 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
836 print STDERR "ioctl failed: $!\n";
837 } elsif ($rc eq "0 but true") {
838 print "Finished (success)\n";
840 print "ioctl returned error code $rc.\n";
845 if (!defined($::client_id)) {
846 print "You must first ``connect''.\n";
852 if (!defined($inode) || scalar($inode) < 1) {
853 print "invalid arguments; type \"help getattr\" for a synopsis\n";
858 my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
860 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
863 print STDERR "ioctl failed: $!\n";
864 } elsif ($rc eq "0 but true") {
865 my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
866 ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
867 unpack("ISssx2lLLLI", $packed);
869 printf("Inode: %d Mode: %o\n", $inode, $mode);
870 printf("User: %6d Group: %6d Size: %d\n", $uid, $gid, $size);
871 printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
872 printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
873 printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
874 printf("flags: %08x\n", $flags);
875 print "Finished (success)\n";
877 print "ioctl returned error code $rc.\n";
882 if (!defined($::client_id)) {
883 print "You must first ``connect''.\n";
889 my $mode = oct(shift);
903 if (defined($size)) {
904 $valid |= &ATTR_SIZE;
906 if (defined($atime)) {
907 $valid |= &ATTR_ATIME;
909 if (defined($mtime)) {
910 $valid |= &ATTR_MTIME;
912 if (defined($ctime)) {
913 $valid |= &ATTR_CTIME;
915 if (defined($mode)) {
916 $valid |= &ATTR_MODE;
919 if (!defined($inode) || scalar($inode) < 1) {
920 print "invalid arguments; type \"help setattr\" for a synopsis\n";
925 # unsigned int ia_valid; (32)
926 # umode_t ia_mode; (16)
929 # -- 16 bit alignment here! --
930 # off_t ia_size; (32)
931 # time_t ia_atime; (32)
932 # time_t ia_mtime; (32)
933 # time_t ia_ctime; (32)
934 # unsigned int ia_attr_flags; (32)
937 printf "valid is %x, mode is %o\n", $valid, $mode;
938 my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
939 $uid, $gid, $size, $atime, $mtime, $ctime, 0);
940 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
943 print STDERR "ioctl failed: $!\n";
944 } elsif ($rc eq "0 but true") {
945 print "Finished (success)\n";
947 print "ioctl returned error code $rc.\n";
952 if (!defined($::client_id)) {
953 print "You must first ``connect''.\n";
961 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
962 $count < 1 || (defined($offset) && $offset < 0)) {
963 print "invalid arguments; type \"help read\" for a synopsis\n";
967 if (!defined($offset)) {
971 print("Reading $count bytes starting at byte $offset from object " .
974 # "allocate" a large enough buffer
975 my $buf = sprintf("%${count}s", " ");
976 die "suck" if (length($buf) != $count);
978 # the perl we're using doesn't support pack type Q, and offset is 64 bits
979 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
981 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
983 $retval = unpack("l", $packed);
986 print STDERR "ioctl failed: $!\n";
987 } elsif ($rc eq "0 but true") {
989 print substr($buf, 0, $retval);
990 print "\nRead $retval of an attempted $count bytes.\n";
991 print "Finished (success)\n";
993 print "Finished (error $retval)\n";
996 print "ioctl returned error code $rc.\n";
1001 if (!defined($::client_id)) {
1002 print "You must first ``connect''.\n";
1010 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
1011 $count < 1 || (defined($offset) && $offset < 0)) {
1012 print "invalid arguments; type \"help read\" for a synopsis\n";
1016 if (!defined($offset)) {
1020 print("Reading $count bytes starting at byte $offset from object " .
1023 # "allocate" a large enough buffer
1024 my $buf = sprintf("%${count}s", " ");
1025 die "suck" if (length($buf) != $count);
1027 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1028 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
1030 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed);
1032 $retval = unpack("l", $packed);
1035 print STDERR "ioctl failed: $!\n";
1036 } elsif ($rc eq "0 but true") {
1038 print substr($buf, 0, $retval);
1039 print "\nRead $retval of an attempted $count bytes.\n";
1040 print "Finished (success)\n";
1042 print "Finished (error $retval)\n";
1045 print "ioctl returned error code $rc.\n";
1050 if (!defined($::client_id)) {
1051 print "You must first ``connect''.\n";
1057 my $text = join(' ', @_);
1058 my $count = length($text);
1060 if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
1061 scalar($offset) < 0) {
1062 print "invalid arguments; type \"help write\" for a synopsis\n";
1066 if (!defined($text)) {
1071 print("Writing $count bytes starting at byte $offset to object " .
1074 # the perl we're using doesn't support pack type Q
1075 my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, $offset, 0);
1076 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
1078 $retval = unpack("l", $packed);
1081 print STDERR "ioctl failed: $!\n";
1082 } elsif ($rc eq "0 but true") {
1084 print "\nWrote $retval of an attempted $count bytes.\n";
1085 print "Finished (success)\n";
1087 print "Finished (error $retval)\n";
1090 print "ioctl returned error code $rc.\n";
1097 if (!defined($::client_id)) {
1098 print "You must first ``connect''.\n";
1102 if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
1106 print "Preallocating $arg inodes...\n";
1107 my $packed = pack("LLx128", $::client_id, $arg);
1108 # client id, alloc, inodes[32]
1110 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
1113 print STDERR "ioctl failed: $!\n";
1114 } elsif ($rc eq "0 but true") {
1115 my $alloc = unpack("x4L", $packed);
1116 my @inodes = unpack("x8L32", $packed);
1119 print "Got $alloc inodes: ";
1120 foreach $i (@inodes) {
1123 print "\nFinished (success)\n";
1125 print "ioctl returned error code $rc.\n";
1130 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
1133 print STDERR "ioctl failed: $!\n";
1134 } elsif ($rc eq "0 but true") {
1135 print "Finished (success)\n";
1137 print "ioctl returned error code $rc.\n";
1142 if (!defined($::client_id)) {
1143 print "You must first ``connect''.\n";
1155 # __kernel_fsid_t f_fsid; (64 bits)
1160 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1163 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
1166 print STDERR "ioctl failed: $!\n";
1167 } elsif ($rc eq "0 but true") {
1168 # skip both the conn_id and the fs_type in the buffer
1169 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
1170 unpack("x4x4LLLLLL", $packed);
1171 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
1172 "$bfree free ($bavail available).\n");
1173 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
1174 print "Finished (success)\n";
1176 print "ioctl returned error code $rc.\n";
1183 if ( !$arg || !$commands{$arg} ) {
1184 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
1186 print "Usage: " . $commands{$arg}->{doc} . "\n";
1192 print "Disconnecting active session ($::client_id)...";
1193 Disconnect($::client_id);