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);
37 static bool do_new_file_bookkeeping(DCR *dcr);
38 static bool do_dvd_size_checks(DCR *dcr);
41 * Dump the block header, then walk through
42 * the block printing out the record headers.
44 void dump_block(DEV_BLOCK *b, const char *msg)
48 char Id[BLKHDR_ID_LENGTH+1];
49 uint32_t CheckSum, BlockCheckSum;
52 uint32_t VolSessionId, VolSessionTime, data_len;
57 unser_begin(b->buf, BLKHDR1_LENGTH);
58 unser_uint32(CheckSum);
59 unser_uint32(block_len);
60 unser_uint32(BlockNumber);
61 unser_bytes(Id, BLKHDR_ID_LENGTH);
62 ASSERT(unser_length(b->buf) == BLKHDR1_LENGTH);
63 Id[BLKHDR_ID_LENGTH] = 0;
65 unser_uint32(VolSessionId);
66 unser_uint32(VolSessionTime);
70 VolSessionId = VolSessionTime = 0;
75 if (block_len > 100000) {
76 Dmsg3(20, "Dump block %s 0x%x blocksize too big %u\n", msg, b, block_len);
80 BlockCheckSum = bcrc32((uint8_t *)b->buf+BLKHDR_CS_LENGTH,
81 block_len-BLKHDR_CS_LENGTH);
82 Pmsg6(000, "Dump block %s %x: size=%d BlkNum=%d\n"
83 " Hdrcksum=%x cksum=%x\n",
84 msg, b, block_len, BlockNumber, CheckSum, BlockCheckSum);
86 while (p < (b->buf + block_len+WRITE_RECHDR_LENGTH)) {
87 unser_begin(p, WRITE_RECHDR_LENGTH);
88 if (rhl == RECHDR1_LENGTH) {
89 unser_uint32(VolSessionId);
90 unser_uint32(VolSessionTime);
92 unser_int32(FileIndex);
94 unser_uint32(data_len);
95 Pmsg6(000, " Rec: VId=%u VT=%u FI=%s Strm=%s len=%d p=%x\n",
96 VolSessionId, VolSessionTime, FI_to_ascii(FileIndex),
97 stream_to_ascii(Stream, FileIndex), data_len, p);
103 * Create a new block structure.
104 * We pass device so that the block can inherit the
105 * min and max block sizes.
107 DEV_BLOCK *new_block(DEVICE *dev)
109 DEV_BLOCK *block = (DEV_BLOCK *)get_memory(sizeof(DEV_BLOCK));
111 memset(block, 0, sizeof(DEV_BLOCK));
113 /* If the user has specified a max_block_size, use it as the default */
114 if (dev->max_block_size == 0) {
115 block->buf_len = DEFAULT_BLOCK_SIZE;
117 block->buf_len = dev->max_block_size;
120 block->block_len = block->buf_len; /* default block size */
121 block->buf = get_memory(block->buf_len);
123 block->BlockVer = BLOCK_VER; /* default write version */
124 Dmsg1(350, "Returning new block=%x\n", block);
130 * Duplicate an existing block (eblock)
132 DEV_BLOCK *dup_block(DEV_BLOCK *eblock)
134 DEV_BLOCK *block = (DEV_BLOCK *)get_memory(sizeof(DEV_BLOCK));
135 int buf_len = sizeof_pool_memory(eblock->buf);
137 memcpy(block, eblock, sizeof(DEV_BLOCK));
138 block->buf = get_memory(buf_len);
139 memcpy(block->buf, eblock->buf, buf_len);
145 * Only the first block checksum error was reported.
146 * If there are more, report it now.
148 void print_block_read_errors(JCR *jcr, DEV_BLOCK *block)
150 if (block->read_errors > 1) {
151 Jmsg(jcr, M_ERROR, 0, _("%d block read errors not printed.\n"),
159 void free_block(DEV_BLOCK *block)
161 Dmsg1(199, "free_block buffer %x\n", block->buf);
162 free_memory(block->buf);
163 Dmsg1(199, "free_block block %x\n", block);
164 free_memory((POOLMEM *)block);
167 /* Empty the block -- for writing */
168 void empty_block(DEV_BLOCK *block)
170 block->binbuf = WRITE_BLKHDR_LENGTH;
171 block->bufp = block->buf + block->binbuf;
173 block->write_failed = false;
174 block->block_read = false;
175 block->FirstIndex = block->LastIndex = 0;
179 * Create block header just before write. The space
180 * in the buffer should have already been reserved by
183 void ser_block_header(DEV_BLOCK *block)
186 uint32_t CheckSum = 0;
187 uint32_t block_len = block->binbuf;
189 Dmsg1(390, "ser_block_header: block_len=%d\n", block_len);
190 ser_begin(block->buf, BLKHDR2_LENGTH);
191 ser_uint32(CheckSum);
192 ser_uint32(block_len);
193 ser_uint32(block->BlockNumber);
194 ser_bytes(WRITE_BLKHDR_ID, BLKHDR_ID_LENGTH);
195 if (BLOCK_VER >= 2) {
196 ser_uint32(block->VolSessionId);
197 ser_uint32(block->VolSessionTime);
200 /* Checksum whole block except for the checksum */
201 CheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH,
202 block_len-BLKHDR_CS_LENGTH);
203 Dmsg1(390, "ser_bloc_header: checksum=%x\n", CheckSum);
204 ser_begin(block->buf, BLKHDR2_LENGTH);
205 ser_uint32(CheckSum); /* now add checksum to block header */
209 * Unserialize the block header for reading block.
210 * This includes setting all the buffer pointers correctly.
212 * Returns: false on failure (not a block)
215 static bool unser_block_header(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
218 char Id[BLKHDR_ID_LENGTH+1];
219 uint32_t CheckSum, BlockCheckSum;
222 uint32_t BlockNumber;
225 unser_begin(block->buf, BLKHDR_LENGTH);
226 unser_uint32(CheckSum);
227 unser_uint32(block_len);
228 unser_uint32(BlockNumber);
229 unser_bytes(Id, BLKHDR_ID_LENGTH);
230 ASSERT(unser_length(block->buf) == BLKHDR1_LENGTH);
232 Id[BLKHDR_ID_LENGTH] = 0;
234 bhl = BLKHDR1_LENGTH;
236 block->bufp = block->buf + bhl;
237 if (strncmp(Id, BLKHDR1_ID, BLKHDR_ID_LENGTH) != 0) {
238 dev->dev_errno = EIO;
239 Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
240 dev->file, dev->block_num, BLKHDR1_ID, Id);
241 if (block->read_errors == 0 || verbose >= 2) {
242 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
244 block->read_errors++;
247 } else if (Id[3] == '2') {
248 unser_uint32(block->VolSessionId);
249 unser_uint32(block->VolSessionTime);
250 bhl = BLKHDR2_LENGTH;
252 block->bufp = block->buf + bhl;
253 if (strncmp(Id, BLKHDR2_ID, BLKHDR_ID_LENGTH) != 0) {
254 dev->dev_errno = EIO;
255 Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
256 dev->file, dev->block_num, BLKHDR2_ID, Id);
257 if (block->read_errors == 0 || verbose >= 2) {
258 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
260 block->read_errors++;
264 dev->dev_errno = EIO;
265 Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
266 dev->file, dev->block_num, BLKHDR2_ID, Id);
267 if (block->read_errors == 0 || verbose >= 2) {
268 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
270 block->read_errors++;
271 unser_uint32(block->VolSessionId);
272 unser_uint32(block->VolSessionTime);
277 if (block_len > MAX_BLOCK_LENGTH) {
278 dev->dev_errno = EIO;
279 Mmsg3(dev->errmsg, _("Volume data error at %u:%u! Block length %u is insane (too large), probably due to a bad archive.\n"),
280 dev->file, dev->block_num, block_len);
281 if (block->read_errors == 0 || verbose >= 2) {
282 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
284 block->read_errors++;
288 Dmsg1(390, "unser_block_header block_len=%d\n", block_len);
289 /* Find end of block or end of buffer whichever is smaller */
290 if (block_len > block->read_len) {
291 block_end = block->read_len;
293 block_end = block_len;
295 block->binbuf = block_end - bhl;
296 block->block_len = block_len;
297 block->BlockNumber = BlockNumber;
298 Dmsg3(390, "Read binbuf = %d %d block_len=%d\n", block->binbuf,
300 if (block_len <= block->read_len) {
301 BlockCheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH,
302 block_len-BLKHDR_CS_LENGTH);
303 if (BlockCheckSum != CheckSum) {
304 dev->dev_errno = EIO;
305 Mmsg6(dev->errmsg, _("Volume data error at %u:%u!\n"
306 "Block checksum mismatch in block=%u len=%d: calc=%x blk=%x\n"),
307 dev->file, dev->block_num, (unsigned)BlockNumber,
308 block_len, BlockCheckSum, CheckSum);
309 if (block->read_errors == 0 || verbose >= 2) {
310 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
312 block->read_errors++;
322 * Write a block to the device, with locking and unlocking
324 * Returns: true on success
328 bool write_block_to_device(DCR *dcr)
331 DEVICE *dev = dcr->dev;
335 stat = write_block_to_spool_file(dcr);
339 if (!dcr->dev_locked) {
344 * If a new volume has been mounted since our last write
345 * Create a JobMedia record for the previous volume written,
346 * and set new parameters to write this volume
347 * The same applies for if we are in a new file.
349 if (dcr->NewVol || dcr->NewFile) {
350 if (job_canceled(jcr)) {
354 /* Create a jobmedia record for this job */
355 if (!dir_create_jobmedia_record(dcr)) {
356 dev->dev_errno = EIO;
357 Jmsg(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
358 dcr->VolCatInfo.VolCatName, jcr->Job);
359 set_new_volume_parameters(dcr);
364 /* Note, setting a new volume also handles any pending new file */
365 set_new_volume_parameters(dcr);
366 dcr->NewFile = false; /* this handled for new file too */
368 set_new_file_parameters(dcr);
372 if (!write_block_to_dev(dcr)) {
373 if (job_canceled(jcr) || jcr->JobType == JT_SYSTEM) {
376 stat = fixup_device_block_write_error(dcr);
381 if (!dcr->dev_locked) {
388 * Write a block to the device
390 * Returns: true on success or EOT
391 * false on hard error
393 bool write_block_to_dev(DCR *dcr)
396 uint32_t wlen; /* length to write */
397 int hit_max1, hit_max2;
399 DEVICE *dev = dcr->dev;
401 DEV_BLOCK *block = dcr->block;
403 #ifdef NO_TAPE_WRITE_TEST
407 ASSERT(block->binbuf == ((uint32_t) (block->bufp - block->buf)));
409 /* dump_block(block, "before write"); */
410 if (dev->state & ST_WEOT) {
411 Dmsg0(100, "return write_block_to_dev with ST_WEOT\n");
412 dev->dev_errno = ENOSPC;
413 Jmsg(jcr, M_FATAL, 0, _("Cannot write block. Device at EOM.\n"));
416 if (!dev->can_append()) {
417 dev->dev_errno = EIO;
418 Jmsg(jcr, M_FATAL, 0, _("Attempt to write on read-only Volume.\n"));
421 wlen = block->binbuf;
422 if (wlen <= WRITE_BLKHDR_LENGTH) { /* Does block have data in it? */
423 Dmsg0(100, "return write_block_to_dev no data to write\n");
427 * Clear to the end of the buffer if it is not full,
428 * and on tape devices, apply min and fixed blocking.
430 if (wlen != block->buf_len) {
431 uint32_t blen; /* current buffer length */
433 Dmsg2(200, "binbuf=%d buf_len=%d\n", block->binbuf, block->buf_len);
436 /* Adjust write size to min/max for tapes only */
437 if (dev->state & ST_TAPE) {
438 /* check for fixed block size */
439 if (dev->min_block_size == dev->max_block_size) {
440 wlen = block->buf_len; /* fixed block size already rounded */
441 /* Check for min block size */
442 } else if (wlen < dev->min_block_size) {
443 wlen = ((dev->min_block_size + TAPE_BSIZE - 1) / TAPE_BSIZE) * TAPE_BSIZE;
444 /* Ensure size is rounded */
446 wlen = ((wlen + TAPE_BSIZE - 1) / TAPE_BSIZE) * TAPE_BSIZE;
450 memset(block->bufp, 0, wlen-blen); /* clear garbage */
454 ser_block_header(block);
456 /* Limit maximum Volume size to value specified by user */
457 hit_max1 = (dev->max_volume_size > 0) &&
458 ((dev->VolCatInfo.VolCatBytes + block->binbuf)) >= dev->max_volume_size;
459 hit_max2 = (dev->VolCatInfo.VolCatMaxBytes > 0) &&
460 ((dev->VolCatInfo.VolCatBytes + block->binbuf)) >= dev->VolCatInfo.VolCatMaxBytes;
461 if (hit_max1 || hit_max2) {
464 Dmsg0(10, "==== Output bytes Triggered medium max capacity.\n");
466 max_cap = dev->max_volume_size;
468 max_cap = dev->VolCatInfo.VolCatMaxBytes;
470 Jmsg(jcr, M_INFO, 0, _("User defined maximum volume capacity %s exceeded on device %s.\n"),
471 edit_uint64_with_commas(max_cap, ed1), dev->dev_name);
472 terminate_writing_volume(dcr);
473 dev->dev_errno = ENOSPC;
477 /* Limit maximum File size on volume to user specified value */
478 if ((dev->max_file_size > 0) &&
479 (dev->file_size+block->binbuf) >= dev->max_file_size) {
480 dev->file_size = 0; /* reset file size */
482 if (weof_dev(dev, 1) != 0) { /* write eof */
483 Dmsg0(190, "WEOF error in max file size.\n");
484 Jmsg(jcr, M_FATAL, 0, _("Unable to write EOF. ERR=%s\n"),
486 terminate_writing_volume(dcr);
487 dev->dev_errno = ENOSPC;
491 if (!do_new_file_bookkeeping(dcr)) {
492 /* Error message already sent */
497 if (!do_dvd_size_checks(dcr)) {
498 /* Error message already sent */
502 dev->VolCatInfo.VolCatWrites++;
503 Dmsg1(300, "Write block of %u bytes\n", wlen);
504 #ifdef DEBUG_BLOCK_ZEROING
505 uint32_t *bp = (uint32_t *)block->buf;
506 if (bp[0] == 0 && bp[1] == 0 && bp[2] == 0 && block->buf[12] == 0) {
507 Jmsg0(jcr, M_ABORT, 0, "Write block header zeroed.\n");
511 stat = write(dev->fd, block->buf, (size_t)wlen);
513 #ifdef DEBUG_BLOCK_ZEROING
514 if (bp[0] == 0 && bp[1] == 0 && bp[2] == 0 && block->buf[12] == 0) {
515 Jmsg0(jcr, M_ABORT, 0, "Write block header zeroed.\n");
519 if (stat != (ssize_t)wlen) {
520 /* Some devices simply report EIO when the volume is full.
521 * With a little more thought we may be able to check
522 * capacity and distinguish real errors and EOT
523 * conditions. In any case, we probably want to
524 * simulate an End of Medium.
528 clrerror_dev(dev, -1);
529 if (dev->dev_errno == 0) {
530 dev->dev_errno = ENOSPC; /* out of space */
532 if (dev->dev_errno != ENOSPC) {
533 Jmsg4(jcr, M_ERROR, 0, _("Write error at %u:%u on device %s. ERR=%s.\n"),
534 dev->file, dev->block_num, dev->dev_name, be.strerror());
537 dev->dev_errno = ENOSPC; /* out of space */
539 if (dev->dev_errno == ENOSPC) {
540 Jmsg(jcr, M_INFO, 0, _("End of Volume \"%s\" at %u:%u on device %s. Write of %u bytes got %d.\n"),
541 dev->VolCatInfo.VolCatName,
542 dev->file, dev->block_num, dev->dev_name, wlen, stat);
544 Dmsg6(100, "=== Write error. size=%u rtn=%d dev_blk=%d blk_blk=%d errno=%d: ERR=%s\n",
545 wlen, stat, dev->block_num, block->BlockNumber, dev->dev_errno, strerror(dev->dev_errno));
547 ok = terminate_writing_volume(dcr);
548 if (!ok && !forge_on) {
552 #define CHECK_LAST_BLOCK
553 #ifdef CHECK_LAST_BLOCK
555 * If the device is a tape and it supports backspace record,
556 * we backspace over one or two eof marks depending on
557 * how many we just wrote, then over the last record,
558 * then re-read it and verify that the block number is
561 if (ok && (dev->state & ST_TAPE) && dev_cap(dev, CAP_BSR)) {
562 /* Now back up over what we wrote and read the last block */
563 if (!bsf_dev(dev, 1)) {
565 Jmsg(jcr, M_ERROR, 0, _("Backspace file at EOT failed. ERR=%s\n"), strerror(dev->dev_errno));
567 if (ok && dev_cap(dev, CAP_TWOEOF) && !bsf_dev(dev, 1)) {
569 Jmsg(jcr, M_ERROR, 0, _("Backspace file at EOT failed. ERR=%s\n"), strerror(dev->dev_errno));
571 /* Backspace over record */
572 if (ok && !bsr_dev(dev, 1)) {
574 Jmsg(jcr, M_ERROR, 0, _("Backspace record at EOT failed. ERR=%s\n"), strerror(dev->dev_errno));
576 * On FreeBSD systems, if the user got here, it is likely that his/her
577 * tape drive is "frozen". The correct thing to do is a
578 * rewind(), but if we do that, higher levels in cleaning up, will
579 * most likely write the EOS record over the beginning of the
580 * tape. The rewind *is* done later in mount.c when another
581 * tape is requested. Note, the clrerror_dev() call in bsr_dev()
582 * calls ioctl(MTCERRSTAT), which *should* fix the problem.
586 DEV_BLOCK *lblock = new_block(dev);
587 /* Note, this can destroy dev->errmsg */
589 if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
590 Jmsg(jcr, M_ERROR, 0, _("Re-read last block at EOT failed. ERR=%s"), dev->errmsg);
592 if (lblock->BlockNumber+1 == block->BlockNumber) {
593 Jmsg(jcr, M_INFO, 0, _("Re-read of last block succeeded.\n"));
595 Jmsg(jcr, M_ERROR, 0, _(
596 "Re-read of last block failed. Last block=%u Current block=%u.\n"),
597 lblock->BlockNumber, block->BlockNumber);
608 /* We successfully wrote the block, now do housekeeping */
610 dev->VolCatInfo.VolCatBytes += block->binbuf;
611 dev->VolCatInfo.VolCatBlocks++;
612 dev->EndBlock = dev->block_num;
613 dev->EndFile = dev->file;
615 block->BlockNumber++;
617 /* Update dcr values */
618 if (dev_state(dev, ST_TAPE)) {
619 dcr->EndBlock = dev->EndBlock;
620 dcr->EndFile = dev->EndFile;
622 /* Save address of start of block just written */
623 dcr->EndBlock = (uint32_t)dev->file_addr;
624 dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
626 if (dcr->VolFirstIndex == 0 && block->FirstIndex > 0) {
627 dcr->VolFirstIndex = block->FirstIndex;
629 if (block->LastIndex > 0) {
630 dcr->VolLastIndex = block->LastIndex;
632 dcr->WroteVol = true;
633 dev->file_addr += wlen; /* update file address */
634 dev->file_size += wlen;
635 dev->part_size += wlen;
637 Dmsg2(300, "write_block: wrote block %d bytes=%d\n", dev->block_num, wlen);
642 static bool terminate_writing_volume(DCR *dcr)
644 DEVICE *dev = dcr->dev;
647 /* Create a JobMedia record to indicated end of tape */
648 dev->VolCatInfo.VolCatFiles = dev->file;
649 if (!dir_create_jobmedia_record(dcr)) {
650 Dmsg0(190, "Error from create JobMedia\n");
651 dev->dev_errno = EIO;
652 Jmsg(dcr->jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
653 dcr->VolCatInfo.VolCatName, dcr->jcr->Job);
657 dcr->block->write_failed = true;
658 if (weof_dev(dev, 1) != 0) { /* end the tape */
659 dev->VolCatInfo.VolCatErrors++;
660 Jmsg(dcr->jcr, M_ERROR, 0, "Error writing final EOF to tape. This tape may not be readable.\n"
663 Dmsg0(100, "WEOF error.\n");
665 dev->VolCatInfo.VolCatFiles = dev->file;
667 if (dev->is_dvd()) { /* Write the current (and last) part. */
671 if (!dir_update_volume_info(dcr, false)) {
674 Dmsg1(100, "dir_update_volume_info terminate writing -- %s\n", ok?"OK":"ERROR");
678 * Walk through all attached dcrs setting flag to call
679 * set_new_file_parameters() when that dcr is next used.
682 foreach_dlist(mdcr, dev->attached_dcrs) {
683 if (mdcr->jcr->JobId == 0) {
686 mdcr->NewFile = true; /* set reminder to do set_new_file_params */
688 /* Set new file/block parameters for current dcr */
689 set_new_file_parameters(dcr);
691 if (ok && dev_cap(dev, CAP_TWOEOF) && weof_dev(dev, 1) != 0) { /* end the tape */
692 dev->VolCatInfo.VolCatErrors++;
693 /* This may not be fatal since we already wrote an EOF */
694 Jmsg(dcr->jcr, M_ERROR, 0, "%s", dev->errmsg);
698 Dmsg1(100, "Leave terminate_writing_volume -- %s\n", ok?"OK":"ERROR");
703 * Do bookkeeping when a new file is created on a Volume. This is
704 * also done for disk files to generate the jobmedia records for
707 static bool do_new_file_bookkeeping(DCR *dcr)
709 DEVICE *dev = dcr->dev;
712 /* Create a JobMedia record so restore can seek */
713 if (!dir_create_jobmedia_record(dcr)) {
714 Dmsg0(190, "Error from create_job_media.\n");
715 dev->dev_errno = EIO;
716 Jmsg(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
717 dcr->VolCatInfo.VolCatName, jcr->Job);
718 terminate_writing_volume(dcr);
719 dev->dev_errno = EIO;
722 dev->VolCatInfo.VolCatFiles = dev->file;
723 if (!dir_update_volume_info(dcr, false)) {
724 Dmsg0(190, "Error from update_vol_info.\n");
725 terminate_writing_volume(dcr);
726 dev->dev_errno = EIO;
729 Dmsg0(100, "dir_update_volume_info max file size -- OK\n");
732 * Walk through all attached dcrs setting flag to call
733 * set_new_file_parameters() when that dcr is next used.
736 foreach_dlist(mdcr, dev->attached_dcrs) {
737 if (mdcr->jcr->JobId == 0) {
740 mdcr->NewFile = true; /* set reminder to do set_new_file_params */
742 /* Set new file/block parameters for current dcr */
743 set_new_file_parameters(dcr);
748 * Do all checks for DVD sizes during writing.
750 static bool do_dvd_size_checks(DCR *dcr)
752 DEVICE *dev = dcr->dev;
754 DEV_BLOCK *block = dcr->block;
756 /* Limit maximum part size to value specified by user (not applicable to tapes/fifos) */
757 if (!(dev->state & (ST_TAPE|ST_FIFO)) && dev->max_part_size > 0 &&
758 (dev->part_size + block->binbuf) >= dev->max_part_size) {
759 if (dev->part < dev->num_parts) {
760 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"),
761 dev->part, dev->num_parts, dev_name(dev));
762 dev->dev_errno = EIO;
766 if (open_next_part(dev) < 0) {
767 Jmsg2(dcr->jcr, M_FATAL, 0, _("Unable to open device next part %s. ERR=%s\n"),
768 dev_name(dev), strerror_dev(dev));
769 dev->dev_errno = EIO;
773 dev->VolCatInfo.VolCatParts = dev->num_parts;
775 if (!dir_update_volume_info(dcr, false)) {
776 Dmsg0(190, "Error from update_vol_info.\n");
777 dev->dev_errno = EIO;
782 if (dev->free_space_errno < 0) { /* Error while getting free space */
783 char ed1[50], ed2[50];
784 Dmsg1(10, "Cannot get free space on the device ERR=%s.\n", dev->errmsg);
785 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"),
786 dev->VolCatInfo.VolCatName,
787 dev->file, dev->block_num, dev->dev_name,
788 edit_uint64_with_commas(dev->part_size, ed1), edit_uint64_with_commas(dev->free_space, ed2),
789 dev->free_space_errno, dev->errmsg);
790 dev->dev_errno = -dev->free_space_errno;
794 if ((dev->free_space_errno > 0 && (dev->part_size + block->binbuf) >= dev->free_space)) {
795 char ed1[50], ed2[50];
796 Dmsg0(10, "==== Just enough free space on the device to write the current part...\n");
797 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"),
798 dev->VolCatInfo.VolCatName,
799 dev->file, dev->block_num, dev->dev_name,
800 edit_uint64_with_commas(dev->part_size, ed1), edit_uint64_with_commas(dev->free_space, ed2),
801 dev->free_space_errno);
802 terminate_writing_volume(dcr);
803 dev->dev_errno = ENOSPC;
811 * Read block with locking
814 bool read_block_from_device(DCR *dcr, bool check_block_numbers)
817 DEVICE *dev = dcr->dev;
818 Dmsg0(200, "Enter read_block_from_device\n");
820 stat = read_block_from_dev(dcr, check_block_numbers);
822 Dmsg0(200, "Leave read_block_from_device\n");
827 * Read the next block into the block structure and unserialize
828 * the block header. For a file, the block may be partially
829 * or completely in the current buffer.
831 bool read_block_from_dev(DCR *dcr, bool check_block_numbers)
835 uint32_t BlockNumber;
838 DEVICE *dev = dcr->dev;
839 DEV_BLOCK *block = dcr->block;
841 if (dev_state(dev, ST_EOT)) {
845 Dmsg1(200, "Full read() in read_block_from_device() len=%d\n",
849 dev->dev_errno = EIO;
850 Mmsg1(dev->errmsg, _("Block buffer size looping problem on device %s\n"),
852 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
857 /*Dmsg1(200, "dev->file_size=%u\n",(unsigned int)dev->file_size);
858 Dmsg1(200, "lseek=%u\n",(unsigned int)lseek(dev->fd, 0, SEEK_CUR));
859 Dmsg1(200, "dev->part_start=%u\n",(unsigned int)dev->part_start);
860 Dmsg1(200, "dev->file_size-dev->part_start=%u\n",(unsigned int)dev->file_size-dev->part_start);
861 Dmsg1(200, "dev->part_size=%u\n", (unsigned int)dev->part_size);
862 Dmsg1(200, "dev->part=%u\n", (unsigned int)dev->part);
863 Dmsg1(200, "dev->VolCatInfo.VolCatParts=%u\n", (unsigned int)dev->VolCatInfo.VolCatParts);
864 Dmsg3(200, "Tests : %d %d %d\n", (dev->VolCatInfo.VolCatParts > 0), ((dev->file_size-dev->part_start) == dev->part_size), (dev->part <= dev->VolCatInfo.VolCatParts));*/
865 /* Check for part file end */
866 if ((dev->num_parts > 0) &&
867 ((dev->file_size-dev->part_start) == dev->part_size) &&
868 (dev->part < dev->num_parts)) {
869 if (open_next_part(dev) < 0) {
870 Jmsg2(dcr->jcr, M_FATAL, 0, _("Unable to open device next part %s. ERR=%s\n"),
871 dev_name(dev), strerror_dev(dev));
872 dev->dev_errno = EIO;
879 // uint32_t *bp = (uint32_t *)block->buf;
880 // Dmsg3(000, "Read %p %u at %llu\n", block->buf, block->buf_len, lseek(dev->fd, 0, SEEK_CUR));
882 stat = read(dev->fd, block->buf, (size_t)block->buf_len);
884 // Dmsg8(000, "stat=%d Csum=%u blen=%u bnum=%u %c%c%c%c\n",stat, bp[0],bp[1],bp[2],
885 // block->buf[12],block->buf[13],block->buf[14],block->buf[15]);
888 dev->VolCatInfo.VolCatErrors++;
890 } while (stat == -1 && (errno == EINTR || errno == EIO) && retry++ < 11);
893 clrerror_dev(dev, -1);
894 Dmsg1(200, "Read device got: ERR=%s\n", be.strerror());
896 Mmsg4(dev->errmsg, _("Read error at file:blk %u:%u on device %s. ERR=%s.\n"),
897 dev->file, dev->block_num, dev->dev_name, be.strerror());
898 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
899 if (dev->at_eof()) { /* EOF just seen? */
900 dev->state |= ST_EOT; /* yes, error => EOT */
904 Dmsg3(200, "Read device got %d bytes at %u:%u\n", stat,
905 dev->file, dev->block_num);
906 if (stat == 0) { /* Got EOF ! */
909 Mmsg3(dev->errmsg, _("Read zero bytes at %u:%u on device %s.\n"),
910 dev->file, dev->block_num, dev->dev_name);
911 if (dev->at_eof()) { /* EOF already read? */
912 dev->state |= ST_EOT; /* yes, 2 EOFs => EOT */
916 return false; /* return eof */
918 /* Continue here for successful read */
919 block->read_len = stat; /* save length read */
920 if (block->read_len < BLKHDR2_LENGTH) {
921 dev->dev_errno = EIO;
922 Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Very short block of %d bytes on device %s discarded.\n"),
923 dev->file, dev->block_num, block->read_len, dev->dev_name);
924 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
925 dev->state |= ST_SHORT; /* set short block */
926 block->read_len = block->binbuf = 0;
927 return false; /* return error */
930 BlockNumber = block->BlockNumber + 1;
931 if (!unser_block_header(jcr, dev, block)) {
933 dev->file_addr += block->read_len;
934 dev->file_size += block->read_len;
941 * If the block is bigger than the buffer, we reposition for
942 * re-reading the block, allocate a buffer of the correct size,
945 if (block->block_len > block->buf_len) {
946 dev->dev_errno = EIO;
947 Mmsg2(dev->errmsg, _("Block length %u is greater than buffer %u. Attempting recovery.\n"),
948 block->block_len, block->buf_len);
949 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
950 Pmsg1(000, "%s", dev->errmsg);
951 /* Attempt to reposition to re-read the block */
952 if (dev->state & ST_TAPE) {
953 Dmsg0(200, "BSR for reread; block too big for buffer.\n");
954 if (!bsr_dev(dev, 1)) {
955 Jmsg(jcr, M_ERROR, 0, "%s", strerror_dev(dev));
960 Dmsg0(200, "Seek to beginning of block for reread.\n");
961 off_t pos = lseek_dev(dev, (off_t)0, SEEK_CUR); /* get curr pos */
962 pos -= block->read_len;
963 lseek_dev(dev, pos, SEEK_SET);
964 dev->file_addr = pos;
966 Mmsg1(dev->errmsg, _("Setting block buffer size to %u bytes.\n"), block->block_len);
967 Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
968 Pmsg1(000, "%s", dev->errmsg);
969 /* Set new block length */
970 dev->max_block_size = block->block_len;
971 block->buf_len = block->block_len;
972 free_memory(block->buf);
973 block->buf = get_memory(block->buf_len);
976 goto reread; /* re-read block with correct block size */
979 if (block->block_len > block->read_len) {
980 dev->dev_errno = EIO;
981 Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Short block of %d bytes on device %s discarded.\n"),
982 dev->file, dev->block_num, block->read_len, dev->dev_name);
983 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
984 dev->state |= ST_SHORT; /* set short block */
985 block->read_len = block->binbuf = 0;
986 return false; /* return error */
989 dev->state &= ~(ST_EOF|ST_SHORT); /* clear EOF and short block */
990 dev->VolCatInfo.VolCatReads++;
991 dev->VolCatInfo.VolCatRBytes += block->read_len;
993 dev->VolCatInfo.VolCatBytes += block->block_len;
994 dev->VolCatInfo.VolCatBlocks++;
995 dev->EndBlock = dev->block_num;
996 dev->EndFile = dev->file;
999 /* Update dcr values */
1000 if (dev->state & ST_TAPE) {
1001 dcr->EndBlock = dev->EndBlock;
1002 dcr->EndFile = dev->EndFile;
1004 dcr->EndBlock = (uint32_t)dev->file_addr;
1005 dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
1006 dev->block_num = dcr->EndBlock;
1007 dev->file = dcr->EndFile;
1009 dev->file_addr += block->block_len;
1010 dev->file_size += block->block_len;
1013 * If we read a short block on disk,
1014 * seek to beginning of next block. This saves us
1015 * from shuffling blocks around in the buffer. Take a
1016 * look at this from an efficiency stand point later, but
1017 * it should only happen once at the end of each job.
1019 * I've been lseek()ing negative relative to SEEK_CUR for 30
1020 * years now. However, it seems that with the new off_t definition,
1021 * it is not possible to seek negative amounts, so we use two
1022 * lseek(). One to get the position, then the second to do an
1023 * absolute positioning -- so much for efficiency. KES Sep 02.
1025 Dmsg0(200, "At end of read block\n");
1026 if (block->read_len > block->block_len && !dev->is_tape()) {
1028 off_t pos = lseek_dev(dev, (off_t)0, SEEK_CUR); /* get curr pos */
1029 pos -= (block->read_len - block->block_len);
1030 lseek_dev(dev, pos, SEEK_SET);
1031 Dmsg3(200, "Did lseek pos=%s blk_size=%d rdlen=%d\n",
1032 edit_uint64(pos, ed1), block->block_len,
1034 dev->file_addr = pos;
1035 dev->file_size = pos;
1037 Dmsg2(200, "Exit read_block read_len=%d block_len=%d\n",
1038 block->read_len, block->block_len);
1039 block->block_read = true;