]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/record.c
9a68849af2640eee8a950ba77f67a55bbe626239
[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    remlen = block->buf_len - block->binbuf;
182
183    ASSERT(block->binbuf == (uint32_t) (block->bufp - block->buf));
184    ASSERT(remlen >= 0);
185
186    Dmsg6(190, "write_record_to_block() FI=%s SessId=%d Strm=%s len=%d\n\
187 rem=%d remainder=%d\n",
188       FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
189       stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
190       remlen, rec->remainder);
191
192    /*
193     * If rec->remainder is non-zero, we have been called a
194     *  second (or subsequent) time to finish writing a record
195     *  that did not previously fit into the block.
196     */
197    if (rec->remainder == 0) {
198       /* Require enough room to write a full header */
199       if (remlen >= WRITE_RECHDR_LENGTH) {
200          ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
201          if (BLOCK_VER == 1) {
202          ser_uint32(rec->VolSessionId);
203          ser_uint32(rec->VolSessionTime);
204          } else {
205             block->VolSessionId = rec->VolSessionId;
206             block->VolSessionTime = rec->VolSessionTime;
207          }
208          ser_int32(rec->FileIndex);
209          ser_int32(rec->Stream);
210          ser_uint32(rec->data_len);
211
212          block->bufp += WRITE_RECHDR_LENGTH;
213          block->binbuf += WRITE_RECHDR_LENGTH;
214          remlen -= WRITE_RECHDR_LENGTH;
215          rec->remainder = rec->data_len;
216       } else {
217          rec->remainder = rec->data_len + WRITE_RECHDR_LENGTH;
218          return 0;
219       }
220    } else {
221       /* 
222        * We are here to write unwritten bytes from a previous
223        * time. Presumably we have a new buffer (possibly 
224        * containing a volume label), so the new header 
225        * should be able to fit in the block -- otherwise we have
226        * an error.  Note, we have to continue splitting the
227        * data record if it is longer than the block.
228        * 
229        * First, write the header, then write as much as 
230        * possible of the data record.
231        *
232        * Every time we write a header and it is a continuation
233        * of a previous partially written record, we store the
234        * Stream as -Stream in the record header.
235        */
236       ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
237       if (BLOCK_VER == 1) {
238       ser_uint32(rec->VolSessionId);
239       ser_uint32(rec->VolSessionTime);
240       } else {
241          block->VolSessionId = rec->VolSessionId;
242          block->VolSessionTime = rec->VolSessionTime;
243       }
244       ser_int32(rec->FileIndex);
245       if (rec->remainder > rec->data_len) {
246          ser_int32(rec->Stream);      /* normal full header */
247          ser_uint32(rec->data_len);
248          rec->remainder = rec->data_len; /* must still do data record */
249       } else {
250          ser_int32(-rec->Stream);     /* mark this as a continuation record */
251          ser_uint32(rec->remainder);  /* bytes to do */
252       }
253
254       /* Require enough room to write a full header */
255       ASSERT(remlen >= WRITE_RECHDR_LENGTH);
256
257       block->bufp += WRITE_RECHDR_LENGTH;
258       block->binbuf += WRITE_RECHDR_LENGTH;
259       remlen -= WRITE_RECHDR_LENGTH;
260    }
261    if (remlen == 0) {
262       return 0;                       /* partial transfer */
263    }
264
265    /*
266     * Now deal with data record.
267     * Part of it may have already been transferred, and we 
268     * may not have enough room to transfer the whole this time.
269     */
270    if (rec->remainder > 0) {
271       /* Write as much of data as possible */
272       if (remlen >= rec->remainder) {
273          memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
274                 rec->remainder);
275          block->bufp += rec->remainder;
276          block->binbuf += rec->remainder;
277       } else {
278          memcpy(block->bufp, rec->data+rec->data_len-rec->remainder, 
279                 remlen);
280 #ifdef xxxxxSMCHECK
281          if (!sm_check_rtn(__FILE__, __LINE__, False)) {
282             /* We damaged a buffer */
283             Dmsg6(0, "Damaged block FI=%s SessId=%d Strm=%s len=%d\n\
284 rem=%d remainder=%d\n",
285                FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
286                stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
287                remlen, rec->remainder);
288             Dmsg5(0, "Damaged block: bufp=%x binbuf=%d buf_len=%d rem=%d moved=%d\n",
289                block->bufp, block->binbuf, block->buf_len, block->buf_len-block->binbuf,
290                remlen);
291             Dmsg2(0, "Damaged block: buf=%x binbuffrombuf=%d \n",
292                block->buf, block->bufp-block->buf);
293
294                Emsg0(M_ABORT, 0, "Damaged buffer\n");
295          }
296 #endif
297
298          block->bufp += remlen;
299          block->binbuf += remlen;
300          rec->remainder -= remlen;
301          return 0;                    /* did partial transfer */
302       }
303    }
304    rec->remainder = 0;                /* did whole transfer */
305    return 1;
306 }
307
308
309 /*
310  * Test if we can write whole record to the block
311  *
312  *  Returns: 0 on failure 
313  *           1 on success (all bytes can be written)
314  */
315 int can_write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
316 {
317    uint32_t remlen;
318
319    remlen = block->buf_len - block->binbuf;
320    if (rec->remainder == 0) {
321       if (remlen >= WRITE_RECHDR_LENGTH) {
322          remlen -= WRITE_RECHDR_LENGTH;
323          rec->remainder = rec->data_len;
324       } else {
325          return 0;
326       }
327    } else {
328       return 0;
329    }
330    if (rec->remainder > 0 && remlen < rec->remainder) {
331       return 0;
332    }
333    return 1;
334 }
335
336
337 /*
338  * Read a Record from the block
339  *  Returns: 0 if nothing read or if the continuation record does not match.
340  *             In both of these cases, a block read must be done.
341  *           1 if at least the record header was read, this 
342  *             routine may have to be called again with a new
343  *             block if the entire record was not read.
344  */
345 int read_record_from_block(DEV_BLOCK *block, DEV_RECORD *rec)
346 {
347    ser_declare;
348    uint32_t remlen;
349    uint32_t VolSessionId;
350    uint32_t VolSessionTime;
351    int32_t  FileIndex;
352    int32_t  Stream;
353    uint32_t data_bytes;
354    uint32_t rhl;
355
356    remlen = block->binbuf;
357    rec->Block = block->BlockNumber;
358    rec->File = ((DEVICE *)block->dev)->file;
359
360    /* Clear state flags */       
361    rec->state = 0;
362    if (((DEVICE *)block->dev)->state & ST_TAPE) {
363       rec->state |= REC_ISTAPE;
364    }
365
366
367    /* 
368     * Get the header. There is always a full header,
369     * otherwise we find it in the next block.
370     */
371    Dmsg3(100, "Block=%d Ver=%d size=%u\n", block->BlockNumber, block->BlockVer,
372          block->block_len);
373    if (block->BlockVer == 1) {
374       rhl = RECHDR1_LENGTH;
375    } else {
376       rhl = RECHDR2_LENGTH;
377    }
378    if (remlen >= rhl) {
379       Dmsg4(90, "Enter read_record_block: remlen=%d data_len=%d rem=%d blkver=%d\n", 
380             remlen, rec->data_len, rec->remainder, block->BlockVer);
381
382       unser_begin(block->bufp, WRITE_RECHDR_LENGTH);
383       if (block->BlockVer == 1) {
384       unser_uint32(VolSessionId);
385       unser_uint32(VolSessionTime);
386       } else {
387          VolSessionId = block->VolSessionId;
388          VolSessionTime = block->VolSessionTime;
389       }
390       unser_int32(FileIndex);
391       unser_int32(Stream);
392       unser_uint32(data_bytes);
393
394       block->bufp += rhl;
395       block->binbuf -= rhl;
396       remlen -= rhl;
397
398       /* If we are looking for more (remainder!=0), we reject anything
399        *  where the VolSessionId and VolSessionTime don't agree
400        */
401       if (rec->remainder && (rec->VolSessionId != VolSessionId || 
402                              rec->VolSessionTime != VolSessionTime)) {
403          rec->state |= REC_NO_MATCH;
404          return 0;                 /* This is from some other Session */
405       }
406
407       /* if Stream is negative, it means that this is a continuation
408        * of a previous partially written record.
409        */
410       if (Stream < 0) {               /* continuation record? */
411          Dmsg1(500, "Got negative Stream => continuation. remainder=%d\n", 
412             rec->remainder);
413          rec->state |= REC_CONTINUATION;
414          if (!rec->remainder) {       /* if we didn't read previously */
415             rec->data_len = 0;        /* return data as if no continuation */
416          } else if (rec->Stream != -Stream) {
417             rec->state |= REC_NO_MATCH;
418             return 0;                 /* This is from some other Session */
419          }
420          rec->Stream = -Stream;       /* set correct Stream */
421       } else {                        /* Regular record */
422          rec->Stream = Stream;
423          rec->data_len = 0;           /* transfer to beginning of data */
424       }
425       rec->VolSessionId = VolSessionId;
426       rec->VolSessionTime = VolSessionTime;
427       rec->FileIndex = FileIndex;
428
429       Dmsg6(100, "rd_rec_blk() got FI=%s SessId=%d Strm=%s len=%u\n\
430 remlen=%d data_len=%d\n",
431          FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
432          stream_to_ascii(rec->Stream, rec->FileIndex), data_bytes, remlen, 
433          rec->data_len);
434    } else {
435       /*    
436        * No more records in this block because the number   
437        * of remaining bytes are less than a record header 
438        * length, so return empty handed, but indicate that
439        * he must read again. By returning, we allow the
440        * higher level routine to fetch the next block and
441        * then reread.
442        */
443       Dmsg0(90, "read_record_block: nothing\n");
444 #ifdef xxx
445       if (!rec->remainder) {
446          rec->remainder = 1;          /* set to expect continuation */
447          rec->data_len = 0;           /* no data transferred */
448       }
449 #endif
450       rec->state |= (REC_NO_HEADER | REC_BLOCK_EMPTY);
451       return 0;
452    }
453
454    ASSERT(data_bytes < MAX_BLOCK_LENGTH);       /* temp sanity check */
455
456    rec->data = check_pool_memory_size(rec->data, rec->data_len+data_bytes);
457    
458    /*
459     * At this point, we have read the header, now we
460     * must transfer as much of the data record as 
461     * possible taking into account: 1. A partial
462     * data record may have previously been transferred,
463     * 2. The current block may not contain the whole data
464     * record.
465     */
466    if (remlen >= data_bytes) {
467       /* Got whole record */
468       memcpy(rec->data+rec->data_len, block->bufp, data_bytes);
469       block->bufp += data_bytes;
470       block->binbuf -= data_bytes;
471       rec->data_len += data_bytes;
472    } else {
473       /* Partial record */
474       memcpy(rec->data+rec->data_len, block->bufp, remlen);
475       block->bufp += remlen;
476       block->binbuf -= remlen;
477       rec->data_len += remlen;
478       rec->remainder = 1;             /* partial record transferred */
479       Dmsg1(90, "read_record_block: partial xfered=%d\n", rec->data_len);
480       rec->state |= (REC_PARTIAL_RECORD | REC_BLOCK_EMPTY);
481       return 1;
482    }
483    rec->remainder = 0;
484    Dmsg4(90, "Rtn full rd_rec_blk FI=%s SessId=%d Strm=%s len=%d\n",
485       FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
486       stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
487    return 1;                          /* transferred full record */
488 }