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;
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 LastWritten = 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 LastWritten, 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);
267 * Request the sysop to create an appendable volume
269 * Entered with device blocked.
270 * Leaves with device blocked.
272 * Returns: 1 on success (operator issues a mount command)
274 * Note, must create dev->errmsg on error return.
276 * On success, jcr->VolumeName and jcr->VolCatInfo contain
277 * information on suggested volume, but this may not be the
278 * same as what is actually mounted.
280 * When we return with success, the correct tape may or may not
281 * actually be mounted. The calling routine must read it and
284 int dir_ask_sysop_to_create_appendable_volume(JCR *jcr, DEVICE *dev)
289 Dmsg0(130, "enter dir_ask_sysop_to_create_appendable_volume\n");
290 ASSERT(dev->dev_blocked);
292 if (job_canceled(jcr)) {
294 _("Job %s canceled while waiting for mount on Storage Device \"%s\".\n"),
295 jcr->Job, jcr->dev_name);
296 Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
299 if (dir_find_next_appendable_volume(jcr)) { /* get suggested volume */
300 jstat = JS_WaitMount;
301 unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
302 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
304 * If we have a valid volume name and we are not
305 * removable media, return now, or if we have a
306 * Slot for an autochanger, otherwise wait
307 * for the operator to mount the media.
309 if (!unmounted && ((jcr->VolumeName[0] && !dev_cap(dev, CAP_REM) &&
310 dev_cap(dev, CAP_LABEL)) ||
311 (jcr->VolumeName[0] && jcr->VolCatInfo.Slot))) {
312 Dmsg0(100, "Return 1 from mount without wait.\n");
315 Jmsg(jcr, M_MOUNT, 0, _(
316 "Please mount Volume \"%s\" on Storage Device \"%s\" for Job %s\n"
317 "Use \"mount\" command to release Job.\n"),
318 jcr->VolumeName, jcr->dev_name, jcr->Job);
319 Dmsg3(190, "Mount %s on %s for Job %s\n",
320 jcr->VolumeName, jcr->dev_name, jcr->Job);
322 jstat = JS_WaitMedia;
324 Jmsg(jcr, M_MOUNT, 0, _(
325 "Job %s waiting. Cannot find any appendable volumes.\n\
326 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);
342 Dmsg1(200, "Poll timeout in create append vol on device %s\n", dev_name(dev));
346 if (stat == ETIMEDOUT) {
347 if (!double_dev_wait_time(dev)) {
348 Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"),
349 dev_name(dev), jcr->Job);
350 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
351 Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
352 return 0; /* exceeded maximum waits */
356 if (stat == EINVAL) {
357 Mmsg2(&dev->errmsg, _("pthread error in mount_next_volume stat=%d ERR=%s\n"),
358 stat, strerror(stat));
359 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
363 Jmsg(jcr, M_WARNING, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
366 Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
368 /* If no VolumeName, and cannot get one, try again */
369 if (jcr->VolumeName[0] == 0 && !job_canceled(jcr) &&
370 !dir_find_next_appendable_volume(jcr)) {
371 Jmsg(jcr, M_MOUNT, 0, _(
372 "Someone woke me up, but I cannot find any appendable\n\
373 volumes for Job=%s.\n"), jcr->Job);
374 /* Restart wait counters after user interaction */
375 init_dev_wait_timers(dev);
378 unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
379 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
381 continue; /* continue to wait */
385 * Device mounted, we have a volume, break and return
389 set_jcr_job_status(jcr, JS_Running);
390 dir_send_job_status(jcr);
391 Dmsg0(130, "leave dir_ask_sysop_to_mount_create_appendable_volume\n");
396 * Request to mount specific Volume
398 * Entered with device blocked and jcr->VolumeName is desired
400 * Leaves with device blocked.
402 * Returns: 1 on success (operator issues a mount command)
404 * Note, must create dev->errmsg on error return.
407 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
412 Dmsg0(130, "enter dir_ask_sysop_to_mount_volume\n");
413 if (!jcr->VolumeName[0]) {
414 Mmsg0(&dev->errmsg, _("Cannot request another volume: no volume name given.\n"));
417 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);
426 * If we have a valid volume name and we are not
427 * removable media, return now, or if we have a
428 * Slot for an autochanger, otherwise wait
429 * for the operator to mount the media.
431 if ((jcr->VolumeName[0] && !dev_cap(dev, CAP_REM) && dev_cap(dev, CAP_LABEL)) ||
432 (jcr->VolumeName[0] && jcr->VolCatInfo.Slot)) {
433 Dmsg0(100, "Return 1 from mount without wait.\n");
438 msg = _("Please mount");
439 Jmsg(jcr, M_MOUNT, 0, _("%s Volume \"%s\" on Storage Device \"%s\" for Job %s\n"),
440 msg, jcr->VolumeName, jcr->dev_name, jcr->Job);
441 Dmsg3(190, "Mount %s on %s for Job %s\n",
442 jcr->VolumeName, jcr->dev_name, jcr->Job);
445 jcr->JobStatus = JS_WaitMount;
446 dir_send_job_status(jcr);
448 stat = wait_for_sysop(jcr, dev); /* wait on device */
450 Dmsg1(200, "Poll timeout in mount vol on device %s\n", dev_name(dev));
451 Dmsg1(200, "Blocked=%d\n", dev->dev_blocked);
455 if (stat == ETIMEDOUT) {
456 if (!double_dev_wait_time(dev)) {
457 Mmsg(&dev->errmsg, _("Gave up waiting to mount Storage Device \"%s\" for Job %s\n"),
458 dev_name(dev), jcr->Job);
459 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
460 Dmsg1(190, "Gave up waiting on device %s\n", dev_name(dev));
461 return 0; /* exceeded maximum waits */
465 if (stat == EINVAL) {
466 Mmsg2(&dev->errmsg, _("pthread error in mount_volume stat=%d ERR=%s\n"),
467 stat, strerror(stat));
468 Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
472 Jmsg(jcr, M_ERROR, 0, _("pthread error in mount_next_volume stat=%d ERR=%s\n"), stat,
475 Dmsg1(190, "Someone woke me for device %s\n", dev_name(dev));
478 set_jcr_job_status(jcr, JS_Running);
479 dir_send_job_status(jcr);
480 Dmsg0(130, "leave dir_ask_sysop_to_mount_volume\n");
485 * Wait for SysOp to mount a tape
487 static int wait_for_sysop(JCR *jcr, DEVICE *dev)
491 struct timespec timeout;
492 time_t last_heartbeat = 0;
493 time_t first_start = time(NULL);
499 unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
500 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
504 * Wait requested time (dev->rem_wait_sec). However, we also wake up every
505 * HB_TIME seconds and send a heartbeat to the FD and the Director
506 * to keep stateful firewalls from closing them down while waiting
509 add_wait = dev->rem_wait_sec;
510 if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
511 add_wait = me->heartbeat_interval;
513 if (!unmounted && dev->vol_poll_interval && add_wait > dev->vol_poll_interval) {
514 add_wait = dev->vol_poll_interval;
516 gettimeofday(&tv, &tz);
517 timeout.tv_nsec = tv.tv_usec * 1000;
518 timeout.tv_sec = tv.tv_sec + add_wait;
521 dev->dev_prev_blocked = dev->dev_blocked;
522 dev->dev_blocked = BST_WAITING_FOR_SYSOP; /* indicate waiting for mount */
525 for ( ; !job_canceled(jcr); ) {
528 Dmsg3(100, "I'm going to sleep on device %s. HB=%d wait=%d\n", dev_name(dev),
529 (int)me->heartbeat_interval, dev->wait_sec);
531 stat = pthread_cond_timedwait(&dev->wait_next_vol, &dev->mutex, &timeout);
532 Dmsg1(100, "Wokeup from sleep on device stat=%d\n", stat);
535 dev->rem_wait_sec -= (now - start);
537 /* Note, this always triggers the first time. We want that. */
538 if (me->heartbeat_interval) {
539 if (now - last_heartbeat >= me->heartbeat_interval) {
540 /* send heartbeats */
541 if (jcr->file_bsock) {
542 bnet_sig(jcr->file_bsock, BNET_HEARTBEAT);
543 Dmsg0(100, "Send heartbeat to FD.\n");
545 if (jcr->dir_bsock) {
546 bnet_sig(jcr->dir_bsock, BNET_HEARTBEAT);
548 last_heartbeat = now;
553 * Check if user unmounted the device while we were waiting
555 unmounted = (dev->dev_blocked == BST_UNMOUNTED) ||
556 (dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP);
558 if (stat != ETIMEDOUT) { /* we blocked the device */
559 break; /* on error return */
561 if (dev->rem_wait_sec <= 0) { /* on exceeding wait time return */
562 Dmsg0(100, "Exceed wait time.\n");
566 if (!unmounted && dev->vol_poll_interval &&
567 (now - first_start >= dev->vol_poll_interval)) {
568 Dmsg1(200, "In wait blocked=%d\n", dev->dev_blocked);
573 * Check if user mounted the device while we were waiting
575 if (dev->dev_blocked == BST_MOUNT) { /* mount request ? */
580 add_wait = dev->wait_sec - (now - start);
584 if (me->heartbeat_interval && add_wait > me->heartbeat_interval) {
585 add_wait = me->heartbeat_interval;
587 gettimeofday(&tv, &tz);
588 timeout.tv_nsec = tv.tv_usec * 1000;
589 timeout.tv_sec = tv.tv_sec + add_wait; /* additional wait */
590 Dmsg1(100, "Additional wait %d sec.\n", add_wait);
594 dev->dev_blocked = dev->dev_prev_blocked; /* restore entry state */