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() ((uint32_t)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);
57 static void pop_reserve_messages(JCR *jcr);
59 /* Requests from the Director daemon */
60 static char use_storage[] = "use storage=%127s media_type=%127s "
61 "pool_name=%127s pool_type=%127s append=%d copy=%d stripe=%d\n";
62 static char use_device[] = "use device=%127s\n";
64 /* Responses sent to Director daemon */
65 static char OK_device[] = "3000 OK use device device=%s\n";
66 static char NO_device[] = "3924 Device \"%s\" not in SD Device resources.\n";
67 static char BAD_use[] = "3913 Bad use command: %s\n";
69 bool use_cmd(JCR *jcr)
72 * Get the device, media, and pool information
74 if (!use_storage_cmd(jcr)) {
75 set_jcr_job_status(jcr, JS_ErrorTerminated);
76 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
82 static int my_compare(void *item1, void *item2)
84 return strcmp(((VOLRES *)item1)->vol_name, ((VOLRES *)item2)->vol_name);
88 void init_reservations_lock()
91 if ((errstat=rwl_init(&reservation_lock)) != 0) {
93 Emsg1(M_ABORT, 0, _("Unable to initialize reservation lock. ERR=%s\n"),
94 be.bstrerror(errstat));
97 if ((errstat=rwl_init(&vol_list_lock)) != 0) {
99 Emsg1(M_ABORT, 0, _("Unable to initialize volume list lock. ERR=%s\n"),
100 be.bstrerror(errstat));
104 void term_reservations_lock()
106 rwl_destroy(&reservation_lock);
107 rwl_destroy(&vol_list_lock);
110 int reservations_lock_count = 0;
112 /* This applies to a drive and to Volumes */
113 void _lock_reservations()
116 reservations_lock_count++;
117 if ((errstat=rwl_writelock(&reservation_lock)) != 0) {
119 Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
120 errstat, be.bstrerror(errstat));
124 void _unlock_reservations()
127 reservations_lock_count--;
128 if ((errstat=rwl_writeunlock(&reservation_lock)) != 0) {
130 Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
131 errstat, be.bstrerror(errstat));
135 int vol_list_lock_count = 0;
140 vol_list_lock_count++;
141 if ((errstat=rwl_writelock(&vol_list_lock)) != 0) {
143 Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
144 errstat, be.bstrerror(errstat));
148 void _unlock_volumes()
151 vol_list_lock_count--;
152 if ((errstat=rwl_writeunlock(&vol_list_lock)) != 0) {
154 Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
155 errstat, be.bstrerror(errstat));
161 * List Volumes -- this should be moved to status.c
168 static void debug_list_volumes(const char *imsg)
171 POOL_MEM msg(PM_MESSAGE);
174 foreach_dlist(vol, vol_list) {
176 Mmsg(msg, "List from %s: %s at %p on device %s\n", imsg,
177 vol->vol_name, vol->vol_name, vol->dev->print_name());
179 Mmsg(msg, "List from %s: %s at %p no dev\n", imsg, vol->vol_name, vol->vol_name);
181 Dmsg2(dbglvl, "jid=%u %s", jid(), msg.c_str());
186 foreach_dlist(vol, vol_list) {
187 if (vol->dev == dev) {
188 Dmsg0(dbglvl, "Two Volumes on same device.\n");
195 // Dmsg2(dbglvl, "List from %s: %d volumes\n", imsg, count);
201 * List Volumes -- this should be moved to status.c
203 void list_volumes(void sendit(const char *msg, int len, void *sarg), void *arg)
206 POOL_MEM msg(PM_MESSAGE);
210 foreach_dlist(vol, vol_list) {
211 DEVICE *dev = vol->dev;
213 len = Mmsg(msg, "%s on device %s\n", vol->vol_name, dev->print_name());
214 sendit(msg.c_str(), len, arg);
215 len = Mmsg(msg, " Reader=%d writers=%d reserved=%d\n", dev->can_read()?1:0,
216 dev->num_writers, dev->reserved_device);
217 sendit(msg.c_str(), len, arg);
219 len = Mmsg(msg, "%s no dev\n", vol->vol_name);
220 sendit(msg.c_str(), len, arg);
227 * Create a Volume item to put in the Volume list
228 * Ensure that the device points to it.
230 static VOLRES *new_vol_item(DCR *dcr, const char *VolumeName)
233 vol = (VOLRES *)malloc(sizeof(VOLRES));
234 memset(vol, 0, sizeof(VOLRES));
235 vol->vol_name = bstrdup(VolumeName);
237 Dmsg4(dbglvl, "jid=%u new Vol=%s at %p dev=%s\n", (int)dcr->jcr->JobId,
238 VolumeName, vol->vol_name, vol->dev->print_name());
242 static void free_vol_item(VOLRES *vol)
246 vol->dev->vol = NULL;
253 * Put a new Volume entry in the Volume list. This
254 * effectively reserves the volume so that it will
255 * not be mounted again.
257 * If the device has any current volume associated with it,
258 * and it is a different Volume, and the device is not busy,
259 * we release the old Volume item and insert the new one.
261 * It is assumed that the device is free and locked so that
262 * we can change the device structure.
264 * Some details of the Volume list handling:
266 * 1. The Volume list entry must be attached to the drive (rather than
267 * attached to a job as it currently is. I.e. the drive that "owns"
268 * the volume (reserved, in use, mounted)
269 * must point to the volume (still to be maintained in a list).
271 * 2. The Volume is entered in the list when a drive is reserved.
273 * 3. When a drive is in use, the device code must appropriately update the
274 * volume name as it changes (currently the list is static -- an entry is
275 * removed when the Volume is no longer reserved, in use or mounted).
276 * The new code must keep the same list entry as long as the drive
277 * has any volume associated with it but the volume name in the list
278 * must be updated when the drive has a different volume mounted.
280 * 4. A job that has reserved a volume, can un-reserve the volume, and if the
281 * volume is not mounted, and not reserved, and not in use, it will be
282 * removed from the list.
284 * 5. If a job wants to reserve a drive with a different Volume from the one on
285 * the drive, it can re-use the drive for the new Volume.
287 * 6. If a job wants a Volume that is in a different drive, it can either use the
288 * other drive or take the volume, only if the other drive is not in use or
291 * One nice aspect of this is that the reserve use count and the writer use count
292 * already exist and are correctly programmed and will need no changes -- use
293 * counts are always very tricky.
295 * The old code had a concept of "reserving" a Volume, but was changed
296 * to reserving and using a drive. A volume is must be attached to (owned by) a
297 * drive and can move from drive to drive or be unused given certain specific
298 * conditions of the drive. The key is that the drive must "own" the Volume.
299 * The old code had the job (dcr) owning the volume (more or less). The job was
300 * to change the insertion and removal of the volumes from the list to be based
301 * on the drive rather than the job.
303 * Return: VOLRES entry on success
304 * NULL volume busy on another drive
306 VOLRES *reserve_volume(DCR *dcr, const char *VolumeName)
309 DEVICE *dev = dcr->dev;
313 Dmsg2(dbglvl, "jid=%u reserve_volume %s\n", jid(), VolumeName);
315 * We lock the reservations system here to ensure
316 * when adding a new volume that no newly scheduled
317 * job can reserve it.
320 debug_list_volumes("begin reserve_volume");
322 * First, remove any old volume attached to this device as it
328 * Make sure we don't remove the current volume we are inserting
329 * because it was probably inserted by another job.
331 if (strcmp(vol->vol_name, VolumeName) == 0) {
332 goto get_out; /* Volume already on this device */
334 if (dev->is_busy() && dev->VolHdr.VolumeName[0]) {
338 Dmsg3(dbglvl, "jid=%u reserve_vol free vol=%s at %p\n",
339 (int)dcr->jcr->JobId, vol->vol_name, vol->vol_name);
340 debug_list_volumes("reserve_vol free");
341 vol_list->remove(vol);
346 /* Create a new Volume entry */
347 nvol = new_vol_item(dcr, VolumeName);
350 * Now try to insert the new Volume
352 vol = (VOLRES *)vol_list->binary_insert(nvol, my_compare);
354 Dmsg3(dbglvl, "jid=%u Found vol=%s dev-same=%d\n", jid(), vol->vol_name, dev==vol->dev);
356 * At this point, a Volume with this name already is in the list,
357 * so we simply release our new Volume entry. Note, this should
358 * only happen if we are moving the volume from one drive to another.
360 Dmsg3(dbglvl, "jid=%u reserve_vol free-tmp vol=%s at %p\n",
361 (int)dcr->jcr->JobId, vol->vol_name, vol->vol_name);
363 * Clear dev pointer so that free_vol_item() doesn't
364 * take away our volume.
366 nvol->dev = NULL; /* don't zap dev entry */
369 /* Check if we are trying to use the Volume on a different drive */
370 if (dev != vol->dev) {
371 /* Caller wants to switch Volume to another device */
372 if (!vol->dev->is_busy()) {
373 /* OK to move it -- I'm not sure this will work */
374 Dmsg4(dbglvl, "==== jid=%u Swap vol=%s from dev=%s to %s\n", jid(), VolumeName,
375 vol->dev->print_name(), dev->print_name());
376 vol->dev->vol = NULL; /* take vol from old drive */
377 vol->dev->VolHdr.VolumeName[0] = 0;
378 vol->dev = dev; /* point vol at new drive */
379 dev->vol = vol; /* point dev at vol */
380 dev->VolHdr.VolumeName[0] = 0;
382 Dmsg4(dbglvl, "jid=%u Volume busy could not swap vol=%s from dev=%s to %s\n",
383 jid(), VolumeName, vol->dev->print_name(), dev->print_name());
384 vol = NULL; /* device busy */
391 debug_list_volumes("end new volume");
397 * Search for a Volume name in the Volume list.
399 * Returns: VOLRES entry on success
400 * NULL if the Volume is not in the list
402 VOLRES *find_volume(DCR *dcr)
405 /* Do not lock reservations here */
407 vol.vol_name = bstrdup(dcr->VolumeName);
408 fvol = (VOLRES *)vol_list->binary_search(&vol, my_compare);
410 Dmsg3(dbglvl, "jid=%u find_vol=%s found=%d\n", jid(), dcr->VolumeName, fvol!=NULL);
411 debug_list_volumes("find_volume");
417 * Remove any reservation from a drive and tell the system
418 * that the volume is unused at least by us.
420 void unreserve_device(DCR *dcr)
422 DEVICE *dev = dcr->dev;
423 if (dcr->reserved_device) {
424 dcr->reserved_device = false;
425 dev->reserved_device--;
426 Dmsg3(dbglvl, "jid=%u Dec reserve=%d dev=%s\n", jid(), dev->reserved_device, dev->print_name());
427 dcr->reserved_device = false;
428 /* If we set read mode in reserving, remove it */
429 if (dev->can_read()) {
432 if (dev->num_writers < 0) {
433 Jmsg1(dcr->jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
434 dev->num_writers = 0;
442 * Free a Volume from the Volume list if it is no longer used
444 * Returns: true if the Volume found and removed from the list
445 * false if the Volume is not in the list or is in use
447 bool volume_unused(DCR *dcr)
449 DEVICE *dev = dcr->dev;
451 if (dev->vol == NULL) {
452 Dmsg2(dbglvl, "jid=%u vol_unused: no vol on %s\n", (int)dcr->jcr->JobId, dev->print_name());
453 debug_list_volumes("null vol cannot unreserve_volume");
457 if (dev->is_busy()) {
458 Dmsg2(dbglvl, "jid=%u vol_unused: no vol on %s\n", (int)dcr->jcr->JobId, dev->print_name());
459 debug_list_volumes("dev busy cannot unreserve_volume");
464 * If this is a tape, we do not free the volume, rather we wait
465 * until the autoloader unloads it, or until another tape is
466 * explicitly read in this drive. This allows the SD to remember
467 * where the tapes are or last were.
469 if (dev->is_tape() || dev->is_autochanger()) {
472 return free_volume(dev);
477 * Unconditionally release the volume
479 bool free_volume(DEVICE *dev)
483 if (dev->vol == NULL) {
484 Dmsg2(dbglvl, "jid=%u No vol on dev %s\n", jid(), dev->print_name());
490 vol_list->remove(vol);
491 Dmsg3(dbglvl, "jid=%u free_volume %s dev=%s\n", jid(), vol->vol_name, dev->print_name());
493 debug_list_volumes("free_volume");
499 /* Create the Volume list */
500 void create_volume_list()
503 if (vol_list == NULL) {
504 vol_list = New(dlist(vol, &vol->link));
508 /* Release all Volumes from the list */
509 void free_volume_list()
516 foreach_dlist(vol, vol_list) {
518 Dmsg3(dbglvl, "jid=%u free vol_list Volume=%s dev=%s\n", jid(),
519 vol->vol_name, vol->dev->print_name());
521 Dmsg3(dbglvl, "jid=%u free vol_list Volume=%s dev=%p\n", jid(),
522 vol->vol_name, vol->dev);
525 vol->vol_name = NULL;
532 bool is_volume_in_use(DCR *dcr)
534 VOLRES *vol = find_volume(dcr);
536 Dmsg2(dbglvl, "jid=%u Vol=%s not in use.\n", jid(), dcr->VolumeName);
537 return false; /* vol not in list */
539 ASSERT(vol->dev != NULL);
541 if (dcr->dev == vol->dev) { /* same device OK */
542 Dmsg2(dbglvl, "jid=%u Vol=%s on same dev.\n", jid(), dcr->VolumeName);
545 Dmsg4(dbglvl, "jid=%u Vol=%s on %s we have %s\n", jid(), dcr->VolumeName,
546 vol->dev->print_name(), dcr->dev->print_name());
548 if (!vol->dev->is_busy()) {
549 Dmsg3(dbglvl, "jid=%u Vol=%s dev=%s not busy.\n", jid(), dcr->VolumeName, vol->dev->print_name());
552 Dmsg3(dbglvl, "jid=%u Vol=%s dev=%s busy.\n", jid(), dcr->VolumeName, vol->dev->print_name());
554 Dmsg3(dbglvl, "jid=%u Vol=%s in use by %s.\n", jid(), dcr->VolumeName, vol->dev->print_name());
560 * We get the following type of information:
562 * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=1 copy=0 strip=0
566 * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=0 copy=0 strip=0
570 static bool use_storage_cmd(JCR *jcr)
572 POOL_MEM store_name, dev_name, media_type, pool_name, pool_type;
573 BSOCK *dir = jcr->dir_bsock;
581 memset(&rctx, 0, sizeof(RCTX));
584 * If there are multiple devices, the director sends us
585 * use_device for each device that it wants to use.
587 dirstore = New(alist(10, not_owned_by_alist));
588 jcr->reserve_msgs = New(alist(10, not_owned_by_alist));
590 Dmsg2(dbglvl, "jid=%u <dird: %s", jid(), dir->msg);
591 ok = sscanf(dir->msg, use_storage, store_name.c_str(),
592 media_type.c_str(), pool_name.c_str(),
593 pool_type.c_str(), &append, &Copy, &Stripe) == 7;
598 jcr->write_store = dirstore;
600 jcr->read_store = dirstore;
602 rctx.append = append;
603 unbash_spaces(store_name);
604 unbash_spaces(media_type);
605 unbash_spaces(pool_name);
606 unbash_spaces(pool_type);
607 store = new DIRSTORE;
608 dirstore->append(store);
609 memset(store, 0, sizeof(DIRSTORE));
610 store->device = New(alist(10));
611 bstrncpy(store->name, store_name, sizeof(store->name));
612 bstrncpy(store->media_type, media_type, sizeof(store->media_type));
613 bstrncpy(store->pool_name, pool_name, sizeof(store->pool_name));
614 bstrncpy(store->pool_type, pool_type, sizeof(store->pool_type));
615 store->append = append;
617 /* Now get all devices */
618 while (dir->recv() >= 0) {
619 Dmsg2(dbglvl, "jid=%u <dird device: %s", jid(), dir->msg);
620 ok = sscanf(dir->msg, use_device, dev_name.c_str()) == 1;
624 unbash_spaces(dev_name);
625 store->device->append(bstrdup(dev_name.c_str()));
627 } while (ok && dir->recv() >= 0);
629 /* Developer debug code */
631 if (debug_level >= dbglvl) {
632 foreach_alist(store, dirstore) {
633 Dmsg6(dbglvl, "jid=%u Storage=%s media_type=%s pool=%s pool_type=%s append=%d\n",
634 (int)rctx.jcr->JobId,
635 store->name, store->media_type, store->pool_name,
636 store->pool_type, store->append);
637 foreach_alist(device_name, store->device) {
638 Dmsg2(dbglvl, "jid=%u Device=%s\n", jid(), device_name);
643 init_jcr_device_wait_timers(jcr);
644 jcr->dcr = new_dcr(jcr, NULL, NULL); /* get a dcr */
646 BSOCK *dir = jcr->dir_bsock;
647 dir->fsend(_("3939 Could not get dcr\n"));
648 Dmsg1(dbglvl, ">dird: %s", dir->msg);
652 * At this point, we have a list of all the Director's Storage
653 * resources indicated for this Job, which include Pool, PoolType,
654 * storage name, and Media type.
655 * Then for each of the Storage resources, we have a list of
656 * device names that were given.
658 * Wiffle through them and find one that can do the backup.
661 int wait_for_device_retries = 0;
664 rctx.notify_dir = true;
667 for ( ; !fail && !job_canceled(jcr); ) {
668 pop_reserve_messages(jcr);
669 rctx.suitable_device = false;
670 rctx.have_volume = false;
671 rctx.VolumeName[0] = 0;
672 rctx.any_drive = false;
673 if (!jcr->PreferMountedVols) {
675 * Here we try to find a drive that is not used.
676 * This will maximize the use of available drives.
679 rctx.num_writers = 20000000; /* start with impossible number */
680 rctx.low_use_drive = NULL;
681 rctx.PreferMountedVols = false;
682 rctx.exact_match = false;
683 rctx.autochanger_only = true;
684 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
685 (int)rctx.jcr->JobId,
686 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
687 rctx.autochanger_only, rctx.any_drive);
688 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
691 /* Look through all drives possibly for low_use drive */
692 if (rctx.low_use_drive) {
693 rctx.try_low_use_drive = true;
694 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
697 rctx.try_low_use_drive = false;
699 rctx.autochanger_only = false;
700 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
701 (int)rctx.jcr->JobId,
702 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
703 rctx.autochanger_only, rctx.any_drive);
704 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
709 * Now we look for a drive that may or may not be in
712 /* Look for an exact Volume match all drives */
713 rctx.PreferMountedVols = true;
714 rctx.exact_match = true;
715 rctx.autochanger_only = false;
716 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
717 (int)rctx.jcr->JobId,
718 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
719 rctx.autochanger_only, rctx.any_drive);
720 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
723 /* Look for any mounted drive */
724 rctx.exact_match = false;
725 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
726 (int)rctx.jcr->JobId,
727 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
728 rctx.autochanger_only, rctx.any_drive);
729 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
733 rctx.any_drive = true;
734 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
735 (int)rctx.jcr->JobId,
736 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
737 rctx.autochanger_only, rctx.any_drive);
738 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
741 /* Keep reservations locked *except* during wait_for_device() */
742 unlock_reservations();
744 * The idea of looping on repeat a few times it to ensure
745 * that if there is some subtle timing problem between two
746 * jobs, we will simply try again, and most likely succeed.
747 * This can happen if one job reserves a drive or finishes using
748 * a drive at the same time a second job wants it.
750 if (repeat++ > 1) { /* try algorithm 3 times */
751 bmicrosleep(30, 0); /* wait a bit */
752 Dmsg1(dbglvl, "jid=%u repeat reserve algorithm\n", (int)rctx.jcr->JobId);
753 } else if (!rctx.suitable_device || !wait_for_device(jcr, wait_for_device_retries)) {
754 Dmsg1(dbglvl, "jid=%u Fail. !suitable_device || !wait_for_device\n",
755 (int)rctx.jcr->JobId);
759 dir->signal(BNET_HEARTBEAT); /* Inform Dir that we are alive */
761 unlock_reservations();
764 * If we get here, there are no suitable devices available, which
765 * means nothing configured. If a device is suitable but busy
766 * with another Volume, we will not come here.
768 unbash_spaces(dir->msg);
769 pm_strcpy(jcr->errmsg, dir->msg);
770 Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
771 Jmsg(jcr, M_FATAL, 0, _("\n"
772 " Device \"%s\" with MediaType \"%s\" requested by DIR not found in SD Device resources.\n"),
773 dev_name.c_str(), media_type.c_str());
774 dir->fsend(NO_device, dev_name.c_str());
776 Dmsg2(dbglvl, "jid=%u >dird: %s", jid(), dir->msg);
779 unbash_spaces(dir->msg);
780 pm_strcpy(jcr->errmsg, dir->msg);
781 Jmsg(jcr, M_FATAL, 0, _("Failed command: %s\n"), jcr->errmsg);
782 dir->fsend(BAD_use, jcr->errmsg);
783 Dmsg2(dbglvl, "jid=%u >dird: %s", jid(), dir->msg);
786 release_reserve_messages(jcr);
792 * Walk through the autochanger resources and check if
793 * the volume is in one of them.
795 * Returns: true if volume is in device
798 static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol)
800 AUTOCHANGER *changer = vol->dev->device->changer_res;
802 /* Find resource, and make sure we were able to open it */
803 if (strcmp(rctx.device_name, changer->hdr.name) == 0) {
804 Dmsg2(dbglvl, "jid=%u Found changer device %s\n",
805 (int)rctx.jcr->JobId, vol->dev->device->hdr.name);
808 Dmsg2(dbglvl, "jid=%u Incorrect changer device %s\n",
809 (int)rctx.jcr->JobId, changer->hdr.name);
814 * Search for a device suitable for this job.
816 bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
825 dirstore = jcr->write_store;
827 dirstore = jcr->read_store;
829 Dmsg5(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d\n",
830 (int)rctx.jcr->JobId,
831 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
832 rctx.autochanger_only);
835 * If the appropriate conditions of this if are met, namely that
836 * we are appending and the user wants mounted drive (or we
837 * force try a mounted drive because they are all busy), we
838 * start by looking at all the Volumes in the volume list.
840 if (!vol_list->empty() && rctx.append && rctx.PreferMountedVols) {
841 dlist *temp_vol_list, *save_vol_list;
844 Dmsg1(dbglvl, "jid=%u lock volumes\n", (uint32_t)rctx.jcr->JobId);
847 * Create a temporary copy of the volume list. We do this,
848 * to avoid having the volume list locked during the
849 * call to reserve_device(), which would cause a deadlock.
850 * Note, we may want to add an update counter on the vol_list
851 * so that if it is modified while we are traversing the copy
852 * we can take note and act accordingly (probably redo the
853 * search at least a few times).
855 Dmsg1(dbglvl, "jid=%u duplicate vol list\n", (int)rctx.jcr->JobId);
856 temp_vol_list = New(dlist(vol, &vol->link));
857 foreach_dlist(vol, vol_list) {
859 VOLRES *tvol = (VOLRES *)malloc(sizeof(VOLRES));
860 memset(tvol, 0, sizeof(VOLRES));
861 tvol->vol_name = bstrdup(vol->vol_name);
862 tvol->dev = vol->dev;
863 nvol = (VOLRES *)temp_vol_list->binary_insert(tvol, my_compare);
865 tvol->dev = NULL; /* don't zap dev entry */
867 Pmsg0(000, "Logic error. Duplicating vol list hit duplicate.\n");
868 Jmsg(jcr, M_WARNING, 0, "Logic error. Duplicating vol list hit duplicate.\n");
871 Dmsg1(dbglvl, "jid=%u unlock volumes\n", (uint32_t)jcr->JobId);
874 /* Look through reserved volumes for one we can use */
875 Dmsg1(dbglvl, "jid=%u look for vol in vol list\n", (int)rctx.jcr->JobId);
876 foreach_dlist(vol, temp_vol_list) {
878 Dmsg2(dbglvl, "jid=%u vol=%s no dev\n", (int)rctx.jcr->JobId, vol->vol_name);
881 /* Check with Director if this Volume is OK */
882 bstrncpy(dcr->VolumeName, vol->vol_name, sizeof(dcr->VolumeName));
883 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
887 Dmsg2(dbglvl, "jid=%u vol=%s OK for this job\n", (int)rctx.jcr->JobId, vol->vol_name);
888 foreach_alist(store, dirstore) {
891 foreach_alist(device_name, store->device) {
892 /* Found a device, try to use it */
893 rctx.device_name = device_name;
894 rctx.device = vol->dev->device;
896 if (vol->dev->is_autochanger()) {
897 Dmsg2(dbglvl, "jid=%u vol=%s is in changer\n", (int)rctx.jcr->JobId,
899 if (!is_vol_in_autochanger(rctx, vol)) {
902 } else if (strcmp(device_name, vol->dev->device->hdr.name) != 0) {
903 Dmsg3(dbglvl, "jid=%u device=%s not suitable want %s\n", (int)rctx.jcr->JobId,
904 vol->dev->device->hdr.name, device_name);
908 bstrncpy(rctx.VolumeName, vol->vol_name, sizeof(rctx.VolumeName));
909 rctx.have_volume = true;
910 /* Try reserving this device and volume */
911 Dmsg3(dbglvl, "jid=%u try vol=%s on device=%s\n", (int)rctx.jcr->JobId,
912 rctx.VolumeName, device_name);
913 stat = reserve_device(rctx);
914 if (stat == 1) { /* found available device */
915 Dmsg2(dbglvl, "jid=%u Suitable device found=%s\n", (int)rctx.jcr->JobId,
919 } else if (stat == 0) { /* device busy */
920 Dmsg2(dbglvl, "jid=%u Suitable device=%s, busy: not use\n",
921 (int)rctx.jcr->JobId, device_name);
923 /* otherwise error */
924 Dmsg1(dbglvl, "jid=%u No suitable device found.\n", (int)rctx.jcr->JobId);
926 rctx.have_volume = false;
935 } /* end for loop over reserved volumes */
937 Dmsg1(dbglvl, "jid=%u lock volumes\n", (uint32_t)rctx.jcr->JobId);
939 save_vol_list = vol_list;
940 vol_list = temp_vol_list;
941 free_volume_list(); /* release temp_vol_list */
942 vol_list = save_vol_list;
943 Dmsg1(dbglvl, "jid=%u deleted temp vol list\n", (int)rctx.jcr->JobId);
944 Dmsg1(dbglvl, "jid=%u lock volumes\n", (uint32_t)rctx.jcr->JobId);
948 Dmsg2(dbglvl, "jid=%u got vol %s from in-use vols list\n", (int)rctx.jcr->JobId,
954 * No reserved volume we can use, so now search for an available device.
956 * For each storage device that the user specified, we
957 * search and see if there is a resource for that device.
959 foreach_alist(store, dirstore) {
961 foreach_alist(device_name, store->device) {
963 rctx.device_name = device_name;
964 stat = search_res_for_device(rctx);
965 if (stat == 1) { /* found available device */
966 Dmsg2(dbglvl, "jid=%u available device found=%s\n", (int)rctx.jcr->JobId,
970 } else if (stat == 0) { /* device busy */
971 Dmsg2(dbglvl, "jid=%u Suitable device=%s, busy: not use\n",
972 (int)rctx.jcr->JobId, device_name);
974 /* otherwise error */
975 Dmsg1(dbglvl, "jid=%u No suitable device found.\n", (int)rctx.jcr->JobId);
986 * Search for a particular storage device with particular storage
987 * characteristics (MediaType).
989 int search_res_for_device(RCTX &rctx)
991 AUTOCHANGER *changer;
994 Dmsg2(dbglvl, "jid=%u search res for %s\n", (int)rctx.jcr->JobId, rctx.device_name);
995 /* Look through Autochangers first */
996 foreach_res(changer, R_AUTOCHANGER) {
997 Dmsg2(dbglvl, "jid=%u Try match changer res=%s\n", (int)rctx.jcr->JobId, changer->hdr.name);
998 /* Find resource, and make sure we were able to open it */
999 if (strcmp(rctx.device_name, changer->hdr.name) == 0) {
1000 /* Try each device in this AutoChanger */
1001 foreach_alist(rctx.device, changer->device) {
1002 Dmsg2(dbglvl, "jid=%u Try changer device %s\n", (int)rctx.jcr->JobId,
1003 rctx.device->hdr.name);
1004 stat = reserve_device(rctx);
1005 if (stat != 1) { /* try another device */
1009 if (rctx.store->append == SD_APPEND) {
1010 Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for append.\n",
1011 (int)rctx.jcr->JobId, rctx.device->hdr.name,
1012 rctx.jcr->dcr->dev->reserved_device);
1014 Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for read.\n",
1015 (int)rctx.jcr->JobId, rctx.device->hdr.name,
1016 rctx.jcr->read_dcr->dev->reserved_device);
1023 /* Now if requested look through regular devices */
1024 if (!rctx.autochanger_only) {
1025 foreach_res(rctx.device, R_DEVICE) {
1026 Dmsg2(dbglvl, "jid=%u Try match res=%s\n", (int)rctx.jcr->JobId, rctx.device->hdr.name);
1027 /* Find resource, and make sure we were able to open it */
1028 if (strcmp(rctx.device_name, rctx.device->hdr.name) == 0) {
1029 stat = reserve_device(rctx);
1030 if (stat != 1) { /* try another device */
1034 if (rctx.store->append == SD_APPEND) {
1035 Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for append.\n",
1036 (int)rctx.jcr->JobId, rctx.device->hdr.name,
1037 rctx.jcr->dcr->dev->reserved_device);
1039 Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for read.\n",
1040 (int)rctx.jcr->JobId, rctx.device->hdr.name,
1041 rctx.jcr->read_dcr->dev->reserved_device);
1047 return -1; /* nothing found */
1051 * Try to reserve a specific device.
1053 * Returns: 1 -- OK, have DCR
1057 static int reserve_device(RCTX &rctx)
1061 const int name_len = MAX_NAME_LENGTH;
1063 /* Make sure MediaType is OK */
1064 Dmsg3(dbglvl, "jid=%u chk MediaType device=%s request=%s\n",
1065 (int)rctx.jcr->JobId,
1066 rctx.device->media_type, rctx.store->media_type);
1067 if (strcmp(rctx.device->media_type, rctx.store->media_type) != 0) {
1071 /* Make sure device exists -- i.e. we can stat() it */
1072 if (!rctx.device->dev) {
1073 rctx.device->dev = init_dev(rctx.jcr, rctx.device);
1075 if (!rctx.device->dev) {
1076 if (rctx.device->changer_res) {
1077 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
1078 " Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
1079 rctx.device->hdr.name, rctx.device_name);
1081 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
1082 " Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
1085 return -1; /* no use waiting */
1088 rctx.suitable_device = true;
1089 Dmsg2(dbglvl, "jid=%u try reserve %s\n", rctx.jcr->JobId, rctx.device->hdr.name);
1090 rctx.jcr->dcr = dcr = new_dcr(rctx.jcr, rctx.jcr->dcr, rctx.device->dev);
1092 BSOCK *dir = rctx.jcr->dir_bsock;
1093 dir->fsend(_("3926 Could not get dcr for device: %s\n"), rctx.device_name);
1094 Dmsg1(dbglvl, ">dird: %s", dir->msg);
1097 bstrncpy(dcr->pool_name, rctx.store->pool_name, name_len);
1098 bstrncpy(dcr->pool_type, rctx.store->pool_type, name_len);
1099 bstrncpy(dcr->media_type, rctx.store->media_type, name_len);
1100 bstrncpy(dcr->dev_name, rctx.device_name, name_len);
1101 if (rctx.store->append == SD_APPEND) {
1102 Dmsg3(dbglvl, "jid=%u have_vol=%d vol=%s\n", (int)rctx.jcr->JobId,
1103 rctx.have_volume, rctx.VolumeName);
1104 ok = reserve_device_for_append(dcr, rctx);
1109 rctx.jcr->dcr = dcr;
1110 Dmsg6(dbglvl, "jid=%u Reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
1111 (int)rctx.jcr->JobId,
1112 dcr->dev->reserved_device,
1113 dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
1114 if (!rctx.have_volume) {
1115 dcr->any_volume = true;
1116 if (dir_find_next_appendable_volume(dcr)) {
1117 bstrncpy(rctx.VolumeName, dcr->VolumeName, sizeof(rctx.VolumeName));
1118 Dmsg2(dbglvl, "jid=%u looking for Volume=%s\n", (int)rctx.jcr->JobId, rctx.VolumeName);
1119 rctx.have_volume = true;
1121 Dmsg1(dbglvl, "jid=%u No next volume found\n", (int)rctx.jcr->JobId);
1122 rctx.have_volume = false;
1123 rctx.VolumeName[0] = 0;
1125 * If there is at least one volume that is valid and in use,
1126 * but we get here, check if we are running with prefers
1127 * non-mounted drives. In that case, we have selected a
1128 * non-used drive and our one and only volume is mounted
1129 * elsewhere, so we bail out and retry using that drive.
1131 if (dcr->volume_in_use && !rctx.PreferMountedVols) {
1132 rctx.PreferMountedVols = true;
1133 if (dcr->VolumeName[0]) {
1138 if (dcr->dev->num_writers != 0) {
1139 if (dcr->VolumeName[0]) {
1147 ok = reserve_device_for_read(dcr);
1149 rctx.jcr->read_dcr = dcr;
1150 Dmsg6(dbglvl, "jid=%u Read reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
1151 (int)rctx.jcr->JobId,
1152 dcr->dev->reserved_device,
1153 dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
1159 if (rctx.notify_dir) {
1161 BSOCK *dir = rctx.jcr->dir_bsock;
1162 pm_strcpy(dev_name, rctx.device->hdr.name);
1163 bash_spaces(dev_name);
1164 ok = dir->fsend(OK_device, dev_name.c_str()); /* Return real device name */
1165 Dmsg2(dbglvl, "jid=%u >dird: %s", jid(), dir->msg);
1172 rctx.have_volume = false;
1174 Dmsg1(dbglvl, "jid=%u Not OK.\n", (int)rctx.jcr->JobId);
1179 * We "reserve" the drive by setting the ST_READ bit. No one else
1180 * should touch the drive until that is cleared.
1181 * This allows the DIR to "reserve" the device before actually
1184 static bool reserve_device_for_read(DCR *dcr)
1186 DEVICE *dev = dcr->dev;
1187 JCR *jcr = dcr->jcr;
1194 if (is_device_unmounted(dev)) {
1195 Dmsg2(dbglvl, "jid=%u Device %s is BLOCKED due to user unmount.\n",
1196 (int)jcr->JobId, dev->print_name());
1197 Mmsg(jcr->errmsg, _("3601 JobId=%u device %s is BLOCKED due to user unmount.\n"),
1198 jcr->JobId, dev->print_name());
1199 queue_reserve_message(jcr);
1203 if (dev->is_busy()) {
1204 Dmsg5(dbglvl, "jid=%u Device %s is busy ST_READ=%d num_writers=%d reserved=%d.\n",
1205 (int)jcr->JobId, dev->print_name(),
1206 dev->state & ST_READ?1:0, dev->num_writers, dev->reserved_device);
1207 Mmsg(jcr->errmsg, _("3602 JobId=%u device %s is busy (already reading/writing).\n"),
1208 jcr->JobId, dev->print_name());
1209 queue_reserve_message(jcr);
1213 dev->clear_append();
1216 dev->reserved_device++;
1217 Dmsg4(dbglvl, "jid=%u Inc reserve=%d dev=%s %p\n", (int)jcr->JobId,
1218 dev->reserved_device, dev->print_name(), dev);
1219 dcr->reserved_device = true;
1228 * We reserve the device for appending by incrementing the
1229 * reserved_device. We do virtually all the same work that
1230 * is done in acquire_device_for_append(), but we do
1231 * not attempt to mount the device. This routine allows
1232 * the DIR to reserve multiple devices before *really*
1233 * starting the job. It also permits the SD to refuse
1234 * certain devices (not up, ...).
1236 * Note, in reserving a device, if the device is for the
1237 * same pool and the same pool type, then it is acceptable.
1238 * The Media Type has already been checked. If we are
1239 * the first tor reserve the device, we put the pool
1240 * name and pool type in the device record.
1242 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx)
1244 JCR *jcr = dcr->jcr;
1245 DEVICE *dev = dcr->dev;
1252 /* If device is being read, we cannot write it */
1253 if (dev->can_read()) {
1254 Mmsg(jcr->errmsg, _("3603 JobId=%u device %s is busy reading.\n"),
1255 jcr->JobId, dev->print_name());
1256 Dmsg2(dbglvl, "jid=%u %s", jid(), jcr->errmsg);
1257 queue_reserve_message(jcr);
1261 /* If device is unmounted, we are out of luck */
1262 if (is_device_unmounted(dev)) {
1263 Mmsg(jcr->errmsg, _("3604 JobId=%u device %s is BLOCKED due to user unmount.\n"),
1264 jcr->JobId, dev->print_name());
1265 Dmsg2(dbglvl, "jid=%u %s", jid(), jcr->errmsg);
1266 queue_reserve_message(jcr);
1270 Dmsg2(dbglvl, "jid=%u reserve_append device is %s\n",
1271 (int)jcr->JobId, dev->print_name());
1273 /* Now do detailed tests ... */
1274 if (can_reserve_drive(dcr, rctx) != 1) {
1275 Dmsg1(dbglvl, "jid=%u can_reserve_drive!=1\n", (int)jcr->JobId);
1279 dev->reserved_device++;
1280 Dmsg4(dbglvl, "jid=%u Inc reserve=%d dev=%s %p\n", (int)jcr->JobId, dev->reserved_device,
1281 dev->print_name(), dev);
1282 dcr->reserved_device = true;
1290 static int is_pool_ok(DCR *dcr)
1292 DEVICE *dev = dcr->dev;
1293 JCR *jcr = dcr->jcr;
1295 /* Now check if we want the same Pool and pool type */
1296 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
1297 strcmp(dev->pool_type, dcr->pool_type) == 0) {
1298 /* OK, compatible device */
1299 Dmsg2(dbglvl, "jid=%u OK dev: %s num_writers=0, reserved, pool matches\n",
1300 jcr->JobId, dev->print_name());
1303 /* Drive Pool not suitable for us */
1304 Mmsg(jcr->errmsg, _(
1305 "3608 JobId=%u wants Pool=\"%s\" but have Pool=\"%s\" nreserve=%d on drive %s.\n"),
1306 jcr->JobId, dcr->pool_name, dev->pool_name,
1307 dev->reserved_device, dev->print_name());
1308 queue_reserve_message(jcr);
1309 Dmsg3(dbglvl, "jid=%u failed: busy num_writers=0, reserved, pool=%s wanted=%s\n",
1310 (int)jcr->JobId, dev->pool_name, dcr->pool_name);
1315 static bool is_max_jobs_ok(DCR *dcr)
1317 DEVICE *dev = dcr->dev;
1318 JCR *jcr = dcr->jcr;
1320 Dmsg5(dbglvl, "jid=%u MaxJobs=%d Jobs=%d reserves=%d Vol=%s\n",
1321 (uint32_t)jcr->JobId, dcr->VolCatInfo.VolCatMaxJobs,
1322 dcr->VolCatInfo.VolCatJobs, dev->reserved_device,
1324 if (dcr->VolCatInfo.VolCatMaxJobs > 0 && dcr->VolCatInfo.VolCatMaxJobs <=
1325 (dcr->VolCatInfo.VolCatJobs + dev->reserved_device)) {
1326 /* Max Job Vols depassed or already reserved */
1327 Mmsg(jcr->errmsg, _("3610 JobId=%u Volume max jobs exceeded on drive %s.\n"),
1328 (uint32_t)jcr->JobId, dev->print_name());
1329 queue_reserve_message(jcr);
1330 Dmsg2(dbglvl, "jid=%u reserve dev failed: %s", (uint32_t)jcr->JobId, jcr->errmsg);
1331 return false; /* wait */
1337 * Returns: 1 if drive can be reserved
1338 * 0 if we should wait
1339 * -1 on error or impossibility
1341 static int can_reserve_drive(DCR *dcr, RCTX &rctx)
1343 DEVICE *dev = dcr->dev;
1344 JCR *jcr = dcr->jcr;
1346 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
1348 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
1349 rctx.autochanger_only, rctx.any_drive);
1351 /* setting any_drive overrides PreferMountedVols flag */
1352 if (!rctx.any_drive) {
1354 * When PreferMountedVols is set, we keep track of the
1355 * drive in use that has the least number of writers, then if
1356 * no unmounted drive is found, we try that drive. This
1357 * helps spread the load to the least used drives.
1359 if (rctx.try_low_use_drive && dev == rctx.low_use_drive) {
1360 Dmsg3(dbglvl, "jid=%u OK dev=%s == low_drive=%s.\n",
1361 jcr->JobId, dev->print_name(), rctx.low_use_drive->print_name());
1364 /* If he wants a free drive, but this one is busy, no go */
1365 if (!rctx.PreferMountedVols && dev->is_busy()) {
1366 /* Save least used drive */
1367 if ((dev->num_writers + dev->reserved_device) < rctx.num_writers) {
1368 rctx.num_writers = dev->num_writers + dev->reserved_device;
1369 rctx.low_use_drive = dev;
1370 Dmsg3(dbglvl, "jid=%u set low use drive=%s num_writers=%d\n",
1371 (int)jcr->JobId, dev->print_name(), rctx.num_writers);
1373 Dmsg2(dbglvl, "jid=%u not low use num_writers=%d\n",
1374 (int)jcr->JobId, dev->num_writers+dev->reserved_device);
1376 Dmsg1(dbglvl, "jid=%u failed: !prefMnt && busy.\n", jcr->JobId);
1377 Mmsg(jcr->errmsg, _("3605 JobId=%u wants free drive but device %s is busy.\n"),
1378 jcr->JobId, dev->print_name());
1379 queue_reserve_message(jcr);
1383 /* Check for prefer mounted volumes */
1384 if (rctx.PreferMountedVols && !dev->vol && dev->is_tape()) {
1385 Mmsg(jcr->errmsg, _("3606 JobId=%u prefers mounted drives, but drive %s has no Volume.\n"),
1386 jcr->JobId, dev->print_name());
1387 queue_reserve_message(jcr);
1388 Dmsg1(dbglvl, "jid=%u failed: want mounted -- no vol\n", (uint32_t)jcr->JobId);
1389 return 0; /* No volume mounted */
1392 /* Check for exact Volume name match */
1393 /* ***FIXME*** for Disk, we can accept any volume that goes with this
1396 if (rctx.exact_match && rctx.have_volume) {
1398 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
1400 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
1401 rctx.autochanger_only, rctx.any_drive);
1402 Dmsg5(dbglvl, "jid=%u have_vol=%d have=%s resvol=%s want=%s\n",
1403 (int)jcr->JobId, rctx.have_volume, dev->VolHdr.VolumeName,
1404 dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1405 ok = strcmp(dev->VolHdr.VolumeName, rctx.VolumeName) == 0 ||
1406 (dev->vol && strcmp(dev->vol->vol_name, rctx.VolumeName) == 0);
1408 Mmsg(jcr->errmsg, _("3607 JobId=%u wants Vol=\"%s\" drive has Vol=\"%s\" on drive %s.\n"),
1409 jcr->JobId, rctx.VolumeName, dev->VolHdr.VolumeName,
1411 queue_reserve_message(jcr);
1412 Dmsg4(dbglvl, "jid=%u not OK: dev have=%s resvol=%s want=%s\n",
1413 (int)jcr->JobId, dev->VolHdr.VolumeName,
1414 dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1417 if (is_volume_in_use(dcr)) {
1418 return 0; /* fail if volume on another drive */
1423 /* Check for unused autochanger drive */
1424 if (rctx.autochanger_only && !dev->is_busy() &&
1425 dev->VolHdr.VolumeName[0] == 0) {
1426 /* Device is available but not yet reserved, reserve it for us */
1427 Dmsg2(dbglvl, "jid=%u OK Res Unused autochanger %s.\n",
1428 jcr->JobId, dev->print_name());
1429 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1430 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1431 return 1; /* reserve drive */
1435 * Handle the case that there are no writers
1437 if (dev->num_writers == 0) {
1438 /* Now check if there are any reservations on the drive */
1439 if (dev->reserved_device) {
1440 if (!is_max_jobs_ok(dcr)) {
1443 return is_pool_ok(dcr);
1444 } else if (dev->can_append()) {
1445 if (is_pool_ok(dcr)) {
1448 /* Changing pool, unload old tape if any in drive */
1449 Dmsg1(dbglvl, "jid=%u OK dev: num_writers=0, not reserved, pool change, unload changer\n",
1451 unload_autochanger(dcr, 0);
1454 /* Device is available but not yet reserved, reserve it for us */
1455 Dmsg2(dbglvl, "jid=%u OK Dev avail reserved %s\n", jcr->JobId, dev->print_name());
1456 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1457 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1458 return 1; /* reserve drive */
1462 * Check if the device is in append mode with writers (i.e.
1463 * available if pool is the same).
1465 if (dev->can_append() || dev->num_writers > 0) {
1466 if (!is_max_jobs_ok(dcr)) {
1469 return is_pool_ok(dcr);
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());
1489 * Queue a reservation error or failure message for this jcr
1491 static void queue_reserve_message(JCR *jcr)
1499 msgs = jcr->reserve_msgs;
1504 * Look for duplicate message. If found, do
1507 for (i=msgs->size()-1; i >= 0; i--) {
1508 msg = (char *)msgs->get(i);
1512 /* Comparison based on 4 digit message number */
1513 if (strncmp(msg, jcr->errmsg, 4) == 0) {
1517 /* Message unique, so insert it */
1518 jcr->reserve_msgs->push(bstrdup(jcr->errmsg));
1525 * Send any reservation messages queued for this jcr
1527 void send_drive_reserve_messages(JCR *jcr, void sendit(const char *msg, int len, void *sarg), void *arg)
1534 msgs = jcr->reserve_msgs;
1535 if (!msgs || msgs->size() == 0) {
1538 for (i=msgs->size()-1; i >= 0; i--) {
1539 msg = (char *)msgs->get(i);
1541 sendit(" ", 3, arg);
1542 sendit(msg, strlen(msg), arg);
1553 * Pop and release any reservations messages
1555 static void pop_reserve_messages(JCR *jcr)
1561 msgs = jcr->reserve_msgs;
1565 while ((msg = (char *)msgs->pop())) {
1573 * Also called from acquire.c
1575 void release_reserve_messages(JCR *jcr)
1577 pop_reserve_messages(jcr);
1579 if (!jcr->reserve_msgs) {
1582 delete jcr->reserve_msgs;
1583 jcr->reserve_msgs = NULL;