3 * Bacula Director -- catreq.c -- handles the message channel
4 * catalog request from the Storage daemon.
6 * Kern Sibbald, March MMI
8 * This routine runs as a thread and must be thread reentrant.
10 * Basic tasks done here:
11 * Handle Catalog services.
16 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
18 This program is free software; you can redistribute it and/or
19 modify it under the terms of the GNU General Public License as
20 published by the Free Software Foundation; either version 2 of
21 the License, or (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public
29 License along with this program; if not, write to the Free
30 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
39 * Handle catalog request
40 * For now, we simply return next Volume to be used
43 /* Requests from the Storage daemon */
44 static char Find_media[] = "CatReq Job=%127s FindMedia=%d\n";
45 static char Get_Vol_Info[] = "CatReq Job=%127s GetVolInfo VolName=%127s write=%d\n";
47 static char Update_media[] = "CatReq Job=%127s UpdateMedia VolName=%s\
48 VolJobs=%u VolFiles=%u VolBlocks=%u VolBytes=%" lld " VolMounts=%u\
49 VolErrors=%u VolWrites=%u MaxVolBytes=%" lld " EndTime=%d VolStatus=%10s\
50 Slot=%d relabel=%d\n";
52 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia \
53 FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u \
54 StartBlock=%u EndBlock=%u\n";
57 /* Responses sent to Storage daemon */
58 static char OK_media[] = "1000 OK VolName=%s VolJobs=%u VolFiles=%u\
59 VolBlocks=%u VolBytes=%s VolMounts=%u VolErrors=%u VolWrites=%u\
60 MaxVolBytes=%s VolCapacityBytes=%s VolStatus=%s Slot=%d\
61 MaxVolJobs=%u MaxVolFiles=%u\n";
63 static char OK_update[] = "1000 OK UpdateMedia\n";
65 /* static char FileAttributes[] = "UpdCat Job=%127s FileAttributes "; */
68 void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
72 char Job[MAX_NAME_LENGTH];
73 int index, ok, relabel, writing, retry = 0;
76 memset(&mr, 0, sizeof(mr));
77 memset(&sdmr, 0, sizeof(sdmr));
78 memset(&jm, 0, sizeof(jm));
81 * Request to find next appendable Volume for this Job
83 Dmsg1(120, "catreq %s", bs->msg);
84 if (sscanf(bs->msg, Find_media, &Job, &index) == 2) {
85 mr.PoolId = jcr->PoolId;
86 strcpy(mr.MediaType, jcr->store->media_type);
87 Dmsg2(120, "CatReq FindMedia: Id=%d, MediaType=%s\n",
88 mr.PoolId, mr.MediaType);
90 * Find the Next Volume for Append
93 strcpy(mr.VolStatus, "Append"); /* want only appendable volumes */
94 ok = db_find_next_volume(jcr->db, index, &mr);
95 Dmsg1(200, "catreq after find_next_vol ok=%d\n", ok);
97 /* Well, try finding recycled tapes */
98 ok = find_recycled_volume(jcr, &mr);
99 Dmsg1(100, "find_recycled_volume1 %d\n", ok);
102 ok = recycle_a_volume(jcr, &mr);
103 Dmsg1(100, "find_recycled_volume2 %d\n", ok);
105 /* See if we can create a new Volume */
107 ok = newVolume(jcr, &mr);
111 /* Check if use duration has expired */
112 Dmsg2(200, "VolJobs=%d FirstWritten=%d\n", mr.VolJobs, mr.FirstWritten);
113 if (ok && mr.VolJobs > 0 && mr.VolUseDuration > 0 &&
114 strcmp(mr.VolStatus, "Recycle") != 0) {
115 utime_t now = time(NULL);
116 if (mr.VolUseDuration <= (now - mr.FirstWritten)) {
117 Dmsg4(100, "Duration=%d now=%d start=%d now-start=%d\n",
118 (int)jcr->pool->VolUseDuration, (int)now, (int)mr.FirstWritten,
119 (int)(now-mr.FirstWritten));
120 Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "
121 "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
122 strcpy(mr.VolStatus, "Used"); /* yes, mark as used */
123 if (!db_update_media_record(jcr->db, &mr)) {
124 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
125 db_strerror(jcr->db));
126 } else if (retry++ < 200) { /* sanity check */
129 Jmsg(jcr, M_ERROR, 0, _(
130 "We seem to be looping trying to find the next volume. I give up. Ask operator.\n"));
132 ok = FALSE; /* give up */
137 * Send Find Media response to Storage daemon
140 char ed1[50], ed2[50], ed3[50];
141 jcr->MediaId = mr.MediaId;
142 pm_strcpy(&jcr->VolumeName, mr.VolumeName);
143 bash_spaces(mr.VolumeName);
144 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
145 mr.VolFiles, mr.VolBlocks, edit_uint64(mr.VolBytes, ed1),
146 mr.VolMounts, mr.VolErrors, mr.VolWrites,
147 edit_uint64(mr.MaxVolBytes, ed2),
148 edit_uint64(mr.VolCapacityBytes, ed3),
149 mr.VolStatus, mr.Slot, mr.MaxVolJobs, mr.MaxVolFiles);
150 Dmsg1(200, "Find media: %s", bs->msg);
152 bnet_fsend(bs, "1999 No Media\n");
156 * Request to find specific Volume information
158 } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
159 Dmsg1(120, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
163 unbash_spaces(mr.VolumeName);
164 if (db_get_media_record(jcr->db, &mr)) {
166 jcr->MediaId = mr.MediaId;
167 Dmsg1(120, "VolumeInfo MediaId=%d\n", jcr->MediaId);
168 pm_strcpy(&jcr->VolumeName, mr.VolumeName);
170 VolSuitable = 1; /* accept anything for read */
173 * Make sure this volume is suitable for this job, i.e.
174 * it is either Append or Recycle and Media Type matches
175 * and Pool allows any volume.
177 if (mr.PoolId == jcr->PoolId &&
178 (strcmp(mr.VolStatus, "Append") == 0 ||
179 strcmp(mr.VolStatus, "Recycle") == 0) &&
180 strcmp(mr.MediaType, jcr->store->media_type) == 0 &&
181 jcr->pool->accept_any_volume) {
186 char ed1[50], ed2[50], ed3[50];
188 * Send Find Media response to Storage daemon
190 bash_spaces(mr.VolumeName);
191 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
192 mr.VolFiles, mr.VolBlocks, edit_uint64(mr.VolBytes, ed1),
193 mr.VolMounts, mr.VolErrors, mr.VolWrites,
194 edit_uint64(mr.MaxVolBytes, ed2),
195 edit_uint64(mr.VolCapacityBytes, ed3),
196 mr.VolStatus, mr.Slot, mr.MaxVolJobs, mr.MaxVolFiles);
197 Dmsg1(200, "Vol Info: %s", bs->msg);
199 /* Not suitable volume */
200 bnet_fsend(bs, "1998 Volume \"%s\"not appropriate.\n",
205 bnet_fsend(bs, "1999 Volume \"%s\" not found.\n", mr.VolumeName);
210 * Request to update Media record. Comes typically at the end
211 * of a Storage daemon Job Session
213 } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName, &sdmr.VolJobs,
214 &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes, &sdmr.VolMounts, &sdmr.VolErrors,
215 &sdmr.VolWrites, &sdmr.MaxVolBytes, &sdmr.LastWritten, &sdmr.VolStatus,
216 &sdmr.Slot, &relabel) == 14) {
218 bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
219 if (!db_get_media_record(jcr->db, &mr)) {
220 Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
221 mr.VolumeName, db_strerror(jcr->db));
222 bnet_fsend(bs, "1991 Catalog Request failed: %s", db_strerror(jcr->db));
225 /* Set first written time if this is first job */
226 if (mr.VolJobs == 0 || sdmr.VolJobs == 1) {
227 mr.FirstWritten = jcr->start_time; /* use Job start time as first write */
229 Dmsg2(200, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
230 /* Copy updated values to original media record */
231 mr.VolJobs = sdmr.VolJobs;
232 mr.VolFiles = sdmr.VolFiles;
233 mr.VolBlocks = sdmr.VolBlocks;
234 mr.VolBytes = sdmr.VolBytes;
235 mr.VolMounts = sdmr.VolMounts;
236 mr.VolErrors = sdmr.VolErrors;
237 mr.VolWrites = sdmr.VolWrites;
238 mr.LastWritten = sdmr.LastWritten;
239 bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
243 * Update Media Record
246 /* First handle Max Volume Bytes */
247 if ((mr.MaxVolBytes > 0 && mr.VolBytes >= mr.MaxVolBytes)) {
248 Jmsg(jcr, M_INFO, 0, _("Max Volume bytes exceeded. "
249 "Marking Volume \"%s\" as Full.\n"), mr.VolumeName);
250 strcpy(mr.VolStatus, "Full");
252 /* Now see if Volume should only be used once */
253 } else if (mr.VolBytes > 0 && jcr->pool->use_volume_once) {
254 Jmsg(jcr, M_INFO, 0, _("Volume used once. "
255 "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
256 strcpy(mr.VolStatus, "Used");
258 /* Now see if Max Jobs written to volume */
259 } else if (mr.MaxVolJobs > 0 && mr.MaxVolJobs <= mr.VolJobs) {
260 Jmsg(jcr, M_INFO, 0, _("Max Volume jobs exceeded. "
261 "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
262 strcpy(mr.VolStatus, "Used");
264 /* Now see if Max Files written to volume */
265 } else if (mr.MaxVolFiles > 0 && mr.MaxVolFiles <= mr.VolFiles) {
266 Jmsg(jcr, M_INFO, 0, _("Max Volume files exceeded. "
267 "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
268 strcpy(mr.VolStatus, "Used");
270 /* Finally, check Use duration expiration */
271 } else if (mr.VolUseDuration > 0) {
272 utime_t now = time(NULL);
273 /* See if Vol Use has expired */
274 if (mr.VolUseDuration <= (now - mr.FirstWritten)) {
275 Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "
276 "Marking Volume \"%s\"as Used.\n"), mr.VolumeName);
277 strcpy(mr.VolStatus, "Used"); /* yes, mark as used */
281 Dmsg2(200, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
282 if (db_update_media_record(jcr->db, &mr)) {
283 bnet_fsend(bs, OK_update);
284 Dmsg0(190, "send OK\n");
286 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
287 db_strerror(jcr->db));
288 bnet_fsend(bs, "1992 Update Media error\n");
289 Dmsg0(190, "send error\n");
293 * Request to create a JobMedia record
295 } else if (sscanf(bs->msg, Create_job_media, &Job,
296 &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
297 &jm.StartBlock, &jm.EndBlock) == 7) {
299 jm.JobId = jcr->JobId;
300 jm.MediaId = jcr->MediaId;
301 Dmsg6(100, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
302 jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
303 if (!db_create_jobmedia_record(jcr->db, &jm)) {
304 Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
305 db_strerror(jcr->db));
306 bnet_fsend(bs, "1991 Update JobMedia error\n");
308 Dmsg0(100, "JobMedia record created\n");
309 bnet_fsend(bs, OK_update);
313 omsg = get_memory(bs->msglen+1);
314 pm_strcpy(&omsg, bs->msg);
315 bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
316 Jmsg1(jcr, M_ERROR, 0, _("Invalid Catalog request: %s"), omsg);
319 Dmsg1(120, ">CatReq response: %s", bs->msg);
320 Dmsg1(200, "Leave catreq jcr 0x%x\n", jcr);
325 * Update File Attributes in the catalog with data
326 * sent by the Storage daemon.
328 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
331 uint32_t VolSessionId, VolSessionTime;
340 if (!jcr->pool->catalog_files) {
343 db_start_transaction(jcr->db); /* start transaction if not already open */
344 skip_nonspaces(&p); /* UpdCat */
346 skip_nonspaces(&p); /* Job=nnn */
348 skip_nonspaces(&p); /* FileAttributes */
351 unser_uint32(VolSessionId);
352 unser_uint32(VolSessionTime);
353 unser_int32(FileIndex);
355 unser_uint32(data_len);
356 p += unser_length(p);
358 Dmsg1(99, "UpdCat msg=%s\n", bs->msg);
359 Dmsg5(99, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
360 VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
362 if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_WIN32_ATTRIBUTES) {
363 skip_nonspaces(&p); /* skip FileIndex */
365 skip_nonspaces(&p); /* skip FileType */
368 len = strlen(fname); /* length before attributes */
369 attr = &fname[len+1];
371 Dmsg2(109, "dird<stored: stream=%d %s\n", Stream, fname);
372 Dmsg1(109, "dird<stored: attr=%s\n", attr);
375 ar.FileIndex = FileIndex;
378 ar.JobId = jcr->JobId;
380 Dmsg2(111, "dird<filed: stream=%d %s\n", Stream, fname);
381 Dmsg1(120, "dird<filed: attr=%s\n", attr);
383 if (!db_create_file_attributes_record(jcr->db, &ar)) {
384 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
386 /* Save values for MD5 update */
387 jcr->FileId = ar.FileId;
388 jcr->FileIndex = FileIndex;
389 } else if (Stream == STREAM_MD5_SIGNATURE) {
391 if (jcr->FileIndex != FileIndex) {
392 Jmsg(jcr, M_WARNING, 0, "Got MD5 but not same File as attributes\n");
394 /* Update MD5 signature in catalog */
395 char MD5buf[50]; /* 24 bytes should be enough */
396 bin_to_base64(MD5buf, fname, 16);
397 Dmsg2(190, "MD5len=%d MD5=%s\n", strlen(MD5buf), MD5buf);
398 if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, MD5buf)) {
399 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5. %s"),
400 db_strerror(jcr->db));