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);
69 eval 'sub OBD_SNAP_RUNIT () { &_IOC(3, ord(\'f\'), 42, 4);}' unless
70 defined(&OBD_SNAP_RUNIT);
71 eval 'sub OBD_EXT2_RUNIT () { &_IOC(3, ord(\'f\'), 61, 4);}' unless
72 defined(&OBD_EXT2_RUNIT);
74 eval 'sub ATTR_MODE () {1;}' unless defined(&ATTR_MODE);
75 eval 'sub ATTR_UID () {2;}' unless defined(&ATTR_UID);
76 eval 'sub ATTR_GID () {4;}' unless defined(&ATTR_GID);
77 eval 'sub ATTR_SIZE () {8;}' unless defined(&ATTR_SIZE);
78 eval 'sub ATTR_ATIME () {16;}' unless defined(&ATTR_ATIME);
79 eval 'sub ATTR_MTIME () {32;}' unless defined(&ATTR_MTIME);
80 eval 'sub ATTR_CTIME () {64;}' unless defined(&ATTR_CTIME);
92 GetOptions("f!" => \$file, "device=s" => \$::device, "fs=s" => $::filesystem) || die "Getoptions";
95 # get a console for the app
102 ('device' => {func => "Device", doc => "device <dev>: open another OBD device"},
103 'filesystem' => {func => "Filesystem", doc => "filesystem <dev>: partition for direct OBD device"},
104 'create' => {func => "Create", doc => "create: creates a new inode"},
105 'attach' => {func => "Attach", doc => "attach type [adapter bus tid lun]"},
106 'detach' => {func => "Detach", doc => "detach this device"},
107 'snapattach' => {func => "SnapAttach", doc => "snapattach snapno table"},
108 'snapset' => {func => "SnapSetTable", doc => "snapset tableno file" },
109 'snapprint' => {func => "SnapPrint", doc => "snapprint tableno"},
111 'testext2iterator' => {func => "TestExt2Iterator", doc => ""},
112 'testsnapiterator' => {func => "TestSnapIterator", doc => ""},
114 'snaptable' => {func => "SnapShotTable", doc => "snaptable: build a snapshot table (interactive)"},
115 'copy' => {func => "Copy", doc => "copy srcid tgtid"},
116 'migrate' => {func => "Migrate", doc => "migrate srcid tgtid"},
117 'format' => {func => "Format", doc => "format type adapter bus tid lun size"},
118 'partition' => {func => "Partition", doc => "partition type adapter bus tid lun partition size"},
119 'setup' => {func => "Setup", doc => "setup: link the ext2 partition (default /dev/loop0) to this obddev"},
120 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
121 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
122 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
123 'destroy' => {func => "Destroy", doc => "setup: destroys an inode"},
124 'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
125 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
126 'read' => {func => "Read", doc => "read <inode> <count> [offset]"},
127 'fsread' => {func => "Read2", doc => "read <inode> <count> [offset]"},
128 'write' => {func => "Write", doc => "write <inode> <offset> <text>"},
129 'setattr' => {func => "Setattr", doc => "setattr <inode> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"},
130 'getattr' => {func => "Getattr", doc => "getattr <inode>: displays inode object attributes"},
131 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num inodes."},
132 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
133 'help' => {func => \&Help, doc => "help: this message"},
134 'quit' => {func => \&Quit, doc => "see \"exit\""},
135 'exit' => {func => \&Quit, doc => "see \"quit\""}
139 # setup completion function
141 my @jcm_cmd_list = keys %commands;
149 Filesystem($::filesystem);
157 return $term->readline(@_);
170 $term = new Term::ReadLine 'obdcontrol ';
171 $attribs = $term->Attribs;
172 $attribs->{attempted_completion_function} = \&completeme;
173 $term->ornaments('md,me,,'); # bold face prompt
175 # make sure stdout is not buffered
176 STDOUT->autoflush(1);
179 # Get on with the show
183 #------------------------------------------------------------------------------
185 my ($text, $line, $start, $end) = @_;
186 if (substr($line, 0, $start) =~ /^\s*$/) {
187 $attribs->{completion_word} = \@jcm_cmd_list;
188 return $term->completion_matches($text,
189 $attribs->{'list_completion_function'});
196 my @completions = completeme($given, $given, 0, length($given));
197 if ($#completions == 0) {
198 $name = shift @completions;
204 # start making requests
207 $line = $term->readline("obdcontrol > ");
215 my @arg = split(' ', $line);
216 my $word = shift @arg;
222 $cmd = find_command($word);
225 printf STDERR "$word: No such command, or not unique.\n";
229 if ($cmd eq "help" || $cmd eq "exit" || $cmd eq "quit") {
230 return (&{$commands{$cmd}->{func}}(@arg));
234 return (&{$commands{$cmd}->{func}}(@arg));
237 # set the object store in the ext2 formatted block device
239 my $filesystem = shift;
240 $filesystem = "/dev/loop0" unless $filesystem;
242 $::filesystem = $filesystem;
243 if (!defined($::st = stat($filesystem))) {
244 die "Unable to stat $filesystem.\n";
248 # select the OBD device we talk to
253 $device = "/dev/obd0";
256 # Open the device, as we need an FD for the ioctl
257 sysopen(DEV_OBD, $device, 0) || die "Cannot open $device";
258 print "Device now $device\n";
269 if ($type eq "obdscsi" ) {
274 $data = pack("iiiii", $adapter, $bus, $tid, $lun, $size);
278 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
280 my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
283 print STDERR "ioctl failed: $!\n";
284 } elsif ($rc eq "0 but true") {
285 print "Finished (success)\n";
287 print "ioctl returned error code $rc.\n";
294 my $rc = ioctl(DEV_OBD, &OBD_IOC_DETACH, $data);
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";
306 sub TestExt2Iterator {
308 my $type = "ext2_obd";
310 $data = pack("i", 4711); # bogus data
313 my $len = length($type);
314 my $cl = length($data);
315 my $add = pack("p", $data);
316 print "type $type (len $len), datalen $datalen ($cl)\n";
317 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
319 my $rc = ioctl(DEV_OBD, &OBD_EXT2_RUNIT, $packed);
322 print STDERR "ioctl failed: $!\n";
323 } elsif ($rc eq "0 but true") {
324 print "Finished (success)\n";
326 print "ioctl returned error code $rc.\n";
331 sub TestSnapIterator {
333 my $type = "snap_obd";
335 $data = pack("i", 4711); # bogus data
338 my $len = length($type);
339 my $cl = length($data);
340 my $add = pack("p", $data);
341 print "type $type (len $len), datalen $datalen ($cl)\n";
342 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
344 my $rc = ioctl(DEV_OBD, &OBD_SNAP_RUNIT, $packed);
347 print STDERR "ioctl failed: $!\n";
348 } elsif ($rc eq "0 but true") {
349 print "Finished (success)\n";
351 print "ioctl returned error code $rc.\n";
358 my $type = "snap_obd";
359 my $snaptableno = shift;
361 $data = pack("i", $snaptableno);
364 my $len = length($type);
365 my $cl = length($data);
366 my $add = pack("p", $data);
367 print "type $type (len $len), datalen $datalen ($cl)\n";
368 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
370 my $rc = ioctl(DEV_OBD, &OBD_SNAP_PRINTTABLE, $packed);
373 print STDERR "ioctl failed: $!\n";
374 } elsif ($rc eq "0 but true") {
375 print "Finished (success)\n";
377 print "ioctl returned error code $rc.\n";
383 my $type = "snap_obd";
384 my $snaptableno = shift;
392 print "No such file $file\n";
395 $table = ReadSnapShotTable($file);
397 $snapcount = keys %{$table};
398 print "Snapcount $snapcount\n";
400 if ( ! defined $table->{0} ) {
401 print "No current snapshot in table! First make one\n";
404 $data = pack("ii", $snaptableno, $snapcount);
406 foreach my $time (sort keys %{$table}) {
407 $data .= pack("Ii", $time, $table->{$time});
411 my $len = length($type);
412 my $cl = length($data);
413 my $add = pack("p", $data);
414 print "type $type (len $len), datalen $datalen ($cl)\n";
415 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
417 my $rc = ioctl(DEV_OBD, &OBD_SNAP_SETTABLE, $packed);
420 print STDERR "ioctl failed: $!\n";
421 } elsif ($rc eq "0 but true") {
422 print "Finished (success)\n";
424 print "ioctl returned error code $rc.\n";
431 my $type = "snap_obd";
439 $data = pack("iii", $snapdev, $snapno, $tableno);
442 my $len = length($type);
443 my $cl = length($data);
444 my $add = pack("p", $data);
445 print "type $type (len $len), datalen $datalen ($cl)\n";
446 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
448 my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
451 print STDERR "ioctl failed: $!\n";
452 } elsif ($rc eq "0 but true") {
453 print "Finished (success)\n";
455 print "ioctl returned error code $rc.\n";
462 my $file = &readl("enter file name: ");
466 my $table = ReadSnapShotTable($file);
469 PrintSnapShotTable($table);
470 my $action = &readl("Add, Delete or Quit [adq]: ");
471 goto done if ($action =~ "^q.*" );
472 goto add if ($action =~ "^a.*");
473 goto del if ($action =~ "^d.*");
477 my $idx = &readl("enter index where you want this snapshot: ");
478 my $time = &readl("enter time or 'now' or 'current': ");
479 my $oldtime = SnapFindTimeFromIdx($idx, $table);
480 if (defined $oldtime) {
481 print "This already exists, first clean up\n";
485 if ( $time eq 'now' ) {
487 } elsif ( $time eq 'current' ) {
490 $table->{$time} = $idx;
494 $didx = &readl("Enter index to delete: ");
495 my $deltime = SnapFindTimeFromIdx($didx, $table);
496 delete $table->{$deltime} if defined $deltime;
500 my $ok = &readl("OK with new table? [Yn]: ");
501 unless ( $ok eq "no" ) {
502 WriteSnapShotTable($file, $table);
506 sub SnapFindTimeFromIdx {
510 foreach my $time ( keys %{$table} ) {
511 if ( $table->{$time} == $idx ) {
518 sub PrintSnapShotTable {
522 foreach $time ( sort keys %{$table} ) {
523 my $stime = localtime($time);
527 printf "Time: %s -- Index %d\n", $stime, $table->{$time};
531 sub ReadSnapShotTable {
538 my ($time, $index) = split ;
539 $table->{$time} = $index;
543 PrintSnapShotTable($table);
548 sub WriteSnapShotTable {
553 foreach my $time ( sort keys %{$table} ) {
554 print FH "$time $table->{$time}\n";
563 my $data = pack("III", $::client_id, $srcid, $tgtid);
566 my $packed = pack("ip", $datalen, $data);
567 my $rc = ioctl(DEV_OBD, &OBD_IOC_COPY, $packed);
570 print STDERR "ioctl failed: $!\n";
571 } elsif ($rc eq "0 but true") {
572 print "Finished (success)\n";
574 print "ioctl returned error code $rc.\n";
582 my $data = pack("III", $::client_id, $srcid, $tgtid);
585 my $packed = pack("ip", $datalen, $data);
586 my $rc = ioctl(DEV_OBD, &OBD_IOC_MIGR, $packed);
589 print STDERR "ioctl failed: $!\n";
590 } elsif ($rc eq "0 but true") {
591 print "Finished (success)\n";
593 print "ioctl returned error code $rc.\n";
601 my $data = pack("i", $size);
604 my $packed = pack("ip", $datalen, $data);
605 my $rc = ioctl(DEV_OBD, &OBD_IOC_FORMATOBD, $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("ii", $partno, $size);
623 my $packed = pack("ip", $datalen, $data);
624 my $rc = ioctl(DEV_OBD, &OBD_IOC_PARTITION, $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";
641 $type = "ext2_obd" unless $type;
643 if ( $type eq "ext2_obd" ) {
645 $dev = $::st->rdev() unless $dev;
646 $data = pack("i", $dev);
650 my $packed = pack("ip", $datalen, $data);
651 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP, $packed);
654 print STDERR "ioctl failed: $!\n";
655 } elsif ($rc eq "0 but true") {
656 print "Finished (success)\n";
658 print "ioctl returned error code $rc.\n";
664 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP, $err);
667 print STDERR "ioctl failed: $!\n";
668 } elsif ($rc eq "0 but true") {
669 print "Finished (success)\n";
672 print "ioctl returned error code $rc.\n";
681 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
682 $id = unpack("I", $packed);
685 print STDERR "ioctl failed: $!\n";
686 } elsif ($rc eq "0 but true") {
688 print "Client ID : $id\n";
689 print "Finished (success)\n";
691 print "ioctl returned error code $rc.\n";
703 print "syntax: disconnect [client ID]\n";
704 print "When client ID is not given, the last valid client ID to be returned by a\n";
705 print "connect command this session is used; there is no such ID.\n";
709 my $packed = pack("L", $id);
710 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
713 print STDERR "ioctl failed: $!\n";
714 } elsif ($rc eq "0 but true") {
715 $::client_id = undef;
716 print "Finished (success)\n";
718 print "ioctl returned error code $rc.\n";
728 if (defined($quiet) && !($quiet eq "quiet")) {
729 print "syntax: create [number of objects [quiet]]\n";
733 my $packed = pack("IL", $::client_id, $prealloc);
734 if (!defined($arg) || scalar($arg) < 2) {
735 print "Creating 1 object...\n";
736 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
737 if (!defined($quiet)) {
738 my $ino = unpack("L", $packed);
739 print "Created object #$ino.\n";
744 print "Creating " . scalar($arg) . " objects...\n";
745 for ($i = 0; $i < scalar($arg); $i++) {
746 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
747 my $ino = unpack("L", $packed);
748 if (!($rc eq "0 but true")) {
750 $packed = pack("IL", $::client_id, $prealloc);
751 } elsif (!defined($quiet)) {
752 $packed = pack("IL", $::client_id, $prealloc);
753 print "Created object #$ino.\n";
759 print STDERR "ioctl failed: $!\n";
760 } elsif ($rc eq "0 but true") {
761 print "Finished (success)\n";
763 print "ioctl returned error code $rc.\n";
769 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
772 print STDERR "ioctl failed: $!\n";
773 } elsif ($rc eq "0 but true") {
774 print "Finished (success)\n";
776 print "ioctl returned error code $rc.\n";
781 if (!defined($::client_id)) {
782 print "You must first ``connect''.\n";
788 if (!defined($arg) || scalar($arg) < 1) {
789 print "destroy requires the object number to destroy.\n";
793 print "Destroying object $arg...\n";
794 my $packed = pack("IL", $::client_id, $arg);
795 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
798 print STDERR "ioctl failed: $!\n";
799 } elsif ($rc eq "0 but true") {
800 print "Finished (success)\n";
802 print "ioctl returned error code $rc.\n";
807 if (!defined($::client_id)) {
808 print "You must first ``connect''.\n";
814 if (!defined($inode) || scalar($inode) < 1) {
815 print "invalid arguments; type \"help getattr\" for a synopsis\n";
820 my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
822 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
825 print STDERR "ioctl failed: $!\n";
826 } elsif ($rc eq "0 but true") {
827 my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
828 ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
829 unpack("ISssx2lLLLI", $packed);
831 printf("Inode: %d Mode: %o\n", $inode, $mode);
832 printf("User: %6d Group: %6d Size: %d\n", $uid, $gid, $size);
833 printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
834 printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
835 printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
836 printf("flags: %08x\n", $flags);
837 print "Finished (success)\n";
839 print "ioctl returned error code $rc.\n";
844 if (!defined($::client_id)) {
845 print "You must first ``connect''.\n";
851 my $mode = oct(shift);
865 if (defined($size)) {
866 $valid |= &ATTR_SIZE;
868 if (defined($atime)) {
869 $valid |= &ATTR_ATIME;
871 if (defined($mtime)) {
872 $valid |= &ATTR_MTIME;
874 if (defined($ctime)) {
875 $valid |= &ATTR_CTIME;
877 if (defined($mode)) {
878 $valid |= &ATTR_MODE;
881 if (!defined($inode) || scalar($inode) < 1) {
882 print "invalid arguments; type \"help setattr\" for a synopsis\n";
887 # unsigned int ia_valid; (32)
888 # umode_t ia_mode; (16)
891 # -- 16 bit alignment here! --
892 # off_t ia_size; (32)
893 # time_t ia_atime; (32)
894 # time_t ia_mtime; (32)
895 # time_t ia_ctime; (32)
896 # unsigned int ia_attr_flags; (32)
899 printf "valid is %x, mode is %o\n", $valid, $mode;
900 my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
901 $uid, $gid, $size, $atime, $mtime, $ctime, 0);
902 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
905 print STDERR "ioctl failed: $!\n";
906 } elsif ($rc eq "0 but true") {
907 print "Finished (success)\n";
909 print "ioctl returned error code $rc.\n";
914 if (!defined($::client_id)) {
915 print "You must first ``connect''.\n";
923 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
924 $count < 1 || (defined($offset) && $offset < 0)) {
925 print "invalid arguments; type \"help read\" for a synopsis\n";
929 if (!defined($offset)) {
933 print("Reading $count bytes starting at byte $offset from object " .
936 # "allocate" a large enough buffer
937 my $buf = sprintf("%${count}s", " ");
938 die "suck" if (length($buf) != $count);
940 # the perl we're using doesn't support pack type Q, and offset is 64 bits
941 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
943 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
945 $retval = unpack("l", $packed);
948 print STDERR "ioctl failed: $!\n";
949 } elsif ($rc eq "0 but true") {
951 print substr($buf, 0, $retval);
952 print "\nRead $retval of an attempted $count bytes.\n";
953 print "Finished (success)\n";
955 print "Finished (error $retval)\n";
958 print "ioctl returned error code $rc.\n";
963 if (!defined($::client_id)) {
964 print "You must first ``connect''.\n";
972 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
973 $count < 1 || (defined($offset) && $offset < 0)) {
974 print "invalid arguments; type \"help read\" for a synopsis\n";
978 if (!defined($offset)) {
982 print("Reading $count bytes starting at byte $offset from object " .
985 # "allocate" a large enough buffer
986 my $buf = sprintf("%${count}s", " ");
987 die "suck" if (length($buf) != $count);
989 # the perl we're using doesn't support pack type Q, and offset is 64 bits
990 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
992 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed);
994 $retval = unpack("l", $packed);
997 print STDERR "ioctl failed: $!\n";
998 } elsif ($rc eq "0 but true") {
1000 print substr($buf, 0, $retval);
1001 print "\nRead $retval of an attempted $count bytes.\n";
1002 print "Finished (success)\n";
1004 print "Finished (error $retval)\n";
1007 print "ioctl returned error code $rc.\n";
1012 if (!defined($::client_id)) {
1013 print "You must first ``connect''.\n";
1019 my $text = join(' ', @_);
1020 my $count = length($text);
1022 if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
1023 scalar($offset) < 0) {
1024 print "invalid arguments; type \"help write\" for a synopsis\n";
1028 if (!defined($text)) {
1033 print("Writing $count bytes starting at byte $offset to object " .
1036 # the perl we're using doesn't support pack type Q
1037 my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, $offset, 0);
1038 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
1040 $retval = unpack("l", $packed);
1043 print STDERR "ioctl failed: $!\n";
1044 } elsif ($rc eq "0 but true") {
1046 print "\nWrote $retval of an attempted $count bytes.\n";
1047 print "Finished (success)\n";
1049 print "Finished (error $retval)\n";
1052 print "ioctl returned error code $rc.\n";
1059 if (!defined($::client_id)) {
1060 print "You must first ``connect''.\n";
1064 if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
1068 print "Preallocating $arg inodes...\n";
1069 my $packed = pack("LLx128", $::client_id, $arg);
1070 # client id, alloc, inodes[32]
1072 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
1075 print STDERR "ioctl failed: $!\n";
1076 } elsif ($rc eq "0 but true") {
1077 my $alloc = unpack("x4L", $packed);
1078 my @inodes = unpack("x8L32", $packed);
1081 print "Got $alloc inodes: ";
1082 foreach $i (@inodes) {
1085 print "\nFinished (success)\n";
1087 print "ioctl returned error code $rc.\n";
1092 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
1095 print STDERR "ioctl failed: $!\n";
1096 } elsif ($rc eq "0 but true") {
1097 print "Finished (success)\n";
1099 print "ioctl returned error code $rc.\n";
1104 if (!defined($::client_id)) {
1105 print "You must first ``connect''.\n";
1117 # __kernel_fsid_t f_fsid; (64 bits)
1122 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1125 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
1128 print STDERR "ioctl failed: $!\n";
1129 } elsif ($rc eq "0 but true") {
1130 # skip both the conn_id and the fs_type in the buffer
1131 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
1132 unpack("x4x4LLLLLL", $packed);
1133 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
1134 "$bfree free ($bavail available).\n");
1135 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
1136 print "Finished (success)\n";
1138 print "ioctl returned error code $rc.\n";
1145 if ( !$arg || !$commands{$arg} ) {
1146 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
1148 print "Usage: " . $commands{$arg}->{doc} . "\n";
1154 print "Disconnecting active session ($::client_id)...";
1155 Disconnect($::client_id);