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;
55 int slot = dcr->VolCatInfo.Slot;
56 int drive = jcr->device->drive_index;
57 int rtn_stat = -1; /* error status */
60 * Handle autoloaders here. If we cannot autoload it, we
61 * will return FALSE to ask the sysop.
63 if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
65 return 0; /* For user, bail out right now */
67 if (dir_find_next_appendable_volume(dcr)) {
68 slot = dcr->VolCatInfo.Slot;
73 Dmsg1(400, "Want changer slot=%d\n", slot);
75 if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
76 uint32_t timeout = jcr->device->max_changer_wait;
80 changer = get_pool_memory(PM_FNAME);
82 loaded = get_autochanger_loaded_slot(jcr);
84 /* If tape we want is not loaded, load it. */
86 offline_or_rewind_dev(dev);
87 /* We are going to load a new tape, so close the device */
89 if (loaded != 0 && loaded != -1) { /* must unload drive */
90 Dmsg0(400, "Doing changer unload.\n");
92 _("3303 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
94 dcr->VolCatInfo.Slot = loaded; /* slot to be unloaded */
95 changer = edit_device_codes(jcr, changer,
96 jcr->device->changer_command, "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(jcr, changer,
116 jcr->device->changer_command, "load");
117 status = run_program(changer, timeout, NULL);
119 Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
123 be.set_errno(status);
124 Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\": ERR=%s.\n"),
125 slot, drive, be.strerror());
127 Dmsg2(400, "load slot %d status=%d\n", slot, status);
129 status = 0; /* we got what we want */
131 free_pool_memory(changer);
132 Dmsg1(400, "After changer, status=%d\n", status);
133 if (status == 0) { /* did we succeed? */
134 rtn_stat = 1; /* tape loaded by changer */
137 rtn_stat = 0; /* no changer found */
142 static int get_autochanger_loaded_slot(JCR *jcr)
144 POOLMEM *changer, *results;
146 uint32_t timeout = jcr->device->max_changer_wait;
147 int drive = jcr->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(jcr, changer, jcr->device->changer_command,
157 status = run_program(changer, timeout, results);
158 Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
160 loaded = atoi(results);
162 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
165 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
170 be.set_errno(status);
171 Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command: ERR=%s.\n"),
172 drive, be.strerror());
173 loaded = -1; /* force unload */
175 free_pool_memory(changer);
176 free_pool_memory(results);
181 * The Volume is not in the correct slot, so mark this
182 * Volume as not being in the Changer.
184 void invalid_slot_in_catalog(DCR *dcr)
187 DEVICE *dev = dcr->dev;
188 Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
189 " Setting slot to zero in catalog.\n"),
190 dcr->VolCatInfo.VolCatName, dcr->VolCatInfo.Slot);
191 dcr->VolCatInfo.InChanger = false;
192 dev->VolCatInfo.InChanger = false;
193 Dmsg0(100, "update vol info in mount\n");
194 dir_update_volume_info(dcr, true); /* set new status */
198 * List the Volumes that are in the autoloader possibly
199 * with their barcodes.
200 * We assume that it is always the Console that is calling us.
202 bool autochanger_list(DCR *dcr, BSOCK *dir)
204 DEVICE *dev = dcr->dev;
206 uint32_t timeout = jcr->device->max_changer_wait;
210 int len = sizeof_pool_memory(dir->msg) - 1;
212 if (!dev_cap(dev, CAP_AUTOCHANGER) || !jcr->device->changer_name ||
213 !jcr->device->changer_command) {
214 bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
218 changer = get_pool_memory(PM_FNAME);
219 offline_or_rewind_dev(dev);
220 /* We are going to load a new tape, so close the device */
221 force_close_dev(dev);
223 /* First unload any tape */
224 loaded = get_autochanger_loaded_slot(jcr);
226 bnet_fsend(dir, _("3305 Issuing autochanger \"unload slot %d\" command.\n"), loaded);
227 slot = dcr->VolCatInfo.Slot;
228 dcr->VolCatInfo.Slot = loaded;
229 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload");
230 int stat = run_program(changer, timeout, NULL);
234 Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d\" command: ERR=%s.\n"),
235 loaded, be.strerror());
237 dcr->VolCatInfo.Slot = slot;
240 /* Now list slots occupied */
241 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "list");
242 bnet_fsend(dir, _("3306 Issuing autochanger \"list\" command.\n"));
243 bpipe = open_bpipe(changer, timeout, "r");
245 bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
246 free_pool_memory(changer);
249 /* Get output from changer */
250 while (fgets(dir->msg, len, bpipe->rfd)) {
251 dir->msglen = strlen(dir->msg);
254 int stat = close_bpipe(bpipe);
258 bnet_fsend(dir, "Autochanger error: ERR=%s\n", be.strerror());
260 bnet_sig(dir, BNET_EOD);
262 free_pool_memory(changer);
268 * Edit codes into ChangerCommand
270 * %a = archive device name
271 * %c = changer device name
272 * %d = changer drive index
281 * omsg = edited output message
282 * imsg = input string containing edit codes (%x)
283 * cmd = command string (load, unload, ...)
286 char *edit_device_codes(JCR *jcr, char *omsg, const char *imsg, const char *cmd)
293 Dmsg1(400, "edit_device_codes: %s\n", imsg);
294 for (p=imsg; *p; p++) {
301 str = dev_name(jcr->device->dev);
304 str = NPRT(jcr->device->changer_name);
307 sprintf(add, "%d", jcr->device->dev->drive_index);
314 sprintf(add, "%d", jcr->dcr->VolCatInfo.Slot - 1);
318 sprintf(add, "%d", jcr->dcr->VolCatInfo.Slot);
321 case 'j': /* Job name */
325 str = NPRT(jcr->dcr->VolumeName);
328 str = NPRT(jcr->client_name);
343 Dmsg1(400, "add_str %s\n", str);
344 pm_strcat(&omsg, (char *)str);
345 Dmsg1(400, "omsg=%s\n", omsg);