Whamcloud - gitweb
Much cleaner separation of the class and simulated obd code.
[fs/lustre-release.git] / lustre / obdclass / obdcontrol
1 #!/usr/bin/perl
2
3 #
4 # This code is issued under the GNU General Public License.
5 # See the file COPYING in this distribution
6 #
7 # Copyright (C) 1998, Stelias Computing
8
9 # Modified for InterMezzo from Gordian's HSM bcache device/jcm module
10 # Copyright (C) 1999, Carnegie Mellon University
11 #
12 # Derived from InterMezzo's incontrol, modified for OBD's
13 # Copyright (C) 1999, Stelias Computing
14 #
15 #
16
17 #use strict;
18 BEGIN { require "asm/errno.ph" };
19 BEGIN { require "asm/ioctl.ph" };
20
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_SUPER () { &_IOC(1, ord(\'f\'), 4, 4);}' unless
25   defined(&OBD_IOC_SETUP_SUPER);
26 eval 'sub OBD_IOC_CLEANUP_SUPER () { &_IOC(0, ord(\'f\'), 5, 0);}' unless
27   defined(&OBD_IOC_CLEANUP_SUPER);
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
51 eval 'sub ATTR_MODE () {1;}' unless defined(&ATTR_MODE);
52 eval 'sub ATTR_UID () {2;}' unless defined(&ATTR_UID);
53 eval 'sub ATTR_GID () {4;}' unless defined(&ATTR_GID);
54 eval 'sub ATTR_SIZE () {8;}' unless defined(&ATTR_SIZE);
55 eval 'sub ATTR_ATIME () {16;}' unless defined(&ATTR_ATIME);
56 eval 'sub ATTR_MTIME () {32;}' unless defined(&ATTR_MTIME);
57 eval 'sub ATTR_CTIME () {64;}' unless defined(&ATTR_CTIME);
58
59 use Getopt::Long;
60 use File::stat;
61 use Storable;
62 use Carp;
63 use Term::ReadLine;
64 use IO::Handle;
65
66 my ($device, $filesystem);
67 # startup options (I'll replace these when I have some to replace with)
68 GetOptions("device=s" => \$device, "fs=s" => $filesystem) || die "Getoptions";
69
70 # genuine new simulated OBD device
71 $device = "/dev/obd" unless $device;
72 # object store in the ext2 formatted block device
73 $filesystem = "/dev/loop0" unless $filesystem;
74
75 # get a console for the app
76 my $term = new Term::ReadLine 'obdcontrol ';
77 my $attribs = $term->Attribs;
78 $term->ornaments('md,me,,');    # bold face prompt
79
80 # make sure stdout is not buffered
81 STDOUT->autoflush(1);
82
83 my $line;
84 my $command;
85 my $arg;
86
87 my %commands =
88     ('create' => {func => "Create", doc => "create: creates a new inode"},
89      'setup' => {func => "Setup", doc => "setup: link the ext2 partition (default /dev/loop0) to this obddev"},
90      'connect' => {func => "Connect", doc => "connect: allocates client ID for this session"},
91      'disconnect' => {func => "Disconnect", doc => "disconnect [id]: frees client resources"},
92      'sync' => {func => "Sync", doc => "sync: flushes buffers to disk"},
93      'destroy' => {func => "Destroy", doc => "setup: destroys an inode"},
94      'cleanup' => {func => "Cleanup", doc => "detach the superblock from this minor obd dev"},
95      'dec_use_count' => {func => "Decusecount", doc => "decreases the module use count so that the module can be removed following an oops"},
96      'read' => {func => "Read", doc => "read <inode> <count> [offset]"},
97      'write' => {func => "Write", doc => "write <inode> <offset> <text>"},
98      'setattr' => {func => "Setattr", doc => "setattr <inode> [mode [uid [gid [size [atime [mtime [ctime]]]]]]]"},
99      'getattr' => {func => "Getattr", doc => "getattr <inode>: displays inode object attributes"},
100      'preallocate' => {func => "Preallocate", doc => "preallocate [num]: requests preallocation of num inodes."},
101      'statfs' => {func => "Statfs", doc => "statfs: filesystem status information"},
102      'help' => {func => \&Help,  doc => "help: this message"},
103      'quit' => {func => \&Quit,  doc => "see \"exit\""},
104      'exit' => {func => \&Quit,  doc => "see \"quit\""}
105     );
106
107 #
108 #       setup completion function
109 #
110 my @jcm_cmd_list = keys %commands;
111
112 $attribs->{attempted_completion_function} = \&completeme;
113 #------------------------------------------------------------------------------
114 # Open the device, as we need an FD for the ioctl
115 sysopen(DEV_OBD, $device, 0) || die "Cannot open $device";
116
117 if (!defined($::st = stat($filesystem))) {
118     die "Unable to stat $filesystem.\n";
119 }
120
121 # Get on with the show
122 process_line();
123
124 #------------------------------------------------------------------------------
125 sub completeme {
126     my ($text, $line, $start, $end) = @_;
127     if (substr($line, 0, $start) =~ /^\s*$/) {
128         $attribs->{completion_word} = \@jcm_cmd_list;
129         return $term->completion_matches($text,
130                                          $attribs->{'list_completion_function'});
131     }
132 }
133
134 sub find_command {
135     my $given = shift;
136     my $name;
137     my @completions = completeme($given, $given, 0, length($given));
138     if ($#completions == 0) {
139         $name = shift @completions;
140     }
141
142     return $name;
143 }
144
145 # start making requests
146 sub process_line {
147   foo:
148     $line = $term->readline("obdcontrol > ");
149     execute_line($line);
150     goto foo;
151 }
152
153 sub execute_line {
154     my $line = shift;
155
156     my @arg = split(' ', $line);
157     my $word = shift @arg;
158
159     my $cmd = find_command($word);
160     unless ($cmd) {
161         printf STDERR "$word: No such command, or not unique.\n";
162         return (-1);
163     }
164
165     if ($cmd eq "help" || $cmd eq "exit" || $cmd eq "quit") {
166         return (&{$commands{$cmd}->{func}}(@arg));
167     }
168
169     # Call the function.
170     return (&{$commands{$cmd}->{func}}(@arg));
171 }
172
173 sub Setup {
174     my $err = 0;
175     
176     my $packed = pack("La24", $::st->rdev(), "sim_obd");
177     my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP_SUPER, $packed);
178
179     if (!defined $rc) {
180         print STDERR "ioctl failed: $!\n";
181     } elsif ($rc eq "0 but true") {
182         print "Finished (success)\n";
183     } else {
184         print "ioctl returned error code $rc.\n";
185     }
186 }
187
188 sub Cleanup {
189     my $err = "0";
190     my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP_SUPER, $err);
191
192     if (!defined $rc) {
193         print STDERR "ioctl failed: $!\n";
194     } elsif ($rc eq "0 but true") {
195         print "Finished (success)\n";
196     } else {
197         print "ioctl returned error code $rc.\n";
198     }
199 }
200
201
202 sub Connect {
203     my $id = 0;
204     my $ino = 0;
205     my $bs = 0;
206     my $bs_b = 0;
207     my $rc;
208
209     # unsigned int conn_id;
210     # unsigned long conn_ino;
211     # unsigned long conn_blocksize;
212     # unsigned char conn_blocksize_bits;
213
214     my $packed = pack("ILLC", $id, $ino, $bs, $bs_b);
215     $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
216     ($id, $ino, $bs, $bs_b) = unpack("ILLC", $packed);
217
218     if (!defined $rc) {
219         print STDERR "ioctl failed: $!\n";
220     } elsif ($rc eq "0 but true") {
221         $::client_id = $id;
222         print "Client ID     : $id\n";
223         print "Root inode    : $ino\n";
224         print "Blocksize     : $bs\n";
225         print "Blocksize bits: $bs_b\n";
226         print "Finished (success)\n";
227     } else {
228         print "ioctl returned error code $rc.\n";
229     }
230 }
231
232 sub Disconnect {
233     my $id = shift;
234
235     if (!defined($id)) {
236         $id = $::client_id;
237     }
238
239     if (!defined($id)) {
240         print "syntax: disconnect [client ID]\n";
241         print "When client ID is not given, the last valid client ID to be returned by a\n";
242         print "connect command this session is used; there is no such ID.\n";
243         return;
244     }
245
246     my $packed = pack("L", $id);
247     my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
248
249     if (!defined $rc) {
250         print STDERR "ioctl failed: $!\n";
251     } elsif ($rc eq "0 but true") {
252         $::client_id = undef;
253         print "Finished (success)\n";
254     } else {
255         print "ioctl returned error code $rc.\n";
256     }
257 }
258
259 sub Create {
260     my $arg = shift;
261     my $quiet = shift;
262     my $rc;
263
264     if (defined($quiet) && !($quiet eq "quiet")) {
265         print "syntax: create [number of objects [quiet]]\n";
266         return;
267     }
268
269     my $packed = pack("I", $::client_id);
270     if (!defined($arg) || scalar($arg) < 2) {
271         print "Creating 1 object...\n";
272         my $packed = pack("I", 0);
273         $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
274         if (!defined($quiet)) {
275             my $ino = unpack("L", $packed);
276             print "Created object #$ino.\n";
277         }
278     } else {
279         my $i;
280
281         print "Creating " . scalar($arg) . " objects...\n";
282         for ($i = 0; $i < scalar($arg); $i++) {
283             $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
284             my $ino = unpack("L", $packed);
285             if (!($rc eq "0 but true") || $packed == 0) {
286                 last;
287             } elsif (!defined($quiet)) {
288                 print "Created object #$ino.\n";
289             }
290         }
291     }
292
293     if (!defined $rc) {
294         print STDERR "ioctl failed: $!\n";
295     } elsif ($rc eq "0 but true") {
296         print "Finished (success)\n";
297     } else {
298         print "ioctl returned error code $rc.\n";
299     }
300 }
301
302 sub Sync {
303     my $err = "0";
304     my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
305
306     if (!defined $rc) {
307         print STDERR "ioctl failed: $!\n";
308     } elsif ($rc eq "0 but true") {
309         print "Finished (success)\n";
310     } else {
311         print "ioctl returned error code $rc.\n";
312     }
313 }
314
315 sub Destroy {
316     if (!defined($::client_id)) {
317         print "You must first ``connect''.\n";
318         return;
319     }
320
321     my $arg = shift;
322
323     if (!defined($arg) || scalar($arg) < 1) {
324         print "destroy requires the object number to destroy.\n";
325         return;
326     }
327
328     print "Destroying object $arg...\n";
329     my $packed = pack("IL", $::client_id, $arg);
330     my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
331
332     if (!defined $rc) {
333         print STDERR "ioctl failed: $!\n";
334     } elsif ($rc eq "0 but true") {
335         print "Finished (success)\n";
336     } else {
337         print "ioctl returned error code $rc.\n";
338     }
339 }
340
341 sub Getattr {
342     if (!defined($::client_id)) {
343         print "You must first ``connect''.\n";
344         return;
345     }
346
347     my $inode = shift;
348
349     if (!defined($inode) || scalar($inode) < 1) {
350         print "invalid arguments; type \"help getattr\" for a synopsis\n";
351         return;
352     }
353
354     # see Setattr
355     my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
356                       0);
357     my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
358
359     if (!defined $rc) {
360         print STDERR "ioctl failed: $!\n";
361     } elsif ($rc eq "0 but true") {
362         my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
363         ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
364           unpack("ISssx2lLLLI", $packed);
365
366         printf("Inode: %d  Mode:  %o\n", $inode, $mode);
367         printf("User: %6d   Group: %6d   Size: %d\n", $uid, $gid, $size);
368         printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
369         printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
370         printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
371         printf("flags: %08x\n", $flags);
372         print "Finished (success)\n";
373     } else {
374         print "ioctl returned error code $rc.\n";
375     }
376 }
377
378 sub Setattr {
379     if (!defined($::client_id)) {
380         print "You must first ``connect''.\n";
381         return;
382     }
383
384     my $inode = shift;
385     my $valid = 0;
386     my $mode = oct(shift);
387     my $uid = shift;
388     my $gid = shift;
389     my $size = shift;
390     my $atime = shift;
391     my $mtime = shift;
392     my $ctime = shift;
393
394     if (defined($uid)) {
395         $valid |= &ATTR_UID;
396     }
397     if (defined($gid)) {
398         $valid |= &ATTR_GID;
399     }
400     if (defined($size)) {
401         $valid |= &ATTR_SIZE;
402     }
403     if (defined($atime)) {
404         $valid |= &ATTR_ATIME;
405     }
406     if (defined($mtime)) {
407         $valid |= &ATTR_MTIME;
408     }
409     if (defined($ctime)) {
410         $valid |= &ATTR_CTIME;
411     }
412     if (defined($mode)) {
413         $valid |= &ATTR_MODE;
414     }
415
416     if (!defined($inode) || scalar($inode) < 1) {
417         print "invalid arguments; type \"help setattr\" for a synopsis\n";
418         return;
419     }
420
421     #struct iattr {
422     #        unsigned int    ia_valid; (32)
423     #        umode_t         ia_mode; (16)
424     #        uid_t           ia_uid; (16)
425     #        gid_t           ia_gid; (16)
426     # -- 16 bit alignment here! --
427     #        off_t           ia_size; (32)
428     #        time_t          ia_atime; (32)
429     #        time_t          ia_mtime; (32)
430     #        time_t          ia_ctime; (32)
431     #        unsigned int    ia_attr_flags; (32)
432     #};
433
434     printf "valid is %x, mode is %o\n", $valid, $mode;
435     my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
436                       $uid, $gid, $size, $atime, $mtime, $ctime, 0);
437     my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
438
439     if (!defined $rc) {
440         print STDERR "ioctl failed: $!\n";
441     } elsif ($rc eq "0 but true") {
442         print "Finished (success)\n";
443     } else {
444         print "ioctl returned error code $rc.\n";
445     }
446 }
447
448 sub Read {
449     if (!defined($::client_id)) {
450         print "You must first ``connect''.\n";
451         return;
452     }
453
454     my $inode = shift;
455     my $count = shift;
456     my $offset = shift;
457   
458     if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
459         $count < 1 || (defined($offset) && $offset < 0)) {
460         print "invalid arguments; type \"help read\" for a synopsis\n";
461         return;
462     }
463
464     if (!defined($offset)) {
465         $offset = 0;
466     }
467
468     print("Reading $count bytes starting at byte $offset from object " .
469           "$inode...\n");
470
471     # "allocate" a large enough buffer
472     my $buf = sprintf("%${count}s", " ");
473     die "suck" if (length($buf) != $count);
474
475     # the perl we're using doesn't support pack type Q, and offset is 64 bits
476     my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, $offset, 0);
477
478     my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
479
480     $retval = unpack("l", $packed);
481
482     if (!defined $rc) {
483         print STDERR "ioctl failed: $!\n";
484     } elsif ($rc eq "0 but true") {
485         if ($retval >= 0) {
486                 print substr($buf, 0, $retval);
487                 print "\nRead $retval of an attempted $count bytes.\n";
488                 print "Finished (success)\n";
489         } else {
490                 print "Finished (error $retval)\n";
491         }
492     } else {
493         print "ioctl returned error code $rc.\n";
494     }
495 }
496
497 sub Write {
498     if (!defined($::client_id)) {
499         print "You must first ``connect''.\n";
500         return;
501     }
502
503     my $inode = shift;
504     my $offset = shift;
505     my $text = join(' ', @_);
506     my $count = length($text);
507
508     if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
509         scalar($offset) < 0) {
510         print "invalid arguments; type \"help write\" for a synopsis\n";
511         return;
512     }
513
514     if (!defined($text)) {
515         $text = "";
516         $count = 0;
517     }
518
519     print("Writing $count bytes starting at byte $offset to object " .
520           "$inode...\n");
521
522     # the perl we're using doesn't support pack type Q
523     my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, $offset, 0);
524     my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
525
526     $retval = unpack("l", $packed);
527
528     if (!defined $rc) {
529         print STDERR "ioctl failed: $!\n";
530     } elsif ($rc eq "0 but true") {
531         if ($retval >= 0) {
532                 print "\nWrote $retval of an attempted $count bytes.\n";
533                 print "Finished (success)\n";
534         } else {
535                 print "Finished (error $retval)\n";
536         }
537     } else {
538         print "ioctl returned error code $rc.\n";
539     }
540 }
541
542 sub Preallocate {
543     my $arg = shift;
544
545     if (!defined($::client_id)) {
546         print "You must first ``connect''.\n";
547         return;
548     }
549
550     if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
551         $arg = 32;
552     }
553
554     print "Preallocating $arg inodes...\n";
555     my $packed = pack("LLx128", $::client_id, $arg);
556     # client id, alloc, inodes[32]
557
558     my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
559
560     if (!defined $rc) {
561         print STDERR "ioctl failed: $!\n";
562     } elsif ($rc eq "0 but true") {
563         my $alloc = unpack("x4L", $packed);
564         my @inodes = unpack("x8L32", $packed);
565         my $i;
566
567         print "Got $alloc inodes: ";
568         foreach $i (@inodes) {
569             print $i . " ";
570         }
571         print "\nFinished (success)\n";
572     } else {
573         print "ioctl returned error code $rc.\n";
574     }
575 }
576
577 sub Decusecount {
578     my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
579
580     if (!defined $rc) {
581         print STDERR "ioctl failed: $!\n";
582     } elsif ($rc eq "0 but true") {
583         print "Finished (success)\n";
584     } else {
585         print "ioctl returned error code $rc.\n";
586     }
587 }
588
589 sub Statfs {
590     if (!defined($::client_id)) {
591         print "You must first ``connect''.\n";
592         return;
593     }
594
595     # struct statfs {
596     #         long f_type;
597     #         long f_bsize;
598     #         long f_blocks;
599     #         long f_bfree;
600     #         long f_bavail;
601     #         long f_files;
602     #         long f_ffree;
603     #         __kernel_fsid_t f_fsid; (64 bits)
604     #         long f_namelen;
605     #         long f_spare[6];
606     # };
607
608     my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
609                       0, 0, 0, 0, 0, 0);
610
611     my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
612
613     if (!defined $rc) {
614         print STDERR "ioctl failed: $!\n";
615     } elsif ($rc eq "0 but true") {
616         # skip both the conn_id and the fs_type in the buffer
617         my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
618             unpack("x4x4LLLLLL", $packed);
619         print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
620               "$bfree free ($bavail available).\n");
621         print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
622         print "Finished (success)\n";
623     } else {
624         print "ioctl returned error code $rc.\n";
625     }
626 }
627
628 sub Help {
629     my $arg = shift;
630
631     if ( !$arg || !$commands{$arg} ) {
632         print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
633     } else {
634         print "Usage: " .  $commands{$arg}->{doc} . "\n";
635     }
636 }
637
638 sub Quit {
639     if ($::client_id) {
640         print "Disconnecting active session ($::client_id)...";
641         Disconnect($::client_id);
642     }
643     exit;
644 }