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
213 ('device' => {func => "Device", doc => "device <dev>: open another OBD device"},
214 'create' => {func => "Create", doc => "create [<num> [<mode> [quiet]]]: create new object(s) (files, unless mode is given)"},
215 'attach' => {func => "Attach", doc => "attach { obdext2 | obdsnap snapdev snapidx tableno | obdscsi adapter bus tid lun }: attach this minor device to the specified driver" },
216 'detach' => {func => "Detach", doc => "detach this minor device"},
217 'testext2iterator' => {func => "TestExt2Iterator", doc => "test ext2 iterator function"},
218 'snapset' => {func => "SnapSetTable", doc => "snapset <tableno> <file>: set the table (created with snaptable) as table #tableno" },
219 'snapprint' => {func => "SnapPrint", doc => "snapprint <tableno>: output the contents of table #tableno to the syslog"},
220 'snapdelete' => {func => "SnapDelete", doc => "snapdelete: delete connected snap obd objects from disk"},
221 'snaprestore' => {func => "SnapRestore", doc => "snaprestore : restore connected old snap objects to be current"},
222 'snaptable' => {func => "SnapShotTable", doc => "snaptable: build a snapshot table (interactive)"},
223 'copy' => {func => "Copy", doc => "copy <srcid> <tgtid>: copy objects"},
224 'migrate' => {func => "Migrate", doc => "migrate <srcid> <tgtid>: migrate data from one object to another"},
225 'partition' => {func => "Partition", doc => "partition <type> <adapter> <bus> <tid> <lun> <partition> <size>: create a partition"},
226 'format' => {func => "Format", doc => "format <type> <adapter> <bus> <tid> <lun> <size>: format a partition"},
227 'setup' => {func => "Setup", doc => "setup [type]: link this OBD device to the underlying device (default type obdext2)"},
228 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
229 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
230 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
231 'destroy' => {func => "Destroy", doc => "destroy <id>: destroys an object"},
232 'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
233 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
234 'read' => {func => "Read", doc => "read <id> <count> [offset]: read data from object"},
235 'fsread' => {func => "Read2", doc => "read <id> <count> [offset]: read data from object"},
236 'write' => {func => "Write", doc => "write <id> <offset> <text>: write data to object"},
237 'punch' => {func => "Punch", doc => "punch <id> <start> <count>: punch a hole in object"},
238 'setattr' => {func => "Setattr", doc => "setattr <id> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]: sets object attributes"},
239 'getattr' => {func => "Getattr", doc => "getattr <id>: displays object attributes"},
240 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num objects."},
241 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
242 'help' => {func => \&Help, doc => "help: this message"},
243 'quit' => {func => \&Quit, doc => "see \"exit\""},
244 'exit' => {func => \&Quit, doc => "see \"quit\""}
248 # setup completion function
250 my @jcm_cmd_list = keys %commands;
265 return $term->readline(@_);
278 $term = new Term::ReadLine 'obdcontrol ';
279 $attribs = $term->Attribs;
280 $attribs->{attempted_completion_function} = \&completeme;
281 $term->ornaments('md,me,,'); # bold face prompt
283 # make sure stdout is not buffered
284 STDOUT->autoflush(1);
287 # Get on with the show
291 #------------------------------------------------------------------------------
293 my ($text, $line, $start, $end) = @_;
294 if (substr($line, 0, $start) =~ /^\s*$/) {
295 $attribs->{completion_word} = \@jcm_cmd_list;
296 return $term->completion_matches($text,
297 $attribs->{'list_completion_function'});
304 my @completions = completeme($given, $given, 0, length($given));
305 if ($#completions == 0) {
306 $name = shift @completions;
312 # start making requests
315 $line = $term->readline("obdcontrol > ");
323 my @cmdline = split(' ', $line);
324 my $word = shift @cmdline;
330 $cmd = find_command($word);
333 printf STDERR "$word: No such command, or not unique.\n";
337 if ($cmd eq "help" || $cmd eq "exit" || $cmd eq "quit") {
338 return (&{$commands{$cmd}->{func}}(@cmdline));
342 return (&{$commands{$cmd}->{func}}(@cmdline));
346 # select the OBD device we talk to
351 print "Disconnecting active session ($::client_id)...";
352 Disconnect($::client_id);
355 $device = "/dev/obd0";
358 # Open the device, as we need an FD for the ioctl
359 sysopen(DEV_OBD, $device, 0) || die "Cannot open $device";
360 print "Device now $device\n";
371 print "error: missing type\n";
373 print "usage: attach {obdext2 | obdsnap | obdscsi}\n";
377 if ($type eq "obdscsi" ) {
383 $data = pack("iiii", $adapter, $bus, $tid, $lun);
385 } elsif ($type eq "obdsnap" ) {
390 $data = pack("iii", $snapdev, $snapidx, $tableno);
392 } elsif ($type eq "obdext2") {
393 $data = pack("i", 4711); # bogus data
396 print "error: unknown attach type $type\n";
400 my $len = length($type);
401 my $cl = length($data);
403 print "type $type (len $len), datalen $datalen ($cl)\n";
404 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
406 my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
409 print STDERR "ioctl failed: $!\n";
410 } elsif ($rc eq "0 but true") {
411 print "Finished (success)\n";
413 print "ioctl returned error code $rc.\n";
421 my $rc = ioctl(DEV_OBD, &OBD_IOC_DETACH, $data);
424 print STDERR "ioctl failed: $!\n";
425 } elsif ($rc eq "0 but true") {
426 print "Finished (success)\n";
428 print "ioctl returned error code $rc.\n";
433 sub TestExt2Iterator {
434 if (!defined($::client_id)) {
435 print "You must first ``connect''.\n";
440 my $type = "obdext2";
442 $data = pack("i", 4711); # bogus data
445 my $len = length($type);
446 my $cl = length($data);
447 print "type $type (len $len), datalen $datalen ($cl)\n";
448 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
450 my $rc = ioctl(DEV_OBD, &OBD_EXT2_RUNIT, $packed);
453 print STDERR "ioctl failed: $!\n";
454 } elsif ($rc eq "0 but true") {
455 print "Finished (success)\n";
457 print "ioctl returned error code $rc.\n";
463 if (!defined($::client_id)) {
464 print "You must first ``connect''.\n";
469 my $type = "obdsnap";
471 $data = pack("i", 4711); # bogus data
474 my $len = length($type);
475 my $cl = length($data);
476 print "type $type (len $len), datalen $datalen ($cl)\n";
477 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
479 # XXX We need to fix this up so that after the objects in this snapshot
480 # are deleted, the snapshot itself is also removed from the table.
481 my $rc = ioctl(DEV_OBD, &OBD_SNAP_DELETE, $packed);
484 print STDERR "ioctl failed: $!\n";
485 } elsif ($rc eq "0 but true") {
486 print "Finished (success)\n";
488 print "ioctl returned error code $rc.\n";
493 # this routine does the whole job
495 my $restoreto = shift;
496 my $snaptable = shift;
500 # don't do anything until connected
501 if (!defined($::client_id)) {
502 print "You must first ``connect''.\n";
506 if ( ! $snaptable || ! defined $restoreto ) {
507 print "Usage: snaprestore \"restore to slot\" \"snaptable\" \"tableno\"\n";
511 if ( ! -f $snaptable ) {
512 print "Table $snaptable doesn't exist\n";
516 my $table = ReadSnapShotTable($snaptable);
517 $restoretime = FindSnapInTable($table, $restoreto);
518 if ( ! defined $table->{0} || ! defined $restoretime ) {
519 PrintSnapShotTable($table);
520 print "No current or $restoreto slot in this table\n";
524 my $currentindex = $table->{0};
525 if ( $table->{$restoretime} == $currentindex ) {
526 print "You should not restore to the current snapshot\n";
530 # swap the entries for 0 and $restoreto
531 my $tmp = $table->{$restoretime};
532 $table->{$restoretime} = $table->{0};
534 # PrintSnapShotTable($table);
537 WriteSnapShotTable($snaptable, $table);
539 # set it in the kernel
540 SnapSetTable($tableno, $snaptable);
542 # ready for the ioctl
544 my $type = "obdsnap";
545 $data = pack("i", $currentindex); # slot of previous current snapshot
548 my $len = length($type);
549 my $cl = length($data);
550 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
552 my $rc = ioctl(DEV_OBD, &OBD_SNAP_RESTORE, $packed);
555 print STDERR "ioctl failed: $!\n";
556 } elsif ($rc eq "0 but true") {
557 print "Snaprestore finished (success)\n";
558 delete $table->{$restoretime} if defined $restoretime;
560 WriteSnapShotTable($snaptable, $table);
562 # set it in the kernel
563 SnapSetTable($tableno, $snaptable);
564 # PrintSnapShotTable($table);
567 print "ioctl returned error code $rc.\n";
571 sub FindSnapInTable {
575 foreach my $restoretime ( keys %{$table} ) {
576 if ( $table->{$restoretime} == $snapno) {
577 print "Found key $restoretime for snapno $snapno\n";
587 my $type = "obdsnap";
588 my $snaptableno = shift;
590 $data = pack("i", $snaptableno);
593 my $len = length($type);
594 my $cl = length($data);
595 print "type $type (len $len), datalen $datalen ($cl)\n";
596 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
598 my $rc = ioctl(DEV_OBD, &OBD_SNAP_PRINTTABLE, $packed);
601 print STDERR "ioctl failed: $!\n";
602 } elsif ($rc eq "0 but true") {
603 print "Finished (success)\n";
605 print "ioctl returned error code $rc.\n";
611 my $type = "obdsnap";
612 my $snaptableno = shift;
620 print "No such file $file\n";
623 $table = ReadSnapShotTable($file);
625 $snapcount = keys %{$table};
626 print "Snapcount $snapcount\n";
628 if ( ! defined $table->{0} ) {
629 print "No current snapshot in table! First make one\n";
632 $data = pack("ii", $snaptableno, $snapcount);
634 foreach my $time (sort keys %{$table}) {
635 # XXX we should change to pack LL instead of I for times
636 $data .= pack("Ii", $time, $table->{$time});
640 my $len = length($type);
641 my $cl = length($data);
642 print "type $type (len $len), datalen $datalen ($cl)\n";
643 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
645 my $rc = ioctl(DEV_OBD, &OBD_SNAP_SETTABLE, $packed);
648 print STDERR "ioctl failed: $!\n";
649 } elsif ($rc eq "0 but true") {
650 print "Finished (success)\n";
652 print "ioctl returned error code $rc.\n";
659 my $file = &readl("enter file name: ");
663 my $table = ReadSnapShotTable($file);
666 PrintSnapShotTable($table);
667 my $action = &readl("Add, Delete or Quit [adq]: ");
668 goto done if ($action =~ "^q.*" );
669 goto add if ($action =~ "^a.*");
670 goto del if ($action =~ "^d.*");
674 my $idx = &readl("enter index where you want this snapshot: ");
675 my $time = &readl("enter time or 'now' or 'current': ");
676 my $oldtime = SnapFindTimeFromIdx($idx, $table);
677 if (defined $oldtime) {
678 print "This already exists, first clean up\n";
682 if ( $time eq 'now' ) {
684 } elsif ( $time eq 'current' ) {
687 $table->{$time} = $idx;
691 $didx = &readl("Enter index to delete: ");
692 my $deltime = SnapFindTimeFromIdx($didx, $table);
693 delete $table->{$deltime} if defined $deltime;
697 my $ok = &readl("OK with new table? [Yn]: ");
698 unless ( $ok eq "n" ) {
699 WriteSnapShotTable($file, $table);
703 sub SnapFindTimeFromIdx {
707 foreach my $time ( keys %{$table} ) {
708 if ( $table->{$time} == $idx ) {
715 sub PrintSnapShotTable {
719 foreach $time ( sort keys %{$table} ) {
720 my $stime = localtime($time);
724 printf "Time: %s -- Index %d\n", $stime, $table->{$time};
728 sub ReadSnapShotTable {
735 my ($time, $index) = split ;
736 $table->{$time} = $index;
740 PrintSnapShotTable($table);
745 sub WriteSnapShotTable {
750 foreach my $time ( sort keys %{$table} ) {
751 print FH "$time $table->{$time}\n";
761 # Note: _copy IOCTL takes parameters as dst, src.
762 # Copy function takes parameters as src, dst.
763 $src_obdo->{id} = shift;
764 $dst_obdo->{id} = shift;
765 $src_obdo->{valid} = &OBD_MD_FLALL;
767 # XXX need to fix copy so we can have 2 client IDs here
768 my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo);
770 my $rc = ioctl(DEV_OBD, &OBD_IOC_COPY, $packed);
773 print STDERR "ioctl failed: $!\n";
774 } elsif ($rc eq "0 but true") {
775 print "Finished (success)\n";
777 print "ioctl returned error code $rc.\n";
784 # Note: _migr IOCTL takes parameters as dst, src.
785 # Migrate function takes parameters as src, dst.
786 $src_obdo->{id} = shift;
787 $dst_obdo->{id} = shift;
788 $src_obdo->{valid} = &OBD_MD_FLALL;
790 # We pack a dummy connection ID here
791 my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo);
793 my $rc = ioctl(DEV_OBD, &OBD_IOC_MIGR, $packed);
796 print STDERR "ioctl failed: $!\n";
797 } elsif ($rc eq "0 but true") {
798 print "Finished (success)\n";
800 print "ioctl returned error code $rc.\n";
808 my $data = pack("i", $size);
811 my $packed = pack("ip", $datalen, $data);
812 my $rc = ioctl(DEV_OBD, &OBD_IOC_FORMATOBD, $packed);
815 print STDERR "ioctl failed: $!\n";
816 } elsif ($rc eq "0 but true") {
817 print "Finished (success)\n";
819 print "ioctl returned error code $rc.\n";
827 my $data = pack("ii", $partno, $size);
830 my $packed = pack("ip", $datalen, $data);
831 my $rc = ioctl(DEV_OBD, &OBD_IOC_PARTITION, $packed);
834 print STDERR "ioctl failed: $!\n";
835 } elsif ($rc eq "0 but true") {
836 print "Finished (success)\n";
838 print "ioctl returned error code $rc.\n";
848 # XXX we need a getinfo ioctl to validate parameters
851 if ($arg && !defined($::st = stat($arg))) {
852 print "$dev is not a valid device\n";
857 $dev = $::st->rdev() unless $dev;
858 $data = pack("i", $dev);
862 my $packed = pack("ip", $datalen, $data);
863 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP, $packed);
866 print STDERR "ioctl failed: $!\n";
867 } elsif ($rc eq "0 but true") {
868 print "Finished (success)\n";
870 print "ioctl returned error code $rc.\n";
876 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP, $err);
879 print STDERR "ioctl failed: $!\n";
880 } elsif ($rc eq "0 but true") {
881 print "Finished (success)\n";
884 print "ioctl returned error code $rc.\n";
893 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
894 $id = unpack("I", $packed);
897 print STDERR "ioctl failed: $!\n";
898 } elsif ($rc eq "0 but true") {
900 print "Client ID : $id\n";
901 print "Finished (success)\n";
903 print "ioctl returned error code $rc.\n";
915 print "syntax: disconnect [client ID]\n";
916 print "When client ID is not given, the last valid client ID to be returned by a\n";
917 print "connect command this session is used; there is no such ID.\n";
921 my $packed = pack("L", $id);
922 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
925 print STDERR "ioctl failed: $!\n";
926 } elsif ($rc eq "0 but true") {
927 $::client_id = undef;
928 print "Finished (success)\n";
930 print "ioctl returned error code $rc.\n";
935 if (!defined($::client_id)) {
936 print "You must first ``connect''.\n";
946 if (!defined($num)) {
950 if (!defined($mode)) {
951 $mode = 0100644; # create a file (rw-r--r--) if not specified
954 if (scalar($num) < 1 || defined($quiet) && $quiet ne "quiet") {
955 print "usage: create [<number of objects> [<mode> [quiet]]]\n";
960 my $id = 0; # can't currently request IDs
962 print "Creating " . scalar($num) . " object";
963 if (scalar($num) > 1) {
968 for ($i = 0; $i < scalar($num); $i++) {
971 $obdo->{mode} = scalar($mode);
972 $obdo->{valid} = &OBD_MD_FLMODE;
974 my $packed = pack("I", $::client_id) . obdo_pack($obdo);
975 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
976 if ($rc ne "0 but true") {
978 } elsif (!defined($quiet)) {
979 $obdo = obdo_unpack($packed, 4);
980 print "Created object #$obdo->{id}.\n";
985 print STDERR "ioctl failed: $!\n";
986 } elsif ($rc eq "0 but true") {
987 print "Finished (success)\n";
989 print "ioctl returned error code $rc.\n";
995 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
998 print STDERR "ioctl failed: $!\n";
999 } elsif ($rc eq "0 but true") {
1000 print "Finished (success)\n";
1002 print "ioctl returned error code $rc.\n";
1007 if (!defined($::client_id)) {
1008 print "You must first ``connect''.\n";
1014 if (!defined($id) || scalar($id) < 1) {
1015 print "usage: destroy <object number>\n";
1019 print "Destroying object $id...\n";
1020 my $packed = pack("IL", $::client_id, $id);
1021 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
1024 print STDERR "ioctl failed: $!\n";
1025 } elsif ($rc eq "0 but true") {
1026 print "Finished (success)\n";
1028 print "ioctl returned error code $rc.\n";
1033 if (!defined($::client_id)) {
1034 print "You must first ``connect''.\n";
1040 if (!defined($id) || scalar($id) < 1) {
1041 print "invalid arguments; type \"help getattr\" for a synopsis\n";
1048 $obdo->{valid} = &OBD_MD_FLALL;
1049 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1050 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
1053 print STDERR "ioctl failed: $!\n";
1054 } elsif ($rc eq "0 but true") {
1055 $obdo = obdo_unpack($packed, 4);
1058 print "ioctl returned error code $rc.\n";
1063 if (!defined($::client_id)) {
1064 print "You must first ``connect''.\n";
1070 if (!defined($id) || scalar($id) < 1) {
1071 print "invalid arguments; type \"help setattr\" for a synopsis\n";
1075 # XXX we do not currently set all of the fields in the obdo
1078 $obdo->{mode} = oct(shift);
1079 $obdo->{uid} = shift;
1080 $obdo->{gid} = shift;
1081 $obdo->{size} = shift;
1082 $obdo->{atime} = shift;
1083 $obdo->{mtime} = shift;
1084 $obdo->{ctime} = shift;
1087 if (defined($obdo->{atime})) {
1088 $obdo->{valid} |= &OBD_MD_FLATIME;
1090 if (defined($obdo->{mtime})) {
1091 $obdo->{valid} |= &OBD_MD_FLMTIME;
1093 if (defined($obdo->{ctime})) {
1094 $obdo->{valid} |= &OBD_MD_FLCTIME;
1096 if (defined($obdo->{size})) {
1097 $obdo->{valid} |= &OBD_MD_FLSIZE;
1099 if (defined($obdo->{mode})) {
1100 $obdo->{valid} |= &OBD_MD_FLMODE;
1102 if (defined($obdo->{uid})) {
1103 $obdo->{valid} |= &OBD_MD_FLUID;
1105 if (defined($obdo->{gid})) {
1106 $obdo->{valid} |= &OBD_MD_FLGID;
1109 printf "valid is %x, mode is %o\n", $obdo->{valid}, $obdo->{mode};
1110 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1111 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
1114 print STDERR "ioctl failed: $!\n";
1115 } elsif ($rc eq "0 but true") {
1116 print "Finished (success)\n";
1118 print "ioctl returned error code $rc.\n";
1123 if (!defined($::client_id)) {
1124 print "You must first ``connect''.\n";
1132 if (!defined($id) || scalar($id) < 1 || !defined($count) ||
1133 $count < 1 || (defined($offset) && $offset < 0)) {
1134 print "invalid arguments; type \"help read\" for a synopsis\n";
1138 if (!defined($offset)) {
1142 print("Reading $count bytes starting at byte $offset from object " .
1145 # "allocate" a large enough buffer
1146 my $buf = sprintf("%${count}s", " ");
1147 die "suck" if (length($buf) != $count);
1152 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1153 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1154 pack("p LL LL", $buf, $count, $offset);
1156 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
1158 $retval = unpack("l", $packed);
1161 print STDERR "ioctl failed: $!\n";
1162 } elsif ($rc eq "0 but true") {
1164 print substr($buf, 0, $retval);
1165 print "\nRead $retval of an attempted $count bytes.\n";
1166 print "Finished (success)\n";
1168 print "Finished (error $retval)\n";
1171 print "ioctl returned error code $rc.\n";
1176 if (!defined($::client_id)) {
1177 print "You must first ``connect''.\n";
1185 if (!defined($id) || scalar($id) < 1 || !defined($count) ||
1186 $count < 1 || (defined($offset) && $offset < 0)) {
1187 print "invalid arguments; type \"help read\" for a synopsis\n";
1191 if (!defined($offset)) {
1195 print("Reading $count bytes starting at byte $offset from object " .
1198 # "allocate" a large enough buffer
1199 my $buf = sprintf("%${count}s", " ");
1200 die "suck" if (length($buf) != $count);
1205 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1206 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1207 pack("p LL LL", $buf, $count, $offset);
1209 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed);
1211 $retval = unpack("l", $packed);
1214 print STDERR "ioctl failed: $!\n";
1215 } elsif ($rc eq "0 but true") {
1217 print substr($buf, 0, $retval);
1218 print "\nRead $retval of an attempted $count bytes.\n";
1219 print "Finished (success)\n";
1221 print "Finished (error $retval)\n";
1224 print "ioctl returned error code $rc.\n";
1229 if (!defined($::client_id)) {
1230 print "You must first ``connect''.\n";
1236 my $text = join(' ', @_);
1237 my $count = length($text);
1239 if (!defined($id) || scalar($id) < 1 || !defined($offset) ||
1240 scalar($offset) < 0) {
1241 print "invalid arguments; type \"help write\" for a synopsis\n";
1245 if (!defined($text)) {
1250 print("Writing $count bytes starting at byte $offset to object $id...\n");
1255 # the perl we're using doesn't support pack type Q
1256 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1257 pack("p LL LL", $buf, $count, $offset);
1259 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
1261 $retval = unpack("l", $packed);
1264 print STDERR "ioctl failed: $!\n";
1265 } elsif ($rc eq "0 but true") {
1267 print "\nWrote $retval of an attempted $count bytes.\n";
1268 print "Finished (success)\n";
1270 print "Finished (error $retval)\n";
1273 print "ioctl returned error code $rc.\n";
1278 if (!defined($::client_id)) {
1279 print "You must first ``connect''.\n";
1287 if (!defined($id) || scalar($id) < 1 || !defined($start) ||
1288 scalar($start) < 0 || !defined($count) || scalar($count) < 0) {
1289 print "invalid arguments; type \"help punch\" for a synopsis\n";
1293 print("Punching $count bytes starting at byte $start from object $id...\n");
1298 # the perl we're using doesn't support pack type Q
1299 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1300 pack("p LL LL", $buf, $start, $count);
1302 my $rc = ioctl(DEV_OBD, &OBD_IOC_PUNCH, $packed);
1304 $retval = unpack("l", $packed);
1307 print STDERR "ioctl failed: $!\n";
1308 } elsif ($rc eq "0 but true") {
1310 print "\nPunched $retval of an attempted $count bytes.\n";
1311 print "Finished (success)\n";
1313 print "Finished (error $retval)\n";
1316 print "ioctl returned error code $rc.\n";
1323 if (!defined($::client_id)) {
1324 print "You must first ``connect''.\n";
1328 if (!defined($num) || scalar($num) < 1 || scalar($num) > 32) {
1332 print "Preallocating $num objects...\n";
1333 # client id, alloc, id[32]
1334 my $packed = pack("LLx128", $::client_id, $num);
1336 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
1339 print STDERR "ioctl failed: $!\n";
1340 } elsif ($rc eq "0 but true") {
1341 my $alloc = unpack("x4L", $packed);
1342 my @ids = unpack("x8L32", $packed);
1345 print "Got $alloc objects: ";
1349 print "\nFinished (success)\n";
1351 print "ioctl returned error code $rc.\n";
1356 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
1359 print STDERR "ioctl failed: $!\n";
1360 } elsif ($rc eq "0 but true") {
1361 print "Finished (success)\n";
1363 print "ioctl returned error code $rc.\n";
1368 if (!defined($::client_id)) {
1369 print "You must first ``connect''.\n";
1381 # __kernel_fsid_t f_fsid; (64 bits)
1386 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1389 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
1392 print STDERR "ioctl failed: $!\n";
1393 } elsif ($rc eq "0 but true") {
1394 # skip both the conn_id and the fs_type in the buffer
1395 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
1396 unpack("x4x4LLLLLL", $packed);
1397 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
1398 "$bfree free ($bavail available).\n");
1399 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
1400 print "Finished (success)\n";
1402 print "ioctl returned error code $rc.\n";
1409 if ( !$cmd || !$commands{$cmd} ) {
1410 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
1412 print "Usage: " . $commands{$cmd}->{doc} . "\n";
1418 print "Disconnecting active session ($::client_id)...";
1419 Disconnect($::client_id);