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_SNAP_SETTABLE () { &_IOC(3, ord(\'f\'), 40, 4);}' unless
65 defined(&OBD_SNAP_SETTABLE);
66 eval 'sub OBD_SNAP_PRINTTABLE () { &_IOC(3, ord(\'f\'), 41, 4);}' unless
67 defined(&OBD_SNAP_PRINTTABLE);
68 eval 'sub OBD_SNAP_DELETE() { &_IOC(3, ord(\'f\'), 42, 4);}' unless
69 defined(&OBD_SNAP_DELETE);
70 eval 'sub OBD_SNAP_RESTORE() { &_IOC(3, ord(\'f\'), 43, 4);}' unless
71 defined(&OBD_SNAP_RESTORE);
73 eval 'sub OBD_EXT2_RUNIT () { &_IOC(3, ord(\'f\'), 61, 4);}' unless
74 defined(&OBD_EXT2_RUNIT);
76 eval 'sub OBD_MD_FLALL () {~0;}' unless defined(&OBD_MD_FLALL);
77 eval 'sub OBD_MD_FLATIME () {1<<1;}' unless defined(&OBD_MD_FLATIME);
78 eval 'sub OBD_MD_FLMTIME () {1<<2;}' unless defined(&OBD_MD_FLMTIME);
79 eval 'sub OBD_MD_FLCTIME () {1<<3;}' unless defined(&OBD_MD_FLCTIME);
80 eval 'sub OBD_MD_FLSIZE () {1<<4;}' unless defined(&OBD_MD_FLSIZE);
81 eval 'sub OBD_MD_FLMODE () {1<<7;}' unless defined(&OBD_MD_FLMODE);
82 eval 'sub OBD_MD_FLUID () {1<<8;}' unless defined(&OBD_MD_FLUID);
83 eval 'sub OBD_MD_FLGID () {1<<9;}' unless defined(&OBD_MD_FLGID);
93 # NOTE long long are layed out in ia32 memory as follows:
94 # u = 0xaaaabbbbccccdddd has ccccdddd at &u and aaaabbbb 4 bytes on
95 # this may be different on other architectures
97 # we use 32-bit integers for all 64-bit quantities in this program
98 # #define OBD_INLINESZ 60
99 # #define OBD_OBDMDSZ 60
100 # /* Note: 64-bit types are 64-bit aligned in structure */
108 # obd_blocks o_blocks;
109 # obd_blksize o_blksize;
114 # obd_flag o_obdflags;
116 # obd_count o_generation;
117 # obd_flag o_valid; /* hot fields in this obdo */
120 # struct list_head o_list;
121 # struct obd_ops *o_op;
126 pack "LL LL LL LL LL LL LL L L L L L L L L L a60 a60 L L L",
145 0, 0, # struct list_head
170 $obdo->{obdmd}) = unpack "x${offset}Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 Lx4 L L L L L L L L L a60 a60", $buf;
178 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",
201 GetOptions("f!" => \$file, "device=s" => \$::device, ) || die "Getoptions";
204 # get a console for the app
211 ('device' => {func => "Device", doc => "device <dev>: open another OBD device"},
212 'create' => {func => "Create", doc => "create: creates a new inode"},
213 'attach' => {func => "Attach", doc => "attach { ext2_obd | snap_obd snapdev snapidx tableno | scsi_obd adapter bus tid lun }" },
214 'detach' => {func => "Detach", doc => "detach this device"},
215 'testext2iterator' => {func => "TestExt2Iterator", doc => "test ext2 iterator function"},
216 'snapset' => {func => "SnapSetTable", doc => "snapset <tableno> <file>: set the table (created with snaptable) as table #tableno" },
217 'snapprint' => {func => "SnapPrint", doc => "snapprint <tableno>: output the contents of table #tableno to the syslog"},
218 'snapdelete' => {func => "SnapDelete", doc => "snapdelete: delete connected snap obd objects from disk"},
219 'snaprestore' => {func => "SnapRestore", doc => "snaprestore : restore connected old snap objects to be current"},
220 'snaptable' => {func => "SnapShotTable", doc => "snaptable: build a snapshot table (interactive)"},
221 'copy' => {func => "Copy", doc => "copy <srcid> <tgtid>: copy objects"},
222 'migrate' => {func => "Migrate", doc => "migrate <srcid> <tgtid>: migrate data from one object to another"},
223 'format' => {func => "Format", doc => "format type adapter bus tid lun size"},
224 'partition' => {func => "Partition", doc => "partition type adapter bus tid lun partition size"},
225 'setup' => {func => "Setup", doc => "setup [type]: link this OBD device to the underlying device (default type ext2_obd)"},
226 'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
227 'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
228 'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
229 'destroy' => {func => "Destroy", doc => "destroy <inode>: destroys an inode"},
230 'cleanup' => {func => "Cleanup", doc => "cleanup the minor obd device"},
231 'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
232 'read' => {func => "Read", doc => "read <inode> <count> [offset]"},
233 'fsread' => {func => "Read2", doc => "read <inode> <count> [offset]"},
234 'write' => {func => "Write", doc => "write <inode> <offset> <text>"},
235 'setattr' => {func => "Setattr", doc => "setattr <inode> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"},
236 'getattr' => {func => "Getattr", doc => "getattr <inode>: displays inode object attributes"},
237 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num inodes."},
238 'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
239 'help' => {func => \&Help, doc => "help: this message"},
240 'quit' => {func => \&Quit, doc => "see \"exit\""},
241 'exit' => {func => \&Quit, doc => "see \"quit\""}
245 # setup completion function
247 my @jcm_cmd_list = keys %commands;
262 return $term->readline(@_);
275 $term = new Term::ReadLine 'obdcontrol ';
276 $attribs = $term->Attribs;
277 $attribs->{attempted_completion_function} = \&completeme;
278 $term->ornaments('md,me,,'); # bold face prompt
280 # make sure stdout is not buffered
281 STDOUT->autoflush(1);
284 # Get on with the show
288 #------------------------------------------------------------------------------
290 my ($text, $line, $start, $end) = @_;
291 if (substr($line, 0, $start) =~ /^\s*$/) {
292 $attribs->{completion_word} = \@jcm_cmd_list;
293 return $term->completion_matches($text,
294 $attribs->{'list_completion_function'});
301 my @completions = completeme($given, $given, 0, length($given));
302 if ($#completions == 0) {
303 $name = shift @completions;
309 # start making requests
312 $line = $term->readline("obdcontrol > ");
320 my @arg = split(' ', $line);
321 my $word = shift @arg;
327 $cmd = find_command($word);
330 printf STDERR "$word: No such command, or not unique.\n";
334 if ($cmd eq "help" || $cmd eq "exit" || $cmd eq "quit") {
335 return (&{$commands{$cmd}->{func}}(@arg));
339 return (&{$commands{$cmd}->{func}}(@arg));
343 # select the OBD device we talk to
348 print "Disconnecting active session ($::client_id)...";
349 Disconnect($::client_id);
352 $device = "/dev/obd0";
355 # Open the device, as we need an FD for the ioctl
356 sysopen(DEV_OBD, $device, 0) || die "Cannot open $device";
357 print "Device now $device\n";
369 print "error: missing type\n";
371 print "usage: attach {ext2_obd | snap_obd | scsi_obd}\n";
375 if ($type eq "scsi_obd" ) {
381 $data = pack("iiii", $adapter, $bus, $tid, $lun);
383 } elsif ($type eq "snap_obd" ) {
388 $data = pack("iii", $snapdev, $snapidx, $tableno);
390 } elsif ($type eq "ext2_obd") {
391 $data = pack("i", 4711); # bogus data
394 print "error: unknown attach type $type\n";
398 my $len = length($type);
399 my $cl = length($data);
401 print "type $type (len $len), datalen $datalen ($cl)\n";
402 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
404 my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
407 print STDERR "ioctl failed: $!\n";
408 } elsif ($rc eq "0 but true") {
409 print "Finished (success)\n";
411 print "ioctl returned error code $rc.\n";
418 my $rc = ioctl(DEV_OBD, &OBD_IOC_DETACH, $data);
421 print STDERR "ioctl failed: $!\n";
422 } elsif ($rc eq "0 but true") {
423 print "Finished (success)\n";
425 print "ioctl returned error code $rc.\n";
430 sub TestExt2Iterator {
431 if (!defined($::client_id)) {
432 print "You must first ``connect''.\n";
437 my $type = "ext2_obd";
439 $data = pack("i", 4711); # bogus data
442 my $len = length($type);
443 my $cl = length($data);
444 my $add = pack("p", $data);
445 print "type $type (len $len), datalen $datalen ($cl)\n";
446 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
448 my $rc = ioctl(DEV_OBD, &OBD_EXT2_RUNIT, $packed);
451 print STDERR "ioctl failed: $!\n";
452 } elsif ($rc eq "0 but true") {
453 print "Finished (success)\n";
455 print "ioctl returned error code $rc.\n";
461 if (!defined($::client_id)) {
462 print "You must first ``connect''.\n";
467 my $type = "snap_obd";
469 $data = pack("i", 4711); # bogus data
472 my $len = length($type);
473 my $cl = length($data);
474 my $add = pack("p", $data);
475 print "type $type (len $len), datalen $datalen ($cl)\n";
476 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
478 # XXX We need to fix this up so that after the objects in this snapshot
479 # are deleted, the snapshot itself is also removed from the table.
480 my $rc = ioctl(DEV_OBD, &OBD_SNAP_DELETE, $packed);
483 print STDERR "ioctl failed: $!\n";
484 } elsif ($rc eq "0 but true") {
485 print "Finished (success)\n";
487 print "ioctl returned error code $rc.\n";
492 # this routine does the whole job
494 my $restoreto = shift;
495 my $snaptable = shift;
499 # don't do anything until connected
500 if (!defined($::client_id)) {
501 print "You must first ``connect''.\n";
505 if ( ! $snaptable || ! defined $restoreto ) {
506 print "Usage: snaprestore \"restore to slot\" \"snaptable\" \"tableno\"\n";
510 if ( ! -f $snaptable ) {
511 print "Table $snaptable doesn't exist\n";
515 my $table = ReadSnapShotTable($snaptable);
516 $restoretime = FindSnapInTable($table, $restoreto);
517 if ( ! defined $table->{0} || ! defined $restoretime ) {
518 PrintSnapShotTable($table);
519 print "No current or $restoreto slot in this table\n";
523 my $currentindex = $table->{0};
524 if ( $table->{$restoretime} == $currentindex ) {
525 print "You should not restore to the current snapshot\n";
529 # swap the entries for 0 and $restoreto
530 my $tmp = $table->{$restoretime};
531 $table->{$restoretime} = $table->{0};
533 # PrintSnapShotTable($table);
536 WriteSnapShotTable($snaptable, $table);
538 # set it in the kernel
539 SnapSetTable($tableno, $snaptable);
541 # ready for the ioctl
543 my $type = "snap_obd";
544 $data = pack("i", $currentindex); # slot of previous current snapshot
547 my $len = length($type);
548 my $cl = length($data);
549 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
551 my $rc = ioctl(DEV_OBD, &OBD_SNAP_RESTORE, $packed);
554 print STDERR "ioctl failed: $!\n";
555 } elsif ($rc eq "0 but true") {
556 print "Snaprestore finished (success)\n";
557 delete $table->{$restoretime} if defined $restoretime;
559 WriteSnapShotTable($snaptable, $table);
561 # set it in the kernel
562 SnapSetTable($tableno, $snaptable);
563 # PrintSnapShotTable($table);
566 print "ioctl returned error code $rc.\n";
570 sub FindSnapInTable {
574 foreach my $restoretime ( keys %{$table} ) {
575 if ( $table->{$restoretime} == $snapno) {
576 print "Found key $restoretime for snapno $snapno\n";
586 my $type = "snap_obd";
587 my $snaptableno = shift;
589 $data = pack("i", $snaptableno);
592 my $len = length($type);
593 my $cl = length($data);
594 my $add = pack("p", $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 = "snap_obd";
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 $data .= pack("Ii", $time, $table->{$time});
639 my $len = length($type);
640 my $cl = length($data);
641 my $add = pack("p", $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 my $data = pack("III", $::client_id, $dstid, $srcid);
766 my $packed = pack("ip", $datalen, $data);
767 my $rc = ioctl(DEV_OBD, &OBD_IOC_COPY, $packed);
770 print STDERR "ioctl failed: $!\n";
771 } elsif ($rc eq "0 but true") {
772 print "Finished (success)\n";
774 print "ioctl returned error code $rc.\n";
783 # Note: _migr IOCTL takes parameters as dst, src.
784 # Migrate function takes parameters as src, dst.
785 my $data = pack("III", $::client_id, $dstid, $srcid);
788 my $packed = pack("ip", $datalen, $data);
789 my $rc = ioctl(DEV_OBD, &OBD_IOC_MIGR, $packed);
792 print STDERR "ioctl failed: $!\n";
793 } elsif ($rc eq "0 but true") {
794 print "Finished (success)\n";
796 print "ioctl returned error code $rc.\n";
804 my $data = pack("i", $size);
807 my $packed = pack("ip", $datalen, $data);
808 my $rc = ioctl(DEV_OBD, &OBD_IOC_FORMATOBD, $packed);
811 print STDERR "ioctl failed: $!\n";
812 } elsif ($rc eq "0 but true") {
813 print "Finished (success)\n";
815 print "ioctl returned error code $rc.\n";
823 my $data = pack("ii", $partno, $size);
826 my $packed = pack("ip", $datalen, $data);
827 my $rc = ioctl(DEV_OBD, &OBD_IOC_PARTITION, $packed);
830 print STDERR "ioctl failed: $!\n";
831 } elsif ($rc eq "0 but true") {
832 print "Finished (success)\n";
834 print "ioctl returned error code $rc.\n";
844 # XXX we need a getinfo ioctl to validate parameters
847 if ($arg && !defined($::st = stat($arg))) {
848 print "$arg is not a valid device\n";
853 $dev = $::st->rdev() unless $dev;
854 $data = pack("i", $dev);
858 my $packed = pack("ip", $datalen, $data);
859 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP, $packed);
862 print STDERR "ioctl failed: $!\n";
863 } elsif ($rc eq "0 but true") {
864 print "Finished (success)\n";
866 print "ioctl returned error code $rc.\n";
872 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP, $err);
875 print STDERR "ioctl failed: $!\n";
876 } elsif ($rc eq "0 but true") {
877 print "Finished (success)\n";
880 print "ioctl returned error code $rc.\n";
889 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
890 $id = unpack("I", $packed);
893 print STDERR "ioctl failed: $!\n";
894 } elsif ($rc eq "0 but true") {
896 print "Client ID : $id\n";
897 print "Finished (success)\n";
899 print "ioctl returned error code $rc.\n";
911 print "syntax: disconnect [client ID]\n";
912 print "When client ID is not given, the last valid client ID to be returned by a\n";
913 print "connect command this session is used; there is no such ID.\n";
917 my $packed = pack("L", $id);
918 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
921 print STDERR "ioctl failed: $!\n";
922 } elsif ($rc eq "0 but true") {
923 $::client_id = undef;
924 print "Finished (success)\n";
926 print "ioctl returned error code $rc.\n";
936 if (defined($quiet) && $quiet ne "quiet") {
937 print "syntax: create [number of objects [quiet]]\n";
941 my $packed = pack("IL", $::client_id, $prealloc);
942 if (!defined($arg) || scalar($arg) < 2) {
943 print "Creating 1 object...\n";
944 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
945 if (!defined($quiet)) {
946 my $obdo = obdo_unpack($packed, 4);
947 print "Created object #$obdo->{id}.\n";
952 print "Creating " . scalar($arg) . " objects...\n";
953 for ($i = 0; $i < scalar($arg); $i++) {
954 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
955 my $ino = unpack("L", $packed);
956 if ($rc ne "0 but true") {
958 $packed = pack("IL", $::client_id, $prealloc);
959 } elsif (!defined($quiet)) {
960 $packed = pack("IL", $::client_id, $prealloc);
961 print "Created object #$ino.\n";
967 print STDERR "ioctl failed: $!\n";
968 } elsif ($rc eq "0 but true") {
969 print "Finished (success)\n";
971 print "ioctl returned error code $rc.\n";
977 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
980 print STDERR "ioctl failed: $!\n";
981 } elsif ($rc eq "0 but true") {
982 print "Finished (success)\n";
984 print "ioctl returned error code $rc.\n";
989 if (!defined($::client_id)) {
990 print "You must first ``connect''.\n";
996 if (!defined($arg) || scalar($arg) < 1) {
997 print "usage: destroy <object number>\n";
1001 print "Destroying object $arg...\n";
1002 my $packed = pack("IL", $::client_id, $arg);
1003 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
1006 print STDERR "ioctl failed: $!\n";
1007 } elsif ($rc eq "0 but true") {
1008 print "Finished (success)\n";
1010 print "ioctl returned error code $rc.\n";
1015 if (!defined($::client_id)) {
1016 print "You must first ``connect''.\n";
1022 if (!defined($inode) || scalar($inode) < 1) {
1023 print "invalid arguments; type \"help getattr\" for a synopsis\n";
1029 $obdo->{id} = $inode;
1030 $obdo->{valid} = &OBD_MD_FLALL;
1031 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1032 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
1036 print STDERR "ioctl failed: $!\n";
1037 } elsif ($rc eq "0 but true") {
1038 $obdo = obdo_unpack($packed, 4);
1041 print "ioctl returned error code $rc.\n";
1046 if (!defined($::client_id)) {
1047 print "You must first ``connect''.\n";
1053 if (!defined($inode) || scalar($inode) < 1) {
1054 print "invalid arguments; type \"help setattr\" for a synopsis\n";
1058 # XXX we do not currently set all of the fields in the obdo
1060 $obdo->{id} = $inode;
1061 $obdo->{mode} = oct(shift);
1062 $obdo->{uid} = shift;
1063 $obdo->{gid} = shift;
1064 $obdo->{size} = shift;
1065 $obdo->{atime} = shift;
1066 $obdo->{mtime} = shift;
1067 $obdo->{ctime} = shift;
1070 if (defined($obdo->{atime})) {
1071 $obdo->{valid} |= &OBD_MD_FLATIME;
1073 if (defined($obdo->{mtime})) {
1074 $obdo->{valid} |= &OBD_MD_FLMTIME;
1076 if (defined($obdo->{ctime})) {
1077 $obdo->{valid} |= &OBD_MD_FLCTIME;
1079 if (defined($obdo->{size})) {
1080 $obdo->{valid} |= &OBD_MD_FLSIZE;
1082 if (defined($obdo->{mode})) {
1083 $obdo->{valid} |= &OBD_MD_FLMODE;
1085 if (defined($obdo->{uid})) {
1086 $obdo->{valid} |= &OBD_MD_FLUID;
1088 if (defined($obdo->{gid})) {
1089 $obdo->{valid} |= &OBD_MD_FLGID;
1092 printf "valid is %x, mode is %o\n", $obdo->{valid}, $obdo->{mode};
1093 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1094 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
1097 print STDERR "ioctl failed: $!\n";
1098 } elsif ($rc eq "0 but true") {
1099 print "Finished (success)\n";
1101 print "ioctl returned error code $rc.\n";
1106 if (!defined($::client_id)) {
1107 print "You must first ``connect''.\n";
1115 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
1116 $count < 1 || (defined($offset) && $offset < 0)) {
1117 print "invalid arguments; type \"help read\" for a synopsis\n";
1121 if (!defined($offset)) {
1125 print("Reading $count bytes starting at byte $offset from object " .
1128 # "allocate" a large enough buffer
1129 my $buf = sprintf("%${count}s", " ");
1130 die "suck" if (length($buf) != $count);
1132 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1133 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
1135 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
1137 $retval = unpack("l", $packed);
1140 print STDERR "ioctl failed: $!\n";
1141 } elsif ($rc eq "0 but true") {
1143 print substr($buf, 0, $retval);
1144 print "\nRead $retval of an attempted $count bytes.\n";
1145 print "Finished (success)\n";
1147 print "Finished (error $retval)\n";
1150 print "ioctl returned error code $rc.\n";
1155 if (!defined($::client_id)) {
1156 print "You must first ``connect''.\n";
1164 if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
1165 $count < 1 || (defined($offset) && $offset < 0)) {
1166 print "invalid arguments; type \"help read\" for a synopsis\n";
1170 if (!defined($offset)) {
1174 print("Reading $count bytes starting at byte $offset from object " .
1177 # "allocate" a large enough buffer
1178 my $buf = sprintf("%${count}s", " ");
1179 die "suck" if (length($buf) != $count);
1181 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1182 my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
1184 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed);
1186 $retval = unpack("l", $packed);
1189 print STDERR "ioctl failed: $!\n";
1190 } elsif ($rc eq "0 but true") {
1192 print substr($buf, 0, $retval);
1193 print "\nRead $retval of an attempted $count bytes.\n";
1194 print "Finished (success)\n";
1196 print "Finished (error $retval)\n";
1199 print "ioctl returned error code $rc.\n";
1204 if (!defined($::client_id)) {
1205 print "You must first ``connect''.\n";
1211 my $text = join(' ', @_);
1212 my $count = length($text);
1214 if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
1215 scalar($offset) < 0) {
1216 print "invalid arguments; type \"help write\" for a synopsis\n";
1220 if (!defined($text)) {
1225 print("Writing $count bytes starting at byte $offset to object " .
1228 # the perl we're using doesn't support pack type Q
1229 my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, $offset, 0);
1230 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
1232 $retval = unpack("l", $packed);
1235 print STDERR "ioctl failed: $!\n";
1236 } elsif ($rc eq "0 but true") {
1238 print "\nWrote $retval of an attempted $count bytes.\n";
1239 print "Finished (success)\n";
1241 print "Finished (error $retval)\n";
1244 print "ioctl returned error code $rc.\n";
1251 if (!defined($::client_id)) {
1252 print "You must first ``connect''.\n";
1256 if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
1260 print "Preallocating $arg inodes...\n";
1261 my $packed = pack("LLx128", $::client_id, $arg);
1262 # client id, alloc, inodes[32]
1264 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
1267 print STDERR "ioctl failed: $!\n";
1268 } elsif ($rc eq "0 but true") {
1269 my $alloc = unpack("x4L", $packed);
1270 my @inodes = unpack("x8L32", $packed);
1273 print "Got $alloc inodes: ";
1274 foreach $i (@inodes) {
1277 print "\nFinished (success)\n";
1279 print "ioctl returned error code $rc.\n";
1284 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
1287 print STDERR "ioctl failed: $!\n";
1288 } elsif ($rc eq "0 but true") {
1289 print "Finished (success)\n";
1291 print "ioctl returned error code $rc.\n";
1296 if (!defined($::client_id)) {
1297 print "You must first ``connect''.\n";
1309 # __kernel_fsid_t f_fsid; (64 bits)
1314 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1317 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
1320 print STDERR "ioctl failed: $!\n";
1321 } elsif ($rc eq "0 but true") {
1322 # skip both the conn_id and the fs_type in the buffer
1323 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
1324 unpack("x4x4LLLLLL", $packed);
1325 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
1326 "$bfree free ($bavail available).\n");
1327 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
1328 print "Finished (success)\n";
1330 print "ioctl returned error code $rc.\n";
1337 if ( !$arg || !$commands{$arg} ) {
1338 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
1340 print "Usage: " . $commands{$arg}->{doc} . "\n";
1346 print "Disconnecting active session ($::client_id)...";
1347 Disconnect($::client_id);