]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/askdir.c
- Change Developers to Developer's Guide as requested by Michael.
[bacula/bacula] / bacula / src / stored / askdir.c
1 /*
2  *  Subroutines to handle Catalog reqests sent to the Director
3  *   Reqests/commands from the Director are handled in dircmd.c
4  *
5  *   Kern Sibbald, December 2000
6  *
7  *   Version $Id$
8  */
9 /*
10    Copyright (C) 2000-2005 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"                   /* pull in global headers */
30 #include "stored.h"                   /* pull in Storage Deamon headers */
31
32 /* Requests sent to the Director */
33 static char Find_media[]   = "CatReq Job=%s FindMedia=%d\n";
34 static char Get_Vol_Info[] = "CatReq Job=%s GetVolInfo VolName=%s write=%d\n";
35 static char Update_media[] = "CatReq Job=%s UpdateMedia VolName=%s"
36    " VolJobs=%u VolFiles=%u VolBlocks=%u VolBytes=%s VolMounts=%u"
37    " VolErrors=%u VolWrites=%u MaxVolBytes=%s EndTime=%d VolStatus=%s"
38    " Slot=%d relabel=%d InChanger=%d VolReadTime=%s VolWriteTime=%s"
39    " VolParts=%u\n";
40 static char Create_job_media[] = "CatReq Job=%s CreateJobMedia"
41    " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u"
42    " StartBlock=%u EndBlock=%u\n";
43 static char FileAttributes[] = "UpdCat Job=%s FileAttributes ";
44 static char Job_status[]     = "Status Job=%s JobStatus=%d\n";
45
46
47 /* Responses received from the Director */
48 static char OK_media[] = "1000 OK VolName=%127s VolJobs=%u VolFiles=%u"
49    " VolBlocks=%u VolBytes=%" lld " VolMounts=%u VolErrors=%u VolWrites=%u"
50    " MaxVolBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%20s"
51    " Slot=%d MaxVolJobs=%u MaxVolFiles=%u InChanger=%d"
52    " VolReadTime=%" lld " VolWriteTime=%" lld " EndFile=%u EndBlock=%u"
53    " VolParts=%u LabelType=%d";
54
55
56 static char OK_create[] = "1000 OK CreateJobMedia\n";
57
58 /* Forward referenced functions */
59 static int wait_for_sysop(DCR *dcr);
60
61 /*
62  * Send current JobStatus to Director
63  */
64 bool dir_send_job_status(JCR *jcr)
65 {
66    return bnet_fsend(jcr->dir_bsock, Job_status, jcr->Job, jcr->JobStatus);
67 }
68
69 /*
70  * Common routine for:
71  *   dir_get_volume_info()
72  * and
73  *   dir_find_next_appendable_volume()
74  *
75  *  Returns: true  on success and vol info in dcr->VolCatInfo
76  *           false on failure
77  */
78 static bool do_get_volume_info(DCR *dcr)
79 {
80     JCR *jcr = dcr->jcr;
81     BSOCK *dir = jcr->dir_bsock;
82     VOLUME_CAT_INFO vol;
83     int n;
84     int InChanger;
85
86     dcr->VolumeName[0] = 0;           /* No volume */
87     if (bnet_recv(dir) <= 0) {
88        Dmsg0(200, "getvolname error bnet_recv\n");
89        Mmsg(jcr->errmsg, _("Network error on bnet_recv in req_vol_info.\n"));
90        return false;
91     }
92     memset(&vol, 0, sizeof(vol));
93     Dmsg1(300, "Get vol info=%s", dir->msg);
94     n = sscanf(dir->msg, OK_media, vol.VolCatName,
95                &vol.VolCatJobs, &vol.VolCatFiles,
96                &vol.VolCatBlocks, &vol.VolCatBytes,
97                &vol.VolCatMounts, &vol.VolCatErrors,
98                &vol.VolCatWrites, &vol.VolCatMaxBytes,
99                &vol.VolCatCapacityBytes, vol.VolCatStatus,
100                &vol.Slot, &vol.VolCatMaxJobs, &vol.VolCatMaxFiles,
101                &InChanger, &vol.VolReadTime, &vol.VolWriteTime,
102                &vol.EndFile, &vol.EndBlock, &vol.VolCatParts,
103                &vol.LabelType);
104     if (n != 21) {
105        Dmsg2(100, "Bad response from Dir fields=%d: %s\n", n, dir->msg);
106        Mmsg(jcr->errmsg, _("Error getting Volume info: %s\n"), dir->msg);
107        return false;
108     }
109     vol.InChanger = InChanger;        /* bool in structure */
110     unbash_spaces(vol.VolCatName);
111     bstrncpy(dcr->VolumeName, vol.VolCatName, sizeof(dcr->VolumeName));
112     memcpy(&dcr->VolCatInfo, &vol, sizeof(dcr->VolCatInfo));
113
114     Dmsg2(300, "do_reqest_vol_info got slot=%d Volume=%s\n",
115           vol.Slot, vol.VolCatName);
116     return true;
117 }
118
119
120 /*
121  * Get Volume info for a specific volume from the Director's Database
122  *
123  * Returns: true  on success   (not Director guarantees that Pool and MediaType
124  *                             are correct and VolStatus==Append or
125  *                             VolStatus==Recycle)
126  *          false on failure
127  *
128  *          Volume information returned in jcr
129  */
130 bool dir_get_volume_info(DCR *dcr, enum get_vol_info_rw writing)
131 {
132     JCR *jcr = dcr->jcr;
133     BSOCK *dir = jcr->dir_bsock;
134
135     bstrncpy(dcr->VolCatInfo.VolCatName, dcr->VolumeName, sizeof(dcr->VolCatInfo.VolCatName));
136     Dmsg1(300, "dir_get_volume_info=%s\n", dcr->VolCatInfo.VolCatName);
137     bash_spaces(dcr->VolCatInfo.VolCatName);
138     bnet_fsend(dir, Get_Vol_Info, jcr->Job, dcr->VolCatInfo.VolCatName,
139        writing==GET_VOL_INFO_FOR_WRITE?1:0);
140     return do_get_volume_info(dcr);
141 }
142
143
144
145 /*
146  * Get info on the next appendable volume in the Director's database
147  * Returns: true  on success
148  *          false on failure
149  *
150  *          Volume information returned in dcr
151  *
152  */
153 bool dir_find_next_appendable_volume(DCR *dcr)
154 {
155     JCR *jcr = dcr->jcr;
156     BSOCK *dir = jcr->dir_bsock;
157     JCR *njcr;
158
159     Dmsg0(200, "dir_find_next_appendable_volume\n");
160     /*
161      * Try the three oldest or most available volumes.  Note,
162      *   the most available could already be mounted on another
163      *   drive, so we continue looking for a not in use Volume.
164      */
165     for (int vol_index=1;  vol_index < 3; vol_index++) {
166        bnet_fsend(dir, Find_media, jcr->Job, vol_index);
167        if (do_get_volume_info(dcr)) {
168           Dmsg2(300, "JobId=%d got possible Vol=%s\n", jcr->JobId, dcr->VolumeName);
169           bool found = false;
170           /*
171            * Walk through all jobs and see if the volume is
172            *  already mounted. If so, try a different one.
173            * This would be better done by walking through
174            *  all the devices.
175            */
176           lock_jcr_chain();
177           foreach_jcr(njcr) {
178              if (jcr == njcr) {
179                 free_locked_jcr(njcr);
180                 continue;             /* us */
181              }
182              Dmsg2(300, "Compare to JobId=%d using Vol=%s\n", njcr->JobId, njcr->dcr->VolumeName);
183              if (njcr->dcr && strcmp(dcr->VolumeName, njcr->dcr->VolumeName) == 0) {
184                 found = true;
185                 Dmsg1(400, "Vol in use by JobId=%u\n", njcr->JobId);
186                 free_locked_jcr(njcr);
187                 break;
188              }
189              free_locked_jcr(njcr);
190           }
191           unlock_jcr_chain();
192           if (!found) {
193              Dmsg0(400, "dir_find_next_appendable_volume return true\n");
194              return true;             /* Got good Volume */
195           }
196        } else {
197           Dmsg0(200, "No volume info, return false\n");
198           return false;
199        }
200     }
201     Dmsg0(400, "dir_find_next_appendable_volume return true\n");
202     return true;
203 }
204
205
206 /*
207  * After writing a Volume, send the updated statistics
208  * back to the director. The information comes from the
209  * dev record.
210  */
211 bool dir_update_volume_info(DCR *dcr, bool label)
212 {
213    JCR *jcr = dcr->jcr;
214    BSOCK *dir = jcr->dir_bsock;
215    DEVICE *dev = dcr->dev;
216    time_t LastWritten = time(NULL);
217    char ed1[50], ed2[50], ed3[50], ed4[50];
218    VOLUME_CAT_INFO *vol = &dev->VolCatInfo;
219    int InChanger;
220    POOL_MEM VolumeName;
221
222    if (vol->VolCatName[0] == 0) {
223       Jmsg0(jcr, M_FATAL, 0, _("NULL Volume name. This shouldn't happen!!!\n"));
224       Pmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
225       return false;
226    }
227    if (dev->can_read()) {
228       Jmsg0(jcr, M_FATAL, 0, _("Attempt to update_volume_info in read mode!!!\n"));
229       Pmsg0(000, "Attempt to update_volume_info in read mode!!!\n");
230       return false;
231    }
232
233    Dmsg1(300, "Update cat VolFiles=%d\n", dev->file);
234    /* Just labeled or relabeled the tape */
235    if (label) {
236       bstrncpy(vol->VolCatStatus, "Append", sizeof(vol->VolCatStatus));
237       vol->VolCatBytes = 1;           /* indicates tape labeled */
238    }
239    pm_strcpy(VolumeName, vol->VolCatName);
240    bash_spaces(VolumeName);
241    InChanger = vol->InChanger;
242    bnet_fsend(dir, Update_media, jcr->Job,
243       VolumeName.c_str(), vol->VolCatJobs, vol->VolCatFiles,
244       vol->VolCatBlocks, edit_uint64(vol->VolCatBytes, ed1),
245       vol->VolCatMounts, vol->VolCatErrors,
246       vol->VolCatWrites, edit_uint64(vol->VolCatMaxBytes, ed2),
247       LastWritten, vol->VolCatStatus, vol->Slot, label,
248       InChanger,                      /* bool in structure */
249       edit_uint64(vol->VolReadTime, ed3),
250       edit_uint64(vol->VolWriteTime, ed4),
251       vol->VolCatParts);
252
253    Dmsg1(300, "update_volume_info(): %s", dir->msg);
254
255    if (!do_get_volume_info(dcr)) {
256       Jmsg(jcr, M_FATAL, 0, "%s", jcr->errmsg);
257       Pmsg2(000, "Didn't get vol info vol=%s: ERR=%s", 
258          vol->VolCatName, jcr->errmsg);
259       return false;
260    }
261    Dmsg1(420, "get_volume_info(): %s", dir->msg);
262    /* Update dev Volume info in case something changed (e.g. expired) */
263    memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
264    return true;
265 }
266
267 /*
268  * After writing a Volume, create the JobMedia record.
269  */
270 bool dir_create_jobmedia_record(DCR *dcr)
271 {
272    JCR *jcr = dcr->jcr;
273    BSOCK *dir = jcr->dir_bsock;
274
275    if (!dcr->WroteVol) {
276       return true;                    /* nothing written to tape */
277    }
278
279    dcr->WroteVol = false;
280    bnet_fsend(dir, Create_job_media, jcr->Job,
281       dcr->VolFirstIndex, dcr->VolLastIndex,
282       dcr->StartFile, dcr->EndFile,
283       dcr->StartBlock, dcr->EndBlock);
284    Dmsg1(400, "create_jobmedia(): %s", dir->msg);
285    if (bnet_recv(dir) <= 0) {
286       Dmsg0(190, "create_jobmedia error bnet_recv\n");
287       Jmsg(jcr, M_FATAL, 0, _("Error creating JobMedia record: ERR=%s\n"),
288            bnet_strerror(dir));
289       return false;
290    }
291    Dmsg1(400, "Create_jobmedia: %s", dir->msg);
292    if (strcmp(dir->msg, OK_create) != 0) {
293       Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
294       Jmsg(jcr, M_FATAL, 0, _("Error creating JobMedia record: %s\n"), dir->msg);
295       return false;
296    }
297    return true;
298 }
299
300
301 /*
302  * Update File Attribute data
303  */
304 bool dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec)
305 {
306    JCR *jcr = dcr->jcr;
307    BSOCK *dir = jcr->dir_bsock;
308    ser_declare;
309
310 #ifdef NO_ATTRIBUTES_TEST
311    return true;
312 #endif
313
314    dir->msglen = sprintf(dir->msg, FileAttributes, jcr->Job);
315    dir->msg = check_pool_memory_size(dir->msg, dir->msglen +
316                 sizeof(DEV_RECORD) + rec->data_len);
317    ser_begin(dir->msg + dir->msglen, 0);
318    ser_uint32(rec->VolSessionId);
319    ser_uint32(rec->VolSessionTime);
320    ser_int32(rec->FileIndex);
321    ser_int32(rec->Stream);
322    ser_uint32(rec->data_len);
323    ser_bytes(rec->data, rec->data_len);
324    dir->msglen = ser_length(dir->msg);
325    return bnet_send(dir);
326 }
327
328
329 /*
330  *   Request the sysop to create an appendable volume
331  *
332  *   Entered with device blocked.
333  *   Leaves with device blocked.
334  *
335  *   Returns: true  on success (operator issues a mount command)
336  *            false on failure
337  *              Note, must create dev->errmsg on error return.
338  *
339  *    On success, dcr->VolumeName and dcr->VolCatInfo contain
340  *      information on suggested volume, but this may not be the
341  *      same as what is actually mounted.
342  *
343  *    When we return with success, the correct tape may or may not
344  *      actually be mounted. The calling routine must read it and
345  *      verify the label.
346  */
347 bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr)
348 {
349    int stat = 0, jstat;
350    bool unmounted;
351    bool first = true;
352    DEVICE *dev = dcr->dev;
353    JCR *jcr = dcr->jcr;
354
355    Dmsg0(400, "enter dir_ask_sysop_to_create_appendable_volume\n");
356    ASSERT(dev->dev_blocked);
357    for ( ;; ) {
358       if (job_canceled(jcr)) {
359          Mmsg(dev->errmsg,
360               _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"),
361               jcr->Job, dcr->dev_name);
362          Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
363          return false;
364       }
365       /* First pass, we *know* there are no appendable volumes, so no need to call */
366       if (!first && dir_find_next_appendable_volume(dcr)) { /* get suggested volume */
367          unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
368                      (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
369          /*
370           * If we have a valid volume name and we are not
371           *   removable media, return now, or if we have a
372           *   Slot for an autochanger, otherwise wait
373           *   for the operator to mount the media.
374           */
375          if (!unmounted && ((dcr->VolumeName[0] && !dev_cap(dev, CAP_REM) &&
376                 dev_cap(dev, CAP_LABEL)) ||
377                  (dcr->VolumeName[0] && dcr->VolCatInfo.Slot))) {
378             Dmsg0(400, "Return 1 from mount without wait.\n");
379             return true;
380          }
381          jstat = JS_WaitMount;
382          if (!dev->poll) {
383             Jmsg(jcr, M_MOUNT, 0, _(
384 "Please mount Volume \"%s\" on Storage Device \"%s\" for Job %s\n"
385 "Use \"mount\" command to release Job.\n"),
386               dcr->VolumeName, dcr->dev_name, jcr->Job);
387             Dmsg3(400, "Mount %s on %s for Job %s\n",
388                   dcr->VolumeName, dcr->dev_name, jcr->Job);
389          }
390       } else {
391          jstat = JS_WaitMedia;
392          if (!dev->poll) {
393             Jmsg(jcr, M_MOUNT, 0, _(
394 "Job %s waiting. Cannot find any appendable volumes.\n"
395 "Please use the \"label\"  command to create a new Volume for:\n"
396 "    Storage:      %s\n"
397 "    Media type:   %s\n"
398 "    Pool:         %s\n"),
399                jcr->Job,
400                dcr->dev_name,
401                dcr->media_type,
402                dcr->pool_name);
403          }
404       }
405       first = false;
406
407       jcr->JobStatus = jstat;
408       dir_send_job_status(jcr);
409
410       stat = wait_for_sysop(dcr);
411       if (dev->poll) {
412          Dmsg1(400, "Poll timeout in create append vol on device %s\n", dev_name(dev));
413          continue;
414       }
415
416       if (stat == ETIMEDOUT) {
417          if (!double_dev_wait_time(dev)) {
418             Mmsg(dev->errmsg, _("Max time exceeded waiting to mount Storage Device \"%s\" for Job %s\n"),
419                dev_name(dev), jcr->Job);
420             Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
421             Dmsg1(400, "Gave up waiting on device %s\n", dev_name(dev));
422             return false;             /* exceeded maximum waits */
423          }
424          continue;
425       }
426       if (stat == EINVAL) {
427          berrno be;
428          Mmsg2(dev->errmsg, _("pthread error in mount_next_volume stat=%d ERR=%s\n"),
429                stat, be.strerror(stat));
430          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
431          return false;
432       }
433       if (stat != 0) {
434          berrno be;
435          Jmsg(jcr, M_WARNING, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
436             be.strerror(stat));
437       }
438       Dmsg1(400, "Someone woke me for device %s\n", dev_name(dev));
439
440       /* If no VolumeName, and cannot get one, try again */
441       if (dcr->VolumeName[0] == 0 && !job_canceled(jcr) &&
442           !dir_find_next_appendable_volume(dcr)) {
443          Jmsg(jcr, M_MOUNT, 0, _(
444 "Someone woke me up, but I cannot find any appendable\n"
445 "volumes for Job=%s.\n"), jcr->Job);
446          /* Restart wait counters after user interaction */
447          init_dev_wait_timers(dev);
448          continue;
449       }
450       unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
451                   (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
452       if (unmounted) {
453          continue;                    /* continue to wait */
454       }
455
456       /*
457        * Device mounted, we have a volume, break and return
458        */
459       break;
460    }
461    set_jcr_job_status(jcr, JS_Running);
462    dir_send_job_status(jcr);
463    Dmsg0(400, "leave dir_ask_sysop_to_mount_create_appendable_volume\n");
464    return true;
465 }
466
467 /*
468  *   Request to mount specific Volume
469  *
470  *   Entered with device blocked and dcr->VolumeName is desired
471  *      volume.
472  *   Leaves with device blocked.
473  *
474  *   Returns: true  on success (operator issues a mount command)
475  *            false on failure
476  *                  Note, must create dev->errmsg on error return.
477  *
478  */
479 bool dir_ask_sysop_to_mount_volume(DCR *dcr)
480 {
481    int stat = 0;
482    const char *msg;
483    DEVICE *dev = dcr->dev;
484    JCR *jcr = dcr->jcr;
485
486    Dmsg0(400, "enter dir_ask_sysop_to_mount_volume\n");
487    if (!dcr->VolumeName[0]) {
488       Mmsg0(dev->errmsg, _("Cannot request another volume: no volume name given.\n"));
489       return false;
490    }
491    ASSERT(dev->dev_blocked);
492    for ( ;; ) {
493       if (job_canceled(jcr)) {
494          Mmsg(dev->errmsg, _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"),
495               jcr->Job, dcr->dev_name);
496          return false;
497       }
498
499       if (!dev->poll) {
500          msg = _("Please mount");
501          Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
502               msg, dcr->VolumeName, dcr->dev_name, jcr->Job);
503          Dmsg3(400, "Mount \"%s\" on device \"%s\" for Job %s\n",
504                dcr->VolumeName, dcr->dev_name, jcr->Job);
505       }
506
507       jcr->JobStatus = JS_WaitMount;
508       dir_send_job_status(jcr);
509
510       stat = wait_for_sysop(dcr);    ;     /* wait on device */
511       if (dev->poll) {
512          Dmsg1(400, "Poll timeout in mount vol on device %s\n", dev_name(dev));
513          Dmsg1(400, "Blocked=%s\n", edit_blocked_reason(dev));
514          return true;
515       }
516
517       if (stat == ETIMEDOUT) {
518          if (!double_dev_wait_time(dev)) {
519             Mmsg(dev->errmsg, _("Max time exceeded waiting to mount Storage Device \"%s\" for Job %s\n"),
520                dev_name(dev), jcr->Job);
521             Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
522             Dmsg1(400, "Gave up waiting on device %s\n", dev_name(dev));
523             return false;             /* exceeded maximum waits */
524          }
525          continue;
526       }
527       if (stat == EINVAL) {
528          berrno be;
529          Mmsg2(dev->errmsg, _("pthread error in mount_volume stat=%d ERR=%s\n"),
530                stat, be.strerror(stat));
531          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
532          return false;
533       }
534       if (stat != 0) {
535          berrno be;
536          Jmsg(jcr, M_FATAL, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
537             be.strerror(stat));
538       }
539       Dmsg1(400, "Someone woke me for device %s\n", dev_name(dev));
540       break;
541    }
542    set_jcr_job_status(jcr, JS_Running);
543    dir_send_job_status(jcr);
544    Dmsg0(400, "leave dir_ask_sysop_to_mount_volume\n");
545    return true;
546 }
547
548 /*
549  * Wait for SysOp to mount a tape
550  */
551 static int wait_for_sysop(DCR *dcr)
552 {
553    struct timeval tv;
554    struct timezone tz;
555    struct timespec timeout;
556    time_t last_heartbeat = 0;
557    time_t first_start = time(NULL);
558    int stat = 0;
559    int add_wait;
560    bool unmounted;
561    DEVICE *dev = dcr->dev;
562    JCR *jcr = dcr->jcr;
563
564    P(dev->mutex);
565    unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
566                 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
567
568    dev->poll = false;
569    /*
570     * Wait requested time (dev->rem_wait_sec).  However, we also wake up every
571     *    HB_TIME seconds and send a heartbeat to the FD and the Director
572     *    to keep stateful firewalls from closing them down while waiting
573     *    for the operator.
574     */
575    add_wait = dev->rem_wait_sec;
576    if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
577       add_wait = me->heartbeat_interval;
578    }
579    /* If the user did not unmount the tape and we are polling, ensure
580     *  that we poll at the correct interval.
581     */
582    if (!unmounted && dev->vol_poll_interval && add_wait > dev->vol_poll_interval) {
583       add_wait = dev->vol_poll_interval;
584    }
585    gettimeofday(&tv, &tz);
586    timeout.tv_nsec = tv.tv_usec * 1000;
587    timeout.tv_sec = tv.tv_sec + add_wait;
588
589    if (!unmounted) {
590       dev->dev_prev_blocked = dev->dev_blocked;
591       dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
592    }
593
594    for ( ; !job_canceled(jcr); ) {
595       time_t now, start;
596
597       Dmsg3(400, "I'm going to sleep on device %s. HB=%d wait=%d\n", dev_name(dev),
598          (int)me->heartbeat_interval, dev->wait_sec);
599       start = time(NULL);
600       /* Wait required time */
601       stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
602       Dmsg1(400, "Wokeup from sleep on device stat=%d\n", stat);
603
604       now = time(NULL);
605       dev->rem_wait_sec -= (now - start);
606
607       /* Note, this always triggers the first time. We want that. */
608       if (me->heartbeat_interval) {
609          if (now - last_heartbeat >= me->heartbeat_interval) {
610             /* send heartbeats */
611             if (jcr->file_bsock) {
612                bnet_sig(jcr->file_bsock, BNET_HEARTBEAT);
613                Dmsg0(400, "Send heartbeat to FD.\n");
614             }
615             if (jcr->dir_bsock) {
616                bnet_sig(jcr->dir_bsock, BNET_HEARTBEAT);
617             }
618             last_heartbeat = now;
619          }
620       }
621
622       /*
623        * Check if user unmounted the device while we were waiting
624        */
625       unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
626                    (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
627
628       if (stat != ETIMEDOUT) {     /* we blocked the device */
629          break;                    /* on error return */
630       }
631       if (dev->rem_wait_sec <= 0) {  /* on exceeding wait time return */
632          Dmsg0(400, "Exceed wait time.\n");
633          break;
634       }
635
636       if (!unmounted && dev->vol_poll_interval &&
637           (now - first_start >= dev->vol_poll_interval)) {
638          Dmsg1(400, "In wait blocked=%s\n", edit_blocked_reason(dev));
639          dev->poll = true;            /* returning a poll event */
640          break;
641       }
642       /*
643        * Check if user mounted the device while we were waiting
644        */
645       if (dev->dev_blocked == BST_MOUNT) {   /* mount request ? */
646          stat = 0;
647          break;
648       }
649
650       add_wait = dev->wait_sec - (now - start);
651       if (add_wait < 0) {
652          add_wait = 0;
653       }
654       if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
655          add_wait = me->heartbeat_interval;
656       }
657       gettimeofday(&tv, &tz);
658       timeout.tv_nsec = tv.tv_usec * 1000;
659       timeout.tv_sec = tv.tv_sec + add_wait; /* additional wait */
660       Dmsg1(400, "Additional wait %d sec.\n", add_wait);
661    }
662
663    if (!unmounted) {
664       dev->dev_blocked = dev->dev_prev_blocked;    /* restore entry state */
665    }
666    V(dev->mutex);
667    return stat;
668 }