2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 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 two of the GNU 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 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 John Walker.
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.
29 * Drive reservation functions for Storage Daemon
33 * Split from job.c and acquire.c June 2005
42 #define jid() ((int)get_jobid_from_tid())
44 const int dbglvl = 50;
46 static dlist *vol_list = NULL;
47 static brwlock_t reservation_lock;
48 static brwlock_t vol_list_lock;
50 /* Forward referenced functions */
51 static int can_reserve_drive(DCR *dcr, RCTX &rctx);
52 static int reserve_device(RCTX &rctx);
53 static bool reserve_device_for_read(DCR *dcr);
54 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx);
55 static bool use_storage_cmd(JCR *jcr);
56 static void queue_reserve_message(JCR *jcr);
58 /* Requests from the Director daemon */
59 static char use_storage[] = "use storage=%127s media_type=%127s "
60 "pool_name=%127s pool_type=%127s append=%d copy=%d stripe=%d\n";
61 static char use_device[] = "use device=%127s\n";
63 /* Responses sent to Director daemon */
64 static char OK_device[] = "3000 OK use device device=%s\n";
65 static char NO_device[] = "3924 Device \"%s\" not in SD Device resources.\n";
66 static char BAD_use[] = "3913 Bad use command: %s\n";
68 bool use_cmd(JCR *jcr)
71 * Get the device, media, and pool information
73 if (!use_storage_cmd(jcr)) {
74 set_jcr_job_status(jcr, JS_ErrorTerminated);
75 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
81 static int my_compare(void *item1, void *item2)
83 return strcmp(((VOLRES *)item1)->vol_name, ((VOLRES *)item2)->vol_name);
87 void init_reservations_lock()
90 if ((errstat=rwl_init(&reservation_lock)) != 0) {
92 Emsg1(M_ABORT, 0, _("Unable to initialize reservation lock. ERR=%s\n"),
93 be.bstrerror(errstat));
96 if ((errstat=rwl_init(&vol_list_lock)) != 0) {
98 Emsg1(M_ABORT, 0, _("Unable to initialize volume list lock. ERR=%s\n"),
99 be.bstrerror(errstat));
103 void term_reservations_lock()
105 rwl_destroy(&reservation_lock);
106 rwl_destroy(&vol_list_lock);
109 int reservations_lock_count = 0;
111 /* This applies to a drive and to Volumes */
112 void _lock_reservations()
115 reservations_lock_count++;
116 if ((errstat=rwl_writelock(&reservation_lock)) != 0) {
118 Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
119 errstat, be.bstrerror(errstat));
123 void _unlock_reservations()
126 reservations_lock_count--;
127 if ((errstat=rwl_writeunlock(&reservation_lock)) != 0) {
129 Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
130 errstat, be.bstrerror(errstat));
134 int vol_list_lock_count = 0;
139 vol_list_lock_count++;
140 if ((errstat=rwl_writelock(&vol_list_lock)) != 0) {
142 Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
143 errstat, be.bstrerror(errstat));
147 void _unlock_volumes()
150 vol_list_lock_count--;
151 if ((errstat=rwl_writeunlock(&vol_list_lock)) != 0) {
153 Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
154 errstat, be.bstrerror(errstat));
160 * List Volumes -- this should be moved to status.c
167 static void debug_list_volumes(const char *imsg)
170 POOL_MEM msg(PM_MESSAGE);
173 foreach_dlist(vol, vol_list) {
175 Mmsg(msg, "List from %s: %s at %p on device %s\n", imsg,
176 vol->vol_name, vol->vol_name, vol->dev->print_name());
178 Mmsg(msg, "List from %s: %s at %p no dev\n", imsg, vol->vol_name, vol->vol_name);
180 Dmsg2(dbglvl, "jid=%u %s", jid(), msg.c_str());
185 foreach_dlist(vol, vol_list) {
186 if (vol->dev == dev) {
187 Dmsg0(000, "Two Volumes on same device.\n");
194 // Dmsg2(dbglvl, "List from %s: %d volumes\n", imsg, count);
200 * List Volumes -- this should be moved to status.c
202 void list_volumes(void sendit(const char *msg, int len, void *sarg), void *arg)
205 POOL_MEM msg(PM_MESSAGE);
209 foreach_dlist(vol, vol_list) {
210 DEVICE *dev = vol->dev;
212 len = Mmsg(msg, "%s on device %s\n", vol->vol_name, dev->print_name());
213 sendit(msg.c_str(), len, arg);
214 len = Mmsg(msg, " Reader=%d writers=%d reserved=%d\n", dev->can_read()?1:0,
215 dev->num_writers, dev->reserved_device);
216 sendit(msg.c_str(), len, arg);
218 len = Mmsg(msg, "%s no dev\n", vol->vol_name);
219 sendit(msg.c_str(), len, arg);
226 * Create a Volume item to put in the Volume list
227 * Ensure that the device points to it.
229 static VOLRES *new_vol_item(DCR *dcr, const char *VolumeName)
232 vol = (VOLRES *)malloc(sizeof(VOLRES));
233 memset(vol, 0, sizeof(VOLRES));
234 vol->vol_name = bstrdup(VolumeName);
236 Dmsg4(dbglvl, "jid=%u new Vol=%s at %p dev=%s\n", (int)dcr->jcr->JobId,
237 VolumeName, vol->vol_name, vol->dev->print_name());
241 static void free_vol_item(VOLRES *vol)
245 vol->dev->vol = NULL;
252 * Put a new Volume entry in the Volume list. This
253 * effectively reserves the volume so that it will
254 * not be mounted again.
256 * If the device has any current volume associated with it,
257 * and it is a different Volume, and the device is not busy,
258 * we release the old Volume item and insert the new one.
260 * It is assumed that the device is free and locked so that
261 * we can change the device structure.
263 * Some details of the Volume list handling:
265 * 1. The Volume list entry must be attached to the drive (rather than
266 * attached to a job as it currently is. I.e. the drive that "owns"
267 * the volume (reserved, in use, mounted)
268 * must point to the volume (still to be maintained in a list).
270 * 2. The Volume is entered in the list when a drive is reserved.
272 * 3. When a drive is in use, the device code must appropriately update the
273 * volume name as it changes (currently the list is static -- an entry is
274 * removed when the Volume is no longer reserved, in use or mounted).
275 * The new code must keep the same list entry as long as the drive
276 * has any volume associated with it but the volume name in the list
277 * must be updated when the drive has a different volume mounted.
279 * 4. A job that has reserved a volume, can un-reserve the volume, and if the
280 * volume is not mounted, and not reserved, and not in use, it will be
281 * removed from the list.
283 * 5. If a job wants to reserve a drive with a different Volume from the one on
284 * the drive, it can re-use the drive for the new Volume.
286 * 6. If a job wants a Volume that is in a different drive, it can either use the
287 * other drive or take the volume, only if the other drive is not in use or
290 * One nice aspect of this is that the reserve use count and the writer use count
291 * already exist and are correctly programmed and will need no changes -- use
292 * counts are always very tricky.
294 * The old code had a concept of "reserving" a Volume, but it needs to be changed
295 * to reserving and using a drive. A volume is must be attached to (owned by) a
296 * drive and can move from drive to drive or be unused given certain specific
297 * conditions of the drive. The key is that the drive must "own" the Volume.
298 * The old code has the job (dcr) owning the volume (more or less). The job is
299 * to change the insertion and removal of the volumes from the list to be based
300 * on the drive rather than the job.
302 * Return: VOLRES entry on success
303 * NULL volume busy on another drive
305 VOLRES *reserve_volume(DCR *dcr, const char *VolumeName)
308 DEVICE *dev = dcr->dev;
312 Dmsg2(dbglvl, "jid=%u reserve_volume %s\n", jid(), VolumeName);
314 * We lock the reservations system here to ensure
315 * when adding a new volume that no newly scheduled
316 * job can reserve it.
319 debug_list_volumes("begin reserve_volume");
321 * First, remove any old volume attached to this device as it
327 * Make sure we don't remove the current volume we are inserting
328 * because it was probably inserted by another job.
330 if (strcmp(vol->vol_name, VolumeName) == 0) {
331 goto get_out; /* Volume already on this device */
333 Dmsg3(dbglvl, "jid=%u reserve_vol free vol=%s at %p\n",
334 (int)dcr->jcr->JobId, vol->vol_name, vol->vol_name);
335 debug_list_volumes("reserve_vol free");
336 vol_list->remove(vol);
341 /* Create a new Volume entry */
342 nvol = new_vol_item(dcr, VolumeName);
345 * Now try to insert the new Volume
347 vol = (VOLRES *)vol_list->binary_insert(nvol, my_compare);
349 Dmsg3(dbglvl, "jid=%u Found vol=%s dev-same=%d\n", jid(), vol->vol_name, dev==vol->dev);
351 * At this point, a Volume with this name already is in the list,
352 * so we simply release our new Volume entry. Note, this should
353 * only happen if we are moving the volume from one drive to another.
355 Dmsg3(dbglvl, "jid=%u reserve_vol free-tmp vol=%s at %p\n",
356 (int)dcr->jcr->JobId, vol->vol_name, vol->vol_name);
358 * Clear dev pointer so that free_vol_item() doesn't
359 * take away our volume.
361 nvol->dev = NULL; /* don't zap dev entry */
364 /* Check if we are trying to use the Volume on a different drive */
365 if (dev != vol->dev) {
366 /* Caller wants to switch Volume to another device */
367 if (!vol->dev->is_busy()) {
368 /* OK to move it -- I'm not sure this will work */
369 Dmsg4(dbglvl, "==== jid=%u Swap vol=%s from dev=%s to %s\n", jid(), VolumeName,
370 vol->dev->print_name(), dev->print_name());
371 vol->dev->vol = NULL; /* take vol from old drive */
372 vol->dev->VolHdr.VolumeName[0] = 0;
373 vol->dev = dev; /* point vol at new drive */
374 dev->vol = vol; /* point dev at vol */
375 dev->VolHdr.VolumeName[0] = 0;
377 Dmsg4(dbglvl, "jid=%u Volume busy could not swap vol=%s from dev=%s to %s\n",
378 jid(), VolumeName, vol->dev->print_name(), dev->print_name());
379 vol = NULL; /* device busy */
386 debug_list_volumes("end new volume");
392 * Search for a Volume name in the Volume list.
394 * Returns: VOLRES entry on success
395 * NULL if the Volume is not in the list
397 VOLRES *find_volume(DCR *dcr)
400 /* Do not lock reservations here */
402 vol.vol_name = bstrdup(dcr->VolumeName);
403 fvol = (VOLRES *)vol_list->binary_search(&vol, my_compare);
405 Dmsg3(dbglvl, "jid=%u find_vol=%s found=%d\n", jid(), dcr->VolumeName, fvol!=NULL);
406 debug_list_volumes("find_volume");
412 * Remove any reservation from a drive and tell the system
413 * that the volume is unused at least by us.
415 void unreserve_device(DCR *dcr)
417 DEVICE *dev = dcr->dev;
419 if (dcr->reserved_device) {
420 dcr->reserved_device = false;
421 dev->reserved_device--;
422 Dmsg3(dbglvl, "jid=%u Dec reserve=%d dev=%s\n", jid(), dev->reserved_device, dev->print_name());
423 dcr->reserved_device = false;
424 /* If we set read mode in reserving, remove it */
425 if (dev->can_read()) {
428 if (dev->num_writers < 0) {
429 Jmsg1(dcr->jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
430 dev->num_writers = 0;
439 * Free a Volume from the Volume list if it is no longer used
441 * Returns: true if the Volume found and removed from the list
442 * false if the Volume is not in the list or is in use
444 bool volume_unused(DCR *dcr)
446 DEVICE *dev = dcr->dev;
448 if (dev->vol == NULL) {
449 Dmsg2(dbglvl, "jid=%u vol_unused: no vol on %s\n", (int)dcr->jcr->JobId, dev->print_name());
450 debug_list_volumes("null vol cannot unreserve_volume");
454 if (dev->is_busy()) {
455 Dmsg2(dbglvl, "jid=%u vol_unused: no vol on %s\n", (int)dcr->jcr->JobId, dev->print_name());
456 debug_list_volumes("dev busy cannot unreserve_volume");
461 * If this is a tape, we do not free the volume, rather we wait
462 * until the autoloader unloads it, or until another tape is
463 * explicitly read in this drive. This allows the SD to remember
464 * where the tapes are or last were.
466 if (dev->is_tape() || dev->is_autochanger()) {
469 return free_volume(dev);
474 * Unconditionally release the volume
476 bool free_volume(DEVICE *dev)
480 if (dev->vol == NULL) {
481 Dmsg2(dbglvl, "jid=%u No vol on dev %s\n", jid(), dev->print_name());
487 vol_list->remove(vol);
488 Dmsg3(dbglvl, "jid=%u free_volume %s dev=%s\n", jid(), vol->vol_name, dev->print_name());
490 debug_list_volumes("free_volume");
496 /* Create the Volume list */
497 void create_volume_list()
500 if (vol_list == NULL) {
501 vol_list = New(dlist(vol, &vol->link));
505 /* Release all Volumes from the list */
506 void free_volume_list()
513 foreach_dlist(vol, vol_list) {
515 Dmsg3(dbglvl, "jid=%u free vol_list Volume=%s dev=%s\n", jid(),
516 vol->vol_name, vol->dev->print_name());
518 Dmsg3(dbglvl, "jid=%u free vol_list Volume=%s dev=%p\n", jid(),
519 vol->vol_name, vol->dev);
522 vol->vol_name = NULL;
529 bool is_volume_in_use(DCR *dcr)
531 VOLRES *vol = find_volume(dcr);
533 Dmsg2(dbglvl, "jid=%u Vol=%s not in use.\n", jid(), dcr->VolumeName);
534 return false; /* vol not in list */
536 ASSERT(vol->dev != NULL);
538 if (dcr->dev == vol->dev) { /* same device OK */
539 Dmsg2(dbglvl, "jid=%u Vol=%s on same dev.\n", jid(), dcr->VolumeName);
542 Dmsg4(dbglvl, "jid=%u Vol=%s on %s we have %s\n", jid(), dcr->VolumeName,
543 vol->dev->print_name(), dcr->dev->print_name());
545 if (!vol->dev->is_busy()) {
546 Dmsg3(dbglvl, "jid=%u Vol=%s dev=%s not busy.\n", jid(), dcr->VolumeName, vol->dev->print_name());
549 Dmsg3(dbglvl, "jid=%u Vol=%s dev=%s busy.\n", jid(), dcr->VolumeName, vol->dev->print_name());
551 Dmsg3(dbglvl, "jid=%u Vol=%s in use by %s.\n", jid(), dcr->VolumeName, vol->dev->print_name());
557 * We get the following type of information:
559 * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=1 copy=0 strip=0
563 * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=0 copy=0 strip=0
567 static bool use_storage_cmd(JCR *jcr)
569 POOL_MEM store_name, dev_name, media_type, pool_name, pool_type;
570 BSOCK *dir = jcr->dir_bsock;
580 memset(&rctx, 0, sizeof(RCTX));
583 * If there are multiple devices, the director sends us
584 * use_device for each device that it wants to use.
586 dirstore = New(alist(10, not_owned_by_alist));
587 msgs = jcr->reserve_msgs = New(alist(10, not_owned_by_alist));
589 Dmsg2(dbglvl, "jid=%u <dird: %s", jid(), dir->msg);
590 ok = sscanf(dir->msg, use_storage, store_name.c_str(),
591 media_type.c_str(), pool_name.c_str(),
592 pool_type.c_str(), &append, &Copy, &Stripe) == 7;
597 jcr->write_store = dirstore;
599 jcr->read_store = dirstore;
601 rctx.append = append;
602 unbash_spaces(store_name);
603 unbash_spaces(media_type);
604 unbash_spaces(pool_name);
605 unbash_spaces(pool_type);
606 store = new DIRSTORE;
607 dirstore->append(store);
608 memset(store, 0, sizeof(DIRSTORE));
609 store->device = New(alist(10));
610 bstrncpy(store->name, store_name, sizeof(store->name));
611 bstrncpy(store->media_type, media_type, sizeof(store->media_type));
612 bstrncpy(store->pool_name, pool_name, sizeof(store->pool_name));
613 bstrncpy(store->pool_type, pool_type, sizeof(store->pool_type));
614 store->append = append;
616 /* Now get all devices */
617 while (dir->recv() >= 0) {
618 Dmsg2(dbglvl, "jid=%u <dird device: %s", jid(), dir->msg);
619 ok = sscanf(dir->msg, use_device, dev_name.c_str()) == 1;
623 unbash_spaces(dev_name);
624 store->device->append(bstrdup(dev_name.c_str()));
626 } while (ok && dir->recv() >= 0);
628 /* Developer debug code */
630 if (debug_level >= dbglvl) {
631 foreach_alist(store, dirstore) {
632 Dmsg6(dbglvl, "jid=%u Storage=%s media_type=%s pool=%s pool_type=%s append=%d\n",
633 (int)rctx.jcr->JobId,
634 store->name, store->media_type, store->pool_name,
635 store->pool_type, store->append);
636 foreach_alist(device_name, store->device) {
637 Dmsg2(dbglvl, "jid=%u Device=%s\n", jid(), device_name);
642 init_jcr_device_wait_timers(jcr);
643 jcr->dcr = new_dcr(jcr, NULL, NULL); /* get a dcr */
645 BSOCK *dir = jcr->dir_bsock;
646 dir->fsend(_("3939 Could not get dcr\n"));
647 Dmsg1(dbglvl, ">dird: %s", dir->msg);
651 * At this point, we have a list of all the Director's Storage
652 * resources indicated for this Job, which include Pool, PoolType,
653 * storage name, and Media type.
654 * Then for each of the Storage resources, we have a list of
655 * device names that were given.
657 * Wiffle through them and find one that can do the backup.
660 int wait_for_device_retries = 0;
663 rctx.notify_dir = true;
666 for ( ; !fail && !job_canceled(jcr); ) {
667 while ((msg = (char *)msgs->pop())) {
670 rctx.suitable_device = false;
671 rctx.have_volume = false;
672 rctx.VolumeName[0] = 0;
673 rctx.any_drive = false;
674 if (!jcr->PreferMountedVols) {
676 * Here we try to find a drive that is not used.
677 * This will maximize the use of available drives.
680 rctx.num_writers = 20000000; /* start with impossible number */
681 rctx.low_use_drive = NULL;
682 rctx.PreferMountedVols = false;
683 rctx.exact_match = false;
684 rctx.autochanger_only = true;
685 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
686 (int)rctx.jcr->JobId,
687 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
688 rctx.autochanger_only, rctx.any_drive);
689 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
692 /* Look through all drives possibly for low_use drive */
693 if (rctx.low_use_drive) {
694 rctx.try_low_use_drive = true;
695 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
698 rctx.try_low_use_drive = false;
700 rctx.autochanger_only = false;
701 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
702 (int)rctx.jcr->JobId,
703 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
704 rctx.autochanger_only, rctx.any_drive);
705 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
710 * Now we look for a drive that may or may not be in
713 /* Look for an exact Volume match all drives */
714 rctx.PreferMountedVols = true;
715 rctx.exact_match = true;
716 rctx.autochanger_only = false;
717 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
718 (int)rctx.jcr->JobId,
719 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
720 rctx.autochanger_only, rctx.any_drive);
721 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
724 /* Look for any mounted drive */
725 rctx.exact_match = false;
726 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
727 (int)rctx.jcr->JobId,
728 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
729 rctx.autochanger_only, rctx.any_drive);
730 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
734 rctx.any_drive = true;
735 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
736 (int)rctx.jcr->JobId,
737 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
738 rctx.autochanger_only, rctx.any_drive);
739 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
742 /* Keep reservations locked *except* during wait_for_device() */
743 unlock_reservations();
745 * The idea of looping on repeat a few times it to ensure
746 * that if there is some subtle timing problem between two
747 * jobs, we will simply try again, and most likely succeed.
748 * This can happen if one job reserves a drive or finishes using
749 * a drive at the same time a second job wants it.
751 if (repeat++ > 1) { /* try algorithm 3 times */
752 bmicrosleep(30, 0); /* wait a bit */
753 Dmsg1(dbglvl, "jid=%u repeat reserve algorithm\n", (int)rctx.jcr->JobId);
754 } else if (!rctx.suitable_device || !wait_for_device(jcr, wait_for_device_retries)) {
755 Dmsg1(dbglvl, "jid=%u Fail. !suitable_device || !wait_for_device\n",
756 (int)rctx.jcr->JobId);
760 dir->signal(BNET_HEARTBEAT); /* Inform Dir that we are alive */
762 unlock_reservations();
765 * If we get here, there are no suitable devices available, which
766 * means nothing configured. If a device is suitable but busy
767 * with another Volume, we will not come here.
769 unbash_spaces(dir->msg);
770 pm_strcpy(jcr->errmsg, dir->msg);
771 Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
772 Jmsg(jcr, M_FATAL, 0, _("\n"
773 " Device \"%s\" with MediaType \"%s\" requested by DIR not found in SD Device resources.\n"),
774 dev_name.c_str(), media_type.c_str());
775 dir->fsend(NO_device, dev_name.c_str());
777 Dmsg2(dbglvl, "jid=%u >dird: %s", jid(), dir->msg);
780 unbash_spaces(dir->msg);
781 pm_strcpy(jcr->errmsg, dir->msg);
782 Jmsg(jcr, M_FATAL, 0, _("Failed command: %s\n"), jcr->errmsg);
783 dir->fsend(BAD_use, jcr->errmsg);
784 Dmsg2(dbglvl, "jid=%u >dird: %s", jid(), dir->msg);
791 void release_msgs(JCR *jcr)
793 alist *msgs = jcr->reserve_msgs;
800 while ((msg = (char *)msgs->pop())) {
804 jcr->reserve_msgs = NULL;
805 unlock_reservations();
809 * Walk through the autochanger resources and check if
810 * the volume is in one of them.
812 * Returns: true if volume is in device
815 static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol)
817 AUTOCHANGER *changer;
821 Dmsg2(dbglvl, "jid=%u search changers for %s\n", (int)rctx.jcr->JobId,
823 foreach_res(changer, R_AUTOCHANGER) {
824 Dmsg3(dbglvl, "jid=%u Try match changer res=%s device=%s\n",
825 (int)rctx.jcr->JobId, changer->hdr.name, rctx.device_name);
826 /* Find resource, and make sure we were able to open it */
827 if (fnmatch(rctx.device_name, changer->hdr.name, 0) == 0) {
829 /* Try each device in this AutoChanger */
830 foreach_alist(device, changer->device) {
831 if (device->dev == vol->dev) {
832 Dmsg2(dbglvl, "jid=%u Found changer device %s\n",
833 (int)rctx.jcr->JobId, device->hdr.name);
836 Dmsg2(dbglvl, "jid=%u Incorrect changer device %s\n",
837 (int)rctx.jcr->JobId, device->hdr.name);
845 * Search for a device suitable for this job.
847 bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
856 dirstore = jcr->write_store;
858 dirstore = jcr->read_store;
860 Dmsg5(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d\n",
861 (int)rctx.jcr->JobId,
862 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
863 rctx.autochanger_only);
866 * If the appropriate conditions of this if are met, namely that
867 * we are appending and the user wants mounted drive (or we
868 * force try a mounted drive because they are all busy), we
869 * start by looking at all the Volumes in the volume list.
871 if (!vol_list->empty() && rctx.append && rctx.PreferMountedVols) {
872 dlist *temp_vol_list, *save_vol_list;
877 * Create a temporary copy of the volume list. We do this,
878 * to avoid having the volume list locked during the
879 * call to reserve_device(), which would cause a deadlock.
880 * Note, we may want to add an update counter on the vol_list
881 * so that if it is modified while we are traversing the copy
882 * we can take note and act accordingly (probably redo the
883 * search at least a few times).
885 Dmsg1(dbglvl, "jid=%u duplicate vol list\n", (int)rctx.jcr->JobId);
886 temp_vol_list = New(dlist(vol, &vol->link));
887 foreach_dlist(vol, vol_list) {
889 VOLRES *tvol = (VOLRES *)malloc(sizeof(VOLRES));
890 memset(tvol, 0, sizeof(VOLRES));
891 tvol->vol_name = bstrdup(vol->vol_name);
892 tvol->dev = vol->dev;
893 nvol = (VOLRES *)temp_vol_list->binary_insert(tvol, my_compare);
895 tvol->dev = NULL; /* don't zap dev entry */
897 Pmsg0(000, "Logic error. Duplicating vol list hit duplicate.\n");
898 Jmsg(jcr, M_WARNING, 0, "Logic error. Duplicating vol list hit duplicate.\n");
903 /* Look through reserved volumes for one we can use */
904 Dmsg1(dbglvl, "jid=%u look for vol in vol list\n", (int)rctx.jcr->JobId);
905 foreach_dlist(vol, temp_vol_list) {
907 Dmsg2(dbglvl, "jid=%u vol=%s no dev\n", (int)rctx.jcr->JobId, vol->vol_name);
910 /* Check with Director if this Volume is OK */
911 bstrncpy(dcr->VolumeName, vol->vol_name, sizeof(dcr->VolumeName));
912 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
916 Dmsg2(dbglvl, "jid=%u vol=%s\n", (int)rctx.jcr->JobId, vol->vol_name);
917 foreach_alist(store, dirstore) {
920 foreach_alist(device_name, store->device) {
921 /* Found a device, try to use it */
922 rctx.device_name = device_name;
923 rctx.device = vol->dev->device;
925 if (vol->dev->is_autochanger()) {
926 Dmsg2(dbglvl, "jid=%u vol=%s is in changer\n", (int)rctx.jcr->JobId,
928 if (!is_vol_in_autochanger(rctx, vol)) {
931 } else if (strcmp(device_name, vol->dev->device->hdr.name) != 0) {
932 Dmsg3(dbglvl, "jid=%u device=%s not suitable want %s\n", (int)rctx.jcr->JobId,
933 vol->dev->device->hdr.name, device_name);
937 bstrncpy(rctx.VolumeName, vol->vol_name, sizeof(rctx.VolumeName));
938 rctx.have_volume = true;
939 /* Try reserving this device and volume */
940 Dmsg3(dbglvl, "jid=%u try vol=%s on device=%s\n", (int)rctx.jcr->JobId,
941 rctx.VolumeName, device_name);
942 stat = reserve_device(rctx);
943 if (stat == 1) { /* found available device */
944 Dmsg2(dbglvl, "jid=%u Suitable device found=%s\n", (int)rctx.jcr->JobId,
948 } else if (stat == 0) { /* device busy */
949 Dmsg2(dbglvl, "jid=%u Suitable device=%s, busy: not use\n",
950 (int)rctx.jcr->JobId, device_name);
952 /* otherwise error */
953 Dmsg1(dbglvl, "jid=%u No suitable device found.\n", (int)rctx.jcr->JobId);
955 rctx.have_volume = false;
961 } /* end for loop over reserved volumes */
964 save_vol_list = vol_list;
965 vol_list = temp_vol_list;
966 free_volume_list(); /* release temp_vol_list */
967 vol_list = save_vol_list;
968 Dmsg1(dbglvl, "jid=%u deleted temp vol list\n", (int)rctx.jcr->JobId);
972 Dmsg2(dbglvl, "jid=%u got vol %s in reserved volums list\n", (int)rctx.jcr->JobId,
978 * No reserved volume we can use, so now search for an available device.
980 * For each storage device that the user specified, we
981 * search and see if there is a resource for that device.
983 foreach_alist(store, dirstore) {
985 foreach_alist(device_name, store->device) {
987 rctx.device_name = device_name;
988 stat = search_res_for_device(rctx);
989 if (stat == 1) { /* found available device */
990 Dmsg2(dbglvl, "jid=%u available device found=%s\n", (int)rctx.jcr->JobId,
994 } else if (stat == 0) { /* device busy */
995 Dmsg2(dbglvl, "jid=%u Suitable device=%s, busy: not use\n",
996 (int)rctx.jcr->JobId, device_name);
998 /* otherwise error */
999 Dmsg1(dbglvl, "jid=%u No suitable device found.\n", (int)rctx.jcr->JobId);
1010 * Search for a particular storage device with particular storage
1011 * characteristics (MediaType).
1013 int search_res_for_device(RCTX &rctx)
1015 AUTOCHANGER *changer;
1018 Dmsg2(dbglvl, "jid=%u search res for %s\n", (int)rctx.jcr->JobId, rctx.device_name);
1019 /* Look through Autochangers first */
1020 foreach_res(changer, R_AUTOCHANGER) {
1021 Dmsg2(dbglvl, "jid=%u Try match changer res=%s\n", (int)rctx.jcr->JobId, changer->hdr.name);
1022 /* Find resource, and make sure we were able to open it */
1023 if (fnmatch(rctx.device_name, changer->hdr.name, 0) == 0) {
1024 /* Try each device in this AutoChanger */
1025 foreach_alist(rctx.device, changer->device) {
1026 Dmsg2(dbglvl, "jid=%u Try changer device %s\n", (int)rctx.jcr->JobId,
1027 rctx.device->hdr.name);
1028 stat = reserve_device(rctx);
1029 if (stat != 1) { /* try another device */
1033 if (rctx.store->append == SD_APPEND) {
1034 Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for append.\n",
1035 (int)rctx.jcr->JobId, rctx.device->hdr.name,
1036 rctx.jcr->dcr->dev->reserved_device);
1038 Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for read.\n",
1039 (int)rctx.jcr->JobId, rctx.device->hdr.name,
1040 rctx.jcr->read_dcr->dev->reserved_device);
1047 /* Now if requested look through regular devices */
1048 if (!rctx.autochanger_only) {
1049 foreach_res(rctx.device, R_DEVICE) {
1050 Dmsg2(dbglvl, "jid=%u Try match res=%s\n", (int)rctx.jcr->JobId, rctx.device->hdr.name);
1051 /* Find resource, and make sure we were able to open it */
1052 if (fnmatch(rctx.device_name, rctx.device->hdr.name, 0) == 0) {
1053 stat = reserve_device(rctx);
1054 if (stat != 1) { /* try another device */
1058 if (rctx.store->append == SD_APPEND) {
1059 Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for append.\n",
1060 (int)rctx.jcr->JobId, rctx.device->hdr.name,
1061 rctx.jcr->dcr->dev->reserved_device);
1063 Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for read.\n",
1064 (int)rctx.jcr->JobId, rctx.device->hdr.name,
1065 rctx.jcr->read_dcr->dev->reserved_device);
1071 return -1; /* nothing found */
1075 * Try to reserve a specific device.
1077 * Returns: 1 -- OK, have DCR
1081 static int reserve_device(RCTX &rctx)
1085 const int name_len = MAX_NAME_LENGTH;
1087 /* Make sure MediaType is OK */
1088 Dmsg3(dbglvl, "jid=%u chk MediaType device=%s request=%s\n",
1089 (int)rctx.jcr->JobId,
1090 rctx.device->media_type, rctx.store->media_type);
1091 if (strcmp(rctx.device->media_type, rctx.store->media_type) != 0) {
1095 /* Make sure device exists -- i.e. we can stat() it */
1096 if (!rctx.device->dev) {
1097 rctx.device->dev = init_dev(rctx.jcr, rctx.device);
1099 if (!rctx.device->dev) {
1100 if (rctx.device->changer_res) {
1101 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
1102 " Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
1103 rctx.device->hdr.name, rctx.device_name);
1105 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
1106 " Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
1109 return -1; /* no use waiting */
1112 rctx.suitable_device = true;
1113 Dmsg2(dbglvl, "jid=%u try reserve %s\n", rctx.jcr->JobId, rctx.device->hdr.name);
1114 rctx.jcr->dcr = dcr = new_dcr(rctx.jcr, rctx.jcr->dcr, rctx.device->dev);
1116 BSOCK *dir = rctx.jcr->dir_bsock;
1117 dir->fsend(_("3926 Could not get dcr for device: %s\n"), rctx.device_name);
1118 Dmsg1(dbglvl, ">dird: %s", dir->msg);
1121 bstrncpy(dcr->pool_name, rctx.store->pool_name, name_len);
1122 bstrncpy(dcr->pool_type, rctx.store->pool_type, name_len);
1123 bstrncpy(dcr->media_type, rctx.store->media_type, name_len);
1124 bstrncpy(dcr->dev_name, rctx.device_name, name_len);
1125 if (rctx.store->append == SD_APPEND) {
1126 Dmsg3(dbglvl, "jid=%u have_vol=%d vol=%s\n", (int)rctx.jcr->JobId,
1127 rctx.have_volume, rctx.VolumeName);
1128 ok = reserve_device_for_append(dcr, rctx);
1133 rctx.jcr->dcr = dcr;
1134 Dmsg6(dbglvl, "jid=%u Reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
1135 (int)rctx.jcr->JobId,
1136 dcr->dev->reserved_device,
1137 dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
1138 if (!rctx.have_volume) {
1139 dcr->any_volume = true;
1140 if (dir_find_next_appendable_volume(dcr)) {
1141 bstrncpy(rctx.VolumeName, dcr->VolumeName, sizeof(rctx.VolumeName));
1142 Dmsg2(dbglvl, "jid=%u looking for Volume=%s\n", (int)rctx.jcr->JobId, rctx.VolumeName);
1143 rctx.have_volume = true;
1145 Dmsg1(dbglvl, "jid=%u No next volume found\n", (int)rctx.jcr->JobId);
1146 rctx.have_volume = false;
1147 rctx.VolumeName[0] = 0;
1149 * If there is at least one volume that is valid and in use,
1150 * but we get here, check if we are running with prefers
1151 * non-mounted drives. In that case, we have selected a
1152 * non-used drive and our one and only volume is mounted
1153 * elsewhere, so we bail out and retry using that drive.
1155 if (dcr->volume_in_use && !rctx.PreferMountedVols) {
1156 rctx.PreferMountedVols = true;
1162 ok = reserve_device_for_read(dcr);
1164 rctx.jcr->read_dcr = dcr;
1165 Dmsg6(dbglvl, "jid=%u Read reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
1166 (int)rctx.jcr->JobId,
1167 dcr->dev->reserved_device,
1168 dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
1174 if (rctx.notify_dir) {
1176 BSOCK *dir = rctx.jcr->dir_bsock;
1177 pm_strcpy(dev_name, rctx.device->hdr.name);
1178 bash_spaces(dev_name);
1179 ok = dir->fsend(OK_device, dev_name.c_str()); /* Return real device name */
1180 Dmsg2(dbglvl, "jid=%u >dird changer: %s", jid(), dir->msg);
1187 rctx.have_volume = false;
1189 Dmsg1(dbglvl, "jid=%u Not OK.\n", (int)rctx.jcr->JobId);
1194 * We "reserve" the drive by setting the ST_READ bit. No one else
1195 * should touch the drive until that is cleared.
1196 * This allows the DIR to "reserve" the device before actually
1199 static bool reserve_device_for_read(DCR *dcr)
1201 DEVICE *dev = dcr->dev;
1202 JCR *jcr = dcr->jcr;
1209 if (is_device_unmounted(dev)) {
1210 Dmsg2(dbglvl, "jid=%u Device %s is BLOCKED due to user unmount.\n",
1211 (int)jcr->JobId, dev->print_name());
1212 Mmsg(jcr->errmsg, _("3601 JobId=%u device %s is BLOCKED due to user unmount.\n"),
1213 jcr->JobId, dev->print_name());
1214 queue_reserve_message(jcr);
1218 if (dev->is_busy()) {
1219 Dmsg5(dbglvl, "jid=%u Device %s is busy ST_READ=%d num_writers=%d reserved=%d.\n",
1220 (int)jcr->JobId, dev->print_name(),
1221 dev->state & ST_READ?1:0, dev->num_writers, dev->reserved_device);
1222 Mmsg(jcr->errmsg, _("3602 JobId=%u device %s is busy (already reading/writing).\n"),
1223 jcr->JobId, dev->print_name());
1224 queue_reserve_message(jcr);
1228 dev->clear_append();
1231 dev->reserved_device++;
1232 Dmsg4(dbglvl, "jid=%u Inc reserve=%d dev=%s %p\n", (int)jcr->JobId,
1233 dev->reserved_device, dev->print_name(), dev);
1234 dcr->reserved_device = true;
1243 * We reserve the device for appending by incrementing the
1244 * reserved_device. We do virtually all the same work that
1245 * is done in acquire_device_for_append(), but we do
1246 * not attempt to mount the device. This routine allows
1247 * the DIR to reserve multiple devices before *really*
1248 * starting the job. It also permits the SD to refuse
1249 * certain devices (not up, ...).
1251 * Note, in reserving a device, if the device is for the
1252 * same pool and the same pool type, then it is acceptable.
1253 * The Media Type has already been checked. If we are
1254 * the first tor reserve the device, we put the pool
1255 * name and pool type in the device record.
1257 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx)
1259 JCR *jcr = dcr->jcr;
1260 DEVICE *dev = dcr->dev;
1267 /* If device is being read, we cannot write it */
1268 if (dev->can_read()) {
1269 Mmsg(jcr->errmsg, _("3603 JobId=%u device %s is busy reading.\n"),
1270 jcr->JobId, dev->print_name());
1271 Dmsg2(dbglvl, "jid=%u %s", jid(), jcr->errmsg);
1272 queue_reserve_message(jcr);
1276 /* If device is unmounted, we are out of luck */
1277 if (is_device_unmounted(dev)) {
1278 Mmsg(jcr->errmsg, _("3604 JobId=%u device %s is BLOCKED due to user unmount.\n"),
1279 jcr->JobId, dev->print_name());
1280 Dmsg2(dbglvl, "jid=%u %s", jid(), jcr->errmsg);
1281 queue_reserve_message(jcr);
1285 Dmsg2(dbglvl, "jid=%u reserve_append device is %s\n",
1286 (int)jcr->JobId, dev->print_name());
1288 /* Now do detailed tests ... */
1289 if (can_reserve_drive(dcr, rctx) != 1) {
1290 Dmsg1(dbglvl, "jid=%u can_reserve_drive!=1\n", (int)jcr->JobId);
1294 dev->reserved_device++;
1295 Dmsg4(dbglvl, "jid=%u Inc reserve=%d dev=%s %p\n", (int)jcr->JobId, dev->reserved_device,
1296 dev->print_name(), dev);
1297 dcr->reserved_device = true;
1306 * Returns: 1 if drive can be reserved
1307 * 0 if we should wait
1308 * -1 on error or impossibility
1310 static int can_reserve_drive(DCR *dcr, RCTX &rctx)
1312 DEVICE *dev = dcr->dev;
1313 JCR *jcr = dcr->jcr;
1315 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
1317 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
1318 rctx.autochanger_only, rctx.any_drive);
1320 /* setting any_drive overrides PreferMountedVols flag */
1321 if (!rctx.any_drive) {
1323 * When PreferMountedVols is set, we keep track of the
1324 * drive in use that has the least number of writers, then if
1325 * no unmounted drive is found, we try that drive. This
1326 * helps spread the load to the least used drives.
1328 if (rctx.try_low_use_drive && dev == rctx.low_use_drive) {
1329 Dmsg3(dbglvl, "jid=%u OK dev=%s == low_drive=%s.\n",
1330 jcr->JobId, dev->print_name(), rctx.low_use_drive->print_name());
1333 /* If he wants a free drive, but this one is busy, no go */
1334 if (!rctx.PreferMountedVols && dev->is_busy()) {
1335 /* Save least used drive */
1336 if ((dev->num_writers + dev->reserved_device) < rctx.num_writers) {
1337 rctx.num_writers = dev->num_writers + dev->reserved_device;
1338 rctx.low_use_drive = dev;
1339 Dmsg3(dbglvl, "jid=%u set low use drive=%s num_writers=%d\n",
1340 (int)jcr->JobId, dev->print_name(), rctx.num_writers);
1342 Dmsg2(dbglvl, "jid=%u not low use num_writers=%d\n",
1343 (int)jcr->JobId, dev->num_writers+dev->reserved_device);
1345 Dmsg1(dbglvl, "jid=%u failed: !prefMnt && busy.\n", jcr->JobId);
1346 Mmsg(jcr->errmsg, _("3605 JobId=%u wants free drive but device %s is busy.\n"),
1347 jcr->JobId, dev->print_name());
1348 queue_reserve_message(jcr);
1352 /* Check for prefer mounted volumes */
1353 if (rctx.PreferMountedVols && !dev->vol && dev->is_tape()) {
1354 Mmsg(jcr->errmsg, _("3606 JobId=%u prefers mounted drives, but drive %s has no Volume.\n"),
1355 jcr->JobId, dev->print_name());
1356 queue_reserve_message(jcr);
1357 Dmsg1(dbglvl, "jid=%u failed: want mounted -- no vol\n", (uint32_t)jcr->JobId);
1358 return 0; /* No volume mounted */
1361 /* Check for exact Volume name match */
1362 /* ***FIXME*** for Disk, we can accept any volume that goes with this
1365 if (rctx.exact_match && rctx.have_volume) {
1367 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
1369 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
1370 rctx.autochanger_only, rctx.any_drive);
1371 Dmsg5(dbglvl, "jid=%u have_vol=%d have=%s resvol=%s want=%s\n",
1372 (int)jcr->JobId, rctx.have_volume, dev->VolHdr.VolumeName,
1373 dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1374 ok = strcmp(dev->VolHdr.VolumeName, rctx.VolumeName) == 0 ||
1375 (dev->vol && strcmp(dev->vol->vol_name, rctx.VolumeName) == 0);
1377 Mmsg(jcr->errmsg, _("3607 JobId=%u wants Vol=\"%s\" drive has Vol=\"%s\" on drive %s.\n"),
1378 jcr->JobId, rctx.VolumeName, dev->VolHdr.VolumeName,
1380 queue_reserve_message(jcr);
1381 Dmsg4(dbglvl, "jid=%u not OK: dev have=%s resvol=%s want=%s\n",
1382 (int)jcr->JobId, dev->VolHdr.VolumeName,
1383 dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1386 if (is_volume_in_use(dcr)) {
1387 return 0; /* fail if volume on another drive */
1392 /* Check for unused autochanger drive */
1393 if (rctx.autochanger_only && !dev->is_busy() &&
1394 dev->VolHdr.VolumeName[0] == 0) {
1395 /* Device is available but not yet reserved, reserve it for us */
1396 Dmsg2(dbglvl, "jid=%u OK Res Unused autochanger %s.\n",
1397 jcr->JobId, dev->print_name());
1398 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1399 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1400 return 1; /* reserve drive */
1404 * Handle the case that there are no writers
1406 if (dev->num_writers == 0) {
1407 /* Now check if there are any reservations on the drive */
1408 if (dev->reserved_device) {
1409 /* Now check if we want the same Pool and pool type */
1410 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
1411 strcmp(dev->pool_type, dcr->pool_type) == 0) {
1412 /* OK, compatible device */
1413 Dmsg2(dbglvl, "jid=%u OK dev: %s num_writers=0, reserved, pool matches\n",
1414 jcr->JobId, dev->print_name());
1417 /* Drive Pool not suitable for us */
1418 Mmsg(jcr->errmsg, _(
1419 "3608 JobId=%u wants Pool=\"%s\" but have Pool=\"%s\" nreserve=%d on drive %s.\n"),
1420 jcr->JobId, dcr->pool_name, dev->pool_name,
1421 dev->reserved_device, dev->print_name());
1422 queue_reserve_message(jcr);
1423 Dmsg3(dbglvl, "jid=%u failed: busy num_writers=0, reserved, pool=%s wanted=%s\n",
1424 (int)jcr->JobId, dev->pool_name, dcr->pool_name);
1425 return 0; /* wait */
1427 } else if (dev->can_append()) {
1428 /* Device in append mode, check if changing pool */
1429 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
1430 strcmp(dev->pool_type, dcr->pool_type) == 0) {
1431 Dmsg2(dbglvl, "jid=%u OK dev: %s num_writers=0, can_append, pool matches.\n",
1432 jcr->JobId, dev->print_name());
1433 /* OK, compatible device */
1436 /* Changing pool, unload old tape if any in drive */
1437 Dmsg1(dbglvl, "jid=%u OK dev: num_writers=0, not reserved, pool change, unload changer\n",
1439 unload_autochanger(dcr, 0);
1442 /* Device is available but not yet reserved, reserve it for us */
1443 Dmsg2(dbglvl, "jid=%u OK Dev avail reserved %s\n", jcr->JobId, dev->print_name());
1444 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1445 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1446 return 1; /* reserve drive */
1450 * Check if the device is in append mode with writers (i.e.
1451 * available if pool is the same).
1453 if (dev->can_append() || dev->num_writers > 0) {
1454 /* Yes, now check if we want the same Pool and pool type */
1455 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
1456 strcmp(dev->pool_type, dcr->pool_type) == 0) {
1457 Dmsg2(dbglvl, "jid=%u OK dev: %s num_writers>=0, can_append, pool matches.\n",
1458 jcr->JobId, dev->print_name());
1459 /* OK, compatible device */
1462 /* Drive Pool not suitable for us */
1463 Mmsg(jcr->errmsg, _("3609 JobId=%u wants Pool=\"%s\" but has Pool=\"%s\" on drive %s.\n"),
1464 jcr->JobId, dcr->pool_name, dev->pool_name, dev->print_name());
1465 queue_reserve_message(jcr);
1466 Dmsg3(dbglvl, "jid=%u failed: busy num_writers>0, can_append, pool=%s wanted=%s\n",
1467 (int)jcr->JobId, dev->pool_name, dcr->pool_name);
1468 return 0; /* wait */
1471 Pmsg1(000, _("Logic error!!!! JobId=%u Should not get here.\n"), (int)jcr->JobId);
1472 Mmsg(jcr->errmsg, _("3910 JobId=%u Logic error!!!! drive %s Should not get here.\n"),
1473 jcr->JobId, dev->print_name());
1474 queue_reserve_message(jcr);
1475 Jmsg0(jcr, M_FATAL, 0, _("Logic error!!!! Should not get here.\n"));
1476 return -1; /* error, should not get here */
1478 Mmsg(jcr->errmsg, _("3911 JobId=%u failed reserve drive %s.\n"),
1479 jcr->JobId, dev->print_name());
1480 queue_reserve_message(jcr);
1481 Dmsg2(dbglvl, "jid=%u failed: No reserve %s\n", jcr->JobId, dev->print_name());
1486 * search_lock is already set on entering this routine
1488 static void queue_reserve_message(JCR *jcr)
1491 alist *msgs = jcr->reserve_msgs;
1498 * Look for duplicate message. If found, do
1501 for (i=msgs->size()-1; i >= 0; i--) {
1502 msg = (char *)msgs->get(i);
1506 /* Comparison based on 4 digit message number */
1507 if (strncmp(msg, jcr->errmsg, 4) == 0) {
1511 /* Message unique, so insert it */
1512 jcr->reserve_msgs->push(bstrdup(jcr->errmsg));
1516 * Send any reservation messages queued for this jcr
1518 void send_drive_reserve_messages(JCR *jcr, void sendit(const char *msg, int len, void *sarg), void *arg)
1524 lock_reservations();
1525 msgs = jcr->reserve_msgs;
1526 if (!msgs || msgs->size() == 0) {
1529 for (i=msgs->size()-1; i >= 0; i--) {
1530 msg = (char *)msgs->get(i);
1532 sendit(" ", 3, arg);
1533 sendit(msg, strlen(msg), arg);
1540 unlock_reservations();