3 * Routines for handling the autochanger.
5 * Kern Sibbald, August MMII
10 Copyright (C) 2000-2003 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, char *imsg, 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(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir)
53 int slot = jcr->VolCatInfo.Slot;
54 int drive = jcr->device->drive_index;
55 int rtn_stat = -1; /* error status */
58 * Handle autoloaders here. If we cannot autoload it, we
59 * will return FALSE to ask the sysop.
61 if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
63 return 0; /* For user, bail out right now */
65 if (dir_find_next_appendable_volume(jcr)) {
66 slot = jcr->VolCatInfo.Slot;
69 Dmsg1(400, "Want changer slot=%d\n", slot);
71 if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
72 uint32_t timeout = jcr->device->max_changer_wait;
73 POOLMEM *changer, *results;
76 results = get_pool_memory(PM_MESSAGE);
77 changer = get_pool_memory(PM_FNAME);
79 loaded = get_autochanger_loaded_slot(jcr);
81 Dmsg1(400, "loaded=%s\n", results);
83 /* If tape we want is not loaded, load it. */
85 offline_or_rewind_dev(dev);
86 /* We are going to load a new tape, so close the device */
88 if (loaded != 0) { /* must unload drive */
89 Dmsg0(400, "Doing changer unload.\n");
91 _("3303 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
93 jcr->VolCatInfo.Slot = loaded; /* slot to be unloaded */
94 changer = edit_device_codes(jcr, changer,
95 jcr->device->changer_command, "unload");
96 status = run_program(changer, timeout, NULL);
97 Dmsg1(400, "unload status=%d\n", status);
100 * Load the desired cassette
102 Dmsg1(400, "Doing changer load slot %d\n", slot);
104 _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"),
106 jcr->VolCatInfo.Slot = slot; /* slot to be loaded */
107 changer = edit_device_codes(jcr, changer,
108 jcr->device->changer_command, "load");
109 status = run_program(changer, timeout, NULL);
111 Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
114 Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\", status=%d.\n"),
115 slot, drive, status);
117 Dmsg2(400, "load slot %d status=%d\n", slot, status);
119 status = 0; /* we got what we want */
121 free_pool_memory(changer);
122 free_pool_memory(results);
123 Dmsg1(400, "After changer, status=%d\n", status);
124 if (status == 0) { /* did we succeed? */
125 rtn_stat = 1; /* tape loaded by changer */
128 rtn_stat = 0; /* no changer found */
133 static int get_autochanger_loaded_slot(JCR *jcr)
135 POOLMEM *changer, *results;
137 uint32_t timeout = jcr->device->max_changer_wait;
138 int drive = jcr->device->drive_index;
140 results = get_pool_memory(PM_MESSAGE);
141 changer = get_pool_memory(PM_FNAME);
143 /* Find out what is loaded, zero means device is unloaded */
144 Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
146 changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
148 status = run_program(changer, timeout, results);
149 Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
151 loaded = atoi(results);
153 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
156 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
160 Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command, status=%d.\n"),
162 loaded = -1; /* force unload */
164 free_pool_memory(changer);
165 free_pool_memory(results);
170 * The Volume is not in the correct slot, so mark this
171 * Volume as not being in the Changer.
173 void invalid_slot_in_catalog(JCR *jcr, DEVICE *dev)
175 Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
176 " Setting slot to zero in catalog.\n"),
177 jcr->VolCatInfo.VolCatName, jcr->VolCatInfo.Slot);
178 jcr->VolCatInfo.InChanger = false;
179 dev->VolCatInfo.InChanger = false;
180 Dmsg0(100, "update vol info in mount\n");
181 dir_update_volume_info(jcr, dev, 1); /* set new status */
185 * List the Volumes that are in the autoloader possibly
186 * with their barcodes.
187 * We assume that it is always the Console that is calling us.
189 int autochanger_list(JCR *jcr, DEVICE *dev, BSOCK *dir)
191 uint32_t timeout = jcr->device->max_changer_wait;
195 int len = sizeof_pool_memory(dir->msg) - 1;
197 if (!dev_cap(dev, CAP_AUTOCHANGER) || !jcr->device->changer_name ||
198 !jcr->device->changer_command) {
199 bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
203 changer = get_pool_memory(PM_FNAME);
204 offline_or_rewind_dev(dev);
205 /* We are going to load a new tape, so close the device */
206 force_close_dev(dev);
208 /* First unload any tape */
209 loaded = get_autochanger_loaded_slot(jcr);
211 bnet_fsend(dir, _("3305 Issuing autochanger \"unload slot %d\" command.\n"), loaded);
212 slot = jcr->VolCatInfo.Slot;
213 jcr->VolCatInfo.Slot = loaded;
214 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload");
215 run_program(changer, timeout, NULL);
216 jcr->VolCatInfo.Slot = slot;
219 /* Now list slots occupied */
220 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "list");
221 bnet_fsend(dir, _("3306 Issuing autochanger \"list\" command.\n"));
222 bpipe = open_bpipe(changer, timeout, "r");
224 bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
227 /* Get output from changer */
228 while (fgets(dir->msg, len, bpipe->rfd)) {
229 dir->msglen = strlen(dir->msg);
232 bnet_sig(dir, BNET_EOD);
236 free_pool_memory(changer);
243 * Edit codes into ChangerCommand
245 * %a = archive device name
246 * %c = changer device name
247 * %d = changer drive index
256 * omsg = edited output message
257 * imsg = input string containing edit codes (%x)
258 * cmd = command string (load, unload, ...)
261 char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd)
268 Dmsg1(400, "edit_device_codes: %s\n", imsg);
269 for (p=imsg; *p; p++) {
276 str = dev_name(jcr->device->dev);
279 str = NPRT(jcr->device->changer_name);
282 sprintf(add, "%d", jcr->device->dev->drive_index);
289 sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
293 sprintf(add, "%d", jcr->VolCatInfo.Slot);
296 case 'j': /* Job name */
300 str = NPRT(jcr->VolumeName);
303 str = NPRT(jcr->client_name);
318 Dmsg1(400, "add_str %s\n", str);
319 pm_strcat(&omsg, (char *)str);
320 Dmsg1(400, "omsg=%s\n", omsg);