]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/autochanger.c
Strip pathname portion off all message routines that print filename:line.
[bacula/bacula] / bacula / src / stored / autochanger.c
index 008cc228aa2c86ca2f122772e627f293389ba017..af5398944655b381eb289ba087a94c3b3c8a98d1 100644 (file)
@@ -7,7 +7,7 @@
  *   Version $Id$
  */
 /*
-   Copyright (C) 2002-2005 Kern Sibbald
+   Copyright (C) 2002-2006 Kern Sibbald
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
@@ -37,7 +37,6 @@ bool init_autochangers()
    /* Ensure that the media_type for each device is the same */
    foreach_res(changer, R_AUTOCHANGER) {
       DEVRES *device;
-      char *media_type = NULL;
       foreach_alist(device, changer->device) {
          /*
           * If the device does not have a changer name or changer command
@@ -62,6 +61,7 @@ bool init_autochangers()
             OK = false;
          }   
 
+#ifdef xxx_needed
          if (media_type == NULL) {
             media_type = device->media_type;     /* get Media Type of first device */
             continue;
@@ -74,6 +74,7 @@ bool init_autochangers()
             OK = false;
             continue;
          }
+#endif
       }
    }
    return OK;
@@ -103,12 +104,16 @@ int autoload_device(DCR *dcr, int writing, BSOCK *dir)
    int rtn_stat = -1;                 /* error status */
    POOLMEM *changer;
 
+   if (!dev->is_autochanger()) {
+      Dmsg0(200, "======== NOT AUTOCHANGER ======\n");
+      return 0;
+   }
    slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
    /*
     * Handle autoloaders here.  If we cannot autoload it, we
     *  will return 0 so that the sysop will be asked to load it.
     */
-   if (writing && dev->is_autochanger() && slot <= 0) {
+   if (writing && slot <= 0) {
       if (dir) {
          return 0;                    /* For user, bail out right now */
       }
@@ -121,7 +126,18 @@ int autoload_device(DCR *dcr, int writing, BSOCK *dir)
    Dmsg1(400, "Want changer slot=%d\n", slot);
 
    changer = get_pool_memory(PM_FNAME);
-   if (slot > 0 && dcr->device->changer_name && dcr->device->changer_command) {
+   if (slot <= 0) {
+      Jmsg(jcr, M_INFO, 0, _("Invalid slot=%d defined, cannot autoload Volume.\n"), slot);
+      rtn_stat = 0;
+   } else if (!dcr->device->changer_name) {
+      Jmsg(jcr, M_INFO, 0, _("No \"Changer Device\" given cannot autoload Volume.\n"));
+      rtn_stat = 0;
+  } else if (!dcr->device->changer_command) {
+      Jmsg(jcr, M_INFO, 0, _("No \"Changer Command\" given cannot autoload Volume.\n"));
+      rtn_stat = 0;
+  } else {
+      /* Attempt to load the Volume */
+
       uint32_t timeout = dcr->device->max_changer_wait;
       int loaded, status;
 
@@ -143,38 +159,41 @@ int autoload_device(DCR *dcr, int writing, BSOCK *dir)
           * Load the desired cassette
           */
          lock_changer(dcr);
-         Dmsg1(400, "Doing changer load slot %d\n", slot);
+         Dmsg1(100, "Doing changer load slot %d\n", slot);
          Jmsg(jcr, M_INFO, 0,
               _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"),
               slot, drive);
          dcr->VolCatInfo.Slot = slot;    /* slot to be loaded */
          changer = edit_device_codes(dcr, changer, 
                       dcr->device->changer_command, "load");
+         dev->close();
+         Dmsg1(200, "Run program=%s\n", changer);
          status = run_program(changer, timeout, NULL);
          if (status == 0) {
             Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
                     slot, drive);
+            Dmsg2(100, "load slot %d, drive %d, status is OK.\n", slot, drive);
             dev->Slot = slot;         /* set currently loaded slot */
          } else {
-           berrno be;
-           be.set_errno(status);
+            berrno be;
+            be.set_errno(status);
+            Dmsg3(100, "load slot %d, drive %d, bad stats=%s.\n", slot, drive,
+               be.strerror());
             Jmsg(jcr, M_FATAL, 0, _("3992 Bad autochanger \"load slot %d, drive %d\": ERR=%s.\n"),
                     slot, drive, be.strerror());
             rtn_stat = -1;            /* hard error */
          }
-         Dmsg2(400, "load slot %d status=%d\n", slot, status);
+         Dmsg2(100, "load slot %d status=%d\n", slot, status);
+         unlock_changer(dcr);
       } else {
          status = 0;                  /* we got what we want */
          dev->Slot = slot;            /* set currently loaded slot */
       }
-      Dmsg1(400, "After changer, status=%d\n", status);
+      Dmsg1(100, "After changer, status=%d\n", status);
       if (status == 0) {              /* did we succeed? */
          rtn_stat = 1;                /* tape loaded by changer */
       }
-   } else {
-      rtn_stat = 0;                   /* no changer found */
    }
-   unlock_changer(dcr);
    free_pool_memory(changer);
    return rtn_stat;
 
@@ -213,10 +232,11 @@ int get_autochanger_loaded_slot(DCR *dcr)
         drive);
    changer = edit_device_codes(dcr, changer, dcr->device->changer_command, "loaded");
    *results = 0;
+   Dmsg1(100, "Run program=%s\n", changer);
    status = run_program(changer, timeout, results);
-   Dmsg3(50, "run_prog: %s stat=%d result=%s\n", changer, status, results);
+   Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
    if (status == 0) {
-      loaded = atoi(results);
+      loaded = str_to_int32(results);
       if (loaded > 0) {
          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
               drive, loaded);
@@ -243,7 +263,7 @@ static void lock_changer(DCR *dcr)
 {
    AUTOCHANGER *changer_res = dcr->device->changer_res;
    if (changer_res) {
-      Dmsg1(100, "Locking changer %s\n", changer_res->hdr.name);
+      Dmsg1(200, "Locking changer %s\n", changer_res->hdr.name);
       P(changer_res->changer_mutex);  /* Lock changer script */
    }
 }
@@ -252,7 +272,7 @@ static void unlock_changer(DCR *dcr)
 {
    AUTOCHANGER *changer_res = dcr->device->changer_res;
    if (changer_res) {
-      Dmsg1(100, "Unlocking changer %s\n", changer_res->hdr.name);
+      Dmsg1(200, "Unlocking changer %s\n", changer_res->hdr.name);
       V(changer_res->changer_mutex);  /* Unlock changer script */
    }
 }
@@ -284,10 +304,6 @@ bool unload_autochanger(DCR *dcr, int loaded)
       loaded = get_autochanger_loaded_slot(dcr);
    }
 
-   /* We are going to load a new tape, so close the device */
-   offline_or_rewind_dev(dev);
-   force_close_device(dev);
-
    if (loaded > 0) {
       POOLMEM *changer = get_pool_memory(PM_FNAME);
       lock_changer(dcr);
@@ -298,6 +314,8 @@ bool unload_autochanger(DCR *dcr, int loaded)
       dcr->VolCatInfo.Slot = loaded;
       changer = edit_device_codes(dcr, changer, 
                    dcr->device->changer_command, "unload");
+      dev->close();
+      Dmsg1(100, "Run program=%s\n", changer);
       int stat = run_program(changer, timeout, NULL);
       dcr->VolCatInfo.Slot = slot;
       if (stat != 0) {
@@ -329,6 +347,7 @@ static bool unload_other_drive(DCR *dcr, int slot)
    AUTOCHANGER *changer = dcr->dev->device->changer_res;
    DEVRES *device;
    bool found = false;
+   bool first = true;
 
    if (!changer) {
       return false;
@@ -347,35 +366,51 @@ static bool unload_other_drive(DCR *dcr, int slot)
    if (!found) {
       return true;
    }
+
+   /* The Volume we want is on another device. */
    if (dev->is_busy()) {
-      Jmsg(jcr, M_WARNING, 0, _("Volume %s is in use by device %s\n"),
-           dcr->VolumeName, dev->print_name());
-      Dmsg2(200, "Volume %s is in use by device %s\n",
+      Dmsg4(100, "Vol %s for dev=%s in use dev=%s slot=%d\n",
+           dcr->VolumeName, dcr->dev->print_name(),
+           dev->print_name(), slot);
+   }   
+   for (int i=0; i < 3; i++) {
+      if (dev->is_busy()) {
+         wait_for_device(dcr->jcr, first);
+         first = false;
+         continue;
+      }
+      break;
+   }
+   P(dev->mutex);
+   if (dev->is_busy()) {
+      Jmsg(jcr, M_WARNING, 0, _("Volume \"%s\" is in use by device %s\n"),
            dcr->VolumeName, dev->print_name());
-      
+      Dmsg4(100, "Vol %s for dev=%s is busy dev=%s slot=%d\n",
+           dcr->VolumeName, dcr->dev->print_name(), dev->print_name(), slot);
+      Dmsg2(100, "num_writ=%d reserv=%d\n", dev->num_writers, dev->reserved_device);
+      V(dev->mutex);
       return false;
    }
 
-   /* We are going to unload a tape, so close the device */
-   offline_or_rewind_dev(dev);
-   force_close_device(dev);
-
    POOLMEM *changer_cmd = get_pool_memory(PM_FNAME);
    lock_changer(dcr);
    Jmsg(jcr, M_INFO, 0,
         _("3307 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
         slot, dev->drive_index);
 
-   Dmsg2(200, "Issuing autochanger \"unload slot %d, drive %d\" command.\n",
+   Dmsg2(100, "Issuing autochanger \"unload slot %d, drive %d\" command.\n",
         slot, dev->drive_index);
 
-   save_slot = dcr->VolCatInfo.Slot;
    save_dev = dcr->dev;
    dcr->dev = dev;
+   save_slot = dcr->VolCatInfo.Slot;
    dcr->VolCatInfo.Slot = slot;
    changer_cmd = edit_device_codes(dcr, changer_cmd, 
                 dcr->device->changer_command, "unload");
-   Dmsg1(200, "Run program=%s\n", changer_cmd);
+   dev->close();
+   Dmsg2(200, "close dev=%s reserve=%d\n", dev->print_name(), 
+      dev->reserved_device);
+   Dmsg1(100, "Run program=%s\n", changer_cmd);
    int stat = run_program(changer_cmd, timeout, NULL);
    dcr->VolCatInfo.Slot = save_slot;
    dcr->dev = save_dev;
@@ -385,14 +420,15 @@ static bool unload_other_drive(DCR *dcr, int slot)
       Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
               slot, dev->drive_index, be.strerror());
 
-      Dmsg3(200, "Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n",
+      Dmsg3(100, "Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n",
               slot, dev->drive_index, be.strerror());
       ok = false;
    } else {
       dev->Slot = 0;            /* nothing loaded */
-      Dmsg0(200, "Slot unloaded\n");
+      Dmsg0(100, "Slot unloaded\n");
    }
    unlock_changer(dcr);
+   V(dev->mutex);
    free_pool_memory(changer_cmd);
    return ok;
 }
@@ -416,6 +452,9 @@ bool autochanger_cmd(DCR *dcr, BSOCK *dir, const char *cmd)
 
    if (!dev->is_autochanger() || !dcr->device->changer_name ||
        !dcr->device->changer_command) {
+      if (strcmp(cmd, "drives") == 0) {
+         bnet_fsend(dir, "drives=1\n");
+      }
       bnet_fsend(dir, _("3993 Device %s not an autochanger device.\n"),
          dev->print_name());
       return false;
@@ -427,8 +466,12 @@ bool autochanger_cmd(DCR *dcr, BSOCK *dir, const char *cmd)
    }
    if (strcmp(cmd, "drives") == 0) {
       AUTOCHANGER *changer_res = dcr->device->changer_res;
-      bnet_fsend(dir, "drives=%d\n", changer_res->device->size());
-      Dmsg1(100, "drives=%d\n", changer_res->device->size());
+      int drives = 1;
+      if (changer_res) {
+         drives = changer_res->device->size();
+      }
+      bnet_fsend(dir, "drives=%d\n", drives);
+      Dmsg1(100, "drives=%d\n", drives);
       return true;
    }
 
@@ -451,12 +494,16 @@ bool autochanger_cmd(DCR *dcr, BSOCK *dir, const char *cmd)
          bnet_send(dir);
       }
    } else if (strcmp(cmd, "slots") == 0 ) {
+      char buf[100], *p;
       /* For slots command, read a single line */
-      bstrncpy(dir->msg, "slots=", len);
-      fgets(dir->msg+6, len-6, bpipe->rfd);
-      dir->msglen = strlen(dir->msg);
+      buf[0] = 0;
+      fgets(buf, sizeof(buf)-1, bpipe->rfd);
+      buf[sizeof(buf)-1] = 0;
+      /* Strip any leading space in front of # of slots */
+      for (p=buf; B_ISSPACE(*p); p++)
+        { }
+      bnet_fsend(dir, "slots=%s", p);
       Dmsg1(100, "<stored: %s", dir->msg);
-      bnet_send(dir);
    } 
                  
    stat = close_bpipe(bpipe);