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, ...
9 * Kern E. Sibbald, August MMII
14 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
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);
41 static char *rec_state_to_str(DEV_RECORD *rec);
44 int read_records(JCR *jcr, DEVICE *dev,
45 int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec),
46 int mount_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block))
49 DEV_RECORD *rec = NULL;
53 SESSION_LABEL sessrec;
54 dlist *recs; /* linked list of rec packets open */
56 block = new_block(dev);
57 recs = new dlist(rec, &rec->link);
58 position_to_first_file(jcr, dev);
60 for ( ; ok && !done; ) {
61 if (job_canceled(jcr)) {
65 if (!read_block_from_device(jcr, dev, block, CHECK_BLOCK_NUMBERS)) {
66 if (dev_state(dev, ST_EOT)) {
67 DEV_RECORD *trec = new_record();
69 Jmsg(jcr, M_INFO, 0, "End of Volume at file %u on device %s, Volume \"%s\"\n",
70 dev->file, dev_name(dev), jcr->VolumeName);
71 if (!mount_cb(jcr, dev, block)) {
72 Jmsg(jcr, M_INFO, 0, "End of all volumes.\n");
75 * Create EOT Label so that Media record may
76 * be properly updated because this is the last
79 trec->FileIndex = EOT_LABEL;
80 trec->File = dev->file;
81 ok = record_cb(jcr, dev, block, trec);
86 * We just have a new tape up, now read the label (first record)
87 * and pass it off to the callback routine, then continue
88 * most likely reading the previous record.
90 read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK);
91 read_record_from_block(block, trec);
92 handle_session_record(dev, trec, &sessrec);
93 ok = record_cb(jcr, dev, block, trec);
95 position_to_first_file(jcr, dev);
96 /* After reading label, we must read first data block */
99 } else if (dev_state(dev, ST_EOF)) {
101 Jmsg(jcr, M_INFO, 0, "Got EOF at file %u on device %s, Volume \"%s\"\n",
102 dev->file, dev_name(dev), jcr->VolumeName);
104 Dmsg3(100, "Got EOF at file %u on device %s, Volume \"%s\"\n",
105 dev->file, dev_name(dev), jcr->VolumeName);
107 } else if (dev_state(dev, ST_SHORT)) {
108 Jmsg1(jcr, M_ERROR, 0, "%s", dev->errmsg);
111 /* I/O error or strange end of tape */
112 display_tape_error_status(jcr, dev);
113 if (forge_on || jcr->ignore_label_errors) {
114 fsr_dev(dev, 1); /* try skipping bad record */
115 Dmsg0(000, "Did fsr\n");
116 continue; /* try to continue */
122 Dmsg2(100, "New block at position=(file:block) %d:%d\n", dev->file, dev->block_num);
123 #ifdef if_and_when_FAST_BLOCK_REJECTION_is_working
124 /* this does not stop when file/block are too big */
125 if (!match_bsr_block(jcr->bsr, block)) {
126 if (try_repositioning(jcr, rec, dev)) {
127 break; /* get next volume */
129 continue; /* skip this record */
134 * Get a new record for each Job as defined by
135 * VolSessionId and VolSessionTime
138 foreach_dlist(rec, recs) {
139 if (rec->VolSessionId == block->VolSessionId &&
140 rec->VolSessionTime == block->VolSessionTime) {
148 Dmsg2(100, "New record for SI=%d ST=%d\n",
149 block->VolSessionId, block->VolSessionTime);
152 if (rec->Block != 0 && (rec->Block+1) != block->BlockNumber) {
153 Jmsg(jcr, M_ERROR, 0, _("Invalid block number. Expected %u, got %u\n"),
154 rec->Block+1, block->BlockNumber);
158 Dmsg3(100, "After mount next vol. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
159 block->BlockNumber, rec->remainder);
162 Dmsg1(100, "Block empty %d\n", is_block_empty(rec));
163 for (rec->state=0; !is_block_empty(rec); ) {
164 if (!read_record_from_block(block, rec)) {
165 Dmsg3(100, "!read-break. state=%s blk=%d rem=%d\n", rec_state_to_str(rec),
166 block->BlockNumber, rec->remainder);
169 Dmsg5(100, "read-OK. state=%s blk=%d rem=%d file:block=%d:%d\n",
170 rec_state_to_str(rec), block->BlockNumber, rec->remainder,
171 dev->file, dev->block_num);
173 * At this point, we have at least a record header.
174 * Now decide if we want this record or not, but remember
175 * before accessing the record, we may need to read again to
179 Dmsg6(100, "recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
180 rec_state_to_str(rec), block->BlockNumber,
181 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
183 if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
184 Dmsg0(40, "Get EOM LABEL\n");
185 break; /* yes, get out */
188 /* Some sort of label? */
189 if (rec->FileIndex < 0) {
190 handle_session_record(dev, rec, &sessrec);
191 ok = record_cb(jcr, dev, block, rec);
192 if (rec->FileIndex == EOS_LABEL) {
193 Dmsg2(100, "Remove rec. SI=%d ST=%d\n", rec->VolSessionId,
194 rec->VolSessionTime);
199 } /* end if label record */
205 int stat = match_bsr(jcr->bsr, rec, &dev->VolHdr, &sessrec);
206 if (stat == -1) { /* no more possible matches */
207 done = true; /* all items found, stop */
208 Dmsg2(100, "All done=(file:block) %d:%d\n", dev->file, dev->block_num);
210 } else if (stat == 0) { /* no match */
211 Dmsg4(100, "Clear rem=%d FI=%d before set_eof pos %d:%d\n",
212 rec->remainder, rec->FileIndex, dev->file, dev->block_num);
214 rec->state &= ~REC_PARTIAL_RECORD;
215 if (try_repositioning(jcr, rec, dev)) {
218 continue; /* we don't want record, read next one */
221 if (is_partial_record(rec)) {
222 Dmsg6(100, "Partial, break. recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
223 rec_state_to_str(rec), block->BlockNumber,
224 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
225 break; /* read second part of record */
227 ok = record_cb(jcr, dev, block, rec);
228 if (rec->Stream == STREAM_MD5_SIGNATURE || rec->Stream == STREAM_SHA1_SIGNATURE) {
229 Dmsg3(100, "Done FI=%d before set_eof pos %d:%d\n", rec->FileIndex,
230 dev->file, dev->block_num);
231 if (match_set_eof(jcr->bsr, rec) && try_repositioning(jcr, rec, dev)) {
232 Dmsg2(100, "Break after match_set_eof pos %d:%d\n",
233 dev->file, dev->block_num);
236 Dmsg2(100, "After set_eof pos %d:%d\n", dev->file, dev->block_num);
238 } /* end for loop over records */
239 Dmsg2(100, "After end records position=(file:block) %d:%d\n", dev->file, dev->block_num);
240 } /* end for loop over blocks */
241 // Dmsg2(100, "Position=(file:block) %d:%d\n", dev->file, dev->block_num);
243 /* Walk down list and free all remaining allocated recs */
244 while (!recs->empty()) {
245 rec = (DEV_RECORD *)recs->first();
250 print_block_read_errors(jcr, block);
256 * See if we can reposition.
257 * Returns: 1 if at end of volume
260 static int try_repositioning(JCR *jcr, DEV_RECORD *rec, DEVICE *dev)
263 bsr = find_next_bsr(jcr->bsr, dev);
264 if (bsr == NULL && jcr->bsr->mount_next_volume) {
265 Dmsg0(100, "Would mount next volume here\n");
266 Dmsg2(100, "Current postion (file:block) %d:%d\n",
267 dev->file, dev->block_num);
268 jcr->bsr->mount_next_volume = false;
269 dev->state |= ST_EOT;
275 Jmsg(jcr, M_INFO, 0, "Reposition from (file:block) %d:%d to %d:%d\n",
276 dev->file, dev->block_num, bsr->volfile->sfile,
277 bsr->volblock->sblock);
279 Dmsg4(100, "Try_Reposition from (file:block) %d:%d to %d:%d\n",
280 dev->file, dev->block_num, bsr->volfile->sfile,
281 bsr->volblock->sblock);
282 reposition_dev(dev, bsr->volfile->sfile, bsr->volblock->sblock);
289 * Position to the first file on this volume
291 static BSR *position_to_first_file(JCR *jcr, DEVICE *dev)
295 * Now find and position to first file and block
299 jcr->bsr->reposition = true; /* force repositioning */
300 bsr = find_next_bsr(jcr->bsr, dev);
301 if (bsr && (bsr->volfile->sfile != 0 || bsr->volblock->sblock != 0)) {
302 Jmsg(jcr, M_INFO, 0, _("Forward spacing to file:block %u:%u.\n"),
303 bsr->volfile->sfile, bsr->volblock->sblock);
304 Dmsg2(100, "Forward spacing to file:block %u:%u.\n",
305 bsr->volfile->sfile, bsr->volblock->sblock);
306 reposition_dev(dev, bsr->volfile->sfile, bsr->volblock->sblock);
313 static void handle_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
318 memset(sessrec, 0, sizeof(sessrec));
319 switch (rec->FileIndex) {
321 rtype = "Fresh Volume Label";
324 rtype = "Volume Label";
325 unser_volume_label(dev, rec);
328 rtype = "Begin Session";
329 unser_session_label(sessrec, rec);
332 rtype = "End Session";
335 rtype = "End of Media";
338 bsnprintf(buf, sizeof(buf), "Unknown code %d\n", rec->FileIndex);
342 Dmsg5(100, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
343 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
347 static char *rec_state_to_str(DEV_RECORD *rec)
349 static char buf[200];
351 if (rec->state & REC_NO_HEADER) {
352 bstrncat(buf, "Nohdr,", sizeof(buf));
354 if (is_partial_record(rec)) {
355 bstrncat(buf, "partial,", sizeof(buf));
357 if (rec->state & REC_BLOCK_EMPTY) {
358 bstrncat(buf, "empty,", sizeof(buf));
360 if (rec->state & REC_NO_MATCH) {
361 bstrncat(buf, "Nomatch,", sizeof(buf));
363 if (rec->state & REC_CONTINUATION) {
364 bstrncat(buf, "cont,", sizeof(buf));
367 buf[strlen(buf)-1] = 0;