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