Whamcloud - gitweb
Over of the changes:
[fs/lustre-release.git] / lustre / obdclass / obdcontrol
index ceb889e..07458c0 100755 (executable)
@@ -21,10 +21,10 @@ BEGIN { require "asm/ioctl.ph" };
 # p2ph generated invalid macros for ioctl stuff, so I override some of it here
 eval 'sub OBD_IOC_CREATE () { &_IOC(2, ord(\'f\'), 3, 4);}' unless
   defined(&OBD_IOC_CREATE);
 # p2ph generated invalid macros for ioctl stuff, so I override some of it here
 eval 'sub OBD_IOC_CREATE () { &_IOC(2, ord(\'f\'), 3, 4);}' unless
   defined(&OBD_IOC_CREATE);
-eval 'sub OBD_IOC_SETUP_SUPER () { &_IOC(1, ord(\'f\'), 4, 4);}' unless
-  defined(&OBD_IOC_SETUP_SUPER);
-eval 'sub OBD_IOC_CLEANUP_SUPER () { &_IOC(0, ord(\'f\'), 5, 0);}' unless
-  defined(&OBD_IOC_CLEANUP_SUPER);
+eval 'sub OBD_IOC_SETUP_OBDDEV () { &_IOC(1, ord(\'f\'), 4, 4);}' unless
+  defined(&OBD_IOC_SETUP_OBDDEV);
+eval 'sub OBD_IOC_CLEANUP () { &_IOC(0, ord(\'f\'), 5, 0);}' unless
+  defined(&OBD_IOC_CLEANUP);
 eval 'sub OBD_IOC_DESTROY () { &_IOC(1, ord(\'f\'), 6, 4);}' unless
   defined(&OBD_IOC_DESTROY);
 eval 'sub OBD_IOC_PREALLOCATE () { &_IOC(3, ord(\'f\'), 7, 4);}' unless
 eval 'sub OBD_IOC_DESTROY () { &_IOC(1, ord(\'f\'), 6, 4);}' unless
   defined(&OBD_IOC_DESTROY);
 eval 'sub OBD_IOC_PREALLOCATE () { &_IOC(3, ord(\'f\'), 7, 4);}' unless
@@ -47,6 +47,14 @@ eval 'sub OBD_IOC_STATFS () { &_IOC(3, ord(\'f\'), 15, 4);}' unless
   defined(&OBD_IOC_STATFS);
 eval 'sub OBD_IOC_SYNC () { &_IOC(2, ord(\'f\'), 16, 4);}' unless
   defined(&OBD_IOC_SYNC);
   defined(&OBD_IOC_STATFS);
 eval 'sub OBD_IOC_SYNC () { &_IOC(2, ord(\'f\'), 16, 4);}' unless
   defined(&OBD_IOC_SYNC);
+eval 'sub OBD_IOC_READ2 () { &_IOC(3, ord(\'f\'), 17, 4);}' unless
+  defined(&OBD_IOC_READ2);
+eval 'sub OBD_IOC_FORMATOBD () { &_IOC(3, ord(\'f\'), 18, 4);}' unless
+  defined(&OBD_IOC_FORMATOBD);
+eval 'sub OBD_IOC_PARTITION () { &_IOC(3, ord(\'f\'), 19, 4);}' unless
+  defined(&OBD_IOC_PARTITION);
+eval 'sub OBD_IOC_ATTACH () { &_IOC(3, ord(\'f\'), 20, 4);}' unless
+  defined(&OBD_IOC_ATTACH);
 
 eval 'sub ATTR_MODE () {1;}' unless defined(&ATTR_MODE);
 eval 'sub ATTR_UID () {2;}' unless defined(&ATTR_UID);
 
 eval 'sub ATTR_MODE () {1;}' unless defined(&ATTR_MODE);
 eval 'sub ATTR_UID () {2;}' unless defined(&ATTR_UID);
@@ -63,22 +71,19 @@ use Carp;
 use Term::ReadLine;
 use IO::Handle;
 
 use Term::ReadLine;
 use IO::Handle;
 
-my ($device, $filesystem);
+
+my ($device, $filesystem, $file);
 # startup options (I'll replace these when I have some to replace with)
 # startup options (I'll replace these when I have some to replace with)
-GetOptions("device=s" => \$device, "fs=s" => $filesystem) || die "Getoptions";
+GetOptions("f!" => \$file, "device=s" => \$device, "fs=s" => $filesystem) || die "Getoptions";
 
 # genuine new simulated OBD device
 
 # genuine new simulated OBD device
-$device = "/dev/obd" unless $device;
+$device = "/dev/obd0" unless $device;
 # object store in the ext2 formatted block device
 $filesystem = "/dev/loop0" unless $filesystem;
 
 # object store in the ext2 formatted block device
 $filesystem = "/dev/loop0" unless $filesystem;
 
-# get a console for the app
-my $term = new Term::ReadLine 'obdcontrol ';
-my $attribs = $term->Attribs;
-$term->ornaments('md,me,,');   # bold face prompt
 
 
-# make sure stdout is not buffered
-STDOUT->autoflush(1);
+
+# get a console for the app
 
 my $line;
 my $command;
 
 my $line;
 my $command;
@@ -86,14 +91,18 @@ my $arg;
 
 my %commands =
     ('create' => {func => "Create", doc => "create: creates a new inode"},
 
 my %commands =
     ('create' => {func => "Create", doc => "create: creates a new inode"},
+     'attach' => {func => "Attach", doc => "format type [adapter bus tid lun]"},
+     '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"},
      '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"},
      'setup' => {func => "Setup", doc => "setup: link the ext2 partition (default /dev/loop0) to this obddev"},
      '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"},
-     'cleanup' => {func => "Cleanup", doc => "detach the superblock from this minor obd dev"},
+     '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]"},
      '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]"},
+     'fsread' => {func => "Read2", doc => "read <inode> <count> [offset]"},
      'write' => {func => "Write", doc => "write <inode> <offset> <text>"},
      'setattr' => {func => "Setattr", doc => "setattr <inode> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"},
      'getattr' => {func => "Getattr", doc => "getattr <inode>: displays inode object attributes"},
      'write' => {func => "Write", doc => "write <inode> <offset> <text>"},
      'setattr' => {func => "Setattr", doc => "setattr <inode> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"},
      'getattr' => {func => "Getattr", doc => "getattr <inode>: displays inode object attributes"},
@@ -109,7 +118,6 @@ my %commands =
 #
 my @jcm_cmd_list = keys %commands;
 
 #
 my @jcm_cmd_list = keys %commands;
 
-$attribs->{attempted_completion_function} = \&completeme;
 #------------------------------------------------------------------------------
 # Open the device, as we need an FD for the ioctl
 sysopen(DEV_OBD, $device, 0) || die "Cannot open $device";
 #------------------------------------------------------------------------------
 # Open the device, as we need an FD for the ioctl
 sysopen(DEV_OBD, $device, 0) || die "Cannot open $device";
@@ -118,8 +126,26 @@ if (!defined($::st = stat($filesystem))) {
     die "Unable to stat $filesystem.\n";
 }
 
     die "Unable to stat $filesystem.\n";
 }
 
-# Get on with the show
-process_line();
+my $term, $attribs;
+
+if ( $file ) {
+       while ( <STDIN> ) {
+           print $_;
+           execute_line($_);
+       }
+       exit 0;
+} else {
+    $term = new Term::ReadLine 'obdcontrol ';
+    $attribs = $term->Attribs;
+    $attribs->{attempted_completion_function} = \&completeme;
+    $term->ornaments('md,me,,');       # bold face prompt
+    
+    # make sure stdout is not buffered
+    STDOUT->autoflush(1);
+
+    # Get on with the show
+    process_line();
+}
 
 #------------------------------------------------------------------------------
 sub completeme {
 
 #------------------------------------------------------------------------------
 sub completeme {
@@ -156,7 +182,12 @@ sub execute_line {
     my @arg = split(' ', $line);
     my $word = shift @arg;
 
     my @arg = split(' ', $line);
     my $word = shift @arg;
 
-    my $cmd = find_command($word);
+    my $cmd;
+    if ( $file ) {
+       $cmd = $word;
+    } else {
+       $cmd = find_command($word);
+    }
     unless ($cmd) {
        printf STDERR "$word: No such command, or not unique.\n";
        return (-1);
     unless ($cmd) {
        printf STDERR "$word: No such command, or not unique.\n";
        return (-1);
@@ -170,10 +201,89 @@ sub execute_line {
     return (&{$commands{$cmd}->{func}}(@arg));
 }
 
     return (&{$commands{$cmd}->{func}}(@arg));
 }
 
+
+sub Attach {
+    my $err = 0;
+    my $type = shift;
+    my $data;
+    my $datalen = 0;
+
+    if ($type eq "obdscsi" ) {
+       my $adapter = shift;
+       my $bus = shift;
+       my $tid = shift;
+       my $lun = shift;
+       $data = pack("iiiii", $adapter, $bus, $tid, $lun, $size);
+       $datalen = 4 * 4;
+    }
+
+    my $packed = pack("ipip", 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 Format {
+    my $err = 0;
+    my $size = shift;
+    my $data = pack("i", $size);
+    my $datalen = 4;
+
+    my $packed = pack("ip", $datalen, $data);
+    my $rc = ioctl(DEV_OBD, &OBD_IOC_FORMATOBD, $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 Partition {
+    my $err = 0;
+    my $partno = shift;
+    my $size = shift;
+    my $data = pack("ii", $partno, $size);
+    my $datalen = 2 * 4;
+
+    my $packed = pack("ip", $datalen, $data);
+    my $rc = ioctl(DEV_OBD, &OBD_IOC_PARTITION, $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 Setup {
     my $err = 0;
 sub Setup {
     my $err = 0;
-    my $packed = pack("L", $::st->rdev());
-    my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP_SUPER, $packed);
+    my $type = shift;
+    my $data;
+    my $datalen = 0;
+    
+    $type = "sim_obd" unless $type;
+
+    if ( $type eq "sim_obd" ) {
+       my $dev = shift;
+       $dev = $::st->rdev() unless $dev;
+       $data = pack("i", $dev);
+       $datalen = 4;
+    }
+
+    my $packed = pack("ip", $datalen, $data);
+    my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP_OBDDEV, $packed);
 
     if (!defined $rc) {
        print STDERR "ioctl failed: $!\n";
 
     if (!defined $rc) {
        print STDERR "ioctl failed: $!\n";
@@ -186,12 +296,13 @@ sub Setup {
 
 sub Cleanup {
     my $err = "0";
 
 sub Cleanup {
     my $err = "0";
-    my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP_SUPER, $err);
+    my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP, $err);
 
     if (!defined $rc) {
        print STDERR "ioctl failed: $!\n";
     } elsif ($rc eq "0 but true") {
        print "Finished (success)\n";
 
     if (!defined $rc) {
        print STDERR "ioctl failed: $!\n";
     } elsif ($rc eq "0 but true") {
        print "Finished (success)\n";
+       $::client_id = 0;
     } else {
        print "ioctl returned error code $rc.\n";
     }
     } else {
        print "ioctl returned error code $rc.\n";
     }
@@ -199,29 +310,17 @@ sub Cleanup {
 
 
 sub Connect {
 
 
 sub Connect {
-    my $id = 0;
-    my $ino = 0;
-    my $bs = 0;
-    my $bs_b = 0;
     my $rc;
 
     my $rc;
 
-    # unsigned int conn_id;
-    # unsigned long conn_ino;
-    # unsigned long conn_blocksize;
-    # unsigned char conn_blocksize_bits;
-
-    my $packed = pack("ILLC", $id, $ino, $bs, $bs_b);
+    my $packed = "";
     $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
     $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
-    ($id, $ino, $bs, $bs_b) = unpack("ILLC", $packed);
+    $id = unpack("I", $packed);
 
     if (!defined $rc) {
        print STDERR "ioctl failed: $!\n";
     } elsif ($rc eq "0 but true") {
        $::client_id = $id;
        print "Client ID     : $id\n";
 
     if (!defined $rc) {
        print STDERR "ioctl failed: $!\n";
     } elsif ($rc eq "0 but true") {
        $::client_id = $id;
        print "Client ID     : $id\n";
-       print "Root inode    : $ino\n";
-       print "Blocksize     : $bs\n";
-       print "Blocksize bits: $bs_b\n";
        print "Finished (success)\n";
     } else {
        print "ioctl returned error code $rc.\n";
        print "Finished (success)\n";
     } else {
        print "ioctl returned error code $rc.\n";
@@ -259,16 +358,16 @@ sub Create {
     my $arg = shift;
     my $quiet = shift;
     my $rc;
     my $arg = shift;
     my $quiet = shift;
     my $rc;
+    my $prealloc = 0;
 
     if (defined($quiet) && !($quiet eq "quiet")) {
        print "syntax: create [number of objects [quiet]]\n";
        return;
     }
 
 
     if (defined($quiet) && !($quiet eq "quiet")) {
        print "syntax: create [number of objects [quiet]]\n";
        return;
     }
 
-    my $packed = pack("I", $::client_id);
+    my $packed = pack("IL", $::client_id, $prealloc);
     if (!defined($arg) || scalar($arg) < 2) {
        print "Creating 1 object...\n";
     if (!defined($arg) || scalar($arg) < 2) {
        print "Creating 1 object...\n";
-       my $packed = pack("I", 0);
        $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
        if (!defined($quiet)) {
            my $ino = unpack("L", $packed);
        $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
        if (!defined($quiet)) {
            my $ino = unpack("L", $packed);
@@ -281,9 +380,11 @@ sub Create {
        for ($i = 0; $i < scalar($arg); $i++) {
            $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
            my $ino = unpack("L", $packed);
        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") || $packed == 0) {
+           if (!($rc eq "0 but true")) {
                last;
                last;
+               $packed = pack("IL", $::client_id, $prealloc);
            } elsif (!defined($quiet)) {
            } elsif (!defined($quiet)) {
+               $packed = pack("IL", $::client_id, $prealloc);
                print "Created object #$ino.\n";
            }
        }
                print "Created object #$ino.\n";
            }
        }
@@ -493,6 +594,55 @@ sub Read {
     }
 }
 
     }
 }
 
+sub Read2 {
+    if (!defined($::client_id)) {
+       print "You must first ``connect''.\n";
+       return;
+    }
+
+    my $inode = shift;
+    my $count = shift;
+    my $offset = shift;
+  
+    if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
+       $count < 1 || (defined($offset) && $offset < 0)) {
+       print "invalid arguments; type \"help read\" for a synopsis\n";
+       return;
+    }
+
+    if (!defined($offset)) {
+       $offset = 0;
+    }
+
+    print("Reading $count bytes starting at byte $offset from object " .
+         "$inode...\n");
+
+    # "allocate" a large enough buffer
+    my $buf = sprintf("%${count}s", " ");
+    die "suck" if (length($buf) != $count);
+
+    # the perl we're using doesn't support pack type Q, and offset is 64 bits
+    my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
+
+    my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed);
+
+    $retval = unpack("l", $packed);
+
+    if (!defined $rc) {
+       print STDERR "ioctl failed: $!\n";
+    } elsif ($rc eq "0 but true") {
+       if ($retval >= 0) {
+               print substr($buf, 0, $retval);
+               print "\nRead $retval of an attempted $count bytes.\n";
+               print "Finished (success)\n";
+       } else {
+               print "Finished (error $retval)\n";
+       }
+    } else {
+       print "ioctl returned error code $rc.\n";
+    }
+}
+
 sub Write {
     if (!defined($::client_id)) {
        print "You must first ``connect''.\n";
 sub Write {
     if (!defined($::client_id)) {
        print "You must first ``connect''.\n";