3 * block.c -- tape block handling functions
5 * Kern Sibbald, March MMI
6 * added BB02 format October MMII
12 Copyright (C) 2000-2005 Kern Sibbald
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;
36 static bool terminate_writing_volume(DCR *dcr);
39 * Dump the block header, then walk through
40 * the block printing out the record headers.
42 void dump_block(DEV_BLOCK *b, const char *msg)
46 char Id[BLKHDR_ID_LENGTH+1];
47 uint32_t CheckSum, BlockCheckSum;
50 uint32_t VolSessionId, VolSessionTime, data_len;
55 unser_begin(b->buf, BLKHDR1_LENGTH);
56 unser_uint32(CheckSum);
57 unser_uint32(block_len);
58 unser_uint32(BlockNumber);
59 unser_bytes(Id, BLKHDR_ID_LENGTH);
60 ASSERT(unser_length(b->buf) == BLKHDR1_LENGTH);
61 Id[BLKHDR_ID_LENGTH] = 0;
63 unser_uint32(VolSessionId);
64 unser_uint32(VolSessionTime);
68 VolSessionId = VolSessionTime = 0;
73 if (block_len > 100000) {
74 Dmsg3(20, "Dump block %s 0x%x blocksize too big %u\n", msg, b, block_len);
78 BlockCheckSum = bcrc32((uint8_t *)b->buf+BLKHDR_CS_LENGTH,
79 block_len-BLKHDR_CS_LENGTH);
80 Pmsg6(000, "Dump block %s %x: size=%d BlkNum=%d\n"
81 " Hdrcksum=%x cksum=%x\n",
82 msg, b, block_len, BlockNumber, CheckSum, BlockCheckSum);
84 while (p < (b->buf + block_len+WRITE_RECHDR_LENGTH)) {
85 unser_begin(p, WRITE_RECHDR_LENGTH);
86 if (rhl == RECHDR1_LENGTH) {
87 unser_uint32(VolSessionId);
88 unser_uint32(VolSessionTime);
90 unser_int32(FileIndex);
92 unser_uint32(data_len);
93 Pmsg6(000, " Rec: VId=%u VT=%u FI=%s Strm=%s len=%d p=%x\n",
94 VolSessionId, VolSessionTime, FI_to_ascii(FileIndex),
95 stream_to_ascii(Stream, FileIndex), data_len, p);
101 * Create a new block structure.
102 * We pass device so that the block can inherit the
103 * min and max block sizes.
105 DEV_BLOCK *new_block(DEVICE *dev)
107 DEV_BLOCK *block = (DEV_BLOCK *)get_memory(sizeof(DEV_BLOCK));
109 memset(block, 0, sizeof(DEV_BLOCK));
111 /* If the user has specified a max_block_size, use it as the default */
112 if (dev->max_block_size == 0) {
113 block->buf_len = DEFAULT_BLOCK_SIZE;
115 block->buf_len = dev->max_block_size;
118 block->block_len = block->buf_len; /* default block size */
119 block->buf = get_memory(block->buf_len);
121 block->BlockVer = BLOCK_VER; /* default write version */
122 Dmsg1(350, "Returning new block=%x\n", block);
128 * Duplicate an existing block (eblock)
130 DEV_BLOCK *dup_block(DEV_BLOCK *eblock)
132 DEV_BLOCK *block = (DEV_BLOCK *)get_memory(sizeof(DEV_BLOCK));
133 int buf_len = sizeof_pool_memory(eblock->buf);
135 memcpy(block, eblock, sizeof(DEV_BLOCK));
136 block->buf = get_memory(buf_len);
137 memcpy(block->buf, eblock->buf, buf_len);
143 * Only the first block checksum error was reported.
144 * If there are more, report it now.
146 void print_block_read_errors(JCR *jcr, DEV_BLOCK *block)
148 if (block->read_errors > 1) {
149 Jmsg(jcr, M_ERROR, 0, _("%d block read errors not printed.\n"),
157 void free_block(DEV_BLOCK *block)
159 Dmsg1(199, "free_block buffer %x\n", block->buf);
160 free_memory(block->buf);
161 Dmsg1(199, "free_block block %x\n", block);
162 free_memory((POOLMEM *)block);
165 /* Empty the block -- for writing */
166 void empty_block(DEV_BLOCK *block)
168 block->binbuf = WRITE_BLKHDR_LENGTH;
169 block->bufp = block->buf + block->binbuf;
171 block->write_failed = false;
172 block->block_read = false;
173 block->FirstIndex = block->LastIndex = 0;
177 * Create block header just before write. The space
178 * in the buffer should have already been reserved by
181 void ser_block_header(DEV_BLOCK *block)
184 uint32_t CheckSum = 0;
185 uint32_t block_len = block->binbuf;
187 Dmsg1(390, "ser_block_header: block_len=%d\n", block_len);
188 ser_begin(block->buf, BLKHDR2_LENGTH);
189 ser_uint32(CheckSum);
190 ser_uint32(block_len);
191 ser_uint32(block->BlockNumber);
192 ser_bytes(WRITE_BLKHDR_ID, BLKHDR_ID_LENGTH);
193 if (BLOCK_VER >= 2) {
194 ser_uint32(block->VolSessionId);
195 ser_uint32(block->VolSessionTime);
198 /* Checksum whole block except for the checksum */
199 CheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH,
200 block_len-BLKHDR_CS_LENGTH);
201 Dmsg1(390, "ser_bloc_header: checksum=%x\n", CheckSum);
202 ser_begin(block->buf, BLKHDR2_LENGTH);
203 ser_uint32(CheckSum); /* now add checksum to block header */
207 * Unserialize the block header for reading block.
208 * This includes setting all the buffer pointers correctly.
210 * Returns: false on failure (not a block)
213 static bool unser_block_header(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
216 char Id[BLKHDR_ID_LENGTH+1];
217 uint32_t CheckSum, BlockCheckSum;
220 uint32_t BlockNumber;
223 unser_begin(block->buf, BLKHDR_LENGTH);
224 unser_uint32(CheckSum);
225 unser_uint32(block_len);
226 unser_uint32(BlockNumber);
227 unser_bytes(Id, BLKHDR_ID_LENGTH);
228 ASSERT(unser_length(block->buf) == BLKHDR1_LENGTH);
230 Id[BLKHDR_ID_LENGTH] = 0;
232 bhl = BLKHDR1_LENGTH;
234 block->bufp = block->buf + bhl;
235 if (strncmp(Id, BLKHDR1_ID, BLKHDR_ID_LENGTH) != 0) {
236 dev->dev_errno = EIO;
237 Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
238 dev->file, dev->block_num, BLKHDR1_ID, Id);
239 if (block->read_errors == 0 || verbose >= 2) {
240 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
242 block->read_errors++;
245 } else if (Id[3] == '2') {
246 unser_uint32(block->VolSessionId);
247 unser_uint32(block->VolSessionTime);
248 bhl = BLKHDR2_LENGTH;
250 block->bufp = block->buf + bhl;
251 if (strncmp(Id, BLKHDR2_ID, BLKHDR_ID_LENGTH) != 0) {
252 dev->dev_errno = EIO;
253 Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
254 dev->file, dev->block_num, BLKHDR2_ID, Id);
255 if (block->read_errors == 0 || verbose >= 2) {
256 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
258 block->read_errors++;
262 dev->dev_errno = EIO;
263 Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
264 dev->file, dev->block_num, BLKHDR2_ID, Id);
265 if (block->read_errors == 0 || verbose >= 2) {
266 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
268 block->read_errors++;
269 unser_uint32(block->VolSessionId);
270 unser_uint32(block->VolSessionTime);
275 if (block_len > MAX_BLOCK_LENGTH) {
276 dev->dev_errno = EIO;
277 Mmsg3(dev->errmsg, _("Volume data error at %u:%u! Block length %u is insane (too large), probably due to a bad archive.\n"),
278 dev->file, dev->block_num, block_len);
279 if (block->read_errors == 0 || verbose >= 2) {
280 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
282 block->read_errors++;
286 Dmsg1(390, "unser_block_header block_len=%d\n", block_len);
287 /* Find end of block or end of buffer whichever is smaller */
288 if (block_len > block->read_len) {
289 block_end = block->read_len;
291 block_end = block_len;
293 block->binbuf = block_end - bhl;
294 block->block_len = block_len;
295 block->BlockNumber = BlockNumber;
296 Dmsg3(390, "Read binbuf = %d %d block_len=%d\n", block->binbuf,
298 if (block_len <= block->read_len) {
299 BlockCheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH,
300 block_len-BLKHDR_CS_LENGTH);
301 if (BlockCheckSum != CheckSum) {
302 dev->dev_errno = EIO;
303 Mmsg5(dev->errmsg, _("Volume data error at %u:%u! Block checksum mismatch in block %u: calc=%x blk=%x\n"),
304 dev->file, dev->block_num, (unsigned)BlockNumber, BlockCheckSum, CheckSum);
305 if (block->read_errors == 0 || verbose >= 2) {
306 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
308 block->read_errors++;
318 * Write a block to the device, with locking and unlocking
320 * Returns: true on success
324 bool write_block_to_device(DCR *dcr)
327 DEVICE *dev = dcr->dev;
331 stat = write_block_to_spool_file(dcr);
335 if (!dcr->dev_locked) {
340 * If a new volume has been mounted since our last write
341 * Create a JobMedia record for the previous volume written,
342 * and set new parameters to write this volume
343 * The same applies for if we are in a new file.
345 if (dcr->NewVol || dcr->NewFile) {
346 if (job_canceled(jcr)) {
350 /* Create a jobmedia record for this job */
351 if (!dir_create_jobmedia_record(dcr)) {
352 dev->dev_errno = EIO;
353 Jmsg(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
354 dcr->VolCatInfo.VolCatName, jcr->Job);
355 set_new_volume_parameters(dcr);
360 /* Note, setting a new volume also handles any pending new file */
361 set_new_volume_parameters(dcr);
362 dcr->NewFile = false; /* this handled for new file too */
364 set_new_file_parameters(dcr);
368 if (!write_block_to_dev(dcr)) {
369 if (job_canceled(jcr) || jcr->JobType == JT_SYSTEM) {
372 stat = fixup_device_block_write_error(dcr);
377 if (!dcr->dev_locked) {
384 * Write a block to the device
386 * Returns: true on success or EOT
387 * false on hard error
389 bool write_block_to_dev(DCR *dcr)
392 uint32_t wlen; /* length to write */
393 int hit_max1, hit_max2;
395 DEVICE *dev = dcr->dev;
397 DEV_BLOCK *block = dcr->block;
399 #ifdef NO_TAPE_WRITE_TEST
403 ASSERT(block->binbuf == ((uint32_t) (block->bufp - block->buf)));
405 /* dump_block(block, "before write"); */
406 if (dev->state & ST_WEOT) {
407 Dmsg0(100, "return write_block_to_dev with ST_WEOT\n");
408 dev->dev_errno = ENOSPC;
409 Jmsg(jcr, M_FATAL, 0, _("Cannot write block. Device at EOM.\n"));
412 if (!(dev->state & ST_APPEND)) {
413 dev->dev_errno = EIO;
414 Jmsg(jcr, M_FATAL, 0, _("Attempt to write on read-only Volume.\n"));
417 wlen = block->binbuf;
418 if (wlen <= WRITE_BLKHDR_LENGTH) { /* Does block have data in it? */
419 Dmsg0(100, "return write_block_to_dev no data to write\n");
423 * Clear to the end of the buffer if it is not full,
424 * and on tape devices, apply min and fixed blocking.
426 if (wlen != block->buf_len) {
427 uint32_t blen; /* current buffer length */
429 Dmsg2(200, "binbuf=%d buf_len=%d\n", block->binbuf, block->buf_len);
432 /* Adjust write size to min/max for tapes only */
433 if (dev->state & ST_TAPE) {
434 /* check for fixed block size */
435 if (dev->min_block_size == dev->max_block_size) {
436 wlen = block->buf_len; /* fixed block size already rounded */
437 /* Check for min block size */
438 } else if (wlen < dev->min_block_size) {
439 wlen = ((dev->min_block_size + TAPE_BSIZE - 1) / TAPE_BSIZE) * TAPE_BSIZE;
440 /* Ensure size is rounded */
442 wlen = ((wlen + TAPE_BSIZE - 1) / TAPE_BSIZE) * TAPE_BSIZE;
446 memset(block->bufp, 0, wlen-blen); /* clear garbage */
450 ser_block_header(block);
452 /* Limit maximum Volume size to value specified by user */
453 hit_max1 = (dev->max_volume_size > 0) &&
454 ((dev->VolCatInfo.VolCatBytes + block->binbuf)) >= dev->max_volume_size;
455 hit_max2 = (dev->VolCatInfo.VolCatMaxBytes > 0) &&
456 ((dev->VolCatInfo.VolCatBytes + block->binbuf)) >= dev->VolCatInfo.VolCatMaxBytes;
457 if (hit_max1 || hit_max2) {
460 Dmsg0(10, "==== Output bytes Triggered medium max capacity.\n");
462 max_cap = dev->max_volume_size;
464 max_cap = dev->VolCatInfo.VolCatMaxBytes;
466 Jmsg(jcr, M_INFO, 0, _("User defined maximum volume capacity %s exceeded on device %s.\n"),
467 edit_uint64_with_commas(max_cap, ed1), dev->dev_name);
468 terminate_writing_volume(dcr);
469 dev->dev_errno = ENOSPC;
473 /* Limit maximum File size on volume to user specified value */
474 if ((dev->max_file_size > 0) &&
475 (dev->file_size+block->binbuf) >= dev->max_file_size) {
476 dev->file_size = 0; /* reset file size */
478 if (dev_state(dev, ST_TAPE) && weof_dev(dev, 1) != 0) { /* write eof */
479 Dmsg0(190, "WEOF error in max file size.\n");
480 terminate_writing_volume(dcr);
481 dev->dev_errno = ENOSPC;
485 /* Create a JobMedia record so restore can seek */
486 if (!dir_create_jobmedia_record(dcr)) {
487 Dmsg0(190, "Error from create_job_media.\n");
488 dev->dev_errno = EIO;
489 Jmsg(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
490 dcr->VolCatInfo.VolCatName, jcr->Job);
491 terminate_writing_volume(dcr);
492 dev->dev_errno = EIO;
495 dev->VolCatInfo.VolCatFiles = dev->file;
496 if (!dir_update_volume_info(dcr, false)) {
497 Dmsg0(190, "Error from update_vol_info.\n");
498 terminate_writing_volume(dcr);
499 dev->dev_errno = EIO;
502 Dmsg0(100, "dir_update_volume_info max file size -- OK\n");
506 * Walk through all attached dcrs setting flag to call
507 * set_new_file_parameters() when that dcr is next used.
510 foreach_dlist(mdcr, dev->attached_dcrs) {
511 if (mdcr->jcr->JobId == 0) {
514 mdcr->NewFile = true; /* set reminder to do set_new_file_params */
516 /* Set new file/block parameters for current dcr */
517 set_new_file_parameters(dcr);
520 /* Limit maximum part size to value specified by user (not applicable to tapes/fifos) */
521 if (!(dev->state & (ST_TAPE|ST_FIFO)) &&
522 (dev->max_part_size > 0) &&
523 ((dev->part_size + block->binbuf) >= dev->max_part_size)) {
524 if (dev->part < dev->num_parts) {
525 Jmsg3(dcr->jcr, M_FATAL, 0, _("Error while writing, current part number is less than the total number of parts (%d/%d, device=%s)\n"),
526 dev->part, dev->num_parts, dev_name(dev));
527 dev->dev_errno = EIO;
531 if (open_next_part(dev) < 0) {
532 Jmsg2(dcr->jcr, M_FATAL, 0, _("Unable to open device next part %s. ERR=%s\n"),
533 dev_name(dev), strerror_dev(dev));
534 dev->dev_errno = EIO;
538 dev->VolCatInfo.VolCatParts = dev->num_parts;
540 if (!dir_update_volume_info(dcr, false)) {
541 Dmsg0(190, "Error from update_vol_info.\n");
542 dev->dev_errno = EIO;
547 if (dev->free_space_errno < 0) { /* Error while getting free space */
550 Dmsg1(10, "Cannot get free space on the device ERR=%s.\n", dev->errmsg);
551 Jmsg(jcr, M_FATAL, 0, _("End of Volume \"%s\" at %u:%u on device %s (part_size=%s, free_space=%s, free_space_errno=%d, errmsg=%s).\n"),
552 dev->VolCatInfo.VolCatName,
553 dev->file, dev->block_num, dev->dev_name,
554 edit_uint64_with_commas(dev->part_size, ed1), edit_uint64_with_commas(dev->free_space, ed2),
555 dev->free_space_errno, dev->errmsg);
556 dev->dev_errno = -dev->free_space_errno;
560 if (((dev->free_space_errno > 0) && ((dev->part_size + block->binbuf) >= dev->free_space))) {
563 Dmsg0(10, "==== Just enough free space on the device to write the current part...\n");
564 Jmsg(jcr, M_INFO, 0, _("End of Volume \"%s\" at %u:%u on device %s (part_size=%s, free_space=%s, free_space_errno=%d).\n"),
565 dev->VolCatInfo.VolCatName,
566 dev->file, dev->block_num, dev->dev_name,
567 edit_uint64_with_commas(dev->part_size, ed1), edit_uint64_with_commas(dev->free_space, ed2),
568 dev->free_space_errno);
569 terminate_writing_volume(dcr);
570 dev->dev_errno = ENOSPC;
574 dev->VolCatInfo.VolCatWrites++;
575 Dmsg1(300, "Write block of %u bytes\n", wlen);
576 #ifdef DEBUG_BLOCK_ZEROING
577 uint32_t *bp = (uint32_t *)block->buf;
578 if (bp[0] == 0 && bp[1] == 0 && bp[2] == 0 && block->buf[12] == 0) {
579 Jmsg0(jcr, M_ABORT, 0, "Write block header zeroed.\n");
583 stat = write(dev->fd, block->buf, (size_t)wlen);
585 #ifdef DEBUG_BLOCK_ZEROING
586 if (bp[0] == 0 && bp[1] == 0 && bp[2] == 0 && block->buf[12] == 0) {
587 Jmsg0(jcr, M_ABORT, 0, "Write block header zeroed.\n");
591 if (stat != (ssize_t)wlen) {
592 /* Some devices simply report EIO when the volume is full.
593 * With a little more thought we may be able to check
594 * capacity and distinguish real errors and EOT
595 * conditions. In any case, we probably want to
596 * simulate an End of Medium.
600 clrerror_dev(dev, -1);
601 if (dev->dev_errno == 0) {
602 dev->dev_errno = ENOSPC; /* out of space */
604 if (dev->dev_errno != ENOSPC) {
605 Jmsg4(jcr, M_ERROR, 0, _("Write error at %u:%u on device %s. ERR=%s.\n"),
606 dev->file, dev->block_num, dev->dev_name, be.strerror());
609 dev->dev_errno = ENOSPC; /* out of space */
611 if (dev->dev_errno == ENOSPC) {
612 Jmsg(jcr, M_INFO, 0, _("End of Volume \"%s\" at %u:%u on device %s. Write of %u bytes got %d.\n"),
613 dev->VolCatInfo.VolCatName,
614 dev->file, dev->block_num, dev->dev_name, wlen, stat);
616 Dmsg6(100, "=== Write error. size=%u rtn=%d dev_blk=%d blk_blk=%d errno=%d: ERR=%s\n",
617 wlen, stat, dev->block_num, block->BlockNumber, dev->dev_errno, strerror(dev->dev_errno));
619 ok = terminate_writing_volume(dcr);
620 if (!ok && !forge_on) {
624 #define CHECK_LAST_BLOCK
625 #ifdef CHECK_LAST_BLOCK
627 * If the device is a tape and it supports backspace record,
628 * we backspace over one or two eof marks depending on
629 * how many we just wrote, then over the last record,
630 * then re-read it and verify that the block number is
633 if (ok && (dev->state & ST_TAPE) && dev_cap(dev, CAP_BSR)) {
634 /* Now back up over what we wrote and read the last block */
635 if (!bsf_dev(dev, 1)) {
637 Jmsg(jcr, M_ERROR, 0, _("Backspace file at EOT failed. ERR=%s\n"), strerror(dev->dev_errno));
639 if (ok && dev_cap(dev, CAP_TWOEOF) && !bsf_dev(dev, 1)) {
641 Jmsg(jcr, M_ERROR, 0, _("Backspace file at EOT failed. ERR=%s\n"), strerror(dev->dev_errno));
643 /* Backspace over record */
644 if (ok && !bsr_dev(dev, 1)) {
646 Jmsg(jcr, M_ERROR, 0, _("Backspace record at EOT failed. ERR=%s\n"), strerror(dev->dev_errno));
648 * On FreeBSD systems, if the user got here, it is likely that his/her
649 * tape drive is "frozen". The correct thing to do is a
650 * rewind(), but if we do that, higher levels in cleaning up, will
651 * most likely write the EOS record over the beginning of the
652 * tape. The rewind *is* done later in mount.c when another
653 * tape is requested. Note, the clrerror_dev() call in bsr_dev()
654 * calls ioctl(MTCERRSTAT), which *should* fix the problem.
658 DEV_BLOCK *lblock = new_block(dev);
659 /* Note, this can destroy dev->errmsg */
661 if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
662 Jmsg(jcr, M_ERROR, 0, _("Re-read last block at EOT failed. ERR=%s"), dev->errmsg);
664 if (lblock->BlockNumber+1 == block->BlockNumber) {
665 Jmsg(jcr, M_INFO, 0, _("Re-read of last block succeeded.\n"));
667 Jmsg(jcr, M_ERROR, 0, _(
668 "Re-read of last block failed. Last block=%u Current block=%u.\n"),
669 lblock->BlockNumber, block->BlockNumber);
680 /* We successfully wrote the block, now do housekeeping */
682 dev->VolCatInfo.VolCatBytes += block->binbuf;
683 dev->VolCatInfo.VolCatBlocks++;
684 dev->EndBlock = dev->block_num;
685 dev->EndFile = dev->file;
687 block->BlockNumber++;
689 /* Update dcr values */
690 if (dev_state(dev, ST_TAPE)) {
691 dcr->EndBlock = dev->EndBlock;
692 dcr->EndFile = dev->EndFile;
694 /* Save address of start of block just written */
695 dcr->EndBlock = (uint32_t)dev->file_addr;
696 dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
698 if (dcr->VolFirstIndex == 0 && block->FirstIndex > 0) {
699 dcr->VolFirstIndex = block->FirstIndex;
701 if (block->LastIndex > 0) {
702 dcr->VolLastIndex = block->LastIndex;
704 dcr->WroteVol = true;
705 dev->file_addr += wlen; /* update file address */
706 dev->file_size += wlen;
707 dev->part_size += wlen;
709 Dmsg2(300, "write_block: wrote block %d bytes=%d\n", dev->block_num, wlen);
714 static bool terminate_writing_volume(DCR *dcr)
716 DEVICE *dev = dcr->dev;
719 /* Create a JobMedia record to indicated end of tape */
720 dev->VolCatInfo.VolCatFiles = dev->file;
721 if (!dir_create_jobmedia_record(dcr)) {
722 Dmsg0(190, "Error from create JobMedia\n");
723 dev->dev_errno = EIO;
724 Jmsg(dcr->jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
725 dcr->VolCatInfo.VolCatName, dcr->jcr->Job);
729 dcr->block->write_failed = true;
730 if (weof_dev(dev, 1) != 0) { /* end the tape */
731 dev->VolCatInfo.VolCatErrors++;
732 Jmsg(dcr->jcr, M_ERROR, 0, "Error writing final EOF to tape. This tape may not be readable.\n"
735 Dmsg0(100, "WEOF error.\n");
737 dev->VolCatInfo.VolCatFiles = dev->file;
739 if (dev_cap(dev, CAP_REQMOUNT)) { /* Write the current (and last) part. */
743 if (!dir_update_volume_info(dcr, false)) {
746 Dmsg1(100, "dir_update_volume_info terminate writing -- %s\n", ok?"OK":"ERROR");
750 * Walk through all attached dcrs setting flag to call
751 * set_new_file_parameters() when that dcr is next used.
754 foreach_dlist(mdcr, dev->attached_dcrs) {
755 if (mdcr->jcr->JobId == 0) {
758 mdcr->NewFile = true; /* set reminder to do set_new_file_params */
760 /* Set new file/block parameters for current dcr */
761 set_new_file_parameters(dcr);
763 if (ok && dev_cap(dev, CAP_TWOEOF) && weof_dev(dev, 1) != 0) { /* end the tape */
764 dev->VolCatInfo.VolCatErrors++;
765 /* This may not be fatal since we already wrote an EOF */
766 Jmsg(dcr->jcr, M_ERROR, 0, "%s", dev->errmsg);
769 dev->state |= (ST_EOF|ST_EOT|ST_WEOT);
770 dev->state &= ~ST_APPEND; /* make tape read-only */
771 Dmsg1(100, "Leave terminate_writing_volume -- %s\n", ok?"OK":"ERROR");
777 * Read block with locking
780 bool read_block_from_device(DCR *dcr, bool check_block_numbers)
783 DEVICE *dev = dcr->dev;
784 Dmsg0(200, "Enter read_block_from_device\n");
786 stat = read_block_from_dev(dcr, check_block_numbers);
788 Dmsg0(200, "Leave read_block_from_device\n");
793 * Read the next block into the block structure and unserialize
794 * the block header. For a file, the block may be partially
795 * or completely in the current buffer.
797 bool read_block_from_dev(DCR *dcr, bool check_block_numbers)
801 uint32_t BlockNumber;
804 DEVICE *dev = dcr->dev;
805 DEV_BLOCK *block = dcr->block;
807 if (dev_state(dev, ST_EOT)) {
811 Dmsg1(200, "Full read() in read_block_from_device() len=%d\n",
815 dev->dev_errno = EIO;
816 Mmsg1(dev->errmsg, _("Block buffer size looping problem on device %s\n"),
818 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
823 /*Dmsg1(200, "dev.c 111->file_size=%u\n",(unsigned int)dev->file_size);
824 Dmsg1(200, "dev.c lseek=%u\n",(unsigned int)lseek(dev->fd, 0, SEEK_CUR));
825 Dmsg1(200, "dev.c dev->part_start=%u\n",(unsigned int)dev->part_start);
826 Dmsg1(200, "dev.c dev->file_size-dev->part_start=%u\n",(unsigned int)dev->file_size-dev->part_start);
827 Dmsg1(200, "dev.c dev->part_size=%u\n", (unsigned int)dev->part_size);
828 Dmsg1(200, "dev.c dev->part=%u\n", (unsigned int)dev->part);
829 Dmsg1(200, "dev.c dev->VolCatInfo.VolCatParts=%u\n", (unsigned int)dev->VolCatInfo.VolCatParts);
830 Dmsg3(200, "dev.c Tests : %d %d %d\n", (dev->VolCatInfo.VolCatParts > 0), ((dev->file_size-dev->part_start) == dev->part_size), (dev->part <= dev->VolCatInfo.VolCatParts));*/
831 /* Check for part file end */
832 if ((dev->num_parts > 0) &&
833 ((dev->file_size-dev->part_start) == dev->part_size) &&
834 (dev->part < dev->num_parts)) {
835 if (open_next_part(dev) < 0) {
836 Jmsg2(dcr->jcr, M_FATAL, 0, _("Unable to open device next part %s. ERR=%s\n"),
837 dev_name(dev), strerror_dev(dev));
838 dev->dev_errno = EIO;
845 // uint32_t *bp = (uint32_t *)block->buf;
846 // Dmsg3(000, "Read %p %u at %llu\n", block->buf, block->buf_len, lseek(dev->fd, 0, SEEK_CUR));
848 stat = read(dev->fd, block->buf, (size_t)block->buf_len);
850 // Dmsg8(000, "stat=%d Csum=%u blen=%u bnum=%u %c%c%c%c\n",stat, bp[0],bp[1],bp[2],
851 // block->buf[12],block->buf[13],block->buf[14],block->buf[15]);
854 dev->VolCatInfo.VolCatErrors++;
856 } while (stat == -1 && (errno == EINTR || errno == EIO) && retry++ < 11);
859 clrerror_dev(dev, -1);
860 Dmsg1(200, "Read device got: ERR=%s\n", be.strerror());
862 Mmsg4(dev->errmsg, _("Read error at file:blk %u:%u on device %s. ERR=%s.\n"),
863 dev->file, dev->block_num, dev->dev_name, be.strerror());
864 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
865 if (dev->state & ST_EOF) { /* EOF just seen? */
866 dev->state |= ST_EOT; /* yes, error => EOT */
870 Dmsg1(200, "Read device got %d bytes\n", stat);
871 if (stat == 0) { /* Got EOF ! */
872 dev->block_num = block->read_len = 0;
873 Mmsg3(dev->errmsg, _("Read zero bytes at %u:%u on device %s.\n"),
874 dev->file, dev->block_num, dev->dev_name);
875 if (dev->state & ST_EOF) { /* EOF already read? */
876 dev->state |= ST_EOT; /* yes, 2 EOFs => EOT */
880 dev->file++; /* increment file */
881 dev->state |= ST_EOF; /* set EOF read */
883 return false; /* return eof */
885 /* Continue here for successful read */
886 block->read_len = stat; /* save length read */
887 if (block->read_len < BLKHDR2_LENGTH) {
888 dev->dev_errno = EIO;
889 Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Very short block of %d bytes on device %s discarded.\n"),
890 dev->file, dev->block_num, block->read_len, dev->dev_name);
891 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
892 dev->state |= ST_SHORT; /* set short block */
893 block->read_len = block->binbuf = 0;
894 return false; /* return error */
897 BlockNumber = block->BlockNumber + 1;
898 if (!unser_block_header(jcr, dev, block)) {
900 dev->file_addr += block->read_len;
901 dev->file_size += block->read_len;
908 * If the block is bigger than the buffer, we reposition for
909 * re-reading the block, allocate a buffer of the correct size,
912 if (block->block_len > block->buf_len) {
913 dev->dev_errno = EIO;
914 Mmsg2(dev->errmsg, _("Block length %u is greater than buffer %u. Attempting recovery.\n"),
915 block->block_len, block->buf_len);
916 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
917 Pmsg1(000, "%s", dev->errmsg);
918 /* Attempt to reposition to re-read the block */
919 if (dev->state & ST_TAPE) {
920 Dmsg0(200, "BSR for reread; block too big for buffer.\n");
921 if (!bsr_dev(dev, 1)) {
922 Jmsg(jcr, M_ERROR, 0, "%s", strerror_dev(dev));
927 Dmsg0(200, "Seek to beginning of block for reread.\n");
928 off_t pos = lseek_dev(dev, (off_t)0, SEEK_CUR); /* get curr pos */
929 pos -= block->read_len;
930 lseek_dev(dev, pos, SEEK_SET);
931 dev->file_addr = pos;
933 Mmsg1(dev->errmsg, _("Setting block buffer size to %u bytes.\n"), block->block_len);
934 Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
935 Pmsg1(000, "%s", dev->errmsg);
936 /* Set new block length */
937 dev->max_block_size = block->block_len;
938 block->buf_len = block->block_len;
939 free_memory(block->buf);
940 block->buf = get_memory(block->buf_len);
943 goto reread; /* re-read block with correct block size */
946 if (block->block_len > block->read_len) {
947 dev->dev_errno = EIO;
948 Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Short block of %d bytes on device %s discarded.\n"),
949 dev->file, dev->block_num, block->read_len, dev->dev_name);
950 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
951 dev->state |= ST_SHORT; /* set short block */
952 block->read_len = block->binbuf = 0;
953 return false; /* return error */
956 dev->state &= ~(ST_EOF|ST_SHORT); /* clear EOF and short block */
957 dev->VolCatInfo.VolCatReads++;
958 dev->VolCatInfo.VolCatRBytes += block->read_len;
960 dev->VolCatInfo.VolCatBytes += block->block_len;
961 dev->VolCatInfo.VolCatBlocks++;
962 dev->EndBlock = dev->block_num;
963 dev->EndFile = dev->file;
966 /* Update dcr values */
967 if (dev->state & ST_TAPE) {
968 dcr->EndBlock = dev->EndBlock;
969 dcr->EndFile = dev->EndFile;
971 dcr->EndBlock = (uint32_t)dev->file_addr;
972 dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
973 dev->block_num = dcr->EndBlock;
974 dev->file = dcr->EndFile;
976 dev->file_addr += block->block_len;
977 dev->file_size += block->block_len;
980 * If we read a short block on disk,
981 * seek to beginning of next block. This saves us
982 * from shuffling blocks around in the buffer. Take a
983 * look at this from an efficiency stand point later, but
984 * it should only happen once at the end of each job.
986 * I've been lseek()ing negative relative to SEEK_CUR for 30
987 * years now. However, it seems that with the new off_t definition,
988 * it is not possible to seek negative amounts, so we use two
989 * lseek(). One to get the position, then the second to do an
990 * absolute positioning -- so much for efficiency. KES Sep 02.
992 Dmsg0(200, "At end of read block\n");
993 if (block->read_len > block->block_len && !(dev->state & ST_TAPE)) {
994 off_t pos = lseek_dev(dev, (off_t)0, SEEK_CUR); /* get curr pos */
995 pos -= (block->read_len - block->block_len);
996 lseek_dev(dev, pos, SEEK_SET);
997 Dmsg2(200, "Did lseek blk_size=%d rdlen=%d\n", block->block_len,
999 dev->file_addr = pos;
1000 dev->file_size = pos;
1002 Dmsg2(200, "Exit read_block read_len=%d block_len=%d\n",
1003 block->read_len, block->block_len);
1004 block->block_read = true;