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-2003 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";
35 static char Update_media[] = "CatReq Job=%s UpdateMedia VolName=%s\
36 VolJobs=%u VolFiles=%u VolBlocks=%u VolBytes=%s VolMounts=%u\
37 VolErrors=%u VolWrites=%u MaxVolBytes=%s EndTime=%d VolStatus=%s\
38 Slot=%d relabel=%d\n";
40 static char Create_job_media[] = "CatReq Job=%s CreateJobMedia \
41 FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u \
42 StartBlock=%u EndBlock=%u\n";
43 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=%u VolFiles=%u\
49 VolBlocks=%u VolBytes=%" lld " VolMounts=%u VolErrors=%u VolWrites=%u\
50 MaxVolBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%20s\
51 Slot=%d MaxVolJobs=%u MaxVolFiles=%u\n";
52 static char OK_update[] = "1000 OK UpdateMedia\n";
54 /* Forward referenced functions */
55 static int wait_for_sysop(JCR *jcr, DEVICE *dev, int wait_sec);
58 * Send current JobStatus to Director
60 int dir_send_job_status(JCR *jcr)
62 return bnet_fsend(jcr->dir_bsock, Job_status, jcr->Job, jcr->JobStatus);
67 * dir_get_volume_info()
69 * dir_find_next_appendable_volume()
71 static int do_request_volume_info(JCR *jcr)
73 BSOCK *dir = jcr->dir_bsock;
76 jcr->VolumeName[0] = 0; /* No volume */
77 if (bnet_recv(dir) <= 0) {
78 Dmsg0(200, "getvolname error bnet_recv\n");
79 Mmsg(&jcr->errmsg, _("Network error on bnet_recv in req_vol_info.\n"));
82 memset(&vol, 0, sizeof(vol));
83 if (sscanf(dir->msg, OK_media, vol.VolCatName,
84 &vol.VolCatJobs, &vol.VolCatFiles,
85 &vol.VolCatBlocks, &vol.VolCatBytes,
86 &vol.VolCatMounts, &vol.VolCatErrors,
87 &vol.VolCatWrites, &vol.VolCatMaxBytes,
88 &vol.VolCatCapacityBytes, vol.VolCatStatus,
89 &vol.Slot, &vol.VolCatMaxJobs, &vol.VolCatMaxFiles) != 14) {
91 Dmsg1(200, "Bad response from Dir: %s\n", dir->msg);
92 Mmsg(&jcr->errmsg, _("Error scanning Dir response: %s\n"), dir->msg);
95 unbash_spaces(vol.VolCatName);
96 pm_strcpy(&jcr->VolumeName, vol.VolCatName); /* set desired VolumeName */
97 memcpy(&jcr->VolCatInfo, &vol, sizeof(jcr->VolCatInfo));
99 Dmsg2(200, "do_reqest_vol_info got slot=%d Volume=%s\n",
100 vol.Slot, vol.VolCatName);
106 * Get Volume info for a specific volume from the Director's Database
108 * Returns: 1 on success (not Director guarantees that Pool and MediaType
109 * are correct and VolStatus==Append or
110 * VolStatus==Recycle)
113 * Volume information returned in jcr
115 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing)
117 BSOCK *dir = jcr->dir_bsock;
119 bstrncpy(jcr->VolCatInfo.VolCatName, jcr->VolumeName, sizeof(jcr->VolCatInfo.VolCatName));
120 Dmsg1(200, "dir_get_volume_info=%s\n", jcr->VolCatInfo.VolCatName);
121 bash_spaces(jcr->VolCatInfo.VolCatName);
122 bnet_fsend(dir, Get_Vol_Info, jcr->Job, jcr->VolCatInfo.VolCatName,
123 writing==GET_VOL_INFO_FOR_WRITE?1:0);
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, DEVICE *dev, int label)
153 BSOCK *dir = jcr->dir_bsock;
154 time_t EndTime = time(NULL);
155 char ed1[50], ed2[50];
156 VOLUME_CAT_INFO *vol = &dev->VolCatInfo;
158 if (vol->VolCatName[0] == 0) {
159 Jmsg0(jcr, M_ERROR, 0, _("NULL Volume name. This shouldn't happen!!!\n"));
162 if (dev_state(dev, ST_READ)) {
163 Jmsg0(jcr, M_ERROR, 0, _("Attempt to update_volume_info in read mode!!!\n"));
166 if (!dev_state(dev, ST_LABEL)) {
167 Jmsg0(jcr, M_ERROR, 0, _("Attempt to update_volume_info on non-labeled Volume!!!\n"));
170 /* Just labeled or relabeled the tape */
172 bstrncpy(vol->VolCatStatus, "Append", sizeof(vol->VolCatStatus));
173 vol->VolCatBytes = 1; /* indicates tape labeled */
175 bash_spaces(vol->VolCatName);
176 bnet_fsend(dir, Update_media, jcr->Job,
177 vol->VolCatName, vol->VolCatJobs, vol->VolCatFiles,
178 vol->VolCatBlocks, edit_uint64(vol->VolCatBytes, ed1),
179 vol->VolCatMounts, vol->VolCatErrors,
180 vol->VolCatWrites, edit_uint64(vol->VolCatMaxBytes, ed2),
181 EndTime, vol->VolCatStatus, vol->Slot, label);
182 Dmsg1(120, "update_volume_data(): %s", dir->msg);
183 unbash_spaces(vol->VolCatName);
184 if (bnet_recv(dir) <= 0) {
185 Dmsg0(190, "updateVolCatInfo error bnet_recv\n");
186 Jmsg(jcr, M_ERROR, 0, _("Error updating Volume info Vol=\"%s\": ERR=%s\n"),
187 vol->VolCatName, bnet_strerror(dir));
190 Dmsg1(120, "Updatevol: %s", dir->msg);
191 if (strcmp(dir->msg, OK_update) != 0) {
192 Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
193 Jmsg(jcr, M_ERROR, 0, _("Error updating Volume info Vol=\"%s\": %s\n"),
194 vol->VolCatName, dir->msg);
201 * After writing a Volume, create the JobMedia record.
203 int dir_create_jobmedia_record(JCR *jcr)
205 BSOCK *dir = jcr->dir_bsock;
207 if (!jcr->WroteVol) {
208 return 1; /* nothing written to tape */
211 jcr->WroteVol = false;
212 bnet_fsend(dir, Create_job_media, jcr->Job,
213 jcr->VolFirstIndex, jcr->VolLastIndex,
214 jcr->StartFile, jcr->EndFile,
215 jcr->StartBlock, jcr->EndBlock);
216 Dmsg1(100, "create_jobmedia(): %s", dir->msg);
217 if (bnet_recv(dir) <= 0) {
218 Dmsg0(190, "create_jobmedia error bnet_recv\n");
219 Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: ERR=%s\n"),
223 Dmsg1(120, "Create_jobmedia: %s", dir->msg);
224 if (strcmp(dir->msg, OK_update) != 0) {
225 Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
226 Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: %s\n"), dir->msg);
234 * Update File Attribute data
236 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec)
238 BSOCK *dir = jcr->dir_bsock;
241 dir->msglen = sprintf(dir->msg, FileAttributes, jcr->Job);
242 dir->msg = check_pool_memory_size(dir->msg, dir->msglen +
243 sizeof(DEV_RECORD) + rec->data_len);
244 ser_begin(dir->msg + dir->msglen, 0);
245 ser_uint32(rec->VolSessionId);
246 ser_uint32(rec->VolSessionTime);
247 ser_int32(rec->FileIndex);
248 ser_int32(rec->Stream);
249 ser_uint32(rec->data_len);
250 ser_bytes(rec->data, rec->data_len);
251 dir->msglen = ser_length(dir->msg);
252 return bnet_send(dir);
258 * Entered with device blocked.
259 * Leaves with device blocked.
261 * Returns: 1 on success (operator issues a mount command)
263 * Note, must create dev->errmsg on error return.
265 * On success, jcr->VolumeName and jcr->VolCatInfo contain
266 * information on suggested volume, but this may not be the
267 * same as what is actually mounted.
269 * When we return with success, the correct tape may or may not
270 * actually be mounted. The calling routine must read it and
273 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev)
276 /* ******FIXME******* put these on config variable */
277 int min_wait = 60 * 60;
278 int max_wait = 24 * 60 * 60;
279 int max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
284 Dmsg0(130, "enter dir_ask_sysop_to_mount_next_volume\n");
285 ASSERT(dev->dev_blocked);
288 if (job_canceled(jcr)) {
289 Mmsg(&dev->errmsg, _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"),
290 jcr->Job, jcr->dev_name);
291 Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
294 if (dir_find_next_appendable_volume(jcr)) { /* get suggested volume */
295 jstat = JS_WaitMount;
297 * If we have a valid volume name and we are not
298 * removable media, return now, or if we have a
299 * Slot for an autochanger, otherwise wait
300 * for the operator to mount the media.
302 if ((jcr->VolumeName[0] && !dev_cap(dev, CAP_REM) && dev_cap(dev, CAP_LABEL)) ||
303 (jcr->VolumeName[0] && jcr->VolCatInfo.Slot)) {
304 Dmsg0(190, "Return 1 from mount without wait.\n");
307 Jmsg(jcr, M_MOUNT, 0, _(
308 "Please mount Volume \"%s\" on Storage Device \"%s\" for Job %s\n"
309 "Use \"mount\" command to release Job.\n"),
310 jcr->VolumeName, jcr->dev_name, jcr->Job);
311 Dmsg3(190, "Mount %s on %s for Job %s\n",
312 jcr->VolumeName, jcr->dev_name, jcr->Job);
314 jstat = JS_WaitMedia;
315 Jmsg(jcr, M_MOUNT, 0, _(
316 "Job %s waiting. Cannot find any appendable volumes.\n\
317 Please use the \"label\" command to create a new Volume for:\n\
327 jcr->JobStatus = jstat;
328 dir_send_job_status(jcr);
330 stat = wait_for_sysop(jcr, dev, wait_sec);
332 if (stat == ETIMEDOUT) {
333 wait_sec *= 2; /* double wait time */
334 if (wait_sec > max_wait) { /* but not longer than maxtime */
338 if (num_wait >= max_num_wait) {
339 Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"),
340 jcr->dev_name, jcr->Job);
341 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
342 Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
343 return 0; /* exceeded maximum waits */
347 if (stat == EINVAL) {
348 Mmsg2(&dev->errmsg, _("pthread error in mount_next_volume stat=%d ERR=%s\n"),
349 stat, strerror(stat));
350 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
354 Jmsg(jcr, M_WARNING, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
357 Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
359 /* Restart wait counters */
362 /* If no VolumeName, and cannot get one, try again */
363 if (jcr->VolumeName[0] == 0 &&
364 !dir_find_next_appendable_volume(jcr) && !job_canceled(jcr)) {
365 Jmsg(jcr, M_MOUNT, 0, _(
366 "Someone woke me up, but I cannot find any appendable\n\
367 volumes for Job=%s.\n"), jcr->Job);
372 set_jcr_job_status(jcr, JS_Running);
373 dir_send_job_status(jcr);
374 Dmsg0(130, "leave dir_ask_sysop_to_mount_next_volume\n");
380 * Entered with device blocked and jcr->VolumeName is desired
382 * Leaves with device blocked.
384 * Returns: 1 on success (operator issues a mount command)
386 * Note, must create dev->errmsg on error return.
389 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
392 /* ******FIXME******* put these on config variable */
393 int min_wait = 60 * 60;
394 int max_wait = 24 * 60 * 60;
395 int max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
400 Dmsg0(130, "enter dir_ask_sysop_to_mount_next_volume\n");
401 if (!jcr->VolumeName[0]) {
402 Mmsg0(&dev->errmsg, _("Cannot request another volume: no volume name given.\n"));
405 ASSERT(dev->dev_blocked);
408 if (job_canceled(jcr)) {
409 Mmsg(&dev->errmsg, _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"),
410 jcr->Job, jcr->dev_name);
413 msg = _("Please mount");
414 Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
415 msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
416 Dmsg3(190, "Mount %s on %s for Job %s\n",
417 jcr->VolumeName, jcr->dev_name, jcr->Job);
419 jcr->JobStatus = JS_WaitMount;
420 dir_send_job_status(jcr);
422 stat = wait_for_sysop(jcr, dev, wait_sec); /* wait on device */
424 if (stat == ETIMEDOUT) {
425 wait_sec *= 2; /* double wait time */
426 if (wait_sec > max_wait) { /* but not longer than maxtime */
430 if (num_wait >= max_num_wait) {
431 Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"),
432 jcr->dev_name, jcr->Job);
433 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
434 Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
435 return 0; /* exceeded maximum waits */
439 if (stat == EINVAL) {
440 Mmsg2(&dev->errmsg, _("pthread error in mount_volume stat=%d ERR=%s\n"),
441 stat, strerror(stat));
442 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
446 Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
449 Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
451 /* Restart wait counters */
456 set_jcr_job_status(jcr, JS_Running);
457 dir_send_job_status(jcr);
458 Dmsg0(130, "leave dir_ask_sysop_to_mount_next_volume\n");
463 * Wait for SysOp to mount a tape
465 static int wait_for_sysop(JCR *jcr, DEVICE *dev, int wait_sec)
469 struct timespec timeout;
471 time_t start = time(NULL);
472 time_t last_heartbeat = 0;
477 * Wait requested time (wait_sec). However, we also wake up every
478 * HB_TIME seconds and send a heartbeat to the FD and the Director
479 * to keep stateful firewalls from closing them down while waiting
483 if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
484 add_wait = me->heartbeat_interval;
486 gettimeofday(&tv, &tz);
487 timeout.tv_nsec = tv.tv_usec * 1000;
488 timeout.tv_sec = tv.tv_sec + add_wait;
491 dev_blocked = dev->dev_blocked;
492 dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
494 for ( ; !job_canceled(jcr); ) {
497 Dmsg3(100, "I'm going to sleep on device %s. HB=%d wait=%d\n", dev_name(dev),
498 (int)me->heartbeat_interval, wait_sec);
499 stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
500 Dmsg1(100, "Wokeup from sleep on device stat=%d\n", stat);
504 /* Note, this always triggers the first time. We want that. */
505 if (me->heartbeat_interval) {
506 if (now - last_heartbeat >= me->heartbeat_interval) {
507 /* send heartbeats */
508 if (jcr->file_bsock) {
509 bnet_sig(jcr->file_bsock, BNET_HEARTBEAT);
510 Dmsg0(100, "Send heartbeat to FD.\n");
512 if (jcr->dir_bsock) {
513 bnet_sig(jcr->dir_bsock, BNET_HEARTBEAT);
515 last_heartbeat = now;
519 if (dev->dev_blocked == BST_MOUNT) { /* mount request ? */
524 if (stat != ETIMEDOUT) { /* we blocked the device */
525 break; /* on error return */
527 if (now - start >= wait_sec) { /* on exceeding wait time return */
528 Dmsg0(100, "Exceed wait time.\n");
531 add_wait = wait_sec - (now - start);
532 if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
533 add_wait = me->heartbeat_interval;
535 gettimeofday(&tv, &tz);
536 timeout.tv_nsec = tv.tv_usec * 1000;
537 timeout.tv_sec = tv.tv_sec + add_wait; /* additional wait */
538 Dmsg1(100, "Additional wait %d sec.\n", add_wait);
541 dev->dev_blocked = dev_blocked;