Whamcloud - gitweb
Fixes for Linux 2.3 as well as bug fixes after Ayman's testing.
[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     my $packed = pack("L", $::st->rdev());
176     my $rc = ioctl(DEV_OBD, &OBD_IOC_SETUP_SUPER, $packed);
177
178     if (!defined $rc) {
179         print STDERR "ioctl failed: $!\n";
180     } elsif ($rc eq "0 but true") {
181         print "Finished (success)\n";
182     } else {
183         print "ioctl returned error code $rc.\n";
184     }
185 }
186
187 sub Cleanup {
188     my $err = "0";
189     my $rc = ioctl(DEV_OBD, &OBD_IOC_CLEANUP_SUPER, $err);
190
191     if (!defined $rc) {
192         print STDERR "ioctl failed: $!\n";
193     } elsif ($rc eq "0 but true") {
194         print "Finished (success)\n";
195     } else {
196         print "ioctl returned error code $rc.\n";
197     }
198 }
199
200
201 sub Connect {
202     my $id = 0;
203     my $ino = 0;
204     my $bs = 0;
205     my $bs_b = 0;
206     my $rc;
207
208     # unsigned int conn_id;
209     # unsigned long conn_ino;
210     # unsigned long conn_blocksize;
211     # unsigned char conn_blocksize_bits;
212
213     my $packed = pack("ILLC", $id, $ino, $bs, $bs_b);
214     $rc = ioctl(DEV_OBD, &OBD_IOC_CONNECT, $packed);
215     ($id, $ino, $bs, $bs_b) = unpack("ILLC", $packed);
216
217     if (!defined $rc) {
218         print STDERR "ioctl failed: $!\n";
219     } elsif ($rc eq "0 but true") {
220         $::client_id = $id;
221         print "Client ID     : $id\n";
222         print "Root inode    : $ino\n";
223         print "Blocksize     : $bs\n";
224         print "Blocksize bits: $bs_b\n";
225         print "Finished (success)\n";
226     } else {
227         print "ioctl returned error code $rc.\n";
228     }
229 }
230
231 sub Disconnect {
232     my $id = shift;
233
234     if (!defined($id)) {
235         $id = $::client_id;
236     }
237
238     if (!defined($id)) {
239         print "syntax: disconnect [client ID]\n";
240         print "When client ID is not given, the last valid client ID to be returned by a\n";
241         print "connect command this session is used; there is no such ID.\n";
242         return;
243     }
244
245     my $packed = pack("L", $id);
246     my $rc = ioctl(DEV_OBD, &OBD_IOC_DISCONNECT, $packed);
247
248     if (!defined $rc) {
249         print STDERR "ioctl failed: $!\n";
250     } elsif ($rc eq "0 but true") {
251         $::client_id = undef;
252         print "Finished (success)\n";
253     } else {
254         print "ioctl returned error code $rc.\n";
255     }
256 }
257
258 sub Create {
259     my $arg = shift;
260     my $quiet = shift;
261     my $rc;
262
263     if (defined($quiet) && !($quiet eq "quiet")) {
264         print "syntax: create [number of objects [quiet]]\n";
265         return;
266     }
267
268     my $packed = pack("I", $::client_id);
269     if (!defined($arg) || scalar($arg) < 2) {
270         print "Creating 1 object...\n";
271         my $packed = pack("I", 0);
272         $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
273         if (!defined($quiet)) {
274             my $ino = unpack("L", $packed);
275             print "Created object #$ino.\n";
276         }
277     } else {
278         my $i;
279
280         print "Creating " . scalar($arg) . " objects...\n";
281         for ($i = 0; $i < scalar($arg); $i++) {
282             $rc = ioctl(DEV_OBD, &OBD_IOC_CREATE, $packed);
283             my $ino = unpack("L", $packed);
284             if (!($rc eq "0 but true") || $packed == 0) {
285                 last;
286             } elsif (!defined($quiet)) {
287                 print "Created object #$ino.\n";
288             }
289         }
290     }
291
292     if (!defined $rc) {
293         print STDERR "ioctl failed: $!\n";
294     } elsif ($rc eq "0 but true") {
295         print "Finished (success)\n";
296     } else {
297         print "ioctl returned error code $rc.\n";
298     }
299 }
300
301 sub Sync {
302     my $err = "0";
303     my $rc = ioctl(DEV_OBD, &OBD_IOC_SYNC, $err);
304
305     if (!defined $rc) {
306         print STDERR "ioctl failed: $!\n";
307     } elsif ($rc eq "0 but true") {
308         print "Finished (success)\n";
309     } else {
310         print "ioctl returned error code $rc.\n";
311     }
312 }
313
314 sub Destroy {
315     if (!defined($::client_id)) {
316         print "You must first ``connect''.\n";
317         return;
318     }
319
320     my $arg = shift;
321
322     if (!defined($arg) || scalar($arg) < 1) {
323         print "destroy requires the object number to destroy.\n";
324         return;
325     }
326
327     print "Destroying object $arg...\n";
328     my $packed = pack("IL", $::client_id, $arg);
329     my $rc = ioctl(DEV_OBD, &OBD_IOC_DESTROY, $packed);
330
331     if (!defined $rc) {
332         print STDERR "ioctl failed: $!\n";
333     } elsif ($rc eq "0 but true") {
334         print "Finished (success)\n";
335     } else {
336         print "ioctl returned error code $rc.\n";
337     }
338 }
339
340 sub Getattr {
341     if (!defined($::client_id)) {
342         print "You must first ``connect''.\n";
343         return;
344     }
345
346     my $inode = shift;
347
348     if (!defined($inode) || scalar($inode) < 1) {
349         print "invalid arguments; type \"help getattr\" for a synopsis\n";
350         return;
351     }
352
353     # see Setattr
354     my $packed = pack("ILsx2lLLLI", $::client_id, $inode, 0, 0, 0, 0, 0, 0, 0,
355                       0);
356     my $rc = ioctl(DEV_OBD, &OBD_IOC_GETATTR, $packed);
357
358     if (!defined $rc) {
359         print STDERR "ioctl failed: $!\n";
360     } elsif ($rc eq "0 but true") {
361         my ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags);
362         ($valid, $mode, $uid, $gid, $size, $atime, $mtime, $ctime, $flags) =
363           unpack("ISssx2lLLLI", $packed);
364
365         printf("Inode: %d  Mode:  %o\n", $inode, $mode);
366         printf("User: %6d   Group: %6d   Size: %d\n", $uid, $gid, $size);
367         printf("ctime: %08lx -- %s\n", $ctime, scalar(gmtime($ctime)));
368         printf("atime: %08lx -- %s\n", $atime, scalar(gmtime($atime)));
369         printf("mtime: %08lx -- %s\n", $mtime, scalar(gmtime($mtime)));
370         printf("flags: %08x\n", $flags);
371         print "Finished (success)\n";
372     } else {
373         print "ioctl returned error code $rc.\n";
374     }
375 }
376
377 sub Setattr {
378     if (!defined($::client_id)) {
379         print "You must first ``connect''.\n";
380         return;
381     }
382
383     my $inode = shift;
384     my $valid = 0;
385     my $mode = oct(shift);
386     my $uid = shift;
387     my $gid = shift;
388     my $size = shift;
389     my $atime = shift;
390     my $mtime = shift;
391     my $ctime = shift;
392
393     if (defined($uid)) {
394         $valid |= &ATTR_UID;
395     }
396     if (defined($gid)) {
397         $valid |= &ATTR_GID;
398     }
399     if (defined($size)) {
400         $valid |= &ATTR_SIZE;
401     }
402     if (defined($atime)) {
403         $valid |= &ATTR_ATIME;
404     }
405     if (defined($mtime)) {
406         $valid |= &ATTR_MTIME;
407     }
408     if (defined($ctime)) {
409         $valid |= &ATTR_CTIME;
410     }
411     if (defined($mode)) {
412         $valid |= &ATTR_MODE;
413     }
414
415     if (!defined($inode) || scalar($inode) < 1) {
416         print "invalid arguments; type \"help setattr\" for a synopsis\n";
417         return;
418     }
419
420     #struct iattr {
421     #        unsigned int    ia_valid; (32)
422     #        umode_t         ia_mode; (16)
423     #        uid_t           ia_uid; (16)
424     #        gid_t           ia_gid; (16)
425     # -- 16 bit alignment here! --
426     #        off_t           ia_size; (32)
427     #        time_t          ia_atime; (32)
428     #        time_t          ia_mtime; (32)
429     #        time_t          ia_ctime; (32)
430     #        unsigned int    ia_attr_flags; (32)
431     #};
432
433     printf "valid is %x, mode is %o\n", $valid, $mode;
434     my $packed = pack("ILLSssx2ILLLL", $::client_id, $inode, $valid, $mode,
435                       $uid, $gid, $size, $atime, $mtime, $ctime, 0);
436     my $rc = ioctl(DEV_OBD, &OBD_IOC_SETATTR, $packed);
437
438     if (!defined $rc) {
439         print STDERR "ioctl failed: $!\n";
440     } elsif ($rc eq "0 but true") {
441         print "Finished (success)\n";
442     } else {
443         print "ioctl returned error code $rc.\n";
444     }
445 }
446
447 sub Read {
448     if (!defined($::client_id)) {
449         print "You must first ``connect''.\n";
450         return;
451     }
452
453     my $inode = shift;
454     my $count = shift;
455     my $offset = shift;
456   
457     if (!defined($inode) || scalar($inode) < 1 || !defined($count) ||
458         $count < 1 || (defined($offset) && $offset < 0)) {
459         print "invalid arguments; type \"help read\" for a synopsis\n";
460         return;
461     }
462
463     if (!defined($offset)) {
464         $offset = 0;
465     }
466
467     print("Reading $count bytes starting at byte $offset from object " .
468           "$inode...\n");
469
470     # "allocate" a large enough buffer
471     my $buf = sprintf("%${count}s", " ");
472     die "suck" if (length($buf) != $count);
473
474     # the perl we're using doesn't support pack type Q, and offset is 64 bits
475     my $packed = pack("ILpLLL", $::client_id, $inode, $buf, $count, 0, $offset);
476
477     my $rc = ioctl(DEV_OBD, &OBD_IOC_READ, $packed);
478
479     $retval = unpack("l", $packed);
480
481     if (!defined $rc) {
482         print STDERR "ioctl failed: $!\n";
483     } elsif ($rc eq "0 but true") {
484         if ($retval >= 0) {
485                 print substr($buf, 0, $retval);
486                 print "\nRead $retval of an attempted $count bytes.\n";
487                 print "Finished (success)\n";
488         } else {
489                 print "Finished (error $retval)\n";
490         }
491     } else {
492         print "ioctl returned error code $rc.\n";
493     }
494 }
495
496 sub Write {
497     if (!defined($::client_id)) {
498         print "You must first ``connect''.\n";
499         return;
500     }
501
502     my $inode = shift;
503     my $offset = shift;
504     my $text = join(' ', @_);
505     my $count = length($text);
506
507     if (!defined($inode) || scalar($inode) < 1 || !defined($offset) ||
508         scalar($offset) < 0) {
509         print "invalid arguments; type \"help write\" for a synopsis\n";
510         return;
511     }
512
513     if (!defined($text)) {
514         $text = "";
515         $count = 0;
516     }
517
518     print("Writing $count bytes starting at byte $offset to object " .
519           "$inode...\n");
520
521     # the perl we're using doesn't support pack type Q
522     my $packed = pack("ILpLLL", $::client_id, $inode, $text, $count, 0, $offset);
523     my $rc = ioctl(DEV_OBD, &OBD_IOC_WRITE, $packed);
524
525     $retval = unpack("l", $packed);
526
527     if (!defined $rc) {
528         print STDERR "ioctl failed: $!\n";
529     } elsif ($rc eq "0 but true") {
530         if ($retval >= 0) {
531                 print "\nWrote $retval of an attempted $count bytes.\n";
532                 print "Finished (success)\n";
533         } else {
534                 print "Finished (error $retval)\n";
535         }
536     } else {
537         print "ioctl returned error code $rc.\n";
538     }
539 }
540
541 sub Preallocate {
542     my $arg = shift;
543
544     if (!defined($::client_id)) {
545         print "You must first ``connect''.\n";
546         return;
547     }
548
549     if (!defined($arg) || scalar($arg) < 1 || scalar($arg) > 32) {
550         $arg = 32;
551     }
552
553     print "Preallocating $arg inodes...\n";
554     my $packed = pack("LLx128", $::client_id, $arg);
555     # client id, alloc, inodes[32]
556
557     my $rc = ioctl(DEV_OBD, &OBD_IOC_PREALLOCATE, $packed);
558
559     if (!defined $rc) {
560         print STDERR "ioctl failed: $!\n";
561     } elsif ($rc eq "0 but true") {
562         my $alloc = unpack("x4L", $packed);
563         my @inodes = unpack("x8L32", $packed);
564         my $i;
565
566         print "Got $alloc inodes: ";
567         foreach $i (@inodes) {
568             print $i . " ";
569         }
570         print "\nFinished (success)\n";
571     } else {
572         print "ioctl returned error code $rc.\n";
573     }
574 }
575
576 sub Decusecount {
577     my $rc = ioctl(DEV_OBD, &OBD_IOC_DEC_USE_COUNT, 0);
578
579     if (!defined $rc) {
580         print STDERR "ioctl failed: $!\n";
581     } elsif ($rc eq "0 but true") {
582         print "Finished (success)\n";
583     } else {
584         print "ioctl returned error code $rc.\n";
585     }
586 }
587
588 sub Statfs {
589     if (!defined($::client_id)) {
590         print "You must first ``connect''.\n";
591         return;
592     }
593
594     # struct statfs {
595     #         long f_type;
596     #         long f_bsize;
597     #         long f_blocks;
598     #         long f_bfree;
599     #         long f_bavail;
600     #         long f_files;
601     #         long f_ffree;
602     #         __kernel_fsid_t f_fsid; (64 bits)
603     #         long f_namelen;
604     #         long f_spare[6];
605     # };
606
607     my $packed = pack("LLLLLLLIILL6", $::client_id, 0, 0, 0, 0, 0, 0, 0, 0, 0,
608                       0, 0, 0, 0, 0, 0);
609
610     my $rc = ioctl(DEV_OBD, &OBD_IOC_STATFS, $packed);
611
612     if (!defined $rc) {
613         print STDERR "ioctl failed: $!\n";
614     } elsif ($rc eq "0 but true") {
615         # skip both the conn_id and the fs_type in the buffer
616         my ($bsize, $blocks, $bfree, $bavail, $files, $ffree) =
617             unpack("x4x4LLLLLL", $packed);
618         print("$bsize byte blocks: $blocks, " . ($blocks - $bfree) . " used, " .
619               "$bfree free ($bavail available).\n");
620         print "$files files, " . ($files - $ffree) . " used, $ffree free.\n";
621         print "Finished (success)\n";
622     } else {
623         print "ioctl returned error code $rc.\n";
624     }
625 }
626
627 sub Help {
628     my $arg = shift;
629
630     if ( !$arg || !$commands{$arg} ) {
631         print "Comands: ", join( ' ', @jcm_cmd_list), "\n";
632     } else {
633         print "Usage: " .  $commands{$arg}->{doc} . "\n";
634     }
635 }
636
637 sub Quit {
638     if ($::client_id) {
639         print "Disconnecting active session ($::client_id)...";
640         Disconnect($::client_id);
641     }
642     exit;
643 }