]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/device.c
Use rentrant mysql lib, eliminate race in sql_list, Win32 streams, misc see kes-1.31
[bacula/bacula] / bacula / src / stored / device.c
index 24adab2ebe6e0f08ab6b1481f591088ae2de2a51..9c34ea667cb9d8ee1f71f5369d185ddb5345bc30 100644 (file)
@@ -29,7 +29,7 @@
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
+   Copyright (C) 2000-2003 Kern Sibbald and John Walker
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -79,11 +79,12 @@ int fixup_device_block_write_error(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
 
    wait_time = time(NULL);
    status_dev(dev, &stat);
-   if (stat & MT_EOD) {
-      Dmsg0(190, "======= Got EOD ========\n");
+   if (stat & BMT_EOD) {
+      Dmsg0(100, "======= Got EOD ========\n");
 
-      new_lock_device_state(dev, BST_DOING_ACQUIRE);
       block_device(dev, BST_DOING_ACQUIRE);
+      /* Unlock, but leave BLOCKED */
+      unlock_device(dev);
 
       /* 
        * Walk through all attached jcrs creating a jobmedia_record()
@@ -91,25 +92,32 @@ int fixup_device_block_write_error(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
       Dmsg1(100, "Walk attached jcrs. Volume=%s\n", dev->VolCatInfo.VolCatName);
       for (JCR *mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
          Dmsg1(100, "create JobMedia for Job %s\n", mjcr->Job);
-        mjcr->end_block = dev->block_num;
-        mjcr->end_file = dev->file;
+        if (dev->state & ST_TAPE) {
+           mjcr->EndBlock = dev->EndBlock;
+           mjcr->EndFile  = dev->EndFile;
+            Dmsg2(200, "Fixup EndFile=%u EndBlock=%u\n", mjcr->EndFile, mjcr->EndBlock);
+        } else {
+           mjcr->EndBlock = (uint32_t)dev->file_addr;
+           mjcr->EndFile = (uint32_t)(dev->file_addr >> 32);
+        }
         if (!dir_create_jobmedia_record(mjcr)) {
             Jmsg(mjcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=%s Job=%s\n"),
               dev->VolCatInfo.VolCatName, mjcr->Job);
+           P(dev->mutex);
+           unblock_device(dev);
            return 0;
         }
       }
 
       strcpy(dev->VolCatInfo.VolCatStatus, "Full");
-      Dmsg2(100, "Call update_vol_info Stat=%s Vol=%s\n", 
+      Dmsg2(200, "Call update_vol_info Stat=%s Vol=%s\n", 
         dev->VolCatInfo.VolCatStatus, dev->VolCatInfo.VolCatName);
       if (!dir_update_volume_info(jcr, &dev->VolCatInfo, 0)) {   /* send Volume info to Director */
-         Jmsg(jcr, M_ERROR, 0, _("Could not update Volume info Volume=%s Job=%s\n"),
-           dev->VolCatInfo.VolCatName, jcr->Job);
-        new_unlock_device(dev);
+        P(dev->mutex);
+        unblock_device(dev);
         return 0;                    /* device locked */
       }
-      Dmsg0(190, "Back from update_vol_info\n");
+      Dmsg0(100, "Back from update_vol_info\n");
 
       strcpy(PrevVolName, dev->VolCatInfo.VolCatName);
       strcpy(dev->VolHdr.PrevVolName, PrevVolName);
@@ -121,19 +129,12 @@ int fixup_device_block_write_error(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
           PrevVolName, edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
           edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2));
 
-      /* Unlock, but leave BLOCKED */
-      unlock_device(dev);
       if (!mount_next_write_volume(jcr, dev, label_blk, 1)) {
-        new_unlock_device(dev);
-#ifndef NEW_LOCK
         P(dev->mutex);
-#endif
         unblock_device(dev);
         return 0;                    /* device locked */
       }
-#ifndef NEW_LOCK
       P(dev->mutex);                 /* lock again */
-#endif
 
       Jmsg(jcr, M_INFO, 0, _("New volume %s mounted on device %s\n"),
         jcr->VolumeName, dev_name(dev));
@@ -145,22 +146,20 @@ int fixup_device_block_write_error(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
        *  empty label_blk, and nothing will be written.
        */
       Dmsg0(190, "write label block to dev\n");
-      if (!write_block_to_dev(dev, label_blk)) {
+      if (!write_block_to_dev(jcr, dev, label_blk)) {
          Pmsg1(0, "write_block_to_device Volume label failed. ERR=%s",
           strerror_dev(dev));
         free_block(label_blk);
-        new_unlock_device(dev);
         unblock_device(dev);
         return 0;                    /* device locked */
       }
 
       /* Write overflow block to tape */
       Dmsg0(190, "Write overflow block to dev\n");
-      if (!write_block_to_dev(dev, block)) {
+      if (!write_block_to_dev(jcr, dev, block)) {
          Pmsg1(0, "write_block_to_device overflow block failed. ERR=%s",
           strerror_dev(dev));
         free_block(label_blk);
-        new_unlock_device(dev);
         unblock_device(dev);
         return 0;                    /* device locked */
       }
@@ -170,19 +169,22 @@ int fixup_device_block_write_error(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
       free_block(label_blk);
       for (JCR *mjcr=NULL; (mjcr=next_attached_jcr(dev, mjcr)); ) {
         /* Set new start/end positions */
-        mjcr->start_block = dev->block_num;
-        mjcr->start_file = dev->file;
+        if (dev->state & ST_TAPE) {
+           mjcr->StartBlock = dev->block_num;
+           mjcr->StartFile = dev->file;
+        } else {
+           mjcr->StartBlock = (uint32_t)dev->file_addr;
+           mjcr->StartFile  = (uint32_t)(dev->file_addr >> 32);
+        }
         mjcr->VolFirstFile = mjcr->JobFiles;
         mjcr->run_time += time(NULL) - wait_time; /* correct run time */
       }
-      new_unlock_device(dev);
       unblock_device(dev);
       return 1;                               /* device locked */
    }
    if (label_blk) {
       free_block(label_blk);
    }
-   new_unlock_device(dev);
    return 0;                         /* device locked */
 }
 
@@ -208,13 +210,11 @@ int open_device(DEVICE *dev)
       return 0;
    }
 
-   new_lock_device(dev);
    lock_device(dev);
 
    /* Defer opening files */
    if (!dev_is_tape(dev)) {
       Dmsg0(129, "Device is file, deferring open.\n");
-      new_unlock_device(dev);
       unlock_device(dev);
       return 1;
    }
@@ -223,14 +223,12 @@ int open_device(DEVICE *dev)
       Dmsg0(129, "Opening device.\n");
       if (open_dev(dev, NULL, READ_WRITE) < 0) {
          Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
-        new_unlock_device(dev);
         unlock_device(dev);
         return 0;
       }
    }
    Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
 
-   new_unlock_device(dev);
    unlock_device(dev);
    return 1;
 }
@@ -240,11 +238,10 @@ int open_device(DEVICE *dev)
  * must wait. The no_wait_id thread is out obtaining a new volume
  * and preparing the label.
  */
-void lock_device(DEVICE *dev)
+void _lock_device(char *file, int line, DEVICE *dev)
 {
-#ifndef NEW_LOCK
    int stat;
-   Dmsg1(190, "lock %d\n", dev->dev_blocked);
+   Dmsg3(100, "lock %d from %s:%d\n", dev->dev_blocked, file, line);
    P(dev->mutex);
    if (dev->dev_blocked && !pthread_equal(dev->no_wait_id, pthread_self())) {
       dev->num_waiting++;            /* indicate that I am waiting */
@@ -257,15 +254,12 @@ void lock_device(DEVICE *dev)
       }
       dev->num_waiting--;            /* no longer waiting */
    }
-#endif
 }
 
-void unlock_device(DEVICE *dev) 
+void _unlock_device(char *file, int line, DEVICE *dev) 
 {
-#ifndef NEW_LOCK
-   Dmsg0(190, "unlock\n");
+   Dmsg2(100, "unlock from %s:%d\n", file, line);
    V(dev->mutex);
-#endif
 }
 
 /* 
@@ -276,35 +270,60 @@ void unlock_device(DEVICE *dev)
  *  the current thread can do slip through the lock_device()
  *  calls without blocking.
  */
-void block_device(DEVICE *dev, int state)
+void _block_device(char *file, int line, DEVICE *dev, int state)
 {
-#ifndef NEW_LOCK
-   Dmsg1(190, "block set %d\n", state);
+   Dmsg3(100, "block set %d from %s:%d\n", state, file, line);
    ASSERT(dev->dev_blocked == BST_NOT_BLOCKED);
    dev->dev_blocked = state;         /* make other threads wait */
    dev->no_wait_id = pthread_self();  /* allow us to continue */
-#endif
 }
 
+
+
 /*
  * Unblock the device, and wake up anyone who went to sleep.
  */
-void unblock_device(DEVICE *dev)
+void _unblock_device(char *file, int line, DEVICE *dev)
 {
-#ifndef NEW_LOCK
-   Dmsg1(190, "unblock %d\n", dev->dev_blocked);
+   Dmsg3(100, "unblock %d from %s:%d\n", dev->dev_blocked, file, line);
    ASSERT(dev->dev_blocked);
    dev->dev_blocked = BST_NOT_BLOCKED;
    if (dev->num_waiting > 0) {
       pthread_cond_broadcast(&dev->wait); /* wake them up */
    }
-#endif
 }
 
+void _steal_device_lock(char *file, int line, DEVICE *dev, bsteal_lock_t *hold, int state)
+{
+   Dmsg4(100, "steal lock. old=%d new=%d from %s:%d\n", dev->dev_blocked, state,
+      file, line);
+   hold->dev_blocked = dev->dev_blocked;
+   hold->no_wait_id = dev->no_wait_id;
+   dev->dev_blocked = state;
+   dev->no_wait_id = pthread_self();
+   V(dev->mutex);
+}
+
+void _return_device_lock(char *file, int line, DEVICE *dev, bsteal_lock_t *hold)          
+{
+   Dmsg4(100, "return lock. old=%d new=%d from %s:%d\n", 
+      dev->dev_blocked, hold->dev_blocked, file, line);
+   P(dev->mutex);
+   dev->dev_blocked = hold->dev_blocked;
+   dev->no_wait_id = hold->no_wait_id;
+}
+
+
+
+/* ==================================================================
+ *  New device locking code.  It is not currently used.
+ * ==================================================================
+ */
+
 /*
  * New device locking scheme 
  */
-void _lock_device(char *file, int line, DEVICE *dev)
+void _new_lock_device(char *file, int line, DEVICE *dev)
 {
 #ifdef NEW_LOCK
    int errstat;
@@ -315,7 +334,7 @@ void _lock_device(char *file, int line, DEVICE *dev)
 #endif
 }    
 
-void _lock_device(char *file, int line, DEVICE *dev, int state)
+void _new_lock_device(char *file, int line, DEVICE *dev, int state)
 {
 #ifdef NEW_LOCK
    int errstat;
@@ -327,7 +346,7 @@ void _lock_device(char *file, int line, DEVICE *dev, int state)
 #endif
 }    
 
-void _unlock_device(char *file, int line, DEVICE *dev)
+void _new_unlock_device(char *file, int line, DEVICE *dev)
 {
 #ifdef NEW_LOCK
    int errstat;
@@ -344,7 +363,6 @@ void _unlock_device(char *file, int line, DEVICE *dev)
 void new_steal_device_lock(DEVICE *dev, brwsteal_t *hold, int state)
 {
 #ifdef NEW_LOCK
-   P(dev->lock.mutex);
    hold->state = dev->dev_blocked;
    hold->writer_id = dev->lock.writer_id;
    dev->dev_blocked = state;
@@ -359,6 +377,5 @@ void new_return_device_lock(DEVICE *dev, brwsteal_t *hold)
    P(dev->lock.mutex);
    dev->dev_blocked = hold->state;
    dev->lock.writer_id = hold->writer_id;
-   V(dev->lock.mutex);
 #endif
 }