]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/match_bsr.c
Modify read_record and friends to properly deal with migration
[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    Copyright (C) 2002-2006 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License
14    version 2 as amended with additional clauses defined in the
15    file LICENSE in the main source directory.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
20    the file LICENSE for additional details.
21
22  */
23
24
25 #include "bacula.h"
26 #include "stored.h"
27 #ifdef HAVE_FNMATCH
28 #include <fnmatch.h>
29 #else
30 #include "lib/fnmatch.h"
31 #endif
32
33 /* Forward references */
34 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done);
35 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done);
36 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec);
37 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done);
38 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done);
39 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done);
40 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done);
41 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done);
42 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done);
43 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done);
44 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done);
45 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec, bool done);
46 static int match_block_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_BLOCK *block);
47 static int match_block_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_BLOCK *block);
48 static BSR *find_smallest_volfile(BSR *fbsr, BSR *bsr);
49
50
51 /*********************************************************************
52  *
53  *  If possible, position the archive device (tape) to read the
54  *  next block.
55  */
56 void position_bsr_block(BSR *bsr, DEV_BLOCK *block)
57 {
58    /* To be implemented */
59 }
60
61 /*********************************************************************
62  *
63  *  Do fast block rejection based on bootstrap records.
64  *    use_fast_rejection will be set if we have VolSessionId and VolSessTime
65  *    in each record. When BlockVer is >= 2, we have those in the block header
66  *    so can do fast rejection.
67  *
68  *   returns:  1 if block may contain valid records
69  *             0 if block may be skipped (i.e. it contains no records of
70  *                  that can match the bsr).
71  *
72  */
73 int match_bsr_block(BSR *bsr, DEV_BLOCK *block)
74 {
75    if (!bsr || !bsr->use_fast_rejection || (block->BlockVer < 2)) {
76       return 1;                       /* cannot fast reject */
77    }
78
79    for ( ; bsr; bsr=bsr->next) {
80       if (!match_block_sesstime(bsr, bsr->sesstime, block)) {
81          continue;
82       }
83       if (!match_block_sessid(bsr, bsr->sessid, block)) {
84          continue;
85       }
86       return 1;
87    }
88    return 0;
89 }
90
91 static int match_block_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_BLOCK *block)
92 {
93    if (!sesstime) {
94       return 1;                       /* no specification matches all */
95    }
96    if (sesstime->sesstime == block->VolSessionTime) {
97       return 1;
98    }
99    if (sesstime->next) {
100       return match_block_sesstime(bsr, sesstime->next, block);
101    }
102    return 0;
103 }
104
105 static int match_block_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_BLOCK *block)
106 {
107    if (!sessid) {
108       return 1;                       /* no specification matches all */
109    }
110    if (sessid->sessid <= block->VolSessionId && sessid->sessid2 >= block->VolSessionId) {
111       return 1;
112    }
113    if (sessid->next) {
114       return match_block_sessid(bsr, sessid->next, block);
115    }
116    return 0;
117 }
118
119
120 /*********************************************************************
121  *
122  *      Match Bootstrap records
123  *        returns  1 on match
124  *        returns  0 no match and reposition is set if we should
125  *                      reposition the tape
126  *       returns -1 no additional matches possible
127  */
128 int match_bsr(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec)
129 {
130    int stat;
131
132    /*
133     * The bsr->reposition flag is set any time a bsr is done.
134     *   In this case, we can probably reposition the
135     *   tape to the next available bsr position.
136     */
137    if (bsr) {
138       bsr->reposition = false;
139       stat = match_all(bsr, rec, volrec, sessrec, true);
140       /*
141        * Note, bsr->reposition is set by match_all when
142        *  a bsr is done. We turn it off if a match was
143        *  found or if we cannot use positioning
144        */
145       if (stat != 0 || !bsr->use_positioning) {
146          bsr->reposition = false;
147       }
148    } else {
149       stat = 1;                       /* no bsr => match all */
150    }
151    return stat;
152 }
153
154 /*
155  * Find the next bsr that applies to the current tape.
156  *   It is the one with the smallest VolFile position.
157  */
158 BSR *find_next_bsr(BSR *root_bsr, DEVICE *dev)
159 {
160    BSR *bsr;
161    BSR *found_bsr = NULL;
162    bool no_file_seek = !dev->is_tape();
163 #ifdef FILE_SEEK
164    no_file_seek = false;
165 #endif
166
167    if (!root_bsr || !root_bsr->use_positioning ||
168        !root_bsr->reposition || no_file_seek) {
169       Dmsg2(300, "No nxt_bsr use_pos=%d repos=%d\n", root_bsr->use_positioning, root_bsr->reposition);
170       return NULL;
171    }
172    Dmsg2(300, "use_pos=%d repos=%d\n", root_bsr->use_positioning, root_bsr->reposition);
173    root_bsr->mount_next_volume = false;
174    for (bsr=root_bsr; bsr; bsr=bsr->next) {
175       if (bsr->done || !match_volume(bsr, bsr->volume, &dev->VolHdr, 1)) {
176          continue;
177       }
178       if (found_bsr == NULL) {
179          found_bsr = bsr;
180       } else {
181          found_bsr = find_smallest_volfile(found_bsr, bsr);
182       }
183    }
184    /*
185     * If we get to this point and found no bsr, it means
186     *  that any additional bsr's must apply to the next
187     *  tape, so set a flag.
188     */
189    if (found_bsr == NULL) {
190       root_bsr->mount_next_volume = true;
191    }
192    return found_bsr;
193 }
194
195 static BSR *find_smallest_volfile(BSR *found_bsr, BSR *bsr)
196 {
197    BSR *return_bsr = found_bsr;
198    BSR_VOLFILE *vf;
199    BSR_VOLBLOCK *vb;
200    uint32_t found_bsr_sfile, bsr_sfile;
201    uint32_t found_bsr_sblock, bsr_sblock;
202
203    vf = found_bsr->volfile;
204    found_bsr_sfile = vf->sfile;
205    while ( (vf=vf->next) ) {
206       if (vf->sfile < found_bsr_sfile) {
207          found_bsr_sfile = vf->sfile;
208       }
209    }
210    vf = bsr->volfile;
211    bsr_sfile = vf->sfile;
212    while ( (vf=vf->next) ) {
213       if (vf->sfile < bsr_sfile) {
214          bsr_sfile = vf->sfile;
215       }
216    }
217    if (found_bsr_sfile > bsr_sfile) {
218       return_bsr = bsr;
219    } else if (found_bsr_sfile == bsr_sfile) {
220       /* Must check block */
221       vb = found_bsr->volblock;
222       found_bsr_sblock = vb->sblock;
223       while ( (vb=vb->next) ) {
224          if (vb->sblock < found_bsr_sblock) {
225             found_bsr_sblock = vb->sblock;
226          }
227       }
228       vb = bsr->volblock;
229       bsr_sblock = vb->sblock;
230       while ( (vb=vb->next) ) {
231          if (vb->sblock < bsr_sblock) {
232             bsr_sblock = vb->sblock;
233          }
234       }
235       if (found_bsr_sblock > bsr_sblock) {
236          return_bsr = bsr;
237       }
238    }
239
240    return return_bsr;
241 }
242
243 /*
244  * Called after the signature record so that
245  *   we can see if the current bsr has been
246  *   fully processed (i.e. is done).
247  *  The bsr argument is not used, but is included
248  *    for consistency with the other match calls.
249  *
250  * Returns: true if we should reposition
251  *        : false otherwise.
252  */
253 bool is_this_bsr_done(BSR *bsr, DEV_RECORD *rec)
254 {
255    BSR *rbsr = rec->bsr;
256    Dmsg1(300, "match_set %d\n", rbsr != NULL);
257    if (!rbsr) {
258       return false;
259    }
260    rec->bsr = NULL;
261    rbsr->found++;
262    if (rbsr->count && rbsr->found >= rbsr->count) {
263       rbsr->done = true;
264       rbsr->root->reposition = true;
265       Dmsg2(500, "is_end_this_bsr set reposition=1 count=%d found=%d\n",
266          rbsr->count, rbsr->found);
267       return true;
268    }
269    return false;
270 }
271
272 /*
273  * Match all the components of current record
274  *   returns  1 on match
275  *   returns  0 no match
276  *   returns -1 no additional matches possible
277  */
278 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec,
279                      SESSION_LABEL *sessrec, bool done)
280 {
281    if (bsr->done) {
282       Dmsg0(300, "bsr->done set\n");
283       goto no_match;
284    }
285    if (!match_volume(bsr, bsr->volume, volrec, 1)) {
286       Dmsg2(300, "bsr vol=%s != rec vol=%s\n", bsr->volume, volrec);
287       goto no_match;
288    }
289    if (!match_volfile(bsr, bsr->volfile, rec, 1)) {
290       Dmsg2(300, "Fail on file. bsr=%d rec=%d\n", bsr->volfile->efile,
291          rec->File);
292       goto no_match;
293    }
294    if (!match_sesstime(bsr, bsr->sesstime, rec, 1)) {
295       Dmsg2(300, "Fail on sesstime. bsr=%d rec=%d\n",
296          bsr->sesstime->sesstime, rec->VolSessionTime);
297       goto no_match;
298    }
299
300    /* NOTE!! This test MUST come after the sesstime test */
301    if (!match_sessid(bsr, bsr->sessid, rec)) {
302       Dmsg2(300, "Fail on sessid. bsr=%d rec=%d\n",
303          bsr->sessid->sessid, rec->VolSessionId);
304       goto no_match;
305    }
306
307    /* NOTE!! This test MUST come after sesstime and sessid tests */
308    if (!match_findex(bsr, bsr->FileIndex, rec, 1)) {
309       Dmsg2(300, "Fail on findex. bsr=%d rec=%d\n",
310          bsr->FileIndex->findex2, rec->FileIndex);
311       goto no_match;
312    }
313    /*
314     * If a count was specified and we have a FileIndex, assume
315     *   it is a Bacula created bsr (or the equivalent). We
316     *   then save the bsr where the match occurred so that
317     *   after processing the record or records, we can update
318     *   the found count. I.e. rec->bsr points to the bsr that
319     *   satisfied the match.
320     */
321    if (bsr->count && bsr->FileIndex) {
322       rec->bsr = bsr;
323       return 1;                       /* this is a complete match */
324    }
325
326    /*
327     * The selections below are not used by Bacula's
328     *   restore command, and don't work because of
329     *   the rec->bsr = bsr optimization above.
330     */
331    if (!match_jobid(bsr, bsr->JobId, sessrec, 1)) {
332       Dmsg0(300, "fail on JobId\n");
333       goto no_match;
334
335    }
336    if (!match_job(bsr, bsr->job, sessrec, 1)) {
337       Dmsg0(300, "fail on Job\n");
338       goto no_match;
339    }
340    if (!match_client(bsr, bsr->client, sessrec, 1)) {
341       Dmsg0(300, "fail on Client\n");
342       goto no_match;
343    }
344    if (!match_job_type(bsr, bsr->JobType, sessrec, 1)) {
345       Dmsg0(300, "fail on Job type\n");
346       goto no_match;
347    }
348    if (!match_job_level(bsr, bsr->JobLevel, sessrec, 1)) {
349       Dmsg0(300, "fail on Job level\n");
350       goto no_match;
351    }
352    if (!match_stream(bsr, bsr->stream, rec, 1)) {
353       Dmsg0(300, "fail on stream\n");
354       goto no_match;
355    }
356    return 1;
357
358 no_match:
359    if (bsr->next) {
360       return match_all(bsr->next, rec, volrec, sessrec, bsr->done && done);
361    }
362    if (bsr->done && done) {
363       return -1;
364    }
365    return 0;
366 }
367
368 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done)
369 {
370    if (!volume) {
371       return 0;                       /* Volume must match */
372    }
373    if (strcmp(volume->VolumeName, volrec->VolumeName) == 0) {
374       return 1;
375    }
376    if (volume->next) {
377       return match_volume(bsr, volume->next, volrec, 1);
378    }
379    return 0;
380 }
381
382 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done)
383 {
384    if (!client) {
385       return 1;                       /* no specification matches all */
386    }
387    if (fnmatch(client->ClientName, sessrec->ClientName, 0) == 0) {
388       return 1;
389    }
390    if (client->next) {
391       return match_client(bsr, client->next, sessrec, 1);
392    }
393    return 0;
394 }
395
396 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done)
397 {
398    if (!job) {
399       return 1;                       /* no specification matches all */
400    }
401    if (fnmatch(job->Job, sessrec->Job, 0) == 0) {
402       return 1;
403    }
404    if (job->next) {
405       return match_job(bsr, job->next, sessrec, 1);
406    }
407    return 0;
408 }
409
410 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done)
411 {
412    if (!job_type) {
413       return 1;                       /* no specification matches all */
414    }
415    if (job_type->JobType == sessrec->JobType) {
416       return 1;
417    }
418    if (job_type->next) {
419       return match_job_type(bsr, job_type->next, sessrec, 1);
420    }
421    return 0;
422 }
423
424 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done)
425 {
426    if (!job_level) {
427       return 1;                       /* no specification matches all */
428    }
429    if (job_level->JobLevel == sessrec->JobLevel) {
430       return 1;
431    }
432    if (job_level->next) {
433       return match_job_level(bsr, job_level->next, sessrec, 1);
434    }
435    return 0;
436 }
437
438 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done)
439 {
440    if (!jobid) {
441       return 1;                       /* no specification matches all */
442    }
443    if (jobid->JobId <= sessrec->JobId && jobid->JobId2 >= sessrec->JobId) {
444       return 1;
445    }
446    if (jobid->next) {
447       return match_jobid(bsr, jobid->next, sessrec, 1);
448    }
449    return 0;
450 }
451
452 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done)
453 {
454    if (!volfile) {
455       return 1;                       /* no specification matches all */
456    }
457    /* For the moment, these tests work only with tapes. */
458    if (!(rec->state & REC_ISTAPE)) {
459       return 1;                       /* All File records OK for this match */
460    }
461 // Dmsg3(300, "match_volfile: sfile=%d efile=%d recfile=%d\n",
462 //             volfile->sfile, volfile->efile, rec->File);
463    if (volfile->sfile <= rec->File && volfile->efile >= rec->File) {
464       return 1;
465    }
466    /* Once we get past last efile, we are done */
467    if (rec->File > volfile->efile) {
468       volfile->done = true;              /* set local done */
469    }
470    if (volfile->next) {
471       return match_volfile(bsr, volfile->next, rec, volfile->done && done);
472    }
473
474    /* If we are done and all prior matches are done, this bsr is finished */
475    if (volfile->done && done) {
476       bsr->done = true;
477       bsr->root->reposition = true;
478       Dmsg2(300, "bsr done from volfile rec=%d volefile=%d\n",
479          rec->File, volfile->efile);
480    }
481    return 0;
482 }
483
484 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done)
485 {
486    if (!stream) {
487       return 1;                       /* no specification matches all */
488    }
489    if (stream->stream == rec->Stream) {
490       return 1;
491    }
492    if (stream->next) {
493       return match_stream(bsr, stream->next, rec, 1);
494    }
495    return 0;
496 }
497
498 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done)
499 {
500    if (!sesstime) {
501       return 1;                       /* no specification matches all */
502    }
503    if (sesstime->sesstime == rec->VolSessionTime) {
504       return 1;
505    }
506    if (rec->VolSessionTime > sesstime->sesstime) {
507       sesstime->done = true;
508    }
509    if (sesstime->next) {
510       return match_sesstime(bsr, sesstime->next, rec, sesstime->done && done);
511    }
512    if (sesstime->done && done) {
513       bsr->done = true;
514       bsr->root->reposition = true;
515       Dmsg0(300, "bsr done from sesstime\n");
516    }
517    return 0;
518 }
519
520 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec)
521 {
522    if (!sessid) {
523       return 1;                       /* no specification matches all */
524    }
525    if (sessid->sessid <= rec->VolSessionId && sessid->sessid2 >= rec->VolSessionId) {
526       return 1;
527    }
528    if (sessid->next) {
529       return match_sessid(bsr, sessid->next, rec);
530    }
531    return 0;
532 }
533
534 /*
535  * When reading the Volume, the Volume Findex (rec->FileIndex) always
536  *   are found in sequential order. Thus we can make optimizations.
537  *
538  *  ***FIXME*** optimizations
539  * We could optimize a lot here by removing the recursion, and 
540  *   stopping the search earlier -- say when rec->FileIndex > findex->findex2
541  *   and findex->next == NULL.  Also, the current entry tests could be skipped
542  *   if findex->done is set.
543  */
544 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done)
545 {
546    if (!findex) {
547       return 1;                       /* no specification matches all */
548    }
549    if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
550       return 1;
551    }
552    if (rec->FileIndex > findex->findex2) {
553       findex->done = true;
554    }
555    if (findex->next) {
556       return match_findex(bsr, findex->next, rec, findex->done && done);
557    }
558    if (findex->done && done) {
559       bsr->done = true;
560       bsr->root->reposition = true;
561       Dmsg1(300, "bsr done from findex %d\n", rec->FileIndex);
562    }
563    return 0;
564 }