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_IOC_PUNCH () { &_IOC(3, ord(\'f\'), 24, 4);}' unless
65 defined(&OBD_IOC_PUNCH);
66 eval 'sub OBD_SNAP_SETTABLE () { &_IOC(3, ord(\'f\'), 40, 4);}' unless
67 defined(&OBD_SNAP_SETTABLE);
68 eval 'sub OBD_SNAP_PRINTTABLE () { &_IOC(3, ord(\'f\'), 41, 4);}' unless
69 defined(&OBD_SNAP_PRINTTABLE);
70 eval 'sub OBD_SNAP_DELETE() { &_IOC(3, ord(\'f\'), 42, 4);}' unless
71 defined(&OBD_SNAP_DELETE);
72 eval 'sub OBD_SNAP_RESTORE() { &_IOC(3, ord(\'f\'), 43, 4);}' unless
73 defined(&OBD_SNAP_RESTORE);
75 eval 'sub OBD_EXT2_RUNIT () { &_IOC(3, ord(\'f\'), 61, 4);}' unless
76 defined(&OBD_EXT2_RUNIT);
78 eval 'sub OBD_MD_FLALL () {~0;}' unless defined(&OBD_MD_FLALL);
79 eval 'sub OBD_MD_FLATIME () {1<<1;}' unless defined(&OBD_MD_FLATIME);
80 eval 'sub OBD_MD_FLMTIME () {1<<2;}' unless defined(&OBD_MD_FLMTIME);
81 eval 'sub OBD_MD_FLCTIME () {1<<3;}' unless defined(&OBD_MD_FLCTIME);
82 eval 'sub OBD_MD_FLSIZE () {1<<4;}' unless defined(&OBD_MD_FLSIZE);
83 eval 'sub OBD_MD_FLMODE () {1<<7;}' unless defined(&OBD_MD_FLMODE);
84 eval 'sub OBD_MD_FLUID () {1<<8;}' unless defined(&OBD_MD_FLUID);
85 eval 'sub OBD_MD_FLGID () {1<<9;}' unless defined(&OBD_MD_FLGID);
95 # NOTE long long are layed out in ia32 memory as follows:
96 # u = 0xaaaabbbbccccdddd has ccccdddd at &u and aaaabbbb 4 bytes on
97 # this may be different on other architectures
99 # we use 32-bit integers for all 64-bit quantities in this program
100 # #define OBD_INLINESZ 60
101 # #define OBD_OBDMDSZ 60
102 # /* Note: 64-bit types are 64-bit aligned in structure */
110 # obd_blocks o_blocks;
111 # obd_blksize o_blksize;
116 # obd_flag o_obdflags;
118 # obd_count o_generation;
119 # obd_flag o_valid; /* hot fields in this obdo */
122 # struct list_head o_list;
123 # struct obd_ops *o_op;
128 pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L",
147 0, 0, # struct list_head
172 $obdo->{obdmd}) = unpack "x${offset}Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 L L L L L L L L L a60 a60", $buf;
180 printf "id: %d\ngrp: %d\natime: %s\nmtime: %s\nctime: %s\nsize: %d\nblocks: %d\nblksize: %d\nmode: %o\nuid: %d\ngid: %d\nflags: %x\nobdflags: %x\nnlink: %d\nvalid: %x\ninline: %s\nobdmd: %s\n",
203 GetOptions("f!" => \$file, "device=s" => \$::device, ) || die "Getoptions";
206 # get a console for the app
212 my @procsysobd_objects = ('debug', 'index', 'reset', 'trace', 'vars');
215 ('status' => {func => "Status", doc => "status: show obd device status"},
216 'procsys' => {func => "Procsys", doc => "procsys <file> <value> (set /proc/sys/obd configuration)"},
217 'shell' => {func => "Shell", doc => "shell <shell-command>: execute shell-commands"},
218 'script' => {func => "Script", doc => "script <filename>: read and execute commands from a file"},
219 'insmod' => {func => "Insmod", doc => "insmod <module>: insert kernel module"},
220 'rmmod' => {func => "Rmmod", doc => "rmmod <module>: insert kernel module"},
221 'lsmod' => {func => "Lsmod", doc => "lsmod <module>: list kernel modules"},
222 'device' => {func => "Device", doc => "device <dev>: open another OBD device"},
223 'close' => {func => "Close", doc => "close <dev>: close OBD device"},
224 'create' => {func => "Create", doc => "create [<num> [<mode> [quiet]]]: create new object(s) (files, unless mode is given)"},
225 'attach' => {func => "Attach", doc => "attach { obdext2 | obdsnap snapdev snapidx tableno | obdscsi adapter bus tid lun }: attach this minor device to the specified driver" },
226 'detach' => {func => "Detach", doc => "detach this minor device"},
227 'testext2iterator' => {func => "TestExt2Iterator", doc => "test ext2 iterator function"},
228 'snapset' => {func => "SnapSetTable", doc => "snapset <tableno> <file>: set the table (created with snaptable) as table #tableno" },
229 'snapprint' => {func => "SnapPrint", doc => "snapprint <tableno>: output the contents of table #tableno to the syslog"},
230 'snapdelete' => {func => "SnapDelete", doc => "snapdelete: delete connected snap obd objects from disk"},
231 'snaprestore' => {func => "SnapRestore", doc => "snaprestore : restore connected old snap objects to be current"},
232 'snaptable' => {func => "SnapShotTable", doc => "snaptable: build a snapshot table (interactive)"},
233 'copy' => {func => "Copy", doc => "copy <srcid> <tgtid>: copy objects"},
234 'migrate' => {func => "Migrate", doc => "migrate <srcid> <tgtid>: migrate data from one object to another"},
235 'partition' => {func => "Partition", doc => "partition <type> <adapter> <bus> <tid> <lun> <partition> <size>: create a partition"},
236 'format' => {func => "Format", doc => "format <type> <adapter> <bus> <tid> <lun> <size>: format a partition"},
237 'setup' => {func => "Setup", doc => "setup [type]: link this OBD device to the underlying device (default type obdext2)"},
238 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
239 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
240 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
241 'destroy' => {func => "Destroy", doc => "destroy <id>: destroys an object"},
242 'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
243 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
244 'read' => {func => "Read", doc => "read <id> <count> [offset]: read data from object"},
245 'fsread' => {func => "Read2", doc => "read <id> <count> [offset]: read data from object"},
246 'write' => {func => "Write", doc => "write <id> <offset> <text>: write data to object"},
247 'punch' => {func => "Punch", doc => "punch <id> <start> <count>: punch a hole in object"},
248 'setattr' => {func => "Setattr", doc => "setattr <id> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]: sets object attributes"},
249 'getattr' => {func => "Getattr", doc => "getattr <id>: displays object attributes"},
250 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num objects."},
251 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
252 'help' => {func => \&Help, doc => "help: this message"},
253 'quit' => {func => \&Quit, doc => "see \"exit\""},
254 'exit' => {func => \&Quit, doc => "see \"quit\""}
258 # setup completion function
260 my @jcm_cmd_list = keys %commands;
275 return $term->readline(@_);
284 my $rc = execute_line($_);
285 if ($rc != 0) { last; }
289 $term = new Term::ReadLine 'obdcontrol ';
290 $attribs = $term->Attribs;
291 $attribs->{attempted_completion_function} = \&completeme;
292 $term->ornaments('md,me,,'); # bold face prompt
294 # make sure stdout is not buffered
295 STDOUT->autoflush(1);
298 # Get on with the show
302 #------------------------------------------------------------------------------
304 my ($text, $line, $start, $end) = @_;
305 if (substr($line, 0, $start) =~ /^\s*$/) {
306 if ($] < 5.6) { # PErl version is less than 5.6.0
307 return (exists $commands{$text}) ? $text : 0;
308 #Above line doesn't perform command completion, but
309 #perl5.005 Term-ReadLine lacks support for completion matching
310 #and perl5.6.0 requires glibc2.2.2 that won't run under Redhat6.2......sigh.
313 $attribs->{completion_word} = \@jcm_cmd_list;
314 return $term->completion_matches($text,
315 $attribs->{'list_completion_function'});
323 my @completions = completeme($given, $given, 0, length($given));
324 if ($#completions == 0) {
325 $name = shift @completions;
331 # start making requests
334 $line = $term->readline("obdcontrol > ");
342 my @cmdline = split(' ', $line);
343 my $word = shift @cmdline;
349 $cmd = find_command($word);
352 printf STDERR "$word: No such command, or not unique.\n";
356 if ($cmd eq "help" || $cmd eq "exit" || $cmd eq "quit") {
357 return (&{$commands{$cmd}->{func}}(@cmdline));
361 return (&{$commands{$cmd}->{func}}(@cmdline));
366 # select the OBD device we talk to
370 if ( ! $device && ! $::device ) { # first time ever
371 $device = '/dev/obd0';
374 if (($device) && ($::device ne $device)) {
379 print "Disconnecting active session ($::client_id)...";
380 Disconnect($::client_id);
383 if ($opendevfds{$device}) {
384 $::dev_obd = $opendevfds{$device};
387 # Open the device, as we need an FD for the ioctl
388 if (!sysopen(NEW_OBD, $device, 0)) {
389 print "Cannot open $device. Did you insert the obdclass module ?\n";
392 print "Openend device $device\n";
393 $opendevfds{$device} = *NEW_OBD;
394 $::dev_obd = *NEW_OBD;
398 print "Current device is $::device\n";
406 if ( ! $device && ! $::device ) { # first time ever
407 print "Nothing to close\n";
415 if ($::device eq $device) {
417 print "Disconnecting active session ($::client_id)...";
418 Disconnect($::client_id);
422 $fd2close = $opendevfds{$device};
423 if ($fd2close) { # XXXX something wrong in this if statement
425 $opendevfds{$device} = undef;
426 print "Closed device $device\n";
429 print "Device $device was not open\n";
433 if ($::device eq $device) {
437 print "No current device. You just closed the current device ($device).\n";
442 my $cmdfilename = shift;
444 if ( ! $cmdfilename ) {
445 print "please specify a command file name\n";
448 if (! open(CMDF, $cmdfilename)) {
449 print "Cannot open $cmdfilename: $!\n";
457 $rc = execute_line($_);
459 print "Something went wrong .......command exit status: $rc\n";
468 my $user_shell=$ENV{'SHELL'};
469 print "% $user_shell -c '@_'\n";
471 print "please specify a shell command\n";
474 system("$user_shell -c '@_'");
479 my $oldfh = select(STDOUT);
482 system('cat /proc/lustre/obd/*/status');
492 my $set_sysobd = shift;
495 foreach $i (0 .. $#procsysobd_objects) {
496 my $sysobd = $procsysobd_objects[$i];
498 if (defined $set_sysobd) {
499 if ($sysobd ne $set_sysobd) { next; }
501 if (defined $value) { # set this one
502 system("echo \"$value\" > /proc/sys/obd/$sysobd");
504 system("echo \"/proc/sys/obd/$sysobd:\"; cat /proc/sys/obd/$sysobd");
508 system("echo \"/proc/sys/obd/$sysobd:\"; cat /proc/sys/obd/$sysobd");
516 system("insmod $module");
522 system("rmmod $module");
528 system("lsmod $module");
539 print "error: missing type\n";
541 print "usage: attach {obdext2 | obdsnap | obdscsi | obdtrace }\n";
545 if ($type eq "obdscsi" ) {
551 $data = pack("iiii", $adapter, $bus, $tid, $lun);
553 } elsif ($type eq "obdsnap" ) {
558 $data = pack("iii", $snapdev, $snapidx, $tableno);
560 } elsif ($type eq "obdext2") {
561 $data = pack("i", 4711); # bogus data
563 } elsif ($type eq "obdtrace") {
564 $data = pack("i", 4711); # bogus data
567 print "error: unknown attach type $type\n";
571 my $len = length($type);
572 my $cl = length($data);
574 print "type $type (len $len), datalen $datalen ($cl)\n";
575 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
577 if (! defined $::dev_obd) {
578 print "No current device.\n";
581 my $rc = ioctl($::dev_obd, &OBD_IOC_ATTACH, $packed);
584 print STDERR "ioctl failed: $!\n";
586 } elsif ($rc eq "0 but true") {
587 print "Finished (success)\n";
590 print "ioctl returned error code $rc.\n";
600 if (! defined $::dev_obd) {
601 print "No current device.\n";
605 my $rc = ioctl($::dev_obd, &OBD_IOC_DETACH, $data);
608 print STDERR "ioctl failed: $!\n";
610 } elsif ($rc eq "0 but true") {
611 print "Finished (success)\n";
614 print "ioctl returned error code $rc.\n";
620 sub TestExt2Iterator {
621 if (!defined($::client_id)) {
622 print "You must first ``connect''.\n";
627 my $type = "obdext2";
629 $data = pack("i", 4711); # bogus data
632 my $len = length($type);
633 my $cl = length($data);
634 print "type $type (len $len), datalen $datalen ($cl)\n";
635 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
636 if (! defined $::dev_obd) {
637 print "No current device.\n";
641 my $rc = ioctl($::dev_obd, &OBD_EXT2_RUNIT, $packed);
644 print STDERR "ioctl failed: $!\n";
646 } elsif ($rc eq "0 but true") {
647 print "Finished (success)\n";
650 print "ioctl returned error code $rc.\n";
657 if (!defined($::client_id)) {
658 print "You must first ``connect''.\n";
663 my $type = "obdsnap";
665 $data = pack("i", 4711); # bogus data
668 my $len = length($type);
669 my $cl = length($data);
670 print "type $type (len $len), datalen $datalen ($cl)\n";
671 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
673 # XXX We need to fix this up so that after the objects in this snapshot
674 # are deleted, the snapshot itself is also removed from the table.
676 if (! defined $::dev_obd) {
677 print "No current device.\n";
681 my $rc = ioctl($::dev_obd, &OBD_SNAP_DELETE, $packed);
684 print STDERR "ioctl failed: $!\n";
686 } elsif ($rc eq "0 but true") {
687 print "Finished (success)\n";
690 print "ioctl returned error code $rc.\n";
696 # this routine does the whole job
698 my $restoreto = shift;
699 my $snaptable = shift;
703 # don't do anything until connected
704 if (!defined($::client_id)) {
705 print "You must first ``connect''.\n";
709 if ( ! $snaptable || ! defined $restoreto ) {
710 print "Usage: snaprestore \"restore to slot\" \"snaptable\" \"tableno\"\n";
714 if ( ! -f $snaptable ) {
715 print "Table $snaptable doesn't exist\n";
719 my $table = ReadSnapShotTable($snaptable);
720 $restoretime = FindSnapInTable($table, $restoreto);
721 if ( ! defined $table->{0} || ! defined $restoretime ) {
722 PrintSnapShotTable($table);
723 print "No current or $restoreto slot in this table\n";
727 my $currentindex = $table->{0};
728 if ( $table->{$restoretime} == $currentindex ) {
729 print "You should not restore to the current snapshot\n";
733 # swap the entries for 0 and $restoreto
734 my $tmp = $table->{$restoretime};
735 $table->{$restoretime} = $table->{0};
737 # PrintSnapShotTable($table);
740 WriteSnapShotTable($snaptable, $table);
742 # set it in the kernel
743 SnapSetTable($tableno, $snaptable);
745 # ready for the ioctl
747 my $type = "obdsnap";
748 $data = pack("i", $currentindex); # slot of previous current snapshot
751 my $len = length($type);
752 my $cl = length($data);
753 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
754 if (! defined $::dev_obd) {
755 print "No current device.\n";
759 my $rc = ioctl($::dev_obd, &OBD_SNAP_RESTORE, $packed);
762 print STDERR "ioctl failed: $!\n";
764 } elsif ($rc eq "0 but true") {
765 print "Snaprestore finished (success)\n";
766 delete $table->{$restoretime} if defined $restoretime;
768 WriteSnapShotTable($snaptable, $table);
770 # set it in the kernel
771 SnapSetTable($tableno, $snaptable);
772 # PrintSnapShotTable($table);
775 print "ioctl returned error code $rc.\n";
780 sub FindSnapInTable {
784 foreach my $restoretime ( keys %{$table} ) {
785 if ( $table->{$restoretime} == $snapno) {
786 print "Found key $restoretime for snapno $snapno\n";
796 my $type = "obdsnap";
797 my $snaptableno = shift;
799 $data = pack("i", $snaptableno);
802 my $len = length($type);
803 my $cl = length($data);
804 print "type $type (len $len), datalen $datalen ($cl)\n";
805 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
806 if (! defined $::dev_obd) {
807 print "No current device.\n";
811 my $rc = ioctl($::dev_obd, &OBD_SNAP_PRINTTABLE, $packed);
814 print STDERR "ioctl failed: $!\n";
816 } elsif ($rc eq "0 but true") {
817 print "Finished (success)\n";
820 print "ioctl returned error code $rc.\n";
827 my $type = "obdsnap";
828 my $snaptableno = shift;
836 print "No such file $file\n";
840 $table = ReadSnapShotTable($file);
842 $snapcount = keys %{$table};
843 print "Snapcount $snapcount\n";
845 if ( ! defined $table->{0} ) {
846 print "No current snapshot in table! First make one\n";
849 $data = pack("ii", $snaptableno, $snapcount);
851 foreach my $time (sort keys %{$table}) {
852 # XXX we should change to pack LL instead of I for times
853 $data .= pack("Ii", $time, $table->{$time});
857 my $len = length($type);
858 my $cl = length($data);
859 print "type $type (len $len), datalen $datalen ($cl)\n";
860 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
861 if (! defined $::dev_obd) {
862 print "No current device.\n";
866 my $rc = ioctl($::dev_obd, &OBD_SNAP_SETTABLE, $packed);
869 print STDERR "ioctl failed: $!\n";
871 } elsif ($rc eq "0 but true") {
872 print "Finished (success)\n";
875 print "ioctl returned error code $rc.\n";
883 my $file = &readl("enter file name: ");
887 my $table = ReadSnapShotTable($file);
890 PrintSnapShotTable($table);
891 my $action = &readl("Add, Delete or Quit [adq]: ");
892 goto done if ($action =~ "^q.*" );
893 goto add if ($action =~ "^a.*");
894 goto del if ($action =~ "^d.*");
898 my $idx = &readl("enter index where you want this snapshot: ");
899 my $time = &readl("enter time or 'now' or 'current': ");
900 my $oldtime = SnapFindTimeFromIdx($idx, $table);
901 if (defined $oldtime) {
902 print "This already exists, first clean up\n";
906 if ( $time eq 'now' ) {
908 } elsif ( $time eq 'current' ) {
911 $table->{$time} = $idx;
915 $didx = &readl("Enter index to delete: ");
916 my $deltime = SnapFindTimeFromIdx($didx, $table);
917 delete $table->{$deltime} if defined $deltime;
921 my $ok = &readl("OK with new table? [Yn]: ");
922 unless ( $ok eq "n" ) {
923 WriteSnapShotTable($file, $table);
928 sub SnapFindTimeFromIdx {
932 foreach my $time ( keys %{$table} ) {
933 if ( $table->{$time} == $idx ) {
940 sub PrintSnapShotTable {
944 foreach $time ( sort keys %{$table} ) {
945 my $stime = localtime($time);
949 printf "Time: %s -- Index %d\n", $stime, $table->{$time};
953 sub ReadSnapShotTable {
960 my ($time, $index) = split ;
961 $table->{$time} = $index;
965 PrintSnapShotTable($table);
970 sub WriteSnapShotTable {
975 foreach my $time ( sort keys %{$table} ) {
976 print FH "$time $table->{$time}\n";
986 # Note: _copy IOCTL takes parameters as dst, src.
987 # Copy function takes parameters as src, dst.
988 $src_obdo->{id} = shift;
989 $dst_obdo->{id} = shift;
990 $src_obdo->{valid} = &OBD_MD_FLALL;
992 # XXX need to fix copy so we can have 2 client IDs here
993 my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo);
994 if (! defined $::dev_obd) {
995 print "No current device.\n";
999 my $rc = ioctl($::dev_obd, &OBD_IOC_COPY, $packed);
1002 print STDERR "ioctl failed: $!\n";
1004 } elsif ($rc eq "0 but true") {
1005 print "Finished (success)\n";
1008 print "ioctl returned error code $rc.\n";
1016 # Note: _migr IOCTL takes parameters as dst, src.
1017 # Migrate function takes parameters as src, dst.
1018 $src_obdo->{id} = shift;
1019 $dst_obdo->{id} = shift;
1020 $src_obdo->{valid} = &OBD_MD_FLALL;
1022 # We pack a dummy connection ID here
1023 my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo);
1024 if (! defined $::dev_obd) {
1025 print "No current device.\n";
1029 my $rc = ioctl($::dev_obd, &OBD_IOC_MIGR, $packed);
1032 print STDERR "ioctl failed: $!\n";
1034 } elsif ($rc eq "0 but true") {
1035 print "Finished (success)\n";
1038 print "ioctl returned error code $rc.\n";
1047 my $data = pack("i", $size);
1050 my $packed = pack("ip", $datalen, $data);
1051 if (! defined $::dev_obd) {
1052 print "No current device.\n";
1055 my $rc = ioctl($::dev_obd, &OBD_IOC_FORMATOBD, $packed);
1058 print STDERR "ioctl failed: $!\n";
1060 } elsif ($rc eq "0 but true") {
1061 print "Finished (success)\n";
1064 print "ioctl returned error code $rc.\n";
1073 my $data = pack("ii", $partno, $size);
1074 my $datalen = 2 * 4;
1076 my $packed = pack("ip", $datalen, $data);
1077 if (! defined $::dev_obd) {
1078 print "No current device.\n";
1081 my $rc = ioctl($::dev_obd, &OBD_IOC_PARTITION, $packed);
1084 print STDERR "ioctl failed: $!\n";
1086 } elsif ($rc eq "0 but true") {
1087 print "Finished (success)\n";
1090 print "ioctl returned error code $rc.\n";
1101 # XXX we need a getinfo ioctl to validate parameters
1104 if ($arg && !defined($::st = stat($arg))) {
1105 print "$arg is not a valid device\n";
1111 $datalen = length($arg)+1; # need null character also
1114 my $packed = pack("ip", $datalen, $data);
1115 if (! defined $::dev_obd) {
1116 print "No current device.\n";
1119 my $rc = ioctl($::dev_obd, &OBD_IOC_SETUP, $packed);
1122 print STDERR "ioctl failed: $!\n";
1124 } elsif ($rc eq "0 but true") {
1125 print "Finished (success)\n";
1128 print "ioctl returned error code $rc.\n";
1135 if (! defined $::dev_obd) {
1136 print "No current device.\n";
1139 my $rc = ioctl($::dev_obd, &OBD_IOC_CLEANUP, $err);
1142 print STDERR "ioctl failed: $!\n";
1144 } elsif ($rc eq "0 but true") {
1145 print "Finished (success)\n";
1149 print "ioctl returned error code $rc.\n";
1159 if (! defined $::dev_obd) {
1160 print "No current device.\n";
1163 $rc = ioctl($::dev_obd, &OBD_IOC_CONNECT, $packed);
1164 $id = unpack("I", $packed);
1167 print STDERR "ioctl failed: $!\n";
1169 } elsif ($rc eq "0 but true") {
1171 print "Client ID : $id\n";
1172 print "Finished (success)\n";
1175 print "ioctl returned error code $rc.\n";
1183 if (!defined($id)) {
1187 if (!defined($id)) {
1188 print "syntax: disconnect [client ID]\n";
1189 print "When client ID is not given, the last valid client ID to be returned by a\n";
1190 print "connect command this session is used; there is no such ID.\n";
1194 my $packed = pack("L", $id);
1195 if (! defined $::dev_obd) {
1196 print "No current device.\n";
1199 my $rc = ioctl($::dev_obd, &OBD_IOC_DISCONNECT, $packed);
1202 print STDERR "ioctl failed: $!\n";
1204 } elsif ($rc eq "0 but true") {
1205 $::client_id = undef;
1206 print "Finished (success)\n";
1209 print "ioctl returned error code $rc.\n";
1215 if (!defined($::client_id)) {
1216 print "You must first ``connect''.\n";
1226 if (!defined($num)) {
1230 if (!defined($mode)) {
1231 $mode = 0100644; # create a file (rw-r--r--) if not specified
1234 if (scalar($num) < 1 || defined($quiet) && $quiet ne "quiet") {
1235 print "usage: create [<number of objects> [<mode> [quiet]]]\n";
1240 my $id = 0; # can't currently request IDs
1242 print "Creating " . scalar($num) . " object";
1243 if (scalar($num) > 1) {
1248 for ($i = 0; $i < scalar($num); $i++) {
1251 $obdo->{mode} = scalar($mode);
1252 $obdo->{valid} = &OBD_MD_FLMODE;
1254 my $packed = pack("I", $::client_id) . obdo_pack($obdo);
1255 if (! defined $::dev_obd) {
1256 print "No current device.\n";
1259 $rc = ioctl($::dev_obd, &OBD_IOC_CREATE, $packed);
1260 if ($rc ne "0 but true") {
1262 } elsif (!defined($quiet)) {
1263 $obdo = obdo_unpack($packed, 4);
1264 print "Created object #$obdo->{id}.\n";
1269 print STDERR "ioctl failed: $!\n";
1271 } elsif ($rc eq "0 but true") {
1272 print "Finished (success)\n";
1275 print "ioctl returned error code $rc.\n";
1282 if (! defined $::dev_obd) {
1283 print "No current device.\n";
1286 my $rc = ioctl($::dev_obd, &OBD_IOC_SYNC, $err);
1289 print STDERR "ioctl failed: $!\n";
1291 } elsif ($rc eq "0 but true") {
1292 print "Finished (success)\n";
1295 print "ioctl returned error code $rc.\n";
1301 if (!defined($::client_id)) {
1302 print "You must first ``connect''.\n";
1308 if (!defined($id) || scalar($id) < 1) {
1309 print "usage: destroy <object number>\n";
1313 print "Destroying object $id...\n";
1314 my $packed = pack("IL", $::client_id, $id);
1315 if (! defined $::dev_obd) {
1316 print "No current device.\n";
1319 my $rc = ioctl($::dev_obd, &OBD_IOC_DESTROY, $packed);
1322 print STDERR "ioctl failed: $!\n";
1324 } elsif ($rc eq "0 but true") {
1325 print "Finished (success)\n";
1328 print "ioctl returned error code $rc.\n";
1334 if (!defined($::client_id)) {
1335 print "You must first ``connect''.\n";
1341 if (!defined($id) || scalar($id) < 1) {
1342 print "invalid arguments; type \"help getattr\" for a synopsis\n";
1349 $obdo->{valid} = &OBD_MD_FLALL;
1350 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1351 if (! defined $::dev_obd) {
1352 print "No current device.\n";
1355 my $rc = ioctl($::dev_obd, &OBD_IOC_GETATTR, $packed);
1358 print STDERR "ioctl failed: $!\n";
1360 } elsif ($rc eq "0 but true") {
1361 $obdo = obdo_unpack($packed, 4);
1365 print "ioctl returned error code $rc.\n";
1371 if (!defined($::client_id)) {
1372 print "You must first ``connect''.\n";
1378 if (!defined($id) || scalar($id) < 1) {
1379 print "invalid arguments; type \"help setattr\" for a synopsis\n";
1383 # XXX we do not currently set all of the fields in the obdo
1386 $obdo->{mode} = oct(shift);
1387 $obdo->{uid} = shift;
1388 $obdo->{gid} = shift;
1389 $obdo->{size} = shift;
1390 $obdo->{atime} = shift;
1391 $obdo->{mtime} = shift;
1392 $obdo->{ctime} = shift;
1395 if (defined($obdo->{atime})) {
1396 $obdo->{valid} |= &OBD_MD_FLATIME;
1398 if (defined($obdo->{mtime})) {
1399 $obdo->{valid} |= &OBD_MD_FLMTIME;
1401 if (defined($obdo->{ctime})) {
1402 $obdo->{valid} |= &OBD_MD_FLCTIME;
1404 if (defined($obdo->{size})) {
1405 $obdo->{valid} |= &OBD_MD_FLSIZE;
1407 if (defined($obdo->{mode})) {
1408 $obdo->{valid} |= &OBD_MD_FLMODE;
1410 if (defined($obdo->{uid})) {
1411 $obdo->{valid} |= &OBD_MD_FLUID;
1413 if (defined($obdo->{gid})) {
1414 $obdo->{valid} |= &OBD_MD_FLGID;
1417 printf "valid is %x, mode is %o\n", $obdo->{valid}, $obdo->{mode};
1418 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1419 if (! defined $::dev_obd) {
1420 print "No current device.\n";
1423 my $rc = ioctl($::dev_obd, &OBD_IOC_SETATTR, $packed);
1426 print STDERR "ioctl failed: $!\n";
1428 } elsif ($rc eq "0 but true") {
1429 print "Finished (success)\n";
1432 print "ioctl returned error code $rc.\n";
1438 if (!defined($::client_id)) {
1439 print "You must first ``connect''.\n";
1447 if (!defined($id) || scalar($id) < 1 || !defined($count) ||
1448 $count < 1 || (defined($offset) && $offset < 0)) {
1449 print "invalid arguments; type \"help read\" for a synopsis\n";
1453 if (!defined($offset)) {
1457 print("Reading $count bytes starting at byte $offset from object " .
1460 # "allocate" a large enough buffer
1461 my $buf = sprintf("%${count}s", " ");
1462 die "suck" if (length($buf) != $count);
1467 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1468 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1469 pack("p LL LL", $buf, $count, $offset);
1471 if (! defined $::dev_obd) {
1472 print "No current device.\n";
1475 my $rc = ioctl($::dev_obd, &OBD_IOC_READ, $packed);
1477 $retval = unpack("l", $packed);
1480 print STDERR "ioctl failed: $!\n";
1482 } elsif ($rc eq "0 but true") {
1484 print substr($buf, 0, $retval);
1485 print "\nRead $retval of an attempted $count bytes.\n";
1486 print "Finished (success)\n";
1489 print "Finished (error $retval)\n";
1493 print "ioctl returned error code $rc.\n";
1499 if (!defined($::client_id)) {
1500 print "You must first ``connect''.\n";
1508 if (!defined($id) || scalar($id) < 1 || !defined($count) ||
1509 $count < 1 || (defined($offset) && $offset < 0)) {
1510 print "invalid arguments; type \"help read\" for a synopsis\n";
1514 if (!defined($offset)) {
1518 print("Reading $count bytes starting at byte $offset from object " .
1521 # "allocate" a large enough buffer
1522 my $buf = sprintf("%${count}s", " ");
1523 die "suck" if (length($buf) != $count);
1528 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1529 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1530 pack("p LL LL", $buf, $count, $offset);
1532 if (! defined $::dev_obd) {
1533 print "No current device.\n";
1536 my $rc = ioctl($::dev_obd, &OBD_IOC_READ2, $packed);
1538 $retval = unpack("l", $packed);
1541 print STDERR "ioctl failed: $!\n";
1543 } elsif ($rc eq "0 but true") {
1545 print substr($buf, 0, $retval);
1546 print "\nRead $retval of an attempted $count bytes.\n";
1547 print "Finished (success)\n";
1550 print "Finished (error $retval)\n";
1554 print "ioctl returned error code $rc.\n";
1560 if (!defined($::client_id)) {
1561 print "You must first ``connect''.\n";
1567 my $text = join(' ', @_);
1568 my $count = length($text);
1570 if (!defined($id) || scalar($id) < 1 || !defined($offset) ||
1571 scalar($offset) < 0) {
1572 print "invalid arguments; type \"help write\" for a synopsis\n";
1576 if (!defined($text)) {
1581 print("Writing $count bytes starting at byte $offset to object $id...\n");
1586 # the perl we're using doesn't support pack type Q
1587 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1588 pack("p LL LL", $text, $count, $offset);
1590 if (! defined $::dev_obd) {
1591 print "No current device.\n";
1594 my $rc = ioctl($::dev_obd, &OBD_IOC_WRITE, $packed);
1596 $retval = unpack("l", $packed);
1599 print STDERR "ioctl failed: $!\n";
1601 } elsif ($rc eq "0 but true") {
1603 print "\nWrote $retval of an attempted $count bytes.\n";
1604 print "Finished (success)\n";
1607 print "Finished (error $retval)\n";
1611 print "ioctl returned error code $rc.\n";
1617 if (!defined($::client_id)) {
1618 print "You must first ``connect''.\n";
1626 if (!defined($id) || scalar($id) < 1 || !defined($start) ||
1627 scalar($start) < 0 || !defined($count) || scalar($count) < 0) {
1628 print "invalid arguments; type \"help punch\" for a synopsis\n";
1632 print("Punching $count bytes starting at byte $start from object $id...\n");
1637 # the perl we're using doesn't support pack type Q
1638 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1639 pack("p LL LL", $buf, $start, $count);
1641 if (! defined $::dev_obd) {
1642 print "No current device.\n";
1645 my $rc = ioctl($::dev_obd, &OBD_IOC_PUNCH, $packed);
1647 $retval = unpack("l", $packed);
1650 print STDERR "ioctl failed: $!\n";
1651 } elsif ($rc eq "0 but true") {
1653 print "\nPunched $retval of an attempted $count bytes.\n";
1654 print "Finished (success)\n";
1657 print "Finished (error $retval)\n";
1661 print "ioctl returned error code $rc.\n";
1669 if (!defined($::client_id)) {
1670 print "You must first ``connect''.\n";
1674 if (!defined($num) || scalar($num) < 1 || scalar($num) > 32) {
1678 print "Preallocating $num objects...\n";
1679 # client id, alloc, id[32]
1680 my $packed = pack("LLx128", $::client_id, $num);
1682 if (! defined $::dev_obd) {
1683 print "No current device.\n";
1686 my $rc = ioctl($::dev_obd, &OBD_IOC_PREALLOCATE, $packed);
1689 print STDERR "ioctl failed: $!\n";
1691 } elsif ($rc eq "0 but true") {
1692 my $alloc = unpack("x4L", $packed);
1693 my @ids = unpack("x8L32", $packed);
1696 print "Got $alloc objects: ";
1700 print "\nFinished (success)\n";
1703 print "ioctl returned error code $rc.\n";
1709 if (! defined $::dev_obd) {
1710 print "No current device.\n";
1713 my $rc = ioctl($::dev_obd, &OBD_IOC_DEC_USE_COUNT, 0);
1716 print STDERR "ioctl failed: $!\n";
1718 } elsif ($rc eq "0 but true") {
1719 print "Finished (success)\n";
1722 print "ioctl returned error code $rc.\n";
1728 if (!defined($::client_id)) {
1729 print "You must first ``connect''.\n";
1741 # __kernel_fsid_t f_fsid; (64 bits)
1746 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1749 if (! defined $::dev_obd) {
1750 print "No current device.\n";
1753 my $rc = ioctl($::dev_obd, &OBD_IOC_STATFS, $packed);
1756 print STDERR "ioctl failed: $!\n";
1758 } elsif ($rc eq "0 but true") {
1759 # skip both the conn_id and the fs_type in the buffer
1760 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
1761 unpack("x4x4LLLLLL", $packed);
1762 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
1763 "$bfree free ($bavail available).\n");
1764 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
1765 print "Finished (success)\n";
1768 print "ioctl returned error code $rc.\n";
1776 if ( !$cmd || !$commands{$cmd} ) {
1777 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
1779 print "Usage: " . $commands{$cmd}->{doc} . "\n";
1786 print "Disconnecting active session ($::client_id)...";
1787 Disconnect($::client_id);