]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/block.c
See kes02Dec02
[bacula/bacula] / bacula / src / stored / block.c
1 /*
2  *
3  *   block.c -- tape block handling functions
4  *
5  *              Kern Sibbald, March 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  * Dump the block header, then walk through
39  * the block printing out the record headers.
40  */
41 void dump_block(DEV_BLOCK *b, char *msg)
42 {
43    ser_declare;
44    char *p;
45    char Id[BLKHDR_ID_LENGTH+1];
46    uint32_t CheckSum, BlockCheckSum;
47    uint32_t block_len;
48    uint32_t BlockNumber;
49    uint32_t VolSessionId, VolSessionTime, data_len;
50    int32_t  FileIndex;
51    int32_t  Stream;
52    int bhl, rhl;
53
54    unser_begin(b->buf, BLKHDR1_LENGTH);
55    unser_uint32(CheckSum);
56    unser_uint32(block_len);
57    unser_uint32(BlockNumber);
58    unser_bytes(Id, BLKHDR_ID_LENGTH);
59    ASSERT(unser_length(b->buf) == BLKHDR1_LENGTH);
60    Id[BLKHDR_ID_LENGTH] = 0;
61    if (Id[3] == '2') {
62       unser_uint32(VolSessionId);
63       unser_uint32(VolSessionTime);
64       bhl = BLKHDR2_LENGTH;
65       rhl = RECHDR2_LENGTH;
66    } else {
67       VolSessionId = VolSessionTime = 0;
68       bhl = BLKHDR1_LENGTH;
69       rhl = RECHDR1_LENGTH;
70    }
71
72    if (block_len > 100000) {
73       Dmsg3(20, "Dump block %s 0x%x blocksize too big %u\n", msg, b, block_len);
74       return;
75    }
76
77    BlockCheckSum = bcrc32((uint8_t *)b->buf+BLKHDR_CS_LENGTH,
78                          block_len-BLKHDR_CS_LENGTH);
79    Pmsg6(000, "Dump block %s %x: size=%d BlkNum=%d\n\
80                Hdrcksum=%x cksum=%x\n",
81       msg, b, block_len, BlockNumber, CheckSum, BlockCheckSum);
82    p = b->buf + bhl;
83    while (p < (b->buf + block_len+WRITE_RECHDR_LENGTH)) { 
84       unser_begin(p, WRITE_RECHDR_LENGTH);
85       if (rhl == RECHDR1_LENGTH) {
86          unser_uint32(VolSessionId);
87          unser_uint32(VolSessionTime);
88       }
89       unser_int32(FileIndex);
90       unser_int32(Stream);
91       unser_uint32(data_len);
92       Pmsg6(000, "   Rec: VId=%u VT=%u FI=%s Strm=%s len=%d p=%x\n",
93            VolSessionId, VolSessionTime, FI_to_ascii(FileIndex), 
94            stream_to_ascii(Stream, FileIndex), data_len, p);
95       p += data_len + rhl;
96   }
97 }
98     
99 /*
100  * Create a new block structure.                           
101  * We pass device so that the block can inherit the
102  * min and max block sizes.
103  */
104 DEV_BLOCK *new_block(DEVICE *dev)
105 {
106    DEV_BLOCK *block = (DEV_BLOCK *)get_memory(sizeof(DEV_BLOCK));
107
108    memset(block, 0, sizeof(DEV_BLOCK));
109
110    /* If the user has specified a max_block_size, use it as the default */
111    if (dev->max_block_size == 0) {
112       block->buf_len = DEFAULT_BLOCK_SIZE;
113    } else {
114       block->buf_len = dev->max_block_size;
115    }
116    block->block_len = block->buf_len;  /* default block size */
117    block->buf = get_memory(block->buf_len); 
118    if (block->buf == NULL) {
119       Mmsg0(&dev->errmsg, _("Unable to malloc block buffer.\n"));
120       Emsg0(M_FATAL, 0, dev->errmsg);
121       return NULL;
122    }
123    empty_block(block);
124    block->BlockVer = BLOCK_VER;       /* default write version */
125    Dmsg1(90, "Returning new block=%x\n", block);
126    return block;
127 }
128
129 /*
130  * Free block 
131  */
132 void free_block(DEV_BLOCK *block)
133 {
134    Dmsg1(199, "free_block buffer %x\n", block->buf);
135    free_memory(block->buf);
136    Dmsg1(199, "free_block block %x\n", block);
137    free_memory((POOLMEM *)block);
138 }
139
140 /* Empty the block -- for writing */
141 void empty_block(DEV_BLOCK *block)
142 {
143    block->binbuf = WRITE_BLKHDR_LENGTH;
144    block->bufp = block->buf + block->binbuf;
145    block->read_len = 0;
146    block->failed_write = FALSE;
147 }
148
149 /*
150  * Create block header just before write. The space
151  * in the buffer should have already been reserved by
152  * init_block.
153  */
154 static void ser_block_header(DEV_BLOCK *block)
155 {
156    ser_declare;
157    uint32_t CheckSum = 0;
158    uint32_t block_len = block->binbuf;
159    
160    Dmsg1(190, "ser_block_header: block_len=%d\n", block_len);
161    ser_begin(block->buf, BLKHDR2_LENGTH);
162    ser_uint32(CheckSum);
163    ser_uint32(block_len);
164    ser_uint32(block->BlockNumber);
165    ser_bytes(WRITE_BLKHDR_ID, BLKHDR_ID_LENGTH);
166    if (BLOCK_VER >= 2) {
167       ser_uint32(block->VolSessionId);
168       ser_uint32(block->VolSessionTime);
169    }
170
171    /* Checksum whole block except for the checksum */
172    CheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH, 
173                  block_len-BLKHDR_CS_LENGTH);
174    Dmsg1(190, "ser_bloc_header: checksum=%x\n", CheckSum);
175    ser_begin(block->buf, BLKHDR2_LENGTH);
176    ser_uint32(CheckSum);              /* now add checksum to block header */
177 }
178
179 /*
180  * Unserialized the block header for reading block.
181  *  This includes setting all the buffer pointers correctly.
182  *
183  *  Returns: 0 on failure (not a block)
184  *           1 on success
185  */
186 static int unser_block_header(DEVICE *dev, DEV_BLOCK *block)
187 {
188    ser_declare;
189    char Id[BLKHDR_ID_LENGTH+1];
190    uint32_t CheckSum, BlockCheckSum;
191    uint32_t block_len;
192    uint32_t EndBlock;
193    uint32_t BlockNumber;
194    int bhl;
195
196    unser_begin(block->buf, BLKHDR_LENGTH);
197    unser_uint32(CheckSum);
198    unser_uint32(block_len);
199    unser_uint32(BlockNumber);
200    unser_bytes(Id, BLKHDR_ID_LENGTH);
201    ASSERT(unser_length(block->buf) == BLKHDR1_LENGTH);
202
203    Id[BLKHDR_ID_LENGTH] = 0;
204    if (Id[3] == '1') {
205       bhl = BLKHDR1_LENGTH;
206       block->BlockVer = 1;
207       block->bufp = block->buf + bhl;
208       if (strncmp(Id, BLKHDR1_ID, BLKHDR_ID_LENGTH) != 0) {
209          Mmsg2(&dev->errmsg, _("Buffer ID error. Wanted: %s, got %s. Buffer discarded.\n"),
210             BLKHDR1_ID, Id);
211          Emsg0(M_ERROR, 0, dev->errmsg);
212          return 0;
213       }
214    } else if (Id[3] == '2') {
215       unser_uint32(block->VolSessionId);
216       unser_uint32(block->VolSessionTime);
217       bhl = BLKHDR2_LENGTH;
218       block->BlockVer = 2;
219       block->bufp = block->buf + bhl;
220       if (strncmp(Id, BLKHDR2_ID, BLKHDR_ID_LENGTH) != 0) {
221          Mmsg2(&dev->errmsg, _("Buffer ID error. Wanted: %s, got %s. Buffer discarded.\n"),
222             BLKHDR2_ID, Id);
223          Emsg0(M_ERROR, 0, dev->errmsg);
224          return 0;
225       }
226    } else {
227       Mmsg1(&dev->errmsg, _("Expected block-id BB01 or BB02, got %s. Buffer discarded.\n"), Id);
228       Emsg0(M_ERROR, 0, dev->errmsg);
229       return 0;
230    }
231
232    /* Sanity check */
233    if (block_len > MAX_BLOCK_LENGTH) {
234       Mmsg1(&dev->errmsg,  _("Block length %u is insane (too large), probably due to a bad archive.\n"),
235          block_len);
236       Emsg0(M_ERROR, 0, dev->errmsg);
237       return 0;
238    }
239
240    Dmsg1(190, "unser_block_header block_len=%d\n", block_len);
241    /* Find end of block or end of buffer whichever is smaller */
242    if (block_len > block->read_len) {
243       EndBlock = block->read_len;
244    } else {
245       EndBlock = block_len;
246    }
247    block->binbuf = EndBlock - bhl;
248    block->block_len = block_len;
249    block->BlockNumber = BlockNumber;
250    Dmsg3(190, "Read binbuf = %d %d block_len=%d\n", block->binbuf,
251       bhl, block_len);
252    if (block_len <= block->read_len) {
253       BlockCheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH,
254                          block_len-BLKHDR_CS_LENGTH);
255       if (BlockCheckSum != CheckSum) {
256          Dmsg2(00, "Block checksum mismatch: calc=%x blk=%x\n", BlockCheckSum,
257             CheckSum);
258          Mmsg2(&dev->errmsg, _("Block checksum mismatch: calc=%x blk=%x\n"), BlockCheckSum,
259             CheckSum);
260          return 0;
261       }
262    }
263    return 1;
264 }
265
266 /*  
267  * Write a block to the device, with locking and unlocking
268  *
269  * Returns: 1 on success
270  *        : 0 on failure
271  *
272  */
273 int write_block_to_device(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
274 {
275    int stat = 1;
276    lock_device(dev);
277    if (!write_block_to_dev(dev, block)) {
278        stat = fixup_device_block_write_error(jcr, dev, block);
279    }
280    unlock_device(dev);
281    return stat;
282 }
283
284 /*
285  * Write a block to the device 
286  *
287  *  Returns: 1 on success or EOT
288  *           0 on hard error
289  */
290 int write_block_to_dev(DEVICE *dev, DEV_BLOCK *block)
291 {
292    size_t stat = 0;
293    uint32_t wlen;                     /* length to write */
294    int hit_max1, hit_max2;
295
296 #ifdef NO_TAPE_WRITE_TEST
297    empty_block(block);
298    return 1;
299 #endif
300    ASSERT(block->binbuf == ((uint32_t) (block->bufp - block->buf)));
301
302    /* dump_block(block, "before write"); */
303    if (dev->state & ST_WEOT) {
304       Dmsg0(100, "return write_block_to_dev with ST_WEOT\n");
305       return 0;
306    }
307    wlen = block->binbuf;
308    if (wlen <= WRITE_BLKHDR_LENGTH) {  /* Does block have data in it? */
309       Dmsg0(100, "return write_block_to_dev no data to write\n");
310       return 1;
311    }
312    /* 
313     * Clear to the end of the buffer if it is not full,
314     *  and on tape devices, apply min and fixed blocking.
315     */
316    if (wlen != block->buf_len) {
317       uint32_t blen;                  /* current buffer length */
318
319       Dmsg2(200, "binbuf=%d buf_len=%d\n", block->binbuf, block->buf_len);
320       blen = wlen;
321
322       /* Adjust write size to min/max for tapes only */
323       if (dev->state & ST_TAPE) {
324          if (wlen < dev->min_block_size) {
325             wlen =  ((dev->min_block_size + TAPE_BSIZE - 1) / TAPE_BSIZE) * TAPE_BSIZE;
326          }
327          /* check for fixed block size */
328          if (dev->min_block_size == dev->max_block_size) {
329             wlen = block->buf_len;    /* fixed block size already rounded */
330          }
331       }
332       if (wlen-blen > 0) {
333          memset(block->bufp, 0, wlen-blen); /* clear garbage */
334       }
335    }  
336
337    ser_block_header(block);
338
339    /* Limit maximum Volume size to value specified by user */
340    hit_max1 = (dev->max_volume_size > 0) &&
341        ((dev->VolCatInfo.VolCatBytes + block->binbuf)) >= dev->max_volume_size;
342    hit_max2 = (dev->VolCatInfo.VolCatMaxBytes > 0) &&
343        ((dev->VolCatInfo.VolCatBytes + block->binbuf)) >= dev->VolCatInfo.VolCatMaxBytes;
344    if (hit_max1 || hit_max2) {   
345       char ed1[50];
346       dev->state |= ST_WEOT;
347       Dmsg0(10, "==== Output bytes Triggered medium max capacity.\n");
348       if (hit_max1) {
349          Mmsg2(&dev->errmsg, _("Max. Volume capacity %s exceeded on device %s.\n"),
350             edit_uint64(dev->max_volume_size, ed1),  dev->dev_name);
351       } else {
352          Mmsg2(&dev->errmsg, _("Max. Volume capacity %s exceeded on device %s.\n"),
353             edit_uint64(dev->VolCatInfo.VolCatMaxBytes, ed1),  dev->dev_name);
354       }
355       block->failed_write = TRUE;
356 /* ****FIXME**** write EOD record here */
357       weof_dev(dev, 1);               /* end the tape */
358       weof_dev(dev, 1);               /* write second eof */
359       return 0;
360    }
361
362    dev->VolCatInfo.VolCatWrites++;
363    Dmsg1(500, "Write block of %u bytes\n", wlen);      
364    if ((uint32_t) (stat=write(dev->fd, block->buf, (size_t)wlen)) != wlen) {
365       /* We should check for errno == ENOSPC, BUT many 
366        * devices simply report EIO when it is full.
367        * with a little more thought we may be able to check
368        * capacity and distinguish real errors and EOT
369        * conditions.  In any case, we probably want to
370        * simulate an End of Medium.
371        */
372 /***FIXME**** if we wrote a partial record, back up over it */
373       dev->state |= ST_EOF | ST_EOT | ST_WEOT;
374       clrerror_dev(dev, -1);
375
376       if (dev->dev_errno == 0) {
377          dev->dev_errno = ENOSPC;        /* out of space */
378       }
379
380       Dmsg4(10, "=== Write error. size=%u rtn=%d  errno=%d: ERR=%s\n", 
381          wlen, stat, dev->dev_errno, strerror(dev->dev_errno));
382
383       Mmsg4(&dev->errmsg, _("Write error on device %s. Write of %u bytes got %d. ERR=%s.\n"), 
384          dev->dev_name, wlen, stat, strerror(dev->dev_errno));
385       block->failed_write = TRUE;
386       weof_dev(dev, 1);               /* end the tape */
387       weof_dev(dev, 1);               /* write second eof */
388       return 0;
389    }
390    dev->VolCatInfo.VolCatBytes += block->binbuf;
391    dev->VolCatInfo.VolCatBlocks++;   
392    dev->file_addr += wlen;
393
394    /* Limit maximum File size on volume to user specified value */
395    if (dev->state & ST_TAPE) {
396       if ((dev->max_file_size > 0) && dev->file_addr >= dev->max_file_size) {
397          weof_dev(dev, 1);               /* write eof */
398       }
399    }
400
401    dev->block_num++;
402    block->BlockNumber++;
403    Dmsg2(190, "write_block: wrote block %d bytes=%d\n", dev->block_num,
404       wlen);
405    empty_block(block);
406    return 1;
407 }
408
409 /*  
410  * Read block with locking
411  *
412  */
413 int read_block_from_device(DEVICE *dev, DEV_BLOCK *block)
414 {
415    int stat;
416    Dmsg0(90, "Enter read_block_from_device\n");
417    lock_device(dev);
418    stat = read_block_from_dev(dev, block);
419    unlock_device(dev);
420    Dmsg0(90, "Leave read_block_from_device\n");
421    return stat;
422 }
423
424 /*
425  * Read the next block into the block structure and unserialize
426  *  the block header.  For a file, the block may be partially
427  *  or completely in the current buffer.
428  */
429 int read_block_from_dev(DEVICE *dev, DEV_BLOCK *block)
430 {
431    size_t stat;
432    int looping;
433
434    looping = 0;
435    Dmsg1(100, "Full read() in read_block_from_device() len=%d\n",
436          block->buf_len);
437 reread:
438    if (looping > 1) {
439       Mmsg1(&dev->errmsg, _("Block buffer size looping problem on device %s\n"),
440          dev->dev_name);
441       block->read_len = 0;
442       return 0;
443    }
444    if ((stat=read(dev->fd, block->buf, (size_t)block->buf_len)) < 0) {
445       Dmsg1(90, "Read device got: ERR=%s\n", strerror(errno));
446       clrerror_dev(dev, -1);
447       block->read_len = 0;
448       Mmsg2(&dev->errmsg, _("Read error on device %s. ERR=%s.\n"), 
449          dev->dev_name, strerror(dev->dev_errno));
450       return 0;
451    }
452    Dmsg1(90, "Read device got %d bytes\n", stat);
453    if (stat == 0) {             /* Got EOF ! */
454       dev->block_num = block->read_len = 0;
455       Mmsg1(&dev->errmsg, _("Read zero bytes on device %s.\n"), dev->dev_name);
456       if (dev->state & ST_EOF) { /* EOF alread read? */
457          dev->state |= ST_EOT;  /* yes, 2 EOFs => EOT */
458          return 0;
459       }
460       dev->file++;              /* increment file */
461       dev->state |= ST_EOF;     /* set EOF read */
462       return 0;                 /* return eof */
463    }
464    /* Continue here for successful read */
465    block->read_len = stat;      /* save length read */
466    if (block->read_len < BLKHDR2_LENGTH) {
467       Mmsg2(&dev->errmsg, _("Very short block of %d bytes on device %s discarded.\n"), 
468          block->read_len, dev->dev_name);
469       dev->state |= ST_SHORT;   /* set short block */
470       block->read_len = block->binbuf = 0;
471       return 0;                 /* return error */
472    }  
473
474    if (!unser_block_header(dev, block)) {
475       return 0;
476    }
477
478    /*
479     * If the block is bigger than the buffer, we reposition for
480     *  re-reading the block, allocate a buffer of the correct size,
481     *  and go re-read.
482     */
483    if (block->block_len > block->buf_len) {
484       Mmsg2(&dev->errmsg,  _("Block length %u is greater than buffer %u. Attempting recovery.\n"),
485          block->block_len, block->buf_len);
486       Emsg0(M_WARNING, 0, dev->errmsg);
487       Dmsg1(000, "%s", dev->errmsg);
488       /* Attempt to reposition to re-read the block */
489       if (dev->state & ST_TAPE) {
490          Dmsg0(000, "Backspace record for reread.\n");
491          if (bsf_dev(dev, 1) != 0) {
492             Emsg0(M_ERROR, 0, dev->errmsg);
493             return 0;
494          }
495       } else {
496          Dmsg0(000, "Seek to beginning of block for reread.\n");
497          off_t pos = lseek(dev->fd, (off_t)0, SEEK_CUR); /* get curr pos */
498          pos -= block->read_len;
499          lseek(dev->fd, pos, SEEK_SET);   
500       }
501       Mmsg1(&dev->errmsg, _("Resetting buffer size to %u bytes.\n"), block->block_len);
502       Emsg0(M_WARNING, 0, dev->errmsg);
503       Dmsg1(000, "%s", dev->errmsg);
504       /* Set new block length */
505       dev->max_block_size = block->block_len;
506       block->buf_len = block->block_len;
507       free_memory(block->buf);
508       block->buf = get_memory(block->buf_len);
509       empty_block(block);
510       looping++;
511       goto reread;                    /* re-read block with correct block size */
512    }
513
514    if (block->block_len > block->read_len) {
515       Mmsg2(&dev->errmsg, _("Short block of %d bytes on device %s discarded.\n"), 
516          block->read_len, dev->dev_name);
517       dev->state |= ST_SHORT;   /* set short block */
518       block->read_len = block->binbuf = 0;
519       return 0;                 /* return error */
520    }  
521
522    dev->state &= ~(ST_EOF|ST_SHORT); /* clear EOF and short block */
523    dev->block_num++;
524
525    /*
526     * If we read a short block on disk,
527     * seek to beginning of next block. This saves us
528     * from shuffling blocks around in the buffer. Take a
529     * look at this from an efficiency stand point later, but
530     * it should only happen once at the end of each job.
531     *
532     * I've been lseek()ing negative relative to SEEK_CUR for 30
533     *   years now. However, it seems that with the new off_t definition,
534     *   it is not possible to seek negative amounts, so we use two
535     *   lseek(). One to get the position, then the second to do an
536     *   absolute positioning -- so much for efficiency.  KES Sep 02.
537     */
538    Dmsg0(200, "At end of read block\n");
539    if (block->read_len > block->block_len && !(dev->state & ST_TAPE)) {
540       off_t pos = lseek(dev->fd, (off_t)0, SEEK_CUR); /* get curr pos */
541       pos -= (block->read_len - block->block_len);
542       lseek(dev->fd, pos, SEEK_SET);   
543       Dmsg2(100, "Did lseek blk_size=%d rdlen=%d\n", block->block_len,
544             block->read_len);
545    }
546    Dmsg2(200, "Exit read_block read_len=%d block_len=%d\n",
547       block->read_len, block->block_len);
548    return 1;
549 }