3 * Routines for handling the autochanger.
5 * Kern Sibbald, August MMII
10 Copyright (C) 2000-2005 Kern Sibbald
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);
35 static void lock_changer(DCR *dcr);
36 static void unlock_changer(DCR *dcr);
39 * Called here to do an autoload using the autochanger, if
40 * configured, and if a Slot has been defined for this Volume.
41 * On success this routine loads the indicated tape, but the
42 * label is not read, so it must be verified.
44 * Note if dir is not NULL, it is the console requesting the
45 * autoload for labeling, so we respond directly to the
48 * Returns: 1 on success
49 * 0 on failure (no changer available)
50 * -1 on error on autochanger
52 int autoload_device(DCR *dcr, int writing, BSOCK *dir)
55 DEVICE *dev = dcr->dev;
57 int drive = dev->device->drive_index;
58 int rtn_stat = -1; /* error status */
61 slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
63 * Handle autoloaders here. If we cannot autoload it, we
64 * will return FALSE to ask the sysop.
66 if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
68 return 0; /* For user, bail out right now */
70 if (dir_find_next_appendable_volume(dcr)) {
71 slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
76 Dmsg1(400, "Want changer slot=%d\n", slot);
78 changer = get_pool_memory(PM_FNAME);
79 if (slot > 0 && dcr->device->changer_name && dcr->device->changer_command) {
80 uint32_t timeout = dcr->device->max_changer_wait;
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 */
91 if (loaded != 0 && loaded != -1) { /* must unload drive */
92 Dmsg0(400, "Doing changer unload.\n");
94 _("3303 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
96 dcr->VolCatInfo.Slot = loaded; /* slot to be unloaded */
97 changer = edit_device_codes(dcr, changer, "unload");
98 status = run_program(changer, timeout, NULL);
101 be.set_errno(status);
102 Jmsg(jcr, M_FATAL, 0, _("3992 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
103 slot, drive, be.strerror());
107 Dmsg1(400, "unload status=%d\n", status);
110 * Load the desired cassette
112 Dmsg1(400, "Doing changer load slot %d\n", slot);
114 _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"),
116 dcr->VolCatInfo.Slot = slot; /* slot to be loaded */
117 changer = edit_device_codes(dcr, changer, "load");
118 status = run_program(changer, timeout, NULL);
120 Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
124 be.set_errno(status);
125 Jmsg(jcr, M_FATAL, 0, _("3992 Bad autochanger \"load slot %d, drive %d\": ERR=%s.\n"),
126 slot, drive, be.strerror());
130 Dmsg2(400, "load slot %d status=%d\n", slot, status);
132 status = 0; /* we got what we want */
134 Dmsg1(400, "After changer, status=%d\n", status);
135 if (status == 0) { /* did we succeed? */
136 rtn_stat = 1; /* tape loaded by changer */
139 rtn_stat = 0; /* no changer found */
141 free_pool_memory(changer);
145 free_pool_memory(changer);
151 static int get_autochanger_loaded_slot(DCR *dcr)
154 POOLMEM *changer, *results;
156 uint32_t timeout = dcr->device->max_changer_wait;
157 int drive = dcr->device->drive_index;
159 results = get_pool_memory(PM_MESSAGE);
160 changer = get_pool_memory(PM_FNAME);
164 /* Find out what is loaded, zero means device is unloaded */
165 Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
167 changer = edit_device_codes(dcr, changer, "loaded");
168 status = run_program(changer, timeout, results);
169 Dmsg3(50, "run_prog: %s stat=%d result=%s", changer, status, results);
171 loaded = atoi(results);
173 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
176 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
181 be.set_errno(status);
182 Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command: ERR=%s.\n"),
183 drive, be.strerror());
184 loaded = -1; /* force unload */
187 free_pool_memory(changer);
188 free_pool_memory(results);
192 static void lock_changer(DCR *dcr)
194 AUTOCHANGER *changer_res = dcr->device->changer_res;
196 Dmsg1(100, "Locking changer %s\n", changer_res->hdr.name);
197 P(changer_res->changer_mutex); /* Lock changer script */
201 static void unlock_changer(DCR *dcr)
203 AUTOCHANGER *changer_res = dcr->device->changer_res;
205 Dmsg1(100, "Unlocking changer %s\n", changer_res->hdr.name);
206 V(changer_res->changer_mutex); /* Unlock changer script */
212 * The Volume is not in the correct slot, so mark this
213 * Volume as not being in the Changer.
215 void mark_volume_not_inchanger(DCR *dcr)
218 DEVICE *dev = dcr->dev;
219 Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
220 " Setting InChanger to zero in catalog.\n"),
221 dcr->VolCatInfo.VolCatName, dcr->VolCatInfo.Slot);
222 dcr->VolCatInfo.InChanger = false;
223 dev->VolCatInfo.InChanger = false;
224 Dmsg0(400, "update vol info in mount\n");
225 dir_update_volume_info(dcr, true); /* set new status */
229 * List the Volumes that are in the autoloader possibly
230 * with their barcodes.
231 * We assume that it is always the Console that is calling us.
233 bool autochanger_cmd(DCR *dcr, BSOCK *dir, const char *cmd)
235 DEVICE *dev = dcr->dev;
237 uint32_t timeout = dcr->device->max_changer_wait;
241 int len = sizeof_pool_memory(dir->msg) - 1;
245 if (!dev_cap(dev, CAP_AUTOCHANGER) || !dcr->device->changer_name ||
246 !dcr->device->changer_command) {
247 bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
251 changer = get_pool_memory(PM_FNAME);
253 if (strcmp(cmd, "list") == 0) {
254 int drive = dev->device->drive_index;
255 /* Yes, to get a good listing, we unload any volumes */
256 offline_or_rewind_dev(dev);
257 /* We are going to load a new tape, so close the device */
258 force_close_dev(dev);
260 /* First unload any tape */
261 loaded = get_autochanger_loaded_slot(dcr);
264 _("3305 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
266 slot = dcr->VolCatInfo.Slot;
267 dcr->VolCatInfo.Slot = loaded;
268 changer = edit_device_codes(dcr, changer, "unload");
270 int stat = run_program(changer, timeout, NULL);
275 Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
276 slot, drive, be.strerror());
278 dcr->VolCatInfo.Slot = slot;
282 /* Now issue the command */
283 changer = edit_device_codes(dcr, changer, cmd);
284 bnet_fsend(dir, _("3306 Issuing autochanger \"%s\" command.\n"), cmd);
286 bpipe = open_bpipe(changer, timeout, "r");
289 bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
292 if (strcmp(cmd, "list") == 0) {
293 /* Get output from changer */
294 while (fgets(dir->msg, len, bpipe->rfd)) {
295 dir->msglen = strlen(dir->msg);
296 Dmsg1(100, "<stored: %s\n", dir->msg);
300 /* For slots command, read a single line */
301 bstrncpy(dir->msg, "slots=", len);
302 fgets(dir->msg+6, len-6, bpipe->rfd);
303 dir->msglen = strlen(dir->msg);
304 Dmsg1(100, "<stored: %s", dir->msg);
308 stat = close_bpipe(bpipe);
313 bnet_fsend(dir, "Autochanger error: ERR=%s\n", be.strerror());
315 bnet_sig(dir, BNET_EOD);
319 free_pool_memory(changer);
325 * Edit codes into ChangerCommand
327 * %a = archive device name
328 * %c = changer device name
329 * %d = changer drive index
338 * omsg = edited output message
339 * imsg = input string containing edit codes (%x)
340 * cmd = command string (load, unload, ...)
343 char *edit_device_codes(DCR *dcr, char *omsg, const char *cmd)
348 const char *imsg = dcr->device->changer_command;
351 Dmsg1(1800, "edit_device_codes: %s\n", imsg);
352 for (p=imsg; *p; p++) {
359 str = dcr->dev->archive_name();
362 str = NPRT(dcr->device->changer_name);
365 sprintf(add, "%d", dcr->dev->drive_index);
372 sprintf(add, "%d", dcr->VolCatInfo.Slot - 1);
376 sprintf(add, "%d", dcr->VolCatInfo.Slot);
379 case 'j': /* Job name */
383 str = NPRT(dcr->VolumeName);
386 str = NPRT(dcr->jcr->client_name);
401 Dmsg1(1900, "add_str %s\n", str);
402 pm_strcat(&omsg, (char *)str);
403 Dmsg1(1800, "omsg=%s\n", omsg);
405 Dmsg1(800, "omsg=%s\n", omsg);