1 # Copyright (c) 2010 Zmanda, Inc. All Rights Reserved.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 2 as published
5 # by the Free Software Foundation.
7 # This program 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 General Public License
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19 package Amanda::Recovery::Scan;
27 use base qw(Exporter);
28 our @EXPORT_OK = qw($DEFAULT_CHANGER);
32 use Amanda::Device qw( :constants );
33 use Amanda::Debug qw( debug );
36 use Amanda::Interactivity;
38 use constant SCAN_ASK => 1; # call Amanda::Interactivity module
39 use constant SCAN_POLL => 2; # wait 'poll_delay' and retry the scan.
40 use constant SCAN_FAIL => 3; # abort
41 use constant SCAN_CONTINUE => 4; # continue to the next step
42 use constant SCAN_ASK_POLL => 5; # call Amanda::Interactivity module and
43 # poll at the same time.
47 Amanda::Recovery::Scan -- interface to scan algorithm
51 use Amanda::Recovey::Scan;
53 # scan the default changer with no interactivity
54 my $scan = Amanda::Recovery::Scan->new();
55 # ..or scan the changer $chg, using $interactivity for interactivity
56 $scan = Amanda::Recovery::Scan->new(chg => $chg,
57 interactivity => $interactivity);
62 my ($err, $reservation) = @_;
66 $dev = $reservation->{device};
71 $reservation->release(finished_cb => $start_next_volume);
74 $scan->quit(); # also quit the changer
79 This package provides a way for programs that need to read data from volumes
80 (loosely called "recovery" programs) to find the volumes they need in a
81 configurable way. It takes care of prompting for volumes when they are not
82 available, juggling multiple changers, and any other unpredictabilities.
86 Like L<Amanda::Changer>, this package operates asynchronously, and thus
87 requires that the caller use L<Amanda::MainLoop> to poll for events.
89 A new Scan object is created with the C<new> function as follows:
91 my $scan = Amanda::Recovery::Scan->new(scan_conf => $scan_conf,
93 interactivity => $interactivity);
95 C<scan_conf> is the configuration for the scan, which at this point should be
96 omitted, as configuration is not yet supported. The C<chg> parameter specifies
97 the changer to start the scan with. The default changer is used if C<chg> is
98 omitted. The C<interactivity> parameter gives an C<Amanda::Interactivity> object.
102 Many of the callbacks used by this package are identical to the callbacks of
103 the same name in L<Amanda::Changer>.
105 When a callback is called with an error, it is an object of type
106 C<Amanda::Changer::Error>. The C<volinuse> reason has a different meaning: it
107 means that the volume with that label is present in the changer, but is in use
114 $scan->find_volume(label => $label,
116 user_msg_fn => $user_msg_fn,
119 Find the volume labelled C<$label> and call C<$res_cb>. C<$user_msg_fn> is
120 used to send progress information, The argumnet it takes are describe in
121 the next section. As with the C<load> method
122 of the changer API, C<set_current> should be set to 1 if you want the scan to
123 set the current slot.
129 The cleanly terminate a scan objet, the changer quit is also called.
133 The user_msg_fn take various arguments
135 Initiate the scan of the slot $slot:
136 $self->user_msg_fn(scan_slot => 1,
139 Initiate the scan of the slot $slot which should have the label $label:
140 $self->user_msg_fn(scan_slot => 1,
144 The result of scanning slot $slot:
145 $self->user_msg_fn(slot_result => 1,
150 Other options can be added at any time. The function can ignore them.
154 our $DEFAULT_CHANGER = {};
159 my $scan_conf = $params{'scan_conf'};
160 my $chg = $params{'chg'};
161 my $interactivity = $params{'interactivity'};
163 #until we have a config for it.
164 $scan_conf = Amanda::Recovery::Scan::Config->new();
165 $chg = Amanda::Changer->new() if !defined $chg;
166 return $chg if $chg->isa("Amanda::Changer::Error");
171 scan_conf => $scan_conf,
172 interactivity => $interactivity,
174 return bless ($self, $class);
180 die("Recovery::Scan detroyed without quit") if defined $self->{'scan_conf'};
186 $self->{'chg'}->quit() if defined $self->{'chg'};
188 foreach (keys %$self) {
198 my $label = $params{'label'};
199 my $user_msg_fn = $params{'user_msg_fn'} || \&_user_msg_fn;
206 my $scan_running = 0;
207 my $interactivity_running = 0;
208 my $restart_scan = 0;
209 my $abort_scan = undef;
210 my $last_err = undef; # keep the last meaningful error, the one reported
211 # to the user, most scan end with the notfound error,
212 # it's more interesting to report an error from the
215 my $remove_undef_state = 0;
216 my $load_for_label = 0; # 1 = Try to load the slot with the correct label
217 # 0 = Load a slot with an unknown label
219 my $steps = define_steps
220 cb_ref => \$params{'res_cb'};
222 step get_first_inventory => sub {
223 Amanda::Debug::debug("find_volume labeled '$label'");
226 $self->{'chg'}->inventory(inventory_cb => $steps->{'got_first_inventory'});
229 step got_first_inventory => sub {
230 (my $err, $inventory) = @_;
232 if ($err && $err->notimpl) {
233 #inventory not implemented
234 return $self->_find_volume_no_inventory(%params);
237 return $steps->{'call_res_cb'}->($err, undef);
240 # find current slot and keep a private copy of the value
241 for my $i (0..(scalar(@$inventory)-1)) {
242 if ($inventory->[$i]->{current}) {
243 $current = $inventory->[$i]->{slot};
248 if (!defined $current) {
249 if (scalar(@$inventory) == 0) {
252 $current = $inventory->[0]->{slot};
256 # continue parsing the inventory
257 $steps->{'parse_inventory'}->($err, $inventory);
260 step restart_scan => sub {
262 return $steps->{'get_inventory'}->();
265 step get_inventory => sub {
266 $self->{'chg'}->inventory(inventory_cb => $steps->{'parse_inventory'});
269 step parse_inventory => sub {
270 (my $err, $inventory) = @_;
272 if ($err && $err->notimpl) {
273 #inventory not implemented
274 return $self->_find_volume_no_inventory(%params);
276 return $steps->{'handle_error'}->($err, undef) if $err;
278 # throw out the inventory result and move on if the situation has
279 # changed while we were waiting
280 return $steps->{'abort_scan'}->() if $abort_scan;
281 return $steps->{'restart_scan'}->() if $restart_scan;
283 # check if label is in the inventory
284 for my $i (0..(scalar(@$inventory)-1)) {
285 my $sl = $inventory->[$i];
286 if (defined $sl->{'label'} &&
287 $sl->{'label'} eq $label) {
288 $slot_scanned = $sl->{'slot'};
289 if ($sl->{'reserved'}) {
290 return $steps->{'handle_error'}->(
291 Amanda::Changer::Error->new('failed',
292 reason => 'volinuse',
293 message => "Volume '$label' in slot $slot_scanned is reserved"),
296 Amanda::Debug::debug("parse_inventory: load slot $slot_scanned with label '$label'");
297 $user_msg_fn->(scan_slot => 1,
298 slot => $slot_scanned,
300 $seen{$slot_scanned} = { device_status => $sl->{'device_status'},
301 f_type => $sl->{'f_type'},
302 label => $sl->{'label'} };
304 return $self->{'chg'}->load(slot => $slot_scanned,
305 res_cb => $steps->{'slot_loaded'},
306 set_current => $params{'set_current'});
310 # Remove from seen all slot that have state == SLOT_UNKNOWN
311 # It is done when as scan is restarted from interactivity object.
312 if ($remove_undef_state) {
313 for my $i (0..(scalar(@$inventory)-1)) {
314 my $slot = $inventory->[$i]->{slot};
315 if (exists($seen{$slot}) &&
316 !defined($inventory->[$i]->{state})) {
320 $remove_undef_state = 0;
323 # remove any slots where the state has changed from the list of seen slots
324 for my $i (0..(scalar(@$inventory)-1)) {
325 my $sl = $inventory->[$i];
326 my $slot = $sl->{slot};
328 defined($sl->{'state'}) &&
329 (($seen{$slot}->{'device_status'} != $sl->{'device_status'}) ||
330 (defined $seen{$slot}->{'device_status'} &&
331 $seen{$slot}->{'device_status'} == $DEVICE_STATUS_SUCCESS &&
332 $seen{$slot}->{'f_type'} != $sl->{'f_type'}) ||
333 (defined $seen{$slot}->{'device_status'} &&
334 $seen{$slot}->{'device_status'} == $DEVICE_STATUS_SUCCESS &&
335 defined $seen{$slot}->{'f_type'} &&
336 $seen{$slot}->{'f_type'} == $Amanda::Header::F_TAPESTART &&
337 $seen{$slot}->{'label'} ne $sl->{'label'}))) {
342 # scan any unseen slot already in a drive, if configured to do so
343 if ($self->{'scan_conf'}->{'scan_drive'}) {
344 for my $sl (@$inventory) {
345 my $slot = $sl->{'slot'};
346 if (defined $sl->{'loaded_in'} &&
347 !$sl->{'reserved'} &&
349 $slot_scanned = $slot;
350 $user_msg_fn->(scan_slot => 1, slot => $slot_scanned);
351 $seen{$slot_scanned} = { device_status => $sl->{'device_status'},
352 f_type => $sl->{'f_type'},
353 label => $sl->{'label'} };
355 return $self->{'chg'}->load(slot => $slot_scanned,
356 res_cb => $steps->{'slot_loaded'},
357 set_current => $params{'set_current'});
363 if ($self->{'scan_conf'}->{'scan_unknown_slot'}) {
364 #find index for current slot
365 my $current_index = undef;
366 for my $i (0..(scalar(@$inventory)-1)) {
367 my $slot = $inventory->[$i]->{slot};
368 if ($slot eq $current) {
373 #scan next slot to scan
374 $current_index = 0 if !defined $current_index;
375 for my $i ($current_index..(scalar(@$inventory)-1), 0..($current_index-1)) {
376 my $sl = $inventory->[$i];
377 my $slot = $sl->{slot};
378 # skip slots we've seen
379 next if defined($seen{$slot});
380 # skip slots that are empty
381 next if defined $sl->{'state'} &&
382 $sl->{'state'} == Amanda::Changer::SLOT_EMPTY;
383 # skip slots for which we have a known label, since it's not the
385 next if defined $sl->{'f_type'} &&
386 $sl->{'f_type'} == $Amanda::Header::F_TAPESTART;
387 next if defined $sl->{'label'};
389 # found a slot to check - reset our current slot
391 $slot_scanned = $current;
392 Amanda::Debug::debug("parse_inventory: load slot $current");
393 $user_msg_fn->(scan_slot => 1, slot => $slot_scanned);
394 $seen{$slot_scanned} = { device_status => $sl->{'device_status'},
395 f_type => $sl->{'f_type'},
396 label => $sl->{'label'} };
398 return $self->{'chg'}->load(slot => $slot_scanned,
399 res_cb => $steps->{'slot_loaded'},
400 set_current => $params{'set_current'});
404 #All slots are seen or empty.
406 return $steps->{'handle_error'}->($last_err, undef);
408 return $steps->{'handle_error'}->(
409 Amanda::Changer::Error->new('failed',
410 reason => 'notfound',
411 message => "Volume '$label' not found"),
416 step slot_loaded => sub {
417 (my $err, $res) = @_;
419 # we don't responsd to abort_scan or restart_scan here, since we
420 # have an open reservation that we should deal with.
422 $user_msg_fn->(slot_result => 1,
423 slot => $slot_scanned,
427 if ($res->{device}->status == $DEVICE_STATUS_SUCCESS &&
428 $res->{device}->volume_label &&
429 $res->{device}->volume_label eq $label) {
430 my $volume_label = $res->{device}->volume_label;
431 return $steps->{'call_res_cb'}->(undef, $res);
434 if (defined $res->{device}->volume_header) {
435 $f_type = $res->{device}->volume_header->{type};
440 # The slot did not contain the volume we wanted, so mark it
441 # as seen and try again.
442 $seen{$slot_scanned} = {
443 device_status => $res->{device}->status,
445 label => $res->{device}->volume_label
449 if ($res->{device}->status == $DEVICE_STATUS_SUCCESS) {
452 $last_err = Amanda::Changer::Error->new('fatal',
453 message => $res->{device}->error_or_status());
455 return $res->release(finished_cb => $steps->{'load_released'});
457 if ($load_for_label == 0 && $err->volinuse) {
458 # Scan semantics for volinuse is different than changer.
459 # If a slot with unknown label is loaded then we map
460 # volinuse to driveinuse.
461 $err->{reason} = "driveinuse";
463 $last_err = $err if $err->fatal || !$err->notfound;
464 if ($load_for_label == 1 && $err->failed && $err->volinuse) {
465 # volinuse is an error
466 return $steps->{'handle_error'}->($err, $steps->{'load_released'});
468 return $steps->{'load_released'}->();
472 step load_released => sub {
479 # throw out the inventory result and move on if the situation has
480 # changed while we were loading a volume
481 return $steps->{'abort_scan'}->() if $abort_scan;
482 return $steps->{'restart_scan'}->() if $restart_scan;
484 $new_slot = $current;
485 $steps->{'get_inventory'}->();
488 step handle_error => sub {
489 my ($err, $continue_cb) = @_;
491 my $scan_method = undef;
496 $poll_src->remove() if defined $poll_src;
499 # prefer to use scan method for $last_err, if present
500 if ($last_err && $err->failed && $err->notfound) {
501 $message = "$last_err";
503 if ($last_err->isa("Amanda::Changer::Error")) {
504 if ($last_err->fatal) {
505 $scan_method = $self->{'scan_conf'}->{'fatal'};
507 $scan_method = $self->{'scan_conf'}->{$last_err->{'reason'}};
509 } elsif ($continue_cb) {
510 $scan_method = SCAN_CONTINUE;
514 #use scan method for $err
515 if (!defined $scan_method) {
517 $message = "$err" if !defined $message;
519 $scan_method = $self->{'scan_conf'}->{'fatal'};
521 $scan_method = $self->{'scan_conf'}->{$err->{'reason'}};
524 die("error not defined");
525 $scan_method = SCAN_ASK_POLL;
529 ## implement the desired scan method
531 if ($scan_method == SCAN_CONTINUE && !defined $continue_cb) {
532 $scan_method = $self->{'scan_conf'}->{'notfound'};
533 if ($scan_method == SCAN_CONTINUE) {
534 $scan_method = SCAN_FAIL;
538 if ($scan_method == SCAN_ASK && !defined $self->{'interactivity'}) {
539 $scan_method = SCAN_FAIL;
542 if ($scan_method == SCAN_ASK_POLL && !defined $self->{'interactivity'}) {
543 $scan_method = SCAN_FAIL;
546 if ($scan_method == SCAN_ASK) {
547 return $steps->{'scan_interactivity'}->("$message");
548 } elsif ($scan_method == SCAN_POLL) {
549 $poll_src = Amanda::MainLoop::call_after(
550 $self->{'scan_conf'}->{'poll_delay'},
551 $steps->{'after_poll'});
553 } elsif ($scan_method == SCAN_ASK_POLL) {
554 $steps->{'scan_interactivity'}->("$message\n");
555 $poll_src = Amanda::MainLoop::call_after(
556 $self->{'scan_conf'}->{'poll_delay'},
557 $steps->{'after_poll'});
559 } elsif ($scan_method == SCAN_FAIL) {
560 return $steps->{'call_res_cb'}->($err, undef);
561 } elsif ($scan_method == SCAN_CONTINUE) {
562 return $continue_cb->($err, undef);
564 die("Invalid SCAN_* value:$err:$err->{'reason'}:$scan_method");
568 step after_poll => sub {
569 $poll_src->remove() if defined $poll_src;
571 return $steps->{'restart_scan'}->();
574 step scan_interactivity => sub {
575 my ($err_message) = @_;
576 if (!$interactivity_running) {
577 $interactivity_running = 1;
578 my $message = "$err_message\nInsert volume labeled '$label' in changer and type <enter>\nor type \"^D\" to abort\n";
579 $self->{'interactivity'}->user_request(
582 err => "$err_message",
583 chg_name => $self->{'chg'}->{'chg_name'},
584 request_cb => $steps->{'scan_interactivity_cb'});
589 step scan_interactivity_cb => sub {
590 my ($err, $message) = @_;
591 $interactivity_running = 0;
592 $poll_src->remove() if defined $poll_src;
601 return $steps->{'call_res_cb'}->($err, undef);
605 if ($message ne '') {
608 if (ref($message) eq 'HASH' and $message == $DEFAULT_CHANGER) {
609 $new_chg = Amanda::Changer->new();
611 $new_chg = Amanda::Changer->new($message);
613 if ($new_chg->isa("Amanda::Changer::Error")) {
614 return $steps->{'scan_interactivity'}->("$new_chg");
616 $self->{'chg'}->quit();
617 $self->{'chg'} = $new_chg;
620 $remove_undef_state = 1;
627 return $steps->{'restart_scan'}->();
631 step abort_scan => sub {
632 $steps->{'call_res_cb'}->($abort_scan, undef);
635 step call_res_cb => sub {
636 (my $err, $res) = @_;
638 # TODO: what happens if the search was aborted or
639 # restarted in the interim?
642 $poll_src->remove() if defined $poll_src;
644 $interactivity_running = 0;
645 $self->{'interactivity'}->abort() if defined $self->{'interactivity'};
646 $params{'res_cb'}->($err, $res);
651 sub _find_volume_no_inventory {
655 my $label = $params{'label'};
663 my $steps = define_steps
664 cb_ref => \$params{'res_cb'};
666 step load_label => sub {
667 return $self->{'chg'}->load(relative_slot => "current",
668 res_cb => $steps->{'load_label_cb'});
671 step load_label_cb => sub {
672 (my $err, $res) = @_;
675 if ($err->failed && $err->notfound) {
676 if ($err->{'message'} eq "all slots have been loaded") {
677 $err->{'message'} = "label '$label' not found";
679 return $params{'res_cb'}->($err, undef);
680 } elsif ($err->failed && $err->volinuse and defined $err->{'slot'}) {
681 $last_slot = $err->{'slot'};
683 #no interactivity yet.
684 return $params{'res_cb'}->($err, undef);
687 $last_slot = $res->{'this_slot'}
690 $seen_slots{$last_slot} = 1 if defined $last_slot;
692 my $dev = $res->{'device'};
693 if (defined $dev->volume_label && $dev->volume_label eq $label) {
694 return $params{'res_cb'}->(undef, $res);
696 return $res->release(finished_cb => $steps->{'released'});
698 return $steps->{'released'}->()
702 step released => sub {
703 $self->{'chg'}->load(relative_slot => "next",
704 except_slots => \%seen_slots,
705 res_cb => $steps->{'load_label_cb'},
714 package Amanda::Recovery::Scan::Config;
720 my $self = bless {}, $class;
722 $self->{'scan_drive'} = 0;
723 $self->{'scan_unknown_slot'} = 1;
724 $self->{'poll_delay'} = 10000; #10 seconds
726 $self->{'fatal'} = Amanda::Recovery::Scan::SCAN_CONTINUE;
727 $self->{'driveinuse'} = Amanda::Recovery::Scan::SCAN_ASK_POLL;
728 $self->{'volinuse'} = Amanda::Recovery::Scan::SCAN_ASK_POLL;
729 $self->{'notfound'} = Amanda::Recovery::Scan::SCAN_ASK_POLL;
730 $self->{'unknown'} = Amanda::Recovery::Scan::SCAN_FAIL;
731 $self->{'notimpl'} = Amanda::Recovery::Scan::SCAN_FAIL;
732 $self->{'invalid'} = Amanda::Recovery::Scan::SCAN_CONTINUE;