3 * Routines for handling the autochanger.
5 * Kern Sibbald, August MMII
10 Copyright (C) 2000-2004 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29 #include "bacula.h" /* pull in global headers */
30 #include "stored.h" /* pull in Storage Deamon headers */
32 /* Forward referenced functions */
33 char *edit_device_codes(DCR *dcr, char *omsg, const char *cmd);
34 static int get_autochanger_loaded_slot(DCR *dcr);
38 * Called here to do an autoload using the autochanger, if
39 * configured, and if a Slot has been defined for this Volume.
40 * On success this routine loads the indicated tape, but the
41 * label is not read, so it must be verified.
43 * Note if dir is not NULL, it is the console requesting the
44 * autoload for labeling, so we respond directly to the
47 * Returns: 1 on success
48 * 0 on failure (no changer available)
49 * -1 on error on autochanger
51 int autoload_device(DCR *dcr, int writing, BSOCK *dir)
54 DEVICE *dev = dcr->dev;
56 int drive = dev->device->drive_index;
57 int rtn_stat = -1; /* error status */
59 slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
61 * Handle autoloaders here. If we cannot autoload it, we
62 * will return FALSE to ask the sysop.
64 if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
66 return 0; /* For user, bail out right now */
68 if (dir_find_next_appendable_volume(dcr)) {
69 slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
74 Dmsg1(400, "Want changer slot=%d\n", slot);
76 if (slot > 0 && dcr->device->changer_name && dcr->device->changer_command) {
77 uint32_t timeout = dcr->device->max_changer_wait;
81 changer = get_pool_memory(PM_FNAME);
83 loaded = get_autochanger_loaded_slot(dcr);
85 /* If tape we want is not loaded, load it. */
87 offline_or_rewind_dev(dev);
88 /* We are going to load a new tape, so close the device */
90 if (loaded != 0 && loaded != -1) { /* must unload drive */
91 Dmsg0(400, "Doing changer unload.\n");
93 _("3303 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
95 dcr->VolCatInfo.Slot = loaded; /* slot to be unloaded */
96 changer = edit_device_codes(dcr, changer, "unload");
97 status = run_program(changer, timeout, NULL);
100 be.set_errno(status);
101 Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
102 slot, drive, be.strerror());
105 Dmsg1(400, "unload status=%d\n", status);
108 * Load the desired cassette
110 Dmsg1(400, "Doing changer load slot %d\n", slot);
112 _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"),
114 dcr->VolCatInfo.Slot = slot; /* slot to be loaded */
115 changer = edit_device_codes(dcr, changer, "load");
116 status = run_program(changer, timeout, NULL);
118 Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
122 be.set_errno(status);
123 Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\": ERR=%s.\n"),
124 slot, drive, be.strerror());
126 Dmsg2(400, "load slot %d status=%d\n", slot, status);
128 status = 0; /* we got what we want */
130 free_pool_memory(changer);
131 Dmsg1(400, "After changer, status=%d\n", status);
132 if (status == 0) { /* did we succeed? */
133 rtn_stat = 1; /* tape loaded by changer */
136 rtn_stat = 0; /* no changer found */
141 static int get_autochanger_loaded_slot(DCR *dcr)
144 POOLMEM *changer, *results;
146 uint32_t timeout = dcr->device->max_changer_wait;
147 int drive = dcr->device->drive_index;
149 results = get_pool_memory(PM_MESSAGE);
150 changer = get_pool_memory(PM_FNAME);
152 /* Find out what is loaded, zero means device is unloaded */
153 Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
155 changer = edit_device_codes(dcr, changer, "loaded");
156 status = run_program(changer, timeout, results);
157 Dmsg3(50, "run_prog: %s stat=%d result=%s\n", changer, status, results);
159 loaded = atoi(results);
161 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
164 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
169 be.set_errno(status);
170 Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command: ERR=%s.\n"),
171 drive, be.strerror());
172 loaded = -1; /* force unload */
174 free_pool_memory(changer);
175 free_pool_memory(results);
180 * The Volume is not in the correct slot, so mark this
181 * Volume as not being in the Changer.
183 void invalid_slot_in_catalog(DCR *dcr)
186 DEVICE *dev = dcr->dev;
187 Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
188 " Setting slot to zero in catalog.\n"),
189 dcr->VolCatInfo.VolCatName, dcr->VolCatInfo.Slot);
190 dcr->VolCatInfo.InChanger = false;
191 dev->VolCatInfo.InChanger = false;
192 Dmsg0(400, "update vol info in mount\n");
193 dir_update_volume_info(dcr, true); /* set new status */
197 * List the Volumes that are in the autoloader possibly
198 * with their barcodes.
199 * We assume that it is always the Console that is calling us.
201 bool autochanger_list(DCR *dcr, BSOCK *dir)
203 DEVICE *dev = dcr->dev;
205 uint32_t timeout = dcr->device->max_changer_wait;
209 int len = sizeof_pool_memory(dir->msg) - 1;
211 if (!dev_cap(dev, CAP_AUTOCHANGER) || !dcr->device->changer_name ||
212 !dcr->device->changer_command) {
213 bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
217 changer = get_pool_memory(PM_FNAME);
218 offline_or_rewind_dev(dev);
219 /* We are going to load a new tape, so close the device */
220 force_close_dev(dev);
222 /* First unload any tape */
223 loaded = get_autochanger_loaded_slot(dcr);
225 bnet_fsend(dir, _("3305 Issuing autochanger \"unload slot %d\" command.\n"), loaded);
226 slot = dcr->VolCatInfo.Slot;
227 dcr->VolCatInfo.Slot = loaded;
228 changer = edit_device_codes(dcr, changer, "unload");
229 int stat = run_program(changer, timeout, NULL);
233 Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d\" command: ERR=%s.\n"),
234 loaded, be.strerror());
236 dcr->VolCatInfo.Slot = slot;
239 /* Now list slots occupied */
240 changer = edit_device_codes(dcr, changer, "list");
241 bnet_fsend(dir, _("3306 Issuing autochanger \"list\" command.\n"));
242 bpipe = open_bpipe(changer, timeout, "r");
244 bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
245 free_pool_memory(changer);
248 /* Get output from changer */
249 while (fgets(dir->msg, len, bpipe->rfd)) {
250 dir->msglen = strlen(dir->msg);
253 int stat = close_bpipe(bpipe);
257 bnet_fsend(dir, "Autochanger error: ERR=%s\n", be.strerror());
259 bnet_sig(dir, BNET_EOD);
261 free_pool_memory(changer);
267 * Edit codes into ChangerCommand
269 * %a = archive device name
270 * %c = changer device name
271 * %d = changer drive index
280 * omsg = edited output message
281 * imsg = input string containing edit codes (%x)
282 * cmd = command string (load, unload, ...)
285 char *edit_device_codes(DCR *dcr, char *omsg, const char *cmd)
290 const char *imsg = dcr->device->changer_command;
293 Dmsg1(800, "edit_device_codes: %s\n", imsg);
294 for (p=imsg; *p; p++) {
301 str = dev_name(dcr->dev);
304 str = NPRT(dcr->device->changer_name);
307 sprintf(add, "%d", dcr->dev->drive_index);
314 sprintf(add, "%d", dcr->VolCatInfo.Slot - 1);
318 sprintf(add, "%d", dcr->VolCatInfo.Slot);
321 case 'j': /* Job name */
325 str = NPRT(dcr->VolumeName);
328 str = NPRT(dcr->jcr->client_name);
343 Dmsg1(900, "add_str %s\n", str);
344 pm_strcat(&omsg, (char *)str);
345 Dmsg1(800, "omsg=%s\n", omsg);