]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/read_record.c
5ecfdf7c4e8ecd15363dd820be5cb86c9f77e9e1
[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-2004 Kern Sibbald and John Walker
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(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec),
46        bool mount_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block))
47 {
48    JCR *jcr = dcr->jcr;
49    DEVICE *dev = dcr->dev;
50    DEV_BLOCK *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    block = new_block(dev);
59    recs = New(dlist(rec, &rec->link));
60    position_to_first_file(jcr, dev);
61
62    for ( ; ok && !done; ) {
63       if (job_canceled(jcr)) {
64          ok = false;
65          break;
66       }
67       if (!read_block_from_device(dcr, block, CHECK_BLOCK_NUMBERS)) {
68          if (dev_state(dev, ST_EOT)) {
69             DEV_RECORD *trec = new_record();
70
71             Jmsg(jcr, M_INFO, 0, "End of Volume at file %u on device %s, Volume \"%s\"\n", 
72                  dev->file, dev_name(dev), jcr->VolumeName);
73             if (!mount_cb(jcr, dev, block)) {
74                Jmsg(jcr, M_INFO, 0, "End of all volumes.\n");
75                ok = false;
76                /*
77                 * Create EOT Label so that Media record may
78                 *  be properly updated because this is the last
79                 *  tape.
80                 */
81                trec->FileIndex = EOT_LABEL;
82                trec->File = dev->file;
83                ok = record_cb(jcr, dev, block, trec);
84                free_record(trec);
85                break;
86             }
87             /*
88              * We just have a new tape up, now read the label (first record)
89              *  and pass it off to the callback routine, then continue
90              *  most likely reading the previous record.
91              */
92             read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK);
93             read_record_from_block(block, trec);
94             handle_session_record(dev, trec, &sessrec);
95             ok = record_cb(jcr, dev, block, trec);
96             free_record(trec);
97             position_to_first_file(jcr, dev);
98             /* After reading label, we must read first data block */
99             continue;
100
101          } else if (dev_state(dev, ST_EOF)) {
102             if (verbose) {
103                Jmsg(jcr, M_INFO, 0, "Got EOF at file %u  on device %s, Volume \"%s\"\n", 
104                   dev->file, dev_name(dev), jcr->VolumeName);
105             }
106             Dmsg3(100, "Got EOF at file %u  on device %s, Volume \"%s\"\n", 
107                   dev->file, dev_name(dev), jcr->VolumeName);
108             continue;
109          } else if (dev_state(dev, ST_SHORT)) {
110             Jmsg1(jcr, M_ERROR, 0, "%s", dev->errmsg);
111             continue;
112          } else {
113             /* I/O error or strange end of tape */
114             display_tape_error_status(jcr, dev);
115             if (forge_on || jcr->ignore_label_errors) {
116                fsr_dev(dev, 1);       /* try skipping bad record */
117                Dmsg0(000, "Did fsr\n");
118                continue;              /* try to continue */
119             }
120             ok = false;
121             break;
122          }
123       }
124       Dmsg2(100, "New block at position=(file:block) %d:%d\n", dev->file, dev->block_num);
125 #ifdef if_and_when_FAST_BLOCK_REJECTION_is_working
126       /* this does not stop when file/block are too big */
127       if (!match_bsr_block(jcr->bsr, block)) {
128          if (try_repositioning(jcr, rec, dev)) {
129             break;                    /* get next volume */
130          }
131          continue;                    /* skip this record */
132       }
133 #endif
134
135       /*
136        * Get a new record for each Job as defined by
137        *   VolSessionId and VolSessionTime 
138        */
139       bool found = false;
140       foreach_dlist(rec, recs) {
141          if (rec->VolSessionId == block->VolSessionId &&
142              rec->VolSessionTime == block->VolSessionTime) {
143             found = true;
144             break;
145           }
146       }
147       if (!found) {
148          rec = new_record();
149          recs->prepend(rec);
150          Dmsg2(100, "New record for SI=%d ST=%d\n",
151              block->VolSessionId, block->VolSessionTime);
152       } else {
153 #ifdef xxx
154          if (rec->Block != 0 && (rec->Block+1) != block->BlockNumber) {
155             Jmsg(jcr, M_ERROR, 0, _("Invalid block number. Expected %u, got %u\n"),
156                  rec->Block+1, block->BlockNumber);
157          }
158 #endif 
159       }
160       Dmsg3(100, "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(100, "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(100, "!read-break. state=%s blk=%d rem=%d\n", rec_state_to_str(rec), 
168                   block->BlockNumber, rec->remainder);
169             break;
170          }
171          Dmsg5(100, "read-OK. state=%s blk=%d rem=%d file:block=%d:%d\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(100, "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(jcr, dev, block, rec);
194             if (rec->FileIndex == EOS_LABEL) {
195                Dmsg2(100, "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(100, "All done=(file:block) %d:%d\n", dev->file, dev->block_num);
211                break;
212             } else if (stat == 0) {  /* no match */
213                Dmsg4(100, "Clear rem=%d FI=%d before set_eof pos %d:%d\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(100, "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(jcr, dev, block, rec);
230          if (rec->Stream == STREAM_MD5_SIGNATURE || rec->Stream == STREAM_SHA1_SIGNATURE) {
231             Dmsg3(100, "Done FI=%d before set_eof pos %d:%d\n", rec->FileIndex,
232                   dev->file, dev->block_num);
233             if (match_set_eof(jcr->bsr, rec) && try_repositioning(jcr, rec, dev)) {
234                Dmsg2(100, "Break after match_set_eof pos %d:%d\n",
235                      dev->file, dev->block_num);
236                break;
237             }
238             Dmsg2(100, "After set_eof pos %d:%d\n", dev->file, dev->block_num);
239          }
240       } /* end for loop over records */
241       Dmsg2(100, "After end records position=(file:block) %d:%d\n", dev->file, dev->block_num);
242    } /* end for loop over blocks */
243 // Dmsg2(100, "Position=(file:block) %d:%d\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    free_block(block);
254    return ok;
255 }
256
257 /*
258  * See if we can reposition.
259  *   Returns:  1 if at end of volume
260  *             0 otherwise
261  */
262 static int try_repositioning(JCR *jcr, DEV_RECORD *rec, DEVICE *dev)
263 {
264    BSR *bsr;
265    bsr = find_next_bsr(jcr->bsr, dev);
266    if (bsr == NULL && jcr->bsr->mount_next_volume) {
267       Dmsg0(100, "Would mount next volume here\n");
268       Dmsg2(100, "Current postion (file:block) %d:%d\n",
269          dev->file, dev->block_num);
270       jcr->bsr->mount_next_volume = false;
271       dev->state |= ST_EOT;
272       rec->Block = 0;
273       return 1;
274    }     
275    if (bsr) {
276       if (verbose) {
277          Jmsg(jcr, M_INFO, 0, "Reposition from (file:block) %d:%d to %d:%d\n",
278             dev->file, dev->block_num, bsr->volfile->sfile,
279             bsr->volblock->sblock);
280       }
281       Dmsg4(100, "Try_Reposition from (file:block) %d:%d to %d:%d\n",
282             dev->file, dev->block_num, bsr->volfile->sfile,
283             bsr->volblock->sblock);
284       reposition_dev(dev, bsr->volfile->sfile, bsr->volblock->sblock);
285       rec->Block = 0;
286    }
287    return 0;
288 }
289
290 /*
291  * Position to the first file on this volume
292  */
293 static BSR *position_to_first_file(JCR *jcr, DEVICE *dev)
294 {
295    BSR *bsr = NULL;
296    /*
297     * Now find and position to first file and block 
298     *   on this tape.
299     */
300    if (jcr->bsr) {
301       jcr->bsr->reposition = true;    /* force repositioning */
302       bsr = find_next_bsr(jcr->bsr, dev);
303       if (bsr && (bsr->volfile->sfile != 0 || bsr->volblock->sblock != 0)) {
304          Jmsg(jcr, M_INFO, 0, _("Forward spacing to file:block %u:%u.\n"), 
305             bsr->volfile->sfile, bsr->volblock->sblock);
306          Dmsg2(100, "Forward spacing to file:block %u:%u.\n", 
307             bsr->volfile->sfile, bsr->volblock->sblock);
308          reposition_dev(dev, bsr->volfile->sfile, bsr->volblock->sblock);
309       }
310    }
311    return bsr;
312 }
313
314
315 static void handle_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
316 {
317    const char *rtype;
318    char buf[100];
319    
320    memset(sessrec, 0, sizeof(sessrec));
321    switch (rec->FileIndex) {
322    case PRE_LABEL:
323       rtype = "Fresh Volume Label";   
324       break;
325    case VOL_LABEL:
326       rtype = "Volume Label";
327       unser_volume_label(dev, rec);
328       break;
329    case SOS_LABEL:
330       rtype = "Begin Session";
331       unser_session_label(sessrec, rec);
332       break;
333    case EOS_LABEL:
334       rtype = "End Session";
335       break;
336    case EOM_LABEL:
337       rtype = "End of Media";
338       break;
339    default:
340       bsnprintf(buf, sizeof(buf), "Unknown code %d\n", rec->FileIndex);
341       rtype = buf;
342       break;
343    }
344    Dmsg5(100, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
345          rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
346 }
347
348 #ifdef DEBUG
349 static char *rec_state_to_str(DEV_RECORD *rec)
350 {
351    static char buf[200]; 
352    buf[0] = 0;
353    if (rec->state & REC_NO_HEADER) {
354       bstrncat(buf, "Nohdr,", sizeof(buf));
355    }
356    if (is_partial_record(rec)) {
357       bstrncat(buf, "partial,", sizeof(buf));
358    }
359    if (rec->state & REC_BLOCK_EMPTY) {
360       bstrncat(buf, "empty,", sizeof(buf));
361    }
362    if (rec->state & REC_NO_MATCH) {
363       bstrncat(buf, "Nomatch,", sizeof(buf));
364    }
365    if (rec->state & REC_CONTINUATION) {
366       bstrncat(buf, "cont,", sizeof(buf));
367    }
368    if (buf[0]) {
369       buf[strlen(buf)-1] = 0;
370    }
371    return buf;
372 }
373 #endif