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 InChanger=%d VolReadTime=%s VolWriteTime=%s\n";
39 static char Create_job_media[] = "CatReq Job=%s CreateJobMedia"
40 " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u"
41 " StartBlock=%u EndBlock=%u\n";
42 static char FileAttributes[] = "UpdCat Job=%s FileAttributes ";
43 static char Job_status[] = "3012 Job %s jobstatus %d\n";
46 /* Responses received from the Director */
47 static char OK_media[] = "1000 OK VolName=%127s VolJobs=%u VolFiles=%u"
48 " VolBlocks=%u VolBytes=%" lld " VolMounts=%u VolErrors=%u VolWrites=%u"
49 " MaxVolBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%20s"
50 " Slot=%d MaxVolJobs=%u MaxVolFiles=%u InChanger=%d"
51 " VolReadTime=%" lld " VolWriteTime=%" lld;
54 static char OK_create[] = "1000 OK CreateJobMedia\n";
56 /* Forward referenced functions */
57 static int wait_for_sysop(JCR *jcr, DEVICE *dev, int wait_sec);
60 * Send current JobStatus to Director
62 int dir_send_job_status(JCR *jcr)
64 return bnet_fsend(jcr->dir_bsock, Job_status, jcr->Job, jcr->JobStatus);
69 * dir_get_volume_info()
71 * dir_find_next_appendable_volume()
73 * Returns: 1 on success and vol info in jcr->VolCatInfo
76 static int do_get_volume_info(JCR *jcr)
78 BSOCK *dir = jcr->dir_bsock;
82 jcr->VolumeName[0] = 0; /* No volume */
83 if (bnet_recv(dir) <= 0) {
84 Dmsg0(200, "getvolname error bnet_recv\n");
85 Mmsg(&jcr->errmsg, _("Network error on bnet_recv in req_vol_info.\n"));
88 memset(&vol, 0, sizeof(vol));
89 Dmsg1(200, "Get vol info=%s\n", dir->msg);
90 n = sscanf(dir->msg, OK_media, vol.VolCatName,
91 &vol.VolCatJobs, &vol.VolCatFiles,
92 &vol.VolCatBlocks, &vol.VolCatBytes,
93 &vol.VolCatMounts, &vol.VolCatErrors,
94 &vol.VolCatWrites, &vol.VolCatMaxBytes,
95 &vol.VolCatCapacityBytes, vol.VolCatStatus,
96 &vol.Slot, &vol.VolCatMaxJobs, &vol.VolCatMaxFiles,
97 &vol.InChanger, &vol.VolReadTime, &vol.VolWriteTime);
99 Dmsg2(100, "Bad response from Dir fields=%d: %s\n", n, dir->msg);
100 Mmsg(&jcr->errmsg, _("Error getting Volume info: %s\n"), dir->msg);
103 unbash_spaces(vol.VolCatName);
104 pm_strcpy(&jcr->VolumeName, vol.VolCatName); /* set desired VolumeName */
105 memcpy(&jcr->VolCatInfo, &vol, sizeof(jcr->VolCatInfo));
107 Dmsg2(200, "do_reqest_vol_info got slot=%d Volume=%s\n",
108 vol.Slot, vol.VolCatName);
114 * Get Volume info for a specific volume from the Director's Database
116 * Returns: 1 on success (not Director guarantees that Pool and MediaType
117 * are correct and VolStatus==Append or
118 * VolStatus==Recycle)
121 * Volume information returned in jcr
123 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing)
125 BSOCK *dir = jcr->dir_bsock;
127 bstrncpy(jcr->VolCatInfo.VolCatName, jcr->VolumeName, sizeof(jcr->VolCatInfo.VolCatName));
128 Dmsg1(200, "dir_get_volume_info=%s\n", jcr->VolCatInfo.VolCatName);
129 bash_spaces(jcr->VolCatInfo.VolCatName);
130 bnet_fsend(dir, Get_Vol_Info, jcr->Job, jcr->VolCatInfo.VolCatName,
131 writing==GET_VOL_INFO_FOR_WRITE?1:0);
132 return do_get_volume_info(jcr);
138 * Get info on the next appendable volume in the Director's database
139 * Returns: 1 on success
142 * Volume information returned in jcr
145 int dir_find_next_appendable_volume(JCR *jcr)
147 BSOCK *dir = jcr->dir_bsock;
149 Dmsg0(200, "dir_find_next_appendable_volume\n");
150 bnet_fsend(dir, Find_media, jcr->Job, 1);
151 return do_get_volume_info(jcr);
156 * After writing a Volume, send the updated statistics
157 * back to the director.
159 int dir_update_volume_info(JCR *jcr, DEVICE *dev, int label)
161 BSOCK *dir = jcr->dir_bsock;
162 time_t EndTime = time(NULL);
163 char ed1[50], ed2[50], ed3[50], ed4[50];
164 VOLUME_CAT_INFO *vol = &dev->VolCatInfo;
166 if (vol->VolCatName[0] == 0) {
167 Jmsg0(jcr, M_ERROR, 0, _("NULL Volume name. This shouldn't happen!!!\n"));
170 if (dev_state(dev, ST_READ)) {
171 Jmsg0(jcr, M_ERROR, 0, _("Attempt to update_volume_info in read mode!!!\n"));
174 if (!dev_state(dev, ST_LABEL)) {
175 Jmsg0(jcr, M_ERROR, 0, _("Attempt to update_volume_info on non-labeled Volume!!!\n"));
179 Dmsg1(100, "Update cat VolFiles=%d\n", dev->file);
180 /* Just labeled or relabeled the tape */
182 bstrncpy(vol->VolCatStatus, "Append", sizeof(vol->VolCatStatus));
183 vol->VolCatBytes = 1; /* indicates tape labeled */
185 bash_spaces(vol->VolCatName);
186 bnet_fsend(dir, Update_media, jcr->Job,
187 vol->VolCatName, vol->VolCatJobs, vol->VolCatFiles,
188 vol->VolCatBlocks, edit_uint64(vol->VolCatBytes, ed1),
189 vol->VolCatMounts, vol->VolCatErrors,
190 vol->VolCatWrites, edit_uint64(vol->VolCatMaxBytes, ed2),
191 EndTime, vol->VolCatStatus, vol->Slot, label,
193 edit_uint64(vol->VolReadTime, ed3),
194 edit_uint64(vol->VolWriteTime, ed4) );
196 Dmsg1(120, "update_volume_info(): %s", dir->msg);
197 unbash_spaces(vol->VolCatName);
199 if (!do_get_volume_info(jcr)) {
200 Jmsg(jcr, M_ERROR, 0, "%s", jcr->errmsg);
203 Dmsg1(120, "get_volume_info(): %s", dir->msg);
204 /* Update dev Volume info in case something changed (e.g. expired) */
205 memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(dev->VolCatInfo));
210 * After writing a Volume, create the JobMedia record.
212 int dir_create_jobmedia_record(JCR *jcr)
214 BSOCK *dir = jcr->dir_bsock;
216 if (!jcr->WroteVol) {
217 return 1; /* nothing written to tape */
220 jcr->WroteVol = false;
221 bnet_fsend(dir, Create_job_media, jcr->Job,
222 jcr->VolFirstIndex, jcr->VolLastIndex,
223 jcr->StartFile, jcr->EndFile,
224 jcr->StartBlock, jcr->EndBlock);
225 Dmsg1(100, "create_jobmedia(): %s", dir->msg);
226 if (bnet_recv(dir) <= 0) {
227 Dmsg0(190, "create_jobmedia error bnet_recv\n");
228 Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: ERR=%s\n"),
232 Dmsg1(120, "Create_jobmedia: %s", dir->msg);
233 if (strcmp(dir->msg, OK_create) != 0) {
234 Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
235 Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: %s\n"), dir->msg);
243 * Update File Attribute data
245 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec)
247 BSOCK *dir = jcr->dir_bsock;
250 dir->msglen = sprintf(dir->msg, FileAttributes, jcr->Job);
251 dir->msg = check_pool_memory_size(dir->msg, dir->msglen +
252 sizeof(DEV_RECORD) + rec->data_len);
253 ser_begin(dir->msg + dir->msglen, 0);
254 ser_uint32(rec->VolSessionId);
255 ser_uint32(rec->VolSessionTime);
256 ser_int32(rec->FileIndex);
257 ser_int32(rec->Stream);
258 ser_uint32(rec->data_len);
259 ser_bytes(rec->data, rec->data_len);
260 dir->msglen = ser_length(dir->msg);
261 return bnet_send(dir);
266 * Request to mount next Volume, which Volume not specified
268 * Entered with device blocked.
269 * Leaves with device blocked.
271 * Returns: 1 on success (operator issues a mount command)
273 * Note, must create dev->errmsg on error return.
275 * On success, jcr->VolumeName and jcr->VolCatInfo contain
276 * information on suggested volume, but this may not be the
277 * same as what is actually mounted.
279 * When we return with success, the correct tape may or may not
280 * actually be mounted. The calling routine must read it and
283 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev)
286 /* ******FIXME******* put these on config variable */
287 int min_wait = 60 * 60;
288 int max_wait = 24 * 60 * 60;
289 int max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
294 Dmsg0(130, "enter dir_ask_sysop_to_mount_next_volume\n");
295 ASSERT(dev->dev_blocked);
298 if (job_canceled(jcr)) {
299 Mmsg(&dev->errmsg, _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"),
300 jcr->Job, jcr->dev_name);
301 Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
304 if (dir_find_next_appendable_volume(jcr)) { /* get suggested volume */
305 jstat = JS_WaitMount;
307 * If we have a valid volume name and we are not
308 * removable media, return now, or if we have a
309 * Slot for an autochanger, otherwise wait
310 * for the operator to mount the media.
312 if ((jcr->VolumeName[0] && !dev_cap(dev, CAP_REM) && dev_cap(dev, CAP_LABEL)) ||
313 (jcr->VolumeName[0] && jcr->VolCatInfo.Slot)) {
314 Dmsg0(100, "Return 1 from mount without wait.\n");
317 Jmsg(jcr, M_MOUNT, 0, _(
318 "Please mount Volume \"%s\" on Storage Device \"%s\" for Job %s\n"
319 "Use \"mount\" command to release Job.\n"),
320 jcr->VolumeName, jcr->dev_name, jcr->Job);
321 Dmsg3(190, "Mount %s on %s for Job %s\n",
322 jcr->VolumeName, jcr->dev_name, jcr->Job);
324 jstat = JS_WaitMedia;
325 Jmsg(jcr, M_MOUNT, 0, _(
326 "Job %s waiting. Cannot find any appendable volumes.\n\
327 Please use the \"label\" command to create a new Volume for:\n\
337 jcr->JobStatus = jstat;
338 dir_send_job_status(jcr);
340 stat = wait_for_sysop(jcr, dev, wait_sec);
342 if (stat == ETIMEDOUT) {
343 wait_sec *= 2; /* double wait time */
344 if (wait_sec > max_wait) { /* but not longer than maxtime */
348 if (num_wait >= max_num_wait) {
349 Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"),
350 jcr->dev_name, jcr->Job);
351 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
352 Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
353 return 0; /* exceeded maximum waits */
357 if (stat == EINVAL) {
358 Mmsg2(&dev->errmsg, _("pthread error in mount_next_volume stat=%d ERR=%s\n"),
359 stat, strerror(stat));
360 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
364 Jmsg(jcr, M_WARNING, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
367 Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
369 /* Restart wait counters */
372 /* If no VolumeName, and cannot get one, try again */
373 if (jcr->VolumeName[0] == 0 && !job_canceled(jcr) &&
374 !dir_find_next_appendable_volume(jcr)) {
375 Jmsg(jcr, M_MOUNT, 0, _(
376 "Someone woke me up, but I cannot find any appendable\n\
377 volumes for Job=%s.\n"), jcr->Job);
382 set_jcr_job_status(jcr, JS_Running);
383 dir_send_job_status(jcr);
384 Dmsg0(130, "leave dir_ask_sysop_to_mount_next_volume\n");
389 * Request to mount specific Volume
391 * Entered with device blocked and jcr->VolumeName is desired
393 * Leaves with device blocked.
395 * Returns: 1 on success (operator issues a mount command)
397 * Note, must create dev->errmsg on error return.
400 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
403 /* ******FIXME******* put these on config variable */
404 int min_wait = 60 * 60;
405 int max_wait = 24 * 60 * 60;
406 int max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
411 Dmsg0(130, "enter dir_ask_sysop_to_mount_next_volume\n");
412 if (!jcr->VolumeName[0]) {
413 Mmsg0(&dev->errmsg, _("Cannot request another volume: no volume name given.\n"));
416 ASSERT(dev->dev_blocked);
419 if (job_canceled(jcr)) {
420 Mmsg(&dev->errmsg, _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"),
421 jcr->Job, jcr->dev_name);
424 msg = _("Please mount");
425 Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
426 msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
427 Dmsg3(190, "Mount %s on %s for Job %s\n",
428 jcr->VolumeName, jcr->dev_name, jcr->Job);
430 jcr->JobStatus = JS_WaitMount;
431 dir_send_job_status(jcr);
433 stat = wait_for_sysop(jcr, dev, wait_sec); /* wait on device */
435 if (stat == ETIMEDOUT) {
436 wait_sec *= 2; /* double wait time */
437 if (wait_sec > max_wait) { /* but not longer than maxtime */
441 if (num_wait >= max_num_wait) {
442 Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"),
443 jcr->dev_name, jcr->Job);
444 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
445 Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
446 return 0; /* exceeded maximum waits */
450 if (stat == EINVAL) {
451 Mmsg2(&dev->errmsg, _("pthread error in mount_volume stat=%d ERR=%s\n"),
452 stat, strerror(stat));
453 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
457 Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
460 Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
462 /* Restart wait counters */
467 set_jcr_job_status(jcr, JS_Running);
468 dir_send_job_status(jcr);
469 Dmsg0(130, "leave dir_ask_sysop_to_mount_next_volume\n");
474 * Wait for SysOp to mount a tape
476 static int wait_for_sysop(JCR *jcr, DEVICE *dev, int wait_sec)
480 struct timespec timeout;
482 time_t start = time(NULL);
483 time_t last_heartbeat = 0;
488 * Wait requested time (wait_sec). However, we also wake up every
489 * HB_TIME seconds and send a heartbeat to the FD and the Director
490 * to keep stateful firewalls from closing them down while waiting
494 if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
495 add_wait = me->heartbeat_interval;
497 gettimeofday(&tv, &tz);
498 timeout.tv_nsec = tv.tv_usec * 1000;
499 timeout.tv_sec = tv.tv_sec + add_wait;
502 dev_blocked = dev->dev_blocked;
503 dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
505 for ( ; !job_canceled(jcr); ) {
508 Dmsg3(100, "I'm going to sleep on device %s. HB=%d wait=%d\n", dev_name(dev),
509 (int)me->heartbeat_interval, wait_sec);
510 stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
511 Dmsg1(100, "Wokeup from sleep on device stat=%d\n", stat);
515 /* Note, this always triggers the first time. We want that. */
516 if (me->heartbeat_interval) {
517 if (now - last_heartbeat >= me->heartbeat_interval) {
518 /* send heartbeats */
519 if (jcr->file_bsock) {
520 bnet_sig(jcr->file_bsock, BNET_HEARTBEAT);
521 Dmsg0(100, "Send heartbeat to FD.\n");
523 if (jcr->dir_bsock) {
524 bnet_sig(jcr->dir_bsock, BNET_HEARTBEAT);
526 last_heartbeat = now;
530 if (dev->dev_blocked == BST_MOUNT) { /* mount request ? */
535 if (stat != ETIMEDOUT) { /* we blocked the device */
536 break; /* on error return */
538 if (now - start >= wait_sec) { /* on exceeding wait time return */
539 Dmsg0(100, "Exceed wait time.\n");
542 add_wait = wait_sec - (now - start);
543 if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
544 add_wait = me->heartbeat_interval;
546 gettimeofday(&tv, &tz);
547 timeout.tv_nsec = tv.tv_usec * 1000;
548 timeout.tv_sec = tv.tv_sec + add_wait; /* additional wait */
549 Dmsg1(100, "Additional wait %d sec.\n", add_wait);
552 dev->dev_blocked = dev_blocked; /* restore entry state */