2 Bacula® - The Network Backup Solution
4 Copyright (C) 2002-2011 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * Routines for handling the autochanger.
32 * Kern Sibbald, August MMII
36 #include "bacula.h" /* pull in global headers */
37 #include "stored.h" /* pull in Storage Deamon headers */
39 /* Forward referenced functions */
40 static void lock_changer(DCR *dcr);
41 static void unlock_changer(DCR *dcr);
42 static bool unload_other_drive(DCR *dcr, int slot);
44 /* Init all the autochanger resources found */
45 bool init_autochangers()
49 /* Ensure that the media_type for each device is the same */
50 foreach_res(changer, R_AUTOCHANGER) {
52 foreach_alist(device, changer->device) {
54 * If the device does not have a changer name or changer command
55 * defined, used the one from the Autochanger resource
57 if (!device->changer_name && changer->changer_name) {
58 device->changer_name = bstrdup(changer->changer_name);
60 if (!device->changer_command && changer->changer_command) {
61 device->changer_command = bstrdup(changer->changer_command);
63 if (!device->changer_name) {
64 Jmsg(NULL, M_ERROR, 0,
65 _("No Changer Name given for device %s. Cannot continue.\n"),
69 if (!device->changer_command) {
70 Jmsg(NULL, M_ERROR, 0,
71 _("No Changer Command given for device %s. Cannot continue.\n"),
77 if (media_type == NULL) {
78 media_type = device->media_type; /* get Media Type of first device */
81 /* Ensure that other devices Media Types are the same */
82 if (strcmp(media_type, device->media_type) != 0) {
83 Jmsg(NULL, M_ERROR, 0,
84 _("Media Type not the same for all devices in changer %s. Cannot continue.\n"),
97 * Called here to do an autoload using the autochanger, if
98 * configured, and if a Slot has been defined for this Volume.
99 * On success this routine loads the indicated tape, but the
100 * label is not read, so it must be verified.
102 * Note if dir is not NULL, it is the console requesting the
103 * autoload for labeling, so we respond directly to the
106 * Returns: 1 on success
107 * 0 on failure (no changer available)
108 * -1 on error on autochanger
110 int autoload_device(DCR *dcr, int writing, BSOCK *dir)
113 DEVICE * volatile dev = dcr->dev;
115 int drive = dev->drive_index;
116 int rtn_stat = -1; /* error status */
119 if (!dev->is_autochanger()) {
120 Dmsg1(100, "Device %s is not an autochanger\n", dev->print_name());
124 /* An empty ChangerCommand => virtual disk autochanger */
125 if (dcr->device->changer_command && dcr->device->changer_command[0] == 0) {
126 Dmsg0(100, "ChangerCommand=0, virtual disk changer\n");
127 return 1; /* nothing to load */
130 slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
131 Dmsg3(100, "autoload: slot=%d InChgr=%d Vol=%s\n", dcr->VolCatInfo.Slot,
132 dcr->VolCatInfo.InChanger, dcr->getVolCatName());
134 * Handle autoloaders here. If we cannot autoload it, we
135 * will return 0 so that the sysop will be asked to load it.
137 if (writing && slot <= 0) {
139 return 0; /* For user, bail out right now */
141 /* ***FIXME*** this really should not be here */
142 if (dir_find_next_appendable_volume(dcr)) {
143 slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
148 Dmsg1(400, "Want changer slot=%d\n", slot);
150 changer = get_pool_memory(PM_FNAME);
152 /* Suppress info when polling */
154 Jmsg(jcr, M_INFO, 0, _("No slot defined in catalog (slot=%d) for Volume \"%s\" on %s.\n"),
155 slot, dcr->getVolCatName(), dev->print_name());
156 Jmsg(jcr, M_INFO, 0, _("Cartridge change or \"update slots\" may be required.\n"));
159 } else if (!dcr->device->changer_name) {
160 /* Suppress info when polling */
162 Jmsg(jcr, M_INFO, 0, _("No \"Changer Device\" for %s. Manual load of Volume may be required.\n"),
166 } else if (!dcr->device->changer_command) {
167 /* Suppress info when polling */
169 Jmsg(jcr, M_INFO, 0, _("No \"Changer Command\" for %s. Manual load of Volume may be requird.\n"),
174 /* Attempt to load the Volume */
176 uint32_t timeout = dcr->device->max_changer_wait;
179 loaded = get_autochanger_loaded_slot(dcr);
181 if (loaded != slot) {
182 POOL_MEM results(PM_MESSAGE);
184 /* Unload anything in our drive */
185 if (!unload_autochanger(dcr, loaded)) {
189 /* Make sure desired slot is unloaded */
190 if (!unload_other_drive(dcr, slot)) {
195 * Load the desired cassette
198 Dmsg2(100, "Doing changer load slot %d %s\n", slot, dev->print_name());
200 _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"),
202 dcr->VolCatInfo.Slot = slot; /* slot to be loaded */
203 changer = edit_device_codes(dcr, changer, dcr->device->changer_command, "load");
205 Dmsg1(200, "Run program=%s\n", changer);
206 status = run_program_full_output(changer, timeout, results.addr());
208 Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
210 Dmsg2(100, "load slot %d, drive %d, status is OK.\n", slot, drive);
211 dev->set_slot(slot); /* set currently loaded slot */
214 be.set_errno(status);
215 Dmsg3(100, "load slot %d, drive %d, bad stats=%s.\n", slot, drive,
217 Jmsg(jcr, M_FATAL, 0, _("3992 Bad autochanger \"load slot %d, drive %d\": "
218 "ERR=%s.\nResults=%s\n"),
219 slot, drive, be.bstrerror(), results.c_str());
220 rtn_stat = -1; /* hard error */
221 dev->set_slot(-1); /* mark unknown */
223 Dmsg2(100, "load slot %d status=%d\n", slot, status);
226 status = 0; /* we got what we want */
227 dev->set_slot(slot); /* set currently loaded slot */
229 Dmsg1(100, "After changer, status=%d\n", status);
230 if (status == 0) { /* did we succeed? */
231 rtn_stat = 1; /* tape loaded by changer */
234 free_pool_memory(changer);
238 free_pool_memory(changer);
244 * Returns: -1 if error from changer command
246 * Note, this is safe to do without releasing the drive
247 * since it does not attempt load/unload a slot.
249 int get_autochanger_loaded_slot(DCR *dcr)
252 DEVICE *dev = dcr->dev;
254 uint32_t timeout = dcr->device->max_changer_wait;
255 int drive = dcr->dev->drive_index;
256 POOL_MEM results(PM_MESSAGE);
259 if (!dev->is_autochanger()) {
262 if (!dcr->device->changer_command) {
263 // Jmsg(jcr, M_FATAL, 0, _("3992 Missing Changer command.\n"));
266 if (dev->get_slot() > 0) {
267 return dev->get_slot();
270 /* Virtual disk autochanger */
271 if (dcr->device->changer_command[0] == 0) {
275 /* Find out what is loaded, zero means device is unloaded */
276 changer = get_pool_memory(PM_FNAME);
278 /* Suppress info when polling */
280 Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded? drive %d\" command.\n"),
283 changer = edit_device_codes(dcr, changer, dcr->device->changer_command, "loaded");
284 Dmsg1(100, "Run program=%s\n", changer);
285 status = run_program_full_output(changer, timeout, results.addr());
286 Dmsg3(100, "run_prog: %s stat=%d result=%s", changer, status, results.c_str());
288 loaded = str_to_int32(results.c_str());
290 /* Suppress info when polling */
292 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded? drive %d\", result is Slot %d.\n"),
295 dev->set_slot(loaded);
297 /* Suppress info when polling */
299 Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded? drive %d\", result: nothing loaded.\n"),
302 if (loaded == 0) { /* no slot loaded */
304 } else { /* probably some error */
305 dev->clear_slot(); /* unknown */
310 be.set_errno(status);
311 Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded? drive %d\" command: "
312 "ERR=%s.\nResults=%s\n"), drive, be.bstrerror(), results.c_str());
313 loaded = -1; /* force unload */
316 free_pool_memory(changer);
320 static void lock_changer(DCR *dcr)
322 AUTOCHANGER *changer_res = dcr->device->changer_res;
325 Dmsg1(200, "Locking changer %s\n", changer_res->hdr.name);
326 if ((errstat=rwl_writelock(&changer_res->changer_lock)) != 0) {
328 Jmsg(dcr->jcr, M_ERROR_TERM, 0, _("Lock failure on autochanger. ERR=%s\n"),
329 be.bstrerror(errstat));
334 static void unlock_changer(DCR *dcr)
336 AUTOCHANGER *changer_res = dcr->device->changer_res;
339 Dmsg1(200, "Unlocking changer %s\n", changer_res->hdr.name);
340 if ((errstat=rwl_writeunlock(&changer_res->changer_lock)) != 0) {
342 Jmsg(dcr->jcr, M_ERROR_TERM, 0, _("Unlock failure on autochanger. ERR=%s\n"),
343 be.bstrerror(errstat));
349 * Unload the volume, if any, in this drive
350 * On entry: loaded == 0 -- nothing to do
351 * loaded < 0 -- check if anything to do
352 * loaded > 0 -- load slot == loaded
354 bool unload_autochanger(DCR *dcr, int loaded)
356 DEVICE *dev = dcr->dev;
359 uint32_t timeout = dcr->device->max_changer_wait;
366 if (!dev->is_autochanger() || !dcr->device->changer_name ||
367 !dcr->device->changer_command) {
371 /* Virtual disk autochanger */
372 if (dcr->device->changer_command[0] == 0) {
379 loaded = get_autochanger_loaded_slot(dcr);
383 POOL_MEM results(PM_MESSAGE);
384 POOLMEM *changer = get_pool_memory(PM_FNAME);
386 _("3307 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
387 loaded, dev->drive_index);
388 slot = dcr->VolCatInfo.Slot;
389 dcr->VolCatInfo.Slot = loaded;
390 changer = edit_device_codes(dcr, changer,
391 dcr->device->changer_command, "unload");
393 Dmsg1(100, "Run program=%s\n", changer);
394 int stat = run_program_full_output(changer, timeout, results.addr());
395 dcr->VolCatInfo.Slot = slot;
399 Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d, drive %d\": "
400 "ERR=%s\nResults=%s\n"),
401 loaded, dev->drive_index, be.bstrerror(), results.c_str());
403 dev->clear_slot(); /* unknown */
405 dev->set_slot(0); /* nothing loaded */
408 free_volume(dev); /* Free any volume associated with this drive */
409 free_pool_memory(changer);
419 * Unload the slot if mounted in a different drive
421 static bool unload_other_drive(DCR *dcr, int slot)
425 AUTOCHANGER *changer = dcr->dev->device->changer_res;
427 int retries = 0; /* wait for device retries */
432 if (changer->device->size() == 1) {
436 foreach_alist(device, changer->device) {
437 if (device->dev && device->dev->get_slot() == slot) {
447 /* The Volume we want is on another device. */
448 if (dev->is_busy()) {
449 Dmsg4(100, "Vol %s for dev=%s in use dev=%s slot=%d\n",
450 dcr->VolumeName, dcr->dev->print_name(),
451 dev->print_name(), slot);
453 for (int i=0; i < 3; i++) {
454 if (dev->is_busy()) {
455 wait_for_device(dcr->jcr, retries);
460 if (dev->is_busy()) {
461 Jmsg(dcr->jcr, M_WARNING, 0, _("Volume \"%s\" wanted on %s is in use by device %s\n"),
462 dcr->VolumeName, dcr->dev->print_name(), dev->print_name());
463 Dmsg4(100, "Vol %s for dev=%s is busy dev=%s slot=%d\n",
464 dcr->VolumeName, dcr->dev->print_name(), dev->print_name(), dev->get_slot());
465 Dmsg2(100, "num_writ=%d reserv=%d\n", dev->num_writers, dev->num_reserved());
469 return unload_dev(dcr, dev);
473 * Unconditionally unload a specified drive
475 bool unload_dev(DCR *dcr, DEVICE *dev)
479 uint32_t timeout = dcr->device->max_changer_wait;
480 AUTOCHANGER *changer = dcr->dev->device->changer_res;
488 save_dev = dcr->dev; /* save dcr device */
489 dcr->dev = dev; /* temporarily point dcr at other device */
491 if (dev->get_slot() <= 0 && get_autochanger_loaded_slot(dcr) <= 0) {
495 save_slot = dcr->VolCatInfo.Slot;
496 dcr->VolCatInfo.Slot = dev->get_slot();
500 POOLMEM *changer_cmd = get_pool_memory(PM_FNAME);
501 POOL_MEM results(PM_MESSAGE);
504 _("3307 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
505 dev->get_slot(), dev->drive_index);
507 Dmsg2(100, "Issuing autochanger \"unload slot %d, drive %d\" command.\n",
508 dev->get_slot(), dev->drive_index);
510 changer_cmd = edit_device_codes(dcr, changer_cmd,
511 dcr->device->changer_command, "unload");
513 Dmsg2(200, "close dev=%s reserve=%d\n", dev->print_name(),
514 dev->num_reserved());
515 Dmsg1(100, "Run program=%s\n", changer_cmd);
516 int stat = run_program_full_output(changer_cmd, timeout, results.addr());
517 dcr->VolCatInfo.Slot = save_slot;
522 Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
523 dev->get_slot(), dev->drive_index, be.bstrerror());
525 Dmsg3(100, "Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n",
526 dev->get_slot(), dev->drive_index, be.bstrerror());
528 dev->clear_slot(); /* unknown */
530 Dmsg2(100, "Slot %d unloaded %s\n", dev->get_slot(), dev->print_name());
531 dev->set_slot(0); /* nothing loaded */
540 free_volume(dev); /* Free any volume associated with this drive */
541 free_pool_memory(changer_cmd);
548 * List the Volumes that are in the autoloader possibly
549 * with their barcodes.
550 * We assume that it is always the Console that is calling us.
552 bool autochanger_cmd(DCR *dcr, BSOCK *dir, const char *cmd)
554 DEVICE *dev = dcr->dev;
555 uint32_t timeout = dcr->device->max_changer_wait;
558 int len = sizeof_pool_memory(dir->msg) - 1;
562 if (!dev->is_autochanger() || !dcr->device->changer_name ||
563 !dcr->device->changer_command) {
564 if (strcmp(cmd, "drives") == 0) {
565 dir->fsend("drives=1\n");
567 dir->fsend(_("3993 Device %s not an autochanger device.\n"),
572 if (strcmp(cmd, "drives") == 0) {
573 AUTOCHANGER *changer_res = dcr->device->changer_res;
576 drives = changer_res->device->size();
578 dir->fsend("drives=%d\n", drives);
579 Dmsg1(100, "drives=%d\n", drives);
583 changer = get_pool_memory(PM_FNAME);
585 /* Now issue the command */
586 changer = edit_device_codes(dcr, changer,
587 dcr->device->changer_command, cmd);
588 dir->fsend(_("3306 Issuing autochanger \"%s\" command.\n"), cmd);
589 bpipe = open_bpipe(changer, timeout, "r");
591 dir->fsend(_("3996 Open bpipe failed.\n"));
594 if (bstrcmp(cmd, "list") || bstrcmp(cmd, "listall")) {
595 /* Get output from changer */
596 while (fgets(dir->msg, len, bpipe->rfd)) {
597 dir->msglen = strlen(dir->msg);
598 Dmsg1(100, "<stored: %s\n", dir->msg);
601 } else if (strcmp(cmd, "slots") == 0 ) {
603 /* For slots command, read a single line */
605 fgets(buf, sizeof(buf)-1, bpipe->rfd);
606 buf[sizeof(buf)-1] = 0;
607 /* Strip any leading space in front of # of slots */
608 for (p=buf; B_ISSPACE(*p); p++)
610 dir->fsend("slots=%s", p);
611 Dmsg1(100, "<stored: %s", dir->msg);
614 stat = close_bpipe(bpipe);
618 dir->fsend(_("Autochanger error: ERR=%s\n"), be.bstrerror());
620 bnet_sig(dir, BNET_EOD);
625 free_pool_memory(changer);
631 * Edit codes into ChangerCommand
633 * %a = archive device name
634 * %c = changer device name
635 * %d = changer drive index
644 * omsg = edited output message
645 * imsg = input string containing edit codes (%x)
646 * cmd = command string (load, unload, ...)
649 char *edit_device_codes(DCR *dcr, char *omsg, const char *imsg, const char *cmd)
656 Dmsg1(1800, "edit_device_codes: %s\n", imsg);
657 for (p=imsg; *p; p++) {
664 str = dcr->dev->archive_name();
667 str = NPRT(dcr->device->changer_name);
670 sprintf(add, "%d", dcr->dev->drive_index);
677 sprintf(add, "%d", dcr->VolCatInfo.Slot - 1);
681 sprintf(add, "%d", dcr->VolCatInfo.Slot);
684 case 'j': /* Job name */
688 if (dcr->VolCatInfo.VolCatName[0]) {
689 str = dcr->VolCatInfo.VolCatName;
690 } else if (dcr->VolumeName[0]) {
691 str = dcr->VolumeName;
692 } else if (dcr->dev->vol && dcr->dev->vol->vol_name) {
693 str = dcr->dev->vol->vol_name;
695 str = dcr->dev->VolHdr.VolumeName;
699 str = NPRT(dcr->jcr->client_name);
714 Dmsg1(1900, "add_str %s\n", str);
715 pm_strcat(&omsg, (char *)str);
716 Dmsg1(1800, "omsg=%s\n", omsg);
718 Dmsg1(800, "omsg=%s\n", omsg);