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-2004 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);
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;
83 jcr->VolumeName[0] = 0; /* No volume */
84 dcr->VolumeName[0] = 0; /* No volume */
85 if (bnet_recv(dir) <= 0) {
86 Dmsg0(200, "getvolname error bnet_recv\n");
87 Mmsg(&jcr->errmsg, _("Network error on bnet_recv in req_vol_info.\n"));
90 memset(&vol, 0, sizeof(vol));
91 Dmsg1(200, "Get vol info=%s\n", dir->msg);
92 n = sscanf(dir->msg, OK_media, vol.VolCatName,
93 &vol.VolCatJobs, &vol.VolCatFiles,
94 &vol.VolCatBlocks, &vol.VolCatBytes,
95 &vol.VolCatMounts, &vol.VolCatErrors,
96 &vol.VolCatWrites, &vol.VolCatMaxBytes,
97 &vol.VolCatCapacityBytes, vol.VolCatStatus,
98 &vol.Slot, &vol.VolCatMaxJobs, &vol.VolCatMaxFiles,
99 &vol.InChanger, &vol.VolReadTime, &vol.VolWriteTime);
101 Dmsg2(100, "Bad response from Dir fields=%d: %s\n", n, dir->msg);
102 Mmsg(&jcr->errmsg, _("Error getting Volume info: %s\n"), dir->msg);
105 unbash_spaces(vol.VolCatName);
106 pm_strcpy(&jcr->VolumeName, vol.VolCatName); /* set desired VolumeName */
107 bstrncpy(dcr->VolumeName, vol.VolCatName, sizeof(dcr->VolumeName));
108 memcpy(&jcr->VolCatInfo, &vol, sizeof(jcr->VolCatInfo));
109 memcpy(&dcr->VolCatInfo, &vol, sizeof(dcr->VolCatInfo));
111 Dmsg2(200, "do_reqest_vol_info got slot=%d Volume=%s\n",
112 vol.Slot, vol.VolCatName);
118 * Get Volume info for a specific volume from the Director's Database
120 * Returns: 1 on success (not Director guarantees that Pool and MediaType
121 * are correct and VolStatus==Append or
122 * VolStatus==Recycle)
125 * Volume information returned in jcr
127 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing)
129 BSOCK *dir = jcr->dir_bsock;
131 bstrncpy(jcr->VolCatInfo.VolCatName, jcr->VolumeName, sizeof(jcr->VolCatInfo.VolCatName));
132 Dmsg1(200, "dir_get_volume_info=%s\n", jcr->VolCatInfo.VolCatName);
133 bash_spaces(jcr->VolCatInfo.VolCatName);
134 bnet_fsend(dir, Get_Vol_Info, jcr->Job, jcr->VolCatInfo.VolCatName,
135 writing==GET_VOL_INFO_FOR_WRITE?1:0);
136 return do_get_volume_info(jcr);
142 * Get info on the next appendable volume in the Director's database
143 * Returns: 1 on success
146 * Volume information returned in jcr
149 int dir_find_next_appendable_volume(JCR *jcr)
151 BSOCK *dir = jcr->dir_bsock;
154 Dmsg0(200, "dir_find_next_appendable_volume\n");
155 for (int vol_index=1; vol_index < 3; vol_index++) {
156 bnet_fsend(dir, Find_media, jcr->Job, vol_index);
157 if (do_get_volume_info(jcr)) {
158 Dmsg2(200, "JobId=%d got possible Vol=%s\n", jcr->JobId, jcr->VolumeName);
161 * Walk through all jobs and see if the volume is
162 * already mounted. If so, try a different one.
163 * This would be better done by walking through
169 free_locked_jcr(njcr);
172 Dmsg2(200, "Compare to JobId=%d using Vol=%s\n", njcr->JobId, njcr->VolumeName);
173 if (strcmp(jcr->VolumeName, njcr->VolumeName) == 0) {
175 Dmsg1(200, "Vol in use by JobId=%u\n", njcr->JobId);
176 free_locked_jcr(njcr);
179 free_locked_jcr(njcr);
183 Dmsg0(200, "dir_find_next_appendable_volume return true\n");
184 return true; /* Got good Volume */
187 Dmsg0(200, "No volume info, return false\n");
191 Dmsg0(200, "dir_find_next_appendable_volume return true\n");
197 * After writing a Volume, send the updated statistics
198 * back to the director.
200 int dir_update_volume_info(JCR *jcr, DEVICE *dev, int label)
202 BSOCK *dir = jcr->dir_bsock;
203 time_t LastWritten = time(NULL);
204 char ed1[50], ed2[50], ed3[50], ed4[50];
205 VOLUME_CAT_INFO *vol = &dev->VolCatInfo;
207 if (vol->VolCatName[0] == 0) {
208 Jmsg0(jcr, M_ERROR, 0, _("NULL Volume name. This shouldn't happen!!!\n"));
211 if (dev_state(dev, ST_READ)) {
212 Jmsg0(jcr, M_ERROR, 0, _("Attempt to update_volume_info in read mode!!!\n"));
215 if (!dev_state(dev, ST_LABEL)) {
216 Jmsg0(jcr, M_ERROR, 0, _("Attempt to update_volume_info on non-labeled Volume!!!\n"));
220 Dmsg1(100, "Update cat VolFiles=%d\n", dev->file);
221 /* Just labeled or relabeled the tape */
223 bstrncpy(vol->VolCatStatus, "Append", sizeof(vol->VolCatStatus));
224 vol->VolCatBytes = 1; /* indicates tape labeled */
226 bash_spaces(vol->VolCatName);
227 bnet_fsend(dir, Update_media, jcr->Job,
228 vol->VolCatName, vol->VolCatJobs, vol->VolCatFiles,
229 vol->VolCatBlocks, edit_uint64(vol->VolCatBytes, ed1),
230 vol->VolCatMounts, vol->VolCatErrors,
231 vol->VolCatWrites, edit_uint64(vol->VolCatMaxBytes, ed2),
232 LastWritten, vol->VolCatStatus, vol->Slot, label,
234 edit_uint64(vol->VolReadTime, ed3),
235 edit_uint64(vol->VolWriteTime, ed4) );
237 Dmsg1(120, "update_volume_info(): %s", dir->msg);
238 unbash_spaces(vol->VolCatName);
240 if (!do_get_volume_info(jcr)) {
241 Jmsg(jcr, M_ERROR, 0, "%s", jcr->errmsg);
244 Dmsg1(120, "get_volume_info(): %s", dir->msg);
245 /* Update dev Volume info in case something changed (e.g. expired) */
246 memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(dev->VolCatInfo));
251 * After writing a Volume, create the JobMedia record.
253 int dir_create_jobmedia_record(JCR *jcr)
255 BSOCK *dir = jcr->dir_bsock;
258 if (!dcr->WroteVol) {
259 return 1; /* nothing written to tape */
262 dcr->WroteVol = false;
263 bnet_fsend(dir, Create_job_media, jcr->Job,
264 dcr->VolFirstIndex, dcr->VolLastIndex,
265 dcr->StartFile, dcr->EndFile,
266 dcr->StartBlock, dcr->EndBlock);
267 Dmsg1(100, "create_jobmedia(): %s", dir->msg);
268 if (bnet_recv(dir) <= 0) {
269 Dmsg0(190, "create_jobmedia error bnet_recv\n");
270 Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: ERR=%s\n"),
274 Dmsg1(120, "Create_jobmedia: %s", dir->msg);
275 if (strcmp(dir->msg, OK_create) != 0) {
276 Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
277 Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: %s\n"), dir->msg);
285 * Update File Attribute data
287 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec)
289 BSOCK *dir = jcr->dir_bsock;
292 dir->msglen = sprintf(dir->msg, FileAttributes, jcr->Job);
293 dir->msg = check_pool_memory_size(dir->msg, dir->msglen +
294 sizeof(DEV_RECORD) + rec->data_len);
295 ser_begin(dir->msg + dir->msglen, 0);
296 ser_uint32(rec->VolSessionId);
297 ser_uint32(rec->VolSessionTime);
298 ser_int32(rec->FileIndex);
299 ser_int32(rec->Stream);
300 ser_uint32(rec->data_len);
301 ser_bytes(rec->data, rec->data_len);
302 dir->msglen = ser_length(dir->msg);
303 return bnet_send(dir);
308 * Request the sysop to create an appendable volume
310 * Entered with device blocked.
311 * Leaves with device blocked.
313 * Returns: 1 on success (operator issues a mount command)
315 * Note, must create dev->errmsg on error return.
317 * On success, jcr->VolumeName and jcr->VolCatInfo contain
318 * information on suggested volume, but this may not be the
319 * same as what is actually mounted.
321 * When we return with success, the correct tape may or may not
322 * actually be mounted. The calling routine must read it and
325 int dir_ask_sysop_to_create_appendable_volume(JCR *jcr, DEVICE *dev)
331 Dmsg0(130, "enter dir_ask_sysop_to_create_appendable_volume\n");
332 ASSERT(dev->dev_blocked);
334 if (job_canceled(jcr)) {
336 _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"),
337 jcr->Job, jcr->dev_name);
338 Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
341 /* First pass, we *know* there are no appendable volumes, so no need to call */
342 if (!first && dir_find_next_appendable_volume(jcr)) { /* get suggested volume */
343 jstat = JS_WaitMount;
344 unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
345 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
347 * If we have a valid volume name and we are not
348 * removable media, return now, or if we have a
349 * Slot for an autochanger, otherwise wait
350 * for the operator to mount the media.
352 if (!unmounted && ((jcr->VolumeName[0] && !dev_cap(dev, CAP_REM) &&
353 dev_cap(dev, CAP_LABEL)) ||
354 (jcr->VolumeName[0] && jcr->VolCatInfo.Slot))) {
355 Dmsg0(100, "Return 1 from mount without wait.\n");
359 Jmsg(jcr, M_MOUNT, 0, _(
360 "Please mount Volume \"%s\" on Storage Device \"%s\" for Job %s\n"
361 "Use \"mount\" command to release Job.\n"),
362 jcr->VolumeName, jcr->dev_name, jcr->Job);
363 Dmsg3(190, "Mount %s on %s for Job %s\n",
364 jcr->VolumeName, jcr->dev_name, jcr->Job);
367 jstat = JS_WaitMedia;
369 Jmsg(jcr, M_MOUNT, 0, _(
370 "Job %s waiting. Cannot find any appendable volumes.\n\
371 Please use the \"label\" command to create a new Volume for:\n\
383 jcr->JobStatus = jstat;
384 dir_send_job_status(jcr);
386 stat = wait_for_sysop(jcr, dev);
388 Dmsg1(200, "Poll timeout in create append vol on device %s\n", dev_name(dev));
392 if (stat == ETIMEDOUT) {
393 if (!double_dev_wait_time(dev)) {
394 Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"),
395 dev_name(dev), jcr->Job);
396 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
397 Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
398 return 0; /* exceeded maximum waits */
402 if (stat == EINVAL) {
403 Mmsg2(&dev->errmsg, _("pthread error in mount_next_volume stat=%d ERR=%s\n"),
404 stat, strerror(stat));
405 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
409 Jmsg(jcr, M_WARNING, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
412 Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
414 /* If no VolumeName, and cannot get one, try again */
415 if (jcr->VolumeName[0] == 0 && !job_canceled(jcr) &&
416 !dir_find_next_appendable_volume(jcr)) {
417 Jmsg(jcr, M_MOUNT, 0, _(
418 "Someone woke me up, but I cannot find any appendable\n\
419 volumes for Job=%s.\n"), jcr->Job);
420 /* Restart wait counters after user interaction */
421 init_dev_wait_timers(dev);
424 unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
425 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
427 continue; /* continue to wait */
431 * Device mounted, we have a volume, break and return
435 set_jcr_job_status(jcr, JS_Running);
436 dir_send_job_status(jcr);
437 Dmsg0(130, "leave dir_ask_sysop_to_mount_create_appendable_volume\n");
442 * Request to mount specific Volume
444 * Entered with device blocked and jcr->VolumeName is desired
446 * Leaves with device blocked.
448 * Returns: 1 on success (operator issues a mount command)
450 * Note, must create dev->errmsg on error return.
453 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
458 Dmsg0(130, "enter dir_ask_sysop_to_mount_volume\n");
459 if (!jcr->VolumeName[0]) {
460 Mmsg0(&dev->errmsg, _("Cannot request another volume: no volume name given.\n"));
463 ASSERT(dev->dev_blocked);
465 if (job_canceled(jcr)) {
466 Mmsg(&dev->errmsg, _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"),
467 jcr->Job, jcr->dev_name);
472 * If we have a valid volume name and we are not
473 * removable media, return now, or if we have a
474 * Slot for an autochanger, otherwise wait
475 * for the operator to mount the media.
477 if ((jcr->VolumeName[0] && !dev_cap(dev, CAP_REM) && dev_cap(dev, CAP_LABEL)) ||
478 (jcr->VolumeName[0] && jcr->VolCatInfo.Slot)) {
479 Dmsg0(100, "Return 1 from mount without wait.\n");
484 msg = _("Please mount");
485 Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
486 msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
487 Dmsg3(190, "Mount %s on %s for Job %s\n",
488 jcr->VolumeName, jcr->dev_name, jcr->Job);
491 jcr->JobStatus = JS_WaitMount;
492 dir_send_job_status(jcr);
494 stat = wait_for_sysop(jcr, dev); /* wait on device */
496 Dmsg1(200, "Poll timeout in mount vol on device %s\n", dev_name(dev));
497 Dmsg1(200, "Blocked=%d\n", dev->dev_blocked);
501 if (stat == ETIMEDOUT) {
502 if (!double_dev_wait_time(dev)) {
503 Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"),
504 dev_name(dev), jcr->Job);
505 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
506 Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
507 return 0; /* exceeded maximum waits */
511 if (stat == EINVAL) {
512 Mmsg2(&dev->errmsg, _("pthread error in mount_volume stat=%d ERR=%s\n"),
513 stat, strerror(stat));
514 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
518 Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
521 Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
524 set_jcr_job_status(jcr, JS_Running);
525 dir_send_job_status(jcr);
526 Dmsg0(130, "leave dir_ask_sysop_to_mount_volume\n");
531 * Wait for SysOp to mount a tape
533 static int wait_for_sysop(JCR *jcr, DEVICE *dev)
537 struct timespec timeout;
538 time_t last_heartbeat = 0;
539 time_t first_start = time(NULL);
545 unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
546 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
550 * Wait requested time (dev->rem_wait_sec). However, we also wake up every
551 * HB_TIME seconds and send a heartbeat to the FD and the Director
552 * to keep stateful firewalls from closing them down while waiting
555 add_wait = dev->rem_wait_sec;
556 if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
557 add_wait = me->heartbeat_interval;
559 if (!unmounted && dev->vol_poll_interval && add_wait > dev->vol_poll_interval) {
560 add_wait = dev->vol_poll_interval;
562 gettimeofday(&tv, &tz);
563 timeout.tv_nsec = tv.tv_usec * 1000;
564 timeout.tv_sec = tv.tv_sec + add_wait;
567 dev->dev_prev_blocked = dev->dev_blocked;
568 dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
571 for ( ; !job_canceled(jcr); ) {
574 Dmsg3(100, "I'm going to sleep on device %s. HB=%d wait=%d\n", dev_name(dev),
575 (int)me->heartbeat_interval, dev->wait_sec);
577 stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
578 Dmsg1(100, "Wokeup from sleep on device stat=%d\n", stat);
581 dev->rem_wait_sec -= (now - start);
583 /* Note, this always triggers the first time. We want that. */
584 if (me->heartbeat_interval) {
585 if (now - last_heartbeat >= me->heartbeat_interval) {
586 /* send heartbeats */
587 if (jcr->file_bsock) {
588 bnet_sig(jcr->file_bsock, BNET_HEARTBEAT);
589 Dmsg0(100, "Send heartbeat to FD.\n");
591 if (jcr->dir_bsock) {
592 bnet_sig(jcr->dir_bsock, BNET_HEARTBEAT);
594 last_heartbeat = now;
599 * Check if user unmounted the device while we were waiting
601 unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
602 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
604 if (stat != ETIMEDOUT) { /* we blocked the device */
605 break; /* on error return */
607 if (dev->rem_wait_sec <= 0) { /* on exceeding wait time return */
608 Dmsg0(100, "Exceed wait time.\n");
612 if (!unmounted && dev->vol_poll_interval &&
613 (now - first_start >= dev->vol_poll_interval)) {
614 Dmsg1(200, "In wait blocked=%d\n", dev->dev_blocked);
619 * Check if user mounted the device while we were waiting
621 if (dev->dev_blocked == BST_MOUNT) { /* mount request ? */
626 add_wait = dev->wait_sec - (now - start);
630 if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
631 add_wait = me->heartbeat_interval;
633 gettimeofday(&tv, &tz);
634 timeout.tv_nsec = tv.tv_usec * 1000;
635 timeout.tv_sec = tv.tv_sec + add_wait; /* additional wait */
636 Dmsg1(100, "Additional wait %d sec.\n", add_wait);
640 dev->dev_blocked = dev->dev_prev_blocked; /* restore entry state */