2 * Match Bootstrap Records (used for restores) against
5 * Kern Sibbald, June MMII
11 Copyright (C) 2000-2003 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 /* Forward references */
35 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done);
36 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done);
37 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec);
38 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done);
39 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done);
40 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done);
41 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done);
42 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done);
43 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done);
44 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done);
45 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done);
46 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec, bool done);
47 static int match_block_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_BLOCK *block);
48 static int match_block_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_BLOCK *block);
49 static BSR *find_smallest_volfile(BSR *fbsr, BSR *bsr);
52 /*********************************************************************
54 * If possible, position the archive device (tape) to read the
57 void position_bsr_block(BSR *bsr, DEV_BLOCK *block)
59 /* To be implemented */
62 /*********************************************************************
64 * Do fast block rejection based on bootstrap records.
65 * use_fast_rejection will be set if we have VolSessionId and VolSessTime
66 * in each record. When BlockVer is >= 2, we have those in the block header
67 * so can do fast rejection.
69 * returns: 1 if block may contain valid records
70 * 0 if block may be skipped (i.e. it contains no records of
71 * that can match the bsr).
74 int match_bsr_block(BSR *bsr, DEV_BLOCK *block)
76 if (!bsr || !bsr->use_fast_rejection || (block->BlockVer < 2)) {
77 return 1; /* cannot fast reject */
80 if (match_block_sesstime(bsr, bsr->sesstime, block)) {
83 return match_block_sessid(bsr, bsr->sessid, block);
86 static int match_block_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_BLOCK *block)
89 return 1; /* no specification matches all */
91 if (sesstime->sesstime == block->VolSessionTime) {
95 return match_block_sesstime(bsr, sesstime->next, block);
100 static int match_block_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_BLOCK *block)
103 return 1; /* no specification matches all */
105 if (sessid->sessid <= block->VolSessionId && sessid->sessid2 >= block->VolSessionId) {
109 return match_block_sessid(bsr, sessid->next, block);
115 /*********************************************************************
117 * Match Bootstrap records
119 * returns 0 no match and reposition is set if we should
120 * reposition the tape
121 * returns -1 no additional matches possible
123 int match_bsr(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec)
128 * The bsr->reposition flag is set any time a bsr is done.
129 * In this case, we can probably reposition the
130 * tape to the next available bsr position.
133 bsr->reposition = false;
134 stat = match_all(bsr, rec, volrec, sessrec, true);
136 * Note, bsr->reposition is set by match_all when
137 * a bsr is done. We turn it off if a match was
138 * found or if we cannot use poistioning
140 if (stat != 0 || !bsr->use_positioning) {
141 bsr->reposition = false;
144 stat = 1; /* no bsr => match all */
150 * Find the next bsr that applies to the current tape.
151 * It is the one with the smallest VolFile position.
153 BSR *find_next_bsr(BSR *root_bsr, DEVICE *dev)
156 BSR *found_bsr = NULL;
158 if (!root_bsr || !root_bsr->use_positioning ||
159 !root_bsr->reposition || !dev_is_tape(dev)) {
160 Dmsg2(000, "use_pos=%d repos=%d\n", root_bsr->use_positioning,
161 root_bsr->reposition);
164 root_bsr->mount_next_volume = false;
165 for (bsr=root_bsr; bsr; bsr=bsr->next) {
166 if (bsr->done || !match_volume(bsr, bsr->volume, &dev->VolHdr, 1)) {
169 if (found_bsr == NULL) {
172 found_bsr = find_smallest_volfile(found_bsr, bsr);
176 * If we get to this point and found no bsr, it means
177 * that any additional bsr's must apply to the next
178 * tape, so set a flag.
180 if (found_bsr == NULL) {
181 root_bsr->mount_next_volume = true;
186 static BSR *find_smallest_volfile(BSR *found_bsr, BSR *bsr)
188 BSR *return_bsr = found_bsr;
191 uint32_t found_bsr_sfile, bsr_sfile;
192 uint32_t found_bsr_sblock, bsr_sblock;
194 vf = found_bsr->volfile;
195 found_bsr_sfile = vf->sfile;
196 while ( (vf=vf->next) ) {
197 if (vf->sfile < found_bsr_sfile) {
198 found_bsr_sfile = vf->sfile;
202 bsr_sfile = vf->sfile;
203 while ( (vf=vf->next) ) {
204 if (vf->sfile < bsr_sfile) {
205 bsr_sfile = vf->sfile;
208 if (found_bsr_sfile > bsr_sfile) {
210 } else if (found_bsr_sfile == bsr_sfile) {
211 /* Must check block */
212 vb = found_bsr->volblock;
213 found_bsr_sblock = vb->sblock;
214 while ( (vb=vb->next) ) {
215 if (vb->sblock < found_bsr_sblock) {
216 found_bsr_sblock = vb->sblock;
220 bsr_sblock = vb->sblock;
221 while ( (vb=vb->next) ) {
222 if (vb->sblock < bsr_sblock) {
223 bsr_sblock = vb->sblock;
226 if (found_bsr_sblock > bsr_sblock) {
235 * Match all the components of current record
238 * returns -1 no additional matches possible
240 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec,
241 SESSION_LABEL *sessrec, bool done)
246 if (bsr->count && bsr->count <= bsr->found) {
248 bsr->root->reposition = true;
249 Dmsg0(100, "bsr done from count\n");
252 if (!match_volume(bsr, bsr->volume, volrec, 1)) {
255 if (!match_volfile(bsr, bsr->volfile, rec, 1)) {
258 if (!match_sesstime(bsr, bsr->sesstime, rec, 1)) {
262 /* NOTE!! This test MUST come after the sesstime test */
263 if (!match_sessid(bsr, bsr->sessid, rec)) {
267 /* NOTE!! This test MUST come after sesstime and sessid tests */
268 if (!match_findex(bsr, bsr->FileIndex, rec, 1)) {
271 if (!match_jobid(bsr, bsr->JobId, sessrec, 1)) {
274 if (!match_job(bsr, bsr->job, sessrec, 1)) {
277 if (!match_client(bsr, bsr->client, sessrec, 1)) {
280 if (!match_job_type(bsr, bsr->JobType, sessrec, 1)) {
283 if (!match_job_level(bsr, bsr->JobLevel, sessrec, 1)) {
286 if (!match_stream(bsr, bsr->stream, rec, 1)) {
294 return match_all(bsr->next, rec, volrec, sessrec, bsr->done && done);
296 if (bsr->done && done) {
302 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done)
305 return 0; /* Volume must match */
307 if (strcmp(volume->VolumeName, volrec->VolName) == 0) {
311 return match_volume(bsr, volume->next, volrec, 1);
316 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done)
319 return 1; /* no specification matches all */
321 if (fnmatch(client->ClientName, sessrec->ClientName, 0) == 0) {
325 return match_client(bsr, client->next, sessrec, 1);
330 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done)
333 return 1; /* no specification matches all */
335 if (fnmatch(job->Job, sessrec->Job, 0) == 0) {
339 return match_job(bsr, job->next, sessrec, 1);
344 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done)
347 return 1; /* no specification matches all */
349 if (job_type->JobType == sessrec->JobType) {
352 if (job_type->next) {
353 return match_job_type(bsr, job_type->next, sessrec, 1);
358 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done)
361 return 1; /* no specification matches all */
363 if (job_level->JobLevel == sessrec->JobLevel) {
366 if (job_level->next) {
367 return match_job_level(bsr, job_level->next, sessrec, 1);
372 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done)
375 return 1; /* no specification matches all */
377 if (jobid->JobId <= sessrec->JobId && jobid->JobId2 >= sessrec->JobId) {
381 return match_jobid(bsr, jobid->next, sessrec, 1);
386 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done)
389 return 1; /* no specification matches all */
391 /* For the moment, these tests work only with tapes. */
392 if (!(rec->state & REC_ISTAPE)) {
393 return 1; /* All File records OK for this match */
395 // Dmsg3(000, "match_volfile: sfile=%d efile=%d recfile=%d\n",
396 // volfile->sfile, volfile->efile, rec->File);
397 if (volfile->sfile <= rec->File && volfile->efile >= rec->File) {
400 /* Once we get past last efile, we are done */
401 if (rec->File > volfile->efile) {
402 volfile->done = true; /* set local done */
405 return match_volfile(bsr, volfile->next, rec, volfile->done && done);
408 /* If we are done and all prior matches are done, this bsr is finished */
409 if (volfile->done && done) {
411 bsr->root->reposition = true;
412 Dmsg0(100, "bsr done from volfile\n");
417 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done)
420 return 1; /* no specification matches all */
422 if (stream->stream == rec->Stream) {
426 return match_stream(bsr, stream->next, rec, 1);
431 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done)
434 return 1; /* no specification matches all */
436 if (sesstime->sesstime == rec->VolSessionTime) {
439 if (rec->VolSessionTime > sesstime->sesstime) {
440 sesstime->done = true;
442 if (sesstime->next) {
443 return match_sesstime(bsr, sesstime->next, rec, sesstime->done && done);
445 if (sesstime->done && done) {
447 bsr->root->reposition = true;
448 Dmsg0(100, "bsr done from sesstime\n");
453 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec)
456 return 1; /* no specification matches all */
458 if (sessid->sessid <= rec->VolSessionId && sessid->sessid2 >= rec->VolSessionId) {
462 return match_sessid(bsr, sessid->next, rec);
467 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done)
470 return 1; /* no specification matches all */
472 if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
475 if (rec->FileIndex > findex->findex2) {
479 return match_findex(bsr, findex->next, rec, findex->done && done);
481 if (findex->done && done) {
483 bsr->root->reposition = true;
484 Dmsg1(100, "bsr done from findex %d\n", rec->FileIndex);