]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/askdir.c
Make Recycle work -- kes25May02
[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, 2001, 2002 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\n";
35
36 static char Update_media[] = "CatReq Job=%s UpdateMedia VolName=%s\
37  VolJobs=%d VolFiles=%d VolBlocks=%d VolBytes=%" lld " VolMounts=%d\
38  VolErrors=%d VolWrites=%d VolMaxBytes=%" lld " EndTime=%d VolStatus=%s\
39  FirstIndex=%d LastIndex=%d StartFile=%d EndFile=%d \
40  StartBlock=%d EndBlock=%d relabel=%d\n";
41
42 static char FileAttributes[] = "UpdCat Job=%s FileAttributes ";
43
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=%d VolFiles=%d\
49  VolBlocks=%d VolBytes=%" lld " VolMounts=%d VolErrors=%d VolWrites=%d\
50  VolMaxBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%20s\n";
51
52 static char OK_update[] = "1000 OK UpdateMedia\n";
53
54
55 /*
56  * Send current JobStatus to Director
57  */
58 int dir_send_job_status(JCR *jcr)
59 {
60    return bnet_fsend(jcr->dir_bsock, Job_status, jcr->Job, jcr->JobStatus);
61 }
62
63 /*
64  * Common routine for:
65  *   dir_get_volume_info()
66  * and
67  *   dir_find_next_appendable_volume()
68  */
69 static int do_request_volume_info(JCR *jcr)
70 {
71     BSOCK *dir = jcr->dir_bsock;
72     VOLUME_CAT_INFO *vol = &jcr->VolCatInfo;
73
74     jcr->VolumeName[0] = 0;           /* No volume */
75     if (bnet_recv(dir) <= 0) {
76        Dmsg0(30, "getvolname error bnet_recv\n");
77        return 0;
78     }
79     if (sscanf(dir->msg, OK_media, vol->VolCatName, 
80                &vol->VolCatJobs, &vol->VolCatFiles,
81                &vol->VolCatBlocks, &vol->VolCatBytes, 
82                &vol->VolCatMounts, &vol->VolCatErrors,
83                &vol->VolCatWrites, &vol->VolCatMaxBytes, 
84                &vol->VolCatCapacityBytes, vol->VolCatStatus) != 11) {
85        Dmsg1(30, "Bad response from Dir: %s\n", dir->msg);
86        return 0;
87     }
88     unbash_spaces(vol->VolCatName);
89     strcpy(jcr->VolumeName, vol->VolCatName); /* set desired VolumeName */
90     
91     Dmsg1(200, "Got Volume=%s\n", vol->VolCatName);
92     return 1;
93 }
94
95
96 /*
97  * Get Volume info for a specific volume from the Director's Database
98  *
99  * Returns: 1 on success   (not Director guarantees that Pool and MediaType
100  *                          are correct and VolStatus==Append or
101  *                          VolStatus==Recycle)
102  *          0 on failure
103  *
104  *          Volume information returned in jcr
105  */
106 int dir_get_volume_info(JCR *jcr)
107 {
108     BSOCK *dir = jcr->dir_bsock;
109
110     strcpy(jcr->VolCatInfo.VolCatName, jcr->VolumeName);
111     Dmsg1(200, "dir_get_volume_info=%s\n", jcr->VolCatInfo.VolCatName);
112     bash_spaces(jcr->VolCatInfo.VolCatName);
113     bnet_fsend(dir, Get_Vol_Info, jcr->Job, jcr->VolCatInfo.VolCatName);
114     return do_request_volume_info(jcr);
115 }
116
117
118
119 /*
120  * Get info on the next appendable volume in the Director's database
121  * Returns: 1 on success
122  *          0 on failure
123  *
124  *          Volume information returned in jcr
125  *
126  */
127 int dir_find_next_appendable_volume(JCR *jcr)
128 {
129     BSOCK *dir = jcr->dir_bsock;
130
131     Dmsg0(200, "dir_find_next_appendable_volume\n");
132     bnet_fsend(dir, Find_media, jcr->Job, 1);
133     return do_request_volume_info(jcr);
134 }
135
136     
137 /*
138  * After writing a Volume, send the updated statistics
139  * back to the director.
140  */
141 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel)
142 {
143    BSOCK *dir = jcr->dir_bsock;
144    time_t EndTime = time(NULL);
145
146    if (vol->VolCatName[0] == 0) {
147       Jmsg0(jcr, M_ERROR, 0, _("NULL Volume name. This shouldn't happen!!!\n"));
148    }
149    bnet_fsend(dir, Update_media, jcr->Job, 
150       vol->VolCatName, vol->VolCatJobs, vol->VolCatFiles,
151       vol->VolCatBlocks, vol->VolCatBytes, 
152       vol->VolCatMounts, vol->VolCatErrors,
153       vol->VolCatWrites, vol->VolCatMaxBytes, EndTime, 
154       vol->VolCatStatus, 
155       jcr->VolFirstFile, jcr->JobFiles,
156       jcr->start_file, jcr->end_file,
157       jcr->start_block, jcr->end_block,
158       relabel);
159    Dmsg1(20, "update_volume_data(): %s", dir->msg);
160    if (bnet_recv(dir) <= 0) {
161       Dmsg0(90, "updateVolCatInfo error bnet_recv\n");
162       return 0;
163    }
164    Dmsg1(20, "Updatevol: %s", dir->msg);
165    if (strcmp(dir->msg, OK_update) != 0) {
166       Dmsg1(30, "Bad response from Dir: %s\n", dir->msg);
167       Jmsg(jcr, M_ERROR, 0, _("Error updating Volume Info: %s\n"), dir->msg);
168       return 0;
169    }
170    return 1;
171 }
172
173 /* 
174  * Update File Attribute data
175  */
176 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec)
177 {
178    BSOCK *dir = jcr->dir_bsock;
179    ser_declare;
180
181    dir->msglen = sprintf(dir->msg, FileAttributes, jcr->Job);
182    dir->msg = (char *) check_pool_memory_size(dir->msg, dir->msglen + 
183                 sizeof(DEV_RECORD) + rec->data_len);
184    ser_begin(dir->msg + dir->msglen, 0);
185    ser_uint32(rec->VolSessionId);
186    ser_uint32(rec->VolSessionTime);
187    ser_int32(rec->FileIndex);
188    ser_int32(rec->Stream);
189    ser_uint32(rec->data_len);
190    ser_bytes(rec->data, rec->data_len);
191    dir->msglen = ser_length(dir->msg);
192    return bnet_send(dir);
193 }
194
195
196 /*
197  *   
198  *   Entered with device blocked.
199  *   Leaves with device blocked.
200  *
201  *   Returns: 1 on success (operator issues a mount command)
202  *            0 on failure
203  *              Note, must create dev->errmsg on error return.
204  *
205  *    On success, jcr->VolumeName and jcr->VolCatInfo contain
206  *      information on suggested volume, but this may not be the
207  *      same as what is actually mounted.
208  */
209 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev)
210 {
211    struct timeval tv;
212    struct timezone tz;
213    struct timespec timeout;
214    int stat, jstat;
215    /* ******FIXME******* put these on config variable */
216    int min_wait = 60 * 60;
217    int max_wait = 24 * 60 * 60;
218    int max_num_wait = 9;              /* 5 waits =~ 1 day, then 1 day at a time */
219
220    int wait_sec;
221    int num_wait = 0;
222    int dev_blocked;
223    char *msg;
224
225    Dmsg0(30, "enter dir_ask_sysop_to_mount_next_volume\n");
226    ASSERT(dev->dev_blocked);
227    wait_sec = min_wait;
228    for ( ;; ) {
229       if (job_cancelled(jcr)) {
230          Mmsg(&dev->errmsg, _("Job %s cancelled while waiting for mount on Storage Device \"%s\".\n"), 
231               jcr->Job, jcr->dev_name);
232          return 0;
233       }
234       if (dir_find_next_appendable_volume(jcr)) {    /* get suggested volume */
235          jstat = JS_WaitMount;
236          /*
237           * If we have a valid volume name and we are not
238           * removable media, return now, otherwise wait
239           * for the operator to mount the media.
240           */
241          if (jcr->VolumeName[0] && !(dev->capabilities & CAP_REM) &&      
242               dev->capabilities & CAP_LABEL) {
243             Dmsg0(90, "Return 1 from mount without wait.\n");
244             return 1;
245          }
246          if (dev->capabilities & CAP_ANONVOLS) {
247             msg = "Suggest mounting";
248          } else {
249             msg = "Please mount";
250          }
251          Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
252               msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
253          Dmsg3(90, "Mount %s on %s for Job %s\n",
254                 jcr->VolumeName, jcr->dev_name, jcr->Job);
255       } else {
256          jstat = JS_WaitMedia;
257          Jmsg(jcr, M_MOUNT, 0, _("Job %s waiting. Cannot find any appendable volumes.\n\
258 Please use the \"unmount\" and \"label\"  commands to create new Volumes for:\n\
259    Storage Device \"%s\" with Pool \"%s\" and Media type \"%s\".\n\
260 Use \"mount\" to resume the job.\n"),
261               jcr->Job, jcr->dev_name, jcr->pool_name, jcr->media_type);
262       }
263       /*
264        * Wait then send message again
265        */
266       gettimeofday(&tv, &tz);
267       timeout.tv_nsec = tv.tv_usec * 1000;
268       timeout.tv_sec = tv.tv_sec + wait_sec;
269
270       P(dev->mutex);
271       dev_blocked = dev->dev_blocked;
272       dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
273       jcr->JobStatus = jstat;
274       dir_send_job_status(jcr);
275
276       for ( ;!job_cancelled(jcr); ) {
277          Dmsg1(90, "I'm going to sleep on device %s\n", dev->dev_name);
278          stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
279          if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
280             break;
281          }
282          /*         
283           * Someone other than us blocked the device (probably the
284           *  user via the Console program.   
285           * So, we continue waiting.
286           */
287          gettimeofday(&tv, &tz);
288          timeout.tv_nsec = 0;
289          timeout.tv_sec = tv.tv_sec + 10; /* wait 10 seconds */
290       }
291       dev->dev_blocked = dev_blocked;
292       V(dev->mutex);
293
294       if (stat == ETIMEDOUT) {
295          wait_sec *= 2;               /* double wait time */
296          if (wait_sec > max_wait) {   /* but not longer than maxtime */
297             wait_sec = max_wait;
298          }
299          num_wait++;
300          if (num_wait >= max_num_wait) {
301             Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"), 
302                  jcr->dev_name, jcr->Job);
303             Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
304             Dmsg1(90, "Gave up waiting on device %s\n", dev->dev_name);
305             return 0;                 /* exceeded maximum waits */
306          }
307          continue;
308       }
309       if (stat == EINVAL) {
310          Mmsg2(&dev->errmsg, _("pthread error in mount_next_volume stat=%d ERR=%s\n"),
311                stat, strerror(stat));
312          Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
313          return 0;
314       }
315       if (stat != 0) {
316          Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
317             strerror(stat));
318       }
319       Dmsg1(90, "Someone woke me for device %s\n", dev->dev_name);
320
321       /* Restart wait counters */
322       wait_sec = min_wait;
323       num_wait = 0;
324       /* If no VolumeName, and cannot get one, try again */
325       if (jcr->VolumeName[0] == 0 && 
326           !dir_find_next_appendable_volume(jcr)) {
327          Jmsg(jcr, M_MOUNT, 0, _(
328 "You woke me up, but I cannot find any appendable\n\
329 volumes for Job=%s.\n"), jcr->Job);
330          continue;
331       }       
332       break;
333    }
334    jcr->JobStatus = JS_Running;
335    dir_send_job_status(jcr);
336    Dmsg0(30, "leave dir_ask_sysop_to_mount_next_volume\n");
337    return 1;
338 }
339
340 /*
341  *   
342  *   Entered with device blocked and jcr->VolumeName is desired
343  *      volume.
344  *   Leaves with device blocked.
345  *
346  *   Returns: 1 on success (operator issues a mount command)
347  *            0 on failure
348  *              Note, must create dev->errmsg on error return.
349  *
350  */
351 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
352 {
353    int stat, jstat;
354    /* ******FIXME******* put these on config variable */
355    int min_wait = 60 * 60;
356    int max_wait = 24 * 60 * 60;
357    int max_num_wait = 9;              /* 5 waits =~ 1 day, then 1 day at a time */
358    int wait_sec;
359    int num_wait = 0;
360    int dev_blocked;
361    char *msg;
362    struct timeval tv;
363    struct timezone tz;
364    struct timespec timeout;
365
366    Dmsg0(30, "enter dir_ask_sysop_to_mount_next_volume\n");
367    if (!jcr->VolumeName[0]) {
368       Mmsg0(&dev->errmsg, _("Cannot request another volume: no volume name given.\n"));
369       return 0;
370    }
371    ASSERT(dev->dev_blocked);
372    wait_sec = min_wait;
373    for ( ;; ) {
374       if (job_cancelled(jcr)) {
375          Mmsg(&dev->errmsg, _("Job %s cancelled while waiting for mount on Storage Device \"%s\".\n"), 
376               jcr->Job, jcr->dev_name);
377          return 0;
378       }
379       msg = _("Please mount");
380       Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
381            msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
382       Dmsg3(90, "Mount %s on %s for Job %s\n",
383             jcr->VolumeName, jcr->dev_name, jcr->Job);
384
385       /*
386        * Wait then send message again
387        */
388       gettimeofday(&tv, &tz);
389       timeout.tv_nsec = tv.tv_usec * 1000;
390       timeout.tv_sec = tv.tv_sec + wait_sec;
391
392       P(dev->mutex);
393       dev_blocked = dev->dev_blocked;
394       dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
395       jcr->JobStatus = jstat;
396       dir_send_job_status(jcr);
397
398       for ( ;!job_cancelled(jcr); ) {
399          Dmsg1(90, "I'm going to sleep on device %s\n", dev->dev_name);
400          stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
401          if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
402             break;
403          }
404          /*         
405           * Someone other than us blocked the device (probably the
406           *  user via the Console program.   
407           * So, we continue waiting.
408           */
409          gettimeofday(&tv, &tz);
410          timeout.tv_nsec = 0;
411          timeout.tv_sec = tv.tv_sec + 10; /* wait 10 seconds */
412       }
413       dev->dev_blocked = dev_blocked;
414       V(dev->mutex);
415
416       if (stat == ETIMEDOUT) {
417          wait_sec *= 2;               /* double wait time */
418          if (wait_sec > max_wait) {   /* but not longer than maxtime */
419             wait_sec = max_wait;
420          }
421          num_wait++;
422          if (num_wait >= max_num_wait) {
423             Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"), 
424                  jcr->dev_name, jcr->Job);
425             Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
426             Dmsg1(90, "Gave up waiting on device %s\n", dev->dev_name);
427             return 0;                 /* exceeded maximum waits */
428          }
429          continue;
430       }
431       if (stat == EINVAL) {
432          Mmsg2(&dev->errmsg, _("pthread error in mount_volume stat=%d ERR=%s\n"),
433                stat, strerror(stat));
434          Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
435          return 0;
436       }
437       if (stat != 0) {
438          Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
439             strerror(stat));
440       }
441       Dmsg1(90, "Someone woke me for device %s\n", dev->dev_name);
442
443       /* Restart wait counters */
444       wait_sec = min_wait;
445       num_wait = 0;
446       break;
447    }
448    jcr->JobStatus = JS_Running;
449    dir_send_job_status(jcr);
450    Dmsg0(30, "leave dir_ask_sysop_to_mount_next_volume\n");
451    return 1;
452 }