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