+/*
+ * Unload the slot if mounted in a different drive
+ */
+static bool unload_other_drive(DCR *dcr, int slot)
+{
+ DEVICE *dev = NULL;
+ bool found = false;
+ AUTOCHANGER *changer = dcr->dev->device->changer_res;
+ DEVRES *device;
+ int retries = 0; /* wait for device retries */
+
+ if (!changer) {
+ return false;
+ }
+ if (changer->device->size() == 1) {
+ return true;
+ }
+
+ foreach_alist(device, changer->device) {
+ if (device->dev && device->dev->get_slot() == slot) {
+ found = true;
+ dev = device->dev;
+ break;
+ }
+ }
+ if (!found) {
+ return true;
+ }
+
+ /* The Volume we want is on another device. */
+ if (dev->is_busy()) {
+ 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, retries);
+ continue;
+ }
+ break;
+ }
+ if (dev->is_busy()) {
+ Jmsg(dcr->jcr, M_WARNING, 0, _("Volume \"%s\" wanted on %s is in use by device %s\n"),
+ dcr->VolumeName, dcr->dev->print_name(), 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(), dev->get_slot());
+ Dmsg2(100, "num_writ=%d reserv=%d\n", dev->num_writers, dev->num_reserved());
+ volume_unused(dcr);
+ return false;
+ }
+ return unload_dev(dcr, dev);
+}
+
+/*
+ * Unconditionally unload a specified drive
+ */
+bool unload_dev(DCR *dcr, DEVICE *dev)
+{
+ JCR *jcr = dcr->jcr;
+ bool ok = true;
+ uint32_t timeout = dcr->device->max_changer_wait;
+ AUTOCHANGER *changer = dcr->dev->device->changer_res;
+ DEVICE *save_dev;
+ int save_slot;
+
+ if (!changer) {
+ return false;
+ }
+
+ save_dev = dcr->dev; /* save dcr device */
+ dcr->dev = dev; /* temporarily point dcr at other device */
+
+ if (dev->get_slot() <= 0 && get_autochanger_loaded_slot(dcr) <= 0) {
+ dcr->dev = save_dev;
+ return false;
+ }
+ save_slot = dcr->VolCatInfo.Slot;
+ dcr->VolCatInfo.Slot = dev->get_slot();
+
+// dev->dlock();
+
+ POOLMEM *changer_cmd = get_pool_memory(PM_FNAME);
+ POOL_MEM results(PM_MESSAGE);
+ lock_changer(dcr);
+ Jmsg(jcr, M_INFO, 0,
+ _("3307 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
+ dev->get_slot(), dev->drive_index);
+
+ Dmsg2(100, "Issuing autochanger \"unload slot %d, drive %d\" command.\n",
+ dev->get_slot(), dev->drive_index);
+
+ changer_cmd = edit_device_codes(dcr, changer_cmd,
+ dcr->device->changer_command, "unload");
+ dev->close();
+ Dmsg2(200, "close dev=%s reserve=%d\n", dev->print_name(),
+ dev->num_reserved());
+ Dmsg1(100, "Run program=%s\n", changer_cmd);
+ int stat = run_program_full_output(changer_cmd, timeout, results.addr());
+ dcr->VolCatInfo.Slot = save_slot;
+ dcr->dev = save_dev;
+ if (stat != 0) {
+ berrno be;
+ be.set_errno(stat);
+ Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
+ dev->get_slot(), dev->drive_index, be.bstrerror());
+
+ Dmsg3(100, "Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n",
+ dev->get_slot(), dev->drive_index, be.bstrerror());
+ ok = false;
+ dev->clear_slot(); /* unknown */
+ } else {
+ Dmsg2(100, "Slot %d unloaded %s\n", dev->get_slot(), dev->print_name());
+ dev->set_slot(0); /* nothing loaded */
+ }
+ if (ok) {
+ dev->clear_unload();
+ }
+ unlock_changer(dcr);
+
+// dev->dunlock();
+
+ free_volume(dev); /* Free any volume associated with this drive */
+ free_pool_memory(changer_cmd);
+ return ok;
+}
+
+