]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/block.c
First cut 1.27 see kes23Oct02
[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 (dev->max_block_size == 0) {
111       block->buf_len = DEFAULT_BLOCK_SIZE;
112    } else {
113       block->buf_len = dev->max_block_size;
114    }
115    /* ****FIXME***** move this up to init_dev() */
116    if (block->buf_len % TAPE_BSIZE != 0) {
117       uint32_t old_len = block->buf_len;
118       block->buf_len = ((old_len + TAPE_BSIZE - 1) / TAPE_BSIZE) * TAPE_BSIZE;
119       Mmsg3(&dev->errmsg, _("Block size %u forced to %u to be multiple of %d\n"), 
120          old_len, block->buf_len, TAPE_BSIZE);
121       Emsg0(M_WARNING, 0, dev->errmsg);
122       dev->max_block_size = block->buf_len;  /* force block size */
123    }
124    block->block_len = block->buf_len;  /* default block size */
125    block->buf = get_memory(block->buf_len); 
126    if (block->buf == NULL) {
127       Mmsg0(&dev->errmsg, _("Unable to malloc block buffer.\n"));
128       Emsg0(M_FATAL, 0, dev->errmsg);
129       return NULL;
130    }
131    empty_block(block);
132    block->BlockVer = BLOCK_VER;       /* default write version */
133    Dmsg1(90, "Returning new block=%x\n", block);
134    return block;
135 }
136
137 /*
138  * Free block 
139  */
140 void free_block(DEV_BLOCK *block)
141 {
142    Dmsg1(199, "free_block buffer %x\n", block->buf);
143    free_memory(block->buf);
144    Dmsg1(199, "free_block block %x\n", block);
145    free_memory((POOLMEM *)block);
146 }
147
148 /* Empty the block -- for writing */
149 void empty_block(DEV_BLOCK *block)
150 {
151    block->binbuf = WRITE_BLKHDR_LENGTH;
152    block->bufp = block->buf + block->binbuf;
153    block->read_len = 0;
154    block->failed_write = FALSE;
155 }
156
157 /*
158  * Create block header just before write. The space
159  * in the buffer should have already been reserved by
160  * init_block.
161  */
162 static void ser_block_header(DEV_BLOCK *block)
163 {
164    ser_declare;
165    uint32_t CheckSum = 0;
166    uint32_t block_len = block->binbuf;
167    
168    Dmsg1(190, "ser_block_header: block_len=%d\n", block_len);
169    ser_begin(block->buf, BLKHDR2_LENGTH);
170    ser_uint32(CheckSum);
171    ser_uint32(block_len);
172    ser_uint32(block->BlockNumber);
173    ser_bytes(WRITE_BLKHDR_ID, BLKHDR_ID_LENGTH);
174    if (BLOCK_VER >= 2) {
175       ser_uint32(block->VolSessionId);
176       ser_uint32(block->VolSessionTime);
177    }
178
179    /* Checksum whole block except for the checksum */
180    CheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH, 
181                  block_len-BLKHDR_CS_LENGTH);
182    Dmsg1(190, "ser_bloc_header: checksum=%x\n", CheckSum);
183    ser_begin(block->buf, BLKHDR2_LENGTH);
184    ser_uint32(CheckSum);              /* now add checksum to block header */
185 }
186
187 /*
188  * Unserialized the block header for reading block.
189  *  This includes setting all the buffer pointers correctly.
190  *
191  *  Returns: 0 on failure (not a block)
192  *           1 on success
193  */
194 static int unser_block_header(DEVICE *dev, DEV_BLOCK *block)
195 {
196    ser_declare;
197    char Id[BLKHDR_ID_LENGTH+1];
198    uint32_t CheckSum, BlockCheckSum;
199    uint32_t block_len;
200    uint32_t EndBlock;
201    uint32_t BlockNumber;
202    int bhl;
203
204    unser_begin(block->buf, BLKHDR_LENGTH);
205    unser_uint32(CheckSum);
206    unser_uint32(block_len);
207    unser_uint32(BlockNumber);
208    unser_bytes(Id, BLKHDR_ID_LENGTH);
209    ASSERT(unser_length(block->buf) == BLKHDR1_LENGTH);
210
211    Id[BLKHDR_ID_LENGTH] = 0;
212    if (Id[3] == '1') {
213       bhl = BLKHDR1_LENGTH;
214       block->BlockVer = 1;
215       block->bufp = block->buf + bhl;
216       if (strncmp(Id, BLKHDR1_ID, BLKHDR_ID_LENGTH) != 0) {
217       Mmsg2(&dev->errmsg, _("Buffer ID error. Wanted: %s, got %s. Buffer discarded.\n"),
218             BLKHDR1_ID, Id);
219       Emsg0(M_ERROR, 0, dev->errmsg);
220       return 0;
221    }
222    } else {
223       unser_uint32(block->VolSessionId);
224       unser_uint32(block->VolSessionTime);
225       bhl = BLKHDR2_LENGTH;
226       block->BlockVer = 2;
227       block->bufp = block->buf + bhl;
228       if (strncmp(Id, BLKHDR2_ID, BLKHDR_ID_LENGTH) != 0) {
229          Mmsg2(&dev->errmsg, _("Buffer ID error. Wanted: %s, got %s. Buffer discarded.\n"),
230             BLKHDR2_ID, Id);
231          Emsg0(M_ERROR, 0, dev->errmsg);
232          return 0;
233       }
234    }
235
236    ASSERT(block_len < MAX_BLOCK_LENGTH);    /* temp sanity check */
237
238    Dmsg1(190, "unser_block_header block_len=%d\n", block_len);
239    /* Find end of block or end of buffer whichever is smaller */
240    if (block_len > block->read_len) {
241       EndBlock = block->read_len;
242    } else {
243       EndBlock = block_len;
244    }
245    block->binbuf = EndBlock - bhl;
246    block->block_len = block_len;
247    block->BlockNumber = BlockNumber;
248    Dmsg3(190, "Read binbuf = %d %d block_len=%d\n", block->binbuf,
249       bhl, block_len);
250    if (block_len > block->buf_len) {
251       Mmsg2(&dev->errmsg,  _("Block length %u is greater than buffer %u\n"),
252          block_len, block->buf_len);
253       Emsg0(M_ERROR, 0, dev->errmsg);
254       return 0;
255    }
256    if (block_len <= block->read_len) {
257       BlockCheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH,
258                          block_len-BLKHDR_CS_LENGTH);
259       if (BlockCheckSum != CheckSum) {
260          Dmsg2(00, "Block checksum mismatch: calc=%x blk=%x\n", BlockCheckSum,
261             CheckSum);
262          Mmsg2(&dev->errmsg, _("Block checksum mismatch: calc=%x blk=%x\n"), BlockCheckSum,
263             CheckSum);
264          return 0;
265       }
266    }
267    return 1;
268 }
269
270 /*  
271  * Write a block to the device, with locking and unlocking
272  *
273  * Returns: 1 on success
274  *        : 0 on failure
275  *
276  */
277 int write_block_to_device(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
278 {
279    int stat = 1;
280    lock_device(dev);
281    if (!write_block_to_dev(dev, block)) {
282        stat = fixup_device_block_write_error(jcr, dev, block);
283    }
284    unlock_device(dev);
285    return stat;
286 }
287
288 /*
289  * Write a block to the device 
290  *
291  *  Returns: 1 on success or EOT
292  *           0 on hard error
293  */
294 int write_block_to_dev(DEVICE *dev, DEV_BLOCK *block)
295 {
296    size_t stat = 0;
297    uint32_t wlen;                     /* length to write */
298
299 #ifdef NO_TAPE_WRITE_TEST
300    empty_block(block);
301    return 1;
302 #endif
303    ASSERT(block->binbuf == ((uint32_t) (block->bufp - block->buf)));
304
305    /* dump_block(block, "before write"); */
306    if (dev->state & ST_WEOT) {
307       Dmsg0(100, "return write_block_to_dev with ST_WEOT\n");
308       return 0;
309    }
310    wlen = block->binbuf;
311    if (wlen <= WRITE_BLKHDR_LENGTH) {  /* Does block have data in it? */
312       Dmsg0(100, "return write_block_to_dev no data to write\n");
313       return 1;
314    }
315    /* 
316     * Clear to the end of the buffer if it is not full,
317     *  and on tape devices, apply min and fixed blocking.
318     */
319    if (wlen != block->buf_len) {
320       uint32_t blen;                  /* current buffer length */
321
322       Dmsg2(200, "binbuf=%d buf_len=%d\n", block->binbuf, block->buf_len);
323       blen = wlen;
324
325       /* Adjust write size to min/max for tapes only */
326       if (dev->state & ST_TAPE) {
327          if (wlen < dev->min_block_size) {
328             wlen =  ((dev->min_block_size + TAPE_BSIZE - 1) / TAPE_BSIZE) * TAPE_BSIZE;
329          }
330          if (dev->min_block_size == dev->max_block_size) {
331             wlen = block->buf_len;    /* fixed block size already rounded */
332          }
333       }
334       if (wlen-blen > 0) {
335          memset(block->bufp, 0, wlen-blen); /* clear garbage */
336       }
337    }  
338
339    ser_block_header(block);
340
341    /* Limit maximum Volume size to value specified by user */
342    if ((dev->max_volume_size > 0) &&
343        ((dev->VolCatInfo.VolCatBytes + block->binbuf)) >= dev->max_volume_size) {
344       dev->state |= ST_WEOT;
345       Dmsg0(10, "==== Output bytes Triggered medium max capacity.\n");
346       Mmsg2(&dev->errmsg, _("Max. Volume capacity %" lld " exceeded on device %s.\n"),
347          dev->max_volume_size, dev->dev_name);
348       block->failed_write = TRUE;
349 /* ****FIXME**** write EOD record here */
350       weof_dev(dev, 1);               /* end the tape */
351       weof_dev(dev, 1);               /* write second eof */
352       return 0;
353    }
354
355    dev->VolCatInfo.VolCatWrites++;
356    Dmsg1(500, "Write block of %u bytes\n", wlen);      
357    if ((uint32_t) (stat=write(dev->fd, block->buf, (size_t)wlen)) != wlen) {
358       /* We should check for errno == ENOSPC, BUT many 
359        * devices simply report EIO when it is full.
360        * with a little more thought we may be able to check
361        * capacity and distinguish real errors and EOT
362        * conditions.  In any case, we probably want to
363        * simulate an End of Medium.
364        */
365 /***FIXME**** if we wrote a partial record, back up over it */
366       dev->state |= ST_EOF | ST_EOT | ST_WEOT;
367       clrerror_dev(dev, -1);
368
369       if (dev->dev_errno == 0) {
370          dev->dev_errno = ENOSPC;        /* out of space */
371       }
372
373       Dmsg4(10, "=== Write error. size=%u rtn=%d  errno=%d: ERR=%s\n", 
374          wlen, stat, dev->dev_errno, strerror(dev->dev_errno));
375
376       Mmsg4(&dev->errmsg, _("Write error on device %s. Write of %u bytes got %d. ERR=%s.\n"), 
377          dev->dev_name, wlen, stat, strerror(dev->dev_errno));
378       block->failed_write = TRUE;
379       weof_dev(dev, 1);               /* end the tape */
380       weof_dev(dev, 1);               /* write second eof */
381       return 0;
382    }
383 // Dmsg1(000, "Pos after write=%lld\n", lseek(dev->fd, (off_t)0, SEEK_CUR));
384    dev->VolCatInfo.VolCatBytes += block->binbuf;
385    dev->VolCatInfo.VolCatBlocks++;   
386    dev->file_addr += wlen;
387
388    /* Limit maximum File size on volume to user specified value */
389    if (dev->state & ST_TAPE) {
390       if ((dev->max_file_size > 0) && dev->file_addr >= dev->max_file_size) {
391          weof_dev(dev, 1);               /* write eof */
392       }
393    }
394
395    dev->block_num++;
396    block->BlockNumber++;
397    Dmsg2(190, "write_block: wrote block %d bytes=%d\n", dev->block_num,
398       wlen);
399    empty_block(block);
400    return 1;
401 }
402
403 /*  
404  * Read block with locking
405  *
406  */
407 int read_block_from_device(DEVICE *dev, DEV_BLOCK *block)
408 {
409    int stat;
410    Dmsg0(90, "Enter read_block_from_device\n");
411    lock_device(dev);
412    stat = read_block_from_dev(dev, block);
413    unlock_device(dev);
414    Dmsg0(90, "Leave read_block_from_device\n");
415    return stat;
416 }
417
418 /*
419  * Read the next block into the block structure and unserialize
420  *  the block header.  For a file, the block may be partially
421  *  or completely in the current buffer.
422  */
423 int read_block_from_dev(DEVICE *dev, DEV_BLOCK *block)
424 {
425    size_t stat;
426
427    Dmsg1(100, "Full read() in read_block_from_device() len=%d\n",
428          block->buf_len);
429 // Dmsg1(000, "Pos before read=%lld\n", lseek(dev->fd, (off_t)0, SEEK_CUR));
430    if ((stat=read(dev->fd, block->buf, (size_t)block->buf_len)) < 0) {
431
432 /* ***FIXME****  add code to detect buffer too small, and
433    reallocate buffer, backspace, and reread.
434    ENOMEM
435  */
436
437       Dmsg1(90, "Read device got: ERR=%s\n", strerror(errno));
438       clrerror_dev(dev, -1);
439       block->read_len = 0;
440       Mmsg2(&dev->errmsg, _("Read error on device %s. ERR=%s.\n"), 
441          dev->dev_name, strerror(dev->dev_errno));
442       return 0;
443    }
444 // Dmsg1(000, "Pos after read=%lld\n", lseek(dev->fd, (off_t)0, SEEK_CUR));
445    Dmsg1(90, "Read device got %d bytes\n", stat);
446    if (stat == 0) {             /* Got EOF ! */
447       dev->block_num = block->read_len = 0;
448       Mmsg1(&dev->errmsg, _("Read zero bytes on device %s.\n"), dev->dev_name);
449       if (dev->state & ST_EOF) { /* EOF alread read? */
450          dev->state |= ST_EOT;  /* yes, 2 EOFs => EOT */
451          return 0;
452       }
453       dev->file++;              /* increment file */
454       dev->state |= ST_EOF;     /* set EOF read */
455       return 0;                 /* return eof */
456    }
457    /* Continue here for successful read */
458    block->read_len = stat;      /* save length read */
459    if (block->read_len < BLKHDR2_LENGTH) {
460       Mmsg2(&dev->errmsg, _("Very short block of %d bytes on device %s discarded.\n"), 
461          block->read_len, dev->dev_name);
462       dev->state |= ST_SHORT;   /* set short block */
463       block->read_len = block->binbuf = 0;
464       return 0;                 /* return error */
465    }  
466    if (!unser_block_header(dev, block)) {
467       return 0;
468    }
469
470    if (block->block_len > block->read_len) {
471       Mmsg2(&dev->errmsg, _("Short block of %d bytes on device %s discarded.\n"), 
472          block->read_len, dev->dev_name);
473       dev->state |= ST_SHORT;   /* set short block */
474       block->read_len = block->binbuf = 0;
475       return 0;                 /* return error */
476    }  
477
478    /* Make sure block size is not too big (temporary
479     * sanity check) and that we read the full block.
480     */
481    ASSERT(block->block_len < MAX_BLOCK_LENGTH);
482
483    dev->state &= ~(ST_EOF|ST_SHORT); /* clear EOF and short block */
484    dev->block_num++;
485
486    /*
487     * If we read a short block on disk,
488     * seek to beginning of next block. This saves us
489     * from shuffling blocks around in the buffer. Take a
490     * look at this from an efficiency stand point later, but
491     * it should only happen once at the end of each job.
492     *
493     * I've been lseek()ing negative relative to SEEK_CUR for 30
494     *   years now. However, it seems that with the new off_t definition,
495     *   it is not possible to seek negative amounts, so we use two
496     *   lseek(). One to get the position, then the second to do an
497     *   absolute positioning -- so much for efficiency.  KES Sep 02.
498     */
499    Dmsg0(200, "At end of read block\n");
500    if (block->read_len > block->block_len && !(dev->state & ST_TAPE)) {
501       off_t pos = lseek(dev->fd, (off_t)0, SEEK_CUR); /* get curr pos */
502       pos -= (block->read_len - block->block_len);
503       lseek(dev->fd, pos, SEEK_SET);   
504       Dmsg2(100, "Did lseek blk_size=%d rdlen=%d\n", block->block_len,
505             block->read_len);
506    }
507    Dmsg2(200, "Exit read_block read_len=%d block_len=%d\n",
508       block->read_len, block->block_len);
509    return 1;
510 }