Whamcloud - gitweb
Fixed packing in obdcontrol
[fs/lustre-release.git] / lustre / obdclass / obdcontrol
index 379256e..6a755aa 100755 (executable)
@@ -89,6 +89,109 @@ use Term::ReadLine;
 use IO::Handle;
 
 
+# NOTE long long are layed out in ia32 memory as follows:
+# u = 0xaaaabbbbccccdddd has ccccdddd at &u and aaaabbbb 4 bytes on
+# this may be different on other architectures
+
+# we use 32-bit integers for all 64-bit quantities in this program
+# #define OBD_INLINESZ 64
+# #define OBD_OBDMDSZ  64
+# /* Note: 64-bit types are 64-bit aligned in structure */
+# struct obdo {
+#      obd_id                  o_id;
+#      obd_gr                  o_gr;
+#      obd_time                o_atime;
+#      obd_time                o_mtime;
+#      obd_time                o_ctime;
+#      obd_size                o_size;
+#      obd_blocks              o_blocks;
+#      obd_blksize             o_blksize;
+#      obd_mode                o_mode;
+#      obd_uid                 o_uid;
+#      obd_gid                 o_gid;
+#      obd_flag                o_flags;
+#      obd_flag                o_obdflags;
+#      obd_count               o_nlink;
+#      obd_flag                o_valid;        /* hot fields in this obdo */
+#      char                    o_inline[OBD_INLINESZ];
+#      char                    o_obdmd[OBD_OBDMDSZ];
+#      struct list_head        o_list;
+#      struct obd_ops          *o_op;
+# };
+
+sub obdo_pack {
+    my $obdo = shift;
+    pack "LL LL LL LL LL LL LL L L L L L L L L a60 a64 L L L", 
+    $obdo->{id}, 0, 
+    $obdo->{gr}, 0, 
+    $obdo->{atime}, 0, 
+    $obdo->{mtime}, 0 ,
+    $obdo->{ctime}, 0, 
+    $obdo->{size}, 0, 
+    $obdo->{blocks}, 0, 
+    $obdo->{blksize},
+    $obdo->{mode},
+    $obdo->{uid},
+    $obdo->{gid},
+    $obdo->{flags},
+    $obdo->{obdflags},
+    $obdo->{nlink},    
+    $obdo->{valid},    
+    $obdo->{inline},
+    $obdo->{obdmd},
+    0, 0, # struct list_head 
+    0;  #  struct obd_ops 
+}
+
+sub obdo_unpack {
+    my $buf = shift;
+    my $offset = shift;
+    my $obdo;
+    ($obdo->{id},
+    $obdo->{gr},
+    $obdo->{atime},
+    $obdo->{mtime},
+    $obdo->{ctime},
+    $obdo->{size},
+    $obdo->{blocks},
+    $obdo->{blksize},
+    $obdo->{mode},
+    $obdo->{uid},
+    $obdo->{gid},
+    $obdo->{flags},
+    $obdo->{obdflags},
+    $obdo->{nlink},    
+    $obdo->{valid},    
+    $obdo->{inline},
+    $obdo->{obdmd}) = unpack "x${offset}Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 L L L L L L L L a60 a64", $buf;
+    $obdo;
+}
+
+sub obdo_print {
+
+    my $obdo = shift;
+
+    printf "id: %d\ngrp: %d\natime: %s\nmtime: %s\nctime: %s\nsize: %d\nblocks: %d\nblksize: %d\nmode: %x\nuid: %d\ngid: %d\nflags: %x\nobdflags: %x\nnlink: %d\nvalid: %x\ninline: %s\nobdmd: %s\n", 
+    $obdo->{id},
+    $obdo->{gr},
+    $obdo->{atime},
+    $obdo->{mtime},
+    $obdo->{ctime},
+    $obdo->{size},
+    $obdo->{blocks},
+    $obdo->{blksize},
+    $obdo->{mode},
+    $obdo->{uid},
+    $obdo->{gid},
+    $obdo->{flags},
+    $obdo->{obdflags},
+    $obdo->{nlink},    
+    $obdo->{valid},    
+    $obdo->{inline},
+    $obdo->{obdmd};
+}
+
+
 my ($file);
 
 GetOptions("f!" => \$file, "device=s" => \$::device, ) || die "Getoptions";
@@ -102,22 +205,20 @@ my $arg;
 
 my %commands =
     ('device' => {func => "Device", doc => "device <dev>: open another OBD device"},
-     'filesystem' => {func => "Filesystem", doc => "filesystem <dev>: partition for direct OBD device"},
      'create' => {func => "Create", doc => "create: creates a new inode"},
-     'attach' => {func => "Attach", doc => "attach type [adapter bus tid lun]"},
+     'attach' => {func => "Attach", doc => "attach { ext2_obd | snap_obd snapdev snapidx tableno | scsi_obd adapter bus tid lun }" },
      'detach' => {func => "Detach", doc => "detach this device"},
-     'testext2iterator' => {func => "TestExt2Iterator", doc => ""},
-     'snapattach' => {func => "SnapAttach", doc => "snapattach snapdev snapidx tableno"},
-     'snapset' => {func => "SnapSetTable", doc => "snapset tableno file" },
-     'snapprint' => {func => "SnapPrint", doc => "snapprint tableno"},
-     'snapdelete' => {func => "SnapDelete", doc => "snapdelete: delete connected snap obd"},
-     'snaprestore' => {func => "SnapRestore", doc => "snaprestore: restore connected old snap to current"},
+     'testext2iterator' => {func => "TestExt2Iterator", doc => "test ext2 iterator function"},
+     'snapset' => {func => "SnapSetTable", doc => "snapset <tableno> <file>: set the table (created with snaptable) as table #tableno" },
+     'snapprint' => {func => "SnapPrint", doc => "snapprint <tableno>: output the contents of table #tableno to the syslog"},
+     'snapdelete' => {func => "SnapDelete", doc => "snapdelete: delete connected snap obd objects from disk"},
+     'snaprestore' => {func => "SnapRestore", doc => "snaprestore : restore connected old snap objects to be current"},
      'snaptable' => {func => "SnapShotTable", doc => "snaptable: build a snapshot table (interactive)"},
-     'copy' => {func => "Copy", doc => "copy srcid tgtid"},
-     'migrate' => {func => "Migrate", doc => "migrate srcid tgtid"},
+     'copy' => {func => "Copy", doc => "copy <srcid> <tgtid>: copy objects"},
+     'migrate' => {func => "Migrate", doc => "migrate <srcid> <tgtid>: migrate data from one object to another"},
      'format' => {func => "Format", doc => "format type adapter bus tid lun size"},
      'partition' => {func => "Partition", doc => "partition type adapter bus tid lun partition size"},
-     'setup' => {func => "Setup", doc => "setup [type]: link this OBD device to the underlying device (default type ext2_obd, filesystem /dev/loop0)"},
+     'setup' => {func => "Setup", doc => "setup [type]: link this OBD device to the underlying device (default type ext2_obd)"},
      'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
      'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
      'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
@@ -153,19 +254,19 @@ sub readl {
        my $str = <STDIN>;
        chop($str);
        return $str;
-  } else {
-      return $term->readline(@_);
-  }
+    } else {
+        return $term->readline(@_);
+    }
 }
 
 
 
 if ( $file ) {
-       while ( <STDIN> ) {
-           print $_;
-           execute_line($_);
-       }
-       exit 0;
+    while ( <STDIN> ) {
+        print $_;
+        execute_line($_);
+    }
+    exit 0;
 } else {
     $term = new Term::ReadLine 'obdcontrol ';
     $attribs = $term->Attribs;
@@ -261,17 +362,38 @@ sub Attach {
     my $datalen = 0;
 
     if ( ! $type ) {
-       print "Usage \"attach type (ext2_obd)\"; 
+       print "error: missing type\n";
+usage:
+       print "usage: attach {ext2_obd | snap_obd | scsi_obd}\n";
+       return;
+    }
 
-    if ($type eq "obdscsi" ) {
+    if ($type eq "scsi_obd" ) {
        my $adapter = shift;
        my $bus = shift;
        my $tid = shift;
        my $lun = shift;
-       $data = pack("iiiii", $adapter, $bus, $tid, $lun, $size);
+
+       $data = pack("iiii", $adapter, $bus, $tid, $lun);
        $datalen = 4 * 4;
+    } elsif ($type eq "snap_obd" ) {
+       my $snapdev = shift;
+       my $snapidx = shift;
+       my $tableno = shift;
+
+       $data = pack("iii", $snapdev, $snapidx, $tableno);
+       $datalen = 3 * 4;
+    } elsif ($type eq "ext2_obd") {
+       1;
+    } else {
+       print "error: unknown attach type $type\n";
+       goto usage;
     }
 
+    my $len = length($type);
+    my $cl = length($data);
+
+    print "type $type (len $len), datalen $datalen ($cl)\n";
     my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
 
     my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
@@ -362,27 +484,63 @@ sub SnapDelete {
 }
 
 
-# XXX the parameter to snaprestore SHOULD be the snapshot number of the
-#     snapshot to be restored, and this routine should handle all of the
-#     removal of intervening snapshots and changing the snaptable to have
-#     the "restored" snapshot as current and move the previous current.
+#      this routine does the whole job
 sub SnapRestore { 
+    my $restoreto = shift;
+    my $snaptable = shift;
+    my $tableno = shift;
+    my $restoretime;
+
+    # don't do anything until connected
     if (!defined($::client_id)) {
        print "You must first ``connect''.\n";
        return;
     }
 
+    if ( ! $snaptable || ! defined $restoreto ) {
+       print "Usage: snaprestore \"restore to slot\" \"snaptable\" \"tableno\"\n";
+       return;
+    }
+
+    if ( ! -f $snaptable ) {
+       print "Table $snaptable doesn't exist\n";
+       return;
+    }
+   
+    my $table = ReadSnapShotTable($snaptable);
+    $restoretime = FindSnapInTable($table, $restoreto);
+    if ( ! defined $table->{0} || ! defined $restoretime ) {
+       PrintSnapShotTable($table);
+       print "No current or $restoreto slot in this table\n";
+       return;
+    }
+
+    my $currentindex = $table->{0};
+    if (  $table->{$restoretime} == $currentindex ) {
+       print "You should not restore to the current snapshot\n";
+       return;
+    }
+    
+    # swap the entries for 0 and $restoreto
+    my $tmp = $table->{$restoretime};
+    $table->{$restoretime} = $table->{0};
+    $table->{0} = $tmp;
+    # PrintSnapShotTable($table);
+
+    # write it back
+    WriteSnapShotTable($snaptable, $table);
+
+    # set it in the kernel
+    SnapSetTable($tableno, $snaptable);
+
+    # ready for the ioctl
     my $err = 0;
     my $type = "snap_obd";
-    my $prevcurrent = shift;
-    $data = pack("i", $prevcurrent); # where the previous current snapshot now lives
+    $data = pack("i", $currentindex); # slot of previous current snapshot 
     $datalen = 4;
 
     my $len = length($type);
     my $cl = length($data);
-    my $add = pack("p", $data);
-    print "type $type (len $len), datalen $datalen ($cl)\n";
     my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
 
     my $rc = ioctl(DEV_OBD, &OBD_SNAP_RESTORE, $packed);
@@ -390,13 +548,33 @@ sub SnapRestore {
     if (!defined $rc) {
        print STDERR "ioctl failed: $!\n";
     } elsif ($rc eq "0 but true") {
-       print "Finished (success)\n";
+       print "Snaprestore finished (success)\n";
+       delete $table->{$restoretime} if defined $restoretime;
+       # write it back
+       WriteSnapShotTable($snaptable, $table);
+       
+       # set it in the kernel
+       SnapSetTable($tableno, $snaptable);
+       # PrintSnapShotTable($table);
+
     } else {
        print "ioctl returned error code $rc.\n";
     }
 }
 
+sub FindSnapInTable { 
+    my $table = shift;
+    my $snapno =shift;
 
+    foreach my $restoretime ( keys %{$table} ) {
+       if ( $table->{$restoretime} == $snapno) { 
+           print "Found key $restoretime for snapno $snapno\n";
+           return $restoretime;
+       }
+    }
+    undef;
+}
+           
 
 sub SnapPrint { 
     my $err = 0;
@@ -471,37 +649,6 @@ sub SnapSetTable {
 }
 
 
-sub SnapAttach {
-    my $err = 0;
-    my $type = "snap_obd";
-    my $snapdev = shift;
-    my $snapidx = shift;
-    my $tableno = shift;
-    my $snapcount;
-    my $data;
-    my $datalen = 0;
-
-    $data = pack("iii", $snapdev, $snapidx, $tableno);
-    $datalen = 3 * 4;
-
-    my $len = length($type);
-    my $cl = length($data);
-    my $add = pack("p", $data);
-    print "type $type (len $len), datalen $datalen ($cl)\n";
-    my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
-
-    my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
-
-    if (!defined $rc) {
-       print STDERR "ioctl failed: $!\n";
-    } elsif ($rc eq "0 but true") {
-       print "Finished (success)\n";
-    } else {
-       print "ioctl returned error code $rc.\n";
-    }
-}
-
-
 sub SnapShotTable  {
 
     my $file = &readl("enter file name: ");
@@ -543,9 +690,9 @@ sub SnapShotTable  {
 
   done:
     my $ok = &readl("OK with new table? [Yn]: ");
-    unless ( $ok eq "no" )  {
-      WriteSnapShotTable($file, $table);
-  }
+    unless ( $ok eq "n" )  {
+       WriteSnapShotTable($file, $table);
+    }
 }
 
 sub SnapFindTimeFromIdx {
@@ -604,8 +751,11 @@ sub WriteSnapShotTable {
 sub Copy {
     my $err = 0;
     my $srcid = shift;
-    my $tgtid = shift;
-    my $data = pack("III", $::client_id, $srcid, $tgtid);
+    my $dstid = shift;
+
+    # Note: _copy IOCTL takes parameters as dst, src.
+    #       Copy function takes parameters as src, dst.
+    my $data = pack("III", $::client_id, $dstid, $srcid);
     my $datalen = 12;
 
     my $packed = pack("ip", $datalen, $data);
@@ -623,8 +773,11 @@ sub Copy {
 sub Migrate {
     my $err = 0;
     my $srcid = shift;
-    my $tgtid = shift;
-    my $data = pack("III", $::client_id, $srcid, $tgtid);
+    my $dstid = shift;
+
+    # Note: _migr IOCTL takes parameters as dst, src.
+    #       Migrate function takes parameters as src, dst.
+    my $data = pack("III", $::client_id, $dstid, $srcid);
     my $datalen = 12;
 
     my $packed = pack("ip", $datalen, $data);
@@ -679,16 +832,20 @@ sub Partition {
 
 sub Setup {
     my $err = 0;
-    my $type = shift;
+    my $arg = shift;
     my $data;
     my $datalen = 0;
-    
-    $type = "ext2_obd" unless $type;
 
-    if ( $type eq "ext2_obd" ) {
-       my $dev = shift;
-       my $st = stat($dev);
-       $dev = $st->rdev() unless $dev;
+    # XXX we need a getinfo ioctl to validate parameters 
+    # by type here
+
+    if ($arg  && !defined($::st = stat($arg))) {
+           print "$arg is not a valid device\n";
+           return;
+    }
+    
+    if ( $arg ) {
+       $dev = $::st->rdev() unless $dev;
        $data = pack("i", $dev);
        $datalen = 4;
     }
@@ -771,7 +928,7 @@ sub Create {
     my $rc;
     my $prealloc = 0;
 
-    if (defined($quiet) && !($quiet eq "quiet")) {
+    if (defined($quiet) && $quiet ne "quiet") {
        print "syntax: create [number of objects [quiet]]\n";
        return;
     }
@@ -791,7 +948,7 @@ sub Create {
        for ($i = 0; $i < scalar($arg); $i++) {
            $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
            my $ino = unpack("L", $packed);
-           if (!($rc eq "0 but true")) {
+           if ($rc ne "0 but true") {
                last;
                $packed = pack("IL", $::client_id, $prealloc);
            } elsif (!defined($quiet)) {
@@ -863,24 +1020,17 @@ sub Getattr {
     }
 
     # see Setattr
-    my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
-                     0);
+    my $obdo;
+    $obdo->{id} = $inode;
+    my $packed = pack("L", $::client_id) . obdo_pack($obdo);
     my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
 
+    
     if (!defined $rc) {
        print STDERR "ioctl failed: $!\n";
     } elsif ($rc eq "0 but true") {
-       my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
-       ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
-         unpack("ISssx2lLLLI", $packed);
-
-       printf("Inode: %d  Mode:  %o\n", $inode, $mode);
-       printf("User: %6d   Group: %6d   Size: %d\n", $uid, $gid, $size);
-       printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
-       printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
-       printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
-       printf("flags: %08x\n", $flags);
-       print "Finished (success)\n";
+       $obdo = obdo_unpack($packed,  4); 
+       obdo_print($obdo);
     } else {
        print "ioctl returned error code $rc.\n";
     }