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 */
106 ok = newVolume(jcr, &mr);
110 /* Check if use duration has expired */
111 if (ok && mr.VolUseDuration > 0 &&
112 strcmp(mr.VolStatus, "Recycle") != 0) {
113 utime_t now = time(NULL);
114 utime_t start = str_to_utime(mr.cFirstWritten);
115 if (start > 0 && mr.VolUseDuration <= (now - start)) {
116 Dmsg4(100, "Duration=%d now=%d start=%d now-start=%d\n",
117 (int)jcr->pool->VolUseDuration, (int)now, (int)start, (int)(now-start));
118 Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "
119 "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
120 strcpy(mr.VolStatus, "Used"); /* yes, mark as used */
121 if (!db_update_media_record(jcr->db, &mr)) {
122 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
123 db_strerror(jcr->db));
124 } else if (retry++ < 200) { /* sanity check */
127 Jmsg(jcr, M_ERROR, 0, _(
128 "We seem to be looping trying to find the next volume. I give up. Ask operator.\n"));
130 ok = FALSE; /* give up */
135 * Send Find Media response to Storage daemon
138 char ed1[50], ed2[50], ed3[50];
139 jcr->MediaId = mr.MediaId;
140 pm_strcpy(&jcr->VolumeName, mr.VolumeName);
141 bash_spaces(mr.VolumeName);
142 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
143 mr.VolFiles, mr.VolBlocks, edit_uint64(mr.VolBytes, ed1),
144 mr.VolMounts, mr.VolErrors, mr.VolWrites,
145 edit_uint64(mr.MaxVolBytes, ed2),
146 edit_uint64(mr.VolCapacityBytes, ed3),
147 mr.VolStatus, mr.Slot, mr.MaxVolJobs, mr.MaxVolFiles);
149 bnet_fsend(bs, "1999 No Media\n");
153 * Request to find specific Volume information
155 } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
156 Dmsg1(120, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
160 unbash_spaces(mr.VolumeName);
161 if (db_get_media_record(jcr->db, &mr)) {
163 jcr->MediaId = mr.MediaId;
164 Dmsg1(120, "VolumeInfo MediaId=%d\n", jcr->MediaId);
165 pm_strcpy(&jcr->VolumeName, mr.VolumeName);
167 VolSuitable = 1; /* accept anything for read */
170 * Make sure this volume is suitable for this job, i.e.
171 * it is either Append or Recycle and Media Type matches
172 * and Pool allows any volume.
174 if (mr.PoolId == jcr->PoolId &&
175 (strcmp(mr.VolStatus, "Append") == 0 ||
176 strcmp(mr.VolStatus, "Recycle") == 0) &&
177 strcmp(mr.MediaType, jcr->store->media_type) == 0 &&
178 jcr->pool->accept_any_volume) {
183 char ed1[50], ed2[50], ed3[50];
185 * Send Find Media response to Storage daemon
187 bash_spaces(mr.VolumeName);
188 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
189 mr.VolFiles, mr.VolBlocks, edit_uint64(mr.VolBytes, ed1),
190 mr.VolMounts, mr.VolErrors, mr.VolWrites,
191 edit_uint64(mr.MaxVolBytes, ed2),
192 edit_uint64(mr.VolCapacityBytes, ed3),
193 mr.VolStatus, mr.Slot, mr.MaxVolJobs, mr.MaxVolFiles);
194 Dmsg5(200, "get_media_record PoolId=%d wanted %d, Status=%s, Slot=%d \
195 MediaType=%s\n", mr.PoolId, jcr->PoolId, mr.VolStatus, mr.Slot, mr.MediaType);
197 /* Not suitable volume */
198 bnet_fsend(bs, "1998 Volume not appropriate.\n");
202 bnet_fsend(bs, "1999 Volume Not Found.\n");
207 * Request to update Media record. Comes typically at the end
208 * of a Storage daemon Job Session
210 } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName, &sdmr.VolJobs,
211 &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes, &sdmr.VolMounts, &sdmr.VolErrors,
212 &sdmr.VolWrites, &sdmr.MaxVolBytes, &sdmr.LastWritten, &sdmr.VolStatus,
213 &sdmr.Slot, &relabel) == 14) {
215 bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
216 if (!db_get_media_record(jcr->db, &mr)) {
217 Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
218 mr.VolumeName, db_strerror(jcr->db));
219 bnet_fsend(bs, "1991 Catalog Request failed: %s", db_strerror(jcr->db));
222 /* Copy updated values to original media record */
223 mr.VolJobs = sdmr.VolJobs;
224 mr.VolFiles = sdmr.VolFiles;
225 mr.VolBlocks = sdmr.VolBlocks;
226 mr.VolBytes = sdmr.VolBytes;
227 mr.VolMounts = sdmr.VolMounts;
228 mr.VolErrors = sdmr.VolErrors;
229 mr.VolWrites = sdmr.VolWrites;
230 mr.LastWritten = sdmr.LastWritten;
231 bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
235 * Update Media Record
238 /* First handle Max Volume Bytes */
239 if ((mr.MaxVolBytes > 0 && mr.VolBytes >= mr.MaxVolBytes)) {
240 Jmsg(jcr, M_INFO, 0, _("Max Volume bytes exceeded. "
241 "Marking Volume \"%s\" as Full.\n"), mr.VolumeName);
242 strcpy(mr.VolStatus, "Full");
244 /* Now see if Volume should only be used once */
245 } else if (mr.VolBytes > 0 && jcr->pool->use_volume_once) {
246 Jmsg(jcr, M_INFO, 0, _("Volume used once. "
247 "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
248 strcpy(mr.VolStatus, "Used");
250 /* Now see if Max Jobs written to volume */
251 } else if (mr.MaxVolJobs > 0 && mr.MaxVolJobs <= mr.VolJobs) {
252 Jmsg(jcr, M_INFO, 0, _("Max Volume jobs exceeded. "
253 "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
254 strcpy(mr.VolStatus, "Used");
256 /* Now see if Max Files written to volume */
257 } else if (mr.MaxVolFiles > 0 && mr.MaxVolFiles <= mr.VolFiles) {
258 Jmsg(jcr, M_INFO, 0, _("Max Volume files exceeded. "
259 "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
260 strcpy(mr.VolStatus, "Used");
262 /* Finally, check Use duration expiration */
263 } else if (mr.VolUseDuration > 0) {
265 utime_t now = time(NULL);
266 start = str_to_utime(mr.cFirstWritten);
267 /* See if Vol Use has expired */
268 if (start > 0 && mr.VolUseDuration <= (now - start)) {
269 Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "
270 "Marking Volume \"%s\"as Used.\n"), mr.VolumeName);
271 strcpy(mr.VolStatus, "Used"); /* yes, mark as used */
275 Dmsg2(200, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
276 if (db_update_media_record(jcr->db, &mr)) {
277 bnet_fsend(bs, OK_update);
278 Dmsg0(190, "send OK\n");
280 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
281 db_strerror(jcr->db));
282 bnet_fsend(bs, "1992 Update Media error\n");
283 Dmsg0(190, "send error\n");
287 * Request to create a JobMedia record
289 } else if (sscanf(bs->msg, Create_job_media, &Job,
290 &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
291 &jm.StartBlock, &jm.EndBlock) == 7) {
293 jm.JobId = jcr->JobId;
294 jm.MediaId = jcr->MediaId;
295 Dmsg6(100, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
296 jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
297 if (!db_create_jobmedia_record(jcr->db, &jm)) {
298 Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
299 db_strerror(jcr->db));
300 bnet_fsend(bs, "1991 Update JobMedia error\n");
302 Dmsg0(100, "JobMedia record created\n");
303 bnet_fsend(bs, OK_update);
307 omsg = get_memory(bs->msglen+1);
308 pm_strcpy(&omsg, bs->msg);
309 bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
310 Jmsg1(jcr, M_ERROR, 0, _("Invalid Catalog request: %s"), omsg);
313 Dmsg1(120, ">CatReq response: %s", bs->msg);
314 Dmsg1(200, "Leave catreq jcr 0x%x\n", jcr);
319 * Update File Attributes in the catalog with data
320 * sent by the Storage daemon.
322 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
325 uint32_t VolSessionId, VolSessionTime;
334 if (!jcr->pool->catalog_files) {
337 db_start_transaction(jcr->db); /* start transaction if not already open */
338 skip_nonspaces(&p); /* UpdCat */
340 skip_nonspaces(&p); /* Job=nnn */
342 skip_nonspaces(&p); /* FileAttributes */
345 unser_uint32(VolSessionId);
346 unser_uint32(VolSessionTime);
347 unser_int32(FileIndex);
349 unser_uint32(data_len);
350 p += unser_length(p);
352 Dmsg1(99, "UpdCat msg=%s\n", bs->msg);
353 Dmsg5(99, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
354 VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
356 if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_WIN32_ATTRIBUTES) {
357 skip_nonspaces(&p); /* skip FileIndex */
359 skip_nonspaces(&p); /* skip FileType */
362 len = strlen(fname); /* length before attributes */
363 attr = &fname[len+1];
365 Dmsg2(109, "dird<stored: stream=%d %s\n", Stream, fname);
366 Dmsg1(109, "dird<stored: attr=%s\n", attr);
369 ar.FileIndex = FileIndex;
372 ar.JobId = jcr->JobId;
374 Dmsg2(111, "dird<filed: stream=%d %s\n", Stream, fname);
375 Dmsg1(120, "dird<filed: attr=%s\n", attr);
377 if (!db_create_file_attributes_record(jcr->db, &ar)) {
378 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
380 /* Save values for MD5 update */
381 jcr->FileId = ar.FileId;
382 jcr->FileIndex = FileIndex;
383 } else if (Stream == STREAM_MD5_SIGNATURE) {
385 if (jcr->FileIndex != FileIndex) {
386 Jmsg(jcr, M_WARNING, 0, "Got MD5 but not same File as attributes\n");
388 /* Update MD5 signature in catalog */
389 char MD5buf[50]; /* 24 bytes should be enough */
390 bin_to_base64(MD5buf, fname, 16);
391 Dmsg2(190, "MD5len=%d MD5=%s\n", strlen(MD5buf), MD5buf);
392 if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, MD5buf)) {
393 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5. %s"),
394 db_strerror(jcr->db));