2 * Subroutines to handle Catalog reqests sent to the Director
3 * Reqests/commands from the Director are handled in dircmd.c
5 * Kern Sibbald, December 2000
10 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
29 #include "bacula.h" /* pull in global headers */
30 #include "stored.h" /* pull in Storage Deamon headers */
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";
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";
42 static char FileAttributes[] = "UpdCat Job=%s FileAttributes ";
44 static char Job_status[] = "3012 Job %s jobstatus %d\n";
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";
52 static char OK_update[] = "1000 OK UpdateMedia\n";
56 * Send current JobStatus to Director
58 int dir_send_job_status(JCR *jcr)
60 return bnet_fsend(jcr->dir_bsock, Job_status, jcr->Job, jcr->JobStatus);
65 * dir_get_volume_info()
67 * dir_find_next_appendable_volume()
69 static int do_request_volume_info(JCR *jcr)
71 BSOCK *dir = jcr->dir_bsock;
72 VOLUME_CAT_INFO *vol = &jcr->VolCatInfo;
74 jcr->VolumeName[0] = 0; /* No volume */
75 if (bnet_recv(dir) <= 0) {
76 Dmsg0(30, "getvolname error bnet_recv\n");
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);
88 unbash_spaces(vol->VolCatName);
89 strcpy(jcr->VolumeName, vol->VolCatName); /* set desired VolumeName */
91 Dmsg1(200, "Got Volume=%s\n", vol->VolCatName);
97 * Get Volume info for a specific volume from the Director's Database
99 * Returns: 1 on success (not Director guarantees that Pool and MediaType
100 * are correct and VolStatus==Append or
101 * VolStatus==Recycle)
104 * Volume information returned in jcr
106 int dir_get_volume_info(JCR *jcr)
108 BSOCK *dir = jcr->dir_bsock;
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);
120 * Get info on the next appendable volume in the Director's database
121 * Returns: 1 on success
124 * Volume information returned in jcr
127 int dir_find_next_appendable_volume(JCR *jcr)
129 BSOCK *dir = jcr->dir_bsock;
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);
138 * After writing a Volume, send the updated statistics
139 * back to the director.
141 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel)
143 BSOCK *dir = jcr->dir_bsock;
144 time_t EndTime = time(NULL);
146 if (vol->VolCatName[0] == 0) {
147 Jmsg0(jcr, M_ERROR, 0, _("NULL Volume name. This shouldn't happen!!!\n"));
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,
155 jcr->VolFirstFile, jcr->JobFiles,
156 jcr->start_file, jcr->end_file,
157 jcr->start_block, jcr->end_block,
159 Dmsg1(20, "update_volume_data(): %s", dir->msg);
160 if (bnet_recv(dir) <= 0) {
161 Dmsg0(90, "updateVolCatInfo error bnet_recv\n");
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);
174 * Update File Attribute data
176 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec)
178 BSOCK *dir = jcr->dir_bsock;
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);
198 * Entered with device blocked.
199 * Leaves with device blocked.
201 * Returns: 1 on success (operator issues a mount command)
203 * Note, must create dev->errmsg on error return.
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.
209 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev)
213 struct timespec timeout;
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 */
225 Dmsg0(30, "enter dir_ask_sysop_to_mount_next_volume\n");
226 ASSERT(dev->dev_blocked);
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);
234 if (dir_find_next_appendable_volume(jcr)) { /* get suggested volume */
235 jstat = JS_WaitMount;
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.
241 if (jcr->VolumeName[0] && !(dev->capabilities & CAP_REM) &&
242 dev->capabilities & CAP_LABEL) {
243 Dmsg0(90, "Return 1 from mount without wait.\n");
246 if (dev->capabilities & CAP_ANONVOLS) {
247 msg = "Suggest mounting";
249 msg = "Please mount";
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);
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);
264 * Wait then send message again
266 gettimeofday(&tv, &tz);
267 timeout.tv_nsec = tv.tv_usec * 1000;
268 timeout.tv_sec = tv.tv_sec + wait_sec;
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);
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) {
283 * Someone other than us blocked the device (probably the
284 * user via the Console program.
285 * So, we continue waiting.
287 gettimeofday(&tv, &tz);
289 timeout.tv_sec = tv.tv_sec + 10; /* wait 10 seconds */
291 dev->dev_blocked = dev_blocked;
294 if (stat == ETIMEDOUT) {
295 wait_sec *= 2; /* double wait time */
296 if (wait_sec > max_wait) { /* but not longer than maxtime */
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 */
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);
316 Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
319 Dmsg1(90, "Someone woke me for device %s\n", dev->dev_name);
321 /* Restart wait counters */
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);
334 jcr->JobStatus = JS_Running;
335 dir_send_job_status(jcr);
336 Dmsg0(30, "leave dir_ask_sysop_to_mount_next_volume\n");
342 * Entered with device blocked and jcr->VolumeName is desired
344 * Leaves with device blocked.
346 * Returns: 1 on success (operator issues a mount command)
348 * Note, must create dev->errmsg on error return.
351 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
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 */
364 struct timespec timeout;
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"));
371 ASSERT(dev->dev_blocked);
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);
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);
386 * Wait then send message again
388 gettimeofday(&tv, &tz);
389 timeout.tv_nsec = tv.tv_usec * 1000;
390 timeout.tv_sec = tv.tv_sec + wait_sec;
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);
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) {
405 * Someone other than us blocked the device (probably the
406 * user via the Console program.
407 * So, we continue waiting.
409 gettimeofday(&tv, &tz);
411 timeout.tv_sec = tv.tv_sec + 10; /* wait 10 seconds */
413 dev->dev_blocked = dev_blocked;
416 if (stat == ETIMEDOUT) {
417 wait_sec *= 2; /* double wait time */
418 if (wait_sec > max_wait) { /* but not longer than maxtime */
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 */
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);
438 Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
441 Dmsg1(90, "Someone woke me for device %s\n", dev->dev_name);
443 /* Restart wait counters */
448 jcr->JobStatus = JS_Running;
449 dir_send_job_status(jcr);
450 Dmsg0(30, "leave dir_ask_sysop_to_mount_next_volume\n");