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;
84 jcr->VolumeName[0] = 0; /* No volume */
85 dcr->VolumeName[0] = 0; /* No volume */
86 if (bnet_recv(dir) <= 0) {
87 Dmsg0(200, "getvolname error bnet_recv\n");
88 Mmsg(&jcr->errmsg, _("Network error on bnet_recv in req_vol_info.\n"));
91 memset(&vol, 0, sizeof(vol));
92 Dmsg1(200, "Get vol info=%s\n", dir->msg);
93 n = sscanf(dir->msg, OK_media, vol.VolCatName,
94 &vol.VolCatJobs, &vol.VolCatFiles,
95 &vol.VolCatBlocks, &vol.VolCatBytes,
96 &vol.VolCatMounts, &vol.VolCatErrors,
97 &vol.VolCatWrites, &vol.VolCatMaxBytes,
98 &vol.VolCatCapacityBytes, vol.VolCatStatus,
99 &vol.Slot, &vol.VolCatMaxJobs, &vol.VolCatMaxFiles,
100 &InChanger, &vol.VolReadTime, &vol.VolWriteTime);
102 Dmsg2(100, "Bad response from Dir fields=%d: %s\n", n, dir->msg);
103 Mmsg(&jcr->errmsg, _("Error getting Volume info: %s\n"), dir->msg);
106 vol.InChanger = InChanger; /* bool in structure */
107 unbash_spaces(vol.VolCatName);
108 pm_strcpy(&jcr->VolumeName, vol.VolCatName); /* set desired VolumeName */
109 bstrncpy(dcr->VolumeName, vol.VolCatName, sizeof(dcr->VolumeName));
110 memcpy(&jcr->VolCatInfo, &vol, sizeof(jcr->VolCatInfo));
111 memcpy(&dcr->VolCatInfo, &vol, sizeof(dcr->VolCatInfo));
113 Dmsg2(200, "do_reqest_vol_info got slot=%d Volume=%s\n",
114 vol.Slot, vol.VolCatName);
120 * Get Volume info for a specific volume from the Director's Database
122 * Returns: 1 on success (not Director guarantees that Pool and MediaType
123 * are correct and VolStatus==Append or
124 * VolStatus==Recycle)
127 * Volume information returned in jcr
129 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing)
131 BSOCK *dir = jcr->dir_bsock;
133 bstrncpy(jcr->VolCatInfo.VolCatName, jcr->VolumeName, sizeof(jcr->VolCatInfo.VolCatName));
134 Dmsg1(200, "dir_get_volume_info=%s\n", jcr->VolCatInfo.VolCatName);
135 bash_spaces(jcr->VolCatInfo.VolCatName);
136 bnet_fsend(dir, Get_Vol_Info, jcr->Job, jcr->VolCatInfo.VolCatName,
137 writing==GET_VOL_INFO_FOR_WRITE?1:0);
138 return do_get_volume_info(jcr);
144 * Get info on the next appendable volume in the Director's database
145 * Returns: 1 on success
148 * Volume information returned in jcr
151 int dir_find_next_appendable_volume(JCR *jcr)
153 BSOCK *dir = jcr->dir_bsock;
156 Dmsg0(200, "dir_find_next_appendable_volume\n");
157 for (int vol_index=1; vol_index < 3; vol_index++) {
158 bnet_fsend(dir, Find_media, jcr->Job, vol_index);
159 if (do_get_volume_info(jcr)) {
160 Dmsg2(200, "JobId=%d got possible Vol=%s\n", jcr->JobId, jcr->VolumeName);
163 * Walk through all jobs and see if the volume is
164 * already mounted. If so, try a different one.
165 * This would be better done by walking through
171 free_locked_jcr(njcr);
174 Dmsg2(200, "Compare to JobId=%d using Vol=%s\n", njcr->JobId, njcr->VolumeName);
175 if (strcmp(jcr->VolumeName, njcr->VolumeName) == 0) {
177 Dmsg1(200, "Vol in use by JobId=%u\n", njcr->JobId);
178 free_locked_jcr(njcr);
181 free_locked_jcr(njcr);
185 Dmsg0(200, "dir_find_next_appendable_volume return true\n");
186 return true; /* Got good Volume */
189 Dmsg0(200, "No volume info, return false\n");
193 Dmsg0(200, "dir_find_next_appendable_volume return true\n");
199 * After writing a Volume, send the updated statistics
200 * back to the director. The information comes from the
203 int dir_update_volume_info(JCR *jcr, DEVICE *dev, int label)
205 BSOCK *dir = jcr->dir_bsock;
206 time_t LastWritten = time(NULL);
207 char ed1[50], ed2[50], ed3[50], ed4[50];
208 VOLUME_CAT_INFO *vol = &dev->VolCatInfo;
211 if (vol->VolCatName[0] == 0) {
212 Jmsg0(jcr, M_ERROR, 0, _("NULL Volume name. This shouldn't happen!!!\n"));
215 if (dev_state(dev, ST_READ)) {
216 Jmsg0(jcr, M_ERROR, 0, _("Attempt to update_volume_info in read mode!!!\n"));
219 if (!dev_state(dev, ST_LABEL)) {
220 Jmsg0(jcr, M_ERROR, 0, _("Attempt to update_volume_info on non-labeled Volume!!!\n"));
224 Dmsg1(100, "Update cat VolFiles=%d\n", dev->file);
225 /* Just labeled or relabeled the tape */
227 bstrncpy(vol->VolCatStatus, "Append", sizeof(vol->VolCatStatus));
228 vol->VolCatBytes = 1; /* indicates tape labeled */
230 bash_spaces(vol->VolCatName);
231 InChanger = vol->InChanger;
232 bnet_fsend(dir, Update_media, jcr->Job,
233 vol->VolCatName, vol->VolCatJobs, vol->VolCatFiles,
234 vol->VolCatBlocks, edit_uint64(vol->VolCatBytes, ed1),
235 vol->VolCatMounts, vol->VolCatErrors,
236 vol->VolCatWrites, edit_uint64(vol->VolCatMaxBytes, ed2),
237 LastWritten, vol->VolCatStatus, vol->Slot, label,
238 InChanger, /* bool in structure */
239 edit_uint64(vol->VolReadTime, ed3),
240 edit_uint64(vol->VolWriteTime, ed4) );
242 Dmsg1(120, "update_volume_info(): %s", dir->msg);
243 unbash_spaces(vol->VolCatName);
245 if (!do_get_volume_info(jcr)) {
246 Jmsg(jcr, M_ERROR, 0, "%s", jcr->errmsg);
249 Dmsg1(120, "get_volume_info(): %s", dir->msg);
250 /* Update dev Volume info in case something changed (e.g. expired) */
251 memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(dev->VolCatInfo));
256 * After writing a Volume, create the JobMedia record.
258 int dir_create_jobmedia_record(JCR *jcr)
260 BSOCK *dir = jcr->dir_bsock;
263 if (!dcr->WroteVol) {
264 return 1; /* nothing written to tape */
267 dcr->WroteVol = false;
268 bnet_fsend(dir, Create_job_media, jcr->Job,
269 dcr->VolFirstIndex, dcr->VolLastIndex,
270 dcr->StartFile, dcr->EndFile,
271 dcr->StartBlock, dcr->EndBlock);
272 Dmsg1(100, "create_jobmedia(): %s", dir->msg);
273 if (bnet_recv(dir) <= 0) {
274 Dmsg0(190, "create_jobmedia error bnet_recv\n");
275 Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: ERR=%s\n"),
279 Dmsg1(120, "Create_jobmedia: %s", dir->msg);
280 if (strcmp(dir->msg, OK_create) != 0) {
281 Dmsg1(130, "Bad response from Dir: %s\n", dir->msg);
282 Jmsg(jcr, M_ERROR, 0, _("Error creating JobMedia record: %s\n"), dir->msg);
290 * Update File Attribute data
292 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec)
294 BSOCK *dir = jcr->dir_bsock;
297 dir->msglen = sprintf(dir->msg, FileAttributes, jcr->Job);
298 dir->msg = check_pool_memory_size(dir->msg, dir->msglen +
299 sizeof(DEV_RECORD) + rec->data_len);
300 ser_begin(dir->msg + dir->msglen, 0);
301 ser_uint32(rec->VolSessionId);
302 ser_uint32(rec->VolSessionTime);
303 ser_int32(rec->FileIndex);
304 ser_int32(rec->Stream);
305 ser_uint32(rec->data_len);
306 ser_bytes(rec->data, rec->data_len);
307 dir->msglen = ser_length(dir->msg);
308 return bnet_send(dir);
313 * Request the sysop to create an appendable volume
315 * Entered with device blocked.
316 * Leaves with device blocked.
318 * Returns: 1 on success (operator issues a mount command)
320 * Note, must create dev->errmsg on error return.
322 * On success, jcr->VolumeName and jcr->VolCatInfo contain
323 * information on suggested volume, but this may not be the
324 * same as what is actually mounted.
326 * When we return with success, the correct tape may or may not
327 * actually be mounted. The calling routine must read it and
330 int dir_ask_sysop_to_create_appendable_volume(JCR *jcr, DEVICE *dev)
336 Dmsg0(130, "enter dir_ask_sysop_to_create_appendable_volume\n");
337 ASSERT(dev->dev_blocked);
339 if (job_canceled(jcr)) {
341 _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"),
342 jcr->Job, jcr->dev_name);
343 Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
346 /* First pass, we *know* there are no appendable volumes, so no need to call */
347 if (!first && dir_find_next_appendable_volume(jcr)) { /* get suggested volume */
348 unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
349 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
351 * If we have a valid volume name and we are not
352 * removable media, return now, or if we have a
353 * Slot for an autochanger, otherwise wait
354 * for the operator to mount the media.
356 if (!unmounted && ((jcr->VolumeName[0] && !dev_cap(dev, CAP_REM) &&
357 dev_cap(dev, CAP_LABEL)) ||
358 (jcr->VolumeName[0] && jcr->VolCatInfo.Slot))) {
359 Dmsg0(100, "Return 1 from mount without wait.\n");
362 jstat = JS_WaitMount;
364 Jmsg(jcr, M_MOUNT, 0, _(
365 "Please mount Volume \"%s\" on Storage Device \"%s\" for Job %s\n"
366 "Use \"mount\" command to release Job.\n"),
367 jcr->VolumeName, jcr->dev_name, jcr->Job);
368 Dmsg3(190, "Mount %s on %s for Job %s\n",
369 jcr->VolumeName, jcr->dev_name, jcr->Job);
372 jstat = JS_WaitMedia;
374 Jmsg(jcr, M_MOUNT, 0, _(
375 "Job %s waiting. Cannot find any appendable volumes.\n\
376 Please use the \"label\" command to create a new Volume for:\n\
388 jcr->JobStatus = jstat;
389 dir_send_job_status(jcr);
391 stat = wait_for_sysop(jcr, dev);
393 Dmsg1(200, "Poll timeout in create append vol on device %s\n", dev_name(dev));
397 if (stat == ETIMEDOUT) {
398 if (!double_dev_wait_time(dev)) {
399 Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"),
400 dev_name(dev), jcr->Job);
401 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
402 Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
403 return 0; /* exceeded maximum waits */
407 if (stat == EINVAL) {
408 Mmsg2(&dev->errmsg, _("pthread error in mount_next_volume stat=%d ERR=%s\n"),
409 stat, strerror(stat));
410 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
414 Jmsg(jcr, M_WARNING, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
417 Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
419 /* If no VolumeName, and cannot get one, try again */
420 if (jcr->VolumeName[0] == 0 && !job_canceled(jcr) &&
421 !dir_find_next_appendable_volume(jcr)) {
422 Jmsg(jcr, M_MOUNT, 0, _(
423 "Someone woke me up, but I cannot find any appendable\n\
424 volumes for Job=%s.\n"), jcr->Job);
425 /* Restart wait counters after user interaction */
426 init_dev_wait_timers(dev);
429 unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
430 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
432 continue; /* continue to wait */
436 * Device mounted, we have a volume, break and return
440 set_jcr_job_status(jcr, JS_Running);
441 dir_send_job_status(jcr);
442 Dmsg0(130, "leave dir_ask_sysop_to_mount_create_appendable_volume\n");
447 * Request to mount specific Volume
449 * Entered with device blocked and jcr->VolumeName is desired
451 * Leaves with device blocked.
453 * Returns: 1 on success (operator issues a mount command)
455 * Note, must create dev->errmsg on error return.
458 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
463 Dmsg0(130, "enter dir_ask_sysop_to_mount_volume\n");
464 if (!jcr->VolumeName[0]) {
465 Mmsg0(&dev->errmsg, _("Cannot request another volume: no volume name given.\n"));
468 ASSERT(dev->dev_blocked);
470 if (job_canceled(jcr)) {
471 Mmsg(&dev->errmsg, _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"),
472 jcr->Job, jcr->dev_name);
478 * If we have a valid volume name and we are not
479 * removable media, return now, or if we have a
480 * Slot for an autochanger, otherwise wait
481 * for the operator to mount the media.
483 if ((!dev_cap(dev, CAP_REM) && dev_cap(dev, CAP_LABEL)) || jcr->VolCatInfo.Slot) {
484 Dmsg0(100, "Return 1 from mount without wait.\n");
490 msg = _("Please mount");
491 Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
492 msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
493 Dmsg3(190, "Mount %s on %s for Job %s\n",
494 jcr->VolumeName, jcr->dev_name, jcr->Job);
497 jcr->JobStatus = JS_WaitMount;
498 dir_send_job_status(jcr);
500 stat = wait_for_sysop(jcr, dev); /* wait on device */
502 Dmsg1(200, "Poll timeout in mount vol on device %s\n", dev_name(dev));
503 Dmsg1(200, "Blocked=%d\n", dev->dev_blocked);
507 if (stat == ETIMEDOUT) {
508 if (!double_dev_wait_time(dev)) {
509 Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"),
510 dev_name(dev), jcr->Job);
511 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
512 Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
513 return 0; /* exceeded maximum waits */
517 if (stat == EINVAL) {
518 Mmsg2(&dev->errmsg, _("pthread error in mount_volume stat=%d ERR=%s\n"),
519 stat, strerror(stat));
520 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
524 Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
527 Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
530 set_jcr_job_status(jcr, JS_Running);
531 dir_send_job_status(jcr);
532 Dmsg0(130, "leave dir_ask_sysop_to_mount_volume\n");
537 * Wait for SysOp to mount a tape
539 static int wait_for_sysop(JCR *jcr, DEVICE *dev)
543 struct timespec timeout;
544 time_t last_heartbeat = 0;
545 time_t first_start = time(NULL);
551 unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
552 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
556 * Wait requested time (dev->rem_wait_sec). However, we also wake up every
557 * HB_TIME seconds and send a heartbeat to the FD and the Director
558 * to keep stateful firewalls from closing them down while waiting
561 add_wait = dev->rem_wait_sec;
562 if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
563 add_wait = me->heartbeat_interval;
565 if (!unmounted && dev->vol_poll_interval && add_wait > dev->vol_poll_interval) {
566 add_wait = dev->vol_poll_interval;
568 gettimeofday(&tv, &tz);
569 timeout.tv_nsec = tv.tv_usec * 1000;
570 timeout.tv_sec = tv.tv_sec + add_wait;
573 dev->dev_prev_blocked = dev->dev_blocked;
574 dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
577 for ( ; !job_canceled(jcr); ) {
580 Dmsg3(100, "I'm going to sleep on device %s. HB=%d wait=%d\n", dev_name(dev),
581 (int)me->heartbeat_interval, dev->wait_sec);
583 stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
584 Dmsg1(100, "Wokeup from sleep on device stat=%d\n", stat);
587 dev->rem_wait_sec -= (now - start);
589 /* Note, this always triggers the first time. We want that. */
590 if (me->heartbeat_interval) {
591 if (now - last_heartbeat >= me->heartbeat_interval) {
592 /* send heartbeats */
593 if (jcr->file_bsock) {
594 bnet_sig(jcr->file_bsock, BNET_HEARTBEAT);
595 Dmsg0(100, "Send heartbeat to FD.\n");
597 if (jcr->dir_bsock) {
598 bnet_sig(jcr->dir_bsock, BNET_HEARTBEAT);
600 last_heartbeat = now;
605 * Check if user unmounted the device while we were waiting
607 unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
608 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
610 if (stat != ETIMEDOUT) { /* we blocked the device */
611 break; /* on error return */
613 if (dev->rem_wait_sec <= 0) { /* on exceeding wait time return */
614 Dmsg0(100, "Exceed wait time.\n");
618 if (!unmounted && dev->vol_poll_interval &&
619 (now - first_start >= dev->vol_poll_interval)) {
620 Dmsg1(200, "In wait blocked=%d\n", dev->dev_blocked);
625 * Check if user mounted the device while we were waiting
627 if (dev->dev_blocked == BST_MOUNT) { /* mount request ? */
632 add_wait = dev->wait_sec - (now - start);
636 if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
637 add_wait = me->heartbeat_interval;
639 gettimeofday(&tv, &tz);
640 timeout.tv_nsec = tv.tv_usec * 1000;
641 timeout.tv_sec = tv.tv_sec + add_wait; /* additional wait */
642 Dmsg1(100, "Additional wait %d sec.\n", add_wait);
646 dev->dev_blocked = dev->dev_prev_blocked; /* restore entry state */