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