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