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);
99 # NOTE long long are layed out in ia32 memory as follows:
100 # u = 0xaaaabbbbccccdddd has ccccdddd at &u and aaaabbbb 4 bytes on
101 # this may be different on other architectures
103 # we use 32-bit integers for all 64-bit quantities in this program
104 # #define OBD_INLINESZ 60
105 # #define OBD_OBDMDSZ 60
106 # /* Note: 64-bit types are 64-bit aligned in structure */
114 # obd_blocks o_blocks;
115 # obd_blksize o_blksize;
120 # obd_flag o_obdflags;
122 # obd_count o_generation;
123 # obd_flag o_valid; /* hot fields in this obdo */
126 # struct list_head o_list;
127 # struct obd_ops *o_op;
132 pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L",
151 0, 0, # struct list_head
176 $obdo->{obdmd}) = unpack "x${offset}Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 L L L L L L L L L a60 a60", $buf;
184 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",
207 GetOptions("f!" => \$file, "device=s" => \$::device, ) || die "Getoptions";
210 # get a console for the app
216 my @procsysobd_objects = ('debug', 'index', 'reset', 'trace', 'vars');
219 ('status' => {func => "Status", doc => "status: show obd device status"},
220 'procsys' => {func => "Procsys", doc => "procsys <file> <value> (set /proc/sys/obd configuration)"},
221 'shell' => {func => "Shell", doc => "shell <shell-command>: execute shell-commands"},
222 'script' => {func => "Script", doc => "script <filename>: read and execute commands from a file"},
223 'insmod' => {func => "Insmod", doc => "insmod <module>: insert kernel module"},
224 'rmmod' => {func => "Rmmod", doc => "rmmod <module>: insert kernel module"},
225 'lsmod' => {func => "Lsmod", doc => "lsmod <module>: list kernel modules"},
226 'device' => {func => "Device", doc => "device <dev>: open another OBD device"},
227 'close' => {func => "Close", doc => "close <dev>: close OBD device"},
228 'create' => {func => "Create", doc => "create [<num> [<mode> [quiet]]]: create new object(s) (files, unless mode is given)"},
229 'attach' => {func => "Attach", doc => "attach { obdext2 | obdsnap snapdev snapidx tableno | obdscsi adapter bus tid lun }: attach this minor device to the specified driver" },
230 'detach' => {func => "Detach", doc => "detach this minor device"},
231 'testext2iterator' => {func => "TestExt2Iterator", doc => "test ext2 iterator function"},
232 'snapset' => {func => "SnapSetTable", doc => "snapset <tableno> <file>: set the table (created with snaptable) as table #tableno" },
233 'snapprint' => {func => "SnapPrint", doc => "snapprint <tableno>: output the contents of table #tableno to the syslog"},
234 'snapdelete' => {func => "SnapDelete", doc => "snapdelete: delete connected snap obd objects from disk"},
235 'snaprestore' => {func => "SnapRestore", doc => "snaprestore : restore connected old snap objects to be current"},
236 'snaptable' => {func => "SnapShotTable", doc => "snaptable: build a snapshot table (interactive)"},
237 'copy' => {func => "Copy", doc => "copy <srcid> <tgtid>: copy objects"},
238 'migrate' => {func => "Migrate", doc => "migrate <srcid> <tgtid>: migrate data from one object to another"},
240 'partition' => {func => "Partition", doc => "partition <type> <adapter> <bus> <tid> <lun> <partition> <size>: create a partition"},
242 'format' => {func => "Format", doc => "format <type> <adapter> <bus> <tid> <lun> <size>: format a partition"},
243 'setup' => {func => "Setup", doc => "setup [type]: link this OBD device to the underlying device (default type obdext2)"},
244 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
245 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
246 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
247 'destroy' => {func => "Destroy", doc => "destroy <id>: destroys an object"},
248 'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
250 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
251 'read' => {func => "Read", doc => "read <id> <count> [offset]: read data from object"},
253 'fsread' => {func => "Read2", doc => "read <id> <count> [offset]: read data from object"},
254 'write' => {func => "Write", doc => "write <id> <offset> <text>: write data to object"},
255 'punch' => {func => "Punch", doc => "punch <id> <start> <count>: punch a hole in object"},
256 'setattr' => {func => "Setattr", doc => "setattr <id> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]: sets object attributes"},
257 'getattr' => {func => "Getattr", doc => "getattr <id>: displays object attributes"},
258 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num objects."},
259 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
260 'help' => {func => \&Help, doc => "help: this message"},
261 'quit' => {func => \&Quit, doc => "see \"exit\""},
262 'exit' => {func => \&Quit, doc => "see \"quit\""}
266 # setup completion function
268 my @jcm_cmd_list = keys %commands;
283 return $term->readline(@_);
292 my $rc = execute_line($_);
293 if ($rc != 0) { last; }
297 $term = new Term::ReadLine 'obdcontrol ';
298 $attribs = $term->Attribs;
299 $attribs->{attempted_completion_function} = \&completeme;
300 $term->ornaments('md,me,,'); # bold face prompt
302 # make sure stdout is not buffered
303 STDOUT->autoflush(1);
306 # Get on with the show
310 #------------------------------------------------------------------------------
312 my ($text, $line, $start, $end) = @_;
313 if (substr($line, 0, $start) =~ /^\s*$/) {
314 if ($] < 5.6) { # PErl version is less than 5.6.0
315 return (exists $commands{$text}) ? $text : 0;
316 #Above line doesn't perform command completion, but
317 #perl5.005 Term-ReadLine lacks support for completion matching
318 #and perl5.6.0 requires glibc2.2.2 that won't run under Redhat6.2......sigh.
321 $attribs->{completion_word} = \@jcm_cmd_list;
322 return $term->completion_matches($text,
323 $attribs->{'list_completion_function'});
331 my @completions = completeme($given, $given, 0, length($given));
332 if ($#completions == 0) {
333 $name = shift @completions;
339 # start making requests
342 $line = $term->readline("obdcontrol > ");
350 my @cmdline = split(' ', $line);
351 my $word = shift @cmdline;
353 return 0 unless ($word);
359 $cmd = find_command($word);
362 printf STDERR "$word: No such command, or not unique.\n";
367 return (&{$commands{$cmd}->{func}}(@cmdline));
372 # select the OBD device we talk to
376 if ( ! $device && ! $::device ) { # first time ever
377 $device = '/dev/obd0';
380 if (($device) && ($::device ne $device)) {
385 print "Disconnecting active session ($::client_id)...";
386 Disconnect($::client_id);
389 if ($opendevfds{$device}) {
390 $::dev_obd = $opendevfds{$device};
393 # Open the device, as we need an FD for the ioctl
394 if (!sysopen(NEW_OBD, $device, 0)) {
395 print "Cannot open $device. Did you insert the obdclass module ?\n";
398 print "Opened device $device\n";
399 $opendevfds{$device} = *NEW_OBD;
400 $::dev_obd = *NEW_OBD;
404 print "Current device is $::device\n";
412 if ( ! $device && ! $::device ) { # first time ever
413 print "Nothing to close\n";
421 if ($::device eq $device) {
423 print "Disconnecting active session ($::client_id)...";
424 Disconnect($::client_id);
428 $fd2close = $opendevfds{$device};
429 if ($fd2close) { # XXXX something wrong in this if statement
431 $opendevfds{$device} = undef;
432 print "Closed device $device\n";
435 print "Device $device was not open\n";
439 if ($::device eq $device) {
443 print "No current device. You just closed the current device ($device).\n";
448 my $cmdfilename = shift;
450 if ( ! $cmdfilename ) {
451 print "please specify a command file name\n";
454 if (! open(CMDF, $cmdfilename)) {
455 print "Cannot open $cmdfilename: $!\n";
463 $rc = execute_line($_);
465 print "Something went wrong .......command exit status: $rc\n";
474 my $user_shell=$ENV{'SHELL'};
475 print "% $user_shell -c '@_'\n";
477 print "please specify a shell command\n";
480 system("$user_shell -c '@_'");
485 my $oldfh = select(STDOUT);
488 system('cat /proc/lustre/obd/*/status');
498 my $set_sysobd = shift;
501 foreach $i (0 .. $#procsysobd_objects) {
502 my $sysobd = $procsysobd_objects[$i];
504 if (defined $set_sysobd) {
505 if ($sysobd ne $set_sysobd) { next; }
507 if (defined $value) { # set this one
508 system("echo \"$value\" > /proc/sys/obd/$sysobd");
510 system("echo \"/proc/sys/obd/$sysobd:\"; cat /proc/sys/obd/$sysobd");
514 system("echo \"/proc/sys/obd/$sysobd:\"; cat /proc/sys/obd/$sysobd");
522 system("insmod $module");
528 system("rmmod $module");
534 system("lsmod $module");
545 print "error: missing type\n";
547 print "usage: attach {obdext2 | obdsnap | obdscsi | obdtrace }\n";
551 if ($type eq "obdscsi" ) {
557 $data = pack("iiii", $adapter, $bus, $tid, $lun);
559 } elsif ($type eq "obdsnap" ) {
564 $data = pack("iii", $snapdev, $snapidx, $tableno);
566 } elsif ($type eq "obdext2") {
567 $data = pack("i", 4711); # bogus data
569 } elsif ($type eq "obdtrace") {
570 $data = pack("i", 4711); # bogus data
573 print "error: unknown attach type $type\n";
577 my $len = length($type);
578 my $cl = length($data);
580 print "type $type (len $len), datalen $datalen ($cl)\n";
581 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
583 if (! defined $::dev_obd) {
584 print "No current device.\n";
587 my $rc = ioctl($::dev_obd, &OBD_IOC_ATTACH, $packed);
590 print STDERR "ioctl failed: $!\n";
592 } elsif ($rc eq "0 but true") {
593 print "Finished (success)\n";
596 print "ioctl returned error code $rc.\n";
606 if (! defined $::dev_obd) {
607 print "No current device.\n";
611 my $rc = ioctl($::dev_obd, &OBD_IOC_DETACH, $data);
614 print STDERR "ioctl failed: $!\n";
616 } elsif ($rc eq "0 but true") {
617 print "Finished (success)\n";
620 print "ioctl returned error code $rc.\n";
626 sub TestExt2Iterator {
627 if (!defined($::client_id)) {
628 print "You must first ``connect''.\n";
633 my $type = "obdext2";
635 $data = pack("i", 4711); # bogus data
638 my $len = length($type);
639 my $cl = length($data);
640 print "type $type (len $len), datalen $datalen ($cl)\n";
641 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
642 if (! defined $::dev_obd) {
643 print "No current device.\n";
647 my $rc = ioctl($::dev_obd, &OBD_EXT2_RUNIT, $packed);
650 print STDERR "ioctl failed: $!\n";
652 } elsif ($rc eq "0 but true") {
653 print "Finished (success)\n";
656 print "ioctl returned error code $rc.\n";
663 if (!defined($::client_id)) {
664 print "You must first ``connect''.\n";
669 my $type = "obdsnap";
671 $data = pack("i", 4711); # bogus data
674 my $len = length($type);
675 my $cl = length($data);
676 print "type $type (len $len), datalen $datalen ($cl)\n";
677 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
679 # XXX We need to fix this up so that after the objects in this snapshot
680 # are deleted, the snapshot itself is also removed from the table.
682 if (! defined $::dev_obd) {
683 print "No current device.\n";
687 my $rc = ioctl($::dev_obd, &OBD_SNAP_DELETE, $packed);
690 print STDERR "ioctl failed: $!\n";
692 } elsif ($rc eq "0 but true") {
693 print "Finished (success)\n";
696 print "ioctl returned error code $rc.\n";
702 # this routine does the whole job
704 my $restoreto = shift;
705 my $snaptable = shift;
709 # don't do anything until connected
710 if (!defined($::client_id)) {
711 print "You must first ``connect''.\n";
715 if ( ! $snaptable || ! defined $restoreto ) {
716 print "Usage: snaprestore \"restore to slot\" \"snaptable\" \"tableno\"\n";
720 if ( ! -f $snaptable ) {
721 print "Table $snaptable doesn't exist\n";
725 my $table = ReadSnapShotTable($snaptable);
726 $restoretime = FindSnapInTable($table, $restoreto);
727 if ( ! defined $table->{0} || ! defined $restoretime ) {
728 PrintSnapShotTable($table);
729 print "No current or $restoreto slot in this table\n";
733 my $currentindex = $table->{0};
734 if ( $table->{$restoretime} == $currentindex ) {
735 print "You should not restore to the current snapshot\n";
739 # swap the entries for 0 and $restoreto
740 my $tmp = $table->{$restoretime};
741 $table->{$restoretime} = $table->{0};
743 # PrintSnapShotTable($table);
746 WriteSnapShotTable($snaptable, $table);
748 # set it in the kernel
749 SnapSetTable($tableno, $snaptable);
751 # ready for the ioctl
753 my $type = "obdsnap";
754 $data = pack("i", $currentindex); # slot of previous current snapshot
757 my $len = length($type);
758 my $cl = length($data);
759 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
760 if (! defined $::dev_obd) {
761 print "No current device.\n";
765 my $rc = ioctl($::dev_obd, &OBD_SNAP_RESTORE, $packed);
768 print STDERR "ioctl failed: $!\n";
770 } elsif ($rc eq "0 but true") {
771 print "Snaprestore finished (success)\n";
772 delete $table->{$restoretime} if defined $restoretime;
774 WriteSnapShotTable($snaptable, $table);
776 # set it in the kernel
777 SnapSetTable($tableno, $snaptable);
778 # PrintSnapShotTable($table);
781 print "ioctl returned error code $rc.\n";
786 sub FindSnapInTable {
790 foreach my $restoretime ( keys %{$table} ) {
791 if ( $table->{$restoretime} == $snapno) {
792 print "Found key $restoretime for snapno $snapno\n";
802 my $type = "obdsnap";
803 my $snaptableno = shift;
805 $data = pack("i", $snaptableno);
808 my $len = length($type);
809 my $cl = length($data);
810 print "type $type (len $len), datalen $datalen ($cl)\n";
811 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
812 if (! defined $::dev_obd) {
813 print "No current device.\n";
817 my $rc = ioctl($::dev_obd, &OBD_SNAP_PRINTTABLE, $packed);
820 print STDERR "ioctl failed: $!\n";
822 } elsif ($rc eq "0 but true") {
823 print "Finished (success)\n";
826 print "ioctl returned error code $rc.\n";
833 my $type = "obdsnap";
834 my $snaptableno = shift;
842 print "No such file $file\n";
846 $table = ReadSnapShotTable($file);
848 $snapcount = keys %{$table};
849 print "Snapcount $snapcount\n";
851 if ( ! defined $table->{0} ) {
852 print "No current snapshot in table! First make one\n";
855 $data = pack("ii", $snaptableno, $snapcount);
857 foreach my $time (sort keys %{$table}) {
858 # XXX we should change to pack LL instead of I for times
859 $data .= pack("Ii", $time, $table->{$time});
863 my $len = length($type);
864 my $cl = length($data);
865 print "type $type (len $len), datalen $datalen ($cl)\n";
866 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
867 if (! defined $::dev_obd) {
868 print "No current device.\n";
872 my $rc = ioctl($::dev_obd, &OBD_SNAP_SETTABLE, $packed);
875 print STDERR "ioctl failed: $!\n";
877 } elsif ($rc eq "0 but true") {
878 print "Finished (success)\n";
881 print "ioctl returned error code $rc.\n";
889 my $file = &readl("enter file name: ");
893 my $table = ReadSnapShotTable($file);
896 PrintSnapShotTable($table);
897 my $action = &readl("Add, Delete or Quit [adq]: ");
898 goto done if ($action =~ "^q.*" );
899 goto add if ($action =~ "^a.*");
900 goto del if ($action =~ "^d.*");
904 my $idx = &readl("enter index where you want this snapshot: ");
905 my $time = &readl("enter time or 'now' or 'current': ");
906 my $oldtime = SnapFindTimeFromIdx($idx, $table);
907 if (defined $oldtime) {
908 print "This already exists, first clean up\n";
912 if ( $time eq 'now' ) {
914 } elsif ( $time eq 'current' ) {
917 $table->{$time} = $idx;
921 $didx = &readl("Enter index to delete: ");
922 my $deltime = SnapFindTimeFromIdx($didx, $table);
923 delete $table->{$deltime} if defined $deltime;
927 my $ok = &readl("OK with new table? [Yn]: ");
928 unless ( $ok eq "n" ) {
929 WriteSnapShotTable($file, $table);
934 sub SnapFindTimeFromIdx {
938 foreach my $time ( keys %{$table} ) {
939 if ( $table->{$time} == $idx ) {
946 sub PrintSnapShotTable {
950 foreach $time ( sort keys %{$table} ) {
951 my $stime = localtime($time);
955 printf "Time: %s -- Index %d\n", $stime, $table->{$time};
959 sub ReadSnapShotTable {
966 my ($time, $index) = split ;
967 $table->{$time} = $index;
971 PrintSnapShotTable($table);
976 sub WriteSnapShotTable {
981 foreach my $time ( sort keys %{$table} ) {
982 print FH "$time $table->{$time}\n";
992 # Note: _copy IOCTL takes parameters as dst, src.
993 # Copy function takes parameters as src, dst.
994 $src_obdo->{id} = shift;
995 $dst_obdo->{id} = shift;
996 $src_obdo->{valid} = &OBD_MD_FLALL;
998 # XXX need to fix copy so we can have 2 client IDs here
999 my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo);
1000 if (! defined $::dev_obd) {
1001 print "No current device.\n";
1005 my $rc = ioctl($::dev_obd, &OBD_IOC_COPY, $packed);
1008 print STDERR "ioctl failed: $!\n";
1010 } elsif ($rc eq "0 but true") {
1011 print "Finished (success)\n";
1014 print "ioctl returned error code $rc.\n";
1022 # Note: _migr IOCTL takes parameters as dst, src.
1023 # Migrate function takes parameters as src, dst.
1024 $src_obdo->{id} = shift;
1025 $dst_obdo->{id} = shift;
1026 $src_obdo->{valid} = &OBD_MD_FLALL;
1028 # We pack a dummy connection ID here
1029 my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo);
1030 if (! defined $::dev_obd) {
1031 print "No current device.\n";
1035 my $rc = ioctl($::dev_obd, &OBD_IOC_MIGR, $packed);
1038 print STDERR "ioctl failed: $!\n";
1040 } elsif ($rc eq "0 but true") {
1041 print "Finished (success)\n";
1044 print "ioctl returned error code $rc.\n";
1053 my $data = pack("i", $size);
1056 my $packed = pack("ip", $datalen, $data);
1057 if (! defined $::dev_obd) {
1058 print "No current device.\n";
1061 my $rc = ioctl($::dev_obd, &OBD_IOC_FORMATOBD, $packed);
1064 print STDERR "ioctl failed: $!\n";
1066 } elsif ($rc eq "0 but true") {
1067 print "Finished (success)\n";
1070 print "ioctl returned error code $rc.\n";
1079 my $data = pack("ii", $partno, $size);
1080 my $datalen = 2 * 4;
1082 my $packed = pack("ip", $datalen, $data);
1083 if (! defined $::dev_obd) {
1084 print "No current device.\n";
1087 my $rc = ioctl($::dev_obd, &OBD_IOC_PARTITION, $packed);
1090 print STDERR "ioctl failed: $!\n";
1092 } elsif ($rc eq "0 but true") {
1093 print "Finished (success)\n";
1096 print "ioctl returned error code $rc.\n";
1107 # XXX we need a getinfo ioctl to validate parameters
1110 if ($arg && !defined($::st = stat($arg))) {
1111 print "$arg is not a valid device\n";
1115 printf "setting up %s, device %x\n", $arg, $::st->rdev();
1118 $datalen = length($arg)+1; # need null character also
1121 my $packed = pack("iip", $datalen, $::st->rdev(), $data);
1122 if (! defined $::dev_obd) {
1123 print "No current device.\n";
1126 my $rc = ioctl($::dev_obd, &OBD_IOC_SETUP, $packed);
1129 print STDERR "ioctl failed: $!\n";
1131 } elsif ($rc eq "0 but true") {
1132 print "Finished (success)\n";
1135 print "ioctl returned error code $rc.\n";
1142 if (! defined $::dev_obd) {
1143 print "No current device.\n";
1146 my $rc = ioctl($::dev_obd, &OBD_IOC_CLEANUP, $err);
1149 print STDERR "ioctl failed: $!\n";
1151 } elsif ($rc eq "0 but true") {
1152 print "Finished (success)\n";
1156 print "ioctl returned error code $rc.\n";
1166 if (! defined $::dev_obd) {
1167 print "No current device.\n";
1170 $rc = ioctl($::dev_obd, &OBD_IOC_CONNECT, $packed);
1171 $id = unpack("I", $packed);
1174 print STDERR "ioctl failed: $!\n";
1176 } elsif ($rc eq "0 but true") {
1178 print "Client ID : $id\n";
1179 print "Finished (success)\n";
1182 print "ioctl returned error code $rc.\n";
1190 if (!defined($id)) {
1194 if (!defined($id)) {
1195 print "syntax: disconnect [client ID]\n";
1196 print "When client ID is not given, the last valid client ID to be returned by a\n";
1197 print "connect command this session is used; there is no such ID.\n";
1201 my $packed = pack("L", $id);
1202 if (! defined $::dev_obd) {
1203 print "No current device.\n";
1206 my $rc = ioctl($::dev_obd, &OBD_IOC_DISCONNECT, $packed);
1209 print STDERR "ioctl failed: $!\n";
1211 } elsif ($rc eq "0 but true") {
1212 $::client_id = undef;
1213 print "Finished (success)\n";
1216 print "ioctl returned error code $rc.\n";
1222 if (!defined($::client_id)) {
1223 print "You must first ``connect''.\n";
1233 if (!defined($num)) {
1237 if (!defined($mode)) {
1238 $mode = 0100644; # create a file (rw-r--r--) if not specified
1241 if (scalar($num) < 1 || defined($quiet) && $quiet ne "quiet") {
1242 print "usage: create [<number of objects> [<mode> [quiet]]]\n";
1247 my $id = 0; # can't currently request IDs
1249 print "Creating " . scalar($num) . " object";
1250 if (scalar($num) > 1) {
1255 for ($i = 0; $i < scalar($num); $i++) {
1258 $obdo->{mode} = scalar($mode);
1259 $obdo->{valid} = &OBD_MD_FLMODE;
1261 my $packed = pack("I", $::client_id) . obdo_pack($obdo);
1262 if (! defined $::dev_obd) {
1263 print "No current device.\n";
1266 $rc = ioctl($::dev_obd, &OBD_IOC_CREATE, $packed);
1267 if ($rc ne "0 but true") {
1269 } elsif (!defined($quiet)) {
1270 $obdo = obdo_unpack($packed, 4);
1271 print "Created object #$obdo->{id}.\n";
1276 print STDERR "ioctl failed: $!\n";
1278 } elsif ($rc eq "0 but true") {
1279 print "Finished (success)\n";
1282 print "ioctl returned error code $rc.\n";
1289 if (! defined $::dev_obd) {
1290 print "No current device.\n";
1293 my $rc = ioctl($::dev_obd, &OBD_IOC_SYNC, $err);
1296 print STDERR "ioctl failed: $!\n";
1298 } elsif ($rc eq "0 but true") {
1299 print "Finished (success)\n";
1302 print "ioctl returned error code $rc.\n";
1308 if (!defined($::client_id)) {
1309 print "You must first ``connect''.\n";
1315 if (!defined($id) || scalar($id) < 1) {
1316 print "usage: destroy <object number>\n";
1320 print "Destroying object $id...\n";
1321 my $packed = pack("IL", $::client_id, $id);
1322 if (! defined $::dev_obd) {
1323 print "No current device.\n";
1326 my $rc = ioctl($::dev_obd, &OBD_IOC_DESTROY, $packed);
1329 print STDERR "ioctl failed: $!\n";
1331 } elsif ($rc eq "0 but true") {
1332 print "Finished (success)\n";
1335 print "ioctl returned error code $rc.\n";
1341 if (!defined($::client_id)) {
1342 print "You must first ``connect''.\n";
1348 if (!defined($id) || scalar($id) < 1) {
1349 print "invalid arguments; type \"help getattr\" for a synopsis\n";
1356 $obdo->{valid} = &OBD_MD_FLALL;
1357 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1358 if (! defined $::dev_obd) {
1359 print "No current device.\n";
1362 my $rc = ioctl($::dev_obd, &OBD_IOC_GETATTR, $packed);
1365 print STDERR "ioctl failed: $!\n";
1367 } elsif ($rc eq "0 but true") {
1368 $obdo = obdo_unpack($packed, 4);
1372 print "ioctl returned error code $rc.\n";
1378 if (!defined($::client_id)) {
1379 print "You must first ``connect''.\n";
1385 if (!defined($id) || scalar($id) < 1) {
1386 print "invalid arguments; type \"help setattr\" for a synopsis\n";
1390 # XXX we do not currently set all of the fields in the obdo
1393 $obdo->{mode} = oct(shift);
1394 $obdo->{uid} = shift;
1395 $obdo->{gid} = shift;
1396 $obdo->{size} = shift;
1397 $obdo->{atime} = shift;
1398 $obdo->{mtime} = shift;
1399 $obdo->{ctime} = shift;
1402 if (defined($obdo->{atime})) {
1403 $obdo->{valid} |= &OBD_MD_FLATIME;
1405 if (defined($obdo->{mtime})) {
1406 $obdo->{valid} |= &OBD_MD_FLMTIME;
1408 if (defined($obdo->{ctime})) {
1409 $obdo->{valid} |= &OBD_MD_FLCTIME;
1411 if (defined($obdo->{size})) {
1412 $obdo->{valid} |= &OBD_MD_FLSIZE;
1414 if (defined($obdo->{mode})) {
1415 $obdo->{valid} |= &OBD_MD_FLMODE;
1417 if (defined($obdo->{uid})) {
1418 $obdo->{valid} |= &OBD_MD_FLUID;
1420 if (defined($obdo->{gid})) {
1421 $obdo->{valid} |= &OBD_MD_FLGID;
1424 printf "valid is %x, mode is %o\n", $obdo->{valid}, $obdo->{mode};
1425 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1426 if (! defined $::dev_obd) {
1427 print "No current device.\n";
1430 my $rc = ioctl($::dev_obd, &OBD_IOC_SETATTR, $packed);
1433 print STDERR "ioctl failed: $!\n";
1435 } elsif ($rc eq "0 but true") {
1436 print "Finished (success)\n";
1439 print "ioctl returned error code $rc.\n";
1445 if (!defined($::client_id)) {
1446 print "You must first ``connect''.\n";
1454 if (!defined($id) || scalar($id) < 1 || !defined($count) ||
1455 $count < 1 || (defined($offset) && $offset < 0)) {
1456 print "invalid arguments; type \"help read\" for a synopsis\n";
1460 if (!defined($offset)) {
1464 print("Reading $count bytes starting at byte $offset from object " .
1467 # "allocate" a large enough buffer
1468 my $buf = sprintf("%${count}s", " ");
1469 die "suck" if (length($buf) != $count);
1474 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1475 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1476 pack("p LL LL", $buf, $count, $offset);
1478 if (! defined $::dev_obd) {
1479 print "No current device.\n";
1482 my $rc = ioctl($::dev_obd, &OBD_IOC_READ, $packed);
1484 $retval = unpack("l", $packed);
1487 print STDERR "ioctl failed: $!\n";
1489 } elsif ($rc eq "0 but true") {
1491 print substr($buf, 0, $retval);
1492 print "\nRead $retval of an attempted $count bytes.\n";
1493 print "Finished (success)\n";
1496 print "Finished (error $retval)\n";
1500 print "ioctl returned error code $rc.\n";
1506 if (!defined($::client_id)) {
1507 print "You must first ``connect''.\n";
1515 if (!defined($id) || scalar($id) < 1 || !defined($count) ||
1516 $count < 1 || (defined($offset) && $offset < 0)) {
1517 print "invalid arguments; type \"help read\" for a synopsis\n";
1521 if (!defined($offset)) {
1525 print("Reading $count bytes starting at byte $offset from object " .
1528 # "allocate" a large enough buffer
1529 my $buf = sprintf("%${count}s", " ");
1530 die "suck" if (length($buf) != $count);
1535 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1536 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1537 pack("p LL LL", $buf, $count, $offset);
1539 if (! defined $::dev_obd) {
1540 print "No current device.\n";
1543 my $rc = ioctl($::dev_obd, &OBD_IOC_READ2, $packed);
1545 $retval = unpack("l", $packed);
1548 print STDERR "ioctl failed: $!\n";
1550 } elsif ($rc eq "0 but true") {
1552 print substr($buf, 0, $retval);
1553 print "\nRead $retval of an attempted $count bytes.\n";
1554 print "Finished (success)\n";
1557 print "Finished (error $retval)\n";
1561 print "ioctl returned error code $rc.\n";
1567 if (!defined($::client_id)) {
1568 print "You must first ``connect''.\n";
1574 my $text = join(' ', @_);
1575 my $count = length($text);
1577 if (!defined($id) || scalar($id) < 1 || !defined($offset) ||
1578 scalar($offset) < 0) {
1579 print "invalid arguments; type \"help write\" for a synopsis\n";
1583 if (!defined($text)) {
1588 print("Writing $count bytes starting at byte $offset to object $id...\n");
1593 # the perl we're using doesn't support pack type Q
1594 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1595 pack("p LL LL", $text, $count, $offset);
1597 if (! defined $::dev_obd) {
1598 print "No current device.\n";
1601 my $rc = ioctl($::dev_obd, &OBD_IOC_WRITE, $packed);
1603 $retval = unpack("l", $packed);
1606 print STDERR "ioctl failed: $!\n";
1608 } elsif ($rc eq "0 but true") {
1610 print "\nWrote $retval of an attempted $count bytes.\n";
1611 print "Finished (success)\n";
1614 print "Finished (error $retval)\n";
1618 print "ioctl returned error code $rc.\n";
1624 if (!defined($::client_id)) {
1625 print "You must first ``connect''.\n";
1633 if (!defined($id) || scalar($id) < 1 || !defined($start) ||
1634 scalar($start) < 0 || !defined($count) || scalar($count) < 0) {
1635 print "invalid arguments; type \"help punch\" for a synopsis\n";
1639 print("Punching $count bytes starting at byte $start from object $id...\n");
1644 # the perl we're using doesn't support pack type Q
1645 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1646 pack("p LL LL", $buf, $start, $count);
1648 if (! defined $::dev_obd) {
1649 print "No current device.\n";
1652 my $rc = ioctl($::dev_obd, &OBD_IOC_PUNCH, $packed);
1654 $retval = unpack("l", $packed);
1657 print STDERR "ioctl failed: $!\n";
1658 } elsif ($rc eq "0 but true") {
1660 print "\nPunched $retval of an attempted $count bytes.\n";
1661 print "Finished (success)\n";
1664 print "Finished (error $retval)\n";
1668 print "ioctl returned error code $rc.\n";
1676 if (!defined($::client_id)) {
1677 print "You must first ``connect''.\n";
1681 if (!defined($num) || scalar($num) < 1 || scalar($num) > 32) {
1685 print "Preallocating $num objects...\n";
1686 # client id, alloc, id[32]
1687 my $packed = pack("LLx128", $::client_id, $num);
1689 if (! defined $::dev_obd) {
1690 print "No current device.\n";
1693 my $rc = ioctl($::dev_obd, &OBD_IOC_PREALLOCATE, $packed);
1696 print STDERR "ioctl failed: $!\n";
1698 } elsif ($rc eq "0 but true") {
1699 my $alloc = unpack("x4L", $packed);
1700 my @ids = unpack("x8L32", $packed);
1703 print "Got $alloc objects: ";
1707 print "\nFinished (success)\n";
1710 print "ioctl returned error code $rc.\n";
1716 if (! defined $::dev_obd) {
1717 print "No current device.\n";
1720 my $rc = ioctl($::dev_obd, &OBD_IOC_DEC_USE_COUNT, 0);
1723 print STDERR "ioctl failed: $!\n";
1725 } elsif ($rc eq "0 but true") {
1726 print "Finished (success)\n";
1729 print "ioctl returned error code $rc.\n";
1735 if (!defined($::client_id)) {
1736 print "You must first ``connect''.\n";
1748 # __kernel_fsid_t f_fsid; (64 bits)
1753 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1756 if (! defined $::dev_obd) {
1757 print "No current device.\n";
1760 my $rc = ioctl($::dev_obd, &OBD_IOC_STATFS, $packed);
1763 print STDERR "ioctl failed: $!\n";
1765 } elsif ($rc eq "0 but true") {
1766 # skip both the conn_id and the fs_type in the buffer
1767 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
1768 unpack("x4x4LLLLLL", $packed);
1769 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
1770 "$bfree free ($bavail available).\n");
1771 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
1772 print "Finished (success)\n";
1775 print "ioctl returned error code $rc.\n";
1783 if ( !$cmd || !$commands{$cmd} ) {
1784 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
1786 print "Usage: " . $commands{$cmd}->{doc} . "\n";
1793 print "Disconnecting active session ($::client_id)...";
1794 Disconnect($::client_id);