* 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;
*/
static int label_cmd(JCR *jcr)
{
- char *dname, *volname, *poolname, *mtype;
+ POOLMEM *dname, *volname, *poolname, *mtype;
BSOCK *dir = jcr->dir_bsock;
DEVRES *device;
DEVICE *dev;
int found = 0;
-
- dname = (char *) get_memory(dir->msglen+1);
- volname = (char *) get_memory(dir->msglen+1);
- poolname = (char *) get_memory(dir->msglen+1);
- mtype = (char *) get_memory(dir->msglen+1);
- if (sscanf(dir->msg, "label %s VolumeName=%s PoolName=%s MediaType=%s",
- dname, volname, poolname, mtype) == 4) {
+ int slot;
+
+ dname = get_memory(dir->msglen+1);
+ volname = get_memory(dir->msglen+1);
+ poolname = get_memory(dir->msglen+1);
+ mtype = get_memory(dir->msglen+1);
+ if (sscanf(dir->msg, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d",
+ dname, volname, poolname, mtype, &slot) == 5) {
unbash_spaces(dname);
unbash_spaces(volname);
unbash_spaces(poolname);
jcr->device = device;
dev = device->dev;
- 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);
} else {
*
* 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;
- brwsteal_t hold;
-#ifndef NEW_LOCK
- int blocked;
- pthread_t no_wait_id;
+ bsteal_lock_t hold;
- blocked = dev->dev_blocked; /* save any prev blocked state */
- dev->dev_blocked = BST_WRITING_LABEL;
- no_wait_id = dev->no_wait_id;
- dev->no_wait_id = pthread_self(); /* let us use the tape */
-#endif
+ steal_device_lock(dev, &hold, BST_WRITING_LABEL);
- new_steal_device_lock(dev, &hold, BST_WRITING_LABEL);
- V(dev->mutex);
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);
- P(dev->mutex);
- new_return_device_lock(dev, &hold);
-#ifndef NEW_LOCK
- dev->dev_blocked = blocked; /* reset blocked state */
- dev->no_wait_id = no_wait_id; /* reset blocking thread id */
-#endif
+ return_device_lock(dev, &hold);
}
int stat;
BSOCK *dir = jcr->dir_bsock;
DEV_BLOCK *block;
- brwsteal_t hold;
#ifndef NEW_LOCK
- int blocked;
- pthread_t no_wait_id;
+ bsteal_lock_t hold;
- blocked = dev->dev_blocked; /* save any prev blocked state */
- no_wait_id = dev->no_wait_id;
- dev->dev_blocked = BST_DOING_ACQUIRE;
- dev->no_wait_id = pthread_self(); /* let us use the tape */
+ steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
#endif
- new_steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
- V(dev->mutex); /* release lock */
jcr->VolumeName[0] = 0;
block = new_block(dev);
break;
}
free_block(block);
- P(dev->mutex);
- new_return_device_lock(dev, &hold);
#ifndef NEW_LOCK
- dev->dev_blocked = blocked; /* reset blocked state */
- dev->no_wait_id = no_wait_id; /* reset blocking thread id */
+ return_device_lock(dev, &hold);
#endif
return stat;
}
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;
+ }
+}