]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/block.c
Massive SD calling sequence reorganization
[bacula/bacula] / bacula / src / stored / block.c
index 8e5725adeb14614090534fb71a64f0f706f12743..915cf6ee65b649a1da733e7f6fa849918e02b883 100644 (file)
@@ -233,7 +233,7 @@ static bool unser_block_header(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
       block->bufp = block->buf + bhl;
       if (strncmp(Id, BLKHDR1_ID, BLKHDR_ID_LENGTH) != 0) {
         dev->dev_errno = EIO;
-         Mmsg4(&dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
+         Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
            dev->file, dev->block_num, BLKHDR1_ID, Id);
         if (block->read_errors == 0 || verbose >= 2) {
             Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
@@ -249,7 +249,7 @@ static bool unser_block_header(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
       block->bufp = block->buf + bhl;
       if (strncmp(Id, BLKHDR2_ID, BLKHDR_ID_LENGTH) != 0) {
         dev->dev_errno = EIO;
-         Mmsg4(&dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
+         Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
            dev->file, dev->block_num, BLKHDR2_ID, Id);
         if (block->read_errors == 0 || verbose >= 2) {
             Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
@@ -259,7 +259,7 @@ static bool unser_block_header(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
       }
    } else {
       dev->dev_errno = EIO;
-      Mmsg4(&dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
+      Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
          dev->file, dev->block_num, BLKHDR2_ID, Id);
       if (block->read_errors == 0 || verbose >= 2) {
          Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
@@ -273,7 +273,7 @@ static bool unser_block_header(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
    /* Sanity check */
    if (block_len > MAX_BLOCK_LENGTH) {
       dev->dev_errno = EIO;
-      Mmsg3(&dev->errmsg,  _("Volume data error at %u:%u! Block length %u is insane (too large), probably due to a bad archive.\n"),
+      Mmsg3(dev->errmsg,  _("Volume data error at %u:%u! Block length %u is insane (too large), probably due to a bad archive.\n"),
         dev->file, dev->block_num, block_len);
       if (block->read_errors == 0 || verbose >= 2) {
          Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
@@ -299,7 +299,7 @@ static bool unser_block_header(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
                         block_len-BLKHDR_CS_LENGTH);
       if (BlockCheckSum != CheckSum) {
         dev->dev_errno = EIO;
-         Mmsg5(&dev->errmsg, _("Volume data error at %u:%u! Block checksum mismatch in block %u: calc=%x blk=%x\n"), 
+         Mmsg5(dev->errmsg, _("Volume data error at %u:%u! Block checksum mismatch in block %u: calc=%x blk=%x\n"), 
            dev->file, dev->block_num, (unsigned)BlockNumber, BlockCheckSum, CheckSum);
         if (block->read_errors == 0 || verbose >= 2) {
             Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
@@ -320,14 +320,14 @@ static bool unser_block_header(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
  *       : false on failure
  *
  */
-bool write_block_to_device(DCR *dcr, DEV_BLOCK *block)
+bool write_block_to_device(DCR *dcr)
 {
    bool stat = true;
    DEVICE *dev = dcr->dev;
    JCR *jcr = dcr->jcr;
 
    if (dcr->spooling) {
-      stat = write_block_to_spool_file(dcr, block);
+      stat = write_block_to_spool_file(dcr);
       return stat;
    }
 
@@ -345,8 +345,8 @@ bool write_block_to_device(DCR *dcr, DEV_BLOCK *block)
       /* Create a jobmedia record for this job */
       if (!dir_create_jobmedia_record(dcr)) {
         dev->dev_errno = EIO;
-         Jmsg(jcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
-           jcr->VolCatInfo.VolCatName, jcr->Job);
+         Jmsg(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
+           dcr->VolCatInfo.VolCatName, jcr->Job);
         set_new_volume_parameters(dcr);
         stat = false;
         goto bail_out;
@@ -360,8 +360,12 @@ bool write_block_to_device(DCR *dcr, DEV_BLOCK *block)
       }
    }
 
-   if (!write_block_to_dev(dcr, block)) {
-       stat = fixup_device_block_write_error(dcr, block);
+   if (!write_block_to_dev(dcr)) {
+       if (job_canceled(jcr)) {
+         stat = 0;
+       } else {
+         stat = fixup_device_block_write_error(dcr);
+       }
    }
 
 bail_out:
@@ -377,14 +381,15 @@ bail_out:
  *  Returns: true  on success or EOT
  *          false on hard error
  */
-bool write_block_to_dev(DCR *dcr, DEV_BLOCK *block)
+bool write_block_to_dev(DCR *dcr)
 {
    ssize_t stat = 0;
    uint32_t wlen;                    /* length to write */
    int hit_max1, hit_max2;
-   bool ok;
+   bool ok = true;
    DEVICE *dev = dcr->dev;
    JCR *jcr = dcr->jcr;
+   DEV_BLOCK *block = dcr->block;
 
 #ifdef NO_TAPE_WRITE_TEST
    empty_block(block);
@@ -452,7 +457,7 @@ bool write_block_to_dev(DCR *dcr, DEV_BLOCK *block)
            edit_uint64_with_commas(max_cap, ed1),  dev->dev_name);
       block->write_failed = true;
       if (weof_dev(dev, 1) != 0) {           /* end tape */
-         Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
+         Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
         dev->VolCatInfo.VolCatErrors++;
       }
       /* Don't do update after second EOF or file count will be wrong */
@@ -460,6 +465,7 @@ bool write_block_to_dev(DCR *dcr, DEV_BLOCK *block)
       dev->VolCatInfo.VolCatFiles = dev->file;
       dir_update_volume_info(dcr, false);
       if (dev_cap(dev, CAP_TWOEOF) && weof_dev(dev, 1) != 0) { /* write eof */
+        /* This may not be fatal since we already wrote an EOF */
          Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
         dev->VolCatInfo.VolCatErrors++;
       }
@@ -474,7 +480,7 @@ bool write_block_to_dev(DCR *dcr, DEV_BLOCK *block)
 
       if (dev_state(dev, ST_TAPE) && weof_dev(dev, 1) != 0) {           /* write eof */
         /* Write EOF */
-         Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
+         Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
         block->write_failed = true;
         dev->VolCatInfo.VolCatErrors++;
         dev->state |= (ST_EOF | ST_EOT | ST_WEOT);
@@ -543,14 +549,14 @@ bool write_block_to_dev(DCR *dcr, DEV_BLOCK *block)
 #endif
 
    if (stat != (ssize_t)wlen) {
-      /* We should check for errno == ENOSPC, BUT many 
-       * devices simply report EIO when the volume is full.
+      /* Some devices simply report EIO when the volume is full.
        * With a little more thought we may be able to check
        * capacity and distinguish real errors and EOT
        * conditions.  In any case, we probably want to
        * simulate an End of Medium.
        */
       if (stat == -1) {
+        berrno be;
         /* I have added the ifdefing here because it appears on
          * FreeBSD where MTIOCERRSTAT is defined, this not only
          * clears the error but clears the residual unwritten
@@ -562,33 +568,37 @@ bool write_block_to_dev(DCR *dcr, DEV_BLOCK *block)
         if (dev->dev_errno == 0) {
            dev->dev_errno = ENOSPC;        /* out of space */
         }
-         Jmsg4(jcr, M_ERROR, 0, _("Write error at %u:%u on device %s. ERR=%s.\n"), 
-           dev->file, dev->block_num, dev->dev_name, strerror(dev->dev_errno));
+        if (dev->dev_errno != ENOSPC) {
+            Jmsg4(jcr, M_ERROR, 0, _("Write error at %u:%u on device %s. ERR=%s.\n"), 
+              dev->file, dev->block_num, dev->dev_name, be.strerror());
+        }
       } else {
        dev->dev_errno = ENOSPC;            /* out of space */
+      }  
+      if (dev->dev_errno == ENOSPC) {
          Jmsg(jcr, M_INFO, 0, _("End of Volume \"%s\" at %u:%u on device %s. Write of %u bytes got %d.\n"), 
            dev->VolCatInfo.VolCatName,
            dev->file, dev->block_num, dev->dev_name, wlen, stat);
-      }  
-
+      }
       Dmsg6(100, "=== Write error. size=%u rtn=%d dev_blk=%d blk_blk=%d errno=%d: ERR=%s\n", 
         wlen, stat, dev->block_num, block->BlockNumber, dev->dev_errno, strerror(dev->dev_errno));
 
       block->write_failed = true;
       if (weof_dev(dev, 1) != 0) {        /* end the tape */
         dev->VolCatInfo.VolCatErrors++;
-         Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
+         Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
+        ok = false;
       }
       Dmsg0(100, "dir_update_volume_info\n");
       dev->VolCatInfo.VolCatFiles = dev->file;
       dir_update_volume_info(dcr, false);
-      if (dev_cap(dev, CAP_TWOEOF) && weof_dev(dev, 1) != 0) { /* end the tape */
+      if (ok && dev_cap(dev, CAP_TWOEOF) && weof_dev(dev, 1) != 0) {  /* end the tape */
         dev->VolCatInfo.VolCatErrors++;
+        /* This may not be fatal since we already wrote an EOF */
          Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
       }
       dev->state |= (ST_EOF | ST_EOT | ST_WEOT);
        
-      ok = true;
 #define CHECK_LAST_BLOCK
 #ifdef CHECK_LAST_BLOCK
       /* 
@@ -598,8 +608,7 @@ bool write_block_to_dev(DCR *dcr, DEV_BLOCK *block)
        *   then re-read it and verify that the block number is
        *   correct.
        */
-      if ((dev->state & ST_TAPE) && dev_cap(dev, CAP_BSR)) {
-
+      if (ok && (dev->state & ST_TAPE) && dev_cap(dev, CAP_BSR)) {
         /* Now back up over what we wrote and read the last block */
         if (!bsf_dev(dev, 1)) {
            ok = false;
@@ -626,7 +635,8 @@ bool write_block_to_dev(DCR *dcr, DEV_BLOCK *block)
         if (ok) {
            DEV_BLOCK *lblock = new_block(dev);
            /* Note, this can destroy dev->errmsg */
-           if (!read_block_from_dev(dcr, lblock, NO_BLOCK_NUMBER_CHECK)) {
+           dcr->block = lblock;
+           if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
                Jmsg(jcr, M_ERROR, 0, _("Re-read last block at EOT failed. ERR=%s"), dev->errmsg);
            } else {
               if (lblock->BlockNumber+1 == block->BlockNumber) {
@@ -638,6 +648,7 @@ bool write_block_to_dev(DCR *dcr, DEV_BLOCK *block)
               }
            }
            free_block(lblock);
+           dcr->block = block;
         }
       }
 #endif
@@ -653,7 +664,7 @@ bool write_block_to_dev(DCR *dcr, DEV_BLOCK *block)
    dev->block_num++;
    block->BlockNumber++;
 
-   /* Update jcr values */
+   /* Update dcr values */
    if (dev_state(dev, ST_TAPE)) {
       dcr->EndBlock = dev->EndBlock;
       dcr->EndFile  = dev->EndFile;
@@ -682,13 +693,13 @@ bool write_block_to_dev(DCR *dcr, DEV_BLOCK *block)
  * Read block with locking
  *
  */
-bool read_block_from_device(DCR *dcr, DEV_BLOCK *block, bool check_block_numbers)
+bool read_block_from_device(DCR *dcr, bool check_block_numbers)
 {
    bool stat;
    DEVICE *dev = dcr->dev;
    Dmsg0(90, "Enter read_block_from_device\n");
    lock_device(dev);
-   stat = read_block_from_dev(dcr, block, check_block_numbers);
+   stat = read_block_from_dev(dcr, check_block_numbers);
    unlock_device(dev);
    Dmsg0(90, "Leave read_block_from_device\n");
    return stat;
@@ -699,7 +710,7 @@ bool read_block_from_device(DCR *dcr, DEV_BLOCK *block, bool check_block_numbers
  *  the block header.  For a file, the block may be partially
  *  or completely in the current buffer.
  */
-bool read_block_from_dev(DCR *dcr, DEV_BLOCK *block, bool check_block_numbers)
+bool read_block_from_dev(DCR *dcr, bool check_block_numbers)
 {
    ssize_t stat;
    int looping;
@@ -707,6 +718,7 @@ bool read_block_from_dev(DCR *dcr, DEV_BLOCK *block, bool check_block_numbers)
    int retry;
    JCR *jcr = dcr->jcr;
    DEVICE *dev = dcr->dev;
+   DEV_BLOCK *block = dcr->block;
 
    if (dev_state(dev, ST_EOT)) {
       return false;
@@ -717,7 +729,7 @@ bool read_block_from_dev(DCR *dcr, DEV_BLOCK *block, bool check_block_numbers)
 reread:
    if (looping > 1) {
       dev->dev_errno = EIO;
-      Mmsg1(&dev->errmsg, _("Block buffer size looping problem on device %s\n"),
+      Mmsg1(dev->errmsg, _("Block buffer size looping problem on device %s\n"),
         dev->dev_name);
       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
       block->read_len = 0;
@@ -738,10 +750,10 @@ reread:
       }
    } while (stat == -1 && (errno == EINTR || errno == EIO) && retry++ < 11);
    if (stat < 0) {
-      Dmsg1(90, "Read device got: ERR=%s\n", strerror(errno));
       clrerror_dev(dev, -1);
+      Dmsg1(90, "Read device got: ERR=%s\n", strerror(errno));
       block->read_len = 0;
-      Mmsg4(&dev->errmsg, _("Read error at file:blk %u:%u on device %s. ERR=%s.\n"), 
+      Mmsg4(dev->errmsg, _("Read error at file:blk %u:%u on device %s. ERR=%s.\n"), 
         dev->file, dev->block_num, dev->dev_name, strerror(dev->dev_errno));
       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
       if (dev->state & ST_EOF) {  /* EOF just seen? */
@@ -752,7 +764,7 @@ reread:
    Dmsg1(90, "Read device got %d bytes\n", stat);
    if (stat == 0) {            /* Got EOF ! */
       dev->block_num = block->read_len = 0;
-      Mmsg3(&dev->errmsg, _("Read zero bytes at %u:%u on device %s.\n"), 
+      Mmsg3(dev->errmsg, _("Read zero bytes at %u:%u on device %s.\n"), 
         dev->file, dev->block_num, dev->dev_name);
       if (dev->state & ST_EOF) { /* EOF already read? */
         dev->state |= ST_EOT;  /* yes, 2 EOFs => EOT */
@@ -768,7 +780,7 @@ reread:
    block->read_len = stat;     /* save length read */
    if (block->read_len < BLKHDR2_LENGTH) {
       dev->dev_errno = EIO;
-      Mmsg4(&dev->errmsg, _("Volume data error at %u:%u! Very short block of %d bytes on device %s discarded.\n"), 
+      Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Very short block of %d bytes on device %s discarded.\n"), 
         dev->file, dev->block_num, block->read_len, dev->dev_name);
       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
       dev->state |= ST_SHORT;  /* set short block */
@@ -793,7 +805,7 @@ reread:
     */
    if (block->block_len > block->buf_len) {
       dev->dev_errno = EIO;
-      Mmsg2(&dev->errmsg,  _("Block length %u is greater than buffer %u. Attempting recovery.\n"),
+      Mmsg2(dev->errmsg,  _("Block length %u is greater than buffer %u. Attempting recovery.\n"),
         block->block_len, block->buf_len);
       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
       Pmsg1(000, "%s", dev->errmsg);
@@ -812,7 +824,7 @@ reread:
         lseek(dev->fd, pos, SEEK_SET);   
         dev->file_addr = pos;
       }
-      Mmsg1(&dev->errmsg, _("Setting block buffer size to %u bytes.\n"), block->block_len);
+      Mmsg1(dev->errmsg, _("Setting block buffer size to %u bytes.\n"), block->block_len);
       Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
       Pmsg1(000, "%s", dev->errmsg);
       /* Set new block length */
@@ -827,7 +839,7 @@ reread:
 
    if (block->block_len > block->read_len) {
       dev->dev_errno = EIO;
-      Mmsg4(&dev->errmsg, _("Volume data error at %u:%u! Short block of %d bytes on device %s discarded.\n"), 
+      Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Short block of %d bytes on device %s discarded.\n"), 
         dev->file, dev->block_num, block->read_len, dev->dev_name);
       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
       dev->state |= ST_SHORT;  /* set short block */
@@ -845,7 +857,7 @@ reread:
    dev->EndFile  = dev->file;
    dev->block_num++;
 
-   /* Update jcr values */
+   /* Update dcr values */
    if (dev->state & ST_TAPE) {
       dcr->EndBlock = dev->EndBlock;
       dcr->EndFile  = dev->EndFile;