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 [<num> [<mode> [quiet]]]: create new object(s) (files, unless mode is given)"},
213 'attach' => {func => "Attach", doc => "attach { obdext2 | obdsnap snapdev snapidx tableno | obdscsi adapter bus tid lun }: attach this minor device to the specified driver" },
214 'detach' => {func => "Detach", doc => "detach this minor 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 'partition' => {func => "Partition", doc => "partition <type> <adapter> <bus> <tid> <lun> <partition> <size>: create a partition"},
224 'format' => {func => "Format", doc => "format <type> <adapter> <bus> <tid> <lun> <size>: format a partition"},
225 'setup' => {func => "Setup", doc => "setup [type]: link this OBD device to the underlying device (default type obdext2)"},
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 <id>: destroys an object"},
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 <id> <count> [offset]: read data from object"},
233 'fsread' => {func => "Read2", doc => "read <id> <count> [offset]: read data from object"},
234 'write' => {func => "Write", doc => "write <id> <offset> <text>: write data to object"},
235 'setattr' => {func => "Setattr", doc => "setattr <id> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]: sets object attributes"},
236 'getattr' => {func => "Getattr", doc => "getattr <id>: displays object attributes"},
237 'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num objects."},
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 @cmdline = split(' ', $line);
321 my $word = shift @cmdline;
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}}(@cmdline));
339 return (&{$commands{$cmd}->{func}}(@cmdline));
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";
368 print "error: missing type\n";
370 print "usage: attach {obdext2 | obdsnap | obdscsi}\n";
374 if ($type eq "obdscsi" ) {
380 $data = pack("iiii", $adapter, $bus, $tid, $lun);
382 } elsif ($type eq "obdsnap" ) {
387 $data = pack("iii", $snapdev, $snapidx, $tableno);
389 } elsif ($type eq "obdext2") {
390 $data = pack("i", 4711); # bogus data
393 print "error: unknown attach type $type\n";
397 my $len = length($type);
398 my $cl = length($data);
400 print "type $type (len $len), datalen $datalen ($cl)\n";
401 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
403 my $rc = ioctl(DEV_OBD, &OBD_IOC_ATTACH, $packed);
406 print STDERR "ioctl failed: $!\n";
407 } elsif ($rc eq "0 but true") {
408 print "Finished (success)\n";
410 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 = "obdext2";
439 $data = pack("i", 4711); # bogus data
442 my $len = length($type);
443 my $cl = length($data);
444 print "type $type (len $len), datalen $datalen ($cl)\n";
445 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
447 my $rc = ioctl(DEV_OBD, &OBD_EXT2_RUNIT, $packed);
450 print STDERR "ioctl failed: $!\n";
451 } elsif ($rc eq "0 but true") {
452 print "Finished (success)\n";
454 print "ioctl returned error code $rc.\n";
460 if (!defined($::client_id)) {
461 print "You must first ``connect''.\n";
466 my $type = "obdsnap";
468 $data = pack("i", 4711); # bogus data
471 my $len = length($type);
472 my $cl = length($data);
473 print "type $type (len $len), datalen $datalen ($cl)\n";
474 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
476 # XXX We need to fix this up so that after the objects in this snapshot
477 # are deleted, the snapshot itself is also removed from the table.
478 my $rc = ioctl(DEV_OBD, &OBD_SNAP_DELETE, $packed);
481 print STDERR "ioctl failed: $!\n";
482 } elsif ($rc eq "0 but true") {
483 print "Finished (success)\n";
485 print "ioctl returned error code $rc.\n";
490 # this routine does the whole job
492 my $restoreto = shift;
493 my $snaptable = shift;
497 # don't do anything until connected
498 if (!defined($::client_id)) {
499 print "You must first ``connect''.\n";
503 if ( ! $snaptable || ! defined $restoreto ) {
504 print "Usage: snaprestore \"restore to slot\" \"snaptable\" \"tableno\"\n";
508 if ( ! -f $snaptable ) {
509 print "Table $snaptable doesn't exist\n";
513 my $table = ReadSnapShotTable($snaptable);
514 $restoretime = FindSnapInTable($table, $restoreto);
515 if ( ! defined $table->{0} || ! defined $restoretime ) {
516 PrintSnapShotTable($table);
517 print "No current or $restoreto slot in this table\n";
521 my $currentindex = $table->{0};
522 if ( $table->{$restoretime} == $currentindex ) {
523 print "You should not restore to the current snapshot\n";
527 # swap the entries for 0 and $restoreto
528 my $tmp = $table->{$restoretime};
529 $table->{$restoretime} = $table->{0};
531 # PrintSnapShotTable($table);
534 WriteSnapShotTable($snaptable, $table);
536 # set it in the kernel
537 SnapSetTable($tableno, $snaptable);
539 # ready for the ioctl
541 my $type = "obdsnap";
542 $data = pack("i", $currentindex); # slot of previous current snapshot
545 my $len = length($type);
546 my $cl = length($data);
547 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
549 my $rc = ioctl(DEV_OBD, &OBD_SNAP_RESTORE, $packed);
552 print STDERR "ioctl failed: $!\n";
553 } elsif ($rc eq "0 but true") {
554 print "Snaprestore finished (success)\n";
555 delete $table->{$restoretime} if defined $restoretime;
557 WriteSnapShotTable($snaptable, $table);
559 # set it in the kernel
560 SnapSetTable($tableno, $snaptable);
561 # PrintSnapShotTable($table);
564 print "ioctl returned error code $rc.\n";
568 sub FindSnapInTable {
572 foreach my $restoretime ( keys %{$table} ) {
573 if ( $table->{$restoretime} == $snapno) {
574 print "Found key $restoretime for snapno $snapno\n";
584 my $type = "obdsnap";
585 my $snaptableno = shift;
587 $data = pack("i", $snaptableno);
590 my $len = length($type);
591 my $cl = length($data);
592 print "type $type (len $len), datalen $datalen ($cl)\n";
593 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
595 my $rc = ioctl(DEV_OBD, &OBD_SNAP_PRINTTABLE, $packed);
598 print STDERR "ioctl failed: $!\n";
599 } elsif ($rc eq "0 but true") {
600 print "Finished (success)\n";
602 print "ioctl returned error code $rc.\n";
608 my $type = "obdsnap";
609 my $snaptableno = shift;
617 print "No such file $file\n";
620 $table = ReadSnapShotTable($file);
622 $snapcount = keys %{$table};
623 print "Snapcount $snapcount\n";
625 if ( ! defined $table->{0} ) {
626 print "No current snapshot in table! First make one\n";
629 $data = pack("ii", $snaptableno, $snapcount);
631 foreach my $time (sort keys %{$table}) {
632 # XXX we should change to pack LL instead of I for times
633 $data .= pack("Ii", $time, $table->{$time});
637 my $len = length($type);
638 my $cl = length($data);
639 print "type $type (len $len), datalen $datalen ($cl)\n";
640 my $packed = pack("Lipip", $::client_id, length($type), $type, $datalen, $data);
642 my $rc = ioctl(DEV_OBD, &OBD_SNAP_SETTABLE, $packed);
645 print STDERR "ioctl failed: $!\n";
646 } elsif ($rc eq "0 but true") {
647 print "Finished (success)\n";
649 print "ioctl returned error code $rc.\n";
656 my $file = &readl("enter file name: ");
660 my $table = ReadSnapShotTable($file);
663 PrintSnapShotTable($table);
664 my $action = &readl("Add, Delete or Quit [adq]: ");
665 goto done if ($action =~ "^q.*" );
666 goto add if ($action =~ "^a.*");
667 goto del if ($action =~ "^d.*");
671 my $idx = &readl("enter index where you want this snapshot: ");
672 my $time = &readl("enter time or 'now' or 'current': ");
673 my $oldtime = SnapFindTimeFromIdx($idx, $table);
674 if (defined $oldtime) {
675 print "This already exists, first clean up\n";
679 if ( $time eq 'now' ) {
681 } elsif ( $time eq 'current' ) {
684 $table->{$time} = $idx;
688 $didx = &readl("Enter index to delete: ");
689 my $deltime = SnapFindTimeFromIdx($didx, $table);
690 delete $table->{$deltime} if defined $deltime;
694 my $ok = &readl("OK with new table? [Yn]: ");
695 unless ( $ok eq "n" ) {
696 WriteSnapShotTable($file, $table);
700 sub SnapFindTimeFromIdx {
704 foreach my $time ( keys %{$table} ) {
705 if ( $table->{$time} == $idx ) {
712 sub PrintSnapShotTable {
716 foreach $time ( sort keys %{$table} ) {
717 my $stime = localtime($time);
721 printf "Time: %s -- Index %d\n", $stime, $table->{$time};
725 sub ReadSnapShotTable {
732 my ($time, $index) = split ;
733 $table->{$time} = $index;
737 PrintSnapShotTable($table);
742 sub WriteSnapShotTable {
747 foreach my $time ( sort keys %{$table} ) {
748 print FH "$time $table->{$time}\n";
758 # Note: _copy IOCTL takes parameters as dst, src.
759 # Copy function takes parameters as src, dst.
760 $src_obdo->{id} = shift;
761 $dst_obdo->{id} = shift;
762 $src_obdo->{valid} = &OBD_MD_FLALL;
764 # XXX need to fix copy so we can have 2 client IDs here
765 my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo);
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";
781 # Note: _migr IOCTL takes parameters as dst, src.
782 # Migrate function takes parameters as src, dst.
783 $src_obdo->{id} = shift;
784 $dst_obdo->{id} = shift;
785 $src_obdo->{valid} = &OBD_MD_FLALL;
787 # We pack a dummy connection ID here
788 my $packed = pack("L", $::client_id) . obdo_pack($dst_obdo) . pack("L", $::client_id) . obdo_pack($src_obdo);
790 my $rc = ioctl(DEV_OBD, &OBD_IOC_MIGR, $packed);
793 print STDERR "ioctl failed: $!\n";
794 } elsif ($rc eq "0 but true") {
795 print "Finished (success)\n";
797 print "ioctl returned error code $rc.\n";
805 my $data = pack("i", $size);
808 my $packed = pack("ip", $datalen, $data);
809 my $rc = ioctl(DEV_OBD, &OBD_IOC_FORMATOBD, $packed);
812 print STDERR "ioctl failed: $!\n";
813 } elsif ($rc eq "0 but true") {
814 print "Finished (success)\n";
816 print "ioctl returned error code $rc.\n";
824 my $data = pack("ii", $partno, $size);
827 my $packed = pack("ip", $datalen, $data);
828 my $rc = ioctl(DEV_OBD, &OBD_IOC_PARTITION, $packed);
831 print STDERR "ioctl failed: $!\n";
832 } elsif ($rc eq "0 but true") {
833 print "Finished (success)\n";
835 print "ioctl returned error code $rc.\n";
845 # XXX we need a getinfo ioctl to validate parameters
848 if ($arg && !defined($::st = stat($arg))) {
849 print "$dev is not a valid device\n";
854 $dev = $::st->rdev() unless $dev;
855 $data = pack("i", $dev);
859 my $packed = pack("ip", $datalen, $data);
860 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP, $packed);
863 print STDERR "ioctl failed: $!\n";
864 } elsif ($rc eq "0 but true") {
865 print "Finished (success)\n";
867 print "ioctl returned error code $rc.\n";
873 my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP, $err);
876 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";
890 $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
891 $id = unpack("I", $packed);
894 print STDERR "ioctl failed: $!\n";
895 } elsif ($rc eq "0 but true") {
897 print "Client ID : $id\n";
898 print "Finished (success)\n";
900 print "ioctl returned error code $rc.\n";
912 print "syntax: disconnect [client ID]\n";
913 print "When client ID is not given, the last valid client ID to be returned by a\n";
914 print "connect command this session is used; there is no such ID.\n";
918 my $packed = pack("L", $id);
919 my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
922 print STDERR "ioctl failed: $!\n";
923 } elsif ($rc eq "0 but true") {
924 $::client_id = undef;
925 print "Finished (success)\n";
927 print "ioctl returned error code $rc.\n";
932 if (!defined($::client_id)) {
933 print "You must first ``connect''.\n";
943 if (!defined($num)) {
947 if (!defined($mode)) {
948 $mode = 0100644; # create a file (rw-r--r--) if not specified
951 if (scalar($num) < 1 || defined($quiet) && $quiet ne "quiet") {
952 print "usage: create [<number of objects> [<mode> [quiet]]]\n";
957 my $id = 0; # can't currently request IDs
959 print "Creating " . scalar($num) . " object";
960 if (scalar($num) > 1) {
965 for ($i = 0; $i < scalar($num); $i++) {
968 $obdo->{mode} = scalar($mode);
969 $obdo->{valid} = &OBD_MD_FLMODE;
971 my $packed = pack("I", $::client_id) . obdo_pack($obdo);
972 $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
973 if ($rc ne "0 but true") {
975 } elsif (!defined($quiet)) {
976 $obdo = obdo_unpack($packed, 4);
977 print "Created object #$obdo->{id}.\n";
982 print STDERR "ioctl failed: $!\n";
983 } elsif ($rc eq "0 but true") {
984 print "Finished (success)\n";
986 print "ioctl returned error code $rc.\n";
992 my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
995 print STDERR "ioctl failed: $!\n";
996 } elsif ($rc eq "0 but true") {
997 print "Finished (success)\n";
999 print "ioctl returned error code $rc.\n";
1004 if (!defined($::client_id)) {
1005 print "You must first ``connect''.\n";
1011 if (!defined($id) || scalar($id) < 1) {
1012 print "usage: destroy <object number>\n";
1016 print "Destroying object $id...\n";
1017 my $packed = pack("IL", $::client_id, $id);
1018 my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
1021 print STDERR "ioctl failed: $!\n";
1022 } elsif ($rc eq "0 but true") {
1023 print "Finished (success)\n";
1025 print "ioctl returned error code $rc.\n";
1030 if (!defined($::client_id)) {
1031 print "You must first ``connect''.\n";
1037 if (!defined($id) || scalar($id) < 1) {
1038 print "invalid arguments; type \"help getattr\" for a synopsis\n";
1045 $obdo->{valid} = &OBD_MD_FLALL;
1046 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1047 my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
1050 print STDERR "ioctl failed: $!\n";
1051 } elsif ($rc eq "0 but true") {
1052 $obdo = obdo_unpack($packed, 4);
1055 print "ioctl returned error code $rc.\n";
1060 if (!defined($::client_id)) {
1061 print "You must first ``connect''.\n";
1067 if (!defined($id) || scalar($id) < 1) {
1068 print "invalid arguments; type \"help setattr\" for a synopsis\n";
1072 # XXX we do not currently set all of the fields in the obdo
1075 $obdo->{mode} = oct(shift);
1076 $obdo->{uid} = shift;
1077 $obdo->{gid} = shift;
1078 $obdo->{size} = shift;
1079 $obdo->{atime} = shift;
1080 $obdo->{mtime} = shift;
1081 $obdo->{ctime} = shift;
1084 if (defined($obdo->{atime})) {
1085 $obdo->{valid} |= &OBD_MD_FLATIME;
1087 if (defined($obdo->{mtime})) {
1088 $obdo->{valid} |= &OBD_MD_FLMTIME;
1090 if (defined($obdo->{ctime})) {
1091 $obdo->{valid} |= &OBD_MD_FLCTIME;
1093 if (defined($obdo->{size})) {
1094 $obdo->{valid} |= &OBD_MD_FLSIZE;
1096 if (defined($obdo->{mode})) {
1097 $obdo->{valid} |= &OBD_MD_FLMODE;
1099 if (defined($obdo->{uid})) {
1100 $obdo->{valid} |= &OBD_MD_FLUID;
1102 if (defined($obdo->{gid})) {
1103 $obdo->{valid} |= &OBD_MD_FLGID;
1106 printf "valid is %x, mode is %o\n", $obdo->{valid}, $obdo->{mode};
1107 my $packed = pack("L", $::client_id) . obdo_pack($obdo);
1108 my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
1111 print STDERR "ioctl failed: $!\n";
1112 } elsif ($rc eq "0 but true") {
1113 print "Finished (success)\n";
1115 print "ioctl returned error code $rc.\n";
1120 if (!defined($::client_id)) {
1121 print "You must first ``connect''.\n";
1129 if (!defined($id) || scalar($id) < 1 || !defined($count) ||
1130 $count < 1 || (defined($offset) && $offset < 0)) {
1131 print "invalid arguments; type \"help read\" for a synopsis\n";
1135 if (!defined($offset)) {
1139 print("Reading $count bytes starting at byte $offset from object " .
1142 # "allocate" a large enough buffer
1143 my $buf = sprintf("%${count}s", " ");
1144 die "suck" if (length($buf) != $count);
1149 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1150 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1151 pack("p LL LL", $buf, $count, $offset);
1153 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
1155 $retval = unpack("l", $packed);
1158 print STDERR "ioctl failed: $!\n";
1159 } elsif ($rc eq "0 but true") {
1161 print substr($buf, 0, $retval);
1162 print "\nRead $retval of an attempted $count bytes.\n";
1163 print "Finished (success)\n";
1165 print "Finished (error $retval)\n";
1168 print "ioctl returned error code $rc.\n";
1173 if (!defined($::client_id)) {
1174 print "You must first ``connect''.\n";
1182 if (!defined($id) || scalar($id) < 1 || !defined($count) ||
1183 $count < 1 || (defined($offset) && $offset < 0)) {
1184 print "invalid arguments; type \"help read\" for a synopsis\n";
1188 if (!defined($offset)) {
1192 print("Reading $count bytes starting at byte $offset from object " .
1195 # "allocate" a large enough buffer
1196 my $buf = sprintf("%${count}s", " ");
1197 die "suck" if (length($buf) != $count);
1202 # the perl we're using doesn't support pack type Q, and offset is 64 bits
1203 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1204 pack("p LL LL", $buf, $count, $offset);
1206 my $rc = ioctl(DEV_OBD, &OBD_IOC_READ2, $packed);
1208 $retval = unpack("l", $packed);
1211 print STDERR "ioctl failed: $!\n";
1212 } elsif ($rc eq "0 but true") {
1214 print substr($buf, 0, $retval);
1215 print "\nRead $retval of an attempted $count bytes.\n";
1216 print "Finished (success)\n";
1218 print "Finished (error $retval)\n";
1221 print "ioctl returned error code $rc.\n";
1226 if (!defined($::client_id)) {
1227 print "You must first ``connect''.\n";
1233 my $text = join(' ', @_);
1234 my $count = length($text);
1236 if (!defined($id) || scalar($id) < 1 || !defined($offset) ||
1237 scalar($offset) < 0) {
1238 print "invalid arguments; type \"help write\" for a synopsis\n";
1242 if (!defined($text)) {
1247 print("Writing $count bytes starting at byte $offset to object " .
1253 # the perl we're using doesn't support pack type Q
1254 my $packed = pack("L", $::client_id) . obdo_pack($obdo) .
1255 pack("p LL LL", $buf, $count, $offset);
1257 my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
1259 $retval = unpack("l", $packed);
1262 print STDERR "ioctl failed: $!\n";
1263 } elsif ($rc eq "0 but true") {
1265 print "\nWrote $retval of an attempted $count bytes.\n";
1266 print "Finished (success)\n";
1268 print "Finished (error $retval)\n";
1271 print "ioctl returned error code $rc.\n";
1278 if (!defined($::client_id)) {
1279 print "You must first ``connect''.\n";
1283 if (!defined($num) || scalar($num) < 1 || scalar($num) > 32) {
1287 print "Preallocating $num objects...\n";
1288 # client id, alloc, id[32]
1289 my $packed = pack("LLx128", $::client_id, $num);
1291 my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
1294 print STDERR "ioctl failed: $!\n";
1295 } elsif ($rc eq "0 but true") {
1296 my $alloc = unpack("x4L", $packed);
1297 my @ids = unpack("x8L32", $packed);
1300 print "Got $alloc objects: ";
1304 print "\nFinished (success)\n";
1306 print "ioctl returned error code $rc.\n";
1311 my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
1314 print STDERR "ioctl failed: $!\n";
1315 } elsif ($rc eq "0 but true") {
1316 print "Finished (success)\n";
1318 print "ioctl returned error code $rc.\n";
1323 if (!defined($::client_id)) {
1324 print "You must first ``connect''.\n";
1336 # __kernel_fsid_t f_fsid; (64 bits)
1341 my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1344 my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
1347 print STDERR "ioctl failed: $!\n";
1348 } elsif ($rc eq "0 but true") {
1349 # skip both the conn_id and the fs_type in the buffer
1350 my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
1351 unpack("x4x4LLLLLL", $packed);
1352 print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
1353 "$bfree free ($bavail available).\n");
1354 print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
1355 print "Finished (success)\n";
1357 print "ioctl returned error code $rc.\n";
1364 if ( !$cmd || !$commands{$cmd} ) {
1365 print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
1367 print "Usage: " . $commands{$cmd}->{doc} . "\n";
1373 print "Disconnecting active session ($::client_id)...";
1374 Disconnect($::client_id);