* subcommands that are handled
* in job.c.
*
+ * 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,
+ * we can do things to a blocked device. CAREFUL!!!!
+ *
* File daemon commands are handled in fdcmd.c
*
* Kern Sibbald, May MMI
static int mount_cmd(JCR *jcr);
static int unmount_cmd(JCR *jcr);
static int status_cmd(JCR *sjcr);
-static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname);
+static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname,
+ int Slot);
+static void send_blocked_status(JCR *jcr, DEVICE *dev);
struct s_cmds {
char *cmd;
} else {
P(jcr->mutex);
oldStatus = jcr->JobStatus;
- jcr->JobStatus = JS_Cancelled;
- if (!jcr->authenticated && jcr->JobStatus == JS_WaitFD) {
+ set_jcr_job_status(jcr, JS_Cancelled);
+ if (!jcr->authenticated && oldStatus == JS_WaitFD) {
pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
}
V(jcr->mutex);
if (jcr->file_bsock) {
bnet_sig(jcr->file_bsock, BNET_TERMINATE);
}
- bnet_fsend(dir, _("3000 Job %s Status=%c marked to be cancelled.\n"),
- jcr->Job, oldStatus);
+ bnet_fsend(dir, _("3000 Job %s marked to be cancelled.\n"), jcr->Job);
free_jcr(jcr);
}
} else {
jcr->device = device;
dev = device->dev;
-/* *****FIXME***** add autochanger code */
-
-#ifdef NEW_LOCK
- P(dev->lock.mutex);
- if (!(dev->state & ST_OPENED)) {
- label_it = TRUE;
- } else if (dev->dev_blocked &&
- dev->dev_blocked != BST_DOING_ACQUIRE) { /* device blocked? */
- label_it = TRUE;
- } else if (dev->state & ST_READ || dev->num_writers) {
- if (dev->state & ST_READ) {
- bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"),
- dev_name(dev));
- } else {
- bnet_fsend(dir, _("3902 Device %s is busy with %d writer(s).\n"),
- dev_name(dev), dev->num_writers);
- }
- } else { /* device not being used */
- label_it = TRUE;
- }
- if (label_it) {
- new_steal_device_lock(dev, &hold, BST_WRITING_LABEL);
- if (!(dev->state & ST_OPENED)) {
- if (open_dev(dev, volname, READ_WRITE) < 0) {
- bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
- } else {
- label_volume_if_ok(jcr, dev, volname, poolname);
- force_close_dev(dev);
- }
- } else {
- label_volume_if_ok(jcr, dev, volname, poolname);
- }
- new_return_device_lock(dev, &hold);
- }
- V(dev->lock.mutex);
-#else
- P(dev->mutex);
+ P(dev->mutex); /* Use P to avoid indefinite block */
if (!(dev->state & ST_OPENED)) {
if (open_dev(dev, volname, READ_WRITE) < 0) {
bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
} else {
- label_volume_if_ok(jcr, dev, volname, poolname);
+ label_volume_if_ok(jcr, dev, volname, poolname, slot);
force_close_dev(dev);
}
+ /* Under certain "safe" conditions, we can steal the lock */
} else if (dev->dev_blocked &&
- dev->dev_blocked != BST_DOING_ACQUIRE) { /* device blocked? */
- label_volume_if_ok(jcr, dev, volname, poolname);
+ (dev->dev_blocked == BST_UNMOUNTED ||
+ dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
+ dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
+ label_volume_if_ok(jcr, dev, volname, poolname, slot);
} else if (dev->state & ST_READ || dev->num_writers) {
if (dev->state & ST_READ) {
bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"),
dev_name(dev), dev->num_writers);
}
} else { /* device not being used */
- label_volume_if_ok(jcr, dev, volname, poolname);
+ label_volume_if_ok(jcr, dev, volname, poolname, slot);
}
V(dev->mutex);
-#endif
} else {
bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
}
*
* Enter with the mutex set
*/
-static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname)
+static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname,
+ int slot)
{
BSOCK *dir = jcr->dir_bsock;
DEV_BLOCK *block;
steal_device_lock(dev, &hold, BST_WRITING_LABEL);
strcpy(jcr->VolumeName, vname);
+ jcr->VolCatInfo.Slot = slot;
+ autoload_device(jcr, dev, 0, dir); /* autoload if possible */
block = new_block(dev);
+
+ /* Ensure that the device is open -- not autoload_device() closes it */
+ for ( ; !(dev->state & ST_OPENED); ) {
+ if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
+ if (dev->dev_errno == EAGAIN || dev->dev_errno == EBUSY) {
+ sleep(30);
+ }
+ bnet_fsend(dir, _("3903 Unable to open device %s. ERR=%s\n"),
+ dev_name(dev), strerror_dev(dev));
+ goto bail_out;
+ }
+ }
+
+ /* See what we have for a Volume */
switch (read_dev_volume_label(jcr, dev, block)) {
case VOL_NAME_ERROR:
case VOL_VERSION_ERROR:
break;
case VOL_IO_ERROR:
case VOL_NO_LABEL:
- write_volume_label_to_dev(jcr, jcr->device, vname, poolname);
+ if (!write_volume_label_to_dev(jcr, jcr->device, vname, poolname)) {
+ bnet_fsend(dir, _("3903 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
+ break;
+ }
strcpy(jcr->VolumeName, vname);
bnet_fsend(dir, _("3000 OK label. Volume=%s Device=%s\n"),
vname, dev->dev_name);
Unknown status %d from read_volume_label()\n"), jcr->label_status);
break;
}
+bail_out:
free_block(block);
return_device_lock(dev, &hold);
}
if (found) {
jcr->device = device;
dev = device->dev;
- P(dev->mutex);
+ P(dev->mutex); /* Use P to avoid indefinite block */
switch (dev->dev_blocked) { /* device blocked? */
DEV_BLOCK *block;
case BST_WAITING_FOR_SYSOP:
if (dev->dev_blocked == BST_UNMOUNTED) {
Dmsg0(90, "Unmounted unblocking device\n");
read_label(jcr, dev);
- new_unlock_device(dev);
unblock_device(dev);
} else {
Dmsg0(90, "Unmounted waiting for mount attempt to wake thread\n");
if (found) {
jcr->device = device;
dev = device->dev;
- P(dev->mutex);
+ P(dev->mutex); /* Use P to avoid indefinite block */
if (!(dev->state & ST_OPENED)) {
Dmsg0(90, "Device already unmounted\n");
bnet_fsend(dir, _("3901 Device %s is already unmounted.\n"), dev_name(dev));
} else { /* device not being used */
Dmsg0(90, "Device not in use, unmounting\n");
- new_lock_device_state(dev, BST_UNMOUNTED);
block_device(dev, BST_UNMOUNTED);
if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
offline_dev(dev);
} else {
bnet_fsend(user, _("Device %s open but no Bacula volume is mounted.\n"), dev_name(dev));
}
- switch (dev->dev_blocked) {
- case BST_UNMOUNTED:
- bnet_fsend(user, _(" Deviced is blocked. User unmounted.\n"));
- break;
- case BST_UNMOUNTED_WAITING_FOR_SYSOP:
- bnet_fsend(user, _(" Deviced is blocked. User unmounted during wait for media/mount.\n"));
- break;
- case BST_WAITING_FOR_SYSOP:
- if (jcr->JobStatus == JS_WaitMount) {
- bnet_fsend(user, _(" Device is blocked waiting for mount.\n"));
- } else {
- bnet_fsend(user, _(" Device is blocked waiting for appendable media.\n"));
- }
- break;
- case BST_DOING_ACQUIRE:
- bnet_fsend(user, _(" Device is being initialized.\n"));
- break;
- case BST_WRITING_LABEL:
- bnet_fsend(user, _(" Device is blocked labeling a Volume.\n"));
- break;
- default:
- break;
- }
+ send_blocked_status(jcr, dev);
bpb = dev->VolCatInfo.VolCatBlocks;
if (bpb <= 0) {
bpb = 1;
} else {
bnet_fsend(user, _("Device %s is not open.\n"), dev_name(dev));
+ send_blocked_status(jcr, dev);
}
}
}
bnet_sig(user, BNET_EOD);
return 1;
}
+
+static void send_blocked_status(JCR *jcr, DEVICE *dev)
+{
+ BSOCK *user = jcr->dir_bsock;
+
+ switch (dev->dev_blocked) {
+ case BST_UNMOUNTED:
+ bnet_fsend(user, _(" Device is BLOCKED. User unmounted.\n"));
+ break;
+ case BST_UNMOUNTED_WAITING_FOR_SYSOP:
+ bnet_fsend(user, _(" Device is BLOCKED. User unmounted during wait for media/mount.\n"));
+ break;
+ case BST_WAITING_FOR_SYSOP:
+ if (jcr->JobStatus == JS_WaitMount) {
+ bnet_fsend(user, _(" Device is BLOCKED waiting for mount.\n"));
+ } else {
+ bnet_fsend(user, _(" Device is BLOCKED waiting for appendable media.\n"));
+ }
+ break;
+ case BST_DOING_ACQUIRE:
+ bnet_fsend(user, _(" Device is being initialized.\n"));
+ break;
+ case BST_WRITING_LABEL:
+ bnet_fsend(user, _(" Device is blocked labeling a Volume.\n"));
+ break;
+ default:
+ break;
+ }
+}