2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * Drive reservation functions for Storage Daemon
22 * Written by Kern Sibbald, MM
24 * Split from job.c and acquire.c June 2005
31 const int dbglvl = 150;
33 static brwlock_t reservation_lock;
34 int reservations_lock_count = 0;
36 /* Forward referenced functions */
37 static int can_reserve_drive(DCR *dcr, RCTX &rctx);
38 static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol);
39 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx);
40 static bool reserve_device_for_read(DCR *dcr);
41 static bool use_device_cmd(JCR *jcr);
42 static int reserve_device(RCTX &rctx);
43 static void pop_reserve_messages(JCR *jcr);
44 static void queue_reserve_message(JCR *jcr);
45 static int is_pool_ok(DCR *dcr);
46 //void switch_device(DCR *dcr, DEVICE *dev);
48 /* Requests from the Director daemon */
49 static char use_storage[] = "use storage=%127s media_type=%127s "
50 "pool_name=%127s pool_type=%127s append=%d copy=%d stripe=%d\n";
51 static char use_device[] = "use device=%127s\n";
53 /* Responses sent to Director daemon */
54 static char OK_device[] = "3000 OK use device device=%s\n";
55 static char NO_device[] = "3924 Device \"%s\" not in SD Device"
56 " resources or no matching Media Type or is disabled.\n";
57 static char BAD_use[] = "3913 Bad use command: %s\n";
60 * This allows a given thread to recursively call lock_reservations.
61 * It must, of course, call unlock_... the same number of times.
63 void init_reservations_lock()
66 if ((errstat=rwl_init(&reservation_lock)) != 0) {
68 Emsg1(M_ABORT, 0, _("Unable to initialize reservation lock. ERR=%s\n"),
69 be.bstrerror(errstat));
76 /* This applies to a drive and to Volumes */
77 void _lock_reservations(const char *file, int line)
80 reservations_lock_count++;
81 if ((errstat=rwl_writelock_p(&reservation_lock, file, line)) != 0) {
83 Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
84 errstat, be.bstrerror(errstat));
88 void _unlock_reservations()
91 reservations_lock_count--;
92 if ((errstat=rwl_writeunlock(&reservation_lock)) != 0) {
94 Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
95 errstat, be.bstrerror(errstat));
99 void term_reservations_lock()
101 rwl_destroy(&reservation_lock);
102 term_vol_list_lock();
105 void DCR::clear_reserved()
110 Dmsg3(dbglvl, "Dec reserve=%d writers=%d dev=%s\n", dev->num_reserved(),
111 dev->num_writers, dev->print_name());
112 if (dev->num_reserved() == 0) {
113 dev->reserved_pool_name[0] = 0;
118 void DCR::set_reserved_for_append()
120 if (dev->num_reserved() == 0) {
121 bstrncpy(dev->reserved_pool_name, pool_name, sizeof(dev->reserved_pool_name));
122 Dmsg1(dbglvl, "Set reserve pool: %s\n", pool_name);
125 dev->set_append_reserve();
127 Dmsg3(dbglvl, "Inc reserve=%d writers=%d dev=%s\n", dev->num_reserved(),
128 dev->num_writers, dev->print_name());
131 void DCR::set_reserved_for_read()
133 /* Called for each volume read, no need to increment each time */
136 dev->set_read_reserve();
138 Dmsg2(dbglvl, "Inc 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(bool locked)
153 reserved_volume = false;
154 /* If we set read mode in reserving, remove it */
155 if (dev->can_read()) {
156 remove_read_volume(jcr, this->VolumeName);
159 if (dev->num_writers < 0) {
160 Jmsg1(jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
161 dev->num_writers = 0;
163 if (dev->num_reserved() == 0 && dev->num_writers == 0) {
164 generate_plugin_event(jcr, bsdEventDeviceClose, this);
173 bool use_cmd(JCR *jcr)
176 * Get the device, media, and pool information
178 if (!use_device_cmd(jcr)) {
179 jcr->setJobStatus(JS_ErrorTerminated);
180 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
187 * Storage search options
189 struct store_opts_t {
190 bool PreferMountedVols;
192 bool autochanger_only;
193 bool try_low_use_drive;
197 /* *** Old pre-8.8 algorithm *** not used here
198 * jcr->PreferMountedVols = true
199 * MntVol exact chgonly lowuse any
200 * 1 * {true, true, false, false, false},
201 * 2 * {true, false, true, false, false},
202 * 3 * {true, false, true, false, true},
204 * jcr->PreferMountedVols = false
205 * MntVol exact chgonly lowuse any
206 * 4 * {false, false, true, false, false},
207 * 5 * {false, false, true, true, false}, * low instantaneous use *
208 * 6 * {false, false, false, true, false},
209 * 7 * {true, true, false, false, false},
210 * 8 * {true, false, true, false, false},
211 * 9 * {true, false, true, false, true}
212 *** End old pre-8.8 algorithm ***
215 store_opts_t store_opts[] = {
216 /* jcr->PreferMountedVols = true */
217 /* MntVol exact chgonly lowuse any */
218 /* 1 */ {true, true, false, true, false}, /* low global use */
219 /* 2 */ {true, false, true, true, false},
220 /* 3 */ {true, false, true, false, true},
221 /* 4 */ {true, true, false, true, false},
222 /* 5 */ {false, true, false, false, false},
223 /* 6 */ {false, false, false, false, false},
224 /* 7 */ {true, false, false, false, true},
226 /* jcr->PreferMountedVols = false */
227 /* MntVol exact chgonly lowuse any */
228 /* 8 */ {false, false, true, true, false},
229 /* 9 */ {false, false, true, false, false},
230 /* 10 */ {false, false, false, true, false},
231 /* 11 */ {true, true, false, true, true},
232 /* 12 */ {true, false, true, true, false},
233 /* 13 */ {true, false, true, false, true}
236 static void set_options(RCTX &rctx, int num)
238 rctx.PreferMountedVols = store_opts[num-1].PreferMountedVols;
239 rctx.exact_match = store_opts[num-1].exact_match;
240 rctx.autochanger_only = store_opts[num-1].autochanger_only;
241 rctx.try_low_use_drive = store_opts[num-1].try_low_use_drive;
242 rctx.any_drive = store_opts[num-1].any_drive;
243 rctx.low_use_drive = NULL;
246 static void prt_options(RCTX &rctx, int num)
248 Dmsg6(dbglvl, "Inx=%d mntVol=%d exact=%d chgonly=%d low_use=%d any=%d\n",
249 num, rctx.PreferMountedVols, rctx.exact_match, rctx.autochanger_only,
250 rctx.try_low_use_drive, rctx.any_drive);
256 * We get the following type of information:
258 * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=1 copy=0 strip=0
262 * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=0 copy=0 strip=0
266 static bool use_device_cmd(JCR *jcr)
268 POOL_MEM store_name, dev_name, media_type, pool_name, pool_type;
269 BSOCK *dir = jcr->dir_bsock;
272 int32_t Copy, Stripe;
277 memset(&rctx, 0, sizeof(RCTX));
280 * If there are multiple devices, the director sends us
281 * use_device for each device that it wants to use.
283 dirstore = New(alist(10, not_owned_by_alist));
284 jcr->reserve_msgs = New(alist(10, not_owned_by_alist));
286 Dmsg1(dbglvl, "<dird: %s", dir->msg);
287 ok = sscanf(dir->msg, use_storage, store_name.c_str(),
288 media_type.c_str(), pool_name.c_str(),
289 pool_type.c_str(), &append, &Copy, &Stripe) == 7;
294 jcr->write_store = dirstore;
296 jcr->read_store = dirstore;
298 rctx.append = append;
299 unbash_spaces(store_name);
300 unbash_spaces(media_type);
301 unbash_spaces(pool_name);
302 unbash_spaces(pool_type);
303 store = new DIRSTORE;
304 dirstore->append(store);
305 memset(store, 0, sizeof(DIRSTORE));
306 store->device = New(alist(10));
307 bstrncpy(store->name, store_name, sizeof(store->name));
308 bstrncpy(store->media_type, media_type, sizeof(store->media_type));
309 bstrncpy(store->pool_name, pool_name, sizeof(store->pool_name));
310 bstrncpy(store->pool_type, pool_type, sizeof(store->pool_type));
311 store->append = append;
313 /* Now get all devices */
314 while (dir->recv() >= 0) {
315 Dmsg1(dbglvl, "<dird device: %s", dir->msg);
316 ok = sscanf(dir->msg, use_device, dev_name.c_str()) == 1;
320 unbash_spaces(dev_name);
321 store->device->append(bstrdup(dev_name.c_str()));
323 } while (ok && dir->recv() >= 0);
326 /* Developers debug code */
328 if (debug_level >= dbglvl) {
329 foreach_alist(store, dirstore) {
330 Dmsg5(dbglvl, "Storage=%s media_type=%s pool=%s pool_type=%s append=%d\n",
331 store->name, store->media_type, store->pool_name,
332 store->pool_type, store->append);
333 foreach_alist(device_name, store->device) {
334 Dmsg1(dbglvl, " Device=%s\n", device_name);
340 init_jcr_device_wait_timers(jcr);
341 jcr->dcr = new_dcr(jcr, NULL, NULL, !rctx.append); /* get a dcr */
343 BSOCK *dir = jcr->dir_bsock;
344 dir->fsend(_("3939 Could not get dcr\n"));
345 Dmsg1(dbglvl, ">dird: %s", dir->msg);
349 * At this point, we have a list of all the Director's Storage
350 * resources indicated for this Job, which include Pool, PoolType,
351 * storage name, and Media type.
352 * Then for each of the Storage resources, we have a list of
353 * device names that were given.
355 * Wiffle through Storage resources sent to us and find one that can do the backup.
358 int wait_for_device_retries = 0;
361 rctx.notify_dir = true;
363 /* Put new dcr in proper location */
365 rctx.jcr->dcr = jcr->dcr;
367 rctx.jcr->read_dcr = jcr->dcr;
370 for ( ; !fail && !job_canceled(jcr); ) {
372 pop_reserve_messages(jcr);
373 rctx.suitable_device = false;
374 rctx.have_volume = false;
375 rctx.VolumeName[0] = 0;
376 rctx.any_drive = false;
377 if (jcr->PreferMountedVols) {
378 for (i=1; i<=7; i++) {
379 set_options(rctx, i);
380 prt_options(rctx, i);
381 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
386 for (i=8; i<=13; i++) {
387 set_options(rctx, i);
388 prt_options(rctx, i);
389 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
397 /* Keep reservations locked *except* during wait_for_device() */
398 unlock_reservations();
400 * The idea of looping on repeat a few times it to ensure
401 * that if there is some subtle timing problem between two
402 * jobs, we will simply try again, and most likely succeed.
403 * This can happen if one job reserves a drive or finishes using
404 * a drive at the same time a second job wants it.
406 if (repeat++ < 20) { /* try sleeping 20 times */
407 bmicrosleep(30, 0); /* wait 30 secs */
408 Dmsg1(dbglvl, "repeat reserve algorithm JobId=%d\n", jcr->JobId);
409 } else if (!rctx.suitable_device || !wait_for_any_device(jcr, wait_for_device_retries)) {
410 Dmsg0(dbglvl, "Fail. !suitable_device || !wait_for_device\n");
414 dir->signal(BNET_HEARTBEAT); /* Inform Dir that we are alive */
416 unlock_reservations();
420 * If we get here, there are no suitable devices available, which
421 * means nothing configured. If a device is suitable but busy
422 * with another Volume, we will not come here.
424 unbash_spaces(dir->msg);
425 pm_strcpy(jcr->errmsg, dir->msg);
426 Jmsg(jcr, M_FATAL, 0, _("Device reservation failed for JobId=%d: %s\n"),
427 jcr->JobId, jcr->errmsg);
428 dir->fsend(NO_device, dev_name.c_str());
430 Dmsg1(dbglvl, ">dird: %s", dir->msg);
433 unbash_spaces(dir->msg);
434 pm_strcpy(jcr->errmsg, dir->msg);
435 Jmsg(jcr, M_FATAL, 0, _("Failed command: %s\n"), jcr->errmsg);
436 dir->fsend(BAD_use, jcr->errmsg);
437 Dmsg1(dbglvl, ">dird: %s", dir->msg);
440 release_reserve_messages(jcr);
445 * Search for a device suitable for this job.
446 * Note, this routine sets sets rctx.suitable_device if any
447 * device exists within the SD. The device may not be actually
449 * It also returns if it finds a useable device.
451 bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
460 dirstore = jcr->write_store;
462 dirstore = jcr->read_store;
464 Dmsg5(dbglvl, "Start find_suit_dev PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
465 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
466 rctx.autochanger_only, rctx.any_drive);
469 * If the appropriate conditions of this if are met, namely that
470 * we are appending and the user wants mounted drive (or we
471 * force try a mounted drive because they are all busy), we
472 * start by looking at all the Volumes in the volume list.
474 if (!is_vol_list_empty() && rctx.append && rctx.PreferMountedVols) {
475 dlist *temp_vol_list;
477 temp_vol_list = dup_vol_list(jcr);
479 /* Look through reserved volumes for one we can use */
480 Dmsg0(dbglvl, "look for vol in vol list\n");
481 foreach_dlist(vol, temp_vol_list) {
483 Dmsg1(dbglvl, "vol=%s no dev\n", vol->vol_name);
486 bstrncpy(dcr->VolumeName, vol->vol_name, sizeof(dcr->VolumeName));
487 /* Check with Director if this Volume is OK */
488 if (!dir_get_volume_info(dcr, dcr->VolumeName, GET_VOL_INFO_FOR_WRITE)) {
491 Dmsg1(dbglvl, "vol=%s OK for this job\n", vol->vol_name);
492 foreach_alist(store, dirstore) {
495 foreach_alist(device_name, store->device) {
496 /* Found a device, try to use it */
497 rctx.device_name = device_name;
498 rctx.device = vol->dev->device;
499 if (vol->dev->read_only) {
502 if (vol->dev->is_autochanger()) {
503 Dmsg1(dbglvl, "vol=%s is in changer\n", vol->vol_name);
504 if (!is_vol_in_autochanger(rctx, vol) || !vol->dev->autoselect ||
505 !vol->dev->enabled) {
508 } else if (strcmp(device_name, vol->dev->device->hdr.name) != 0) {
509 Dmsg2(dbglvl, "device=%s not suitable want %s\n",
510 vol->dev->device->hdr.name, device_name);
513 bstrncpy(rctx.VolumeName, vol->vol_name, sizeof(rctx.VolumeName));
514 rctx.have_volume = true;
515 /* Try reserving this device and volume */
516 Dmsg2(dbglvl, "Try reserve vol=%s on device=%s\n", rctx.VolumeName, device_name);
517 stat = reserve_device(rctx);
518 if (stat == 1) { /* found available device */
519 Dmsg1(dbglvl, "Device reserved=%s\n", device_name);
522 /* Error or no suitable device found */
523 Dmsg0(dbglvl, "No suitable device found.\n");
524 rctx.have_volume = false;
525 rctx.VolumeName[0] = 0;
531 } /* end of loop over storages */
535 } /* end for loop over reserved volumes */
537 Dmsg0(dbglvl, "lock volumes\n");
538 free_temp_vol_list(temp_vol_list);
539 } /* End test for vol_list, ... */
541 Dmsg1(dbglvl, "OK dev found. Vol=%s from in-use vols list\n", rctx.VolumeName);
546 * No reserved volume we can use, so now search for an available device.
548 * For each storage device that the user specified, we
549 * search and see if there is a resource for that device.
551 foreach_alist(store, dirstore) {
553 foreach_alist(device_name, store->device) {
555 rctx.device_name = device_name;
556 stat = search_res_for_device(rctx);
557 if (stat == 1) { /* found available device */
558 Dmsg1(dbglvl, "available device found=%s\n", device_name);
561 } else if (stat == 0) { /* device busy */
562 Dmsg1(dbglvl, "No usable device=%s, busy: not use\n", device_name);
564 /* otherwise error */
565 Dmsg0(dbglvl, "No usable device found.\n");
573 Dmsg1(dbglvl, "OK dev found. Vol=%s\n", rctx.VolumeName);
575 Dmsg0(dbglvl, "Leave find_suit_dev: no dev found.\n");
581 * Search for a particular storage device with particular storage
582 * characteristics (MediaType).
584 int search_res_for_device(RCTX &rctx)
586 AUTOCHANGER *changer;
589 Dmsg1(dbglvl, "search res for %s\n", rctx.device_name);
590 /* Look through Autochangers first */
591 foreach_res(changer, R_AUTOCHANGER) {
592 Dmsg1(dbglvl, "Try match changer res=%s\n", changer->hdr.name);
593 /* Find resource, and make sure we were able to open it */
594 if (strcmp(rctx.device_name, changer->hdr.name) == 0) {
595 /* Try each device in this AutoChanger */
596 foreach_alist(rctx.device, changer->device) {
597 Dmsg1(dbglvl, "Top try changer device %s\n", rctx.device->hdr.name);
598 if (rctx.store->append && rctx.device->read_only) {
601 if (!rctx.device->autoselect) {
602 Dmsg1(dbglvl, "Device %s not autoselect skipped.\n",
603 rctx.device->hdr.name);
604 continue; /* device is not available */
606 if (rctx.try_low_use_drive) {
607 if (!rctx.low_use_drive) {
608 rctx.low_use_drive = rctx.device->dev;
609 Dmsg2(dbglvl, "Set low_use usage=%lld drv=%s\n",
610 rctx.low_use_drive->usage,
611 rctx.low_use_drive->print_name());
612 } else if ((rctx.low_use_drive->usage > rctx.device->dev->usage) ||
613 (rctx.low_use_drive->usage == rctx.device->dev->usage &&
614 rctx.low_use_drive->num_reserved() > rctx.device->dev->num_reserved())) {
615 rctx.low_use_drive = rctx.device->dev;
616 Dmsg2(dbglvl, "Reset low_use usage=%lld drv=%s\n",
617 rctx.low_use_drive->usage,
618 rctx.low_use_drive->print_name());
620 Dmsg2(dbglvl, "Skip low_use usage=%lld drv=%s\n",
621 rctx.low_use_drive->usage,
622 rctx.low_use_drive->print_name());
625 Dmsg2(dbglvl, "try reserve vol=%s on device=%s\n", rctx.VolumeName, rctx.device->hdr.name);
626 stat = reserve_device(rctx);
627 if (stat != 1) { /* try another device */
631 if (rctx.store->append) {
632 Dmsg2(dbglvl, "Device %s reserved=%d for append.\n",
633 rctx.device->hdr.name, rctx.jcr->dcr->dev->num_reserved());
635 Dmsg2(dbglvl, "Device %s reserved=%d for read.\n",
636 rctx.device->hdr.name, rctx.jcr->read_dcr->dev->num_reserved());
641 /* If found a drive try to reserve it */
642 if (rctx.try_low_use_drive && rctx.low_use_drive) {
643 rctx.device = rctx.low_use_drive->device;
644 Dmsg2(dbglvl, "Try reserve vol=%s on device=%s\n", rctx.VolumeName, rctx.device->hdr.name);
645 stat = reserve_device(rctx);
648 if (rctx.store->append) {
649 Dmsg3(dbglvl, "JobId=%d device %s reserved=%d for append.\n",
650 rctx.jcr->JobId, rctx.device->hdr.name, rctx.jcr->dcr->dev->num_reserved());
652 Dmsg3(dbglvl, "JobId=%d device %s reserved=%d for read.\n",
653 rctx.jcr->JobId, rctx.device->hdr.name, rctx.jcr->read_dcr->dev->num_reserved());
656 Dmsg2(dbglvl, "Reserve for %s failed for JobId=%d.\n",
657 rctx.store->append ? "append" : "read", rctx.jcr->JobId);
664 /* Now if requested look through regular devices */
665 if (!rctx.autochanger_only) {
666 foreach_res(rctx.device, R_DEVICE) {
667 Dmsg1(dbglvl, "Try match res=%s\n", rctx.device->hdr.name);
668 /* Find resource, and make sure we were able to open it */
669 if (strcmp(rctx.device_name, rctx.device->hdr.name) == 0) {
670 Dmsg2(dbglvl, "Try reserve vol=%s on device=%s\n", rctx.VolumeName, rctx.device->hdr.name);
671 stat = reserve_device(rctx);
672 if (stat != 1) { /* try another device */
676 if (rctx.store->append) {
677 Dmsg2(dbglvl, "Device %s reserved=%d for append.\n",
678 rctx.device->hdr.name, rctx.jcr->dcr->dev->num_reserved());
680 Dmsg2(dbglvl, "Device %s reserved=%d for read.\n",
681 rctx.device->hdr.name, rctx.jcr->read_dcr->dev->num_reserved());
687 return -1; /* nothing found */
691 * Walk through the autochanger resources and check if
692 * the volume is in one of them.
694 * Returns: true if volume is in device
697 static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol)
699 AUTOCHANGER *changer = vol->dev->device->changer_res;
701 /* Find resource, and make sure we were able to open it */
702 if (changer && strcmp(rctx.device_name, changer->hdr.name) == 0) {
703 Dmsg1(dbglvl, "Found changer device %s\n", vol->dev->device->hdr.name);
706 Dmsg1(dbglvl, "Incorrect changer device %s\n", changer->hdr.name);
712 * Try to reserve a specific device.
714 * Returns: 1 -- OK, have DCR
718 static int reserve_device(RCTX &rctx)
722 const int name_len = MAX_NAME_LENGTH;
724 /* Make sure MediaType is OK */
725 Dmsg2(dbglvl, "chk MediaType device=%s request=%s\n",
726 rctx.device->media_type, rctx.store->media_type);
727 if (strcmp(rctx.device->media_type, rctx.store->media_type) != 0) {
731 /* Make sure device exists -- i.e. we can stat() it */
732 if (!rctx.device->dev) {
733 rctx.device->dev = init_dev(rctx.jcr, rctx.device);
735 if (!rctx.device->dev) {
736 if (rctx.device->changer_res) {
737 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
738 " Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
739 rctx.device->hdr.name, rctx.device_name);
741 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
742 " Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
745 return -1; /* no use waiting */
746 } else if (!rctx.device->dev->enabled) {
747 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
748 " Device \"%s\" requested by DIR is disabled.\n"),
750 return -1; /* no use waiting */
753 rctx.suitable_device = true;
754 Dmsg1(dbglvl, "try reserve %s\n", rctx.device->hdr.name);
755 if (rctx.store->append) {
756 dcr = new_dcr(rctx.jcr, rctx.jcr->dcr, rctx.device->dev, SD_APPEND);
758 dcr = new_dcr(rctx.jcr, rctx.jcr->read_dcr, rctx.device->dev, SD_READ);
761 BSOCK *dir = rctx.jcr->dir_bsock;
762 dir->fsend(_("3926 Could not get dcr for device: %s\n"), rctx.device_name);
763 Dmsg1(dbglvl, ">dird: %s", dir->msg);
766 bstrncpy(dcr->pool_name, rctx.store->pool_name, name_len);
767 bstrncpy(dcr->pool_type, rctx.store->pool_type, name_len);
768 bstrncpy(dcr->media_type, rctx.store->media_type, name_len);
769 bstrncpy(dcr->dev_name, rctx.device_name, name_len);
770 if (rctx.store->append) {
771 Dmsg2(dbglvl, "call reserve for append: have_vol=%d vol=%s\n", rctx.have_volume, rctx.VolumeName);
772 ok = reserve_device_for_append(dcr, rctx);
777 Dmsg5(dbglvl, "Reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
778 dcr->dev->num_reserved(),
779 dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
780 Dmsg4(dbglvl, "Vol=%s num_writers=%d, reserved=%d have_vol=%d\n",
781 rctx.VolumeName, dcr->dev->num_writers, dcr->dev->num_reserved(),
783 if (rctx.have_volume) {
784 Dmsg0(dbglvl, "Call reserve_volume for append.\n");
785 if (reserve_volume(dcr, rctx.VolumeName)) {
786 Dmsg1(dbglvl, "Reserved vol=%s\n", rctx.VolumeName);
788 Dmsg1(dbglvl, "Could not reserve vol=%s\n", rctx.VolumeName);
792 dcr->any_volume = true;
793 Dmsg0(dbglvl, "no vol, call find_next_appendable_vol.\n");
794 if (dir_find_next_appendable_volume(dcr)) {
795 bstrncpy(rctx.VolumeName, dcr->VolumeName, sizeof(rctx.VolumeName));
796 rctx.have_volume = true;
797 Dmsg1(dbglvl, "looking for Volume=%s\n", rctx.VolumeName);
798 if (!dcr->can_i_use_volume() || !is_pool_ok(dcr)) {
799 rctx.have_volume = false;
800 rctx.VolumeName[0] = 0;
801 dcr->unreserve_device(false);
805 dcr->dev->clear_wait();
806 Dmsg0(dbglvl, "No next volume found\n");
807 rctx.have_volume = false;
808 rctx.VolumeName[0] = 0;
810 * If there is at least one volume that is valid and in use,
811 * but we get here, check if we are running with prefers
812 * non-mounted drives. In that case, we have selected a
813 * non-used drive and our one and only volume is mounted
814 * elsewhere, so we bail out and retry using that drive.
816 if (dcr->found_in_use() && !rctx.PreferMountedVols) {
817 rctx.PreferMountedVols = true;
818 if (dcr->VolumeName[0]) {
819 dcr->unreserve_device(false);
824 * Note. Under some circumstances, the Director can hand us
825 * a Volume name that is not the same as the one on the current
826 * drive, and in that case, the call above to find the next
827 * volume will fail because in attempting to reserve the Volume
828 * the code will realize that we already have a tape mounted,
829 * and it will fail. This *should* only happen if there are
830 * writers, thus the following test. In that case, we simply
831 * bail out, and continue waiting, rather than plunging on
832 * and hoping that the operator can resolve the problem.
834 if (dcr->dev->num_writers != 0) {
835 if (dcr->VolumeName[0]) {
836 dcr->unreserve_device(false);
843 ok = reserve_device_for_read(dcr);
845 rctx.jcr->read_dcr = dcr;
846 Dmsg5(dbglvl, "Read reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
847 dcr->dev->num_reserved(),
848 dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
855 if (rctx.notify_dir) {
857 BSOCK *dir = rctx.jcr->dir_bsock;
858 pm_strcpy(dev_name, rctx.device->hdr.name);
859 bash_spaces(dev_name);
860 ok = dir->fsend(OK_device, dev_name.c_str()); /* Return real device name */
861 Dmsg1(dbglvl, ">dird: %s", dir->msg);
863 dcr->unreserve_device(false);
871 rctx.have_volume = false;
872 rctx.VolumeName[0] = 0;
873 Dmsg0(dbglvl, "Not OK.\n");
879 * We reserve the device for appending by incrementing
880 * num_reserved(). We do virtually all the same work that
881 * is done in acquire_device_for_append(), but we do
882 * not attempt to mount the device. This routine allows
883 * the DIR to reserve multiple devices before *really*
884 * starting the job. It also permits the SD to refuse
885 * certain devices (not up, ...).
887 * Note, in reserving a device, if the device is for the
888 * same pool and the same pool type, then it is acceptable.
889 * The Media Type has already been checked. If we are
890 * the first to reserve the device, we put the pool
891 * name and pool type in the device record.
893 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx)
896 DEVICE *dev = dcr->dev;
899 ASSERT2(dcr, "No dcr in reserve_device_for_append!");
900 if (job_canceled(jcr)) {
906 /* If device is being read or reserved for read, we cannot write it */
907 if (dev->can_read() || dev->is_reserved_for_read()) {
908 Mmsg(jcr->errmsg, _("3603 JobId=%u %s device %s is busy reading.\n"),
909 jcr->JobId, dev->print_type(), dev->print_name());
910 queue_reserve_message(jcr);
911 Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
915 /* If device is unmounted, we are out of luck */
916 if (dev->is_device_unmounted()) {
917 Mmsg(jcr->errmsg, _("3604 JobId=%u %s device %s is BLOCKED due to user unmount.\n"),
918 jcr->JobId, dev->print_type(), dev->print_name());
919 queue_reserve_message(jcr);
920 Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
924 Dmsg2(dbglvl, "reserve_append %s device is %s\n", dev->print_type(), dev->print_name());
926 /* Now do detailed tests ... */
927 if (can_reserve_drive(dcr, rctx) != 1) {
928 Dmsg0(dbglvl, "can_reserve_drive!=1\n");
932 /* Note: on failure this returns jcr->errmsg properly edited */
933 if (generate_plugin_event(jcr, bsdEventDeviceTryOpen, dcr) != bRC_OK) {
934 queue_reserve_message(jcr);
937 dcr->set_reserved_for_append();
946 * We "reserve" the drive by setting the ST_READ bit. No one else
947 * should touch the drive until that is cleared.
948 * This allows the DIR to "reserve" the device before actually
951 static bool reserve_device_for_read(DCR *dcr)
953 DEVICE *dev = dcr->dev;
957 ASSERT2(dcr, "No dcr in reserve_device_for_read!");
958 if (job_canceled(jcr)) {
964 if (dev->is_device_unmounted()) {
965 Mmsg(jcr->errmsg, _("3601 JobId=%u %s device %s is BLOCKED due to user unmount.\n"),
966 jcr->JobId, dev->print_type(), dev->print_name());
967 queue_reserve_message(jcr);
968 Dmsg1(dbglvl, "Device %s is BLOCKED due to user unmount.\n", dev->print_name());
972 if (dev->is_busy()) {
973 Mmsg(jcr->errmsg, _("3602 JobId=%u %s device %s is busy (already reading/writing)."
974 " read=%d, writers=%d reserved=%d\n"),
975 jcr->JobId, dev->print_type(), dev->print_name(),
976 dev->state & ST_READ?1:0, dev->num_writers, dev->num_reserved());
977 queue_reserve_message(jcr);
978 Dmsg4(dbglvl, "Device %s is busy ST_READ=%d num_writers=%d reserved=%d.\n",
980 dev->state & ST_READ?1:0, dev->num_writers, dev->num_reserved());
984 /* Note: on failure this returns jcr->errmsg properly edited */
985 if (generate_plugin_event(jcr, bsdEventDeviceTryOpen, dcr) != bRC_OK) {
986 queue_reserve_message(jcr);
990 dcr->set_reserved_for_read();
998 static bool is_max_jobs_ok(DCR *dcr)
1000 DEVICE *dev = dcr->dev;
1001 JCR *jcr = dcr->jcr;
1003 Dmsg6(dbglvl, "MaxJobs=%d VolCatJobs=%d writers=%d reserves=%d Status=%s Vol=%s\n",
1004 dcr->VolCatInfo.VolCatMaxJobs,
1005 dcr->VolCatInfo.VolCatJobs,
1006 dev->num_writers, dev->num_reserved(),
1007 dcr->VolCatInfo.VolCatStatus,
1009 /* Limit max concurrent jobs on this drive */
1010 if (dev->max_concurrent_jobs > 0 && (int)dev->max_concurrent_jobs <=
1011 (dev->num_writers + dev->num_reserved())) {
1012 /* Max Concurrent Jobs depassed or already reserved */
1013 Mmsg(jcr->errmsg, _("3609 JobId=%u Max concurrent jobs=%d exceeded on %s device %s.\n"),
1014 (uint32_t)jcr->JobId, dev->max_concurrent_jobs,
1015 dev->print_type(), dev->print_name());
1016 queue_reserve_message(jcr);
1017 Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
1020 if (strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0) {
1023 if (!dev->allow_maxbytes_concurrency(dcr)) {
1024 queue_reserve_message(jcr);
1025 Dmsg1(dbglvl, "reserve dev failed: %s", jcr->errmsg);
1026 return false; /* wait */
1029 if (dcr->VolCatInfo.VolCatMaxJobs > 0 && (int)dcr->VolCatInfo.VolCatMaxJobs <=
1030 (dev->num_writers + dev->num_reserved())) {
1031 /* Max Job Vols depassed or already reserved */
1032 Mmsg(jcr->errmsg, _("3611 JobId=%u Volume max jobs=%d exceeded on %s device %s.\n"),
1033 (uint32_t)jcr->JobId, dcr->VolCatInfo.VolCatMaxJobs,
1034 dev->print_type(), dev->print_name());
1035 queue_reserve_message(jcr);
1036 Dmsg1(dbglvl, "reserve dev failed: %s", jcr->errmsg);
1037 return false; /* wait */
1043 static int is_pool_ok(DCR *dcr)
1045 DEVICE *dev = dcr->dev;
1046 JCR *jcr = dcr->jcr;
1048 if (dev->num_writers >= 0) {
1049 /* Now check if we want the same Pool and pool type */
1050 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
1051 strcmp(dev->pool_type, dcr->pool_type) == 0) {
1052 /* OK, compatible device */
1053 Dmsg1(dbglvl, "OK dev: %s pool matches\n", dev->print_name());
1056 } else if (dev->num_reserved() > 0) {
1057 if (strcmp(dev->reserved_pool_name, dcr->pool_name) == 0) {
1058 /* OK, compatible device */
1059 Dmsg1(dbglvl, "OK dev: %s pool matches\n", dev->print_name());
1063 /* Drive Pool not suitable for us */
1064 Mmsg(jcr->errmsg, _(
1065 "3608 JobId=%u wants Pool=\"%s\" but have Pool=\"%s\" nreserve=%d on %s device %s.\n"),
1066 (uint32_t)jcr->JobId, dcr->pool_name, dev->pool_name,
1067 dev->num_reserved(), dev->print_type(), dev->print_name());
1068 Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
1069 queue_reserve_message(jcr);
1074 * Returns: 1 if drive can be reserved
1075 * 0 if we should wait
1076 * -1 on error or impossibility
1078 static int can_reserve_drive(DCR *dcr, RCTX &rctx)
1080 DEVICE *dev = dcr->dev;
1081 JCR *jcr = dcr->jcr;
1083 Dmsg5(dbglvl, "PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
1084 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
1085 rctx.autochanger_only, rctx.any_drive);
1087 /* Check for max jobs on this Volume */
1088 if (!is_max_jobs_ok(dcr)) {
1092 /* setting any_drive overrides PreferMountedVols flag */
1093 if (!rctx.any_drive) {
1095 * When PreferMountedVols is set, we keep track of the
1096 * drive in use that has the least number of writers, then if
1097 * no unmounted drive is found, we try that drive. This
1098 * helps spread the load to the least used drives.
1100 if (rctx.try_low_use_drive && dev == rctx.low_use_drive &&
1102 Dmsg2(dbglvl, "OK dev=%s == low_drive=%s.\n",
1103 dev->print_name(), rctx.low_use_drive->print_name());
1104 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1105 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1108 /* If he wants a free drive, but this one is busy, no go */
1109 if (!rctx.PreferMountedVols && dev->is_busy()) {
1110 Mmsg(jcr->errmsg, _("3605 JobId=%u wants free drive but %s device %s is busy.\n"),
1111 jcr->JobId, dev->print_type(), dev->print_name());
1112 queue_reserve_message(jcr);
1113 Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
1117 /* Check for prefer mounted volumes */
1118 if (rctx.PreferMountedVols && !dev->vol && dev->is_tape()) {
1119 Mmsg(jcr->errmsg, _("3606 JobId=%u prefers mounted drives, but %s device %s has no Volume.\n"),
1120 jcr->JobId, dev->print_type(), dev->print_name());
1121 queue_reserve_message(jcr);
1122 Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
1123 return 0; /* No volume mounted */
1126 /* Check for exact Volume name match */
1127 /* ***FIXME*** for Disk, we can accept any volume that goes with this
1130 if (rctx.exact_match && rctx.have_volume) {
1132 Dmsg5(dbglvl, "PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
1133 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
1134 rctx.autochanger_only, rctx.any_drive);
1135 Dmsg4(dbglvl, "have_vol=%d have=%s resvol=%s want=%s\n",
1136 rctx.have_volume, dev->VolHdr.VolumeName,
1137 dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1138 ok = strcmp(dev->VolHdr.VolumeName, rctx.VolumeName) == 0 ||
1139 (dev->vol && strcmp(dev->vol->vol_name, rctx.VolumeName) == 0);
1141 Mmsg(jcr->errmsg, _("3607 JobId=%u wants Vol=\"%s\" drive has Vol=\"%s\" on %s device %s.\n"),
1142 jcr->JobId, rctx.VolumeName, dev->VolHdr.VolumeName,
1143 dev->print_type(), dev->print_name());
1144 queue_reserve_message(jcr);
1145 Dmsg3(dbglvl, "not OK: dev have=%s resvol=%s want=%s\n",
1146 dev->VolHdr.VolumeName, dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1149 if (!dcr->can_i_use_volume()) {
1150 return 0; /* fail if volume on another drive */
1155 /* Check for unused autochanger drive */
1156 if (rctx.autochanger_only && !dev->is_busy() &&
1157 dev->VolHdr.VolumeName[0] == 0 && is_pool_ok(dcr)) {
1158 /* Device is available but not yet reserved, reserve it for us */
1159 Dmsg1(dbglvl, "OK Res Unused autochanger %s.\n", dev->print_name());
1160 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1161 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1162 return 1; /* reserve drive */
1166 * Handle the case that there are no writers
1168 if (dev->num_writers == 0) {
1169 /* Now check if there are any reservations on the drive */
1170 if (dev->num_reserved()) {
1171 return is_pool_ok(dcr);
1172 } else if (dev->can_append()) {
1173 if (is_pool_ok(dcr)) {
1176 /* Changing pool, unload old tape if any in drive */
1177 Dmsg0(dbglvl, "OK dev: num_writers=0, not reserved, pool change, unload changer\n");
1178 /* ***FIXME*** use set_unload() */
1179 unload_autochanger(dcr, -1);
1182 /* Device is available but not yet reserved, reserve it for us */
1183 Dmsg1(dbglvl, "OK Dev avail reserved %s\n", dev->print_name());
1184 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1185 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1186 return 1; /* reserve drive */
1190 * Check if the device is in append mode with writers (i.e.
1191 * available if pool is the same).
1193 if (dev->can_append() || dev->num_writers > 0 || dev->num_reserved() > 0) {
1194 return is_pool_ok(dcr);
1196 Pmsg1(000, _("Logic error!!!! JobId=%u Should not get here.\n"), (int)jcr->JobId);
1197 Mmsg(jcr->errmsg, _("3910 JobId=%u Logic error!!!! %s device %s Should not get here.\n"),
1198 jcr->JobId, dev->print_type(), dev->print_name());
1199 queue_reserve_message(jcr);
1200 Jmsg0(jcr, M_FATAL, 0, _("Logic error!!!! Should not get here.\n"));
1201 return -1; /* error, should not get here */
1203 Mmsg(jcr->errmsg, _("3911 JobId=%u failed reserve %s device %s.\n"),
1204 jcr->JobId, dev->print_type(), dev->print_name());
1205 queue_reserve_message(jcr);
1206 Dmsg1(dbglvl, "Failed: No reserve %s\n", dev->print_name());
1212 * Queue a reservation error or failure message for this jcr
1214 static void queue_reserve_message(JCR *jcr)
1222 msgs = jcr->reserve_msgs;
1227 * Look for duplicate message. If found, do
1230 for (i=msgs->size()-1; i >= 0; i--) {
1231 msg = (char *)msgs->get(i);
1235 /* Comparison based on 4 digit message number */
1236 if (strncmp(msg, jcr->errmsg, 4) == 0) {
1240 /* Message unique, so insert it */
1241 jcr->reserve_msgs->push(bstrdup(jcr->errmsg));
1248 * Send any reservation messages queued for this jcr
1250 void send_drive_reserve_messages(JCR *jcr, void sendit(const char *msg, int len, void *sarg), void *arg)
1257 msgs = jcr->reserve_msgs;
1258 if (!msgs || msgs->size() == 0) {
1261 for (i=msgs->size()-1; i >= 0; i--) {
1262 msg = (char *)msgs->get(i);
1264 sendit(" ", 3, arg);
1265 sendit(msg, strlen(msg), arg);
1276 * Pop and release any reservations messages
1278 static void pop_reserve_messages(JCR *jcr)
1284 msgs = jcr->reserve_msgs;
1288 while ((msg = (char *)msgs->pop())) {
1296 * Also called from acquire.c
1298 void release_reserve_messages(JCR *jcr)
1300 pop_reserve_messages(jcr);
1302 if (!jcr->reserve_msgs) {
1305 delete jcr->reserve_msgs;
1306 jcr->reserve_msgs = NULL;