]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/acquire.c
64053c7e566f3a83d862f1cb2e3baead5fab55e4
[bacula/bacula] / bacula / src / stored / acquire.c
1 /*
2  *  Routines to acquire and release a device for read/write
3  *
4  *   Kern Sibbald, August MMII
5  *
6  *   Version $Id$
7  */
8 /*
9    Copyright (C) 2002-2005 Kern Sibbald
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2 of
14    the License, or (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public
22    License along with this program; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"                   /* pull in global headers */
29 #include "stored.h"                   /* pull in Storage Deamon headers */
30
31 static int can_reserve_drive(DCR *dcr);
32
33 /*
34  * Create a new Device Control Record and attach
35  *   it to the device (if this is a real job).
36  */
37 DCR *new_dcr(JCR *jcr, DEVICE *dev)
38 {
39    if (jcr && jcr->dcr) {
40       return jcr->dcr;
41    }
42    DCR *dcr = (DCR *)malloc(sizeof(DCR));
43    memset(dcr, 0, sizeof(DCR));
44    if (jcr) {
45       jcr->dcr = dcr;
46    }
47    dcr->jcr = jcr;
48    dcr->dev = dev;
49    if (dev) {
50       dcr->device = dev->device;
51    }
52    dcr->block = new_block(dev);
53    dcr->rec = new_record();
54    dcr->spool_fd = -1;
55    dcr->max_spool_size = dev->device->max_spool_size;
56    /* Attach this dcr only if dev is initialized */
57    if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
58       dev->attached_dcrs->append(dcr);  /* attach dcr to device */
59 //    jcr->dcrs->append(dcr);         /* put dcr in list for Job */
60    }
61    return dcr;
62 }
63
64 /*
65  * Search the dcrs list for the given dcr. If it is found,
66  *  as it should be, then remove it. Also zap the jcr pointer
67  *  to the dcr if it is the same one.
68  */
69 #ifdef needed
70 static void remove_dcr_from_dcrs(DCR *dcr)
71 {
72    JCR *jcr = dcr->jcr;
73    if (jcr->dcrs) {
74       int i = 0;
75       DCR *ldcr;
76       int num = jcr->dcrs->size();
77       for (i=0; i < num; i++) {
78          ldcr = (DCR *)jcr->dcrs->get(i);
79          if (ldcr == dcr) {
80             jcr->dcrs->remove(i);
81             if (jcr->dcr == dcr) {
82                jcr->dcr = NULL;
83             }
84          }
85       }
86    }
87 }
88 #endif
89
90 /*
91  * Free up all aspects of the given dcr -- i.e. dechain it,
92  *  release allocated memory, zap pointers, ...
93  */
94 void free_dcr(DCR *dcr)
95 {
96    JCR *jcr = dcr->jcr;
97    DEVICE *dev = dcr->dev;
98
99    /*
100     * If we reserved the device, we must decrement the
101     *  number of writers.
102     */
103    if (dcr->reserved_device) {
104       lock_device(dev);
105       dev->num_writers--;
106       if (dev->num_writers < 0) {
107          Jmsg1(dcr->jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
108          dev->num_writers = 0;
109          dcr->reserved_device = false;
110       }
111       unlock_device(dev);
112    }
113
114    /* Detach this dcr only if the dev is initialized */
115    if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
116       dev->attached_dcrs->remove(dcr);  /* detach dcr from device */
117 //    remove_dcr_from_dcrs(dcr);      /* remove dcr from jcr list */
118    }
119    if (dcr->block) {
120       free_block(dcr->block);
121    }
122    if (dcr->rec) {
123       free_record(dcr->rec);
124    }
125    if (dcr->jcr) {
126       dcr->jcr->dcr = NULL;
127    }
128    free(dcr);
129 }
130
131
132 /*
133  * We "reserve" the drive by setting the ST_READ bit. No one else
134  *  should touch the drive until that is cleared.
135  *  This allows the DIR to "reserve" the device before actually
136  *  starting the job. If the device is not available, the DIR
137  *  can wait (to be implemented 1/05).
138  */
139 bool reserve_device_for_read(DCR *dcr)
140 {
141    DEVICE *dev = dcr->dev;
142    JCR *jcr = dcr->jcr;
143    bool first;
144
145    ASSERT(dcr);
146
147    dev->block(BST_DOING_ACQUIRE);
148
149    Mmsg(jcr->errmsg, _("Device %s is BLOCKED due to user unmount.\n"),
150         dev->print_name());
151    for (first=true; device_is_unmounted(dev); first=false) {
152       dev->unblock();
153       if (!wait_for_device(dcr, jcr->errmsg, first))  {
154          return false;
155       }
156      dev->block(BST_DOING_ACQUIRE);
157    }
158
159    Mmsg2(jcr->errmsg, _("Device %s is busy. Job %d canceled.\n"),
160          dev->print_name(), jcr->JobId);
161    for (first=true; dev->is_busy(); first=false) {
162       dev->unblock();
163       if (!wait_for_device(dcr, jcr->errmsg, first)) {
164          return false;
165       }
166       dev->block(BST_DOING_ACQUIRE);
167    }
168
169    dev->clear_append();
170    dev->set_read();
171    dev->unblock();
172    return true;
173 }
174
175
176 /*********************************************************************
177  * Acquire device for reading. 
178  *  The drive should have previously been reserved by calling 
179  *  reserve_device_for_read(). We read the Volume label from the block and
180  *  leave the block pointers just after the label.
181  *
182  *  Returns: NULL if failed for any reason
183  *           dcr  if successful
184  */
185 DCR *acquire_device_for_read(DCR *dcr)
186 {
187    DEVICE *dev = dcr->dev;
188    JCR *jcr = dcr->jcr;
189    bool vol_ok = false;
190    bool tape_previously_mounted;
191    bool tape_initially_mounted;
192    VOL_LIST *vol;
193    bool try_autochanger = true;
194    int i;
195    int vol_label_status;
196    
197    dev->block(BST_DOING_ACQUIRE);
198
199    if (dev->num_writers > 0) {
200       Jmsg2(jcr, M_FATAL, 0, _("Num_writers=%d not zero. Job %d canceled.\n"), 
201          dev->num_writers, jcr->JobId);
202       goto get_out;
203    }
204
205    /* Find next Volume, if any */
206    vol = jcr->VolList;
207    if (!vol) {
208       Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
209       goto get_out;
210    }
211    jcr->CurVolume++;
212    for (i=1; i<jcr->CurVolume; i++) {
213       vol = vol->next;
214    }
215    if (!vol) {
216       goto get_out;                   /* should not happen */   
217    }
218    bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
219
220    init_device_wait_timers(dcr);
221
222    tape_previously_mounted = dev->can_read() ||
223                              dev->can_append() ||
224                              dev->is_labeled();
225    tape_initially_mounted = tape_previously_mounted;
226
227
228    /* Volume info is always needed because of VolParts */
229    Dmsg0(200, "dir_get_volume_info\n");
230    if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_READ)) {
231       Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
232    }
233    
234    dev->num_parts = dcr->VolCatInfo.VolCatParts;
235    
236    for (i=0; i<5; i++) {
237       dev->clear_labeled();              /* force reread of label */
238       if (job_canceled(jcr)) {
239          Mmsg1(dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
240          goto get_out;                /* error return */
241       }
242       /*
243        * This code ensures that the device is ready for
244        * reading. If it is a file, it opens it.
245        * If it is a tape, it checks the volume name
246        */
247       for ( ; !dev->is_open(); ) {
248          Dmsg1(120, "bstored: open vol=%s\n", dcr->VolumeName);
249          if (open_dev(dev, dcr->VolumeName, OPEN_READ_ONLY) < 0) {
250             if (dev->dev_errno == EIO) {   /* no tape loaded */
251                goto default_path;
252             }
253             
254             /* If we have a dvd that requires mount, 
255              * we need to try to open the label, so the info can be reported
256              * if a wrong volume has been mounted. */
257             if (dev->is_dvd() && (dcr->VolCatInfo.VolCatParts > 0)) {
258                break;
259             }
260             
261             Jmsg(jcr, M_FATAL, 0, _("Open device %s Volume \"%s\" failed: ERR=%s\n"),
262                 dev->print_name(), dcr->VolumeName, strerror_dev(dev));
263             goto get_out;
264          }
265          Dmsg1(129, "open_dev %s OK\n", dev->print_name());
266       }
267       
268       if (dev->is_dvd()) {
269          vol_label_status = read_dev_volume_label_guess(dcr, 0);
270       } else {
271          vol_label_status = read_dev_volume_label(dcr);
272       }
273       
274       Dmsg0(200, "calling read-vol-label\n");
275       switch (vol_label_status) {
276       case VOL_OK:
277          vol_ok = true;
278          memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
279          break;                    /* got it */
280       case VOL_IO_ERROR:
281          /*
282           * Send error message generated by read_dev_volume_label()
283           *  only we really had a tape mounted. This supresses superfluous
284           *  error messages when nothing is mounted.
285           */
286          if (tape_previously_mounted) {
287             Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
288          }
289          goto default_path;
290       case VOL_NAME_ERROR:
291          if (tape_initially_mounted) {
292             tape_initially_mounted = false;
293             goto default_path;
294          }
295          /* Fall through */
296       default:
297          Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
298 default_path:
299          tape_previously_mounted = true;
300          
301          /* If the device requires mount, close it, so the device can be ejected.
302           * FIXME: This should perhaps be done for all devices. */
303          if (dev_cap(dev, CAP_REQMOUNT)) {
304             force_close_dev(dev);
305          }
306          
307          /* Call autochanger only once unless ask_sysop called */
308          if (try_autochanger) {
309             int stat;
310             Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
311                dcr->VolumeName, dcr->VolCatInfo.Slot);
312             stat = autoload_device(dcr, 0, NULL);
313             if (stat > 0) {
314                try_autochanger = false;
315                continue;              /* try reading volume mounted */
316             }
317          }
318          
319          /* Mount a specific volume and no other */
320          Dmsg0(200, "calling dir_ask_sysop\n");
321          if (!dir_ask_sysop_to_mount_volume(dcr)) {
322             goto get_out;             /* error return */
323          }
324          try_autochanger = true;      /* permit using autochanger again */
325          continue;                    /* try reading again */
326       } /* end switch */
327       break;
328    } /* end for loop */
329    if (!vol_ok) {
330       Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device %s.\n"),
331             dev->print_name());
332       goto get_out;
333    }
334
335    dev->clear_append();
336    dev->set_read();
337    set_jcr_job_status(jcr, JS_Running);
338    dir_send_job_status(jcr);
339    Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
340       dcr->VolumeName, dev->print_name());
341
342 get_out:
343    dev->unblock();
344    if (!vol_ok) {
345       free_dcr(dcr);
346       dcr = NULL;
347    }
348    return dcr;
349 }
350
351 /*
352  * We reserve the device for appending by incrementing the 
353  *  reserved_device. We do virtually all the same work that
354  *  is done in acquire_device_for_append(), but we do
355  *  not attempt to mount the device. This routine allows
356  *  the DIR to reserve multiple devices before *really* 
357  *  starting the job. It also permits the SD to refuse 
358  *  certain devices (not up, ...).
359  *
360  * Note, in reserving a device, if the device is for the
361  *  same pool and the same pool type, then it is acceptable.
362  *  The Media Type has already been checked. If we are
363  *  the first tor reserve the device, we put the pool
364  *  name and pool type in the device record.
365  */
366 bool reserve_device_for_append(DCR *dcr)
367 {
368    JCR *jcr = dcr->jcr;
369    DEVICE *dev = dcr->dev;
370    bool ok = false;
371    bool first;
372
373    ASSERT(dcr);
374    dev->block(BST_DOING_ACQUIRE);
375
376    Mmsg2(jcr->errmsg, _("Device %s is busy reading. Job %d canceled.\n"),
377          dev->print_name(), jcr->JobId);
378    for (first=true; dev->can_read(); first=false) {
379       dev->unblock();
380       if (!wait_for_device(dcr, jcr->errmsg, first)) {
381          return false;
382       }
383       dev->block(BST_DOING_ACQUIRE);
384    }
385
386
387    Mmsg(jcr->errmsg, _("Device %s is BLOCKED due to user unmount.\n"),
388         dev->print_name());
389    for (first=true; device_is_unmounted(dev); first=false) {
390       dev->unblock();
391       if (!wait_for_device(dcr, jcr->errmsg, first))  {
392          return false;
393       }
394      dev->block(BST_DOING_ACQUIRE);
395    }
396
397    Dmsg1(190, "reserve_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
398
399    for ( ;; ) {
400       switch (can_reserve_drive(dcr)) {
401       case 0:
402          /* ****FIXME**** Make wait */
403          goto bail_out;
404       case -1:
405          goto bail_out;               /* error */
406       default:
407          break;                       /* OK, reserve drive */
408       }
409       break;
410    }
411
412
413    dev->reserved_device++;
414    dcr->reserved_device = true;
415    ok = true;
416
417 bail_out:
418    dev->unblock();
419    return ok;
420 }
421
422 /*
423  * Returns: 1 if drive can be reserved
424  *          0 if we should wait
425  *         -1 on error
426  */
427 static int can_reserve_drive(DCR *dcr) 
428 {
429    DEVICE *dev = dcr->dev;
430    JCR *jcr = dcr->jcr;
431    /*
432     * First handle the case that the drive is not yet in append mode
433     */
434    if (!dev->can_append() && dev->num_writers == 0) {
435       /* Now check if there are any reservations on the drive */
436       if (dev->reserved_device) {           
437          /* Yes, now check if we want the same Pool and pool type */
438          if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
439              strcmp(dev->pool_type, dcr->pool_type) == 0) {
440             /* OK, compatible device */
441          } else {
442             /* Drive not suitable for us */
443             Jmsg(jcr, M_WARNING, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
444             return 0;                 /* wait */
445          }
446       } else {
447          /* Device is available but not yet reserved, reserve it for us */
448          bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
449          bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
450          dev->PoolId = dcr->PoolId;
451       }
452       return 1;                       /* reserve drive */
453    }
454
455    /*
456     * Now check if the device is in append mode 
457     */
458    if (dev->can_append() || dev->num_writers > 0) {
459       Dmsg0(190, "device already in append.\n");
460       /* Yes, now check if we want the same Pool and pool type */
461       if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
462           strcmp(dev->pool_type, dcr->pool_type) == 0) {
463          /* OK, compatible device */
464       } else {
465          /* Drive not suitable for us */
466          Jmsg(jcr, M_WARNING, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
467          return 0;                    /* wait */
468       }
469    } else {
470       Pmsg0(000, "Logic error!!!! Should not get here.\n");
471       Jmsg0(jcr, M_FATAL, 0, _("Logic error!!!! Should not get here.\n"));
472       return -1;                      /* error, should not get here */
473    }
474    return 1;                          /* reserve drive */
475 }
476
477 /*
478  * Acquire device for writing. We permit multiple writers.
479  *  If this is the first one, we read the label.
480  *
481  *  Returns: NULL if failed for any reason
482  *           dcr if successful.
483  *   Note, normally reserve_device_for_append() is called
484  *   before this routine.
485  */
486 DCR *acquire_device_for_append(DCR *dcr)
487 {
488    bool release = false;
489    bool recycle = false;
490    bool do_mount = false;
491    DEVICE *dev = dcr->dev;
492    JCR *jcr = dcr->jcr;
493
494    dev->block(BST_DOING_ACQUIRE);
495    Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
496
497    if (dcr->reserved_device) {
498       dev->reserved_device--;
499       dcr->reserved_device = false;
500    }
501
502    /*
503     * With the reservation system, this should not happen
504     */
505    if (dev->can_read()) {
506       Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev->print_name());
507       goto get_out;
508    }
509
510    if (dev->can_append()) {
511       Dmsg0(190, "device already in append.\n");
512       /*
513        * Device already in append mode
514        *
515        * Check if we have the right Volume mounted
516        *   OK if current volume info OK
517        *   OK if next volume matches current volume
518        *   otherwise mount desired volume obtained from
519        *    dir_find_next_appendable_volume
520        */
521       bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
522       if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE) &&
523           !(dir_find_next_appendable_volume(dcr) &&
524             strcmp(dev->VolHdr.VolName, dcr->VolumeName) == 0)) { /* wrong tape mounted */
525          Dmsg0(190, "Wrong tape mounted.\n");
526          if (dev->num_writers != 0 || dev->reserved_device) {
527             Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
528             goto get_out;
529          }
530          /* Wrong tape mounted, release it, then fall through to get correct one */
531          Dmsg0(190, "Wrong tape mounted, release and try mount.\n");
532          release = true;
533          do_mount = true;
534       } else {
535          /*
536           * At this point, the correct tape is already mounted, so
537           *   we do not need to do mount_next_write_volume(), unless
538           *   we need to recycle the tape.
539           */
540           recycle = strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
541           Dmsg1(190, "Correct tape mounted. recycle=%d\n", recycle);
542           if (recycle && dev->num_writers != 0) {
543              Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
544                   " because it is in use by another job.\n"));
545              goto get_out;
546           }
547           if (dev->num_writers == 0) {
548              memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
549           }
550        }
551    } else {
552       /* Not already in append mode, so mount the device */
553       Dmsg0(190, "Not in append mode, try mount.\n");
554       ASSERT(dev->num_writers == 0);
555       do_mount = true;
556    }
557
558    if (do_mount || recycle) {
559       Dmsg0(190, "Do mount_next_write_vol\n");
560       bool mounted = mount_next_write_volume(dcr, release);
561       if (!mounted) {
562          if (!job_canceled(jcr)) {
563             /* Reduce "noise" -- don't print if job canceled */
564             Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
565                dev->print_name());
566          }
567          goto get_out;
568       }
569    }
570
571    dev->num_writers++;                /* we are now a writer */
572    if (jcr->NumVolumes == 0) {
573       jcr->NumVolumes = 1;
574    }
575    goto ok_out;
576
577 /*
578  * If we jump here, it is an error return because
579  *  rtn_dev will still be NULL
580  */
581 get_out:
582    free_dcr(dcr);
583    dcr = NULL;
584 ok_out:
585    dev->unblock();
586    return dcr;
587 }
588
589 /*
590  * This job is done, so release the device. From a Unix standpoint,
591  *  the device remains open.
592  *
593  */
594 bool release_device(DCR *dcr)
595 {
596    bool ok = true;
597    JCR *jcr = dcr->jcr;
598    DEVICE *dev = dcr->dev;
599
600    lock_device(dev);
601    Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
602
603    /* if device is reserved, job never started, so release the reserve here */
604    if (dcr->reserved_device) {
605       dev->reserved_device--;
606       dcr->reserved_device = false;
607    }
608
609    if (dev->can_read()) {
610       dev->clear_read();              /* clear read bit */
611       /* Close if file or !CAP_ALWAYSOPEN */
612       if (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN)) {
613          offline_or_rewind_dev(dev);
614          close_dev(dev);
615       }
616       /******FIXME**** send read volume usage statistics to director */
617
618    } else if (dev->num_writers > 0) {
619       dev->num_writers--;
620       Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
621       if (dev->is_labeled()) {
622          Dmsg0(100, "dir_create_jobmedia_record. Release\n");
623          if (!dir_create_jobmedia_record(dcr)) {
624             Jmsg(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
625                dcr->VolCatInfo.VolCatName, jcr->Job);
626             ok = false;
627          }
628          /* If no more writers, write an EOF */
629          if (!dev->num_writers && dev_can_write(dev)) {
630             weof_dev(dev, 1);
631             write_ansi_ibm_labels(dcr, ANSI_EOF_LABEL, dev->VolHdr.VolName);
632          }
633          dev->VolCatInfo.VolCatFiles = dev->file;   /* set number of files */
634          dev->VolCatInfo.VolCatJobs++;              /* increment number of jobs */
635          /* Note! do volume update before close, which zaps VolCatInfo */
636          Dmsg0(100, "dir_update_vol_info. Release0\n");
637          dir_update_volume_info(dcr, false); /* send Volume info to Director */
638          Dmsg0(100, "==== write ansi eof label \n");
639       }
640
641       /* If no writers, close if file or !CAP_ALWAYS_OPEN */
642       if (dev->num_writers == 0 && (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN))) {
643          offline_or_rewind_dev(dev);
644          close_dev(dev);
645       }
646    } else {
647       Jmsg2(jcr, M_FATAL, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
648             dev->print_name(), NPRT(dcr->VolumeName));
649       Jmsg2(jcr, M_ERROR, 0, _("num_writers=%d state=%x\n"), dev->num_writers, dev->state);
650       ok = false;
651    }
652
653    /* Fire off Alert command and include any output */
654    if (!job_canceled(jcr) && dcr->device->alert_command) {
655       POOLMEM *alert;
656       int status = 1;
657       BPIPE *bpipe;
658       char line[MAXSTRING];
659       alert = get_pool_memory(PM_FNAME);
660       alert = edit_device_codes(dcr, alert, "");
661       bpipe = open_bpipe(alert, 0, "r");
662       if (bpipe) {
663          while (fgets(line, sizeof(line), bpipe->rfd)) {
664             Jmsg(jcr, M_ALERT, 0, _("Alert: %s"), line);
665          }
666          status = close_bpipe(bpipe);
667       } else {
668          status = errno;
669       }
670       if (status != 0) {
671          berrno be;
672          Jmsg(jcr, M_ALERT, 0, _("3997 Bad alert command: %s: ERR=%s.\n"),
673               alert, be.strerror(status));
674       }
675
676       Dmsg1(400, "alert status=%d\n", status);
677       free_pool_memory(alert);
678    }
679    unlock_device(dev);
680    free_dcr(dcr);
681    jcr->dcr = NULL;
682    pthread_cond_broadcast(&wait_device_release);
683    return ok;
684 }