]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/reserve.c
Tweak leave SQL library links in rpm
[bacula/bacula] / bacula / src / stored / reserve.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
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    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *   Drive reservation functions for Storage Daemon
21  *
22  *   Written by Kern Sibbald, MM
23  *
24  *   Split from job.c and acquire.c June 2005
25  *
26  */
27
28 #include "bacula.h"
29 #include "stored.h"
30
31 const int dbglvl = 150;
32
33 static brwlock_t reservation_lock;
34 int reservations_lock_count = 0;
35
36 /* Forward referenced functions */
37 static int  can_reserve_drive(DCR *dcr, RCTX &rctx);
38 static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol);
39 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx);
40 static bool reserve_device_for_read(DCR *dcr);
41 static bool use_device_cmd(JCR *jcr);
42 static int  reserve_device(RCTX &rctx);
43 static void pop_reserve_messages(JCR *jcr);
44 static void queue_reserve_message(JCR *jcr);
45 //void switch_device(DCR *dcr, DEVICE *dev);
46
47 /* Requests from the Director daemon */
48 static char use_storage[]  = "use storage=%127s media_type=%127s "
49     "pool_name=%127s pool_type=%127s append=%d copy=%d stripe=%d\n";
50 static char use_device[]  = "use device=%127s\n";
51
52 /* Responses sent to Director daemon */
53 static char OK_device[] = "3000 OK use device device=%s\n";
54 static char NO_device[] = "3924 Device \"%s\" not in SD Device"
55      " resources or no matching Media Type.\n";
56 static char BAD_use[]   = "3913 Bad use command: %s\n";
57
58 /*
59  * This allows a given thread to recursively call lock_reservations.
60  *   It must, of course, call unlock_... the same number of times.
61  */
62 void init_reservations_lock()
63 {
64    int errstat;
65    if ((errstat=rwl_init(&reservation_lock)) != 0) {
66       berrno be;
67       Emsg1(M_ABORT, 0, _("Unable to initialize reservation lock. ERR=%s\n"),
68             be.bstrerror(errstat));
69    }
70
71    init_vol_list_lock();
72 }
73
74
75 /* This applies to a drive and to Volumes */
76 void _lock_reservations(const char *file, int line)
77 {
78    int errstat;
79    reservations_lock_count++;
80    if ((errstat=rwl_writelock_p(&reservation_lock, file, line)) != 0) {
81       berrno be;
82       Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
83            errstat, be.bstrerror(errstat));
84    }
85 }
86
87 void _unlock_reservations()
88 {
89    int errstat;
90    reservations_lock_count--;
91    if ((errstat=rwl_writeunlock(&reservation_lock)) != 0) {
92       berrno be;
93       Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
94            errstat, be.bstrerror(errstat));
95    }
96 }
97
98 void term_reservations_lock()
99 {
100    rwl_destroy(&reservation_lock);
101    term_vol_list_lock();
102 }
103
104 void DCR::clear_reserved()
105 {
106    if (m_reserved) {
107       m_reserved = false;
108       dev->dec_reserved();
109       Dmsg2(dbglvl, "Dec reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
110    }
111 }
112
113 void DCR::set_reserved_for_append()
114 {
115    m_reserved = true;
116    dev->set_append_reserve();
117    Dmsg2(dbglvl, "Inc reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
118    dev->inc_reserved();
119 }
120
121 void DCR::set_reserved_for_read()
122 {
123    m_reserved = true;
124    dev->set_read_reserve();
125    Dmsg2(dbglvl, "Inc reserve=%d dev=%s\n", dev->num_reserved(), dev->print_name());
126    dev->inc_reserved();
127 }
128
129 /*
130  * Remove any reservation from a drive and tell the system
131  *  that the volume is unused at least by us.
132  */
133 void DCR::unreserve_device(bool locked)
134 {
135    if (!locked) {
136       dev->Lock();
137    }
138    if (is_reserved()) {
139       clear_reserved();
140       reserved_volume = false;
141       /* If we set read mode in reserving, remove it */
142       if (dev->can_read()) {
143          remove_read_volume(jcr, this->VolumeName);
144          dev->clear_read();
145       }
146       if (dev->num_writers < 0) {
147          Jmsg1(jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
148          dev->num_writers = 0;
149       }
150       if (dev->num_reserved() == 0 && dev->num_writers == 0) {
151          generate_plugin_event(jcr, bsdEventDeviceClose, this);
152          volume_unused(this);
153       }
154    }
155    if (!locked) {
156       dev->Unlock();
157    }
158 }
159
160 bool use_cmd(JCR *jcr)
161 {
162    /*
163     * Get the device, media, and pool information
164     */
165    if (!use_device_cmd(jcr)) {
166       jcr->setJobStatus(JS_ErrorTerminated);
167       memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
168       return false;
169    }
170    return true;
171 }
172
173 /*
174  * We get the following type of information:
175  *
176  * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=1 copy=0 strip=0
177  *  use device=zzz
178  *  use device=aaa
179  *  use device=bbb
180  * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=0 copy=0 strip=0
181  *  use device=bbb
182  *
183  */
184 static bool use_device_cmd(JCR *jcr)
185 {
186    POOL_MEM store_name, dev_name, media_type, pool_name, pool_type;
187    BSOCK *dir = jcr->dir_bsock;
188    int32_t append;
189    bool ok;
190    int32_t Copy, Stripe;
191    DIRSTORE *store;
192    RCTX rctx;
193    alist *dirstore;
194
195    memset(&rctx, 0, sizeof(RCTX));
196    rctx.jcr = jcr;
197    /*
198     * If there are multiple devices, the director sends us
199     *   use_device for each device that it wants to use.
200     */
201    dirstore = New(alist(10, not_owned_by_alist));
202    jcr->reserve_msgs = New(alist(10, not_owned_by_alist));
203    do {
204       Dmsg1(dbglvl, "<dird: %s", dir->msg);
205       ok = sscanf(dir->msg, use_storage, store_name.c_str(),
206                   media_type.c_str(), pool_name.c_str(),
207                   pool_type.c_str(), &append, &Copy, &Stripe) == 7;
208       if (!ok) {
209          break;
210       }
211       if (append) {
212          jcr->write_store = dirstore;
213       } else {
214          jcr->read_store = dirstore;
215       }
216       rctx.append = append;
217       unbash_spaces(store_name);
218       unbash_spaces(media_type);
219       unbash_spaces(pool_name);
220       unbash_spaces(pool_type);
221       store = new DIRSTORE;
222       dirstore->append(store);
223       memset(store, 0, sizeof(DIRSTORE));
224       store->device = New(alist(10));
225       bstrncpy(store->name, store_name, sizeof(store->name));
226       bstrncpy(store->media_type, media_type, sizeof(store->media_type));
227       bstrncpy(store->pool_name, pool_name, sizeof(store->pool_name));
228       bstrncpy(store->pool_type, pool_type, sizeof(store->pool_type));
229       store->append = append;
230
231       /* Now get all devices */
232       while (dir->recv() >= 0) {
233          Dmsg1(dbglvl, "<dird device: %s", dir->msg);
234          ok = sscanf(dir->msg, use_device, dev_name.c_str()) == 1;
235          if (!ok) {
236             break;
237          }
238          unbash_spaces(dev_name);
239          store->device->append(bstrdup(dev_name.c_str()));
240       }
241    }  while (ok && dir->recv() >= 0);
242
243 #ifdef xxxx
244    /* Developers debug code */
245    char *device_name;
246    if (debug_level >= dbglvl) {
247       foreach_alist(store, dirstore) {
248          Dmsg5(dbglvl, "Storage=%s media_type=%s pool=%s pool_type=%s append=%d\n",
249             store->name, store->media_type, store->pool_name,
250             store->pool_type, store->append);
251          foreach_alist(device_name, store->device) {
252             Dmsg1(dbglvl, "     Device=%s\n", device_name);
253          }
254       }
255    }
256 #endif
257
258    init_jcr_device_wait_timers(jcr);
259    jcr->dcr = new_dcr(jcr, NULL, NULL, !rctx.append);   /* get a dcr */
260    if (!jcr->dcr) {
261       BSOCK *dir = jcr->dir_bsock;
262       dir->fsend(_("3939 Could not get dcr\n"));
263       Dmsg1(dbglvl, ">dird: %s", dir->msg);
264       ok = false;
265    }
266    /*
267     * At this point, we have a list of all the Director's Storage
268     *  resources indicated for this Job, which include Pool, PoolType,
269     *  storage name, and Media type.
270     * Then for each of the Storage resources, we have a list of
271     *  device names that were given.
272     *
273     * Wiffle through them and find one that can do the backup.
274     */
275    if (ok) {
276       int wait_for_device_retries = 0;
277       int repeat = 0;
278       bool fail = false;
279       rctx.notify_dir = true;
280
281       /* Put new dcr in proper location */
282       if (rctx.append) {
283          rctx.jcr->dcr = jcr->dcr;
284       } else {
285          rctx.jcr->read_dcr = jcr->dcr;
286       }
287       lock_reservations();
288       for ( ; !fail && !job_canceled(jcr); ) {
289          pop_reserve_messages(jcr);
290          rctx.suitable_device = false;
291          rctx.have_volume = false;
292          rctx.VolumeName[0] = 0;
293          rctx.any_drive = false;
294          if (!jcr->PreferMountedVols) {
295             /*
296              * Here we try to find a drive that is not used.
297              * This will maximize the use of available drives.
298              *
299              */
300             rctx.num_writers = 20000000;   /* start with impossible number */
301             rctx.low_use_drive = NULL;
302             rctx.PreferMountedVols = false;
303             rctx.exact_match = false;
304             rctx.autochanger_only = true;
305             if ((ok = find_suitable_device_for_job(jcr, rctx))) {
306                break;
307             }
308             /* Look through all drives possibly for low_use drive */
309             if (rctx.low_use_drive) {
310                rctx.try_low_use_drive = true;
311                if ((ok = find_suitable_device_for_job(jcr, rctx))) {
312                   break;
313                }
314                rctx.try_low_use_drive = false;
315             }
316             rctx.autochanger_only = false;
317             if ((ok = find_suitable_device_for_job(jcr, rctx))) {
318                break;
319             }
320          }
321          /*
322           * Now we look for a drive that may or may not be in
323           *  use.
324           */
325          /* Look for an exact Volume match all drives */
326          rctx.PreferMountedVols = true;
327          rctx.exact_match = true;
328          rctx.autochanger_only = false;
329          if ((ok = find_suitable_device_for_job(jcr, rctx))) {
330             break;
331          }
332          /* Look for any mounted drive */
333          rctx.exact_match = false;
334          if ((ok = find_suitable_device_for_job(jcr, rctx))) {
335             break;
336          }
337          /* Try any drive */
338          rctx.any_drive = true;
339          if ((ok = find_suitable_device_for_job(jcr, rctx))) {
340             break;
341          }
342          /* Keep reservations locked *except* during wait_for_device() */
343          unlock_reservations();
344          /*
345           * The idea of looping on repeat a few times it to ensure
346           * that if there is some subtle timing problem between two
347           * jobs, we will simply try again, and most likely succeed.
348           * This can happen if one job reserves a drive or finishes using
349           * a drive at the same time a second job wants it.
350           */
351          if (repeat++ > 1) {              /* try algorithm 3 times */
352             bmicrosleep(30, 0);           /* wait a bit */
353             Dmsg1(100, "repeat reserve algorithm JobId=%d\n", jcr->JobId);
354          } else if (!rctx.suitable_device || !wait_for_any_device(jcr, wait_for_device_retries)) {
355             Dmsg0(100, "Fail. !suitable_device || !wait_for_device\n");
356             fail = true;
357          }
358          lock_reservations();
359          dir->signal(BNET_HEARTBEAT);  /* Inform Dir that we are alive */
360       }
361       unlock_reservations();
362
363       if (!ok) {
364          /*
365           * If we get here, there are no suitable devices available, which
366           *  means nothing configured.  If a device is suitable but busy
367           *  with another Volume, we will not come here.
368           */
369          unbash_spaces(dir->msg);
370          pm_strcpy(jcr->errmsg, dir->msg);
371          Jmsg(jcr, M_FATAL, 0, _("Device reservation failed for JobId=%d: %s\n"),
372               jcr->JobId, jcr->errmsg);
373          dir->fsend(NO_device, dev_name.c_str());
374
375          Dmsg1(dbglvl, ">dird: %s", dir->msg);
376       }
377    } else {
378       unbash_spaces(dir->msg);
379       pm_strcpy(jcr->errmsg, dir->msg);
380       Jmsg(jcr, M_FATAL, 0, _("Failed command: %s\n"), jcr->errmsg);
381       dir->fsend(BAD_use, jcr->errmsg);
382       Dmsg1(dbglvl, ">dird: %s", dir->msg);
383    }
384
385    release_reserve_messages(jcr);
386    return ok;
387 }
388
389 /*
390  * Search for a device suitable for this job.
391  * Note, this routine sets sets rctx.suitable_device if any
392  *   device exists within the SD.  The device may not be actually
393  *   useable.
394  * It also returns if it finds a useable device.
395  */
396 bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
397 {
398    bool ok = false;
399    DIRSTORE *store;
400    char *device_name;
401    alist *dirstore;
402    DCR *dcr = jcr->dcr;
403
404    if (rctx.append) {
405       dirstore = jcr->write_store;
406    } else {
407       dirstore = jcr->read_store;
408    }
409    Dmsg5(dbglvl, "Start find_suit_dev PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
410          rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
411          rctx.autochanger_only, rctx.any_drive);
412
413    /*
414     * If the appropriate conditions of this if are met, namely that
415     *  we are appending and the user wants mounted drive (or we
416     *  force try a mounted drive because they are all busy), we
417     *  start by looking at all the Volumes in the volume list.
418     */
419    if (!is_vol_list_empty() && rctx.append && rctx.PreferMountedVols) {
420       dlist *temp_vol_list;
421       VOLRES *vol = NULL;
422       temp_vol_list = dup_vol_list(jcr);
423
424       /* Look through reserved volumes for one we can use */
425       Dmsg0(dbglvl, "look for vol in vol list\n");
426       foreach_dlist(vol, temp_vol_list) {
427          if (!vol->dev) {
428             Dmsg1(dbglvl, "vol=%s no dev\n", vol->vol_name);
429             continue;
430          }
431          /* Check with Director if this Volume is OK */
432          bstrncpy(dcr->VolumeName, vol->vol_name, sizeof(dcr->VolumeName));
433          if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
434             continue;
435          }
436
437          Dmsg1(dbglvl, "vol=%s OK for this job\n", vol->vol_name);
438          foreach_alist(store, dirstore) {
439             int stat;
440             rctx.store = store;
441             foreach_alist(device_name, store->device) {
442                /* Found a device, try to use it */
443                rctx.device_name = device_name;
444                rctx.device = vol->dev->device;
445
446                if (vol->dev->read_only) {
447                   continue;
448                }
449                if (vol->dev->is_autochanger()) {
450                   Dmsg1(dbglvl, "vol=%s is in changer\n", vol->vol_name);
451                   if (!is_vol_in_autochanger(rctx, vol) || !vol->dev->autoselect ||
452                      !vol->dev->enabled) {
453                      continue;
454                   }
455                } else if (strcmp(device_name, vol->dev->device->hdr.name) != 0) {
456                   Dmsg2(dbglvl, "device=%s not suitable want %s\n",
457                         vol->dev->device->hdr.name, device_name);
458                   continue;
459                }
460
461                bstrncpy(rctx.VolumeName, vol->vol_name, sizeof(rctx.VolumeName));
462                rctx.have_volume = true;
463                /* Try reserving this device and volume */
464                Dmsg2(dbglvl, "try vol=%s on device=%s\n", rctx.VolumeName, device_name);
465                stat = reserve_device(rctx);
466                if (stat == 1) {             /* found available device */
467                   Dmsg1(dbglvl, "Suitable device found=%s\n", device_name);
468                   ok = true;
469                   break;
470                } else if (stat == 0) {      /* device busy */
471                   Dmsg1(dbglvl, "Suitable device=%s, busy: not use\n", device_name);
472                } else {
473                   /* otherwise error */
474                   Dmsg0(dbglvl, "No suitable device found.\n");
475                }
476                rctx.have_volume = false;
477                rctx.VolumeName[0] = 0;
478             }
479             if (ok) {
480                break;
481             }
482          }
483          if (ok) {
484             break;
485          }
486       } /* end for loop over reserved volumes */
487
488       Dmsg0(dbglvl, "lock volumes\n");
489       free_temp_vol_list(temp_vol_list);
490    }
491    if (ok) {
492       Dmsg1(dbglvl, "OK dev found. Vol=%s from in-use vols list\n", rctx.VolumeName);
493       return true;
494    }
495
496    /*
497     * No reserved volume we can use, so now search for an available device.
498     *
499     * For each storage device that the user specified, we
500     *  search and see if there is a resource for that device.
501     */
502    foreach_alist(store, dirstore) {
503       rctx.store = store;
504       foreach_alist(device_name, store->device) {
505          int stat;
506          rctx.device_name = device_name;
507          stat = search_res_for_device(rctx);
508          if (stat == 1) {             /* found available device */
509             Dmsg1(dbglvl, "available device found=%s\n", device_name);
510             ok = true;
511             break;
512          } else if (stat == 0) {      /* device busy */
513             Dmsg1(dbglvl, "No usable device=%s, busy: not use\n", device_name);
514          } else {
515             /* otherwise error */
516             Dmsg0(dbglvl, "No usable device found.\n");
517          }
518       }
519       if (ok) {
520          break;
521       }
522    }
523    if (ok) {
524       Dmsg1(dbglvl, "OK dev found. Vol=%s\n", rctx.VolumeName);
525    } else {
526       Dmsg0(dbglvl, "Leave find_suit_dev: no dev found.\n");
527    }
528    return ok;
529 }
530
531 /*
532  * Search for a particular storage device with particular storage
533  *  characteristics (MediaType).
534  */
535 int search_res_for_device(RCTX &rctx)
536 {
537    AUTOCHANGER *changer;
538    int stat;
539
540    Dmsg1(dbglvl, "search res for %s\n", rctx.device_name);
541    /* Look through Autochangers first */
542    foreach_res(changer, R_AUTOCHANGER) {
543       Dmsg1(dbglvl, "Try match changer res=%s\n", changer->hdr.name);
544       /* Find resource, and make sure we were able to open it */
545       if (strcmp(rctx.device_name, changer->hdr.name) == 0) {
546          /* Try each device in this AutoChanger */
547          foreach_alist(rctx.device, changer->device) {
548             Dmsg1(dbglvl, "Try changer device %s\n", rctx.device->hdr.name);
549             if (rctx.store->append && rctx.device->read_only) {
550                continue;
551             }
552             if (!rctx.device->autoselect) {
553                Dmsg1(100, "Device %s not autoselect skipped.\n",
554                rctx.device->hdr.name);
555                continue;              /* device is not available */
556             }
557             stat = reserve_device(rctx);
558             if (stat != 1) {             /* try another device */
559                continue;
560             }
561             /* Debug code */
562             if (rctx.store->append) {
563                Dmsg2(dbglvl, "Device %s reserved=%d for append.\n",
564                   rctx.device->hdr.name, rctx.jcr->dcr->dev->num_reserved());
565             } else {
566                Dmsg2(dbglvl, "Device %s reserved=%d for read.\n",
567                   rctx.device->hdr.name, rctx.jcr->read_dcr->dev->num_reserved());
568             }
569             return stat;
570          }
571       }
572    }
573
574    /* Now if requested look through regular devices */
575    if (!rctx.autochanger_only) {
576       foreach_res(rctx.device, R_DEVICE) {
577          Dmsg1(dbglvl, "Try match res=%s\n", rctx.device->hdr.name);
578          /* Find resource, and make sure we were able to open it */
579          if (strcmp(rctx.device_name, rctx.device->hdr.name) == 0) {
580             stat = reserve_device(rctx);
581             if (stat != 1) {             /* try another device */
582                continue;
583             }
584             /* Debug code */
585             if (rctx.store->append) {
586                Dmsg2(dbglvl, "Device %s reserved=%d for append.\n",
587                   rctx.device->hdr.name, rctx.jcr->dcr->dev->num_reserved());
588             } else {
589                Dmsg2(dbglvl, "Device %s reserved=%d for read.\n",
590                   rctx.device->hdr.name, rctx.jcr->read_dcr->dev->num_reserved());
591             }
592             return stat;
593          }
594       }
595    }
596    return -1;                    /* nothing found */
597 }
598
599 /*
600  * Walk through the autochanger resources and check if
601  *  the volume is in one of them.
602  *
603  * Returns:  true  if volume is in device
604  *           false otherwise
605  */
606 static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol)
607 {
608    AUTOCHANGER *changer = vol->dev->device->changer_res;
609
610    if (!changer) {
611       return false;
612    }
613
614    /* Find resource, and make sure we were able to open it */
615    if (strcmp(rctx.device_name, changer->hdr.name) == 0) {
616       Dmsg1(dbglvl, "Found changer device %s\n", vol->dev->device->hdr.name);
617       return true;
618    }
619    Dmsg1(dbglvl, "Incorrect changer device %s\n", changer->hdr.name);
620    return false;
621 }
622
623
624 /*
625  *  Try to reserve a specific device.
626  *
627  *  Returns: 1 -- OK, have DCR
628  *           0 -- must wait
629  *          -1 -- fatal error
630  */
631 static int reserve_device(RCTX &rctx)
632 {
633    bool ok;
634    DCR *dcr;
635    const int name_len = MAX_NAME_LENGTH;
636
637    /* Make sure MediaType is OK */
638    Dmsg2(dbglvl, "chk MediaType device=%s request=%s\n",
639          rctx.device->media_type, rctx.store->media_type);
640    if (strcmp(rctx.device->media_type, rctx.store->media_type) != 0) {
641       return -1;
642    }
643
644    /* Make sure device exists -- i.e. we can stat() it */
645    if (!rctx.device->dev) {
646       rctx.device->dev = init_dev(rctx.jcr, rctx.device);
647    }
648    if (!rctx.device->dev) {
649       if (rctx.device->changer_res) {
650         Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
651            "     Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
652              rctx.device->hdr.name, rctx.device_name);
653       } else {
654          Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
655             "     Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
656               rctx.device_name);
657       }
658       return -1;  /* no use waiting */
659    } else if (!rctx.device->dev->enabled) {
660       Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
661            "     Device \"%s\" requested by DIR is disabled.\n"),
662               rctx.device_name);
663       return -1;  /* no use waiting */
664    }
665
666    rctx.suitable_device = true;
667    Dmsg1(dbglvl, "try reserve %s\n", rctx.device->hdr.name);
668    if (rctx.store->append) {
669       dcr = new_dcr(rctx.jcr, rctx.jcr->dcr, rctx.device->dev, SD_APPEND);
670    } else {
671       dcr = new_dcr(rctx.jcr, rctx.jcr->read_dcr, rctx.device->dev, SD_READ);
672    }
673    if (!dcr) {
674       BSOCK *dir = rctx.jcr->dir_bsock;
675       dir->fsend(_("3926 Could not get dcr for device: %s\n"), rctx.device_name);
676       Dmsg1(dbglvl, ">dird: %s", dir->msg);
677       return -1;
678    }
679    bstrncpy(dcr->pool_name, rctx.store->pool_name, name_len);
680    bstrncpy(dcr->pool_type, rctx.store->pool_type, name_len);
681    bstrncpy(dcr->media_type, rctx.store->media_type, name_len);
682    bstrncpy(dcr->dev_name, rctx.device_name, name_len);
683    if (rctx.store->append) {
684       Dmsg2(dbglvl, "call reserve for append: have_vol=%d vol=%s\n", rctx.have_volume, rctx.VolumeName);
685       ok = reserve_device_for_append(dcr, rctx);
686       if (!ok) {
687          goto bail_out;
688       }
689
690       rctx.jcr->dcr = dcr;
691       Dmsg5(dbglvl, "Reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
692                dcr->dev->num_reserved(),
693                dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
694       Dmsg3(dbglvl, "Vol=%s num_writers=%d, have_vol=%d\n",
695          rctx.VolumeName, dcr->dev->num_writers, rctx.have_volume);
696       if (rctx.have_volume) {
697          Dmsg0(dbglvl, "Call reserve_volume for append.\n");
698          if (reserve_volume(dcr, rctx.VolumeName)) {
699             Dmsg1(dbglvl, "Reserved vol=%s\n", rctx.VolumeName);
700          } else {
701             Dmsg1(dbglvl, "Could not reserve vol=%s\n", rctx.VolumeName);
702             goto bail_out;
703          }
704       } else {
705          dcr->any_volume = true;
706          Dmsg0(dbglvl, "no vol, call find_next_appendable_vol.\n");
707          if (dir_find_next_appendable_volume(dcr)) {
708             bstrncpy(rctx.VolumeName, dcr->VolumeName, sizeof(rctx.VolumeName));
709             rctx.have_volume = true;
710             Dmsg1(dbglvl, "looking for Volume=%s\n", rctx.VolumeName);
711          } else {
712             dcr->dev->clear_wait();
713             Dmsg0(dbglvl, "No next volume found\n");
714             rctx.have_volume = false;
715             rctx.VolumeName[0] = 0;
716             /*
717              * If there is at least one volume that is valid and in use,
718              *   but we get here, check if we are running with prefers
719              *   non-mounted drives.  In that case, we have selected a
720              *   non-used drive and our one and only volume is mounted
721              *   elsewhere, so we bail out and retry using that drive.
722              */
723             if (dcr->found_in_use() && !rctx.PreferMountedVols) {
724                rctx.PreferMountedVols = true;
725                if (dcr->VolumeName[0]) {
726                   dcr->unreserve_device(false);
727                }
728                goto bail_out;
729             }
730             /*
731              * Note. Under some circumstances, the Director can hand us
732              *  a Volume name that is not the same as the one on the current
733              *  drive, and in that case, the call above to find the next
734              *  volume will fail because in attempting to reserve the Volume
735              *  the code will realize that we already have a tape mounted,
736              *  and it will fail.  This *should* only happen if there are
737              *  writers, thus the following test.  In that case, we simply
738              *  bail out, and continue waiting, rather than plunging on
739              *  and hoping that the operator can resolve the problem.
740              */
741             if (dcr->dev->num_writers != 0) {
742                if (dcr->VolumeName[0]) {
743                   dcr->unreserve_device(false);
744                }
745                goto bail_out;
746             }
747          }
748       }
749    } else {
750       ok = reserve_device_for_read(dcr);
751       if (ok) {
752          rctx.jcr->read_dcr = dcr;
753          Dmsg5(dbglvl, "Read reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
754                dcr->dev->num_reserved(),
755                dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
756       }
757    }
758    if (!ok) {
759       goto bail_out;
760    }
761
762    if (rctx.notify_dir) {
763       POOL_MEM dev_name;
764       BSOCK *dir = rctx.jcr->dir_bsock;
765       pm_strcpy(dev_name, rctx.device->hdr.name);
766       bash_spaces(dev_name);
767       ok = dir->fsend(OK_device, dev_name.c_str());  /* Return real device name */
768       Dmsg1(dbglvl, ">dird: %s", dir->msg);
769       if (!ok) {
770          dcr->unreserve_device(false);
771       }
772    } else {
773       ok = true;
774    }
775    return ok ? 1 : -1;
776
777 bail_out:
778    rctx.have_volume = false;
779    rctx.VolumeName[0] = 0;
780    Dmsg0(dbglvl, "Not OK.\n");
781    return 0;
782 }
783
784
785 /*
786  * We reserve the device for appending by incrementing
787  *  num_reserved(). We do virtually all the same work that
788  *  is done in acquire_device_for_append(), but we do
789  *  not attempt to mount the device. This routine allows
790  *  the DIR to reserve multiple devices before *really*
791  *  starting the job. It also permits the SD to refuse
792  *  certain devices (not up, ...).
793  *
794  * Note, in reserving a device, if the device is for the
795  *  same pool and the same pool type, then it is acceptable.
796  *  The Media Type has already been checked. If we are
797  *  the first tor reserve the device, we put the pool
798  *  name and pool type in the device record.
799  */
800 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx)
801 {
802    JCR *jcr = dcr->jcr;
803    DEVICE *dev = dcr->dev;
804    bool ok = false;
805
806    ASSERT2(dcr, "No dcr in reserve_device_for_append!");
807    if (job_canceled(jcr)) {
808       return false;
809    }
810
811    dev->Lock();
812
813    /* If device is being read or reserved for read, we cannot write it */
814    if (dev->can_read() || dev->is_reserved_for_read()) {
815       Mmsg(jcr->errmsg, _("3603 JobId=%u %s device %s is busy reading.\n"),
816          jcr->JobId, dev->print_type(), dev->print_name());
817       queue_reserve_message(jcr);
818       Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
819       goto bail_out;
820    }
821
822    /* If device is unmounted, we are out of luck */
823    if (dev->is_device_unmounted()) {
824       Mmsg(jcr->errmsg, _("3604 JobId=%u %s device %s is BLOCKED due to user unmount.\n"),
825          jcr->JobId, dev->print_type(), dev->print_name());
826       queue_reserve_message(jcr);
827       Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
828       goto bail_out;
829    }
830
831    Dmsg2(dbglvl, "reserve_append %s device is %s\n", dev->print_type(), dev->print_name());
832
833    /* Now do detailed tests ... */
834    if (can_reserve_drive(dcr, rctx) != 1) {
835       Dmsg0(dbglvl, "can_reserve_drive!=1\n");
836       goto bail_out;
837    }
838
839    /* Note: on failure this returns jcr->errmsg properly edited */
840    if (generate_plugin_event(jcr, bsdEventDeviceTryOpen, dcr) != bRC_OK) {
841       queue_reserve_message(jcr);
842       goto bail_out;
843    }
844    dcr->set_reserved_for_append();
845    ok = true;
846
847 bail_out:
848    dev->Unlock();
849    return ok;
850 }
851
852 /*
853  * We "reserve" the drive by setting the ST_READ bit. No one else
854  *  should touch the drive until that is cleared.
855  *  This allows the DIR to "reserve" the device before actually
856  *  starting the job.
857  */
858 static bool reserve_device_for_read(DCR *dcr)
859 {
860    DEVICE *dev = dcr->dev;
861    JCR *jcr = dcr->jcr;
862    bool ok = false;
863
864    ASSERT2(dcr, "No dcr in reserve_device_for_read!");
865    if (job_canceled(jcr)) {
866       return false;
867    }
868
869    dev->Lock();
870
871    if (dev->is_device_unmounted()) {
872       Mmsg(jcr->errmsg, _("3601 JobId=%u %s device %s is BLOCKED due to user unmount.\n"),
873            jcr->JobId, dev->print_type(), dev->print_name());
874       queue_reserve_message(jcr);
875       Dmsg1(dbglvl, "Device %s is BLOCKED due to user unmount.\n", dev->print_name());
876       goto bail_out;
877    }
878
879    if (dev->is_busy()) {
880       Mmsg(jcr->errmsg, _("3602 JobId=%u %s device %s is busy (already reading/writing)."
881             " read=%d, writers=%d reserved=%d\n"),
882             jcr->JobId, dev->print_type(), dev->print_name(),
883             dev->state & ST_READ?1:0, dev->num_writers, dev->num_reserved());
884       queue_reserve_message(jcr);
885       Dmsg4(dbglvl, "Device %s is busy ST_READ=%d num_writers=%d reserved=%d.\n",
886          dev->print_name(),
887          dev->state & ST_READ?1:0, dev->num_writers, dev->num_reserved());
888       goto bail_out;
889    }
890
891    /* Note: on failure this returns jcr->errmsg properly edited */
892    if (generate_plugin_event(jcr, bsdEventDeviceTryOpen, dcr) != bRC_OK) {
893       queue_reserve_message(jcr);
894       goto bail_out;
895    }
896    dev->clear_append();
897    dcr->set_reserved_for_read();
898    ok = true;
899
900 bail_out:
901    dev->Unlock();
902    return ok;
903 }
904
905 static bool is_max_jobs_ok(DCR *dcr)
906 {
907    DEVICE *dev = dcr->dev;
908    JCR *jcr = dcr->jcr;
909
910    Dmsg5(dbglvl, "MaxJobs=%d Jobs=%d reserves=%d Status=%s Vol=%s\n",
911          dcr->VolCatInfo.VolCatMaxJobs,
912          dcr->VolCatInfo.VolCatJobs, dev->num_reserved(),
913          dcr->VolCatInfo.VolCatStatus,
914          dcr->VolumeName);
915    /* Limit max concurrent jobs on this drive */
916    if (dev->max_concurrent_jobs > 0 && dev->max_concurrent_jobs <=
917               (uint32_t)(dev->num_writers + dev->num_reserved())) {
918       /* Max Concurrent Jobs depassed or already reserved */
919       Mmsg(jcr->errmsg, _("3609 JobId=%u Max concurrent jobs=%d exceeded on %s device %s.\n"),
920             (uint32_t)jcr->JobId, dev->max_concurrent_jobs,
921              dev->print_type(), dev->print_name());
922       queue_reserve_message(jcr);
923       Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
924       return false;
925    }
926    if (strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0) {
927       return true;
928    }
929
930    if (dcr->VolCatInfo.VolCatMaxJobs > 0 && dcr->VolCatInfo.VolCatMaxJobs <=
931         (dcr->VolCatInfo.VolCatJobs + dev->num_reserved())) {
932       /* Max Job Vols depassed or already reserved */
933       Mmsg(jcr->errmsg, _("3611 JobId=%u Volume max jobs=%d exceeded on %s device %s.\n"),
934             (uint32_t)jcr->JobId, dcr->VolCatInfo.VolCatMaxJobs,
935             dev->print_type(), dev->print_name());
936       queue_reserve_message(jcr);
937       Dmsg1(dbglvl, "reserve dev failed: %s", jcr->errmsg);
938       return false;                /* wait */
939    }
940    return true;
941 }
942
943
944 static int is_pool_ok(DCR *dcr)
945 {
946    DEVICE *dev = dcr->dev;
947    JCR *jcr = dcr->jcr;
948
949    /* Now check if we want the same Pool and pool type */
950    if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
951        strcmp(dev->pool_type, dcr->pool_type) == 0) {
952       /* OK, compatible device */
953       Dmsg1(dbglvl, "OK dev: %s num_writers=0, reserved, pool matches\n", dev->print_name());
954       return 1;
955    } else {
956       /* Drive Pool not suitable for us */
957       Mmsg(jcr->errmsg, _(
958 "3608 JobId=%u wants Pool=\"%s\" but have Pool=\"%s\" nreserve=%d on %s device %s.\n"),
959             (uint32_t)jcr->JobId, dcr->pool_name, dev->pool_name,
960             dev->num_reserved(), dev->print_type(), dev->print_name());
961       Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
962       queue_reserve_message(jcr);
963    }
964    return 0;
965 }
966
967 /*
968  * Returns: 1 if drive can be reserved
969  *          0 if we should wait
970  *         -1 on error or impossibility
971  */
972 static int can_reserve_drive(DCR *dcr, RCTX &rctx)
973 {
974    DEVICE *dev = dcr->dev;
975    JCR *jcr = dcr->jcr;
976
977    Dmsg5(dbglvl, "PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
978          rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
979          rctx.autochanger_only, rctx.any_drive);
980
981    /* Check for max jobs on this Volume */
982    if (!is_max_jobs_ok(dcr)) {
983       return 0;
984    }
985
986    /* setting any_drive overrides PreferMountedVols flag */
987    if (!rctx.any_drive) {
988       /*
989        * When PreferMountedVols is set, we keep track of the
990        *  drive in use that has the least number of writers, then if
991        *  no unmounted drive is found, we try that drive. This
992        *  helps spread the load to the least used drives.
993        */
994       if (rctx.try_low_use_drive && dev == rctx.low_use_drive) {
995          Dmsg2(dbglvl, "OK dev=%s == low_drive=%s.\n",
996             dev->print_name(), rctx.low_use_drive->print_name());
997          return 1;
998       }
999       /* If he wants a free drive, but this one is busy, no go */
1000       if (!rctx.PreferMountedVols && dev->is_busy()) {
1001          /* Save least used drive */
1002          if ((dev->num_writers + dev->num_reserved()) < rctx.num_writers) {
1003             rctx.num_writers = dev->num_writers + dev->num_reserved();
1004             rctx.low_use_drive = dev;
1005             Dmsg2(dbglvl, "set low use drive=%s num_writers=%d\n",
1006                dev->print_name(), rctx.num_writers);
1007          } else {
1008             Dmsg1(dbglvl, "not low use num_writers=%d\n", dev->num_writers+dev->num_reserved());
1009          }
1010          Mmsg(jcr->errmsg, _("3605 JobId=%u wants free drive but %s device %s is busy.\n"),
1011             jcr->JobId, dev->print_type(), dev->print_name());
1012          queue_reserve_message(jcr);
1013          Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
1014          return 0;
1015       }
1016
1017       /* Check for prefer mounted volumes */
1018       if (rctx.PreferMountedVols && !dev->vol && dev->is_tape()) {
1019          Mmsg(jcr->errmsg, _("3606 JobId=%u prefers mounted drives, but %s device %s has no Volume.\n"),
1020             jcr->JobId, dev->print_type(), dev->print_name());
1021          queue_reserve_message(jcr);
1022          Dmsg1(dbglvl, "Failed: %s", jcr->errmsg);
1023          return 0;                 /* No volume mounted */
1024       }
1025
1026       /* Check for exact Volume name match */
1027       /* ***FIXME*** for Disk, we can accept any volume that goes with this
1028        *    drive.
1029        */
1030       if (rctx.exact_match && rctx.have_volume) {
1031          bool ok;
1032          Dmsg5(dbglvl, "PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
1033                rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
1034                rctx.autochanger_only, rctx.any_drive);
1035          Dmsg4(dbglvl, "have_vol=%d have=%s resvol=%s want=%s\n",
1036                   rctx.have_volume, dev->VolHdr.VolumeName,
1037                   dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1038          ok = strcmp(dev->VolHdr.VolumeName, rctx.VolumeName) == 0 ||
1039                  (dev->vol && strcmp(dev->vol->vol_name, rctx.VolumeName) == 0);
1040          if (!ok) {
1041             Mmsg(jcr->errmsg, _("3607 JobId=%u wants Vol=\"%s\" drive has Vol=\"%s\" on %s device %s.\n"),
1042                jcr->JobId, rctx.VolumeName, dev->VolHdr.VolumeName,
1043                dev->print_type(), dev->print_name());
1044             queue_reserve_message(jcr);
1045             Dmsg3(dbglvl, "not OK: dev have=%s resvol=%s want=%s\n",
1046                   dev->VolHdr.VolumeName, dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1047             return 0;
1048          }
1049          if (!dcr->can_i_use_volume()) {
1050             return 0;              /* fail if volume on another drive */
1051          }
1052       }
1053    }
1054
1055    /* Check for unused autochanger drive */
1056    if (rctx.autochanger_only && !dev->is_busy() &&
1057        dev->VolHdr.VolumeName[0] == 0) {
1058       /* Device is available but not yet reserved, reserve it for us */
1059       Dmsg1(dbglvl, "OK Res Unused autochanger %s.\n", dev->print_name());
1060       bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1061       bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1062       return 1;                       /* reserve drive */
1063    }
1064
1065    /*
1066     * Handle the case that there are no writers
1067     */
1068    if (dev->num_writers == 0) {
1069       /* Now check if there are any reservations on the drive */
1070       if (dev->num_reserved()) {
1071          return is_pool_ok(dcr);
1072       } else if (dev->can_append()) {
1073          if (is_pool_ok(dcr)) {
1074             return 1;
1075          } else {
1076             /* Changing pool, unload old tape if any in drive */
1077             Dmsg0(dbglvl, "OK dev: num_writers=0, not reserved, pool change, unload changer\n");
1078             /* ***FIXME*** use set_unload() */
1079             unload_autochanger(dcr, -1);
1080          }
1081       }
1082       /* Device is available but not yet reserved, reserve it for us */
1083       Dmsg1(dbglvl, "OK Dev avail reserved %s\n", dev->print_name());
1084       bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1085       bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1086       return 1;                       /* reserve drive */
1087    }
1088
1089    /*
1090     * Check if the device is in append mode with writers (i.e.
1091     *  available if pool is the same).
1092     */
1093    if (dev->can_append() || dev->num_writers > 0) {
1094       return is_pool_ok(dcr);
1095    } else {
1096       Pmsg1(000, _("Logic error!!!! JobId=%u Should not get here.\n"), (int)jcr->JobId);
1097       Mmsg(jcr->errmsg, _("3910 JobId=%u Logic error!!!! %s device %s Should not get here.\n"),
1098             jcr->JobId, dev->print_type(), dev->print_name());
1099       queue_reserve_message(jcr);
1100       Jmsg0(jcr, M_FATAL, 0, _("Logic error!!!! Should not get here.\n"));
1101       return -1;                      /* error, should not get here */
1102    }
1103    Mmsg(jcr->errmsg, _("3911 JobId=%u failed reserve %s device %s.\n"),
1104          jcr->JobId, dev->print_type(), dev->print_name());
1105    queue_reserve_message(jcr);
1106    Dmsg1(dbglvl, "Failed: No reserve %s\n", dev->print_name());
1107    return 0;
1108 }
1109
1110
1111 /*
1112  * Queue a reservation error or failure message for this jcr
1113  */
1114 static void queue_reserve_message(JCR *jcr)
1115 {
1116    int i;
1117    alist *msgs;
1118    char *msg;
1119
1120    jcr->lock();
1121
1122    msgs = jcr->reserve_msgs;
1123    if (!msgs) {
1124       goto bail_out;
1125    }
1126    /*
1127     * Look for duplicate message.  If found, do
1128     * not insert
1129     */
1130    for (i=msgs->size()-1; i >= 0; i--) {
1131       msg = (char *)msgs->get(i);
1132       if (!msg) {
1133          goto bail_out;
1134       }
1135       /* Comparison based on 4 digit message number */
1136       if (strncmp(msg, jcr->errmsg, 4) == 0) {
1137          goto bail_out;
1138       }
1139    }
1140    /* Message unique, so insert it */
1141    jcr->reserve_msgs->push(bstrdup(jcr->errmsg));
1142
1143 bail_out:
1144    jcr->unlock();
1145 }
1146
1147 /*
1148  * Send any reservation messages queued for this jcr
1149  */
1150 void send_drive_reserve_messages(JCR *jcr, void sendit(const char *msg, int len, void *sarg), void *arg)
1151 {
1152    int i;
1153    alist *msgs;
1154    char *msg;
1155
1156    jcr->lock();
1157    msgs = jcr->reserve_msgs;
1158    if (!msgs || msgs->size() == 0) {
1159       goto bail_out;
1160    }
1161    for (i=msgs->size()-1; i >= 0; i--) {
1162       msg = (char *)msgs->get(i);
1163       if (msg) {
1164          sendit("   ", 3, arg);
1165          sendit(msg, strlen(msg), arg);
1166       } else {
1167          break;
1168       }
1169    }
1170
1171 bail_out:
1172    jcr->unlock();
1173 }
1174
1175 /*
1176  * Pop and release any reservations messages
1177  */
1178 static void pop_reserve_messages(JCR *jcr)
1179 {
1180    alist *msgs;
1181    char *msg;
1182
1183    jcr->lock();
1184    msgs = jcr->reserve_msgs;
1185    if (!msgs) {
1186       goto bail_out;
1187    }
1188    while ((msg = (char *)msgs->pop())) {
1189       free(msg);
1190    }
1191 bail_out:
1192    jcr->unlock();
1193 }
1194
1195 /*
1196  * Also called from acquire.c
1197  */
1198 void release_reserve_messages(JCR *jcr)
1199 {
1200    pop_reserve_messages(jcr);
1201    jcr->lock();
1202    if (!jcr->reserve_msgs) {
1203       goto bail_out;
1204    }
1205    delete jcr->reserve_msgs;
1206    jcr->reserve_msgs = NULL;
1207
1208 bail_out:
1209    jcr->unlock();
1210 }