]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/askdir.c
Add SD heartbeat
[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-2003 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\n";
39
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[]     = "3012 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\n";
52 static char OK_update[] = "1000 OK UpdateMedia\n";
53
54 /* Forward referenced functions */
55 static int device_wait(JCR *jcr, DEVICE *dev, int wait_sec);
56
57 /*
58  * Send current JobStatus to Director
59  */
60 int dir_send_job_status(JCR *jcr)
61 {
62    return bnet_fsend(jcr->dir_bsock, Job_status, jcr->Job, jcr->JobStatus);
63 }
64
65 /*
66  * Common routine for:
67  *   dir_get_volume_info()
68  * and
69  *   dir_find_next_appendable_volume()
70  */
71 static int do_request_volume_info(JCR *jcr)
72 {
73     BSOCK *dir = jcr->dir_bsock;
74     VOLUME_CAT_INFO *vol = &jcr->VolCatInfo;
75
76     jcr->VolumeName[0] = 0;           /* No volume */
77     if (bnet_recv(dir) <= 0) {
78        Dmsg0(200, "getvolname error bnet_recv\n");
79        Mmsg(&jcr->errmsg, _("Network error on bnet_recv in req_vol_info.\n"));
80        return 0;
81     }
82     if (sscanf(dir->msg, OK_media, vol->VolCatName, 
83                &vol->VolCatJobs, &vol->VolCatFiles,
84                &vol->VolCatBlocks, &vol->VolCatBytes,
85                &vol->VolCatMounts, &vol->VolCatErrors,
86                &vol->VolCatWrites, &vol->VolCatMaxBytes,
87                &vol->VolCatCapacityBytes, vol->VolCatStatus,
88                &vol->Slot, &vol->VolCatMaxJobs, &vol->VolCatMaxFiles) != 14) {
89
90        Dmsg1(200, "Bad response from Dir: %s\n", dir->msg);
91        Mmsg(&jcr->errmsg, _("Error scanning Dir response: %s\n"), dir->msg);
92        return 0;
93     }
94     unbash_spaces(vol->VolCatName);
95     pm_strcpy(&jcr->VolumeName, vol->VolCatName); /* set desired VolumeName */
96     
97     Dmsg2(200, "do_reqest_vol_info got slot=%d Volume=%s\n", 
98           vol->Slot, vol->VolCatName);
99     return 1;
100 }
101
102
103 /*
104  * Get Volume info for a specific volume from the Director's Database
105  *
106  * Returns: 1 on success   (not Director guarantees that Pool and MediaType
107  *                          are correct and VolStatus==Append or
108  *                          VolStatus==Recycle)
109  *          0 on failure
110  *
111  *          Volume information returned in jcr
112  */
113 int dir_get_volume_info(JCR *jcr, int writing)
114 {
115     BSOCK *dir = jcr->dir_bsock;
116
117     strcpy(jcr->VolCatInfo.VolCatName, jcr->VolumeName);
118     Dmsg1(200, "dir_get_volume_info=%s\n", jcr->VolCatInfo.VolCatName);
119     bash_spaces(jcr->VolCatInfo.VolCatName);
120     bnet_fsend(dir, Get_Vol_Info, jcr->Job, jcr->VolCatInfo.VolCatName, writing);
121     return do_request_volume_info(jcr);
122 }
123
124
125
126 /*
127  * Get info on the next appendable volume in the Director's database
128  * Returns: 1 on success
129  *          0 on failure
130  *
131  *          Volume information returned in jcr
132  *
133  */
134 int dir_find_next_appendable_volume(JCR *jcr)
135 {
136     BSOCK *dir = jcr->dir_bsock;
137
138     Dmsg0(200, "dir_find_next_appendable_volume\n");
139     bnet_fsend(dir, Find_media, jcr->Job, 1);
140     return do_request_volume_info(jcr);
141 }
142
143     
144 /*
145  * After writing a Volume, send the updated statistics
146  * back to the director.
147  */
148 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel)
149 {
150    BSOCK *dir = jcr->dir_bsock;
151    time_t EndTime = time(NULL);
152    char ed1[50], ed2[50];
153
154    if (vol->VolCatName[0] == 0) {
155       Jmsg0(jcr, M_ERROR, 0, _("NULL Volume name. This shouldn't happen!!!\n"));
156       return 0;
157    }
158    bash_spaces(vol->VolCatName);
159    bnet_fsend(dir, Update_media, jcr->Job, 
160       vol->VolCatName, vol->VolCatJobs, vol->VolCatFiles,
161       vol->VolCatBlocks, edit_uint64(vol->VolCatBytes, ed1),
162       vol->VolCatMounts, vol->VolCatErrors,
163       vol->VolCatWrites, edit_uint64(vol->VolCatMaxBytes, ed2), 
164       EndTime, vol->VolCatStatus, vol->Slot, relabel);
165    Dmsg1(120, "update_volume_data(): %s", dir->msg);
166    unbash_spaces(vol->VolCatName);
167    if (bnet_recv(dir) <= 0) {
168       Dmsg0(190, "updateVolCatInfo error bnet_recv\n");
169       Jmsg(jcr, M_ERROR, 0, _("Error updating Volume Info: %s\n"), 
170            bnet_strerror(dir));
171       return 0;
172    }
173    Dmsg1(120, "Updatevol: %s", dir->msg);
174    if (strcmp(dir->msg, OK_update) != 0) {
175       Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
176       Jmsg(jcr, M_ERROR, 0, _("Error updating Volume Info: %s\n"), dir->msg);
177       return 0;
178    }
179    return 1;
180 }
181
182 /*
183  * After writing a Volume, create the JobMedia record.
184  */
185 int dir_create_jobmedia_record(JCR *jcr)
186 {
187    BSOCK *dir = jcr->dir_bsock;
188
189    bnet_fsend(dir, Create_job_media, jcr->Job, 
190       jcr->VolFirstFile, jcr->JobFiles,
191       jcr->StartFile, jcr->EndFile,
192       jcr->StartBlock, jcr->EndBlock);
193    Dmsg1(100, "create_jobmedia(): %s", dir->msg);
194    if (bnet_recv(dir) <= 0) {
195       Dmsg0(190, "create_jobmedia error bnet_recv\n");
196       Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: %s\n"), 
197            bnet_strerror(dir));
198       return 0;
199    }
200    Dmsg1(120, "Create_jobmedia: %s", dir->msg);
201    if (strcmp(dir->msg, OK_update) != 0) {
202       Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
203       Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: %s\n"), dir->msg);
204       return 0;
205    }
206    return 1;
207 }
208
209
210 /* 
211  * Update File Attribute data
212  */
213 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec)
214 {
215    BSOCK *dir = jcr->dir_bsock;
216    ser_declare;
217
218    dir->msglen = sprintf(dir->msg, FileAttributes, jcr->Job);
219    dir->msg = check_pool_memory_size(dir->msg, dir->msglen + 
220                 sizeof(DEV_RECORD) + rec->data_len);
221    ser_begin(dir->msg + dir->msglen, 0);
222    ser_uint32(rec->VolSessionId);
223    ser_uint32(rec->VolSessionTime);
224    ser_int32(rec->FileIndex);
225    ser_int32(rec->Stream);
226    ser_uint32(rec->data_len);
227    ser_bytes(rec->data, rec->data_len);
228    dir->msglen = ser_length(dir->msg);
229    return bnet_send(dir);
230 }
231
232
233 /*
234  *   
235  *   Entered with device blocked.
236  *   Leaves with device blocked.
237  *
238  *   Returns: 1 on success (operator issues a mount command)
239  *            0 on failure
240  *              Note, must create dev->errmsg on error return.
241  *
242  *    On success, jcr->VolumeName and jcr->VolCatInfo contain
243  *      information on suggested volume, but this may not be the
244  *      same as what is actually mounted.
245  *
246  *    When we return with success, the correct tape may or may not
247  *      actually be mounted. The calling routine must read it and
248  *      verify the label.
249  */
250 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev)
251 {
252    int stat = 0, jstat;
253    /* ******FIXME******* put these on config variable */
254    int min_wait = 60 * 60;
255    int max_wait = 24 * 60 * 60;
256    int max_num_wait = 9;              /* 5 waits =~ 1 day, then 1 day at a time */
257
258    int wait_sec;
259    int num_wait = 0;
260
261    Dmsg0(130, "enter dir_ask_sysop_to_mount_next_volume\n");
262    ASSERT(dev->dev_blocked);
263    wait_sec = min_wait;
264    for ( ;; ) {
265       if (job_canceled(jcr)) {
266          Mmsg(&dev->errmsg, _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"), 
267               jcr->Job, jcr->dev_name);
268          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
269          return 0;
270       }
271       if (dir_find_next_appendable_volume(jcr)) {    /* get suggested volume */
272          jstat = JS_WaitMount;
273          /*
274           * If we have a valid volume name and we are not
275           * removable media, return now, otherwise wait
276           * for the operator to mount the media.
277           */
278          if (jcr->VolumeName[0] && !dev_cap(dev, CAP_REM) && dev_cap(dev, CAP_LABEL)) {
279             Dmsg0(190, "Return 1 from mount without wait.\n");
280             return 1;
281          }
282          Jmsg(jcr, M_MOUNT, 0, _(
283 "Please mount Volume \"%s\" on Storage Device \"%s\" for Job %s\n"
284 "Use \"mount\" command to release Job.\n"),
285               jcr->VolumeName, jcr->dev_name, jcr->Job);
286          Dmsg3(190, "Mount %s on %s for Job %s\n",
287                 jcr->VolumeName, jcr->dev_name, jcr->Job);
288       } else {
289          jstat = JS_WaitMedia;
290          Jmsg(jcr, M_MOUNT, 0, _(
291 "Job %s waiting. Cannot find any appendable volumes.\n\
292 Please use the \"label\"  command to create a new Volume for:\n\
293     Storage:      %s\n\
294     Media type:   %s\n\
295     Pool:         %s\n"),
296               jcr->Job, 
297               jcr->dev_name, 
298               jcr->media_type,
299               jcr->pool_name);
300       }
301
302       jcr->JobStatus = jstat;
303       dir_send_job_status(jcr);
304
305       stat = device_wait(jcr, dev, wait_sec);
306
307       if (stat == ETIMEDOUT) {
308          wait_sec *= 2;               /* double wait time */
309          if (wait_sec > max_wait) {   /* but not longer than maxtime */
310             wait_sec = max_wait;
311          }
312          num_wait++;
313          if (num_wait >= max_num_wait) {
314             Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"), 
315                  jcr->dev_name, jcr->Job);
316             Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
317             Dmsg1(190, "Gave up waiting on device %s\n", dev->dev_name);
318             return 0;                 /* exceeded maximum waits */
319          }
320          continue;
321       }
322       if (stat == EINVAL) {
323          Mmsg2(&dev->errmsg, _("pthread error in mount_next_volume stat=%d ERR=%s\n"),
324                stat, strerror(stat));
325          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
326          return 0;
327       }
328       if (stat != 0) {
329          Jmsg(jcr, M_WARNING, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
330             strerror(stat));
331       }
332       Dmsg1(190, "Someone woke me for device %s\n", dev->dev_name);
333
334       /* Restart wait counters */
335       wait_sec = min_wait;
336       num_wait = 0;
337       /* If no VolumeName, and cannot get one, try again */
338       if (jcr->VolumeName[0] == 0 && 
339           !dir_find_next_appendable_volume(jcr)) {
340          Jmsg(jcr, M_MOUNT, 0, _(
341 "Someone woke me up, but I cannot find any appendable\n\
342 volumes for Job=%s.\n"), jcr->Job);
343          continue;
344       }       
345       break;
346    }
347    set_jcr_job_status(jcr, JS_Running);
348    dir_send_job_status(jcr);
349    Dmsg0(130, "leave dir_ask_sysop_to_mount_next_volume\n");
350    return 1;
351 }
352
353 /*
354  *   
355  *   Entered with device blocked and jcr->VolumeName is desired
356  *      volume.
357  *   Leaves with device blocked.
358  *
359  *   Returns: 1 on success (operator issues a mount command)
360  *            0 on failure
361  *              Note, must create dev->errmsg on error return.
362  *
363  */
364 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
365 {
366    int stat = 0;
367    /* ******FIXME******* put these on config variable */
368    int min_wait = 60 * 60;
369    int max_wait = 24 * 60 * 60;
370    int max_num_wait = 9;              /* 5 waits =~ 1 day, then 1 day at a time */
371    int wait_sec;
372    int num_wait = 0;
373    char *msg;
374
375    Dmsg0(130, "enter dir_ask_sysop_to_mount_next_volume\n");
376    if (!jcr->VolumeName[0]) {
377       Mmsg0(&dev->errmsg, _("Cannot request another volume: no volume name given.\n"));
378       return 0;
379    }
380    ASSERT(dev->dev_blocked);
381    wait_sec = min_wait;
382    for ( ;; ) {
383       if (job_canceled(jcr)) {
384          Mmsg(&dev->errmsg, _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"), 
385               jcr->Job, jcr->dev_name);
386          return 0;
387       }
388       msg = _("Please mount");
389       Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
390            msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
391       Dmsg3(190, "Mount %s on %s for Job %s\n",
392             jcr->VolumeName, jcr->dev_name, jcr->Job);
393
394       jcr->JobStatus = JS_WaitMount;
395       dir_send_job_status(jcr);
396
397       stat = device_wait(jcr, dev, wait_sec); /* wait on device */
398
399       if (stat == ETIMEDOUT) {
400          wait_sec *= 2;               /* double wait time */
401          if (wait_sec > max_wait) {   /* but not longer than maxtime */
402             wait_sec = max_wait;
403          }
404          num_wait++;
405          if (num_wait >= max_num_wait) {
406             Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"), 
407                  jcr->dev_name, jcr->Job);
408             Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
409             Dmsg1(190, "Gave up waiting on device %s\n", dev->dev_name);
410             return 0;                 /* exceeded maximum waits */
411          }
412          continue;
413       }
414       if (stat == EINVAL) {
415          Mmsg2(&dev->errmsg, _("pthread error in mount_volume stat=%d ERR=%s\n"),
416                stat, strerror(stat));
417          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
418          return 0;
419       }
420       if (stat != 0) {
421          Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
422             strerror(stat));
423       }
424       Dmsg1(190, "Someone woke me for device %s\n", dev->dev_name);
425
426       /* Restart wait counters */
427       wait_sec = min_wait;
428       num_wait = 0;
429       break;
430    }
431    set_jcr_job_status(jcr, JS_Running);
432    dir_send_job_status(jcr);
433    Dmsg0(130, "leave dir_ask_sysop_to_mount_next_volume\n");
434    return 1;
435 }
436
437 #define HB_TIME 20*60   /* send a heatbeat once every 20 minutes while waiting */
438
439 static int device_wait(JCR *jcr, DEVICE *dev, int wait_sec)
440 {
441    struct timeval tv;
442    struct timezone tz;
443    struct timespec timeout;
444    int dev_blocked;
445    time_t start = time(NULL);
446    time_t last_heartbeat = 0;
447    int stat = 0;
448    
449    /*
450     * Wait requested time (wait_sec).  However, we also wake up every
451     *    HB_TIME seconds and send a heartbeat to the FD and the Director
452     *    to keep stateful firewalls from closing them down while waiting
453     *    for the operator.
454     */
455    gettimeofday(&tv, &tz);
456    timeout.tv_nsec = tv.tv_usec * 1000;
457    timeout.tv_sec = tv.tv_sec + (wait_sec > HB_TIME ? HB_TIME: wait_sec);
458
459    P(dev->mutex);
460    dev_blocked = dev->dev_blocked;
461    dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
462
463    for ( ; !job_canceled(jcr); ) {
464       int add_wait;
465
466       Dmsg1(190, "I'm going to sleep on device %s\n", dev->dev_name);
467       stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
468
469       /* Note, this always triggers the first time. We want that. */
470       time_t now = time(NULL);
471       if (now - last_heartbeat >= HB_TIME) {
472          /* send heartbeats */
473          if (jcr->file_bsock) {
474             bnet_sig(jcr->file_bsock, BNET_HEARTBEAT);
475          }
476          if (jcr->dir_bsock) {
477             bnet_sig(jcr->dir_bsock, BNET_HEARTBEAT);
478          }
479          last_heartbeat = now;
480       }
481
482       /* Check if we blocked the device */
483       if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
484          if (stat != ETIMEDOUT) {     /* we blocked the device */
485             break;                    /* on error return */
486          }
487          if (now - start >= wait_sec) {  /* on exceeding wait time return */
488             break;
489          }
490          add_wait = wait_sec - (now - start);
491          if (add_wait > HB_TIME) {
492             add_wait = HB_TIME;
493          }
494       } else {                        /* Oops someone else has it blocked now */
495          add_wait = 10;               /* hang around until he releases it */
496       }
497       /*         
498        * Note, if dev_blocked is not BST_WAITING FOR_SYSOP,
499        *  someone other than us has blocked the device (probably the
500        *  user via the Console program), so we continue waiting
501        *  until he releases the device back to us.
502        */
503       gettimeofday(&tv, &tz);
504       timeout.tv_nsec = tv.tv_usec * 1000;
505       timeout.tv_sec = tv.tv_sec + add_wait; /* additional wait */
506    }
507
508    dev->dev_blocked = dev_blocked;
509    V(dev->mutex);
510    return stat;
511 }