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 write=%d\n";
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";
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";
46 static char FileAttributes[] = "UpdCat Job=%s FileAttributes ";
48 static char Job_status[] = "3012 Job %s jobstatus %d\n";
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";
57 static char OK_update[] = "1000 OK UpdateMedia\n";
61 * Send current JobStatus to Director
63 int dir_send_job_status(JCR *jcr)
65 return bnet_fsend(jcr->dir_bsock, Job_status, jcr->Job, jcr->JobStatus);
70 * dir_get_volume_info()
72 * dir_find_next_appendable_volume()
74 static int do_request_volume_info(JCR *jcr)
76 BSOCK *dir = jcr->dir_bsock;
77 VOLUME_CAT_INFO *vol = &jcr->VolCatInfo;
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"));
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) {
93 Dmsg1(000, "Bad response from Dir: %s\n", dir->msg);
94 Mmsg(&jcr->errmsg, _("Error scanning Dir response: %s\n"), dir->msg);
97 unbash_spaces(vol->VolCatName);
98 strcpy(jcr->VolumeName, vol->VolCatName); /* set desired VolumeName */
100 Dmsg2(200, "do_reqest_vol_info got slot=%d Volume=%s\n",
101 vol->Slot, vol->VolCatName);
107 * Get Volume info for a specific volume from the Director's Database
109 * Returns: 1 on success (not Director guarantees that Pool and MediaType
110 * are correct and VolStatus==Append or
111 * VolStatus==Recycle)
114 * Volume information returned in jcr
116 int dir_get_volume_info(JCR *jcr, int writing)
118 BSOCK *dir = jcr->dir_bsock;
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);
130 * Get info on the next appendable volume in the Director's database
131 * Returns: 1 on success
134 * Volume information returned in jcr
137 int dir_find_next_appendable_volume(JCR *jcr)
139 BSOCK *dir = jcr->dir_bsock;
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);
148 * After writing a Volume, send the updated statistics
149 * back to the director.
151 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel)
153 BSOCK *dir = jcr->dir_bsock;
154 time_t EndTime = time(NULL);
155 char ed1[50], ed2[50];
157 if (vol->VolCatName[0] == 0) {
158 Jmsg0(jcr, M_ERROR, 0, _("NULL Volume name. This shouldn't happen!!!\n"));
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 if (bnet_recv(dir) <= 0) {
169 Dmsg0(190, "updateVolCatInfo error bnet_recv\n");
172 Dmsg1(120, "Updatevol: %s", dir->msg);
173 if (strcmp(dir->msg, OK_update) != 0) {
174 Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
175 Jmsg(jcr, M_ERROR, 0, _("Error updating Volume Info: %s\n"), dir->msg);
182 * After writing a Volume, create the JobMedia record.
184 int dir_create_jobmedia_record(JCR *jcr)
186 BSOCK *dir = jcr->dir_bsock;
188 bnet_fsend(dir, Create_job_media, jcr->Job,
189 jcr->VolFirstFile, jcr->JobFiles,
190 jcr->StartFile, jcr->EndFile,
191 jcr->StartBlock, jcr->EndBlock);
192 Dmsg1(100, "create_jobmedia(): %s", dir->msg);
193 if (bnet_recv(dir) <= 0) {
194 Dmsg0(190, "create_jobmedia error bnet_recv\n");
197 Dmsg1(120, "Create_jobmedia: %s", dir->msg);
198 if (strcmp(dir->msg, OK_update) != 0) {
199 Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
200 Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: %s\n"), dir->msg);
208 * Update File Attribute data
210 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec)
212 BSOCK *dir = jcr->dir_bsock;
215 dir->msglen = sprintf(dir->msg, FileAttributes, jcr->Job);
216 dir->msg = check_pool_memory_size(dir->msg, dir->msglen +
217 sizeof(DEV_RECORD) + rec->data_len);
218 ser_begin(dir->msg + dir->msglen, 0);
219 ser_uint32(rec->VolSessionId);
220 ser_uint32(rec->VolSessionTime);
221 ser_int32(rec->FileIndex);
222 ser_int32(rec->Stream);
223 ser_uint32(rec->data_len);
224 ser_bytes(rec->data, rec->data_len);
225 dir->msglen = ser_length(dir->msg);
226 return bnet_send(dir);
232 * Entered with device blocked.
233 * Leaves with device blocked.
235 * Returns: 1 on success (operator issues a mount command)
237 * Note, must create dev->errmsg on error return.
239 * On success, jcr->VolumeName and jcr->VolCatInfo contain
240 * information on suggested volume, but this may not be the
241 * same as what is actually mounted.
243 * When we return with success, the correct tape may or may not
244 * actually be mounted. The calling routine must read it and
247 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev)
251 struct timespec timeout;
253 /* ******FIXME******* put these on config variable */
254 int min_wait = 60 * 60;
255 int max_wait = 24 * 60 * 60;
256 int max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
262 Dmsg0(130, "enter dir_ask_sysop_to_mount_next_volume\n");
263 ASSERT(dev->dev_blocked);
266 if (job_cancelled(jcr)) {
267 Mmsg(&dev->errmsg, _("Job %s cancelled while waiting for mount on Storage Device \"%s\".\n"),
268 jcr->Job, jcr->dev_name);
269 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
272 if (dir_find_next_appendable_volume(jcr)) { /* get suggested volume */
273 jstat = JS_WaitMount;
275 * If we have a valid volume name and we are not
276 * removable media, return now, otherwise wait
277 * for the operator to mount the media.
279 if (jcr->VolumeName[0] && !(dev->capabilities & CAP_REM) &&
280 dev->capabilities & CAP_LABEL) {
281 Dmsg0(190, "Return 1 from mount without wait.\n");
284 Jmsg(jcr, M_MOUNT, 0, _(
285 "Please mount Volume \"%s\" on Storage Device \"%s\" for Job %s\n"
286 "Use \"mount\" command to release Job.\n"),
287 jcr->VolumeName, jcr->dev_name, jcr->Job);
288 Dmsg3(190, "Mount %s on %s for Job %s\n",
289 jcr->VolumeName, jcr->dev_name, jcr->Job);
291 jstat = JS_WaitMedia;
292 Jmsg(jcr, M_MOUNT, 0, _(
293 "Job %s waiting. Cannot find any appendable volumes.\n\
294 Please use the \"label\" command to create a new Volume for:\n\
304 * Wait then send message again
306 gettimeofday(&tv, &tz);
307 timeout.tv_nsec = tv.tv_usec * 1000;
308 timeout.tv_sec = tv.tv_sec + wait_sec;
311 dev_blocked = dev->dev_blocked;
312 dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
313 jcr->JobStatus = jstat;
314 dir_send_job_status(jcr);
316 for ( ;!job_cancelled(jcr); ) {
317 Dmsg1(190, "I'm going to sleep on device %s\n", dev->dev_name);
318 stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
319 if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
323 * Someone other than us blocked the device (probably the
324 * user via the Console program.
325 * So, we continue waiting.
327 gettimeofday(&tv, &tz);
329 timeout.tv_sec = tv.tv_sec + 10; /* wait 10 seconds */
331 dev->dev_blocked = dev_blocked;
334 if (stat == ETIMEDOUT) {
335 wait_sec *= 2; /* double wait time */
336 if (wait_sec > max_wait) { /* but not longer than maxtime */
340 if (num_wait >= max_num_wait) {
341 Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"),
342 jcr->dev_name, jcr->Job);
343 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
344 Dmsg1(190, "Gave up waiting on device %s\n", dev->dev_name);
345 return 0; /* exceeded maximum waits */
349 if (stat == EINVAL) {
350 Mmsg2(&dev->errmsg, _("pthread error in mount_next_volume stat=%d ERR=%s\n"),
351 stat, strerror(stat));
352 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
356 Jmsg(jcr, M_WARNING, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
359 Dmsg1(190, "Someone woke me for device %s\n", dev->dev_name);
361 /* Restart wait counters */
364 /* If no VolumeName, and cannot get one, try again */
365 if (jcr->VolumeName[0] == 0 &&
366 !dir_find_next_appendable_volume(jcr)) {
367 Jmsg(jcr, M_MOUNT, 0, _(
368 "Someone woke me up, but I cannot find any appendable\n\
369 volumes for Job=%s.\n"), jcr->Job);
374 jcr->JobStatus = JS_Running;
375 dir_send_job_status(jcr);
376 Dmsg0(130, "leave dir_ask_sysop_to_mount_next_volume\n");
382 * Entered with device blocked and jcr->VolumeName is desired
384 * Leaves with device blocked.
386 * Returns: 1 on success (operator issues a mount command)
388 * Note, must create dev->errmsg on error return.
391 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
394 /* ******FIXME******* put these on config variable */
395 int min_wait = 60 * 60;
396 int max_wait = 24 * 60 * 60;
397 int max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
404 struct timespec timeout;
406 Dmsg0(130, "enter dir_ask_sysop_to_mount_next_volume\n");
407 if (!jcr->VolumeName[0]) {
408 Mmsg0(&dev->errmsg, _("Cannot request another volume: no volume name given.\n"));
411 ASSERT(dev->dev_blocked);
414 if (job_cancelled(jcr)) {
415 Mmsg(&dev->errmsg, _("Job %s cancelled while waiting for mount on Storage Device \"%s\".\n"),
416 jcr->Job, jcr->dev_name);
419 msg = _("Please mount");
420 Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
421 msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
422 Dmsg3(190, "Mount %s on %s for Job %s\n",
423 jcr->VolumeName, jcr->dev_name, jcr->Job);
426 * Wait then send message again
428 gettimeofday(&tv, &tz);
429 timeout.tv_nsec = tv.tv_usec * 1000;
430 timeout.tv_sec = tv.tv_sec + wait_sec;
433 dev_blocked = dev->dev_blocked;
434 dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
435 jcr->JobStatus = JS_WaitMount;
436 dir_send_job_status(jcr);
438 for ( ;!job_cancelled(jcr); ) {
439 Dmsg1(190, "I'm going to sleep on device %s\n", dev->dev_name);
440 stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
441 if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
445 * Someone other than us blocked the device (probably the
446 * user via the Console program.
447 * So, we continue waiting.
449 gettimeofday(&tv, &tz);
451 timeout.tv_sec = tv.tv_sec + 10; /* wait 10 seconds */
453 dev->dev_blocked = dev_blocked;
456 if (stat == ETIMEDOUT) {
457 wait_sec *= 2; /* double wait time */
458 if (wait_sec > max_wait) { /* but not longer than maxtime */
462 if (num_wait >= max_num_wait) {
463 Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"),
464 jcr->dev_name, jcr->Job);
465 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
466 Dmsg1(190, "Gave up waiting on device %s\n", dev->dev_name);
467 return 0; /* exceeded maximum waits */
471 if (stat == EINVAL) {
472 Mmsg2(&dev->errmsg, _("pthread error in mount_volume stat=%d ERR=%s\n"),
473 stat, strerror(stat));
474 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
478 Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
481 Dmsg1(190, "Someone woke me for device %s\n", dev->dev_name);
483 /* Restart wait counters */
488 jcr->JobStatus = JS_Running;
489 dir_send_job_status(jcr);
490 Dmsg0(130, "leave dir_ask_sysop_to_mount_next_volume\n");