2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from many
7 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 Bacula® is a registered trademark of Kern Sibbald.
17 * Drive reservation functions for Storage Daemon
19 * Written by Kern Sibbald, MM
21 * Split from job.c and acquire.c June 2005
28 const int dbglvl = 150;
30 static brwlock_t reservation_lock;
31 int reservations_lock_count = 0;
33 /* Forward referenced functions */
34 static int can_reserve_drive(DCR *dcr, RCTX &rctx);
35 static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol);
36 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx);
37 static bool reserve_device_for_read(DCR *dcr);
38 static bool use_device_cmd(JCR *jcr);
39 static int reserve_device(RCTX &rctx);
40 static void pop_reserve_messages(JCR *jcr);
41 static void queue_reserve_message(JCR *jcr);
42 //void switch_device(DCR *dcr, DEVICE *dev);
44 /* Requests from the Director daemon */
45 static char use_storage[] = "use storage=%127s media_type=%127s "
46 "pool_name=%127s pool_type=%127s append=%d copy=%d stripe=%d\n";
47 static char use_device[] = "use device=%127s\n";
49 /* Responses sent to Director daemon */
50 static char OK_device[] = "3000 OK use device device=%s\n";
51 static char NO_device[] = "3924 Device \"%s\" not in SD Device"
52 " resources or no matching Media Type.\n";
53 static char BAD_use[] = "3913 Bad use command: %s\n";
56 * This allows a given thread to recursively call lock_reservations.
57 * It must, of course, call unlock_... the same number of times.
59 void init_reservations_lock()
62 if ((errstat=rwl_init(&reservation_lock)) != 0) {
64 Emsg1(M_ABORT, 0, _("Unable to initialize reservation lock. ERR=%s\n"),
65 be.bstrerror(errstat));
72 /* This applies to a drive and to Volumes */
73 void _lock_reservations(const char *file, int line)
76 reservations_lock_count++;
77 if ((errstat=rwl_writelock_p(&reservation_lock, file, line)) != 0) {
79 Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
80 errstat, be.bstrerror(errstat));
84 void _unlock_reservations()
87 reservations_lock_count--;
88 if ((errstat=rwl_writeunlock(&reservation_lock)) != 0) {
90 Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
91 errstat, be.bstrerror(errstat));
95 void term_reservations_lock()
97 rwl_destroy(&reservation_lock);
101 void DCR::clear_reserved()
106 Dmsg2(dbglvl, "Dec reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
110 void DCR::set_reserved_for_append()
113 dev->set_append_reserve();
114 Dmsg2(dbglvl, "Inc reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
118 void DCR::set_reserved_for_read()
121 dev->set_read_reserve();
122 Dmsg2(dbglvl, "Inc reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
127 * Remove any reservation from a drive and tell the system
128 * that the volume is unused at least by us.
130 void DCR::unreserve_device(bool locked)
137 reserved_volume = false;
138 /* If we set read mode in reserving, remove it */
139 if (dev->can_read()) {
140 remove_read_volume(jcr, this->VolumeName);
143 if (dev->num_writers < 0) {
144 Jmsg1(jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
145 dev->num_writers = 0;
147 if (dev->num_reserved() == 0 && dev->num_writers == 0) {
148 generate_plugin_event(jcr, bsdEventDeviceClose, this);
157 bool use_cmd(JCR *jcr)
160 * Get the device, media, and pool information
162 if (!use_device_cmd(jcr)) {
163 jcr->setJobStatus(JS_ErrorTerminated);
164 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
171 * We get the following type of information:
173 * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=1 copy=0 strip=0
177 * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=0 copy=0 strip=0
181 static bool use_device_cmd(JCR *jcr)
183 POOL_MEM store_name, dev_name, media_type, pool_name, pool_type;
184 BSOCK *dir = jcr->dir_bsock;
187 int32_t Copy, Stripe;
192 memset(&rctx, 0, sizeof(RCTX));
195 * If there are multiple devices, the director sends us
196 * use_device for each device that it wants to use.
198 dirstore = New(alist(10, not_owned_by_alist));
199 jcr->reserve_msgs = New(alist(10, not_owned_by_alist));
201 Dmsg1(dbglvl, "<dird: %s", dir->msg);
202 ok = sscanf(dir->msg, use_storage, store_name.c_str(),
203 media_type.c_str(), pool_name.c_str(),
204 pool_type.c_str(), &append, &Copy, &Stripe) == 7;
209 jcr->write_store = dirstore;
211 jcr->read_store = dirstore;
213 rctx.append = append;
214 unbash_spaces(store_name);
215 unbash_spaces(media_type);
216 unbash_spaces(pool_name);
217 unbash_spaces(pool_type);
218 store = new DIRSTORE;
219 dirstore->append(store);
220 memset(store, 0, sizeof(DIRSTORE));
221 store->device = New(alist(10));
222 bstrncpy(store->name, store_name, sizeof(store->name));
223 bstrncpy(store->media_type, media_type, sizeof(store->media_type));
224 bstrncpy(store->pool_name, pool_name, sizeof(store->pool_name));
225 bstrncpy(store->pool_type, pool_type, sizeof(store->pool_type));
226 store->append = append;
228 /* Now get all devices */
229 while (dir->recv() >= 0) {
230 Dmsg1(dbglvl, "<dird device: %s", dir->msg);
231 ok = sscanf(dir->msg, use_device, dev_name.c_str()) == 1;
235 unbash_spaces(dev_name);
236 store->device->append(bstrdup(dev_name.c_str()));
238 } while (ok && dir->recv() >= 0);
241 /* Developers debug code */
243 if (debug_level >= dbglvl) {
244 foreach_alist(store, dirstore) {
245 Dmsg5(dbglvl, "Storage=%s media_type=%s pool=%s pool_type=%s append=%d\n",
246 store->name, store->media_type, store->pool_name,
247 store->pool_type, store->append);
248 foreach_alist(device_name, store->device) {
249 Dmsg1(dbglvl, " Device=%s\n", device_name);
255 init_jcr_device_wait_timers(jcr);
256 jcr->dcr = new_dcr(jcr, NULL, NULL, !rctx.append); /* get a dcr */
258 BSOCK *dir = jcr->dir_bsock;
259 dir->fsend(_("3939 Could not get dcr\n"));
260 Dmsg1(dbglvl, ">dird: %s", dir->msg);
264 * At this point, we have a list of all the Director's Storage
265 * resources indicated for this Job, which include Pool, PoolType,
266 * storage name, and Media type.
267 * Then for each of the Storage resources, we have a list of
268 * device names that were given.
270 * Wiffle through them and find one that can do the backup.
273 int wait_for_device_retries = 0;
276 rctx.notify_dir = true;
278 /* Put new dcr in proper location */
280 rctx.jcr->dcr = jcr->dcr;
282 rctx.jcr->read_dcr = jcr->dcr;
285 for ( ; !fail && !job_canceled(jcr); ) {
286 pop_reserve_messages(jcr);
287 rctx.suitable_device = false;
288 rctx.have_volume = false;
289 rctx.VolumeName[0] = 0;
290 rctx.any_drive = false;
291 if (!jcr->PreferMountedVols) {
293 * Here we try to find a drive that is not used.
294 * This will maximize the use of available drives.
297 rctx.num_writers = 20000000; /* start with impossible number */
298 rctx.low_use_drive = NULL;
299 rctx.PreferMountedVols = false;
300 rctx.exact_match = false;
301 rctx.autochanger_only = true;
302 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
305 /* Look through all drives possibly for low_use drive */
306 if (rctx.low_use_drive) {
307 rctx.try_low_use_drive = true;
308 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
311 rctx.try_low_use_drive = false;
313 rctx.autochanger_only = false;
314 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
319 * Now we look for a drive that may or may not be in
322 /* Look for an exact Volume match all drives */
323 rctx.PreferMountedVols = true;
324 rctx.exact_match = true;
325 rctx.autochanger_only = false;
326 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
329 /* Look for any mounted drive */
330 rctx.exact_match = false;
331 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
335 rctx.any_drive = true;
336 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
339 /* Keep reservations locked *except* during wait_for_device() */
340 unlock_reservations();
342 * The idea of looping on repeat a few times it to ensure
343 * that if there is some subtle timing problem between two
344 * jobs, we will simply try again, and most likely succeed.
345 * This can happen if one job reserves a drive or finishes using
346 * a drive at the same time a second job wants it.
348 if (repeat++ > 1) { /* try algorithm 3 times */
349 bmicrosleep(30, 0); /* wait a bit */
350 Dmsg1(100, "repeat reserve algorithm JobId=%d\n", jcr->JobId);
351 } else if (!rctx.suitable_device || !wait_for_any_device(jcr, wait_for_device_retries)) {
352 Dmsg0(100, "Fail. !suitable_device || !wait_for_device\n");
356 dir->signal(BNET_HEARTBEAT); /* Inform Dir that we are alive */
358 unlock_reservations();
362 * If we get here, there are no suitable devices available, which
363 * means nothing configured. If a device is suitable but busy
364 * with another Volume, we will not come here.
366 unbash_spaces(dir->msg);
367 pm_strcpy(jcr->errmsg, dir->msg);
368 Jmsg(jcr, M_FATAL, 0, _("Device reservation failed for JobId=%d: %s\n"),
369 jcr->JobId, jcr->errmsg);
370 dir->fsend(NO_device, dev_name.c_str());
372 Dmsg1(dbglvl, ">dird: %s", dir->msg);
375 unbash_spaces(dir->msg);
376 pm_strcpy(jcr->errmsg, dir->msg);
377 Jmsg(jcr, M_FATAL, 0, _("Failed command: %s\n"), jcr->errmsg);
378 dir->fsend(BAD_use, jcr->errmsg);
379 Dmsg1(dbglvl, ">dird: %s", dir->msg);
382 release_reserve_messages(jcr);
387 * Search for a device suitable for this job.
388 * Note, this routine sets sets rctx.suitable_device if any
389 * device exists within the SD. The device may not be actually
391 * It also returns if it finds a useable device.
393 bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
402 dirstore = jcr->write_store;
404 dirstore = jcr->read_store;
406 Dmsg5(dbglvl, "Start find_suit_dev PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
407 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
408 rctx.autochanger_only, rctx.any_drive);
411 * If the appropriate conditions of this if are met, namely that
412 * we are appending and the user wants mounted drive (or we
413 * force try a mounted drive because they are all busy), we
414 * start by looking at all the Volumes in the volume list.
416 if (!is_vol_list_empty() && rctx.append && rctx.PreferMountedVols) {
417 dlist *temp_vol_list;
419 temp_vol_list = dup_vol_list(jcr);
421 /* Look through reserved volumes for one we can use */
422 Dmsg0(dbglvl, "look for vol in vol list\n");
423 foreach_dlist(vol, temp_vol_list) {
425 Dmsg1(dbglvl, "vol=%s no dev\n", vol->vol_name);
428 /* Check with Director if this Volume is OK */
429 bstrncpy(dcr->VolumeName, vol->vol_name, sizeof(dcr->VolumeName));
430 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
434 Dmsg1(dbglvl, "vol=%s OK for this job\n", vol->vol_name);
435 foreach_alist(store, dirstore) {
438 foreach_alist(device_name, store->device) {
439 /* Found a device, try to use it */
440 rctx.device_name = device_name;
441 rctx.device = vol->dev->device;
443 if (vol->dev->read_only) {
446 if (vol->dev->is_autochanger()) {
447 Dmsg1(dbglvl, "vol=%s is in changer\n", vol->vol_name);
448 if (!is_vol_in_autochanger(rctx, vol) || vol->dev->autoselect) {
451 } else if (strcmp(device_name, vol->dev->device->hdr.name) != 0) {
452 Dmsg2(dbglvl, "device=%s not suitable want %s\n",
453 vol->dev->device->hdr.name, device_name);
457 bstrncpy(rctx.VolumeName, vol->vol_name, sizeof(rctx.VolumeName));
458 rctx.have_volume = true;
459 /* Try reserving this device and volume */
460 Dmsg2(dbglvl, "try vol=%s on device=%s\n", rctx.VolumeName, device_name);
461 stat = reserve_device(rctx);
462 if (stat == 1) { /* found available device */
463 Dmsg1(dbglvl, "Suitable device found=%s\n", device_name);
466 } else if (stat == 0) { /* device busy */
467 Dmsg1(dbglvl, "Suitable device=%s, busy: not use\n", device_name);
469 /* otherwise error */
470 Dmsg0(dbglvl, "No suitable device found.\n");
472 rctx.have_volume = false;
473 rctx.VolumeName[0] = 0;
482 } /* end for loop over reserved volumes */
484 Dmsg0(dbglvl, "lock volumes\n");
485 free_temp_vol_list(temp_vol_list);
488 Dmsg1(dbglvl, "OK dev found. Vol=%s from in-use vols list\n", rctx.VolumeName);
493 * No reserved volume we can use, so now search for an available device.
495 * For each storage device that the user specified, we
496 * search and see if there is a resource for that device.
498 foreach_alist(store, dirstore) {
500 foreach_alist(device_name, store->device) {
502 rctx.device_name = device_name;
503 stat = search_res_for_device(rctx);
504 if (stat == 1) { /* found available device */
505 Dmsg1(dbglvl, "available device found=%s\n", device_name);
508 } else if (stat == 0) { /* device busy */
509 Dmsg1(dbglvl, "No usable device=%s, busy: not use\n", device_name);
511 /* otherwise error */
512 Dmsg0(dbglvl, "No usable device found.\n");
520 Dmsg1(dbglvl, "OK dev found. Vol=%s\n", rctx.VolumeName);
522 Dmsg0(dbglvl, "Leave find_suit_dev: no dev found.\n");
528 * Search for a particular storage device with particular storage
529 * characteristics (MediaType).
531 int search_res_for_device(RCTX &rctx)
533 AUTOCHANGER *changer;
536 Dmsg1(dbglvl, "search res for %s\n", rctx.device_name);
537 /* Look through Autochangers first */
538 foreach_res(changer, R_AUTOCHANGER) {
539 Dmsg1(dbglvl, "Try match changer res=%s\n", changer->hdr.name);
540 /* Find resource, and make sure we were able to open it */
541 if (strcmp(rctx.device_name, changer->hdr.name) == 0) {
542 /* Try each device in this AutoChanger */
543 foreach_alist(rctx.device, changer->device) {
544 Dmsg1(dbglvl, "Try changer device %s\n", rctx.device->hdr.name);
545 if (rctx.store->append && rctx.device->read_only) {
548 if (!rctx.device->autoselect) {
549 Dmsg1(100, "Device %s not autoselect skipped.\n",
550 rctx.device->hdr.name);
551 continue; /* device is not available */
553 stat = reserve_device(rctx);
554 if (stat != 1) { /* try another device */
558 if (rctx.store->append) {
559 Dmsg2(dbglvl, "Device %s reserved=%d for append.\n",
560 rctx.device->hdr.name, rctx.jcr->dcr->dev->num_reserved());
562 Dmsg2(dbglvl, "Device %s reserved=%d for read.\n",
563 rctx.device->hdr.name, rctx.jcr->read_dcr->dev->num_reserved());
570 /* Now if requested look through regular devices */
571 if (!rctx.autochanger_only) {
572 foreach_res(rctx.device, R_DEVICE) {
573 Dmsg1(dbglvl, "Try match res=%s\n", rctx.device->hdr.name);
574 /* Find resource, and make sure we were able to open it */
575 if (strcmp(rctx.device_name, rctx.device->hdr.name) == 0) {
576 stat = reserve_device(rctx);
577 if (stat != 1) { /* try another device */
581 if (rctx.store->append) {
582 Dmsg2(dbglvl, "Device %s reserved=%d for append.\n",
583 rctx.device->hdr.name, rctx.jcr->dcr->dev->num_reserved());
585 Dmsg2(dbglvl, "Device %s reserved=%d for read.\n",
586 rctx.device->hdr.name, rctx.jcr->read_dcr->dev->num_reserved());
592 return -1; /* nothing found */
596 * Walk through the autochanger resources and check if
597 * the volume is in one of them.
599 * Returns: true if volume is in device
602 static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol)
604 AUTOCHANGER *changer = vol->dev->device->changer_res;
606 /* Find resource, and make sure we were able to open it */
607 if (changer && strcmp(rctx.device_name, changer->hdr.name) == 0) {
608 Dmsg1(dbglvl, "Found changer device %s\n", vol->dev->device->hdr.name);
611 Dmsg1(dbglvl, "Incorrect changer device %s\n", changer->hdr.name);
617 * Try to reserve a specific device.
619 * Returns: 1 -- OK, have DCR
623 static int reserve_device(RCTX &rctx)
627 const int name_len = MAX_NAME_LENGTH;
629 /* Make sure MediaType is OK */
630 Dmsg2(dbglvl, "chk MediaType device=%s request=%s\n",
631 rctx.device->media_type, rctx.store->media_type);
632 if (strcmp(rctx.device->media_type, rctx.store->media_type) != 0) {
636 /* Make sure device exists -- i.e. we can stat() it */
637 if (!rctx.device->dev) {
638 rctx.device->dev = init_dev(rctx.jcr, rctx.device);
640 if (!rctx.device->dev) {
641 if (rctx.device->changer_res) {
642 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
643 " Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
644 rctx.device->hdr.name, rctx.device_name);
646 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
647 " Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
650 return -1; /* no use waiting */
653 rctx.suitable_device = true;
654 Dmsg1(dbglvl, "try reserve %s\n", rctx.device->hdr.name);
655 if (rctx.store->append) {
656 dcr = new_dcr(rctx.jcr, rctx.jcr->dcr, rctx.device->dev, SD_APPEND);
658 dcr = new_dcr(rctx.jcr, rctx.jcr->read_dcr, rctx.device->dev, SD_READ);
661 BSOCK *dir = rctx.jcr->dir_bsock;
662 dir->fsend(_("3926 Could not get dcr for device: %s\n"), rctx.device_name);
663 Dmsg1(dbglvl, ">dird: %s", dir->msg);
666 bstrncpy(dcr->pool_name, rctx.store->pool_name, name_len);
667 bstrncpy(dcr->pool_type, rctx.store->pool_type, name_len);
668 bstrncpy(dcr->media_type, rctx.store->media_type, name_len);
669 bstrncpy(dcr->dev_name, rctx.device_name, name_len);
670 if (rctx.store->append) {
671 Dmsg2(dbglvl, "call reserve for append: have_vol=%d vol=%s\n", rctx.have_volume, rctx.VolumeName);
672 ok = reserve_device_for_append(dcr, rctx);
678 Dmsg5(dbglvl, "Reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
679 dcr->dev->num_reserved(),
680 dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
681 Dmsg3(dbglvl, "Vol=%s num_writers=%d, have_vol=%d\n",
682 rctx.VolumeName, dcr->dev->num_writers, rctx.have_volume);
683 if (rctx.have_volume) {
684 Dmsg0(dbglvl, "Call reserve_volume for append.\n");
685 if (reserve_volume(dcr, rctx.VolumeName)) {
686 Dmsg1(dbglvl, "Reserved vol=%s\n", rctx.VolumeName);
688 Dmsg1(dbglvl, "Could not reserve vol=%s\n", rctx.VolumeName);
692 dcr->any_volume = true;
693 Dmsg0(dbglvl, "no vol, call find_next_appendable_vol.\n");
694 if (dir_find_next_appendable_volume(dcr)) {
695 bstrncpy(rctx.VolumeName, dcr->VolumeName, sizeof(rctx.VolumeName));
696 rctx.have_volume = true;
697 Dmsg1(dbglvl, "looking for Volume=%s\n", rctx.VolumeName);
699 dcr->dev->clear_wait();
700 Dmsg0(dbglvl, "No next volume found\n");
701 rctx.have_volume = false;
702 rctx.VolumeName[0] = 0;
704 * If there is at least one volume that is valid and in use,
705 * but we get here, check if we are running with prefers
706 * non-mounted drives. In that case, we have selected a
707 * non-used drive and our one and only volume is mounted
708 * elsewhere, so we bail out and retry using that drive.
710 if (dcr->found_in_use() && !rctx.PreferMountedVols) {
711 rctx.PreferMountedVols = true;
712 if (dcr->VolumeName[0]) {
713 dcr->unreserve_device(false);
718 * Note. Under some circumstances, the Director can hand us
719 * a Volume name that is not the same as the one on the current
720 * drive, and in that case, the call above to find the next
721 * volume will fail because in attempting to reserve the Volume
722 * the code will realize that we already have a tape mounted,
723 * and it will fail. This *should* only happen if there are
724 * writers, thus the following test. In that case, we simply
725 * bail out, and continue waiting, rather than plunging on
726 * and hoping that the operator can resolve the problem.
728 if (dcr->dev->num_writers != 0) {
729 if (dcr->VolumeName[0]) {
730 dcr->unreserve_device(false);
737 ok = reserve_device_for_read(dcr);
739 rctx.jcr->read_dcr = dcr;
740 Dmsg5(dbglvl, "Read reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
741 dcr->dev->num_reserved(),
742 dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
749 if (rctx.notify_dir) {
751 BSOCK *dir = rctx.jcr->dir_bsock;
752 pm_strcpy(dev_name, rctx.device->hdr.name);
753 bash_spaces(dev_name);
754 ok = dir->fsend(OK_device, dev_name.c_str()); /* Return real device name */
755 Dmsg1(dbglvl, ">dird: %s", dir->msg);
757 dcr->unreserve_device(false);
765 rctx.have_volume = false;
766 rctx.VolumeName[0] = 0;
767 Dmsg0(dbglvl, "Not OK.\n");
773 * We reserve the device for appending by incrementing
774 * num_reserved(). We do virtually all the same work that
775 * is done in acquire_device_for_append(), but we do
776 * not attempt to mount the device. This routine allows
777 * the DIR to reserve multiple devices before *really*
778 * starting the job. It also permits the SD to refuse
779 * certain devices (not up, ...).
781 * Note, in reserving a device, if the device is for the
782 * same pool and the same pool type, then it is acceptable.
783 * The Media Type has already been checked. If we are
784 * the first tor reserve the device, we put the pool
785 * name and pool type in the device record.
787 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx)
790 DEVICE *dev = dcr->dev;
793 ASSERT2(dcr, "No dcr in reserve_device_for_append!");
794 if (job_canceled(jcr)) {
800 /* If device is being read or reserved for read, we cannot write it */
801 if (dev->can_read() || dev->is_reserved_for_read()) {
802 Mmsg(jcr->errmsg, _("3603 JobId=%u %s device %s is busy reading.\n"),
803 jcr->JobId, dev->print_type(), dev->print_name());
804 queue_reserve_message(jcr);
805 Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
809 /* If device is unmounted, we are out of luck */
810 if (dev->is_device_unmounted()) {
811 Mmsg(jcr->errmsg, _("3604 JobId=%u %s device %s is BLOCKED due to user unmount.\n"),
812 jcr->JobId, dev->print_type(), dev->print_name());
813 queue_reserve_message(jcr);
814 Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
818 Dmsg2(dbglvl, "reserve_append %s device is %s\n", dev->print_type(), dev->print_name());
820 /* Now do detailed tests ... */
821 if (can_reserve_drive(dcr, rctx) != 1) {
822 Dmsg0(dbglvl, "can_reserve_drive!=1\n");
826 /* Note: on failure this returns jcr->errmsg properly edited */
827 if (generate_plugin_event(jcr, bsdEventDeviceTryOpen, dcr) != bRC_OK) {
828 queue_reserve_message(jcr);
831 dcr->set_reserved_for_append();
840 * We "reserve" the drive by setting the ST_READ bit. No one else
841 * should touch the drive until that is cleared.
842 * This allows the DIR to "reserve" the device before actually
845 static bool reserve_device_for_read(DCR *dcr)
847 DEVICE *dev = dcr->dev;
851 ASSERT2(dcr, "No dcr in reserve_device_for_read!");
852 if (job_canceled(jcr)) {
858 if (dev->is_device_unmounted()) {
859 Mmsg(jcr->errmsg, _("3601 JobId=%u %s device %s is BLOCKED due to user unmount.\n"),
860 jcr->JobId, dev->print_type(), dev->print_name());
861 queue_reserve_message(jcr);
862 Dmsg1(dbglvl, "Device %s is BLOCKED due to user unmount.\n", dev->print_name());
866 if (dev->is_busy()) {
867 Mmsg(jcr->errmsg, _("3602 JobId=%u %s device %s is busy (already reading/writing)."
868 " read=%d, writers=%d reserved=%d\n"),
869 jcr->JobId, dev->print_type(), dev->print_name(),
870 dev->state & ST_READ?1:0, dev->num_writers, dev->num_reserved());
871 queue_reserve_message(jcr);
872 Dmsg4(dbglvl, "Device %s is busy ST_READ=%d num_writers=%d reserved=%d.\n",
874 dev->state & ST_READ?1:0, dev->num_writers, dev->num_reserved());
878 /* Note: on failure this returns jcr->errmsg properly edited */
879 if (generate_plugin_event(jcr, bsdEventDeviceTryOpen, dcr) != bRC_OK) {
880 queue_reserve_message(jcr);
884 dcr->set_reserved_for_read();
892 static bool is_max_jobs_ok(DCR *dcr)
894 DEVICE *dev = dcr->dev;
897 Dmsg5(dbglvl, "MaxJobs=%d Jobs=%d reserves=%d Status=%s Vol=%s\n",
898 dcr->VolCatInfo.VolCatMaxJobs,
899 dcr->VolCatInfo.VolCatJobs, dev->num_reserved(),
900 dcr->VolCatInfo.VolCatStatus,
902 /* Limit max concurrent jobs on this drive */
903 if (dev->max_concurrent_jobs > 0 && dev->max_concurrent_jobs <=
904 (uint32_t)(dev->num_writers + dev->num_reserved())) {
905 /* Max Concurrent Jobs depassed or already reserved */
906 Mmsg(jcr->errmsg, _("3609 JobId=%u Max concurrent jobs=%d exceeded on %s device %s.\n"),
907 (uint32_t)jcr->JobId, dev->max_concurrent_jobs,
908 dev->print_type(), dev->print_name());
909 queue_reserve_message(jcr);
910 Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
913 if (strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0) {
917 if (dcr->VolCatInfo.VolCatMaxJobs > 0 && dcr->VolCatInfo.VolCatMaxJobs <=
918 (dcr->VolCatInfo.VolCatJobs + dev->num_reserved())) {
919 /* Max Job Vols depassed or already reserved */
920 Mmsg(jcr->errmsg, _("3611 JobId=%u Volume max jobs=%d exceeded on %s device %s.\n"),
921 (uint32_t)jcr->JobId, dcr->VolCatInfo.VolCatMaxJobs,
922 dev->print_type(), dev->print_name());
923 queue_reserve_message(jcr);
924 Dmsg1(dbglvl, "reserve dev failed: %s", jcr->errmsg);
925 return false; /* wait */
931 static int is_pool_ok(DCR *dcr)
933 DEVICE *dev = dcr->dev;
936 /* Now check if we want the same Pool and pool type */
937 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
938 strcmp(dev->pool_type, dcr->pool_type) == 0) {
939 /* OK, compatible device */
940 Dmsg1(dbglvl, "OK dev: %s num_writers=0, reserved, pool matches\n", dev->print_name());
943 /* Drive Pool not suitable for us */
945 "3608 JobId=%u wants Pool=\"%s\" but have Pool=\"%s\" nreserve=%d on %s device %s.\n"),
946 (uint32_t)jcr->JobId, dcr->pool_name, dev->pool_name,
947 dev->num_reserved(), dev->print_type(), dev->print_name());
948 Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
949 queue_reserve_message(jcr);
955 * Returns: 1 if drive can be reserved
956 * 0 if we should wait
957 * -1 on error or impossibility
959 static int can_reserve_drive(DCR *dcr, RCTX &rctx)
961 DEVICE *dev = dcr->dev;
964 Dmsg5(dbglvl, "PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
965 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
966 rctx.autochanger_only, rctx.any_drive);
968 /* Check for max jobs on this Volume */
969 if (!is_max_jobs_ok(dcr)) {
973 /* setting any_drive overrides PreferMountedVols flag */
974 if (!rctx.any_drive) {
976 * When PreferMountedVols is set, we keep track of the
977 * drive in use that has the least number of writers, then if
978 * no unmounted drive is found, we try that drive. This
979 * helps spread the load to the least used drives.
981 if (rctx.try_low_use_drive && dev == rctx.low_use_drive) {
982 Dmsg2(dbglvl, "OK dev=%s == low_drive=%s.\n",
983 dev->print_name(), rctx.low_use_drive->print_name());
986 /* If he wants a free drive, but this one is busy, no go */
987 if (!rctx.PreferMountedVols && dev->is_busy()) {
988 /* Save least used drive */
989 if ((dev->num_writers + dev->num_reserved()) < rctx.num_writers) {
990 rctx.num_writers = dev->num_writers + dev->num_reserved();
991 rctx.low_use_drive = dev;
992 Dmsg2(dbglvl, "set low use drive=%s num_writers=%d\n",
993 dev->print_name(), rctx.num_writers);
995 Dmsg1(dbglvl, "not low use num_writers=%d\n", dev->num_writers+dev->num_reserved());
997 Mmsg(jcr->errmsg, _("3605 JobId=%u wants free drive but %s device %s is busy.\n"),
998 jcr->JobId, dev->print_type(), dev->print_name());
999 queue_reserve_message(jcr);
1000 Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
1004 /* Check for prefer mounted volumes */
1005 if (rctx.PreferMountedVols && !dev->vol && dev->is_tape()) {
1006 Mmsg(jcr->errmsg, _("3606 JobId=%u prefers mounted drives, but %s device %s has no Volume.\n"),
1007 jcr->JobId, dev->print_type(), dev->print_name());
1008 queue_reserve_message(jcr);
1009 Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
1010 return 0; /* No volume mounted */
1013 /* Check for exact Volume name match */
1014 /* ***FIXME*** for Disk, we can accept any volume that goes with this
1017 if (rctx.exact_match && rctx.have_volume) {
1019 Dmsg5(dbglvl, "PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
1020 rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
1021 rctx.autochanger_only, rctx.any_drive);
1022 Dmsg4(dbglvl, "have_vol=%d have=%s resvol=%s want=%s\n",
1023 rctx.have_volume, dev->VolHdr.VolumeName,
1024 dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1025 ok = strcmp(dev->VolHdr.VolumeName, rctx.VolumeName) == 0 ||
1026 (dev->vol && strcmp(dev->vol->vol_name, rctx.VolumeName) == 0);
1028 Mmsg(jcr->errmsg, _("3607 JobId=%u wants Vol=\"%s\" drive has Vol=\"%s\" on %s device %s.\n"),
1029 jcr->JobId, rctx.VolumeName, dev->VolHdr.VolumeName,
1030 dev->print_type(), dev->print_name());
1031 queue_reserve_message(jcr);
1032 Dmsg3(dbglvl, "not OK: dev have=%s resvol=%s want=%s\n",
1033 dev->VolHdr.VolumeName, dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1036 if (!dcr->can_i_use_volume()) {
1037 return 0; /* fail if volume on another drive */
1042 /* Check for unused autochanger drive */
1043 if (rctx.autochanger_only && !dev->is_busy() &&
1044 dev->VolHdr.VolumeName[0] == 0) {
1045 /* Device is available but not yet reserved, reserve it for us */
1046 Dmsg1(dbglvl, "OK Res Unused autochanger %s.\n", dev->print_name());
1047 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1048 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1049 return 1; /* reserve drive */
1053 * Handle the case that there are no writers
1055 if (dev->num_writers == 0) {
1056 /* Now check if there are any reservations on the drive */
1057 if (dev->num_reserved()) {
1058 return is_pool_ok(dcr);
1059 } else if (dev->can_append()) {
1060 if (is_pool_ok(dcr)) {
1063 /* Changing pool, unload old tape if any in drive */
1064 Dmsg0(dbglvl, "OK dev: num_writers=0, not reserved, pool change, unload changer\n");
1065 /* ***FIXME*** use set_unload() */
1066 unload_autochanger(dcr, -1);
1069 /* Device is available but not yet reserved, reserve it for us */
1070 Dmsg1(dbglvl, "OK Dev avail reserved %s\n", dev->print_name());
1071 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1072 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1073 return 1; /* reserve drive */
1077 * Check if the device is in append mode with writers (i.e.
1078 * available if pool is the same).
1080 if (dev->can_append() || dev->num_writers > 0) {
1081 return is_pool_ok(dcr);
1083 Pmsg1(000, _("Logic error!!!! JobId=%u Should not get here.\n"), (int)jcr->JobId);
1084 Mmsg(jcr->errmsg, _("3910 JobId=%u Logic error!!!! %s device %s Should not get here.\n"),
1085 jcr->JobId, dev->print_type(), dev->print_name());
1086 queue_reserve_message(jcr);
1087 Jmsg0(jcr, M_FATAL, 0, _("Logic error!!!! Should not get here.\n"));
1088 return -1; /* error, should not get here */
1090 Mmsg(jcr->errmsg, _("3911 JobId=%u failed reserve %s device %s.\n"),
1091 jcr->JobId, dev->print_type(), dev->print_name());
1092 queue_reserve_message(jcr);
1093 Dmsg1(dbglvl, "Failed: No reserve %s\n", dev->print_name());
1099 * Queue a reservation error or failure message for this jcr
1101 static void queue_reserve_message(JCR *jcr)
1109 msgs = jcr->reserve_msgs;
1114 * Look for duplicate message. If found, do
1117 for (i=msgs->size()-1; i >= 0; i--) {
1118 msg = (char *)msgs->get(i);
1122 /* Comparison based on 4 digit message number */
1123 if (strncmp(msg, jcr->errmsg, 4) == 0) {
1127 /* Message unique, so insert it */
1128 jcr->reserve_msgs->push(bstrdup(jcr->errmsg));
1135 * Send any reservation messages queued for this jcr
1137 void send_drive_reserve_messages(JCR *jcr, void sendit(const char *msg, int len, void *sarg), void *arg)
1144 msgs = jcr->reserve_msgs;
1145 if (!msgs || msgs->size() == 0) {
1148 for (i=msgs->size()-1; i >= 0; i--) {
1149 msg = (char *)msgs->get(i);
1151 sendit(" ", 3, arg);
1152 sendit(msg, strlen(msg), arg);
1163 * Pop and release any reservations messages
1165 static void pop_reserve_messages(JCR *jcr)
1171 msgs = jcr->reserve_msgs;
1175 while ((msg = (char *)msgs->pop())) {
1183 * Also called from acquire.c
1185 void release_reserve_messages(JCR *jcr)
1187 pop_reserve_messages(jcr);
1189 if (!jcr->reserve_msgs) {
1192 delete jcr->reserve_msgs;
1193 jcr->reserve_msgs = NULL;