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(200, "catreq %s", bs->msg);
84 if (sscanf(bs->msg, Find_media, &Job, &index) == 2) {
85 mr.PoolId = jcr->PoolId;
86 bstrncpy(mr.MediaType, jcr->store->media_type, sizeof(mr.MediaType));
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, jcr->db, index, &mr);
95 Dmsg2(100, "catreq after find_next_vol ok=%d FW=%d\n", ok, mr.FirstWritten);
97 /* Well, try finding recycled volumes */
98 ok = find_recycled_volume(jcr, &mr);
99 Dmsg2(100, "find_recycled_volume %d FW=%d\n", ok, mr.FirstWritten);
102 ok = recycle_oldest_purged_volume(jcr, &mr);
103 Dmsg2(200, "find_recycled_volume2 %d FW=%d\n", ok, mr.FirstWritten);
105 /* See if we can create a new Volume */
106 ok = newVolume(jcr, &mr);
110 if (!ok && jcr->pool->purge_oldest_volume) {
111 Dmsg1(200, "No next volume found. PurgeOldest=%d\n",
112 jcr->pool->purge_oldest_volume);
113 /* Find oldest volume to recycle */
114 ok = db_find_next_volume(jcr, jcr->db, -1, &mr);
115 Dmsg1(400, "Find oldest=%d\n", ok);
118 Dmsg0(400, "Try purge.\n");
119 /* Try to purge oldest volume */
120 ua = new_ua_context(jcr);
121 Jmsg(jcr, M_INFO, 0, _("Purging oldest volume \"%s\"\n"), mr.VolumeName);
122 ok = purge_jobs_from_volume(ua, &mr);
125 ok = recycle_oldest_purged_volume(jcr, &mr);
126 Dmsg1(400, "Recycle after recycle oldest=%d\n", ok);
131 /* Check if use duration applies, then if it has expired */
132 Dmsg2(100, "VolJobs=%d FirstWritten=%d\n", mr.VolJobs, mr.FirstWritten);
133 if (ok && mr.VolJobs > 0 && mr.VolUseDuration > 0 &&
134 strcmp(mr.VolStatus, "Append") == 0) {
135 utime_t now = time(NULL);
136 if (mr.VolUseDuration <= (now - mr.FirstWritten)) {
138 Dmsg4(100, "Duration=%d now=%d start=%d now-start=%d\n",
139 (int)mr.VolUseDuration, (int)now, (int)mr.FirstWritten,
140 (int)(now-mr.FirstWritten));
141 Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "
142 "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
143 strcpy(mr.VolStatus, "Used"); /* yes, mark as used */
144 if (!db_update_media_record(jcr, jcr->db, &mr)) {
145 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating volume \"%s\". ERR=%s"),
146 mr.VolumeName, db_strerror(jcr->db));
148 if (retry++ < 200) { /* sanity check */
149 continue; /* try again from the top */
151 Jmsg(jcr, M_ERROR, 0, _(
152 "We seem to be looping trying to find the next volume. I give up.\n"));
160 * Send Find Media response to Storage daemon
163 char ed1[50], ed2[50], ed3[50];
164 jcr->MediaId = mr.MediaId;
165 pm_strcpy(&jcr->VolumeName, mr.VolumeName);
166 bash_spaces(mr.VolumeName);
167 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
168 mr.VolFiles, mr.VolBlocks, edit_uint64(mr.VolBytes, ed1),
169 mr.VolMounts, mr.VolErrors, mr.VolWrites,
170 edit_uint64(mr.MaxVolBytes, ed2),
171 edit_uint64(mr.VolCapacityBytes, ed3),
172 mr.VolStatus, mr.Slot, mr.MaxVolJobs, mr.MaxVolFiles);
173 Dmsg2(100, "Find media for %s: %s", jcr->Job, bs->msg);
175 bnet_fsend(bs, "1901 No Media.\n");
179 * Request to find specific Volume information
181 } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
182 Dmsg1(400, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
186 unbash_spaces(mr.VolumeName);
187 if (db_get_media_record(jcr, jcr->db, &mr)) {
189 char *reason = ""; /* detailed reason for rejection */
190 jcr->MediaId = mr.MediaId;
191 Dmsg1(120, "VolumeInfo MediaId=%d\n", jcr->MediaId);
192 pm_strcpy(&jcr->VolumeName, mr.VolumeName);
194 VolSuitable = 1; /* accept anything for read */
197 * SD wants to write this Volume, so make
198 * sure it is suitable for this job, i.e.
199 * Pool matches, and it is either Append or Recycle
200 * and Media Type matches and Pool allows any volume.
202 if (mr.PoolId != jcr->PoolId) {
203 reason = "not in Pool";
204 } else if (strcmp(mr.VolStatus, "Append") != 0 &&
205 strcmp(mr.VolStatus, "Recycle") != 0) {
206 reason = "not Append or Recycle";
207 } else if (strcmp(mr.MediaType, jcr->store->media_type) != 0) {
208 reason = "not correct MediaType";
209 } else if (!jcr->pool->accept_any_volume) {
210 reason = "Volume not in sequence";
216 char ed1[50], ed2[50], ed3[50];
218 * Send Find Media response to Storage daemon
220 bash_spaces(mr.VolumeName);
221 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
222 mr.VolFiles, mr.VolBlocks, edit_uint64(mr.VolBytes, ed1),
223 mr.VolMounts, mr.VolErrors, mr.VolWrites,
224 edit_uint64(mr.MaxVolBytes, ed2),
225 edit_uint64(mr.VolCapacityBytes, ed3),
226 mr.VolStatus, mr.Slot, mr.MaxVolJobs, mr.MaxVolFiles);
227 Dmsg2(100, "Vol Info for %s: %s", jcr->Job, bs->msg);
229 /* Not suitable volume */
230 bnet_fsend(bs, "1998 Volume \"%s\" %s.\n",
231 mr.VolumeName, reason);
235 bnet_fsend(bs, "1997 Volume \"%s\" not in catalog.\n", mr.VolumeName);
240 * Request to update Media record. Comes typically at the end
241 * of a Storage daemon Job Session
243 } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName, &sdmr.VolJobs,
244 &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes, &sdmr.VolMounts, &sdmr.VolErrors,
245 &sdmr.VolWrites, &sdmr.MaxVolBytes, &sdmr.LastWritten, &sdmr.VolStatus,
246 &sdmr.Slot, &relabel) == 14) {
248 Dmsg3(400, "Update media %s oldStat=%s newStat=%s\n", sdmr.VolumeName,
249 mr.VolStatus, sdmr.VolStatus);
250 bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
251 unbash_spaces(mr.VolumeName);
252 if (!db_get_media_record(jcr, jcr->db, &mr)) {
253 Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
254 mr.VolumeName, db_strerror(jcr->db));
255 bnet_fsend(bs, "1991 Catalog Request failed: %s", db_strerror(jcr->db));
258 /* Set first written time if this is first job */
259 if (mr.VolJobs == 0 || sdmr.VolJobs == 1) {
260 mr.FirstWritten = jcr->start_time; /* use Job start time as first write */
262 Dmsg2(200, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
263 /* Copy updated values to original media record */
264 mr.VolJobs = sdmr.VolJobs;
265 mr.VolFiles = sdmr.VolFiles;
266 mr.VolBlocks = sdmr.VolBlocks;
267 mr.VolBytes = sdmr.VolBytes;
268 mr.VolMounts = sdmr.VolMounts;
269 mr.VolErrors = sdmr.VolErrors;
270 mr.VolWrites = sdmr.VolWrites;
271 mr.LastWritten = sdmr.LastWritten;
272 bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
276 * Update Media Record
279 /* Check limits and expirations if "Append" and not a relable request */
280 if (strcmp(mr.VolStatus, "Append") == 0 && !relabel) {
281 /* First handle Max Volume Bytes */
282 if ((mr.MaxVolBytes > 0 && mr.VolBytes >= mr.MaxVolBytes)) {
283 Jmsg(jcr, M_INFO, 0, _("Max Volume bytes exceeded. "
284 "Marking Volume \"%s\" as Full.\n"), mr.VolumeName);
285 strcpy(mr.VolStatus, "Full");
287 /* Now see if Volume should only be used once */
288 } else if (mr.VolBytes > 0 && jcr->pool->use_volume_once) {
289 Jmsg(jcr, M_INFO, 0, _("Volume used once. "
290 "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
291 strcpy(mr.VolStatus, "Used");
293 /* Now see if Max Jobs written to volume */
294 } else if (mr.MaxVolJobs > 0 && mr.MaxVolJobs <= mr.VolJobs) {
295 Jmsg(jcr, M_INFO, 0, _("Max Volume jobs exceeded. "
296 "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
297 strcpy(mr.VolStatus, "Used");
299 /* Now see if Max Files written to volume */
300 } else if (mr.MaxVolFiles > 0 && mr.MaxVolFiles <= mr.VolFiles) {
301 Jmsg(jcr, M_INFO, 0, _("Max Volume files exceeded. "
302 "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
303 strcpy(mr.VolStatus, "Used");
305 /* Finally, check Use duration expiration */
306 } else if (mr.VolUseDuration > 0) {
307 utime_t now = time(NULL);
308 /* See if Vol Use has expired */
309 if (mr.VolUseDuration <= (now - mr.FirstWritten)) {
310 Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "
311 "Marking Volume \"%s\"as Used.\n"), mr.VolumeName);
312 strcpy(mr.VolStatus, "Used"); /* yes, mark as used */
316 Dmsg2(200, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
317 if (db_update_media_record(jcr, jcr->db, &mr)) {
318 bnet_fsend(bs, OK_update);
319 Dmsg0(190, "send OK\n");
321 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
322 db_strerror(jcr->db));
323 bnet_fsend(bs, "1992 Update Media error\n");
324 Dmsg0(190, "send error\n");
328 * Request to create a JobMedia record
330 } else if (sscanf(bs->msg, Create_job_media, &Job,
331 &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
332 &jm.StartBlock, &jm.EndBlock) == 7) {
334 jm.JobId = jcr->JobId;
335 jm.MediaId = jcr->MediaId;
336 Dmsg6(100, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
337 jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
338 if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
339 Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
340 db_strerror(jcr->db));
341 bnet_fsend(bs, "1991 Update JobMedia error\n");
343 Dmsg0(100, "JobMedia record created\n");
344 bnet_fsend(bs, OK_update);
348 omsg = get_memory(bs->msglen+1);
349 pm_strcpy(&omsg, bs->msg);
350 bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
351 Jmsg1(jcr, M_ERROR, 0, _("Invalid Catalog request: %s"), omsg);
354 Dmsg1(120, ">CatReq response: %s", bs->msg);
355 Dmsg1(200, "Leave catreq jcr 0x%x\n", jcr);
360 * Update File Attributes in the catalog with data
361 * sent by the Storage daemon. Note, we receive the whole
362 * attribute record, but we select out only the stat packet,
363 * VolSessionId, VolSessionTime, FileIndex, and file name
364 * to store in the catalog.
366 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
369 uint32_t VolSessionId, VolSessionTime;
378 if (!jcr->pool->catalog_files) {
381 db_start_transaction(jcr, jcr->db); /* start transaction if not already open */
382 skip_nonspaces(&p); /* UpdCat */
384 skip_nonspaces(&p); /* Job=nnn */
386 skip_nonspaces(&p); /* FileAttributes */
389 unser_uint32(VolSessionId);
390 unser_uint32(VolSessionTime);
391 unser_int32(FileIndex);
393 unser_uint32(data_len);
394 p += unser_length(p);
396 Dmsg1(99, "UpdCat msg=%s\n", bs->msg);
397 Dmsg5(99, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
398 VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
400 if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_UNIX_ATTRIBUTES_EX) {
401 skip_nonspaces(&p); /* skip FileIndex */
403 skip_nonspaces(&p); /* skip FileType */
406 len = strlen(fname); /* length before attributes */
407 attr = &fname[len+1];
409 Dmsg2(109, "dird<stored: stream=%d %s\n", Stream, fname);
410 Dmsg1(109, "dird<stored: attr=%s\n", attr);
413 ar.FileIndex = FileIndex;
416 ar.JobId = jcr->JobId;
418 Dmsg2(111, "dird<filed: stream=%d %s\n", Stream, fname);
419 Dmsg1(120, "dird<filed: attr=%s\n", attr);
421 if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
422 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
424 /* Save values for SIG update */
425 jcr->FileId = ar.FileId;
426 jcr->FileIndex = FileIndex;
427 } else if (Stream == STREAM_MD5_SIGNATURE || Stream == STREAM_SHA1_SIGNATURE) {
429 if (jcr->FileIndex != FileIndex) {
430 Jmsg(jcr, M_WARNING, 0, "Got MD5/SHA1 but not same File as attributes\n");
432 /* Update signature in catalog */
433 char SIGbuf[50]; /* 24 bytes should be enough */
435 if (Stream == STREAM_MD5_SIGNATURE) {
442 bin_to_base64(SIGbuf, fname, len);
443 Dmsg3(190, "SIGlen=%d SIG=%s type=%d\n", strlen(SIGbuf), SIGbuf, Stream);
444 if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIGbuf, type)) {
445 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5/SHA1. %s"),
446 db_strerror(jcr->db));