1 # Copyright (c) 2009,2010 Zmanda, Inc. All Rights Reserved.
3 # This library is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU Lesser General Public License version 2.1 as
5 # published by the Free Software Foundation.
7 # This library is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10 # License for more details.
12 # You should have received a copy of the GNU Lesser General Public License
13 # along with this library; if not, write to the Free Software Foundation,
14 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19 package Amanda::Changer::robot;
25 @ISA = qw( Amanda::Changer );
30 use Amanda::MainLoop qw( :GIOCondition make_cb define_steps step );
31 use Amanda::Config qw( :getconf );
32 use Amanda::Debug qw( debug warning );
33 use Amanda::Device qw( :constants );
35 use Amanda::Constants;
39 Amanda::Changer::robot -- control a physical tape changer
43 This package controls a physical tape changer via 'mtx'.
45 See the amanda-changers(7) manpage for usage information.
51 # This is one of the more sophisticated changers. Here are some notes that may
52 # help while reading the source code.
56 # The device state is shared between all changers accessing the same library.
57 # It is a hash with keys:
60 # drive_lru - recently used drives, least recent first
61 # bc2lb - hash mapping known barcodes to known labels
62 # current_slot - the current slot
63 # last_operation_time - time the last operation finished
64 # last_operation_delay - required delay for that operation
65 # last_status - last time a 'status' command finished
67 # The 'slots' key is a hash, with slot numbers as keys and hashes
68 # as values. Each slot's hash has keys
69 # state - SLOT_FULL/SLOT_EMPTY/SLOT_UNKNOWN
70 # device_status - the status of the device
71 # device_error - the error message of the device
72 # f_type - The f_type of the header
73 # label - volume label, if known
74 # barcode - volume barcode, if available
75 # loaded_in - drive this volume is loaded in
76 # ie - 1 if this is an import/export slot
77 # note that this state pretends that a tape physically located
78 # in a tape drive is still located in its original slot.
80 # The 'drives' key is also a hash by drive numbere, the values of
81 # which are hashes with keys
82 # state - SLOT_FULL/SLOT_EMPTY/SLOT_UNKNOWN
83 # label - volume label
84 # barcode - volume barcode
85 # orig_slot - slot from which this tape was loaded
89 # This package uses Amanda::Changer's with_locked_state to lock a statefile and
90 # load its contents. Every time the state is locked, the package also
91 # considers running 'status' to update the state; the status_interval protects
92 # against running status too often.
94 # Each changer method has an "_unlocked" version that does the actual work, and
95 # is called with an additional 'state' parameter containing the locked state.
96 # This is particularly useful when the load method calls the eject method to
97 # empty a drive that it wants to use.
101 # Reservations are currently represented by a PID in the state file. If that
102 # pid is no longer running, then the reservation is considered stale and is
103 # discarded (with a warning).
105 # Reservation objects defer most of the interesting operations back to the
106 # changer itself, since the operations require locked access to the state.
110 # All of the operating-system-specific functionality is abstracted into the
111 # Interface class. This is written in such a way that it could be replaced
112 # by a direct SCSI interface.
116 my ($config, $tpchanger) = @_;
118 # strip the "chg-foo:" prefix from $tpchanger
119 my $device_name = $tpchanger;
120 $device_name =~ s/^[^:]*://;
122 # get the 'chg-foo' form of this changer script
123 my $class_name = $class;
124 $class_name =~ s/Amanda::Changer::/chg-/;
128 device_name => $device_name,
131 # set below from properties
133 drive2device => {}, # { drive => device name }
134 driveorder => [], # order of tape-device properties
135 drive_choice => 'lru',
136 eject_before_unload => 0,
139 status_interval => 2, # in seconds
140 load_poll => [0, 2, 120], # delay, poll, until
141 eject_delay => 0, # in seconds
142 unload_delay => 0, # in seconds
143 class_name => $class_name,
145 bless ($self, $class);
147 # handle some config and properties
148 my $properties = $config->{'properties'};
150 if (defined $config->{'changerdev'} and $config->{'changerdev'} ne '') {
151 return Amanda::Changer->make_error("fatal", undef,
152 message => "'changerdev' is not allowed with $self->{class_name}");
155 if ($config->{'changerfile'}) {
156 $self->{'statefile'} = Amanda::Config::config_dir_relative($config->{'changerfile'});
158 my $safe_filename = "$self->{class_name}:$device_name";
159 $safe_filename =~ tr/a-zA-Z0-9/-/cs;
160 $safe_filename =~ s/^-*//;
161 $self->{'statefile'} = "$localstatedir/amanda/$safe_filename";
163 $self->_debug("using statefile '$self->{statefile}'");
165 # figure out the drive number to device name mapping
166 if (exists $config->{'tapedev'}
167 and $config->{'tapedev'} ne ''
168 and !exists $properties->{'tape-device'}) {
169 # if the tapedev points to us (the changer), then give an error
170 if ($config->{'tapedev'} eq $tpchanger) {
171 return Amanda::Changer->make_error("fatal", undef,
172 message => "must specify a tape-device property");
174 $self->{'drive2device'} = { '0' => $config->{'tapedev'} };
175 push @{$self->{'driveorder'}}, '0';
177 if (!exists $properties->{'tape-device'}) {
178 return Amanda::Changer->make_error("fatal", undef,
179 message => "no 'tape-device' property specified");
181 for my $pval (@{$properties->{'tape-device'}->{'values'}}) {
182 my ($drive, $device);
183 unless (($drive, $device) = ($pval =~ /(\d+)=(.*)/)) {
184 return Amanda::Changer->make_error("fatal", undef,
185 message => "invalid 'tape-device' property '$pval'");
187 if (exists $self->{'drive2device'}->{$drive}) {
188 return Amanda::Changer->make_error("fatal", undef,
189 message => "tape-device drive $drive defined more than once");
191 $self->{'drive2device'}->{$drive} = $device;
192 push @{$self->{'driveorder'}}, $drive;
196 # eject-before-unload
197 my $ebu = $self->{'config'}->get_boolean_property(
198 "eject-before-unload", 0);
200 return Amanda::Changer->make_error("fatal", undef,
201 message => "invalid 'eject-before-unload' value");
203 $self->{'eject_before_unload'} = $ebu;
206 my $fast_search = $self->{'config'}->get_boolean_property(
208 if (!defined $fast_search) {
209 return Amanda::Changer->make_error("fatal", undef,
210 message => "invalid 'fast-search' value");
212 $self->{'fast_search'} = $fast_search;
215 if (exists $properties->{'use-slots'}) {
216 $self->{'use_slots'} = join ",", @{$properties->{'use-slots'}->{'values'}};
217 if ($self->{'use_slots'} !~ /\d+(-\d+)?(,\d+(-\d+)?)*/) {
218 return Amanda::Changer->make_error("fatal", undef,
219 message => "invalid 'use-slots' value '$self->{use_slots}'");
224 if (exists $properties->{'drive-choice'}) {
225 my $pval = $properties->{'drive-choice'}->{'values'}->[0];
226 if (!grep { lc($_) eq $pval } ('lru', 'firstavail')) {
227 return Amanda::Changer->make_error("fatal", undef,
228 message => "invalid 'drive-choice' value '$pval'");
230 $self->{'drive_choice'} = $pval;
235 next unless exists $config->{'properties'}->{'load-poll'};
236 if (@{$config->{'properties'}->{'load-poll'}->{'values'}} > 1) {
237 return Amanda::Changer->make_error("fatal", undef,
238 message => "only one value allowed for 'load-poll'");
240 my $propval = $config->{'properties'}->{'load-poll'}->{'values'}->[0];
241 my ($delay, $delayu, $poll, $pollu, $until, $untilu) = ($propval =~ /^
253 if (!defined $delay) {
254 return Amanda::Changer->make_error("fatal", undef,
255 message => "invalid delay value '$propval' for 'load-poll'");
258 $delay *= 60 if (defined $delayu and $delayu =~ /m/i);
260 $poll = 0 unless defined $poll;
261 $poll *= 60 if (defined $pollu and $pollu =~ /m/i);
263 $until = 0 unless defined $until;
264 $until *= 60 if (defined $untilu and $untilu =~ /m/i);
266 $self->{'load_poll'} = [ $delay, $poll, $until ];
269 # status-interval, eject-delay, unload-delay
270 for my $propname (qw(status-interval eject-delay unload-delay)) {
271 next unless exists $config->{'properties'}->{$propname};
272 if (@{$config->{'properties'}->{$propname}->{'values'}} > 1) {
273 return Amanda::Changer->make_error("fatal", undef,
274 message => "only one value allowed for $propname");
276 my $propval = $config->{'properties'}->{$propname}->{'values'}->[0];
277 my ($time, $timeu) = ($propval =~ /^(\d+)([ms]?)/ix);
279 if (!defined $time) {
280 return Amanda::Changer->make_error("fatal", undef,
281 message => "invalid time value '$propval' for '$propname'");
284 $time *= 60 if (defined $timeu and $timeu =~ /m/i);
288 $self->{$key} = $time;
291 my $ignore_barcodes = $self->{'config'}->get_boolean_property(
292 "ignore-barcodes", 0);
293 if (!defined $ignore_barcodes) {
294 return Amanda::Changer->make_error("fatal", undef,
295 message => "invalid 'ignore-barcodes' value");
298 # get the interface, returning an error if we get one
299 $self->{'interface'} = $self->get_interface($device_name, $ignore_barcodes);
300 return $self->{'interface'}
301 if ($self->{'interface'}->isa("Amanda::Changer::Error"));
309 $self->validate_params('load', \%params);
311 return if $self->check_error($params{'res_cb'});
313 $self->_with_updated_state(\%params, 'res_cb', sub { $self->load_unlocked(@_) });
319 my ($slot, $drive, $need_unload);
320 my $state = $params{'state'};
322 my $steps = define_steps
323 cb_ref => \$params{'res_cb'};
325 step calculate_slot => sub {
326 # make sure the slot is numeric
327 if (exists $params{'slot'}) {
328 if ($params{'slot'} =~ /^\d+$/) {
329 $params{'slot'} = $params{'slot'}+0;
331 return $self->make_error("failed", $params{'res_cb'},
333 message => "invalid slot '$params{slot}'");
337 if (exists $params{'relative_slot'}) {
338 if ($params{'relative_slot'} eq "next") {
339 if (exists $params{'slot'}) {
340 $slot = $self->_get_next_slot($state, $params{'slot'});
341 $self->_debug("loading next relative to $params{slot}: $slot");
343 $slot = $self->_get_next_slot($state, $state->{'current_slot'});
344 $self->_debug("loading next relative to current slot: $slot");
347 return $self->make_error("failed", $params{'res_cb'},
349 message => "could not find next slot");
351 } elsif ($params{'relative_slot'} eq "current") {
352 $slot = $state->{'current_slot'};
354 # seek to the first slot
355 $slot = $self->_get_next_slot($state, $state->{'current_slot'});
358 return $self->make_error("failed", $params{'res_cb'},
360 message => "no current slot");
363 return $self->make_error("failed", $params{'res_cb'},
365 message => "invalid relative_slot '$params{relative_slot}'");
368 } elsif (exists $params{'slot'}) {
369 $slot = $params{'slot'};
370 $self->_debug("loading slot '$params{slot}'");
372 if (!defined $slot or !exists $state->{'slots'}->{$slot}) {
373 return $self->make_error("failed", $params{'res_cb'},
375 message => "invalid slot '$slot'");
378 } elsif (exists $params{'label'}) {
379 $self->_debug("loading label '$params{label}'");
380 while (my ($sl, $info) = each(%{$state->{'slots'}})) {
381 if (defined $info->{'label'} and $info->{'label'} eq $params{'label'}) {
387 if (!defined $slot) {
388 return $self->make_error("failed", $params{'res_cb'},
389 reason => "notfound",
390 message => "label '$params{label}' not recognized or not found");
394 return $self->make_error("failed", $params{'res_cb'},
396 message => "no 'slot' or 'label' specified to load()");
399 if (!$self->_is_slot_allowed($slot)) {
400 if (exists $params{'label'}) {
401 return $self->make_error("failed", $params{'res_cb'},
403 message => "label '$params{label}' is in slot $slot, which is " .
404 "not in use-slots ($self->{use_slots})");
406 return $self->make_error("failed", $params{'res_cb'},
408 message => "slot $slot not in use-slots ($self->{use_slots})");
412 if (exists $params{'except_slots'} and exists $params{'except_slots'}->{$slot}) {
413 return $self->make_error("failed", $params{'res_cb'},
414 reason => "notfound",
415 message => "all slots have been loaded");
418 if ($state->{'slots'}->{$slot}->{'state'} eq Amanda::Changer::SLOT_EMPTY) {
419 return $self->make_error("failed", $params{'res_cb'},
421 message => "slot $slot is empty");
424 return $steps->{'calculate_drive'}->();
427 step calculate_drive => sub {
431 # see if the tape is already in a drive
432 $drive = $state->{'slots'}->{$slot}->{'loaded_in'};
433 if (defined $drive) {
434 $self->_debug("requested volume is already in drive $drive");
435 my $info = $state->{'drives'}->{$drive};
437 # if it's reserved, it can't be used
438 if ($info->{'res_info'} and $self->_res_info_verify($info->{'res_info'})) {
439 return $self->make_error("failed", $params{'res_cb'},
440 reason => "volinuse",
442 message => "the requested volume is in use (drive $drive)");
445 # if it's not reserved, but not in our list of drives, well, it still
447 if (!exists $self->{'drive2device'}->{$drive}) {
448 return $self->make_error("failed", $params{'res_cb'},
449 # not 'volinuse' because we can't expect the tape to be magically
450 # unloaded any time soon -- it's not actually in use, just inaccessible
452 message => "the requested volume is in drive $drive, which this " .
453 "changer instance cannot access");
456 # otherwise, we can jump all the way to the end of this process
457 return $steps->{'start_polling'}->();
460 # here is where we implement each of the drive-selection algorithms
462 if ($self->{'drive_choice'} eq 'lru') {
463 my %lru = map { $_, 1 } @{$state->{'drive_lru'}};
464 my @unused = grep { ! exists $lru{$_} } @{$self->{'driveorder'}};
466 # search through unused drives, then the LRU list
467 @check_order = (@unused, @{$state->{'drive_lru'}});
468 } elsif ($self->{'drive_choice'} eq 'firstavail') {
469 # just the drive order, so we tend to prefer the first drive in
471 @check_order = (@{$self->{'driveorder'}});
473 # the constructor should detect this circumstance
474 die "invalid drive_choice";
478 for my $dr (@check_order) {
479 my $info = $state->{'drives'}->{$dr};
480 next unless defined $info;
481 next if exists $checked{$dr}; # don't check drives repeatedly
484 # skip drives we don't have rights to use
485 next unless exists $self->{'drive2device'}->{$dr};
487 # skip reserved drives
488 if ($info->{'res_info'}) {
489 if ($self->_res_info_verify($info->{'res_info'})) {
490 # this is a valid reservation -> skip this drive
491 $self->_debug("skipping drive $dr - already reserved");
494 warning("invalidating stale reservation on drive $dr");
495 $info->{'res_info'} = undef;
499 # otherwise, the drive is available, so use it (whether it contains
502 if ($info->{'state'} != Amanda::Changer::SLOT_EMPTY) {
508 if (!defined $drive) {
509 return $self->make_error("failed", $params{'res_cb'},
510 reason => "driveinuse",
511 message => "no drives available");
514 # remove this drive from the lru and put it at the end
515 $state->{'drive_lru'} = [ grep { $_ ne $drive } @{$state->{'drive_lru'}} ];
516 push @{$state->{'drive_lru'}}, $drive;
518 $self->_debug("using drive $drive");
520 $steps->{'wait_to_start'}->();
523 step wait_to_start => sub {
524 $self->_after_delay($state, $steps->{'start_operation'});
527 step start_operation => sub {
528 # $need_unload is set in $steps->{calculate_drive}
530 $steps->{'start_eject'}->();
532 $steps->{'start_load'}->();
536 step start_eject => sub {
537 # we use the 'eject' method to unload here -- it ejects the volume
538 # if the configuration calls for it, then puts the volume away in its
540 $self->eject_unlocked(
541 finished_cb => $steps->{'eject_finished'},
546 step eject_finished => sub {
550 return $params{'res_cb'}->($err);
553 $steps->{'wait_to_load'}->();
556 step wait_to_load => sub {
557 $self->_after_delay($state, $steps->{'start_load'});
560 step start_load => sub {
561 # $slot and $drive are set
562 $self->{'interface'}->load($slot, $drive, $steps->{'load_finished'});
565 step load_finished => sub {
566 # $slot and $drive are set
570 return $self->make_error("failed", $params{'res_cb'},
575 $steps->{'start_polling'}->();
578 my ($next_poll, $last_poll);
579 step start_polling => sub {
580 my ($delay, $poll, $until) = @{ $self->{'load_poll'} };
582 $next_poll = $now + $delay;
583 $last_poll = $now + $until;
585 return Amanda::MainLoop::call_after(1000 * ($next_poll - $now), $steps->{'check_device'});
588 step check_device => sub {
589 my $device_name = $self->{'drive2device'}->{$drive};
590 die "drive $drive not found in drive2device" unless $device_name; # shouldn't happen
592 $self->_debug("polling '$device_name' to see if it's ready");
594 my $device = $self->get_device($device_name);
595 return $params{'res_cb'}->($device) if $device->isa("Amanda::Changer::Error");
598 $device->read_label();
600 # see if the device thinks it's possible it's busy or empty
601 if ($device->status & $DEVICE_STATUS_VOLUME_MISSING
602 or $device->status & $DEVICE_STATUS_DEVICE_BUSY) {
603 # device is not ready -- set up for the next polling step
604 my ($delay, $poll, $until) = @{ $self->{'load_poll'} };
607 $next_poll = $now + 1 if ($next_poll < $now);
608 if ($poll != 0 and $next_poll < $last_poll) {
609 return Amanda::MainLoop::call_after(
610 1000 * ($next_poll - $now), $steps->{'check_device'});
613 # (fall through if we're done polling)
616 if ($device->status == $DEVICE_STATUS_SUCCESS) {
617 $label = $device->volume_label;
618 } elsif ($device->status & $DEVICE_STATUS_VOLUME_UNLABELED) {
625 $steps->{'make_res'}->($device, $label);
628 step make_res => sub {
629 my ($device, $label) = @_;
631 # check the label against the desired label, in case this isn't the
633 if ($label and $params{'label'} and $label ne $params{'label'}) {
634 $self->_debug("Expected label '$params{label}', but got '$label'");
636 # update metadata with this new information
637 $state->{'slots'}->{$slot}->{'state'} = Amanda::Changer::SLOT_FULL;
638 $state->{'slots'}->{$slot}->{'device_status'} = $device->status;
639 $state->{'slots'}->{$slot}->{'device_error'} = $device->error;
640 if (defined $device->{'volume_header'}) {
641 $state->{'slots'}->{$slot}->{'f_type'} = $device->{'volume_header'}->{type};
643 $state->{'slots'}->{$slot}->{'f_type'} = undef;
645 $state->{'slots'}->{$slot}->{'label'} = $label;
646 if ($state->{'slots'}->{$slot}->{'barcode'}) {
647 $state->{'bc2lb'}->{$state->{'slots'}->{$slot}->{'barcode'}} = $label;
650 return $self->make_error("failed", $params{'res_cb'},
651 reason => "notfound",
652 message => "Found unexpected tape '$label' while looking " .
653 "for '$params{label}'");
656 if (!$label and $params{'label'}) {
657 $self->_debug("Expected label '$params{label}', but got an unlabeled tape");
659 # update metadata with this new information
660 $state->{'slots'}->{$slot}->{'state'} = Amanda::Changer::SLOT_FULL;
661 $state->{'slots'}->{$slot}->{'device_status'} = $device->status;
662 $state->{'slots'}->{$slot}->{'device_error'} = $device->error;
663 if (defined $device->{'volume_header'}) {
664 $state->{'slots'}->{$slot}->{'f_type'} = $device->{'volume_header'}->{type};
666 $state->{'slots'}->{$slot}->{'f_type'} = undef;
668 $state->{'slots'}->{$slot}->{'label'} = undef;
669 if ($state->{'slots'}->{$slot}->{'barcode'}) {
670 delete $state->{'bc2lb'}->{$state->{'slots'}->{$slot}->{'barcode'}};
673 return $self->make_error("failed", $params{'res_cb'},
674 reason => "notfound",
675 message => "Found unlabeled tape while looking for '$params{label}'");
678 my $res = Amanda::Changer::robot::Reservation->new($self, $slot, $drive,
679 $device, $state->{'slots'}->{$slot}->{'barcode'});
681 # mark this as reserved
682 $state->{'drives'}->{$drive}->{'res_info'} = $self->_res_info_new();
684 # update our state before returning
685 $state->{'slots'}->{$slot}->{'loaded_in'} = $drive;
686 $state->{'drives'}->{$drive}->{'orig_slot'} = $slot;
687 $state->{'slots'}->{$slot}->{'label'} = $label;
688 $state->{'drives'}->{$drive}->{'label'} = $label;
689 $state->{'drives'}->{$drive}->{'state'} = Amanda::Changer::SLOT_FULL;
690 $state->{'drives'}->{$drive}->{'barcode'} = $state->{'slots'}->{$slot}->{'barcode'};
691 $state->{'slots'}->{$slot}->{'device_status'} = $device->status;
692 if ($label and $state->{'slots'}->{$slot}->{'barcode'}) {
693 $state->{'bc2lb'}->{$state->{'slots'}->{$slot}->{'barcode'}} = $label;
695 if ($params{'set_current'}) {
696 $self->_debug("setting current slot to $slot");
697 $state->{'current_slot'} = $slot;
700 return $params{'res_cb'}->(undef, $res);
706 my ($key, %params) = @_;
708 if ($key eq 'fast_search') {
709 $self->info_key_fast_search(%params);
710 } elsif ($key eq 'vendor_string') {
711 $self->info_key_vendor_string(%params);
712 } elsif ($key eq 'num_slots') {
713 $self->info_key_num_slots(%params);
717 sub info_key_fast_search {
721 $params{'info_cb'}->(undef,
722 fast_search => $self->{'fast_search'},
726 sub info_key_vendor_string {
730 $self->{'interface'}->inquiry(make_cb(inquiry_cb => sub {
731 my ($err, $info) = @_;
732 return $self->make_error("fatal", $params{'info_cb'},
733 message => "$err") if $err;
735 my $vendor_string = sprintf "%s %s",
736 ($info->{'vendor id'} or "<unknown>"),
737 ($info->{'product id'} or "<unknown>");
739 $params{'info_cb'}->(undef,
740 vendor_string => $vendor_string,
745 sub info_key_num_slots {
749 $self->_with_updated_state(\%params, 'info_cb',
750 sub { $self->info_key_num_slots_unlocked(@_) });
753 sub info_key_num_slots_unlocked {
756 my $state = $params{'state'};
758 my @allowed_slots = grep { $self->_is_slot_allowed($_) }
759 keys %{$state->{'slots'}};
761 $params{'info_cb'}->(undef, num_slots => scalar @allowed_slots);
764 sub get_interface { # (overridden by subclasses)
766 my ($device_name, $ignore_barcodes) = @_;
769 if (exists $self->{'config'}->{'properties'}->{'mtx'}) {
770 if (@{$self->{'config'}->{'properties'}->{'mtx'}->{'values'}} > 1) {
771 return Amanda::Changer->make_error("fatal", undef,
772 message => "only one value allowed for 'mtx'");
774 $mtx = $self->{'config'}->{'properties'}->{'mtx'}->{'values'}->[0];
776 $mtx = $Amanda::Constants::MTX;
780 return Amanda::Changer->make_error("fatal", undef,
781 message => "no default value for property MTX");
784 return Amanda::Changer::robot::Interface::MTX->new($device_name, $mtx, $ignore_barcodes),
787 # get, configure, and return a new device, or return a changer error
788 sub get_device { # (overridden by subclasses)
790 my ($device_name) = @_;
792 my $device = Amanda::Device->new($device_name);
793 if ($device->status != $DEVICE_STATUS_SUCCESS) {
794 return Amanda::Changer->make_error("fatal", undef,
796 message => "opening '$device_name': " . $device->error_or_status());
799 if (my $err = $self->{'config'}->configure_device($device)) {
800 return Amanda::Changer->make_error("fatal", undef,
812 return if $self->check_error($params{'finished_cb'});
814 $self->_with_updated_state(\%params, 'finished_cb',
815 sub { $self->_set_label_unlocked(@_); });
818 sub _set_label_unlocked {
821 my $state = $params{'state'};
823 # update all of the various pieces of cached information
824 my $drive = $params{'drive'};
825 my $slot = $state->{'drives'}->{$drive}->{'orig_slot'};
826 my $label = $params{'label'};
827 my $barcode = $state->{'drives'}->{$drive}->{'barcode'};
828 my $dev = $params{dev};
830 $state->{'drives'}->{$drive}->{'label'} = $label;
832 $state->{'slots'}->{$slot}->{'state'} = Amanda::Changer::SLOT_FULL;
833 $state->{'slots'}->{$slot}->{'device_status'} = "".$dev->status;
834 if ($dev->status != $DEVICE_STATUS_SUCCESS) {
835 $state->{'slots'}->{$slot}->{'device_error'} = $dev->error;
837 $state->{'slots'}->{$slot}->{'device_error'} = undef;
839 my $volume_header = $dev->volume_header;
840 if (defined $volume_header) {
841 $state->{'slots'}->{$slot}->{'f_type'} = "".$volume_header->{type};
843 $state->{'slots'}->{$slot}->{'f_type'} = undef;
845 $state->{'slots'}->{$slot}->{'label'} = $label;
847 if (defined $barcode) {
848 $state->{'bc2lb'}->{$barcode} = $label;
851 $params{'finished_cb'}->(undef);
858 return if $self->check_error($params{'finished_cb'});
860 $self->_with_updated_state(\%params, 'finished_cb',
861 sub { $self->_release_unlocked(@_); });
864 sub _release_unlocked {
867 my $state = $params{'state'};
868 my $drive = $params{'drive'};
870 # delete the reservation and save the statefile
871 if (!$self->_res_info_is_mine($state->{'drives'}->{$drive}->{'res_info'})) {
872 # this should *never* happen
873 return $self->make_error("fatal", $params{'finished_cb'},
874 message => "reservation belongs to another instance");
876 $state->{'drives'}->{$drive}->{'res_info'} = undef;
878 # bounce off to eject if the user has requested it, using the xx_unlocked
879 # variant since we've already got the statefile open
880 if ($params{'eject'}) {
881 $self->eject_unlocked(
883 finished_cb => $params{'finished_cb'},
887 $params{'finished_cb'}->();
895 return if $self->check_error($params{'finished_cb'});
897 $self->_with_updated_state(\%params, 'finished_cb',
898 sub { $self->reset_unlocked(@_); });
904 my $state = $params{'state'};
906 $state->{'current_slot'} = $self->_get_next_slot($state, -1);
908 $params{'finished_cb'}->();
915 return if $self->check_error($params{'finished_cb'});
917 $self->_with_updated_state(\%params, 'finished_cb',
918 sub { $self->eject_unlocked(@_); });
924 my $state = $params{'state'};
925 my ($drive, $drive_info);
927 return if $self->check_error($params{'finished_cb'});
929 my $steps = define_steps
930 cb_ref => \$params{'finished_cb'};
932 # note that this changer treats "eject" as "unload", which may also require an eject
933 # operation if the eject_before_unload property is set
936 # if drive isn't specified, see if we only have one
937 if (!exists $params{'drive'}) {
938 if ((keys %{$self->{'drive2device'}}) == 1) {
939 $params{'drive'} = (keys %{$self->{'drive2device'}})[0];
941 return $self->make_error("failed", $params{'finished_cb'},
943 message => "no drive specified");
946 $drive = $params{'drive'};
948 $self->_debug("unloading drive $drive");
949 $drive_info = $state->{'drives'}->{$drive};
951 return $self->make_error("failed", $params{'finished_cb'},
953 message => "invalid drive '$drive'");
956 # if the drive exists, but not configured in this changer, then
958 if (!defined $self->{'drive2device'}->{$drive}) {
959 return $self->make_error("failed", $params{'finished_cb'},
961 message => "this changer instance is not configured to access drive $drive");
965 # check for a reservation
966 if ($drive_info->{'res_info'}
967 and $self->_res_info_verify($drive_info->{'res_info'})) {
968 return $self->make_error("failed", $params{'finished_cb'},
969 reason => "volinuse",
970 message => "tape in drive '$drive' is in use");
973 if ($self->{'eject_before_unload'}) {
974 $steps->{'wait_to_eject'}->();
976 $steps->{'wait_to_unload'}->();
980 step wait_to_eject => sub {
981 $self->_after_delay($state, $steps->{'eject'});
985 my $device_name = $self->{'drive2device'}->{$drive};
986 $self->_debug("ejecting $device_name before unload");
988 my $device = $self->get_device($device_name);
989 return $device if $device->isa("Amanda::Changer::Error");
991 if (!$device->eject()) {
992 return $self->make_error("failed", $params{'finished_cb'},
994 message => "while ejecting volume: " . $device->error_or_status);
998 $self->_set_delay($state, $self->{'eject_delay'});
1000 $steps->{'wait_to_unload'}->();
1003 step wait_to_unload => sub {
1004 $self->_after_delay($state, $steps->{'unload'});
1007 step unload => sub {
1008 # find target slot and unload it - note that the target slot may not be
1009 # in the USE-SLOTS list, as it may belong to another config
1010 my $orig_slot = $drive_info->{'orig_slot'};
1011 $self->{'interface'}->unload($drive, $orig_slot, $steps->{'unload_finished'});
1014 step unload_finished => sub {
1018 return $self->make_error("failed", $params{'finished_cb'},
1019 reason => "unknown",
1023 $self->_debug("unload complete");
1024 my $orig_slot = $state->{'drives'}->{$drive}->{'orig_slot'};
1025 $state->{'slots'}->{$orig_slot}->{'state'} = $state->{'drives'}->{$drive}->{'state'};
1026 $state->{'slots'}->{$orig_slot}->{'label'} = $state->{'drives'}->{$drive}->{'label'};
1027 $state->{'slots'}->{$orig_slot}->{'barcode'} = $state->{'drives'}->{$drive}->{'barcode'};
1028 $state->{'slots'}->{$orig_slot}->{'loaded_in'} = undef;
1029 $state->{'drives'}->{$drive}->{'state'} = Amanda::Changer::SLOT_EMPTY;
1030 $state->{'drives'}->{$drive}->{'label'} = undef;
1031 $state->{'drives'}->{$drive}->{'barcode'} = undef;
1032 $state->{'drives'}->{$drive}->{'orig_slot'} = undef;
1034 $self->_set_delay($state, $self->{'unload_delay'});
1035 $params{'finished_cb'}->();
1043 return if $self->check_error($params{'finished_cb'});
1045 $self->_with_updated_state(\%params, 'finished_cb',
1046 sub { $self->update_unlocked(@_); });
1049 sub update_unlocked {
1053 my $state = $params{'state'};
1054 my $set_to_unknown = 0;
1056 return if $self->check_error($params{'finished_cb'});
1058 my $user_msg_fn = $params{'user_msg_fn'};
1059 $user_msg_fn ||= sub { $self->_debug($_[0]); };
1061 my $steps = define_steps
1062 cb_ref => \$params{'finished_cb'};
1064 step handle_assignment => sub {
1065 # check for the SL=LABEL format, and handle it here
1066 if (exists $params{'changed'} and $params{'changed'} =~ /^\d+=\S+$/) {
1067 my ($slot, $label) = ($params{'changed'} =~ /^(\d+)=(\S+)$/);
1069 # let's list the reasons we *can't* do what the user has asked
1071 if (!exists $state->{'slots'}) {
1072 $whynot = "slot $slot does not exist";
1073 } elsif (!$self->_is_slot_allowed($slot)) {
1074 $whynot = "slot $slot is not used by this changer";
1075 } elsif ($state->{'slots'}->{$slot}->{'state'} ==
1076 Amanda::Changer::SLOT_EMPTY) {
1077 $whynot = "slot $slot is empty";
1078 } elsif (defined $state->{'slots'}->{$slot}->{'loaded_in'}) {
1079 $whynot = "slot $slot is currently loaded";
1083 return $self->make_error("failed", $params{'finished_cb'},
1084 reason => "unknown", message => $whynot);
1087 $user_msg_fn->("recoding volume '$label' in slot $slot");
1088 # ok, now erase all knowledge of that label
1089 while (my ($bc, $lb) = each %{$state->{'bc2lb'}}) {
1090 if ($lb eq $label) {
1091 delete $state->{'bc2lb'}->{$bc};
1095 while (my ($sl, $inf) = each %{$state->{'slots'}}) {
1096 if ($inf->{'label'} and $inf->{'label'} eq $label) {
1097 delete $inf->{'device_status'};
1098 delete $inf->{'device_error'};
1099 delete $inf->{'f_type'};
1100 delete $inf->{'label'};
1104 # and add knowledge of the label to the given slot
1105 #$state->{'slots'}->{$slot}->{'device_status'} = $DEVICE_STATUS_SUCCESS;
1106 #$state->{'slots'}->{$slot}->{'f_type'} = $Amanda::Header::F_TAPESTART;
1107 $state->{'slots'}->{$slot}->{'label'} = $label;
1108 if ($state->{'slots'}->{$slot}->{'barcode'}) {
1109 my $bc = $state->{'slots'}->{$slot}->{'barcode'};
1110 $state->{'bc2lb'}->{$bc} = $label;
1113 # that's it -- no changer motion required
1114 return $params{'finished_cb'}->(undef);
1115 } elsif (exists $params{'changed'} and
1116 $params{'changed'} =~ /^(.+)=$/) {
1117 $params{'changed'} = $1;
1118 $set_to_unknown = 1;
1119 $steps->{'calculate_slots'}->($steps->{'set_to_unknown'});
1121 $steps->{'calculate_slots'}->($steps->{'update_slot'});
1125 step calculate_slots => sub {
1126 my ($update_slot_cb) = shift @_;
1127 if (exists $params{'changed'}) {
1128 # parse the string just like use-slots, using a hash for uniqueness
1130 for my $range (split ',', $params{'changed'}) {
1131 my ($first, $last) = ($range =~ /(\d+)(?:-(\d+))?/);
1132 $last = $first unless defined($last);
1133 for ($first .. $last) {
1134 $changed{$_} = undef;
1138 @slots_to_check = keys %changed;
1139 @slots_to_check = grep { exists $state->{'slots'}->{$_} } @slots_to_check;
1141 @slots_to_check = keys %{ $state->{'slots'} };
1144 # limit the update to allowed slots, and sort them so we don't confuse
1145 # the user with a "random" order
1146 @slots_to_check = grep { $self->_is_slot_allowed($_) } @slots_to_check;
1147 @slots_to_check = grep { $state->{'slots'}->{$_}->{'state'} == Amanda::Changer::SLOT_FULL} @slots_to_check;
1148 @slots_to_check = sort { $a <=> $b } @slots_to_check;
1150 $update_slot_cb->();
1153 step set_to_unknown => sub {
1154 return $steps->{'done'}->() if (!@slots_to_check);
1156 my $slot = shift @slots_to_check;
1157 $user_msg_fn->("Removing entry for slot $slot");
1158 if (!defined $state->{'slots'}->{$slot}->{'barcode'}) {
1159 $state->{'slots'}->{$slot}->{'label'} = undef;
1160 $state->{'slots'}->{$slot}->{'device_status'} = undef;
1161 $state->{'slots'}->{$slot}->{'device_error'} = undef;
1162 $state->{'slots'}->{$slot}->{'f_type'} = undef;
1163 if (defined $state->{'slots'}->{$slot}->{'loaded_in'}) {
1164 my $drive = $state->{'slots'}->{$slot}->{'loaded_in'};
1165 $state->{'drives'}->{$drive}->{'label'} = undef;
1166 $state->{'drives'}->{$drive}->{'state'} =
1167 Amanda::Changer::SLOT_FULL;
1170 $steps->{'set_to_unknown'}->();
1173 # TODO: parallelize this if multiple drives are available
1175 step update_slot => sub {
1176 return $steps->{'done'}->() if (!@slots_to_check);
1178 my $slot = shift @slots_to_check;
1179 $user_msg_fn->("scanning slot $slot");
1181 $self->load_unlocked(
1183 res_cb => $steps->{'slot_loaded'},
1187 step slot_loaded => sub {
1188 my ($err, $res) = @_;
1190 return $params{'finished_cb'}->($err);
1193 # load() already fixed up the metadata, so just release; but we have to
1194 # be careful to do an unlocked release.
1196 finished_cb => $steps->{'released'},
1201 step released => sub {
1204 return $params{'finished_cb'}->($err);
1207 $steps->{'update_slot'}->();
1211 $params{'finished_cb'}->(undef);
1219 return if $self->check_error($params{'inventory_cb'});
1221 $self->_with_updated_state(\%params, 'inventory_cb',
1222 sub { $self->inventory_unlocked(@_); });
1225 sub inventory_unlocked {
1228 my $state = $params{'state'};
1230 my @slot_names = sort { $a <=> $b } keys %{ $state->{'slots'} };
1232 for my $slot_name (@slot_names) {
1234 next unless $self->_is_slot_allowed($slot_name);
1235 my $slot = $state->{'slots'}->{$slot_name};
1237 $i->{'slot'} = $slot_name;
1238 $i->{'state'} = $slot->{'state'};
1239 $i->{'device_status'} = $slot->{'device_status'};
1240 $i->{'device_error'} = $slot->{'device_error'};
1241 $i->{'f_type'} = $slot->{'f_type'};
1242 $i->{'label'} = $slot->{'label'};
1243 $i->{'barcode'} = $slot->{'barcode'}
1244 if ($slot->{'barcode'});
1245 if (defined $slot->{'loaded_in'}) {
1246 $i->{'loaded_in'} = $slot->{'loaded_in'};
1247 my $drive = $state->{'drives'}->{$slot->{'loaded_in'}};
1248 if ($drive->{'res_info'} and $self->_res_info_verify($drive->{'res_info'})) {
1249 $i->{'reserved'} = 1;
1252 $i->{'import_export'} = 1
1256 if $slot_name eq $state->{'current_slot'};
1261 $params{'inventory_cb'}->(undef, \@inv);
1268 return if $self->check_error($params{'finished_cb'});
1270 $self->_with_updated_state(\%params, 'finished_cb',
1271 sub { $self->move_unlocked(@_); });
1277 my $state = $params{'state'};
1279 my $from_slot = $params{'from_slot'};
1280 my $to_slot = $params{'to_slot'};
1282 # make sure this is OK
1283 for ($from_slot, $to_slot) {
1284 if (!$self->_is_slot_allowed($_)) {
1285 return $self->make_error("failed", $params{'finished_cb'},
1286 reason => "invalid",
1287 message => "invalid slot $_");
1291 if ($state->{'slots'}->{$from_slot}->{'state'} == Amanda::Changer::SLOT_EMPTY) {
1292 return $self->make_error("failed", $params{'finished_cb'},
1293 reason => "invalid",
1294 message => "slot $from_slot is empty");
1297 my $in_drive = $state->{'slots'}->{$from_slot}->{'loaded_in'};
1298 if (defined $in_drive) {
1299 my $info = $state->{'drives'}->{$in_drive};
1300 if ($info->{'res_info'} and $self->_res_info_verify($info->{'res_info'})) {
1301 return $self->make_error("failed", $params{'finished_cb'},
1302 reason => "invalid",
1303 message => "slot $from_slot is currently loaded and reserved");
1307 if ($state->{'slots'}->{$to_slot}->{'state'} == Amanda::Changer::SLOT_FULL) {
1308 return $self->make_error("failed", $params{'finished_cb'},
1309 reason => "invalid",
1310 message => "slot $to_slot is not empty");
1313 # if the destination slot is loaded, then we could do an "exchange", but
1316 my $transfer_complete = make_cb(transfer_complete => sub {
1318 return $params{'finished_cb'}->($err) if $err;
1321 if ($from_slot ne $to_slot) {
1322 my $f = $state->{'slots'}->{$from_slot};
1323 my $t = $state->{'slots'}->{$to_slot};
1325 $t->{'device_status'} = $f->{'device_status'};
1326 $f->{'device_status'} = undef;
1328 $t->{'state'} = $f->{'state'};
1329 $f->{'state'} = Amanda::Changer::SLOT_EMPTY;
1331 $t->{'f_type'} = $f->{'f_type'};
1332 $f->{'f_type'} = undef;
1334 $t->{'label'} = $f->{'label'};
1335 $f->{'label'} = undef;
1337 $t->{'barcode'} = $f->{'barcode'};
1338 $f->{'barcode'} = undef;
1341 # properly represent the unload operation, if it was performed
1342 if (defined $in_drive) {
1343 $state->{'slots'}->{$from_slot}->{'loaded_in'} = undef;
1344 $state->{'slots'}->{$to_slot}->{'loaded_in'} = undef;
1346 $state->{'drives'}->{$in_drive}->{'state'} =
1347 Amanda::Changer::SLOT_EMPTY;
1348 $state->{'drives'}->{$in_drive}->{'label'} = undef;
1349 $state->{'drives'}->{$in_drive}->{'barcode'} = undef;
1350 $state->{'drives'}->{$in_drive}->{'orig_slot'} = undef;
1353 $params{'finished_cb'}->();
1356 # if the source slot is loaded, then this is just a directed unload operation;
1357 # otherwise, it's a transfer.
1358 if (defined $in_drive) {
1359 Amanda::Debug::debug("move(): unloading drive $in_drive to slot $to_slot");
1360 $self->{'interface'}->unload($in_drive, $to_slot, $transfer_complete);
1362 $self->{'interface'}->transfer($from_slot, $to_slot, $transfer_complete);
1369 # calculate the next highest non-empty slot after $slot (assuming that
1370 # the changer status has been updated)
1371 sub _get_next_slot {
1373 my ($state, $slot) = @_;
1375 my @nonempty = sort { $a <=> $b } grep {
1376 $state->{'slots'}->{$_}->{'state'} == Amanda::Changer::SLOT_FULL
1377 and $self->_is_slot_allowed($_)
1378 } keys(%{$state->{'slots'}});
1380 my @higher = grep { $_ > $slot } @nonempty;
1382 # return the next higher slot, or the first nonempty slot (to loop around)
1383 return $higher[0] if (@higher);
1384 return $nonempty[0];
1387 # is $slot in the slots specified by the use-slots property?
1388 sub _is_slot_allowed {
1392 # if use-slots is not specified, all slots are available
1393 return 1 unless ($self->{'use_slots'});
1395 for my $range (split ',', $self->{'use_slots'}) {
1396 my ($first, $last) = ($range =~ /(\d+)(?:-(\d+))?/);
1397 $last = $first unless defined($last);
1398 return 1 if ($slot >= $first and $slot <= $last);
1404 # add a prefix and call Amanda::Debug::debug
1408 # chg_name is not set until *after* the constructor finishes
1409 my $chg_name = $self->{'chg_name'} || $self->{'class_name'};
1410 debug("$chg_name: $msg");
1416 # Wait until the delay from the last operation has expired, and call the
1417 # given callback with the given arguments
1420 my ($state, $cb, @args) = @_;
1422 confess("undefined \$cb") unless (defined $cb);
1424 # if the current time is before $start, then we'll perform the action anyway; this
1425 # saves us from long delays when clocks fall out of sync or run backward, but delays
1427 my ($start, $end, $now);
1428 $start = $state->{'last_operation_time'};
1429 if (!defined $start) {
1430 return $cb->(@args);
1433 $end = $start + $state->{'last_operation_delay'};
1436 if ($now >= $start and $now < $end) {
1437 Amanda::MainLoop::call_after(1000 * ($end - $now), $cb, @args);
1439 return $cb->(@args);
1443 # set the delay parameters in the statefile
1446 my ($state, $delay) = @_;
1448 $state->{'last_operation_time'} = time;
1449 $state->{'last_operation_delay'} = $delay;
1453 # Statefile management
1455 # wrapper around Amanda::Changer's with_locked_state to lock the statefile and
1456 # then update the state with the results of the 'status' command.
1458 # Like with_locked_state, this method assumes the keyword-based parameter
1459 # style, and adds a 'state' parameter with the new state. Also like
1460 # with_locked_state, it replaces the $cbname key with a wrapped version of that
1461 # callback. It then calls $sub.
1462 sub _with_updated_state {
1464 my ($paramsref, $cbname, $sub) = @_;
1465 my %params = %$paramsref;
1468 my $steps = define_steps
1469 cb_ref => \$paramsref->{$cbname};
1472 $self->with_locked_state($self->{'statefile'},
1473 $params{$cbname}, $steps->{'got_lock'});
1476 step got_lock => sub {
1477 ($state, my $new_cb) = @_;
1479 # set up params for calling through to $sub later
1480 $params{'state'} = $state;
1481 $params{$cbname} = $new_cb;
1483 if (!keys %$state) {
1484 $state->{'slots'} = {};
1485 $state->{'drives'} = {};
1486 $state->{'drive_lru'} = [];
1487 $state->{'bc2lb'} = {};
1488 $state->{'current_slot'} = -1;
1491 # this is for testing ONLY!
1492 $self->{'__last_state'} = $state;
1494 # if it's not time for another run of the status command yet, then just skip to
1496 if (defined $state->{'last_status'}
1497 and time < $state->{'last_status'} + $self->{'status_interval'}) {
1498 $self->_debug("too early for another 'status' invocation");
1499 $steps->{'done'}->();
1501 $steps->{'wait'}->();
1506 $self->_after_delay($state, $steps->{'call_status'});
1509 step call_status => sub {
1510 $self->{'interface'}->status($steps->{'status_cb'});
1513 step status_cb => sub {
1514 my ($err, $status) = @_;
1516 return $self->make_error("fatal", $params{$cbname},
1520 $state->{'last_status'} = time;
1521 $self->_debug("updating state");
1523 # process the results; $status can update our slot->label
1524 # mapping, but the barcode->label mapping stays the same.
1527 my ($drv, $slot, $info);
1529 # note that loaded_in is always undef; it will be set correctly
1530 # when the drives are scanned
1531 while (($slot, $info) = each %{$status->{'slots'}}) {
1532 if ($info->{'empty'}) {
1534 $new_slots->{$slot} = {
1535 state => Amanda::Changer::SLOT_EMPTY,
1536 device_status => undef,
1537 device_error => undef,
1542 ie => $info->{'ie'},
1547 if (defined $info->{'barcode'}) {
1549 my $label = $state->{'bc2lb'}->{$info->{'barcode'}};
1551 $new_slots->{$slot} = {
1552 state => Amanda::Changer::SLOT_FULL,
1553 device_status => $state->{'slots'}->{$slot}->{device_status},
1554 device_error => $state->{'slots'}->{$slot}->{device_error},
1555 f_type => $state->{'slots'}->{$slot}->{f_type},
1557 barcode => $info->{'barcode'},
1559 ie => $info->{'ie'},
1562 # assume the status of this slot has not changed since the last
1563 # time we looked at it, although mark it as not loaded in a slot
1564 if (exists $state->{'slots'}->{$slot}) {
1565 $new_slots->{$slot} = $state->{'slots'}->{$slot};
1566 $new_slots->{$slot}->{'loaded_in'} = undef;
1568 $new_slots->{$slot} = {
1569 state => Amanda::Changer::SLOT_FULL,
1570 device_status => undef,
1571 device_error => undef,
1576 ie => $info->{'ie'},
1581 my $old_slots_state = $state->{'slots'};
1582 $state->{'slots'} = $new_slots;
1584 # now handle the drives
1585 my $new_drives = {};
1586 while (($drv, $info) = each %{$status->{'drives'}}) {
1587 my $old_drive = $state->{'drives'}->{$drv};
1589 # if this drive still has a valid reservation, don't change it
1590 if (defined $old_drive->{'res_info'}
1591 and $self->_res_info_verify($old_drive->{'res_info'})) {
1592 $new_drives->{$drv} = $old_drive;
1596 # if the drive is empty, this is pretty easy
1597 if (!defined $info) {
1598 $new_drives->{$drv} = {
1599 state => Amanda::Changer::SLOT_EMPTY,
1607 # trust our own orig_slot over that from the changer, if possible,
1608 # as some changers do not report this information accurately
1609 my ($orig_slot, $label);
1610 if (defined $old_drive->{'orig_slot'}) {
1611 $orig_slot = $old_drive->{'orig_slot'};
1612 $label = $old_drive->{'label'};
1615 # but don't trust it if the barcode has changed
1616 if (defined $info->{'barcode'}
1617 and defined $old_drive->{'barcode'}
1618 and $info->{'barcode'} ne $old_drive->{'barcode'}) {
1623 # get the robot's notion of the original slot if we don't know ourselves
1624 if (!defined $orig_slot) {
1625 $orig_slot = $info->{'orig_slot'};
1628 # but if there's a tape in that slot, then we've got a problem
1629 if (defined $orig_slot
1630 and $state->{'slots'}->{$orig_slot}->{'state'} != Amanda::Changer::SLOT_EMPTY) {
1631 warning("mtx indicates tape in drive $drv should go to slot $orig_slot, " .
1632 "but that slot is not empty.");
1634 for my $slot (keys %{ $state->{'slots'} }) {
1635 if ($state->{'slots'}->{$slot}->{'state'} == Amanda::Changer::SLOT_EMPTY) {
1640 if (!defined $orig_slot) {
1641 warning("cannot find an empty slot for the tape in drive $drv");
1645 # and look up the label by barcode if possible
1646 if (!defined $label && defined $info->{'barcode'}) {
1647 $label = $state->{'bc2lb'}->{$info->{'barcode'}};
1650 $new_drives->{$drv} = {
1651 state => Amanda::Changer::SLOT_FULL,
1653 barcode => $info->{'barcode'},
1654 orig_slot => $orig_slot,
1657 $state->{'drives'} = $new_drives;
1659 # update the loaded_in info for the relevant slots
1660 while (($drv, $info) = each %$new_drives) {
1661 # also update the slots with the relevant 'loaded_in' info
1662 if (defined $info->{'orig_slot'}) {
1663 my $old_state = $old_slots_state->{$info->{'orig_slot'}};
1664 $state->{'slots'}->{$info->{'orig_slot'}} = {
1665 state => $info->{'state'},
1666 device_status => $old_state->{'device_status'},
1667 device_error => $old_state->{'device_error'},
1668 f_type => $old_state->{'f_type'},
1669 label => $info->{'label'},
1670 barcode => $info->{'barcode'},
1676 # sanity check that we don't have tape-device info for nonexistent drives
1677 for my $dr (@{$self->{'driveorder'}}) {
1678 if (!exists $state->{'drives'}->{$dr}) {
1679 warning("tape-device property specified for drive $dr, but no such " .
1680 "drive exists in the library");
1684 if ($state->{'current_slot'} == -1) {
1685 $state->{'current_slot'} = $self->_get_next_slot($state, -1);
1688 $steps->{'done'}->();
1692 # finally, call through to the user's method; $params{$cbname} has been
1693 # properly patched to release the state lock when this method is done.
1699 # reservation records
1701 # A reservation record is recorded in the statefile, and is distinct from an
1702 # Amanda::Changer::robot:Reservation object in that it is seen by all users of
1703 # the tape device, whether in this process or another.
1705 # This is abstracted out to enable support for a more robust mechanism than
1710 return { pid => $$, };
1713 sub _res_info_verify {
1715 my ($res_info) = @_;
1717 # true if this is our reservation
1718 return 1 if ($res_info->{'pid'} == $$);
1720 # or if the process is dead
1721 return kill 0, $res_info->{'pid'};
1724 sub _res_info_is_mine {
1726 my ($res_info) = @_;
1728 return 1 if ($res_info and $res_info->{'pid'} == $$);
1731 package Amanda::Changer::robot::Reservation;
1732 use vars qw( @ISA );
1733 use Amanda::Debug qw( debug warning );
1734 @ISA = qw( Amanda::Changer::Reservation );
1738 my ($chg, $slot, $drive, $device, $barcode) = @_;
1739 my $self = Amanda::Changer::Reservation::new($class);
1741 $self->{'chg'} = $chg;
1743 $self->{'drive'} = $drive;
1744 $self->{'device'} = $device;
1745 $self->{'this_slot'} = $slot;
1746 $self->{'barcode'} = $barcode;
1755 # if we're in global cleanup and the changer is already dead,
1757 return unless $self->{'chg'};
1759 # unref the device, for good measure
1760 $self->{'device'} = undef;
1762 # punt this method off to the changer itself, optionally calling
1763 # the unlocked version if we have the 'state' parameter
1764 if (exists $params{'unlocked'} and exists $params{'state'}) {
1765 $self->{'chg'}->_release_unlocked(drive => $self->{'drive'}, %params);
1767 $self->{'chg'}->_release(drive => $self->{'drive'}, %params);
1775 return unless $self->{'chg'};
1776 $self->{'chg'}->_set_label(drive => $self->{'drive'},
1777 dev => $self->{device}, %params);
1780 package Amanda::Changer::robot::Interface;
1782 # The physical interface to the changer is abstracted out to allow several
1783 # implementations (see chg-ndmp for one of them). This API is "reasonably
1784 # stable", but is really only known to this changer and its subclasses, so it's
1785 # not documented in POD. The methods are:
1787 # $iface->inquiry($inquiry_cb);
1789 # Inquire as to the relevant information about the changer. The result is a
1790 # hash table of lowercased key names and values, $info. The inquiry_cb is
1791 # called as $inquiry_cb->($errmsg, $info). The resulting strings have quotes
1792 # and whitespace stripped. Keys include 'vendor id' and 'product id'.
1794 # $iface->status($status_cb)
1796 # Get the READ ELEMENT STATUS output for the changer. The status_cb is called
1797 # as $status_cb->($errmsg, $status). $status is a hash with keys 'drives' and
1798 # 'slots', each of which is a hash indexed by the element address (note that drive
1799 # element addresses can and usually do overlap with slots. The values of the slots
1800 # hash are hashes with keys
1801 # - 'empty' (1 if the slot is empty)
1802 # - 'barcode' (which may be undef if the changer does not support barcodes)
1803 # - 'ie' (a boolean indicating whether this is an import/export slot).
1804 # The values of the drives are undef for empty drive, or hashes with keys
1805 # - 'barcode' (which may be undef if the changer does not support barcodes)
1806 # - 'orig_slot' (slot from which this volume was taken, if known)
1808 # $iface->load($slot, $drive, $finished_cb)
1810 # Load $slot into $drive. The finished_cb gets a single argument, $error,
1811 # which is only defined if an error occurred. Note that this does not
1812 # necessarily wait until the load operation is complete (most drives give
1813 # no such indication) (this method also implements unload, if $un=1)
1815 # $iface->unload($drive, $slot, $finished_cb);
1817 # Unload $drive into $slot. Finished_cb is just as for load().
1819 # $iface->eject($drive_name, $finished_cb);
1821 # Eject $drive_name (named /dev/whatever, not the drive number), and call finished_cb.
1823 # $iface->transfer($src_slot, $dst_slot, $finished_cb);
1825 # Move the tape in $src_slot into $dst_slot. The finished_cb gets a single
1826 # argument, $error, which is only defined if an error occurred. Note that this
1827 # does not necessarily wait until the load operation is complete.
1829 package Amanda::Changer::robot::Interface::MTX;
1832 use Amanda::Config qw( :getconf );
1833 use Amanda::Debug qw( debug warning );
1834 use Amanda::MainLoop qw( :GIOCondition synchronized make_cb define_steps step );
1835 use Amanda::Device qw( :constants );
1839 my ($device_name, $mtx, $ignore_barcodes) = @_;
1841 unless (-e $device_name) {
1842 return Amanda::Changer->make_error("fatal", undef,
1843 message => "'$device_name' not found");
1847 # This object uses a big lock to block *all* operations, not just mtx
1848 # invocations. This allows us to add delays to certain operations, while still
1851 device_name => $device_name,
1853 ignore_barcodes => $ignore_barcodes,
1859 my ($inquiry_cb) = @_;
1861 synchronized($self->{'lock'}, $inquiry_cb, sub {
1862 my ($inquiry_cb) = @_;
1863 my $sys_cb = make_cb(sys_cb => sub {
1864 my ($exitstatus, $output) = @_;
1865 if ($exitstatus != 0) {
1866 return $inquiry_cb->("error from mtx: " . $output, {});
1869 for my $line (split '\n', $output) {
1870 if (my ($k, $v) = ($line =~ /^(.*):\s*(.*)$/)) {
1871 $v =~ s/^'(.*)'$/$1/;
1876 return $inquiry_cb->(undef, \%info);
1880 $self->_run_system_command($sys_cb,
1881 $self->{'mtx'}, "-f", $self->{'device_name'}, 'inquiry');
1887 my ($status_cb) = @_;
1889 synchronized($self->{'lock'}, $status_cb, sub {
1890 my ($status_cb) = @_;
1891 my ($counter) = 120;
1894 my $run_mtx = make_cb(run_mtx => sub {
1895 my @nobarcode = ('nobarcode') if $self->{'ignore_barcodes'};
1896 $self->_run_system_command($sys_cb,
1897 $self->{'mtx'}, "-f", $self->{'device_name'}, @nobarcode,
1901 $sys_cb = make_cb(sys_cb => sub {
1902 my ($exitstatus, $output) = @_;
1903 if ($exitstatus != 0) {
1905 # if it's a regular SCSI error, just show the sense key
1906 my ($sensekey) = ($err =~ /mtx: Request Sense: Sense Key=(.*)\n/);
1907 $err = "SCSI error; Sense Key=$sensekey" if $sensekey;
1909 if ($sensekey eq "Not Ready" and $counter > 0) {
1911 return Amanda::MainLoop::call_after(1000, $run_mtx);
1913 return $status_cb->("error from mtx: " . $err, {});
1916 for my $line (split '\n', $output) {
1917 my ($slot, $ie, $slinfo);
1919 # drives (data transfer elements)
1920 if (($slot, $slinfo) = ($line =~
1921 /^Data Transfer Element\s*(\d+)?\s*:\s*(.*)/i)) {
1922 # assume 0 when not given a drive #
1923 $slot = 0 unless defined $slot;
1924 if ($slinfo =~ /^Empty/i) {
1925 $status{'drives'}->{$slot} = undef;
1926 } elsif ($slinfo =~ /^Full/i) {
1927 my ($barcode, $orig_slot);
1928 ($barcode) = ($slinfo =~ /:VolumeTag\s*=\s*(\S+)/i);
1929 ($orig_slot) = ($slinfo =~ /\(Storage Element (\d+) Loaded\)/i);
1930 $status{'drives'}->{$slot} = {
1931 barcode => $barcode,
1932 orig_slot => $orig_slot,
1936 # slots (storage elements)
1937 } elsif (($slot, $ie, $slinfo) = ($line =~
1938 /^\s*Storage Element\s*(\d+)\s*(IMPORT\/EXPORT)?\s*:\s*(.*)/i)) {
1940 if ($slinfo =~ /^Empty/i) {
1941 $status{'slots'}->{$slot} = {
1945 } elsif ($slinfo =~ /^Full/i) {
1947 ($barcode) = ($slinfo =~ /:VolumeTag\s*=\s*(\S+)/i)
1948 unless ($self->{'ignore_barcodes'});
1949 $status{'slots'}->{$slot} = {
1950 barcode => $barcode,
1957 return $status_cb->(undef, \%status);
1967 my ($slot, $drive, $finished_cb, $un) = @_;
1969 synchronized($self->{'lock'}, $finished_cb, sub {
1970 my ($finished_cb) = @_;
1972 my $sys_cb = make_cb(sys_cb => sub {
1973 my ($exitstatus, $output) = @_;
1974 if ($exitstatus != 0) {
1975 return $finished_cb->("error from mtx: " . $output);
1977 return $finished_cb->(undef);
1982 $self->_run_system_command($sys_cb,
1983 $self->{'mtx'}, "-f", $self->{'device_name'},
1984 $un? 'unload':'load', $slot, $drive);
1990 my ($drive, $slot, $finished_cb) = @_;
1991 return $self->load($slot, $drive, $finished_cb, 1);
1996 my ($src_slot, $dst_slot, $finished_cb) = @_;
1998 synchronized($self->{'lock'}, $finished_cb, sub {
1999 my ($finished_cb) = @_;
2001 my $sys_cb = make_cb(sys_cb => sub {
2002 my ($exitstatus, $output) = @_;
2003 if ($exitstatus != 0) {
2004 return $finished_cb->("error from mtx: " . $output);
2006 return $finished_cb->(undef);
2010 $self->_run_system_command($sys_cb,
2011 $self->{'mtx'}, "-f", $self->{'device_name'},
2012 'transfer', $src_slot, $dst_slot);
2016 # Run 'mtx' and capture the output. Standard output and error
2017 # are lumped together.
2019 # @param $sys_cb: called with ($exitstatus, $output)
2020 # @param @args: args to pass to exec()
2021 sub _run_system_command {
2022 my ($self, $sys_cb, @args) = @_;
2024 debug("invoking " . join(" ", @args));
2026 my ($readfd, $writefd) = POSIX::pipe();
2027 if (!defined($writefd)) {
2028 die("Error creating pipe: $!");
2032 if (!defined($pid) or $pid < 0) {
2033 die("Can't fork to run changer script: $!");
2039 # get our file-handle house in order
2040 POSIX::close($readfd);
2041 POSIX::dup2($writefd, 1);
2042 POSIX::dup2($writefd, 2);
2043 POSIX::close($writefd);
2045 %ENV = Amanda::Util::safe_env();
2047 { exec { $args[0] } @args; } # braces protect against warning
2053 # clean up file descriptors from the fork
2054 POSIX::close($writefd);
2056 # the callbacks that follow share these lexical variables
2058 my $child_output = '';
2060 my $child_exit_status = 0;
2061 my ($fdsrc, $cwsrc);
2062 my $open_sources = 0;
2064 my $steps = define_steps
2069 Amanda::MainLoop::fd_source($readfd, $G_IO_IN | $G_IO_ERR | $G_IO_HUP)
2070 ->set_callback($steps->{'fd_source_cb'});
2073 Amanda::MainLoop::child_watch_source($pid)
2074 ->set_callback($steps->{'child_watch_source_cb'});
2077 step immediate => 1,
2078 fd_source_cb => sub {
2081 $len = POSIX::read($readfd, $bytes, 1024);
2083 # if we got an EOF, shut things down.
2086 POSIX::close($readfd);
2088 $fdsrc = undef; # break a reference loop
2089 $steps->{'maybe_finished'}->();
2091 # otherwise, just keep the bytes
2092 $child_output .= $bytes;
2096 step immediate => 1,
2097 child_watch_source_cb => sub {
2098 my ($cwsrc, $got_pid, $got_status) = @_;
2100 $cwsrc = undef; # break a reference loop
2102 $child_exit_status = $got_status;
2104 $steps->{'maybe_finished'}->();
2107 step maybe_finished => sub {
2108 return if --$open_sources;
2110 # everything is finished -- process the results and invoke the callback
2111 chomp $child_output;
2113 # let the callback take care of any further interpretation
2114 my $exitval = POSIX::WEXITSTATUS($child_exit_status);
2115 $sys_cb->($exitval, $child_output);