]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/reserve.c
Backport from BEE
[bacula/bacula] / bacula / src / stored / reserve.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
5
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.
8
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.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  *   Drive reservation functions for Storage Daemon
18  *
19  *   Written by Kern Sibbald, MM
20  *
21  *   Split from job.c and acquire.c June 2005
22  *
23  */
24
25 #include "bacula.h"
26 #include "stored.h"
27
28 const int dbglvl = 150;
29
30 static brwlock_t reservation_lock;
31 int reservations_lock_count = 0;
32
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);
43
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";
48
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";
54
55 /*
56  * This allows a given thread to recursively call lock_reservations.
57  *   It must, of course, call unlock_... the same number of times.
58  */
59 void init_reservations_lock()
60 {
61    int errstat;
62    if ((errstat=rwl_init(&reservation_lock)) != 0) {
63       berrno be;
64       Emsg1(M_ABORT, 0, _("Unable to initialize reservation lock. ERR=%s\n"),
65             be.bstrerror(errstat));
66    }
67
68    init_vol_list_lock();
69 }
70
71
72 /* This applies to a drive and to Volumes */
73 void _lock_reservations(const char *file, int line)
74 {
75    int errstat;
76    reservations_lock_count++;
77    if ((errstat=rwl_writelock_p(&reservation_lock, file, line)) != 0) {
78       berrno be;
79       Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
80            errstat, be.bstrerror(errstat));
81    }
82 }
83
84 void _unlock_reservations()
85 {
86    int errstat;
87    reservations_lock_count--;
88    if ((errstat=rwl_writeunlock(&reservation_lock)) != 0) {
89       berrno be;
90       Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
91            errstat, be.bstrerror(errstat));
92    }
93 }
94
95 void term_reservations_lock()
96 {
97    rwl_destroy(&reservation_lock);
98    term_vol_list_lock();
99 }
100
101 void DCR::clear_reserved()
102 {
103    if (m_reserved) {
104       m_reserved = false;
105       dev->dec_reserved();
106       Dmsg2(dbglvl, "Dec reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
107    }
108 }
109
110 void DCR::set_reserved_for_append()
111 {
112    m_reserved = true;
113    dev->set_append_reserve();
114    Dmsg2(dbglvl, "Inc reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
115    dev->inc_reserved();
116 }
117
118 void DCR::set_reserved_for_read()
119 {
120    m_reserved = true;
121    dev->set_read_reserve();
122    Dmsg2(dbglvl, "Inc reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
123    dev->inc_reserved();
124 }
125
126 /*
127  * Remove any reservation from a drive and tell the system
128  *  that the volume is unused at least by us.
129  */
130 void DCR::unreserve_device(bool locked)
131 {
132    if (!locked) {
133       dev->Lock();
134    }
135    if (is_reserved()) {
136       clear_reserved();
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);
141          dev->clear_read();
142       }
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;
146       }
147       if (dev->num_reserved() == 0 && dev->num_writers == 0) {
148          generate_plugin_event(jcr, bsdEventDeviceClose, this);
149          volume_unused(this);
150       }
151    }
152    if (!locked) {
153       dev->Unlock();
154    }
155 }
156
157 bool use_cmd(JCR *jcr)
158 {
159    /*
160     * Get the device, media, and pool information
161     */
162    if (!use_device_cmd(jcr)) {
163       jcr->setJobStatus(JS_ErrorTerminated);
164       memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
165       return false;
166    }
167    return true;
168 }
169
170 /*
171  * We get the following type of information:
172  *
173  * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=1 copy=0 strip=0
174  *  use device=zzz
175  *  use device=aaa
176  *  use device=bbb
177  * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=0 copy=0 strip=0
178  *  use device=bbb
179  *
180  */
181 static bool use_device_cmd(JCR *jcr)
182 {
183    POOL_MEM store_name, dev_name, media_type, pool_name, pool_type;
184    BSOCK *dir = jcr->dir_bsock;
185    int32_t append;
186    bool ok;
187    int32_t Copy, Stripe;
188    DIRSTORE *store;
189    RCTX rctx;
190    alist *dirstore;
191
192    memset(&rctx, 0, sizeof(RCTX));
193    rctx.jcr = jcr;
194    /*
195     * If there are multiple devices, the director sends us
196     *   use_device for each device that it wants to use.
197     */
198    dirstore = New(alist(10, not_owned_by_alist));
199    jcr->reserve_msgs = New(alist(10, not_owned_by_alist));
200    do {
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;
205       if (!ok) {
206          break;
207       }
208       if (append) {
209          jcr->write_store = dirstore;
210       } else {
211          jcr->read_store = dirstore;
212       }
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;
227
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;
232          if (!ok) {
233             break;
234          }
235          unbash_spaces(dev_name);
236          store->device->append(bstrdup(dev_name.c_str()));
237       }
238    }  while (ok && dir->recv() >= 0);
239
240 #ifdef xxxx
241    /* Developers debug code */
242    char *device_name;
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);
250          }
251       }
252    }
253 #endif
254
255    init_jcr_device_wait_timers(jcr);
256    jcr->dcr = new_dcr(jcr, NULL, NULL, !rctx.append);   /* get a dcr */
257    if (!jcr->dcr) {
258       BSOCK *dir = jcr->dir_bsock;
259       dir->fsend(_("3939 Could not get dcr\n"));
260       Dmsg1(dbglvl, ">dird: %s", dir->msg);
261       ok = false;
262    }
263    /*
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.
269     *
270     * Wiffle through them and find one that can do the backup.
271     */
272    if (ok) {
273       int wait_for_device_retries = 0;
274       int repeat = 0;
275       bool fail = false;
276       rctx.notify_dir = true;
277
278       /* Put new dcr in proper location */
279       if (rctx.append) {
280          rctx.jcr->dcr = jcr->dcr;
281       } else {
282          rctx.jcr->read_dcr = jcr->dcr;
283       }
284       lock_reservations();
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) {
292             /*
293              * Here we try to find a drive that is not used.
294              * This will maximize the use of available drives.
295              *
296              */
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))) {
303                break;
304             }
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))) {
309                   break;
310                }
311                rctx.try_low_use_drive = false;
312             }
313             rctx.autochanger_only = false;
314             if ((ok = find_suitable_device_for_job(jcr, rctx))) {
315                break;
316             }
317          }
318          /*
319           * Now we look for a drive that may or may not be in
320           *  use.
321           */
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))) {
327             break;
328          }
329          /* Look for any mounted drive */
330          rctx.exact_match = false;
331          if ((ok = find_suitable_device_for_job(jcr, rctx))) {
332             break;
333          }
334          /* Try any drive */
335          rctx.any_drive = true;
336          if ((ok = find_suitable_device_for_job(jcr, rctx))) {
337             break;
338          }
339          /* Keep reservations locked *except* during wait_for_device() */
340          unlock_reservations();
341          /*
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.
347           */
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");
353             fail = true;
354          }
355          lock_reservations();
356          dir->signal(BNET_HEARTBEAT);  /* Inform Dir that we are alive */
357       }
358       unlock_reservations();
359
360       if (!ok) {
361          /*
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.
365           */
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());
371
372          Dmsg1(dbglvl, ">dird: %s", dir->msg);
373       }
374    } else {
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);
380    }
381
382    release_reserve_messages(jcr);
383    return ok;
384 }
385
386 /*
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
390  *   useable.
391  * It also returns if it finds a useable device.
392  */
393 bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
394 {
395    bool ok = false;
396    DIRSTORE *store;
397    char *device_name;
398    alist *dirstore;
399    DCR *dcr = jcr->dcr;
400
401    if (rctx.append) {
402       dirstore = jcr->write_store;
403    } else {
404       dirstore = jcr->read_store;
405    }
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);
409
410    /*
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.
415     */
416    if (!is_vol_list_empty() && rctx.append && rctx.PreferMountedVols) {
417       dlist *temp_vol_list;
418       VOLRES *vol = NULL;
419       temp_vol_list = dup_vol_list(jcr);
420
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) {
424          if (!vol->dev) {
425             Dmsg1(dbglvl, "vol=%s no dev\n", vol->vol_name);
426             continue;
427          }
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)) {
431             continue;
432          }
433
434          Dmsg1(dbglvl, "vol=%s OK for this job\n", vol->vol_name);
435          foreach_alist(store, dirstore) {
436             int stat;
437             rctx.store = store;
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;
442
443                if (vol->dev->read_only) {
444                   continue;
445                }
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) {
449                      continue;
450                   }
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);
454                   continue;
455                }
456
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);
464                   ok = true;
465                   break;
466                } else if (stat == 0) {      /* device busy */
467                   Dmsg1(dbglvl, "Suitable device=%s, busy: not use\n", device_name);
468                } else {
469                   /* otherwise error */
470                   Dmsg0(dbglvl, "No suitable device found.\n");
471                }
472                rctx.have_volume = false;
473                rctx.VolumeName[0] = 0;
474             }
475             if (ok) {
476                break;
477             }
478          }
479          if (ok) {
480             break;
481          }
482       } /* end for loop over reserved volumes */
483
484       Dmsg0(dbglvl, "lock volumes\n");
485       free_temp_vol_list(temp_vol_list);
486    }
487    if (ok) {
488       Dmsg1(dbglvl, "OK dev found. Vol=%s from in-use vols list\n", rctx.VolumeName);
489       return true;
490    }
491
492    /*
493     * No reserved volume we can use, so now search for an available device.
494     *
495     * For each storage device that the user specified, we
496     *  search and see if there is a resource for that device.
497     */
498    foreach_alist(store, dirstore) {
499       rctx.store = store;
500       foreach_alist(device_name, store->device) {
501          int stat;
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);
506             ok = true;
507             break;
508          } else if (stat == 0) {      /* device busy */
509             Dmsg1(dbglvl, "No usable device=%s, busy: not use\n", device_name);
510          } else {
511             /* otherwise error */
512             Dmsg0(dbglvl, "No usable device found.\n");
513          }
514       }
515       if (ok) {
516          break;
517       }
518    }
519    if (ok) {
520       Dmsg1(dbglvl, "OK dev found. Vol=%s\n", rctx.VolumeName);
521    } else {
522       Dmsg0(dbglvl, "Leave find_suit_dev: no dev found.\n");
523    }
524    return ok;
525 }
526
527 /*
528  * Search for a particular storage device with particular storage
529  *  characteristics (MediaType).
530  */
531 int search_res_for_device(RCTX &rctx)
532 {
533    AUTOCHANGER *changer;
534    int stat;
535
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) {
546                continue;
547             }
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 */
552             }
553             stat = reserve_device(rctx);
554             if (stat != 1) {             /* try another device */
555                continue;
556             }
557             /* Debug code */
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());
561             } else {
562                Dmsg2(dbglvl, "Device %s reserved=%d for read.\n",
563                   rctx.device->hdr.name, rctx.jcr->read_dcr->dev->num_reserved());
564             }
565             return stat;
566          }
567       }
568    }
569
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 */
578                continue;
579             }
580             /* Debug code */
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());
584             } else {
585                Dmsg2(dbglvl, "Device %s reserved=%d for read.\n",
586                   rctx.device->hdr.name, rctx.jcr->read_dcr->dev->num_reserved());
587             }
588             return stat;
589          }
590       }
591    }
592    return -1;                    /* nothing found */
593 }
594
595 /*
596  * Walk through the autochanger resources and check if
597  *  the volume is in one of them.
598  *
599  * Returns:  true  if volume is in device
600  *           false otherwise
601  */
602 static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol)
603 {
604    AUTOCHANGER *changer = vol->dev->device->changer_res;
605
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);
609       return true;
610    }
611    Dmsg1(dbglvl, "Incorrect changer device %s\n", changer->hdr.name);
612    return false;
613 }
614
615
616 /*
617  *  Try to reserve a specific device.
618  *
619  *  Returns: 1 -- OK, have DCR
620  *           0 -- must wait
621  *          -1 -- fatal error
622  */
623 static int reserve_device(RCTX &rctx)
624 {
625    bool ok;
626    DCR *dcr;
627    const int name_len = MAX_NAME_LENGTH;
628
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) {
633       return -1;
634    }
635
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);
639    }
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);
645       } else {
646          Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
647             "     Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
648               rctx.device_name);
649       }
650       return -1;  /* no use waiting */
651    }
652
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);
657    } else {
658       dcr = new_dcr(rctx.jcr, rctx.jcr->read_dcr, rctx.device->dev, SD_READ);
659    }
660    if (!dcr) {
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);
664       return -1;
665    }
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);
673       if (!ok) {
674          goto bail_out;
675       }
676
677       rctx.jcr->dcr = dcr;
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);
687          } else {
688             Dmsg1(dbglvl, "Could not reserve vol=%s\n", rctx.VolumeName);
689             goto bail_out;
690          }
691       } else {
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);
698          } else {
699             dcr->dev->clear_wait();
700             Dmsg0(dbglvl, "No next volume found\n");
701             rctx.have_volume = false;
702             rctx.VolumeName[0] = 0;
703             /*
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.
709              */
710             if (dcr->found_in_use() && !rctx.PreferMountedVols) {
711                rctx.PreferMountedVols = true;
712                if (dcr->VolumeName[0]) {
713                   dcr->unreserve_device(false);
714                }
715                goto bail_out;
716             }
717             /*
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.
727              */
728             if (dcr->dev->num_writers != 0) {
729                if (dcr->VolumeName[0]) {
730                   dcr->unreserve_device(false);
731                }
732                goto bail_out;
733             }
734          }
735       }
736    } else {
737       ok = reserve_device_for_read(dcr);
738       if (ok) {
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);
743       }
744    }
745    if (!ok) {
746       goto bail_out;
747    }
748
749    if (rctx.notify_dir) {
750       POOL_MEM dev_name;
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);
756       if (!ok) {
757          dcr->unreserve_device(false);
758       }
759    } else {
760       ok = true;
761    }
762    return ok ? 1 : -1;
763
764 bail_out:
765    rctx.have_volume = false;
766    rctx.VolumeName[0] = 0;
767    Dmsg0(dbglvl, "Not OK.\n");
768    return 0;
769 }
770
771
772 /*
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, ...).
780  *
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.
786  */
787 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx)
788 {
789    JCR *jcr = dcr->jcr;
790    DEVICE *dev = dcr->dev;
791    bool ok = false;
792
793    ASSERT2(dcr, "No dcr in reserve_device_for_append!");
794    if (job_canceled(jcr)) {
795       return false;
796    }
797
798    dev->Lock();
799
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);
806       goto bail_out;
807    }
808
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);
815       goto bail_out;
816    }
817
818    Dmsg2(dbglvl, "reserve_append %s device is %s\n", dev->print_type(), dev->print_name());
819
820    /* Now do detailed tests ... */
821    if (can_reserve_drive(dcr, rctx) != 1) {
822       Dmsg0(dbglvl, "can_reserve_drive!=1\n");
823       goto bail_out;
824    }
825
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);
829       goto bail_out;
830    }
831    dcr->set_reserved_for_append();
832    ok = true;
833
834 bail_out:
835    dev->Unlock();
836    return ok;
837 }
838
839 /*
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
843  *  starting the job.
844  */
845 static bool reserve_device_for_read(DCR *dcr)
846 {
847    DEVICE *dev = dcr->dev;
848    JCR *jcr = dcr->jcr;
849    bool ok = false;
850
851    ASSERT2(dcr, "No dcr in reserve_device_for_read!");
852    if (job_canceled(jcr)) {
853       return false;
854    }
855
856    dev->Lock();
857
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());
863       goto bail_out;
864    }
865
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",
873          dev->print_name(),
874          dev->state & ST_READ?1:0, dev->num_writers, dev->num_reserved());
875       goto bail_out;
876    }
877
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);
881       goto bail_out;
882    }
883    dev->clear_append();
884    dcr->set_reserved_for_read();
885    ok = true;
886
887 bail_out:
888    dev->Unlock();
889    return ok;
890 }
891
892 static bool is_max_jobs_ok(DCR *dcr)
893 {
894    DEVICE *dev = dcr->dev;
895    JCR *jcr = dcr->jcr;
896
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,
901          dcr->VolumeName);
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);
911       return false;
912    }
913    if (strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0) {
914       return true;
915    }
916
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 */
926    }
927    return true;
928 }
929
930
931 static int is_pool_ok(DCR *dcr)
932 {
933    DEVICE *dev = dcr->dev;
934    JCR *jcr = dcr->jcr;
935
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());
941       return 1;
942    } else {
943       /* Drive Pool not suitable for us */
944       Mmsg(jcr->errmsg, _(
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);
950    }
951    return 0;
952 }
953
954 /*
955  * Returns: 1 if drive can be reserved
956  *          0 if we should wait
957  *         -1 on error or impossibility
958  */
959 static int can_reserve_drive(DCR *dcr, RCTX &rctx)
960 {
961    DEVICE *dev = dcr->dev;
962    JCR *jcr = dcr->jcr;
963
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);
967
968    /* Check for max jobs on this Volume */
969    if (!is_max_jobs_ok(dcr)) {
970       return 0;
971    }
972
973    /* setting any_drive overrides PreferMountedVols flag */
974    if (!rctx.any_drive) {
975       /*
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.
980        */
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());
984          return 1;
985       }
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);
994          } else {
995             Dmsg1(dbglvl, "not low use num_writers=%d\n", dev->num_writers+dev->num_reserved());
996          }
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);
1001          return 0;
1002       }
1003
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 */
1011       }
1012
1013       /* Check for exact Volume name match */
1014       /* ***FIXME*** for Disk, we can accept any volume that goes with this
1015        *    drive.
1016        */
1017       if (rctx.exact_match && rctx.have_volume) {
1018          bool ok;
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);
1027          if (!ok) {
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);
1034             return 0;
1035          }
1036          if (!dcr->can_i_use_volume()) {
1037             return 0;              /* fail if volume on another drive */
1038          }
1039       }
1040    }
1041
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 */
1050    }
1051
1052    /*
1053     * Handle the case that there are no writers
1054     */
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)) {
1061             return 1;
1062          } else {
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);
1067          }
1068       }
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 */
1074    }
1075
1076    /*
1077     * Check if the device is in append mode with writers (i.e.
1078     *  available if pool is the same).
1079     */
1080    if (dev->can_append() || dev->num_writers > 0) {
1081       return is_pool_ok(dcr);
1082    } else {
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 */
1089    }
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());
1094    return 0;
1095 }
1096
1097
1098 /*
1099  * Queue a reservation error or failure message for this jcr
1100  */
1101 static void queue_reserve_message(JCR *jcr)
1102 {
1103    int i;
1104    alist *msgs;
1105    char *msg;
1106
1107    jcr->lock();
1108
1109    msgs = jcr->reserve_msgs;
1110    if (!msgs) {
1111       goto bail_out;
1112    }
1113    /*
1114     * Look for duplicate message.  If found, do
1115     * not insert
1116     */
1117    for (i=msgs->size()-1; i >= 0; i--) {
1118       msg = (char *)msgs->get(i);
1119       if (!msg) {
1120          goto bail_out;
1121       }
1122       /* Comparison based on 4 digit message number */
1123       if (strncmp(msg, jcr->errmsg, 4) == 0) {
1124          goto bail_out;
1125       }
1126    }
1127    /* Message unique, so insert it */
1128    jcr->reserve_msgs->push(bstrdup(jcr->errmsg));
1129
1130 bail_out:
1131    jcr->unlock();
1132 }
1133
1134 /*
1135  * Send any reservation messages queued for this jcr
1136  */
1137 void send_drive_reserve_messages(JCR *jcr, void sendit(const char *msg, int len, void *sarg), void *arg)
1138 {
1139    int i;
1140    alist *msgs;
1141    char *msg;
1142
1143    jcr->lock();
1144    msgs = jcr->reserve_msgs;
1145    if (!msgs || msgs->size() == 0) {
1146       goto bail_out;
1147    }
1148    for (i=msgs->size()-1; i >= 0; i--) {
1149       msg = (char *)msgs->get(i);
1150       if (msg) {
1151          sendit("   ", 3, arg);
1152          sendit(msg, strlen(msg), arg);
1153       } else {
1154          break;
1155       }
1156    }
1157
1158 bail_out:
1159    jcr->unlock();
1160 }
1161
1162 /*
1163  * Pop and release any reservations messages
1164  */
1165 static void pop_reserve_messages(JCR *jcr)
1166 {
1167    alist *msgs;
1168    char *msg;
1169
1170    jcr->lock();
1171    msgs = jcr->reserve_msgs;
1172    if (!msgs) {
1173       goto bail_out;
1174    }
1175    while ((msg = (char *)msgs->pop())) {
1176       free(msg);
1177    }
1178 bail_out:
1179    jcr->unlock();
1180 }
1181
1182 /*
1183  * Also called from acquire.c
1184  */
1185 void release_reserve_messages(JCR *jcr)
1186 {
1187    pop_reserve_messages(jcr);
1188    jcr->lock();
1189    if (!jcr->reserve_msgs) {
1190       goto bail_out;
1191    }
1192    delete jcr->reserve_msgs;
1193    jcr->reserve_msgs = NULL;
1194
1195 bail_out:
1196    jcr->unlock();
1197 }