]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/reserve.c
Move reservations message lock to lock jcr only this
[bacula/bacula] / bacula / src / stored / reserve.c
index 429f20cf62ed9907395693d939b9a1b18c3ccdae..de2732daf823d7cd0a76d2e3b41376ade607828a 100644 (file)
@@ -207,8 +207,12 @@ void list_volumes(void sendit(const char *msg, int len, void *sarg), void *arg)
 
    lock_volumes();
    foreach_dlist(vol, vol_list) {
-      if (vol->dev) {
-         len = Mmsg(msg, "%s on device %s\n", vol->vol_name, vol->dev->print_name());
+      DEVICE *dev = vol->dev;
+      if (dev) {
+         len = Mmsg(msg, "%s on device %s\n", vol->vol_name, dev->print_name());
+         sendit(msg.c_str(), len, arg);
+         len = Mmsg(msg, "    Reader=%d writers=%d reserved=%d\n", dev->can_read()?1:0,
+            dev->num_writers, dev->reserved_device);
          sendit(msg.c_str(), len, arg);
       } else {
          len = Mmsg(msg, "%s no dev\n", vol->vol_name);
@@ -453,7 +457,17 @@ bool volume_unused(DCR *dcr)
       return false;
    }
 
-   return free_volume(dev);
+   /*  
+    * If this is a tape, we do not free the volume, rather we wait
+    *  until the autoloader unloads it, or until another tape is
+    *  explicitly read in this drive. This allows the SD to remember
+    *  where the tapes are or last were.
+    */
+   if (dev->is_tape() || dev->is_autochanger()) {
+      return true;
+   } else {
+      return free_volume(dev);
+   }
 }
 
 /*
@@ -497,7 +511,13 @@ void free_volume_list()
    }
    lock_volumes();
    foreach_dlist(vol, vol_list) {
-      Dmsg3(dbglvl, "jid=%u Unreleased Volume=%s dev=%p\n", jid(), vol->vol_name, vol->dev);
+      if (vol->dev) {
+         Dmsg3(dbglvl, "jid=%u free vol_list Volume=%s dev=%s\n", jid(),
+               vol->vol_name, vol->dev->print_name());
+      } else {
+         Dmsg3(dbglvl, "jid=%u free vol_list Volume=%s dev=%p\n", jid(), 
+               vol->vol_name, vol->dev);
+      }
       free(vol->vol_name);
       vol->vol_name = NULL;
    }
@@ -620,6 +640,13 @@ static bool use_storage_cmd(JCR *jcr)
    }
 
    init_jcr_device_wait_timers(jcr);
+   jcr->dcr = new_dcr(jcr, NULL, NULL);         /* get a dcr */
+   if (!jcr->dcr) {
+      BSOCK *dir = jcr->dir_bsock;
+      dir->fsend(_("3939 Could not get dcr\n"));
+      Dmsg1(dbglvl, ">dird: %s", dir->msg);
+      ok = false;
+   }
    /*                    
     * At this point, we have a list of all the Director's Storage
     *  resources indicated for this Job, which include Pool, PoolType,
@@ -634,6 +661,7 @@ static bool use_storage_cmd(JCR *jcr)
       int repeat = 0;
       bool fail = false;
       rctx.notify_dir = true;
+
       lock_reservations();
       for ( ; !fail && !job_canceled(jcr); ) {
          while ((msg = (char *)msgs->pop())) {
@@ -644,7 +672,11 @@ static bool use_storage_cmd(JCR *jcr)
          rctx.VolumeName[0] = 0;
          rctx.any_drive = false;
          if (!jcr->PreferMountedVols) {
-            /* Look for unused drives in autochangers */
+            /*
+             * Here we try to find a drive that is not used.
+             * This will maximize the use of available drives.
+             *
+             */
             rctx.num_writers = 20000000;   /* start with impossible number */
             rctx.low_use_drive = NULL;
             rctx.PreferMountedVols = false;                
@@ -674,7 +706,11 @@ static bool use_storage_cmd(JCR *jcr)
                break;
             }
          }
-         /* Look for an exact match all drives */
+         /*
+          * Now we look for a drive that may or may not be in
+          *  use.
+          */
+         /* Look for an exact Volume match all drives */
          rctx.PreferMountedVols = true;
          rctx.exact_match = true;
          rctx.autochanger_only = false;
@@ -752,21 +788,27 @@ static bool use_storage_cmd(JCR *jcr)
    return ok;
 }
 
-void release_msgs(JCR *jcr)
+
+/*
+ * Walk through the autochanger resources and check if
+ *  the volume is in one of them.
+ * 
+ * Returns:  true  if volume is in device
+ *           false otherwise
+ */
+static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol)
 {
-   alist *msgs = jcr->reserve_msgs;
-   char *msg;
+   AUTOCHANGER *changer = vol->dev->device->changer_res;
 
-   if (!msgs) {
-      return;
-   }
-   lock_reservations();
-   while ((msg = (char *)msgs->pop())) {
-      free(msg);
-   }
-   delete msgs;
-   jcr->reserve_msgs = NULL;
-   unlock_reservations();
+   /* Find resource, and make sure we were able to open it */
+   if (fnmatch(rctx.device_name, changer->hdr.name, 0) == 0) {
+      Dmsg2(dbglvl, "jid=%u Found changer device %s\n",
+                     (int)rctx.jcr->JobId, vol->dev->device->hdr.name);
+      return true;
+   }  
+   Dmsg2(dbglvl, "jid=%u Incorrect changer device %s\n", 
+                  (int)rctx.jcr->JobId, changer->hdr.name);
+   return false;
 }
 
 /*
@@ -778,6 +820,7 @@ bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
    DIRSTORE *store;
    char *device_name;
    alist *dirstore;
+   DCR *dcr = jcr->dcr;
 
    if (rctx.append) {
       dirstore = jcr->write_store;
@@ -789,6 +832,12 @@ bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
       rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
       rctx.autochanger_only);
 
+   /* 
+    * If the appropriate conditions of this if are met, namely that
+    *  we are appending and the user wants mounted drive (or we
+    *  force try a mounted drive because they are all busy), we
+    *  start by looking at all the Volumes in the volume list.
+    */
    if (!vol_list->empty() && rctx.append && rctx.PreferMountedVols) {
       dlist *temp_vol_list, *save_vol_list;
       VOLRES *vol = NULL;
@@ -822,19 +871,39 @@ bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
       unlock_volumes();
 
       /* Look through reserved volumes for one we can use */
+      Dmsg1(dbglvl, "jid=%u look for vol in vol list\n", (int)rctx.jcr->JobId);
       foreach_dlist(vol, temp_vol_list) {
          if (!vol->dev) {
+            Dmsg2(dbglvl, "jid=%u vol=%s no dev\n", (int)rctx.jcr->JobId, vol->vol_name);
             continue;
          }
+         /* Check with Director if this Volume is OK */
+         bstrncpy(dcr->VolumeName, vol->vol_name, sizeof(dcr->VolumeName));
+         if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
+            continue;
+         }
+
+         Dmsg2(dbglvl, "jid=%u vol=%s OK for this job\n", (int)rctx.jcr->JobId, vol->vol_name);
          foreach_alist(store, dirstore) {
+            int stat;
             rctx.store = store;
             foreach_alist(device_name, store->device) {
-               int stat;
-               if (strcmp(device_name, vol->dev->device->hdr.name) != 0) {
-                  continue;
-               }
+               /* Found a device, try to use it */
                rctx.device_name = device_name;
                rctx.device = vol->dev->device;
+
+               if (vol->dev->is_autochanger()) {
+                  Dmsg2(dbglvl, "jid=%u vol=%s is in changer\n", (int)rctx.jcr->JobId, 
+                        vol->vol_name);
+                  if (!is_vol_in_autochanger(rctx, vol)) {
+                     continue;
+                  }
+               } else if (strcmp(device_name, vol->dev->device->hdr.name) != 0) {
+                  Dmsg3(dbglvl, "jid=%u device=%s not suitable want %s\n", (int)rctx.jcr->JobId, 
+                        vol->dev->device->hdr.name, device_name);
+                  continue;
+               }
+
                bstrncpy(rctx.VolumeName, vol->vol_name, sizeof(rctx.VolumeName));
                rctx.have_volume = true;
                /* Try reserving this device and volume */
@@ -859,7 +928,11 @@ bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
                break;
             }
          }
+         if (ok) {
+            break;
+         }
       } /* end for loop over reserved volumes */
+
       lock_volumes();
       save_vol_list = vol_list;
       vol_list = temp_vol_list;
@@ -869,7 +942,7 @@ bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
       unlock_volumes();
    }
    if (ok) {
-      Dmsg2(dbglvl, "jid=%u got vol %s in reserved volums list\n", (int)rctx.jcr->JobId,
+      Dmsg2(dbglvl, "jid=%u got vol %s from in-use vols list\n", (int)rctx.jcr->JobId,
             rctx.VolumeName);
       return true;
    }
@@ -1011,7 +1084,7 @@ static int reserve_device(RCTX &rctx)
 
    rctx.suitable_device = true;
    Dmsg2(dbglvl, "jid=%u try reserve %s\n", rctx.jcr->JobId, rctx.device->hdr.name);
-   dcr = new_dcr(rctx.jcr, rctx.device->dev);
+   rctx.jcr->dcr = dcr = new_dcr(rctx.jcr, rctx.jcr->dcr, rctx.device->dev);
    if (!dcr) {
       BSOCK *dir = rctx.jcr->dir_bsock;
       dir->fsend(_("3926 Could not get dcr for device: %s\n"), rctx.device_name);
@@ -1085,7 +1158,7 @@ static int reserve_device(RCTX &rctx)
 
 bail_out:
    rctx.have_volume = false;
-   free_dcr(dcr);
+// free_dcr(dcr);
    Dmsg1(dbglvl, "jid=%u Not OK.\n", (int)rctx.jcr->JobId);
    return 0;
 }
@@ -1382,17 +1455,23 @@ static int can_reserve_drive(DCR *dcr, RCTX &rctx)
    return 0;
 }
 
+
+
+
 /*
- * search_lock is already set on entering this routine 
+ * Queue a reservation error or failure message for this jcr
  */
 static void queue_reserve_message(JCR *jcr)
 {
    int i;   
-   alist *msgs = jcr->reserve_msgs;
+   alist *msgs;
    char *msg;
 
+   jcr->lock();
+
+   msgs = jcr->reserve_msgs;
    if (!msgs) {
-      return;
+      goto bail_out;
    }
    /*
     * Look for duplicate message.  If found, do
@@ -1401,7 +1480,7 @@ static void queue_reserve_message(JCR *jcr)
    for (i=msgs->size()-1; i >= 0; i--) {
       msg = (char *)msgs->get(i);
       if (!msg) {
-         return;
+         goto bail_out;
       }
       /* Comparison based on 4 digit message number */
       if (strncmp(msg, jcr->errmsg, 4) == 0) {
@@ -1410,6 +1489,9 @@ static void queue_reserve_message(JCR *jcr)
    }      
    /* Message unique, so insert it */
    jcr->reserve_msgs->push(bstrdup(jcr->errmsg));
+
+bail_out:
+   jcr->unlock();
 }
 
 /*
@@ -1421,7 +1503,7 @@ void send_drive_reserve_messages(JCR *jcr, void sendit(const char *msg, int len,
    alist *msgs;
    char *msg;
 
-   lock_reservations();
+   jcr->lock();
    msgs = jcr->reserve_msgs;
    if (!msgs || msgs->size() == 0) {
       goto bail_out;
@@ -1437,5 +1519,25 @@ void send_drive_reserve_messages(JCR *jcr, void sendit(const char *msg, int len,
    }
 
 bail_out:
-   unlock_reservations();
+   jcr->unlock();
+}
+
+void release_msgs(JCR *jcr)
+{
+   alist *msgs;
+   char *msg;
+
+   jcr->lock();
+   msgs = jcr->reserve_msgs;
+   if (!msgs) {
+      goto bail_out;
+   }
+   while ((msg = (char *)msgs->pop())) {
+      free(msg);
+   }
+   delete msgs;
+   jcr->reserve_msgs = NULL;
+
+bail_out:
+   jcr->unlock();
 }