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>: set the table (created with snaptable) as table #tableno" },
110 'snapprint' => {func => "SnapPrint", doc => "snapprint <tableno>: output the contents of table #tableno to the syslog"},
111 'snapdelete' => {func => "SnapDelete", doc => "snapdelete: delete connected snap obd objects from disk"},
112 'snaprestore' => {func => "SnapRestore", doc => "snaprestore : restore connected old snap objects to be current"},
113 'snaptable' => {func => "SnapShotTable", doc => "snaptable: build a snapshot table (interactive)"},
114 'copy' => {func => "Copy", doc => "copy <srcid> <tgtid>: copy objects"},
115 'migrate' => {func => "Migrate", doc => "migrate <srcid> <tgtid>: migrate data from one object to another"},
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 | scsi_obd}\n";
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") {
286 print "error: unknown attach type $type\n";
290 my $len = length($type);
291 my $cl = length($data);
293 print "type $type (len $len), datalen $datalen ($cl)\n";
294 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
296 my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
299 print STDERR "ioctl failed: $!\n";
300 } elsif ($rc eq "0 but true") {
301 print "Finished (success)\n";
303 print "ioctl returned error code $rc.\n";
310 my $rc = ioctl(DEV_OBD, &OBD_IOC_DETACH, $data);
313 print STDERR "ioctl failed: $!\n";
314 } elsif ($rc eq "0 but true") {
315 print "Finished (success)\n";
317 print "ioctl returned error code $rc.\n";
322 sub TestExt2Iterator {
323 if (!defined($::client_id)) {
324 print "You must first ``connect''.\n";
329 my $type = "ext2_obd";
331 $data = pack("i", 4711); # bogus data
334 my $len = length($type);
335 my $cl = length($data);
336 my $add = pack("p", $data);
337 print "type $type (len $len), datalen $datalen ($cl)\n";
338 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
340 my $rc = ioctl(DEV_OBD, &OBD_EXT2_RUNIT, $packed);
343 print STDERR "ioctl failed: $!\n";
344 } elsif ($rc eq "0 but true") {
345 print "Finished (success)\n";
347 print "ioctl returned error code $rc.\n";
353 if (!defined($::client_id)) {
354 print "You must first ``connect''.\n";
359 my $type = "snap_obd";
361 $data = pack("i", 4711); # bogus data
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 # XXX We need to fix this up so that after the objects in this snapshot
371 # are deleted, the snapshot itself is also removed from the table.
372 my $rc = ioctl(DEV_OBD, &OBD_SNAP_DELETE, $packed);
375 print STDERR "ioctl failed: $!\n";
376 } elsif ($rc eq "0 but true") {
377 print "Finished (success)\n";
379 print "ioctl returned error code $rc.\n";
384 # this routine does the whole job
386 my $restoreto = shift;
387 my $snaptable = shift;
391 # don't do anything until connected
392 if (!defined($::client_id)) {
393 print "You must first ``connect''.\n";
397 if ( ! $snaptable || ! defined $restoreto ) {
398 print "Usage: snaprestore \"restore to slot\" \"snaptable\" \"tableno\"\n";
402 if ( ! -f $snaptable ) {
403 print "Table $snaptable doesn't exist\n";
407 my $table = ReadSnapShotTable($snaptable);
408 $restoretime = FindSnapInTable($table, $restoreto);
409 if ( ! defined $table->{0} || ! defined $restoretime ) {
410 PrintSnapShotTable($table);
411 print "No current or $restoreto slot in this table\n";
415 my $currentindex = $table->{0};
416 if ( $table->{$restoretime} == $currentindex ) {
417 print "You should not restore to the current snapshot\n";
421 # swap the entries for 0 and $restoreto
422 my $tmp = $table->{$restoretime};
423 $table->{$restoretime} = $table->{0};
425 # PrintSnapShotTable($table);
428 WriteSnapShotTable($snaptable, $table);
430 # set it in the kernel
431 SnapSetTable($tableno, $snaptable);
433 # ready for the ioctl
435 my $type = "snap_obd";
436 $data = pack("i", $currentindex); # slot of previous current snapshot
439 my $len = length($type);
440 my $cl = length($data);
441 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
443 my $rc = ioctl(DEV_OBD, &OBD_SNAP_RESTORE, $packed);
446 print STDERR "ioctl failed: $!\n";
447 } elsif ($rc eq "0 but true") {
448 print "Snaprestore finished (success)\n";
449 delete $table->{$restoretime} if defined $restoretime;
451 WriteSnapShotTable($snaptable, $table);
453 # set it in the kernel
454 SnapSetTable($tableno, $snaptable);
455 # PrintSnapShotTable($table);
458 print "ioctl returned error code $rc.\n";
462 sub FindSnapInTable {
466 foreach my $restoretime ( keys %{$table} ) {
467 if ( $table->{$restoretime} == $snapno) {
468 print "Found key $restoretime for snapno $snapno\n";
478 my $type = "snap_obd";
479 my $snaptableno = shift;
481 $data = pack("i", $snaptableno);
484 my $len = length($type);
485 my $cl = length($data);
486 my $add = pack("p", $data);
487 print "type $type (len $len), datalen $datalen ($cl)\n";
488 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
490 my $rc = ioctl(DEV_OBD, &OBD_SNAP_PRINTTABLE, $packed);
493 print STDERR "ioctl failed: $!\n";
494 } elsif ($rc eq "0 but true") {
495 print "Finished (success)\n";
497 print "ioctl returned error code $rc.\n";
503 my $type = "snap_obd";
504 my $snaptableno = shift;
512 print "No such file $file\n";
515 $table = ReadSnapShotTable($file);
517 $snapcount = keys %{$table};
518 print "Snapcount $snapcount\n";
520 if ( ! defined $table->{0} ) {
521 print "No current snapshot in table! First make one\n";
524 $data = pack("ii", $snaptableno, $snapcount);
526 foreach my $time (sort keys %{$table}) {
527 $data .= pack("Ii", $time, $table->{$time});
531 my $len = length($type);
532 my $cl = length($data);
533 my $add = pack("p", $data);
534 print "type $type (len $len), datalen $datalen ($cl)\n";
535 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
537 my $rc = ioctl(DEV_OBD, &OBD_SNAP_SETTABLE, $packed);
540 print STDERR "ioctl failed: $!\n";
541 } elsif ($rc eq "0 but true") {
542 print "Finished (success)\n";
544 print "ioctl returned error code $rc.\n";
551 my $file = &readl("enter file name: ");
555 my $table = ReadSnapShotTable($file);
558 PrintSnapShotTable($table);
559 my $action = &readl("Add, Delete or Quit [adq]: ");
560 goto done if ($action =~ "^q.*" );
561 goto add if ($action =~ "^a.*");
562 goto del if ($action =~ "^d.*");
566 my $idx = &readl("enter index where you want this snapshot: ");
567 my $time = &readl("enter time or 'now' or 'current': ");
568 my $oldtime = SnapFindTimeFromIdx($idx, $table);
569 if (defined $oldtime) {
570 print "This already exists, first clean up\n";
574 if ( $time eq 'now' ) {
576 } elsif ( $time eq 'current' ) {
579 $table->{$time} = $idx;
583 $didx = &readl("Enter index to delete: ");
584 my $deltime = SnapFindTimeFromIdx($didx, $table);
585 delete $table->{$deltime} if defined $deltime;
589 my $ok = &readl("OK with new table? [Yn]: ");
590 unless ( $ok eq "n" ) {
591 WriteSnapShotTable($file, $table);
595 sub SnapFindTimeFromIdx {
599 foreach my $time ( keys %{$table} ) {
600 if ( $table->{$time} == $idx ) {
607 sub PrintSnapShotTable {
611 foreach $time ( sort keys %{$table} ) {
612 my $stime = localtime($time);
616 printf "Time: %s -- Index %d\n", $stime, $table->{$time};
620 sub ReadSnapShotTable {
627 my ($time, $index) = split ;
628 $table->{$time} = $index;
632 PrintSnapShotTable($table);
637 sub WriteSnapShotTable {
642 foreach my $time ( sort keys %{$table} ) {
643 print FH "$time $table->{$time}\n";
652 my $data = pack("III", $::client_id, $srcid, $tgtid);
655 my $packed = pack("ip", $datalen, $data);
656 my $rc = ioctl(DEV_OBD, &OBD_IOC_COPY, $packed);
659 print STDERR "ioctl failed: $!\n";
660 } elsif ($rc eq "0 but true") {
661 print "Finished (success)\n";
663 print "ioctl returned error code $rc.\n";
671 my $data = pack("III", $::client_id, $srcid, $tgtid);
674 my $packed = pack("ip", $datalen, $data);
675 my $rc = ioctl(DEV_OBD, &OBD_IOC_MIGR, $packed);
678 print STDERR "ioctl failed: $!\n";
679 } elsif ($rc eq "0 but true") {
680 print "Finished (success)\n";
682 print "ioctl returned error code $rc.\n";
690 my $data = pack("i", $size);
693 my $packed = pack("ip", $datalen, $data);
694 my $rc = ioctl(DEV_OBD, &OBD_IOC_FORMATOBD, $packed);
697 print STDERR "ioctl failed: $!\n";
698 } elsif ($rc eq "0 but true") {
699 print "Finished (success)\n";
701 print "ioctl returned error code $rc.\n";
709 my $data = pack("ii", $partno, $size);
712 my $packed = pack("ip", $datalen, $data);
713 my $rc = ioctl(DEV_OBD, &OBD_IOC_PARTITION, $packed);
716 print STDERR "ioctl failed: $!\n";
717 } elsif ($rc eq "0 but true") {
718 print "Finished (success)\n";
720 print "ioctl returned error code $rc.\n";
730 # XXX we need a getinfo ioctl to validate parameters
733 if ($arg && !defined($::st = stat($arg))) {
734 print "$arg is not a valid device\n";
739 $dev = $::st->rdev() unless $dev;
740 $data = pack("i", $dev);
744 my $packed = pack("ip", $datalen, $data);
745 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP, $packed);
748 print STDERR "ioctl failed: $!\n";
749 } elsif ($rc eq "0 but true") {
750 print "Finished (success)\n";
752 print "ioctl returned error code $rc.\n";
758 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP, $err);
761 print STDERR "ioctl failed: $!\n";
762 } elsif ($rc eq "0 but true") {
763 print "Finished (success)\n";
766 print "ioctl returned error code $rc.\n";
775 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
776 $id = unpack("I", $packed);
779 print STDERR "ioctl failed: $!\n";
780 } elsif ($rc eq "0 but true") {
782 print "Client ID : $id\n";
783 print "Finished (success)\n";
785 print "ioctl returned error code $rc.\n";
797 print "syntax: disconnect [client ID]\n";
798 print "When client ID is not given, the last valid client ID to be returned by a\n";
799 print "connect command this session is used; there is no such ID.\n";
803 my $packed = pack("L", $id);
804 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
807 print STDERR "ioctl failed: $!\n";
808 } elsif ($rc eq "0 but true") {
809 $::client_id = undef;
810 print "Finished (success)\n";
812 print "ioctl returned error code $rc.\n";
822 if (defined($quiet) && $quiet ne "quiet") {
823 print "syntax: create [number of objects [quiet]]\n";
827 my $packed = pack("IL", $::client_id, $prealloc);
828 if (!defined($arg) || scalar($arg) < 2) {
829 print "Creating 1 object...\n";
830 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
831 if (!defined($quiet)) {
832 my $ino = unpack("L", $packed);
833 print "Created object #$ino.\n";
838 print "Creating " . scalar($arg) . " objects...\n";
839 for ($i = 0; $i < scalar($arg); $i++) {
840 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
841 my $ino = unpack("L", $packed);
842 if ($rc ne "0 but true") {
844 $packed = pack("IL", $::client_id, $prealloc);
845 } elsif (!defined($quiet)) {
846 $packed = pack("IL", $::client_id, $prealloc);
847 print "Created object #$ino.\n";
853 print STDERR "ioctl failed: $!\n";
854 } elsif ($rc eq "0 but true") {
855 print "Finished (success)\n";
857 print "ioctl returned error code $rc.\n";
863 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
866 print STDERR "ioctl failed: $!\n";
867 } elsif ($rc eq "0 but true") {
868 print "Finished (success)\n";
870 print "ioctl returned error code $rc.\n";
875 if (!defined($::client_id)) {
876 print "You must first ``connect''.\n";
882 if (!defined($arg) || scalar($arg) < 1) {
883 print "destroy requires the object number to destroy.\n";
887 print "Destroying object $arg...\n";
888 my $packed = pack("IL", $::client_id, $arg);
889 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
892 print STDERR "ioctl failed: $!\n";
893 } elsif ($rc eq "0 but true") {
894 print "Finished (success)\n";
896 print "ioctl returned error code $rc.\n";
901 if (!defined($::client_id)) {
902 print "You must first ``connect''.\n";
908 if (!defined($inode) || scalar($inode) < 1) {
909 print "invalid arguments; type \"help getattr\" for a synopsis\n";
914 my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
916 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
919 print STDERR "ioctl failed: $!\n";
920 } elsif ($rc eq "0 but true") {
921 my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
922 ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
923 unpack("ISssx2lLLLI", $packed);
925 printf("Inode: %d Mode: %o\n", $inode, $mode);
926 printf("User: %6d Group: %6d Size: %d\n", $uid, $gid, $size);
927 printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
928 printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
929 printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
930 printf("flags: %08x\n", $flags);
931 print "Finished (success)\n";
933 print "ioctl returned error code $rc.\n";
938 if (!defined($::client_id)) {
939 print "You must first ``connect''.\n";
945 my $mode = oct(shift);
959 if (defined($size)) {
960 $valid |= &ATTR_SIZE;
962 if (defined($atime)) {
963 $valid |= &ATTR_ATIME;
965 if (defined($mtime)) {
966 $valid |= &ATTR_MTIME;
968 if (defined($ctime)) {
969 $valid |= &ATTR_CTIME;
971 if (defined($mode)) {
972 $valid |= &ATTR_MODE;
975 if (!defined($inode) || scalar($inode) < 1) {
976 print "invalid arguments; type \"help setattr\" for a synopsis\n";
981 # unsigned int ia_valid; (32)
982 # umode_t ia_mode; (16)
985 # -- 16 bit alignment here! --
986 # off_t ia_size; (32)
987 # time_t ia_atime; (32)
988 # time_t ia_mtime; (32)
989 # time_t ia_ctime; (32)
990 # unsigned int ia_attr_flags; (32)
993 printf "valid is %x, mode is %o\n", $valid, $mode;
994 my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
995 $uid, $gid, $size, $atime, $mtime, $ctime, 0);
996 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
999 print STDERR "ioctl failed: $!\n";
1000 } elsif ($rc eq "0 but true") {
1001 print "Finished (success)\n";
1003 print "ioctl returned error code $rc.\n";
1008 if (!defined($::client_id)) {
1009 print "You must first ``connect''.\n";
1017 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
1018 $count < 1 || (defined($offset) && $offset < 0)) {
1019 print "invalid arguments; type \"help read\" for a synopsis\n";
1023 if (!defined($offset)) {
1027 print("Reading $count bytes starting at byte $offset from object " .
1030 # "allocate" a large enough buffer
1031 my $buf = sprintf("%${count}s", " ");
1032 die "suck" if (length($buf) != $count);
1034 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1035 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
1037 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
1039 $retval = unpack("l", $packed);
1042 print STDERR "ioctl failed: $!\n";
1043 } elsif ($rc eq "0 but true") {
1045 print substr($buf, 0, $retval);
1046 print "\nRead $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";
1057 if (!defined($::client_id)) {
1058 print "You must first ``connect''.\n";
1066 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
1067 $count < 1 || (defined($offset) && $offset < 0)) {
1068 print "invalid arguments; type \"help read\" for a synopsis\n";
1072 if (!defined($offset)) {
1076 print("Reading $count bytes starting at byte $offset from object " .
1079 # "allocate" a large enough buffer
1080 my $buf = sprintf("%${count}s", " ");
1081 die "suck" if (length($buf) != $count);
1083 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1084 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
1086 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed);
1088 $retval = unpack("l", $packed);
1091 print STDERR "ioctl failed: $!\n";
1092 } elsif ($rc eq "0 but true") {
1094 print substr($buf, 0, $retval);
1095 print "\nRead $retval of an attempted $count bytes.\n";
1096 print "Finished (success)\n";
1098 print "Finished (error $retval)\n";
1101 print "ioctl returned error code $rc.\n";
1106 if (!defined($::client_id)) {
1107 print "You must first ``connect''.\n";
1113 my $text = join(' ', @_);
1114 my $count = length($text);
1116 if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
1117 scalar($offset) < 0) {
1118 print "invalid arguments; type \"help write\" for a synopsis\n";
1122 if (!defined($text)) {
1127 print("Writing $count bytes starting at byte $offset to object " .
1130 # the perl we're using doesn't support pack type Q
1131 my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, $offset, 0);
1132 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
1134 $retval = unpack("l", $packed);
1137 print STDERR "ioctl failed: $!\n";
1138 } elsif ($rc eq "0 but true") {
1140 print "\nWrote $retval of an attempted $count bytes.\n";
1141 print "Finished (success)\n";
1143 print "Finished (error $retval)\n";
1146 print "ioctl returned error code $rc.\n";
1153 if (!defined($::client_id)) {
1154 print "You must first ``connect''.\n";
1158 if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
1162 print "Preallocating $arg inodes...\n";
1163 my $packed = pack("LLx128", $::client_id, $arg);
1164 # client id, alloc, inodes[32]
1166 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
1169 print STDERR "ioctl failed: $!\n";
1170 } elsif ($rc eq "0 but true") {
1171 my $alloc = unpack("x4L", $packed);
1172 my @inodes = unpack("x8L32", $packed);
1175 print "Got $alloc inodes: ";
1176 foreach $i (@inodes) {
1179 print "\nFinished (success)\n";
1181 print "ioctl returned error code $rc.\n";
1186 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
1189 print STDERR "ioctl failed: $!\n";
1190 } elsif ($rc eq "0 but true") {
1191 print "Finished (success)\n";
1193 print "ioctl returned error code $rc.\n";
1198 if (!defined($::client_id)) {
1199 print "You must first ``connect''.\n";
1211 # __kernel_fsid_t f_fsid; (64 bits)
1216 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1219 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
1222 print STDERR "ioctl failed: $!\n";
1223 } elsif ($rc eq "0 but true") {
1224 # skip both the conn_id and the fs_type in the buffer
1225 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
1226 unpack("x4x4LLLLLL", $packed);
1227 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
1228 "$bfree free ($bavail available).\n");
1229 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
1230 print "Finished (success)\n";
1232 print "ioctl returned error code $rc.\n";
1239 if ( !$arg || !$commands{$arg} ) {
1240 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
1242 print "Usage: " . $commands{$arg}->{doc} . "\n";
1248 print "Disconnecting active session ($::client_id)...";
1249 Disconnect($::client_id);