]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/match_bsr.c
ece77a6bd24165fe62b2dfb1e32cca2006ce8439
[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 1;
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 poistioning
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, "use_pos=%d repos=%d\n", root_bsr->use_positioning,
161         root_bsr->reposition);
162       return NULL;
163    }
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  * Match all the components of current record
236  *   returns  1 on match
237  *   returns  0 no match
238  *   returns -1 no additional matches possible
239  */
240 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, 
241                      SESSION_LABEL *sessrec, bool done)
242 {
243    if (bsr->done) {
244       goto no_match;
245    }
246    if (bsr->count && bsr->count <= bsr->found) {
247       bsr->done = true;
248       bsr->root->reposition = true;
249       Dmsg0(100, "bsr done from count\n");
250       goto no_match;
251    }
252    if (!match_volume(bsr, bsr->volume, volrec, 1)) {
253       goto no_match;
254    }
255    if (!match_volfile(bsr, bsr->volfile, rec, 1)) {
256       goto no_match;
257    }
258    if (!match_sesstime(bsr, bsr->sesstime, rec, 1)) {
259       goto no_match;
260    }
261
262    /* NOTE!! This test MUST come after the sesstime test */
263    if (!match_sessid(bsr, bsr->sessid, rec)) {
264       goto no_match;
265    }
266
267    /* NOTE!! This test MUST come after sesstime and sessid tests */
268    if (!match_findex(bsr, bsr->FileIndex, rec, 1)) {
269       goto no_match;
270    }
271    if (!match_jobid(bsr, bsr->JobId, sessrec, 1)) {
272       goto no_match;
273    }
274    if (!match_job(bsr, bsr->job, sessrec, 1)) {
275       goto no_match;
276    }
277    if (!match_client(bsr, bsr->client, sessrec, 1)) {
278       goto no_match;
279    }
280    if (!match_job_type(bsr, bsr->JobType, sessrec, 1)) {
281       goto no_match;
282    }
283    if (!match_job_level(bsr, bsr->JobLevel, sessrec, 1)) {
284       goto no_match;
285    }
286    if (!match_stream(bsr, bsr->stream, rec, 1)) {
287       goto no_match;
288    }
289    bsr->found++;
290    return 1;
291
292 no_match:
293    if (bsr->next) {
294       return match_all(bsr->next, rec, volrec, sessrec, bsr->done && done);
295    }
296    if (bsr->done && done) {
297       return -1;
298    }
299    return 0;
300 }
301
302 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done) 
303 {
304    if (!volume) {
305       return 0;                       /* Volume must match */
306    }
307    if (strcmp(volume->VolumeName, volrec->VolName) == 0) {
308       return 1;
309    }
310    if (volume->next) {
311       return match_volume(bsr, volume->next, volrec, 1);
312    }
313    return 0;
314 }
315
316 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done)
317 {
318    if (!client) {
319       return 1;                       /* no specification matches all */
320    }
321    if (fnmatch(client->ClientName, sessrec->ClientName, 0) == 0) {
322       return 1;
323    }
324    if (client->next) {
325       return match_client(bsr, client->next, sessrec, 1);
326    }
327    return 0;
328 }
329
330 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done)
331 {
332    if (!job) {
333       return 1;                       /* no specification matches all */
334    }
335    if (fnmatch(job->Job, sessrec->Job, 0) == 0) {
336       return 1;
337    }
338    if (job->next) {
339       return match_job(bsr, job->next, sessrec, 1);
340    }
341    return 0;
342 }
343
344 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done)
345 {
346    if (!job_type) {
347       return 1;                       /* no specification matches all */
348    }
349    if (job_type->JobType == sessrec->JobType) {
350       return 1;
351    }
352    if (job_type->next) {
353       return match_job_type(bsr, job_type->next, sessrec, 1);
354    }
355    return 0;
356 }
357
358 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done)
359 {
360    if (!job_level) {
361       return 1;                       /* no specification matches all */
362    }
363    if (job_level->JobLevel == sessrec->JobLevel) {
364       return 1;
365    }
366    if (job_level->next) {
367       return match_job_level(bsr, job_level->next, sessrec, 1);
368    }
369    return 0;
370 }
371
372 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done)
373 {
374    if (!jobid) {
375       return 1;                       /* no specification matches all */
376    }
377    if (jobid->JobId <= sessrec->JobId && jobid->JobId2 >= sessrec->JobId) {
378       return 1;
379    }
380    if (jobid->next) {
381       return match_jobid(bsr, jobid->next, sessrec, 1);
382    }
383    return 0;
384 }
385
386 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done)
387 {
388    if (!volfile) {
389       return 1;                       /* no specification matches all */
390    }
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 */
394    }
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) {
398       return 1;
399    }
400    /* Once we get past last efile, we are done */
401    if (rec->File > volfile->efile) {
402       volfile->done = true;              /* set local done */
403    }
404    if (volfile->next) {
405       return match_volfile(bsr, volfile->next, rec, volfile->done && done);
406    }
407
408    /* If we are done and all prior matches are done, this bsr is finished */
409    if (volfile->done && done) {
410       bsr->done = true;
411       bsr->root->reposition = true;
412       Dmsg0(100, "bsr done from volfile\n");
413    }
414    return 0;
415 }
416
417 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done)
418 {
419    if (!stream) {
420       return 1;                       /* no specification matches all */
421    }
422    if (stream->stream == rec->Stream) {
423       return 1;
424    }
425    if (stream->next) {
426       return match_stream(bsr, stream->next, rec, 1);
427    }
428    return 0;
429 }
430
431 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done)
432 {
433    if (!sesstime) {
434       return 1;                       /* no specification matches all */
435    }
436    if (sesstime->sesstime == rec->VolSessionTime) {
437       return 1;
438    }
439    if (rec->VolSessionTime > sesstime->sesstime) {
440       sesstime->done = true;
441    }
442    if (sesstime->next) {
443       return match_sesstime(bsr, sesstime->next, rec, sesstime->done && done);
444    }
445    if (sesstime->done && done) {
446       bsr->done = true;
447       bsr->root->reposition = true;
448       Dmsg0(100, "bsr done from sesstime\n");
449    }
450    return 0;
451 }
452
453 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec)
454 {
455    if (!sessid) {
456       return 1;                       /* no specification matches all */
457    }
458    if (sessid->sessid <= rec->VolSessionId && sessid->sessid2 >= rec->VolSessionId) {
459       return 1;
460    }
461    if (sessid->next) {
462       return match_sessid(bsr, sessid->next, rec);
463    }
464    return 0;
465 }
466
467 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done)
468 {
469    if (!findex) {
470       return 1;                       /* no specification matches all */
471    }
472    if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
473       return 1;
474    }
475    if (rec->FileIndex > findex->findex2) {
476       findex->done = true;
477    }
478    if (findex->next) {
479       return match_findex(bsr, findex->next, rec, findex->done && done);
480    }
481    if (findex->done && done) {
482       bsr->done = true;
483       bsr->root->reposition = true;
484       Dmsg1(100, "bsr done from findex %d\n", rec->FileIndex);
485    }
486    return 0;
487 }