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::Taper::Scan::traditional;
23 Amanda::Taper::Scan::traditional
27 This package implements the "traditional" taperscan algorithm. See
28 C<amanda-taperscan(7)>.
34 use base qw( Amanda::Taper::Scan );
36 use Amanda::Config qw( :getconf );
37 use Amanda::Device qw( :constants );
39 use Amanda::Debug qw( :logging );
46 # parent will set all of the $params{..} keys for us
61 die "Can only run one scan at a time" if $self->{'scanning'};
62 $self->{'scanning'} = 1;
63 $self->{'user_msg_fn'} = $params{'user_msg_fn'} || sub {};
65 # refresh the tapelist at every scan
66 $self->read_tapelist();
68 # count the number of scans we do, so we can only load 'current' on the
70 $self->{'scan_num'}++;
72 $self->stage_1($params{'result_cb'});
78 $self->{'user_msg_fn'}->(%params);
85 my @result = ($params{'error'}, $params{'res'}, $params{'label'},
86 $params{'mode'}, $params{'is_new'});
88 if ($params{'error'}) {
89 debug("Amanda::Taper::Scan::traditional result: error=$params{'error'}");
91 # if we already had a reservation when the error occurred, then we'll need
92 # to release that reservation before signalling the error
94 my $finished_cb = make_cb(finished_cb => sub {
96 # if there was an error releasing, log it and ignore it
97 Amanda::Debug::warn("while releasing reservation: $err") if $err;
99 $self->{'scanning'} = 0;
100 $params{'result_cb'}->(@result);
102 return $params{'res'}->release(finished_cb => $finished_cb);
104 } elsif ($params{'res'}) {
105 my $devname = $params{'res'}->{'device'}->device_name;
106 my $slot = $params{'res'}->{'this_slot'};
107 debug("Amanda::Taper::Scan::traditional result: '$params{label}' " .
108 "on $devname slot $slot, mode $params{mode}");
110 debug("Amanda::Taper::Scan::traditional result: scan failed");
112 # we may not ever have looked for this, the oldest reusable volume, if
113 # the changer is not fast-searchable. But we'll tell the user about it
115 my $oldest_reusable = $self->oldest_reusable_volume(new_label_ok => 0);
116 $self->_user_msg(scan_failed => 1,
117 expected_label => $oldest_reusable,
119 @result = ("No acceptable volumes found");
122 $self->{'scanning'} = 0;
123 $params{'result_cb'}->(@result);
127 # stage 1: search for the oldest reusable volume
131 my ($result_cb) = @_;
134 my $steps = define_steps
135 cb_ref => \$result_cb;
138 debug("Amanda::Taper::Scan::traditional stage 1: search for oldest reusable volume");
139 $oldest_reusable = $self->oldest_reusable_volume(
140 new_label_ok => 0, # stage 1 never selects new volumes
143 if (!defined $oldest_reusable) {
144 debug("Amanda::Taper::Scan::traditional no oldest reusable volume");
145 return $self->stage_2($result_cb);
147 debug("Amanda::Taper::Scan::traditional oldest reusable volume is '$oldest_reusable'");
149 # try loading that oldest volume, but only if the changer is fast-search capable
150 $steps->{'get_info'}->();
153 step get_info => sub {
154 $self->{'changer'}->info(
155 info => [ "fast_search" ],
156 info_cb => $steps->{'got_info'},
160 step got_info => sub {
161 my ($error, %results) = @_;
163 return $self->scan_result(error => $error, result_cb => $result_cb);
166 if ($results{'fast_search'}) {
167 debug("Amanda::Taper::Scan::traditional stage 1: searching oldest reusable " .
168 "volume '$oldest_reusable'");
169 $self->_user_msg(search_label => 1,
170 label => $oldest_reusable);
172 $steps->{'do_load'}->();
174 # no fast search, so skip to stage 2
175 debug("Amanda::Taper::Scan::traditional changer is not fast-searchable; skipping to stage 2");
176 $self->stage_2($result_cb);
180 step do_load => sub {
181 $self->{'changer'}->load(
182 label => $oldest_reusable,
184 res_cb => $steps->{'load_done'});
187 step load_done => sub {
188 my ($err, $res) = @_;
190 $self->_user_msg(search_result => 1, res => $res, err => $err);
192 if ($err->failed and $err->notfound) {
193 debug("Amanda::Taper::Scan::traditional oldest reusable volume not found");
194 return $self->stage_2($result_cb);
196 return $self->scan_result(error => $err,
197 res => $res, result_cb => $result_cb);
201 $self->{'seen'}->{$res->{'this_slot'}} = 1;
203 my $status = $res->{'device'}->status;
204 if ($status != $DEVICE_STATUS_SUCCESS) {
205 warning "Error reading label after searching for '$oldest_reusable'";
206 return $self->release_and_stage_2($res, $result_cb);
209 # go on to stage 2 if we didn't get the expected volume
210 my $label = $res->{'device'}->volume_label;
211 my $labelstr = $self->{'labelstr'};
212 if ($label !~ /$labelstr/) {
213 warning "Searched for label '$oldest_reusable' but found a volume labeled '$label'";
214 return $self->release_and_stage_2($res, $result_cb);
217 # great! -- volume found
218 return $self->scan_result(res => $res, label => $oldest_reusable,
219 mode => $ACCESS_WRITE, is_new => 0, result_cb => $result_cb);
225 my ($res, $result_cb) = @_;
227 my $slot = $res->{'this_slot'};
228 my $dev = $res->{'device'};
229 my $status = $dev->status;
230 my $labelstr = $self->{'labelstr'};
232 my $autolabel = $self->{'autolabel'};
234 if ($status == $DEVICE_STATUS_SUCCESS) {
235 $label = $dev->volume_label;
237 if ($label !~ /$labelstr/) {
238 if (!$autolabel->{'other_config'}) {
239 $self->_user_msg(slot_result => 1,
240 does_not_match_labelstr => 1,
241 labelstr => $labelstr,
247 # verify that the label is in the tapelist
248 my $tle = $self->{'tapelist'}->lookup_tapelabel($label);
250 $self->_user_msg(slot_result => 1,
251 not_in_tapelist => 1,
257 # see if it's reusable
258 if (!$self->is_reusable_volume(label => $label, new_label_ok => 1)) {
259 $self->_user_msg(slot_result => 1,
265 $self->_user_msg(slot_result => 1,
268 $self->scan_result(res => $res, label => $label,
269 mode => $ACCESS_WRITE, is_new => 0, result_cb => $result_cb);
274 if (!defined $autolabel->{'template'} ||
275 $autolabel->{'template'} eq "") {
276 $self->_user_msg(slot_result => 1,
282 $self->_user_msg(slot_result => 1, slot => $slot, res => $res);
284 if ($status & $DEVICE_STATUS_VOLUME_UNLABELED and
285 $dev->volume_header and
286 $dev->volume_header->{'type'} == $Amanda::Header::F_EMPTY) {
287 return 0 if (!$autolabel->{'empty'});
288 } elsif ($status & $DEVICE_STATUS_VOLUME_UNLABELED and
289 $dev->volume_header and
290 $dev->volume_header->{'type'} == $Amanda::Header::F_WEIRD) {
291 return 0 if (!$autolabel->{'non_amanda'});
292 } elsif ($status & $DEVICE_STATUS_VOLUME_ERROR) {
293 return 0 if (!$autolabel->{'volume_error'});
294 } elsif ($status != $DEVICE_STATUS_SUCCESS) {
298 ($label, my $err) = $self->make_new_tape_label();
299 if (!defined $label) {
300 # make this fatal, rather than silently skipping new tapes
301 $self->scan_result(error => $err, res => $res, result_cb => $result_cb);
305 $self->scan_result(res => $res, label => $label, mode => $ACCESS_WRITE,
306 is_new => 1, result_cb => $result_cb);
311 # stage 2: scan for any usable volume
313 sub release_and_stage_2 {
315 my ($res, $result_cb) = @_;
317 $res->release(finished_cb => sub {
320 $self->scan_result(error => $error, result_cb => $result_cb);
322 $self->stage_2($result_cb);
329 my ($result_cb) = @_;
332 my $load_current = ($self->{'scan_num'} == 1);
333 my $steps = define_steps
334 cb_ref => \$result_cb;
339 debug("Amanda::Taper::Scan::traditional stage 2: scan for any reusable volume");
341 # bail on an error releasing a reservation
343 return $self->scan_result(error => $err, result_cb => $result_cb);
346 # load the current or next slot
349 # load 'current' the first time through
351 relative_slot => 'current',
355 relative_slot => 'next',
356 (defined $last_slot)? (slot => $last_slot) : (),
360 $self->{'changer'}->load(
363 res_cb => $steps->{'loaded'},
364 except_slots => $self->{'seen'},
370 my ($err, $res) = @_;
371 my $loaded_current = $load_current;
372 $load_current = 0; # don't load current a second time
374 # bail out immediately if the scan is complete
375 if ($err and $err->failed and $err->notfound) {
376 # no error, no reservation -> end of the scan
377 return $self->scan_result(result_cb => $result_cb);
380 # tell user_msg which slot we're looking at..
382 $self->_user_msg(scan_slot => 1, slot => $res->{'this_slot'});
383 } elsif (defined $err->{'slot'}) {
384 $self->_user_msg(scan_slot => 1, slot => $err->{'slot'});
386 $self->_user_msg(scan_slot => 1, slot => "?");
389 # and then tell it the result if already known (error) or try
390 # loading the volume.
392 my $ignore_error = 0;
393 # there are two "acceptable" errors: if the slot exists but the volume
395 $ignore_error = 1 if ($err->volinuse && $err->{slot});
396 # or if we loaded the 'current' slot and it was invalid (this happens if
397 # the user changes 'use-slots', for example
398 $ignore_error = 1 if ($loaded_current && $err->invalid);
401 $self->_user_msg(slot_result => 1, err => $err);
402 if ($err->{'slot'}) {
403 $last_slot = $err->{slot};
404 $self->{'seen'}->{$last_slot} = 1;
406 return $steps->{'load'}->(undef);
408 # if we have a fatal error or something other than "notfound"
409 # or "volinuse", bail out.
410 $self->_user_msg(slot_result => 1, err => $err);
411 return $self->scan_result(error => $err, res => $res,
412 result_cb => $result_cb);
416 $self->{'seen'}->{$res->{'this_slot'}} = 1;
418 # we're done if try_volume calls result_cb (with success or an error)
419 return if ($self->try_volume($res, $result_cb));
421 # no luck -- release this reservation and get the next
422 $last_slot = $res->{'this_slot'};
424 $res->release(finished_cb => $steps->{'load'});