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