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 'filesystem' => {func => "Filesystem", doc => "filesystem <dev>: partition for direct OBD device"},
106 'create' => {func => "Create", doc => "create: creates a new inode"},
107 'attach' => {func => "Attach", doc => "attach type [adapter bus tid lun]"},
108 'detach' => {func => "Detach", doc => "detach this device"},
109 'testext2iterator' => {func => "TestExt2Iterator", doc => ""},
110 'snapattach' => {func => "SnapAttach", doc => "snapattach snapdev snapidx tableno"},
111 'snapset' => {func => "SnapSetTable", doc => "snapset tableno file" },
112 'snapprint' => {func => "SnapPrint", doc => "snapprint tableno"},
113 'snapdelete' => {func => "SnapDelete", doc => "snapdelete: delete connected snap obd"},
114 'snaprestore' => {func => "SnapRestore", doc => "snaprestore: restore connected old snap to current"},
115 'snaptable' => {func => "SnapShotTable", doc => "snaptable: build a snapshot table (interactive)"},
116 'copy' => {func => "Copy", doc => "copy srcid tgtid"},
117 'migrate' => {func => "Migrate", doc => "migrate srcid tgtid"},
118 'format' => {func => "Format", doc => "format type adapter bus tid lun size"},
119 'partition' => {func => "Partition", doc => "partition type adapter bus tid lun partition size"},
120 'setup' => {func => "Setup", doc => "setup [type]: link this OBD device to the underlying device (default type ext2_obd, filesystem /dev/loop0)"},
121 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
122 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
123 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
124 'destroy' => {func => "Destroy", doc => "destroy <inode>: destroys an inode"},
125 'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
126 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
127 'read' => {func => "Read", doc => "read <inode> <count> [offset]"},
128 'fsread' => {func => "Read2", doc => "read <inode> <count> [offset]"},
129 'write' => {func => "Write", doc => "write <inode> <offset> <text>"},
130 'setattr' => {func => "Setattr", doc => "setattr <inode> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"},
131 'getattr' => {func => "Getattr", doc => "getattr <inode>: displays inode object attributes"},
132 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num inodes."},
133 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
134 'help' => {func => \&Help, doc => "help: this message"},
135 'quit' => {func => \&Quit, doc => "see \"exit\""},
136 'exit' => {func => \&Quit, doc => "see \"quit\""}
140 # setup completion function
142 my @jcm_cmd_list = keys %commands;
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));
238 # select the OBD device we talk to
243 print "Disconnecting active session ($::client_id)...";
244 Disconnect($::client_id);
247 $device = "/dev/obd0";
250 # Open the device, as we need an FD for the ioctl
251 sysopen(DEV_OBD, $device, 0) || die "Cannot open $device";
252 print "Device now $device\n";
264 print "Usage \"attach type (ext2_obd)\";
266 if ($type eq "obdscsi" ) {
271 $data = pack("iiiii", $adapter, $bus, $tid, $lun, $size);
275 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
277 my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
280 print STDERR "ioctl failed: $!\n";
281 } elsif ($rc eq "0 but true") {
282 print "Finished (success)\n";
284 print "ioctl returned error code $rc.\n";
291 my $rc = ioctl(DEV_OBD, &OBD_IOC_DETACH, $data);
294 print STDERR "ioctl failed: $!\n";
295 } elsif ($rc eq "0 but true") {
296 print "Finished (success)\n";
298 print "ioctl returned error code $rc.\n";
303 sub TestExt2Iterator {
304 if (!defined($::client_id)) {
305 print "You must first ``connect''.\n";
310 my $type = "ext2_obd";
312 $data = pack("i", 4711); # bogus data
315 my $len = length($type);
316 my $cl = length($data);
317 my $add = pack("p", $data);
318 print "type $type (len $len), datalen $datalen ($cl)\n";
319 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
321 my $rc = ioctl(DEV_OBD, &OBD_EXT2_RUNIT, $packed);
324 print STDERR "ioctl failed: $!\n";
325 } elsif ($rc eq "0 but true") {
326 print "Finished (success)\n";
328 print "ioctl returned error code $rc.\n";
334 if (!defined($::client_id)) {
335 print "You must first ``connect''.\n";
340 my $type = "snap_obd";
342 $data = pack("i", 4711); # bogus data
345 my $len = length($type);
346 my $cl = length($data);
347 my $add = pack("p", $data);
348 print "type $type (len $len), datalen $datalen ($cl)\n";
349 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
351 # XXX We need to fix this up so that after the objects in this snapshot
352 # are deleted, the snapshot itself is also removed from the table.
353 my $rc = ioctl(DEV_OBD, &OBD_SNAP_DELETE, $packed);
356 print STDERR "ioctl failed: $!\n";
357 } elsif ($rc eq "0 but true") {
358 print "Finished (success)\n";
360 print "ioctl returned error code $rc.\n";
365 # XXX the parameter to snaprestore SHOULD be the snapshot number of the
366 # snapshot to be restored, and this routine should handle all of the
367 # removal of intervening snapshots and changing the snaptable to have
368 # the "restored" snapshot as current and move the previous current.
370 if (!defined($::client_id)) {
371 print "You must first ``connect''.\n";
376 my $type = "snap_obd";
377 my $prevcurrent = shift;
379 $data = pack("i", $prevcurrent); # where the previous current snapshot now lives
382 my $len = length($type);
383 my $cl = length($data);
384 my $add = pack("p", $data);
385 print "type $type (len $len), datalen $datalen ($cl)\n";
386 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
388 my $rc = ioctl(DEV_OBD, &OBD_SNAP_RESTORE, $packed);
391 print STDERR "ioctl failed: $!\n";
392 } elsif ($rc eq "0 but true") {
393 print "Finished (success)\n";
395 print "ioctl returned error code $rc.\n";
403 my $type = "snap_obd";
404 my $snaptableno = shift;
406 $data = pack("i", $snaptableno);
409 my $len = length($type);
410 my $cl = length($data);
411 my $add = pack("p", $data);
412 print "type $type (len $len), datalen $datalen ($cl)\n";
413 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
415 my $rc = ioctl(DEV_OBD, &OBD_SNAP_PRINTTABLE, $packed);
418 print STDERR "ioctl failed: $!\n";
419 } elsif ($rc eq "0 but true") {
420 print "Finished (success)\n";
422 print "ioctl returned error code $rc.\n";
428 my $type = "snap_obd";
429 my $snaptableno = shift;
437 print "No such file $file\n";
440 $table = ReadSnapShotTable($file);
442 $snapcount = keys %{$table};
443 print "Snapcount $snapcount\n";
445 if ( ! defined $table->{0} ) {
446 print "No current snapshot in table! First make one\n";
449 $data = pack("ii", $snaptableno, $snapcount);
451 foreach my $time (sort keys %{$table}) {
452 $data .= pack("Ii", $time, $table->{$time});
456 my $len = length($type);
457 my $cl = length($data);
458 my $add = pack("p", $data);
459 print "type $type (len $len), datalen $datalen ($cl)\n";
460 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
462 my $rc = ioctl(DEV_OBD, &OBD_SNAP_SETTABLE, $packed);
465 print STDERR "ioctl failed: $!\n";
466 } elsif ($rc eq "0 but true") {
467 print "Finished (success)\n";
469 print "ioctl returned error code $rc.\n";
476 my $type = "snap_obd";
484 $data = pack("iii", $snapdev, $snapidx, $tableno);
487 my $len = length($type);
488 my $cl = length($data);
489 my $add = pack("p", $data);
490 print "type $type (len $len), datalen $datalen ($cl)\n";
491 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
493 my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
496 print STDERR "ioctl failed: $!\n";
497 } elsif ($rc eq "0 but true") {
498 print "Finished (success)\n";
500 print "ioctl returned error code $rc.\n";
507 my $file = &readl("enter file name: ");
511 my $table = ReadSnapShotTable($file);
514 PrintSnapShotTable($table);
515 my $action = &readl("Add, Delete or Quit [adq]: ");
516 goto done if ($action =~ "^q.*" );
517 goto add if ($action =~ "^a.*");
518 goto del if ($action =~ "^d.*");
522 my $idx = &readl("enter index where you want this snapshot: ");
523 my $time = &readl("enter time or 'now' or 'current': ");
524 my $oldtime = SnapFindTimeFromIdx($idx, $table);
525 if (defined $oldtime) {
526 print "This already exists, first clean up\n";
530 if ( $time eq 'now' ) {
532 } elsif ( $time eq 'current' ) {
535 $table->{$time} = $idx;
539 $didx = &readl("Enter index to delete: ");
540 my $deltime = SnapFindTimeFromIdx($didx, $table);
541 delete $table->{$deltime} if defined $deltime;
545 my $ok = &readl("OK with new table? [Yn]: ");
546 unless ( $ok eq "no" ) {
547 WriteSnapShotTable($file, $table);
551 sub SnapFindTimeFromIdx {
555 foreach my $time ( keys %{$table} ) {
556 if ( $table->{$time} == $idx ) {
563 sub PrintSnapShotTable {
567 foreach $time ( sort keys %{$table} ) {
568 my $stime = localtime($time);
572 printf "Time: %s -- Index %d\n", $stime, $table->{$time};
576 sub ReadSnapShotTable {
583 my ($time, $index) = split ;
584 $table->{$time} = $index;
588 PrintSnapShotTable($table);
593 sub WriteSnapShotTable {
598 foreach my $time ( sort keys %{$table} ) {
599 print FH "$time $table->{$time}\n";
608 my $data = pack("III", $::client_id, $srcid, $tgtid);
611 my $packed = pack("ip", $datalen, $data);
612 my $rc = ioctl(DEV_OBD, &OBD_IOC_COPY, $packed);
615 print STDERR "ioctl failed: $!\n";
616 } elsif ($rc eq "0 but true") {
617 print "Finished (success)\n";
619 print "ioctl returned error code $rc.\n";
627 my $data = pack("III", $::client_id, $srcid, $tgtid);
630 my $packed = pack("ip", $datalen, $data);
631 my $rc = ioctl(DEV_OBD, &OBD_IOC_MIGR, $packed);
634 print STDERR "ioctl failed: $!\n";
635 } elsif ($rc eq "0 but true") {
636 print "Finished (success)\n";
638 print "ioctl returned error code $rc.\n";
646 my $data = pack("i", $size);
649 my $packed = pack("ip", $datalen, $data);
650 my $rc = ioctl(DEV_OBD, &OBD_IOC_FORMATOBD, $packed);
653 print STDERR "ioctl failed: $!\n";
654 } elsif ($rc eq "0 but true") {
655 print "Finished (success)\n";
657 print "ioctl returned error code $rc.\n";
665 my $data = pack("ii", $partno, $size);
668 my $packed = pack("ip", $datalen, $data);
669 my $rc = ioctl(DEV_OBD, &OBD_IOC_PARTITION, $packed);
672 print STDERR "ioctl failed: $!\n";
673 } elsif ($rc eq "0 but true") {
674 print "Finished (success)\n";
676 print "ioctl returned error code $rc.\n";
686 $type = "ext2_obd" unless $type;
688 if ( $type eq "ext2_obd" ) {
691 $dev = $st->rdev() unless $dev;
692 $data = pack("i", $dev);
696 my $packed = pack("ip", $datalen, $data);
697 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP, $packed);
700 print STDERR "ioctl failed: $!\n";
701 } elsif ($rc eq "0 but true") {
702 print "Finished (success)\n";
704 print "ioctl returned error code $rc.\n";
710 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP, $err);
713 print STDERR "ioctl failed: $!\n";
714 } elsif ($rc eq "0 but true") {
715 print "Finished (success)\n";
718 print "ioctl returned error code $rc.\n";
727 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
728 $id = unpack("I", $packed);
731 print STDERR "ioctl failed: $!\n";
732 } elsif ($rc eq "0 but true") {
734 print "Client ID : $id\n";
735 print "Finished (success)\n";
737 print "ioctl returned error code $rc.\n";
749 print "syntax: disconnect [client ID]\n";
750 print "When client ID is not given, the last valid client ID to be returned by a\n";
751 print "connect command this session is used; there is no such ID.\n";
755 my $packed = pack("L", $id);
756 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
759 print STDERR "ioctl failed: $!\n";
760 } elsif ($rc eq "0 but true") {
761 $::client_id = undef;
762 print "Finished (success)\n";
764 print "ioctl returned error code $rc.\n";
774 if (defined($quiet) && !($quiet eq "quiet")) {
775 print "syntax: create [number of objects [quiet]]\n";
779 my $packed = pack("IL", $::client_id, $prealloc);
780 if (!defined($arg) || scalar($arg) < 2) {
781 print "Creating 1 object...\n";
782 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
783 if (!defined($quiet)) {
784 my $ino = unpack("L", $packed);
785 print "Created object #$ino.\n";
790 print "Creating " . scalar($arg) . " objects...\n";
791 for ($i = 0; $i < scalar($arg); $i++) {
792 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
793 my $ino = unpack("L", $packed);
794 if (!($rc eq "0 but true")) {
796 $packed = pack("IL", $::client_id, $prealloc);
797 } elsif (!defined($quiet)) {
798 $packed = pack("IL", $::client_id, $prealloc);
799 print "Created object #$ino.\n";
805 print STDERR "ioctl failed: $!\n";
806 } elsif ($rc eq "0 but true") {
807 print "Finished (success)\n";
809 print "ioctl returned error code $rc.\n";
815 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
818 print STDERR "ioctl failed: $!\n";
819 } elsif ($rc eq "0 but true") {
820 print "Finished (success)\n";
822 print "ioctl returned error code $rc.\n";
827 if (!defined($::client_id)) {
828 print "You must first ``connect''.\n";
834 if (!defined($arg) || scalar($arg) < 1) {
835 print "destroy requires the object number to destroy.\n";
839 print "Destroying object $arg...\n";
840 my $packed = pack("IL", $::client_id, $arg);
841 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
844 print STDERR "ioctl failed: $!\n";
845 } elsif ($rc eq "0 but true") {
846 print "Finished (success)\n";
848 print "ioctl returned error code $rc.\n";
853 if (!defined($::client_id)) {
854 print "You must first ``connect''.\n";
860 if (!defined($inode) || scalar($inode) < 1) {
861 print "invalid arguments; type \"help getattr\" for a synopsis\n";
866 my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
868 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
871 print STDERR "ioctl failed: $!\n";
872 } elsif ($rc eq "0 but true") {
873 my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
874 ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
875 unpack("ISssx2lLLLI", $packed);
877 printf("Inode: %d Mode: %o\n", $inode, $mode);
878 printf("User: %6d Group: %6d Size: %d\n", $uid, $gid, $size);
879 printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
880 printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
881 printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
882 printf("flags: %08x\n", $flags);
883 print "Finished (success)\n";
885 print "ioctl returned error code $rc.\n";
890 if (!defined($::client_id)) {
891 print "You must first ``connect''.\n";
897 my $mode = oct(shift);
911 if (defined($size)) {
912 $valid |= &ATTR_SIZE;
914 if (defined($atime)) {
915 $valid |= &ATTR_ATIME;
917 if (defined($mtime)) {
918 $valid |= &ATTR_MTIME;
920 if (defined($ctime)) {
921 $valid |= &ATTR_CTIME;
923 if (defined($mode)) {
924 $valid |= &ATTR_MODE;
927 if (!defined($inode) || scalar($inode) < 1) {
928 print "invalid arguments; type \"help setattr\" for a synopsis\n";
933 # unsigned int ia_valid; (32)
934 # umode_t ia_mode; (16)
937 # -- 16 bit alignment here! --
938 # off_t ia_size; (32)
939 # time_t ia_atime; (32)
940 # time_t ia_mtime; (32)
941 # time_t ia_ctime; (32)
942 # unsigned int ia_attr_flags; (32)
945 printf "valid is %x, mode is %o\n", $valid, $mode;
946 my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
947 $uid, $gid, $size, $atime, $mtime, $ctime, 0);
948 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
951 print STDERR "ioctl failed: $!\n";
952 } elsif ($rc eq "0 but true") {
953 print "Finished (success)\n";
955 print "ioctl returned error code $rc.\n";
960 if (!defined($::client_id)) {
961 print "You must first ``connect''.\n";
969 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
970 $count < 1 || (defined($offset) && $offset < 0)) {
971 print "invalid arguments; type \"help read\" for a synopsis\n";
975 if (!defined($offset)) {
979 print("Reading $count bytes starting at byte $offset from object " .
982 # "allocate" a large enough buffer
983 my $buf = sprintf("%${count}s", " ");
984 die "suck" if (length($buf) != $count);
986 # the perl we're using doesn't support pack type Q, and offset is 64 bits
987 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
989 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
991 $retval = unpack("l", $packed);
994 print STDERR "ioctl failed: $!\n";
995 } elsif ($rc eq "0 but true") {
997 print substr($buf, 0, $retval);
998 print "\nRead $retval of an attempted $count bytes.\n";
999 print "Finished (success)\n";
1001 print "Finished (error $retval)\n";
1004 print "ioctl returned error code $rc.\n";
1009 if (!defined($::client_id)) {
1010 print "You must first ``connect''.\n";
1018 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
1019 $count < 1 || (defined($offset) && $offset < 0)) {
1020 print "invalid arguments; type \"help read\" for a synopsis\n";
1024 if (!defined($offset)) {
1028 print("Reading $count bytes starting at byte $offset from object " .
1031 # "allocate" a large enough buffer
1032 my $buf = sprintf("%${count}s", " ");
1033 die "suck" if (length($buf) != $count);
1035 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1036 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
1038 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed);
1040 $retval = unpack("l", $packed);
1043 print STDERR "ioctl failed: $!\n";
1044 } elsif ($rc eq "0 but true") {
1046 print substr($buf, 0, $retval);
1047 print "\nRead $retval of an attempted $count bytes.\n";
1048 print "Finished (success)\n";
1050 print "Finished (error $retval)\n";
1053 print "ioctl returned error code $rc.\n";
1058 if (!defined($::client_id)) {
1059 print "You must first ``connect''.\n";
1065 my $text = join(' ', @_);
1066 my $count = length($text);
1068 if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
1069 scalar($offset) < 0) {
1070 print "invalid arguments; type \"help write\" for a synopsis\n";
1074 if (!defined($text)) {
1079 print("Writing $count bytes starting at byte $offset to object " .
1082 # the perl we're using doesn't support pack type Q
1083 my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, $offset, 0);
1084 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
1086 $retval = unpack("l", $packed);
1089 print STDERR "ioctl failed: $!\n";
1090 } elsif ($rc eq "0 but true") {
1092 print "\nWrote $retval of an attempted $count bytes.\n";
1093 print "Finished (success)\n";
1095 print "Finished (error $retval)\n";
1098 print "ioctl returned error code $rc.\n";
1105 if (!defined($::client_id)) {
1106 print "You must first ``connect''.\n";
1110 if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
1114 print "Preallocating $arg inodes...\n";
1115 my $packed = pack("LLx128", $::client_id, $arg);
1116 # client id, alloc, inodes[32]
1118 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
1121 print STDERR "ioctl failed: $!\n";
1122 } elsif ($rc eq "0 but true") {
1123 my $alloc = unpack("x4L", $packed);
1124 my @inodes = unpack("x8L32", $packed);
1127 print "Got $alloc inodes: ";
1128 foreach $i (@inodes) {
1131 print "\nFinished (success)\n";
1133 print "ioctl returned error code $rc.\n";
1138 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
1141 print STDERR "ioctl failed: $!\n";
1142 } elsif ($rc eq "0 but true") {
1143 print "Finished (success)\n";
1145 print "ioctl returned error code $rc.\n";
1150 if (!defined($::client_id)) {
1151 print "You must first ``connect''.\n";
1163 # __kernel_fsid_t f_fsid; (64 bits)
1168 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1171 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
1174 print STDERR "ioctl failed: $!\n";
1175 } elsif ($rc eq "0 but true") {
1176 # skip both the conn_id and the fs_type in the buffer
1177 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
1178 unpack("x4x4LLLLLL", $packed);
1179 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
1180 "$bfree free ($bavail available).\n");
1181 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
1182 print "Finished (success)\n";
1184 print "ioctl returned error code $rc.\n";
1191 if ( !$arg || !$commands{$arg} ) {
1192 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
1194 print "Usage: " . $commands{$arg}->{doc} . "\n";
1200 print "Disconnecting active session ($::client_id)...";
1201 Disconnect($::client_id);