X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=bacula%2Fsrc%2Fstored%2Fautochanger.c;h=f127e96fe20bbafb14c3b1d6d308e2b2641c992c;hb=7503a438c87931cf1748bd0fda3bb932e2af3346;hp=67cca5bad6ebbbe8f7e74ec4844b8d2d1348a4f0;hpb=82dde94cc7bdb5ca09db107bea131cbe031ae282;p=bacula%2Fbacula diff --git a/bacula/src/stored/autochanger.c b/bacula/src/stored/autochanger.c index 67cca5bad6..f127e96fe2 100644 --- a/bacula/src/stored/autochanger.c +++ b/bacula/src/stored/autochanger.c @@ -7,7 +7,7 @@ * 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 @@ -30,9 +30,10 @@ #include "stored.h" /* pull in Storage Deamon headers */ /* Forward referenced functions */ -char *edit_device_codes(JCR *jcr, char *omsg, const char *imsg, const char *cmd); -static int get_autochanger_loaded_slot(JCR *jcr); - +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 @@ -40,20 +41,23 @@ static int get_autochanger_loaded_slot(JCR *jcr); * 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 (no changer available) + * 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 drive = jcr->device->drive_index; + 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. @@ -62,66 +66,71 @@ int autoload_device(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir) 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(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 loaded, status; + 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); - loaded = get_autochanger_loaded_slot(jcr); - - Dmsg1(400, "loaded=%s\n", results); + loaded = get_autochanger_loaded_slot(dcr); /* If tape we want is not loaded, load it. */ - if (loaded != slot) { + if (loaded != slot) { offline_or_rewind_dev(dev); /* We are going to load a new tape, so close the device */ force_close_dev(dev); + lock_changer(dcr); if (loaded != 0 && loaded != -1) { /* must unload drive */ Dmsg0(400, "Doing changer unload.\n"); - Jmsg(jcr, M_INFO, 0, + Jmsg(jcr, M_INFO, 0, _("3303 Issuing autochanger \"unload slot %d, drive %d\" command.\n"), loaded, drive); - jcr->VolCatInfo.Slot = loaded; /* slot to be unloaded */ - changer = edit_device_codes(jcr, changer, - jcr->device->changer_command, "unload"); + dcr->VolCatInfo.Slot = loaded; /* slot to be unloaded */ + changer = edit_device_codes(dcr, changer, "unload"); status = run_program(changer, timeout, NULL); + 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(400, "Doing changer load slot %d\n", slot); - Jmsg(jcr, M_INFO, 0, - _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"), + Jmsg(jcr, M_INFO, 0, + _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"), slot, drive); - jcr->VolCatInfo.Slot = slot; /* slot to be loaded */ - changer = edit_device_codes(jcr, changer, - jcr->device->changer_command, "load"); + 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, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"), slot, drive); } else { - Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\", status=%d.\n"), - slot, drive, 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()); } + unlock_changer(dcr); Dmsg2(400, "load slot %d status=%d\n", slot, status); - } else { + } else { status = 0; /* we got what we want */ } free_pool_memory(changer); - free_pool_memory(results); Dmsg1(400, "After changer, status=%d\n", status); if (status == 0) { /* did we succeed? */ rtn_stat = 1; /* tape loaded by changer */ @@ -132,23 +141,25 @@ int autoload_device(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir) return rtn_stat; } -static int get_autochanger_loaded_slot(JCR *jcr) +static int get_autochanger_loaded_slot(DCR *dcr) { + JCR *jcr = dcr->jcr; POOLMEM *changer, *results; int status, loaded; - uint32_t timeout = jcr->device->max_changer_wait; - int drive = jcr->device->drive_index; + 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(jcr, changer, jcr->device->changer_command, - "loaded"); + changer = edit_device_codes(dcr, changer, "loaded"); status = run_program(changer, timeout, results); - Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results); + Dmsg3(50, "run_prog: %s stat=%d result=%s", changer, status, results); if (status == 0) { loaded = atoi(results); if (loaded > 0) { @@ -159,28 +170,52 @@ static int get_autochanger_loaded_slot(JCR *jcr) drive); } } else { - Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command, status=%d.\n"), - drive, status); + 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 + * The Volume is not in the correct slot, so mark this * Volume as not being in the Changer. */ -void invalid_slot_in_catalog(JCR *jcr, DEVICE *dev) +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.InChanger = false; +" Setting InChanger to zero in catalog.\n"), + dcr->VolCatInfo.VolCatName, dcr->VolCatInfo.Slot); + dcr->VolCatInfo.InChanger = false; dev->VolCatInfo.InChanger = false; - Dmsg0(100, "update vol info in mount\n"); - dir_update_volume_info(jcr, dev, 1); /* set new status */ + Dmsg0(400, "update vol info in mount\n"); + dir_update_volume_info(dcr, true); /* set new status */ } /* @@ -188,65 +223,103 @@ void invalid_slot_in_catalog(JCR *jcr, DEVICE *dev) * 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 */ - loaded = get_autochanger_loaded_slot(jcr); - if (loaded > 0) { - bnet_fsend(dir, _("3305 Issuing autochanger \"unload slot %d\" command.\n"), loaded); - slot = jcr->VolCatInfo.Slot; - jcr->VolCatInfo.Slot = loaded; - changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload"); - run_program(changer, timeout, NULL); - jcr->VolCatInfo.Slot = slot; + /* 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, "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, "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 + * %d = changer drive index * %f = Client's name * %j = Job name * %o = command @@ -257,17 +330,18 @@ bail_out: * * omsg = edited output message * imsg = input string containing edit codes (%x) - * cmd = command string (load, unload, ...) + * cmd = command string (load, unload, ...) * */ -char *edit_device_codes(JCR *jcr, char *omsg, const char *imsg, const char *cmd) +char *edit_device_codes(DCR *dcr, char *omsg, const char *cmd) { const char *p; const char *str; char add[20]; + const char *imsg = dcr->device->changer_command; *omsg = 0; - Dmsg1(400, "edit_device_codes: %s\n", imsg); + Dmsg1(1800, "edit_device_codes: %s\n", imsg); for (p=imsg; *p; p++) { if (*p == '%') { switch (*++p) { @@ -275,34 +349,34 @@ char *edit_device_codes(JCR *jcr, char *omsg, const char *imsg, const char *cmd) 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", jcr->device->dev->drive_index); + 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: @@ -317,9 +391,10 @@ char *edit_device_codes(JCR *jcr, char *omsg, const char *imsg, const char *cmd) add[1] = 0; str = add; } - Dmsg1(400, "add_str %s\n", str); + Dmsg1(1900, "add_str %s\n", str); pm_strcat(&omsg, (char *)str); - Dmsg1(400, "omsg=%s\n", omsg); + Dmsg1(1800, "omsg=%s\n", omsg); } + Dmsg1(800, "omsg=%s\n", omsg); return omsg; }