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