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