+ DEVICE *dev = dcr->dev;
+ JCR *jcr = dcr->jcr;
+ int slot;
+ uint32_t timeout = dcr->device->max_changer_wait;
+ bool ok = true;
+
+ if (loaded == 0) {
+ return true;
+ }
+
+ if (!dev->is_autochanger() || !dcr->device->changer_name ||
+ !dcr->device->changer_command) {
+ return false;
+ }
+
+ /* Virtual disk autochanger */
+ if (dcr->device->changer_command[0] == 0) {
+ dev->clear_unload();
+ return true;
+ }
+
+ lock_changer(dcr);
+ if (loaded < 0) {
+ loaded = get_autochanger_loaded_slot(dcr);
+ }
+
+ if (loaded > 0) {
+ POOL_MEM results(PM_MESSAGE);
+ POOLMEM *changer = get_pool_memory(PM_FNAME);
+ Jmsg(jcr, M_INFO, 0,
+ _("3307 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
+ loaded, dev->drive_index);
+ slot = dcr->VolCatInfo.Slot;
+ 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_full_output(changer, timeout, results.addr());
+ dcr->VolCatInfo.Slot = slot;
+ if (stat != 0) {
+ berrno be;
+ be.set_errno(stat);
+ Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d, drive %d\": "
+ "ERR=%s\nResults=%s\n"),
+ loaded, dev->drive_index, be.bstrerror(), results.c_str());
+ ok = false;
+ dev->clear_slot(); /* unknown */
+ } else {
+ dev->set_slot(0); /* nothing loaded */
+ }
+
+ free_volume(dev); /* Free any volume associated with this drive */
+ free_pool_memory(changer);
+ }
+ unlock_changer(dcr);
+ if (ok) {
+ dev->clear_unload();
+ }
+ return ok;
+}
+
+/*
+ * 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);