2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2008 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);
57 static void pop_reserve_messages(JCR *jcr);
58 //void switch_device(DCR *dcr, DEVICE *dev);
60 /* Requests from the Director daemon */
61 static char use_storage[] = "use storage=%127s media_type=%127s "
62 "pool_name=%127s pool_type=%127s append=%d copy=%d stripe=%d\n";
63 static char use_device[] = "use device=%127s\n";
65 /* Responses sent to Director daemon */
66 static char OK_device[] = "3000 OK use device device=%s\n";
67 static char NO_device[] = "3924 Device \"%s\" not in SD Device resources.\n";
68 static char BAD_use[] = "3913 Bad use command: %s\n";
70 bool use_cmd(JCR *jcr)
73 * Get the device, media, and pool information
75 if (!use_storage_cmd(jcr)) {
76 set_jcr_job_status(jcr, JS_ErrorTerminated);
77 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
83 static int my_compare(void *item1, void *item2)
85 return strcmp(((VOLRES *)item1)->vol_name, ((VOLRES *)item2)->vol_name);
89 * This allows a given thread to recursively call lock_reservations.
90 * It must, of course, call unlock_... the same number of times.
92 void init_reservations_lock()
95 if ((errstat=rwl_init(&reservation_lock)) != 0) {
97 Emsg1(M_ABORT, 0, _("Unable to initialize reservation lock. ERR=%s\n"),
98 be.bstrerror(errstat));
101 if ((errstat=rwl_init(&vol_list_lock)) != 0) {
103 Emsg1(M_ABORT, 0, _("Unable to initialize volume list lock. ERR=%s\n"),
104 be.bstrerror(errstat));
108 void term_reservations_lock()
110 rwl_destroy(&reservation_lock);
111 rwl_destroy(&vol_list_lock);
114 int reservations_lock_count = 0;
116 /* This applies to a drive and to Volumes */
117 void _lock_reservations()
120 reservations_lock_count++;
121 if ((errstat=rwl_writelock(&reservation_lock)) != 0) {
123 Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
124 errstat, be.bstrerror(errstat));
128 void _unlock_reservations()
131 reservations_lock_count--;
132 if ((errstat=rwl_writeunlock(&reservation_lock)) != 0) {
134 Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
135 errstat, be.bstrerror(errstat));
139 int vol_list_lock_count = 0;
142 * This allows a given thread to recursively call to lock_volumes()
147 vol_list_lock_count++;
148 if ((errstat=rwl_writelock(&vol_list_lock)) != 0) {
150 Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
151 errstat, be.bstrerror(errstat));
155 void _unlock_volumes()
158 vol_list_lock_count--;
159 if ((errstat=rwl_writeunlock(&vol_list_lock)) != 0) {
161 Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
162 errstat, be.bstrerror(errstat));
168 * List Volumes -- this should be moved to status.c
175 void debug_list_volumes(const char *imsg)
178 POOL_MEM msg(PM_MESSAGE);
181 foreach_dlist(vol, vol_list) {
183 Mmsg(msg, "List %s: %s in_use=%d on device %s\n", imsg,
184 vol->vol_name, vol->is_in_use(), vol->dev->print_name());
186 Mmsg(msg, "List %s: %s in_use=%d no dev\n", imsg, vol->vol_name,
189 Dmsg2(dbglvl, "jid=%u %s", jid(), msg.c_str());
197 * List Volumes -- this should be moved to status.c
199 void list_volumes(void sendit(const char *msg, int len, void *sarg), void *arg)
202 POOL_MEM msg(PM_MESSAGE);
206 foreach_dlist(vol, vol_list) {
207 DEVICE *dev = vol->dev;
209 len = Mmsg(msg, "%s on device %s\n", vol->vol_name, dev->print_name());
210 sendit(msg.c_str(), len, arg);
211 len = Mmsg(msg, " Reader=%d writers=%d devres=%d volinuse=%d\n",
212 dev->can_read()?1:0, dev->num_writers, dev->num_reserved(),
214 sendit(msg.c_str(), len, arg);
216 len = Mmsg(msg, "%s no device. volinuse= %d\n", vol->vol_name,
218 sendit(msg.c_str(), len, arg);
225 * Create a Volume item to put in the Volume list
226 * Ensure that the device points to it.
228 static VOLRES *new_vol_item(DCR *dcr, const char *VolumeName)
231 vol = (VOLRES *)malloc(sizeof(VOLRES));
232 memset(vol, 0, sizeof(VOLRES));
233 vol->vol_name = bstrdup(VolumeName);
235 Dmsg4(dbglvl, "jid=%u new Vol=%s at %p dev=%s\n", (int)dcr->jcr->JobId,
236 VolumeName, vol->vol_name, vol->dev->print_name());
240 static void free_vol_item(VOLRES *vol)
255 * Put a new Volume entry in the Volume list. This
256 * effectively reserves the volume so that it will
257 * not be mounted again.
259 * If the device has any current volume associated with it,
260 * and it is a different Volume, and the device is not busy,
261 * we release the old Volume item and insert the new one.
263 * It is assumed that the device is free and locked so that
264 * we can change the device structure.
266 * Some details of the Volume list handling:
268 * 1. The Volume list entry must be attached to the drive (rather than
269 * attached to a job as it currently is. I.e. the drive that "owns"
270 * the volume (in use, mounted)
271 * must point to the volume (still to be maintained in a list).
273 * 2. The Volume is entered in the list when a drive is reserved.
275 * 3. When a drive is in use, the device code must appropriately update the
276 * volume name as it changes (currently the list is static -- an entry is
277 * removed when the Volume is no longer reserved, in use or mounted).
278 * The new code must keep the same list entry as long as the drive
279 * has any volume associated with it but the volume name in the list
280 * must be updated when the drive has a different volume mounted.
282 * 4. A job that has reserved a volume, can un-reserve the volume, and if the
283 * volume is not mounted, and not reserved, and not in use, it will be
284 * removed from the list.
286 * 5. If a job wants to reserve a drive with a different Volume from the one on
287 * the drive, it can re-use the drive for the new Volume.
289 * 6. If a job wants a Volume that is in a different drive, it can either use the
290 * other drive or take the volume, only if the other drive is not in use or
293 * One nice aspect of this is that the reserve use count and the writer use count
294 * already exist and are correctly programmed and will need no changes -- use
295 * counts are always very tricky.
297 * The old code had a concept of "reserving" a Volume, but was changed
298 * to reserving and using a drive. A volume is must be attached to (owned by) a
299 * drive and can move from drive to drive or be unused given certain specific
300 * conditions of the drive. The key is that the drive must "own" the Volume.
301 * The old code had the job (dcr) owning the volume (more or less). The job was
302 * to change the insertion and removal of the volumes from the list to be based
303 * on the drive rather than the job.
305 * Return: VOLRES entry on success
306 * NULL volume busy on another drive
308 VOLRES *reserve_volume(DCR *dcr, const char *VolumeName)
311 DEVICE * volatile dev = dcr->dev;
315 Dmsg3(dbglvl, "jid=%u enter reserve_volume=%s drive=%s\n", jid(), VolumeName,
316 dcr->dev->print_name());
318 * We lock the reservations system here to ensure
319 * when adding a new volume that no newly scheduled
320 * job can reserve it.
323 debug_list_volumes("begin reserve_volume");
325 * First, remove any old volume attached to this device as it
330 Dmsg5(dbglvl, "jid=%u Vol attached=%s, newvol=%s volinuse=%d on %s\n",
331 jid(), vol->vol_name, VolumeName, vol->is_in_use(), dev->print_name());
333 * Make sure we don't remove the current volume we are inserting
334 * because it was probably inserted by another job, or it
335 * is not being used and is marked as not reserved.
337 if (strcmp(vol->vol_name, VolumeName) == 0) {
338 Dmsg3(dbglvl, "jid=%u === set reserved vol=%s dev=%s\n", jid(), VolumeName,
339 vol->dev->print_name());
340 vol->set_in_use(); /* retake vol if released previously */
341 dcr->reserved_volume = true; /* reserved volume */
342 goto get_out; /* Volume already on this device */
344 /* Don't release a volume if it was reserved by someone other than us */
345 if (vol->is_in_use() && !dcr->reserved_volume) {
346 Dmsg2(dbglvl, "jid=%u Cannot free vol=%s. It is reserved.\n", jid(), vol->vol_name);
347 vol = NULL; /* vol in use */
350 Dmsg3(dbglvl, "jid=%u reserve_vol free vol=%s at %p\n", jid(), vol->vol_name, vol->vol_name);
352 dev->set_unload(); /* have to unload current volume */
353 debug_list_volumes("reserve_vol free");
357 /* Create a new Volume entry */
358 nvol = new_vol_item(dcr, VolumeName);
361 * Now try to insert the new Volume
363 vol = (VOLRES *)vol_list->binary_insert(nvol, my_compare);
365 Dmsg3(dbglvl, "jid=%u Found vol=%s dev-same=%d\n", jid(), vol->vol_name, dev==vol->dev);
367 * At this point, a Volume with this name already is in the list,
368 * so we simply release our new Volume entry. Note, this should
369 * only happen if we are moving the volume from one drive to another.
371 Dmsg3(dbglvl, "jid=%u reserve_vol free-tmp vol=%s at %p\n",
372 (int)dcr->jcr->JobId, vol->vol_name, vol->vol_name);
374 * Clear dev pointer so that free_vol_item() doesn't
375 * take away our volume.
377 nvol->dev = NULL; /* don't zap dev entry */
381 * Check if we are trying to use the Volume on a different drive
383 * vol->dev is where the Volume we want is
385 if (dev != vol->dev) {
386 /* Caller wants to switch Volume to another device */
387 if (!vol->dev->is_busy() && !vol->is_swapping()) {
389 Dmsg4(dbglvl, "==== jid=%u Swap vol=%s from dev=%s to %s\n", jid(),
390 VolumeName, vol->dev->print_name(), dev->print_name());
391 free_volume(dev); /* free any volume attached to our drive */
392 dev->set_unload(); /* Unload any volume that is on our drive */
393 dcr->dev = vol->dev; /* temp point to other dev */
394 slot = get_autochanger_loaded_slot(dcr); /* get slot on other drive */
395 dcr->dev = dev; /* restore dev */
396 vol->set_slot(slot); /* save slot */
397 vol->dev->set_unload(); /* unload the other drive */
398 vol->set_swapping(); /* swap from other drive */
399 dev->swap_dev = vol->dev; /* remember to get this vol */
400 dev->set_load(); /* then reload on our drive */
401 vol->dev->vol = NULL; /* remove volume from other drive */
402 vol->dev = dev; /* point the Volume at our drive */
403 dev->vol = vol; /* point our drive at the Volume */
405 Dmsg4(dbglvl, "jid=%u ==== Swap not possible Vol busy vol=%s from dev=%s to %s\n", jid(),
406 VolumeName, vol->dev->print_name(), dev->print_name());
407 vol = NULL; /* device busy */
414 dev->vol = vol; /* point to newly inserted volume */
419 Dmsg3(dbglvl, "jid=%u === set in_use. vol=%s dev=%s\n", jid(), vol->vol_name,
420 vol->dev->print_name());
422 dcr->reserved_volume = true;
423 bstrncpy(dcr->VolumeName, vol->vol_name, sizeof(dcr->VolumeName));
425 debug_list_volumes("end new volume");
431 * Switch from current device to given device
435 void switch_device(DCR *dcr, DEVICE *dev)
440 memcpy(&save_dcr, dcr, sizeof(save_dcr));
441 clean_device(dcr); /* clean up the dcr */
443 dcr->dev = dev; /* get new device pointer */
444 Jmsg(dcr->jcr, M_INFO, 0, _("Device switch. New device %s chosen.\n"),
445 dcr->dev->print_name());
447 bstrncpy(dcr->VolumeName, save_dcr.VolumeName, sizeof(dcr->VolumeName));
448 bstrncpy(dcr->media_type, save_dcr.media_type, sizeof(dcr->media_type));
449 dcr->VolCatInfo.Slot = save_dcr.VolCatInfo.Slot;
450 bstrncpy(dcr->pool_name, save_dcr.pool_name, sizeof(dcr->pool_name));
451 bstrncpy(dcr->pool_type, save_dcr.pool_type, sizeof(dcr->pool_type));
452 bstrncpy(dcr->dev_name, dev->dev_name, sizeof(dcr->dev_name));
454 // dcr->set_reserved();
461 * Search for a Volume name in the Volume list.
463 * Returns: VOLRES entry on success
464 * NULL if the Volume is not in the list
466 VOLRES *find_volume(const char *VolumeName)
469 /* Do not lock reservations here */
471 vol.vol_name = bstrdup(VolumeName);
472 fvol = (VOLRES *)vol_list->binary_search(&vol, my_compare);
474 Dmsg3(dbglvl, "jid=%u find_vol=%s found=%d\n", jid(), VolumeName, fvol!=NULL);
475 debug_list_volumes("find_volume");
480 void DCR::set_reserved()
483 Dmsg2(dbglvl, "Inc reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
487 void DCR::clear_reserved()
492 Dmsg2(dbglvl, "Dec reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
497 * Remove any reservation from a drive and tell the system
498 * that the volume is unused at least by us.
500 void DCR::unreserve_device()
505 reserved_volume = false;
506 /* If we set read mode in reserving, remove it */
507 if (dev->can_read()) {
510 if (dev->num_writers < 0) {
511 Jmsg1(jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
512 dev->num_writers = 0;
514 if (dev->num_reserved() == 0 && dev->num_writers == 0) {
522 * Free a Volume from the Volume list if it is no longer used
523 * Note, for tape drives we want to remember where the Volume
524 * was when last used, so rather than free the volume entry,
525 * we simply mark it "not reserved" so when the drive is really
526 * needed for another volume, we can reuse it.
528 * Returns: true if the Volume found and "removed" from the list
529 * false if the Volume is not in the list or is in use
531 bool volume_unused(DCR *dcr)
533 DEVICE *dev = dcr->dev;
536 Dmsg2(dbglvl, "jid=%u vol_unused: no vol on %s\n", (int)dcr->jcr->JobId, dev->print_name());
537 debug_list_volumes("null vol cannot unreserve_volume");
540 if (dev->vol->is_swapping()) {
541 Dmsg1(dbglvl, "vol_unused: vol being swapped on %s\n", dev->print_name());
542 debug_list_volumes("swapping vol cannot unreserve_volume");
547 if (dev->is_busy()) {
548 Dmsg2(dbglvl, "jid=%u vol_unused: busy on %s\n", (int)dcr->jcr->JobId, dev->print_name());
549 debug_list_volumes("dev busy cannot unreserve_volume");
554 if (dev->num_writers > 0 || dev->num_reserved() > 0) {
560 * If this is a tape, we do not free the volume, rather we wait
561 * until the autoloader unloads it, or until another tape is
562 * explicitly read in this drive. This allows the SD to remember
563 * where the tapes are or last were.
565 Dmsg5(dbglvl, "jid=%u === set not reserved vol=%s num_writers=%d dev_reserved=%d dev=%s\n",
566 jid(), dev->vol->vol_name, dev->num_writers, dev->num_reserved(), dev->print_name());
567 dev->vol->clear_in_use();
568 if (dev->is_tape() || dev->is_autochanger()) {
572 * Note, this frees the volume reservation entry, but the
573 * file descriptor remains open with the OS.
575 return free_volume(dev);
580 * Unconditionally release the volume entry
582 bool free_volume(DEVICE *dev)
586 if (dev->vol == NULL) {
587 Dmsg2(dbglvl, "jid=%u No vol on dev %s\n", jid(), dev->print_name());
592 /* Don't free a volume while it is being swapped */
593 if (!vol->is_swapping()) {
595 vol_list->remove(vol);
596 Dmsg3(dbglvl, "jid=%u === remove volume %s dev=%s\n", jid(), vol->vol_name, dev->print_name());
598 debug_list_volumes("free_volume");
605 /* Create the Volume list */
606 void create_volume_list()
609 if (vol_list == NULL) {
610 vol_list = New(dlist(vol, &vol->link));
614 /* Release all Volumes from the list */
615 void free_volume_list()
622 foreach_dlist(vol, vol_list) {
624 Dmsg3(dbglvl, "jid=%u free vol_list Volume=%s dev=%s\n", jid(),
625 vol->vol_name, vol->dev->print_name());
627 Dmsg3(dbglvl, "jid=%u free vol_list Volume=%s dev=%p\n", jid(),
628 vol->vol_name, vol->dev);
631 vol->vol_name = NULL;
638 bool DCR::can_i_use_volume()
644 vol = find_volume(VolumeName);
646 Dmsg2(dbglvl, "jid=%u Vol=%s not in use.\n", jid(), VolumeName);
647 goto get_out; /* vol not in list */
649 ASSERT(vol->dev != NULL);
651 if (dev == vol->dev) { /* same device OK */
652 Dmsg2(dbglvl, "jid=%u Vol=%s on same dev.\n", jid(), VolumeName);
655 Dmsg4(dbglvl, "jid=%u Vol=%s on %s we have %s\n", jid(), VolumeName,
656 vol->dev->print_name(), dev->print_name());
658 /* ***FIXME*** check this ... */
659 if (!vol->dev->is_busy()) {
660 Dmsg3(dbglvl, "jid=%u Vol=%s dev=%s not busy.\n", jid(), VolumeName, vol->dev->print_name());
663 Dmsg3(dbglvl, "jid=%u Vol=%s dev=%s busy.\n", jid(), VolumeName, vol->dev->print_name());
665 Dmsg3(dbglvl, "jid=%u Vol=%s in use by %s.\n", jid(), VolumeName, vol->dev->print_name());
676 * We get the following type of information:
678 * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=1 copy=0 strip=0
682 * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=0 copy=0 strip=0
686 static bool use_storage_cmd(JCR *jcr)
688 POOL_MEM store_name, dev_name, media_type, pool_name, pool_type;
689 BSOCK *dir = jcr->dir_bsock;
697 memset(&rctx, 0, sizeof(RCTX));
700 * If there are multiple devices, the director sends us
701 * use_device for each device that it wants to use.
703 dirstore = New(alist(10, not_owned_by_alist));
704 jcr->reserve_msgs = New(alist(10, not_owned_by_alist));
706 Dmsg2(dbglvl, "jid=%u <dird: %s", jid(), dir->msg);
707 ok = sscanf(dir->msg, use_storage, store_name.c_str(),
708 media_type.c_str(), pool_name.c_str(),
709 pool_type.c_str(), &append, &Copy, &Stripe) == 7;
714 jcr->write_store = dirstore;
716 jcr->read_store = dirstore;
718 rctx.append = append;
719 unbash_spaces(store_name);
720 unbash_spaces(media_type);
721 unbash_spaces(pool_name);
722 unbash_spaces(pool_type);
723 store = new DIRSTORE;
724 dirstore->append(store);
725 memset(store, 0, sizeof(DIRSTORE));
726 store->device = New(alist(10));
727 bstrncpy(store->name, store_name, sizeof(store->name));
728 bstrncpy(store->media_type, media_type, sizeof(store->media_type));
729 bstrncpy(store->pool_name, pool_name, sizeof(store->pool_name));
730 bstrncpy(store->pool_type, pool_type, sizeof(store->pool_type));
731 store->append = append;
733 /* Now get all devices */
734 while (dir->recv() >= 0) {
735 Dmsg2(dbglvl, "jid=%u <dird device: %s", jid(), dir->msg);
736 ok = sscanf(dir->msg, use_device, dev_name.c_str()) == 1;
740 unbash_spaces(dev_name);
741 store->device->append(bstrdup(dev_name.c_str()));
743 } while (ok && dir->recv() >= 0);
745 /* Developer debug code */
747 if (debug_level >= dbglvl) {
748 foreach_alist(store, dirstore) {
749 Dmsg6(dbglvl, "jid=%u Storage=%s media_type=%s pool=%s pool_type=%s append=%d\n",
750 (int)rctx.jcr->JobId,
751 store->name, store->media_type, store->pool_name,
752 store->pool_type, store->append);
753 foreach_alist(device_name, store->device) {
754 Dmsg2(dbglvl, "jid=%u Device=%s\n", jid(), device_name);
759 init_jcr_device_wait_timers(jcr);
760 jcr->dcr = new_dcr(jcr, NULL, NULL); /* get a dcr */
762 BSOCK *dir = jcr->dir_bsock;
763 dir->fsend(_("3939 Could not get dcr\n"));
764 Dmsg1(dbglvl, ">dird: %s", dir->msg);
768 * At this point, we have a list of all the Director's Storage
769 * resources indicated for this Job, which include Pool, PoolType,
770 * storage name, and Media type.
771 * Then for each of the Storage resources, we have a list of
772 * device names that were given.
774 * Wiffle through them and find one that can do the backup.
777 int wait_for_device_retries = 0;
780 rctx.notify_dir = true;
783 for ( ; !fail && !job_canceled(jcr); ) {
784 pop_reserve_messages(jcr);
785 rctx.suitable_device = false;
786 rctx.have_volume = false;
787 rctx.VolumeName[0] = 0;
788 rctx.any_drive = false;
789 if (!jcr->PreferMountedVols) {
791 * Here we try to find a drive that is not used.
792 * This will maximize the use of available drives.
795 rctx.num_writers = 20000000; /* start with impossible number */
796 rctx.low_use_drive = NULL;
797 rctx.PreferMountedVols = false;
798 rctx.exact_match = false;
799 rctx.autochanger_only = true;
800 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
803 /* Look through all drives possibly for low_use drive */
804 if (rctx.low_use_drive) {
805 rctx.try_low_use_drive = true;
806 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
809 rctx.try_low_use_drive = false;
811 rctx.autochanger_only = false;
812 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
817 * Now we look for a drive that may or may not be in
820 /* Look for an exact Volume match all drives */
821 rctx.PreferMountedVols = true;
822 rctx.exact_match = true;
823 rctx.autochanger_only = false;
824 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
827 /* Look for any mounted drive */
828 rctx.exact_match = false;
829 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
833 rctx.any_drive = true;
834 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
837 /* Keep reservations locked *except* during wait_for_device() */
838 unlock_reservations();
840 * The idea of looping on repeat a few times it to ensure
841 * that if there is some subtle timing problem between two
842 * jobs, we will simply try again, and most likely succeed.
843 * This can happen if one job reserves a drive or finishes using
844 * a drive at the same time a second job wants it.
846 if (repeat++ > 1) { /* try algorithm 3 times */
847 bmicrosleep(30, 0); /* wait a bit */
848 Dmsg1(dbglvl, "jid=%u repeat reserve algorithm\n", (int)rctx.jcr->JobId);
849 } else if (!rctx.suitable_device || !wait_for_device(jcr, wait_for_device_retries)) {
850 Dmsg1(dbglvl, "jid=%u Fail. !suitable_device || !wait_for_device\n",
851 (int)rctx.jcr->JobId);
855 dir->signal(BNET_HEARTBEAT); /* Inform Dir that we are alive */
857 unlock_reservations();
860 * If we get here, there are no suitable devices available, which
861 * means nothing configured. If a device is suitable but busy
862 * with another Volume, we will not come here.
864 unbash_spaces(dir->msg);
865 pm_strcpy(jcr->errmsg, dir->msg);
866 Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
867 Jmsg(jcr, M_FATAL, 0, _("\n"
868 " Device \"%s\" with MediaType \"%s\" requested by DIR not found in SD Device resources.\n"),
869 dev_name.c_str(), media_type.c_str());
870 dir->fsend(NO_device, dev_name.c_str());
872 Dmsg2(dbglvl, "jid=%u >dird: %s", jid(), dir->msg);
875 unbash_spaces(dir->msg);
876 pm_strcpy(jcr->errmsg, dir->msg);
877 Jmsg(jcr, M_FATAL, 0, _("Failed command: %s\n"), jcr->errmsg);
878 dir->fsend(BAD_use, jcr->errmsg);
879 Dmsg2(dbglvl, "jid=%u >dird: %s", jid(), dir->msg);
882 release_reserve_messages(jcr);
888 * Walk through the autochanger resources and check if
889 * the volume is in one of them.
891 * Returns: true if volume is in device
894 static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol)
896 AUTOCHANGER *changer = vol->dev->device->changer_res;
898 /* Find resource, and make sure we were able to open it */
899 if (strcmp(rctx.device_name, changer->hdr.name) == 0) {
900 Dmsg2(dbglvl, "jid=%u Found changer device %s\n", (int)rctx.jcr->JobId, vol->dev->device->hdr.name);
903 Dmsg2(dbglvl, "jid=%u Incorrect changer device %s\n", (int)rctx.jcr->JobId, changer->hdr.name);
908 * Search for a device suitable for this job.
909 * Note, this routine sets sets rctx.suitable_device if any
910 * device exists within the SD. The device may not be actually
912 * It also returns if it finds a useable device.
914 bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
923 dirstore = jcr->write_store;
925 dirstore = jcr->read_store;
927 Dmsg5(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d\n",
928 (int)rctx.jcr->JobId,
929 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
930 rctx.autochanger_only);
933 * If the appropriate conditions of this if are met, namely that
934 * we are appending and the user wants mounted drive (or we
935 * force try a mounted drive because they are all busy), we
936 * start by looking at all the Volumes in the volume list.
938 if (!vol_list->empty() && rctx.append && rctx.PreferMountedVols) {
939 dlist *temp_vol_list, *save_vol_list;
942 Dmsg0(dbglvl, "lock volumes\n");
945 * Create a temporary copy of the volume list. We do this,
946 * to avoid having the volume list locked during the
947 * call to reserve_device(), which would cause a deadlock.
948 * Note, we may want to add an update counter on the vol_list
949 * so that if it is modified while we are traversing the copy
950 * we can take note and act accordingly (probably redo the
951 * search at least a few times).
953 Dmsg1(dbglvl, "jid=%u duplicate vol list\n", (int)rctx.jcr->JobId);
954 temp_vol_list = New(dlist(vol, &vol->link));
955 foreach_dlist(vol, vol_list) {
957 VOLRES *tvol = (VOLRES *)malloc(sizeof(VOLRES));
958 memset(tvol, 0, sizeof(VOLRES));
959 tvol->vol_name = bstrdup(vol->vol_name);
960 tvol->dev = vol->dev;
961 nvol = (VOLRES *)temp_vol_list->binary_insert(tvol, my_compare);
963 tvol->dev = NULL; /* don't zap dev entry */
965 Pmsg0(000, "Logic error. Duplicating vol list hit duplicate.\n");
966 Jmsg(jcr, M_WARNING, 0, "Logic error. Duplicating vol list hit duplicate.\n");
971 /* Look through reserved volumes for one we can use */
972 Dmsg1(dbglvl, "jid=%u look for vol in vol list\n", (int)rctx.jcr->JobId);
973 foreach_dlist(vol, temp_vol_list) {
975 Dmsg2(dbglvl, "jid=%u vol=%s no dev\n", (int)rctx.jcr->JobId, vol->vol_name);
978 /* Check with Director if this Volume is OK */
979 bstrncpy(dcr->VolumeName, vol->vol_name, sizeof(dcr->VolumeName));
980 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
984 Dmsg2(dbglvl, "jid=%u vol=%s OK for this job\n", (int)rctx.jcr->JobId, vol->vol_name);
985 foreach_alist(store, dirstore) {
988 foreach_alist(device_name, store->device) {
989 /* Found a device, try to use it */
990 rctx.device_name = device_name;
991 rctx.device = vol->dev->device;
993 if (vol->dev->is_autochanger()) {
994 Dmsg2(dbglvl, "jid=%u vol=%s is in changer\n", (int)rctx.jcr->JobId,
996 if (!is_vol_in_autochanger(rctx, vol)) {
999 } else if (strcmp(device_name, vol->dev->device->hdr.name) != 0) {
1000 Dmsg3(dbglvl, "jid=%u device=%s not suitable want %s\n", (int)rctx.jcr->JobId,
1001 vol->dev->device->hdr.name, device_name);
1005 bstrncpy(rctx.VolumeName, vol->vol_name, sizeof(rctx.VolumeName));
1006 rctx.have_volume = true;
1007 /* Try reserving this device and volume */
1008 Dmsg3(dbglvl, "jid=%u try vol=%s on device=%s\n", (int)rctx.jcr->JobId,
1009 rctx.VolumeName, device_name);
1010 stat = reserve_device(rctx);
1011 if (stat == 1) { /* found available device */
1012 Dmsg2(dbglvl, "jid=%u Suitable device found=%s\n", (int)rctx.jcr->JobId,
1016 } else if (stat == 0) { /* device busy */
1017 Dmsg2(dbglvl, "jid=%u Suitable device=%s, busy: not use\n",
1018 (int)rctx.jcr->JobId, device_name);
1020 /* otherwise error */
1021 Dmsg1(dbglvl, "jid=%u No suitable device found.\n", (int)rctx.jcr->JobId);
1023 rctx.have_volume = false;
1024 rctx.VolumeName[0] = 0;
1033 } /* end for loop over reserved volumes */
1035 Dmsg1(dbglvl, "%u lock volumes\n", jid());
1037 save_vol_list = vol_list;
1038 vol_list = temp_vol_list;
1039 free_volume_list(); /* release temp_vol_list */
1040 vol_list = save_vol_list;
1041 Dmsg1(dbglvl, "jid=%u deleted temp vol list\n", (int)rctx.jcr->JobId);
1042 Dmsg1(dbglvl, "jid=%u unlock volumes\n", (int)rctx.jcr->JobId);
1046 Dmsg2(dbglvl, "jid=%u got vol %s from in-use vols list\n", (int)rctx.jcr->JobId,
1052 * No reserved volume we can use, so now search for an available device.
1054 * For each storage device that the user specified, we
1055 * search and see if there is a resource for that device.
1057 foreach_alist(store, dirstore) {
1059 foreach_alist(device_name, store->device) {
1061 rctx.device_name = device_name;
1062 stat = search_res_for_device(rctx);
1063 if (stat == 1) { /* found available device */
1064 Dmsg2(dbglvl, "jid=%u available device found=%s\n", (int)rctx.jcr->JobId,device_name);
1067 } else if (stat == 0) { /* device busy */
1068 Dmsg2(dbglvl, "jid=%u No usable device=%s, busy: not use\n", (int)rctx.jcr->JobId,device_name);
1070 /* otherwise error */
1071 Dmsg1(dbglvl, "jid=%u No usable device found.\n", (int)rctx.jcr->JobId);
1079 Dmsg1(dbglvl, "OK dev found. Vol=%s\n", rctx.VolumeName);
1081 Dmsg0(dbglvl, "Leave find_suit_dev: no dev found.\n");
1087 * Search for a particular storage device with particular storage
1088 * characteristics (MediaType).
1090 int search_res_for_device(RCTX &rctx)
1092 AUTOCHANGER *changer;
1095 Dmsg2(dbglvl, "jid=%u search res for %s\n", (int)rctx.jcr->JobId, rctx.device_name);
1096 /* Look through Autochangers first */
1097 foreach_res(changer, R_AUTOCHANGER) {
1098 Dmsg2(dbglvl, "jid=%u Try match changer res=%s\n", (int)rctx.jcr->JobId, changer->hdr.name);
1099 /* Find resource, and make sure we were able to open it */
1100 if (strcmp(rctx.device_name, changer->hdr.name) == 0) {
1101 /* Try each device in this AutoChanger */
1102 foreach_alist(rctx.device, changer->device) {
1103 Dmsg2(dbglvl, "jid=%u Try changer device %s\n", (int)rctx.jcr->JobId,
1104 rctx.device->hdr.name);
1105 stat = reserve_device(rctx);
1106 if (stat != 1) { /* try another device */
1110 if (rctx.store->append == SD_APPEND) {
1111 Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for append.\n",
1112 (int)rctx.jcr->JobId, rctx.device->hdr.name,
1113 rctx.jcr->dcr->dev->num_reserved());
1115 Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for read.\n",
1116 (int)rctx.jcr->JobId, rctx.device->hdr.name,
1117 rctx.jcr->read_dcr->dev->num_reserved());
1124 /* Now if requested look through regular devices */
1125 if (!rctx.autochanger_only) {
1126 foreach_res(rctx.device, R_DEVICE) {
1127 Dmsg2(dbglvl, "jid=%u Try match res=%s\n", (int)rctx.jcr->JobId, rctx.device->hdr.name);
1128 /* Find resource, and make sure we were able to open it */
1129 if (strcmp(rctx.device_name, rctx.device->hdr.name) == 0) {
1130 stat = reserve_device(rctx);
1131 if (stat != 1) { /* try another device */
1135 if (rctx.store->append == SD_APPEND) {
1136 Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for append.\n",
1137 (int)rctx.jcr->JobId, rctx.device->hdr.name,
1138 rctx.jcr->dcr->dev->num_reserved());
1140 Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for read.\n",
1141 (int)rctx.jcr->JobId, rctx.device->hdr.name,
1142 rctx.jcr->read_dcr->dev->num_reserved());
1148 return -1; /* nothing found */
1152 * Try to reserve a specific device.
1154 * Returns: 1 -- OK, have DCR
1158 static int reserve_device(RCTX &rctx)
1162 const int name_len = MAX_NAME_LENGTH;
1164 /* Make sure MediaType is OK */
1165 Dmsg3(dbglvl, "jid=%u chk MediaType device=%s request=%s\n",
1166 (int)rctx.jcr->JobId,
1167 rctx.device->media_type, rctx.store->media_type);
1168 if (strcmp(rctx.device->media_type, rctx.store->media_type) != 0) {
1172 /* Make sure device exists -- i.e. we can stat() it */
1173 if (!rctx.device->dev) {
1174 rctx.device->dev = init_dev(rctx.jcr, rctx.device);
1176 if (!rctx.device->dev) {
1177 if (rctx.device->changer_res) {
1178 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
1179 " Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
1180 rctx.device->hdr.name, rctx.device_name);
1182 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
1183 " Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
1186 return -1; /* no use waiting */
1189 rctx.suitable_device = true;
1190 Dmsg1(dbglvl, "try reserve %s\n", rctx.device->hdr.name);
1191 rctx.jcr->dcr = dcr = new_dcr(rctx.jcr, rctx.jcr->dcr, rctx.device->dev);
1193 BSOCK *dir = rctx.jcr->dir_bsock;
1194 dir->fsend(_("3926 Could not get dcr for device: %s\n"), rctx.device_name);
1195 Dmsg1(dbglvl, ">dird: %s", dir->msg);
1198 bstrncpy(dcr->pool_name, rctx.store->pool_name, name_len);
1199 bstrncpy(dcr->pool_type, rctx.store->pool_type, name_len);
1200 bstrncpy(dcr->media_type, rctx.store->media_type, name_len);
1201 bstrncpy(dcr->dev_name, rctx.device_name, name_len);
1202 if (rctx.store->append == SD_APPEND) {
1203 Dmsg3(dbglvl, "jid=%u have_vol=%d vol=%s\n", (int)rctx.jcr->JobId,
1204 rctx.have_volume, rctx.VolumeName);
1205 ok = reserve_device_for_append(dcr, rctx);
1210 rctx.jcr->dcr = dcr;
1211 Dmsg6(dbglvl, "jid=%u Reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
1212 (int)rctx.jcr->JobId,
1213 dcr->dev->num_reserved(),
1214 dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
1215 if (rctx.have_volume) {
1216 if (reserve_volume(dcr, rctx.VolumeName)) {
1217 Dmsg2(dbglvl, "jid=%u Reserved vol=%s\n", jid(), rctx.VolumeName);
1219 Dmsg2(dbglvl, "jid=%u Could not reserve vol=%s\n", jid(), rctx.VolumeName);
1223 dcr->any_volume = true;
1224 if (dir_find_next_appendable_volume(dcr)) {
1225 bstrncpy(rctx.VolumeName, dcr->VolumeName, sizeof(rctx.VolumeName));
1226 Dmsg2(dbglvl, "jid=%u looking for Volume=%s\n", (int)rctx.jcr->JobId, rctx.VolumeName);
1227 rctx.have_volume = true;
1229 Dmsg1(dbglvl, "jid=%u No next volume found\n", (int)rctx.jcr->JobId);
1230 rctx.have_volume = false;
1231 rctx.VolumeName[0] = 0;
1233 * If there is at least one volume that is valid and in use,
1234 * but we get here, check if we are running with prefers
1235 * non-mounted drives. In that case, we have selected a
1236 * non-used drive and our one and only volume is mounted
1237 * elsewhere, so we bail out and retry using that drive.
1239 if (dcr->found_in_use() && !rctx.PreferMountedVols) {
1240 rctx.PreferMountedVols = true;
1241 if (dcr->VolumeName[0]) {
1242 dcr->unreserve_device();
1247 * Note. Under some circumstances, the Director can hand us
1248 * a Volume name that is not the same as the one on the current
1249 * drive, and in that case, the call above to find the next
1250 * volume will fail because in attempting to reserve the Volume
1251 * the code will realize that we already have a tape mounted,
1252 * and it will fail. This *should* only happen if there are
1253 * writers, thus the following test. In that case, we simply
1254 * bail out, and continue waiting, rather than plunging on
1255 * and hoping that the operator can resolve the problem.
1257 if (dcr->dev->num_writers != 0) {
1258 if (dcr->VolumeName[0]) {
1259 dcr->unreserve_device();
1266 ok = reserve_device_for_read(dcr);
1268 rctx.jcr->read_dcr = dcr;
1269 Dmsg6(dbglvl, "jid=%u Read reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
1270 (int)rctx.jcr->JobId,
1271 dcr->dev->num_reserved(),
1272 dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
1279 if (rctx.notify_dir) {
1281 BSOCK *dir = rctx.jcr->dir_bsock;
1282 pm_strcpy(dev_name, rctx.device->hdr.name);
1283 bash_spaces(dev_name);
1284 ok = dir->fsend(OK_device, dev_name.c_str()); /* Return real device name */
1285 Dmsg2(dbglvl, "jid=%u >dird changer: %s", jid(), dir->msg);
1292 rctx.have_volume = false;
1293 Dmsg1(dbglvl, "jid=%u Not OK.\n", (int)rctx.jcr->JobId);
1294 rctx.VolumeName[0] = 0;
1299 * We "reserve" the drive by setting the ST_READ bit. No one else
1300 * should touch the drive until that is cleared.
1301 * This allows the DIR to "reserve" the device before actually
1304 static bool reserve_device_for_read(DCR *dcr)
1306 DEVICE *dev = dcr->dev;
1307 JCR *jcr = dcr->jcr;
1314 if (is_device_unmounted(dev)) {
1315 Dmsg2(dbglvl, "jid=%u Device %s is BLOCKED due to user unmount.\n",
1316 (int)jcr->JobId, dev->print_name());
1317 Mmsg(jcr->errmsg, _("3601 JobId=%u device %s is BLOCKED due to user unmount.\n"),
1318 jcr->JobId, dev->print_name());
1319 queue_reserve_message(jcr);
1323 if (dev->is_busy()) {
1324 Dmsg5(dbglvl, "jid=%u Device %s is busy ST_READ=%d num_writers=%d reserved=%d.\n",
1325 (int)jcr->JobId, dev->print_name(),
1326 dev->state & ST_READ?1:0, dev->num_writers, dev->num_reserved());
1327 Mmsg(jcr->errmsg, _("3602 JobId=%u device %s is busy (already reading/writing).\n"),
1328 jcr->JobId, dev->print_name());
1329 queue_reserve_message(jcr);
1333 dev->clear_append();
1336 dcr->set_reserved();
1345 * We reserve the device for appending by incrementing
1346 * num_reserved(). We do virtually all the same work that
1347 * is done in acquire_device_for_append(), but we do
1348 * not attempt to mount the device. This routine allows
1349 * the DIR to reserve multiple devices before *really*
1350 * starting the job. It also permits the SD to refuse
1351 * certain devices (not up, ...).
1353 * Note, in reserving a device, if the device is for the
1354 * same pool and the same pool type, then it is acceptable.
1355 * The Media Type has already been checked. If we are
1356 * the first tor reserve the device, we put the pool
1357 * name and pool type in the device record.
1359 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx)
1361 JCR *jcr = dcr->jcr;
1362 DEVICE *dev = dcr->dev;
1369 /* If device is being read, we cannot write it */
1370 if (dev->can_read()) {
1371 Mmsg(jcr->errmsg, _("3603 JobId=%u device %s is busy reading.\n"),
1372 jcr->JobId, dev->print_name());
1373 Dmsg2(dbglvl, "jid=%u %s", jid(), jcr->errmsg);
1374 queue_reserve_message(jcr);
1378 /* If device is unmounted, we are out of luck */
1379 if (is_device_unmounted(dev)) {
1380 Mmsg(jcr->errmsg, _("3604 JobId=%u device %s is BLOCKED due to user unmount.\n"),
1381 jcr->JobId, dev->print_name());
1382 Dmsg2(dbglvl, "jid=%u %s", jid(), jcr->errmsg);
1383 queue_reserve_message(jcr);
1387 Dmsg2(dbglvl, "jid=%u reserve_append device is %s\n", jid(), dev->print_name());
1389 /* Now do detailed tests ... */
1390 if (can_reserve_drive(dcr, rctx) != 1) {
1391 Dmsg1(dbglvl, "jid=%u can_reserve_drive!=1\n", (int)jcr->JobId);
1395 dcr->set_reserved();
1403 static int is_pool_ok(DCR *dcr)
1405 DEVICE *dev = dcr->dev;
1406 JCR *jcr = dcr->jcr;
1408 /* Now check if we want the same Pool and pool type */
1409 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
1410 strcmp(dev->pool_type, dcr->pool_type) == 0) {
1411 /* OK, compatible device */
1412 Dmsg1(dbglvl, "OK dev: %s num_writers=0, reserved, pool matches\n", dev->print_name());
1415 /* Drive Pool not suitable for us */
1416 Mmsg(jcr->errmsg, _(
1417 "3608 JobId=%u wants Pool=\"%s\" but have Pool=\"%s\" nreserve=%d on drive %s.\n"),
1418 (uint32_t)jcr->JobId, dcr->pool_name, dev->pool_name,
1419 dev->num_reserved(), dev->print_name());
1420 queue_reserve_message(jcr);
1421 Dmsg2(dbglvl, "failed: busy num_writers=0, reserved, pool=%s wanted=%s\n",
1422 dev->pool_name, dcr->pool_name);
1427 static bool is_max_jobs_ok(DCR *dcr)
1429 DEVICE *dev = dcr->dev;
1430 JCR *jcr = dcr->jcr;
1432 Dmsg5(dbglvl, "MaxJobs=%d Jobs=%d reserves=%d Status=%s Vol=%s\n",
1433 dcr->VolCatInfo.VolCatMaxJobs,
1434 dcr->VolCatInfo.VolCatJobs, dev->num_reserved(),
1435 dcr->VolCatInfo.VolCatStatus,
1437 if (strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0) {
1440 if (dcr->VolCatInfo.VolCatMaxJobs > 0 && dcr->VolCatInfo.VolCatMaxJobs <=
1441 (dcr->VolCatInfo.VolCatJobs + dev->num_reserved())) {
1442 /* Max Job Vols depassed or already reserved */
1443 Mmsg(jcr->errmsg, _("3610 JobId=%u Volume max jobs exceeded on drive %s.\n"),
1444 (uint32_t)jcr->JobId, dev->print_name());
1445 queue_reserve_message(jcr);
1446 Dmsg1(dbglvl, "reserve dev failed: %s", jcr->errmsg);
1447 return false; /* wait */
1453 * Returns: 1 if drive can be reserved
1454 * 0 if we should wait
1455 * -1 on error or impossibility
1457 static int can_reserve_drive(DCR *dcr, RCTX &rctx)
1459 DEVICE *dev = dcr->dev;
1460 JCR *jcr = dcr->jcr;
1462 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
1464 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
1465 rctx.autochanger_only, rctx.any_drive);
1467 /* Check for max jobs on this Volume */
1468 if (!is_max_jobs_ok(dcr)) {
1472 /* setting any_drive overrides PreferMountedVols flag */
1473 if (!rctx.any_drive) {
1475 * When PreferMountedVols is set, we keep track of the
1476 * drive in use that has the least number of writers, then if
1477 * no unmounted drive is found, we try that drive. This
1478 * helps spread the load to the least used drives.
1480 if (rctx.try_low_use_drive && dev == rctx.low_use_drive) {
1481 Dmsg3(dbglvl, "jid=%u OK dev=%s == low_drive=%s.\n",
1482 jcr->JobId, dev->print_name(), rctx.low_use_drive->print_name());
1485 /* If he wants a free drive, but this one is busy, no go */
1486 if (!rctx.PreferMountedVols && dev->is_busy()) {
1487 /* Save least used drive */
1488 if ((dev->num_writers + dev->num_reserved()) < rctx.num_writers) {
1489 rctx.num_writers = dev->num_writers + dev->num_reserved();
1490 rctx.low_use_drive = dev;
1491 Dmsg3(dbglvl, "jid=%u set low use drive=%s num_writers=%d\n",
1492 (int)jcr->JobId, dev->print_name(), rctx.num_writers);
1494 Dmsg2(dbglvl, "jid=%u not low use num_writers=%d\n",
1495 (int)jcr->JobId, dev->num_writers+dev->num_reserved());
1497 Dmsg1(dbglvl, "jid=%u failed: !prefMnt && busy.\n", jcr->JobId);
1498 Mmsg(jcr->errmsg, _("3605 JobId=%u wants free drive but device %s is busy.\n"),
1499 jcr->JobId, dev->print_name());
1500 queue_reserve_message(jcr);
1504 /* Check for prefer mounted volumes */
1505 if (rctx.PreferMountedVols && !dev->vol && dev->is_tape()) {
1506 Mmsg(jcr->errmsg, _("3606 JobId=%u prefers mounted drives, but drive %s has no Volume.\n"),
1507 jcr->JobId, dev->print_name());
1508 queue_reserve_message(jcr);
1509 Dmsg1(dbglvl, "jid=%u failed: want mounted -- no vol\n", (uint32_t)jcr->JobId);
1510 return 0; /* No volume mounted */
1513 /* Check for exact Volume name match */
1514 /* ***FIXME*** for Disk, we can accept any volume that goes with this
1517 if (rctx.exact_match && rctx.have_volume) {
1519 Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
1521 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
1522 rctx.autochanger_only, rctx.any_drive);
1523 Dmsg5(dbglvl, "jid=%u have_vol=%d have=%s resvol=%s want=%s\n",
1524 (int)jcr->JobId, rctx.have_volume, dev->VolHdr.VolumeName,
1525 dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1526 ok = strcmp(dev->VolHdr.VolumeName, rctx.VolumeName) == 0 ||
1527 (dev->vol && strcmp(dev->vol->vol_name, rctx.VolumeName) == 0);
1529 Mmsg(jcr->errmsg, _("3607 JobId=%u wants Vol=\"%s\" drive has Vol=\"%s\" on drive %s.\n"),
1530 jcr->JobId, rctx.VolumeName, dev->VolHdr.VolumeName,
1532 queue_reserve_message(jcr);
1533 Dmsg4(dbglvl, "jid=%u not OK: dev have=%s resvol=%s want=%s\n", (int)jcr->JobId,
1534 dev->VolHdr.VolumeName, dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1537 if (!dcr->can_i_use_volume()) {
1538 return 0; /* fail if volume on another drive */
1543 /* Check for unused autochanger drive */
1544 if (rctx.autochanger_only && !dev->is_busy() &&
1545 dev->VolHdr.VolumeName[0] == 0) {
1546 /* Device is available but not yet reserved, reserve it for us */
1547 Dmsg2(dbglvl, "jid=%u OK Res Unused autochanger %s.\n",
1548 jcr->JobId, dev->print_name());
1549 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1550 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1551 return 1; /* reserve drive */
1555 * Handle the case that there are no writers
1557 if (dev->num_writers == 0) {
1558 /* Now check if there are any reservations on the drive */
1559 if (dev->num_reserved()) {
1560 return is_pool_ok(dcr);
1561 } else if (dev->can_append()) {
1562 if (is_pool_ok(dcr)) {
1565 /* Changing pool, unload old tape if any in drive */
1566 Dmsg1(dbglvl, "jid=%u OK dev: num_writers=0, not reserved, pool change, unload changer\n", (int)jcr->JobId);
1567 /* ***FIXME*** use set_unload() */
1568 unload_autochanger(dcr, -1);
1571 /* Device is available but not yet reserved, reserve it for us */
1572 Dmsg2(dbglvl, "jid=%u OK Dev avail reserved %s\n", (int)jcr->JobId, dev->print_name());
1573 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1574 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1575 return 1; /* reserve drive */
1579 * Check if the device is in append mode with writers (i.e.
1580 * available if pool is the same).
1582 if (dev->can_append() || dev->num_writers > 0) {
1583 return is_pool_ok(dcr);
1585 Pmsg1(000, _("Logic error!!!! JobId=%u Should not get here.\n"), (int)jcr->JobId);
1586 Mmsg(jcr->errmsg, _("3910 JobId=%u Logic error!!!! drive %s Should not get here.\n"),
1587 jcr->JobId, dev->print_name());
1588 queue_reserve_message(jcr);
1589 Jmsg0(jcr, M_FATAL, 0, _("Logic error!!!! Should not get here.\n"));
1590 return -1; /* error, should not get here */
1592 Mmsg(jcr->errmsg, _("3911 JobId=%u failed reserve drive %s.\n"),
1593 jcr->JobId, dev->print_name());
1594 queue_reserve_message(jcr);
1595 Dmsg2(dbglvl, "jid=%u failed: No reserve %s\n", jcr->JobId, dev->print_name());
1603 * Queue a reservation error or failure message for this jcr
1605 static void queue_reserve_message(JCR *jcr)
1613 msgs = jcr->reserve_msgs;
1618 * Look for duplicate message. If found, do
1621 for (i=msgs->size()-1; i >= 0; i--) {
1622 msg = (char *)msgs->get(i);
1626 /* Comparison based on 4 digit message number */
1627 if (strncmp(msg, jcr->errmsg, 4) == 0) {
1631 /* Message unique, so insert it */
1632 jcr->reserve_msgs->push(bstrdup(jcr->errmsg));
1639 * Send any reservation messages queued for this jcr
1641 void send_drive_reserve_messages(JCR *jcr, void sendit(const char *msg, int len, void *sarg), void *arg)
1648 msgs = jcr->reserve_msgs;
1649 if (!msgs || msgs->size() == 0) {
1652 for (i=msgs->size()-1; i >= 0; i--) {
1653 msg = (char *)msgs->get(i);
1655 sendit(" ", 3, arg);
1656 sendit(msg, strlen(msg), arg);
1667 * Pop and release any reservations messages
1669 static void pop_reserve_messages(JCR *jcr)
1675 msgs = jcr->reserve_msgs;
1679 while ((msg = (char *)msgs->pop())) {
1687 * Also called from acquire.c
1689 void release_reserve_messages(JCR *jcr)
1691 pop_reserve_messages(jcr);
1693 if (!jcr->reserve_msgs) {
1696 delete jcr->reserve_msgs;
1697 jcr->reserve_msgs = NULL;