]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/autochanger.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / stored / autochanger.c
index 5da5ce5c254ab25f25784ca33e510a18f6f4a171..8c822b9ebbd7eb1295d0ce050a07a3151db7b795 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,15 @@ int autoload_device(DCR *dcr, int writing, BSOCK *dir)
    int rtn_stat = -1;                 /* error status */
    POOLMEM *changer;
 
+   if (!dev->is_autochanger()) {
+      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 */
       }
@@ -150,6 +154,8 @@ int autoload_device(DCR *dcr, int writing, BSOCK *dir)
          dcr->VolCatInfo.Slot = slot;    /* slot to be loaded */
          changer = edit_device_codes(dcr, changer, 
                       dcr->device->changer_command, "load");
+         offline_or_rewind_dev(dev);
+         force_close_device(dev);
          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"),
@@ -163,6 +169,7 @@ int autoload_device(DCR *dcr, int writing, BSOCK *dir)
             rtn_stat = -1;            /* hard error */
          }
          Dmsg2(400, "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 */
@@ -174,7 +181,6 @@ int autoload_device(DCR *dcr, int writing, BSOCK *dir)
    } else {
       rtn_stat = 0;                   /* no changer found */
    }
-   unlock_changer(dcr);
    free_pool_memory(changer);
    return rtn_stat;
 
@@ -216,7 +222,7 @@ int get_autochanger_loaded_slot(DCR *dcr)
    status = run_program(changer, timeout, results);
    Dmsg3(50, "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);
@@ -284,10 +290,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 +300,8 @@ bool unload_autochanger(DCR *dcr, int loaded)
       dcr->VolCatInfo.Slot = loaded;
       changer = edit_device_codes(dcr, changer, 
                    dcr->device->changer_command, "unload");
+      offline_or_rewind_dev(dev);
+      force_close_device(dev);
       int stat = run_program(changer, timeout, NULL);
       dcr->VolCatInfo.Slot = slot;
       if (stat != 0) {
@@ -329,6 +333,8 @@ 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,19 +353,27 @@ static bool unload_other_drive(DCR *dcr, int slot)
    if (!found) {
       return true;
    }
+
+   /* The Volume we want is on another device. */
+       
+   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());
       Dmsg2(200, "Volume %s is in use by device %s\n",
            dcr->VolumeName, dev->print_name());
-      
+      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,
@@ -376,6 +390,8 @@ static bool unload_other_drive(DCR *dcr, int slot)
    changer_cmd = edit_device_codes(dcr, changer_cmd, 
                 dcr->device->changer_command, "unload");
    Dmsg1(200, "Run program=%s\n", changer_cmd);
+   offline_or_rewind_dev(dev);
+   force_close_device(dev);
    int stat = run_program(changer_cmd, timeout, NULL);
    dcr->VolCatInfo.Slot = save_slot;
    dcr->dev = save_dev;
@@ -393,6 +409,7 @@ static bool unload_other_drive(DCR *dcr, int slot)
       Dmsg0(200, "Slot unloaded\n");
    }
    unlock_changer(dcr);
+   V(dev->mutex);
    free_pool_memory(changer_cmd);
    return ok;
 }
@@ -416,17 +433,30 @@ 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;
    }
 
-   changer = get_pool_memory(PM_FNAME);
    /* List command? */
    if (strcmp(cmd, "list") == 0) {
       unload_autochanger(dcr, -1);
    }
+   if (strcmp(cmd, "drives") == 0) {
+      AUTOCHANGER *changer_res = dcr->device->changer_res;
+      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;
+   }
 
+   changer = get_pool_memory(PM_FNAME);
    lock_changer(dcr);
    /* Now issue the command */
    changer = edit_device_codes(dcr, changer, 
@@ -444,14 +474,18 @@ bool autochanger_cmd(DCR *dcr, BSOCK *dir, const char *cmd)
          Dmsg1(100, "<stored: %s\n", dir->msg);
          bnet_send(dir);
       }
-   } else {
+   } 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);
    if (stat != 0) {