]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/match_bsr.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / stored / match_bsr.c
1 /*     
2  *   Match Bootstrap Records (used for restores) against
3  *     Volume Records
4  *  
5  *     Kern Sibbald, June MMII
6  *
7  *   Version $Id$
8  */
9
10 /*
11    Copyright (C) 2000-2003 Kern Sibbald and John Walker
12
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.
17
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.
22
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,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "stored.h"
32 #include <fnmatch.h>
33
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);
50
51
52 /*********************************************************************
53  *
54  *  If possible, position the archive device (tape) to read the
55  *  next block.
56  */
57 void position_bsr_block(BSR *bsr, DEV_BLOCK *block)
58 {
59    /* To be implemented */
60 }
61
62 /*********************************************************************
63  *
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.
68  *
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).
72  *
73  */
74 int match_bsr_block(BSR *bsr, DEV_BLOCK *block)
75 {
76    if (!bsr || !bsr->use_fast_rejection || (block->BlockVer < 2)) {
77       return 1;                       /* cannot fast reject */
78    }
79
80    for ( ; bsr; bsr=bsr->next) {
81    if (!match_block_sesstime(bsr, bsr->sesstime, block)) {
82          continue;
83       }
84       if (!match_block_sessid(bsr, bsr->sessid, block)) {
85          continue;
86    }
87       return 1;
88    }
89    return 0;
90 }
91
92 static int match_block_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_BLOCK *block)
93 {
94    if (!sesstime) {
95       return 1;                       /* no specification matches all */
96    }
97    if (sesstime->sesstime == block->VolSessionTime) {
98       return 1;
99    }
100    if (sesstime->next) {
101       return match_block_sesstime(bsr, sesstime->next, block);
102    }
103    return 0;
104 }
105
106 static int match_block_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_BLOCK *block)
107 {
108    if (!sessid) {
109       return 1;                       /* no specification matches all */
110    }
111    if (sessid->sessid <= block->VolSessionId && sessid->sessid2 >= block->VolSessionId) {
112       return 1;
113    }
114    if (sessid->next) {
115       return match_block_sessid(bsr, sessid->next, block);
116    }
117    return 0;
118 }
119
120
121 /*********************************************************************
122  *
123  *      Match Bootstrap records
124  *        returns  1 on match
125  *        returns  0 no match and reposition is set if we should
126  *                      reposition the tape
127  *       returns -1 no additional matches possible
128  */
129 int match_bsr(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec)
130 {
131    int stat;
132
133    /*
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.
137     */
138    if (bsr) {
139       bsr->reposition = false;
140       stat = match_all(bsr, rec, volrec, sessrec, true);
141       /* 
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
145        */
146       if (stat != 0 || !bsr->use_positioning) {
147          bsr->reposition = false;
148       }
149    } else {
150       stat = 1;                       /* no bsr => match all */
151    }
152    return stat;
153 }
154
155 /*
156  * Find the next bsr that applies to the current tape.
157  *   It is the one with the smallest VolFile position.
158  */
159 BSR *find_next_bsr(BSR *root_bsr, DEVICE *dev)
160 {
161    BSR *bsr;
162    BSR *found_bsr = NULL;
163
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);
167       return NULL;
168    }
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)) {
173          continue;
174       }
175       if (found_bsr == NULL) {
176          found_bsr = bsr;
177       } else {
178          found_bsr = find_smallest_volfile(found_bsr, bsr);
179       }
180    }
181    /*
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.
185     */
186    if (found_bsr == NULL) {
187       root_bsr->mount_next_volume = true;
188    }
189    return found_bsr;
190 }
191
192 static BSR *find_smallest_volfile(BSR *found_bsr, BSR *bsr)
193 {
194    BSR *return_bsr = found_bsr;
195    BSR_VOLFILE *vf;
196    BSR_VOLBLOCK *vb;
197    uint32_t found_bsr_sfile, bsr_sfile;
198    uint32_t found_bsr_sblock, bsr_sblock;
199
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;
205       }
206    }
207    vf = bsr->volfile;
208    bsr_sfile = vf->sfile;
209    while ( (vf=vf->next) ) {
210       if (vf->sfile < bsr_sfile) {
211          bsr_sfile = vf->sfile;
212       }
213    }
214    if (found_bsr_sfile > bsr_sfile) {
215       return_bsr = bsr;
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;
223          }
224       }
225       vb = bsr->volblock;
226       bsr_sblock = vb->sblock;
227       while ( (vb=vb->next) ) {
228          if (vb->sblock < bsr_sblock) {
229             bsr_sblock = vb->sblock;
230          }
231       }
232       if (found_bsr_sblock > bsr_sblock) {
233          return_bsr = bsr;
234       }
235    }
236
237    return return_bsr;
238 }
239
240 /* 
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.
245  * 
246  * Returns: true if we should reposition
247  *        : false otherwise.
248  */
249 bool match_set_eof(BSR *bsr, DEV_RECORD *rec)
250 {
251    BSR *rbsr = rec->bsr;
252    Dmsg1(100, "match_set %d\n", rbsr != NULL);
253    if (!rbsr) {
254       return false;
255    }
256    rec->bsr = NULL;
257    rbsr->found++;
258    if (rbsr->count && rbsr->found >= rbsr->count) {
259       rbsr->done = true;
260       rbsr->root->reposition = true;
261       Dmsg2(100, "match_set_eof reposition count=%d found=%d\n",
262          rbsr->count, rbsr->found);
263       return true;
264    }
265    return false;
266 }
267
268 /* 
269  * Match all the components of current record
270  *   returns  1 on match
271  *   returns  0 no match
272  *   returns -1 no additional matches possible
273  */
274 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, 
275                      SESSION_LABEL *sessrec, bool done)
276 {
277    if (bsr->done) {
278       goto no_match;
279    }
280    if (!match_volume(bsr, bsr->volume, volrec, 1)) {
281       goto no_match;
282    }
283    if (!match_volfile(bsr, bsr->volfile, rec, 1)) {
284       Dmsg2(100, "Fail on file. bsr=%d rec=%d\n", bsr->volfile->efile,
285          rec->File);
286       goto no_match;
287    }
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);
291       goto no_match;
292    }
293
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);
298       goto no_match;
299    }
300
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);
305       goto no_match;
306    }
307    /*
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.
314     */
315    if (bsr->count && bsr->FileIndex) {
316       rec->bsr = bsr;
317       return 1;                       /* this is a complete match */
318    }
319
320    /*
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.
324     */
325    if (!match_jobid(bsr, bsr->JobId, sessrec, 1)) {
326       goto no_match;
327        
328    }
329    if (!match_job(bsr, bsr->job, sessrec, 1)) {
330       goto no_match;
331    }
332    if (!match_client(bsr, bsr->client, sessrec, 1)) {
333       goto no_match;
334    }
335    if (!match_job_type(bsr, bsr->JobType, sessrec, 1)) {
336       goto no_match;
337    }
338    if (!match_job_level(bsr, bsr->JobLevel, sessrec, 1)) {
339       goto no_match;
340    }
341    if (!match_stream(bsr, bsr->stream, rec, 1)) {
342       goto no_match;
343    }
344    return 1;
345
346 no_match:
347    if (bsr->next) {
348       return match_all(bsr->next, rec, volrec, sessrec, bsr->done && done);
349    }
350    if (bsr->done && done) {
351       return -1;
352    }
353    return 0;
354 }
355
356 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done) 
357 {
358    if (!volume) {
359       return 0;                       /* Volume must match */
360    }
361    if (strcmp(volume->VolumeName, volrec->VolName) == 0) {
362       return 1;
363    }
364    if (volume->next) {
365       return match_volume(bsr, volume->next, volrec, 1);
366    }
367    return 0;
368 }
369
370 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done)
371 {
372    if (!client) {
373       return 1;                       /* no specification matches all */
374    }
375    if (fnmatch(client->ClientName, sessrec->ClientName, 0) == 0) {
376       return 1;
377    }
378    if (client->next) {
379       return match_client(bsr, client->next, sessrec, 1);
380    }
381    return 0;
382 }
383
384 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done)
385 {
386    if (!job) {
387       return 1;                       /* no specification matches all */
388    }
389    if (fnmatch(job->Job, sessrec->Job, 0) == 0) {
390       return 1;
391    }
392    if (job->next) {
393       return match_job(bsr, job->next, sessrec, 1);
394    }
395    return 0;
396 }
397
398 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done)
399 {
400    if (!job_type) {
401       return 1;                       /* no specification matches all */
402    }
403    if (job_type->JobType == sessrec->JobType) {
404       return 1;
405    }
406    if (job_type->next) {
407       return match_job_type(bsr, job_type->next, sessrec, 1);
408    }
409    return 0;
410 }
411
412 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done)
413 {
414    if (!job_level) {
415       return 1;                       /* no specification matches all */
416    }
417    if (job_level->JobLevel == sessrec->JobLevel) {
418       return 1;
419    }
420    if (job_level->next) {
421       return match_job_level(bsr, job_level->next, sessrec, 1);
422    }
423    return 0;
424 }
425
426 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done)
427 {
428    if (!jobid) {
429       return 1;                       /* no specification matches all */
430    }
431    if (jobid->JobId <= sessrec->JobId && jobid->JobId2 >= sessrec->JobId) {
432       return 1;
433    }
434    if (jobid->next) {
435       return match_jobid(bsr, jobid->next, sessrec, 1);
436    }
437    return 0;
438 }
439
440 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done)
441 {
442    if (!volfile) {
443       return 1;                       /* no specification matches all */
444    }
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 */
448    }
449 // Dmsg3(000, "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) {
452       return 1;
453    }
454    /* Once we get past last efile, we are done */
455    if (rec->File > volfile->efile) {
456       volfile->done = true;              /* set local done */
457    }
458    if (volfile->next) {
459       return match_volfile(bsr, volfile->next, rec, volfile->done && done);
460    }
461
462    /* If we are done and all prior matches are done, this bsr is finished */
463    if (volfile->done && done) {
464       bsr->done = true;
465       bsr->root->reposition = true;
466       Dmsg2(100, "bsr done from volfile rec=%d volefile=%d\n", 
467          rec->File, volfile->efile);
468    }
469    return 0;
470 }
471
472 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done)
473 {
474    if (!stream) {
475       return 1;                       /* no specification matches all */
476    }
477    if (stream->stream == rec->Stream) {
478       return 1;
479    }
480    if (stream->next) {
481       return match_stream(bsr, stream->next, rec, 1);
482    }
483    return 0;
484 }
485
486 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done)
487 {
488    if (!sesstime) {
489       return 1;                       /* no specification matches all */
490    }
491    if (sesstime->sesstime == rec->VolSessionTime) {
492       return 1;
493    }
494    if (rec->VolSessionTime > sesstime->sesstime) {
495       sesstime->done = true;
496    }
497    if (sesstime->next) {
498       return match_sesstime(bsr, sesstime->next, rec, sesstime->done && done);
499    }
500    if (sesstime->done && done) {
501       bsr->done = true;
502       bsr->root->reposition = true;
503       Dmsg0(100, "bsr done from sesstime\n");
504    }
505    return 0;
506 }
507
508 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec)
509 {
510    if (!sessid) {
511       return 1;                       /* no specification matches all */
512    }
513    if (sessid->sessid <= rec->VolSessionId && sessid->sessid2 >= rec->VolSessionId) {
514       return 1;
515    }
516    if (sessid->next) {
517       return match_sessid(bsr, sessid->next, rec);
518    }
519    return 0;
520 }
521
522 /*
523  * When reading the Volume, the Volume Findex (rec->FileIndex) always
524  *   are found in sequential order. Thus we can make optimizations.
525  */
526 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done)
527 {
528    if (!findex) {
529       return 1;                       /* no specification matches all */
530    }
531    if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
532       return 1;
533    }
534    if (rec->FileIndex > findex->findex2) {
535       findex->done = true;
536    }
537    if (findex->next) {
538       return match_findex(bsr, findex->next, rec, findex->done && done);
539    }
540    if (findex->done && done) {
541       bsr->done = true;
542       bsr->root->reposition = true;
543       Dmsg1(100, "bsr done from findex %d\n", rec->FileIndex);
544    }
545    return 0;
546 }