- Added patch my Witold Kowolik to allow enable/disable of qla_isp cards.
[mirror/scst/.git] / scstadmin / scstadmin
1 #!/usr/bin/perl
2 $Version  = 'SCST Configurator v1.0.3';
3
4 # Configures SCST
5 #
6 # Author:       Mark R. Buechler
7 # License:      GPLv2
8 # Copyright (c) 2005-2007 Mark R. Buechler
9
10 sub usage
11   {
12     die <<"EndUsage";
13 $Version
14
15 Usage:
16 General Operations
17      -config <config>     : Configure SCST given the specified configuration file.
18      -ClearConfig         : Clear all SCST configuration.
19      -WriteConfig <file>  : Writes the current configuration out to the specified file.
20      -checkConfig <file>  : Checks the saved configuration in the specified file.
21      -sessions            : List current initiator sessions.
22      
23 Target Driver Operations
24      -enable <wwn|host>   : Enable target mode for driver at specified WWN or host.
25      -disable <wwn|host>  : Disable target mode for driver at specified WWN or host.
26
27 Devices Operations
28      -adddev <device>     : Adds a device to a handler.
29          -handler <handler>
30          -path <path>
31          -options <options>
32          -blocksize <bytes>
33      -RemoveDev <device>  : Remove a device from a handler.
34          -handler <handler>
35  
36 Users Operations
37      -adduser <user>      : Adds a user to a security group.
38          -group <group>
39      -RemoveUser <user>   : Delete a user from a security group.
40          -group <group>
41      -ClearUsers          : Clear all users from a given security group.
42          -group <group>
43
44 Groups Operations
45      -addgroup <group>    : Add a given group to available security groups.
46      -RemoveGroup <group> : Remove a give group from available security groups.
47      
48 Assignments Operations
49      -assigndev <device>  : Assign a given device to a security group.
50          -group <group>
51          -lun <lun>
52      -ReleaseDev <device> : Remove a given device from a security group.
53          -group <group>
54      -ClearDevs           : Clear all device assignments for a security group.
55          -group <group>
56
57 Options
58      -ForceConfig         : Force all confuration changes, even deletions (DANGER!).
59   
60 Debugging (limited support)
61      -debug               : Debug mode - don\'t do anything destructive.
62
63 Available Handlers:
64       disk, vdisk, disk_perf, cdrom, vcdrom, changer, modisk, modisk_perf, tape, tape_perf
65
66 Available Options for create and open:
67       WRITE_THROUGH, READ_ONLY, O_DIRECT, NULLIO, NV_CACHE, BLOCK_IO, REMOVABLE
68
69 Examples:
70      Enable target mode for fibre card specifying its WWN
71        scstadmin -enable 50:06:0B:00:00:39:71:78
72
73      Disable target mode for SCSI host specifying host number
74        scstadmin -disable host4 
75
76      Create a new security group:
77        scstadmin -addgroup HOST01
78        
79      Create a device given an already existing disk file:
80        scstadmin -adddev DISK01 -handler vdisk -path /vdisks/disk01.dsk -options READ_ONLY,WRITE_THROUGH
81        
82      Assign a device to a security group:
83        scstadmin -assigndev DISK01 -group HOST01 -lun 1
84
85 EndUsage
86   }
87
88 use SCST::SCST;
89 use Getopt::Long;
90 use IO::File;
91 use IO::Dir;
92 use POSIX;
93 use strict;
94
95 my $_DEF_CONFIG_ = '/etc/scst.conf';
96
97 my $TRUE  = 1;
98 my $FALSE = 0;
99
100 my $_MAX_LUNS_       = 255;
101 my $_DEFAULT_GROUP_  = 'Default';
102
103 my $_SCSI_CLASS_     = '/sys/class/scsi_host';
104 my $_FC_CLASS_       = '/sys/class/fc_host';
105 my $_SCSI_ISP_       = '/proc/scsi/isp';
106 my $_SCSITGT_QLAISP_ = '/proc/scsi_tgt/qla_isp';
107
108 my $SCST;
109 my $DEVICES;
110 my $TARGETS;
111 my %USERS;
112 my %ASSIGNMENTS;
113 my %HANDLERS;
114 my %GROUPS;
115 my $_DEBUG_;
116
117 my %_HANDLER_MAP_ = ('cdrom' => $SCST::SCST::CDROM_TYPE,
118                      'changer' => $SCST::SCST::CHANGER_TYPE,
119                      'disk' => $SCST::SCST::DISK_TYPE,
120                      'vdisk' => $SCST::SCST::VDISK_TYPE,
121                      'vcdrom' => $SCST::SCST::VCDROM_TYPE,
122                      'disk_perf' => $SCST::SCST::DISKPERF_TYPE,
123                      'modisk' => $SCST::SCST::MODISK_TYPE,
124                      'modisk_perf' => $SCST::SCST::MODISKPERF_TYPE,
125                      'tape' => $SCST::SCST::TAPE_TYPE,
126                      'tape_perf' => $SCST::SCST::TAPEPERF_TYPE,
127                      'processor' => $SCST::SCST::PROCESSOR_TYPE,
128                 # Add in the dev_ names as well
129                      'dev_cdrom' => $SCST::SCST::CDROM_TYPE,
130                      'dev_changer' => $SCST::SCST::CHANGER_TYPE,
131                      'dev_disk' => $SCST::SCST::DISK_TYPE,
132                      'dev_disk_perf' => $SCST::SCST::DISKPERF_TYPE,
133                      'dev_modisk' => $SCST::SCST::MODISK_TYPE,
134                      'dev_modisk_perf' => $SCST::SCST::MODISKPERF_TYPE,
135                      'dev_tape' => $SCST::SCST::TAPE_TYPE,
136                      'dev_tape_perf' => $SCST::SCST::TAPEPERF_TYPE,
137                      'dev_processor' => $SCST::SCST::PROCESSOR_TYPE);
138                      
139 my %_REVERSE_MAP_ = ($SCST::SCST::CDROM_TYPE => 'cdrom',
140                      $SCST::SCST::CHANGER_TYPE => 'changer',
141                      $SCST::SCST::DISK_TYPE => 'disk',
142                      $SCST::SCST::VDISK_TYPE => 'vdisk',
143                      $SCST::SCST::VCDROM_TYPE => 'vcdrom',
144                      $SCST::SCST::DISKPERF_TYPE => 'disk_perf',
145                      $SCST::SCST::MODISK_TYPE => 'modisk',
146                      $SCST::SCST::MODISKPERF_TYPE => 'modisk_perf',
147                      $SCST::SCST::TAPE_TYPE => 'tape',
148                      $SCST::SCST::TAPEPERF_TYPE => 'tape_perf',
149                      $SCST::SCST::PROCESSOR_TYPE => 'processor');
150
151 my %_HANDLER_TYPE_MAP_ = ($SCST::SCST::IOTYPE_PHYSICAL => 'physical',
152                           $SCST::SCST::IOTYPE_VIRTUAL => 'virtual',
153                           $SCST::SCST::IOTYPE_PERFORMANCE => 'performance');
154
155 $SIG{INT} = \&commitSuicide;
156
157 use vars qw($Version);
158
159 POSIX::setsid();
160
161 &main();
162
163 sub getArgs {
164         my $applyConfig;
165         my $forceConfig;
166         my $clearConfig;
167         my $writeConfig;
168         my $checkConfig;
169         my $showSessions;
170         my $addDev;
171         my $devPath;
172         my $removeDev;
173         my $addUser;
174         my $removeUser;
175         my $clearUsers;
176         my $addGroup;
177         my $removeGroup;
178         my $assignDev;
179         my $releaseDev;
180         my $clearDevs;
181         my $devLun;
182         my $handler;
183         my $group;
184         my $options;
185         my $blocksize;
186         my $enable;
187         my $disable;
188
189         my $p = new Getopt::Long::Parser;
190
191         if (!$p->getoptions('config:s'          => \$applyConfig,
192                             'ClearConfig'       => \$clearConfig,
193                             'ForceConfig'       => \$forceConfig,
194                             'WriteConfig=s'     => \$writeConfig,
195                             'checkConfig=s'     => \$checkConfig,
196                             'sessions'          => \$showSessions,
197                             'adddev=s'          => \$addDev,
198                             'path=s'            => \$devPath,
199                             'RemoveDev=s'       => \$removeDev,
200                             'lun=s'             => \$devLun,
201                             'adduser=s'         => \$addUser,
202                             'RemoveUser=s'      => \$removeUser,
203                             'ClearUsers'        => \$clearUsers,
204                             'addgroup=s'        => \$addGroup,
205                             'RemoveGroup=s'     => \$removeGroup,
206                             'assigndev=s'       => \$assignDev,
207                             'ReleaseDev=s'      => \$releaseDev,
208                             'ClearDevs'         => \$clearDevs,
209                             'handler=s'         => \$handler,
210                             'group=s'           => \$group,
211                             'options=s'         => \$options,
212                             'blocksize=s'       => \$blocksize,
213                             'enable=s'          => \$enable,
214                             'disable=s'         => \$disable,
215                             'debug'             => \$_DEBUG_)) {
216                 &usage();
217         }
218
219         if ((defined($enable) && !$enable) || (defined($disable) && !$disable)) {
220                 print "Argument -enable/-disable requires a WWN or host.\n\n";
221                 usage();
222         }
223
224         if ($handler && !$_HANDLER_MAP_{$handler}) {
225                 print "Invalid handler '$handler' specified. Available handlers are:\n\n";
226                 foreach my $_handler (keys %_HANDLER_MAP_) {
227                         print "\t$_handler\n";
228                 }
229                 print "\n";
230                 exit 1;
231         }
232
233         if ($addDev && !($handler && $devPath)) {
234                 print "Please specify -handler and -path with -adddev.\n\n";
235                 usage();
236         }
237
238         if (defined($blocksize) && !$blocksize) {
239                 print "Please specify bytes with -blocksize.\n\n";
240                 usage();
241         }
242
243         if ($blocksize && !$addDev) {
244                 print "Please specify -adddev with -blocksize.\n";
245                 usage();
246         }
247
248         if (defined($forceConfig) && !defined($applyConfig)) {
249                 print "Please specify -config with -ForceConfig.\n\n";
250                 usage();
251         }
252
253         if ($removeDev && !$handler) {
254                 print "Please specify -handler with -RemoveDev.\n\n";
255                 usage();
256         }
257
258         if ($addUser && !defined($group)) {
259                 print "Please specify -group with -adduser.\n\n";
260                 usage();
261         }
262
263         if ($removeUser && !defined($group)) {
264                 print "Please specify -group with -RemoveUser.\n\n";
265                 usage();
266         }
267
268         if ($clearUsers && !defined($group)) {
269                 print "Please specify -group with -ClearUsers.\n\n";
270                 usage();
271         }
272
273         if ($assignDev && !(defined($group) && defined($devLun))) {
274                 print "Please specify -group and -lun with -assigndev.\n\n";
275                 usage();
276         }
277
278         if ($releaseDev && !defined($group)) {
279                 print "Please specify -group with -ReleaseDev.\n\n";
280                 usage();
281         }
282
283         if ($clearDevs && !defined($group)) {
284                 print "Please specify -group with -ClearDevs.\n\n";
285                 usage();
286         }
287
288         if (defined($writeConfig) && !$writeConfig) {
289                 print "Please specify a file name to write configuration to..\n\n";
290                 usage();
291         }
292
293         $_DEBUG_ = $TRUE if (defined($_DEBUG_));
294
295         $forceConfig = $TRUE if (defined($forceConfig));
296         $showSessions = $TRUE if (defined($showSessions));
297
298         $enable =~ tr/A-Z/a-z/; $disable =~ tr/A-Z/a-z/;
299         $options =~ tr/a-z/A-Z/ if ($options);
300
301         if ((defined($showSessions) + defined($addDev) + defined($removeDev) +
302              defined($addUser) + defined($enable) + defined($disable) +
303              defined($removeUser) + defined($clearUsers) + defined($assignDev) +
304              defined($releaseDev) + defined($clearDevs) + defined($applyConfig) +
305              defined($clearConfig) + defined($writeConfig) + defined($checkConfig)) > 1) {
306                 print "Please specify only one operation at a time.\n";
307                 usage();
308         }
309
310         $applyConfig = $_DEF_CONFIG_ if (defined($applyConfig) && !$applyConfig);
311         $checkConfig = $_DEF_CONFIG_ if (defined($checkConfig) && !$checkConfig);
312
313         return ($enable, $disable, $addDev, $devPath, $devLun, $removeDev, $addUser, $removeUser,
314                 $clearUsers, $addGroup, $removeGroup, $assignDev, $releaseDev, $clearDevs,
315                 $handler, $group, $options, $blocksize, $applyConfig, $forceConfig,
316                 $clearConfig, $writeConfig, $checkConfig, $showSessions);
317 }
318
319 sub main {
320         my $rc;
321
322         STDOUT->autoflush(1);
323
324         # We need to run as root
325         if ( $> ) {die("This program must run as root.\n");}
326
327         my ($enable, $disable, $addDev, $devPath, $devLun, $removeDev, $addUser, $removeUser,
328             $clearUsers, $addGroup, $removeGroup, $assignDev, $releaseDev, $clearDevs,
329             $handler, $group, $options, $blocksize, $applyConfig, $forceConfig,
330             $clearConfig, $writeConfig, $checkConfig, $showSessions) = getArgs();
331
332         $SCST = new SCST::SCST($_DEBUG_);
333
334         readWorkingConfig();
335
336         SWITCH: {
337                 $applyConfig && do {
338                         if ($forceConfig) {
339                                 $rc = applyConfiguration($applyConfig, $FALSE, $TRUE);
340                                 die("Configuration errors found, aborting.\n") if ($rc);
341
342                                 print "\nConfiguration will apply in 10 seconds, type ctrl-c to abort..\n";
343                                 sleep 10;
344                         }
345
346                         readWorkingConfig();
347                         $rc = applyConfiguration($applyConfig, $forceConfig, $FALSE);
348                         last SWITCH;
349                 };
350                 $checkConfig && do {
351                         $rc = applyConfiguration($checkConfig, $FALSE, $TRUE);
352                         last SWITCH;
353                 };
354                 $writeConfig && do {
355                         $rc = writeConfiguration($writeConfig);
356                         last SWITCH;
357                 };
358                 $showSessions && do {
359                         $rc = showSessions();
360                         last SWITCH;
361                 };
362                 defined($clearConfig) && do {
363                         $rc = clearConfiguration();
364                         last SWITCH;
365                 };
366                 $addDev && do {
367                         $rc = addDevice($handler, $addDev, $devPath, $options, $blocksize);
368                         last SWITCH;
369                 };
370                 $removeDev && do {
371                         $rc = removeDevice($handler, $removeDev);
372                         last SWITCH;
373                 };
374                 $addUser && do {
375                         $rc = addUser($group, $addUser);
376                         last SWITCH;
377                 };
378                 $removeUser && do {
379                         $rc = removeUser($group, $removeUser);
380                         last SWITCH;
381                 };
382                 defined($clearUsers) && do {
383                         $rc = clearUsers($group);
384                         last SWITCH;
385                 };
386                 $addGroup && do {
387                         $rc = addGroup($addGroup);
388                         last SWITCH;
389                 };
390                 $removeGroup && do {
391                         $rc = removeGroup($removeGroup);
392                         last SWITCH;
393                 };
394                 $assignDev && do {
395                         $rc = assignDevice($group, $assignDev, $devLun);
396                         last SWITCH;
397                 };
398                 $releaseDev && do {
399                         $rc = releaseDevice($group, $releaseDev);
400                         last SWITCH;
401                 };
402                 defined($clearDevs) && do {
403                         $rc = clearDevices($group);
404                         last SWITCH;
405                 };
406                 $enable && do {
407                         $enable = unformatTarget($enable);
408                         $rc = enableTarget($enable, $TRUE);
409                         last SWITCH;
410                 };
411                 $disable && do {
412                         $disable = unformatTarget($disable);
413                         $rc = enableTarget($disable, $FALSE);
414                         last SWITCH;
415                 };
416
417                 print "No valid operations specified.\n";
418                 usage();
419                 exit $TRUE;
420         }
421
422         print "All done.\n";
423
424         exit $rc;
425 }
426
427 sub readWorkingConfig {
428         my %empty;
429
430         print "Collecting current configuration.. ";
431
432         $TARGETS  = undef;
433         $DEVICES  = undef;
434         %HANDLERS = ();
435         %GROUPS   = ();
436         %USERS    = ();
437
438         my $eHandlers = $SCST->handlers();
439
440         immediateExit($SCST->errorString());
441
442         foreach my $handler (@{$eHandlers}) {
443                 $HANDLERS{$handler}++; # For quick lookups
444         }
445
446         $TARGETS = targets();
447
448         $DEVICES = $SCST->devices();
449         immediateExit($SCST->errorString());
450
451         my $_eGroups = $SCST->groups();
452         immediateExit($SCST->errorString());
453
454         foreach my $group (@{$_eGroups}) {
455                 $GROUPS{$group}++;
456                 $ASSIGNMENTS{$group} = $SCST->groupDevices($group);
457                 my $eUsers = $SCST->users($group);
458
459                 foreach my $user (@{$eUsers}) {
460                         $USERS{$group}->{$user}++; # For quick lookups
461                 }
462                 $USERS{$group} = \%empty if (!$USERS{$group});
463         }
464
465         print "done.\n\n";
466 }
467
468 sub writeConfiguration {
469         my $file = shift;
470
471         if (-f $file) {
472                 if (!unlink $file) {
473                         print "Failed to save current configuration, specified ".
474                           "file exists and cannot be deleted.\n";
475                         return 1;
476                 }
477         }
478
479         my $io = new IO::File $file, O_CREAT|O_WRONLY;
480
481         if (!$io) {
482                 print "Failed to save configuration to file '$file': $!\n";
483                 return 1;
484         }
485
486         print "Writing current configuration to file '$file'.. ";
487
488         print $io "# Automatically generated by $Version.\n\n";
489
490         # Device information
491         foreach my $handler (sort keys %HANDLERS) {
492                 print $io "[HANDLER ".$_REVERSE_MAP_{$handler}."]\n";
493
494                 if ($SCST->handlerType($handler) == $SCST::SCST::IOTYPE_VIRTUAL) {
495                         print $io "#DEVICE <vdisk name>,<device path>";
496                         if ($handler == $SCST::SCST::VDISK_TYPE) {
497                                 print $io ",<options>,<block size>\n";
498                         } else {
499                                 print $io "\n";
500                         }
501                 } else {
502                         print $io "#DEVICE <H:C:I:L>\n";
503                 }
504
505                 my $devices = $SCST->handlerDevices($handler);
506
507                 immediateExit($SCST->errorString());
508
509                 foreach my $device (sort keys %{$devices}) {
510                         my $options = $$devices{$device}->{'OPTIONS'};
511
512                         $options =~ s/\,/\|/g;
513
514                         print $io "DEVICE $device,".$$devices{$device}->{'PATH'};
515                         print $io ",$options";
516                         print $io ",".$$devices{$device}->{'BLOCKSIZE'};
517                         print $io "\n";
518                 }
519
520                 print $io "\n";
521         }
522
523         # User configuration
524         foreach my $group (sort keys %USERS) {
525                 print $io "[GROUP $group]\n";
526                 print $io "#USER <user wwn>\n";
527
528                 foreach my $user (keys %{$USERS{$group}}) {
529                         print $io "USER $user\n";
530                 }
531
532                 print $io "\n";
533         }
534
535         # Assignments configuration
536         foreach my $group (sort keys %ASSIGNMENTS) {
537                 print $io "[ASSIGNMENT $group]\n";
538                 print $io "#DEVICE <device name>,<lun>\n";
539
540                 my $pointer = $ASSIGNMENTS{$group};
541                 foreach my $device (sort keys %{$pointer}) {
542                         print $io "DEVICE $device,".$$pointer{$device}."\n";
543                 }
544
545                 print $io "\n";
546         }
547
548         # Targets configuration
549         foreach my $type ('enable', 'disable') {
550                 print $io "[TARGETS $type]\n";
551                 print $io "#HOST <wwn identifier>\n";
552
553                 foreach my $target (sort keys %{$TARGETS}) {
554                         if ((($type eq 'enable') && $$TARGETS{$target}->{'enabled'}) ||
555                             (($type eq 'disable') && !$$TARGETS{$target}->{'enabled'})) {
556                                 my $f_target = formatTarget($target);
557                                 print $io "HOST $f_target\n" if (!$$TARGETS{$target}->{'duplicate'});
558                         }
559                 }
560
561                 print $io "\n";
562         }
563
564         print "done\n";
565
566         close $io;
567
568         return 0;
569 }
570
571 sub applyConfiguration {
572         my $confile = shift;
573         my $force = shift;
574         my $check = shift;
575         my $config = readConfig($confile);
576         my $errs;
577         my $changes = 0;
578
579         my %used_devs;
580         my %used_users;
581         my %used_assignments;
582         my %empty;
583
584         # Cache device/handler configuration
585         foreach my $entry (keys %{$$config{'HANDLER'}}) {
586                 foreach my $device (@{$$config{'HANDLER'}->{$entry}->{'DEVICE'}}) {
587                         my($vname, undef) = split(/\,/, $device, 2);
588                         $vname = cleanupString($vname);
589                         $used_devs{$vname} = $entry;
590                 }
591         }
592
593         # Cache user/group configuration
594         foreach my $group (keys %{$$config{'GROUP'}}) {
595                 foreach my $user (@{$$config{'GROUP'}->{$group}->{'USER'}}) {
596                         $used_users{$group}->{$user}++;
597                 }
598                 $used_users{$group} = \%empty if (!$used_users{$group});
599         }
600
601         # Cache device association configuration
602         foreach my $group (keys %{$$config{'ASSIGNMENT'}}) {
603                 foreach my $device (@{$$config{'ASSIGNMENT'}->{$group}->{'DEVICE'}}) {
604                         my($vname, $lun) = split(/\,/, $device);
605                         $vname = cleanupString($vname);
606                         $used_assignments{$group}->{$vname} = $lun;
607                 }
608         }
609
610         # If -ForceConfig is used, check for configurations which we've deleted but are still active.
611         if ($force || $check) {
612                 # Associations
613                 foreach my $group (sort keys %ASSIGNMENTS) {
614                         if (!defined($used_assignments{$group}) && (keys %{$ASSIGNMENTS{$group}})) {
615                                 print "\t-> WARNING: Group '$group' has no associations in saved configuration";
616
617                                 if (!$check) {
618                                         print ", clearing all associations.\n";
619                                         if (clearDevices($group)) {
620                                                 $errs++;
621                                         } else {
622                                                 $changes++;
623                                         }
624                                 } else {
625                                         print ".\n";
626                                         $changes++;
627                                 }
628                         } else {
629                                 my $_assignments = $ASSIGNMENTS{$group};
630
631                                 foreach my $device (sort keys %{$_assignments}) {
632                                         if (!defined($used_assignments{$group}->{$device}) ||
633                                            ($$_assignments{$device} != $used_assignments{$group}->{$device})) {
634                                                 if ($$_assignments{$device} != $used_assignments{$group}->{$device}) {
635                                                         print "\t-> WARNING: Device '$device' assigned to group '$group' is at LUN ".
636                                                           $used_assignments{$group}->{$device}.
637                                                           " whereas working configuration reflects LUN ".$$_assignments{$device}; 
638                                                 } else {
639                                                         print "\t-> WARNING: Device '$device' is not associated with group ".
640                                                           "'$group' in saved configuration";
641                                                 }
642
643                                                 if (!$check) {
644                                                         print ", releasing.\n";
645                                                         if (releaseDevice($group, $device)) {
646                                                                 $errs++;
647                                                         } else {
648                                                                 $changes++;
649                                                         }
650                                                 } else {
651                                                         print ".\n";
652                                                         $changes++;
653                                                 }
654                                         }
655                                 }
656                         }
657                 }
658
659                 # Users & Groups
660                 foreach my $group (sort keys %USERS) {
661                         if (!defined($used_users{$group})) {
662                                 print "\t-> WARNING: Group '$group' does not exist in saved configuration";
663
664                                 if (!$check) {
665                                         print ", removing.\n";
666                                         if (clearUsers($group)) {
667                                                 $errs++;
668                                         } else {
669                                                 $changes++;
670                                         }
671
672                                         if (removeGroup($group)) {
673                                                 $errs++;
674                                         } else {
675                                                 $changes++;
676                                         }
677                                 } else {
678                                         print ".\n";
679                                         $changes++;
680                                 }
681                         } else {
682                                 foreach my $user (sort keys %{$USERS{$group}}) {
683                                         if (!defined($used_users{$group}->{$user})) {
684                                                 print "\t-> WARNING: User '$user' is not defined as part of group '$group' ".
685                                                   "in saved configuration";
686
687                                                 if (!$check) {
688                                                         print ", removing.\n";
689                                                         if (removeUser($group, $user)) {
690                                                                 $errs++;
691                                                         } else {
692                                                                 $changes++;
693                                                         }
694                                                 } else {
695                                                         print ".\n";
696                                                         $changes++;
697                                                 }
698                                         }
699                                 }
700                         }
701                 }
702
703                 # Devices
704                 foreach my $device (sort keys %{$DEVICES}) {
705                         if ($$DEVICES{$device} && !defined($used_devs{$device})) {
706                                 # Device gone, but is it still assigned to a group?
707                                 my $isAssigned = $FALSE;
708                                 foreach my $group (sort keys %used_assignments) {
709                                         if (defined($used_assignments{$group}->{$device})) {
710                                                 print "\t-> WARNING: Device '$device' is not defined in saved configuration, ".
711                                                   "however, it is still assigned to group '$group'! Ignoring removal.\n";
712                                                 $isAssigned = $TRUE;
713                                         }
714                                 }
715
716                                 if (!$isAssigned && ($SCST->handlerType($$DEVICES{$device}) == $SCST::SCST::IOTYPE_VIRTUAL)) {
717                                         print "\t-> WARNING: Device '$device' is not defined in saved configuration";
718
719                                         if (!$check) {
720                                                 print ", removing.\n";
721                                                 if (removeDevice($device)) {
722                                                         $errs++;
723                                                 } else {
724                                                         $changes++;
725                                                 }
726                                         } else {
727                                                 print ".\n";
728                                                 $changes++;
729                                         }
730                                 }
731                         } else {
732                                 # Handler change
733                                 if ($_HANDLER_MAP_{$used_devs{$device}} != $$DEVICES{$device}) {
734                                         my $handler = $used_devs{$device};
735
736                                         if ($HANDLERS{$_HANDLER_MAP_{$handler}}) {
737                                                 print "\t-> WARNING: Device '$device' changes handler to '$handler'";
738
739                                                 if (!$check) {
740                                                         print ", changing.\n";
741                                                         if ($SCST->assignDeviceToHandler($device,
742                                                                                          $_HANDLER_MAP_{$handler})) {
743                                                                 $errs++;
744                                                         } else {
745                                                                 $changes++;
746                                                         }
747                                                 } else {
748                                                         print ".\n";
749                                                         $changes++;
750                                                 }
751                                         }
752                                 }
753                         }
754                 }
755         }
756
757         print "Applying configuration additions..\n" if (!$check);
758         print "\n";
759
760         readWorkingConfig() if ($force);
761
762         foreach my $_handler (sort keys %{$$config{'HANDLER'}}) {
763                 if (!$HANDLERS{$_HANDLER_MAP_{$_handler}}) {
764                         print "\t-> WARNING: Handler '$_handler' does not exist.\n";
765                         $errs += 1;
766                         next;
767                 }
768
769                 foreach my $device (@{$$config{'HANDLER'}->{$_handler}->{'DEVICE'}}) {
770                         my($vname, $path, $options, $blocksize) = split(/\,/, $device);
771                         $path = cleanupString($path);
772                         $options =~ s/\s+//g;
773
774                         if (defined($$DEVICES{$vname}) && ($_HANDLER_MAP_{$_handler} == $$DEVICES{$vname})) {
775                                 next;
776                         } elsif (defined($$DEVICES{$vname}) && ($_HANDLER_MAP_{$_handler} != $$DEVICES{$vname})) {
777                                 if ($HANDLERS{$_HANDLER_MAP_{$_handler}}) {
778                                         print "\t-> WARNING: Device '$vname' changes handler from '".
779                                           $_REVERSE_MAP_{$$DEVICES{$vname}}."' to '$_handler'.\n".
780                                           "\t   Use -ForceConfig to change device handler.\n" if (!$force && !$check);
781                                 }
782                                 next;
783                         }
784
785                         if ($check) {
786                                 print "\t-> New device '$_handler:$vname' at path '$path', options '$options', ".
787                                   "blocksize $blocksize.\n";
788                                 $$DEVICES{$vname} = $_HANDLER_MAP_{$_handler};
789                                 $changes++;
790                         } else {
791                                 if (addDevice($_handler, $vname, $path, $options, $blocksize)) {
792                                         $errs++;
793                                 } else {
794                                         $changes++;
795                                 }
796                         }
797                 }
798         }
799
800         # Create new groups and add users..
801         foreach my $group (keys %used_users) {
802                 if (!defined($USERS{$group})) {
803                         if ($check) {
804                                 print "\t-> New group definition '$group.'\n";
805                                 $GROUPS{$group}++;
806                                 $changes++;
807                         } else {
808                                 if (addGroup($group)) {
809                                         $errs++;
810                                 } else {
811                                         $changes++;
812                                 }
813                         }
814                 }
815
816                 foreach my $user (keys %{$used_users{$group}}) {
817                         if (!defined($USERS{$group}->{$user})) {
818                                 if ($check) {
819                                         print "\t-> New user definition '$user' for group '$group'.\n";
820                                         $USERS{$group}->{$user}++;
821                                         $changes++;
822                                 } else {
823                                         if (addUser($group, $user)) {
824                                                 $errs++;
825                                         } else {
826                                                 $changes++;
827                                         }
828                                 }
829                         }
830                 }
831         }
832
833         # Assign new devices to groups..
834         foreach my $group (keys %used_assignments) {
835                 if (!defined($GROUPS{$group})) {
836                         # Looks like we're lacking a group. We'll create an empty one
837
838                         print "\t-> WARNING: Auto-creating an empty group '$group' since none was configured.\n";
839
840                         if (addGroup($group)) {
841                                 $errs++;
842                         } else {
843                                 $changes++;
844                         }
845                 }
846
847                 if (!defined($GROUPS{$group})) {
848                         print "\t-> WARNING: Unable to assign to non-existant group '$group'.\n";
849                         $errs += 1;
850                         next;
851                 }
852
853                 foreach my $vname (keys %{$used_assignments{$group}}) {
854                         my $lun = $used_assignments{$group}->{$vname};
855                         my $_assignments = $ASSIGNMENTS{$group};
856
857                         if (defined($$_assignments{$vname}) && ($$_assignments{$vname} == $lun)) {
858                                 next;
859                         } elsif (defined($$_assignments{$vname}) && ($$_assignments{$vname} != $lun)) {
860                                 print "\t-> Device '$vname' assigned to group '$group' is at LUN ".$$_assignments{$vname}.
861                                   ", whereas the working configuration reflects LUN $lun.\n".
862                                   "\t   Use -ForceConfig to force this LUN change.\n" if (!$force && !$check);
863                         } else {
864                                 if ($check) {
865                                         $lun = 'auto' if (!defined($lun));
866                                         print "\t-> New device assignment for '$vname' to group '$group' at LUN $lun.\n";
867                                         $changes++;
868                                 } else {
869                                         if (assignDevice($group, $vname, $lun)) {
870                                                 $errs++;
871                                         } else {
872                                                 $changes++;
873                                         }
874                                 }
875                         }
876                 }
877         }
878
879         # Enable/Disable configured targets
880         foreach my $type (keys %{$$config{'TARGETS'}}) {
881                 my $enable;
882
883                 if ($type eq 'enable') {
884                         $enable = $TRUE;
885                 } elsif ($type eq 'disable') {
886                         $enable = $FALSE;
887                 } else {
888                         print "\t-> WARNING: Ignoring invalid TARGETS specifier '$type'. ".
889                           "Should be one of enable,disable.\n";
890                         next;
891                 }
892
893                 foreach my $target (@{$$config{'TARGETS'}->{$type}->{'HOST'}}) {
894                         my $i_target = unformatTarget($target);
895
896                         if (!defined($$TARGETS{$i_target})) {
897                                 print "\t-> WARNING: Target '$target' not found on system.\n";
898                                 $errs += 1;
899                                 next;
900                         }
901
902                         next if ($enable == targetEnabled($i_target));
903
904                         if (!$enable && targetEnabled($target)) {
905                                 if ($force || $check) {
906                                         print "\t-> WARNING: Target mode for '$target' is currently enabled, ".
907                                           "however configuration file wants it disabled";
908
909                                         if (!$check) {
910                                                 print ", disabling.\n";
911                                                 if (enableTarget($target, $enable)) {
912                                                         $errs++;
913                                                 } else {
914                                                         $changes++;
915                                                 }
916                                         } else {
917                                                 print ".\n";
918                                                 $changes++;
919                                         }
920                                 }
921                         } else {
922                                 print "\t-> Target '$target' is enabled in configuration file, ".
923                                   "however is currently disabled";
924
925                                 if (!$check) {
926                                         print ", enabling.\n";
927                                         if (enableTarget($target, $enable)) {
928                                                 $errs++;
929                                         } else {
930                                                 $changes++;
931                                         }
932                                 } else {
933                                         print ".\n";
934                                         $changes++;
935                                 }
936                         }
937                 }
938         }
939
940         print "\nEncountered $errs error(s) while processing.\n" if ($errs);
941
942         if ($check) {
943                 print "Configuration checked, $changes difference(s) found with working configuration.\n";
944         } else {
945                 $changes = 0 if ($_DEBUG_);
946                 print "Configuration applied, $changes changes made.\n";
947         }
948
949         return $TRUE if ($errs);
950         return $FALSE;
951 }
952
953 sub clearConfiguration {
954         my $errs;
955
956         print "WARNING: This removes ALL applied SCST configuration and may result in data loss!\n";
957         print "If this is not what you intend, press ctrl-c now. Waiting 10 seconds.\n\n";
958         sleep 10;
959
960         print "\nRemoving all user and groups:\n\n";
961         foreach my $group (keys %GROUPS) {
962                 $errs += removeGroup($group) if ($group ne $_DEFAULT_GROUP_);
963         }
964
965         print "\nRemoving all handler devices:\n\n";
966         foreach my $device (keys %{$DEVICES}) {
967                 next if (!$$DEVICES{$device});
968                 next if ($SCST->handlerType($$DEVICES{$device}) != $SCST::SCST::IOTYPE_VIRTUAL);
969                 $errs += removeDevice($_REVERSE_MAP_{$$DEVICES{$device}}, $device);
970         }
971
972         print "\nEncountered $errs error(s) while processing.\n" if ($errs);
973         print "\nConfiguration cleared.\n";
974
975         return $TRUE if ($errs);
976         return $FALSE;
977 }
978
979 sub showSessions {
980         my $sessions = $SCST->sessions();
981         immediateExit($SCST->errorString());
982
983         print "\n\tTarget Name\tInitiator Name\t\t\tGroup Name\t\tCommand Count\n";
984
985         foreach my $target (keys %{$sessions}) {
986                 foreach my $group (keys %{$$sessions{$target}}) {
987                         foreach my $user (keys %{$$sessions{$target}->{$group}}) {
988                                 my $commands = $$sessions{$target}->{$group}->{$user};
989
990                                 print "\t$target\t$user\t\t$group\t\t$commands\n";
991                         }
992                 }
993         }
994
995         print "\n";
996
997         return $FALSE;
998 }
999
1000 sub addDevice {
1001         my $handler = shift;
1002         my $device = shift;
1003         my $path = shift;
1004         my $options = shift;
1005         my $blocksize = shift;
1006
1007         my $_handler = $_HANDLER_MAP_{$handler};
1008         my $htype = $SCST->handlerType($_handler);
1009
1010         if (!$htype) {
1011                 print "WARNING: Internal error occured: ".$SCST->errorString()."\n";
1012                 return $TRUE;
1013         }
1014
1015         if ($htype != $SCST::SCST::IOTYPE_VIRTUAL) {
1016                 my $typeString = $_HANDLER_TYPE_MAP_{$htype};
1017                 my $validType = $_HANDLER_TYPE_MAP_{$SCST::SCST::IOTYPE_VIRTUAL};
1018                 print "WARNING: Handler $handler of type $typeString is incapable of ".
1019                   "opening/closing devices. Valid handlers are:\n".
1020                   validHandlerTypes($SCST::SCST::IOTYPE_VIRTUAL)."\n";
1021                 return $TRUE;
1022         }
1023
1024         if (defined($$DEVICES{$device})) {
1025                 print "WARNING: Device '$device' already defined.\n";
1026                 return $TRUE;
1027         }
1028
1029         print "\t-> Opening virtual device '$device' at path '$path' using handler '$handler'..\n";
1030
1031         if ($SCST->openDevice($_handler, $device, $path, $options, $blocksize)) {
1032                 print "WARNING: Failed to open virtual device '$device' at path '$path': ".
1033                   $SCST->errorString()."\n";
1034                 return $TRUE;
1035         }
1036
1037         $$DEVICES{$device} = $_handler;
1038
1039         return $FALSE;
1040 }
1041
1042 sub removeDevice {
1043         my $handler = shift;
1044         my $device = shift;
1045
1046         my $_handler = $_HANDLER_MAP_{$handler};
1047         my $htype = $SCST->handlerType($_handler);
1048
1049         if (!$htype) {
1050                 print "WARNING: Internal error occured: ".$SCST->errorString()."\n";
1051                 return $TRUE;
1052         }
1053
1054         if ($htype != $SCST::SCST::IOTYPE_VIRTUAL) {
1055                 my $typeString = $_HANDLER_TYPE_MAP_{$htype};
1056                 my $validType = $_HANDLER_TYPE_MAP_{$SCST::SCST::IOTYPE_VIRTUAL};
1057                 print "WARNING: Handler $handler of type $typeString is incapable of ".
1058                   "opening/closing devices. Valid handlers are:\n".
1059                   validHandlerTypes($SCST::SCST::IOTYPE_VIRTUAL)."\n";
1060                 return $TRUE;
1061         }
1062
1063         if (!defined($$DEVICES{$device})) {
1064                 print "WARNING: Device '$device' not defined.\n";
1065                 return $TRUE;
1066         }
1067
1068         print "\t-> Closing virtual device '$device'..\n";
1069
1070         if ($SCST->closeDevice($_handler, $device)) {
1071                 print "WARNING: Failed to close virtual device '$device': ".
1072                   $SCST->errorString()."\n";
1073                 return $TRUE;
1074         }
1075
1076         undef $$DEVICES{$device};
1077
1078         return $FALSE;
1079 }
1080
1081 sub addGroup {
1082         my $group = shift;
1083
1084         if (defined($GROUPS{$group})) {
1085                 print "WARNING: Group '$group' already exists.\n";
1086                 return $TRUE;
1087         }
1088
1089         print "\t-> Creating security group '$group'..\n";
1090
1091         if ($SCST->addGroup($group)) {
1092                 print "WARNING: Failed to create security group '$group': ".
1093                   $SCST->errorString()."\n";
1094                 return $TRUE;
1095         }
1096
1097         $GROUPS{$group}++;
1098
1099         return $FALSE;
1100 }
1101
1102 sub removeGroup {
1103         my $group = shift;
1104
1105         if (!defined($GROUPS{$group})) {
1106                 print "WARNING: Group '$group' does not exist.\n";
1107                 return $TRUE;
1108         }
1109
1110         print "\t-> Removing security group '$group'..\n";
1111
1112         if ($SCST->removeGroup($group)) {
1113                 print "WARNING: Failed to remove security group '$group': ".
1114                   $SCST->errorString()."\n";
1115                 return $TRUE;
1116         }
1117
1118         undef $GROUPS{$group};
1119
1120         return $FALSE;
1121 }
1122
1123 sub addUser {
1124         my $group = shift;
1125         my $user = shift;
1126
1127         if (!defined($GROUPS{$group})) {
1128                 print "WARNING: Failed to add user '$user' to group '$group', group does not exist.\n";
1129                 return $TRUE;
1130         }
1131
1132         if (defined($USERS{$group}->{$user})) {
1133                 print "WARNING: User '$user' already exists in security group '$group'.\n";
1134                 return $TRUE;
1135         }
1136
1137         print "\t-> Adding user '$user' to security group '$group'..\n";
1138
1139         if ($SCST->addUser($user, $group)) {
1140                 print "WARNING: Failed to add user '$user' to security group '$group': ".
1141                   $SCST->errorString()."\n";
1142                 return $TRUE;
1143         }
1144
1145         $USERS{$group}->{$user}++;
1146
1147         return $FALSE;
1148 }
1149
1150 sub removeUser {
1151         my $group = shift;
1152         my $user = shift;
1153
1154         if (!defined($GROUPS{$group})) {
1155                 print "WARNING: Failed to remove user '$user' from group '$group', group does not exist.\n";
1156                 return $TRUE;
1157         }
1158
1159         if (!defined($USERS{$group}->{$user})) {
1160                 print "WARNING: User '$user' doesn\'t exist in security group '$group'.\n";
1161                 return $TRUE;
1162         }
1163
1164         print "\t-> Removing user '$user' from security group '$group'..\n";
1165
1166         if ($SCST->removeUser($user, $group)) {
1167                 print "WARNING: Failed to add user '$user' to security group '$group': ".
1168                   $SCST->errorString()."\n";
1169                 return $TRUE;
1170         }
1171
1172         undef $USERS{$group}->{$user};
1173
1174         return $FALSE;
1175 }
1176
1177 sub clearUsers {
1178         my $group = shift;
1179
1180         if (!defined($GROUPS{$group})) {
1181                 print "WARNING: Failed to clear users from group '$group', group does not exist.\n";
1182                 return $TRUE;
1183         }
1184
1185         print "\t-> Clearing users from security group '$group'..\n";
1186
1187         if ($SCST->clearUsers($group)) {
1188                 print "WARNING: Failed to clear users from security group '$group': ".
1189                   $SCST->errorString()."\n";
1190                 return $TRUE;
1191         }
1192
1193         undef $USERS{$group};
1194
1195         return $FALSE;
1196 }
1197
1198 sub assignDevice {
1199         my $group = shift;
1200         my $device = shift;
1201         my $lun = shift;
1202         my %allLuns;
1203
1204         # Put luns into something easier to parse..
1205         foreach my $_group (keys %ASSIGNMENTS) {
1206                 my $_gAssigns = $ASSIGNMENTS{$_group};
1207
1208                 foreach my $_device (keys %{$_gAssigns}) {
1209                         @{$allLuns{$_group}}[$$_gAssigns{$_device}] = $_device;
1210                 }
1211         }
1212
1213         # Use the next available LUN if none specified
1214         if ($lun !~ /\d+/) {
1215                 $lun = ($#{$allLuns{$group}} + 1);
1216                 if ($lun > $_MAX_LUNS_) {
1217                         print "ERROR: Unable to use next available LUN of $lun, lun out of range.\n";
1218                         return $TRUE;
1219                 }
1220
1221                 print "\t-> Device '$device': Using next available LUN of $lun for group '$group'.\n";
1222         }
1223                         
1224         if (($lun < 0) || ($lun > $_MAX_LUNS_)) {
1225                 print "ERROR: Unable to assign device '$device', lun '$lun' is out of range.\n";
1226                 return $TRUE;
1227         }
1228
1229         if (!defined($$DEVICES{$device})) {
1230                 print "WARNING: Unable to assign non-existant device '$device' to group '$group'.\n";
1231                 return $TRUE;
1232         }
1233
1234         if (@{$allLuns{$group}}[$lun]) {
1235                 print "ERROR: Device '$device': Lun '$lun' is already assigned to device '".@{$allLuns{$group}}[$lun]."'.\n";
1236                 return $TRUE;
1237         }
1238
1239         print "\t-> Assign virtual device '$device' to group '$group' at LUN '$lun'..\n";
1240
1241         if ($SCST->assignDeviceToGroup($device, $group, $lun)) {
1242                 print "WARNING: Failed to assign device '$device' to group '$group': ".
1243                   $SCST->errorString()."\n";
1244                 return $TRUE;
1245         }
1246
1247         if (!defined($ASSIGNMENTS{$group})) {
1248                 my %assignments_t;
1249                 $ASSIGNMENTS{$group} = \%assignments_t;
1250         }
1251
1252         my $_assignments = $ASSIGNMENTS{$group};
1253
1254         $$_assignments{$device} = $lun; 
1255
1256         return $FALSE;
1257 }
1258
1259 sub releaseDevice {
1260         my $group = shift;
1261         my $device = shift;
1262
1263         if (!defined($GROUPS{$group})) {
1264                 print "WARNING: Failed to release device '$device' from group '$group', group does not exist.\n";
1265                 return $TRUE;
1266         }
1267
1268         if (!defined($$DEVICES{$device})) {
1269                 print "WARNING: Failed to release device '$device', device not defined.\n";
1270                 return $TRUE;
1271         }
1272
1273         print "\t-> Release virtual device '$device' from group '$group'..\n";
1274
1275         if ($SCST->removeDeviceFromGroup($device, $group)) {
1276                 print "WARNING: Failed to release device '$device' from group '$group': ".
1277                   $SCST->errorString()."\n";
1278                 return $TRUE;
1279         }
1280
1281         my $_assignments = $ASSIGNMENTS{$group};
1282
1283         undef $$_assignments{$device};
1284
1285         return $FALSE;
1286 }
1287
1288 sub clearDevices {
1289         my $group = shift;
1290
1291         if (!defined($GROUPS{$group})) {
1292                 print "WARNING: Failed to clear devices from group '$group', group does not exist.\n";
1293                 return $TRUE;
1294         }
1295
1296         print "\t-> Clear virtual devices from group '$group'..\n";
1297
1298         if ($SCST->clearGroupDevices($group)) {
1299                 print "WARNING: Failed to clear devices from group '$group': ".
1300                   $SCST->errorString()."\n";
1301                 return $TRUE;
1302         }
1303
1304         undef $ASSIGNMENTS{$group};
1305
1306         return $FALSE;
1307 }
1308
1309 sub targets {
1310         my %targets;
1311         my %fcards;
1312
1313         my $root = new IO::Dir $_FC_CLASS_ if (-d $_FC_CLASS_);
1314
1315         if ($root) {
1316                 while (my $entry = $root->read()) {
1317                         next if (($entry eq '.') || ($entry eq '..'));
1318
1319                         my $io = new IO::File "$_FC_CLASS_/$entry/port_name", O_RDONLY;
1320
1321                         if ($io) {
1322                                 my $wwn = <$io>;
1323                                 chomp $wwn;
1324                                 close $io;
1325
1326                                 $fcards{$entry} = $wwn;
1327                         }
1328                 }
1329         }
1330
1331         $root = new IO::Dir $_SCSI_CLASS_ if (-d $_SCSI_CLASS_);
1332
1333         if ($root) {
1334                 while (my $entry = $root->read()) {
1335                         next if (($entry eq '.') || ($entry eq '..'));
1336
1337                         my $io = new IO::File "$_SCSI_CLASS_/$entry/target_mode_enabled", O_RDONLY;
1338
1339                         if ($io) {
1340                                 my $enabled = <$io>;
1341                                 chomp $enabled;
1342                                 close $io;
1343
1344                                 $targets{$entry}->{'path'} = "$_SCSI_CLASS_/$entry/target_mode_enabled";
1345                                 $targets{$entry}->{'enabled'} = $enabled;
1346                                 $targets{$entry}->{'qla_isp'} = $FALSE;
1347
1348                                 if ($fcards{$entry}) {
1349                                         $targets{$fcards{$entry}}->{'enabled'} = $enabled;
1350                                         $targets{$fcards{$entry}}->{'path'} =
1351                                           "$_SCSI_CLASS_/$entry/target_mode_enabled";
1352                                         $targets{$entry}->{'duplicate'} = $TRUE;
1353                                 } else {
1354                                         $targets{$entry}->{'duplicate'} = $FALSE;
1355                                 }
1356                         }
1357                 }
1358         }
1359
1360         $root = new IO::Dir $_SCSI_ISP_ if (-d $_SCSI_ISP_);
1361
1362         if ($root) {
1363                 while (my $entry = $root->read()) {
1364                         next if (($entry eq '.') || ($entry eq '..'));
1365
1366                         local $/;
1367                         my $io = new IO::File "$_SCSI_ISP_/$entry", O_RDONLY;
1368
1369                         if ($io) {
1370                                 my $wwn;
1371                                 my @fcont;
1372                                 my $fstr;
1373                                 my $enabled2;
1374
1375                                 $fstr = <$io>;
1376                                 close $io;
1377
1378                                 ($wwn) = ($fstr =~ '.*?Port WWN +([^\ ]+) .*');
1379                                 $fcards{$entry} = $wwn;
1380
1381                                 $io = new IO::File "$_SCSITGT_QLAISP_/$entry", O_RDONLY;
1382                                 if ($io) {
1383                                         $fstr = <$io>;
1384                                         close $io;
1385
1386                                         ($enabled2) = ($fstr =~ '[^\n]+\n *\d *: *(\d)');
1387                                         $targets{$entry}->{'path'} = "$_SCSITGT_QLAISP_/$entry";
1388                                         $targets{$entry}->{'enabled'} = $enabled2;
1389                                         $targets{$entry}->{'qla_isp'} = $TRUE;
1390
1391                                         if ($fcards{$entry}) {
1392                                                 $targets{$fcards{$entry}}->{'enabled'} = $enabled2;
1393                                                 $targets{$fcards{$entry}}->{'path'} = "$_SCSITGT_QLAISP_/$entry";
1394                                                 $targets{$fcards{$entry}}->{'qla_isp'} = $TRUE;
1395                                                 $targets{$entry}->{'duplicate'} = $TRUE;
1396                                         } else {
1397                                                 $targets{$entry}->{'duplicate'} = $FALSE;
1398                                         }
1399                                 }
1400                         }
1401                 }
1402         }
1403
1404         return \%targets;
1405 }
1406
1407 sub targetEnabled {
1408         my $target = shift;
1409
1410         return undef if (!defined($$TARGETS{$target}));
1411         return $$TARGETS{$target}->{'enabled'};
1412 }
1413
1414 sub enableTarget {
1415         my $target = shift;
1416         my $enable = shift;
1417
1418         return undef if (!defined($$TARGETS{$target}));
1419
1420         my $io = new IO::File $$TARGETS{$target}->{'path'}, O_WRONLY;
1421         return $TRUE if (!$io);
1422
1423         print $enable ? "\t-> Enabling" : "\t-> Disabling";
1424         print " target mode for SCST host '$target'.\n";
1425
1426         if ($_DEBUG_) {
1427                 print "DBG($$): ".$$TARGETS{$target}->{'path'}." -> $enable\n\n";
1428         } else {
1429                 if ($$TARGETS{$target}->{'qla_isp'} == $FALSE) {
1430                         print $io $enable;
1431                 } else {
1432                         print $io $enable ? "enable all" : "disable all";
1433                 }
1434         }
1435
1436         close $io;
1437
1438         $$TARGETS{unformatTarget($target)}->{'enabled'} = $enable;
1439
1440         return $FALSE;
1441 }
1442
1443 sub readConfig {
1444         my $confile = shift;
1445         my %config;
1446         my $section;
1447         my $last_section;
1448         my $arg;
1449         my $last_arg;
1450         my %empty;
1451
1452         my $io = new IO::File $confile, O_RDONLY;
1453
1454         die("FATAL: Unable to open specified configuration file $confile: $!\n") if (!$io);
1455
1456         while (my $line = <$io>) {
1457                 ($line, undef) = split(/\#/, $line, 2);
1458                 $line = cleanupString($line);
1459
1460                 if ($line =~ /^\[(.*)\]$/) {
1461                         ($section, $arg) = split(/\s+/, $1, 2);
1462
1463                         if ($last_arg && ($last_section ne $section) &&
1464                             !defined($config{$last_section}->{$last_arg})) {
1465                                 $config{$last_section}->{$last_arg} = \%empty;
1466                         }   
1467
1468                         $last_arg = $arg;
1469                         $last_section = $section;
1470                 } elsif ($section && $arg && $line) {
1471                         my($parameter, $value) = split(/\s+/, $line, 2);
1472
1473                         push @{$config{$section}->{$arg}->{$parameter}}, $value;
1474                 }
1475         }
1476
1477         close $io;
1478
1479         return \%config;
1480 }
1481
1482 sub cleanupString {
1483         my $string = shift;
1484
1485         $string =~ s/^\s+//;
1486         $string =~ s/\s+$//;
1487
1488         return $string;
1489 }
1490
1491 sub formatTarget {
1492         my $target = shift;
1493
1494         if ($target =~ /^0x/) {
1495                 $target =~ s/^0x//;
1496                 my($o1, $o2, $o3, $o4, $o5, $o6, $o7, $o8) = unpack("A2A2A2A2A2A2A2A2", $target);
1497                 $target = "$o1:$o2:$o3:$o4:$o5:$o6:$o7:$o8";
1498         }
1499
1500         $target =~ tr/A-Z/a-z/;
1501
1502         return $target;
1503 }
1504
1505 sub unformatTarget {
1506         my $target = shift;
1507
1508         if ($target =~ /^.{2}\:.{2}\:.{2}\:.{2}\:.{2}\:.{2}\:.{2}\:.{2}/) {
1509                 $target =~ s/\://g;
1510                 $target = "0x$target";
1511         }
1512
1513         $target =~ tr/A-Z/a-z/;
1514
1515         return $target;
1516 }
1517
1518 sub validHandlerTypes {
1519         my $type = shift;
1520         my $buffer = "\n";
1521
1522         foreach my $handler (keys %_REVERSE_MAP_) {
1523                 $buffer .= "\t".$_REVERSE_MAP_{$handler}."\n" if ($SCST->handlerType($handler) == $type);
1524         }
1525
1526         return $buffer;
1527 }
1528
1529 # If we have an unread error from SCST, exit immediately
1530 sub immediateExit {
1531         my $error = shift;
1532
1533         return if (!$error);
1534
1535         print "\n\nFATAL: Received the following error:\n\n\t";
1536         print "$error\n\n";
1537
1538         exit 1;
1539 }
1540
1541 # Hey! Stop that!
1542 sub commitSuicide {
1543         print "\n\nAborting immediately.\n";
1544         exit 1;
1545 }