]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/askdir.c
Misc
[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-2004 Kern Sibbald and John Walker
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\n";
39 static char Create_job_media[] = "CatReq Job=%s CreateJobMedia" 
40    " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u" 
41    " StartBlock=%u EndBlock=%u\n";
42 static char FileAttributes[] = "UpdCat Job=%s FileAttributes ";
43 static char Job_status[]     = "3012 Job %s jobstatus %d\n";
44
45
46 /* Responses received from the Director */
47 static char OK_media[] = "1000 OK VolName=%127s VolJobs=%u VolFiles=%u"
48    " VolBlocks=%u VolBytes=%" lld " VolMounts=%u VolErrors=%u VolWrites=%u"
49    " MaxVolBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%20s"
50    " Slot=%d MaxVolJobs=%u MaxVolFiles=%u InChanger=%d"
51    " VolReadTime=%" lld " VolWriteTime=%" lld;
52
53
54 static char OK_create[] = "1000 OK CreateJobMedia\n";
55
56 /* Forward referenced functions */
57 static int wait_for_sysop(JCR *jcr, DEVICE *dev);
58
59 /*
60  * Send current JobStatus to Director
61  */
62 int dir_send_job_status(JCR *jcr)
63 {
64    return bnet_fsend(jcr->dir_bsock, Job_status, jcr->Job, jcr->JobStatus);
65 }
66
67 /*
68  * Common routine for:
69  *   dir_get_volume_info()
70  * and
71  *   dir_find_next_appendable_volume()
72  * 
73  *  Returns: 1 on success and vol info in jcr->VolCatInfo
74  *           0 on failure
75  */
76 static int do_get_volume_info(JCR *jcr)
77 {
78     BSOCK *dir = jcr->dir_bsock;
79     VOLUME_CAT_INFO vol;
80     int n;
81
82     jcr->VolumeName[0] = 0;           /* No volume */
83     if (bnet_recv(dir) <= 0) {
84        Dmsg0(200, "getvolname error bnet_recv\n");
85        Mmsg(&jcr->errmsg, _("Network error on bnet_recv in req_vol_info.\n"));
86        return 0;
87     }
88     memset(&vol, 0, sizeof(vol));
89     Dmsg1(200, "Get vol info=%s\n", dir->msg);
90     n = sscanf(dir->msg, OK_media, vol.VolCatName, 
91                &vol.VolCatJobs, &vol.VolCatFiles,
92                &vol.VolCatBlocks, &vol.VolCatBytes,
93                &vol.VolCatMounts, &vol.VolCatErrors,
94                &vol.VolCatWrites, &vol.VolCatMaxBytes,
95                &vol.VolCatCapacityBytes, vol.VolCatStatus,
96                &vol.Slot, &vol.VolCatMaxJobs, &vol.VolCatMaxFiles,
97                &vol.InChanger, &vol.VolReadTime, &vol.VolWriteTime);
98     if (n != 17) {
99        Dmsg2(100, "Bad response from Dir fields=%d: %s\n", n, dir->msg);
100        Mmsg(&jcr->errmsg, _("Error getting Volume info: %s\n"), dir->msg);
101        return 0;
102     }
103     unbash_spaces(vol.VolCatName);
104     pm_strcpy(&jcr->VolumeName, vol.VolCatName); /* set desired VolumeName */
105     memcpy(&jcr->VolCatInfo, &vol, sizeof(jcr->VolCatInfo));
106     
107     Dmsg2(200, "do_reqest_vol_info got slot=%d Volume=%s\n", 
108           vol.Slot, vol.VolCatName);
109     return 1;
110 }
111
112
113 /*
114  * Get Volume info for a specific volume from the Director's Database
115  *
116  * Returns: 1 on success   (not Director guarantees that Pool and MediaType
117  *                          are correct and VolStatus==Append or
118  *                          VolStatus==Recycle)
119  *          0 on failure
120  *
121  *          Volume information returned in jcr
122  */
123 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing)
124 {
125     BSOCK *dir = jcr->dir_bsock;
126
127     bstrncpy(jcr->VolCatInfo.VolCatName, jcr->VolumeName, sizeof(jcr->VolCatInfo.VolCatName));
128     Dmsg1(200, "dir_get_volume_info=%s\n", jcr->VolCatInfo.VolCatName);
129     bash_spaces(jcr->VolCatInfo.VolCatName);
130     bnet_fsend(dir, Get_Vol_Info, jcr->Job, jcr->VolCatInfo.VolCatName, 
131        writing==GET_VOL_INFO_FOR_WRITE?1:0);
132     return do_get_volume_info(jcr);
133 }
134
135
136
137 /*
138  * Get info on the next appendable volume in the Director's database
139  * Returns: 1 on success
140  *          0 on failure
141  *
142  *          Volume information returned in jcr
143  *
144  */
145 int dir_find_next_appendable_volume(JCR *jcr)
146 {
147     BSOCK *dir = jcr->dir_bsock;
148
149     Dmsg0(200, "dir_find_next_appendable_volume\n");
150     bnet_fsend(dir, Find_media, jcr->Job, 1);
151     return do_get_volume_info(jcr);
152 }
153
154     
155 /*
156  * After writing a Volume, send the updated statistics
157  * back to the director.
158  */
159 int dir_update_volume_info(JCR *jcr, DEVICE *dev, int label)
160 {
161    BSOCK *dir = jcr->dir_bsock;
162    time_t LastWritten = time(NULL);
163    char ed1[50], ed2[50], ed3[50], ed4[50];
164    VOLUME_CAT_INFO *vol = &dev->VolCatInfo;
165
166    if (vol->VolCatName[0] == 0) {
167       Jmsg0(jcr, M_ERROR, 0, _("NULL Volume name. This shouldn't happen!!!\n"));
168       return 0;
169    }
170    if (dev_state(dev, ST_READ)) {
171       Jmsg0(jcr, M_ERROR, 0, _("Attempt to update_volume_info in read mode!!!\n"));
172       return 0;
173    }
174    if (!dev_state(dev, ST_LABEL)) {
175       Jmsg0(jcr, M_ERROR, 0, _("Attempt to update_volume_info on non-labeled Volume!!!\n"));
176       return 0;
177    }
178
179    Dmsg1(100, "Update cat VolFiles=%d\n", dev->file);
180    /* Just labeled or relabeled the tape */
181    if (label) {
182       bstrncpy(vol->VolCatStatus, "Append", sizeof(vol->VolCatStatus));
183       vol->VolCatBytes = 1;           /* indicates tape labeled */
184    }
185    bash_spaces(vol->VolCatName);
186    bnet_fsend(dir, Update_media, jcr->Job, 
187       vol->VolCatName, vol->VolCatJobs, vol->VolCatFiles,
188       vol->VolCatBlocks, edit_uint64(vol->VolCatBytes, ed1),
189       vol->VolCatMounts, vol->VolCatErrors,
190       vol->VolCatWrites, edit_uint64(vol->VolCatMaxBytes, ed2), 
191       LastWritten, vol->VolCatStatus, vol->Slot, label,
192       vol->InChanger,
193       edit_uint64(vol->VolReadTime, ed3), 
194       edit_uint64(vol->VolWriteTime, ed4) );
195
196    Dmsg1(120, "update_volume_info(): %s", dir->msg);
197    unbash_spaces(vol->VolCatName);
198
199    if (!do_get_volume_info(jcr)) {
200       Jmsg(jcr, M_ERROR, 0, "%s", jcr->errmsg);
201       return 0;
202    }
203    Dmsg1(120, "get_volume_info(): %s", dir->msg);
204    /* Update dev Volume info in case something changed (e.g. expired) */
205    memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(dev->VolCatInfo));
206    return 1;
207 }
208
209 /*
210  * After writing a Volume, create the JobMedia record.
211  */
212 int dir_create_jobmedia_record(JCR *jcr)
213 {
214    BSOCK *dir = jcr->dir_bsock;
215
216    if (!jcr->WroteVol) {
217       return 1;                       /* nothing written to tape */
218    }
219
220    jcr->WroteVol = false;
221    bnet_fsend(dir, Create_job_media, jcr->Job, 
222       jcr->VolFirstIndex, jcr->VolLastIndex,
223       jcr->StartFile, jcr->EndFile,
224       jcr->StartBlock, jcr->EndBlock);
225    Dmsg1(100, "create_jobmedia(): %s", dir->msg);
226    if (bnet_recv(dir) <= 0) {
227       Dmsg0(190, "create_jobmedia error bnet_recv\n");
228       Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: ERR=%s\n"), 
229            bnet_strerror(dir));
230       return 0;
231    }
232    Dmsg1(120, "Create_jobmedia: %s", dir->msg);
233    if (strcmp(dir->msg, OK_create) != 0) {
234       Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
235       Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: %s\n"), dir->msg);
236       return 0;
237    }
238    return 1;
239 }
240
241
242 /* 
243  * Update File Attribute data
244  */
245 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec)
246 {
247    BSOCK *dir = jcr->dir_bsock;
248    ser_declare;
249
250    dir->msglen = sprintf(dir->msg, FileAttributes, jcr->Job);
251    dir->msg = check_pool_memory_size(dir->msg, dir->msglen + 
252                 sizeof(DEV_RECORD) + rec->data_len);
253    ser_begin(dir->msg + dir->msglen, 0);
254    ser_uint32(rec->VolSessionId);
255    ser_uint32(rec->VolSessionTime);
256    ser_int32(rec->FileIndex);
257    ser_int32(rec->Stream);
258    ser_uint32(rec->data_len);
259    ser_bytes(rec->data, rec->data_len);
260    dir->msglen = ser_length(dir->msg);
261    return bnet_send(dir);
262 }
263
264
265 /*
266  *   Request the sysop to create an appendable volume
267  *
268  *   Entered with device blocked.
269  *   Leaves with device blocked.
270  *
271  *   Returns: 1 on success (operator issues a mount command)
272  *            0 on failure
273  *              Note, must create dev->errmsg on error return.
274  *
275  *    On success, jcr->VolumeName and jcr->VolCatInfo contain
276  *      information on suggested volume, but this may not be the
277  *      same as what is actually mounted.
278  *
279  *    When we return with success, the correct tape may or may not
280  *      actually be mounted. The calling routine must read it and
281  *      verify the label.
282  */
283 int dir_ask_sysop_to_create_appendable_volume(JCR *jcr, DEVICE *dev)
284 {
285    int stat = 0, jstat;
286    bool unmounted;
287    bool first = true;
288
289    Dmsg0(130, "enter dir_ask_sysop_to_create_appendable_volume\n");
290    ASSERT(dev->dev_blocked);
291    for ( ;; ) {
292       if (job_canceled(jcr)) {
293          Mmsg(&dev->errmsg,
294               _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"), 
295               jcr->Job, jcr->dev_name);
296          Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
297          return 0;
298       }
299       /* First pass, we *know* there are no appendable volumes, so no need to call */
300       if (!first && dir_find_next_appendable_volume(jcr)) { /* get suggested volume */
301          jstat = JS_WaitMount;
302          unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
303                      (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
304          /*
305           * If we have a valid volume name and we are not
306           *   removable media, return now, or if we have a
307           *   Slot for an autochanger, otherwise wait
308           *   for the operator to mount the media.
309           */
310          if (!unmounted && ((jcr->VolumeName[0] && !dev_cap(dev, CAP_REM) && 
311                 dev_cap(dev, CAP_LABEL)) ||
312                  (jcr->VolumeName[0] && jcr->VolCatInfo.Slot))) {
313             Dmsg0(100, "Return 1 from mount without wait.\n");
314             return 1;
315          }
316          if (!dev->poll) {
317             Jmsg(jcr, M_MOUNT, 0, _(
318 "Please mount Volume \"%s\" on Storage Device \"%s\" for Job %s\n"
319 "Use \"mount\" command to release Job.\n"),
320               jcr->VolumeName, jcr->dev_name, jcr->Job);
321             Dmsg3(190, "Mount %s on %s for Job %s\n",
322                   jcr->VolumeName, jcr->dev_name, jcr->Job);
323          }
324       } else {
325          jstat = JS_WaitMedia;
326          if (!dev->poll) {
327             Jmsg(jcr, M_MOUNT, 0, _(
328 "Job %s waiting. Cannot find any appendable volumes.\n\
329 Please use the \"label\"  command to create a new Volume for:\n\
330     Storage:      %s\n\
331     Media type:   %s\n\
332     Pool:         %s\n"),
333                jcr->Job, 
334                jcr->dev_name, 
335                jcr->media_type,
336                jcr->pool_name);
337          }
338       }
339       first = false;
340
341       jcr->JobStatus = jstat;
342       dir_send_job_status(jcr);
343
344       stat = wait_for_sysop(jcr, dev);
345       if (dev->poll) {
346          Dmsg1(200, "Poll timeout in create append vol on device %s\n", dev_name(dev));
347          continue;
348       }
349
350       if (stat == ETIMEDOUT) {
351          if (!double_dev_wait_time(dev)) {
352             Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"), 
353                dev_name(dev), jcr->Job);
354             Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
355             Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
356             return 0;                 /* exceeded maximum waits */
357          }
358          continue;
359       }
360       if (stat == EINVAL) {
361          Mmsg2(&dev->errmsg, _("pthread error in mount_next_volume stat=%d ERR=%s\n"),
362                stat, strerror(stat));
363          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
364          return 0;
365       }
366       if (stat != 0) {
367          Jmsg(jcr, M_WARNING, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
368             strerror(stat));
369       }
370       Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
371
372       /* If no VolumeName, and cannot get one, try again */
373       if (jcr->VolumeName[0] == 0 && !job_canceled(jcr) &&
374           !dir_find_next_appendable_volume(jcr)) {
375          Jmsg(jcr, M_MOUNT, 0, _(
376 "Someone woke me up, but I cannot find any appendable\n\
377 volumes for Job=%s.\n"), jcr->Job);
378          /* Restart wait counters after user interaction */
379          init_dev_wait_timers(dev);
380          continue;
381       }       
382       unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
383                   (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
384       if (unmounted) {
385          continue;                    /* continue to wait */
386       }
387
388       /*
389        * Device mounted, we have a volume, break and return   
390        */
391       break;
392    }
393    set_jcr_job_status(jcr, JS_Running);
394    dir_send_job_status(jcr);
395    Dmsg0(130, "leave dir_ask_sysop_to_mount_create_appendable_volume\n");
396    return 1;
397 }
398
399 /*
400  *   Request to mount specific Volume
401  *
402  *   Entered with device blocked and jcr->VolumeName is desired
403  *      volume.
404  *   Leaves with device blocked.
405  *
406  *   Returns: 1 on success (operator issues a mount command)
407  *            0 on failure
408  *              Note, must create dev->errmsg on error return.
409  *
410  */
411 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
412 {
413    int stat = 0;
414    char *msg;
415
416    Dmsg0(130, "enter dir_ask_sysop_to_mount_volume\n");
417    if (!jcr->VolumeName[0]) {
418       Mmsg0(&dev->errmsg, _("Cannot request another volume: no volume name given.\n"));
419       return 0;
420    }
421    ASSERT(dev->dev_blocked);
422    for ( ;; ) {
423       if (job_canceled(jcr)) {
424          Mmsg(&dev->errmsg, _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"), 
425               jcr->Job, jcr->dev_name);
426          return 0;
427       }
428
429       /*
430        * If we have a valid volume name and we are not
431        *   removable media, return now, or if we have a
432        *   Slot for an autochanger, otherwise wait
433        *   for the operator to mount the media.
434        */
435       if ((jcr->VolumeName[0] && !dev_cap(dev, CAP_REM) && dev_cap(dev, CAP_LABEL)) ||
436           (jcr->VolumeName[0] && jcr->VolCatInfo.Slot)) {
437          Dmsg0(100, "Return 1 from mount without wait.\n");
438          return 1;
439       }
440
441       if (!dev->poll) {
442          msg = _("Please mount");
443          Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
444               msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
445          Dmsg3(190, "Mount %s on %s for Job %s\n",
446                jcr->VolumeName, jcr->dev_name, jcr->Job);
447       }
448
449       jcr->JobStatus = JS_WaitMount;
450       dir_send_job_status(jcr);
451
452       stat = wait_for_sysop(jcr, dev);     /* wait on device */
453       if (dev->poll) {
454          Dmsg1(200, "Poll timeout in mount vol on device %s\n", dev_name(dev));
455          Dmsg1(200, "Blocked=%d\n", dev->dev_blocked);
456          return 1;
457       }
458
459       if (stat == ETIMEDOUT) {
460          if (!double_dev_wait_time(dev)) {
461             Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"), 
462                dev_name(dev), jcr->Job);
463             Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
464             Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
465             return 0;                 /* exceeded maximum waits */
466          }
467          continue;
468       }
469       if (stat == EINVAL) {
470          Mmsg2(&dev->errmsg, _("pthread error in mount_volume stat=%d ERR=%s\n"),
471                stat, strerror(stat));
472          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
473          return 0;
474       }
475       if (stat != 0) {
476          Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
477             strerror(stat));
478       }
479       Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
480       break;
481    }
482    set_jcr_job_status(jcr, JS_Running);
483    dir_send_job_status(jcr);
484    Dmsg0(130, "leave dir_ask_sysop_to_mount_volume\n");
485    return 1;
486 }
487
488 /*
489  * Wait for SysOp to mount a tape
490  */
491 static int wait_for_sysop(JCR *jcr, DEVICE *dev)
492 {
493    struct timeval tv;
494    struct timezone tz;
495    struct timespec timeout;
496    time_t last_heartbeat = 0;
497    time_t first_start = time(NULL);
498    int stat = 0;
499    int add_wait;
500    bool unmounted;
501    
502    P(dev->mutex);
503    unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
504                 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
505
506    dev->poll = false;
507    /*
508     * Wait requested time (dev->rem_wait_sec).  However, we also wake up every
509     *    HB_TIME seconds and send a heartbeat to the FD and the Director
510     *    to keep stateful firewalls from closing them down while waiting
511     *    for the operator.
512     */
513    add_wait = dev->rem_wait_sec;
514    if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
515       add_wait = me->heartbeat_interval;
516    }
517    if (!unmounted && dev->vol_poll_interval && add_wait > dev->vol_poll_interval) {
518       add_wait = dev->vol_poll_interval;
519    }
520    gettimeofday(&tv, &tz);
521    timeout.tv_nsec = tv.tv_usec * 1000;
522    timeout.tv_sec = tv.tv_sec + add_wait;
523
524    if (!unmounted) {
525       dev->dev_prev_blocked = dev->dev_blocked;
526       dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
527    }
528
529    for ( ; !job_canceled(jcr); ) {
530       time_t now, start;
531
532       Dmsg3(100, "I'm going to sleep on device %s. HB=%d wait=%d\n", dev_name(dev),
533          (int)me->heartbeat_interval, dev->wait_sec);
534       start = time(NULL);
535       stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
536       Dmsg1(100, "Wokeup from sleep on device stat=%d\n", stat);
537
538       now = time(NULL);
539       dev->rem_wait_sec -= (now - start);
540
541       /* Note, this always triggers the first time. We want that. */
542       if (me->heartbeat_interval) {
543          if (now - last_heartbeat >= me->heartbeat_interval) {
544             /* send heartbeats */
545             if (jcr->file_bsock) {
546                bnet_sig(jcr->file_bsock, BNET_HEARTBEAT);
547                Dmsg0(100, "Send heartbeat to FD.\n");
548             }
549             if (jcr->dir_bsock) {
550                bnet_sig(jcr->dir_bsock, BNET_HEARTBEAT);
551             }
552             last_heartbeat = now;
553          }
554       }
555
556       /*
557        * Check if user unmounted the device while we were waiting
558        */
559       unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
560                    (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
561
562       if (stat != ETIMEDOUT) {     /* we blocked the device */
563          break;                    /* on error return */
564       }
565       if (dev->rem_wait_sec <= 0) {  /* on exceeding wait time return */
566          Dmsg0(100, "Exceed wait time.\n");
567          break;
568       }
569       
570       if (!unmounted && dev->vol_poll_interval &&       
571           (now - first_start >= dev->vol_poll_interval)) {
572          Dmsg1(200, "In wait blocked=%d\n", dev->dev_blocked);
573          dev->poll = true;
574          break;
575       }
576       /*
577        * Check if user mounted the device while we were waiting
578        */
579       if (dev->dev_blocked == BST_MOUNT) {   /* mount request ? */
580          stat = 0;
581          break;
582       }
583
584       add_wait = dev->wait_sec - (now - start);
585       if (add_wait < 0) {
586          add_wait = 0;
587       }
588       if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
589          add_wait = me->heartbeat_interval;
590       }
591       gettimeofday(&tv, &tz);
592       timeout.tv_nsec = tv.tv_usec * 1000;
593       timeout.tv_sec = tv.tv_sec + add_wait; /* additional wait */
594       Dmsg1(100, "Additional wait %d sec.\n", add_wait);
595    }
596
597    if (!unmounted) {
598       dev->dev_blocked = dev->dev_prev_blocked;    /* restore entry state */
599    }
600    V(dev->mutex);
601    return stat;
602 }