* Version $Id$
*/
/*
- Copyright (C) 2000-2003 Kern Sibbald and John Walker
+ Copyright (C) 2000-2005 Kern Sibbald
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
#include "stored.h" /* pull in Storage Deamon headers */
/* Forward referenced functions */
-static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd);
-
+char *edit_device_codes(DCR *dcr, char *omsg, const char *cmd);
+static int get_autochanger_loaded_slot(DCR *dcr);
+static void lock_changer(DCR *dcr);
+static void unlock_changer(DCR *dcr);
/*
* Called here to do an autoload using the autochanger, if
* On success this routine loads the indicated tape, but the
* label is not read, so it must be verified.
*
- * Note if dir is not NULL, it is the console requesting the
+ * Note if dir is not NULL, it is the console requesting the
* autoload for labeling, so we respond directly to the
* dir bsock.
*
* Returns: 1 on success
- * 0 on failure
+ * 0 on failure (no changer available)
+ * -1 on error on autochanger
*/
-int autoload_device(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir)
+int autoload_device(DCR *dcr, int writing, BSOCK *dir)
{
- int slot = jcr->VolCatInfo.Slot;
- int rtn_stat = 0;
-
+ JCR *jcr = dcr->jcr;
+ DEVICE *dev = dcr->dev;
+ int slot;
+ int drive = dev->device->drive_index;
+ int rtn_stat = -1; /* error status */
+
+ slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
/*
* Handle autoloaders here. If we cannot autoload it, we
* will return FALSE to ask the sysop.
if (dir) {
return 0; /* For user, bail out right now */
}
- if (dir_find_next_appendable_volume(jcr)) {
- slot = jcr->VolCatInfo.Slot;
+ if (dir_find_next_appendable_volume(dcr)) {
+ slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
+ } else {
+ slot = 0;
}
}
- Dmsg1(100, "Want changer slot=%d\n", slot);
+ Dmsg1(400, "Want changer slot=%d\n", slot);
- if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
- uint32_t timeout = jcr->device->max_changer_wait;
- POOLMEM *changer, *results;
- int status, loaded;
+ if (slot > 0 && dcr->device->changer_name && dcr->device->changer_command) {
+ uint32_t timeout = dcr->device->max_changer_wait;
+ POOLMEM *changer;
+ int loaded, status;
- results = get_pool_memory(PM_MESSAGE);
changer = get_pool_memory(PM_FNAME);
- /* Find out what is loaded, zero means device is unloaded */
- Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded\" command.\n"));
- changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
- "loaded");
- status = run_program(changer, timeout, results);
- Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
- if (status == 0) {
- loaded = atoi(results);
- } else {
- Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"load slot\" status=%d.\n"), status);
- loaded = -1; /* force unload */
- }
- Dmsg1(100, "loaded=%s\n", results);
+ loaded = get_autochanger_loaded_slot(dcr);
- /* If bad status or tape we want is not loaded, load it. */
- if (status != 0 || loaded != slot) {
+ /* If tape we want is not loaded, load it. */
+ if (loaded != slot) {
offline_or_rewind_dev(dev);
/* We are going to load a new tape, so close the device */
force_close_dev(dev);
- if (loaded != 0) { /* must unload drive */
- Dmsg0(100, "Doing changer unload.\n");
- Jmsg(jcr, M_INFO, 0, _("3302 Issuing autochanger \"unload\" command.\n"));
- changer = edit_device_codes(jcr, changer,
- jcr->device->changer_command, "unload");
+ lock_changer(dcr);
+ if (loaded != 0 && loaded != -1) { /* must unload drive */
+ Dmsg0(400, "Doing changer unload.\n");
+ Jmsg(jcr, M_INFO, 0,
+ _("3303 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
+ loaded, drive);
+ dcr->VolCatInfo.Slot = loaded; /* slot to be unloaded */
+ changer = edit_device_codes(dcr, changer, "unload");
status = run_program(changer, timeout, NULL);
- Dmsg1(100, "unload status=%d\n", status);
+ if (status != 0) {
+ berrno be;
+ be.set_errno(status);
+ Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
+ slot, drive, be.strerror());
+ }
+
+ Dmsg1(400, "unload status=%d\n", status);
}
/*
- * Load the desired cassette
+ * Load the desired cassette
*/
- Dmsg1(100, "Doing changer load slot %d\n", slot);
- Jmsg(jcr, M_INFO, 0, _("3303 Issuing autochanger \"load slot %d\" command.\n"),
- slot);
- changer = edit_device_codes(jcr, changer,
- jcr->device->changer_command, "load");
+ Dmsg1(400, "Doing changer load slot %d\n", slot);
+ Jmsg(jcr, M_INFO, 0,
+ _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"),
+ slot, drive);
+ dcr->VolCatInfo.Slot = slot; /* slot to be loaded */
+ changer = edit_device_codes(dcr, changer, "load");
status = run_program(changer, timeout, NULL);
if (status == 0) {
- Jmsg(jcr, M_INFO, 0, _("3304 Autochanger \"load slot %d\" status is OK.\n"),
- slot);
+ Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
+ slot, drive);
} else {
- Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot\" status=%d.\n"),
- status);
+ berrno be;
+ be.set_errno(status);
+ Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\": ERR=%s.\n"),
+ slot, drive, be.strerror());
}
- Dmsg2(100, "load slot %d status=%d\n", slot, status);
+ unlock_changer(dcr);
+ Dmsg2(400, "load slot %d status=%d\n", slot, status);
+ } else {
+ status = 0; /* we got what we want */
}
free_pool_memory(changer);
- free_pool_memory(results);
- Dmsg1(100, "After changer, status=%d\n", status);
- if (status == 0) { /* did we succeed? */
- rtn_stat = 1; /* tape loaded by changer */
+ Dmsg1(400, "After changer, status=%d\n", status);
+ if (status == 0) { /* did we succeed? */
+ rtn_stat = 1; /* tape loaded by changer */
}
+ } else {
+ rtn_stat = 0; /* no changer found */
}
return rtn_stat;
}
-void invalidate_slot_in_catalog(JCR *jcr)
+static int get_autochanger_loaded_slot(DCR *dcr)
{
+ JCR *jcr = dcr->jcr;
+ POOLMEM *changer, *results;
+ int status, loaded;
+ uint32_t timeout = dcr->device->max_changer_wait;
+ int drive = dcr->device->drive_index;
+
+ results = get_pool_memory(PM_MESSAGE);
+ changer = get_pool_memory(PM_FNAME);
+
+ lock_changer(dcr);
+
+ /* Find out what is loaded, zero means device is unloaded */
+ Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
+ drive);
+ changer = edit_device_codes(dcr, changer, "loaded");
+ status = run_program(changer, timeout, results);
+ Dmsg3(50, "run_prog: %s stat=%d result=%s", changer, status, results);
+ if (status == 0) {
+ loaded = atoi(results);
+ if (loaded > 0) {
+ Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
+ drive, loaded);
+ } else {
+ Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
+ drive);
+ }
+ } else {
+ berrno be;
+ be.set_errno(status);
+ Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command: ERR=%s.\n"),
+ drive, be.strerror());
+ loaded = -1; /* force unload */
+ }
+ unlock_changer(dcr);
+ free_pool_memory(changer);
+ free_pool_memory(results);
+ return loaded;
+}
+
+static void lock_changer(DCR *dcr)
+{
+ AUTOCHANGER *changer_res = dcr->device->changer_res;
+ if (changer_res) {
+ Dmsg1(100, "Locking changer %s\n", changer_res->hdr.name);
+ P(changer_res->changer_mutex); /* Lock changer script */
+ }
+}
+
+static void unlock_changer(DCR *dcr)
+{
+ AUTOCHANGER *changer_res = dcr->device->changer_res;
+ if (changer_res) {
+ Dmsg1(100, "Unlocking changer %s\n", changer_res->hdr.name);
+ V(changer_res->changer_mutex); /* Unlock changer script */
+ }
+}
+
+
+/*
+ * The Volume is not in the correct slot, so mark this
+ * Volume as not being in the Changer.
+ */
+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 slot to zero in catalog.\n"),
- jcr->VolCatInfo.VolCatName, jcr->VolCatInfo.Slot);
- jcr->VolCatInfo.Slot = 0; /* invalidate slot */
- Dmsg0(200, "update vol info in mount\n");
- dir_update_volume_info(jcr, &jcr->VolCatInfo, 1); /* set slot */
+" Setting InChanger to zero in catalog.\n"),
+ dcr->VolCatInfo.VolCatName, dcr->VolCatInfo.Slot);
+ 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 */
}
/*
* with their barcodes.
* We assume that it is always the Console that is calling us.
*/
-int autochanger_list(JCR *jcr, DEVICE *dev, BSOCK *dir)
+bool autochanger_cmd(DCR *dcr, BSOCK *dir, const char *cmd)
{
- uint32_t timeout = jcr->device->max_changer_wait;
+ DEVICE *dev = dcr->dev;
+ JCR *jcr = dcr->jcr;
+ uint32_t timeout = dcr->device->max_changer_wait;
POOLMEM *changer;
BPIPE *bpipe;
+ int slot, loaded;
int len = sizeof_pool_memory(dir->msg) - 1;
+ bool ok = false;
+ int stat;
- if (!dev_cap(dev, CAP_AUTOCHANGER) || !jcr->device->changer_name ||
- !jcr->device->changer_command) {
+ if (!dev_cap(dev, CAP_AUTOCHANGER) || !dcr->device->changer_name ||
+ !dcr->device->changer_command) {
bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
- return 0;
+ return false;
}
changer = get_pool_memory(PM_FNAME);
- offline_or_rewind_dev(dev);
- /* We are going to load a new tape, so close the device */
- force_close_dev(dev);
+ /* List command? */
+ if (strcmp(cmd, "list") == 0) {
+ int drive = dev->device->drive_index;
+ /* Yes, to get a good listing, we unload any volumes */
+ offline_or_rewind_dev(dev);
+ /* We are going to load a new tape, so close the device */
+ force_close_dev(dev);
- /* First unload any tape */
- bnet_fsend(dir, _("3305 Issuing autochanger \"unload\" command.\n"));
- changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload");
- run_program(changer, timeout, NULL);
+ /* First unload any tape */
+ loaded = get_autochanger_loaded_slot(dcr);
+ if (loaded > 0) {
+ bnet_fsend(dir,
+ _("3305 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
+ loaded, drive);
+ slot = dcr->VolCatInfo.Slot;
+ dcr->VolCatInfo.Slot = loaded;
+ changer = edit_device_codes(dcr, changer, "unload");
+ lock_changer(dcr);
+ int stat = run_program(changer, timeout, NULL);
+ unlock_changer(dcr);
+ if (stat != 0) {
+ berrno be;
+ be.set_errno(stat);
+ Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
+ slot, drive, be.strerror());
+ }
+ dcr->VolCatInfo.Slot = slot;
+ }
+ }
- /* Now list slots occupied */
- changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "list");
- bnet_fsend(dir, _("3306 Issuing autochanger \"list\" command.\n"));
+ /* Now issue the command */
+ changer = edit_device_codes(dcr, changer, cmd);
+ bnet_fsend(dir, _("3306 Issuing autochanger \"%s\" command.\n"), cmd);
+ lock_changer(dcr);
bpipe = open_bpipe(changer, timeout, "r");
if (!bpipe) {
+ unlock_changer(dcr);
bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
goto bail_out;
}
- /* Get output from changer */
- while (fgets(dir->msg, len, bpipe->rfd)) {
+ if (strcmp(cmd, "list") == 0) {
+ /* Get output from changer */
+ while (fgets(dir->msg, len, bpipe->rfd)) {
+ dir->msglen = strlen(dir->msg);
+ Dmsg1(100, "<stored: %s\n", dir->msg);
+ bnet_send(dir);
+ }
+ } else {
+ /* For slots command, read a single line */
+ bstrncpy(dir->msg, "slots=", len);
+ fgets(dir->msg+6, len-6, bpipe->rfd);
dir->msglen = strlen(dir->msg);
+ Dmsg1(100, "<stored: %s", dir->msg);
bnet_send(dir);
}
+
+ stat = close_bpipe(bpipe);
+ unlock_changer(dcr);
+ if (stat != 0) {
+ berrno be;
+ be.set_errno(stat);
+ bnet_fsend(dir, "Autochanger error: ERR=%s\n", be.strerror());
+ }
bnet_sig(dir, BNET_EOD);
- close_bpipe(bpipe);
+ ok = true;
bail_out:
free_pool_memory(changer);
- return 1;
+ return true;
}
-
/*
* Edit codes into ChangerCommand
* %% = %
* %a = archive device name
* %c = changer device name
+ * %d = changer drive index
* %f = Client's name
* %j = Job name
* %o = command
*
* omsg = edited output message
* imsg = input string containing edit codes (%x)
- * cmd = command string (load, unload, ...)
+ * cmd = command string (load, unload, ...)
*
*/
-static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd)
+char *edit_device_codes(DCR *dcr, char *omsg, const char *cmd)
{
- char *p;
+ const char *p;
const char *str;
char add[20];
+ const char *imsg = dcr->device->changer_command;
*omsg = 0;
- Dmsg1(200, "edit_device_codes: %s\n", imsg);
+ Dmsg1(1800, "edit_device_codes: %s\n", imsg);
for (p=imsg; *p; p++) {
if (*p == '%') {
switch (*++p) {
str = "%";
break;
case 'a':
- str = dev_name(jcr->device->dev);
+ str = dcr->dev->archive_name();
break;
case 'c':
- str = NPRT(jcr->device->changer_name);
+ str = NPRT(dcr->device->changer_name);
+ break;
+ case 'd':
+ sprintf(add, "%d", dcr->dev->drive_index);
+ str = add;
break;
case 'o':
str = NPRT(cmd);
break;
case 's':
- sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
+ sprintf(add, "%d", dcr->VolCatInfo.Slot - 1);
str = add;
break;
case 'S':
- sprintf(add, "%d", jcr->VolCatInfo.Slot);
+ sprintf(add, "%d", dcr->VolCatInfo.Slot);
str = add;
break;
case 'j': /* Job name */
- str = jcr->Job;
+ str = dcr->jcr->Job;
break;
case 'v':
- str = NPRT(jcr->VolumeName);
+ str = NPRT(dcr->VolumeName);
break;
case 'f':
- str = NPRT(jcr->client_name);
+ str = NPRT(dcr->jcr->client_name);
break;
default:
add[1] = 0;
str = add;
}
- Dmsg1(200, "add_str %s\n", str);
+ Dmsg1(1900, "add_str %s\n", str);
pm_strcat(&omsg, (char *)str);
- Dmsg1(200, "omsg=%s\n", omsg);
+ Dmsg1(1800, "omsg=%s\n", omsg);
}
+ Dmsg1(800, "omsg=%s\n", omsg);
return omsg;
}