+ if (dev->poll && !dev->is_tape()) {
+ return try_default; /* if polling, don't try to create new labels */
+ }
+ /* For a tape require it to be opened and read before labeling */
+ if (!opened && dev->is_tape()) {
+ return try_default;
+ }
+ if (dev->has_cap(CAP_LABEL) && (dcr->VolCatInfo.VolCatBytes == 0 ||
+ (!dev->is_tape() && strcmp(dcr->VolCatInfo.VolCatStatus,
+ "Recycle") == 0))) {
+ Dmsg0(150, "Create volume label\n");
+ /* Create a new Volume label and write it to the device */
+ if (!write_new_volume_label_to_dev(dcr, dcr->VolumeName,
+ dcr->pool_name, false, /* no relabel */ false /* defer DVD label */)) {
+ Dmsg0(150, "!write_vol_label\n");
+ if (opened) {
+ mark_volume_in_error(dcr);
+ }
+ return try_next_vol;
+ }
+ Dmsg0(150, "dir_update_vol_info. Set Append\n");
+ /* Copy Director's info into the device info */
+ dev->VolCatInfo = dcr->VolCatInfo; /* structure assignment */
+ if (!dir_update_volume_info(dcr, true)) { /* indicate tape labeled */
+ return try_error;
+ }
+ Jmsg(dcr->jcr, M_INFO, 0, _("Labeled new Volume \"%s\" on device %s.\n"),
+ dcr->VolumeName, dev->print_name());
+ return try_read_vol; /* read label we just wrote */
+ }
+ if (!dev->has_cap(CAP_LABEL) && dcr->VolCatInfo.VolCatBytes == 0) {
+ Jmsg(dcr->jcr, M_WARNING, 0, _("Device %s not configured to autolabel Volumes.\n"),
+ dev->print_name());
+ }
+ /* If not removable, Volume is broken */
+ if (!dev->is_removable()) {
+ Jmsg(dcr->jcr, M_WARNING, 0, _("Volume \"%s\" not on device %s.\n"),
+ dcr->VolumeName, dev->print_name());
+ mark_volume_in_error(dcr);
+ return try_next_vol;
+ }
+ return try_default;
+}
+
+
+/*
+ * Mark volume in error in catalog
+ */
+void mark_volume_in_error(DCR *dcr)
+{
+ DEVICE *dev = dcr->dev;
+ Jmsg(dcr->jcr, M_INFO, 0, _("Marking Volume \"%s\" in Error in Catalog.\n"),
+ dcr->VolumeName);
+ dev->VolCatInfo = dcr->VolCatInfo; /* structure assignment */
+ bstrncpy(dev->VolCatInfo.VolCatStatus, "Error", sizeof(dev->VolCatInfo.VolCatStatus));
+ Dmsg0(150, "dir_update_vol_info. Set Error.\n");
+ dir_update_volume_info(dcr, false);
+}
+
+/*
+ * The Volume is not in the correct slot, so mark this
+ * Volume as not being in the Changer.
+ */
+static void mark_volume_not_inchanger(DCR *dcr)
+{
+ JCR *jcr = dcr->jcr;
+ DEVICE *dev = dcr->dev;
+ Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
+" Setting InChanger to zero in catalog.\n"),
+ dcr->VolCatInfo.VolCatName, dcr->VolCatInfo.Slot);
+ dev->VolCatInfo = dcr->VolCatInfo; /* structure assignment */
+ dcr->VolCatInfo.InChanger = false;
+ dev->VolCatInfo.InChanger = false;
+ Dmsg0(400, "update vol info in mount\n");
+ dir_update_volume_info(dcr, true); /* set new status */
+}
+
+/*
+ * Either because we are going to hang a new volume, or because
+ * of explicit user request, we release the current volume.
+ */
+void release_volume(DCR *dcr)
+{
+ JCR *jcr = dcr->jcr;
+ DEVICE *dev = dcr->dev;
+ if (dcr->WroteVol) {
+ Jmsg0(jcr, M_ERROR, 0, _("Hey!!!!! WroteVol non-zero !!!!!\n"));
+ Dmsg0(190, "Hey!!!!! WroteVol non-zero !!!!!\n");
+ }
+ /*
+ * First erase all memory of the current volume
+ */
+ dev->block_num = dev->file = 0;
+ dev->EndBlock = dev->EndFile = 0;
+ memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
+ memset(&dcr->VolCatInfo, 0, sizeof(dcr->VolCatInfo));
+ free_volume(dev);
+ memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
+ /* Force re-read of label */
+ dev->clear_labeled();
+ dev->clear_read();
+ dev->clear_append();
+ dev->label_type = B_BACULA_LABEL;
+ dcr->VolumeName[0] = 0;
+
+ if (dev->is_open() && (!dev->is_tape() || !dev->has_cap(CAP_ALWAYSOPEN))) {
+ dev->close();
+ }
+
+ /* If we have not closed the device, then at least rewind the tape */
+ if (dev->is_open()) {
+ dev->offline_or_rewind();
+ }
+ Dmsg0(190, "release_volume\n");
+}
+
+/*
+ * If we are reading, we come here at the end of the tape
+ * and see if there are more volumes to be mounted.
+ */
+bool mount_next_read_volume(DCR *dcr)