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 for ( ; bsr; bsr=bsr->next) {
81 if (!match_block_sesstime(bsr, bsr->sesstime, block)) {
84 if (!match_block_sessid(bsr, bsr->sessid, block)) {
92 static int match_block_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_BLOCK *block)
95 return 1; /* no specification matches all */
97 if (sesstime->sesstime == block->VolSessionTime) {
100 if (sesstime->next) {
101 return match_block_sesstime(bsr, sesstime->next, block);
106 static int match_block_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_BLOCK *block)
109 return 1; /* no specification matches all */
111 if (sessid->sessid <= block->VolSessionId && sessid->sessid2 >= block->VolSessionId) {
115 return match_block_sessid(bsr, sessid->next, block);
121 /*********************************************************************
123 * Match Bootstrap records
125 * returns 0 no match and reposition is set if we should
126 * reposition the tape
127 * returns -1 no additional matches possible
129 int match_bsr(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec)
134 * The bsr->reposition flag is set any time a bsr is done.
135 * In this case, we can probably reposition the
136 * tape to the next available bsr position.
139 bsr->reposition = false;
140 stat = match_all(bsr, rec, volrec, sessrec, true);
142 * Note, bsr->reposition is set by match_all when
143 * a bsr is done. We turn it off if a match was
144 * found or if we cannot use positioning
146 if (stat != 0 || !bsr->use_positioning) {
147 bsr->reposition = false;
150 stat = 1; /* no bsr => match all */
156 * Find the next bsr that applies to the current tape.
157 * It is the one with the smallest VolFile position.
159 BSR *find_next_bsr(BSR *root_bsr, DEVICE *dev)
162 BSR *found_bsr = NULL;
164 if (!root_bsr || !root_bsr->use_positioning ||
165 !root_bsr->reposition || !dev_is_tape(dev)) {
166 Dmsg2(100, "No nxt_bsr use_pos=%d repos=%d\n", root_bsr->use_positioning, root_bsr->reposition);
169 Dmsg2(100, "use_pos=%d repos=%d\n", root_bsr->use_positioning, root_bsr->reposition);
170 root_bsr->mount_next_volume = false;
171 for (bsr=root_bsr; bsr; bsr=bsr->next) {
172 if (bsr->done || !match_volume(bsr, bsr->volume, &dev->VolHdr, 1)) {
175 if (found_bsr == NULL) {
178 found_bsr = find_smallest_volfile(found_bsr, bsr);
182 * If we get to this point and found no bsr, it means
183 * that any additional bsr's must apply to the next
184 * tape, so set a flag.
186 if (found_bsr == NULL) {
187 root_bsr->mount_next_volume = true;
192 static BSR *find_smallest_volfile(BSR *found_bsr, BSR *bsr)
194 BSR *return_bsr = found_bsr;
197 uint32_t found_bsr_sfile, bsr_sfile;
198 uint32_t found_bsr_sblock, bsr_sblock;
200 vf = found_bsr->volfile;
201 found_bsr_sfile = vf->sfile;
202 while ( (vf=vf->next) ) {
203 if (vf->sfile < found_bsr_sfile) {
204 found_bsr_sfile = vf->sfile;
208 bsr_sfile = vf->sfile;
209 while ( (vf=vf->next) ) {
210 if (vf->sfile < bsr_sfile) {
211 bsr_sfile = vf->sfile;
214 if (found_bsr_sfile > bsr_sfile) {
216 } else if (found_bsr_sfile == bsr_sfile) {
217 /* Must check block */
218 vb = found_bsr->volblock;
219 found_bsr_sblock = vb->sblock;
220 while ( (vb=vb->next) ) {
221 if (vb->sblock < found_bsr_sblock) {
222 found_bsr_sblock = vb->sblock;
226 bsr_sblock = vb->sblock;
227 while ( (vb=vb->next) ) {
228 if (vb->sblock < bsr_sblock) {
229 bsr_sblock = vb->sblock;
232 if (found_bsr_sblock > bsr_sblock) {
241 * Called to tell the matcher that the end of
242 * the current file has been reached.
243 * The bsr argument is not used, but is included
244 * for consistency with the other match calls.
246 * Returns: true if we should reposition
249 bool match_set_eof(BSR *bsr, DEV_RECORD *rec)
251 BSR *rbsr = rec->bsr;
252 Dmsg1(100, "match_set %d\n", rbsr != NULL);
258 if (rbsr->count && rbsr->found >= rbsr->count) {
260 rbsr->root->reposition = true;
261 Dmsg2(100, "match_set_eof reposition count=%d found=%d\n",
262 rbsr->count, rbsr->found);
269 * Match all the components of current record
272 * returns -1 no additional matches possible
274 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec,
275 SESSION_LABEL *sessrec, bool done)
280 if (!match_volume(bsr, bsr->volume, volrec, 1)) {
283 if (!match_volfile(bsr, bsr->volfile, rec, 1)) {
284 Dmsg2(100, "Fail on file. bsr=%d rec=%d\n", bsr->volfile->efile,
288 if (!match_sesstime(bsr, bsr->sesstime, rec, 1)) {
289 Dmsg2(100, "Fail on sesstime. bsr=%d rec=%d\n",
290 bsr->sesstime->sesstime, rec->VolSessionTime);
294 /* NOTE!! This test MUST come after the sesstime test */
295 if (!match_sessid(bsr, bsr->sessid, rec)) {
296 Dmsg2(100, "Fail on sessid. bsr=%d rec=%d\n",
297 bsr->sessid->sessid, rec->VolSessionId);
301 /* NOTE!! This test MUST come after sesstime and sessid tests */
302 if (!match_findex(bsr, bsr->FileIndex, rec, 1)) {
303 Dmsg2(100, "Fail on findex. bsr=%d rec=%d\n",
304 bsr->FileIndex->findex2, rec->FileIndex);
308 * If a count was specified and we have a FileIndex, assume
309 * it is a Bacula created bsr (or the equivalent). We
310 * then save the bsr where the match occurred so that
311 * after processing the record or records, we can update
312 * the found count. I.e. rec->bsr points to the bsr that
313 * satisfied the match.
315 if (bsr->count && bsr->FileIndex) {
317 return 1; /* this is a complete match */
321 * The selections below are not used by Bacula's
322 * restore command, and don't work because of
323 * the rec->bsr = bsr optimization above.
325 if (!match_jobid(bsr, bsr->JobId, sessrec, 1)) {
329 if (!match_job(bsr, bsr->job, sessrec, 1)) {
332 if (!match_client(bsr, bsr->client, sessrec, 1)) {
335 if (!match_job_type(bsr, bsr->JobType, sessrec, 1)) {
338 if (!match_job_level(bsr, bsr->JobLevel, sessrec, 1)) {
341 if (!match_stream(bsr, bsr->stream, rec, 1)) {
348 return match_all(bsr->next, rec, volrec, sessrec, bsr->done && done);
350 if (bsr->done && done) {
356 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done)
359 return 0; /* Volume must match */
361 if (strcmp(volume->VolumeName, volrec->VolName) == 0) {
365 return match_volume(bsr, volume->next, volrec, 1);
370 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done)
373 return 1; /* no specification matches all */
375 if (fnmatch(client->ClientName, sessrec->ClientName, 0) == 0) {
379 return match_client(bsr, client->next, sessrec, 1);
384 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done)
387 return 1; /* no specification matches all */
389 if (fnmatch(job->Job, sessrec->Job, 0) == 0) {
393 return match_job(bsr, job->next, sessrec, 1);
398 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done)
401 return 1; /* no specification matches all */
403 if (job_type->JobType == sessrec->JobType) {
406 if (job_type->next) {
407 return match_job_type(bsr, job_type->next, sessrec, 1);
412 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done)
415 return 1; /* no specification matches all */
417 if (job_level->JobLevel == sessrec->JobLevel) {
420 if (job_level->next) {
421 return match_job_level(bsr, job_level->next, sessrec, 1);
426 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done)
429 return 1; /* no specification matches all */
431 if (jobid->JobId <= sessrec->JobId && jobid->JobId2 >= sessrec->JobId) {
435 return match_jobid(bsr, jobid->next, sessrec, 1);
440 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done)
443 return 1; /* no specification matches all */
445 /* For the moment, these tests work only with tapes. */
446 if (!(rec->state & REC_ISTAPE)) {
447 return 1; /* All File records OK for this match */
449 // Dmsg3(100, "match_volfile: sfile=%d efile=%d recfile=%d\n",
450 // volfile->sfile, volfile->efile, rec->File);
451 if (volfile->sfile <= rec->File && volfile->efile >= rec->File) {
454 /* Once we get past last efile, we are done */
455 if (rec->File > volfile->efile) {
456 volfile->done = true; /* set local done */
459 return match_volfile(bsr, volfile->next, rec, volfile->done && done);
462 /* If we are done and all prior matches are done, this bsr is finished */
463 if (volfile->done && done) {
465 bsr->root->reposition = true;
466 Dmsg2(100, "bsr done from volfile rec=%d volefile=%d\n",
467 rec->File, volfile->efile);
472 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done)
475 return 1; /* no specification matches all */
477 if (stream->stream == rec->Stream) {
481 return match_stream(bsr, stream->next, rec, 1);
486 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done)
489 return 1; /* no specification matches all */
491 if (sesstime->sesstime == rec->VolSessionTime) {
494 if (rec->VolSessionTime > sesstime->sesstime) {
495 sesstime->done = true;
497 if (sesstime->next) {
498 return match_sesstime(bsr, sesstime->next, rec, sesstime->done && done);
500 if (sesstime->done && done) {
502 bsr->root->reposition = true;
503 Dmsg0(100, "bsr done from sesstime\n");
508 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec)
511 return 1; /* no specification matches all */
513 if (sessid->sessid <= rec->VolSessionId && sessid->sessid2 >= rec->VolSessionId) {
517 return match_sessid(bsr, sessid->next, rec);
523 * When reading the Volume, the Volume Findex (rec->FileIndex) always
524 * are found in sequential order. Thus we can make optimizations.
526 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done)
529 return 1; /* no specification matches all */
531 if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
534 if (rec->FileIndex > findex->findex2) {
538 return match_findex(bsr, findex->next, rec, findex->done && done);
540 if (findex->done && done) {
542 bsr->root->reposition = true;
543 Dmsg1(100, "bsr done from findex %d\n", rec->FileIndex);