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";
653 # Note: _copy IOCTL takes parameters as dst, src.
654 # Copy function takes parameters as src, dst.
655 my $data = pack("III", $::client_id, $dstid, $srcid);
658 my $packed = pack("ip", $datalen, $data);
659 my $rc = ioctl(DEV_OBD, &OBD_IOC_COPY, $packed);
662 print STDERR "ioctl failed: $!\n";
663 } elsif ($rc eq "0 but true") {
664 print "Finished (success)\n";
666 print "ioctl returned error code $rc.\n";
675 # Note: _migr IOCTL takes parameters as dst, src.
676 # Migrate function takes parameters as src, dst.
677 my $data = pack("III", $::client_id, $dstid, $srcid);
680 my $packed = pack("ip", $datalen, $data);
681 my $rc = ioctl(DEV_OBD, &OBD_IOC_MIGR, $packed);
684 print STDERR "ioctl failed: $!\n";
685 } elsif ($rc eq "0 but true") {
686 print "Finished (success)\n";
688 print "ioctl returned error code $rc.\n";
696 my $data = pack("i", $size);
699 my $packed = pack("ip", $datalen, $data);
700 my $rc = ioctl(DEV_OBD, &OBD_IOC_FORMATOBD, $packed);
703 print STDERR "ioctl failed: $!\n";
704 } elsif ($rc eq "0 but true") {
705 print "Finished (success)\n";
707 print "ioctl returned error code $rc.\n";
715 my $data = pack("ii", $partno, $size);
718 my $packed = pack("ip", $datalen, $data);
719 my $rc = ioctl(DEV_OBD, &OBD_IOC_PARTITION, $packed);
722 print STDERR "ioctl failed: $!\n";
723 } elsif ($rc eq "0 but true") {
724 print "Finished (success)\n";
726 print "ioctl returned error code $rc.\n";
736 # XXX we need a getinfo ioctl to validate parameters
739 if ($arg && !defined($::st = stat($arg))) {
740 print "$arg is not a valid device\n";
745 $dev = $::st->rdev() unless $dev;
746 $data = pack("i", $dev);
750 my $packed = pack("ip", $datalen, $data);
751 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP, $packed);
754 print STDERR "ioctl failed: $!\n";
755 } elsif ($rc eq "0 but true") {
756 print "Finished (success)\n";
758 print "ioctl returned error code $rc.\n";
764 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP, $err);
767 print STDERR "ioctl failed: $!\n";
768 } elsif ($rc eq "0 but true") {
769 print "Finished (success)\n";
772 print "ioctl returned error code $rc.\n";
781 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
782 $id = unpack("I", $packed);
785 print STDERR "ioctl failed: $!\n";
786 } elsif ($rc eq "0 but true") {
788 print "Client ID : $id\n";
789 print "Finished (success)\n";
791 print "ioctl returned error code $rc.\n";
803 print "syntax: disconnect [client ID]\n";
804 print "When client ID is not given, the last valid client ID to be returned by a\n";
805 print "connect command this session is used; there is no such ID.\n";
809 my $packed = pack("L", $id);
810 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
813 print STDERR "ioctl failed: $!\n";
814 } elsif ($rc eq "0 but true") {
815 $::client_id = undef;
816 print "Finished (success)\n";
818 print "ioctl returned error code $rc.\n";
828 if (defined($quiet) && $quiet ne "quiet") {
829 print "syntax: create [number of objects [quiet]]\n";
833 my $packed = pack("IL", $::client_id, $prealloc);
834 if (!defined($arg) || scalar($arg) < 2) {
835 print "Creating 1 object...\n";
836 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
837 if (!defined($quiet)) {
838 my $ino = unpack("L", $packed);
839 print "Created object #$ino.\n";
844 print "Creating " . scalar($arg) . " objects...\n";
845 for ($i = 0; $i < scalar($arg); $i++) {
846 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
847 my $ino = unpack("L", $packed);
848 if ($rc ne "0 but true") {
850 $packed = pack("IL", $::client_id, $prealloc);
851 } elsif (!defined($quiet)) {
852 $packed = pack("IL", $::client_id, $prealloc);
853 print "Created object #$ino.\n";
859 print STDERR "ioctl failed: $!\n";
860 } elsif ($rc eq "0 but true") {
861 print "Finished (success)\n";
863 print "ioctl returned error code $rc.\n";
869 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
872 print STDERR "ioctl failed: $!\n";
873 } elsif ($rc eq "0 but true") {
874 print "Finished (success)\n";
876 print "ioctl returned error code $rc.\n";
881 if (!defined($::client_id)) {
882 print "You must first ``connect''.\n";
888 if (!defined($arg) || scalar($arg) < 1) {
889 print "destroy requires the object number to destroy.\n";
893 print "Destroying object $arg...\n";
894 my $packed = pack("IL", $::client_id, $arg);
895 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
898 print STDERR "ioctl failed: $!\n";
899 } elsif ($rc eq "0 but true") {
900 print "Finished (success)\n";
902 print "ioctl returned error code $rc.\n";
907 if (!defined($::client_id)) {
908 print "You must first ``connect''.\n";
914 if (!defined($inode) || scalar($inode) < 1) {
915 print "invalid arguments; type \"help getattr\" for a synopsis\n";
920 my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
922 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
925 print STDERR "ioctl failed: $!\n";
926 } elsif ($rc eq "0 but true") {
927 my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
928 ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
929 unpack("ISssx2lLLLI", $packed);
931 printf("Inode: %d Mode: %o\n", $inode, $mode);
932 printf("User: %6d Group: %6d Size: %d\n", $uid, $gid, $size);
933 printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
934 printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
935 printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
936 printf("flags: %08x\n", $flags);
937 print "Finished (success)\n";
939 print "ioctl returned error code $rc.\n";
944 if (!defined($::client_id)) {
945 print "You must first ``connect''.\n";
951 my $mode = oct(shift);
965 if (defined($size)) {
966 $valid |= &ATTR_SIZE;
968 if (defined($atime)) {
969 $valid |= &ATTR_ATIME;
971 if (defined($mtime)) {
972 $valid |= &ATTR_MTIME;
974 if (defined($ctime)) {
975 $valid |= &ATTR_CTIME;
977 if (defined($mode)) {
978 $valid |= &ATTR_MODE;
981 if (!defined($inode) || scalar($inode) < 1) {
982 print "invalid arguments; type \"help setattr\" for a synopsis\n";
987 # unsigned int ia_valid; (32)
988 # umode_t ia_mode; (16)
991 # -- 16 bit alignment here! --
992 # off_t ia_size; (32)
993 # time_t ia_atime; (32)
994 # time_t ia_mtime; (32)
995 # time_t ia_ctime; (32)
996 # unsigned int ia_attr_flags; (32)
999 printf "valid is %x, mode is %o\n", $valid, $mode;
1000 my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
1001 $uid, $gid, $size, $atime, $mtime, $ctime, 0);
1002 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
1005 print STDERR "ioctl failed: $!\n";
1006 } elsif ($rc eq "0 but true") {
1007 print "Finished (success)\n";
1009 print "ioctl returned error code $rc.\n";
1014 if (!defined($::client_id)) {
1015 print "You must first ``connect''.\n";
1023 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
1024 $count < 1 || (defined($offset) && $offset < 0)) {
1025 print "invalid arguments; type \"help read\" for a synopsis\n";
1029 if (!defined($offset)) {
1033 print("Reading $count bytes starting at byte $offset from object " .
1036 # "allocate" a large enough buffer
1037 my $buf = sprintf("%${count}s", " ");
1038 die "suck" if (length($buf) != $count);
1040 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1041 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
1043 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
1045 $retval = unpack("l", $packed);
1048 print STDERR "ioctl failed: $!\n";
1049 } elsif ($rc eq "0 but true") {
1051 print substr($buf, 0, $retval);
1052 print "\nRead $retval of an attempted $count bytes.\n";
1053 print "Finished (success)\n";
1055 print "Finished (error $retval)\n";
1058 print "ioctl returned error code $rc.\n";
1063 if (!defined($::client_id)) {
1064 print "You must first ``connect''.\n";
1072 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
1073 $count < 1 || (defined($offset) && $offset < 0)) {
1074 print "invalid arguments; type \"help read\" for a synopsis\n";
1078 if (!defined($offset)) {
1082 print("Reading $count bytes starting at byte $offset from object " .
1085 # "allocate" a large enough buffer
1086 my $buf = sprintf("%${count}s", " ");
1087 die "suck" if (length($buf) != $count);
1089 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1090 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
1092 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed);
1094 $retval = unpack("l", $packed);
1097 print STDERR "ioctl failed: $!\n";
1098 } elsif ($rc eq "0 but true") {
1100 print substr($buf, 0, $retval);
1101 print "\nRead $retval of an attempted $count bytes.\n";
1102 print "Finished (success)\n";
1104 print "Finished (error $retval)\n";
1107 print "ioctl returned error code $rc.\n";
1112 if (!defined($::client_id)) {
1113 print "You must first ``connect''.\n";
1119 my $text = join(' ', @_);
1120 my $count = length($text);
1122 if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
1123 scalar($offset) < 0) {
1124 print "invalid arguments; type \"help write\" for a synopsis\n";
1128 if (!defined($text)) {
1133 print("Writing $count bytes starting at byte $offset to object " .
1136 # the perl we're using doesn't support pack type Q
1137 my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, $offset, 0);
1138 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
1140 $retval = unpack("l", $packed);
1143 print STDERR "ioctl failed: $!\n";
1144 } elsif ($rc eq "0 but true") {
1146 print "\nWrote $retval of an attempted $count bytes.\n";
1147 print "Finished (success)\n";
1149 print "Finished (error $retval)\n";
1152 print "ioctl returned error code $rc.\n";
1159 if (!defined($::client_id)) {
1160 print "You must first ``connect''.\n";
1164 if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
1168 print "Preallocating $arg inodes...\n";
1169 my $packed = pack("LLx128", $::client_id, $arg);
1170 # client id, alloc, inodes[32]
1172 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
1175 print STDERR "ioctl failed: $!\n";
1176 } elsif ($rc eq "0 but true") {
1177 my $alloc = unpack("x4L", $packed);
1178 my @inodes = unpack("x8L32", $packed);
1181 print "Got $alloc inodes: ";
1182 foreach $i (@inodes) {
1185 print "\nFinished (success)\n";
1187 print "ioctl returned error code $rc.\n";
1192 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
1195 print STDERR "ioctl failed: $!\n";
1196 } elsif ($rc eq "0 but true") {
1197 print "Finished (success)\n";
1199 print "ioctl returned error code $rc.\n";
1204 if (!defined($::client_id)) {
1205 print "You must first ``connect''.\n";
1217 # __kernel_fsid_t f_fsid; (64 bits)
1222 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1225 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
1228 print STDERR "ioctl failed: $!\n";
1229 } elsif ($rc eq "0 but true") {
1230 # skip both the conn_id and the fs_type in the buffer
1231 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
1232 unpack("x4x4LLLLLL", $packed);
1233 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
1234 "$bfree free ($bavail available).\n");
1235 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
1236 print "Finished (success)\n";
1238 print "ioctl returned error code $rc.\n";
1245 if ( !$arg || !$commands{$arg} ) {
1246 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
1248 print "Usage: " . $commands{$arg}->{doc} . "\n";
1254 print "Disconnecting active session ($::client_id)...";
1255 Disconnect($::client_id);