]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/record.c
First cut 1.27 see kes23Oct02
[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, 2001, 2002 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_MD5_SIGNATURE:
96        return "MD5";
97     case STREAM_GZIP_DATA:
98        return "GZIP";
99     case STREAM_WIN32_ATTRIBUTES:
100        return "WIN32-ATTR";
101     case STREAM_SPARSE_DATA:
102        return "SPARSE-DATA";
103     case STREAM_SPARSE_GZIP_DATA:
104        return "SPARSE-GZIP";
105     case STREAM_PROGRAM_NAMES:
106        return "PROG-NAMES";
107     case STREAM_PROGRAM_DATA:
108        return "PROG-DATA";
109     case -STREAM_UNIX_ATTRIBUTES:
110        return "contUATTR";
111     case -STREAM_FILE_DATA:
112        return "contDATA";
113     case -STREAM_MD5_SIGNATURE:
114        return "contMD5";
115     case -STREAM_GZIP_DATA:
116        return "contGZIP";
117     case -STREAM_WIN32_ATTRIBUTES:
118        return "contWIN32-ATTR";
119     case -STREAM_SPARSE_DATA:
120        return "contSPARSE-DATA";
121     case -STREAM_SPARSE_GZIP_DATA:
122        return "contSPARSE-GZIP";
123     case -STREAM_PROGRAM_NAMES:
124        return "contPROG-NAMES";
125     case -STREAM_PROGRAM_DATA:
126        return "contPROG-DATA";
127     default:
128        sprintf(buf, "%d", stream);
129        return buf;     
130     }
131 }
132
133 /* 
134  * Return a new record entity
135  */
136 DEV_RECORD *new_record(void)
137 {
138    DEV_RECORD *rec;
139
140    rec = (DEV_RECORD *) get_memory(sizeof(DEV_RECORD));
141    memset(rec, 0, sizeof(DEV_RECORD));
142    rec->data = get_pool_memory(PM_MESSAGE);
143    return rec;
144 }
145
146 /*
147  * Free the record entity 
148  *
149  */
150 void free_record(DEV_RECORD *rec) 
151 {
152    Dmsg0(150, "Enter free_record.\n");
153    if (rec->data) {
154       free_pool_memory(rec->data);
155    }
156    Dmsg0(150, "Data buf is freed.\n");
157    free_pool_memory((POOLMEM *)rec);
158    Dmsg0(150, "Leave free_record.\n");
159
160
161
162 /*
163  * Write a Record to the block
164  *
165  *  Returns: 0 on failure (none or partially written)
166  *           1 on success (all bytes written)
167  *
168  *  and remainder returned in packet.
169  *
170  *  We require enough room for the header, and we deal with
171  *  two special cases. 1. Only part of the record may have
172  *  been transferred the last time (when remainder is
173  *  non-zero), and 2. The remaining bytes to write may not
174  *  all fit into the block.
175  */
176 int write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
177 {
178    ser_declare;
179    uint32_t remlen;
180
181    sm_check(__FILE__, __LINE__, False);
182    remlen = block->buf_len - block->binbuf;
183
184    ASSERT(block->binbuf == (uint32_t) (block->bufp - block->buf));
185    ASSERT(remlen >= 0);
186
187    Dmsg6(190, "write_record_to_block() FI=%s SessId=%d Strm=%s len=%d\n\
188 rem=%d remainder=%d\n",
189       FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
190       stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
191       remlen, rec->remainder);
192
193    /*
194     * If rec->remainder is non-zero, we have been called a
195     *  second (or subsequent) time to finish writing a record
196     *  that did not previously fit into the block.
197     */
198    if (rec->remainder == 0) {
199       /* Require enough room to write a full header */
200       if (remlen >= WRITE_RECHDR_LENGTH) {
201          ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
202          if (BLOCK_VER == 1) {
203          ser_uint32(rec->VolSessionId);
204          ser_uint32(rec->VolSessionTime);
205          } else {
206             block->VolSessionId = rec->VolSessionId;
207             block->VolSessionTime = rec->VolSessionTime;
208          }
209          ser_int32(rec->FileIndex);
210          ser_int32(rec->Stream);
211          ser_uint32(rec->data_len);
212
213          block->bufp += WRITE_RECHDR_LENGTH;
214          block->binbuf += WRITE_RECHDR_LENGTH;
215          remlen -= WRITE_RECHDR_LENGTH;
216          rec->remainder = rec->data_len;
217       } else {
218          rec->remainder = rec->data_len + WRITE_RECHDR_LENGTH;
219          sm_check(__FILE__, __LINE__, False);
220          return 0;
221       }
222    } else {
223       /* 
224        * We are here to write unwritten bytes from a previous
225        * time. Presumably we have a new buffer (possibly 
226        * containing a volume label), so the new header 
227        * should be able to fit in the block -- otherwise we have
228        * an error.  Note, we have to continue splitting the
229        * data record if it is longer than the block.
230        * 
231        * First, write the header, then write as much as 
232        * possible of the data record.
233        *
234        * Every time we write a header and it is a continuation
235        * of a previous partially written record, we store the
236        * Stream as -Stream in the record header.
237        */
238       ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
239       if (BLOCK_VER == 1) {
240       ser_uint32(rec->VolSessionId);
241       ser_uint32(rec->VolSessionTime);
242       } else {
243          block->VolSessionId = rec->VolSessionId;
244          block->VolSessionTime = rec->VolSessionTime;
245       }
246       ser_int32(rec->FileIndex);
247       if (rec->remainder > rec->data_len) {
248          ser_int32(rec->Stream);      /* normal full header */
249          ser_uint32(rec->data_len);
250          rec->remainder = rec->data_len; /* must still do data record */
251       } else {
252          ser_int32(-rec->Stream);     /* mark this as a continuation record */
253          ser_uint32(rec->remainder);  /* bytes to do */
254       }
255
256       /* Require enough room to write a full header */
257       ASSERT(remlen >= WRITE_RECHDR_LENGTH);
258
259       block->bufp += WRITE_RECHDR_LENGTH;
260       block->binbuf += WRITE_RECHDR_LENGTH;
261       remlen -= WRITE_RECHDR_LENGTH;
262    }
263    if (remlen == 0) {
264       sm_check(__FILE__, __LINE__, False);
265       return 0;                       /* partial transfer */
266    }
267
268    /*
269     * Now deal with data record.
270     * Part of it may have already been transferred, and we 
271     * may not have enough room to transfer the whole this time.
272     */
273    if (rec->remainder > 0) {
274       /* Write as much of data as possible */
275       if (remlen >= rec->remainder) {
276          memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
277                 rec->remainder);
278          block->bufp += rec->remainder;
279          block->binbuf += rec->remainder;
280       } else {
281          memcpy(block->bufp, rec->data+rec->data_len-rec->remainder, 
282                 remlen);
283 #ifdef SMCHECK
284          if (!sm_check_rtn(__FILE__, __LINE__, False)) {
285             /* We damaged a buffer */
286             Dmsg6(0, "Damaged block FI=%s SessId=%d Strm=%s len=%d\n\
287 rem=%d remainder=%d\n",
288                FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
289                stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
290                remlen, rec->remainder);
291             Dmsg5(0, "Damaged block: bufp=%x binbuf=%d buf_len=%d rem=%d moved=%d\n",
292                block->bufp, block->binbuf, block->buf_len, block->buf_len-block->binbuf,
293                remlen);
294             Dmsg2(0, "Damaged block: buf=%x binbuffrombuf=%d \n",
295                block->buf, block->bufp-block->buf);
296
297                Emsg0(M_ABORT, 0, "Damaged buffer\n");
298          }
299 #endif
300
301          block->bufp += remlen;
302          block->binbuf += remlen;
303          rec->remainder -= remlen;
304          return 0;                    /* did partial transfer */
305       }
306    }
307    rec->remainder = 0;                /* did whole transfer */
308    sm_check(__FILE__, __LINE__, False);
309    return 1;
310 }
311
312
313 /*
314  * Test if we can write whole record to the block
315  *
316  *  Returns: 0 on failure 
317  *           1 on success (all bytes can be written)
318  */
319 int can_write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
320 {
321    uint32_t remlen;
322
323    remlen = block->buf_len - block->binbuf;
324    if (rec->remainder == 0) {
325       if (remlen >= WRITE_RECHDR_LENGTH) {
326          remlen -= WRITE_RECHDR_LENGTH;
327          rec->remainder = rec->data_len;
328       } else {
329          return 0;
330       }
331    } else {
332       return 0;
333    }
334    if (rec->remainder > 0 && remlen < rec->remainder) {
335       return 0;
336    }
337    return 1;
338 }
339
340
341 /*
342  * Read a Record from the block
343  *  Returns: 0 if nothing read or if the continuation record does not match.
344  *             In both of these cases, a block read must be done.
345  *           1 if at least the record header was read, this 
346  *             routine may have to be called again with a new
347  *             block if the entire record was not read.
348  */
349 int read_record_from_block(DEV_BLOCK *block, DEV_RECORD *rec)
350 {
351    ser_declare;
352    uint32_t remlen;
353    uint32_t VolSessionId;
354    uint32_t VolSessionTime;
355    int32_t  FileIndex;
356    int32_t  Stream;
357    uint32_t data_bytes;
358    uint32_t rhl;
359
360    remlen = block->binbuf;
361    rec->Block = block->BlockNumber;
362
363    /* Clear state flags */
364    rec->state = 0;
365
366    /* 
367     * Get the header. There is always a full header,
368     * otherwise we find it in the next block.
369     */
370    Dmsg3(100, "Block=%d Ver=%d size=%u\n", block->BlockNumber, block->BlockVer,
371          block->block_len);
372    if (block->BlockVer == 1) {
373       rhl = RECHDR1_LENGTH;
374    } else {
375       rhl = RECHDR2_LENGTH;
376    }
377    if (remlen >= rhl) {
378       Dmsg4(90, "Enter read_record_block: remlen=%d data_len=%d rem=%d blkver=%d\n", 
379             remlen, rec->data_len, rec->remainder, block->BlockVer);
380
381       unser_begin(block->bufp, WRITE_RECHDR_LENGTH);
382       if (block->BlockVer == 1) {
383       unser_uint32(VolSessionId);
384       unser_uint32(VolSessionTime);
385       } else {
386          VolSessionId = block->VolSessionId;
387          VolSessionTime = block->VolSessionTime;
388       }
389       unser_int32(FileIndex);
390       unser_int32(Stream);
391       unser_uint32(data_bytes);
392
393       block->bufp += rhl;
394       block->binbuf -= rhl;
395       remlen -= rhl;
396
397       /* If we are looking for more (remainder!=0), we reject anything
398        *  where the VolSessionId and VolSessionTime don't agree
399        */
400       if (rec->remainder && (rec->VolSessionId != VolSessionId || 
401                              rec->VolSessionTime != VolSessionTime)) {
402          rec->state |= REC_NO_MATCH;
403          return 0;                 /* This is from some other Session */
404       }
405
406       /* if Stream is negative, it means that this is a continuation
407        * of a previous partially written record.
408        */
409       if (Stream < 0) {               /* continuation record? */
410          Dmsg1(500, "Got negative Stream => continuation. remainder=%d\n", 
411             rec->remainder);
412          rec->state |= REC_CONTINUATION;
413          if (!rec->remainder) {       /* if we didn't read previously */
414             rec->data_len = 0;        /* return data as if no continuation */
415          } else if (rec->Stream != -Stream) {
416             rec->state |= REC_NO_MATCH;
417             return 0;                 /* This is from some other Session */
418          }
419          rec->Stream = -Stream;       /* set correct Stream */
420       } else {                        /* Regular record */
421          rec->Stream = Stream;
422          rec->data_len = 0;           /* transfer to beginning of data */
423       }
424       rec->VolSessionId = VolSessionId;
425       rec->VolSessionTime = VolSessionTime;
426       rec->FileIndex = FileIndex;
427
428       Dmsg6(100, "rd_rec_blk() got FI=%s SessId=%d Strm=%s len=%u\n\
429 remlen=%d data_len=%d\n",
430          FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
431          stream_to_ascii(rec->Stream, rec->FileIndex), data_bytes, remlen, 
432          rec->data_len);
433    } else {
434       /*    
435        * No more records in this block because the number   
436        * of remaining bytes are less than a record header 
437        * length, so return empty handed, but indicate that
438        * he must read again. By returning, we allow the
439        * higher level routine to fetch the next block and
440        * then reread.
441        */
442       Dmsg0(90, "read_record_block: nothing\n");
443 #ifdef xxx
444       if (!rec->remainder) {
445          rec->remainder = 1;          /* set to expect continuation */
446          rec->data_len = 0;           /* no data transferred */
447       }
448 #endif
449       rec->state |= (REC_NO_HEADER | REC_BLOCK_EMPTY);
450       return 0;
451    }
452
453    ASSERT(data_bytes < MAX_BLOCK_LENGTH);       /* temp sanity check */
454
455    rec->data = check_pool_memory_size(rec->data, rec->data_len+data_bytes);
456    
457    /*
458     * At this point, we have read the header, now we
459     * must transfer as much of the data record as 
460     * possible taking into account: 1. A partial
461     * data record may have previously been transferred,
462     * 2. The current block may not contain the whole data
463     * record.
464     */
465    if (remlen >= data_bytes) {
466       /* Got whole record */
467       memcpy(rec->data+rec->data_len, block->bufp, data_bytes);
468       block->bufp += data_bytes;
469       block->binbuf -= data_bytes;
470       rec->data_len += data_bytes;
471    } else {
472       /* Partial record */
473       memcpy(rec->data+rec->data_len, block->bufp, remlen);
474       block->bufp += remlen;
475       block->binbuf -= remlen;
476       rec->data_len += remlen;
477       rec->remainder = 1;             /* partial record transferred */
478       Dmsg1(90, "read_record_block: partial xfered=%d\n", rec->data_len);
479       rec->state |= (REC_PARTIAL_RECORD | REC_BLOCK_EMPTY);
480       return 1;
481    }
482    rec->remainder = 0;
483    Dmsg4(90, "Rtn full rd_rec_blk FI=%s SessId=%d Strm=%s len=%d\n",
484       FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
485       stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
486    return 1;                          /* transferred full record */
487 }