2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
5 Copyright (C) 2001-2014 Free Software Foundation Europe e.V.
7 The original author of Bacula is Kern Sibbald, with contributions
8 from many others, a complete list can be found in the file AUTHORS.
10 You may use this file and others of this release according to the
11 license defined in the LICENSE file, which includes the Affero General
12 Public License, v3.0 ("AGPLv3") and some additional permissions and
13 terms pursuant to its AGPLv3 Section 7.
15 This notice must be preserved when any source code is
16 conveyed and/or propagated.
18 Bacula(R) is a registered trademark of Kern Sibbald.
22 * block.c -- tape block handling functions
24 * Kern Sibbald, March MMI
25 * added BB02 format October MMII
33 #ifdef DEBUG_BLOCK_CHECKSUM
34 static const bool debug_block_checksum = true;
36 static const bool debug_block_checksum = false;
39 #ifdef NO_TAPE_WRITE_TEST
40 static const bool no_tape_write_test = true;
42 static const bool no_tape_write_test = false;
46 bool do_new_file_bookkeeping(DCR *dcr);
47 //bool do_dvd_size_checks(DCR *dcr);
48 void reread_last_block(DCR *dcr);
49 uint32_t get_len_and_clear_block(DEV_BLOCK *block, DEVICE *dev, uint32_t &pad);
50 uint32_t ser_block_header(DEV_BLOCK *block, bool do_checksum);
51 bool unser_block_header(JCR *jcr, DEVICE *dev, DEV_BLOCK *block);
54 * Write a block to the device, with locking and unlocking
56 * Returns: true on success
60 bool DCR::write_block_to_device(bool final)
66 Dmsg0(200, "Write to spool\n");
67 ok = write_block_to_spool_file(dcr);
71 if (!is_dev_locked()) { /* device already locked? */
72 /* note, do not change this to dcr->rLock */
73 dev->rLock(false); /* no, lock it */
76 if (!check_for_newvol_or_newfile(dcr)) {
78 goto bail_out; /* fatal error */
81 Dmsg1(500, "Write block to dev=%p\n", dcr->dev);
82 if (!write_block_to_dev()) {
83 Dmsg1(40, "*** Failed write_block_to_dev block=%p\n", block);
84 if (job_canceled(jcr) || jcr->getJobType() == JT_SYSTEM) {
86 Dmsg2(40, "cancel=%d or SYSTEM=%d\n", job_canceled(jcr),
87 jcr->getJobType() == JT_SYSTEM);
89 ok = fixup_device_block_write_error(dcr);
92 if (ok && final && !dir_create_jobmedia_record(dcr)) {
93 Jmsg(jcr, M_FATAL, 0, _("Error writing final JobMedia record to catalog.\n"));
97 if (!dcr->is_dev_locked()) { /* did we lock dev above? */
98 /* note, do not change this to dcr->dunlock */
99 dev->Unlock(); /* unlock it now */
105 * Write a block to the device
107 * Returns: true on success or EOT
108 * false on hard error
110 bool DCR::write_block_to_dev()
113 uint32_t wlen; /* length to write */
117 uint32_t pad; /* padding or zeros written */
119 if (no_tape_write_test) {
123 if (job_canceled(jcr)) {
127 Dmsg3(200, "fd=%d bufp-buf=%d binbuf=%d\n", dev->fd(),
128 block->bufp-block->buf, block->binbuf);
129 ASSERT2(block->binbuf == ((uint32_t)(block->bufp - block->buf)), "binbuf badly set");
131 if (is_block_empty(block)) { /* Does block have data in it? */
132 Dmsg0(150, "return write_block_to_dev no data to write\n");
136 dump_block(block, "before write");
137 if (dev->at_weot()) {
138 Dmsg0(50, "==== FATAL: At EOM with ST_WEOT.\n");
139 dev->dev_errno = ENOSPC;
140 Jmsg1(jcr, M_FATAL, 0, _("Cannot write block. Device at EOM. dev=%s\n"), dev->print_name());
143 if (!dev->can_append()) {
144 dev->dev_errno = EIO;
145 Jmsg1(jcr, M_FATAL, 0, _("Attempt to write on read-only Volume. dev=%s\n"), dev->print_name());
146 Dmsg1(50, "Attempt to write on read-only Volume. dev=%s\n", dev->print_name());
150 if (!dev->is_open()) {
151 Jmsg1(jcr, M_FATAL, 0, _("Attempt to write on closed device=%s\n"), dev->print_name());
152 Dmsg1(50, "Attempt to write on closed device=%s\n", dev->print_name());
156 wlen = get_len_and_clear_block(block, dev, pad);
157 block->block_len = wlen;
159 checksum = ser_block_header(block, dev->do_checksum());
161 if (user_volume_size_reached(dcr, true)) {
162 Dmsg0(40, "Calling terminate_writing_volume\n");
163 terminate_writing_volume(dcr);
164 reread_last_block(dcr); /* Only used on tapes */
165 dev->dev_errno = ENOSPC;
170 * Limit maximum File size on volume to user specified value.
171 * In practical terms, this means to put an EOF mark on
172 * a tape after every X bytes. This effectively determines
173 * how many index records we have (JobMedia). If you set
174 * max_file_size too small, it will cause a lot of shoe-shine
175 * on very fast modern tape (LTO-3 and above).
177 if ((dev->max_file_size > 0) &&
178 (dev->file_size+block->binbuf) >= dev->max_file_size) {
179 dev->file_size = 0; /* reset file size */
181 if (!dev->weof(1)) { /* write eof */
182 Dmsg0(50, "WEOF error in max file size.\n");
183 Jmsg(jcr, M_FATAL, 0, _("Unable to write EOF. ERR=%s\n"),
185 Dmsg0(40, "Calling terminate_writing_volume\n");
186 terminate_writing_volume(dcr);
187 dev->dev_errno = ENOSPC;
190 if (!write_ansi_ibm_labels(dcr, ANSI_EOF_LABEL, dev->VolHdr.VolumeName)) {
194 if (!do_new_file_bookkeeping(dcr)) {
195 /* Error message already sent */
200 dev->updateVolCatWrites(1);
202 #ifdef DEBUG_BLOCK_ZEROING
203 uint32_t *bp = (uint32_t *)block->buf;
204 if (bp[0] == 0 && bp[1] == 0 && bp[2] == 0 && block->buf[12] == 0) {
205 Jmsg0(jcr, M_ABORT, 0, _("Write block header zeroed.\n"));
210 * Do write here, make a somewhat feeble attempt to recover from
211 * I/O errors, or from the OS telling us it is busy.
217 if (retry > 0 && stat == -1 && errno == EBUSY) {
219 Dmsg4(100, "===== write retry=%d stat=%d errno=%d: ERR=%s\n",
220 retry, stat, errno, be.bstrerror());
221 bmicrosleep(5, 0); /* pause a bit if busy or lots of errors */
224 stat = dev->write(block->buf, (size_t)wlen);
225 Dmsg4(110, "Write() BlockAddr=%lld NextAddr=%lld Vol=%s wlen=%d\n",
226 block->BlockAddr, dev->lseek(dcr, 0, SEEK_CUR),
227 dev->VolHdr.VolumeName, wlen);
228 } while (stat == -1 && (errno == EBUSY || errno == EIO) && retry++ < 3);
230 if (debug_block_checksum) {
231 uint32_t achecksum = ser_block_header(block, dev->do_checksum());
232 if (checksum != achecksum) {
233 Jmsg2(jcr, M_ERROR, 0, _("Block checksum changed during write: before=%ud after=%ud\n"),
234 checksum, achecksum);
235 dump_block(block, "with checksum error");
239 #ifdef DEBUG_BLOCK_ZEROING
240 if (bp[0] == 0 && bp[1] == 0 && bp[2] == 0 && block->buf[12] == 0) {
241 Jmsg0(jcr, M_ABORT, 0, _("Write block header zeroed.\n"));
245 if (stat != (ssize_t)wlen) {
246 /* Some devices simply report EIO when the volume is full.
247 * With a little more thought we may be able to check
248 * capacity and distinguish real errors and EOT
249 * conditions. In any case, we probably want to
250 * simulate an End of Medium.
254 dev->clrerror(-1); /* saves errno in dev->dev_errno */
255 if (dev->dev_errno == 0) {
256 dev->dev_errno = ENOSPC; /* out of space */
258 if (dev->dev_errno != ENOSPC) {
259 dev->VolCatInfo.VolCatErrors++;
260 Jmsg4(jcr, M_ERROR, 0, _("Write error at %u:%u on device %s. ERR=%s.\n"),
261 dev->file, dev->block_num, dev->print_name(), be.bstrerror());
264 dev->dev_errno = ENOSPC; /* out of space */
266 if (dev->dev_errno == ENOSPC) {
267 dev->update_freespace();
268 if (dev->is_freespace_ok() && dev->free_space < dev->min_free_space) {
270 Jmsg(jcr, M_WARNING, 0, _("Out of freespace caused End of Volume \"%s\" at %u:%u on device %s. Write of %u bytes got %d.\n"),
271 dev->getVolCatName(),
272 dev->file, dev->block_num, dev->print_name(), wlen, stat);
274 dev->clear_nospace();
275 Jmsg(jcr, M_INFO, 0, _("End of Volume \"%s\" at %u:%u on device %s. Write of %u bytes got %d.\n"),
276 dev->getVolCatName(),
277 dev->file, dev->block_num, dev->print_name(), wlen, stat);
280 if (chk_dbglvl(100)) {
282 Dmsg7(90, "==== Write error. fd=%d size=%u rtn=%d dev_blk=%d blk_blk=%d errno=%d: ERR=%s\n",
283 dev->fd(), wlen, stat, dev->block_num, block->BlockNumber,
284 dev->dev_errno, be.bstrerror(dev->dev_errno));
287 Dmsg0(40, "Calling terminate_writing_volume\n");
288 ok = terminate_writing_volume(dcr);
290 reread_last_block(dcr);
295 /* We successfully wrote the block, now do housekeeping */
296 Dmsg2(1300, "VolCatBytes=%lld newVolCatBytes=%lld\n", dev->VolCatInfo.VolCatBytes,
297 (dev->VolCatInfo.VolCatBytes+wlen));
298 dev->updateVolCatBytes(wlen);
299 dev->updateVolCatBlocks(1);
300 dev->EndBlock = dev->block_num;
301 dev->EndFile = dev->file;
302 dev->LastBlock = block->BlockNumber;
303 block->BlockNumber++;
305 /* Update dcr values */
306 if (dev->is_tape()) {
307 if (dcr->EndFile != dev->EndFile || dev->EndBlock > dcr->EndBlock) {
308 dcr->EndBlock = dev->EndBlock;
309 dcr->EndFile = dev->EndFile;
313 /* Save address of block just written */
314 uint64_t addr = dev->file_addr + wlen - 1;
315 if (dcr->EndFile == (uint32_t)(addr >> 32) &&
316 (uint32_t)addr < dcr->EndBlock) {
317 Pmsg4(000, "Possible incorrect EndBlock oldEndBlock=%d newEndBlock=%d oldEndFile=%d newEndFile=%d\n",
318 dcr->EndBlock, (uint32_t)addr, dcr->EndFile, (uint32_t)(addr >> 32));
320 dcr->EndBlock = (uint32_t)addr;
321 dcr->EndFile = (uint32_t)(addr >> 32);
323 dev->block_num = (uint32_t)addr;
324 dev->file = (uint32_t)(addr >> 32);
325 block->BlockAddr = dev->file_addr + wlen;
326 Dmsg3(150, "Set block->BlockAddr=%lld wlen=%d block=%x\n",
327 block->BlockAddr, wlen, block);
329 dcr->VolMediaId = dev->VolCatInfo.VolMediaId;
330 Dmsg3(150, "VolFirstIndex=%d blockFirstIndex=%d Vol=%s\n",
331 dcr->VolFirstIndex, block->FirstIndex, dcr->VolumeName);
332 if (dcr->VolFirstIndex == 0 && block->FirstIndex > 0) {
333 dcr->VolFirstIndex = block->FirstIndex;
335 if (block->LastIndex > (int32_t)dcr->VolLastIndex) {
336 dcr->VolLastIndex = block->LastIndex;
338 dcr->WroteVol = true;
339 dev->file_addr += wlen; /* update file address */
340 dev->file_size += wlen;
341 dev->part_size += wlen;
342 dev->setVolCatInfo(false); /* Needs update */
344 Dmsg2(1300, "write_block: wrote block %d bytes=%d\n", dev->block_num, wlen);
351 * Read block with locking
354 bool DCR::read_block_from_device(bool check_block_numbers)
358 Dmsg0(250, "Enter read_block_from_device\n");
360 ok = read_block_from_dev(check_block_numbers);
362 Dmsg0(250, "Leave read_block_from_device\n");
366 static void set_block_position(DCR *dcr, DEVICE *dev, DEV_BLOCK *block)
368 if (dev->is_tape()) {
369 block->Block = dev->block_num;
370 block->File = dev->file;
372 block->Block = (uint32_t)dev->file_addr;
373 block->File = (uint32_t)(dev->file_addr >> 32);
379 * Read the next block into the block structure and unserialize
380 * the block header. For a file, the block may be partially
381 * or completely in the current buffer.
383 bool DCR::read_block_from_dev(bool check_block_numbers)
390 if (job_canceled(jcr)) {
391 Mmsg(dev->errmsg, _("Job failed or canceled.\n"));
397 Mmsg(dev->errmsg, _("Attempt to read past end of tape or file.\n"));
402 Dmsg1(250, "Full read in read_block_from_device() len=%d\n", block->buf_len);
404 if (!dev->is_open()) {
405 Mmsg4(dev->errmsg, _("Attempt to read closed device: fd=%d at file:blk %u:%u on device %s\n"),
406 dev->fd(), dev->file, dev->block_num, dev->print_name());
407 Jmsg(dcr->jcr, M_FATAL, 0, "%s", dev->errmsg);
408 Pmsg1(000, "%s", dev->errmsg);
413 set_block_position(dcr, dev, block);
417 dev->dev_errno = EIO;
418 Mmsg1(dev->errmsg, _("Block buffer size looping problem on device %s\n"),
420 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
425 dump_block(block, "before read");
428 /* Check for DVD part file end */
429 if (dev->at_eof() && dev->is_dvd() && dev->num_dvd_parts > 0 &&
430 dev->part <= dev->num_dvd_parts) {
431 Dmsg0(400, "Call dvd_open_next_part\n");
432 if (dvd_open_next_part(dcr) < 0) {
433 Mmsg3(dev->errmsg, _("Unable to open device part=%d %s: ERR=%s\n"),
434 dev->part, dev->print_name(), dev->bstrerror());
435 Jmsg(dcr->jcr, M_FATAL, 0, "%s", dev->errmsg);
436 dev->dev_errno = EIO;
440 #endif /* xxxBUILD_DVD */
446 boffset_t pos = dev->lseek(dcr, (boffset_t)0, SEEK_CUR); /* get curr pos */
448 if ((retry > 0 && stat == -1 && errno == EBUSY)) {
450 Dmsg4(100, "===== read retry=%d stat=%d errno=%d: ERR=%s\n",
451 retry, stat, errno, be.bstrerror());
452 bmicrosleep(10, 0); /* pause a bit if busy or lots of errors */
455 stat = dev->read(block->buf, (size_t)block->buf_len);
457 } while (stat == -1 && (errno == EBUSY || errno == EINTR || errno == EIO) && retry++ < 3);
458 Dmsg3(110, "Read() vol=%s nbytes=%d addr=%lld\n",
459 dev->VolHdr.VolumeName, stat, pos);
463 Dmsg2(90, "Read device fd=%d got: ERR=%s\n", dev->fd(), be.bstrerror());
465 if (dev->file == 0 && dev->block_num == 0) { /* Attempt to read label */
466 Mmsg(dev->errmsg, _("The Volume=%s on device=%s appears to be unlabeled.\n"),
467 "", dev->VolCatInfo.VolCatName, dev->print_name());
469 Mmsg5(dev->errmsg, _("Read error on fd=%d at file:blk %u:%u on device %s. ERR=%s.\n"),
470 dev->fd(), dev->file, dev->block_num, dev->print_name(), be.bstrerror());
472 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
473 if (dev->at_eof()) { /* EOF just seen? */
474 dev->set_eot(); /* yes, error => EOT */
478 if (stat == 0) { /* Got EOF ! */
479 if (dev->file == 0 && dev->block_num == 0) { /* Attempt to read label */
480 Mmsg3(dev->errmsg, _("The %sVolume=%s on device=%s appears to be unlabeled.\n"),
481 "", dev->VolCatInfo.VolCatName, dev->print_name());
483 Mmsg4(dev->errmsg, _("Read zero %sbytes Vol=%s at %lld on device %s.\n"),
484 "", dev->VolCatInfo.VolCatName, pos, dev->print_name());
488 Dmsg1(100, "%s", dev->errmsg);
489 if (dev->at_eof()) { /* EOF already read? */
490 dev->set_eot(); /* yes, 2 EOFs => EOT */
494 Dmsg2(150, "==== Read zero bytes. vol=%s at %lld\n", dev->VolCatInfo.VolCatName, pos);
495 return false; /* return eof */
498 /* Continue here for successful read */
500 block->read_len = stat; /* save length read */
501 if (block->read_len == 80 &&
502 (dcr->VolCatInfo.LabelType != B_BACULA_LABEL ||
503 dcr->device->label_type != B_BACULA_LABEL)) {
504 /* ***FIXME*** should check label */
505 Dmsg2(100, "Ignore 80 byte ANSI label at %u:%u\n", dev->file, dev->block_num);
507 goto reread; /* skip ANSI/IBM label */
510 if (block->read_len < BLKHDR2_LENGTH) {
511 dev->dev_errno = EIO;
512 Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Very short block of %d bytes on device %s discarded.\n"),
513 dev->file, dev->block_num, block->read_len, dev->print_name());
514 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
515 dev->set_short_block();
516 block->read_len = block->binbuf = 0;
517 Dmsg2(50, "set block=%p binbuf=%d\n", block, block->binbuf);
518 return false; /* return error */
521 //BlockNumber = block->BlockNumber + 1;
522 if (!unser_block_header(jcr, dev, block)) {
524 dev->file_addr += block->read_len;
525 dev->file_size += block->read_len;
532 * If the block is bigger than the buffer, we reposition for
533 * re-reading the block, allocate a buffer of the correct size,
536 Dmsg2(150, "block_len=%d buf_len=%d\n", block->block_len, block->buf_len);
537 if (block->block_len > block->buf_len) {
538 dev->dev_errno = EIO;
539 Mmsg2(dev->errmsg, _("Block length %u is greater than buffer %u. Attempting recovery.\n"),
540 block->block_len, block->buf_len);
541 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
542 Pmsg1(000, "%s", dev->errmsg);
543 /* Attempt to reposition to re-read the block */
544 if (dev->is_tape()) {
545 Dmsg0(250, "BSR for reread; block too big for buffer.\n");
547 Mmsg(dev->errmsg, "%s", dev->bstrerror());
548 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
553 Dmsg0(250, "Seek to beginning of block for reread.\n");
554 boffset_t pos = dev->lseek(dcr, (boffset_t)0, SEEK_CUR); /* get curr pos */
555 pos -= block->read_len;
556 dev->lseek(dcr, pos, SEEK_SET);
557 dev->file_addr = pos;
559 Mmsg1(dev->errmsg, _("Setting block buffer size to %u bytes.\n"), block->block_len);
560 Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
561 Pmsg1(000, "%s", dev->errmsg);
562 /* Set new block length */
563 dev->max_block_size = block->block_len;
564 block->buf_len = block->block_len;
565 free_memory(block->buf);
566 block->buf = get_memory(block->buf_len);
569 goto reread; /* re-read block with correct block size */
572 if (block->block_len > block->read_len) {
573 dev->dev_errno = EIO;
574 Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Short block of %d bytes on device %s discarded.\n"),
575 dev->file, dev->block_num, block->read_len, dev->print_name());
576 Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
577 dev->set_short_block();
578 block->read_len = block->binbuf = 0;
579 return false; /* return error */
582 dev->clear_short_block();
584 dev->updateVolCatReads(1);
585 dev->updateVolCatReadBytes(block->read_len);
587 dev->EndBlock = dev->block_num;
588 dev->EndFile = dev->file;
591 /* Update dcr values */
592 if (dev->is_tape()) {
593 if (dcr->EndFile != dev->EndFile || dev->EndBlock > dcr->EndBlock) {
594 dcr->EndBlock = dev->EndBlock;
595 dcr->EndFile = dev->EndFile;
598 /* We need to take care about a short block in EndBlock/File
601 uint32_t len = MIN(block->read_len, block->block_len);
602 uint64_t addr = dev->file_addr + len - 1;
603 if ((uint32_t)(addr >> 32) != dcr->EndFile || (uint32_t)addr > dcr->EndBlock) {
604 dcr->EndBlock = (uint32_t)addr;
605 dcr->EndFile = (uint32_t)(addr >> 32);
607 dev->block_num = dev->EndBlock = (uint32_t)addr;
608 dev->file = dev->EndFile = (uint32_t)(addr >> 32);
610 dcr->VolMediaId = dev->VolCatInfo.VolMediaId;
611 dev->file_addr += block->read_len;
612 dev->file_size += block->read_len;
615 * If we read a short block on disk,
616 * seek to beginning of next block. This saves us
617 * from shuffling blocks around in the buffer. Take a
618 * look at this from an efficiency stand point later, but
619 * it should only happen once at the end of each job.
621 * I've been lseek()ing negative relative to SEEK_CUR for 30
622 * years now. However, it seems that with the new off_t definition,
623 * it is not possible to seek negative amounts, so we use two
624 * lseek(). One to get the position, then the second to do an
625 * absolute positioning -- so much for efficiency. KES Sep 02.
627 Dmsg0(250, "At end of read block\n");
628 if (block->read_len > block->block_len && !dev->is_tape()) {
630 boffset_t pos = dev->lseek(dcr, (boffset_t)0, SEEK_CUR); /* get curr pos */
631 Dmsg1(250, "Current lseek pos=%s\n", edit_int64(pos, ed1));
632 pos -= (block->read_len - block->block_len);
633 dev->lseek(dcr, pos, SEEK_SET);
634 Dmsg3(250, "Did lseek pos=%s blk_size=%d rdlen=%d\n",
635 edit_int64(pos, ed1), block->block_len,
637 dev->file_addr = pos;
638 dev->file_size = pos;
640 Dmsg3(150, "Exit read_block read_len=%d block_len=%d binbuf=%d\n",
641 block->read_len, block->block_len, block->binbuf);
642 block->block_read = true;