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-2005 Kern Sibbald
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 PoolId=%lld\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 InChanger=%d VolReadTime=%" lld " VolWriteTime=%" lld
53 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
54 " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
55 " StartBlock=%u EndBlock=%u\n";
58 /* Responses sent to Storage daemon */
59 static char OK_media[] = "1000 OK VolName=%s VolJobs=%u VolFiles=%u"
60 " VolBlocks=%u VolBytes=%s VolMounts=%u VolErrors=%u VolWrites=%u"
61 " MaxVolBytes=%s VolCapacityBytes=%s VolStatus=%s Slot=%d"
62 " MaxVolJobs=%u MaxVolFiles=%u InChanger=%d VolReadTime=%s"
63 " VolWriteTime=%s EndFile=%u EndBlock=%u VolParts=%u LabelType=%d\n";
65 static char OK_create[] = "1000 OK CreateJobMedia\n";
68 static int send_volume_info_to_storage_daemon(JCR *jcr, BSOCK *sd, MEDIA_DBR *mr)
71 char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50];
73 jcr->MediaId = mr->MediaId;
74 pm_strcpy(jcr->VolumeName, mr->VolumeName);
75 bash_spaces(mr->VolumeName);
76 stat = bnet_fsend(sd, OK_media, mr->VolumeName, mr->VolJobs,
77 mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
78 mr->VolMounts, mr->VolErrors, mr->VolWrites,
79 edit_uint64(mr->MaxVolBytes, ed2),
80 edit_uint64(mr->VolCapacityBytes, ed3),
81 mr->VolStatus, mr->Slot, mr->MaxVolJobs, mr->MaxVolFiles,
83 edit_uint64(mr->VolReadTime, ed4),
84 edit_uint64(mr->VolWriteTime, ed5),
85 mr->EndFile, mr->EndBlock,
88 unbash_spaces(mr->VolumeName);
89 Dmsg2(400, "Vol Info for %s: %s", jcr->Job, sd->msg);
93 void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
97 char Job[MAX_NAME_LENGTH];
99 int index, ok, label, writing;
102 memset(&mr, 0, sizeof(mr));
103 memset(&sdmr, 0, sizeof(sdmr));
104 memset(&jm, 0, sizeof(jm));
107 * Request to find next appendable Volume for this Job
109 Dmsg1(400, "catreq %s", bs->msg);
111 omsg = get_memory(bs->msglen+1);
112 pm_strcpy(omsg, bs->msg);
113 bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
114 Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request; DB not open: %s"), omsg);
119 * Find next appendable medium for SD
121 if (sscanf(bs->msg, Find_media, &Job, &index, &PoolId) == 3) {
123 ok = find_next_volume_for_append(jcr, &mr, true /*permit create new vol*/);
125 * Send Find Media response to Storage daemon
128 send_volume_info_to_storage_daemon(jcr, bs, &mr);
130 bnet_fsend(bs, "1901 No Media.\n");
134 * Request to find specific Volume information
136 } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
137 Dmsg1(400, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
141 unbash_spaces(mr.VolumeName);
142 if (db_get_media_record(jcr, jcr->db, &mr)) {
143 const char *reason = NULL; /* detailed reason for rejection */
145 * If we are reading, accept any volume (reason == NULL)
146 * If we are writing, check if the Volume is valid
147 * for this job, and do a recycle if necessary
151 * SD wants to write this Volume, so make
152 * sure it is suitable for this job, i.e.
153 * Pool matches, and it is either Append or Recycle
154 * and Media Type matches and Pool allows any volume.
156 if (mr.PoolId != jcr->PoolId) {
157 reason = "not in Pool";
158 } else if (strcmp(mr.MediaType, jcr->store->media_type) != 0) {
159 reason = "not correct MediaType";
163 * This test (accept_any_volume) is turned off
164 * because it doesn't properly check if the volume
165 * really is out of sequence!
167 * } else if (!jcr->pool->accept_any_volume) {
168 * reason = "Volume not in sequence";
172 * Now try recycling if necessary
173 * reason set non-NULL if we cannot use it
175 check_if_volume_valid_or_recyclable(jcr, &mr, &reason);
178 if (reason == NULL) {
180 * Send Find Media response to Storage daemon
182 send_volume_info_to_storage_daemon(jcr, bs, &mr);
184 /* Not suitable volume */
185 bnet_fsend(bs, "1998 Volume \"%s\" status is %s, %s.\n", mr.VolumeName,
186 mr.VolStatus, reason);
190 bnet_fsend(bs, "1997 Volume \"%s\" not in catalog.\n", mr.VolumeName);
194 * Request to update Media record. Comes typically at the end
195 * of a Storage daemon Job Session, when labeling/relabeling a
196 * Volume, or when an EOF mark is written.
198 } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName,
199 &sdmr.VolJobs, &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes,
200 &sdmr.VolMounts, &sdmr.VolErrors, &sdmr.VolWrites, &sdmr.MaxVolBytes,
201 &sdmr.LastWritten, &sdmr.VolStatus, &sdmr.Slot, &label, &sdmr.InChanger,
202 &sdmr.VolReadTime, &sdmr.VolWriteTime, &sdmr.VolParts) == 18) {
205 Dmsg3(400, "Update media %s oldStat=%s newStat=%s\n", sdmr.VolumeName,
206 mr.VolStatus, sdmr.VolStatus);
207 bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
208 unbash_spaces(mr.VolumeName);
209 if (!db_get_media_record(jcr, jcr->db, &mr)) {
210 Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
211 mr.VolumeName, db_strerror(jcr->db));
212 bnet_fsend(bs, "1991 Catalog Request for vol=%s failed: %s",
213 mr.VolumeName, db_strerror(jcr->db));
217 /* Set first written time if this is first job */
218 if (mr.FirstWritten == 0) {
219 mr.FirstWritten = jcr->start_time; /* use Job start time as first write */
220 mr.set_first_written = true;
222 /* If we just labeled the tape set time */
223 if (label || mr.LabelDate == 0) {
224 mr.LabelDate = jcr->start_time;
225 mr.set_label_date = true;
226 Dmsg2(400, "label=%d labeldate=%d\n", label, mr.LabelDate);
229 * Insanity check for VolFiles get set to a smaller value
231 if (sdmr.VolFiles < mr.VolFiles) {
232 Jmsg(jcr, M_ERROR, 0, _("ERROR!! Volume Files at %u being set to %u. This is probably wrong.\n"),
233 mr.VolFiles, sdmr.VolFiles);
236 Dmsg2(400, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
237 /* Copy updated values to original media record */
238 mr.VolJobs = sdmr.VolJobs;
239 mr.VolFiles = sdmr.VolFiles;
240 mr.VolBlocks = sdmr.VolBlocks;
241 mr.VolBytes = sdmr.VolBytes;
242 mr.VolMounts = sdmr.VolMounts;
243 mr.VolErrors = sdmr.VolErrors;
244 mr.VolWrites = sdmr.VolWrites;
245 mr.LastWritten = sdmr.LastWritten;
247 mr.InChanger = sdmr.InChanger;
248 mr.VolReadTime = sdmr.VolReadTime;
249 mr.VolWriteTime = sdmr.VolWriteTime;
250 mr.VolParts = sdmr.VolParts;
251 bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
253 Dmsg2(400, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
255 * Check if it has expired, and if not update the DB. Note, if
256 * Volume has expired, has_volume_expired() will update the DB.
258 if (has_volume_expired(jcr, &mr) || db_update_media_record(jcr, jcr->db, &mr)) {
259 send_volume_info_to_storage_daemon(jcr, bs, &mr);
261 Jmsg(jcr, M_FATAL, 0, _("Catalog error updating Media record. %s"),
262 db_strerror(jcr->db));
263 bnet_fsend(bs, "1992 Update Media error\n");
264 Dmsg0(400, "send error\n");
269 * Request to create a JobMedia record
271 } else if (sscanf(bs->msg, Create_job_media, &Job,
272 &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
273 &jm.StartBlock, &jm.EndBlock) == 7) {
275 jm.JobId = jcr->JobId;
276 jm.MediaId = jcr->MediaId;
277 Dmsg6(400, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
278 jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
279 if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
280 Jmsg(jcr, M_FATAL, 0, _("Catalog error creating JobMedia record. %s"),
281 db_strerror(jcr->db));
282 bnet_fsend(bs, "1991 Update JobMedia error\n");
284 Dmsg0(400, "JobMedia record created\n");
285 bnet_fsend(bs, OK_create);
289 omsg = get_memory(bs->msglen+1);
290 pm_strcpy(omsg, bs->msg);
291 bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
292 Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request: %s"), omsg);
295 Dmsg1(400, ">CatReq response: %s", bs->msg);
296 Dmsg1(400, "Leave catreq jcr 0x%x\n", jcr);
301 * Update File Attributes in the catalog with data
302 * sent by the Storage daemon. Note, we receive the whole
303 * attribute record, but we select out only the stat packet,
304 * VolSessionId, VolSessionTime, FileIndex, and file name
305 * to store in the catalog.
307 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
310 uint32_t VolSessionId, VolSessionTime;
319 if (!jcr->pool->catalog_files) {
322 db_start_transaction(jcr, jcr->db); /* start transaction if not already open */
323 skip_nonspaces(&p); /* UpdCat */
325 skip_nonspaces(&p); /* Job=nnn */
327 skip_nonspaces(&p); /* FileAttributes */
330 unser_uint32(VolSessionId);
331 unser_uint32(VolSessionTime);
332 unser_int32(FileIndex);
334 unser_uint32(data_len);
335 p += unser_length(p);
337 Dmsg1(400, "UpdCat msg=%s\n", bs->msg);
338 Dmsg5(400, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
339 VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
341 if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_UNIX_ATTRIBUTES_EX) {
342 skip_nonspaces(&p); /* skip FileIndex */
344 skip_nonspaces(&p); /* skip FileType */
347 len = strlen(fname); /* length before attributes */
348 attr = &fname[len+1];
350 Dmsg2(400, "dird<stored: stream=%d %s\n", Stream, fname);
351 Dmsg1(400, "dird<stored: attr=%s\n", attr);
354 ar.FileIndex = FileIndex;
357 ar.JobId = jcr->JobId;
359 Dmsg2(400, "dird<filed: stream=%d %s\n", Stream, fname);
360 Dmsg1(400, "dird<filed: attr=%s\n", attr);
362 if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
363 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
365 /* Save values for SIG update */
366 jcr->FileId = ar.FileId;
367 jcr->FileIndex = FileIndex;
368 } else if (Stream == STREAM_MD5_SIGNATURE || Stream == STREAM_SHA1_SIGNATURE) {
370 if (jcr->FileIndex != FileIndex) {
371 Jmsg(jcr, M_WARNING, 0, "Got MD5/SHA1 but not same File as attributes\n");
373 /* Update signature in catalog */
374 char SIGbuf[50]; /* 24 bytes should be enough */
376 if (Stream == STREAM_MD5_SIGNATURE) {
383 bin_to_base64(SIGbuf, fname, len);
384 Dmsg3(400, "SIGlen=%d SIG=%s type=%d\n", strlen(SIGbuf), SIGbuf, Stream);
385 if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIGbuf, type)) {
386 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5/SHA1. %s"),
387 db_strerror(jcr->db));