]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/reserve.c
bc102f2d1b4d8431612492ede3de09b834580276
[bacula/bacula] / bacula / src / stored / reserve.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2008 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 two of the GNU 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 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 John Walker.
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 #define jid() ((int)get_jobid_from_tid())
43
44 const int dbglvl =  50;
45
46 static dlist *vol_list = NULL;
47 static brwlock_t reservation_lock;
48 static brwlock_t vol_list_lock;
49
50 /* Forward referenced functions */
51 static int can_reserve_drive(DCR *dcr, RCTX &rctx);
52 static int reserve_device(RCTX &rctx);
53 static bool reserve_device_for_read(DCR *dcr);
54 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx);
55 static bool use_storage_cmd(JCR *jcr);
56 static void queue_reserve_message(JCR *jcr);
57 static void pop_reserve_messages(JCR *jcr);
58
59 /* Requests from the Director daemon */
60 static char use_storage[]  = "use storage=%127s media_type=%127s "
61    "pool_name=%127s pool_type=%127s append=%d copy=%d stripe=%d\n";
62 static char use_device[]  = "use device=%127s\n";
63
64 /* Responses sent to Director daemon */
65 static char OK_device[] = "3000 OK use device device=%s\n";
66 static char NO_device[] = "3924 Device \"%s\" not in SD Device resources.\n";
67 static char BAD_use[]   = "3913 Bad use command: %s\n";
68
69 bool use_cmd(JCR *jcr) 
70 {
71    /*
72     * Get the device, media, and pool information
73     */
74    if (!use_storage_cmd(jcr)) {
75       set_jcr_job_status(jcr, JS_ErrorTerminated);
76       memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
77       return false;
78    }
79    return true;
80 }
81
82 static int my_compare(void *item1, void *item2)
83 {
84    return strcmp(((VOLRES *)item1)->vol_name, ((VOLRES *)item2)->vol_name);
85 }
86
87
88 void init_reservations_lock()
89 {
90    int errstat;
91    if ((errstat=rwl_init(&reservation_lock)) != 0) {
92       berrno be;
93       Emsg1(M_ABORT, 0, _("Unable to initialize reservation lock. ERR=%s\n"),
94             be.bstrerror(errstat));
95    }
96
97    if ((errstat=rwl_init(&vol_list_lock)) != 0) {
98       berrno be;
99       Emsg1(M_ABORT, 0, _("Unable to initialize volume list lock. ERR=%s\n"),
100             be.bstrerror(errstat));
101    }
102 }
103
104 void term_reservations_lock()
105 {
106    rwl_destroy(&reservation_lock);
107    rwl_destroy(&vol_list_lock);
108 }
109
110 int reservations_lock_count = 0;
111
112 /* This applies to a drive and to Volumes */
113 void _lock_reservations()
114 {
115    int errstat;
116    reservations_lock_count++;
117    if ((errstat=rwl_writelock(&reservation_lock)) != 0) {
118       berrno be;
119       Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
120            errstat, be.bstrerror(errstat));
121    }
122 }
123
124 void _unlock_reservations()
125 {
126    int errstat;
127    reservations_lock_count--;
128    if ((errstat=rwl_writeunlock(&reservation_lock)) != 0) {
129       berrno be;
130       Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
131            errstat, be.bstrerror(errstat));
132    }
133 }
134
135 int vol_list_lock_count = 0;
136
137 void _lock_volumes()
138 {
139    int errstat;
140    vol_list_lock_count++;
141    if ((errstat=rwl_writelock(&vol_list_lock)) != 0) {
142       berrno be;
143       Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
144            errstat, be.bstrerror(errstat));
145    }
146 }
147
148 void _unlock_volumes()
149 {
150    int errstat;
151    vol_list_lock_count--;
152    if ((errstat=rwl_writeunlock(&vol_list_lock)) != 0) {
153       berrno be;
154       Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
155            errstat, be.bstrerror(errstat));
156    }
157 }
158
159
160 /*
161  * List Volumes -- this should be moved to status.c
162  */
163 enum {
164    debug_lock = true,
165    debug_nolock = false
166 };
167
168 static void debug_list_volumes(const char *imsg)
169 {
170    VOLRES *vol;
171    POOL_MEM msg(PM_MESSAGE);
172
173    lock_volumes();
174    foreach_dlist(vol, vol_list) {
175       if (vol->dev) {
176          Mmsg(msg, "List from %s: %s at %p on device %s\n", imsg, 
177               vol->vol_name, vol->vol_name, vol->dev->print_name());
178       } else {
179          Mmsg(msg, "List from %s: %s at %p no dev\n", imsg, vol->vol_name, vol->vol_name);
180       }
181       Dmsg2(dbglvl, "jid=%u %s", jid(), msg.c_str());
182    }
183
184 #ifdef xxx
185    DEVICE *dev = NULL;
186    foreach_dlist(vol, vol_list) {
187       if (vol->dev == dev) {
188          Dmsg0(000, "Two Volumes on same device.\n");
189          ASSERT(0);
190          dev = vol->dev;
191       }
192    }
193 #endif
194
195 // Dmsg2(dbglvl, "List from %s: %d volumes\n", imsg, count);
196    unlock_volumes();
197 }
198
199
200 /*
201  * List Volumes -- this should be moved to status.c
202  */
203 void list_volumes(void sendit(const char *msg, int len, void *sarg), void *arg)
204 {
205    VOLRES *vol;
206    POOL_MEM msg(PM_MESSAGE);
207    int len;
208
209    lock_volumes();
210    foreach_dlist(vol, vol_list) {
211       DEVICE *dev = vol->dev;
212       if (dev) {
213          len = Mmsg(msg, "%s on device %s\n", vol->vol_name, dev->print_name());
214          sendit(msg.c_str(), len, arg);
215          len = Mmsg(msg, "    Reader=%d writers=%d reserved=%d released=%d\n", 
216             dev->can_read()?1:0, dev->num_writers, dev->reserved_device, vol->released);
217          sendit(msg.c_str(), len, arg);
218       } else {
219          len = Mmsg(msg, "%s no device. released=%d\n", vol->vol_name, vol->released);
220          sendit(msg.c_str(), len, arg);
221       }
222    }
223    unlock_volumes();
224 }
225
226 /*
227  * Create a Volume item to put in the Volume list
228  *   Ensure that the device points to it.
229  */
230 static VOLRES *new_vol_item(DCR *dcr, const char *VolumeName)
231 {
232    VOLRES *vol;
233    vol = (VOLRES *)malloc(sizeof(VOLRES));
234    memset(vol, 0, sizeof(VOLRES));
235    vol->vol_name = bstrdup(VolumeName);
236    vol->dev = dcr->dev;
237    Dmsg4(dbglvl, "jid=%u new Vol=%s at %p dev=%s\n", (int)dcr->jcr->JobId,
238          VolumeName, vol->vol_name, vol->dev->print_name());
239    return vol;
240 }
241
242 static void free_vol_item(VOLRES *vol)
243 {
244    free(vol->vol_name);
245    if (vol->dev) {
246       vol->dev->vol = NULL;
247    }
248    free(vol);
249 }
250
251
252 /*
253  * Put a new Volume entry in the Volume list. This
254  *  effectively reserves the volume so that it will
255  *  not be mounted again.
256  *
257  * If the device has any current volume associated with it,
258  *  and it is a different Volume, and the device is not busy,
259  *  we release the old Volume item and insert the new one.
260  * 
261  * It is assumed that the device is free and locked so that
262  *  we can change the device structure.
263  *
264  * Some details of the Volume list handling:
265  *
266  *  1. The Volume list entry must be attached to the drive (rather than 
267  *       attached to a job as it currently is. I.e. the drive that "owns" 
268  *       the volume (reserved, in use, mounted)
269  *       must point to the volume (still to be maintained in a list).
270  *
271  *  2. The Volume is entered in the list when a drive is reserved.  
272  *
273  *  3. When a drive is in use, the device code must appropriately update the
274  *      volume name as it changes (currently the list is static -- an entry is
275  *      removed when the Volume is no longer reserved, in use or mounted).  
276  *      The new code must keep the same list entry as long as the drive
277  *       has any volume associated with it but the volume name in the list
278  *       must be updated when the drive has a different volume mounted.
279  *
280  *  4. A job that has reserved a volume, can un-reserve the volume, and if the 
281  *      volume is not mounted, and not reserved, and not in use, it will be
282  *      removed from the list.
283  *
284  *  5. If a job wants to reserve a drive with a different Volume from the one on
285  *      the drive, it can re-use the drive for the new Volume.
286  *
287  *  6. If a job wants a Volume that is in a different drive, it can either use the
288  *      other drive or take the volume, only if the other drive is not in use or
289  *      not reserved.
290  *
291  *  One nice aspect of this is that the reserve use count and the writer use count 
292  *  already exist and are correctly programmed and will need no changes -- use 
293  *  counts are always very tricky.
294  *
295  *  The old code had a concept of "reserving" a Volume, but was changed 
296  *  to reserving and using a drive.  A volume is must be attached to (owned by) a 
297  *  drive and can move from drive to drive or be unused given certain specific 
298  *  conditions of the drive.  The key is that the drive must "own" the Volume.  
299  *  The old code had the job (dcr) owning the volume (more or less).  The job was
300  *  to change the insertion and removal of the volumes from the list to be based 
301  *  on the drive rather than the job.  
302  *
303  *  Return: VOLRES entry on success
304  *          NULL volume busy on another drive
305  */
306 VOLRES *reserve_volume(DCR *dcr, const char *VolumeName)
307 {
308    VOLRES *vol, *nvol;
309    DEVICE *dev = dcr->dev;
310
311    ASSERT(dev != NULL);
312
313    Dmsg2(dbglvl, "jid=%u reserve_volume %s\n", jid(), VolumeName);
314    /* 
315     * We lock the reservations system here to ensure
316     *  when adding a new volume that no newly scheduled
317     *  job can reserve it.
318     */
319    lock_volumes();
320    debug_list_volumes("begin reserve_volume");
321    /* 
322     * First, remove any old volume attached to this device as it
323     *  is no longer used.
324     */
325    if (dev->vol) {
326       vol = dev->vol;
327       Dmsg5(dbglvl, "jid=%u Vol attached=%s, newvol=%s release=%d on %s\n",
328          jid(), vol->vol_name, VolumeName, vol->released, dev->print_name());
329       /*
330        * Make sure we don't remove the current volume we are inserting
331        *  because it was probably inserted by another job, or it
332        *  is not being used and is marked as released.
333        */
334       if (strcmp(vol->vol_name, VolumeName) == 0) {
335          Dmsg2(dbglvl, "jid=%u === OK, vol=%s on device. set not released.\n", jid(), VolumeName);
336          vol->released = false;         /* retake vol if released previously */
337          goto get_out;                  /* Volume already on this device */
338       } else {
339          /* Don't release a volume if it is in use */
340          if (!vol->released) {
341             Dmsg2(dbglvl, "jid=%u Cannot free vol=%s. It is not released.\n", jid(), vol->vol_name);
342             vol = NULL;                  /* vol in use */
343             goto get_out;
344          }
345          Dmsg3(dbglvl, "jid=%u reserve_vol free vol=%s at %p\n", jid(), vol->vol_name, vol->vol_name);
346          unload_autochanger(dcr, -1);   /* unload the volume */
347          free_volume(dev);
348          debug_list_volumes("reserve_vol free");
349       }
350    }
351
352    /* Create a new Volume entry */
353    nvol = new_vol_item(dcr, VolumeName);
354
355    /*
356     * Now try to insert the new Volume
357     */
358    vol = (VOLRES *)vol_list->binary_insert(nvol, my_compare);
359    if (vol != nvol) {
360       Dmsg3(dbglvl, "jid=%u Found vol=%s dev-same=%d\n", jid(), vol->vol_name, dev==vol->dev);
361       /*
362        * At this point, a Volume with this name already is in the list,
363        *   so we simply release our new Volume entry. Note, this should
364        *   only happen if we are moving the volume from one drive to another.
365        */
366       Dmsg3(dbglvl, "jid=%u reserve_vol free-tmp vol=%s at %p\n", 
367             (int)dcr->jcr->JobId, vol->vol_name, vol->vol_name);
368       /*
369        * Clear dev pointer so that free_vol_item() doesn't 
370        *  take away our volume. 
371        */
372       nvol->dev = NULL;                   /* don't zap dev entry */
373       free_vol_item(nvol);
374
375       /* Check if we are trying to use the Volume on a different drive */
376       if (dev != vol->dev) {
377          /* Caller wants to switch Volume to another device */
378          if (!vol->dev->is_busy()) {
379             /* OK to move it -- I'm not sure this will work */
380             Dmsg4(dbglvl, "==== jid=%u Swap vol=%s from dev=%s to %s\n", jid(), VolumeName,
381                vol->dev->print_name(), dev->print_name());
382             vol->dev->vol = NULL;         /* take vol from old drive */
383             vol->dev->VolHdr.VolumeName[0] = 0;
384             vol->dev = dev;               /* point vol at new drive */
385             dev->vol = vol;               /* point dev at vol */
386             dev->VolHdr.VolumeName[0] = 0;
387          } else {
388             Dmsg4(dbglvl, "jid=%u Volume busy could not swap vol=%s from dev=%s to %s\n", 
389                jid(), VolumeName, vol->dev->print_name(), dev->print_name());
390             vol = NULL;                /* device busy */
391             goto get_out;
392          }
393       }
394    }
395    dev->vol = vol;
396
397 get_out:
398    if (vol) {
399       Dmsg2(dbglvl, "jid=%u === set not released. vol=%s\n", jid(), vol->vol_name);
400       vol->released = false;
401    }
402    debug_list_volumes("end new volume");
403    unlock_volumes();
404    return vol;
405 }
406
407 /* 
408  * Switch from current device to given device  
409  *   (not yet used) 
410  */
411 void switch_device(DCR *dcr, DEVICE *dev)
412 {
413    DCR save_dcr;
414
415    dev->dlock();
416    memcpy(&save_dcr, dcr, sizeof(save_dcr));
417    clean_device(dcr);                  /* clean up the dcr */
418
419    dcr->dev = dev;                     /* get new device pointer */
420    Jmsg(dcr->jcr, M_INFO, 0, _("Device switch. New device %s chosen.\n"),
421       dcr->dev->print_name());
422
423    bstrncpy(dcr->VolumeName, save_dcr.VolumeName, sizeof(dcr->VolumeName));
424    bstrncpy(dcr->media_type, save_dcr.media_type, sizeof(dcr->media_type));
425    dcr->VolCatInfo.Slot = save_dcr.VolCatInfo.Slot;
426    bstrncpy(dcr->pool_name, save_dcr.pool_name, sizeof(dcr->pool_name));
427    bstrncpy(dcr->pool_type, save_dcr.pool_type, sizeof(dcr->pool_type));
428    bstrncpy(dcr->dev_name, dev->dev_name, sizeof(dcr->dev_name));
429
430    dev->reserved_device++;
431    dcr->reserved_device = true;
432
433    dev->dunlock();
434 }
435
436 /*
437  * Search for a Volume name in the Volume list.
438  *
439  *  Returns: VOLRES entry on success
440  *           NULL if the Volume is not in the list
441  */
442 VOLRES *find_volume(DCR *dcr)
443 {
444    VOLRES vol, *fvol;
445    /* Do not lock reservations here */
446    lock_volumes();
447    vol.vol_name = bstrdup(dcr->VolumeName);
448    fvol = (VOLRES *)vol_list->binary_search(&vol, my_compare);
449    free(vol.vol_name);
450    Dmsg3(dbglvl, "jid=%u find_vol=%s found=%d\n", jid(), dcr->VolumeName, fvol!=NULL);
451    debug_list_volumes("find_volume");
452    unlock_volumes();
453    return fvol;
454 }
455
456 /* 
457  * Remove any reservation from a drive and tell the system
458  *  that the volume is unused at least by us.
459  */
460 void unreserve_device(DCR *dcr)
461 {
462    DEVICE *dev = dcr->dev;
463    if (dcr->reserved_device) {
464       dcr->reserved_device = false;
465       dev->reserved_device--;
466       Dmsg3(dbglvl, "jid=%u Dec reserve=%d dev=%s\n", jid(), dev->reserved_device, dev->print_name());
467       dcr->reserved_device = false;
468       /* If we set read mode in reserving, remove it */
469       if (dev->can_read()) {
470          dev->clear_read();
471       }
472       if (dev->num_writers < 0) {
473          Jmsg1(dcr->jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
474          dev->num_writers = 0;
475       }
476       if (dev->reserved_device == 0 && dev->num_writers == 0) {
477          volume_unused(dcr);
478       }
479    }
480 }
481
482 /*  
483  * Free a Volume from the Volume list if it is no longer used
484  *   Note, for tape drives we want to remember where the Volume
485  *   was when last used, so rather than free the volume entry,
486  *   we simply mark it "released" so when the drive is really
487  *   needed for another volume, we can reuse it.
488  *
489  *  Returns: true if the Volume found and "removed" from the list
490  *           false if the Volume is not in the list or is in use
491  */
492 bool volume_unused(DCR *dcr)
493 {
494    DEVICE *dev = dcr->dev;
495
496    if (dev->vol == NULL) {
497       Dmsg2(dbglvl, "jid=%u vol_unused: no vol on %s\n", (int)dcr->jcr->JobId, dev->print_name());
498       debug_list_volumes("null vol cannot unreserve_volume");
499       return false;
500    }
501
502 #ifdef xxx
503    if (dev->is_busy()) {
504       Dmsg2(dbglvl, "jid=%u vol_unused: no vol on %s\n", (int)dcr->jcr->JobId, dev->print_name());
505       debug_list_volumes("dev busy cannot unreserve_volume");
506       return false;
507    }
508 #endif
509 #ifdef xxx
510    if (dev->num_writers > 0 || dev->reserved_device > 0) {
511       ASSERT(0);
512    }
513 #endif
514
515    /*  
516     * If this is a tape, we do not free the volume, rather we wait
517     *  until the autoloader unloads it, or until another tape is
518     *  explicitly read in this drive. This allows the SD to remember
519     *  where the tapes are or last were.
520     */
521    dev->vol->released = true;
522    if (dev->is_tape()) { // || dev->is_autochanger()) {
523       return true;
524    } else {
525       /*
526        * Note, this frees the volume reservation entry, but the
527        *   file descriptor remains open with the OS.
528        */
529       return free_volume(dev);
530    }
531 }
532
533 /*
534  * Unconditionally release the volume entry
535  */
536 bool free_volume(DEVICE *dev)
537 {
538    VOLRES *vol;
539
540    if (dev->vol == NULL) {
541       Dmsg2(dbglvl, "jid=%u No vol on dev %s\n", jid(), dev->print_name());
542       return false;
543    }
544    lock_volumes();
545    vol = dev->vol;
546    dev->vol = NULL;
547    vol_list->remove(vol);
548    Dmsg3(dbglvl, "jid=%u free_volume %s dev=%s\n", jid(), vol->vol_name, dev->print_name());
549    free_vol_item(vol);
550    debug_list_volumes("free_volume");
551    unlock_volumes();
552    return vol != NULL;
553 }
554
555       
556 /* Create the Volume list */
557 void create_volume_list()
558 {
559    VOLRES *vol = NULL;
560    if (vol_list == NULL) {
561       vol_list = New(dlist(vol, &vol->link));
562    }
563 }
564
565 /* Release all Volumes from the list */
566 void free_volume_list()
567 {
568    VOLRES *vol;
569    if (!vol_list) {
570       return;
571    }
572    lock_volumes();
573    foreach_dlist(vol, vol_list) {
574       if (vol->dev) {
575          Dmsg3(dbglvl, "jid=%u free vol_list Volume=%s dev=%s\n", jid(),
576                vol->vol_name, vol->dev->print_name());
577       } else {
578          Dmsg3(dbglvl, "jid=%u free vol_list Volume=%s dev=%p\n", jid(), 
579                vol->vol_name, vol->dev);
580       }
581       free(vol->vol_name);
582       vol->vol_name = NULL;
583    }
584    delete vol_list;
585    vol_list = NULL;
586    unlock_volumes();
587 }
588
589 bool is_volume_in_use(DCR *dcr)
590 {
591    bool rtn = false;
592    VOLRES *vol;
593
594    lock_volumes();
595    vol = find_volume(dcr);
596    if (!vol) {
597       Dmsg2(dbglvl, "jid=%u Vol=%s not in use.\n", jid(), dcr->VolumeName);
598       goto get_out;                   /* vol not in list */
599    }
600    ASSERT(vol->dev != NULL);
601
602    if (dcr->dev == vol->dev) {        /* same device OK */
603       Dmsg2(dbglvl, "jid=%u Vol=%s on same dev.\n", jid(), dcr->VolumeName);
604       goto get_out;
605    } else {
606       Dmsg4(dbglvl, "jid=%u Vol=%s on %s we have %s\n", jid(), dcr->VolumeName,
607             vol->dev->print_name(), dcr->dev->print_name());
608    }
609    if (!vol->dev->is_busy()) {
610       Dmsg3(dbglvl, "jid=%u Vol=%s dev=%s not busy.\n", jid(), dcr->VolumeName, vol->dev->print_name());
611       goto get_out;
612    } else {
613       Dmsg3(dbglvl, "jid=%u Vol=%s dev=%s busy.\n", jid(), dcr->VolumeName, vol->dev->print_name());
614    }
615    Dmsg3(dbglvl, "jid=%u Vol=%s in use by %s.\n", jid(), dcr->VolumeName, vol->dev->print_name());
616    rtn = true;
617
618 get_out:
619    unlock_volumes();
620    return rtn;
621 }
622
623
624 /*
625  * We get the following type of information:
626  *
627  * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=1 copy=0 strip=0
628  *  use device=zzz
629  *  use device=aaa
630  *  use device=bbb
631  * use storage=xxx media_type=yyy pool_name=xxx pool_type=yyy append=0 copy=0 strip=0
632  *  use device=bbb
633  *
634  */
635 static bool use_storage_cmd(JCR *jcr)
636 {
637    POOL_MEM store_name, dev_name, media_type, pool_name, pool_type;
638    BSOCK *dir = jcr->dir_bsock;
639    int append;
640    bool ok;       
641    int Copy, Stripe;
642    DIRSTORE *store;
643    RCTX rctx;
644    alist *dirstore;
645
646    memset(&rctx, 0, sizeof(RCTX));
647    rctx.jcr = jcr;
648    /*
649     * If there are multiple devices, the director sends us
650     *   use_device for each device that it wants to use.
651     */
652    dirstore = New(alist(10, not_owned_by_alist));
653    jcr->reserve_msgs = New(alist(10, not_owned_by_alist));  
654    do {
655       Dmsg2(dbglvl, "jid=%u <dird: %s", jid(), dir->msg);
656       ok = sscanf(dir->msg, use_storage, store_name.c_str(), 
657                   media_type.c_str(), pool_name.c_str(), 
658                   pool_type.c_str(), &append, &Copy, &Stripe) == 7;
659       if (!ok) {
660          break;
661       }
662       if (append) {
663          jcr->write_store = dirstore;
664       } else {
665          jcr->read_store = dirstore;
666       }
667       rctx.append = append;
668       unbash_spaces(store_name);
669       unbash_spaces(media_type);
670       unbash_spaces(pool_name);
671       unbash_spaces(pool_type);
672       store = new DIRSTORE;
673       dirstore->append(store);
674       memset(store, 0, sizeof(DIRSTORE));
675       store->device = New(alist(10));
676       bstrncpy(store->name, store_name, sizeof(store->name));
677       bstrncpy(store->media_type, media_type, sizeof(store->media_type));
678       bstrncpy(store->pool_name, pool_name, sizeof(store->pool_name));
679       bstrncpy(store->pool_type, pool_type, sizeof(store->pool_type));
680       store->append = append;
681
682       /* Now get all devices */
683       while (dir->recv() >= 0) {
684          Dmsg2(dbglvl, "jid=%u <dird device: %s", jid(), dir->msg);
685          ok = sscanf(dir->msg, use_device, dev_name.c_str()) == 1;
686          if (!ok) {
687             break;
688          }
689          unbash_spaces(dev_name);
690          store->device->append(bstrdup(dev_name.c_str()));
691       }
692    }  while (ok && dir->recv() >= 0);
693
694    /* Developer debug code */
695    char *device_name;
696    if (debug_level >= dbglvl) {
697       foreach_alist(store, dirstore) {
698          Dmsg6(dbglvl, "jid=%u Storage=%s media_type=%s pool=%s pool_type=%s append=%d\n", 
699             (int)rctx.jcr->JobId,
700             store->name, store->media_type, store->pool_name, 
701             store->pool_type, store->append);
702          foreach_alist(device_name, store->device) {
703             Dmsg2(dbglvl, "jid=%u     Device=%s\n", jid(), device_name);
704          }
705       }
706    }
707
708    init_jcr_device_wait_timers(jcr);
709    jcr->dcr = new_dcr(jcr, NULL, NULL);         /* get a dcr */
710    if (!jcr->dcr) {
711       BSOCK *dir = jcr->dir_bsock;
712       dir->fsend(_("3939 Could not get dcr\n"));
713       Dmsg1(dbglvl, ">dird: %s", dir->msg);
714       ok = false;
715    }
716    /*                    
717     * At this point, we have a list of all the Director's Storage
718     *  resources indicated for this Job, which include Pool, PoolType,
719     *  storage name, and Media type.     
720     * Then for each of the Storage resources, we have a list of
721     *  device names that were given.
722     *
723     * Wiffle through them and find one that can do the backup.
724     */
725    if (ok) {
726       int wait_for_device_retries = 0;  
727       int repeat = 0;
728       bool fail = false;
729       rctx.notify_dir = true;
730
731       lock_reservations();
732       for ( ; !fail && !job_canceled(jcr); ) {
733          pop_reserve_messages(jcr);
734          rctx.suitable_device = false;
735          rctx.have_volume = false;
736          rctx.VolumeName[0] = 0;
737          rctx.any_drive = false;
738          if (!jcr->PreferMountedVols) {
739             /*
740              * Here we try to find a drive that is not used.
741              * This will maximize the use of available drives.
742              *
743              */
744             rctx.num_writers = 20000000;   /* start with impossible number */
745             rctx.low_use_drive = NULL;
746             rctx.PreferMountedVols = false;                
747             rctx.exact_match = false;
748             rctx.autochanger_only = true;
749             Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
750                (int)rctx.jcr->JobId,
751                rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
752                rctx.autochanger_only, rctx.any_drive);
753             if ((ok = find_suitable_device_for_job(jcr, rctx))) {
754                break;
755             }
756             /* Look through all drives possibly for low_use drive */
757             if (rctx.low_use_drive) {
758                rctx.try_low_use_drive = true;
759                if ((ok = find_suitable_device_for_job(jcr, rctx))) {
760                   break;
761                }
762                rctx.try_low_use_drive = false;
763             }
764             rctx.autochanger_only = false;
765             Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
766                (int)rctx.jcr->JobId,
767                rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
768                rctx.autochanger_only, rctx.any_drive);
769             if ((ok = find_suitable_device_for_job(jcr, rctx))) {
770                break;
771             }
772          }
773          /*
774           * Now we look for a drive that may or may not be in
775           *  use.
776           */
777          /* Look for an exact Volume match all drives */
778          rctx.PreferMountedVols = true;
779          rctx.exact_match = true;
780          rctx.autochanger_only = false;
781          Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
782             (int)rctx.jcr->JobId,
783             rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
784             rctx.autochanger_only, rctx.any_drive);
785          if ((ok = find_suitable_device_for_job(jcr, rctx))) {
786             break;
787          }
788          /* Look for any mounted drive */
789          rctx.exact_match = false;
790          Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
791             (int)rctx.jcr->JobId,
792             rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
793             rctx.autochanger_only, rctx.any_drive);
794          if ((ok = find_suitable_device_for_job(jcr, rctx))) {
795             break;
796          }
797          /* Try any drive */
798          rctx.any_drive = true;
799          Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
800             (int)rctx.jcr->JobId,
801             rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
802             rctx.autochanger_only, rctx.any_drive);
803          if ((ok = find_suitable_device_for_job(jcr, rctx))) {
804             break;
805          }
806          /* Keep reservations locked *except* during wait_for_device() */
807          unlock_reservations();
808          /*     
809           * The idea of looping on repeat a few times it to ensure
810           * that if there is some subtle timing problem between two
811           * jobs, we will simply try again, and most likely succeed.
812           * This can happen if one job reserves a drive or finishes using
813           * a drive at the same time a second job wants it.
814           */
815          if (repeat++ > 1) {              /* try algorithm 3 times */
816             bmicrosleep(30, 0);           /* wait a bit */
817             Dmsg1(dbglvl, "jid=%u repeat reserve algorithm\n", (int)rctx.jcr->JobId);
818          } else if (!rctx.suitable_device || !wait_for_device(jcr, wait_for_device_retries)) {
819             Dmsg1(dbglvl, "jid=%u Fail. !suitable_device || !wait_for_device\n",
820                  (int)rctx.jcr->JobId);
821             fail = true;
822          }   
823          lock_reservations();
824          dir->signal(BNET_HEARTBEAT);  /* Inform Dir that we are alive */
825       }
826       unlock_reservations();
827       if (!ok) {
828          /*
829           * If we get here, there are no suitable devices available, which
830           *  means nothing configured.  If a device is suitable but busy
831           *  with another Volume, we will not come here.
832           */
833          unbash_spaces(dir->msg);
834          pm_strcpy(jcr->errmsg, dir->msg);
835          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
836          Jmsg(jcr, M_FATAL, 0, _("\n"
837             "     Device \"%s\" with MediaType \"%s\" requested by DIR not found in SD Device resources.\n"),
838               dev_name.c_str(), media_type.c_str());
839          dir->fsend(NO_device, dev_name.c_str());
840
841          Dmsg2(dbglvl, "jid=%u >dird: %s", jid(), dir->msg);
842       }
843    } else {
844       unbash_spaces(dir->msg);
845       pm_strcpy(jcr->errmsg, dir->msg);
846       Jmsg(jcr, M_FATAL, 0, _("Failed command: %s\n"), jcr->errmsg);
847       dir->fsend(BAD_use, jcr->errmsg);
848       Dmsg2(dbglvl, "jid=%u >dird: %s", jid(), dir->msg);
849    }
850
851    release_reserve_messages(jcr);
852    return ok;
853 }
854
855
856 /*
857  * Walk through the autochanger resources and check if
858  *  the volume is in one of them.
859  * 
860  * Returns:  true  if volume is in device
861  *           false otherwise
862  */
863 static bool is_vol_in_autochanger(RCTX &rctx, VOLRES *vol)
864 {
865    AUTOCHANGER *changer = vol->dev->device->changer_res;
866
867    /* Find resource, and make sure we were able to open it */
868    if (strcmp(rctx.device_name, changer->hdr.name) == 0) {
869       Dmsg2(dbglvl, "jid=%u Found changer device %s\n",
870                      (int)rctx.jcr->JobId, vol->dev->device->hdr.name);
871       return true;
872    }  
873    Dmsg2(dbglvl, "jid=%u Incorrect changer device %s\n", 
874                   (int)rctx.jcr->JobId, changer->hdr.name);
875    return false;
876 }
877
878 /*
879  * Search for a device suitable for this job.
880  */
881 bool find_suitable_device_for_job(JCR *jcr, RCTX &rctx)
882 {
883    bool ok = false;
884    DIRSTORE *store;
885    char *device_name;
886    alist *dirstore;
887    DCR *dcr = jcr->dcr;
888
889    if (rctx.append) {
890       dirstore = jcr->write_store;
891    } else {
892       dirstore = jcr->read_store;
893    }
894    Dmsg5(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d\n",
895       (int)rctx.jcr->JobId,
896       rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
897       rctx.autochanger_only);
898
899    /* 
900     * If the appropriate conditions of this if are met, namely that
901     *  we are appending and the user wants mounted drive (or we
902     *  force try a mounted drive because they are all busy), we
903     *  start by looking at all the Volumes in the volume list.
904     */
905    if (!vol_list->empty() && rctx.append && rctx.PreferMountedVols) {
906       dlist *temp_vol_list, *save_vol_list;
907       VOLRES *vol = NULL;
908       lock_volumes();
909       Dmsg0(dbglvl, "lock volumes\n");                           
910
911       /*  
912        * Create a temporary copy of the volume list.  We do this,
913        *   to avoid having the volume list locked during the
914        *   call to reserve_device(), which would cause a deadlock.
915        * Note, we may want to add an update counter on the vol_list
916        *   so that if it is modified while we are traversing the copy
917        *   we can take note and act accordingly (probably redo the 
918        *   search at least a few times).
919        */
920       Dmsg1(dbglvl, "jid=%u duplicate vol list\n", (int)rctx.jcr->JobId);
921       temp_vol_list = New(dlist(vol, &vol->link));
922       foreach_dlist(vol, vol_list) {
923          VOLRES *nvol;
924          VOLRES *tvol = (VOLRES *)malloc(sizeof(VOLRES));
925          memset(tvol, 0, sizeof(VOLRES));
926          tvol->vol_name = bstrdup(vol->vol_name);
927          tvol->dev = vol->dev;
928          nvol = (VOLRES *)temp_vol_list->binary_insert(tvol, my_compare);
929          if (tvol != nvol) {
930             tvol->dev = NULL;                   /* don't zap dev entry */
931             free_vol_item(tvol);
932             Pmsg0(000, "Logic error. Duplicating vol list hit duplicate.\n");
933             Jmsg(jcr, M_WARNING, 0, "Logic error. Duplicating vol list hit duplicate.\n");
934          }
935       }
936       unlock_volumes();
937
938       /* Look through reserved volumes for one we can use */
939       Dmsg1(dbglvl, "jid=%u look for vol in vol list\n", (int)rctx.jcr->JobId);
940       foreach_dlist(vol, temp_vol_list) {
941          if (!vol->dev) {
942             Dmsg2(dbglvl, "jid=%u vol=%s no dev\n", (int)rctx.jcr->JobId, vol->vol_name);
943             continue;
944          }
945          /* Check with Director if this Volume is OK */
946          bstrncpy(dcr->VolumeName, vol->vol_name, sizeof(dcr->VolumeName));
947          if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE)) {
948             continue;
949          }
950
951          Dmsg2(dbglvl, "jid=%u vol=%s OK for this job\n", (int)rctx.jcr->JobId, vol->vol_name);
952          foreach_alist(store, dirstore) {
953             int stat;
954             rctx.store = store;
955             foreach_alist(device_name, store->device) {
956                /* Found a device, try to use it */
957                rctx.device_name = device_name;
958                rctx.device = vol->dev->device;
959
960                if (vol->dev->is_autochanger()) {
961                   Dmsg2(dbglvl, "jid=%u vol=%s is in changer\n", (int)rctx.jcr->JobId, 
962                         vol->vol_name);
963                   if (!is_vol_in_autochanger(rctx, vol)) {
964                      continue;
965                   }
966                } else if (strcmp(device_name, vol->dev->device->hdr.name) != 0) {
967                   Dmsg3(dbglvl, "jid=%u device=%s not suitable want %s\n", (int)rctx.jcr->JobId, 
968                         vol->dev->device->hdr.name, device_name);
969                   continue;
970                }
971
972                bstrncpy(rctx.VolumeName, vol->vol_name, sizeof(rctx.VolumeName));
973                rctx.have_volume = true;
974                /* Try reserving this device and volume */
975                Dmsg3(dbglvl, "jid=%u try vol=%s on device=%s\n", (int)rctx.jcr->JobId, 
976                      rctx.VolumeName, device_name);
977                stat = reserve_device(rctx);
978                if (stat == 1) {             /* found available device */
979                   Dmsg2(dbglvl, "jid=%u Suitable device found=%s\n", (int)rctx.jcr->JobId, 
980                         device_name);
981                   ok = true;
982                   break;
983                } else if (stat == 0) {      /* device busy */
984                   Dmsg2(dbglvl, "jid=%u Suitable device=%s, busy: not use\n", 
985                         (int)rctx.jcr->JobId, device_name);
986                } else {
987                   /* otherwise error */
988                   Dmsg1(dbglvl, "jid=%u No suitable device found.\n", (int)rctx.jcr->JobId);
989                }
990                rctx.have_volume = false;
991                rctx.VolumeName[0] = 0;
992             }
993             if (ok) {
994                break;
995             }
996          }
997          if (ok) {
998             break;
999          }
1000       } /* end for loop over reserved volumes */
1001
1002       Dmsg1(dbglvl, "%u lock volumes\n", jid());
1003       lock_volumes();
1004       save_vol_list = vol_list;
1005       vol_list = temp_vol_list;
1006       free_volume_list();                  /* release temp_vol_list */
1007       vol_list = save_vol_list;
1008       Dmsg1(dbglvl, "jid=%u deleted temp vol list\n", (int)rctx.jcr->JobId);
1009       Dmsg1(dbglvl, "jid=%u unlock volumes\n", (int)rctx.jcr->JobId);
1010       unlock_volumes();
1011    }
1012    if (ok) {
1013       Dmsg2(dbglvl, "jid=%u got vol %s from in-use vols list\n", (int)rctx.jcr->JobId,
1014             rctx.VolumeName);
1015       return true;
1016    }
1017
1018    /* 
1019     * No reserved volume we can use, so now search for an available device.  
1020     *
1021     * For each storage device that the user specified, we
1022     *  search and see if there is a resource for that device.
1023     */
1024    foreach_alist(store, dirstore) {
1025       rctx.store = store;
1026       foreach_alist(device_name, store->device) {
1027          int stat;
1028          rctx.device_name = device_name;
1029          stat = search_res_for_device(rctx); 
1030          if (stat == 1) {             /* found available device */
1031             Dmsg2(dbglvl, "jid=%u available device found=%s\n", (int)rctx.jcr->JobId, 
1032                   device_name);
1033             ok = true;
1034             break;
1035          } else if (stat == 0) {      /* device busy */
1036             Dmsg2(dbglvl, "jid=%u Suitable device=%s, busy: not use\n", 
1037                   (int)rctx.jcr->JobId, device_name);
1038          } else {
1039             /* otherwise error */
1040             Dmsg1(dbglvl, "jid=%u No suitable device found.\n", (int)rctx.jcr->JobId);
1041          }
1042       }
1043       if (ok) {
1044          break;
1045       }
1046    }
1047    return ok;
1048 }
1049
1050 /*
1051  * Search for a particular storage device with particular storage
1052  *  characteristics (MediaType).
1053  */
1054 int search_res_for_device(RCTX &rctx) 
1055 {
1056    AUTOCHANGER *changer;
1057    int stat;
1058
1059    Dmsg2(dbglvl, "jid=%u search res for %s\n", (int)rctx.jcr->JobId, rctx.device_name);
1060    /* Look through Autochangers first */
1061    foreach_res(changer, R_AUTOCHANGER) {
1062       Dmsg2(dbglvl, "jid=%u Try match changer res=%s\n", (int)rctx.jcr->JobId, changer->hdr.name);
1063       /* Find resource, and make sure we were able to open it */
1064       if (strcmp(rctx.device_name, changer->hdr.name) == 0) {
1065          /* Try each device in this AutoChanger */
1066          foreach_alist(rctx.device, changer->device) {
1067             Dmsg2(dbglvl, "jid=%u Try changer device %s\n", (int)rctx.jcr->JobId, 
1068                   rctx.device->hdr.name);
1069             stat = reserve_device(rctx);
1070             if (stat != 1) {             /* try another device */
1071                continue;
1072             }
1073             /* Debug code */
1074             if (rctx.store->append == SD_APPEND) {
1075                Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for append.\n", 
1076                   (int)rctx.jcr->JobId, rctx.device->hdr.name,
1077                   rctx.jcr->dcr->dev->reserved_device);
1078             } else {
1079                Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for read.\n", 
1080                   (int)rctx.jcr->JobId, rctx.device->hdr.name,
1081                   rctx.jcr->read_dcr->dev->reserved_device);
1082             }
1083             return stat;
1084          }
1085       }
1086    }
1087
1088    /* Now if requested look through regular devices */
1089    if (!rctx.autochanger_only) {
1090       foreach_res(rctx.device, R_DEVICE) {
1091          Dmsg2(dbglvl, "jid=%u Try match res=%s\n", (int)rctx.jcr->JobId, rctx.device->hdr.name);
1092          /* Find resource, and make sure we were able to open it */
1093          if (strcmp(rctx.device_name, rctx.device->hdr.name) == 0) {
1094             stat = reserve_device(rctx);
1095             if (stat != 1) {             /* try another device */
1096                continue;
1097             }
1098             /* Debug code */
1099             if (rctx.store->append == SD_APPEND) {
1100                Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for append.\n", 
1101                   (int)rctx.jcr->JobId, rctx.device->hdr.name,
1102                   rctx.jcr->dcr->dev->reserved_device);
1103             } else {
1104                Dmsg3(dbglvl, "jid=%u Device %s reserved=%d for read.\n", 
1105                   (int)rctx.jcr->JobId, rctx.device->hdr.name,
1106                   rctx.jcr->read_dcr->dev->reserved_device);
1107             }
1108             return stat;
1109          }
1110       }
1111    }
1112    return -1;                    /* nothing found */
1113 }
1114
1115 /*
1116  *  Try to reserve a specific device.
1117  *
1118  *  Returns: 1 -- OK, have DCR
1119  *           0 -- must wait
1120  *          -1 -- fatal error
1121  */
1122 static int reserve_device(RCTX &rctx)
1123 {
1124    bool ok;
1125    DCR *dcr;
1126    const int name_len = MAX_NAME_LENGTH;
1127
1128    /* Make sure MediaType is OK */
1129    Dmsg3(dbglvl, "jid=%u chk MediaType device=%s request=%s\n",
1130          (int)rctx.jcr->JobId,
1131          rctx.device->media_type, rctx.store->media_type);
1132    if (strcmp(rctx.device->media_type, rctx.store->media_type) != 0) {
1133       return -1;
1134    }
1135
1136    /* Make sure device exists -- i.e. we can stat() it */
1137    if (!rctx.device->dev) {
1138       rctx.device->dev = init_dev(rctx.jcr, rctx.device);
1139    }
1140    if (!rctx.device->dev) {
1141       if (rctx.device->changer_res) {
1142         Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
1143            "     Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
1144              rctx.device->hdr.name, rctx.device_name);
1145       } else {
1146          Jmsg(rctx.jcr, M_WARNING, 0, _("\n"
1147             "     Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
1148               rctx.device_name);
1149       }
1150       return -1;  /* no use waiting */
1151    }  
1152
1153    rctx.suitable_device = true;
1154    Dmsg2(dbglvl, "jid=%u try reserve %s\n", rctx.jcr->JobId, rctx.device->hdr.name);
1155    rctx.jcr->dcr = dcr = new_dcr(rctx.jcr, rctx.jcr->dcr, rctx.device->dev);
1156    if (!dcr) {
1157       BSOCK *dir = rctx.jcr->dir_bsock;
1158       dir->fsend(_("3926 Could not get dcr for device: %s\n"), rctx.device_name);
1159       Dmsg1(dbglvl, ">dird: %s", dir->msg);
1160       return -1;
1161    }
1162    bstrncpy(dcr->pool_name, rctx.store->pool_name, name_len);
1163    bstrncpy(dcr->pool_type, rctx.store->pool_type, name_len);
1164    bstrncpy(dcr->media_type, rctx.store->media_type, name_len);
1165    bstrncpy(dcr->dev_name, rctx.device_name, name_len);
1166    if (rctx.store->append == SD_APPEND) {
1167       Dmsg3(dbglvl, "jid=%u have_vol=%d vol=%s\n", (int)rctx.jcr->JobId,
1168           rctx.have_volume, rctx.VolumeName);                                   
1169       ok = reserve_device_for_append(dcr, rctx);
1170       if (!ok) {
1171          goto bail_out;
1172       }
1173
1174       rctx.jcr->dcr = dcr;
1175       Dmsg6(dbglvl, "jid=%u Reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
1176                (int)rctx.jcr->JobId,
1177                dcr->dev->reserved_device,
1178                dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
1179       if (rctx.have_volume) {
1180          if (reserve_volume(dcr, rctx.VolumeName)) {
1181             Dmsg2(dbglvl, "jid=%u Reserved vol=%s\n", jid(), rctx.VolumeName);
1182          } else {
1183             Dmsg2(dbglvl, "jid=%u Could not reserve vol=%s\n", jid(), rctx.VolumeName);
1184             goto bail_out;
1185          }
1186       } else {
1187          dcr->any_volume = true;
1188          if (dir_find_next_appendable_volume(dcr)) {
1189             bstrncpy(rctx.VolumeName, dcr->VolumeName, sizeof(rctx.VolumeName));
1190             Dmsg2(dbglvl, "jid=%u looking for Volume=%s\n", (int)rctx.jcr->JobId, rctx.VolumeName);
1191             rctx.have_volume = true;
1192          } else {
1193             Dmsg1(dbglvl, "jid=%u No next volume found\n", (int)rctx.jcr->JobId);
1194             rctx.have_volume = false;
1195             rctx.VolumeName[0] = 0;
1196             /*
1197              * If there is at least one volume that is valid and in use,
1198              *   but we get here, check if we are running with prefers
1199              *   non-mounted drives.  In that case, we have selected a
1200              *   non-used drive and our one and only volume is mounted
1201              *   elsewhere, so we bail out and retry using that drive.
1202              */
1203             if (dcr->volume_in_use && !rctx.PreferMountedVols) {
1204                rctx.PreferMountedVols = true;
1205                if (dcr->VolumeName[0]) {
1206                   unreserve_device(dcr);
1207                }
1208                goto bail_out;
1209             }
1210             /*
1211              * Note. Under some circumstances, the Director can hand us
1212              *  a Volume name that is not the same as the one on the current
1213              *  drive, and in that case, the call above to find the next
1214              *  volume will fail because in attempting to reserve the Volume
1215              *  the code will realize that we already have a tape mounted,
1216              *  and it will fail.  This *should* only happen if there are 
1217              *  writers, thus the following test.  In that case, we simply
1218              *  bail out, and continue waiting, rather than plunging on
1219              *  and hoping that the operator can resolve the problem. 
1220              */
1221             if (dcr->dev->num_writers != 0) {
1222                if (dcr->VolumeName[0]) {
1223                   unreserve_device(dcr);
1224                }
1225                goto bail_out;
1226             }
1227          }
1228       }
1229    } else {
1230       ok = reserve_device_for_read(dcr);
1231       if (ok) {
1232          rctx.jcr->read_dcr = dcr;
1233          Dmsg6(dbglvl, "jid=%u Read reserved=%d dev_name=%s mediatype=%s pool=%s ok=%d\n",
1234                (int)rctx.jcr->JobId,
1235                dcr->dev->reserved_device,
1236                dcr->dev_name, dcr->media_type, dcr->pool_name, ok);
1237       }
1238    }
1239    if (!ok) {
1240       goto bail_out;
1241    }
1242
1243    if (rctx.notify_dir) {
1244       POOL_MEM dev_name;
1245       BSOCK *dir = rctx.jcr->dir_bsock;
1246       pm_strcpy(dev_name, rctx.device->hdr.name);
1247       bash_spaces(dev_name);
1248       ok = dir->fsend(OK_device, dev_name.c_str());  /* Return real device name */
1249       Dmsg2(dbglvl, "jid=%u >dird changer: %s", jid(), dir->msg);
1250    } else {
1251       ok = true;
1252    }
1253    return ok ? 1 : -1;
1254
1255 bail_out:
1256    rctx.have_volume = false;
1257    Dmsg1(dbglvl, "jid=%u Not OK.\n", (int)rctx.jcr->JobId);
1258    rctx.VolumeName[0] = 0;
1259    return 0;
1260 }
1261
1262 /*
1263  * We "reserve" the drive by setting the ST_READ bit. No one else
1264  *  should touch the drive until that is cleared.
1265  *  This allows the DIR to "reserve" the device before actually
1266  *  starting the job. 
1267  */
1268 static bool reserve_device_for_read(DCR *dcr)
1269 {
1270    DEVICE *dev = dcr->dev;
1271    JCR *jcr = dcr->jcr;
1272    bool ok = false;
1273
1274    ASSERT(dcr);
1275
1276    dev->dlock();  
1277
1278    if (is_device_unmounted(dev)) {             
1279       Dmsg2(dbglvl, "jid=%u Device %s is BLOCKED due to user unmount.\n", 
1280          (int)jcr->JobId, dev->print_name());
1281       Mmsg(jcr->errmsg, _("3601 JobId=%u device %s is BLOCKED due to user unmount.\n"),
1282            jcr->JobId, dev->print_name());
1283       queue_reserve_message(jcr);
1284       goto bail_out;
1285    }
1286
1287    if (dev->is_busy()) {
1288       Dmsg5(dbglvl, "jid=%u Device %s is busy ST_READ=%d num_writers=%d reserved=%d.\n", 
1289          (int)jcr->JobId, dev->print_name(),
1290          dev->state & ST_READ?1:0, dev->num_writers, dev->reserved_device);
1291       Mmsg(jcr->errmsg, _("3602 JobId=%u device %s is busy (already reading/writing).\n"),
1292             jcr->JobId, dev->print_name());
1293       queue_reserve_message(jcr);
1294       goto bail_out;
1295    }
1296
1297    dev->clear_append();
1298    dev->set_read();
1299    ok = true;
1300    dev->reserved_device++;
1301    Dmsg4(dbglvl, "jid=%u Inc reserve=%d dev=%s %p\n", (int)jcr->JobId,
1302       dev->reserved_device, dev->print_name(), dev);
1303    dcr->reserved_device = true;
1304
1305 bail_out:
1306    dev->dunlock();
1307    return ok;
1308 }
1309
1310
1311 /*
1312  * We reserve the device for appending by incrementing the 
1313  *  reserved_device. We do virtually all the same work that
1314  *  is done in acquire_device_for_append(), but we do
1315  *  not attempt to mount the device. This routine allows
1316  *  the DIR to reserve multiple devices before *really* 
1317  *  starting the job. It also permits the SD to refuse 
1318  *  certain devices (not up, ...).
1319  *
1320  * Note, in reserving a device, if the device is for the
1321  *  same pool and the same pool type, then it is acceptable.
1322  *  The Media Type has already been checked. If we are
1323  *  the first tor reserve the device, we put the pool
1324  *  name and pool type in the device record.
1325  */
1326 static bool reserve_device_for_append(DCR *dcr, RCTX &rctx)
1327 {
1328    JCR *jcr = dcr->jcr;
1329    DEVICE *dev = dcr->dev;
1330    bool ok = false;
1331
1332    ASSERT(dcr);
1333
1334    dev->dlock();
1335
1336    /* If device is being read, we cannot write it */
1337    if (dev->can_read()) {
1338       Mmsg(jcr->errmsg, _("3603 JobId=%u device %s is busy reading.\n"), 
1339          jcr->JobId, dev->print_name());
1340       Dmsg2(dbglvl, "jid=%u %s", jid(), jcr->errmsg);
1341       queue_reserve_message(jcr);
1342       goto bail_out;
1343    }
1344
1345    /* If device is unmounted, we are out of luck */
1346    if (is_device_unmounted(dev)) {
1347       Mmsg(jcr->errmsg, _("3604 JobId=%u device %s is BLOCKED due to user unmount.\n"), 
1348          jcr->JobId, dev->print_name());
1349       Dmsg2(dbglvl, "jid=%u %s", jid(), jcr->errmsg);
1350       queue_reserve_message(jcr);
1351       goto bail_out;
1352    }
1353
1354    Dmsg2(dbglvl, "jid=%u reserve_append device is %s\n", 
1355        (int)jcr->JobId, dev->print_name());
1356
1357    /* Now do detailed tests ... */
1358    if (can_reserve_drive(dcr, rctx) != 1) {
1359       Dmsg1(dbglvl, "jid=%u can_reserve_drive!=1\n", (int)jcr->JobId);
1360       goto bail_out;
1361    }
1362
1363    dev->reserved_device++;
1364    Dmsg4(dbglvl, "jid=%u Inc reserve=%d dev=%s %p\n", (int)jcr->JobId, dev->reserved_device, 
1365       dev->print_name(), dev);
1366    dcr->reserved_device = true;
1367    ok = true;
1368
1369 bail_out:
1370    dev->dunlock();
1371    return ok;
1372 }
1373
1374 static int is_pool_ok(DCR *dcr)
1375 {
1376    DEVICE *dev = dcr->dev;
1377    JCR *jcr = dcr->jcr;
1378
1379    /* Now check if we want the same Pool and pool type */
1380    if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
1381        strcmp(dev->pool_type, dcr->pool_type) == 0) {
1382       /* OK, compatible device */
1383       Dmsg1(dbglvl, "OK dev: %s num_writers=0, reserved, pool matches\n", dev->print_name());
1384       return 1;
1385    } else {
1386       /* Drive Pool not suitable for us */
1387       Mmsg(jcr->errmsg, _(
1388 "3608 JobId=%u wants Pool=\"%s\" but have Pool=\"%s\" nreserve=%d on drive %s.\n"), 
1389             (uint32_t)jcr->JobId, dcr->pool_name, dev->pool_name,
1390             dev->reserved_device, dev->print_name());
1391       queue_reserve_message(jcr);
1392       Dmsg2(dbglvl, "failed: busy num_writers=0, reserved, pool=%s wanted=%s\n",
1393          dev->pool_name, dcr->pool_name);
1394    }
1395    return 0;
1396 }
1397
1398 static bool is_max_jobs_ok(DCR *dcr) 
1399 {
1400    DEVICE *dev = dcr->dev;
1401    JCR *jcr = dcr->jcr;
1402
1403    Dmsg4(dbglvl, "MaxJobs=%d Jobs=%d reserves=%d Vol=%s\n",
1404          dcr->VolCatInfo.VolCatMaxJobs,
1405          dcr->VolCatInfo.VolCatJobs, dev->reserved_device,
1406          dcr->VolumeName);
1407    if (dcr->VolCatInfo.VolCatMaxJobs > 0 && dcr->VolCatInfo.VolCatMaxJobs <=
1408         (dcr->VolCatInfo.VolCatJobs + dev->reserved_device)) {
1409       /* Max Job Vols depassed or already reserved */
1410       Mmsg(jcr->errmsg, _("3610 JobId=%u Volume max jobs exceeded on drive %s.\n"), 
1411             (uint32_t)jcr->JobId, dev->print_name());
1412       queue_reserve_message(jcr);
1413       Dmsg1(dbglvl, "reserve dev failed: %s", jcr->errmsg);
1414       return false;                /* wait */
1415    }
1416    return true;
1417 }
1418
1419 /*
1420  * Returns: 1 if drive can be reserved
1421  *          0 if we should wait
1422  *         -1 on error or impossibility
1423  */
1424 static int can_reserve_drive(DCR *dcr, RCTX &rctx) 
1425 {
1426    DEVICE *dev = dcr->dev;
1427    JCR *jcr = dcr->jcr;
1428
1429    Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
1430          (int)jcr->JobId,
1431          rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
1432          rctx.autochanger_only, rctx.any_drive);
1433
1434    /* Check for max jobs on this Volume */
1435    if (!is_max_jobs_ok(dcr)) {
1436       return 0;
1437    }
1438
1439    /* setting any_drive overrides PreferMountedVols flag */
1440    if (!rctx.any_drive) {
1441       /*
1442        * When PreferMountedVols is set, we keep track of the 
1443        *  drive in use that has the least number of writers, then if
1444        *  no unmounted drive is found, we try that drive. This   
1445        *  helps spread the load to the least used drives.  
1446        */
1447       if (rctx.try_low_use_drive && dev == rctx.low_use_drive) {
1448          Dmsg3(dbglvl, "jid=%u OK dev=%s == low_drive=%s.\n",
1449             jcr->JobId, dev->print_name(), rctx.low_use_drive->print_name());
1450          return 1;
1451       }
1452       /* If he wants a free drive, but this one is busy, no go */
1453       if (!rctx.PreferMountedVols && dev->is_busy()) {
1454          /* Save least used drive */
1455          if ((dev->num_writers + dev->reserved_device) < rctx.num_writers) {
1456             rctx.num_writers = dev->num_writers + dev->reserved_device;
1457             rctx.low_use_drive = dev;
1458             Dmsg3(dbglvl, "jid=%u set low use drive=%s num_writers=%d\n", 
1459                (int)jcr->JobId, dev->print_name(), rctx.num_writers);
1460          } else {
1461             Dmsg2(dbglvl, "jid=%u not low use num_writers=%d\n", 
1462                (int)jcr->JobId, dev->num_writers+dev->reserved_device);
1463          }
1464          Dmsg1(dbglvl, "jid=%u failed: !prefMnt && busy.\n", jcr->JobId);
1465          Mmsg(jcr->errmsg, _("3605 JobId=%u wants free drive but device %s is busy.\n"), 
1466             jcr->JobId, dev->print_name());
1467          queue_reserve_message(jcr);
1468          return 0;
1469       }
1470
1471       /* Check for prefer mounted volumes */
1472       if (rctx.PreferMountedVols && !dev->vol && dev->is_tape()) {
1473          Mmsg(jcr->errmsg, _("3606 JobId=%u prefers mounted drives, but drive %s has no Volume.\n"), 
1474             jcr->JobId, dev->print_name());
1475          queue_reserve_message(jcr);
1476          Dmsg1(dbglvl, "jid=%u failed: want mounted -- no vol\n", (uint32_t)jcr->JobId);
1477          return 0;                 /* No volume mounted */
1478       }
1479
1480       /* Check for exact Volume name match */
1481       /* ***FIXME*** for Disk, we can accept any volume that goes with this
1482        *    drive.
1483        */
1484       if (rctx.exact_match && rctx.have_volume) {
1485          bool ok;
1486          Dmsg6(dbglvl, "jid=%u PrefMnt=%d exact=%d suitable=%d chgronly=%d any=%d\n",
1487                (int)jcr->JobId,
1488                rctx.PreferMountedVols, rctx.exact_match, rctx.suitable_device,
1489                rctx.autochanger_only, rctx.any_drive);
1490          Dmsg5(dbglvl, "jid=%u have_vol=%d have=%s resvol=%s want=%s\n",
1491                   (int)jcr->JobId, rctx.have_volume, dev->VolHdr.VolumeName, 
1492                   dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1493          ok = strcmp(dev->VolHdr.VolumeName, rctx.VolumeName) == 0 ||
1494                  (dev->vol && strcmp(dev->vol->vol_name, rctx.VolumeName) == 0);
1495          if (!ok) {
1496             Mmsg(jcr->errmsg, _("3607 JobId=%u wants Vol=\"%s\" drive has Vol=\"%s\" on drive %s.\n"), 
1497                jcr->JobId, rctx.VolumeName, dev->VolHdr.VolumeName, 
1498                dev->print_name());
1499             queue_reserve_message(jcr);
1500             Dmsg4(dbglvl, "jid=%u not OK: dev have=%s resvol=%s want=%s\n",
1501                   (int)jcr->JobId, dev->VolHdr.VolumeName, 
1502                   dev->vol?dev->vol->vol_name:"*none*", rctx.VolumeName);
1503             return 0;
1504          }
1505          if (is_volume_in_use(dcr)) {
1506             return 0;              /* fail if volume on another drive */
1507          }
1508       }
1509    }
1510
1511    /* Check for unused autochanger drive */
1512    if (rctx.autochanger_only && !dev->is_busy() &&
1513        dev->VolHdr.VolumeName[0] == 0) {
1514       /* Device is available but not yet reserved, reserve it for us */
1515       Dmsg2(dbglvl, "jid=%u OK Res Unused autochanger %s.\n",
1516          jcr->JobId, dev->print_name());
1517       bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1518       bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1519       return 1;                       /* reserve drive */
1520    }
1521
1522    /*
1523     * Handle the case that there are no writers
1524     */
1525    if (dev->num_writers == 0) {
1526       /* Now check if there are any reservations on the drive */
1527       if (dev->reserved_device) {           
1528          return is_pool_ok(dcr);
1529       } else if (dev->can_append()) {
1530          if (is_pool_ok(dcr)) {
1531             return 1; 
1532          } else {
1533             /* Changing pool, unload old tape if any in drive */
1534             Dmsg1(dbglvl, "jid=%u OK dev: num_writers=0, not reserved, pool change, unload changer\n",
1535                 (int)jcr->JobId);
1536             unload_autochanger(dcr, 0);
1537          }
1538       }
1539       /* Device is available but not yet reserved, reserve it for us */
1540       Dmsg2(dbglvl, "jid=%u OK Dev avail reserved %s\n", jcr->JobId, dev->print_name());
1541       bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
1542       bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
1543       return 1;                       /* reserve drive */
1544    }
1545
1546    /*
1547     * Check if the device is in append mode with writers (i.e.
1548     *  available if pool is the same).
1549     */
1550    if (dev->can_append() || dev->num_writers > 0) {
1551       return is_pool_ok(dcr);
1552    } else {
1553       Pmsg1(000, _("Logic error!!!! JobId=%u Should not get here.\n"), (int)jcr->JobId);
1554       Mmsg(jcr->errmsg, _("3910 JobId=%u Logic error!!!! drive %s Should not get here.\n"),
1555             jcr->JobId, dev->print_name());
1556       queue_reserve_message(jcr);
1557       Jmsg0(jcr, M_FATAL, 0, _("Logic error!!!! Should not get here.\n"));
1558       return -1;                      /* error, should not get here */
1559    }
1560    Mmsg(jcr->errmsg, _("3911 JobId=%u failed reserve drive %s.\n"), 
1561          jcr->JobId, dev->print_name());
1562    queue_reserve_message(jcr);
1563    Dmsg2(dbglvl, "jid=%u failed: No reserve %s\n", jcr->JobId, dev->print_name());
1564    return 0;
1565 }
1566
1567
1568
1569
1570 /*
1571  * Queue a reservation error or failure message for this jcr
1572  */
1573 static void queue_reserve_message(JCR *jcr)
1574 {
1575    int i;   
1576    alist *msgs;
1577    char *msg;
1578
1579    jcr->lock();
1580
1581    msgs = jcr->reserve_msgs;
1582    if (!msgs) {
1583       goto bail_out;
1584    }
1585    /*
1586     * Look for duplicate message.  If found, do
1587     * not insert
1588     */
1589    for (i=msgs->size()-1; i >= 0; i--) {
1590       msg = (char *)msgs->get(i);
1591       if (!msg) {
1592          goto bail_out;
1593       }
1594       /* Comparison based on 4 digit message number */
1595       if (strncmp(msg, jcr->errmsg, 4) == 0) {
1596          goto bail_out;
1597       }
1598    }      
1599    /* Message unique, so insert it */
1600    jcr->reserve_msgs->push(bstrdup(jcr->errmsg));
1601
1602 bail_out:
1603    jcr->unlock();
1604 }
1605
1606 /*
1607  * Send any reservation messages queued for this jcr
1608  */
1609 void send_drive_reserve_messages(JCR *jcr, void sendit(const char *msg, int len, void *sarg), void *arg)
1610 {
1611    int i;
1612    alist *msgs;
1613    char *msg;
1614
1615    jcr->lock();
1616    msgs = jcr->reserve_msgs;
1617    if (!msgs || msgs->size() == 0) {
1618       goto bail_out;
1619    }
1620    for (i=msgs->size()-1; i >= 0; i--) {
1621       msg = (char *)msgs->get(i);
1622       if (msg) {
1623          sendit("   ", 3, arg);
1624          sendit(msg, strlen(msg), arg);
1625       } else {
1626          break;
1627       }
1628    }
1629
1630 bail_out:
1631    jcr->unlock();
1632 }
1633
1634 /*
1635  * Pop and release any reservations messages
1636  */
1637 static void pop_reserve_messages(JCR *jcr)
1638 {
1639    alist *msgs;
1640    char *msg;
1641
1642    jcr->lock();
1643    msgs = jcr->reserve_msgs;
1644    if (!msgs) {
1645       goto bail_out;
1646    }
1647    while ((msg = (char *)msgs->pop())) {
1648       free(msg);
1649    }
1650 bail_out:
1651    jcr->unlock();
1652 }
1653
1654 /*
1655  * Also called from acquire.c 
1656  */
1657 void release_reserve_messages(JCR *jcr)
1658 {
1659    pop_reserve_messages(jcr);
1660    jcr->lock();
1661    if (!jcr->reserve_msgs) {
1662       goto bail_out;
1663    }
1664    delete jcr->reserve_msgs;
1665    jcr->reserve_msgs = NULL;
1666
1667 bail_out:
1668    jcr->unlock();
1669 }