]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/read_record.c
- Remove Created new FileSet message as it always comes out in
[bacula/bacula] / bacula / src / stored / read_record.c
1 /*
2  *
3  *  This routine provides a routine that will handle all
4  *    the gory little details of reading a record from a Bacula
5  *    archive. It uses a callback to pass you each record in turn,
6  *    as well as a callback for mounting the next tape.  It takes
7  *    care of reading blocks, applying the bsr, ...
8  *    Note, this routine is really the heart of the restore routines,
9  *    and we are *really* bit pushing here so be careful about making
10  *    any modifications.
11  *
12  *    Kern E. Sibbald, August MMII
13  *
14  *   Version $Id$
15  */
16 /*
17    Copyright (C) 2000-2005 Kern Sibbald
18
19    This program is free software; you can redistribute it and/or
20    modify it under the terms of the GNU General Public License as
21    published by the Free Software Foundation; either version 2 of
22    the License, or (at your option) any later version.
23
24    This program is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27    General Public License for more details.
28
29    You should have received a copy of the GNU General Public
30    License along with this program; if not, write to the Free
31    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32    MA 02111-1307, USA.
33
34  */
35
36 #include "bacula.h"
37 #include "stored.h"
38
39 /* Forward referenced functions */
40 static void handle_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec);
41 static BSR *position_to_first_file(JCR *jcr, DEVICE *dev);
42 static bool try_repositioning(JCR *jcr, DEV_RECORD *rec, DEVICE *dev);
43 #ifdef DEBUG
44 static char *rec_state_to_str(DEV_RECORD *rec);
45 #endif
46
47 bool read_records(DCR *dcr,
48        bool record_cb(DCR *dcr, DEV_RECORD *rec),
49        bool mount_cb(DCR *dcr))
50 {
51    JCR *jcr = dcr->jcr;
52    DEVICE *dev = dcr->dev;
53    DEV_BLOCK *block = dcr->block;
54    DEV_RECORD *rec = NULL;
55    uint32_t record;
56    bool ok = true;
57    bool done = false;
58    SESSION_LABEL sessrec;
59    dlist *recs;                         /* linked list of rec packets open */
60
61    recs = New(dlist(rec, &rec->link));
62    position_to_first_file(jcr, dev);
63    jcr->mount_next_volume = false;
64
65    for ( ; ok && !done; ) {
66       if (job_canceled(jcr)) {
67          ok = false;
68          break;
69       }
70       if (!read_block_from_device(dcr, CHECK_BLOCK_NUMBERS)) {
71          if (dev->at_eot()) {
72             DEV_RECORD *trec = new_record();
73             Jmsg(jcr, M_INFO, 0, "End of Volume at file %u on device %s, Volume \"%s\"\n",
74                  dev->file, dev->print_name(), dcr->VolumeName);
75             if (!mount_cb(dcr)) {
76                Jmsg(jcr, M_INFO, 0, "End of all volumes.\n");
77                ok = false;            /* Stop everything */
78                /*
79                 * Create EOT Label so that Media record may
80                 *  be properly updated because this is the last
81                 *  tape.
82                 */
83                trec->FileIndex = EOT_LABEL;
84                trec->File = dev->file;
85                ok = record_cb(dcr, trec);
86                free_record(trec);
87                if (jcr->mount_next_volume) {
88                   jcr->mount_next_volume = false;
89                   dev->clear_eot();
90                }
91                break;
92             }
93             jcr->mount_next_volume = false;
94             /*
95              * We just have a new tape up, now read the label (first record)
96              *  and pass it off to the callback routine, then continue
97              *  most likely reading the previous record.
98              */
99             read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK);
100             read_record_from_block(block, trec);
101             handle_session_record(dev, trec, &sessrec);
102             ok = record_cb(dcr, trec);
103             free_record(trec);
104             position_to_first_file(jcr, dev);
105             /* After reading label, we must read first data block */
106             continue;
107
108          } else if (dev->at_eof()) {
109             if (verbose) {
110                Jmsg(jcr, M_INFO, 0, "Got EOF at file %u  on device %s, Volume \"%s\"\n",
111                   dev->file, dev->print_name(), dcr->VolumeName);
112             }
113             Dmsg3(200, "Got EOF at file %u  on device %s, Volume \"%s\"\n",
114                   dev->file, dev->print_name(), dcr->VolumeName);
115             continue;
116          } else if (dev_state(dev, ST_SHORT)) {
117             Jmsg1(jcr, M_ERROR, 0, "%s", dev->errmsg);
118             continue;
119          } else {
120             /* I/O error or strange end of tape */
121             display_tape_error_status(jcr, dev);
122             if (forge_on || jcr->ignore_label_errors) {
123                fsr_dev(dev, 1);       /* try skipping bad record */
124                Pmsg0(000, "Did fsr\n");
125                continue;              /* try to continue */
126             }
127             ok = false;               /* stop everything */
128             break;
129          }
130       }
131       Dmsg2(300, "New block at position=(file:block) %u:%u\n", dev->file, dev->block_num);
132 #ifdef if_and_when_FAST_BLOCK_REJECTION_is_working
133       /* this does not stop when file/block are too big */
134       if (!match_bsr_block(jcr->bsr, block)) {
135          if (try_repositioning(jcr, rec, dev)) {
136             break;                    /* get next volume */
137          }
138          continue;                    /* skip this record */
139       }
140 #endif
141
142       /*
143        * Get a new record for each Job as defined by
144        *   VolSessionId and VolSessionTime
145        */
146       bool found = false;
147       foreach_dlist(rec, recs) {
148          if (rec->VolSessionId == block->VolSessionId &&
149              rec->VolSessionTime == block->VolSessionTime) {
150             found = true;
151             break;
152           }
153       }
154       if (!found) {
155          rec = new_record();
156          recs->prepend(rec);
157          Dmsg2(300, "New record for SI=%d ST=%d\n",
158              block->VolSessionId, block->VolSessionTime);
159       }
160       Dmsg3(300, "After mount next vol. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
161             block->BlockNumber, rec->remainder);
162       record = 0;
163       rec->state = 0;
164       Dmsg1(300, "Block empty %d\n", is_block_empty(rec));
165       for (rec->state=0; !is_block_empty(rec); ) {
166          if (!read_record_from_block(block, rec)) {
167             Dmsg3(400, "!read-break. state=%s blk=%d rem=%d\n", rec_state_to_str(rec),
168                   block->BlockNumber, rec->remainder);
169             break;
170          }
171          Dmsg5(300, "read-OK. state=%s blk=%d rem=%d file:block=%u:%u\n",
172                  rec_state_to_str(rec), block->BlockNumber, rec->remainder,
173                  dev->file, dev->block_num);
174          /*
175           * At this point, we have at least a record header.
176           *  Now decide if we want this record or not, but remember
177           *  before accessing the record, we may need to read again to
178           *  get all the data.
179           */
180          record++;
181          Dmsg6(300, "recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
182             rec_state_to_str(rec), block->BlockNumber,
183             rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
184
185          if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
186             Dmsg0(40, "Get EOM LABEL\n");
187             break;                         /* yes, get out */
188          }
189
190          /* Some sort of label? */
191          if (rec->FileIndex < 0) {
192             handle_session_record(dev, rec, &sessrec);
193             ok = record_cb(dcr, rec);
194             if (rec->FileIndex == EOS_LABEL) {
195                Dmsg2(300, "Remove rec. SI=%d ST=%d\n", rec->VolSessionId,
196                   rec->VolSessionTime);
197                recs->remove(rec);
198                free_record(rec);
199             }
200             continue;
201          } /* end if label record */
202
203          /*
204           * Apply BSR filter
205           */
206          if (jcr->bsr) {
207             int stat = match_bsr(jcr->bsr, rec, &dev->VolHdr, &sessrec);
208             if (stat == -1) { /* no more possible matches */
209                done = true;   /* all items found, stop */
210                Dmsg2(300, "All done=(file:block) %u:%u\n", dev->file, dev->block_num);
211                break;
212             } else if (stat == 0) {  /* no match */
213                Dmsg4(300, "Clear rem=%d FI=%d before set_eof pos %u:%u\n",
214                   rec->remainder, rec->FileIndex, dev->file, dev->block_num);
215                rec->remainder = 0;
216                rec->state &= ~REC_PARTIAL_RECORD;
217                if (try_repositioning(jcr, rec, dev)) {
218                   break;
219                }
220                continue;              /* we don't want record, read next one */
221             }
222          }
223          if (is_partial_record(rec)) {
224             Dmsg6(300, "Partial, break. recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
225                rec_state_to_str(rec), block->BlockNumber,
226                rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
227             break;                    /* read second part of record */
228          }
229          ok = record_cb(dcr, rec);
230          if (rec->Stream == STREAM_MD5_SIGNATURE || rec->Stream == STREAM_SHA1_SIGNATURE) {
231             Dmsg3(300, "Done FI=%u before set_eof pos %u:%u\n", rec->FileIndex,
232                   dev->file, dev->block_num);
233             if (match_set_eof(jcr->bsr, rec) && try_repositioning(jcr, rec, dev)) {
234                Dmsg2(300, "Break after match_set_eof pos %u:%u\n",
235                      dev->file, dev->block_num);
236                break;
237             }
238             Dmsg2(300, "After set_eof pos %u:%u\n", dev->file, dev->block_num);
239          }
240       } /* end for loop over records */
241       Dmsg2(300, "After end records position=(file:block) %u:%u\n", dev->file, dev->block_num);
242    } /* end for loop over blocks */
243 // Dmsg2(300, "Position=(file:block) %u:%u\n", dev->file, dev->block_num);
244
245    /* Walk down list and free all remaining allocated recs */
246    while (!recs->empty()) {
247       rec = (DEV_RECORD *)recs->first();
248       recs->remove(rec);
249       free_record(rec);
250    }
251    delete recs;
252    print_block_read_errors(jcr, block);
253    return ok;
254 }
255
256 /*
257  * See if we can reposition.
258  *   Returns:  true  if at end of volume
259  *             false otherwise
260  */
261 static bool try_repositioning(JCR *jcr, DEV_RECORD *rec, DEVICE *dev)
262 {
263    BSR *bsr;
264    bsr = find_next_bsr(jcr->bsr, dev);
265    if (bsr == NULL && jcr->bsr->mount_next_volume) {
266       Dmsg0(300, "Would mount next volume here\n");
267       Dmsg2(300, "Current postion (file:block) %u:%u\n",
268          dev->file, dev->block_num);
269       jcr->bsr->mount_next_volume = false;
270       if (!dev->at_eot()) {
271          /* Set EOT flag to force mount of next Volume */
272          jcr->mount_next_volume = true;
273          dev->state |= ST_EOT;
274       }
275       rec->Block = 0;
276       return true;
277    }
278    if (bsr) {
279       if (verbose) {
280          Jmsg(jcr, M_INFO, 0, "Reposition from (file:block) %u:%u to %u:%u\n",
281             dev->file, dev->block_num, bsr->volfile->sfile,
282             bsr->volblock->sblock);
283       }
284       Dmsg4(300, "Try_Reposition from (file:block) %u:%u to %u:%u\n",
285             dev->file, dev->block_num, bsr->volfile->sfile,
286             bsr->volblock->sblock);
287       reposition_dev(dev, bsr->volfile->sfile, bsr->volblock->sblock);
288       rec->Block = 0;
289    }
290    return false;
291 }
292
293 /*
294  * Position to the first file on this volume
295  */
296 static BSR *position_to_first_file(JCR *jcr, DEVICE *dev)
297 {
298    BSR *bsr = NULL;
299    /*
300     * Now find and position to first file and block
301     *   on this tape.
302     */
303    if (jcr->bsr) {
304       jcr->bsr->reposition = true;    /* force repositioning */
305       bsr = find_next_bsr(jcr->bsr, dev);
306       if (bsr && (bsr->volfile->sfile != 0 || bsr->volblock->sblock != 0)) {
307          Jmsg(jcr, M_INFO, 0, _("Forward spacing to file:block %u:%u.\n"),
308             bsr->volfile->sfile, bsr->volblock->sblock);
309          Dmsg2(300, "Forward spacing to file:block %u:%u.\n",
310             bsr->volfile->sfile, bsr->volblock->sblock);
311          reposition_dev(dev, bsr->volfile->sfile, bsr->volblock->sblock);
312       }
313    }
314    return bsr;
315 }
316
317
318 static void handle_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
319 {
320    const char *rtype;
321    char buf[100];
322
323    memset(sessrec, 0, sizeof(sessrec));
324    switch (rec->FileIndex) {
325    case PRE_LABEL:
326       rtype = "Fresh Volume Label";
327       break;
328    case VOL_LABEL:
329       rtype = "Volume Label";
330       unser_volume_label(dev, rec);
331       break;
332    case SOS_LABEL:
333       rtype = "Begin Session";
334       unser_session_label(sessrec, rec);
335       break;
336    case EOS_LABEL:
337       rtype = "End Session";
338       break;
339    case EOM_LABEL:
340       rtype = "End of Media";
341       break;
342    default:
343       bsnprintf(buf, sizeof(buf), "Unknown code %d\n", rec->FileIndex);
344       rtype = buf;
345       break;
346    }
347    Dmsg5(300, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
348          rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
349 }
350
351 #ifdef DEBUG
352 static char *rec_state_to_str(DEV_RECORD *rec)
353 {
354    static char buf[200];
355    buf[0] = 0;
356    if (rec->state & REC_NO_HEADER) {
357       bstrncat(buf, "Nohdr,", sizeof(buf));
358    }
359    if (is_partial_record(rec)) {
360       bstrncat(buf, "partial,", sizeof(buf));
361    }
362    if (rec->state & REC_BLOCK_EMPTY) {
363       bstrncat(buf, "empty,", sizeof(buf));
364    }
365    if (rec->state & REC_NO_MATCH) {
366       bstrncat(buf, "Nomatch,", sizeof(buf));
367    }
368    if (rec->state & REC_CONTINUATION) {
369       bstrncat(buf, "cont,", sizeof(buf));
370    }
371    if (buf[0]) {
372       buf[strlen(buf)-1] = 0;
373    }
374    return buf;
375 }
376 #endif