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);
33 eval 'sub OBD_IOC_DEC_USE_COUNT () { &_IOC(0, ord(\'f\'), 8, 0);}' unless
34 defined(&OBD_IOC_DEC_USE_COUNT);
35 eval 'sub OBD_IOC_SETATTR () { &_IOC(1, ord(\'f\'), 9, 4);}' unless
36 defined(&OBD_IOC_SETATTR);
37 eval 'sub OBD_IOC_GETATTR () { &_IOC(2, ord(\'f\'), 10, 4);}' unless
38 defined(&OBD_IOC_GETATTR);
39 eval 'sub OBD_IOC_READ () { &_IOC(3, ord(\'f\'), 11, 4);}' unless
40 defined(&OBD_IOC_READ);
41 eval 'sub OBD_IOC_WRITE () { &_IOC(3, ord(\'f\'), 12, 4);}' unless
42 defined(&OBD_IOC_WRITE);
43 eval 'sub OBD_IOC_CONNECT () { &_IOC(2, ord(\'f\'), 13, 4);}' unless
44 defined(&OBD_IOC_CONNECT);
45 eval 'sub OBD_IOC_DISCONNECT () { &_IOC(1, ord(\'f\'), 14, 4);}' unless
46 defined(&OBD_IOC_DISCONNECT);
47 eval 'sub OBD_IOC_STATFS () { &_IOC(3, ord(\'f\'), 15, 4);}' unless
48 defined(&OBD_IOC_STATFS);
49 eval 'sub OBD_IOC_SYNC () { &_IOC(2, ord(\'f\'), 16, 4);}' unless
50 defined(&OBD_IOC_SYNC);
52 eval 'sub OBD_IOC_READ2 () { &_IOC(3, ord(\'f\'), 17, 4);}' unless
53 defined(&OBD_IOC_READ2);
55 eval 'sub OBD_IOC_FORMATOBD () { &_IOC(3, ord(\'f\'), 18, 4);}' unless
56 defined(&OBD_IOC_FORMATOBD);
58 eval 'sub OBD_IOC_PARTITION () { &_IOC(3, ord(\'f\'), 19, 4);}' unless
59 defined(&OBD_IOC_PARTITION);
60 eval 'sub OBD_IOC_ATTACH () { &_IOC(3, ord(\'f\'), 20, 4);}' unless
61 defined(&OBD_IOC_ATTACH);
62 eval 'sub OBD_IOC_DETACH () { &_IOC(3, ord(\'f\'), 21, 4);}' unless
63 defined(&OBD_IOC_DETACH);
64 eval 'sub OBD_IOC_COPY () { &_IOC(3, ord(\'f\'), 22, 4);}' unless
65 defined(&OBD_IOC_COPY);
66 eval 'sub OBD_IOC_MIGR () { &_IOC(3, ord(\'f\'), 23, 4);}' unless
67 defined(&OBD_IOC_MIGR);
68 eval 'sub OBD_IOC_PUNCH () { &_IOC(3, ord(\'f\'), 24, 4);}' unless
69 defined(&OBD_IOC_PUNCH);
70 eval 'sub OBD_SNAP_SETTABLE () { &_IOC(3, ord(\'f\'), 40, 4);}' unless
71 defined(&OBD_SNAP_SETTABLE);
72 eval 'sub OBD_SNAP_PRINTTABLE () { &_IOC(3, ord(\'f\'), 41, 4);}' unless
73 defined(&OBD_SNAP_PRINTTABLE);
74 eval 'sub OBD_SNAP_DELETE() { &_IOC(3, ord(\'f\'), 42, 4);}' unless
75 defined(&OBD_SNAP_DELETE);
76 eval 'sub OBD_SNAP_RESTORE() { &_IOC(3, ord(\'f\'), 43, 4);}' unless
77 defined(&OBD_SNAP_RESTORE);
79 eval 'sub OBD_EXT2_RUNIT () { &_IOC(3, ord(\'f\'), 61, 4);}' unless
80 defined(&OBD_EXT2_RUNIT);
82 eval 'sub OBD_MD_FLALL () {~0;}' unless defined(&OBD_MD_FLALL);
83 eval 'sub OBD_MD_FLATIME () {1<<1;}' unless defined(&OBD_MD_FLATIME);
84 eval 'sub OBD_MD_FLMTIME () {1<<2;}' unless defined(&OBD_MD_FLMTIME);
85 eval 'sub OBD_MD_FLCTIME () {1<<3;}' unless defined(&OBD_MD_FLCTIME);
86 eval 'sub OBD_MD_FLSIZE () {1<<4;}' unless defined(&OBD_MD_FLSIZE);
87 eval 'sub OBD_MD_FLMODE () {1<<7;}' unless defined(&OBD_MD_FLMODE);
88 eval 'sub OBD_MD_FLUID () {1<<8;}' unless defined(&OBD_MD_FLUID);
89 eval 'sub OBD_MD_FLGID () {1<<9;}' unless defined(&OBD_MD_FLGID);
100 # NOTE long long are layed out in ia32 memory as follows:
101 # u = 0xaaaabbbbccccdddd has ccccdddd at &u and aaaabbbb 4 bytes on
102 # this may be different on other architectures
104 # we use 32-bit integers for all 64-bit quantities in this program
105 # #define OBD_INLINESZ 60
106 # #define OBD_OBDMDSZ 60
107 # /* Note: 64-bit types are 64-bit aligned in structure */
115 # obd_blocks o_blocks;
116 # obd_blksize o_blksize;
121 # obd_flag o_obdflags;
123 # obd_count o_generation;
124 # obd_flag o_valid; /* hot fields in this obdo */
127 # struct list_head o_list;
128 # struct obd_ops *o_op;
133 pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L",
152 0, 0, # struct list_head
177 $obdo->{obdmd}) = unpack "x${offset}Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 L L L L L L L L L a60 a60", $buf;
185 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",
208 GetOptions("f!" => \$file, "device=s" => \$::device, ) || die "Getoptions";
211 # get a console for the app
217 my @procsysobd_objects = ('debug', 'index', 'reset', 'trace', 'vars');
220 ('status' => {func => "Status", doc => "status: show obd device status"},
221 'procsys' => {func => "Procsys", doc => "procsys <file> <value> (set /proc/sys/obd configuration)"},
222 'shell' => {func => "Shell", doc => "shell <shell-command>: execute shell-commands"},
223 'script' => {func => "Script", doc => "script <filename>: read and execute commands from a file"},
224 'insmod' => {func => "Insmod", doc => "insmod <module>: insert kernel module"},
225 'rmmod' => {func => "Rmmod", doc => "rmmod <module>: insert kernel module"},
226 'lsmod' => {func => "Lsmod", doc => "lsmod <module>: list kernel modules"},
227 'device' => {func => "Device", doc => "device <dev>: open another OBD device"},
228 'close' => {func => "Close", doc => "close <dev>: close OBD device"},
229 'create' => {func => "Create", doc => "create [<num> [<mode> [quiet]]]: create new object(s) (files, unless mode is given)"},
230 'attach' => {func => "Attach", doc => "attach { obdext2 | obdsnap snapdev snapidx tableno | obdscsi adapter bus tid lun }: attach this minor device to the specified driver" },
231 'detach' => {func => "Detach", doc => "detach this minor device"},
232 'testext2iterator' => {func => "TestExt2Iterator", doc => "test ext2 iterator function"},
233 'snapset' => {func => "SnapSetTable", doc => "snapset <tableno> <file>: set the table (created with snaptable) as table #tableno" },
234 'snapprint' => {func => "SnapPrint", doc => "snapprint <tableno>: output the contents of table #tableno to the syslog"},
235 'snapdelete' => {func => "SnapDelete", doc => "snapdelete: delete connected snap obd objects from disk"},
236 'snaprestore' => {func => "SnapRestore", doc => "snaprestore : restore connected old snap objects to be current"},
237 'snaptable' => {func => "SnapShotTable", doc => "snaptable: build a snapshot table (interactive)"},
238 'copy' => {func => "Copy", doc => "copy <srcid> <tgtid>: copy objects"},
239 'migrate' => {func => "Migrate", doc => "migrate <srcid> <tgtid>: migrate data from one object to another"},
241 'partition' => {func => "Partition", doc => "partition <type> <adapter> <bus> <tid> <lun> <partition> <size>: create a partition"},
243 'format' => {func => "Format", doc => "format <type> <adapter> <bus> <tid> <lun> <size>: format a partition"},
244 'setup' => {func => "Setup", doc => "setup [type]: link this OBD device to the underlying device (default type obdext2)"},
245 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
246 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
247 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
248 'destroy' => {func => "Destroy", doc => "destroy <id>: destroys an object"},
249 'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
251 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
252 'read' => {func => "Read", doc => "read <id> <count> [offset]: read data from object"},
254 'fsread' => {func => "Read2", doc => "read <id> <count> [offset]: read data from object"},
255 'write' => {func => "Write", doc => "write <id> <offset> <text>: write data to object"},
256 'punch' => {func => "Punch", doc => "punch <id> <start> <count>: punch a hole in object"},
257 'setattr' => {func => "Setattr", doc => "setattr <id> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]: sets object attributes"},
258 'getattr' => {func => "Getattr", doc => "getattr <id>: displays object attributes"},
259 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num objects."},
260 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
261 'help' => {func => \&Help, doc => "help: this message"},
262 'quit' => {func => \&Quit, doc => "see \"exit\""},
263 'exit' => {func => \&Quit, doc => "see \"quit\""}
267 # setup completion function
269 my @jcm_cmd_list = keys %commands;
284 return $term->readline(@_);
293 my $rc = execute_line($_);
294 if ($rc != 0) { last; }
298 $term = new Term::ReadLine 'obdcontrol ';
299 $attribs = $term->Attribs;
300 $attribs->{attempted_completion_function} = \&completeme;
301 $term->ornaments('md,me,,'); # bold face prompt
303 # make sure stdout is not buffered
304 STDOUT->autoflush(1);
307 # Get on with the show
311 #------------------------------------------------------------------------------
313 my ($text, $line, $start, $end) = @_;
314 if (substr($line, 0, $start) =~ /^\s*$/) {
315 if ($] < 5.6) { # PErl version is less than 5.6.0
316 return (exists $commands{$text}) ? $text : 0;
317 #Above line doesn't perform command completion, but
318 #perl5.005 Term-ReadLine lacks support for completion matching
319 #and perl5.6.0 requires glibc2.2.2 that won't run under Redhat6.2......sigh.
322 $attribs->{completion_word} = \@jcm_cmd_list;
323 return $term->completion_matches($text,
324 $attribs->{'list_completion_function'});
332 my @completions = completeme($given, $given, 0, length($given));
333 if ($#completions == 0) {
334 $name = shift @completions;
340 # start making requests
343 $line = $term->readline("obdcontrol > ");
351 my @cmdline = split(' ', $line);
352 my $word = shift @cmdline;
354 return 0 unless ($word);
360 $cmd = find_command($word);
363 printf STDERR "$word: No such command, or not unique.\n";
368 return (&{$commands{$cmd}->{func}}(@cmdline));
373 # select the OBD device we talk to
377 if ( ! $device && ! $::device ) { # first time ever
378 $device = '/dev/obd0';
381 if (($device) && ($::device ne $device)) {
386 print "Disconnecting active session ($::client_id)...";
387 Disconnect($::client_id);
390 if ($opendevfds{$device}) {
391 $::dev_obd = $opendevfds{$device};
394 # Open the device, as we need an FD for the ioctl
395 if (!sysopen(NEW_OBD, $device, 0)) {
396 print "Cannot open $device. Did you insert the obdclass module ?\n";
399 print "Opened device $device\n";
400 $opendevfds{$device} = *NEW_OBD;
401 $::dev_obd = *NEW_OBD;
405 print "Current device is $::device\n";
413 if ( ! $device && ! $::device ) { # first time ever
414 print "Nothing to close\n";
422 if ($::device eq $device) {
424 print "Disconnecting active session ($::client_id)...";
425 Disconnect($::client_id);
429 $fd2close = $opendevfds{$device};
430 if ($fd2close) { # XXXX something wrong in this if statement
432 $opendevfds{$device} = undef;
433 print "Closed device $device\n";
436 print "Device $device was not open\n";
440 if ($::device eq $device) {
444 print "No current device. You just closed the current device ($device).\n";
449 my $cmdfilename = shift;
451 if ( ! $cmdfilename ) {
452 print "please specify a command file name\n";
455 if (! open(CMDF, $cmdfilename)) {
456 print "Cannot open $cmdfilename: $!\n";
464 $rc = execute_line($_);
466 print "Something went wrong .......command exit status: $rc\n";
475 my $user_shell=$ENV{'SHELL'};
476 print "% $user_shell -c '@_'\n";
478 print "please specify a shell command\n";
481 system("$user_shell -c '@_'");
486 my $oldfh = select(STDOUT);
489 system('cat /proc/lustre/obd/*/status');
499 my $set_sysobd = shift;
502 foreach $i (0 .. $#procsysobd_objects) {
503 my $sysobd = $procsysobd_objects[$i];
505 if (defined $set_sysobd) {
506 if ($sysobd ne $set_sysobd) { next; }
508 if (defined $value) { # set this one
509 system("echo \"$value\" > /proc/sys/obd/$sysobd");
511 system("echo \"/proc/sys/obd/$sysobd:\"; cat /proc/sys/obd/$sysobd");
515 system("echo \"/proc/sys/obd/$sysobd:\"; cat /proc/sys/obd/$sysobd");
523 system("insmod $module");
529 system("rmmod $module");
535 system("lsmod $module");
546 print "error: missing type\n";
548 print "usage: attach {obdext2 | obdsnap | obdscsi | obdtrace }\n";
552 if ($type eq "obdscsi" ) {
558 $data = pack("iiii", $adapter, $bus, $tid, $lun);
560 } elsif ($type eq "obdsnap" ) {
565 $data = pack("iii", $snapdev, $snapidx, $tableno);
567 } elsif ($type eq "obdext2") {
568 $data = pack("i", 4711); # bogus data
570 } elsif ($type eq "obdtrace") {
571 $data = pack("i", 4711); # bogus data
574 print "error: unknown attach type $type\n";
578 my $len = length($type);
579 my $cl = length($data);
581 print "type $type (len $len), datalen $datalen ($cl)\n";
582 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
584 if (! defined $::dev_obd) {
585 print "No current device.\n";
588 my $rc = ioctl($::dev_obd, &OBD_IOC_ATTACH, $packed);
591 print STDERR "ioctl failed: $!\n";
593 } elsif ($rc eq "0 but true") {
594 print "Finished (success)\n";
597 print "ioctl returned error code $rc.\n";
607 if (! defined $::dev_obd) {
608 print "No current device.\n";
612 my $rc = ioctl($::dev_obd, &OBD_IOC_DETACH, $data);
615 print STDERR "ioctl failed: $!\n";
617 } elsif ($rc eq "0 but true") {
618 print "Finished (success)\n";
621 print "ioctl returned error code $rc.\n";
627 sub TestExt2Iterator {
628 if (!defined($::client_id)) {
629 print "You must first ``connect''.\n";
634 my $type = "obdext2";
636 $data = pack("i", 4711); # bogus data
639 my $len = length($type);
640 my $cl = length($data);
641 print "type $type (len $len), datalen $datalen ($cl)\n";
642 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
643 if (! defined $::dev_obd) {
644 print "No current device.\n";
648 my $rc = ioctl($::dev_obd, &OBD_EXT2_RUNIT, $packed);
651 print STDERR "ioctl failed: $!\n";
653 } elsif ($rc eq "0 but true") {
654 print "Finished (success)\n";
657 print "ioctl returned error code $rc.\n";
664 if (!defined($::client_id)) {
665 print "You must first ``connect''.\n";
670 my $type = "obdsnap";
672 $data = pack("i", 4711); # bogus data
675 my $len = length($type);
676 my $cl = length($data);
677 print "type $type (len $len), datalen $datalen ($cl)\n";
678 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
680 # XXX We need to fix this up so that after the objects in this snapshot
681 # are deleted, the snapshot itself is also removed from the table.
683 if (! defined $::dev_obd) {
684 print "No current device.\n";
688 my $rc = ioctl($::dev_obd, &OBD_SNAP_DELETE, $packed);
691 print STDERR "ioctl failed: $!\n";
693 } elsif ($rc eq "0 but true") {
694 print "Finished (success)\n";
697 print "ioctl returned error code $rc.\n";
703 # this routine does the whole job
705 my $restoreto = shift;
706 my $snaptable = shift;
710 # don't do anything until connected
711 if (!defined($::client_id)) {
712 print "You must first ``connect''.\n";
716 if ( ! $snaptable || ! defined $restoreto ) {
717 print "Usage: snaprestore \"restore to slot\" \"snaptable\" \"tableno\"\n";
721 if ( ! -f $snaptable ) {
722 print "Table $snaptable doesn't exist\n";
726 my $table = ReadSnapShotTable($snaptable);
727 $restoretime = FindSnapInTable($table, $restoreto);
728 if ( ! defined $table->{0} || ! defined $restoretime ) {
729 PrintSnapShotTable($table);
730 print "No current or $restoreto slot in this table\n";
734 my $currentindex = $table->{0};
735 if ( $table->{$restoretime} == $currentindex ) {
736 print "You should not restore to the current snapshot\n";
740 # swap the entries for 0 and $restoreto
741 my $tmp = $table->{$restoretime};
742 $table->{$restoretime} = $table->{0};
744 # PrintSnapShotTable($table);
747 WriteSnapShotTable($snaptable, $table);
749 # set it in the kernel
750 SnapSetTable($tableno, $snaptable);
752 # ready for the ioctl
754 my $type = "obdsnap";
755 $data = pack("i", $currentindex); # slot of previous current snapshot
758 my $len = length($type);
759 my $cl = length($data);
760 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
761 if (! defined $::dev_obd) {
762 print "No current device.\n";
766 my $rc = ioctl($::dev_obd, &OBD_SNAP_RESTORE, $packed);
769 print STDERR "ioctl failed: $!\n";
771 } elsif ($rc eq "0 but true") {
772 print "Snaprestore finished (success)\n";
773 delete $table->{$restoretime} if defined $restoretime;
775 WriteSnapShotTable($snaptable, $table);
777 # set it in the kernel
778 SnapSetTable($tableno, $snaptable);
779 # PrintSnapShotTable($table);
782 print "ioctl returned error code $rc.\n";
787 sub FindSnapInTable {
791 foreach my $restoretime ( keys %{$table} ) {
792 if ( $table->{$restoretime} == $snapno) {
793 print "Found key $restoretime for snapno $snapno\n";
803 my $type = "obdsnap";
804 my $snaptableno = shift;
806 $data = pack("i", $snaptableno);
809 my $len = length($type);
810 my $cl = length($data);
811 print "type $type (len $len), datalen $datalen ($cl)\n";
812 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
813 if (! defined $::dev_obd) {
814 print "No current device.\n";
818 my $rc = ioctl($::dev_obd, &OBD_SNAP_PRINTTABLE, $packed);
821 print STDERR "ioctl failed: $!\n";
823 } elsif ($rc eq "0 but true") {
824 print "Finished (success)\n";
827 print "ioctl returned error code $rc.\n";
834 my $type = "obdsnap";
835 my $snaptableno = shift;
843 print "No such file $file\n";
847 $table = ReadSnapShotTable($file);
849 $snapcount = keys %{$table};
850 print "Snapcount $snapcount\n";
852 if ( ! defined $table->{0} ) {
853 print "No current snapshot in table! First make one\n";
856 $data = pack("ii", $snaptableno, $snapcount);
858 foreach my $time (sort keys %{$table}) {
859 # XXX we should change to pack LL instead of I for times
860 $data .= pack("Ii", $time, $table->{$time});
864 my $len = length($type);
865 my $cl = length($data);
866 print "type $type (len $len), datalen $datalen ($cl)\n";
867 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
868 if (! defined $::dev_obd) {
869 print "No current device.\n";
873 my $rc = ioctl($::dev_obd, &OBD_SNAP_SETTABLE, $packed);
876 print STDERR "ioctl failed: $!\n";
878 } elsif ($rc eq "0 but true") {
879 print "Finished (success)\n";
882 print "ioctl returned error code $rc.\n";
890 my $file = &readl("enter file name: ");
894 my $table = ReadSnapShotTable($file);
897 PrintSnapShotTable($table);
898 my $action = &readl("Add, Delete or Quit [adq]: ");
899 goto done if ($action =~ "^q.*" );
900 goto add if ($action =~ "^a.*");
901 goto del if ($action =~ "^d.*");
905 my $idx = &readl("enter index where you want this snapshot: ");
906 my $time = &readl("enter time or 'now' or 'current': ");
907 my $oldtime = SnapFindTimeFromIdx($idx, $table);
908 if (defined $oldtime) {
909 print "This already exists, first clean up\n";
913 if ( $time eq 'now' ) {
915 } elsif ( $time eq 'current' ) {
918 $table->{$time} = $idx;
922 $didx = &readl("Enter index to delete: ");
923 my $deltime = SnapFindTimeFromIdx($didx, $table);
924 delete $table->{$deltime} if defined $deltime;
928 my $ok = &readl("OK with new table? [Yn]: ");
929 unless ( $ok eq "n" ) {
930 WriteSnapShotTable($file, $table);
935 sub SnapFindTimeFromIdx {
939 foreach my $time ( keys %{$table} ) {
940 if ( $table->{$time} == $idx ) {
947 sub PrintSnapShotTable {
951 foreach $time ( sort keys %{$table} ) {
952 my $stime = localtime($time);
956 printf "Time: %s -- Index %d\n", $stime, $table->{$time};
960 sub ReadSnapShotTable {
967 my ($time, $index) = split ;
968 $table->{$time} = $index;
972 PrintSnapShotTable($table);
977 sub WriteSnapShotTable {
982 foreach my $time ( sort keys %{$table} ) {
983 print FH "$time $table->{$time}\n";
993 # Note: _copy IOCTL takes parameters as dst, src.
994 # Copy function takes parameters as src, dst.
995 $src_obdo->{id} = shift;
996 $dst_obdo->{id} = shift;
997 $src_obdo->{valid} = &OBD_MD_FLALL;
999 # XXX need to fix copy so we can have 2 client IDs here
1000 my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo);
1001 if (! defined $::dev_obd) {
1002 print "No current device.\n";
1006 my $rc = ioctl($::dev_obd, &OBD_IOC_COPY, $packed);
1009 print STDERR "ioctl failed: $!\n";
1011 } elsif ($rc eq "0 but true") {
1012 print "Finished (success)\n";
1015 print "ioctl returned error code $rc.\n";
1023 # Note: _migr IOCTL takes parameters as dst, src.
1024 # Migrate function takes parameters as src, dst.
1025 $src_obdo->{id} = shift;
1026 $dst_obdo->{id} = shift;
1027 $src_obdo->{valid} = &OBD_MD_FLALL;
1029 # We pack a dummy connection ID here
1030 my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo);
1031 if (! defined $::dev_obd) {
1032 print "No current device.\n";
1036 my $rc = ioctl($::dev_obd, &OBD_IOC_MIGR, $packed);
1039 print STDERR "ioctl failed: $!\n";
1041 } elsif ($rc eq "0 but true") {
1042 print "Finished (success)\n";
1045 print "ioctl returned error code $rc.\n";
1054 my $data = pack("i", $size);
1057 my $packed = pack("ip", $datalen, $data);
1058 if (! defined $::dev_obd) {
1059 print "No current device.\n";
1062 my $rc = ioctl($::dev_obd, &OBD_IOC_FORMATOBD, $packed);
1065 print STDERR "ioctl failed: $!\n";
1067 } elsif ($rc eq "0 but true") {
1068 print "Finished (success)\n";
1071 print "ioctl returned error code $rc.\n";
1080 my $data = pack("ii", $partno, $size);
1081 my $datalen = 2 * 4;
1083 my $packed = pack("ip", $datalen, $data);
1084 if (! defined $::dev_obd) {
1085 print "No current device.\n";
1088 my $rc = ioctl($::dev_obd, &OBD_IOC_PARTITION, $packed);
1091 print STDERR "ioctl failed: $!\n";
1093 } elsif ($rc eq "0 but true") {
1094 print "Finished (success)\n";
1097 print "ioctl returned error code $rc.\n";
1108 # XXX we need a getinfo ioctl to validate parameters
1111 if ($arg && !defined($::st = stat($arg))) {
1112 print "$arg is not a valid device\n";
1116 printf "setting up %s, device %x\n", $arg, $::st->rdev();
1119 $datalen = length($arg)+1; # need null character also
1122 my $packed = pack("iip", $datalen, $::st->rdev(), $data);
1123 if (! defined $::dev_obd) {
1124 print "No current device.\n";
1127 my $rc = ioctl($::dev_obd, &OBD_IOC_SETUP, $packed);
1130 print STDERR "ioctl failed: $!\n";
1132 } elsif ($rc eq "0 but true") {
1133 print "Finished (success)\n";
1136 print "ioctl returned error code $rc.\n";
1143 if (! defined $::dev_obd) {
1144 print "No current device.\n";
1147 my $rc = ioctl($::dev_obd, &OBD_IOC_CLEANUP, $err);
1150 print STDERR "ioctl failed: $!\n";
1152 } elsif ($rc eq "0 but true") {
1153 print "Finished (success)\n";
1157 print "ioctl returned error code $rc.\n";
1167 if (! defined $::dev_obd) {
1168 print "No current device.\n";
1171 $rc = ioctl($::dev_obd, &OBD_IOC_CONNECT, $packed);
1172 $id = unpack("I", $packed);
1175 print STDERR "ioctl failed: $!\n";
1177 } elsif ($rc eq "0 but true") {
1179 print "Client ID : $id\n";
1180 print "Finished (success)\n";
1183 print "ioctl returned error code $rc.\n";
1191 if (!defined($id)) {
1195 if (!defined($id)) {
1196 print "syntax: disconnect [client ID]\n";
1197 print "When client ID is not given, the last valid client ID to be returned by a\n";
1198 print "connect command this session is used; there is no such ID.\n";
1202 my $packed = pack("L", $id);
1203 if (! defined $::dev_obd) {
1204 print "No current device.\n";
1207 my $rc = ioctl($::dev_obd, &OBD_IOC_DISCONNECT, $packed);
1210 print STDERR "ioctl failed: $!\n";
1212 } elsif ($rc eq "0 but true") {
1213 $::client_id = undef;
1214 print "Finished (success)\n";
1217 print "ioctl returned error code $rc.\n";
1223 if (!defined($::client_id)) {
1224 print "You must first ``connect''.\n";
1234 if (!defined($num)) {
1238 if (!defined($mode)) {
1239 $mode = 0100644; # create a file (rw-r--r--) if not specified
1242 if (scalar($num) < 1 || defined($quiet) && $quiet ne "quiet") {
1243 print "usage: create [<number of objects> [<mode> [quiet]]]\n";
1248 my $id = 0; # can't currently request IDs
1250 print "Creating " . scalar($num) . " object";
1251 if (scalar($num) > 1) {
1256 for ($i = 0; $i < scalar($num); $i++) {
1259 $obdo->{mode} = scalar($mode);
1260 $obdo->{valid} = &OBD_MD_FLMODE;
1262 my $packed = pack("I", $::client_id) . obdo_pack($obdo);
1263 if (! defined $::dev_obd) {
1264 print "No current device.\n";
1267 $rc = ioctl($::dev_obd, &OBD_IOC_CREATE, $packed);
1268 if ($rc ne "0 but true") {
1270 } elsif (!defined($quiet)) {
1271 $obdo = obdo_unpack($packed, 4);
1272 print "Created object #$obdo->{id}.\n";
1277 print STDERR "ioctl failed: $!\n";
1279 } elsif ($rc eq "0 but true") {
1280 print "Finished (success)\n";
1283 print "ioctl returned error code $rc.\n";
1290 if (! defined $::dev_obd) {
1291 print "No current device.\n";
1294 my $rc = ioctl($::dev_obd, &OBD_IOC_SYNC, $err);
1297 print STDERR "ioctl failed: $!\n";
1299 } elsif ($rc eq "0 but true") {
1300 print "Finished (success)\n";
1303 print "ioctl returned error code $rc.\n";
1309 if (!defined($::client_id)) {
1310 print "You must first ``connect''.\n";
1316 if (!defined($id) || scalar($id) < 1) {
1317 print "usage: destroy <object number>\n";
1321 print "Destroying object $id...\n";
1322 my $packed = pack("IL", $::client_id, $id);
1323 if (! defined $::dev_obd) {
1324 print "No current device.\n";
1327 my $rc = ioctl($::dev_obd, &OBD_IOC_DESTROY, $packed);
1330 print STDERR "ioctl failed: $!\n";
1332 } elsif ($rc eq "0 but true") {
1333 print "Finished (success)\n";
1336 print "ioctl returned error code $rc.\n";
1342 if (!defined($::client_id)) {
1343 print "You must first ``connect''.\n";
1349 if (!defined($id) || scalar($id) < 1) {
1350 print "invalid arguments; type \"help getattr\" for a synopsis\n";
1357 $obdo->{valid} = &OBD_MD_FLALL;
1358 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1359 if (! defined $::dev_obd) {
1360 print "No current device.\n";
1363 my $rc = ioctl($::dev_obd, &OBD_IOC_GETATTR, $packed);
1366 print STDERR "ioctl failed: $!\n";
1368 } elsif ($rc eq "0 but true") {
1369 $obdo = obdo_unpack($packed, 4);
1373 print "ioctl returned error code $rc.\n";
1379 if (!defined($::client_id)) {
1380 print "You must first ``connect''.\n";
1386 if (!defined($id) || scalar($id) < 1) {
1387 print "invalid arguments; type \"help setattr\" for a synopsis\n";
1391 # XXX we do not currently set all of the fields in the obdo
1394 $obdo->{mode} = oct(shift);
1395 $obdo->{uid} = shift;
1396 $obdo->{gid} = shift;
1397 $obdo->{size} = shift;
1398 $obdo->{atime} = shift;
1399 $obdo->{mtime} = shift;
1400 $obdo->{ctime} = shift;
1403 if (defined($obdo->{atime})) {
1404 $obdo->{valid} |= &OBD_MD_FLATIME;
1406 if (defined($obdo->{mtime})) {
1407 $obdo->{valid} |= &OBD_MD_FLMTIME;
1409 if (defined($obdo->{ctime})) {
1410 $obdo->{valid} |= &OBD_MD_FLCTIME;
1412 if (defined($obdo->{size})) {
1413 $obdo->{valid} |= &OBD_MD_FLSIZE;
1415 if (defined($obdo->{mode})) {
1416 $obdo->{valid} |= &OBD_MD_FLMODE;
1418 if (defined($obdo->{uid})) {
1419 $obdo->{valid} |= &OBD_MD_FLUID;
1421 if (defined($obdo->{gid})) {
1422 $obdo->{valid} |= &OBD_MD_FLGID;
1425 printf "valid is %x, mode is %o\n", $obdo->{valid}, $obdo->{mode};
1426 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1427 if (! defined $::dev_obd) {
1428 print "No current device.\n";
1431 my $rc = ioctl($::dev_obd, &OBD_IOC_SETATTR, $packed);
1434 print STDERR "ioctl failed: $!\n";
1436 } elsif ($rc eq "0 but true") {
1437 print "Finished (success)\n";
1440 print "ioctl returned error code $rc.\n";
1446 if (!defined($::client_id)) {
1447 print "You must first ``connect''.\n";
1455 if (!defined($id) || scalar($id) < 1 || !defined($count) ||
1456 $count < 1 || (defined($offset) && $offset < 0)) {
1457 print "invalid arguments; type \"help read\" for a synopsis\n";
1461 if (!defined($offset)) {
1465 print("Reading $count bytes starting at byte $offset from object " .
1468 # "allocate" a large enough buffer
1469 my $buf = sprintf("%${count}s", " ");
1470 die "suck" if (length($buf) != $count);
1475 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1476 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1477 pack("p LL LL", $buf, $count, $offset);
1479 if (! defined $::dev_obd) {
1480 print "No current device.\n";
1483 my $rc = ioctl($::dev_obd, &OBD_IOC_READ, $packed);
1485 $retval = unpack("l", $packed);
1488 print STDERR "ioctl failed: $!\n";
1490 } elsif ($rc eq "0 but true") {
1492 print substr($buf, 0, $retval);
1493 print "\nRead $retval of an attempted $count bytes.\n";
1494 print "Finished (success)\n";
1497 print "Finished (error $retval)\n";
1501 print "ioctl returned error code $rc.\n";
1507 if (!defined($::client_id)) {
1508 print "You must first ``connect''.\n";
1516 if (!defined($id) || scalar($id) < 1 || !defined($count) ||
1517 $count < 1 || (defined($offset) && $offset < 0)) {
1518 print "invalid arguments; type \"help read\" for a synopsis\n";
1522 if (!defined($offset)) {
1526 print("Reading $count bytes starting at byte $offset from object " .
1529 # "allocate" a large enough buffer
1530 my $buf = sprintf("%${count}s", " ");
1531 die "suck" if (length($buf) != $count);
1536 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1537 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1538 pack("p LL LL", $buf, $count, $offset);
1540 if (! defined $::dev_obd) {
1541 print "No current device.\n";
1544 my $rc = ioctl($::dev_obd, &OBD_IOC_READ2, $packed);
1546 $retval = unpack("l", $packed);
1549 print STDERR "ioctl failed: $!\n";
1551 } elsif ($rc eq "0 but true") {
1553 print substr($buf, 0, $retval);
1554 print "\nRead $retval of an attempted $count bytes.\n";
1555 print "Finished (success)\n";
1558 print "Finished (error $retval)\n";
1562 print "ioctl returned error code $rc.\n";
1568 if (!defined($::client_id)) {
1569 print "You must first ``connect''.\n";
1575 my $text = join(' ', @_);
1576 my $count = length($text);
1578 if (!defined($id) || scalar($id) < 1 || !defined($offset) ||
1579 scalar($offset) < 0) {
1580 print "invalid arguments; type \"help write\" for a synopsis\n";
1584 if (!defined($text)) {
1589 print("Writing $count bytes starting at byte $offset to object $id...\n");
1594 # the perl we're using doesn't support pack type Q
1595 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1596 pack("p LL LL", $text, $count, $offset);
1598 if (! defined $::dev_obd) {
1599 print "No current device.\n";
1602 my $rc = ioctl($::dev_obd, &OBD_IOC_WRITE, $packed);
1604 $retval = unpack("l", $packed);
1607 print STDERR "ioctl failed: $!\n";
1609 } elsif ($rc eq "0 but true") {
1611 print "\nWrote $retval of an attempted $count bytes.\n";
1612 print "Finished (success)\n";
1615 print "Finished (error $retval)\n";
1619 print "ioctl returned error code $rc.\n";
1625 if (!defined($::client_id)) {
1626 print "You must first ``connect''.\n";
1634 if (!defined($id) || scalar($id) < 1 || !defined($start) ||
1635 scalar($start) < 0 || !defined($count) || scalar($count) < 0) {
1636 print "invalid arguments; type \"help punch\" for a synopsis\n";
1640 print("Punching $count bytes starting at byte $start from object $id...\n");
1645 # the perl we're using doesn't support pack type Q
1646 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1647 pack("p LL LL", $buf, $start, $count);
1649 if (! defined $::dev_obd) {
1650 print "No current device.\n";
1653 my $rc = ioctl($::dev_obd, &OBD_IOC_PUNCH, $packed);
1655 $retval = unpack("l", $packed);
1658 print STDERR "ioctl failed: $!\n";
1659 } elsif ($rc eq "0 but true") {
1661 print "\nPunched $retval of an attempted $count bytes.\n";
1662 print "Finished (success)\n";
1665 print "Finished (error $retval)\n";
1669 print "ioctl returned error code $rc.\n";
1677 if (!defined($::client_id)) {
1678 print "You must first ``connect''.\n";
1682 if (!defined($num) || scalar($num) < 1 || scalar($num) > 32) {
1686 print "Preallocating $num objects...\n";
1687 # client id, alloc, id[32]
1688 my $packed = pack("LLx128", $::client_id, $num);
1690 if (! defined $::dev_obd) {
1691 print "No current device.\n";
1694 my $rc = ioctl($::dev_obd, &OBD_IOC_PREALLOCATE, $packed);
1697 print STDERR "ioctl failed: $!\n";
1699 } elsif ($rc eq "0 but true") {
1700 my $alloc = unpack("x4L", $packed);
1701 my @ids = unpack("x8L32", $packed);
1704 print "Got $alloc objects: ";
1708 print "\nFinished (success)\n";
1711 print "ioctl returned error code $rc.\n";
1717 if (! defined $::dev_obd) {
1718 print "No current device.\n";
1721 my $rc = ioctl($::dev_obd, &OBD_IOC_DEC_USE_COUNT, 0);
1724 print STDERR "ioctl failed: $!\n";
1726 } elsif ($rc eq "0 but true") {
1727 print "Finished (success)\n";
1730 print "ioctl returned error code $rc.\n";
1736 if (!defined($::client_id)) {
1737 print "You must first ``connect''.\n";
1749 # __kernel_fsid_t f_fsid; (64 bits)
1754 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1757 if (! defined $::dev_obd) {
1758 print "No current device.\n";
1761 my $rc = ioctl($::dev_obd, &OBD_IOC_STATFS, $packed);
1764 print STDERR "ioctl failed: $!\n";
1766 } elsif ($rc eq "0 but true") {
1767 # skip both the conn_id and the fs_type in the buffer
1768 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
1769 unpack("x4x4LLLLLL", $packed);
1770 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
1771 "$bfree free ($bavail available).\n");
1772 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
1773 print "Finished (success)\n";
1776 print "ioctl returned error code $rc.\n";
1784 if ( !$cmd || !$commands{$cmd} ) {
1785 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
1787 print "Usage: " . $commands{$cmd}->{doc} . "\n";
1794 print "Disconnecting active session ($::client_id)...";
1795 Disconnect($::client_id);