3 * Routines for handling the autochanger.
5 * Kern Sibbald, August MMII
10 Bacula® - The Network Backup Solution
12 Copyright (C) 2002-2006 Free Software Foundation Europe e.V.
14 The main author of Bacula is Kern Sibbald, with contributions from
15 many others, a complete list can be found in the file AUTHORS.
16 This program is Free Software; you can redistribute it and/or
17 modify it under the terms of version two of the GNU General Public
18 License as published by the Free Software Foundation plus additions
19 that are listed in the file LICENSE.
21 This program is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 Bacula® is a registered trademark of John Walker.
32 The licensor of Bacula is the Free Software Foundation Europe
33 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
34 Switzerland, email:ftf@fsfeurope.org.
37 #include "bacula.h" /* pull in global headers */
38 #include "stored.h" /* pull in Storage Deamon headers */
40 /* Forward referenced functions */
41 static void lock_changer(DCR *dcr);
42 static void unlock_changer(DCR *dcr);
43 static bool unload_other_drive(DCR *dcr, int slot);
45 /* Init all the autochanger resources found */
46 bool init_autochangers()
50 /* Ensure that the media_type for each device is the same */
51 foreach_res(changer, R_AUTOCHANGER) {
53 foreach_alist(device, changer->device) {
55 * If the device does not have a changer name or changer command
56 * defined, used the one from the Autochanger resource
58 if (!device->changer_name && changer->changer_name) {
59 device->changer_name = bstrdup(changer->changer_name);
61 if (!device->changer_command && changer->changer_command) {
62 device->changer_command = bstrdup(changer->changer_command);
64 if (!device->changer_name) {
65 Jmsg(NULL, M_ERROR, 0,
66 _("No Changer Name given for device %s. Cannot continue.\n"),
70 if (!device->changer_command) {
71 Jmsg(NULL, M_ERROR, 0,
72 _("No Changer Command given for device %s. Cannot continue.\n"),
78 if (media_type == NULL) {
79 media_type = device->media_type; /* get Media Type of first device */
82 /* Ensure that other devices Media Types are the same */
83 if (strcmp(media_type, device->media_type) != 0) {
84 Jmsg(NULL, M_ERROR, 0,
85 _("Media Type not the same for all devices in changer %s. Cannot continue.\n"),
98 * Called here to do an autoload using the autochanger, if
99 * configured, and if a Slot has been defined for this Volume.
100 * On success this routine loads the indicated tape, but the
101 * label is not read, so it must be verified.
103 * Note if dir is not NULL, it is the console requesting the
104 * autoload for labeling, so we respond directly to the
107 * Returns: 1 on success
108 * 0 on failure (no changer available)
109 * -1 on error on autochanger
111 int autoload_device(DCR *dcr, int writing, BSOCK *dir)
114 DEVICE *dev = dcr->dev;
116 int drive = dev->drive_index;
117 int rtn_stat = -1; /* error status */
120 if (!dev->is_autochanger()) {
121 Dmsg1(200, "Device %s is not an autochanger\n", dev->print_name());
125 /* An empty ChangerCommand => virtual disk autochanger */
126 if (dcr->device->changer_command && dcr->device->changer_command[0] == 0) {
127 return 1; /* nothing to load */
130 slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
132 * Handle autoloaders here. If we cannot autoload it, we
133 * will return 0 so that the sysop will be asked to load it.
135 if (writing && slot <= 0) {
137 return 0; /* For user, bail out right now */
139 if (dir_find_next_appendable_volume(dcr)) {
140 slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
145 Dmsg1(400, "Want changer slot=%d\n", slot);
147 changer = get_pool_memory(PM_FNAME);
149 Jmsg(jcr, M_INFO, 0, _("Invalid slot=%d defined in catalog. Manual load my be required.\n"), slot);
151 } else if (!dcr->device->changer_name) {
152 Jmsg(jcr, M_INFO, 0, _("No \"Changer Device\" manual load of Volume may be required.\n"));
154 } else if (!dcr->device->changer_command) {
155 Jmsg(jcr, M_INFO, 0, _("No \"Changer Command\" manual load of Volume may be requird.\n"));
158 /* Attempt to load the Volume */
160 uint32_t timeout = dcr->device->max_changer_wait;
163 loaded = get_autochanger_loaded_slot(dcr);
165 if (loaded != slot) {
166 POOL_MEM results(PM_MESSAGE);
168 /* Unload anything in our drive */
169 if (!unload_autochanger(dcr, loaded)) {
173 /* Make sure desired slot is unloaded */
174 if (!unload_other_drive(dcr, slot)) {
179 * Load the desired cassette
182 Dmsg1(100, "Doing changer load slot %d\n", slot);
184 _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"),
186 dcr->VolCatInfo.Slot = slot; /* slot to be loaded */
187 changer = edit_device_codes(dcr, changer, dcr->device->changer_command, "load");
189 Dmsg1(200, "Run program=%s\n", changer);
190 status = run_program_full_output(changer, timeout, results.c_str());
192 Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
194 Dmsg2(100, "load slot %d, drive %d, status is OK.\n", slot, drive);
195 dev->Slot = slot; /* set currently loaded slot */
198 be.set_errno(status);
199 Dmsg3(100, "load slot %d, drive %d, bad stats=%s.\n", slot, drive,
201 Jmsg(jcr, M_FATAL, 0, _("3992 Bad autochanger \"load slot %d, drive %d\": "
202 "ERR=%s.\nResults=%s\n"),
203 slot, drive, be.strerror(), results.c_str());
204 rtn_stat = -1; /* hard error */
205 dev->Slot = -1; /* mark unknown */
207 Dmsg2(100, "load slot %d status=%d\n", slot, status);
210 status = 0; /* we got what we want */
211 dev->Slot = slot; /* set currently loaded slot */
213 Dmsg1(100, "After changer, status=%d\n", status);
214 if (status == 0) { /* did we succeed? */
215 rtn_stat = 1; /* tape loaded by changer */
218 free_pool_memory(changer);
222 free_pool_memory(changer);
228 * Returns: -1 if error from changer command
230 * Note, this is safe to do without releasing the drive
231 * since it does not attempt load/unload a slot.
233 int get_autochanger_loaded_slot(DCR *dcr)
236 DEVICE *dev = dcr->dev;
238 uint32_t timeout = dcr->device->max_changer_wait;
239 int drive = dcr->dev->drive_index;
240 POOL_MEM results(PM_MESSAGE);
243 if (!dev->is_autochanger()) {
246 if (!dcr->device->changer_command) {
247 Jmsg(jcr, M_FATAL, 0, _("3992 Missing Changer command.\n"));
253 /* Virtual disk autochanger */
254 if (dcr->device->changer_command[0] ==0) {
258 /* Find out what is loaded, zero means device is unloaded */
259 changer = get_pool_memory(PM_FNAME);
261 Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded? drive %d\" command.\n"),
263 changer = edit_device_codes(dcr, changer, dcr->device->changer_command, "loaded");
264 *results.c_str() = 0;
265 Dmsg1(100, "Run program=%s\n", changer);
266 status = run_program_full_output(changer, timeout, results.c_str());
267 Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results.c_str());
269 loaded = str_to_int32(results.c_str());
271 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded? drive %d\", result is Slot %d.\n"),
275 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded? drive %d\", result: nothing loaded.\n"),
277 dev->Slot = -1; /* unknown */
281 be.set_errno(status);
282 Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded? drive %d\" command: "
283 "ERR=%s.\nResults=%s\n"), drive, be.strerror(), results.c_str());
284 loaded = -1; /* force unload */
287 free_pool_memory(changer);
291 static void lock_changer(DCR *dcr)
293 AUTOCHANGER *changer_res = dcr->device->changer_res;
295 Dmsg1(200, "Locking changer %s\n", changer_res->hdr.name);
296 P(changer_res->changer_mutex); /* Lock changer script */
300 static void unlock_changer(DCR *dcr)
302 AUTOCHANGER *changer_res = dcr->device->changer_res;
304 Dmsg1(200, "Unlocking changer %s\n", changer_res->hdr.name);
305 V(changer_res->changer_mutex); /* Unlock changer script */
310 * Unload the volume, if any, in this drive
311 * On entry: loaded == 0 -- nothing to do
312 * loaded < 0 -- check if anything to do
313 * loaded > 0 -- load slot == loaded
315 bool unload_autochanger(DCR *dcr, int loaded)
317 DEVICE *dev = dcr->dev;
320 uint32_t timeout = dcr->device->max_changer_wait;
327 if (!dev->is_autochanger() || !dcr->device->changer_name ||
328 !dcr->device->changer_command) {
333 loaded = get_autochanger_loaded_slot(dcr);
337 POOL_MEM results(PM_MESSAGE);
338 POOLMEM *changer = get_pool_memory(PM_FNAME);
341 _("3307 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
342 loaded, dev->drive_index);
343 slot = dcr->VolCatInfo.Slot;
344 dcr->VolCatInfo.Slot = loaded;
345 changer = edit_device_codes(dcr, changer,
346 dcr->device->changer_command, "unload");
348 Dmsg1(100, "Run program=%s\n", changer);
349 *results.c_str() = 0;
350 int stat = run_program_full_output(changer, timeout, results.c_str());
351 dcr->VolCatInfo.Slot = slot;
355 Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d, drive %d\": "
356 "ERR=%s\nResults=%s\n"),
357 loaded, dev->drive_index, be.strerror(), results.c_str());
359 dev->Slot = -1; /* unknown */
361 dev->Slot = 0; /* nothing loaded */
363 free_pool_memory(changer);
370 * Unload the slot if mounted in a different drive
372 static bool unload_other_drive(DCR *dcr, int slot)
378 uint32_t timeout = dcr->device->max_changer_wait;
380 AUTOCHANGER *changer = dcr->dev->device->changer_res;
388 if (changer->device->size() == 1) {
392 foreach_alist(device, changer->device) {
393 if (device->dev && device->dev->Slot == slot) {
403 /* The Volume we want is on another device. */
404 if (dev->is_busy()) {
405 Dmsg4(100, "Vol %s for dev=%s in use dev=%s slot=%d\n",
406 dcr->VolumeName, dcr->dev->print_name(),
407 dev->print_name(), slot);
409 for (int i=0; i < 3; i++) {
410 if (dev->is_busy()) {
411 wait_for_device(dcr->jcr, first);
418 if (dev->is_busy()) {
419 Jmsg(jcr, M_WARNING, 0, _("Volume \"%s\" is in use by device %s\n"),
420 dcr->VolumeName, dev->print_name());
421 Dmsg4(100, "Vol %s for dev=%s is busy dev=%s slot=%d\n",
422 dcr->VolumeName, dcr->dev->print_name(), dev->print_name(), slot);
423 Dmsg2(100, "num_writ=%d reserv=%d\n", dev->num_writers, dev->reserved_device);
428 POOLMEM *changer_cmd = get_pool_memory(PM_FNAME);
429 POOL_MEM results(PM_MESSAGE);
432 _("3307 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
433 slot, dev->drive_index);
435 Dmsg2(100, "Issuing autochanger \"unload slot %d, drive %d\" command.\n",
436 slot, dev->drive_index);
440 save_slot = dcr->VolCatInfo.Slot;
441 dcr->VolCatInfo.Slot = slot;
442 changer_cmd = edit_device_codes(dcr, changer_cmd,
443 dcr->device->changer_command, "unload");
445 Dmsg2(200, "close dev=%s reserve=%d\n", dev->print_name(),
446 dev->reserved_device);
447 Dmsg1(100, "Run program=%s\n", changer_cmd);
448 int stat = run_program_full_output(changer_cmd, timeout, results.c_str());
449 dcr->VolCatInfo.Slot = save_slot;
454 Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
455 slot, dev->drive_index, be.strerror());
457 Dmsg3(100, "Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n",
458 slot, dev->drive_index, be.strerror());
460 dev->Slot = -1; /* unknown */
462 dev->Slot = 0; /* nothing loaded */
463 Dmsg0(100, "Slot unloaded\n");
467 free_pool_memory(changer_cmd);
474 * List the Volumes that are in the autoloader possibly
475 * with their barcodes.
476 * We assume that it is always the Console that is calling us.
478 bool autochanger_cmd(DCR *dcr, BSOCK *dir, const char *cmd)
480 DEVICE *dev = dcr->dev;
481 uint32_t timeout = dcr->device->max_changer_wait;
484 int len = sizeof_pool_memory(dir->msg) - 1;
488 if (!dev->is_autochanger() || !dcr->device->changer_name ||
489 !dcr->device->changer_command) {
490 if (strcmp(cmd, "drives") == 0) {
491 bnet_fsend(dir, "drives=1\n");
493 bnet_fsend(dir, _("3993 Device %s not an autochanger device.\n"),
498 if (strcmp(cmd, "drives") == 0) {
499 AUTOCHANGER *changer_res = dcr->device->changer_res;
502 drives = changer_res->device->size();
504 bnet_fsend(dir, "drives=%d\n", drives);
505 Dmsg1(100, "drives=%d\n", drives);
509 changer = get_pool_memory(PM_FNAME);
511 /* Now issue the command */
512 changer = edit_device_codes(dcr, changer,
513 dcr->device->changer_command, cmd);
514 bnet_fsend(dir, _("3306 Issuing autochanger \"%s\" command.\n"), cmd);
515 bpipe = open_bpipe(changer, timeout, "r");
517 bnet_fsend(dir, _("3996 Open bpipe failed.\n"));
520 if (strcmp(cmd, "list") == 0) {
521 /* Get output from changer */
522 while (fgets(dir->msg, len, bpipe->rfd)) {
523 dir->msglen = strlen(dir->msg);
524 Dmsg1(100, "<stored: %s\n", dir->msg);
527 } else if (strcmp(cmd, "slots") == 0 ) {
529 /* For slots command, read a single line */
531 fgets(buf, sizeof(buf)-1, bpipe->rfd);
532 buf[sizeof(buf)-1] = 0;
533 /* Strip any leading space in front of # of slots */
534 for (p=buf; B_ISSPACE(*p); p++)
536 bnet_fsend(dir, "slots=%s", p);
537 Dmsg1(100, "<stored: %s", dir->msg);
540 stat = close_bpipe(bpipe);
544 bnet_fsend(dir, _("Autochanger error: ERR=%s\n"), be.strerror());
546 bnet_sig(dir, BNET_EOD);
551 free_pool_memory(changer);
557 * Edit codes into ChangerCommand
559 * %a = archive device name
560 * %c = changer device name
561 * %d = changer drive index
570 * omsg = edited output message
571 * imsg = input string containing edit codes (%x)
572 * cmd = command string (load, unload, ...)
575 char *edit_device_codes(DCR *dcr, char *omsg, const char *imsg, const char *cmd)
582 Dmsg1(1800, "edit_device_codes: %s\n", imsg);
583 for (p=imsg; *p; p++) {
590 str = dcr->dev->archive_name();
593 str = NPRT(dcr->device->changer_name);
596 sprintf(add, "%d", dcr->dev->drive_index);
603 sprintf(add, "%d", dcr->VolCatInfo.Slot - 1);
607 sprintf(add, "%d", dcr->VolCatInfo.Slot);
610 case 'j': /* Job name */
614 str = NPRT(dcr->VolumeName);
617 str = NPRT(dcr->jcr->client_name);
632 Dmsg1(1900, "add_str %s\n", str);
633 pm_strcat(&omsg, (char *)str);
634 Dmsg1(1800, "omsg=%s\n", omsg);
636 Dmsg1(800, "omsg=%s\n", omsg);