2 * Drive reservation functions for Storage Daemon
6 * Split from job.c and acquire.c June 2005
12 Copyright (C) 2000-2005 Kern Sibbald
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License
16 version 2 as amended with additional clauses defined in the
17 file LICENSE in the main source directory.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 the file LICENSE for additional details.
30 * Use Device command from Director
31 * He tells is what Device Name to use, the Media Type,
32 * the Pool Name, and the Pool Type.
34 * Ensure that the device exists and is opened, then store
35 * the media and pool info in the JCR. This class is used
36 * only temporarily in this file.
42 char name[MAX_NAME_LENGTH];
43 char media_type[MAX_NAME_LENGTH];
44 char pool_name[MAX_NAME_LENGTH];
45 char pool_type[MAX_NAME_LENGTH];
56 bool PreferMountedVols;
60 bool available_autochanger;
61 char VolumeName[MAX_NAME_LENGTH];
64 static dlist *vol_list = NULL;
65 static pthread_mutex_t vol_list_lock = PTHREAD_MUTEX_INITIALIZER;
66 static pthread_mutex_t search_lock = PTHREAD_MUTEX_INITIALIZER;
68 /* Forward referenced functions */
69 static int can_reserve_drive(DCR *dcr, RCTX &rctx);
70 static int search_res_for_device(RCTX &rctx);
71 static int reserve_device(RCTX &rctx);
72 static bool reserve_device_for_read(DCR *dcr);
73 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx);
74 static bool use_storage_cmd(JCR *jcr);
75 bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx);
77 /* Requests from the Director daemon */
78 static char use_storage[] = "use storage=%127s media_type=%127s "
79 "pool_name=%127s pool_type=%127s append=%d copy=%d stripe=%d\n";
80 static char use_device[] = "use device=%127s\n";
82 /* Responses sent to Director daemon */
83 static char OK_device[] = "3000 OK use device device=%s\n";
84 static char NO_device[] = "3924 Device \"%s\" not in SD Device resources.\n";
85 static char BAD_use[] = "3913 Bad use command: %s\n";
87 bool use_cmd(JCR *jcr)
90 * Get the device, media, and pool information
92 if (!use_storage_cmd(jcr)) {
93 set_jcr_job_status(jcr, JS_ErrorTerminated);
94 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
100 static int my_compare(void *item1, void *item2)
102 return strcmp(((VOLRES *)item1)->vol_name, ((VOLRES *)item2)->vol_name);
107 * Put a new Volume entry in the Volume list. This
108 * effectively reserves the volume so that it will
109 * not be mounted again.
111 * Return: VOLRES entry on success
112 * NULL if the Volume is already in the list
114 VOLRES *new_volume(DCR *dcr, const char *VolumeName)
118 Dmsg1(400, "new_volume %s\n", VolumeName);
122 foreach_dlist(vol, vol_list) {
123 if (vol && vol->dev == dcr->dev) {
124 vol_list->remove(vol);
133 vol = (VOLRES *)malloc(sizeof(VOLRES));
134 memset(vol, 0, sizeof(VOLRES));
135 vol->vol_name = bstrdup(VolumeName);
138 nvol = (VOLRES *)vol_list->binary_insert(vol, my_compare);
144 nvol->dev = dcr->dev;
152 * Search for a Volume name in the Volume list.
154 * Returns: VOLRES entry on success
155 * NULL if the Volume is not in the list
157 VOLRES *find_volume(const char *VolumeName)
161 vol.vol_name = bstrdup(VolumeName);
162 fvol = (VOLRES *)vol_list->binary_search(&vol, my_compare);
169 * Free a Volume from the Volume list
171 * Returns: true if the Volume found and removed from the list
172 * false if the Volume is not in the list
174 bool free_volume(DEVICE *dev)
179 if (dev->VolHdr.VolumeName[0] == 0) {
181 * Our device has no VolumeName listed, but
182 * search the list for any Volume attached to
183 * this device and remove it.
185 foreach_dlist(fvol, vol_list) {
186 if (fvol && fvol->dev == dev) {
187 vol_list->remove(fvol);
188 if (fvol->vol_name) {
189 free(fvol->vol_name);
197 Dmsg1(400, "free_volume %s\n", dev->VolHdr.VolumeName);
198 vol.vol_name = bstrdup(dev->VolHdr.VolumeName);
199 fvol = (VOLRES *)vol_list->binary_search(&vol, my_compare);
201 vol_list->remove(fvol);
202 free(fvol->vol_name);
206 dev->VolHdr.VolumeName[0] = 0;
212 /* Free volume reserved by this dcr but not attached to a dev */
213 void free_unused_volume(DCR *dcr)
217 for (vol=(VOLRES *)vol_list->first(); vol; vol=(VOLRES *)vol_list->next(vol)) {
218 if (vol->dcr == dcr && (vol->dev == NULL ||
219 strcmp(vol->vol_name, vol->dev->VolHdr.VolumeName) != 0)) {
220 vol_list->remove(vol);
230 * List Volumes -- this should be moved to status.c
232 void list_volumes(BSOCK *user)
235 for (vol=(VOLRES *)vol_list->first(); vol; vol=(VOLRES *)vol_list->next(vol)) {
237 bnet_fsend(user, "%s on device %s\n", vol->vol_name, vol->dev->print_name());
239 bnet_fsend(user, "%s\n", vol->vol_name);
244 /* Create the Volume list */
245 void create_volume_list()
247 VOLRES *dummy = NULL;
248 if (vol_list == NULL) {
249 vol_list = New(dlist(dummy, &dummy->link));
253 /* Release all Volumes from the list */
254 void free_volume_list()
260 for (vol=(VOLRES *)vol_list->first(); vol; vol=(VOLRES *)vol_list->next(vol)) {
261 Dmsg3(000, "Unreleased Volume=%s dcr=0x%x dev=0x%x\n", vol->vol_name,
268 bool is_volume_in_use(DCR *dcr)
270 VOLRES *vol = find_volume(dcr->VolumeName);
272 return false; /* vol not in list */
274 if (!vol->dev) { /* vol not attached to device */
277 if (dcr->dev == vol->dev) { /* same device OK */
280 if (!vol->dev->is_busy()) {
287 static bool use_storage_cmd(JCR *jcr)
289 POOL_MEM store_name, dev_name, media_type, pool_name, pool_type;
290 BSOCK *dir = jcr->dir_bsock;
300 memset(&rctx, 0, sizeof(RCTX));
303 * If there are multiple devices, the director sends us
304 * use_device for each device that it wants to use.
306 jcr->dirstore = New(alist(10, not_owned_by_alist));
308 Dmsg1(100, "<dird: %s", dir->msg);
309 ok = sscanf(dir->msg, use_storage, store_name.c_str(),
310 media_type.c_str(), pool_name.c_str(),
311 pool_type.c_str(), &append, &Copy, &Stripe) == 7;
315 unbash_spaces(store_name);
316 unbash_spaces(media_type);
317 unbash_spaces(pool_name);
318 unbash_spaces(pool_type);
319 store = new DIRSTORE;
320 jcr->dirstore->append(store);
321 memset(store, 0, sizeof(DIRSTORE));
322 store->device = New(alist(10));
323 bstrncpy(store->name, store_name, sizeof(store->name));
324 bstrncpy(store->media_type, media_type, sizeof(store->media_type));
325 bstrncpy(store->pool_name, pool_name, sizeof(store->pool_name));
326 bstrncpy(store->pool_type, pool_type, sizeof(store->pool_type));
327 store->append = append;
329 /* Now get all devices */
330 while (bnet_recv(dir) >= 0) {
331 Dmsg1(100, "<dird device: %s", dir->msg);
332 ok = sscanf(dir->msg, use_device, dev_name.c_str()) == 1;
336 unbash_spaces(dev_name);
337 store->device->append(bstrdup(dev_name.c_str()));
339 } while (ok && bnet_recv(dir) >= 0);
342 /* This loop is debug code and can be removed */
343 /* ***FIXME**** remove after 1.38 release */
345 foreach_alist(store, jcr->dirstore) {
346 Dmsg4(100, "Storage=%s media_type=%s pool=%s pool_type=%s\n",
347 store->name, store->media_type, store->pool_name,
349 foreach_alist(device_name, store->device) {
350 Dmsg1(100, " Device=%s\n", device_name);
356 * At this point, we have a list of all the Director's Storage
357 * resources indicated for this Job, which include Pool, PoolType,
358 * storage name, and Media type.
359 * Then for each of the Storage resources, we have a list of
360 * device names that were given.
362 * Wiffle through them and find one that can do the backup.
366 * First look for an exact match of Volume name as the
367 * tape may already be mounted.
369 rctx.do_not_wait = true;
370 rctx.exact_match = true;
371 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
374 rctx.exact_match = false;
376 /* Now search if an unused autochanger slot is available */
377 rctx.available_autochanger = true;
378 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
381 rctx.available_autochanger = false;
385 * Make up to two passes. The first with PreferMountedVols possibly
386 * set to true. In that case, we look only for an available
387 * drive with something mounted. If that fails, then we
388 * do a second pass with PerferMountedVols set false.
390 rctx.PreferMountedVols = jcr->PreferMountedVols;
391 if (!rctx.PreferMountedVols) {
392 rctx.do_not_wait = false;
394 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
397 if (rctx.PreferMountedVols) {
398 rctx.PreferMountedVols = false;
399 rctx.do_not_wait = false;
400 if ((ok = find_suitable_device_for_job(jcr, rctx))) {
405 unbash_spaces(dir->msg);
406 pm_strcpy(jcr->errmsg, dir->msg);
407 Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
409 Jmsg(jcr, M_FATAL, 0, _("\n"
410 " Device \"%s\" with MediaType \"%s\" requested by DIR not found in SD Device resources.\n"),
411 dev_name.c_str(), media_type.c_str());
412 bnet_fsend(dir, NO_device, dev_name.c_str());
414 for (error=(char*)rctx->errors.first(); error;
415 error=(char*)rctx->errors.next()) {
416 Jmsg(jcr, M_INFO, 0, "%s", error);
419 Dmsg1(100, ">dird: %s", dir->msg);
421 unbash_spaces(dir->msg);
422 pm_strcpy(jcr->errmsg, dir->msg);
424 Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
426 bnet_fsend(dir, BAD_use, jcr->errmsg);
427 Dmsg1(100, ">dird: %s", dir->msg);
431 foreach_alist(store, jcr->dirstore) {
432 delete store->device;
435 delete jcr->dirstore;
437 for (error=(char*)rctx->errors.first(); error;
438 error=(char*)rctx->errors.next()) {
447 * Search for a device suitable for this job.
449 bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
457 init_jcr_device_wait_timers(jcr);
459 int can_wait = false;
462 foreach_alist(store, jcr->dirstore) {
464 foreach_alist(device_name, store->device) {
466 rctx.device_name = device_name;
467 stat = search_res_for_device(rctx);
468 if (stat == 1) { /* found available device */
471 } else if (stat == 0) { /* device busy */
474 /* otherwise error */
475 // rctx->errors.push(bstrdup(jcr->errmsg));
483 * We did not find a suitable device, so
484 * if there is some device for which we can wait, then
485 * wait and try again until the wait time expires
487 if (rctx.do_not_wait || !can_wait || !wait_for_device(jcr, first)) {
490 first = false; /* first wait complete */
493 for (error=(char*)rctx->errors.first(); error;
494 error=(char*)rctx->errors.next()) {
506 * Search for a particular storage device with particular storage
507 * characteristics (MediaType).
509 static int search_res_for_device(RCTX &rctx)
511 AUTOCHANGER *changer;
512 BSOCK *dir = rctx.jcr->dir_bsock;
516 Dmsg1(100, "Search res for %s\n", rctx.device_name);
517 if (!rctx.available_autochanger) {
518 foreach_res(rctx.device, R_DEVICE) {
519 Dmsg1(100, "Try res=%s\n", rctx.device->hdr.name);
520 /* Find resource, and make sure we were able to open it */
521 if (fnmatch(rctx.device_name, rctx.device->hdr.name, 0) == 0) {
522 stat = reserve_device(rctx);
526 Dmsg1(220, "Got: %s", dir->msg);
527 bash_spaces(rctx.device_name);
528 ok = bnet_fsend(dir, OK_device, rctx.device_name);
529 Dmsg1(100, ">dird dev: %s", dir->msg);
534 foreach_res(changer, R_AUTOCHANGER) {
535 Dmsg1(100, "Try changer res=%s\n", changer->hdr.name);
536 /* Find resource, and make sure we were able to open it */
537 if (fnmatch(rctx.device_name, changer->hdr.name, 0) == 0) {
538 /* Try each device in this AutoChanger */
539 foreach_alist(rctx.device, changer->device) {
540 Dmsg1(100, "Try changer device %s\n", rctx.device->hdr.name);
541 stat = reserve_device(rctx);
542 if (stat == -1) { /* hard error */
545 if (stat == 0) { /* must wait, try next one */
549 Dmsg1(100, "Device %s opened.\n", rctx.device_name);
550 pm_strcpy(dev_name, rctx.device->hdr.name);
551 bash_spaces(dev_name);
552 ok = bnet_fsend(dir, OK_device, dev_name.c_str()); /* Return real device name */
553 Dmsg1(100, ">dird changer: %s", dir->msg);
558 return 0; /* nothing found */
562 * Try to reserve a specific device.
564 * Returns: 1 -- OK, have DCR
568 static int reserve_device(RCTX &rctx)
572 const int name_len = MAX_NAME_LENGTH;
574 /* Make sure MediaType is OK */
575 if (strcmp(rctx.device->media_type, rctx.store->media_type) != 0) {
579 if (!rctx.device->dev) {
580 rctx.device->dev = init_dev(rctx.jcr, rctx.device);
582 if (!rctx.device->dev) {
583 if (rctx.device->changer_res) {
584 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
585 " Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
586 rctx.device->hdr.name, rctx.device_name);
588 Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
589 " Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
592 return -1; /* no use waiting */
594 Dmsg1(100, "Found device %s\n", rctx.device->hdr.name);
595 dcr = new_dcr(rctx.jcr, rctx.device->dev);
597 BSOCK *dir = rctx.jcr->dir_bsock;
598 bnet_fsend(dir, _("3926 Could not get dcr for device: %s\n"), rctx.device_name);
599 Dmsg1(100, ">dird: %s", dir->msg);
603 bstrncpy(dcr->pool_name, rctx.store->pool_name, name_len);
604 bstrncpy(dcr->pool_type, rctx.store->pool_type, name_len);
605 bstrncpy(dcr->media_type, rctx.store->media_type, name_len);
606 bstrncpy(dcr->dev_name, rctx.device_name, name_len);
607 if (rctx.store->append == SD_APPEND) {
608 if (rctx.exact_match && !rctx.have_volume) {
609 dcr->any_volume = true;
610 if (dir_find_next_appendable_volume(dcr)) {
611 Dmsg1(200, "Looking for Volume=%s\n", dcr->VolumeName);
612 bstrncpy(rctx.VolumeName, dcr->VolumeName, sizeof(rctx.VolumeName));
613 rctx.have_volume = true;
615 Dmsg0(200, "No next volume found\n");
616 rctx.VolumeName[0] = 0;
619 ok = reserve_device_for_append(dcr, rctx);
620 Dmsg3(200, "dev_name=%s mediatype=%s ok=%d\n", dcr->dev_name, dcr->media_type, ok);
622 ok = reserve_device_for_read(dcr);
625 free_dcr(rctx.jcr->dcr);
632 * We "reserve" the drive by setting the ST_READ bit. No one else
633 * should touch the drive until that is cleared.
634 * This allows the DIR to "reserve" the device before actually
637 static bool reserve_device_for_read(DCR *dcr)
639 DEVICE *dev = dcr->dev;
645 dev->block(BST_DOING_ACQUIRE);
647 if (is_device_unmounted(dev)) {
648 Dmsg1(200, "Device %s is BLOCKED due to user unmount.\n", dev->print_name());
649 Mmsg(jcr->errmsg, _("Device %s is BLOCKED due to user unmount.\n"),
654 if (dev->is_busy()) {
655 Dmsg4(200, "Device %s is busy ST_READ=%d num_writers=%d reserved=%d.\n", dev->print_name(),
656 dev->state & ST_READ?1:0, dev->num_writers, dev->reserved_device);
657 Mmsg1(jcr->errmsg, _("Device %s is busy.\n"),
673 * We reserve the device for appending by incrementing the
674 * reserved_device. We do virtually all the same work that
675 * is done in acquire_device_for_append(), but we do
676 * not attempt to mount the device. This routine allows
677 * the DIR to reserve multiple devices before *really*
678 * starting the job. It also permits the SD to refuse
679 * certain devices (not up, ...).
681 * Note, in reserving a device, if the device is for the
682 * same pool and the same pool type, then it is acceptable.
683 * The Media Type has already been checked. If we are
684 * the first tor reserve the device, we put the pool
685 * name and pool type in the device record.
687 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx)
690 DEVICE *dev = dcr->dev;
695 dev->block(BST_DOING_ACQUIRE);
697 if (dev->can_read()) {
698 Mmsg1(jcr->errmsg, _("Device %s is busy reading.\n"), dev->print_name());
699 Dmsg1(100, "%s", jcr->errmsg);
703 if (is_device_unmounted(dev)) {
704 Mmsg(jcr->errmsg, _("Device %s is BLOCKED due to user unmount.\n"), dev->print_name());
705 Dmsg1(100, "%s", jcr->errmsg);
709 Dmsg1(190, "reserve_append device is %s\n", dev->is_tape()?"tape":"disk");
711 if (can_reserve_drive(dcr, rctx) != 1) {
712 Dmsg1(100, "%s", jcr->errmsg);
716 dev->reserved_device++;
717 Dmsg1(200, "Inc reserve=%d\n", dev->reserved_device);
718 dcr->reserved_device = true;
727 * Returns: 1 if drive can be reserved
728 * 0 if we should wait
731 static int can_reserve_drive(DCR *dcr, RCTX &rctx)
733 DEVICE *dev = dcr->dev;
736 /* Check for prefer mounted volumes */
737 if (rctx.PreferMountedVols && !dev->VolHdr.VolumeName[0] && dev->is_tape()) {
738 Mmsg0(jcr->errmsg, _("Want mounted Volume, drive is empty\n"));
739 Dmsg0(200, "want mounted -- no vol\n");
740 return 0; /* No volume mounted */
743 /* Check for exact Volume name match */
744 if (rctx.exact_match && rctx.have_volume &&
745 strcmp(dev->VolHdr.VolumeName, rctx.VolumeName) != 0) {
746 Mmsg2(jcr->errmsg, _("Not exact match have=%s want=%s\n"),
747 dev->VolHdr.VolumeName, rctx.VolumeName);
748 Dmsg2(200, "Not exact match have=%s want=%s\n",
749 dev->VolHdr.VolumeName, rctx.VolumeName);
753 /* Check for unused autochanger drive */
754 if (rctx.available_autochanger && dev->num_writers == 0 &&
755 dev->VolHdr.VolumeName[0] == 0) {
756 /* Device is available but not yet reserved, reserve it for us */
757 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
758 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
759 return 1; /* reserve drive */
763 * Handle the case that there are no writers
765 if (dev->num_writers == 0) {
766 /* Now check if there are any reservations on the drive */
767 if (dev->reserved_device) {
768 /* Now check if we want the same Pool and pool type */
769 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
770 strcmp(dev->pool_type, dcr->pool_type) == 0) {
771 /* OK, compatible device */
772 Dmsg0(200, "got dev: num_writers=0, reserved, pool matches\n");
775 /* Drive not suitable for us */
776 Mmsg2(jcr->errmsg, _("Drive busy wrong pool: num_writers=0, reserved, pool=%s wanted=%s\n"),
777 dev->pool_name, dcr->pool_name);
778 Dmsg2(200, "busy: num_writers=0, reserved, pool=%s wanted=%s\n",
779 dev->pool_name, dcr->pool_name);
782 } else if (dev->can_append()) {
783 /* Device in append mode, check if changing pool */
784 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
785 strcmp(dev->pool_type, dcr->pool_type) == 0) {
786 Dmsg0(200, "got dev: num_writers=0, can_append, pool matches\n");
787 /* OK, compatible device */
790 /* Changing pool, unload old tape if any in drive */
791 Dmsg0(200, "got dev: num_writers=0, reserved, pool change\n");
792 unload_autochanger(dcr, 0);
795 /* Device is available but not yet reserved, reserve it for us */
796 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
797 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
798 return 1; /* reserve drive */
802 * Check if the device is in append mode with writers (i.e.
803 * available if pool is the same).
805 if (dev->can_append() || dev->num_writers > 0) {
806 Dmsg0(190, "device already in append.\n");
807 /* Yes, now check if we want the same Pool and pool type */
808 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
809 strcmp(dev->pool_type, dcr->pool_type) == 0) {
810 Dmsg0(200, "got dev: num_writers>=0, can_append, pool matches\n");
811 /* OK, compatible device */
814 /* Drive not suitable for us */
815 Mmsg(jcr->errmsg, _("Wanted Pool \"%s\", but device %s is using Pool \"%s\" .\n"),
816 dcr->pool_name, dev->print_name(), dev->pool_name);
817 Dmsg2(200, "busy: num_writers>0, can_append, pool=%s wanted=%s\n",
818 dev->pool_name, dcr->pool_name);
822 Pmsg0(000, _("Logic error!!!! Should not get here.\n"));
823 Jmsg0(jcr, M_FATAL, 0, _("Logic error!!!! Should not get here.\n"));
824 return -1; /* error, should not get here */