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) 2001-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
20 version 2 as amended with additional clauses defined in the
21 file LICENSE in the main source directory.
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
26 the file LICENSE for additional details.
34 * Handle catalog request
35 * For now, we simply return next Volume to be used
38 /* Requests from the Storage daemon */
39 static char Find_media[] = "CatReq Job=%127s FindMedia=%d pool_name=%127s media_type=%127s\n";
40 static char Get_Vol_Info[] = "CatReq Job=%127s GetVolInfo VolName=%127s write=%d\n";
42 static char Update_media[] = "CatReq Job=%127s UpdateMedia VolName=%s"
43 " VolJobs=%u VolFiles=%u VolBlocks=%u VolBytes=%" lld " VolMounts=%u"
44 " VolErrors=%u VolWrites=%u MaxVolBytes=%" lld " EndTime=%d VolStatus=%10s"
45 " Slot=%d relabel=%d InChanger=%d VolReadTime=%" lld " VolWriteTime=%" lld
48 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
49 " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
50 " StartBlock=%u EndBlock=%u Copy=%d Strip=%d\n";
53 /* Responses sent to Storage daemon */
54 static char OK_media[] = "1000 OK VolName=%s VolJobs=%u VolFiles=%u"
55 " VolBlocks=%u VolBytes=%s VolMounts=%u VolErrors=%u VolWrites=%u"
56 " MaxVolBytes=%s VolCapacityBytes=%s VolStatus=%s Slot=%d"
57 " MaxVolJobs=%u MaxVolFiles=%u InChanger=%d VolReadTime=%s"
58 " VolWriteTime=%s EndFile=%u EndBlock=%u VolParts=%u LabelType=%d\n";
60 static char OK_create[] = "1000 OK CreateJobMedia\n";
63 static int send_volume_info_to_storage_daemon(JCR *jcr, BSOCK *sd, MEDIA_DBR *mr)
66 char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50];
68 jcr->MediaId = mr->MediaId;
69 pm_strcpy(jcr->VolumeName, mr->VolumeName);
70 bash_spaces(mr->VolumeName);
71 stat = bnet_fsend(sd, OK_media, mr->VolumeName, mr->VolJobs,
72 mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
73 mr->VolMounts, mr->VolErrors, mr->VolWrites,
74 edit_uint64(mr->MaxVolBytes, ed2),
75 edit_uint64(mr->VolCapacityBytes, ed3),
76 mr->VolStatus, mr->Slot, mr->MaxVolJobs, mr->MaxVolFiles,
78 edit_uint64(mr->VolReadTime, ed4),
79 edit_uint64(mr->VolWriteTime, ed5),
80 mr->EndFile, mr->EndBlock,
83 unbash_spaces(mr->VolumeName);
84 Dmsg2(100, "Vol Info for %s: %s", jcr->Job, sd->msg);
88 void catalog_request(JCR *jcr, BSOCK *bs)
92 char Job[MAX_NAME_LENGTH];
93 char pool_name[MAX_NAME_LENGTH];
94 int index, ok, label, writing;
98 memset(&mr, 0, sizeof(mr));
99 memset(&sdmr, 0, sizeof(sdmr));
100 memset(&jm, 0, sizeof(jm));
103 * Request to find next appendable Volume for this Job
105 Dmsg1(100, "catreq %s", bs->msg);
107 omsg = get_memory(bs->msglen+1);
108 pm_strcpy(omsg, bs->msg);
109 bnet_fsend(bs, _("1990 Invalid Catalog Request: %s"), omsg);
110 Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request; DB not open: %s"), omsg);
115 * Find next appendable medium for SD
117 if (sscanf(bs->msg, Find_media, &Job, &index, &pool_name, &mr.MediaType) == 4) {
118 memset(&pr, 0, sizeof(pr));
119 bstrncpy(pr.Name, pool_name, sizeof(pr.Name));
120 unbash_spaces(pr.Name);
121 ok = db_get_pool_record(jcr, jcr->db, &pr);
123 mr.PoolId = pr.PoolId;
124 ok = find_next_volume_for_append(jcr, &mr, true /*permit create new vol*/);
127 * Send Find Media response to Storage daemon
130 send_volume_info_to_storage_daemon(jcr, bs, &mr);
132 bnet_fsend(bs, _("1901 No Media.\n"));
133 Dmsg0(500, "1901 No Media.\n");
137 * Request to find specific Volume information
139 } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
140 Dmsg1(100, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
144 unbash_spaces(mr.VolumeName);
145 if (db_get_media_record(jcr, jcr->db, &mr)) {
146 const char *reason = NULL; /* detailed reason for rejection */
148 * If we are reading, accept any volume (reason == NULL)
149 * If we are writing, check if the Volume is valid
150 * for this job, and do a recycle if necessary
154 * SD wants to write this Volume, so make
155 * sure it is suitable for this job, i.e.
156 * Pool matches, and it is either Append or Recycle
157 * and Media Type matches and Pool allows any volume.
159 if (mr.PoolId != jcr->PoolId) {
160 reason = _("not in Pool");
161 } else if (strcmp(mr.MediaType, jcr->store->media_type) != 0) {
162 reason = _("not correct MediaType");
166 * This test (accept_any_volume) is turned off
167 * because it doesn't properly check if the volume
168 * really is out of sequence!
170 * } else if (!jcr->pool->accept_any_volume) {
171 * reason = "Volume not in sequence";
175 * Now try recycling if necessary
176 * reason set non-NULL if we cannot use it
178 check_if_volume_valid_or_recyclable(jcr, &mr, &reason);
181 if (reason == NULL) {
183 * Send Find Media response to Storage daemon
185 send_volume_info_to_storage_daemon(jcr, bs, &mr);
187 /* Not suitable volume */
188 bnet_fsend(bs, _("1998 Volume \"%s\" status is %s, %s.\n"), mr.VolumeName,
189 mr.VolStatus, reason);
193 bnet_fsend(bs, _("1997 Volume \"%s\" not in catalog.\n"), mr.VolumeName);
194 Dmsg1(100, "1997 Volume \"%s\" not in catalog.\n", mr.VolumeName);
198 * Request to update Media record. Comes typically at the end
199 * of a Storage daemon Job Session, when labeling/relabeling a
200 * Volume, or when an EOF mark is written.
202 } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName,
203 &sdmr.VolJobs, &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes,
204 &sdmr.VolMounts, &sdmr.VolErrors, &sdmr.VolWrites, &sdmr.MaxVolBytes,
205 &sdmr.LastWritten, &sdmr.VolStatus, &sdmr.Slot, &label, &sdmr.InChanger,
206 &sdmr.VolReadTime, &sdmr.VolWriteTime, &sdmr.VolParts) == 18) {
209 Dmsg3(400, "Update media %s oldStat=%s newStat=%s\n", sdmr.VolumeName,
210 mr.VolStatus, sdmr.VolStatus);
211 bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
212 unbash_spaces(mr.VolumeName);
213 if (!db_get_media_record(jcr, jcr->db, &mr)) {
214 Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
215 mr.VolumeName, db_strerror(jcr->db));
216 bnet_fsend(bs, _("1991 Catalog Request for vol=%s failed: %s"),
217 mr.VolumeName, db_strerror(jcr->db));
221 /* Set first written time if this is first job */
222 if (mr.FirstWritten == 0) {
223 mr.FirstWritten = jcr->start_time; /* use Job start time as first write */
224 mr.set_first_written = true;
226 /* If we just labeled the tape set time */
227 if (label || mr.LabelDate == 0) {
228 mr.LabelDate = jcr->start_time;
229 mr.set_label_date = true;
230 Dmsg2(400, "label=%d labeldate=%d\n", label, mr.LabelDate);
233 * Insanity check for VolFiles get set to a smaller value
235 if (sdmr.VolFiles < mr.VolFiles) {
236 Jmsg(jcr, M_FATAL, 0, _("Volume Files at %u being set to %u"
237 " for Volume \"%s\". This is incorrect.\n"),
238 mr.VolFiles, sdmr.VolFiles, mr.VolumeName);
239 bnet_fsend(bs, _("1992 Update Media error. VolFiles=%u, CatFiles=%u\n"),
240 sdmr.VolFiles, mr.VolFiles);
245 Dmsg2(400, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
246 /* Copy updated values to original media record */
247 mr.VolJobs = sdmr.VolJobs;
248 mr.VolFiles = sdmr.VolFiles;
249 mr.VolBlocks = sdmr.VolBlocks;
250 mr.VolBytes = sdmr.VolBytes;
251 mr.VolMounts = sdmr.VolMounts;
252 mr.VolErrors = sdmr.VolErrors;
253 mr.VolWrites = sdmr.VolWrites;
254 mr.LastWritten = sdmr.LastWritten;
256 mr.InChanger = sdmr.InChanger;
257 mr.VolReadTime = sdmr.VolReadTime;
258 mr.VolWriteTime = sdmr.VolWriteTime;
259 mr.VolParts = sdmr.VolParts;
260 bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
261 if (jcr->store->StorageId) {
262 mr.StorageId = jcr->store->StorageId;
265 Dmsg2(400, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
267 * Update the database, then before sending the response to the
268 * SD, check if the Volume has expired.
270 if (!db_update_media_record(jcr, jcr->db, &mr)) {
271 Jmsg(jcr, M_FATAL, 0, _("Catalog error updating Media record. %s"),
272 db_strerror(jcr->db));
273 bnet_fsend(bs, _("1993 Update Media error\n"));
274 Dmsg0(400, "send error\n");
276 (void)has_volume_expired(jcr, &mr);
277 send_volume_info_to_storage_daemon(jcr, bs, &mr);
282 * Request to create a JobMedia record
284 } else if (sscanf(bs->msg, Create_job_media, &Job,
285 &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
286 &jm.StartBlock, &jm.EndBlock, &jm.Copy, &jm.Stripe) == 9) {
288 jm.JobId = jcr->JobId;
289 jm.MediaId = jcr->MediaId;
290 Dmsg6(400, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
291 jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
292 if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
293 Jmsg(jcr, M_FATAL, 0, _("Catalog error creating JobMedia record. %s"),
294 db_strerror(jcr->db));
295 bnet_fsend(bs, _("1991 Update JobMedia error\n"));
297 Dmsg0(400, "JobMedia record created\n");
298 bnet_fsend(bs, OK_create);
302 omsg = get_memory(bs->msglen+1);
303 pm_strcpy(omsg, bs->msg);
304 bnet_fsend(bs, _("1990 Invalid Catalog Request: %s"), omsg);
305 Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request: %s"), omsg);
308 Dmsg1(400, ">CatReq response: %s", bs->msg);
309 Dmsg1(400, "Leave catreq jcr 0x%x\n", jcr);
314 * Update File Attributes in the catalog with data
315 * sent by the Storage daemon. Note, we receive the whole
316 * attribute record, but we select out only the stat packet,
317 * VolSessionId, VolSessionTime, FileIndex, and file name
318 * to store in the catalog.
320 void catalog_update(JCR *jcr, BSOCK *bs)
323 uint32_t VolSessionId, VolSessionTime;
333 if (!jcr->pool->catalog_files) {
334 return; /* user disabled cataloging */
337 omsg = get_memory(bs->msglen+1);
338 pm_strcpy(omsg, bs->msg);
339 bnet_fsend(bs, _("1991 Invalid Catalog Update: %s"), omsg);
340 Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog Update; DB not open: %s"), omsg);
345 /* Start transaction allocates jcr->attr and jcr->ar if needed */
346 db_start_transaction(jcr, jcr->db); /* start transaction if not already open */
349 /* Start by scanning directly in the message buffer to get Stream
350 * there may be a cached attr so we cannot yet write into
351 * jcr->attr or jcr->ar
354 skip_nonspaces(&p); /* UpdCat */
356 skip_nonspaces(&p); /* Job=nnn */
358 skip_nonspaces(&p); /* FileAttributes */
361 unser_uint32(VolSessionId);
362 unser_uint32(VolSessionTime);
363 unser_int32(FileIndex);
365 unser_uint32(data_len);
366 p += unser_length(p);
368 Dmsg1(400, "UpdCat msg=%s\n", bs->msg);
369 Dmsg5(400, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
370 VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
372 if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_UNIX_ATTRIBUTES_EX) {
373 if (jcr->cached_attribute) {
374 Dmsg2(400, "Cached attr. Stream=%d fname=%s\n", ar->Stream, ar->fname);
375 if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
376 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
379 /* Any cached attr is flushed so we can reuse jcr->attr and jcr->ar */
380 jcr->attr = check_pool_memory_size(jcr->attr, bs->msglen);
381 memcpy(jcr->attr, bs->msg, bs->msglen);
382 p = jcr->attr - bs->msg + p; /* point p into jcr->attr */
383 skip_nonspaces(&p); /* skip FileIndex */
385 skip_nonspaces(&p); /* skip FileType */
388 len = strlen(fname); /* length before attributes */
389 attr = &fname[len+1];
391 Dmsg2(400, "dird<stored: stream=%d %s\n", Stream, fname);
392 Dmsg1(400, "dird<stored: attr=%s\n", attr);
395 ar->FileIndex = FileIndex;
398 ar->JobId = jcr->JobId;
401 jcr->cached_attribute = true;
403 Dmsg2(400, "dird<filed: stream=%d %s\n", Stream, fname);
404 Dmsg1(400, "dird<filed: attr=%s\n", attr);
407 if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
408 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
411 } else if (Stream == STREAM_MD5_SIGNATURE || Stream == STREAM_SHA1_SIGNATURE) {
413 if (ar->FileIndex != FileIndex) {
414 Jmsg(jcr, M_WARNING, 0, _("Got MD5/SHA1 but not same File as attributes\n"));
416 /* Update signature in catalog */
417 char SIGbuf[50]; /* 24 bytes should be enough */
419 if (Stream == STREAM_MD5_SIGNATURE) {
426 bin_to_base64(SIGbuf, fname, len);
427 Dmsg3(400, "SIGlen=%d SIG=%s type=%d\n", strlen(SIGbuf), SIGbuf, Stream);
428 if (jcr->cached_attribute) {
431 Dmsg2(400, "Cached attr with SIG. Stream=%d fname=%s\n", ar->Stream, ar->fname);
432 if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
433 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
435 jcr->cached_attribute = false;
437 if (!db_add_SIG_to_file_record(jcr, jcr->db, ar->FileId, SIGbuf, type)) {
438 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5/SHA1. %s"),
439 db_strerror(jcr->db));