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 # f_type - The f_type of the header
72 # label - volume label, if known
73 # barcode - volume barcode, if available
74 # loaded_in - drive this volume is loaded in
75 # ie - 1 if this is an import/export slot
76 # note that this state pretends that a tape physically located
77 # in a tape drive is still located in its original slot.
79 # The 'drives' key is also a hash by drive numbere, the values of
80 # which are hashes with keys
81 # state - SLOT_FULL/SLOT_EMPTY/SLOT_UNKNOWN
82 # label - volume label
83 # barcode - volume barcode
84 # orig_slot - slot from which this tape was loaded
88 # This package uses Amanda::Changer's with_locked_state to lock a statefile and
89 # load its contents. Every time the state is locked, the package also
90 # considers running 'status' to update the state; the status_interval protects
91 # against running status too often.
93 # Each changer method has an "_unlocked" version that does the actual work, and
94 # is called with an additional 'state' parameter containing the locked state.
95 # This is particularly useful when the load method calls the eject method to
96 # empty a drive that it wants to use.
100 # Reservations are currently represented by a PID in the state file. If that
101 # pid is no longer running, then the reservation is considered stale and is
102 # discarded (with a warning).
104 # Reservation objects defer most of the interesting operations back to the
105 # changer itself, since the operations require locked access to the state.
109 # All of the operating-system-specific functionality is abstracted into the
110 # Interface class. This is written in such a way that it could be replaced
111 # by a direct SCSI interface.
115 my ($config, $tpchanger) = @_;
117 # strip the "chg-foo:" prefix from $tpchanger
118 my $device_name = $tpchanger;
119 $device_name =~ s/^[^:]*://;
121 # get the 'chg-foo' form of this changer script
122 my $class_name = $class;
123 $class_name =~ s/Amanda::Changer::/chg-/;
127 device_name => $device_name,
130 # set below from properties
132 drive2device => {}, # { drive => device name }
133 driveorder => [], # order of tape-device properties
134 drive_choice => 'lru',
135 eject_before_unload => 0,
138 status_interval => 2, # in seconds
139 load_poll => [0, 2, 120], # delay, poll, until
140 eject_delay => 0, # in seconds
141 unload_delay => 0, # in seconds
142 class_name => $class_name,
144 bless ($self, $class);
146 # handle some config and properties
147 my $properties = $config->{'properties'};
149 if (defined $config->{'changerdev'} and $config->{'changerdev'} ne '') {
150 return Amanda::Changer->make_error("fatal", undef,
151 message => "'changerdev' is not allowed with $self->{class_name}");
154 if ($config->{'changerfile'}) {
155 $self->{'statefile'} = Amanda::Config::config_dir_relative($config->{'changerfile'});
157 my $safe_filename = "$self->{class_name}:$device_name";
158 $safe_filename =~ tr/a-zA-Z0-9/-/cs;
159 $safe_filename =~ s/^-*//;
160 $self->{'statefile'} = "$localstatedir/amanda/$safe_filename";
162 $self->_debug("using statefile '$self->{statefile}'");
164 # figure out the drive number to device name mapping
165 if (exists $config->{'tapedev'}
166 and $config->{'tapedev'} ne ''
167 and !exists $properties->{'tape-device'}) {
168 # if the tapedev points to us (the changer), then give an error
169 if ($config->{'tapedev'} eq $tpchanger) {
170 return Amanda::Changer->make_error("fatal", undef,
171 message => "must specify a tape-device property");
173 $self->{'drive2device'} = { '0' => $config->{'tapedev'} };
174 push @{$self->{'driveorder'}}, '0';
176 if (!exists $properties->{'tape-device'}) {
177 return Amanda::Changer->make_error("fatal", undef,
178 message => "no 'tape-device' property specified");
180 for my $pval (@{$properties->{'tape-device'}->{'values'}}) {
181 my ($drive, $device);
182 unless (($drive, $device) = ($pval =~ /(\d+)=(.*)/)) {
183 return Amanda::Changer->make_error("fatal", undef,
184 message => "invalid 'tape-device' property '$pval'");
186 if (exists $self->{'drive2device'}->{$drive}) {
187 return Amanda::Changer->make_error("fatal", undef,
188 message => "tape-device drive $drive defined more than once");
190 $self->{'drive2device'}->{$drive} = $device;
191 push @{$self->{'driveorder'}}, $drive;
195 # eject-before-unload
196 my $ebu = $self->get_boolean_property($self->{'config'},
197 "eject-before-unload", 0);
199 return Amanda::Changer->make_error("fatal", undef,
200 message => "invalid 'eject-before-unload' value");
202 $self->{'eject_before_unload'} = $ebu;
205 my $fast_search = $self->get_boolean_property($self->{'config'},
207 if (!defined $fast_search) {
208 return Amanda::Changer->make_error("fatal", undef,
209 message => "invalid 'fast-search' value");
211 $self->{'fast_search'} = $fast_search;
214 if (exists $properties->{'use-slots'}) {
215 $self->{'use_slots'} = join ",", @{$properties->{'use-slots'}->{'values'}};
216 if ($self->{'use_slots'} !~ /\d+(-\d+)?(,\d+(-\d+)?)*/) {
217 return Amanda::Changer->make_error("fatal", undef,
218 message => "invalid 'use-slots' value '$self->{use_slots}'");
223 if (exists $properties->{'drive-choice'}) {
224 my $pval = $properties->{'drive-choice'}->{'values'}->[0];
225 if (!grep { lc($_) eq $pval } ('lru', 'firstavail')) {
226 return Amanda::Changer->make_error("fatal", undef,
227 message => "invalid 'drive-choice' value '$pval'");
229 $self->{'drive_choice'} = $pval;
234 next unless exists $config->{'properties'}->{'load-poll'};
235 if (@{$config->{'properties'}->{'load-poll'}->{'values'}} > 1) {
236 return Amanda::Changer->make_error("fatal", undef,
237 message => "only one value allowed for 'load-poll'");
239 my $propval = $config->{'properties'}->{'load-poll'}->{'values'}->[0];
240 my ($delay, $delayu, $poll, $pollu, $until, $untilu) = ($propval =~ /^
252 if (!defined $delay) {
253 return Amanda::Changer->make_error("fatal", undef,
254 message => "invalid delay value '$propval' for 'load-poll'");
257 $delay *= 60 if (defined $delayu and $delayu =~ /m/i);
259 $poll = 0 unless defined $poll;
260 $poll *= 60 if (defined $pollu and $pollu =~ /m/i);
262 $until = 0 unless defined $until;
263 $until *= 60 if (defined $untilu and $untilu =~ /m/i);
265 $self->{'load_poll'} = [ $delay, $poll, $until ];
268 # status-interval, eject-delay, unload-delay
269 for my $propname qw(status-interval eject-delay unload-delay) {
270 next unless exists $config->{'properties'}->{$propname};
271 if (@{$config->{'properties'}->{$propname}->{'values'}} > 1) {
272 return Amanda::Changer->make_error("fatal", undef,
273 message => "only one value allowed for $propname");
275 my $propval = $config->{'properties'}->{$propname}->{'values'}->[0];
276 my ($time, $timeu) = ($propval =~ /^(\d+)([ms]?)/ix);
278 if (!defined $time) {
279 return Amanda::Changer->make_error("fatal", undef,
280 message => "invalid time value '$propval' for '$propname'");
283 $time *= 60 if (defined $timeu and $timeu =~ /m/i);
287 $self->{$key} = $time;
290 my $ignore_barcodes = $self->get_boolean_property($self->{'config'},
291 "ignore-barcodes", 0);
292 if (!defined $ignore_barcodes) {
293 return Amanda::Changer->make_error("fatal", undef,
294 message => "invalid 'ignore-barcodes' value");
297 # get the interface, returning an error if we get one
298 $self->{'interface'} = $self->get_interface($device_name, $ignore_barcodes);
299 return $self->{'interface'}
300 if ($self->{'interface'}->isa("Amanda::Changer::Error"));
308 $self->validate_params('load', \%params);
310 return if $self->check_error($params{'res_cb'});
312 $self->_with_updated_state(\%params, 'res_cb', sub { $self->load_unlocked(@_) });
318 my ($slot, $drive, $need_unload);
319 my $state = $params{'state'};
321 my $steps = define_steps
322 cb_ref => \$params{'res_cb'};
324 step calculate_slot => sub {
325 # make sure the slot is numeric
326 if (exists $params{'slot'}) {
327 if ($params{'slot'} =~ /^\d+$/) {
328 $params{'slot'} = $params{'slot'}+0;
330 return $self->make_error("failed", $params{'res_cb'},
332 message => "invalid slot '$params{slot}'");
336 if (exists $params{'relative_slot'}) {
337 if ($params{'relative_slot'} eq "next") {
338 if (exists $params{'slot'}) {
339 $slot = $self->_get_next_slot($state, $params{'slot'});
340 $self->_debug("loading next relative to $params{slot}: $slot");
342 $slot = $self->_get_next_slot($state, $state->{'current_slot'});
343 $self->_debug("loading next relative to current slot: $slot");
346 return $self->make_error("failed", $params{'res_cb'},
348 message => "could not find next slot");
350 } elsif ($params{'relative_slot'} eq "current") {
351 $slot = $state->{'current_slot'};
353 # seek to the first slot
354 $slot = $self->_get_next_slot($state, $state->{'current_slot'});
357 return $self->make_error("failed", $params{'res_cb'},
359 message => "no current slot");
362 return $self->make_error("failed", $params{'res_cb'},
364 message => "invalid relative_slot '$params{relative_slot}'");
367 } elsif (exists $params{'slot'}) {
368 $slot = $params{'slot'};
369 $self->_debug("loading slot '$params{slot}'");
371 if (!defined $slot or !exists $state->{'slots'}->{$slot}) {
372 return $self->make_error("failed", $params{'res_cb'},
374 message => "invalid slot '$slot'");
377 } elsif (exists $params{'label'}) {
378 $self->_debug("loading label '$params{label}'");
379 while (my ($sl, $info) = each(%{$state->{'slots'}})) {
380 if (defined $info->{'label'} and $info->{'label'} eq $params{'label'}) {
386 if (!defined $slot) {
387 return $self->make_error("failed", $params{'res_cb'},
388 reason => "notfound",
389 message => "label '$params{label}' not recognized or not found");
393 return $self->make_error("failed", $params{'res_cb'},
395 message => "no 'slot' or 'label' specified to load()");
398 if (!$self->_is_slot_allowed($slot)) {
399 if (exists $params{'label'}) {
400 return $self->make_error("failed", $params{'res_cb'},
402 message => "label '$params{label}' is in slot $slot, which is " .
403 "not in use-slots ($self->{use_slots})");
405 return $self->make_error("failed", $params{'res_cb'},
407 message => "slot $slot not in use-slots ($self->{use_slots})");
411 if (exists $params{'except_slots'} and exists $params{'except_slots'}->{$slot}) {
412 return $self->make_error("failed", $params{'res_cb'},
413 reason => "notfound",
414 message => "all slots have been loaded");
417 if ($state->{'slots'}->{$slot}->{'state'} eq Amanda::Changer::SLOT_EMPTY) {
418 return $self->make_error("failed", $params{'res_cb'},
419 reason => "notfound",
420 message => "slot $slot is empty");
423 return $steps->{'calculate_drive'}->();
426 step calculate_drive => sub {
430 # see if the tape is already in a drive
431 $drive = $state->{'slots'}->{$slot}->{'loaded_in'};
432 if (defined $drive) {
433 $self->_debug("requested volume is already in drive $drive");
434 my $info = $state->{'drives'}->{$drive};
436 # if it's reserved, it can't be used
437 if ($info->{'res_info'} and $self->_res_info_verify($info->{'res_info'})) {
438 return $self->make_error("failed", $params{'res_cb'},
439 reason => "volinuse",
441 message => "the requested volume is in use (drive $drive)");
444 # if it's not reserved, but not in our list of drives, well, it still
446 if (!exists $self->{'drive2device'}->{$drive}) {
447 return $self->make_error("failed", $params{'res_cb'},
448 # not 'volinuse' because we can't expect the tape to be magically
449 # unloaded any time soon -- it's not actually in use, just inaccessible
451 message => "the requested volume is in drive $drive, which this " .
452 "changer instance cannot access");
455 # otherwise, we can jump all the way to the end of this process
456 return $steps->{'check_device'}->();
459 # here is where we implement each of the drive-selection algorithms
461 if ($self->{'drive_choice'} eq 'lru') {
462 my %lru = map { $_, 1 } @{$state->{'drive_lru'}};
463 my @unused = grep { ! exists $lru{$_} } @{$self->{'driveorder'}};
465 # search through unused drives, then the LRU list
466 @check_order = (@unused, @{$state->{'drive_lru'}});
467 } elsif ($self->{'drive_choice'} eq 'firstavail') {
468 # just the drive order, so we tend to prefer the first drive in
470 @check_order = (@{$self->{'driveorder'}});
472 # the constructor should detect this circumstance
473 die "invalid drive_choice";
477 for my $dr (@check_order) {
478 my $info = $state->{'drives'}->{$dr};
479 next unless defined $info;
480 next if exists $checked{$dr}; # don't check drives repeatedly
483 # skip drives we don't have rights to use
484 next unless exists $self->{'drive2device'}->{$dr};
486 # skip reserved drives
487 if ($info->{'res_info'}) {
488 if ($self->_res_info_verify($info->{'res_info'})) {
489 # this is a valid reservation -> skip this drive
490 $self->_debug("skipping drive $dr - already reserved");
493 warning("invalidating stale reservation on drive $dr");
494 $info->{'res_info'} = undef;
498 # otherwise, the drive is available, so use it (whether it contains
501 if ($info->{'state'} != Amanda::Changer::SLOT_EMPTY) {
507 if (!defined $drive) {
508 return $self->make_error("failed", $params{'res_cb'},
509 reason => "driveinuse",
510 message => "no drives available");
513 # remove this drive from the lru and put it at the end
514 $state->{'drive_lru'} = [ grep { $_ ne $drive } @{$state->{'drive_lru'}} ];
515 push @{$state->{'drive_lru'}}, $drive;
517 $self->_debug("using drive $drive");
519 $steps->{'wait_to_start'}->();
522 step wait_to_start => sub {
523 $self->_after_delay($state, $steps->{'start_operation'});
526 step start_operation => sub {
527 # $need_unload is set in $steps->{calculate_drive}
529 $steps->{'start_eject'}->();
531 $steps->{'start_load'}->();
535 step start_eject => sub {
536 # we use the 'eject' method to unload here -- it ejects the volume
537 # if the configuration calls for it, then puts the volume away in its
539 $self->eject_unlocked(
540 finished_cb => $steps->{'eject_finished'},
545 step eject_finished => sub {
549 return $params{'res_cb'}->($err);
552 $steps->{'wait_to_load'}->();
555 step wait_to_load => sub {
556 $self->_after_delay($state, $steps->{'start_load'});
559 step start_load => sub {
560 # $slot and $drive are set
561 $self->{'interface'}->load($slot, $drive, $steps->{'load_finished'});
564 step load_finished => sub {
565 # $slot and $drive are set
569 return $self->make_error("failed", $params{'res_cb'},
574 $steps->{'start_polling'}->();
577 my ($next_poll, $last_poll);
578 step start_polling => sub {
579 my ($delay, $poll, $until) = @{ $self->{'load_poll'} };
581 $next_poll = $now + $delay;
582 $last_poll = $now + $until;
584 return Amanda::MainLoop::call_after(1000 * ($next_poll - $now), $steps->{'check_device'});
587 step check_device => sub {
588 my $device_name = $self->{'drive2device'}->{$drive};
589 die "drive $drive not found in drive2device" unless $device_name; # shouldn't happen
591 $self->_debug("polling '$device_name' to see if it's ready");
593 my $device = $self->get_device($device_name);
594 return $params{'res_cb'}->($device) if $device->isa("Amanda::Changer::Error");
597 $device->read_label();
599 # see if the device thinks it's possible it's busy or empty
600 if ($device->status & $DEVICE_STATUS_VOLUME_MISSING
601 or $device->status & $DEVICE_STATUS_DEVICE_BUSY) {
602 # device is not ready -- set up for the next polling step
603 my ($delay, $poll, $until) = @{ $self->{'load_poll'} };
606 $next_poll = $now + 1 if ($next_poll < $now);
607 if ($poll != 0 and $next_poll < $last_poll) {
608 return Amanda::MainLoop::call_after(
609 1000 * ($next_poll - $now), $steps->{'check_device'});
612 # (fall through if we're done polling)
615 if ($device->status == $DEVICE_STATUS_SUCCESS) {
616 $label = $device->volume_label;
617 } elsif ($device->status & $DEVICE_STATUS_VOLUME_UNLABELED) {
620 return $self->make_error("fatal", $params{'res_cb'},
621 message => "while waiting for '$device_name' to become ready: "
622 . $device->error_or_status());
626 $steps->{'make_res'}->($device, $label);
629 step make_res => sub {
630 my ($device, $label) = @_;
632 # check the label against the desired label, in case this isn't the
634 if ($label and $params{'label'} and $label ne $params{'label'}) {
635 $self->_debug("Expected label '$params{label}', but got '$label'");
637 # update metadata with this new information
638 $state->{'slots'}->{$slot}->{'state'} = Amanda::Changer::SLOT_FULL;
639 $state->{'slots'}->{$slot}->{'device_status'} = $device->status;
640 if (defined $device->{'header'}) {
641 $state->{'slots'}->{$slot}->{'f_type'} = $device->{'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 if (defined $device->{'header'}) {
663 $state->{'slots'}->{$slot}->{'f_type'} = $device->{'header'}->{type};
665 $state->{'slots'}->{$slot}->{'f_type'} = undef;
667 $state->{'slots'}->{$slot}->{'label'} = undef;
668 if ($state->{'slots'}->{$slot}->{'barcode'}) {
669 delete $state->{'bc2lb'}->{$state->{'slots'}->{$slot}->{'barcode'}};
672 return $self->make_error("failed", $params{'res_cb'},
673 reason => "notfound",
674 message => "Found unlabeled tape while looking for '$params{label}'");
677 my $res = Amanda::Changer::robot::Reservation->new($self, $slot, $drive,
678 $device, $state->{'slots'}->{$slot}->{'barcode'});
680 # mark this as reserved
681 $state->{'drives'}->{$drive}->{'res_info'} = $self->_res_info_new();
683 # update our state before returning
684 $state->{'slots'}->{$slot}->{'loaded_in'} = $drive;
685 $state->{'drives'}->{$drive}->{'orig_slot'} = $slot;
686 $state->{'slots'}->{$slot}->{'label'} = $label;
687 $state->{'drives'}->{$drive}->{'label'} = $label;
688 $state->{'drives'}->{$drive}->{'state'} = Amanda::Changer::SLOT_FULL;
689 $state->{'drives'}->{$drive}->{'barcode'} = $state->{'slots'}->{$slot}->{'barcode'};
690 #$state->{'slots'}->{$slot}->{'device_status'} = 9;
691 if ($label and $state->{'slots'}->{$slot}->{'barcode'}) {
692 $state->{'bc2lb'}->{$state->{'slots'}->{$slot}->{'barcode'}} = $label;
694 if ($params{'set_current'}) {
695 $self->_debug("setting current slot to $slot");
696 $state->{'current_slot'} = $slot;
699 return $params{'res_cb'}->(undef, $res);
705 my ($key, %params) = @_;
707 if ($key eq 'fast_search') {
708 $self->info_key_fast_search(%params);
709 } elsif ($key eq 'vendor_string') {
710 $self->info_key_vendor_string(%params);
711 } elsif ($key eq 'num_slots') {
712 $self->info_key_num_slots(%params);
716 sub info_key_fast_search {
720 $params{'info_cb'}->(undef,
721 fast_search => $self->{'fast_search'},
725 sub info_key_vendor_string {
729 $self->{'interface'}->inquiry(make_cb(inquiry_cb => sub {
730 my ($err, $info) = @_;
731 return $params{'info_cb'}->($err) if $err;
733 my $vendor_string = sprintf "%s %s",
734 ($info->{'vendor id'} or "<unknown>"),
735 ($info->{'product id'} or "<unknown>");
737 $params{'info_cb'}->(undef,
738 vendor_string => $vendor_string,
743 sub info_key_num_slots {
747 $self->_with_updated_state(\%params, 'info_cb',
748 sub { $self->info_key_num_slots_unlocked(@_) });
751 sub info_key_num_slots_unlocked {
754 my $state = $params{'state'};
756 my @allowed_slots = grep { $self->_is_slot_allowed($_) }
757 keys %{$state->{'slots'}};
759 $params{'info_cb'}->(undef, num_slots => scalar @allowed_slots);
762 sub get_interface { # (overridden by subclasses)
764 my ($device_name, $ignore_barcodes) = @_;
767 if (exists $self->{'config'}->{'properties'}->{'mtx'}) {
768 if (@{$self->{'config'}->{'properties'}->{'mtx'}->{'values'}} > 1) {
769 return Amanda::Changer->make_error("fatal", undef,
770 message => "only one value allowed for 'mtx'");
772 $mtx = $self->{'config'}->{'properties'}->{'mtx'}->{'values'}->[0];
774 $mtx = $Amanda::Constants::MTX;
778 return Amanda::Changer->make_error("fatal", undef,
779 message => "no default value for property MTX");
782 return Amanda::Changer::robot::Interface::MTX->new($device_name, $mtx, $ignore_barcodes),
785 # get, configure, and return a new device, or return a changer error
786 sub get_device { # (overridden by subclasses)
788 my ($device_name) = @_;
790 my $device = Amanda::Device->new($device_name);
791 if ($device->status != $DEVICE_STATUS_SUCCESS) {
792 return $self->make_error("failed", undef,
794 message => "opening '$device_name': " . $device->error_or_status());
797 if (my $err = $self->{'config'}->configure_device($device)) {
798 return $self->make_error("failed", undef,
810 return if $self->check_error($params{'finished_cb'});
812 $self->_with_updated_state(\%params, 'finished_cb',
813 sub { $self->_set_label_unlocked(@_); });
816 sub _set_label_unlocked {
819 my $state = $params{'state'};
821 # update all of the various pieces of cached information
822 my $drive = $params{'drive'};
823 my $slot = $state->{'drives'}->{$drive}->{'orig_slot'};
824 my $label = $params{'label'};
825 my $barcode = $state->{'drives'}->{$drive}->{'barcode'};
826 my $dev = $params{dev};
828 $state->{'drives'}->{$drive}->{'label'} = $label;
830 delete $state->{'slots'}->{$slot}->{'unkknown_state'};
831 $state->{'slots'}->{$slot}->{'state'} = Amanda::Changer::SLOT_FULL;
832 $state->{'slots'}->{$slot}->{'device_status'} = "".$dev->status;
833 my $volume_header = $dev->volume_header;
834 if (defined $volume_header) {
835 $state->{'slots'}->{$slot}->{'f_type'} = "".$volume_header->{type};
837 $state->{'slots'}->{$slot}->{'f_type'} = undef;
839 $state->{'slots'}->{$slot}->{'label'} = $label;
841 if (defined $barcode) {
842 $state->{'bc2lb'}->{$barcode} = $label;
845 $params{'finished_cb'}->(undef);
852 return if $self->check_error($params{'finished_cb'});
854 $self->_with_updated_state(\%params, 'finished_cb',
855 sub { $self->_release_unlocked(@_); });
858 sub _release_unlocked {
861 my $state = $params{'state'};
862 my $drive = $params{'drive'};
864 # delete the reservation and save the statefile
865 if (!$self->_res_info_is_mine($state->{'drives'}->{$drive}->{'res_info'})) {
866 # this should *never* happen
867 return $self->make_error("fatal", $params{'finished_cb'},
868 message => "reservation belongs to another instance");
870 $state->{'drives'}->{$drive}->{'res_info'} = undef;
872 # bounce off to eject if the user has requested it, using the xx_unlocked
873 # variant since we've already got the statefile open
874 if ($params{'eject'}) {
875 $self->eject_unlocked(
877 finished_cb => $params{'finished_cb'},
881 $params{'finished_cb'}->();
889 return if $self->check_error($params{'finished_cb'});
891 $self->_with_updated_state(\%params, 'finished_cb',
892 sub { $self->reset_unlocked(@_); });
898 my $state = $params{'state'};
900 $state->{'current_slot'} = $self->_get_next_slot($state, -1);
902 $params{'finished_cb'}->();
909 return if $self->check_error($params{'finished_cb'});
911 $self->_with_updated_state(\%params, 'finished_cb',
912 sub { $self->eject_unlocked(@_); });
918 my $state = $params{'state'};
919 my ($drive, $drive_info);
921 return if $self->check_error($params{'finished_cb'});
923 my $steps = define_steps
924 cb_ref => \$params{'finished_cb'};
926 # note that this changer treats "eject" as "unload", which may also require an eject
927 # operation if the eject_before_unload property is set
930 # if drive isn't specified, see if we only have one
931 if (!exists $params{'drive'}) {
932 if ((keys %{$self->{'drive2device'}}) == 1) {
933 $params{'drive'} = (keys %{$self->{'drive2device'}})[0];
935 return $self->make_error("failed", $params{'finished_cb'},
937 message => "no drive specified");
940 $drive = $params{'drive'};
942 $self->_debug("unloading drive $drive");
943 $drive_info = $state->{'drives'}->{$drive};
945 return $self->make_error("failed", $params{'finished_cb'},
947 message => "invalid drive '$drive'");
950 # if the drive exists, but not configured in this changer, then
952 if (!defined $self->{'drive2device'}->{$drive}) {
953 return $self->make_error("failed", $params{'finished_cb'},
955 message => "this changer instance is not configured to access drive $drive");
959 # check for a reservation
960 if ($drive_info->{'res_info'}
961 and $self->_res_info_verify($drive_info->{'res_info'})) {
962 return $self->make_error("failed", $params{'finished_cb'},
963 reason => "volinuse",
964 message => "tape in drive '$drive' is in use");
967 if ($self->{'eject_before_unload'}) {
968 $steps->{'wait_to_eject'}->();
970 $steps->{'wait_to_unload'}->();
974 step wait_to_eject => sub {
975 $self->_after_delay($state, $steps->{'eject'});
979 my $device_name = $self->{'drive2device'}->{$drive};
980 $self->_debug("ejecting $device_name before unload");
982 my $device = $self->get_device($device_name);
983 return $device if $device->isa("Amanda::Changer::Error");
985 if (!$device->eject()) {
986 return $self->make_error("failed", $params{'finished_cb'},
988 message => "while ejecting volume: " . $device->error_or_status);
992 $self->_set_delay($state, $self->{'eject_delay'});
994 $steps->{'wait_to_unload'}->();
997 step wait_to_unload => sub {
998 $self->_after_delay($state, $steps->{'unload'});
1001 step unload => sub {
1002 # find target slot and unload it - note that the target slot may not be
1003 # in the USE-SLOTS list, as it may belong to another config
1004 my $orig_slot = $drive_info->{'orig_slot'};
1005 $self->{'interface'}->unload($drive, $orig_slot, $steps->{'unload_finished'});
1008 step unload_finished => sub {
1012 return $self->make_error("failed", $params{'finished_cb'},
1013 reason => "unknown",
1017 $self->_debug("unload complete");
1018 my $orig_slot = $state->{'drives'}->{$drive}->{'orig_slot'};
1019 $state->{'slots'}->{$orig_slot}->{'state'} = $state->{'drives'}->{$drive}->{'state'};
1020 $state->{'slots'}->{$orig_slot}->{'label'} = $state->{'drives'}->{$drive}->{'label'};
1021 $state->{'slots'}->{$orig_slot}->{'barcode'} = $state->{'drives'}->{$drive}->{'barcode'};
1022 $state->{'slots'}->{$orig_slot}->{'loaded_in'} = undef;
1023 $state->{'drives'}->{$drive}->{'state'} = Amanda::Changer::SLOT_EMPTY;
1024 $state->{'drives'}->{$drive}->{'label'} = undef;
1025 $state->{'drives'}->{$drive}->{'barcode'} = undef;
1026 $state->{'drives'}->{$drive}->{'orig_slot'} = undef;
1028 $self->_set_delay($state, $self->{'unload_delay'});
1029 $params{'finished_cb'}->();
1037 return if $self->check_error($params{'finished_cb'});
1039 $self->_with_updated_state(\%params, 'finished_cb',
1040 sub { $self->update_unlocked(@_); });
1043 sub update_unlocked {
1047 my $state = $params{'state'};
1048 my $set_to_unknown = 0;
1050 return if $self->check_error($params{'finished_cb'});
1052 my $user_msg_fn = $params{'user_msg_fn'};
1053 $user_msg_fn ||= sub { $self->_debug($_[0]); };
1055 my $steps = define_steps
1056 cb_ref => \$params{'finished_cb'};
1058 step handle_assignment => sub {
1059 # check for the SL=LABEL format, and handle it here
1060 if (exists $params{'changed'} and $params{'changed'} =~ /^\d+=\S+$/) {
1061 my ($slot, $label) = ($params{'changed'} =~ /^(\d+)=(\S+)$/);
1063 # let's list the reasons we *can't* do what the user has asked
1065 if (!exists $state->{'slots'}) {
1066 $whynot = "slot $slot does not exist";
1067 } elsif (!$self->_is_slot_allowed($slot)) {
1068 $whynot = "slot $slot is not used by this changer";
1069 } elsif ($state->{'slots'}->{$slot}->{'state'} ==
1070 Amanda::Changer::SLOT_EMPTY) {
1071 $whynot = "slot $slot is empty";
1072 } elsif (defined $state->{'slots'}->{$slot}->{'loaded_in'}) {
1073 $whynot = "slot $slot is currently loaded";
1077 return $self->make_error("failed", $params{'finished_cb'},
1078 reason => "unknown", message => $whynot);
1081 $user_msg_fn->("recoding volume '$label' in slot $slot");
1082 # ok, now erase all knowledge of that label
1083 while (my ($bc, $lb) = each %{$state->{'bc2lb'}}) {
1084 if ($lb eq $label) {
1085 delete $state->{'bc2lb'}->{$bc};
1089 while (my ($sl, $inf) = each %{$state->{'slots'}}) {
1090 if ($inf->{'label'} and $inf->{'label'} eq $label) {
1091 delete $inf->{'device_status'};
1092 delete $inf->{'f_type'};
1093 delete $inf->{'label'};
1097 # and add knowledge of the label to the given slot
1098 #$state->{'slots'}->{$slot}->{'device_status'} = $DEVICE_STATUS_SUCCESS;
1099 #$state->{'slots'}->{$slot}->{'f_type'} = $Amanda::Header::F_TAPESTART;
1100 $state->{'slots'}->{$slot}->{'label'} = $label;
1101 if ($state->{'slots'}->{$slot}->{'barcode'}) {
1102 my $bc = $state->{'slots'}->{$slot}->{'barcode'};
1103 $state->{'bc2lb'}->{$bc} = $label;
1106 # that's it -- no changer motion required
1107 return $params{'finished_cb'}->(undef);
1108 } elsif (exists $params{'changed'} and
1109 $params{'changed'} =~ /^(.+)=$/) {
1110 $params{'changed'} = $1;
1111 $set_to_unknown = 1;
1112 $steps->{'calculate_slots'}->($steps->{'set_to_unknown'});
1114 $steps->{'calculate_slots'}->($steps->{'update_slot'});
1118 step calculate_slots => sub {
1119 my ($update_slot_cb) = shift @_;
1120 if (exists $params{'changed'}) {
1121 # parse the string just like use-slots, using a hash for uniqueness
1123 for my $range (split ',', $params{'changed'}) {
1124 my ($first, $last) = ($range =~ /(\d+)(?:-(\d+))?/);
1125 $last = $first unless defined($last);
1126 for ($first .. $last) {
1127 $changed{$_} = undef;
1131 @slots_to_check = keys %changed;
1132 @slots_to_check = grep { exists $state->{'slots'}->{$_} } @slots_to_check;
1134 @slots_to_check = keys %{ $state->{'slots'} };
1137 # limit the update to allowed slots, and sort them so we don't confuse
1138 # the user with a "random" order
1139 @slots_to_check = grep { $self->_is_slot_allowed($_) } @slots_to_check;
1140 @slots_to_check = grep { $state->{'slots'}->{$_}->{'state'} == Amanda::Changer::SLOT_FULL} @slots_to_check;
1141 @slots_to_check = sort { $a <=> $b } @slots_to_check;
1143 $update_slot_cb->();
1146 step set_to_unknown => sub {
1147 return $steps->{'done'}->() if (!@slots_to_check);
1149 my $slot = shift @slots_to_check;
1150 $user_msg_fn->("Removing entry for slot $slot");
1151 if (!defined $state->{'slots'}->{$slot}->{'barcode'}) {
1152 $state->{'slots'}->{$slot}->{'label'} = undef;
1153 $state->{'slots'}->{$slot}->{'device_status'} = undef;
1154 $state->{'slots'}->{$slot}->{'f_type'} = undef;
1155 if (defined $state->{'slots'}->{$slot}->{'loaded_in'}) {
1156 my $drive = $state->{'slots'}->{$slot}->{'loaded_in'};
1157 $state->{'drives'}->{$drive}->{'label'} = undef;
1158 $state->{'drives'}->{$drive}->{'state'} =
1159 Amanda::Changer::SLOT_FULL;
1162 $steps->{'set_to_unknown'}->();
1165 # TODO: parallelize this if multiple drives are available
1167 step update_slot => sub {
1168 return $steps->{'done'}->() if (!@slots_to_check);
1170 my $slot = shift @slots_to_check;
1171 $user_msg_fn->("scanning slot $slot");
1173 $self->load_unlocked(
1175 res_cb => $steps->{'slot_loaded'},
1179 step slot_loaded => sub {
1180 my ($err, $res) = @_;
1182 return $params{'finished_cb'}->($err);
1185 # load() already fixed up the metadata, so just release; but we have to
1186 # be careful to do an unlocked release.
1188 finished_cb => $steps->{'released'},
1193 step released => sub {
1196 return $params{'finished_cb'}->($err);
1199 $steps->{'update_slot'}->();
1203 $params{'finished_cb'}->(undef);
1211 return if $self->check_error($params{'inventory_cb'});
1213 $self->_with_updated_state(\%params, 'inventory_cb',
1214 sub { $self->inventory_unlocked(@_); });
1217 sub inventory_unlocked {
1220 my $state = $params{'state'};
1222 my @slot_names = sort { $a <=> $b } keys %{ $state->{'slots'} };
1224 for my $slot_name (@slot_names) {
1226 next unless $self->_is_slot_allowed($slot_name);
1227 my $slot = $state->{'slots'}->{$slot_name};
1229 $i->{'slot'} = $slot_name;
1230 $i->{'state'} = $slot->{'state'};
1231 $i->{'device_status'} = $slot->{'device_status'};
1232 $i->{'f_type'} = $slot->{'f_type'};
1233 $i->{'label'} = $slot->{'label'};
1234 $i->{'barcode'} = $slot->{'barcode'}
1235 if ($slot->{'barcode'});
1236 if (defined $slot->{'loaded_in'}) {
1237 $i->{'loaded_in'} = $slot->{'loaded_in'};
1238 my $drive = $state->{'drives'}->{$slot->{'loaded_in'}};
1239 if ($drive->{'res_info'} and $self->_res_info_verify($drive->{'res_info'})) {
1240 $i->{'reserved'} = 1;
1243 $i->{'import_export'} = 1
1247 if $slot_name eq $state->{'current_slot'};
1252 $params{'inventory_cb'}->(undef, \@inv);
1259 return if $self->check_error($params{'finished_cb'});
1261 $self->_with_updated_state(\%params, 'finished_cb',
1262 sub { $self->move_unlocked(@_); });
1268 my $state = $params{'state'};
1270 my $from_slot = $params{'from_slot'};
1271 my $to_slot = $params{'to_slot'};
1273 # make sure this is OK
1274 for ($from_slot, $to_slot) {
1275 if (!$self->_is_slot_allowed($_)) {
1276 return $self->make_error("failed", $params{'finished_cb'},
1277 reason => "invalid",
1278 message => "invalid slot $_");
1282 if ($state->{'slots'}->{$from_slot}->{'state'} == Amanda::Changer::SLOT_EMPTY) {
1283 return $self->make_error("failed", $params{'finished_cb'},
1284 reason => "invalid",
1285 message => "slot $from_slot is empty");
1288 if (defined $state->{'slots'}->{$from_slot}->{'loaded_in'}) {
1289 return $self->make_error("failed", $params{'finished_cb'},
1290 reason => "invalid",
1291 message => "slot $from_slot is currently loaded");
1294 if ($state->{'slots'}->{$to_slot}->{'state'} == Amanda::Changer::SLOT_FULL) {
1295 return $self->make_error("failed", $params{'finished_cb'},
1296 reason => "invalid",
1297 message => "slot $to_slot is not empty");
1300 # if the destination slot is loaded, then we could do an "exchange", but
1303 my $transfer_complete = make_cb(transfer_complete => sub {
1305 return $params{'finished_cb'}->($err) if $err;
1308 $state->{'slots'}->{$to_slot} = { %{ $state->{'slots'}->{$from_slot} } };
1309 $state->{'slots'}->{$from_slot}->{'state'} =
1310 Amanda::Changer::SLOT_EMPTY;
1311 $state->{'slots'}->{$from_slot}->{'device_status'} = undef;
1312 $state->{'slots'}->{$from_slot}->{'f_type'} = undef;
1313 $state->{'slots'}->{$from_slot}->{'label'} = undef;
1314 $state->{'slots'}->{$from_slot}->{'barcode'} = undef;
1316 $params{'finished_cb'}->();
1318 $self->{'interface'}->transfer($from_slot, $to_slot, $transfer_complete);
1324 # calculate the next highest non-empty slot after $slot (assuming that
1325 # the changer status has been updated)
1326 sub _get_next_slot {
1328 my ($state, $slot) = @_;
1330 my @nonempty = sort { $a <=> $b } grep {
1331 $state->{'slots'}->{$_}->{'state'} == Amanda::Changer::SLOT_FULL
1332 and $self->_is_slot_allowed($_)
1333 } keys(%{$state->{'slots'}});
1335 my @higher = grep { $_ > $slot } @nonempty;
1337 # return the next higher slot, or the first nonempty slot (to loop around)
1338 return $higher[0] if (@higher);
1339 return $nonempty[0];
1342 # is $slot in the slots specified by the use-slots property?
1343 sub _is_slot_allowed {
1347 # if use-slots is not specified, all slots are available
1348 return 1 unless ($self->{'use_slots'});
1350 for my $range (split ',', $self->{'use_slots'}) {
1351 my ($first, $last) = ($range =~ /(\d+)(?:-(\d+))?/);
1352 $last = $first unless defined($last);
1353 return 1 if ($slot >= $first and $slot <= $last);
1359 # add a prefix and call Amanda::Debug::debug
1363 # chg_name is not set until *after* the constructor finishes
1364 my $chg_name = $self->{'chg_name'} || $self->{'class_name'};
1365 debug("$chg_name: $msg");
1371 # Wait until the delay from the last operation has expired, and call the
1372 # given callback with the given arguments
1375 my ($state, $cb, @args) = @_;
1377 confess("undefined \$cb") unless (defined $cb);
1379 # if the current time is before $start, then we'll perform the action anyway; this
1380 # saves us from long delays when clocks fall out of sync or run backward, but delays
1382 my ($start, $end, $now);
1383 $start = $state->{'last_operation_time'};
1384 if (!defined $start) {
1385 return $cb->(@args);
1388 $end = $start + $state->{'last_operation_delay'};
1391 if ($now >= $start and $now < $end) {
1392 Amanda::MainLoop::call_after(1000 * ($end - $now), $cb, @args);
1394 return $cb->(@args);
1398 # set the delay parameters in the statefile
1401 my ($state, $delay) = @_;
1403 $state->{'last_operation_time'} = time;
1404 $state->{'last_operation_delay'} = $delay;
1408 # Statefile management
1410 # wrapper around Amanda::Changer's with_locked_state to lock the statefile and
1411 # then update the state with the results of the 'status' command.
1413 # Like with_locked_state, this method assumes the keyword-based parameter
1414 # style, and adds a 'state' parameter with the new state. Also like
1415 # with_locked_state, it replaces the $cbname key with a wrapped version of that
1416 # callback. It then calls $sub.
1417 sub _with_updated_state {
1419 my ($paramsref, $cbname, $sub) = @_;
1420 my %params = %$paramsref;
1423 my $steps = define_steps
1424 cb_ref => \$paramsref->{$cbname};
1427 $self->with_locked_state($self->{'statefile'},
1428 $params{$cbname}, $steps->{'got_lock'});
1431 step got_lock => sub {
1432 ($state, my $new_cb) = @_;
1434 # set up params for calling through to $sub later
1435 $params{'state'} = $state;
1436 $params{$cbname} = $new_cb;
1438 if (!keys %$state) {
1439 $state->{'slots'} = {};
1440 $state->{'drives'} = {};
1441 $state->{'drive_lru'} = [];
1442 $state->{'bc2lb'} = {};
1443 $state->{'current_slot'} = -1;
1446 # this is for testing ONLY!
1447 $self->{'__last_state'} = $state;
1449 # if it's not time for another run of the status command yet, then just skip to
1451 if (defined $state->{'last_status'}
1452 and time < $state->{'last_status'} + $self->{'status_interval'}) {
1453 $self->_debug("too early for another 'status' invocation");
1454 $steps->{'done'}->();
1456 $steps->{'wait'}->();
1461 $self->_after_delay($state, $steps->{'call_status'});
1464 step call_status => sub {
1465 $self->{'interface'}->status($steps->{'status_cb'});
1468 step status_cb => sub {
1469 my ($err, $status) = @_;
1471 return $self->make_error("fatal", $params{$cbname},
1475 $state->{'last_status'} = time;
1476 $self->_debug("updating state");
1478 # process the results; $status can update our slot->label
1479 # mapping, but the barcode->label mapping stays the same.
1482 my ($drv, $slot, $info);
1484 # note that loaded_in is always undef; it will be set correctly
1485 # when the drives are scanned
1486 while (($slot, $info) = each %{$status->{'slots'}}) {
1487 if ($info->{'empty'}) {
1489 $new_slots->{$slot} = {
1490 state => Amanda::Changer::SLOT_EMPTY,
1491 device_status => undef,
1496 ie => $info->{'ie'},
1501 if (defined $info->{'barcode'}) {
1503 my $label = $state->{'bc2lb'}->{$info->{'barcode'}};
1505 $new_slots->{$slot} = {
1506 state => Amanda::Changer::SLOT_FULL,
1507 device_status => $state->{'slots'}->{$slot}->{device_status},
1508 f_type => $state->{'slots'}->{$slot}->{f_type},
1510 barcode => $info->{'barcode'},
1512 ie => $info->{'ie'},
1515 # assume the status of this slot has not changed since the last
1516 # time we looked at it, although mark it as not loaded in a slot
1517 if (exists $state->{'slots'}->{$slot}) {
1518 $new_slots->{$slot} = $state->{'slots'}->{$slot};
1519 $new_slots->{$slot}->{'loaded_in'} = undef;
1521 $new_slots->{$slot} = {
1522 state => Amanda::Changer::SLOT_FULL,
1523 device_status => undef,
1528 ie => $info->{'ie'},
1533 my $old_slots_state = $state->{'slots'};
1534 $state->{'slots'} = $new_slots;
1536 # now handle the drives
1537 my $new_drives = {};
1538 while (($drv, $info) = each %{$status->{'drives'}}) {
1539 my $old_drive = $state->{'drives'}->{$drv};
1541 # if this drive still has a valid reservation, don't change it
1542 if (defined $old_drive->{'res_info'}
1543 and $self->_res_info_verify($old_drive->{'res_info'})) {
1544 $new_drives->{$drv} = $old_drive;
1548 # if the drive is empty, this is pretty easy
1549 if (!defined $info) {
1550 $new_drives->{$drv} = {
1551 state => Amanda::Changer::SLOT_EMPTY,
1559 # trust our own orig_slot over that from the changer, if possible,
1560 # as some changers do not report this information accurately
1561 my ($orig_slot, $label);
1562 if (defined $old_drive->{'orig_slot'}) {
1563 $orig_slot = $old_drive->{'orig_slot'};
1564 $label = $old_drive->{'label'};
1567 # but don't trust it if the barcode has changed
1568 if (defined $info->{'barcode'}
1569 and defined $old_drive->{'barcode'}
1570 and $info->{'barcode'} ne $old_drive->{'barcode'}) {
1575 # get the robot's notion of the original slot if we don't know ourselves
1576 if (!defined $orig_slot) {
1577 $orig_slot = $info->{'orig_slot'};
1580 # but if there's a tape in that slot, then we've got a problem
1581 if (defined $orig_slot
1582 and $state->{'slots'}->{$orig_slot}->{'state'} != Amanda::Changer::SLOT_EMPTY) {
1583 warning("mtx indicates tape in drive $drv should go to slot $orig_slot, " .
1584 "but that slot is not empty.");
1586 for my $slot (keys %{ $state->{'slots'} }) {
1587 if ($state->{'slots'}->{$slot}->{'state'} == Amanda::Changer::SLOT_EMPTY) {
1592 if (!defined $orig_slot) {
1593 warning("cannot find an empty slot for the tape in drive $drv");
1597 # and look up the label by barcode if possible
1598 if (!defined $label && defined $info->{'barcode'}) {
1599 $label = $state->{'bc2lb'}->{$info->{'barcode'}};
1602 $new_drives->{$drv} = {
1603 state => Amanda::Changer::SLOT_FULL,
1605 barcode => $info->{'barcode'},
1606 orig_slot => $orig_slot,
1609 $state->{'drives'} = $new_drives;
1611 # update the loaded_in info for the relevant slots
1612 while (($drv, $info) = each %$new_drives) {
1613 # also update the slots with the relevant 'loaded_in' info
1614 if (defined $info->{'orig_slot'}) {
1615 my $old_state = $old_slots_state->{$info->{'orig_slot'}};
1616 $state->{'slots'}->{$info->{'orig_slot'}} = {
1617 state => $info->{'state'},
1618 device_status => $old_state->{'device_status'},
1619 f_type => $old_state->{'f_type'},
1620 label => $info->{'label'},
1621 barcode => $info->{'barcode'},
1627 # sanity check that we don't have tape-device info for nonexistent drives
1628 for my $dr (@{$self->{'driveorder'}}) {
1629 if (!exists $state->{'drives'}->{$dr}) {
1630 warning("tape-device property specified for drive $dr, but no such " .
1631 "drive exists in the library");
1635 if ($state->{'current_slot'} == -1) {
1636 $state->{'current_slot'} = $self->_get_next_slot($state, -1);
1639 $steps->{'done'}->();
1643 # finally, call through to the user's method; $params{$cbname} has been
1644 # properly patched to release the state lock when this method is done.
1650 # reservation records
1652 # A reservation record is recorded in the statefile, and is distinct from an
1653 # Amanda::Changer::robot:Reservation object in that it is seen by all users of
1654 # the tape device, whether in this process or another.
1656 # This is abstracted out to enable support for a more robust mechanism than
1661 return { pid => $$, };
1664 sub _res_info_verify {
1666 my ($res_info) = @_;
1668 # true if this is our reservation
1669 return 1 if ($res_info->{'pid'} == $$);
1671 # or if the process is dead
1672 return kill 0, $res_info->{'pid'};
1675 sub _res_info_is_mine {
1677 my ($res_info) = @_;
1679 return 1 if ($res_info and $res_info->{'pid'} == $$);
1682 package Amanda::Changer::robot::Reservation;
1683 use vars qw( @ISA );
1684 use Amanda::Debug qw( debug warning );
1685 @ISA = qw( Amanda::Changer::Reservation );
1689 my ($chg, $slot, $drive, $device, $barcode) = @_;
1690 my $self = Amanda::Changer::Reservation::new($class);
1692 $self->{'chg'} = $chg;
1694 $self->{'drive'} = $drive;
1695 $self->{'device'} = $device;
1696 $self->{'this_slot'} = $slot;
1697 $self->{'barcode'} = $barcode;
1706 # if we're in global cleanup and the changer is already dead,
1708 return unless $self->{'chg'};
1710 # unref the device, for good measure
1711 $self->{'device'} = undef;
1713 # punt this method off to the changer itself, optionally calling
1714 # the unlocked version if we have the 'state' parameter
1715 if (exists $params{'unlocked'} and exists $params{'state'}) {
1716 $self->{'chg'}->_release_unlocked(drive => $self->{'drive'}, %params);
1718 $self->{'chg'}->_release(drive => $self->{'drive'}, %params);
1726 return unless $self->{'chg'};
1727 $self->{'chg'}->_set_label(drive => $self->{'drive'},
1728 dev => $self->{device}, %params);
1731 package Amanda::Changer::robot::Interface;
1733 # The physical interface to the changer is abstracted out to allow several
1734 # implementations (see chg-ndmp for one of them). This API is "reasonably
1735 # stable", but is really only known to this changer and its subclasses, so it's
1736 # not documented in POD. The methods are:
1738 # $iface->inquiry($inquiry_cb);
1740 # Inquire as to the relevant information about the changer. The result is a
1741 # hash table of lowercased key names and values, $info. The inquiry_cb is
1742 # called as $inquiry_cb->($errmsg, $info). The resulting strings have quotes
1743 # and whitespace stripped. Keys include 'vendor id' and 'product id'.
1745 # $iface->status($status_cb)
1747 # Get the READ ELEMENT STATUS output for the changer. The status_cb is called
1748 # as $status_cb->($errmsg, $status). $status is a hash with keys 'drives' and
1749 # 'slots', each of which is a hash indexed by the element address (note that drive
1750 # element addresses can and usually do overlap with slots. The values of the slots
1751 # hash are hashes with keys
1752 # - 'empty' (1 if the slot is empty)
1753 # - 'barcode' (which may be undef if the changer does not support barcodes)
1754 # - 'ie' (a boolean indicating whether this is an import/export slot).
1755 # The values of the drives are undef for empty drive, or hashes with keys
1756 # - 'barcode' (which may be undef if the changer does not support barcodes)
1757 # - 'orig_slot' (slot from which this volume was taken, if known)
1759 # $iface->load($slot, $drive, $finished_cb)
1761 # Load $slot into $drive. The finished_cb gets a single argument, $error,
1762 # which is only defined if an error occurred. Note that this does not
1763 # necessarily wait until the load operation is complete (most drives give
1764 # no such indication) (this method also implements unload, if $un=1)
1766 # $iface->unload($drive, $slot, $finished_cb);
1768 # Unload $drive into $slot. Finished_cb is just as for load().
1770 # $iface->eject($drive_name, $finished_cb);
1772 # Eject $drive_name (named /dev/whatever, not the drive number), and call finished_cb.
1774 # $iface->transfer($src_slot, $dst_slot, $finished_cb);
1776 # Move the tape in $src_slot into $dst_slot. The finished_cb gets a single
1777 # argument, $error, which is only defined if an error occurred. Note that this
1778 # does not necessarily wait until the load operation is complete.
1780 package Amanda::Changer::robot::Interface::MTX;
1783 use Amanda::Config qw( :getconf );
1784 use Amanda::Debug qw( debug warning );
1785 use Amanda::MainLoop qw( :GIOCondition synchronized make_cb define_steps step );
1786 use Amanda::Device qw( :constants );
1790 my ($device_name, $mtx, $ignore_barcodes) = @_;
1792 unless (-e $device_name) {
1793 return Amanda::Changer->make_error("fatal", undef,
1794 message => "'$device_name' not found");
1798 # This object uses a big lock to block *all* operations, not just mtx
1799 # invocations. This allows us to add delays to certain operations, while still
1802 device_name => $device_name,
1804 ignore_barcodes => $ignore_barcodes,
1810 my ($inquiry_cb) = @_;
1812 synchronized($self->{'lock'}, $inquiry_cb, sub {
1813 my ($inquiry_cb) = @_;
1814 my $sys_cb = make_cb(sys_cb => sub {
1815 my ($exitstatus, $output) = @_;
1816 if ($exitstatus != 0) {
1817 return $inquiry_cb->("error from mtx: " . $output, {});
1820 for my $line (split '\n', $output) {
1821 if (my ($k, $v) = ($line =~ /^(.*):\s*(.*)$/)) {
1822 $v =~ s/^'(.*)'$/$1/;
1827 return $inquiry_cb->(undef, \%info);
1831 $self->_run_system_command($sys_cb,
1832 $self->{'mtx'}, "-f", $self->{'device_name'}, 'inquiry');
1838 my ($status_cb) = @_;
1840 synchronized($self->{'lock'}, $status_cb, sub {
1841 my ($status_cb) = @_;
1843 my $sys_cb = make_cb(sys_cb => sub {
1844 my ($exitstatus, $output) = @_;
1845 if ($exitstatus != 0) {
1847 # if it's a regular SCSI error, just show the sense key
1848 my ($sensekey) = ($err =~ /mtx: Request Sense: Sense Key=(.*)\n/);
1849 $err = "SCSI error; Sense Key=$sensekey" if $sensekey;
1850 return $status_cb->("error from mtx: " . $err, {});
1853 for my $line (split '\n', $output) {
1854 my ($slot, $ie, $slinfo);
1856 # drives (data transfer elements)
1857 if (($slot, $slinfo) = ($line =~
1858 /^Data Transfer Element\s*(\d+)?\s*:\s*(.*)/i)) {
1859 # assume 0 when not given a drive #
1860 $slot = 0 unless defined $slot;
1861 if ($slinfo =~ /^Empty/i) {
1862 $status{'drives'}->{$slot} = undef;
1863 } elsif ($slinfo =~ /^Full/i) {
1864 my ($barcode, $orig_slot);
1865 ($barcode) = ($slinfo =~ /:VolumeTag\s*=\s*(\S+)/i);
1866 ($orig_slot) = ($slinfo =~ /\(Storage Element (\d+) Loaded\)/i);
1867 $status{'drives'}->{$slot} = {
1868 barcode => $barcode,
1869 orig_slot => $orig_slot,
1873 # slots (storage elements)
1874 } elsif (($slot, $ie, $slinfo) = ($line =~
1875 /^\s*Storage Element\s*(\d+)\s*(IMPORT\/EXPORT)?\s*:\s*(.*)/i)) {
1877 if ($slinfo =~ /^Empty/i) {
1878 $status{'slots'}->{$slot} = {
1882 } elsif ($slinfo =~ /^Full/i) {
1884 ($barcode) = ($slinfo =~ /:VolumeTag\s*=\s*(\S+)/i)
1885 unless ($self->{'ignore_barcodes'});
1886 $status{'slots'}->{$slot} = {
1887 barcode => $barcode,
1894 return $status_cb->(undef, \%status);
1898 my @nobarcode = ('nobarcode') if $self->{'ignore_barcodes'};
1899 $self->_run_system_command($sys_cb,
1900 $self->{'mtx'}, "-f", $self->{'device_name'}, @nobarcode, 'status');
1906 my ($slot, $drive, $finished_cb, $un) = @_;
1908 synchronized($self->{'lock'}, $finished_cb, sub {
1909 my ($finished_cb) = @_;
1911 my $sys_cb = make_cb(sys_cb => sub {
1912 my ($exitstatus, $output) = @_;
1913 if ($exitstatus != 0) {
1914 return $finished_cb->("error from mtx: " . $output);
1916 return $finished_cb->(undef);
1921 $self->_run_system_command($sys_cb,
1922 $self->{'mtx'}, "-f", $self->{'device_name'},
1923 $un? 'unload':'load', $slot, $drive);
1929 my ($drive, $slot, $finished_cb) = @_;
1930 return $self->load($slot, $drive, $finished_cb, 1);
1935 my ($src_slot, $dst_slot, $finished_cb) = @_;
1937 synchronized($self->{'lock'}, $finished_cb, sub {
1938 my ($finished_cb) = @_;
1940 my $sys_cb = make_cb(sys_cb => sub {
1941 my ($exitstatus, $output) = @_;
1942 if ($exitstatus != 0) {
1943 return $finished_cb->("error from mtx: " . $output);
1945 return $finished_cb->(undef);
1949 $self->_run_system_command($sys_cb,
1950 $self->{'mtx'}, "-f", $self->{'device_name'},
1951 'transfer', $src_slot, $dst_slot);
1955 # Run 'mtx' and capture the output. Standard output and error
1956 # are lumped together.
1958 # @param $sys_cb: called with ($exitstatus, $output)
1959 # @param @args: args to pass to exec()
1960 sub _run_system_command {
1961 my ($self, $sys_cb, @args) = @_;
1963 debug("invoking " . join(" ", @args));
1965 my ($readfd, $writefd) = POSIX::pipe();
1966 if (!defined($writefd)) {
1967 die("Error creating pipe: $!");
1971 if (!defined($pid) or $pid < 0) {
1972 die("Can't fork to run changer script: $!");
1978 # get our file-handle house in order
1979 POSIX::close($readfd);
1980 POSIX::dup2($writefd, 1);
1981 POSIX::dup2($writefd, 2);
1982 POSIX::close($writefd);
1984 %ENV = Amanda::Util::safe_env();
1986 { exec { $args[0] } @args; } # braces protect against warning
1992 # clean up file descriptors from the fork
1993 POSIX::close($writefd);
1995 # the callbacks that follow share these lexical variables
1997 my $child_output = '';
1999 my $child_exit_status = 0;
2000 my ($fdsrc, $cwsrc);
2001 my $open_sources = 0;
2003 my $steps = define_steps
2008 Amanda::MainLoop::fd_source($readfd, $G_IO_IN | $G_IO_ERR | $G_IO_HUP)
2009 ->set_callback($steps->{'fd_source_cb'});
2012 Amanda::MainLoop::child_watch_source($pid)
2013 ->set_callback($steps->{'child_watch_source_cb'});
2016 step immediate => 1,
2017 fd_source_cb => sub {
2020 $len = POSIX::read($readfd, $bytes, 1024);
2022 # if we got an EOF, shut things down.
2025 POSIX::close($readfd);
2027 $fdsrc = undef; # break a reference loop
2028 $steps->{'maybe_finished'}->();
2030 # otherwise, just keep the bytes
2031 $child_output .= $bytes;
2035 step immediate => 1,
2036 child_watch_source_cb => sub {
2037 my ($cwsrc, $got_pid, $got_status) = @_;
2039 $cwsrc = undef; # break a reference loop
2041 $child_exit_status = $got_status;
2043 $steps->{'maybe_finished'}->();
2046 step maybe_finished => sub {
2047 return if --$open_sources;
2049 # everything is finished -- process the results and invoke the callback
2050 chomp $child_output;
2052 # let the callback take care of any further interpretation
2053 my $exitval = POSIX::WEXITSTATUS($child_exit_status);
2054 $sys_cb->($exitval, $child_output);