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 Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Drive reservation functions for Storage Daemon
33 * Split from job.c and acquire.c June 2005
42 const int dbglvl = 150;
44 static brwlock_t reservation_lock;
46 /* Forward referenced functions */
47 static int can_reserve_drive(DCR *dcr, RCTX &rctx);
48 static int reserve_device(RCTX &rctx);
49 static bool reserve_device_for_read(DCR *dcr);
50 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx);
51 static bool use_storage_cmd(JCR *jcr);
52 static void queue_reserve_message(JCR *jcr);
53 static void pop_reserve_messages(JCR *jcr);
54 //void switch_device(DCR *dcr, DEVICE *dev);
56 /* Requests from the Director daemon */
57 static char use_storage[] = "use storage=%127s media_type=%127s "
58 "pool_name=%127s pool_type=%127s append=%d copy=%d stripe=%d\n";
59 static char use_device[] = "use device=%127s\n";
61 /* Responses sent to Director daemon */
62 static char OK_device[] = "3000 OK use device device=%s\n";
63 static char NO_device[] = "3924 Device \"%s\" not in SD Device resources.\n";
64 static char BAD_use[] = "3913 Bad use command: %s\n";
66 bool use_cmd(JCR *jcr)
69 * Get the device, media, and pool information
71 if (!use_storage_cmd(jcr)) {
72 set_jcr_job_status(jcr, JS_ErrorTerminated);
73 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
80 * This allows a given thread to recursively call lock_reservations.
81 * It must, of course, call unlock_... the same number of times.
83 void init_reservations_lock()
86 if ((errstat=rwl_init(&reservation_lock)) != 0) {
88 Emsg1(M_ABORT, 0, _("Unable to initialize reservation lock. ERR=%s\n"),
89 be.bstrerror(errstat));
95 void term_reservations_lock()
97 rwl_destroy(&reservation_lock);
101 int reservations_lock_count = 0;
103 /* This applies to a drive and to Volumes */
104 void _lock_reservations()
107 reservations_lock_count++;
108 if ((errstat=rwl_writelock(&reservation_lock)) != 0) {
110 Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
111 errstat, be.bstrerror(errstat));
115 void _unlock_reservations()
118 reservations_lock_count--;
119 if ((errstat=rwl_writeunlock(&reservation_lock)) != 0) {
121 Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
122 errstat, be.bstrerror(errstat));
126 void DCR::set_reserved()
129 Dmsg2(dbglvl, "Inc reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
133 void DCR::clear_reserved()
138 Dmsg2(dbglvl, "Dec reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
143 * Remove any reservation from a drive and tell the system
144 * that the volume is unused at least by us.
146 void DCR::unreserve_device()
151 reserved_volume = false;
152 /* If we set read mode in reserving, remove it */
153 if (dev->can_read()) {
156 if (dev->num_writers < 0) {
157 Jmsg1(jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
158 dev->num_writers = 0;
160 if (dev->num_reserved() == 0 && dev->num_writers == 0) {
168 * We get the following type of information:
170 * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=1 copy=0 strip=0
174 * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=0 copy=0 strip=0
178 static bool use_storage_cmd(JCR *jcr)
180 POOL_MEM store_name, dev_name, media_type, pool_name, pool_type;
181 BSOCK *dir = jcr->dir_bsock;
189 memset(&rctx, 0, sizeof(RCTX));
192 * If there are multiple devices, the director sends us
193 * use_device for each device that it wants to use.
195 dirstore = New(alist(10, not_owned_by_alist));
196 jcr->reserve_msgs = New(alist(10, not_owned_by_alist));
198 Dmsg1(dbglvl, "<dird: %s", dir->msg);
199 ok = sscanf(dir->msg, use_storage, store_name.c_str(),
200 media_type.c_str(), pool_name.c_str(),
201 pool_type.c_str(), &append, &Copy, &Stripe) == 7;
206 jcr->write_store = dirstore;
208 jcr->read_store = dirstore;
210 rctx.append = append;
211 unbash_spaces(store_name);
212 unbash_spaces(media_type);
213 unbash_spaces(pool_name);
214 unbash_spaces(pool_type);
215 store = new DIRSTORE;
216 dirstore->append(store);
217 memset(store, 0, sizeof(DIRSTORE));
218 store->device = New(alist(10));
219 bstrncpy(store->name, store_name, sizeof(store->name));
220 bstrncpy(store->media_type, media_type, sizeof(store->media_type));
221 bstrncpy(store->pool_name, pool_name, sizeof(store->pool_name));
222 bstrncpy(store->pool_type, pool_type, sizeof(store->pool_type));
223 store->append = append;
225 /* Now get all devices */
226 while (dir->recv() >= 0) {
227 Dmsg1(dbglvl, "<dird device: %s", dir->msg);
228 ok = sscanf(dir->msg, use_device, dev_name.c_str()) == 1;
232 unbash_spaces(dev_name);
233 store->device->append(bstrdup(dev_name.c_str()));
235 } while (ok && dir->recv() >= 0);
237 /* Developer debug code */
239 if (debug_level >= dbglvl) {
240 foreach_alist(store, dirstore) {
241 Dmsg5(dbglvl, "Storage=%s media_type=%s pool=%s pool_type=%s append=%d\n",
242 store->name, store->media_type, store->pool_name,
243 store->pool_type, store->append);
244 foreach_alist(device_name, store->device) {
245 Dmsg1(dbglvl, " Device=%s\n", device_name);
250 init_jcr_device_wait_timers(jcr);
251 jcr->dcr = new_dcr(jcr, NULL, NULL); /* get a dcr */
253 BSOCK *dir = jcr->dir_bsock;
254 dir->fsend(_("3939 Could not get dcr\n"));
255 Dmsg1(dbglvl, ">dird: %s", dir->msg);
259 * At this point, we have a list of all the Director's Storage
260 * resources indicated for this Job, which include Pool, PoolType,
261 * storage name, and Media type.
262 * Then for each of the Storage resources, we have a list of
263 * device names that were given.
265 * Wiffle through them and find one that can do the backup.
268 int wait_for_device_retries = 0;
271 rctx.notify_dir = true;
274 for ( ; !fail && !job_canceled(jcr); ) {
275 pop_reserve_messages(jcr);
276 rctx.suitable_device = false;
277 rctx.have_volume = false;
278 rctx.VolumeName[0] = 0;
279 rctx.any_drive = false;
280 if (!jcr->PreferMountedVols) {
282 * Here we try to find a drive that is not used.
283 * This will maximize the use of available drives.
286 rctx.num_writers = 20000000; /* start with impossible number */
287 rctx.low_use_drive = NULL;
288 rctx.PreferMountedVols = false;
289 rctx.exact_match = false;
290 rctx.autochanger_only = true;
291 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
294 /* Look through all drives possibly for low_use drive */
295 if (rctx.low_use_drive) {
296 rctx.try_low_use_drive = true;
297 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
300 rctx.try_low_use_drive = false;
302 rctx.autochanger_only = false;
303 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
308 * Now we look for a drive that may or may not be in
311 /* Look for an exact Volume match all drives */
312 rctx.PreferMountedVols = true;
313 rctx.exact_match = true;
314 rctx.autochanger_only = false;
315 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
318 /* Look for any mounted drive */
319 rctx.exact_match = false;
320 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
324 rctx.any_drive = true;
325 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
328 /* Keep reservations locked *except* during wait_for_device() */
329 unlock_reservations();
331 * The idea of looping on repeat a few times it to ensure
332 * that if there is some subtle timing problem between two
333 * jobs, we will simply try again, and most likely succeed.
334 * This can happen if one job reserves a drive or finishes using
335 * a drive at the same time a second job wants it.
337 if (repeat++ > 1) { /* try algorithm 3 times */
338 bmicrosleep(30, 0); /* wait a bit */
339 Dmsg0(dbglvl, "repeat reserve algorithm\n");
340 } else if (!rctx.suitable_device || !wait_for_device(jcr, wait_for_device_retries)) {
341 Dmsg0(dbglvl, "Fail. !suitable_device || !wait_for_device\n");
345 dir->signal(BNET_HEARTBEAT); /* Inform Dir that we are alive */
347 unlock_reservations();
350 * If we get here, there are no suitable devices available, which
351 * means nothing configured. If a device is suitable but busy
352 * with another Volume, we will not come here.
354 unbash_spaces(dir->msg);
355 pm_strcpy(jcr->errmsg, dir->msg);
356 Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
357 Jmsg(jcr, M_FATAL, 0, _("\n"
358 " Device \"%s\" with MediaType \"%s\" requested by DIR not found in SD Device resources.\n"),
359 dev_name.c_str(), media_type.c_str());
360 dir->fsend(NO_device, dev_name.c_str());
362 Dmsg1(dbglvl, ">dird: %s", dir->msg);
365 unbash_spaces(dir->msg);
366 pm_strcpy(jcr->errmsg, dir->msg);
367 Jmsg(jcr, M_FATAL, 0, _("Failed command: %s\n"), jcr->errmsg);
368 dir->fsend(BAD_use, jcr->errmsg);
369 Dmsg1(dbglvl, ">dird: %s", dir->msg);
372 release_reserve_messages(jcr);
378 * Walk through the autochanger resources and check if
379 * the volume is in one of them.
381 * Returns: true if volume is in device
384 static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol)
386 AUTOCHANGER *changer = vol->dev->device->changer_res;
388 /* Find resource, and make sure we were able to open it */
389 if (strcmp(rctx.device_name, changer->hdr.name) == 0) {
390 Dmsg1(dbglvl, "Found changer device %s\n", vol->dev->device->hdr.name);
393 Dmsg1(dbglvl, "Incorrect changer device %s\n", changer->hdr.name);
398 * Search for a device suitable for this job.
399 * Note, this routine sets sets rctx.suitable_device if any
400 * device exists within the SD. The device may not be actually
402 * It also returns if it finds a useable device.
404 bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
413 dirstore = jcr->write_store;
415 dirstore = jcr->read_store;
417 Dmsg5(dbglvl, "Start find_suit_dev PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
418 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
419 rctx.autochanger_only, rctx.any_drive);
422 * If the appropriate conditions of this if are met, namely that
423 * we are appending and the user wants mounted drive (or we
424 * force try a mounted drive because they are all busy), we
425 * start by looking at all the Volumes in the volume list.
427 if (!is_vol_list_empty() && rctx.append && rctx.PreferMountedVols) {
428 dlist *temp_vol_list;
430 temp_vol_list = dup_vol_list(jcr);
432 /* Look through reserved volumes for one we can use */
433 Dmsg0(dbglvl, "look for vol in vol list\n");
434 foreach_dlist(vol, temp_vol_list) {
436 Dmsg1(dbglvl, "vol=%s no dev\n", vol->vol_name);
439 /* Check with Director if this Volume is OK */
440 bstrncpy(dcr->VolumeName, vol->vol_name, sizeof(dcr->VolumeName));
441 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
445 Dmsg1(dbglvl, "vol=%s OK for this job\n", vol->vol_name);
446 foreach_alist(store, dirstore) {
449 foreach_alist(device_name, store->device) {
450 /* Found a device, try to use it */
451 rctx.device_name = device_name;
452 rctx.device = vol->dev->device;
454 if (vol->dev->is_autochanger()) {
455 Dmsg1(dbglvl, "vol=%s is in changer\n", vol->vol_name);
456 if (!is_vol_in_autochanger(rctx, vol)) {
459 } else if (strcmp(device_name, vol->dev->device->hdr.name) != 0) {
460 Dmsg2(dbglvl, "device=%s not suitable want %s\n",
461 vol->dev->device->hdr.name, device_name);
465 bstrncpy(rctx.VolumeName, vol->vol_name, sizeof(rctx.VolumeName));
466 rctx.have_volume = true;
467 /* Try reserving this device and volume */
468 Dmsg2(dbglvl, "try vol=%s on device=%s\n", rctx.VolumeName, device_name);
469 stat = reserve_device(rctx);
470 if (stat == 1) { /* found available device */
471 Dmsg1(dbglvl, "Suitable device found=%s\n", device_name);
474 } else if (stat == 0) { /* device busy */
475 Dmsg1(dbglvl, "Suitable device=%s, busy: not use\n", device_name);
477 /* otherwise error */
478 Dmsg0(dbglvl, "No suitable device found.\n");
480 rctx.have_volume = false;
481 rctx.VolumeName[0] = 0;
490 } /* end for loop over reserved volumes */
492 Dmsg0(dbglvl, "lock volumes\n");
493 free_temp_vol_list(temp_vol_list);
496 Dmsg1(dbglvl, "OK dev found. Vol=%s from in-use vols list\n", rctx.VolumeName);
501 * No reserved volume we can use, so now search for an available device.
503 * For each storage device that the user specified, we
504 * search and see if there is a resource for that device.
506 foreach_alist(store, dirstore) {
508 foreach_alist(device_name, store->device) {
510 rctx.device_name = device_name;
511 stat = search_res_for_device(rctx);
512 if (stat == 1) { /* found available device */
513 Dmsg1(dbglvl, "available device found=%s\n", device_name);
516 } else if (stat == 0) { /* device busy */
517 Dmsg1(dbglvl, "No usable device=%s, busy: not use\n", device_name);
519 /* otherwise error */
520 Dmsg0(dbglvl, "No usable device found.\n");
528 Dmsg1(dbglvl, "OK dev found. Vol=%s\n", rctx.VolumeName);
530 Dmsg0(dbglvl, "Leave find_suit_dev: no dev found.\n");
536 * Search for a particular storage device with particular storage
537 * characteristics (MediaType).
539 int search_res_for_device(RCTX &rctx)
541 AUTOCHANGER *changer;
544 Dmsg1(dbglvl, "search res for %s\n", rctx.device_name);
545 /* Look through Autochangers first */
546 foreach_res(changer, R_AUTOCHANGER) {
547 Dmsg1(dbglvl, "Try match changer res=%s\n", changer->hdr.name);
548 /* Find resource, and make sure we were able to open it */
549 if (strcmp(rctx.device_name, changer->hdr.name) == 0) {
550 /* Try each device in this AutoChanger */
551 foreach_alist(rctx.device, changer->device) {
552 Dmsg1(dbglvl, "Try changer device %s\n", rctx.device->hdr.name);
553 if (!rctx.device->autoselect) {
554 Dmsg1(100, "Device %s not autoselect skipped.\n",
555 rctx.device->hdr.name);
556 continue; /* device is not available */
558 stat = reserve_device(rctx);
559 if (stat != 1) { /* try another device */
563 if (rctx.store->append == SD_APPEND) {
564 Dmsg2(dbglvl, "Device %s reserved=%d for append.\n",
565 rctx.device->hdr.name, rctx.jcr->dcr->dev->num_reserved());
567 Dmsg2(dbglvl, "Device %s reserved=%d for read.\n",
568 rctx.device->hdr.name, rctx.jcr->read_dcr->dev->num_reserved());
575 /* Now if requested look through regular devices */
576 if (!rctx.autochanger_only) {
577 foreach_res(rctx.device, R_DEVICE) {
578 Dmsg1(dbglvl, "Try match res=%s\n", rctx.device->hdr.name);
579 /* Find resource, and make sure we were able to open it */
580 if (strcmp(rctx.device_name, rctx.device->hdr.name) == 0) {
581 stat = reserve_device(rctx);
582 if (stat != 1) { /* try another device */
586 if (rctx.store->append == SD_APPEND) {
587 Dmsg2(dbglvl, "Device %s reserved=%d for append.\n",
588 rctx.device->hdr.name, rctx.jcr->dcr->dev->num_reserved());
590 Dmsg2(dbglvl, "Device %s reserved=%d for read.\n",
591 rctx.device->hdr.name, rctx.jcr->read_dcr->dev->num_reserved());
597 return -1; /* nothing found */
601 * Try to reserve a specific device.
603 * Returns: 1 -- OK, have DCR
607 static int reserve_device(RCTX &rctx)
611 const int name_len = MAX_NAME_LENGTH;
613 /* Make sure MediaType is OK */
614 Dmsg2(dbglvl, "chk MediaType device=%s request=%s\n",
615 rctx.device->media_type, rctx.store->media_type);
616 if (strcmp(rctx.device->media_type, rctx.store->media_type) != 0) {
620 /* Make sure device exists -- i.e. we can stat() it */
621 if (!rctx.device->dev) {
622 rctx.device->dev = init_dev(rctx.jcr, rctx.device);
624 if (!rctx.device->dev) {
625 if (rctx.device->changer_res) {
626 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
627 " Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
628 rctx.device->hdr.name, rctx.device_name);
630 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
631 " Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
634 return -1; /* no use waiting */
637 rctx.suitable_device = true;
638 Dmsg1(dbglvl, "try reserve %s\n", rctx.device->hdr.name);
639 rctx.jcr->dcr = dcr = new_dcr(rctx.jcr, rctx.jcr->dcr, rctx.device->dev);
641 BSOCK *dir = rctx.jcr->dir_bsock;
642 dir->fsend(_("3926 Could not get dcr for device: %s\n"), rctx.device_name);
643 Dmsg1(dbglvl, ">dird: %s", dir->msg);
646 bstrncpy(dcr->pool_name, rctx.store->pool_name, name_len);
647 bstrncpy(dcr->pool_type, rctx.store->pool_type, name_len);
648 bstrncpy(dcr->media_type, rctx.store->media_type, name_len);
649 bstrncpy(dcr->dev_name, rctx.device_name, name_len);
650 if (rctx.store->append == SD_APPEND) {
651 Dmsg2(dbglvl, "call reserve for append: have_vol=%d vol=%s\n", rctx.have_volume, rctx.VolumeName);
652 ok = reserve_device_for_append(dcr, rctx);
658 Dmsg5(dbglvl, "Reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
659 dcr->dev->num_reserved(),
660 dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
661 Dmsg3(dbglvl, "Vol=%s num_writers=%d, have_vol=%d\n",
662 rctx.VolumeName, dcr->dev->num_writers, rctx.have_volume);
663 if (rctx.have_volume) {
664 Dmsg0(dbglvl, "Call reserve_volume\n");
665 if (reserve_volume(dcr, rctx.VolumeName)) {
666 Dmsg1(dbglvl, "Reserved vol=%s\n", rctx.VolumeName);
668 Dmsg1(dbglvl, "Could not reserve vol=%s\n", rctx.VolumeName);
672 dcr->any_volume = true;
673 Dmsg0(dbglvl, "no vol, call find_next_appendable_vol.\n");
674 if (dir_find_next_appendable_volume(dcr)) {
675 bstrncpy(rctx.VolumeName, dcr->VolumeName, sizeof(rctx.VolumeName));
676 rctx.have_volume = true;
677 Dmsg1(dbglvl, "looking for Volume=%s\n", rctx.VolumeName);
679 Dmsg0(dbglvl, "No next volume found\n");
680 rctx.have_volume = false;
681 rctx.VolumeName[0] = 0;
683 * If there is at least one volume that is valid and in use,
684 * but we get here, check if we are running with prefers
685 * non-mounted drives. In that case, we have selected a
686 * non-used drive and our one and only volume is mounted
687 * elsewhere, so we bail out and retry using that drive.
689 if (dcr->found_in_use() && !rctx.PreferMountedVols) {
690 rctx.PreferMountedVols = true;
691 if (dcr->VolumeName[0]) {
692 dcr->unreserve_device();
697 * Note. Under some circumstances, the Director can hand us
698 * a Volume name that is not the same as the one on the current
699 * drive, and in that case, the call above to find the next
700 * volume will fail because in attempting to reserve the Volume
701 * the code will realize that we already have a tape mounted,
702 * and it will fail. This *should* only happen if there are
703 * writers, thus the following test. In that case, we simply
704 * bail out, and continue waiting, rather than plunging on
705 * and hoping that the operator can resolve the problem.
707 if (dcr->dev->num_writers != 0) {
708 if (dcr->VolumeName[0]) {
709 dcr->unreserve_device();
716 ok = reserve_device_for_read(dcr);
718 rctx.jcr->read_dcr = dcr;
719 Dmsg5(dbglvl, "Read reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
720 dcr->dev->num_reserved(),
721 dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
728 if (rctx.notify_dir) {
730 BSOCK *dir = rctx.jcr->dir_bsock;
731 pm_strcpy(dev_name, rctx.device->hdr.name);
732 bash_spaces(dev_name);
733 ok = dir->fsend(OK_device, dev_name.c_str()); /* Return real device name */
734 Dmsg1(dbglvl, ">dird: %s", dir->msg);
741 rctx.have_volume = false;
742 rctx.VolumeName[0] = 0;
743 Dmsg0(dbglvl, "Not OK.\n");
748 * We "reserve" the drive by setting the ST_READ bit. No one else
749 * should touch the drive until that is cleared.
750 * This allows the DIR to "reserve" the device before actually
753 static bool reserve_device_for_read(DCR *dcr)
755 DEVICE *dev = dcr->dev;
763 if (is_device_unmounted(dev)) {
764 Dmsg1(dbglvl, "Device %s is BLOCKED due to user unmount.\n", dev->print_name());
765 Mmsg(jcr->errmsg, _("3601 JobId=%u device %s is BLOCKED due to user unmount.\n"),
766 jcr->JobId, dev->print_name());
767 queue_reserve_message(jcr);
771 if (dev->is_busy()) {
772 Dmsg4(dbglvl, "Device %s is busy ST_READ=%d num_writers=%d reserved=%d.\n",
774 dev->state & ST_READ?1:0, dev->num_writers, dev->num_reserved());
775 Mmsg(jcr->errmsg, _("3602 JobId=%u device %s is busy (already reading/writing).\n"),
776 jcr->JobId, dev->print_name());
777 queue_reserve_message(jcr);
793 * We reserve the device for appending by incrementing
794 * num_reserved(). We do virtually all the same work that
795 * is done in acquire_device_for_append(), but we do
796 * not attempt to mount the device. This routine allows
797 * the DIR to reserve multiple devices before *really*
798 * starting the job. It also permits the SD to refuse
799 * certain devices (not up, ...).
801 * Note, in reserving a device, if the device is for the
802 * same pool and the same pool type, then it is acceptable.
803 * The Media Type has already been checked. If we are
804 * the first tor reserve the device, we put the pool
805 * name and pool type in the device record.
807 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx)
810 DEVICE *dev = dcr->dev;
817 /* If device is being read, we cannot write it */
818 if (dev->can_read()) {
819 Mmsg(jcr->errmsg, _("3603 JobId=%u device %s is busy reading.\n"),
820 jcr->JobId, dev->print_name());
821 Dmsg1(dbglvl, "%s", jcr->errmsg);
822 queue_reserve_message(jcr);
826 /* If device is unmounted, we are out of luck */
827 if (is_device_unmounted(dev)) {
828 Mmsg(jcr->errmsg, _("3604 JobId=%u device %s is BLOCKED due to user unmount.\n"),
829 jcr->JobId, dev->print_name());
830 Dmsg1(dbglvl, "%s", jcr->errmsg);
831 queue_reserve_message(jcr);
835 Dmsg1(dbglvl, "reserve_append device is %s\n", dev->print_name());
837 /* Now do detailed tests ... */
838 if (can_reserve_drive(dcr, rctx) != 1) {
839 Dmsg0(dbglvl, "can_reserve_drive!=1\n");
851 static int is_pool_ok(DCR *dcr)
853 DEVICE *dev = dcr->dev;
856 /* Now check if we want the same Pool and pool type */
857 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
858 strcmp(dev->pool_type, dcr->pool_type) == 0) {
859 /* OK, compatible device */
860 Dmsg1(dbglvl, "OK dev: %s num_writers=0, reserved, pool matches\n", dev->print_name());
863 /* Drive Pool not suitable for us */
865 "3608 JobId=%u wants Pool=\"%s\" but have Pool=\"%s\" nreserve=%d on drive %s.\n"),
866 (uint32_t)jcr->JobId, dcr->pool_name, dev->pool_name,
867 dev->num_reserved(), dev->print_name());
868 queue_reserve_message(jcr);
869 Dmsg2(dbglvl, "failed: busy num_writers=0, reserved, pool=%s wanted=%s\n",
870 dev->pool_name, dcr->pool_name);
875 static bool is_max_jobs_ok(DCR *dcr)
877 DEVICE *dev = dcr->dev;
880 Dmsg5(dbglvl, "MaxJobs=%d Jobs=%d reserves=%d Status=%s Vol=%s\n",
881 dcr->VolCatInfo.VolCatMaxJobs,
882 dcr->VolCatInfo.VolCatJobs, dev->num_reserved(),
883 dcr->VolCatInfo.VolCatStatus,
885 if (strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0) {
888 if (dcr->VolCatInfo.VolCatMaxJobs > 0 && dcr->VolCatInfo.VolCatMaxJobs <=
889 (dcr->VolCatInfo.VolCatJobs + dev->num_reserved())) {
890 /* Max Job Vols depassed or already reserved */
891 Mmsg(jcr->errmsg, _("3610 JobId=%u Volume max jobs exceeded on drive %s.\n"),
892 (uint32_t)jcr->JobId, dev->print_name());
893 queue_reserve_message(jcr);
894 Dmsg1(dbglvl, "reserve dev failed: %s", jcr->errmsg);
895 return false; /* wait */
901 * Returns: 1 if drive can be reserved
902 * 0 if we should wait
903 * -1 on error or impossibility
905 static int can_reserve_drive(DCR *dcr, RCTX &rctx)
907 DEVICE *dev = dcr->dev;
910 Dmsg5(dbglvl, "PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
911 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
912 rctx.autochanger_only, rctx.any_drive);
914 /* Check for max jobs on this Volume */
915 if (!is_max_jobs_ok(dcr)) {
919 /* setting any_drive overrides PreferMountedVols flag */
920 if (!rctx.any_drive) {
922 * When PreferMountedVols is set, we keep track of the
923 * drive in use that has the least number of writers, then if
924 * no unmounted drive is found, we try that drive. This
925 * helps spread the load to the least used drives.
927 if (rctx.try_low_use_drive && dev == rctx.low_use_drive) {
928 Dmsg2(dbglvl, "OK dev=%s == low_drive=%s.\n",
929 dev->print_name(), rctx.low_use_drive->print_name());
932 /* If he wants a free drive, but this one is busy, no go */
933 if (!rctx.PreferMountedVols && dev->is_busy()) {
934 /* Save least used drive */
935 if ((dev->num_writers + dev->num_reserved()) < rctx.num_writers) {
936 rctx.num_writers = dev->num_writers + dev->num_reserved();
937 rctx.low_use_drive = dev;
938 Dmsg2(dbglvl, "set low use drive=%s num_writers=%d\n",
939 dev->print_name(), rctx.num_writers);
941 Dmsg1(dbglvl, "not low use num_writers=%d\n", dev->num_writers+dev->num_reserved());
943 Dmsg0(dbglvl, "failed: !prefMnt && busy.\n");
944 Mmsg(jcr->errmsg, _("3605 JobId=%u wants free drive but device %s is busy.\n"),
945 jcr->JobId, dev->print_name());
946 queue_reserve_message(jcr);
950 /* Check for prefer mounted volumes */
951 if (rctx.PreferMountedVols && !dev->vol && dev->is_tape()) {
952 Mmsg(jcr->errmsg, _("3606 JobId=%u prefers mounted drives, but drive %s has no Volume.\n"),
953 jcr->JobId, dev->print_name());
954 queue_reserve_message(jcr);
955 Dmsg0(dbglvl, "failed: want mounted -- no vol\n");
956 return 0; /* No volume mounted */
959 /* Check for exact Volume name match */
960 /* ***FIXME*** for Disk, we can accept any volume that goes with this
963 if (rctx.exact_match && rctx.have_volume) {
965 Dmsg5(dbglvl, "PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
966 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
967 rctx.autochanger_only, rctx.any_drive);
968 Dmsg4(dbglvl, "have_vol=%d have=%s resvol=%s want=%s\n",
969 rctx.have_volume, dev->VolHdr.VolumeName,
970 dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
971 ok = strcmp(dev->VolHdr.VolumeName, rctx.VolumeName) == 0 ||
972 (dev->vol && strcmp(dev->vol->vol_name, rctx.VolumeName) == 0);
974 Mmsg(jcr->errmsg, _("3607 JobId=%u wants Vol=\"%s\" drive has Vol=\"%s\" on drive %s.\n"),
975 jcr->JobId, rctx.VolumeName, dev->VolHdr.VolumeName,
977 queue_reserve_message(jcr);
978 Dmsg3(dbglvl, "not OK: dev have=%s resvol=%s want=%s\n",
979 dev->VolHdr.VolumeName, dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
982 if (!dcr->can_i_use_volume()) {
983 return 0; /* fail if volume on another drive */
988 /* Check for unused autochanger drive */
989 if (rctx.autochanger_only && !dev->is_busy() &&
990 dev->VolHdr.VolumeName[0] == 0) {
991 /* Device is available but not yet reserved, reserve it for us */
992 Dmsg1(dbglvl, "OK Res Unused autochanger %s.\n", dev->print_name());
993 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
994 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
995 return 1; /* reserve drive */
999 * Handle the case that there are no writers
1001 if (dev->num_writers == 0) {
1002 /* Now check if there are any reservations on the drive */
1003 if (dev->num_reserved()) {
1004 return is_pool_ok(dcr);
1005 } else if (dev->can_append()) {
1006 if (is_pool_ok(dcr)) {
1009 /* Changing pool, unload old tape if any in drive */
1010 Dmsg0(dbglvl, "OK dev: num_writers=0, not reserved, pool change, unload changer\n");
1011 /* ***FIXME*** use set_unload() */
1012 unload_autochanger(dcr, -1);
1015 /* Device is available but not yet reserved, reserve it for us */
1016 Dmsg1(dbglvl, "OK Dev avail reserved %s\n", dev->print_name());
1017 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1018 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1019 return 1; /* reserve drive */
1023 * Check if the device is in append mode with writers (i.e.
1024 * available if pool is the same).
1026 if (dev->can_append() || dev->num_writers > 0) {
1027 return is_pool_ok(dcr);
1029 Pmsg1(000, _("Logic error!!!! JobId=%u Should not get here.\n"), (int)jcr->JobId);
1030 Mmsg(jcr->errmsg, _("3910 JobId=%u Logic error!!!! drive %s Should not get here.\n"),
1031 jcr->JobId, dev->print_name());
1032 queue_reserve_message(jcr);
1033 Jmsg0(jcr, M_FATAL, 0, _("Logic error!!!! Should not get here.\n"));
1034 return -1; /* error, should not get here */
1036 Mmsg(jcr->errmsg, _("3911 JobId=%u failed reserve drive %s.\n"),
1037 jcr->JobId, dev->print_name());
1038 queue_reserve_message(jcr);
1039 Dmsg1(dbglvl, "failed: No reserve %s\n", dev->print_name());
1047 * Queue a reservation error or failure message for this jcr
1049 static void queue_reserve_message(JCR *jcr)
1057 msgs = jcr->reserve_msgs;
1062 * Look for duplicate message. If found, do
1065 for (i=msgs->size()-1; i >= 0; i--) {
1066 msg = (char *)msgs->get(i);
1070 /* Comparison based on 4 digit message number */
1071 if (strncmp(msg, jcr->errmsg, 4) == 0) {
1075 /* Message unique, so insert it */
1076 jcr->reserve_msgs->push(bstrdup(jcr->errmsg));
1083 * Send any reservation messages queued for this jcr
1085 void send_drive_reserve_messages(JCR *jcr, void sendit(const char *msg, int len, void *sarg), void *arg)
1092 msgs = jcr->reserve_msgs;
1093 if (!msgs || msgs->size() == 0) {
1096 for (i=msgs->size()-1; i >= 0; i--) {
1097 msg = (char *)msgs->get(i);
1099 sendit(" ", 3, arg);
1100 sendit(msg, strlen(msg), arg);
1111 * Pop and release any reservations messages
1113 static void pop_reserve_messages(JCR *jcr)
1119 msgs = jcr->reserve_msgs;
1123 while ((msg = (char *)msgs->pop())) {
1131 * Also called from acquire.c
1133 void release_reserve_messages(JCR *jcr)
1135 pop_reserve_messages(jcr);
1137 if (!jcr->reserve_msgs) {
1140 delete jcr->reserve_msgs;
1141 jcr->reserve_msgs = NULL;