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 positioning
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(100, "No nxt_bsr use_pos=%d repos=%d\n", root_bsr->use_positioning, root_bsr->reposition);
163 Dmsg2(100, "use_pos=%d repos=%d\n", root_bsr->use_positioning, 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 * Called to tell the matcher that the end of
236 * the current file has been reached.
237 * The bsr argument is not used, but is included
238 * for consistency with the other match calls.
240 * Returns: true if we should reposition
243 bool match_set_eof(BSR *bsr, DEV_RECORD *rec)
245 BSR *rbsr = rec->bsr;
246 Dmsg1(100, "match_set %d\n", rbsr != NULL);
252 if (rbsr->count && rbsr->found >= rbsr->count) {
254 rbsr->root->reposition = true;
255 Dmsg2(100, "match_set_eof reposition count=%d found=%d\n",
256 rbsr->count, rbsr->found);
263 * Match all the components of current record
266 * returns -1 no additional matches possible
268 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec,
269 SESSION_LABEL *sessrec, bool done)
274 if (!match_volume(bsr, bsr->volume, volrec, 1)) {
277 if (!match_volfile(bsr, bsr->volfile, rec, 1)) {
278 Dmsg2(100, "Fail on file. bsr=%d rec=%d\n", bsr->volfile->efile,
282 if (!match_sesstime(bsr, bsr->sesstime, rec, 1)) {
283 Dmsg2(100, "Fail on sesstime. bsr=%d rec=%d\n",
284 bsr->sesstime->sesstime, rec->VolSessionTime);
288 /* NOTE!! This test MUST come after the sesstime test */
289 if (!match_sessid(bsr, bsr->sessid, rec)) {
290 Dmsg2(100, "Fail on sessid. bsr=%d rec=%d\n",
291 bsr->sessid->sessid, rec->VolSessionId);
295 /* NOTE!! This test MUST come after sesstime and sessid tests */
296 if (!match_findex(bsr, bsr->FileIndex, rec, 1)) {
297 Dmsg2(100, "Fail on findex. bsr=%d rec=%d\n",
298 bsr->FileIndex->findex2, rec->FileIndex);
302 * If a count was specified and we have a FileIndex, assume
303 * it is a Bacula created bsr (or the equivalent). We
304 * then save the bsr where the match occurred so that
305 * after processing the record or records, we can update
306 * the found count. I.e. rec->bsr points to the bsr that
307 * satisfied the match.
309 if (bsr->count && bsr->FileIndex) {
311 return 1; /* this is a complete match */
315 * The selections below are not used by Bacula's
316 * restore command, and don't work because of
317 * the rec->bsr = bsr optimization above.
319 if (!match_jobid(bsr, bsr->JobId, sessrec, 1)) {
323 if (!match_job(bsr, bsr->job, sessrec, 1)) {
326 if (!match_client(bsr, bsr->client, sessrec, 1)) {
329 if (!match_job_type(bsr, bsr->JobType, sessrec, 1)) {
332 if (!match_job_level(bsr, bsr->JobLevel, sessrec, 1)) {
335 if (!match_stream(bsr, bsr->stream, rec, 1)) {
342 return match_all(bsr->next, rec, volrec, sessrec, bsr->done && done);
344 if (bsr->done && done) {
350 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done)
353 return 0; /* Volume must match */
355 if (strcmp(volume->VolumeName, volrec->VolName) == 0) {
359 return match_volume(bsr, volume->next, volrec, 1);
364 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done)
367 return 1; /* no specification matches all */
369 if (fnmatch(client->ClientName, sessrec->ClientName, 0) == 0) {
373 return match_client(bsr, client->next, sessrec, 1);
378 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done)
381 return 1; /* no specification matches all */
383 if (fnmatch(job->Job, sessrec->Job, 0) == 0) {
387 return match_job(bsr, job->next, sessrec, 1);
392 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done)
395 return 1; /* no specification matches all */
397 if (job_type->JobType == sessrec->JobType) {
400 if (job_type->next) {
401 return match_job_type(bsr, job_type->next, sessrec, 1);
406 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done)
409 return 1; /* no specification matches all */
411 if (job_level->JobLevel == sessrec->JobLevel) {
414 if (job_level->next) {
415 return match_job_level(bsr, job_level->next, sessrec, 1);
420 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done)
423 return 1; /* no specification matches all */
425 if (jobid->JobId <= sessrec->JobId && jobid->JobId2 >= sessrec->JobId) {
429 return match_jobid(bsr, jobid->next, sessrec, 1);
434 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done)
437 return 1; /* no specification matches all */
439 /* For the moment, these tests work only with tapes. */
440 if (!(rec->state & REC_ISTAPE)) {
441 return 1; /* All File records OK for this match */
443 // Dmsg3(000, "match_volfile: sfile=%d efile=%d recfile=%d\n",
444 // volfile->sfile, volfile->efile, rec->File);
445 if (volfile->sfile <= rec->File && volfile->efile >= rec->File) {
448 /* Once we get past last efile, we are done */
449 if (rec->File > volfile->efile) {
450 volfile->done = true; /* set local done */
453 return match_volfile(bsr, volfile->next, rec, volfile->done && done);
456 /* If we are done and all prior matches are done, this bsr is finished */
457 if (volfile->done && done) {
459 bsr->root->reposition = true;
460 Dmsg2(100, "bsr done from volfile rec=%d volefile=%d\n",
461 rec->File, volfile->efile);
466 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done)
469 return 1; /* no specification matches all */
471 if (stream->stream == rec->Stream) {
475 return match_stream(bsr, stream->next, rec, 1);
480 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done)
483 return 1; /* no specification matches all */
485 if (sesstime->sesstime == rec->VolSessionTime) {
488 if (rec->VolSessionTime > sesstime->sesstime) {
489 sesstime->done = true;
491 if (sesstime->next) {
492 return match_sesstime(bsr, sesstime->next, rec, sesstime->done && done);
494 if (sesstime->done && done) {
496 bsr->root->reposition = true;
497 Dmsg0(100, "bsr done from sesstime\n");
502 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec)
505 return 1; /* no specification matches all */
507 if (sessid->sessid <= rec->VolSessionId && sessid->sessid2 >= rec->VolSessionId) {
511 return match_sessid(bsr, sessid->next, rec);
517 * When reading the Volume, the Volume Findex (rec->FileIndex) always
518 * are found in sequential order. Thus we can make optimizations.
520 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done)
523 return 1; /* no specification matches all */
525 if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
528 if (rec->FileIndex > findex->findex2) {
532 return match_findex(bsr, findex->next, rec, findex->done && done);
534 if (findex->done && done) {
536 bsr->root->reposition = true;
537 Dmsg1(100, "bsr done from findex %d\n", rec->FileIndex);