]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/record.c
Add Bytes/Blocks read by SD during restore
[bacula/bacula] / bacula / src / stored / record.c
1 /*
2  *
3  *   record.c -- tape record handling functions
4  *
5  *              Kern Sibbald, April MMI
6  *                added BB02 format October MMII
7  *
8  *   Version $Id$
9  *
10  */
11 /*
12    Copyright (C) 2000-2003 Kern Sibbald and John Walker
13
14    This program is free software; you can redistribute it and/or
15    modify it under the terms of the GNU General Public License as
16    published by the Free Software Foundation; either version 2 of
17    the License, or (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public
25    License along with this program; if not, write to the Free
26    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27    MA 02111-1307, USA.
28
29  */
30
31
32 #include "bacula.h"
33 #include "stored.h"
34
35 extern int debug_level;
36
37 /*
38  * Convert a FileIndex into a printable
39  *   ASCII string.  Not reentrant.
40  * If the FileIndex is negative, it flags the
41  *   record as a Label, otherwise it is simply
42  *   the FileIndex of the current file.
43  */
44 char *FI_to_ascii(int fi)
45 {
46    static char buf[20];
47    if (fi >= 0) {
48       sprintf(buf, "%d", fi);
49       return buf;
50    }
51    switch (fi) {
52    case PRE_LABEL:
53       return "PRE_LABEL";
54    case VOL_LABEL:
55       return "VOL_LABEL";
56    case EOM_LABEL:
57       return "EOM_LABEL";
58    case SOS_LABEL:
59       return "SOS_LABEL";
60    case EOS_LABEL:
61       return "EOS_LABEL";
62    case EOT_LABEL:
63       return "EOT_LABEL";
64       break;
65    default:
66      sprintf(buf, "unknown: %d", fi);
67      return buf;
68    }
69 }
70
71
72 /* 
73  * Convert a Stream ID into a printable
74  * ASCII string.  Not reentrant.
75
76  * A negative stream number represents
77  *   stream data that is continued from a
78  *   record in the previous block.
79  * If the FileIndex is negative, we are
80  *   dealing with a Label, hence the
81  *   stream is the JobId.
82  */
83 char *stream_to_ascii(int stream, int fi)
84 {
85     static char buf[20];
86     if (fi < 0) {
87        sprintf(buf, "%d", stream);
88        return buf;     
89     }
90     switch (stream) {
91     case STREAM_UNIX_ATTRIBUTES:
92        return "UATTR";
93     case STREAM_FILE_DATA:
94        return "DATA";
95     case STREAM_WIN32_DATA:
96        return "WIN32-DATA";
97     case STREAM_WIN32_GZIP_DATA:
98        return "WIN32-GZIP";
99     case STREAM_MD5_SIGNATURE:
100        return "MD5";
101     case STREAM_SHA1_SIGNATURE:
102        return "SHA1";
103     case STREAM_GZIP_DATA:
104        return "GZIP";
105     case STREAM_WIN32_ATTRIBUTES:
106        return "WIN32-ATTR";
107     case STREAM_SPARSE_DATA:
108        return "SPARSE-DATA";
109     case STREAM_SPARSE_GZIP_DATA:
110        return "SPARSE-GZIP";
111     case STREAM_PROGRAM_NAMES:
112        return "PROG-NAMES";
113     case STREAM_PROGRAM_DATA:
114        return "PROG-DATA";
115     case -STREAM_UNIX_ATTRIBUTES:
116        return "contUATTR";
117     case -STREAM_FILE_DATA:
118        return "contDATA";
119     case -STREAM_WIN32_DATA:
120        return "contWIN32-DATA";
121     case -STREAM_WIN32_GZIP_DATA:
122        return "contWIN32-GZIP";
123     case -STREAM_MD5_SIGNATURE:
124        return "contMD5";
125     case -STREAM_SHA1_SIGNATURE:
126        return "contSHA1";
127     case -STREAM_GZIP_DATA:
128        return "contGZIP";
129     case -STREAM_WIN32_ATTRIBUTES:
130        return "contWIN32-ATTR";
131     case -STREAM_SPARSE_DATA:
132        return "contSPARSE-DATA";
133     case -STREAM_SPARSE_GZIP_DATA:
134        return "contSPARSE-GZIP";
135     case -STREAM_PROGRAM_NAMES:
136        return "contPROG-NAMES";
137     case -STREAM_PROGRAM_DATA:
138        return "contPROG-DATA";
139     default:
140        sprintf(buf, "%d", stream);
141        return buf;     
142     }
143 }
144
145 /* 
146  * Return a new record entity
147  */
148 DEV_RECORD *new_record(void)
149 {
150    DEV_RECORD *rec;
151
152    rec = (DEV_RECORD *) get_memory(sizeof(DEV_RECORD));
153    memset(rec, 0, sizeof(DEV_RECORD));
154    rec->data = get_pool_memory(PM_MESSAGE);
155    return rec;
156 }
157
158 /*
159  * Free the record entity 
160  *
161  */
162 void free_record(DEV_RECORD *rec) 
163 {
164    Dmsg0(150, "Enter free_record.\n");
165    if (rec->data) {
166       free_pool_memory(rec->data);
167    }
168    Dmsg0(150, "Data buf is freed.\n");
169    free_pool_memory((POOLMEM *)rec);
170    Dmsg0(150, "Leave free_record.\n");
171
172
173
174 /*
175  * Write a Record to the block
176  *
177  *  Returns: 0 on failure (none or partially written)
178  *           1 on success (all bytes written)
179  *
180  *  and remainder returned in packet.
181  *
182  *  We require enough room for the header, and we deal with
183  *  two special cases. 1. Only part of the record may have
184  *  been transferred the last time (when remainder is
185  *  non-zero), and 2. The remaining bytes to write may not
186  *  all fit into the block.
187  */
188 int write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
189 {
190    ser_declare;
191    uint32_t remlen;
192
193    remlen = block->buf_len - block->binbuf;
194
195    ASSERT(block->binbuf == (uint32_t) (block->bufp - block->buf));
196    ASSERT(remlen >= 0);
197
198    Dmsg6(190, "write_record_to_block() FI=%s SessId=%d Strm=%s len=%d\n\
199 rem=%d remainder=%d\n",
200       FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
201       stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
202       remlen, rec->remainder);
203
204    /*
205     * If rec->remainder is non-zero, we have been called a
206     *  second (or subsequent) time to finish writing a record
207     *  that did not previously fit into the block.
208     */
209    if (rec->remainder == 0) {
210       /* Require enough room to write a full header */
211       if (remlen >= WRITE_RECHDR_LENGTH) {
212          ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
213          if (BLOCK_VER == 1) {
214          ser_uint32(rec->VolSessionId);
215          ser_uint32(rec->VolSessionTime);
216          } else {
217             block->VolSessionId = rec->VolSessionId;
218             block->VolSessionTime = rec->VolSessionTime;
219          }
220          ser_int32(rec->FileIndex);
221          ser_int32(rec->Stream);
222          ser_uint32(rec->data_len);
223
224          block->bufp += WRITE_RECHDR_LENGTH;
225          block->binbuf += WRITE_RECHDR_LENGTH;
226          remlen -= WRITE_RECHDR_LENGTH;
227          rec->remainder = rec->data_len;
228       } else {
229          rec->remainder = rec->data_len + WRITE_RECHDR_LENGTH;
230          return 0;
231       }
232    } else {
233       /* 
234        * We are here to write unwritten bytes from a previous
235        * time. Presumably we have a new buffer (possibly 
236        * containing a volume label), so the new header 
237        * should be able to fit in the block -- otherwise we have
238        * an error.  Note, we have to continue splitting the
239        * data record if it is longer than the block.
240        * 
241        * First, write the header, then write as much as 
242        * possible of the data record.
243        *
244        * Every time we write a header and it is a continuation
245        * of a previous partially written record, we store the
246        * Stream as -Stream in the record header.
247        */
248       ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
249       if (BLOCK_VER == 1) {
250       ser_uint32(rec->VolSessionId);
251       ser_uint32(rec->VolSessionTime);
252       } else {
253          block->VolSessionId = rec->VolSessionId;
254          block->VolSessionTime = rec->VolSessionTime;
255       }
256       ser_int32(rec->FileIndex);
257       if (rec->remainder > rec->data_len) {
258          ser_int32(rec->Stream);      /* normal full header */
259          ser_uint32(rec->data_len);
260          rec->remainder = rec->data_len; /* must still do data record */
261       } else {
262          ser_int32(-rec->Stream);     /* mark this as a continuation record */
263          ser_uint32(rec->remainder);  /* bytes to do */
264       }
265
266       /* Require enough room to write a full header */
267       ASSERT(remlen >= WRITE_RECHDR_LENGTH);
268
269       block->bufp += WRITE_RECHDR_LENGTH;
270       block->binbuf += WRITE_RECHDR_LENGTH;
271       remlen -= WRITE_RECHDR_LENGTH;
272    }
273    if (remlen == 0) {
274       return 0;                       /* partial transfer */
275    }
276
277    /*
278     * Now deal with data record.
279     * Part of it may have already been transferred, and we 
280     * may not have enough room to transfer the whole this time.
281     */
282    if (rec->remainder > 0) {
283       /* Write as much of data as possible */
284       if (remlen >= rec->remainder) {
285          memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
286                 rec->remainder);
287          block->bufp += rec->remainder;
288          block->binbuf += rec->remainder;
289       } else {
290          memcpy(block->bufp, rec->data+rec->data_len-rec->remainder, 
291                 remlen);
292 #ifdef xxxxxSMCHECK
293          if (!sm_check_rtn(__FILE__, __LINE__, False)) {
294             /* We damaged a buffer */
295             Dmsg6(0, "Damaged block FI=%s SessId=%d Strm=%s len=%d\n\
296 rem=%d remainder=%d\n",
297                FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
298                stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
299                remlen, rec->remainder);
300             Dmsg5(0, "Damaged block: bufp=%x binbuf=%d buf_len=%d rem=%d moved=%d\n",
301                block->bufp, block->binbuf, block->buf_len, block->buf_len-block->binbuf,
302                remlen);
303             Dmsg2(0, "Damaged block: buf=%x binbuffrombuf=%d \n",
304                block->buf, block->bufp-block->buf);
305
306                Emsg0(M_ABORT, 0, "Damaged buffer\n");
307          }
308 #endif
309
310          block->bufp += remlen;
311          block->binbuf += remlen;
312          rec->remainder -= remlen;
313          return 0;                    /* did partial transfer */
314       }
315    }
316    rec->remainder = 0;                /* did whole transfer */
317    return 1;
318 }
319
320
321 /*
322  * Test if we can write whole record to the block
323  *
324  *  Returns: 0 on failure 
325  *           1 on success (all bytes can be written)
326  */
327 int can_write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
328 {
329    uint32_t remlen;
330
331    remlen = block->buf_len - block->binbuf;
332    if (rec->remainder == 0) {
333       if (remlen >= WRITE_RECHDR_LENGTH) {
334          remlen -= WRITE_RECHDR_LENGTH;
335          rec->remainder = rec->data_len;
336       } else {
337          return 0;
338       }
339    } else {
340       return 0;
341    }
342    if (rec->remainder > 0 && remlen < rec->remainder) {
343       return 0;
344    }
345    return 1;
346 }
347
348
349 /*
350  * Read a Record from the block
351  *  Returns: 0 if nothing read or if the continuation record does not match.
352  *             In both of these cases, a block read must be done.
353  *           1 if at least the record header was read, this 
354  *             routine may have to be called again with a new
355  *             block if the entire record was not read.
356  */
357 int read_record_from_block(DEV_BLOCK *block, DEV_RECORD *rec)
358 {
359    ser_declare;
360    uint32_t remlen;
361    uint32_t VolSessionId;
362    uint32_t VolSessionTime;
363    int32_t  FileIndex;
364    int32_t  Stream;
365    uint32_t data_bytes;
366    uint32_t rhl;
367
368    remlen = block->binbuf;
369    rec->Block = block->BlockNumber;
370    rec->File = ((DEVICE *)block->dev)->file;
371
372    /* Clear state flags */       
373    rec->state = 0;
374    if (((DEVICE *)block->dev)->state & ST_TAPE) {
375       rec->state |= REC_ISTAPE;
376    }
377
378
379    /* 
380     * Get the header. There is always a full header,
381     * otherwise we find it in the next block.
382     */
383    Dmsg3(100, "Block=%d Ver=%d size=%u\n", block->BlockNumber, block->BlockVer,
384          block->block_len);
385    if (block->BlockVer == 1) {
386       rhl = RECHDR1_LENGTH;
387    } else {
388       rhl = RECHDR2_LENGTH;
389    }
390    if (remlen >= rhl) {
391       Dmsg4(90, "Enter read_record_block: remlen=%d data_len=%d rem=%d blkver=%d\n", 
392             remlen, rec->data_len, rec->remainder, block->BlockVer);
393
394       unser_begin(block->bufp, WRITE_RECHDR_LENGTH);
395       if (block->BlockVer == 1) {
396       unser_uint32(VolSessionId);
397       unser_uint32(VolSessionTime);
398       } else {
399          VolSessionId = block->VolSessionId;
400          VolSessionTime = block->VolSessionTime;
401       }
402       unser_int32(FileIndex);
403       unser_int32(Stream);
404       unser_uint32(data_bytes);
405
406       block->bufp += rhl;
407       block->binbuf -= rhl;
408       remlen -= rhl;
409
410       /* If we are looking for more (remainder!=0), we reject anything
411        *  where the VolSessionId and VolSessionTime don't agree
412        */
413       if (rec->remainder && (rec->VolSessionId != VolSessionId || 
414                              rec->VolSessionTime != VolSessionTime)) {
415          rec->state |= REC_NO_MATCH;
416          return 0;                 /* This is from some other Session */
417       }
418
419       /* if Stream is negative, it means that this is a continuation
420        * of a previous partially written record.
421        */
422       if (Stream < 0) {               /* continuation record? */
423          Dmsg1(500, "Got negative Stream => continuation. remainder=%d\n", 
424             rec->remainder);
425          rec->state |= REC_CONTINUATION;
426          if (!rec->remainder) {       /* if we didn't read previously */
427             rec->data_len = 0;        /* return data as if no continuation */
428          } else if (rec->Stream != -Stream) {
429             rec->state |= REC_NO_MATCH;
430             return 0;                 /* This is from some other Session */
431          }
432          rec->Stream = -Stream;       /* set correct Stream */
433       } else {                        /* Regular record */
434          rec->Stream = Stream;
435          rec->data_len = 0;           /* transfer to beginning of data */
436       }
437       rec->VolSessionId = VolSessionId;
438       rec->VolSessionTime = VolSessionTime;
439       rec->FileIndex = FileIndex;
440
441       Dmsg6(100, "rd_rec_blk() got FI=%s SessId=%d Strm=%s len=%u\n\
442 remlen=%d data_len=%d\n",
443          FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
444          stream_to_ascii(rec->Stream, rec->FileIndex), data_bytes, remlen, 
445          rec->data_len);
446    } else {
447       /*    
448        * No more records in this block because the number   
449        * of remaining bytes are less than a record header 
450        * length, so return empty handed, but indicate that
451        * he must read again. By returning, we allow the
452        * higher level routine to fetch the next block and
453        * then reread.
454        */
455       Dmsg0(90, "read_record_block: nothing\n");
456 #ifdef xxx
457       if (!rec->remainder) {
458          rec->remainder = 1;          /* set to expect continuation */
459          rec->data_len = 0;           /* no data transferred */
460       }
461 #endif
462       rec->state |= (REC_NO_HEADER | REC_BLOCK_EMPTY);
463       empty_block(block);                      /* mark block empty */
464       return 0;
465    }
466
467    ASSERT(data_bytes < MAX_BLOCK_LENGTH);       /* temp sanity check */
468
469    rec->data = check_pool_memory_size(rec->data, rec->data_len+data_bytes);
470    
471    /*
472     * At this point, we have read the header, now we
473     * must transfer as much of the data record as 
474     * possible taking into account: 1. A partial
475     * data record may have previously been transferred,
476     * 2. The current block may not contain the whole data
477     * record.
478     */
479    if (remlen >= data_bytes) {
480       /* Got whole record */
481       memcpy(rec->data+rec->data_len, block->bufp, data_bytes);
482       block->bufp += data_bytes;
483       block->binbuf -= data_bytes;
484       rec->data_len += data_bytes;
485    } else {
486       /* Partial record */
487       memcpy(rec->data+rec->data_len, block->bufp, remlen);
488       block->bufp += remlen;
489       block->binbuf -= remlen;
490       rec->data_len += remlen;
491       rec->remainder = 1;             /* partial record transferred */
492       Dmsg1(90, "read_record_block: partial xfered=%d\n", rec->data_len);
493       rec->state |= (REC_PARTIAL_RECORD | REC_BLOCK_EMPTY);
494       return 1;
495    }
496    rec->remainder = 0;
497    Dmsg4(90, "Rtn full rd_rec_blk FI=%s SessId=%d Strm=%s len=%d\n",
498       FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
499       stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
500    return 1;                          /* transferred full record */
501 }