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(JCR *jcr, char *omsg, const char *imsg, const char *cmd);
34 static int get_autochanger_loaded_slot(JCR *jcr);
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 = jcr->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 && jcr->device->changer_name && jcr->device->changer_command) {
77 uint32_t timeout = jcr->device->max_changer_wait;
81 changer = get_pool_memory(PM_FNAME);
83 loaded = get_autochanger_loaded_slot(jcr);
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(jcr, changer,
97 jcr->device->changer_command, "unload");
98 status = run_program(changer, timeout, NULL);
101 be.set_errno(status);
102 Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
103 slot, drive, be.strerror());
106 Dmsg1(400, "unload status=%d\n", status);
109 * Load the desired cassette
111 Dmsg1(400, "Doing changer load slot %d\n", slot);
113 _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"),
115 dcr->VolCatInfo.Slot = slot; /* slot to be loaded */
116 changer = edit_device_codes(jcr, changer,
117 jcr->device->changer_command, "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_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\": ERR=%s.\n"),
126 slot, drive, be.strerror());
128 Dmsg2(400, "load slot %d status=%d\n", slot, status);
130 status = 0; /* we got what we want */
132 free_pool_memory(changer);
133 Dmsg1(400, "After changer, status=%d\n", status);
134 if (status == 0) { /* did we succeed? */
135 rtn_stat = 1; /* tape loaded by changer */
138 rtn_stat = 0; /* no changer found */
143 static int get_autochanger_loaded_slot(JCR *jcr)
145 POOLMEM *changer, *results;
147 uint32_t timeout = jcr->device->max_changer_wait;
148 int drive = jcr->device->drive_index;
150 results = get_pool_memory(PM_MESSAGE);
151 changer = get_pool_memory(PM_FNAME);
153 /* Find out what is loaded, zero means device is unloaded */
154 Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
156 changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
158 status = run_program(changer, timeout, results);
159 Dmsg3(50, "run_prog: %s stat=%d result=%s\n", changer, status, results);
161 loaded = atoi(results);
163 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
166 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
171 be.set_errno(status);
172 Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command: ERR=%s.\n"),
173 drive, be.strerror());
174 loaded = -1; /* force unload */
176 free_pool_memory(changer);
177 free_pool_memory(results);
182 * The Volume is not in the correct slot, so mark this
183 * Volume as not being in the Changer.
185 void invalid_slot_in_catalog(DCR *dcr)
188 DEVICE *dev = dcr->dev;
189 Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
190 " Setting slot to zero in catalog.\n"),
191 dcr->VolCatInfo.VolCatName, dcr->VolCatInfo.Slot);
192 dcr->VolCatInfo.InChanger = false;
193 dev->VolCatInfo.InChanger = false;
194 Dmsg0(400, "update vol info in mount\n");
195 dir_update_volume_info(dcr, true); /* set new status */
199 * List the Volumes that are in the autoloader possibly
200 * with their barcodes.
201 * We assume that it is always the Console that is calling us.
203 bool autochanger_list(DCR *dcr, BSOCK *dir)
205 DEVICE *dev = dcr->dev;
207 uint32_t timeout = jcr->device->max_changer_wait;
211 int len = sizeof_pool_memory(dir->msg) - 1;
213 if (!dev_cap(dev, CAP_AUTOCHANGER) || !jcr->device->changer_name ||
214 !jcr->device->changer_command) {
215 bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
219 changer = get_pool_memory(PM_FNAME);
220 offline_or_rewind_dev(dev);
221 /* We are going to load a new tape, so close the device */
222 force_close_dev(dev);
224 /* First unload any tape */
225 loaded = get_autochanger_loaded_slot(jcr);
227 bnet_fsend(dir, _("3305 Issuing autochanger \"unload slot %d\" command.\n"), loaded);
228 slot = dcr->VolCatInfo.Slot;
229 dcr->VolCatInfo.Slot = loaded;
230 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload");
231 int stat = run_program(changer, timeout, NULL);
235 Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d\" command: ERR=%s.\n"),
236 loaded, be.strerror());
238 dcr->VolCatInfo.Slot = slot;
241 /* Now list slots occupied */
242 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "list");
243 bnet_fsend(dir, _("3306 Issuing autochanger \"list\" command.\n"));
244 bpipe = open_bpipe(changer, timeout, "r");
246 bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
247 free_pool_memory(changer);
250 /* Get output from changer */
251 while (fgets(dir->msg, len, bpipe->rfd)) {
252 dir->msglen = strlen(dir->msg);
255 int stat = close_bpipe(bpipe);
259 bnet_fsend(dir, "Autochanger error: ERR=%s\n", be.strerror());
261 bnet_sig(dir, BNET_EOD);
263 free_pool_memory(changer);
269 * Edit codes into ChangerCommand
271 * %a = archive device name
272 * %c = changer device name
273 * %d = changer drive index
282 * omsg = edited output message
283 * imsg = input string containing edit codes (%x)
284 * cmd = command string (load, unload, ...)
287 char *edit_device_codes(JCR *jcr, char *omsg, const char *imsg, const char *cmd)
294 Dmsg1(800, "edit_device_codes: %s\n", imsg);
295 for (p=imsg; *p; p++) {
302 str = dev_name(jcr->device->dev);
305 str = NPRT(jcr->device->changer_name);
308 sprintf(add, "%d", jcr->device->dev->drive_index);
315 sprintf(add, "%d", jcr->dcr->VolCatInfo.Slot - 1);
319 sprintf(add, "%d", jcr->dcr->VolCatInfo.Slot);
322 case 'j': /* Job name */
326 str = NPRT(jcr->dcr->VolumeName);
329 str = NPRT(jcr->client_name);
344 Dmsg1(900, "add_str %s\n", str);
345 pm_strcat(&omsg, (char *)str);
346 Dmsg1(800, "omsg=%s\n", omsg);