-/*
- * We reserve the device for appending by incrementing the
- * reserved_device. We do virtually all the same work that
- * is done in acquire_device_for_append(), but we do
- * not attempt to mount the device. This routine allows
- * the DIR to reserve multiple devices before *really*
- * starting the job. It also permits the SD to refuse
- * certain devices (not up, ...).
- */
-bool reserve_device_for_append(JCR *jcr, DEVICE *dev)
-{
- DCR *dcr = jcr->dcr;
- bool recycle;
- bool ok = false;
-
- ASSERT(dcr);
-
- lock_device(dev);
- block_device(dev, BST_DOING_ACQUIRE);
- unlock_device(dev);
- if (device_is_unmounted(dev)) {
- Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
- dev_name(dev));
- goto get_out;
- }
- Dmsg1(190, "reserve_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
- if (dev->can_append() || dev->num_writers > 0 || dev->reserved_device) {
- Dmsg0(190, "device already in append.\n");
- /*
- * Device already in append mode or reserved for write
- *
- * Check if we have the right Volume mounted
- * OK if current volume info OK
- * OK if next volume matches current volume
- */
- bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
- if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE) &&
- !(dir_find_next_appendable_volume(dcr) &&
- strcmp(dev->VolHdr.VolName, dcr->VolumeName) == 0)) { /* wrong tape mounted */
- Dmsg0(190, "Wrong tape mounted.\n");
- if (dev->num_writers != 0 || dev->reserved_device) {
- Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
- goto get_out;
- }
- } else {
- /*
- * At this point, the correct tape is already mounted, so
- * we do not need to do mount_next_write_volume(), unless
- * we need to recycle the tape.
- */
- recycle = strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
- Dmsg1(190, "Correct tape mounted. recycle=%d\n", recycle);
- if (recycle && dev->num_writers != 0) {
- Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
- " because it is in use by another job.\n"));
- goto get_out;
- }
- if (dev->num_writers == 0) {
- memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
- }
- }
- } else {
- if (dev->can_read()) {
- Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
- goto get_out;
- }
- ASSERT(dev->num_writers == 0);
- }
-
- dev->reserved_device++;
- dcr->reserved_device = true;
- ok = true;
-
-get_out:
- P(dev->mutex);
- unblock_device(dev);
- V(dev->mutex);
- return ok;
-}