]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/next_vol.c
Add heap stats to Dir and SD -- eliminate #ifdefs
[bacula/bacula] / bacula / src / dird / next_vol.c
index ddb4e125b7e9f480ba9682bf1b6c0b55d84e1e80..687d85cdf872b5b9ba18d4dabff1280a6e7e91f0 100644 (file)
@@ -9,7 +9,7 @@
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
+   Copyright (C) 2001-2004 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
  *   MEDIA_DBR mr (zeroed out)
  *   create -- whether or not to create a new volume
  */
-int find_next_volume_for_append(JCR *jcr, MEDIA_DBR *mr, int create)
+int find_next_volume_for_append(JCR *jcr, MEDIA_DBR *mr, bool create)
 {
-   int ok, retry = 0;
+   int retry = 0;
+   bool ok;
+   bool InChanger;
 
    mr->PoolId = jcr->PoolId;
    bstrncpy(mr->MediaType, jcr->store->media_type, sizeof(mr->MediaType));
    Dmsg2(120, "CatReq FindMedia: Id=%d, MediaType=%s\n", mr->PoolId, mr->MediaType);
+   /*
+    * If we are using an Autochanger, restrict Volume 
+    *  search to the Autochanger on the first pass 
+    */
+   InChanger = jcr->store->autochanger;
    /*
     * Find the Next Volume for Append
     */
    db_lock(jcr->db);
    for ( ;; ) {
       bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));  /* want only appendable volumes */
-      ok = db_find_next_volume(jcr, jcr->db, 1, mr);  
+      /*
+       *  1. Look for volume with "Append" status.
+       */
+      ok = db_find_next_volume(jcr, jcr->db, 1, InChanger, mr);  
       Dmsg2(100, "catreq after find_next_vol ok=%d FW=%d\n", ok, mr->FirstWritten);
       if (!ok) {
-        /* Well, try finding recycled volumes */
-        ok = find_recycled_volume(jcr, mr);
+        /*
+         * 2. Try finding a recycled volume
+         */
+        ok = find_recycled_volume(jcr, InChanger, mr);
          Dmsg2(100, "find_recycled_volume %d FW=%d\n", ok, mr->FirstWritten);
         if (!ok) {
+           /*
+            * 3. Try pruning Volumes
+            */
            prune_volumes(jcr);  
-           ok = recycle_oldest_purged_volume(jcr, mr);
+           ok = recycle_oldest_purged_volume(jcr, InChanger, mr);
+           if (InChanger) {
+              InChanger = false;
+              if (!ok) {
+                 continue;           /* retry again accepting any volume */
+              }
+           }
             Dmsg2(200, "find_recycled_volume2 %d FW=%d\n", ok, mr->FirstWritten);
            if (!ok && create) {
-              /* See if we can create a new Volume */
+              /*
+                * 4. Try "creating" a new Volume
+               */
               ok = newVolume(jcr, mr);
            }
         }
 
+        /* 
+         *  Look at more drastic ways to find an Appendable Volume
+         */ 
         if (!ok && (jcr->pool->purge_oldest_volume ||
                     jcr->pool->recycle_oldest_volume)) {
             Dmsg2(200, "No next volume found. PurgeOldest=%d\n RecyleOldest=%d",
                jcr->pool->purge_oldest_volume, jcr->pool->recycle_oldest_volume);
            /* Find oldest volume to recycle */
-           ok = db_find_next_volume(jcr, jcr->db, -1, mr);
+           ok = db_find_next_volume(jcr, jcr->db, -1, InChanger, mr);
             Dmsg1(400, "Find oldest=%d\n", ok);
            if (ok) {
               UAContext *ua;
                Dmsg0(400, "Try purge.\n");
-              /* Try to purge oldest volume */
+              /* 
+               * 5.  Try to purging oldest volume only if not UA calling us.
+               */
               ua = new_ua_context(jcr);
-              if (jcr->pool->purge_oldest_volume) {
+              if (jcr->pool->purge_oldest_volume && create) {
                   Jmsg(jcr, M_INFO, 0, _("Purging oldest volume \"%s\"\n"), mr->VolumeName);
                  ok = purge_jobs_from_volume(ua, mr);
-              } else {
+              /*
+               * 5. or try recycling the oldest volume
+               */
+              } else if (jcr->pool->recycle_oldest_volume) {
                   Jmsg(jcr, M_INFO, 0, _("Pruning oldest volume \"%s\"\n"), mr->VolumeName);
                  ok = prune_volume(ua, mr);
               }
@@ -100,11 +131,6 @@ int find_next_volume_for_append(JCR *jcr, MEDIA_DBR *mr, int create)
       if (ok) {
         /* If we can use the volume, check if it is expired */
         if (has_volume_expired(jcr, mr)) {
-           /* Need to update media */
-           if (!db_update_media_record(jcr, jcr->db, mr)) {
-               Jmsg(jcr, M_ERROR, 0, _("Catalog error updating volume \"%s\". ERR=%s"),
-                   mr->VolumeName, db_strerror(jcr->db));
-           }           
            if (retry++ < 200) {            /* sanity check */
               continue;                    /* try again from the top */
            } else {
@@ -172,6 +198,13 @@ bool has_volume_expired(JCR *jcr, MEDIA_DBR *mr)
         }
       }
    }
+   if (expired) {
+      /* Need to update media */
+      if (!db_update_media_record(jcr, jcr->db, mr)) {
+         Jmsg(jcr, M_ERROR, 0, _("Catalog error updating volume \"%s\". ERR=%s"),
+             mr->VolumeName, db_strerror(jcr->db));
+      }          
+   }  
    return expired;
 }
 
@@ -181,34 +214,39 @@ bool has_volume_expired(JCR *jcr, MEDIA_DBR *mr)
  *  Returns: on failure - reason = NULL
  *          on success - reason - pointer to reason
  */
-bool is_volume_valid_or_recyclable(JCR *jcr, MEDIA_DBR *mr, char **reason)
+void check_if_volume_valid_or_recyclable(JCR *jcr, MEDIA_DBR *mr, const char **reason)
 {
    int ok;
 
    *reason = NULL;
 
    /*  Check if a duration or limit has expired */
-   has_volume_expired(jcr, mr);
+   if (has_volume_expired(jcr, mr)) {
+      *reason = "volume has expired";
+      /* Keep going because we may be able to recycle volume */
+   }
 
    /*
     * Now see if we can use the volume as is
     */
    if (strcmp(mr->VolStatus, "Append") == 0 ||
        strcmp(mr->VolStatus, "Recycle") == 0) {
-      return true;
+      *reason = NULL;
+      return;
    }
                                                                                        
    /*
-    * Check if the Volume is alreay marked for recycling
+    * Check if the Volume is already marked for recycling
     */
    if (strcmp(mr->VolStatus, "Purged") == 0) {
       if (recycle_volume(jcr, mr)) {
          Jmsg(jcr, M_INFO, 0, "Recycled current volume \"%s\"\n", mr->VolumeName);
-        return true;
+        *reason = NULL;
+        return;
       } else {
          /* In principle this shouldn't happen */
          *reason = "and recycling of current volume failed";
-        return false;
+        return;
       }
    }
                                                                                        
@@ -239,7 +277,7 @@ bool is_volume_valid_or_recyclable(JCR *jcr, MEDIA_DBR *mr, char **reason)
         /* If fully purged, recycle current volume */
         if (recycle_volume(jcr, mr)) {
             Jmsg(jcr, M_INFO, 0, "Recycled current volume \"%s\"\n", mr->VolumeName);
-           return true;               /* Good volume */
+           *reason = NULL;
         } else {
             *reason = "but should be Append, Purged or Recycle (recycling of the "
                "current volume failed)";
@@ -249,5 +287,4 @@ bool is_volume_valid_or_recyclable(JCR *jcr, MEDIA_DBR *mr, char **reason)
             "recycle current volume, as it still contains unpruned data)";
       }
    }
-   return *reason ? false : true;
 }