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