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 Drive=%d InChanger=%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 Drive=%d InChanger=%d\n";
63 static char OK_create[] = "1000 OK CreateJobMedia\n";
66 static int send_volume_info_to_storage_daemon(JCR *jcr, BSOCK *sd, MEDIA_DBR *mr)
69 char ed1[50], ed2[50], ed3[50];
71 jcr->MediaId = mr->MediaId;
72 pm_strcpy(&jcr->VolumeName, mr->VolumeName);
73 bash_spaces(mr->VolumeName);
74 stat = bnet_fsend(sd, OK_media, mr->VolumeName, mr->VolJobs,
75 mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
76 mr->VolMounts, mr->VolErrors, mr->VolWrites,
77 edit_uint64(mr->MaxVolBytes, ed2),
78 edit_uint64(mr->VolCapacityBytes, ed3),
79 mr->VolStatus, mr->Slot, mr->MaxVolJobs, mr->MaxVolFiles,
80 mr->Drive, mr->InChanger);
81 unbash_spaces(mr->VolumeName);
82 Dmsg2(100, "Vol Info for %s: %s", jcr->Job, sd->msg);
86 void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
90 char Job[MAX_NAME_LENGTH];
91 int index, ok, label, writing;
94 memset(&mr, 0, sizeof(mr));
95 memset(&sdmr, 0, sizeof(sdmr));
96 memset(&jm, 0, sizeof(jm));
99 * Request to find next appendable Volume for this Job
101 Dmsg1(200, "catreq %s", bs->msg);
102 if (sscanf(bs->msg, Find_media, &Job, &index) == 2) {
103 ok = find_next_volume_for_append(jcr, &mr, TRUE /*permit create new vol*/);
105 * Send Find Media response to Storage daemon
108 send_volume_info_to_storage_daemon(jcr, bs, &mr);
110 bnet_fsend(bs, "1901 No Media.\n");
114 * Request to find specific Volume information
116 } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
117 Dmsg1(400, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
121 unbash_spaces(mr.VolumeName);
122 if (db_get_media_record(jcr, jcr->db, &mr)) {
123 char *reason = NULL; /* detailed reason for rejection */
125 * If we are reading, accept any volume (reason == NULL)
126 * If we are writing, check if the Volume is valid
127 * for this job, and do a recycle if necessary
131 * SD wants to write this Volume, so make
132 * sure it is suitable for this job, i.e.
133 * Pool matches, and it is either Append or Recycle
134 * and Media Type matches and Pool allows any volume.
136 if (mr.PoolId != jcr->PoolId) {
137 reason = "not in Pool";
138 } else if (strcmp(mr.MediaType, jcr->store->media_type) != 0) {
139 reason = "not correct MediaType";
143 * This test (accept_any_volume) is turned off
144 * because it doesn't properly check if the volume
145 * really is out of sequence!
147 * } else if (!jcr->pool->accept_any_volume) {
148 * reason = "Volume not in sequence";
152 * Now try recycling if necessary
153 * reason set non-NULL if we cannot use it
155 check_if_volume_valid_or_recyclable(jcr, &mr, &reason);
158 if (reason == NULL) {
160 * Send Find Media response to Storage daemon
162 send_volume_info_to_storage_daemon(jcr, bs, &mr);
164 /* Not suitable volume */
165 bnet_fsend(bs, "1998 Volume \"%s\" status is %s, %s.\n", mr.VolumeName,
166 mr.VolStatus, reason);
170 bnet_fsend(bs, "1997 Volume \"%s\" not in catalog.\n", mr.VolumeName);
175 * Request to update Media record. Comes typically at the end
176 * of a Storage daemon Job Session or when labeling/relabeling a
179 } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName, &sdmr.VolJobs,
180 &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes, &sdmr.VolMounts, &sdmr.VolErrors,
181 &sdmr.VolWrites, &sdmr.MaxVolBytes, &sdmr.LastWritten, &sdmr.VolStatus,
182 &sdmr.Slot, &label, &sdmr.Drive, &sdmr.InChanger) == 16) {
185 Dmsg3(100, "Update media %s oldStat=%s newStat=%s\n", sdmr.VolumeName,
186 mr.VolStatus, sdmr.VolStatus);
187 bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
188 unbash_spaces(mr.VolumeName);
189 if (!db_get_media_record(jcr, jcr->db, &mr)) {
190 Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
191 mr.VolumeName, db_strerror(jcr->db));
192 bnet_fsend(bs, "1991 Catalog Request failed: %s", db_strerror(jcr->db));
196 /* Set first written time if this is first job */
197 if (mr.VolJobs == 0 || sdmr.VolJobs == 1) {
198 mr.FirstWritten = jcr->start_time; /* use Job start time as first write */
200 /* If we just labeled the tape set time */
201 Dmsg2(100, "label=%d labeldate=%d\n", label, mr.LabelDate);
202 if (label || mr.LabelDate == 0) {
203 mr.LabelDate = time(NULL);
205 Dmsg2(100, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
206 /* Copy updated values to original media record */
207 mr.VolJobs = sdmr.VolJobs;
208 mr.VolFiles = sdmr.VolFiles;
209 mr.VolBlocks = sdmr.VolBlocks;
210 mr.VolBytes = sdmr.VolBytes;
211 mr.VolMounts = sdmr.VolMounts;
212 mr.VolErrors = sdmr.VolErrors;
213 mr.VolWrites = sdmr.VolWrites;
214 mr.LastWritten = sdmr.LastWritten;
216 mr.Drive = sdmr.Drive;
217 mr.InChanger = sdmr.InChanger;
218 bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
220 Dmsg2(100, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
222 * Check if it has expired, and if not update the DB. Note, if
223 * Volume has expired, has_volume_expired() will update the DB.
225 if (has_volume_expired(jcr, &mr)) {
226 send_volume_info_to_storage_daemon(jcr, bs, &mr);
227 } else if (db_update_media_record(jcr, jcr->db, &mr)) {
228 send_volume_info_to_storage_daemon(jcr, bs, &mr);
230 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
231 db_strerror(jcr->db));
232 bnet_fsend(bs, "1992 Update Media error\n");
233 Dmsg0(190, "send error\n");
238 * Request to create a JobMedia record
240 } else if (sscanf(bs->msg, Create_job_media, &Job,
241 &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
242 &jm.StartBlock, &jm.EndBlock) == 7) {
244 jm.JobId = jcr->JobId;
245 jm.MediaId = jcr->MediaId;
246 Dmsg6(100, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
247 jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
248 if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
249 Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
250 db_strerror(jcr->db));
251 bnet_fsend(bs, "1991 Update JobMedia error\n");
253 Dmsg0(100, "JobMedia record created\n");
254 bnet_fsend(bs, OK_create);
258 omsg = get_memory(bs->msglen+1);
259 pm_strcpy(&omsg, bs->msg);
260 bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
261 Jmsg1(jcr, M_ERROR, 0, _("Invalid Catalog request: %s"), omsg);
264 Dmsg1(120, ">CatReq response: %s", bs->msg);
265 Dmsg1(200, "Leave catreq jcr 0x%x\n", jcr);
270 * Update File Attributes in the catalog with data
271 * sent by the Storage daemon. Note, we receive the whole
272 * attribute record, but we select out only the stat packet,
273 * VolSessionId, VolSessionTime, FileIndex, and file name
274 * to store in the catalog.
276 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
279 uint32_t VolSessionId, VolSessionTime;
288 if (!jcr->pool->catalog_files) {
291 db_start_transaction(jcr, jcr->db); /* start transaction if not already open */
292 skip_nonspaces(&p); /* UpdCat */
294 skip_nonspaces(&p); /* Job=nnn */
296 skip_nonspaces(&p); /* FileAttributes */
299 unser_uint32(VolSessionId);
300 unser_uint32(VolSessionTime);
301 unser_int32(FileIndex);
303 unser_uint32(data_len);
304 p += unser_length(p);
306 Dmsg1(99, "UpdCat msg=%s\n", bs->msg);
307 Dmsg5(99, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
308 VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
310 if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_UNIX_ATTRIBUTES_EX) {
311 skip_nonspaces(&p); /* skip FileIndex */
313 skip_nonspaces(&p); /* skip FileType */
316 len = strlen(fname); /* length before attributes */
317 attr = &fname[len+1];
319 Dmsg2(109, "dird<stored: stream=%d %s\n", Stream, fname);
320 Dmsg1(109, "dird<stored: attr=%s\n", attr);
323 ar.FileIndex = FileIndex;
326 ar.JobId = jcr->JobId;
328 Dmsg2(111, "dird<filed: stream=%d %s\n", Stream, fname);
329 Dmsg1(120, "dird<filed: attr=%s\n", attr);
331 if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
332 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
334 /* Save values for SIG update */
335 jcr->FileId = ar.FileId;
336 jcr->FileIndex = FileIndex;
337 } else if (Stream == STREAM_MD5_SIGNATURE || Stream == STREAM_SHA1_SIGNATURE) {
339 if (jcr->FileIndex != FileIndex) {
340 Jmsg(jcr, M_WARNING, 0, "Got MD5/SHA1 but not same File as attributes\n");
342 /* Update signature in catalog */
343 char SIGbuf[50]; /* 24 bytes should be enough */
345 if (Stream == STREAM_MD5_SIGNATURE) {
352 bin_to_base64(SIGbuf, fname, len);
353 Dmsg3(190, "SIGlen=%d SIG=%s type=%d\n", strlen(SIGbuf), SIGbuf, Stream);
354 if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIGbuf, type)) {
355 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5/SHA1. %s"),
356 db_strerror(jcr->db));