]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/dircmd.c
- Modify DVD code so that it keeps a state flag that indicates
[bacula/bacula] / bacula / src / stored / dircmd.c
index 43397cae5e23041109ff590a14b7df92a3d4bfce..398607c3a7fe1f1d50d8a9431f5fa1b646fd2b0c 100644 (file)
@@ -8,7 +8,8 @@
  *
  *    N.B. in this file, in general we must use P(dev->mutex) rather
  *      than lock_device(dev) so that we can examine the blocked
- *      state rather than blocking ourselves. In some "safe" cases,
+ *      state rather than blocking ourselves because a Job
+ *      thread has the device blocked. In some "safe" cases,
  *      we can do things to a blocked device. CAREFUL!!!!
  *
  *    File daemon commands are handled in fdcmd.c
@@ -45,6 +46,7 @@ extern struct s_res resources[];
 extern char my_name[];
 extern time_t daemon_start_time;
 extern struct s_last_job last_job;
+extern bool init_done;
 
 /* Static variables */
 static char derrmsg[]     = "3900 Invalid command\n";
@@ -71,7 +73,7 @@ static bool mount_cmd(JCR *jcr);
 static bool unmount_cmd(JCR *jcr);
 static bool autochanger_cmd(JCR *sjcr);
 static bool do_label(JCR *jcr, int relabel);
-static DEVICE *find_device(JCR *jcr, POOL_MEM &dev_name);
+static DEVICE *find_device(JCR *jcr, POOL_MEM &dev_name, int drive);
 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot);
 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
                                char *newname, char *poolname,
@@ -190,6 +192,10 @@ void *handle_connection_request(void *arg)
          break;               /* connection terminated */
       }
       Dmsg1(199, "<dird: %s\n", bs->msg);
+      /* Ensure that device initialization is complete */
+      while (!init_done) {
+         bmicrosleep(1, 0);
+      }
       found = false;
       for (i=0; cmds[i].cmd; i++) {
         if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
@@ -232,7 +238,7 @@ static bool setdebug_cmd(JCR *jcr)
 
    Dmsg1(10, "setdebug_cmd: %s", dir->msg);
    if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
-      bnet_fsend(dir, "3991 Bad setdebug command: %s\n", dir->msg);
+      bnet_fsend(dir, _("3991 Bad setdebug command: %s\n"), dir->msg);
       return 0;
    }
    debug_level = level;
@@ -302,20 +308,24 @@ static bool do_label(JCR *jcr, int relabel)
    DEVICE *dev;
    bool ok = false;
    int slot;
+   int drive;
 
    newname = get_memory(dir->msglen+1);
    oldname = get_memory(dir->msglen+1);
    poolname = get_memory(dir->msglen+1);
    mtype = get_memory(dir->msglen+1);
    if (relabel) {
-      if (sscanf(dir->msg, "relabel %127s OldName=%127s NewName=%127s PoolName=%127s MediaType=%127s Slot=%d",
-          dev_name.c_str(), oldname, newname, poolname, mtype, &slot) == 6) {
+      if (sscanf(dir->msg, "relabel %127s OldName=%127s NewName=%127s PoolName=%127s "
+                 "MediaType=%127s Slot=%d drive=%d",
+                  dev_name.c_str(), oldname, newname, poolname, mtype, 
+                  &slot, &drive) == 7) {
          ok = true;
       }
    } else {
       *oldname = 0;
-      if (sscanf(dir->msg, "label %127s VolumeName=%127s PoolName=%127s MediaType=%127s Slot=%d",
-          dev_name.c_str(), newname, poolname, mtype, &slot) == 5) {
+      if (sscanf(dir->msg, "label %127s VolumeName=%127s PoolName=%127s "
+                 "MediaType=%127s Slot=%d drive=%d", 
+          dev_name.c_str(), newname, poolname, mtype, &slot, &drive) == 6) {
          ok = true;
       }
    }
@@ -324,7 +334,7 @@ static bool do_label(JCR *jcr, int relabel)
       unbash_spaces(oldname);
       unbash_spaces(poolname);
       unbash_spaces(mtype);
-      dev = find_device(jcr, dev_name);
+      dev = find_device(jcr, dev_name, drive);
       if (dev) {
          P(dev->mutex);               /* Use P to avoid indefinite block */
          if (!dev->is_open()) {
@@ -396,6 +406,7 @@ static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
              dev->VolHdr.VolumeName);
          break;
       }
+
       /* Relabel request. If oldname matches, continue */
       if (strcmp(oldname, dev->VolHdr.VolumeName) != 0) {
          bnet_fsend(dir, _("3921 Wrong volume mounted.\n"));
@@ -468,7 +479,7 @@ static bool read_label(DCR *dcr)
    return ok;
 }
 
-static DEVICE *find_device(JCR *jcr, POOL_MEM &devname)
+static DEVICE *find_device(JCR *jcr, POOL_MEM &devname, int drive)
 {
    DEVRES *device;
    AUTOCHANGER *changer;
@@ -511,9 +522,11 @@ static DEVICE *find_device(JCR *jcr, POOL_MEM &devname)
             if (!device->dev->autoselect) {
                continue;              /* device is not available */
             }
-            Dmsg1(20, "Found changer device %s\n", device->hdr.name);
-            found = true;
-            break;
+            if (drive < 0 || drive == (int)device->dev->drive_index) {
+               Dmsg1(20, "Found changer device %s\n", device->hdr.name);
+               found = true;
+               break;
+            }
          }
          break;                    /* we found it but could not open a device */
       }
@@ -537,9 +550,10 @@ static bool mount_cmd(JCR *jcr)
    BSOCK *dir = jcr->dir_bsock;
    DEVICE *dev;
    DCR *dcr;
+   int drive;
 
-   if (sscanf(dir->msg, "mount %127s", devname.c_str()) == 1) {
-      dev = find_device(jcr, devname);
+   if (sscanf(dir->msg, "mount %127s drive=%d", devname.c_str(), &drive) == 2) {
+      dev = find_device(jcr, devname, drive);
       dcr = jcr->dcr;
       if (dev) {
          P(dev->mutex);               /* Use P to avoid indefinite block */
@@ -625,7 +639,7 @@ static bool mount_cmd(JCR *jcr)
                   bnet_fsend(dir, _("3002 Device %s is mounted.\n"), 
                      dev->print_name());
                } else {
-                  bnet_fsend(dir, "3907 %s", strerror_dev(dev));
+                  bnet_fsend(dir, _("3907 %s"), strerror_dev(dev));
                } 
             } else { /* must be file */
                bnet_fsend(dir, _("3906 File device %s is always mounted.\n"),
@@ -657,9 +671,10 @@ static bool unmount_cmd(JCR *jcr)
    POOL_MEM devname;
    BSOCK *dir = jcr->dir_bsock;
    DEVICE *dev;
+   int drive;
 
-   if (sscanf(dir->msg, "unmount %127s", devname.c_str()) == 1) {
-      dev = find_device(jcr, devname);
+   if (sscanf(dir->msg, "unmount %127s drive=%d", devname.c_str(), &drive) == 2) {
+      dev = find_device(jcr, devname, drive);
       if (dev) {
          P(dev->mutex);               /* Use P to avoid indefinite block */
          if (!dev->is_open()) {
@@ -726,9 +741,10 @@ static bool release_cmd(JCR *jcr)
    POOL_MEM devname;
    BSOCK *dir = jcr->dir_bsock;
    DEVICE *dev;
+   int drive;
 
-   if (sscanf(dir->msg, "release %127s", devname.c_str()) == 1) {
-      dev = find_device(jcr, devname);
+   if (sscanf(dir->msg, "release %127s drive=%d", devname.c_str(), &drive) == 2) {
+      dev = find_device(jcr, devname, drive);
       if (dev) {
          P(dev->mutex);               /* Use P to avoid indefinite block */
          if (!dev->is_open()) {
@@ -794,17 +810,15 @@ static bool autochanger_cmd(JCR *jcr)
       ok = true;
    }
    if (ok) {
-      dev = find_device(jcr, devname);
+      dev = find_device(jcr, devname, -1);
       dcr = jcr->dcr;
       if (dev) {
          P(dev->mutex);               /* Use P to avoid indefinite block */
          if (!dev->is_tape()) {
             bnet_fsend(dir, _("3995 Device %s is not an autochanger.\n"), 
                dev->print_name());
-         } else if (!dev->is_open()) {
-            autochanger_cmd(dcr, dir, cmd);
          /* Under certain "safe" conditions, we can steal the lock */
-         } else if (dev->can_steal_lock()) {
+         } else if (!dev->is_open() || dev->can_steal_lock()) {
             autochanger_cmd(dcr, dir, cmd);
          } else if (dev->is_busy() || dev->is_blocked()) {
             send_dir_busy_message(dir, dev);
@@ -833,9 +847,11 @@ static bool readlabel_cmd(JCR *jcr)
    BSOCK *dir = jcr->dir_bsock;
    DEVICE *dev;
    int Slot;
+   int drive;
 
-   if (sscanf(dir->msg, "readlabel %127s Slot=%d", devname.c_str(), &Slot) == 2) {
-      dev = find_device(jcr, devname);
+   if (sscanf(dir->msg, "readlabel %127s Slot=%d drive=%d", devname.c_str(), 
+       &Slot, &drive) == 3) {
+      dev = find_device(jcr, devname, drive);
       if (dev) {
          P(dev->mutex);               /* Use P to avoid indefinite block */
          if (!dev->is_open()) {
@@ -911,12 +927,10 @@ static bool try_autoload_device(JCR *jcr, int slot, const char *VolName)
    }
 
    /* Ensure that the device is open -- autoload_device() closes it */
-   for ( ; !dev->is_open(); ) {
-      if (dev->open(dcr, OPEN_READ_WRITE) < 0) {
-         bnet_fsend(dir, _("3910 Unable to open device %s: ERR=%s\n"),
-            dev->print_name(), dev->strerror());
-         return false;
-      }
+   if (dev->open(dcr, OPEN_READ_WRITE) < 0) {
+      bnet_fsend(dir, _("3910 Unable to open device %s: ERR=%s\n"),
+         dev->print_name(), dev->strerror());
+      return false;
    }
    return true;
 }