]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/block.c
Backport new lock calls + debug for SD
[bacula/bacula] / bacula / src / stored / block.c
index 1368e3903efbf071f4b8b7e554276a834f01b7d9..e731320b880b2a459c8916bfad46c32c18dded34 100644 (file)
 #include "bacula.h"
 #include "stored.h"
 
+#ifdef DEBUG_BLOCK_CHECKSUM
+static const bool debug_block_checksum = true;
+#else
+static const bool debug_block_checksum = false;
+#endif
+
+#ifdef NO_TAPE_WRITE_TEST
+static const bool no_tape_write_test = true;
+#else
+static const bool no_tape_write_test = false;
+#endif
+
+
 static bool terminate_writing_volume(DCR *dcr);
 static bool do_new_file_bookkeeping(DCR *dcr);
 static bool do_dvd_size_checks(DCR *dcr);
@@ -165,10 +178,12 @@ void print_block_read_errors(JCR *jcr, DEV_BLOCK *block)
  */
 void free_block(DEV_BLOCK *block)
 {
-   Dmsg1(999, "free_block buffer %x\n", block->buf);
-   free_memory(block->buf);
-   Dmsg1(999, "free_block block %x\n", block);
-   free_memory((POOLMEM *)block);
+   if (block) {
+      Dmsg1(999, "free_block buffer %x\n", block->buf);
+      free_memory(block->buf);
+      Dmsg1(999, "free_block block %x\n", block);
+      free_memory((POOLMEM *)block);
+   }
 }
 
 /* Empty the block -- for writing */
@@ -343,13 +358,14 @@ bool DCR::write_block_to_device()
    DCR *dcr = this;
 
    if (dcr->spooling) {
+      Dmsg0(100, "Write to spool\n");
       stat = write_block_to_spool_file(dcr);
       return stat;
    }
 
    if (!dcr->is_dev_locked()) {        /* device already locked? */
       /* note, do not change this to dcr->r_dlock */
-      dev->r_dlock();                  /* no, lock it */
+      dev->rLock();                  /* no, lock it */
    }
 
    /*
@@ -361,6 +377,7 @@ bool DCR::write_block_to_device()
    if (dcr->NewVol || dcr->NewFile) {
       if (job_canceled(jcr)) {
          stat = false;
+         Dmsg0(100, "Canceled\n");
          goto bail_out;
       }
       /* Create a jobmedia record for this job */
@@ -370,6 +387,7 @@ bool DCR::write_block_to_device()
             dcr->getVolCatName(), jcr->Job);
          set_new_volume_parameters(dcr);
          stat = false;
+         Dmsg0(100, "cannot create media record\n");
          goto bail_out;
       }
       if (dcr->NewVol) {
@@ -391,7 +409,7 @@ bool DCR::write_block_to_device()
 bail_out:
    if (!dcr->is_dev_locked()) {        /* did we lock dev above? */
       /* note, do not change this to dcr->dunlock */
-      dev->dunlock();                  /* unlock it now */
+      dev->Unlock();                  /* unlock it now */
    }
    return stat;
 }
@@ -409,11 +427,12 @@ bool DCR::write_block_to_dev()
    int hit_max1, hit_max2;
    bool ok = true;
    DCR *dcr = this;
+   uint32_t checksum;
 
-#ifdef NO_TAPE_WRITE_TEST
-   empty_block(block);
-   return true;
-#endif
+   if (no_tape_write_test) {
+      empty_block(block);
+      return true;
+   }
    if (job_canceled(jcr)) {
       return false;
    }
@@ -431,18 +450,22 @@ bool DCR::write_block_to_dev()
       Dmsg0(100, "return write_block_to_dev with ST_WEOT\n");
       dev->dev_errno = ENOSPC;
       Jmsg1(jcr, M_FATAL, 0,  _("Cannot write block. Device at EOM. dev=%s\n"), dev->print_name());
+      Dmsg1(100, "Attempt to write on read-only Volume. dev=%s\n", dev->print_name());
       return false;
    }
    if (!dev->can_append()) {
       dev->dev_errno = EIO;
       Jmsg1(jcr, M_FATAL, 0, _("Attempt to write on read-only Volume. dev=%s\n"), dev->print_name());
+      Dmsg1(100, "Attempt to write on read-only Volume. dev=%s\n", dev->print_name());
       return false;
    }
-
    if (!dev->is_open()) {
       Jmsg1(jcr, M_FATAL, 0, _("Attempt to write on closed device=%s\n"), dev->print_name());
+      Dmsg1(100, "Attempt to write on closed device=%s\n", dev->print_name());
       return false;
    }
+
    /*
     * Clear to the end of the buffer if it is not full,
     *  and on tape devices, apply min and fixed blocking.
@@ -471,12 +494,7 @@ bool DCR::write_block_to_dev()
       }
    }
 
-#ifdef DEBUG_BLOCK_CHECKSUM
-   uint32_t checksum;
    checksum = ser_block_header(block, dev->do_checksum());
-#else
-   ser_block_header(block, dev->do_checksum());
-#endif
 
    /* Limit maximum Volume size to value specified by user */
    hit_max1 = (dev->max_volume_size > 0) &&
@@ -556,14 +574,14 @@ bool DCR::write_block_to_dev()
 
    } while (stat == -1 && (errno == EBUSY || errno == EIO) && retry++ < 3);
 
-#ifdef DEBUG_BLOCK_CHECKSUM
-   uint32_t achecksum = ser_block_header(block, dev->do_checksum());
-   if (checksum != achecksum) {
-      Jmsg2(jcr, M_ERROR, 0, _("Block checksum changed during write: before=%ud after=%ud\n"),
-         checksum, achecksum);
-      dump_block(block, "with checksum error");
+   if (debug_block_checksum) {
+      uint32_t achecksum = ser_block_header(block, dev->do_checksum());
+      if (checksum != achecksum) {
+         Jmsg2(jcr, M_ERROR, 0, _("Block checksum changed during write: before=%ud after=%ud\n"),
+            checksum, achecksum);
+         dump_block(block, "with checksum error");
+      }
    }
-#endif
 
 #ifdef DEBUG_BLOCK_ZEROING
    if (bp[0] == 0 && bp[1] == 0 && bp[2] == 0 && block->buf[12] == 0) {
@@ -748,8 +766,9 @@ static bool terminate_writing_volume(DCR *dcr)
    if (!dir_create_jobmedia_record(dcr)) {
       Dmsg0(50, "Error from create JobMedia\n");
       dev->dev_errno = EIO;
-       Jmsg2(dcr->jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
+        Mmsg2(dev->errmsg, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
             dcr->getVolCatName(), dcr->jcr->Job);
+       Jmsg(dcr->jcr, M_FATAL, 0, "%s", dev->errmsg);
        ok = false;
    }
    dcr->block->write_failed = true;
@@ -779,6 +798,7 @@ static bool terminate_writing_volume(DCR *dcr)
    }
    
    if (!dir_update_volume_info(dcr, false, true)) {
+      Mmsg(dev->errmsg, _("Error sending Volume info to Director.\n"));
       ok = false;
       Dmsg0(50, "Error updating volume info.\n");
    }
@@ -937,9 +957,9 @@ bool DCR::read_block_from_device(bool check_block_numbers)
    bool ok;
 
    Dmsg0(250, "Enter read_block_from_device\n");
-   dev->r_dlock();
+   dev->rLock();
    ok = read_block_from_dev(check_block_numbers);
-   dev->dunlock();
+   dev->Unlock();
    Dmsg0(250, "Leave read_block_from_device\n");
    return ok;
 }
@@ -994,8 +1014,9 @@ reread:
         dev->part <= dev->num_dvd_parts) {
       Dmsg0(400, "Call dvd_open_next_part\n");
       if (dvd_open_next_part(dcr) < 0) {
-         Jmsg3(dcr->jcr, M_FATAL, 0, _("Unable to open device part=%d %s: ERR=%s\n"),
+         Mmsg3(dev->errmsg, _("Unable to open device part=%d %s: ERR=%s\n"),
                dev->part, dev->print_name(), dev->bstrerror());
+         Jmsg(dcr->jcr, M_FATAL, 0, "%s", dev->errmsg);
          dev->dev_errno = EIO;
          return false;
       }
@@ -1062,6 +1083,7 @@ reread:
       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
       dev->set_short_block();   
       block->read_len = block->binbuf = 0;
+      Dmsg2(200, "set block=%p binbuf=%d\n", block, block->binbuf);
       return false;             /* return error */
    }
 
@@ -1090,7 +1112,8 @@ reread:
       if (dev->is_tape()) {
          Dmsg0(250, "BSR for reread; block too big for buffer.\n");
          if (!dev->bsr(1)) {
-            Jmsg(jcr, M_ERROR, 0, "%s", dev->bstrerror());
+            Mmsg(dev->errmsg, "%s", dev->bstrerror());
+            Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
             block->read_len = 0;
             return false;
          }