X-Git-Url: https://git.whamcloud.com/?p=fs%2Flustre-release.git;a=blobdiff_plain;f=lustre%2Fobdclass%2Fobdcontrol;h=295a7d11f4e834b5bcb5d28a41b63716961646f0;hp=da661fb5c18bfad2b135fab1dcd23780fb50add8;hb=8c1e3ed09d09830bb6628d4e9b6f8ed37b4ec338;hpb=1e7d496794cc655eabb30c1edb1a269aec6309dc diff --git a/lustre/obdclass/obdcontrol b/lustre/obdclass/obdcontrol index da661fb..295a7d1 100755 --- a/lustre/obdclass/obdcontrol +++ b/lustre/obdclass/obdcontrol @@ -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); -eval 'sub OBD_IOC_SETUP_OBDDEV () { &_IOC(1, ord(\'f\'), 4, 4);}' unless - defined(&OBD_IOC_SETUP_OBDDEV); -eval 'sub OBD_IOC_CLEANUP_OBDDEV () { &_IOC(0, ord(\'f\'), 5, 0);}' unless - defined(&OBD_IOC_CLEANUP_OBDDEV); +eval 'sub OBD_IOC_SETUP () { &_IOC(1, ord(\'f\'), 4, 4);}' unless + defined(&OBD_IOC_SETUP); +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 @@ -37,8 +37,6 @@ eval 'sub OBD_IOC_GETATTR () { &_IOC(2, ord(\'f\'), 10, 4);}' unless defined(&OBD_IOC_GETATTR); eval 'sub OBD_IOC_READ () { &_IOC(3, ord(\'f\'), 11, 4);}' unless defined(&OBD_IOC_READ); -eval 'sub OBD_IOC_READ2 () { &_IOC(3, ord(\'f\'), 17, 4);}' unless - defined(&OBD_IOC_READ2); eval 'sub OBD_IOC_WRITE () { &_IOC(3, ord(\'f\'), 12, 4);}' unless defined(&OBD_IOC_WRITE); eval 'sub OBD_IOC_CONNECT () { &_IOC(2, ord(\'f\'), 13, 4);}' unless @@ -49,14 +47,40 @@ 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); - -eval 'sub ATTR_MODE () {1;}' unless defined(&ATTR_MODE); -eval 'sub ATTR_UID () {2;}' unless defined(&ATTR_UID); -eval 'sub ATTR_GID () {4;}' unless defined(&ATTR_GID); -eval 'sub ATTR_SIZE () {8;}' unless defined(&ATTR_SIZE); -eval 'sub ATTR_ATIME () {16;}' unless defined(&ATTR_ATIME); -eval 'sub ATTR_MTIME () {32;}' unless defined(&ATTR_MTIME); -eval 'sub ATTR_CTIME () {64;}' unless defined(&ATTR_CTIME); +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 OBD_IOC_DETACH () { &_IOC(3, ord(\'f\'), 21, 4);}' unless + defined(&OBD_IOC_DETACH); +eval 'sub OBD_IOC_COPY () { &_IOC(3, ord(\'f\'), 22, 4);}' unless + defined(&OBD_IOC_COPY); +eval 'sub OBD_IOC_MIGR () { &_IOC(3, ord(\'f\'), 23, 4);}' unless + defined(&OBD_IOC_MIGR); +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_EXT2_RUNIT () { &_IOC(3, ord(\'f\'), 61, 4);}' unless + defined(&OBD_EXT2_RUNIT); + +eval 'sub OBD_MD_FLALL () {~0;}' unless defined(&OBD_MD_FLALL); +eval 'sub OBD_MD_FLATIME () {1<<1;}' unless defined(&OBD_MD_FLATIME); +eval 'sub OBD_MD_FLMTIME () {1<<2;}' unless defined(&OBD_MD_FLMTIME); +eval 'sub OBD_MD_FLCTIME () {1<<3;}' unless defined(&OBD_MD_FLCTIME); +eval 'sub OBD_MD_FLSIZE () {1<<4;}' unless defined(&OBD_MD_FLSIZE); +eval 'sub OBD_MD_FLMODE () {1<<7;}' unless defined(&OBD_MD_FLMODE); +eval 'sub OBD_MD_FLUID () {1<<8;}' unless defined(&OBD_MD_FLUID); +eval 'sub OBD_MD_FLGID () {1<<9;}' unless defined(&OBD_MD_FLGID); use Getopt::Long; use File::stat; @@ -65,42 +89,152 @@ use Carp; use Term::ReadLine; use IO::Handle; -my ($device, $filesystem); -# startup options (I'll replace these when I have some to replace with) -GetOptions("device=s" => \$device, "fs=s" => $filesystem) || die "Getoptions"; -# genuine new simulated OBD device -$device = "/dev/obd" unless $device; -# object store in the ext2 formatted block device -$filesystem = "/dev/loop0" unless $filesystem; +# 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 60 +# #define OBD_OBDMDSZ 60 +# /* 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_count o_generation; +# obd_flag o_valid; /* hot fields in this obdo */ +# char o_inline[60]; +# char o_obdmd[60]; +# 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 L a60 a60 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->{generation}, + $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->{generation}, + $obdo->{valid}, + $obdo->{inline}, + $obdo->{obdmd}) = unpack "x${offset}Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 L L L L L L L L L a60 a60", $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: %o\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"; -# 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 $arg; my %commands = - ('create' => {func => "Create", doc => "create: creates a new inode"}, - 'setup' => {func => "Setup", doc => "setup: link the ext2 partition (default /dev/loop0) to this obddev"}, + ('device' => {func => "Device", doc => "device : open another OBD device"}, + 'create' => {func => "Create", doc => "create [ [ [quiet]]]: create new object(s) (files, unless mode is given)"}, + 'attach' => {func => "Attach", doc => "attach { ext2_obd | snap_obd snapdev snapidx tableno | scsi_obd adapter bus tid lun }: attach this minor device to the specified driver" }, + 'detach' => {func => "Detach", doc => "detach this minor device"}, + 'testext2iterator' => {func => "TestExt2Iterator", doc => "test ext2 iterator function"}, + 'snapset' => {func => "SnapSetTable", doc => "snapset : set the table (created with snaptable) as table #tableno" }, + 'snapprint' => {func => "SnapPrint", doc => "snapprint : 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 : copy objects"}, + 'migrate' => {func => "Migrate", doc => "migrate : migrate data from one object to another"}, + 'partition' => {func => "Partition", doc => "partition : create a partition"}, + 'format' => {func => "Format", doc => "format : format a partition"}, + '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 : destroys an object"}, '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 [offset]"}, - 'fsread' => {func => "Read2", doc => "read [offset]"}, - 'write' => {func => "Write", doc => "write "}, - 'setattr' => {func => "Setattr", doc => "setattr [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"}, - 'getattr' => {func => "Getattr", doc => "getattr : displays inode object attributes"}, - 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num inodes."}, + 'read' => {func => "Read", doc => "read [offset]: read data from object"}, + 'fsread' => {func => "Read2", doc => "read [offset]: read data from object"}, + 'write' => {func => "Write", doc => "write : write data to object"}, + 'setattr' => {func => "Setattr", doc => "setattr [mode [uid [gid [size [atime [mtime [ctime]]]]]]]: sets object attributes"}, + 'getattr' => {func => "Getattr", doc => "getattr : displays object attributes"}, + 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num objects."}, 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"}, 'help' => {func => \&Help, doc => "help: this message"}, 'quit' => {func => \&Quit, doc => "see \"exit\""}, @@ -112,17 +246,44 @@ my %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"; +my $term, $attribs; + + +# Get going.... + +Device($::device); -if (!defined($::st = stat($filesystem))) { - die "Unable to stat $filesystem.\n"; +sub readl { + if ( $file ) { + my $str = ; + chop($str); + return $str; + } else { + return $term->readline(@_); + } } -# Get on with the show -process_line(); + + +if ( $file ) { + while ( ) { + 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 { @@ -156,28 +317,547 @@ sub process_line { sub execute_line { my $line = shift; - my @arg = split(' ', $line); - my $word = shift @arg; + my @cmdline = split(' ', $line); + my $word = shift @cmdline; - 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); } if ($cmd eq "help" || $cmd eq "exit" || $cmd eq "quit") { - return (&{$commands{$cmd}->{func}}(@arg)); + return (&{$commands{$cmd}->{func}}(@cmdline)); } # Call the function. - return (&{$commands{$cmd}->{func}}(@arg)); + return (&{$commands{$cmd}->{func}}(@cmdline)); +} + + +# 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"; + } + $::device = $device; + # Open the device, as we need an FD for the ioctl + sysopen(DEV_OBD, $device, 0) || die "Cannot open $device"; + print "Device now $device\n"; +} + + +sub Attach { + my $err = 0; + my $type = shift; + my $data; + my $datalen = 0; + + 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("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") { + $data = pack("i", 4711); # bogus data + $datalen = 0; + } 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); + + 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 Detach { + my $err = 0; + my $data = ""; + my $rc = ioctl(DEV_OBD, &OBD_IOC_DETACH, $data); + + 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 TestExt2Iterator { + if (!defined($::client_id)) { + print "You must first ``connect''.\n"; + return; + } + + my $err = 0; + my $type = "ext2_obd"; + + $data = pack("i", 4711); # bogus data + $datalen = 4; + + 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_EXT2_RUNIT, $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 SnapDelete { + if (!defined($::client_id)) { + print "You must first ``connect''.\n"; + return; + } + + my $err = 0; + my $type = "snap_obd"; + + $data = pack("i", 4711); # bogus data + $datalen = 4; + + 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); + + # 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"; + } elsif ($rc eq "0 but true") { + print "Finished (success)\n"; + } else { + print "ioctl returned error code $rc.\n"; + } +} + + +# 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"; + my $snaptableno = shift; + + $data = pack("i", $snaptableno); + $datalen = 4; + + 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_SNAP_PRINTTABLE, $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 SnapSetTable { + my $err = 0; + my $type = "snap_obd"; + my $snaptableno = shift; + my $file = shift; + my $snapcount; + my $table = {}; + my $data; + my $datalen = 0; + + if ( ! -f $file ) { + print "No such file $file\n"; + } + + $table = ReadSnapShotTable($file); + + $snapcount = keys %{$table}; + print "Snapcount $snapcount\n"; + + if ( ! defined $table->{0} ) { + print "No current snapshot in table! First make one\n"; + return ; + } + $data = pack("ii", $snaptableno, $snapcount); + $datalen = 2 * 4; + foreach my $time (sort keys %{$table}) { + # XXX we should change to pack LL instead of I for times + $data .= pack("Ii", $time, $table->{$time}); + $datalen += 8; + } + + 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_SNAP_SETTABLE, $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: "); + if ( ! -f $file ) { + `touch $file`; + } + my $table = ReadSnapShotTable($file); + + again: + PrintSnapShotTable($table); + my $action = &readl("Add, Delete or Quit [adq]: "); + goto done if ($action =~ "^q.*" ); + goto add if ($action =~ "^a.*"); + goto del if ($action =~ "^d.*"); + goto again; + + add: + my $idx = &readl("enter index where you want this snapshot: "); + my $time = &readl("enter time or 'now' or 'current': "); + my $oldtime = SnapFindTimeFromIdx($idx, $table); + if (defined $oldtime) { + print "This already exists, first clean up\n"; + goto again; + } + + if ( $time eq 'now' ) { + $time = time; + } elsif ( $time eq 'current' ) { + $time = 0; + } + $table->{$time} = $idx; + goto again; + + del: + $didx = &readl("Enter index to delete: "); + my $deltime = SnapFindTimeFromIdx($didx, $table); + delete $table->{$deltime} if defined $deltime; + goto again; + + done: + my $ok = &readl("OK with new table? [Yn]: "); + unless ( $ok eq "n" ) { + WriteSnapShotTable($file, $table); + } +} + +sub SnapFindTimeFromIdx { + my $idx = shift; + my $table = shift; + + foreach my $time ( keys %{$table} ) { + if ( $table->{$time} == $idx ) { + return $time; + } + } + undef; +} + +sub PrintSnapShotTable { + my $table = shift; + my $time; + + foreach $time ( sort keys %{$table} ) { + my $stime = localtime($time); + if ( ! $time ) { + $stime = "current"; + } + printf "Time: %s -- Index %d\n", $stime, $table->{$time}; + } +} + +sub ReadSnapShotTable { + + my $file = shift; + my $table = {}; + + open FH, "<$file"; + while ( ) { + my ($time, $index) = split ; + $table->{$time} = $index; + } + close FH; + + PrintSnapShotTable($table); + + return $table; +} + +sub WriteSnapShotTable { + my $file = shift; + my $table = shift; + + open FH, ">$file"; + foreach my $time ( sort keys %{$table} ) { + print FH "$time $table->{$time}\n"; + } + close FH; +} + +sub Copy { + my $err = 0; + my $src_obdo; + my $dst_obdo; + + # Note: _copy IOCTL takes parameters as dst, src. + # Copy function takes parameters as src, dst. + $src_obdo->{id} = shift; + $dst_obdo->{id} = shift; + $src_obdo->{valid} = &OBD_MD_FLALL; + + # XXX need to fix copy so we can have 2 client IDs here + my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo); + + my $rc = ioctl(DEV_OBD, &OBD_IOC_COPY, $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 Migrate { + my $err = 0; + + # Note: _migr IOCTL takes parameters as dst, src. + # Migrate function takes parameters as src, dst. + $src_obdo->{id} = shift; + $dst_obdo->{id} = shift; + $src_obdo->{valid} = &OBD_MD_FLALL; + + # We pack a dummy connection ID here + my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo); + + my $rc = ioctl(DEV_OBD, &OBD_IOC_MIGR, $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; + my $arg = shift; + my $data; + my $datalen = 0; + + # XXX we need a getinfo ioctl to validate parameters + # by type here + + if ($arg && !defined($::st = stat($arg))) { + print "$dev is not a valid device\n"; + return; + } - my $packed = pack("La24", $::st->rdev(), "sim_obd"); - my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP_OBDDEV, $packed); + if ( $arg ) { + $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, $packed); if (!defined $rc) { print STDERR "ioctl failed: $!\n"; @@ -190,12 +870,13 @@ sub Setup { sub Cleanup { my $err = "0"; - my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP_OBDDEV, $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"; + $::client_id = 0; } else { print "ioctl returned error code $rc.\n"; } @@ -203,29 +884,17 @@ sub Cleanup { sub Connect { - my $id = 0; - my $ino = 0; - my $bs = 0; - my $bs_b = 0; 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); - ($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"; - 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"; @@ -260,36 +929,52 @@ sub Disconnect { } sub Create { - my $arg = shift; + if (!defined($::client_id)) { + print "You must first ``connect''.\n"; + return; + } + + my $num = shift; + my $mode = shift; my $quiet = shift; my $rc; + my $prealloc = 0; + + if (!defined($num)) { + $num = 1; + } + + if (!defined($mode)) { + $mode = 0100644; # create a file (rw-r--r--) if not specified + } - if (defined($quiet) && !($quiet eq "quiet")) { - print "syntax: create [number of objects [quiet]]\n"; + if (scalar($num) < 1 || defined($quiet) && $quiet ne "quiet") { + print "usage: create [ [ [quiet]]]\n"; return; } - my $packed = pack("I", $::client_id); - 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); - print "Created object #$ino.\n"; - } - } else { - my $i; + my $i; + my $id = 0; # can't currently request IDs + + print "Creating " . scalar($num) . " object"; + if (scalar($num) > 1) { + print "s"; + } + print "\n"; + + for ($i = 0; $i < scalar($num); $i++) { + my $obdo; + $obdo->{id} = $id; + $obdo->{mode} = scalar($mode); + $obdo->{valid} = &OBD_MD_FLMODE; - print "Creating " . scalar($arg) . " objects...\n"; - 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) { - last; - } elsif (!defined($quiet)) { - print "Created object #$ino.\n"; - } + my $packed = pack("I", $::client_id) . obdo_pack($obdo); + $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed); + if ($rc ne "0 but true") { + last; + } elsif (!defined($quiet)) { + $obdo = obdo_unpack($packed, 4); + print "Created object #$obdo->{id}.\n"; } } @@ -321,15 +1006,15 @@ sub Destroy { return; } - my $arg = shift; + my $id = shift; - if (!defined($arg) || scalar($arg) < 1) { - print "destroy requires the object number to destroy.\n"; + if (!defined($id) || scalar($id) < 1) { + print "usage: destroy \n"; return; } - print "Destroying object $arg...\n"; - my $packed = pack("IL", $::client_id, $arg); + print "Destroying object $id...\n"; + my $packed = pack("IL", $::client_id, $id); my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed); if (!defined $rc) { @@ -347,32 +1032,25 @@ sub Getattr { return; } - my $inode = shift; + my $id = shift; - if (!defined($inode) || scalar($inode) < 1) { + if (!defined($id) || scalar($id) < 1) { print "invalid arguments; type \"help getattr\" for a synopsis\n"; return; } # see Setattr - my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0, - 0); + my $obdo; + $obdo->{id} = $id; + $obdo->{valid} = &OBD_MD_FLALL; + 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"; } @@ -384,59 +1062,49 @@ sub Setattr { return; } - my $inode = shift; - my $valid = 0; - my $mode = oct(shift); - my $uid = shift; - my $gid = shift; - my $size = shift; - my $atime = shift; - my $mtime = shift; - my $ctime = shift; + my $id = shift; - if (defined($uid)) { - $valid |= &ATTR_UID; + if (!defined($id) || scalar($id) < 1) { + print "invalid arguments; type \"help setattr\" for a synopsis\n"; + return; } - if (defined($gid)) { - $valid |= &ATTR_GID; + + # XXX we do not currently set all of the fields in the obdo + my $obdo; + $obdo->{id} = $id; + $obdo->{mode} = oct(shift); + $obdo->{uid} = shift; + $obdo->{gid} = shift; + $obdo->{size} = shift; + $obdo->{atime} = shift; + $obdo->{mtime} = shift; + $obdo->{ctime} = shift; + $obdo->{valid} = 0; + + if (defined($obdo->{atime})) { + $obdo->{valid} |= &OBD_MD_FLATIME; } - if (defined($size)) { - $valid |= &ATTR_SIZE; + if (defined($obdo->{mtime})) { + $obdo->{valid} |= &OBD_MD_FLMTIME; } - if (defined($atime)) { - $valid |= &ATTR_ATIME; + if (defined($obdo->{ctime})) { + $obdo->{valid} |= &OBD_MD_FLCTIME; } - if (defined($mtime)) { - $valid |= &ATTR_MTIME; + if (defined($obdo->{size})) { + $obdo->{valid} |= &OBD_MD_FLSIZE; } - if (defined($ctime)) { - $valid |= &ATTR_CTIME; + if (defined($obdo->{mode})) { + $obdo->{valid} |= &OBD_MD_FLMODE; } - if (defined($mode)) { - $valid |= &ATTR_MODE; + if (defined($obdo->{uid})) { + $obdo->{valid} |= &OBD_MD_FLUID; } - - if (!defined($inode) || scalar($inode) < 1) { - print "invalid arguments; type \"help setattr\" for a synopsis\n"; - return; + if (defined($obdo->{gid})) { + $obdo->{valid} |= &OBD_MD_FLGID; } - #struct iattr { - # unsigned int ia_valid; (32) - # umode_t ia_mode; (16) - # uid_t ia_uid; (16) - # gid_t ia_gid; (16) - # -- 16 bit alignment here! -- - # off_t ia_size; (32) - # time_t ia_atime; (32) - # time_t ia_mtime; (32) - # time_t ia_ctime; (32) - # unsigned int ia_attr_flags; (32) - #}; - - printf "valid is %x, mode is %o\n", $valid, $mode; - my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode, - $uid, $gid, $size, $atime, $mtime, $ctime, 0); + printf "valid is %x, mode is %o\n", $obdo->{valid}, $obdo->{mode}; + my $packed = pack("L", $::client_id) . obdo_pack($obdo); my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed); if (!defined $rc) { @@ -454,11 +1122,11 @@ sub Read { return; } - my $inode = shift; + my $id = shift; my $count = shift; my $offset = shift; - if (!defined($inode) || scalar($inode) < 1 || !defined($count) || + if (!defined($id) || scalar($id) < 1 || !defined($count) || $count < 1 || (defined($offset) && $offset < 0)) { print "invalid arguments; type \"help read\" for a synopsis\n"; return; @@ -469,14 +1137,18 @@ sub Read { } print("Reading $count bytes starting at byte $offset from object " . - "$inode...\n"); + "$id...\n"); # "allocate" a large enough buffer my $buf = sprintf("%${count}s", " "); die "suck" if (length($buf) != $count); + my $obdo; + $obdo->{id} = $id; + # 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 $packed = pack("L", $::client_id) . obdo_pack($obdo) . + pack("p LL LL", $buf, $count, $offset); my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed); @@ -503,11 +1175,11 @@ sub Read2 { return; } - my $inode = shift; + my $id = shift; my $count = shift; my $offset = shift; - if (!defined($inode) || scalar($inode) < 1 || !defined($count) || + if (!defined($id) || scalar($id) < 1 || !defined($count) || $count < 1 || (defined($offset) && $offset < 0)) { print "invalid arguments; type \"help read\" for a synopsis\n"; return; @@ -518,14 +1190,18 @@ sub Read2 { } print("Reading $count bytes starting at byte $offset from object " . - "$inode...\n"); + "$id...\n"); # "allocate" a large enough buffer my $buf = sprintf("%${count}s", " "); die "suck" if (length($buf) != $count); + my $obdo; + $obdo->{id} = $id; + # 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 $packed = pack("L", $::client_id) . obdo_pack($obdo) . + pack("p LL LL", $buf, $count, $offset); my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed); @@ -552,12 +1228,12 @@ sub Write { return; } - my $inode = shift; + my $id = shift; my $offset = shift; my $text = join(' ', @_); my $count = length($text); - if (!defined($inode) || scalar($inode) < 1 || !defined($offset) || + if (!defined($id) || scalar($id) < 1 || !defined($offset) || scalar($offset) < 0) { print "invalid arguments; type \"help write\" for a synopsis\n"; return; @@ -569,10 +1245,15 @@ sub Write { } print("Writing $count bytes starting at byte $offset to object " . - "$inode...\n"); + "$id...\n"); + + my $obdo; + $obdo->{id} = $id; # the perl we're using doesn't support pack type Q - my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, $offset, 0); + my $packed = pack("L", $::client_id) . obdo_pack($obdo) . + pack("p LL LL", $buf, $count, $offset); + my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed); $retval = unpack("l", $packed); @@ -592,20 +1273,20 @@ sub Write { } sub Preallocate { - my $arg = shift; + my $num = shift; if (!defined($::client_id)) { print "You must first ``connect''.\n"; return; } - if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) { - $arg = 32; + if (!defined($num) || scalar($num) < 1 || scalar($num) > 32) { + $num = 32; } - print "Preallocating $arg inodes...\n"; - my $packed = pack("LLx128", $::client_id, $arg); - # client id, alloc, inodes[32] + print "Preallocating $num objects...\n"; + # client id, alloc, id[32] + my $packed = pack("LLx128", $::client_id, $num); my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed); @@ -613,11 +1294,11 @@ sub Preallocate { print STDERR "ioctl failed: $!\n"; } elsif ($rc eq "0 but true") { my $alloc = unpack("x4L", $packed); - my @inodes = unpack("x8L32", $packed); + my @ids = unpack("x8L32", $packed); my $i; - print "Got $alloc inodes: "; - foreach $i (@inodes) { + print "Got $alloc objects: "; + foreach $i (@ids) { print $i . " "; } print "\nFinished (success)\n"; @@ -678,12 +1359,12 @@ sub Statfs { } sub Help { - my $arg = shift; + my $cmd = shift; - if ( !$arg || !$commands{$arg} ) { + if ( !$cmd || !$commands{$cmd} ) { print "Comands: ", join( ' ', @jcm_cmd_list), "\n"; } else { - print "Usage: " . $commands{$arg}->{doc} . "\n"; + print "Usage: " . $commands{$cmd}->{doc} . "\n"; } }