3 * block.c -- tape block handling functions
5 * Kern Sibbald, March MMI
6 * added BB02 format October MMII
12 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
35 extern int debug_level;
38 * Dump the block header, then walk through
39 * the block printing out the record headers.
41 void dump_block(DEV_BLOCK *b, char *msg)
45 char Id[BLKHDR_ID_LENGTH+1];
46 uint32_t CheckSum, BlockCheckSum;
49 uint32_t VolSessionId, VolSessionTime, data_len;
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;
62 unser_uint32(VolSessionId);
63 unser_uint32(VolSessionTime);
67 VolSessionId = VolSessionTime = 0;
72 if (block_len > 100000) {
73 Dmsg3(20, "Dump block %s 0x%x blocksize too big %u\n", msg, b, block_len);
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);
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);
89 unser_int32(FileIndex);
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);
100 * Create a new block structure.
101 * We pass device so that the block can inherit the
102 * min and max block sizes.
104 DEV_BLOCK *new_block(DEVICE *dev)
106 DEV_BLOCK *block = (DEV_BLOCK *)get_memory(sizeof(DEV_BLOCK));
108 memset(block, 0, sizeof(DEV_BLOCK));
110 if (dev->max_block_size == 0) {
111 block->buf_len = DEFAULT_BLOCK_SIZE;
113 block->buf_len = dev->max_block_size;
115 block->block_len = block->buf_len; /* default block size */
116 block->buf = get_memory(block->buf_len);
117 if (block->buf == NULL) {
118 Mmsg0(&dev->errmsg, _("Unable to malloc block buffer.\n"));
119 Emsg0(M_FATAL, 0, dev->errmsg);
123 block->BlockVer = BLOCK_VER; /* default write version */
124 Dmsg1(90, "Returning new block=%x\n", block);
131 void free_block(DEV_BLOCK *block)
133 Dmsg1(199, "free_block buffer %x\n", block->buf);
134 free_memory(block->buf);
135 Dmsg1(199, "free_block block %x\n", block);
136 free_memory((POOLMEM *)block);
139 /* Empty the block -- for writing */
140 void empty_block(DEV_BLOCK *block)
142 block->binbuf = WRITE_BLKHDR_LENGTH;
143 block->bufp = block->buf + block->binbuf;
145 block->failed_write = FALSE;
149 * Create block header just before write. The space
150 * in the buffer should have already been reserved by
153 static void ser_block_header(DEV_BLOCK *block)
156 uint32_t CheckSum = 0;
157 uint32_t block_len = block->binbuf;
159 Dmsg1(190, "ser_block_header: block_len=%d\n", block_len);
160 ser_begin(block->buf, BLKHDR2_LENGTH);
161 ser_uint32(CheckSum);
162 ser_uint32(block_len);
163 ser_uint32(block->BlockNumber);
164 ser_bytes(WRITE_BLKHDR_ID, BLKHDR_ID_LENGTH);
165 if (BLOCK_VER >= 2) {
166 ser_uint32(block->VolSessionId);
167 ser_uint32(block->VolSessionTime);
170 /* Checksum whole block except for the checksum */
171 CheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH,
172 block_len-BLKHDR_CS_LENGTH);
173 Dmsg1(190, "ser_bloc_header: checksum=%x\n", CheckSum);
174 ser_begin(block->buf, BLKHDR2_LENGTH);
175 ser_uint32(CheckSum); /* now add checksum to block header */
179 * Unserialized the block header for reading block.
180 * This includes setting all the buffer pointers correctly.
182 * Returns: 0 on failure (not a block)
185 static int unser_block_header(DEVICE *dev, DEV_BLOCK *block)
188 char Id[BLKHDR_ID_LENGTH+1];
189 uint32_t CheckSum, BlockCheckSum;
192 uint32_t BlockNumber;
195 unser_begin(block->buf, BLKHDR_LENGTH);
196 unser_uint32(CheckSum);
197 unser_uint32(block_len);
198 unser_uint32(BlockNumber);
199 unser_bytes(Id, BLKHDR_ID_LENGTH);
200 ASSERT(unser_length(block->buf) == BLKHDR1_LENGTH);
202 Id[BLKHDR_ID_LENGTH] = 0;
204 bhl = BLKHDR1_LENGTH;
206 block->bufp = block->buf + bhl;
207 if (strncmp(Id, BLKHDR1_ID, BLKHDR_ID_LENGTH) != 0) {
208 Mmsg2(&dev->errmsg, _("Buffer ID error. Wanted: %s, got %s. Buffer discarded.\n"),
210 Emsg0(M_ERROR, 0, dev->errmsg);
214 unser_uint32(block->VolSessionId);
215 unser_uint32(block->VolSessionTime);
216 bhl = BLKHDR2_LENGTH;
218 block->bufp = block->buf + bhl;
219 if (strncmp(Id, BLKHDR2_ID, BLKHDR_ID_LENGTH) != 0) {
220 Mmsg2(&dev->errmsg, _("Buffer ID error. Wanted: %s, got %s. Buffer discarded.\n"),
222 Emsg0(M_ERROR, 0, dev->errmsg);
227 ASSERT(block_len < MAX_BLOCK_LENGTH); /* temp sanity check */
229 Dmsg1(190, "unser_block_header block_len=%d\n", block_len);
230 /* Find end of block or end of buffer whichever is smaller */
231 if (block_len > block->read_len) {
232 EndBlock = block->read_len;
234 EndBlock = block_len;
236 block->binbuf = EndBlock - bhl;
237 block->block_len = block_len;
238 block->BlockNumber = BlockNumber;
239 Dmsg3(190, "Read binbuf = %d %d block_len=%d\n", block->binbuf,
241 if (block_len > block->buf_len) {
242 Mmsg2(&dev->errmsg, _("Block length %u is greater than buffer %u\n"),
243 block_len, block->buf_len);
244 Emsg0(M_ERROR, 0, dev->errmsg);
247 if (block_len <= block->read_len) {
248 BlockCheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH,
249 block_len-BLKHDR_CS_LENGTH);
250 if (BlockCheckSum != CheckSum) {
251 Dmsg2(00, "Block checksum mismatch: calc=%x blk=%x\n", BlockCheckSum,
253 Mmsg2(&dev->errmsg, _("Block checksum mismatch: calc=%x blk=%x\n"), BlockCheckSum,
262 * Write a block to the device, with locking and unlocking
264 * Returns: 1 on success
268 int write_block_to_device(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
272 if (!write_block_to_dev(dev, block)) {
273 stat = fixup_device_block_write_error(jcr, dev, block);
280 * Write a block to the device
282 * Returns: 1 on success or EOT
285 int write_block_to_dev(DEVICE *dev, DEV_BLOCK *block)
288 uint32_t wlen; /* length to write */
290 #ifdef NO_TAPE_WRITE_TEST
294 ASSERT(block->binbuf == ((uint32_t) (block->bufp - block->buf)));
296 /* dump_block(block, "before write"); */
297 if (dev->state & ST_WEOT) {
298 Dmsg0(100, "return write_block_to_dev with ST_WEOT\n");
301 wlen = block->binbuf;
302 if (wlen <= WRITE_BLKHDR_LENGTH) { /* Does block have data in it? */
303 Dmsg0(100, "return write_block_to_dev no data to write\n");
307 * Clear to the end of the buffer if it is not full,
308 * and on tape devices, apply min and fixed blocking.
310 if (wlen != block->buf_len) {
311 uint32_t blen; /* current buffer length */
313 Dmsg2(200, "binbuf=%d buf_len=%d\n", block->binbuf, block->buf_len);
316 /* Adjust write size to min/max for tapes only */
317 if (dev->state & ST_TAPE) {
318 if (wlen < dev->min_block_size) {
319 wlen = ((dev->min_block_size + TAPE_BSIZE - 1) / TAPE_BSIZE) * TAPE_BSIZE;
321 /* check for fixed block size */
322 if (dev->min_block_size == dev->max_block_size) {
323 wlen = block->buf_len; /* fixed block size already rounded */
327 memset(block->bufp, 0, wlen-blen); /* clear garbage */
331 ser_block_header(block);
333 /* Limit maximum Volume size to value specified by user */
334 if ((dev->max_volume_size > 0) &&
335 ((dev->VolCatInfo.VolCatBytes + block->binbuf)) >= dev->max_volume_size) {
336 dev->state |= ST_WEOT;
337 Dmsg0(10, "==== Output bytes Triggered medium max capacity.\n");
338 Mmsg2(&dev->errmsg, _("Max. Volume capacity %" lld " exceeded on device %s.\n"),
339 dev->max_volume_size, dev->dev_name);
340 block->failed_write = TRUE;
341 /* ****FIXME**** write EOD record here */
342 weof_dev(dev, 1); /* end the tape */
343 weof_dev(dev, 1); /* write second eof */
347 dev->VolCatInfo.VolCatWrites++;
348 Dmsg1(500, "Write block of %u bytes\n", wlen);
349 if ((uint32_t) (stat=write(dev->fd, block->buf, (size_t)wlen)) != wlen) {
350 /* We should check for errno == ENOSPC, BUT many
351 * devices simply report EIO when it is full.
352 * with a little more thought we may be able to check
353 * capacity and distinguish real errors and EOT
354 * conditions. In any case, we probably want to
355 * simulate an End of Medium.
357 /***FIXME**** if we wrote a partial record, back up over it */
358 dev->state |= ST_EOF | ST_EOT | ST_WEOT;
359 clrerror_dev(dev, -1);
361 if (dev->dev_errno == 0) {
362 dev->dev_errno = ENOSPC; /* out of space */
365 Dmsg4(10, "=== Write error. size=%u rtn=%d errno=%d: ERR=%s\n",
366 wlen, stat, dev->dev_errno, strerror(dev->dev_errno));
368 Mmsg4(&dev->errmsg, _("Write error on device %s. Write of %u bytes got %d. ERR=%s.\n"),
369 dev->dev_name, wlen, stat, strerror(dev->dev_errno));
370 block->failed_write = TRUE;
371 weof_dev(dev, 1); /* end the tape */
372 weof_dev(dev, 1); /* write second eof */
375 // Dmsg1(000, "Pos after write=%lld\n", lseek(dev->fd, (off_t)0, SEEK_CUR));
376 dev->VolCatInfo.VolCatBytes += block->binbuf;
377 dev->VolCatInfo.VolCatBlocks++;
378 dev->file_addr += wlen;
380 /* Limit maximum File size on volume to user specified value */
381 if (dev->state & ST_TAPE) {
382 if ((dev->max_file_size > 0) && dev->file_addr >= dev->max_file_size) {
383 weof_dev(dev, 1); /* write eof */
388 block->BlockNumber++;
389 Dmsg2(190, "write_block: wrote block %d bytes=%d\n", dev->block_num,
396 * Read block with locking
399 int read_block_from_device(DEVICE *dev, DEV_BLOCK *block)
402 Dmsg0(90, "Enter read_block_from_device\n");
404 stat = read_block_from_dev(dev, block);
406 Dmsg0(90, "Leave read_block_from_device\n");
411 * Read the next block into the block structure and unserialize
412 * the block header. For a file, the block may be partially
413 * or completely in the current buffer.
415 int read_block_from_dev(DEVICE *dev, DEV_BLOCK *block)
419 Dmsg1(100, "Full read() in read_block_from_device() len=%d\n",
421 // Dmsg1(000, "Pos before read=%lld\n", lseek(dev->fd, (off_t)0, SEEK_CUR));
422 if ((stat=read(dev->fd, block->buf, (size_t)block->buf_len)) < 0) {
424 /* ***FIXME**** add code to detect buffer too small, and
425 reallocate buffer, backspace, and reread.
429 Dmsg1(90, "Read device got: ERR=%s\n", strerror(errno));
430 clrerror_dev(dev, -1);
432 Mmsg2(&dev->errmsg, _("Read error on device %s. ERR=%s.\n"),
433 dev->dev_name, strerror(dev->dev_errno));
436 // Dmsg1(000, "Pos after read=%lld\n", lseek(dev->fd, (off_t)0, SEEK_CUR));
437 Dmsg1(90, "Read device got %d bytes\n", stat);
438 if (stat == 0) { /* Got EOF ! */
439 dev->block_num = block->read_len = 0;
440 Mmsg1(&dev->errmsg, _("Read zero bytes on device %s.\n"), dev->dev_name);
441 if (dev->state & ST_EOF) { /* EOF alread read? */
442 dev->state |= ST_EOT; /* yes, 2 EOFs => EOT */
445 dev->file++; /* increment file */
446 dev->state |= ST_EOF; /* set EOF read */
447 return 0; /* return eof */
449 /* Continue here for successful read */
450 block->read_len = stat; /* save length read */
451 if (block->read_len < BLKHDR2_LENGTH) {
452 Mmsg2(&dev->errmsg, _("Very short block of %d bytes on device %s discarded.\n"),
453 block->read_len, dev->dev_name);
454 dev->state |= ST_SHORT; /* set short block */
455 block->read_len = block->binbuf = 0;
456 return 0; /* return error */
458 if (!unser_block_header(dev, block)) {
462 if (block->block_len > block->read_len) {
463 Mmsg2(&dev->errmsg, _("Short block of %d bytes on device %s discarded.\n"),
464 block->read_len, dev->dev_name);
465 dev->state |= ST_SHORT; /* set short block */
466 block->read_len = block->binbuf = 0;
467 return 0; /* return error */
470 /* Make sure block size is not too big (temporary
471 * sanity check) and that we read the full block.
473 ASSERT(block->block_len < MAX_BLOCK_LENGTH);
475 dev->state &= ~(ST_EOF|ST_SHORT); /* clear EOF and short block */
479 * If we read a short block on disk,
480 * seek to beginning of next block. This saves us
481 * from shuffling blocks around in the buffer. Take a
482 * look at this from an efficiency stand point later, but
483 * it should only happen once at the end of each job.
485 * I've been lseek()ing negative relative to SEEK_CUR for 30
486 * years now. However, it seems that with the new off_t definition,
487 * it is not possible to seek negative amounts, so we use two
488 * lseek(). One to get the position, then the second to do an
489 * absolute positioning -- so much for efficiency. KES Sep 02.
491 Dmsg0(200, "At end of read block\n");
492 if (block->read_len > block->block_len && !(dev->state & ST_TAPE)) {
493 off_t pos = lseek(dev->fd, (off_t)0, SEEK_CUR); /* get curr pos */
494 pos -= (block->read_len - block->block_len);
495 lseek(dev->fd, pos, SEEK_SET);
496 Dmsg2(100, "Did lseek blk_size=%d rdlen=%d\n", block->block_len,
499 Dmsg2(200, "Exit read_block read_len=%d block_len=%d\n",
500 block->read_len, block->block_len);