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