]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/askdir.c
Make mark run *MUCH* faster in restore tree + update copyright on changed files
[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 /*
267  *   Request the sysop to create an appendable volume
268  *
269  *   Entered with device blocked.
270  *   Leaves with device blocked.
271  *
272  *   Returns: 1 on success (operator issues a mount command)
273  *            0 on failure
274  *              Note, must create dev->errmsg on error return.
275  *
276  *    On success, jcr->VolumeName and jcr->VolCatInfo contain
277  *      information on suggested volume, but this may not be the
278  *      same as what is actually mounted.
279  *
280  *    When we return with success, the correct tape may or may not
281  *      actually be mounted. The calling routine must read it and
282  *      verify the label.
283  */
284 int dir_ask_sysop_to_create_appendable_volume(JCR *jcr, DEVICE *dev)
285 {
286    int stat = 0, jstat;
287    bool unmounted;
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       if (dir_find_next_appendable_volume(jcr)) {    /* get suggested volume */
300          jstat = JS_WaitMount;
301          unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
302                      (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
303          /*
304           * If we have a valid volume name and we are not
305           *   removable media, return now, or if we have a
306           *   Slot for an autochanger, otherwise wait
307           *   for the operator to mount the media.
308           */
309          if (!unmounted && ((jcr->VolumeName[0] && !dev_cap(dev, CAP_REM) && 
310                 dev_cap(dev, CAP_LABEL)) ||
311                  (jcr->VolumeName[0] && jcr->VolCatInfo.Slot))) {
312             Dmsg0(100, "Return 1 from mount without wait.\n");
313             return 1;
314          }
315          Jmsg(jcr, M_MOUNT, 0, _(
316 "Please mount Volume \"%s\" on Storage Device \"%s\" for Job %s\n"
317 "Use \"mount\" command to release Job.\n"),
318               jcr->VolumeName, jcr->dev_name, jcr->Job);
319          Dmsg3(190, "Mount %s on %s for Job %s\n",
320                 jcr->VolumeName, jcr->dev_name, jcr->Job);
321       } else {
322          jstat = JS_WaitMedia;
323          if (!dev->poll) {
324             Jmsg(jcr, M_MOUNT, 0, _(
325 "Job %s waiting. Cannot find any appendable volumes.\n\
326 Please use the \"label\"  command to create a new Volume for:\n\
327     Storage:      %s\n\
328     Media type:   %s\n\
329     Pool:         %s\n"),
330                jcr->Job, 
331                jcr->dev_name, 
332                jcr->media_type,
333                jcr->pool_name);
334          }
335       }
336
337       jcr->JobStatus = jstat;
338       dir_send_job_status(jcr);
339
340       stat = wait_for_sysop(jcr, dev);
341       if (dev->poll) {
342          Dmsg1(200, "Poll timeout in create append vol on device %s\n", dev_name(dev));
343          continue;
344       }
345
346       if (stat == ETIMEDOUT) {
347          if (!double_dev_wait_time(dev)) {
348             Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"), 
349                dev_name(dev), jcr->Job);
350             Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
351             Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
352             return 0;                 /* exceeded maximum waits */
353          }
354          continue;
355       }
356       if (stat == EINVAL) {
357          Mmsg2(&dev->errmsg, _("pthread error in mount_next_volume stat=%d ERR=%s\n"),
358                stat, strerror(stat));
359          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
360          return 0;
361       }
362       if (stat != 0) {
363          Jmsg(jcr, M_WARNING, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
364             strerror(stat));
365       }
366       Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
367
368       /* If no VolumeName, and cannot get one, try again */
369       if (jcr->VolumeName[0] == 0 && !job_canceled(jcr) &&
370           !dir_find_next_appendable_volume(jcr)) {
371          Jmsg(jcr, M_MOUNT, 0, _(
372 "Someone woke me up, but I cannot find any appendable\n\
373 volumes for Job=%s.\n"), jcr->Job);
374          /* Restart wait counters after user interaction */
375          init_dev_wait_timers(dev);
376          continue;
377       }       
378       unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
379                   (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
380       if (unmounted) {
381          continue;                    /* continue to wait */
382       }
383
384       /*
385        * Device mounted, we have a volume, break and return   
386        */
387       break;
388    }
389    set_jcr_job_status(jcr, JS_Running);
390    dir_send_job_status(jcr);
391    Dmsg0(130, "leave dir_ask_sysop_to_mount_create_appendable_volume\n");
392    return 1;
393 }
394
395 /*
396  *   Request to mount specific Volume
397  *
398  *   Entered with device blocked and jcr->VolumeName is desired
399  *      volume.
400  *   Leaves with device blocked.
401  *
402  *   Returns: 1 on success (operator issues a mount command)
403  *            0 on failure
404  *              Note, must create dev->errmsg on error return.
405  *
406  */
407 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
408 {
409    int stat = 0;
410    char *msg;
411
412    Dmsg0(130, "enter dir_ask_sysop_to_mount_volume\n");
413    if (!jcr->VolumeName[0]) {
414       Mmsg0(&dev->errmsg, _("Cannot request another volume: no volume name given.\n"));
415       return 0;
416    }
417    ASSERT(dev->dev_blocked);
418    for ( ;; ) {
419       if (job_canceled(jcr)) {
420          Mmsg(&dev->errmsg, _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"), 
421               jcr->Job, jcr->dev_name);
422          return 0;
423       }
424
425       /*
426        * If we have a valid volume name and we are not
427        *   removable media, return now, or if we have a
428        *   Slot for an autochanger, otherwise wait
429        *   for the operator to mount the media.
430        */
431       if ((jcr->VolumeName[0] && !dev_cap(dev, CAP_REM) && dev_cap(dev, CAP_LABEL)) ||
432           (jcr->VolumeName[0] && jcr->VolCatInfo.Slot)) {
433          Dmsg0(100, "Return 1 from mount without wait.\n");
434          return 1;
435       }
436
437       if (!dev->poll) {
438          msg = _("Please mount");
439          Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
440               msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
441          Dmsg3(190, "Mount %s on %s for Job %s\n",
442                jcr->VolumeName, jcr->dev_name, jcr->Job);
443       }
444
445       jcr->JobStatus = JS_WaitMount;
446       dir_send_job_status(jcr);
447
448       stat = wait_for_sysop(jcr, dev);     /* wait on device */
449       if (dev->poll) {
450          Dmsg1(200, "Poll timeout in mount vol on device %s\n", dev_name(dev));
451          Dmsg1(200, "Blocked=%d\n", dev->dev_blocked);
452          return 1;
453       }
454
455       if (stat == ETIMEDOUT) {
456          if (!double_dev_wait_time(dev)) {
457             Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"), 
458                dev_name(dev), jcr->Job);
459             Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
460             Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
461             return 0;                 /* exceeded maximum waits */
462          }
463          continue;
464       }
465       if (stat == EINVAL) {
466          Mmsg2(&dev->errmsg, _("pthread error in mount_volume stat=%d ERR=%s\n"),
467                stat, strerror(stat));
468          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
469          return 0;
470       }
471       if (stat != 0) {
472          Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
473             strerror(stat));
474       }
475       Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
476       break;
477    }
478    set_jcr_job_status(jcr, JS_Running);
479    dir_send_job_status(jcr);
480    Dmsg0(130, "leave dir_ask_sysop_to_mount_volume\n");
481    return 1;
482 }
483
484 /*
485  * Wait for SysOp to mount a tape
486  */
487 static int wait_for_sysop(JCR *jcr, DEVICE *dev)
488 {
489    struct timeval tv;
490    struct timezone tz;
491    struct timespec timeout;
492    time_t last_heartbeat = 0;
493    time_t first_start = time(NULL);
494    int stat = 0;
495    int add_wait;
496    bool unmounted;
497    
498    P(dev->mutex);
499    unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
500                 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
501
502    dev->poll = false;
503    /*
504     * Wait requested time (dev->rem_wait_sec).  However, we also wake up every
505     *    HB_TIME seconds and send a heartbeat to the FD and the Director
506     *    to keep stateful firewalls from closing them down while waiting
507     *    for the operator.
508     */
509    add_wait = dev->rem_wait_sec;
510    if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
511       add_wait = me->heartbeat_interval;
512    }
513    if (!unmounted && dev->vol_poll_interval && add_wait > dev->vol_poll_interval) {
514       add_wait = dev->vol_poll_interval;
515    }
516    gettimeofday(&tv, &tz);
517    timeout.tv_nsec = tv.tv_usec * 1000;
518    timeout.tv_sec = tv.tv_sec + add_wait;
519
520    if (!unmounted) {
521       dev->dev_prev_blocked = dev->dev_blocked;
522       dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
523    }
524
525    for ( ; !job_canceled(jcr); ) {
526       time_t now, start;
527
528       Dmsg3(100, "I'm going to sleep on device %s. HB=%d wait=%d\n", dev_name(dev),
529          (int)me->heartbeat_interval, dev->wait_sec);
530       start = time(NULL);
531       stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
532       Dmsg1(100, "Wokeup from sleep on device stat=%d\n", stat);
533
534       now = time(NULL);
535       dev->rem_wait_sec -= (now - start);
536
537       /* Note, this always triggers the first time. We want that. */
538       if (me->heartbeat_interval) {
539          if (now - last_heartbeat >= me->heartbeat_interval) {
540             /* send heartbeats */
541             if (jcr->file_bsock) {
542                bnet_sig(jcr->file_bsock, BNET_HEARTBEAT);
543                Dmsg0(100, "Send heartbeat to FD.\n");
544             }
545             if (jcr->dir_bsock) {
546                bnet_sig(jcr->dir_bsock, BNET_HEARTBEAT);
547             }
548             last_heartbeat = now;
549          }
550       }
551
552       /*
553        * Check if user unmounted the device while we were waiting
554        */
555       unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
556                    (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
557
558       if (stat != ETIMEDOUT) {     /* we blocked the device */
559          break;                    /* on error return */
560       }
561       if (dev->rem_wait_sec <= 0) {  /* on exceeding wait time return */
562          Dmsg0(100, "Exceed wait time.\n");
563          break;
564       }
565       
566       if (!unmounted && dev->vol_poll_interval &&       
567           (now - first_start >= dev->vol_poll_interval)) {
568          Dmsg1(200, "In wait blocked=%d\n", dev->dev_blocked);
569          dev->poll = true;
570          break;
571       }
572       /*
573        * Check if user mounted the device while we were waiting
574        */
575       if (dev->dev_blocked == BST_MOUNT) {   /* mount request ? */
576          stat = 0;
577          break;
578       }
579
580       add_wait = dev->wait_sec - (now - start);
581       if (add_wait < 0) {
582          add_wait = 0;
583       }
584       if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
585          add_wait = me->heartbeat_interval;
586       }
587       gettimeofday(&tv, &tz);
588       timeout.tv_nsec = tv.tv_usec * 1000;
589       timeout.tv_sec = tv.tv_sec + add_wait; /* additional wait */
590       Dmsg1(100, "Additional wait %d sec.\n", add_wait);
591    }
592
593    if (!unmounted) {
594       dev->dev_blocked = dev->dev_prev_blocked;    /* restore entry state */
595    }
596    V(dev->mutex);
597    return stat;
598 }