Imported Upstream version 3.2.0
[debian/amanda] / perl / Amanda / Taper / Scan / traditional.pm
1 # Copyright (c) 2009, 2010 Zmanda, Inc.  All Rights Reserved.
2 #
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.
6 #
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.
11 #
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.
15 #
16 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 package Amanda::Taper::Scan::traditional;
20
21 =head1 NAME
22
23 Amanda::Taper::Scan::traditional
24
25 =head1 SYNOPSIS
26
27 This package implements the "traditional" taperscan algorithm.  See
28 C<amanda-taperscan(7)>.
29
30 =cut
31
32 use strict;
33 use warnings;
34 use base qw( Amanda::Taper::Scan );
35 use Amanda::Tapelist;
36 use Amanda::Config qw( :getconf );
37 use Amanda::Device qw( :constants );
38 use Amanda::Header;
39 use Amanda::Debug qw( :logging );
40 use Amanda::MainLoop;
41
42 sub new {
43     my $class = shift;
44     my %params = @_;
45
46     # parent will set all of the $params{..} keys for us
47     my $self = bless {
48         scanning => 0,
49         tapelist => undef,
50         seen => {},
51         scan_num => 0,
52     }, $class;
53
54     return $self;
55 }
56
57 sub scan {
58     my $self = shift;
59     my %params = @_;
60
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 {};
64
65     # refresh the tapelist at every scan
66     $self->read_tapelist();
67
68     # count the number of scans we do, so we can only load 'current' on the
69     # first scan
70     $self->{'scan_num'}++;
71
72     $self->stage_1($params{'result_cb'});
73 }
74
75 sub _user_msg {
76     my $self = shift;
77     my %params = @_;
78     $self->{'user_msg_fn'}->(%params);
79 }
80
81 sub scan_result {
82     my $self = shift;
83     my %params = @_;
84
85     my @result = ($params{'error'}, $params{'res'}, $params{'label'},
86                   $params{'mode'}, $params{'is_new'});
87
88     if ($params{'error'}) {
89         debug("Amanda::Taper::Scan::traditional result: error=$params{'error'}");
90
91         # if we already had a reservation when the error occurred, then we'll need
92         # to release that reservation before signalling the error
93         if ($params{'res'}) {
94             my $finished_cb = make_cb(finished_cb => sub {
95                 my ($err) = @_;
96                 # if there was an error releasing, log it and ignore it
97                 Amanda::Debug::warn("while releasing reservation: $err") if $err;
98
99                 $self->{'scanning'} = 0;
100                 $params{'result_cb'}->(@result);
101             });
102             return $params{'res'}->release(finished_cb => $finished_cb);
103         }
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}");
109     } else {
110         debug("Amanda::Taper::Scan::traditional result: scan failed");
111
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
114         # anyway.
115         my $oldest_reusable = $self->oldest_reusable_volume(new_label_ok => 0);
116         $self->_user_msg(scan_failed => 1,
117                          expected_label => $oldest_reusable,
118                          expected_new => 1);
119         @result = ("No acceptable volumes found");
120     }
121
122     $self->{'scanning'} = 0;
123     $params{'result_cb'}->(@result);
124 }
125
126 ##
127 # stage 1: search for the oldest reusable volume
128
129 sub stage_1 {
130     my $self = shift;
131     my ($result_cb) = @_;
132     my $oldest_reusable;
133
134     my $steps = define_steps
135         cb_ref => \$result_cb;
136
137     step setup => sub {
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
141         );
142
143         if (!defined $oldest_reusable) {
144             debug("Amanda::Taper::Scan::traditional no oldest reusable volume");
145             return $self->stage_2($result_cb);
146         }
147         debug("Amanda::Taper::Scan::traditional oldest reusable volume is '$oldest_reusable'");
148
149         # try loading that oldest volume, but only if the changer is fast-search capable
150         $steps->{'get_info'}->();
151     };
152
153     step get_info => sub {
154         $self->{'changer'}->info(
155             info => [ "fast_search" ],
156             info_cb => $steps->{'got_info'},
157         );
158     };
159
160     step got_info => sub {
161         my ($error, %results) = @_;
162         if ($error) {
163             return $self->scan_result(error => $error, result_cb => $result_cb);
164         }
165
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);
171
172             $steps->{'do_load'}->();
173         } else {
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);
177         }
178     };
179
180     step do_load => sub {
181         $self->{'changer'}->load(
182             label => $oldest_reusable,
183             set_current => 1,
184             res_cb => $steps->{'load_done'});
185     };
186
187     step load_done => sub {
188         my ($err, $res) = @_;
189
190         $self->_user_msg(search_result => 1, res => $res, err => $err);
191         if ($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);
195             } else {
196                 return $self->scan_result(error => $err,
197                         res => $res, result_cb => $result_cb);
198             }
199         }
200
201         $self->{'seen'}->{$res->{'this_slot'}} = 1;
202
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);
207         }
208
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);
215         }
216
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);
220     };
221 }
222
223 sub try_volume {
224     my $self = shift;
225     my ($res, $result_cb) = @_;
226
227     my $slot = $res->{'this_slot'};
228     my $dev = $res->{'device'};
229     my $status = $dev->status;
230     my $labelstr = $self->{'labelstr'};
231     my $label;
232     my $autolabel = $self->{'autolabel'};
233
234     if ($status == $DEVICE_STATUS_SUCCESS) {
235         $label = $dev->volume_label;
236
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,
242                                  slot                    => $slot,
243                                  res                     => $res);
244                 return 0;
245             }
246         } else {
247             # verify that the label is in the tapelist
248             my $tle = $self->{'tapelist'}->lookup_tapelabel($label);
249             if (!$tle) {
250                 $self->_user_msg(slot_result     => 1,
251                                  not_in_tapelist => 1,
252                                  slot            => $slot,
253                                  res             => $res);
254                 return 0;
255             }
256
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,
260                                  active      => 1,
261                                  slot        => $slot,
262                                  res         => $res);
263                 return 0;
264             }
265             $self->_user_msg(slot_result => 1,
266                              slot        => $slot,
267                              res         => $res);
268             $self->scan_result(res => $res, label => $label,
269                     mode => $ACCESS_WRITE, is_new => 0, result_cb => $result_cb);
270             return 1;
271         }
272     }
273
274     if (!defined $autolabel->{'template'} ||
275         $autolabel->{'template'} eq "") {
276         $self->_user_msg(slot_result => 1,
277                          slot        => $slot,
278                          res         => $res);
279         return 0;
280     }
281
282     $self->_user_msg(slot_result => 1, slot => $slot, res => $res);
283
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) {
295         return 0;
296     }
297
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);
302         return 1;
303     }
304
305     $self->scan_result(res => $res, label => $label, mode => $ACCESS_WRITE,
306             is_new => 1, result_cb => $result_cb);
307     return 1;
308 }
309
310 ##
311 # stage 2: scan for any usable volume
312
313 sub release_and_stage_2 {
314     my $self = shift;
315     my ($res, $result_cb) = @_;
316
317     $res->release(finished_cb => sub {
318         my ($error) = @_;
319         if ($error) {
320             $self->scan_result(error => $error, result_cb => $result_cb);
321         } else {
322             $self->stage_2($result_cb);
323         }
324     });
325 }
326
327 sub stage_2 {
328     my $self = shift;
329     my ($result_cb) = @_;
330
331     my $last_slot;
332     my $load_current = ($self->{'scan_num'} == 1);
333     my $steps = define_steps
334         cb_ref => \$result_cb;
335
336     step load => sub {
337         my ($err) = @_;
338
339         debug("Amanda::Taper::Scan::traditional stage 2: scan for any reusable volume");
340
341         # bail on an error releasing a reservation
342         if ($err) {
343             return $self->scan_result(error => $err, result_cb => $result_cb);
344         }
345
346         # load the current or next slot
347         my @load_args;
348         if ($load_current) {
349             # load 'current' the first time through
350             @load_args = (
351                 relative_slot => 'current',
352             );
353         } else {
354             @load_args = (
355                 relative_slot => 'next',
356                 (defined $last_slot)? (slot => $last_slot) : (),
357             );
358         }
359
360         $self->{'changer'}->load(
361             @load_args,
362             set_current => 1,
363             res_cb => $steps->{'loaded'},
364             except_slots => $self->{'seen'},
365             mode => "write",
366         );
367     };
368
369     step loaded => sub {
370         my ($err, $res) = @_;
371         my $loaded_current = $load_current;
372         $load_current = 0; # don't load current a second time
373
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);
378         }
379
380         # tell user_msg which slot we're looking at..
381         if (defined $res) {
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'});
385         } else {
386             $self->_user_msg(scan_slot => 1, slot => "?");
387         }
388
389         # and then tell it the result if already known (error) or try
390         # loading the volume.
391         if ($err) {
392             my $ignore_error = 0;
393             # there are two "acceptable" errors: if the slot exists but the volume
394             # is already in use
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);
399
400             if ($ignore_error) {
401                 $self->_user_msg(slot_result => 1, err => $err);
402                 if ($err->{'slot'}) {
403                     $last_slot = $err->{slot};
404                     $self->{'seen'}->{$last_slot} = 1;
405                 }
406                 return $steps->{'load'}->(undef);
407             } else {
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);
413             }
414         }
415
416         $self->{'seen'}->{$res->{'this_slot'}} = 1;
417
418         # we're done if try_volume calls result_cb (with success or an error)
419         return if ($self->try_volume($res, $result_cb));
420
421         # no luck -- release this reservation and get the next
422         $last_slot = $res->{'this_slot'};
423
424         $res->release(finished_cb => $steps->{'load'});
425     };
426 }
427
428 1;