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);
32 eval 'sub OBD_IOC_DEC_USE_COUNT () { &_IOC(0, ord(\'f\'), 8, 0);}' unless
33 defined(&OBD_IOC_DEC_USE_COUNT);
34 eval 'sub OBD_IOC_SETATTR () { &_IOC(1, ord(\'f\'), 9, 4);}' unless
35 defined(&OBD_IOC_SETATTR);
36 eval 'sub OBD_IOC_GETATTR () { &_IOC(2, ord(\'f\'), 10, 4);}' unless
37 defined(&OBD_IOC_GETATTR);
38 eval 'sub OBD_IOC_READ () { &_IOC(3, ord(\'f\'), 11, 4);}' unless
39 defined(&OBD_IOC_READ);
40 eval 'sub OBD_IOC_WRITE () { &_IOC(3, ord(\'f\'), 12, 4);}' unless
41 defined(&OBD_IOC_WRITE);
42 eval 'sub OBD_IOC_CONNECT () { &_IOC(2, ord(\'f\'), 13, 4);}' unless
43 defined(&OBD_IOC_CONNECT);
44 eval 'sub OBD_IOC_DISCONNECT () { &_IOC(1, ord(\'f\'), 14, 4);}' unless
45 defined(&OBD_IOC_DISCONNECT);
46 eval 'sub OBD_IOC_STATFS () { &_IOC(3, ord(\'f\'), 15, 4);}' unless
47 defined(&OBD_IOC_STATFS);
48 eval 'sub OBD_IOC_SYNC () { &_IOC(2, ord(\'f\'), 16, 4);}' unless
49 defined(&OBD_IOC_SYNC);
50 eval 'sub OBD_IOC_READ2 () { &_IOC(3, ord(\'f\'), 17, 4);}' unless
51 defined(&OBD_IOC_READ2);
52 eval 'sub OBD_IOC_FORMATOBD () { &_IOC(3, ord(\'f\'), 18, 4);}' unless
53 defined(&OBD_IOC_FORMATOBD);
54 eval 'sub OBD_IOC_PARTITION () { &_IOC(3, ord(\'f\'), 19, 4);}' unless
55 defined(&OBD_IOC_PARTITION);
56 eval 'sub OBD_IOC_ATTACH () { &_IOC(3, ord(\'f\'), 20, 4);}' unless
57 defined(&OBD_IOC_ATTACH);
58 eval 'sub OBD_IOC_DETACH () { &_IOC(3, ord(\'f\'), 21, 4);}' unless
59 defined(&OBD_IOC_DETACH);
60 eval 'sub OBD_IOC_COPY () { &_IOC(3, ord(\'f\'), 22, 4);}' unless
61 defined(&OBD_IOC_COPY);
62 eval 'sub OBD_IOC_MIGR () { &_IOC(3, ord(\'f\'), 23, 4);}' unless
63 defined(&OBD_IOC_MIGR);
64 eval 'sub OBD_IOC_PUNCH () { &_IOC(3, ord(\'f\'), 24, 4);}' unless
65 defined(&OBD_IOC_PUNCH);
66 eval 'sub OBD_SNAP_SETTABLE () { &_IOC(3, ord(\'f\'), 40, 4);}' unless
67 defined(&OBD_SNAP_SETTABLE);
68 eval 'sub OBD_SNAP_PRINTTABLE () { &_IOC(3, ord(\'f\'), 41, 4);}' unless
69 defined(&OBD_SNAP_PRINTTABLE);
70 eval 'sub OBD_SNAP_DELETE() { &_IOC(3, ord(\'f\'), 42, 4);}' unless
71 defined(&OBD_SNAP_DELETE);
72 eval 'sub OBD_SNAP_RESTORE() { &_IOC(3, ord(\'f\'), 43, 4);}' unless
73 defined(&OBD_SNAP_RESTORE);
75 eval 'sub OBD_EXT2_RUNIT () { &_IOC(3, ord(\'f\'), 61, 4);}' unless
76 defined(&OBD_EXT2_RUNIT);
78 eval 'sub OBD_MD_FLALL () {~0;}' unless defined(&OBD_MD_FLALL);
79 eval 'sub OBD_MD_FLATIME () {1<<1;}' unless defined(&OBD_MD_FLATIME);
80 eval 'sub OBD_MD_FLMTIME () {1<<2;}' unless defined(&OBD_MD_FLMTIME);
81 eval 'sub OBD_MD_FLCTIME () {1<<3;}' unless defined(&OBD_MD_FLCTIME);
82 eval 'sub OBD_MD_FLSIZE () {1<<4;}' unless defined(&OBD_MD_FLSIZE);
83 eval 'sub OBD_MD_FLMODE () {1<<7;}' unless defined(&OBD_MD_FLMODE);
84 eval 'sub OBD_MD_FLUID () {1<<8;}' unless defined(&OBD_MD_FLUID);
85 eval 'sub OBD_MD_FLGID () {1<<9;}' unless defined(&OBD_MD_FLGID);
95 # NOTE long long are layed out in ia32 memory as follows:
96 # u = 0xaaaabbbbccccdddd has ccccdddd at &u and aaaabbbb 4 bytes on
97 # this may be different on other architectures
99 # we use 32-bit integers for all 64-bit quantities in this program
100 # #define OBD_INLINESZ 60
101 # #define OBD_OBDMDSZ 60
102 # /* Note: 64-bit types are 64-bit aligned in structure */
110 # obd_blocks o_blocks;
111 # obd_blksize o_blksize;
116 # obd_flag o_obdflags;
118 # obd_count o_generation;
119 # obd_flag o_valid; /* hot fields in this obdo */
122 # struct list_head o_list;
123 # struct obd_ops *o_op;
128 pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L",
147 0, 0, # struct list_head
172 $obdo->{obdmd}) = unpack "x${offset}Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 L L L L L L L L L a60 a60", $buf;
180 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",
203 GetOptions("f!" => \$file, "device=s" => \$::device, ) || die "Getoptions";
206 # get a console for the app
212 my @procsysobd_objects = ('debug', 'index', 'reset', 'trace', 'vars');
215 ('status' => {func => "Status", doc => "status: show obd device status"},
216 'procsys' => {func => "Procsys", doc => "procsys <file> <value> (set /proc/sys/obd configuration)"},
217 'shell' => {func => "Shell", doc => "shell <shell-command>: execute shell-commands"},
218 'script' => {func => "Script", doc => "script <filename>: read and execute commands from a file"},
219 'insmod' => {func => "Insmod", doc => "insmod <module>: insert kernel module"},
220 'rmmod' => {func => "Rmmod", doc => "rmmod <module>: insert kernel module"},
221 'lsmod' => {func => "Lsmod", doc => "lsmod <module>: list kernel modules"},
222 'device' => {func => "Device", doc => "device <dev>: open another OBD device"},
223 'close' => {func => "Close", doc => "close <dev>: close OBD device"},
224 'create' => {func => "Create", doc => "create [<num> [<mode> [quiet]]]: create new object(s) (files, unless mode is given)"},
225 'attach' => {func => "Attach", doc => "attach { obdext2 | obdsnap snapdev snapidx tableno | obdscsi adapter bus tid lun }: attach this minor device to the specified driver" },
226 'detach' => {func => "Detach", doc => "detach this minor device"},
227 'testext2iterator' => {func => "TestExt2Iterator", doc => "test ext2 iterator function"},
228 'snapset' => {func => "SnapSetTable", doc => "snapset <tableno> <file>: set the table (created with snaptable) as table #tableno" },
229 'snapprint' => {func => "SnapPrint", doc => "snapprint <tableno>: output the contents of table #tableno to the syslog"},
230 'snapdelete' => {func => "SnapDelete", doc => "snapdelete: delete connected snap obd objects from disk"},
231 'snaprestore' => {func => "SnapRestore", doc => "snaprestore : restore connected old snap objects to be current"},
232 'snaptable' => {func => "SnapShotTable", doc => "snaptable: build a snapshot table (interactive)"},
233 'copy' => {func => "Copy", doc => "copy <srcid> <tgtid>: copy objects"},
234 'migrate' => {func => "Migrate", doc => "migrate <srcid> <tgtid>: migrate data from one object to another"},
235 'partition' => {func => "Partition", doc => "partition <type> <adapter> <bus> <tid> <lun> <partition> <size>: create a partition"},
236 'format' => {func => "Format", doc => "format <type> <adapter> <bus> <tid> <lun> <size>: format a partition"},
237 'setup' => {func => "Setup", doc => "setup [type]: link this OBD device to the underlying device (default type obdext2)"},
238 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
239 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
240 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
241 'destroy' => {func => "Destroy", doc => "destroy <id>: destroys an object"},
242 'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
243 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
244 'read' => {func => "Read", doc => "read <id> <count> [offset]: read data from object"},
245 'fsread' => {func => "Read2", doc => "read <id> <count> [offset]: read data from object"},
246 'write' => {func => "Write", doc => "write <id> <offset> <text>: write data to object"},
247 'punch' => {func => "Punch", doc => "punch <id> <start> <count>: punch a hole in object"},
248 'setattr' => {func => "Setattr", doc => "setattr <id> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]: sets object attributes"},
249 'getattr' => {func => "Getattr", doc => "getattr <id>: displays object attributes"},
250 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num objects."},
251 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
252 'help' => {func => \&Help, doc => "help: this message"},
253 'quit' => {func => \&Quit, doc => "see \"exit\""},
254 'exit' => {func => \&Quit, doc => "see \"quit\""}
258 # setup completion function
260 my @jcm_cmd_list = keys %commands;
275 return $term->readline(@_);
284 my $rc = execute_line($_);
285 if ($rc != 0) { last; }
289 $term = new Term::ReadLine 'obdcontrol ';
290 $attribs = $term->Attribs;
291 $attribs->{attempted_completion_function} = \&completeme;
292 $term->ornaments('md,me,,'); # bold face prompt
294 # make sure stdout is not buffered
295 STDOUT->autoflush(1);
298 # Get on with the show
302 #------------------------------------------------------------------------------
304 my ($text, $line, $start, $end) = @_;
305 if (substr($line, 0, $start) =~ /^\s*$/) {
306 if ($] < 5.6) { # PErl version is less than 5.6.0
307 return (exists $commands{$text}) ? $text : 0;
308 #Above line doesn't perform command completion, but
309 #perl5.005 Term-ReadLine lacks support for completion matching
310 #and perl5.6.0 requires glibc2.2.2 that won't run under Redhat6.2......sigh.
313 $attribs->{completion_word} = \@jcm_cmd_list;
314 return $term->completion_matches($text,
315 $attribs->{'list_completion_function'});
323 my @completions = completeme($given, $given, 0, length($given));
324 if ($#completions == 0) {
325 $name = shift @completions;
331 # start making requests
334 $line = $term->readline("obdcontrol > ");
342 my @cmdline = split(' ', $line);
343 my $word = shift @cmdline;
345 return 0 unless ($word);
351 $cmd = find_command($word);
354 printf STDERR "$word: No such command, or not unique.\n";
359 return (&{$commands{$cmd}->{func}}(@cmdline));
364 # select the OBD device we talk to
368 if ( ! $device && ! $::device ) { # first time ever
369 $device = '/dev/obd0';
372 if (($device) && ($::device ne $device)) {
377 print "Disconnecting active session ($::client_id)...";
378 Disconnect($::client_id);
381 if ($opendevfds{$device}) {
382 $::dev_obd = $opendevfds{$device};
385 # Open the device, as we need an FD for the ioctl
386 if (!sysopen(NEW_OBD, $device, 0)) {
387 print "Cannot open $device. Did you insert the obdclass module ?\n";
390 print "Opened device $device\n";
391 $opendevfds{$device} = *NEW_OBD;
392 $::dev_obd = *NEW_OBD;
396 print "Current device is $::device\n";
404 if ( ! $device && ! $::device ) { # first time ever
405 print "Nothing to close\n";
413 if ($::device eq $device) {
415 print "Disconnecting active session ($::client_id)...";
416 Disconnect($::client_id);
420 $fd2close = $opendevfds{$device};
421 if ($fd2close) { # XXXX something wrong in this if statement
423 $opendevfds{$device} = undef;
424 print "Closed device $device\n";
427 print "Device $device was not open\n";
431 if ($::device eq $device) {
435 print "No current device. You just closed the current device ($device).\n";
440 my $cmdfilename = shift;
442 if ( ! $cmdfilename ) {
443 print "please specify a command file name\n";
446 if (! open(CMDF, $cmdfilename)) {
447 print "Cannot open $cmdfilename: $!\n";
455 $rc = execute_line($_);
457 print "Something went wrong .......command exit status: $rc\n";
466 my $user_shell=$ENV{'SHELL'};
467 print "% $user_shell -c '@_'\n";
469 print "please specify a shell command\n";
472 system("$user_shell -c '@_'");
477 my $oldfh = select(STDOUT);
480 system('cat /proc/lustre/obd/*/status');
490 my $set_sysobd = shift;
493 foreach $i (0 .. $#procsysobd_objects) {
494 my $sysobd = $procsysobd_objects[$i];
496 if (defined $set_sysobd) {
497 if ($sysobd ne $set_sysobd) { next; }
499 if (defined $value) { # set this one
500 system("echo \"$value\" > /proc/sys/obd/$sysobd");
502 system("echo \"/proc/sys/obd/$sysobd:\"; cat /proc/sys/obd/$sysobd");
506 system("echo \"/proc/sys/obd/$sysobd:\"; cat /proc/sys/obd/$sysobd");
514 system("insmod $module");
520 system("rmmod $module");
526 system("lsmod $module");
537 print "error: missing type\n";
539 print "usage: attach {obdext2 | obdsnap | obdscsi | obdtrace }\n";
543 if ($type eq "obdscsi" ) {
549 $data = pack("iiii", $adapter, $bus, $tid, $lun);
551 } elsif ($type eq "obdsnap" ) {
556 $data = pack("iii", $snapdev, $snapidx, $tableno);
558 } elsif ($type eq "obdext2") {
559 $data = pack("i", 4711); # bogus data
561 } elsif ($type eq "obdtrace") {
562 $data = pack("i", 4711); # bogus data
565 print "error: unknown attach type $type\n";
569 my $len = length($type);
570 my $cl = length($data);
572 print "type $type (len $len), datalen $datalen ($cl)\n";
573 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
575 if (! defined $::dev_obd) {
576 print "No current device.\n";
579 my $rc = ioctl($::dev_obd, &OBD_IOC_ATTACH, $packed);
582 print STDERR "ioctl failed: $!\n";
584 } elsif ($rc eq "0 but true") {
585 print "Finished (success)\n";
588 print "ioctl returned error code $rc.\n";
598 if (! defined $::dev_obd) {
599 print "No current device.\n";
603 my $rc = ioctl($::dev_obd, &OBD_IOC_DETACH, $data);
606 print STDERR "ioctl failed: $!\n";
608 } elsif ($rc eq "0 but true") {
609 print "Finished (success)\n";
612 print "ioctl returned error code $rc.\n";
618 sub TestExt2Iterator {
619 if (!defined($::client_id)) {
620 print "You must first ``connect''.\n";
625 my $type = "obdext2";
627 $data = pack("i", 4711); # bogus data
630 my $len = length($type);
631 my $cl = length($data);
632 print "type $type (len $len), datalen $datalen ($cl)\n";
633 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
634 if (! defined $::dev_obd) {
635 print "No current device.\n";
639 my $rc = ioctl($::dev_obd, &OBD_EXT2_RUNIT, $packed);
642 print STDERR "ioctl failed: $!\n";
644 } elsif ($rc eq "0 but true") {
645 print "Finished (success)\n";
648 print "ioctl returned error code $rc.\n";
655 if (!defined($::client_id)) {
656 print "You must first ``connect''.\n";
661 my $type = "obdsnap";
663 $data = pack("i", 4711); # bogus data
666 my $len = length($type);
667 my $cl = length($data);
668 print "type $type (len $len), datalen $datalen ($cl)\n";
669 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
671 # XXX We need to fix this up so that after the objects in this snapshot
672 # are deleted, the snapshot itself is also removed from the table.
674 if (! defined $::dev_obd) {
675 print "No current device.\n";
679 my $rc = ioctl($::dev_obd, &OBD_SNAP_DELETE, $packed);
682 print STDERR "ioctl failed: $!\n";
684 } elsif ($rc eq "0 but true") {
685 print "Finished (success)\n";
688 print "ioctl returned error code $rc.\n";
694 # this routine does the whole job
696 my $restoreto = shift;
697 my $snaptable = shift;
701 # don't do anything until connected
702 if (!defined($::client_id)) {
703 print "You must first ``connect''.\n";
707 if ( ! $snaptable || ! defined $restoreto ) {
708 print "Usage: snaprestore \"restore to slot\" \"snaptable\" \"tableno\"\n";
712 if ( ! -f $snaptable ) {
713 print "Table $snaptable doesn't exist\n";
717 my $table = ReadSnapShotTable($snaptable);
718 $restoretime = FindSnapInTable($table, $restoreto);
719 if ( ! defined $table->{0} || ! defined $restoretime ) {
720 PrintSnapShotTable($table);
721 print "No current or $restoreto slot in this table\n";
725 my $currentindex = $table->{0};
726 if ( $table->{$restoretime} == $currentindex ) {
727 print "You should not restore to the current snapshot\n";
731 # swap the entries for 0 and $restoreto
732 my $tmp = $table->{$restoretime};
733 $table->{$restoretime} = $table->{0};
735 # PrintSnapShotTable($table);
738 WriteSnapShotTable($snaptable, $table);
740 # set it in the kernel
741 SnapSetTable($tableno, $snaptable);
743 # ready for the ioctl
745 my $type = "obdsnap";
746 $data = pack("i", $currentindex); # slot of previous current snapshot
749 my $len = length($type);
750 my $cl = length($data);
751 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
752 if (! defined $::dev_obd) {
753 print "No current device.\n";
757 my $rc = ioctl($::dev_obd, &OBD_SNAP_RESTORE, $packed);
760 print STDERR "ioctl failed: $!\n";
762 } elsif ($rc eq "0 but true") {
763 print "Snaprestore finished (success)\n";
764 delete $table->{$restoretime} if defined $restoretime;
766 WriteSnapShotTable($snaptable, $table);
768 # set it in the kernel
769 SnapSetTable($tableno, $snaptable);
770 # PrintSnapShotTable($table);
773 print "ioctl returned error code $rc.\n";
778 sub FindSnapInTable {
782 foreach my $restoretime ( keys %{$table} ) {
783 if ( $table->{$restoretime} == $snapno) {
784 print "Found key $restoretime for snapno $snapno\n";
794 my $type = "obdsnap";
795 my $snaptableno = shift;
797 $data = pack("i", $snaptableno);
800 my $len = length($type);
801 my $cl = length($data);
802 print "type $type (len $len), datalen $datalen ($cl)\n";
803 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
804 if (! defined $::dev_obd) {
805 print "No current device.\n";
809 my $rc = ioctl($::dev_obd, &OBD_SNAP_PRINTTABLE, $packed);
812 print STDERR "ioctl failed: $!\n";
814 } elsif ($rc eq "0 but true") {
815 print "Finished (success)\n";
818 print "ioctl returned error code $rc.\n";
825 my $type = "obdsnap";
826 my $snaptableno = shift;
834 print "No such file $file\n";
838 $table = ReadSnapShotTable($file);
840 $snapcount = keys %{$table};
841 print "Snapcount $snapcount\n";
843 if ( ! defined $table->{0} ) {
844 print "No current snapshot in table! First make one\n";
847 $data = pack("ii", $snaptableno, $snapcount);
849 foreach my $time (sort keys %{$table}) {
850 # XXX we should change to pack LL instead of I for times
851 $data .= pack("Ii", $time, $table->{$time});
855 my $len = length($type);
856 my $cl = length($data);
857 print "type $type (len $len), datalen $datalen ($cl)\n";
858 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
859 if (! defined $::dev_obd) {
860 print "No current device.\n";
864 my $rc = ioctl($::dev_obd, &OBD_SNAP_SETTABLE, $packed);
867 print STDERR "ioctl failed: $!\n";
869 } elsif ($rc eq "0 but true") {
870 print "Finished (success)\n";
873 print "ioctl returned error code $rc.\n";
881 my $file = &readl("enter file name: ");
885 my $table = ReadSnapShotTable($file);
888 PrintSnapShotTable($table);
889 my $action = &readl("Add, Delete or Quit [adq]: ");
890 goto done if ($action =~ "^q.*" );
891 goto add if ($action =~ "^a.*");
892 goto del if ($action =~ "^d.*");
896 my $idx = &readl("enter index where you want this snapshot: ");
897 my $time = &readl("enter time or 'now' or 'current': ");
898 my $oldtime = SnapFindTimeFromIdx($idx, $table);
899 if (defined $oldtime) {
900 print "This already exists, first clean up\n";
904 if ( $time eq 'now' ) {
906 } elsif ( $time eq 'current' ) {
909 $table->{$time} = $idx;
913 $didx = &readl("Enter index to delete: ");
914 my $deltime = SnapFindTimeFromIdx($didx, $table);
915 delete $table->{$deltime} if defined $deltime;
919 my $ok = &readl("OK with new table? [Yn]: ");
920 unless ( $ok eq "n" ) {
921 WriteSnapShotTable($file, $table);
926 sub SnapFindTimeFromIdx {
930 foreach my $time ( keys %{$table} ) {
931 if ( $table->{$time} == $idx ) {
938 sub PrintSnapShotTable {
942 foreach $time ( sort keys %{$table} ) {
943 my $stime = localtime($time);
947 printf "Time: %s -- Index %d\n", $stime, $table->{$time};
951 sub ReadSnapShotTable {
958 my ($time, $index) = split ;
959 $table->{$time} = $index;
963 PrintSnapShotTable($table);
968 sub WriteSnapShotTable {
973 foreach my $time ( sort keys %{$table} ) {
974 print FH "$time $table->{$time}\n";
984 # Note: _copy IOCTL takes parameters as dst, src.
985 # Copy function takes parameters as src, dst.
986 $src_obdo->{id} = shift;
987 $dst_obdo->{id} = shift;
988 $src_obdo->{valid} = &OBD_MD_FLALL;
990 # XXX need to fix copy so we can have 2 client IDs here
991 my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo);
992 if (! defined $::dev_obd) {
993 print "No current device.\n";
997 my $rc = ioctl($::dev_obd, &OBD_IOC_COPY, $packed);
1000 print STDERR "ioctl failed: $!\n";
1002 } elsif ($rc eq "0 but true") {
1003 print "Finished (success)\n";
1006 print "ioctl returned error code $rc.\n";
1014 # Note: _migr IOCTL takes parameters as dst, src.
1015 # Migrate function takes parameters as src, dst.
1016 $src_obdo->{id} = shift;
1017 $dst_obdo->{id} = shift;
1018 $src_obdo->{valid} = &OBD_MD_FLALL;
1020 # We pack a dummy connection ID here
1021 my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo);
1022 if (! defined $::dev_obd) {
1023 print "No current device.\n";
1027 my $rc = ioctl($::dev_obd, &OBD_IOC_MIGR, $packed);
1030 print STDERR "ioctl failed: $!\n";
1032 } elsif ($rc eq "0 but true") {
1033 print "Finished (success)\n";
1036 print "ioctl returned error code $rc.\n";
1045 my $data = pack("i", $size);
1048 my $packed = pack("ip", $datalen, $data);
1049 if (! defined $::dev_obd) {
1050 print "No current device.\n";
1053 my $rc = ioctl($::dev_obd, &OBD_IOC_FORMATOBD, $packed);
1056 print STDERR "ioctl failed: $!\n";
1058 } elsif ($rc eq "0 but true") {
1059 print "Finished (success)\n";
1062 print "ioctl returned error code $rc.\n";
1071 my $data = pack("ii", $partno, $size);
1072 my $datalen = 2 * 4;
1074 my $packed = pack("ip", $datalen, $data);
1075 if (! defined $::dev_obd) {
1076 print "No current device.\n";
1079 my $rc = ioctl($::dev_obd, &OBD_IOC_PARTITION, $packed);
1082 print STDERR "ioctl failed: $!\n";
1084 } elsif ($rc eq "0 but true") {
1085 print "Finished (success)\n";
1088 print "ioctl returned error code $rc.\n";
1099 # XXX we need a getinfo ioctl to validate parameters
1102 if ($arg && !defined($::st = stat($arg))) {
1103 print "$arg is not a valid device\n";
1109 $datalen = length($arg)+1; # need null character also
1112 my $packed = pack("ip", $datalen, $data);
1113 if (! defined $::dev_obd) {
1114 print "No current device.\n";
1117 my $rc = ioctl($::dev_obd, &OBD_IOC_SETUP, $packed);
1120 print STDERR "ioctl failed: $!\n";
1122 } elsif ($rc eq "0 but true") {
1123 print "Finished (success)\n";
1126 print "ioctl returned error code $rc.\n";
1133 if (! defined $::dev_obd) {
1134 print "No current device.\n";
1137 my $rc = ioctl($::dev_obd, &OBD_IOC_CLEANUP, $err);
1140 print STDERR "ioctl failed: $!\n";
1142 } elsif ($rc eq "0 but true") {
1143 print "Finished (success)\n";
1147 print "ioctl returned error code $rc.\n";
1157 if (! defined $::dev_obd) {
1158 print "No current device.\n";
1161 $rc = ioctl($::dev_obd, &OBD_IOC_CONNECT, $packed);
1162 $id = unpack("I", $packed);
1165 print STDERR "ioctl failed: $!\n";
1167 } elsif ($rc eq "0 but true") {
1169 print "Client ID : $id\n";
1170 print "Finished (success)\n";
1173 print "ioctl returned error code $rc.\n";
1181 if (!defined($id)) {
1185 if (!defined($id)) {
1186 print "syntax: disconnect [client ID]\n";
1187 print "When client ID is not given, the last valid client ID to be returned by a\n";
1188 print "connect command this session is used; there is no such ID.\n";
1192 my $packed = pack("L", $id);
1193 if (! defined $::dev_obd) {
1194 print "No current device.\n";
1197 my $rc = ioctl($::dev_obd, &OBD_IOC_DISCONNECT, $packed);
1200 print STDERR "ioctl failed: $!\n";
1202 } elsif ($rc eq "0 but true") {
1203 $::client_id = undef;
1204 print "Finished (success)\n";
1207 print "ioctl returned error code $rc.\n";
1213 if (!defined($::client_id)) {
1214 print "You must first ``connect''.\n";
1224 if (!defined($num)) {
1228 if (!defined($mode)) {
1229 $mode = 0100644; # create a file (rw-r--r--) if not specified
1232 if (scalar($num) < 1 || defined($quiet) && $quiet ne "quiet") {
1233 print "usage: create [<number of objects> [<mode> [quiet]]]\n";
1238 my $id = 0; # can't currently request IDs
1240 print "Creating " . scalar($num) . " object";
1241 if (scalar($num) > 1) {
1246 for ($i = 0; $i < scalar($num); $i++) {
1249 $obdo->{mode} = scalar($mode);
1250 $obdo->{valid} = &OBD_MD_FLMODE;
1252 my $packed = pack("I", $::client_id) . obdo_pack($obdo);
1253 if (! defined $::dev_obd) {
1254 print "No current device.\n";
1257 $rc = ioctl($::dev_obd, &OBD_IOC_CREATE, $packed);
1258 if ($rc ne "0 but true") {
1260 } elsif (!defined($quiet)) {
1261 $obdo = obdo_unpack($packed, 4);
1262 print "Created object #$obdo->{id}.\n";
1267 print STDERR "ioctl failed: $!\n";
1269 } elsif ($rc eq "0 but true") {
1270 print "Finished (success)\n";
1273 print "ioctl returned error code $rc.\n";
1280 if (! defined $::dev_obd) {
1281 print "No current device.\n";
1284 my $rc = ioctl($::dev_obd, &OBD_IOC_SYNC, $err);
1287 print STDERR "ioctl failed: $!\n";
1289 } elsif ($rc eq "0 but true") {
1290 print "Finished (success)\n";
1293 print "ioctl returned error code $rc.\n";
1299 if (!defined($::client_id)) {
1300 print "You must first ``connect''.\n";
1306 if (!defined($id) || scalar($id) < 1) {
1307 print "usage: destroy <object number>\n";
1311 print "Destroying object $id...\n";
1312 my $packed = pack("IL", $::client_id, $id);
1313 if (! defined $::dev_obd) {
1314 print "No current device.\n";
1317 my $rc = ioctl($::dev_obd, &OBD_IOC_DESTROY, $packed);
1320 print STDERR "ioctl failed: $!\n";
1322 } elsif ($rc eq "0 but true") {
1323 print "Finished (success)\n";
1326 print "ioctl returned error code $rc.\n";
1332 if (!defined($::client_id)) {
1333 print "You must first ``connect''.\n";
1339 if (!defined($id) || scalar($id) < 1) {
1340 print "invalid arguments; type \"help getattr\" for a synopsis\n";
1347 $obdo->{valid} = &OBD_MD_FLALL;
1348 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1349 if (! defined $::dev_obd) {
1350 print "No current device.\n";
1353 my $rc = ioctl($::dev_obd, &OBD_IOC_GETATTR, $packed);
1356 print STDERR "ioctl failed: $!\n";
1358 } elsif ($rc eq "0 but true") {
1359 $obdo = obdo_unpack($packed, 4);
1363 print "ioctl returned error code $rc.\n";
1369 if (!defined($::client_id)) {
1370 print "You must first ``connect''.\n";
1376 if (!defined($id) || scalar($id) < 1) {
1377 print "invalid arguments; type \"help setattr\" for a synopsis\n";
1381 # XXX we do not currently set all of the fields in the obdo
1384 $obdo->{mode} = oct(shift);
1385 $obdo->{uid} = shift;
1386 $obdo->{gid} = shift;
1387 $obdo->{size} = shift;
1388 $obdo->{atime} = shift;
1389 $obdo->{mtime} = shift;
1390 $obdo->{ctime} = shift;
1393 if (defined($obdo->{atime})) {
1394 $obdo->{valid} |= &OBD_MD_FLATIME;
1396 if (defined($obdo->{mtime})) {
1397 $obdo->{valid} |= &OBD_MD_FLMTIME;
1399 if (defined($obdo->{ctime})) {
1400 $obdo->{valid} |= &OBD_MD_FLCTIME;
1402 if (defined($obdo->{size})) {
1403 $obdo->{valid} |= &OBD_MD_FLSIZE;
1405 if (defined($obdo->{mode})) {
1406 $obdo->{valid} |= &OBD_MD_FLMODE;
1408 if (defined($obdo->{uid})) {
1409 $obdo->{valid} |= &OBD_MD_FLUID;
1411 if (defined($obdo->{gid})) {
1412 $obdo->{valid} |= &OBD_MD_FLGID;
1415 printf "valid is %x, mode is %o\n", $obdo->{valid}, $obdo->{mode};
1416 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1417 if (! defined $::dev_obd) {
1418 print "No current device.\n";
1421 my $rc = ioctl($::dev_obd, &OBD_IOC_SETATTR, $packed);
1424 print STDERR "ioctl failed: $!\n";
1426 } elsif ($rc eq "0 but true") {
1427 print "Finished (success)\n";
1430 print "ioctl returned error code $rc.\n";
1436 if (!defined($::client_id)) {
1437 print "You must first ``connect''.\n";
1445 if (!defined($id) || scalar($id) < 1 || !defined($count) ||
1446 $count < 1 || (defined($offset) && $offset < 0)) {
1447 print "invalid arguments; type \"help read\" for a synopsis\n";
1451 if (!defined($offset)) {
1455 print("Reading $count bytes starting at byte $offset from object " .
1458 # "allocate" a large enough buffer
1459 my $buf = sprintf("%${count}s", " ");
1460 die "suck" if (length($buf) != $count);
1465 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1466 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1467 pack("p LL LL", $buf, $count, $offset);
1469 if (! defined $::dev_obd) {
1470 print "No current device.\n";
1473 my $rc = ioctl($::dev_obd, &OBD_IOC_READ, $packed);
1475 $retval = unpack("l", $packed);
1478 print STDERR "ioctl failed: $!\n";
1480 } elsif ($rc eq "0 but true") {
1482 print substr($buf, 0, $retval);
1483 print "\nRead $retval of an attempted $count bytes.\n";
1484 print "Finished (success)\n";
1487 print "Finished (error $retval)\n";
1491 print "ioctl returned error code $rc.\n";
1497 if (!defined($::client_id)) {
1498 print "You must first ``connect''.\n";
1506 if (!defined($id) || scalar($id) < 1 || !defined($count) ||
1507 $count < 1 || (defined($offset) && $offset < 0)) {
1508 print "invalid arguments; type \"help read\" for a synopsis\n";
1512 if (!defined($offset)) {
1516 print("Reading $count bytes starting at byte $offset from object " .
1519 # "allocate" a large enough buffer
1520 my $buf = sprintf("%${count}s", " ");
1521 die "suck" if (length($buf) != $count);
1526 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1527 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1528 pack("p LL LL", $buf, $count, $offset);
1530 if (! defined $::dev_obd) {
1531 print "No current device.\n";
1534 my $rc = ioctl($::dev_obd, &OBD_IOC_READ2, $packed);
1536 $retval = unpack("l", $packed);
1539 print STDERR "ioctl failed: $!\n";
1541 } elsif ($rc eq "0 but true") {
1543 print substr($buf, 0, $retval);
1544 print "\nRead $retval of an attempted $count bytes.\n";
1545 print "Finished (success)\n";
1548 print "Finished (error $retval)\n";
1552 print "ioctl returned error code $rc.\n";
1558 if (!defined($::client_id)) {
1559 print "You must first ``connect''.\n";
1565 my $text = join(' ', @_);
1566 my $count = length($text);
1568 if (!defined($id) || scalar($id) < 1 || !defined($offset) ||
1569 scalar($offset) < 0) {
1570 print "invalid arguments; type \"help write\" for a synopsis\n";
1574 if (!defined($text)) {
1579 print("Writing $count bytes starting at byte $offset to object $id...\n");
1584 # the perl we're using doesn't support pack type Q
1585 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1586 pack("p LL LL", $text, $count, $offset);
1588 if (! defined $::dev_obd) {
1589 print "No current device.\n";
1592 my $rc = ioctl($::dev_obd, &OBD_IOC_WRITE, $packed);
1594 $retval = unpack("l", $packed);
1597 print STDERR "ioctl failed: $!\n";
1599 } elsif ($rc eq "0 but true") {
1601 print "\nWrote $retval of an attempted $count bytes.\n";
1602 print "Finished (success)\n";
1605 print "Finished (error $retval)\n";
1609 print "ioctl returned error code $rc.\n";
1615 if (!defined($::client_id)) {
1616 print "You must first ``connect''.\n";
1624 if (!defined($id) || scalar($id) < 1 || !defined($start) ||
1625 scalar($start) < 0 || !defined($count) || scalar($count) < 0) {
1626 print "invalid arguments; type \"help punch\" for a synopsis\n";
1630 print("Punching $count bytes starting at byte $start from object $id...\n");
1635 # the perl we're using doesn't support pack type Q
1636 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1637 pack("p LL LL", $buf, $start, $count);
1639 if (! defined $::dev_obd) {
1640 print "No current device.\n";
1643 my $rc = ioctl($::dev_obd, &OBD_IOC_PUNCH, $packed);
1645 $retval = unpack("l", $packed);
1648 print STDERR "ioctl failed: $!\n";
1649 } elsif ($rc eq "0 but true") {
1651 print "\nPunched $retval of an attempted $count bytes.\n";
1652 print "Finished (success)\n";
1655 print "Finished (error $retval)\n";
1659 print "ioctl returned error code $rc.\n";
1667 if (!defined($::client_id)) {
1668 print "You must first ``connect''.\n";
1672 if (!defined($num) || scalar($num) < 1 || scalar($num) > 32) {
1676 print "Preallocating $num objects...\n";
1677 # client id, alloc, id[32]
1678 my $packed = pack("LLx128", $::client_id, $num);
1680 if (! defined $::dev_obd) {
1681 print "No current device.\n";
1684 my $rc = ioctl($::dev_obd, &OBD_IOC_PREALLOCATE, $packed);
1687 print STDERR "ioctl failed: $!\n";
1689 } elsif ($rc eq "0 but true") {
1690 my $alloc = unpack("x4L", $packed);
1691 my @ids = unpack("x8L32", $packed);
1694 print "Got $alloc objects: ";
1698 print "\nFinished (success)\n";
1701 print "ioctl returned error code $rc.\n";
1707 if (! defined $::dev_obd) {
1708 print "No current device.\n";
1711 my $rc = ioctl($::dev_obd, &OBD_IOC_DEC_USE_COUNT, 0);
1714 print STDERR "ioctl failed: $!\n";
1716 } elsif ($rc eq "0 but true") {
1717 print "Finished (success)\n";
1720 print "ioctl returned error code $rc.\n";
1726 if (!defined($::client_id)) {
1727 print "You must first ``connect''.\n";
1739 # __kernel_fsid_t f_fsid; (64 bits)
1744 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1747 if (! defined $::dev_obd) {
1748 print "No current device.\n";
1751 my $rc = ioctl($::dev_obd, &OBD_IOC_STATFS, $packed);
1754 print STDERR "ioctl failed: $!\n";
1756 } elsif ($rc eq "0 but true") {
1757 # skip both the conn_id and the fs_type in the buffer
1758 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
1759 unpack("x4x4LLLLLL", $packed);
1760 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
1761 "$bfree free ($bavail available).\n");
1762 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
1763 print "Finished (success)\n";
1766 print "ioctl returned error code $rc.\n";
1774 if ( !$cmd || !$commands{$cmd} ) {
1775 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
1777 print "Usage: " . $commands{$cmd}->{doc} . "\n";
1784 print "Disconnecting active session ($::client_id)...";
1785 Disconnect($::client_id);