]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/match_bsr.c
1aaef0e7552ac50a5524d343bacae2aedf05877f
[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    if (!match_block_sesstime(bsr, bsr->sesstime, block)) {
81       return 0;
82    }
83    return match_block_sessid(bsr, bsr->sessid, block);
84 }
85
86 static int match_block_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_BLOCK *block)
87 {
88    if (!sesstime) {
89       return 1;                       /* no specification matches all */
90    }
91    if (sesstime->sesstime == block->VolSessionTime) {
92       return 1;
93    }
94    if (sesstime->next) {
95       return match_block_sesstime(bsr, sesstime->next, block);
96    }
97    return 0;
98 }
99
100 static int match_block_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_BLOCK *block)
101 {
102    if (!sessid) {
103       return 1;                       /* no specification matches all */
104    }
105    if (sessid->sessid <= block->VolSessionId && sessid->sessid2 >= block->VolSessionId) {
106       return 1;
107    }
108    if (sessid->next) {
109       return match_block_sessid(bsr, sessid->next, block);
110    }
111    return 0;
112 }
113
114
115 /*********************************************************************
116  *
117  *      Match Bootstrap records
118  *        returns  1 on match
119  *        returns  0 no match and reposition is set if we should
120  *                      reposition the tape
121  *       returns -1 no additional matches possible
122  */
123 int match_bsr(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec)
124 {
125    int stat;
126
127    /*
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.
131     */
132    if (bsr) {
133       bsr->reposition = false;
134       stat = match_all(bsr, rec, volrec, sessrec, true);
135       /* 
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
139        */
140       if (stat != 0 || !bsr->use_positioning) {
141          bsr->reposition = false;
142       }
143    } else {
144       stat = 1;                       /* no bsr => match all */
145    }
146    return stat;
147 }
148
149 /*
150  * Find the next bsr that applies to the current tape.
151  *   It is the one with the smallest VolFile position.
152  */
153 BSR *find_next_bsr(BSR *root_bsr, DEVICE *dev)
154 {
155    BSR *bsr;
156    BSR *found_bsr = NULL;
157
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);
161       return NULL;
162    }
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)) {
167          continue;
168       }
169       if (found_bsr == NULL) {
170          found_bsr = bsr;
171       } else {
172          found_bsr = find_smallest_volfile(found_bsr, bsr);
173       }
174    }
175    /*
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.
179     */
180    if (found_bsr == NULL) {
181       root_bsr->mount_next_volume = true;
182    }
183    return found_bsr;
184 }
185
186 static BSR *find_smallest_volfile(BSR *found_bsr, BSR *bsr)
187 {
188    BSR *return_bsr = found_bsr;
189    BSR_VOLFILE *vf;
190    BSR_VOLBLOCK *vb;
191    uint32_t found_bsr_sfile, bsr_sfile;
192    uint32_t found_bsr_sblock, bsr_sblock;
193
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;
199       }
200    }
201    vf = bsr->volfile;
202    bsr_sfile = vf->sfile;
203    while ( (vf=vf->next) ) {
204       if (vf->sfile < bsr_sfile) {
205          bsr_sfile = vf->sfile;
206       }
207    }
208    if (found_bsr_sfile > bsr_sfile) {
209       return_bsr = bsr;
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;
217          }
218       }
219       vb = bsr->volblock;
220       bsr_sblock = vb->sblock;
221       while ( (vb=vb->next) ) {
222          if (vb->sblock < bsr_sblock) {
223             bsr_sblock = vb->sblock;
224          }
225       }
226       if (found_bsr_sblock > bsr_sblock) {
227          return_bsr = bsr;
228       }
229    }
230
231    return return_bsr;
232 }
233
234 /* 
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.
239  * 
240  * Returns: true if we should reposition
241  *        : false otherwise.
242  */
243 bool match_set_eof(BSR *bsr, DEV_RECORD *rec)
244 {
245    BSR *rbsr = rec->bsr;
246    Dmsg1(100, "match_set %d\n", rbsr != NULL);
247    if (!rbsr) {
248       return false;
249    }
250    rec->bsr = NULL;
251    rbsr->found++;
252    if (rbsr->count && rbsr->found >= rbsr->count) {
253       rbsr->done = true;
254       rbsr->root->reposition = true;
255       Dmsg2(100, "match_set_eof reposition count=%d found=%d\n",
256          rbsr->count, rbsr->found);
257       return true;
258    }
259    return false;
260 }
261
262 /* 
263  * Match all the components of current record
264  *   returns  1 on match
265  *   returns  0 no match
266  *   returns -1 no additional matches possible
267  */
268 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, 
269                      SESSION_LABEL *sessrec, bool done)
270 {
271    if (bsr->done) {
272       goto no_match;
273    }
274    if (!match_volume(bsr, bsr->volume, volrec, 1)) {
275       goto no_match;
276    }
277    if (!match_volfile(bsr, bsr->volfile, rec, 1)) {
278       Dmsg2(100, "Fail on file. bsr=%d rec=%d\n", bsr->volfile->efile,
279          rec->File);
280       goto no_match;
281    }
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);
285       goto no_match;
286    }
287
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);
292       goto no_match;
293    }
294
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);
299       goto no_match;
300    }
301    /*
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.
308     */
309    if (bsr->count && bsr->FileIndex) {
310       rec->bsr = bsr;
311       return 1;                       /* this is a complete match */
312    }
313
314    /*
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.
318     */
319    if (!match_jobid(bsr, bsr->JobId, sessrec, 1)) {
320       goto no_match;
321        
322    }
323    if (!match_job(bsr, bsr->job, sessrec, 1)) {
324       goto no_match;
325    }
326    if (!match_client(bsr, bsr->client, sessrec, 1)) {
327       goto no_match;
328    }
329    if (!match_job_type(bsr, bsr->JobType, sessrec, 1)) {
330       goto no_match;
331    }
332    if (!match_job_level(bsr, bsr->JobLevel, sessrec, 1)) {
333       goto no_match;
334    }
335    if (!match_stream(bsr, bsr->stream, rec, 1)) {
336       goto no_match;
337    }
338    return 1;
339
340 no_match:
341    if (bsr->next) {
342       return match_all(bsr->next, rec, volrec, sessrec, bsr->done && done);
343    }
344    if (bsr->done && done) {
345       return -1;
346    }
347    return 0;
348 }
349
350 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done) 
351 {
352    if (!volume) {
353       return 0;                       /* Volume must match */
354    }
355    if (strcmp(volume->VolumeName, volrec->VolName) == 0) {
356       return 1;
357    }
358    if (volume->next) {
359       return match_volume(bsr, volume->next, volrec, 1);
360    }
361    return 0;
362 }
363
364 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done)
365 {
366    if (!client) {
367       return 1;                       /* no specification matches all */
368    }
369    if (fnmatch(client->ClientName, sessrec->ClientName, 0) == 0) {
370       return 1;
371    }
372    if (client->next) {
373       return match_client(bsr, client->next, sessrec, 1);
374    }
375    return 0;
376 }
377
378 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done)
379 {
380    if (!job) {
381       return 1;                       /* no specification matches all */
382    }
383    if (fnmatch(job->Job, sessrec->Job, 0) == 0) {
384       return 1;
385    }
386    if (job->next) {
387       return match_job(bsr, job->next, sessrec, 1);
388    }
389    return 0;
390 }
391
392 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done)
393 {
394    if (!job_type) {
395       return 1;                       /* no specification matches all */
396    }
397    if (job_type->JobType == sessrec->JobType) {
398       return 1;
399    }
400    if (job_type->next) {
401       return match_job_type(bsr, job_type->next, sessrec, 1);
402    }
403    return 0;
404 }
405
406 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done)
407 {
408    if (!job_level) {
409       return 1;                       /* no specification matches all */
410    }
411    if (job_level->JobLevel == sessrec->JobLevel) {
412       return 1;
413    }
414    if (job_level->next) {
415       return match_job_level(bsr, job_level->next, sessrec, 1);
416    }
417    return 0;
418 }
419
420 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done)
421 {
422    if (!jobid) {
423       return 1;                       /* no specification matches all */
424    }
425    if (jobid->JobId <= sessrec->JobId && jobid->JobId2 >= sessrec->JobId) {
426       return 1;
427    }
428    if (jobid->next) {
429       return match_jobid(bsr, jobid->next, sessrec, 1);
430    }
431    return 0;
432 }
433
434 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done)
435 {
436    if (!volfile) {
437       return 1;                       /* no specification matches all */
438    }
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 */
442    }
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) {
446       return 1;
447    }
448    /* Once we get past last efile, we are done */
449    if (rec->File > volfile->efile) {
450       volfile->done = true;              /* set local done */
451    }
452    if (volfile->next) {
453       return match_volfile(bsr, volfile->next, rec, volfile->done && done);
454    }
455
456    /* If we are done and all prior matches are done, this bsr is finished */
457    if (volfile->done && done) {
458       bsr->done = true;
459       bsr->root->reposition = true;
460       Dmsg2(100, "bsr done from volfile rec=%d volefile=%d\n", 
461          rec->File, volfile->efile);
462    }
463    return 0;
464 }
465
466 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done)
467 {
468    if (!stream) {
469       return 1;                       /* no specification matches all */
470    }
471    if (stream->stream == rec->Stream) {
472       return 1;
473    }
474    if (stream->next) {
475       return match_stream(bsr, stream->next, rec, 1);
476    }
477    return 0;
478 }
479
480 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done)
481 {
482    if (!sesstime) {
483       return 1;                       /* no specification matches all */
484    }
485    if (sesstime->sesstime == rec->VolSessionTime) {
486       return 1;
487    }
488    if (rec->VolSessionTime > sesstime->sesstime) {
489       sesstime->done = true;
490    }
491    if (sesstime->next) {
492       return match_sesstime(bsr, sesstime->next, rec, sesstime->done && done);
493    }
494    if (sesstime->done && done) {
495       bsr->done = true;
496       bsr->root->reposition = true;
497       Dmsg0(100, "bsr done from sesstime\n");
498    }
499    return 0;
500 }
501
502 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec)
503 {
504    if (!sessid) {
505       return 1;                       /* no specification matches all */
506    }
507    if (sessid->sessid <= rec->VolSessionId && sessid->sessid2 >= rec->VolSessionId) {
508       return 1;
509    }
510    if (sessid->next) {
511       return match_sessid(bsr, sessid->next, rec);
512    }
513    return 0;
514 }
515
516 /*
517  * When reading the Volume, the Volume Findex (rec->FileIndex) always
518  *   are found in sequential order. Thus we can make optimizations.
519  */
520 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done)
521 {
522    if (!findex) {
523       return 1;                       /* no specification matches all */
524    }
525    if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
526       return 1;
527    }
528    if (rec->FileIndex > findex->findex2) {
529       findex->done = true;
530    }
531    if (findex->next) {
532       return match_findex(bsr, findex->next, rec, findex->done && done);
533    }
534    if (findex->done && done) {
535       bsr->done = true;
536       bsr->root->reposition = true;
537       Dmsg1(100, "bsr done from findex %d\n", rec->FileIndex);
538    }
539    return 0;
540 }