Whamcloud - gitweb
ext2obd/ext2_obd.c, snap/snap.c: change parameters for create
[fs/lustre-release.git] / lustre / obdclass / obdcontrol
index 51f63df..15eb769 100755 (executable)
@@ -65,9 +65,11 @@ eval 'sub OBD_SNAP_SETTABLE () { &_IOC(3, ord(\'f\'), 40, 4);}' unless
   defined(&OBD_SNAP_SETTABLE);
 eval 'sub OBD_SNAP_PRINTTABLE () { &_IOC(3, ord(\'f\'), 41, 4);}' unless
   defined(&OBD_SNAP_PRINTTABLE);
+eval 'sub OBD_SNAP_DELETE() { &_IOC(3, ord(\'f\'), 42, 4);}' unless
+  defined(&OBD_SNAP_DELETE);
+eval 'sub OBD_SNAP_RESTORE() { &_IOC(3, ord(\'f\'), 43, 4);}' unless
+  defined(&OBD_SNAP_RESTORE);
 
-eval 'sub OBD_SNAP_RUNIT () { &_IOC(3, ord(\'f\'), 42, 4);}' unless
-  defined(&OBD_SNAP_RUNIT);
 eval 'sub OBD_EXT2_RUNIT () { &_IOC(3, ord(\'f\'), 61, 4);}' unless
   defined(&OBD_EXT2_RUNIT);
 
@@ -89,7 +91,7 @@ use IO::Handle;
 
 my ($file);
 
-GetOptions("f!" => \$file, "device=s" => \$::device, "fs=s" => $::filesystem) || die "Getoptions";
+GetOptions("f!" => \$file, "device=s" => \$::device, ) || die "Getoptions";
 
 
 # get a console for the app
@@ -100,27 +102,24 @@ 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"},
-     'snapattach' => {func => "SnapAttach", doc => "snapattach snapno table"},
-     'snapset' => {func => "SnapSetTable", doc => "snapset tableno file" },
-     'snapprint' => {func => "SnapPrint", doc => "snapprint tableno"},
-
-     'testext2iterator' => {func => "TestExt2Iterator", doc => ""},
-     'testsnapiterator' => {func => "TestSnapIterator", doc => ""},
-
+     '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: link the ext2 partition (default /dev/loop0) to this obddev"},
+     '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"},
-     'destroy' => {func => "Destroy", doc => "setup: destroys an inode"},
+     'destroy' => {func => "Destroy", doc => "destroy <inode>: destroys an inode"},
      'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
      'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
      'read' => {func => "Read", doc => "read <inode> <count> [offset]"},
@@ -146,26 +145,25 @@ my $term, $attribs;
 # Get going....
 
 Device($::device);
-Filesystem($::filesystem);
 
 sub readl {
     if ( $file ) {
        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;
@@ -234,21 +232,15 @@ sub execute_line {
     return (&{$commands{$cmd}->{func}}(@arg));
 }
 
-# set the object store in the ext2 formatted block device
-sub Filesystem {
-    my $filesystem = shift;
-    $filesystem = "/dev/loop0" unless $filesystem;
-    
-    $::filesystem = $filesystem;
-    if (!defined($::st = stat($filesystem))) {
-       die "Unable to stat $filesystem.\n";
-    }
-}
 
 # select the OBD device we talk to
 sub Device {
     my $device = shift;
 
+    if ($::client_id) {
+       print "Disconnecting active session ($::client_id)...";
+       Disconnect($::client_id);
+    }
     if (! $device ) {
        $device = "/dev/obd0";
     }
@@ -266,15 +258,39 @@ sub Attach {
     my $data;
     my $datalen = 0;
 
-    if ($type eq "obdscsi" ) {
+    if ( ! $type ) {
+       print "error: missing type\n";
+usage:
+       print "usage: attach {ext2_obd | snap_obd | scsi_obd}\n";
+       return;
+    }
+
+    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);
@@ -304,6 +320,11 @@ sub Detach {
 
 
 sub TestExt2Iterator { 
+    if (!defined($::client_id)) {
+       print "You must first ``connect''.\n";
+       return;
+    }
+
     my $err = 0;
     my $type = "ext2_obd";
  
@@ -328,7 +349,12 @@ sub TestExt2Iterator {
 }
 
 
-sub TestSnapIterator { 
+sub SnapDelete { 
+    if (!defined($::client_id)) {
+       print "You must first ``connect''.\n";
+       return;
+    }
+
     my $err = 0;
     my $type = "snap_obd";
  
@@ -341,7 +367,9 @@ sub TestSnapIterator {
     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_RUNIT, $packed);
+    # XXX We need to fix this up so that after the objects in this snapshot
+    #     are deleted, the snapshot itself is also removed from the table.
+    my $rc = ioctl(DEV_OBD, &OBD_SNAP_DELETE, $packed);
 
     if (!defined $rc) {
        print STDERR "ioctl failed: $!\n";
@@ -353,6 +381,98 @@ sub TestSnapIterator {
 }
 
 
+#      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";
+    $data = pack("i", $currentindex); # slot of previous current snapshot 
+    $datalen = 4;
+
+    my $len = length($type);
+    my $cl = length($data);
+    my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
+
+    my $rc = ioctl(DEV_OBD, &OBD_SNAP_RESTORE, $packed);
+
+    if (!defined $rc) {
+       print STDERR "ioctl failed: $!\n";
+    } elsif ($rc eq "0 but true") {
+       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;
     my $type = "snap_obd";
@@ -426,37 +546,6 @@ sub SnapSetTable {
 }
 
 
-sub SnapAttach {
-    my $err = 0;
-    my $type = "snap_obd";
-    my $snapdev = shift;
-    my $snapno = shift;
-    my $tableno = shift;
-    my $snapcount;
-    my $data;
-    my $datalen = 0;
-
-    $data = pack("iii", $snapdev, $snapno, $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: ");
@@ -498,9 +587,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 {
@@ -559,8 +648,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);
@@ -578,8 +670,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);
@@ -634,14 +729,19 @@ 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;
+    # 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;
@@ -725,7 +825,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;
     }
@@ -745,7 +845,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)) {