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