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