]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/askdir.c
Autochanger for read + 64 bit addrs + Session key, see kes14Sep02
[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 write=%d\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  Slot=%d relabel=%d\n";
40
41 static char Create_job_media[] = "CatReq Job=%s CreateJobMedia \
42  FirstIndex=%d LastIndex=%d StartFile=%d EndFile=%d \
43  StartBlock=%d EndBlock=%d\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=%d VolFiles=%d\
53  VolBlocks=%d VolBytes=%" lld " VolMounts=%d VolErrors=%d VolWrites=%d\
54  VolMaxBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%20s\
55  Slot=%d\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        return 0;
83     }
84     if (sscanf(dir->msg, OK_media, vol->VolCatName, 
85                &vol->VolCatJobs, &vol->VolCatFiles,
86                &vol->VolCatBlocks, &vol->VolCatBytes, 
87                &vol->VolCatMounts, &vol->VolCatErrors,
88                &vol->VolCatWrites, &vol->VolCatMaxBytes, 
89                &vol->VolCatCapacityBytes, vol->VolCatStatus,
90                &vol->Slot) != 12) {
91        Dmsg1(200, "Bad response from Dir: %s\n", dir->msg);
92        return 0;
93     }
94     unbash_spaces(vol->VolCatName);
95     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
153    if (vol->VolCatName[0] == 0) {
154       Jmsg0(jcr, M_ERROR, 0, _("NULL Volume name. This shouldn't happen!!!\n"));
155       return 0;
156    }
157    bnet_fsend(dir, Update_media, jcr->Job, 
158       vol->VolCatName, vol->VolCatJobs, vol->VolCatFiles,
159       vol->VolCatBlocks, vol->VolCatBytes, 
160       vol->VolCatMounts, vol->VolCatErrors,
161       vol->VolCatWrites, vol->VolCatMaxBytes, EndTime, 
162       vol->VolCatStatus, vol->Slot, relabel);
163    Dmsg1(120, "update_volume_data(): %s", dir->msg);
164    if (bnet_recv(dir) <= 0) {
165       Dmsg0(190, "updateVolCatInfo error bnet_recv\n");
166       return 0;
167    }
168    Dmsg1(120, "Updatevol: %s", dir->msg);
169    if (strcmp(dir->msg, OK_update) != 0) {
170       Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
171       Jmsg(jcr, M_ERROR, 0, _("Error updating Volume Info: %s\n"), dir->msg);
172       return 0;
173    }
174    return 1;
175 }
176
177 /*
178  * After writing a Volume, create the JobMedia record.
179  */
180 int dir_create_jobmedia_record(JCR *jcr)
181 {
182    BSOCK *dir = jcr->dir_bsock;
183
184    bnet_fsend(dir, Create_job_media, jcr->Job, 
185       jcr->VolFirstFile, jcr->JobFiles,
186       jcr->start_file, jcr->end_file,
187       jcr->start_block, jcr->end_block);
188    Dmsg1(100, "create_jobmedia(): %s", dir->msg);
189    if (bnet_recv(dir) <= 0) {
190       Dmsg0(190, "create_jobmedia error bnet_recv\n");
191       return 0;
192    }
193    Dmsg1(120, "Create_jobmedia: %s", dir->msg);
194    if (strcmp(dir->msg, OK_update) != 0) {
195       Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
196       Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: %s\n"), dir->msg);
197       return 0;
198    }
199    return 1;
200 }
201
202
203 /* 
204  * Update File Attribute data
205  */
206 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec)
207 {
208    BSOCK *dir = jcr->dir_bsock;
209    ser_declare;
210
211    dir->msglen = sprintf(dir->msg, FileAttributes, jcr->Job);
212    dir->msg = check_pool_memory_size(dir->msg, dir->msglen + 
213                 sizeof(DEV_RECORD) + rec->data_len);
214    ser_begin(dir->msg + dir->msglen, 0);
215    ser_uint32(rec->VolSessionId);
216    ser_uint32(rec->VolSessionTime);
217    ser_int32(rec->FileIndex);
218    ser_int32(rec->Stream);
219    ser_uint32(rec->data_len);
220    ser_bytes(rec->data, rec->data_len);
221    dir->msglen = ser_length(dir->msg);
222    return bnet_send(dir);
223 }
224
225
226 /*
227  *   
228  *   Entered with device blocked.
229  *   Leaves with device blocked.
230  *
231  *   Returns: 1 on success (operator issues a mount command)
232  *            0 on failure
233  *              Note, must create dev->errmsg on error return.
234  *
235  *    On success, jcr->VolumeName and jcr->VolCatInfo contain
236  *      information on suggested volume, but this may not be the
237  *      same as what is actually mounted.
238  *
239  *    When we return with success, the correct tape may or may not
240  *      actually be mounted. The calling routine must read it and
241  *      verify the label.
242  */
243 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev)
244 {
245    struct timeval tv;
246    struct timezone tz;
247    struct timespec timeout;
248    int stat = 0, jstat;
249    /* ******FIXME******* put these on config variable */
250    int min_wait = 60 * 60;
251    int max_wait = 24 * 60 * 60;
252    int max_num_wait = 9;              /* 5 waits =~ 1 day, then 1 day at a time */
253
254    int wait_sec;
255    int num_wait = 0;
256    int dev_blocked;
257    char *msg;
258
259    Dmsg0(130, "enter dir_ask_sysop_to_mount_next_volume\n");
260    ASSERT(dev->dev_blocked);
261    wait_sec = min_wait;
262    for ( ;; ) {
263       if (job_cancelled(jcr)) {
264          Mmsg(&dev->errmsg, _("Job %s cancelled while waiting for mount on Storage Device \"%s\".\n"), 
265               jcr->Job, jcr->dev_name);
266          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
267          return 0;
268       }
269       if (dir_find_next_appendable_volume(jcr)) {    /* get suggested volume */
270          jstat = JS_WaitMount;
271          /*
272           * If we have a valid volume name and we are not
273           * removable media, return now, otherwise wait
274           * for the operator to mount the media.
275           */
276          if (jcr->VolumeName[0] && !(dev->capabilities & CAP_REM) &&      
277               dev->capabilities & CAP_LABEL) {
278             Dmsg0(190, "Return 1 from mount without wait.\n");
279             return 1;
280          }
281          if (dev->capabilities & CAP_ANONVOLS) {
282             msg = "Suggest mounting";
283          } else {
284             msg = "Please mount";
285          }
286          Jmsg(jcr, M_MOUNT, 0, _(
287 "%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"
288 "Use \"mount\" command to release Job.\n"),
289               msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
290          Dmsg3(190, "Mount %s on %s for Job %s\n",
291                 jcr->VolumeName, jcr->dev_name, jcr->Job);
292       } else {
293          jstat = JS_WaitMedia;
294          Jmsg(jcr, M_MOUNT, 0, _(
295 "Job %s waiting. Cannot find any appendable volumes.\n\
296 Please use the \"label\"  command to create a new Volume for:\n\
297     Storage:      %s\n\
298     Media type:   %s\n\
299     Pool:         %s\n"),
300               jcr->Job, 
301               jcr->dev_name, 
302               jcr->media_type,
303               jcr->pool_name);
304       }
305       /*
306        * Wait then send message again
307        */
308       gettimeofday(&tv, &tz);
309       timeout.tv_nsec = tv.tv_usec * 1000;
310       timeout.tv_sec = tv.tv_sec + wait_sec;
311
312       P(dev->mutex);
313       dev_blocked = dev->dev_blocked;
314       dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
315       jcr->JobStatus = jstat;
316       dir_send_job_status(jcr);
317
318       for ( ;!job_cancelled(jcr); ) {
319          Dmsg1(190, "I'm going to sleep on device %s\n", dev->dev_name);
320          stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
321          if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
322             break;
323          }
324          /*         
325           * Someone other than us blocked the device (probably the
326           *  user via the Console program.   
327           * So, we continue waiting.
328           */
329          gettimeofday(&tv, &tz);
330          timeout.tv_nsec = 0;
331          timeout.tv_sec = tv.tv_sec + 10; /* wait 10 seconds */
332       }
333       dev->dev_blocked = dev_blocked;
334       V(dev->mutex);
335
336       if (stat == ETIMEDOUT) {
337          wait_sec *= 2;               /* double wait time */
338          if (wait_sec > max_wait) {   /* but not longer than maxtime */
339             wait_sec = max_wait;
340          }
341          num_wait++;
342          if (num_wait >= max_num_wait) {
343             Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"), 
344                  jcr->dev_name, jcr->Job);
345             Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
346             Dmsg1(190, "Gave up waiting on device %s\n", dev->dev_name);
347             return 0;                 /* exceeded maximum waits */
348          }
349          continue;
350       }
351       if (stat == EINVAL) {
352          Mmsg2(&dev->errmsg, _("pthread error in mount_next_volume stat=%d ERR=%s\n"),
353                stat, strerror(stat));
354          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
355          return 0;
356       }
357       if (stat != 0) {
358          Jmsg(jcr, M_WARNING, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
359             strerror(stat));
360       }
361       Dmsg1(190, "Someone woke me for device %s\n", dev->dev_name);
362
363       /* Restart wait counters */
364       wait_sec = min_wait;
365       num_wait = 0;
366       /* If no VolumeName, and cannot get one, try again */
367       if (jcr->VolumeName[0] == 0 && 
368           !dir_find_next_appendable_volume(jcr)) {
369          Jmsg(jcr, M_MOUNT, 0, _(
370 "Someone woke me up, but I cannot find any appendable\n\
371 volumes for Job=%s.\n"), jcr->Job);
372          continue;
373       }       
374       break;
375    }
376    jcr->JobStatus = JS_Running;
377    dir_send_job_status(jcr);
378    Dmsg0(130, "leave dir_ask_sysop_to_mount_next_volume\n");
379    return 1;
380 }
381
382 /*
383  *   
384  *   Entered with device blocked and jcr->VolumeName is desired
385  *      volume.
386  *   Leaves with device blocked.
387  *
388  *   Returns: 1 on success (operator issues a mount command)
389  *            0 on failure
390  *              Note, must create dev->errmsg on error return.
391  *
392  */
393 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
394 {
395    int stat = 0;
396    /* ******FIXME******* put these on config variable */
397    int min_wait = 60 * 60;
398    int max_wait = 24 * 60 * 60;
399    int max_num_wait = 9;              /* 5 waits =~ 1 day, then 1 day at a time */
400    int wait_sec;
401    int num_wait = 0;
402    int dev_blocked;
403    char *msg;
404    struct timeval tv;
405    struct timezone tz;
406    struct timespec timeout;
407
408    Dmsg0(130, "enter dir_ask_sysop_to_mount_next_volume\n");
409    if (!jcr->VolumeName[0]) {
410       Mmsg0(&dev->errmsg, _("Cannot request another volume: no volume name given.\n"));
411       return 0;
412    }
413    ASSERT(dev->dev_blocked);
414    wait_sec = min_wait;
415    for ( ;; ) {
416       if (job_cancelled(jcr)) {
417          Mmsg(&dev->errmsg, _("Job %s cancelled while waiting for mount on Storage Device \"%s\".\n"), 
418               jcr->Job, jcr->dev_name);
419          return 0;
420       }
421       msg = _("Please mount");
422       Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
423            msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
424       Dmsg3(190, "Mount %s on %s for Job %s\n",
425             jcr->VolumeName, jcr->dev_name, jcr->Job);
426
427       /*
428        * Wait then send message again
429        */
430       gettimeofday(&tv, &tz);
431       timeout.tv_nsec = tv.tv_usec * 1000;
432       timeout.tv_sec = tv.tv_sec + wait_sec;
433
434       P(dev->mutex);
435       dev_blocked = dev->dev_blocked;
436       dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
437       jcr->JobStatus = JS_WaitMount;
438       dir_send_job_status(jcr);
439
440       for ( ;!job_cancelled(jcr); ) {
441          Dmsg1(190, "I'm going to sleep on device %s\n", dev->dev_name);
442          stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
443          if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
444             break;
445          }
446          /*         
447           * Someone other than us blocked the device (probably the
448           *  user via the Console program.   
449           * So, we continue waiting.
450           */
451          gettimeofday(&tv, &tz);
452          timeout.tv_nsec = 0;
453          timeout.tv_sec = tv.tv_sec + 10; /* wait 10 seconds */
454       }
455       dev->dev_blocked = dev_blocked;
456       V(dev->mutex);
457
458       if (stat == ETIMEDOUT) {
459          wait_sec *= 2;               /* double wait time */
460          if (wait_sec > max_wait) {   /* but not longer than maxtime */
461             wait_sec = max_wait;
462          }
463          num_wait++;
464          if (num_wait >= max_num_wait) {
465             Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"), 
466                  jcr->dev_name, jcr->Job);
467             Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
468             Dmsg1(190, "Gave up waiting on device %s\n", dev->dev_name);
469             return 0;                 /* exceeded maximum waits */
470          }
471          continue;
472       }
473       if (stat == EINVAL) {
474          Mmsg2(&dev->errmsg, _("pthread error in mount_volume stat=%d ERR=%s\n"),
475                stat, strerror(stat));
476          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
477          return 0;
478       }
479       if (stat != 0) {
480          Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
481             strerror(stat));
482       }
483       Dmsg1(190, "Someone woke me for device %s\n", dev->dev_name);
484
485       /* Restart wait counters */
486       wait_sec = min_wait;
487       num_wait = 0;
488       break;
489    }
490    jcr->JobStatus = JS_Running;
491    dir_send_job_status(jcr);
492    Dmsg0(130, "leave dir_ask_sysop_to_mount_next_volume\n");
493    return 1;
494 }