2 Bacula® - The Network Backup Solution
4 Copyright (C) 2001-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * Bacula Director -- catreq.c -- handles the message channel
31 * catalog request from the Storage daemon.
33 * Kern Sibbald, March MMI
35 * This routine runs as a thread and must be thread reentrant.
37 * Basic tasks done here:
38 * Handle Catalog services.
45 #include "findlib/find.h"
48 * Handle catalog request
49 * For now, we simply return next Volume to be used
52 /* Requests from the Storage daemon */
53 static char Find_media[] = "CatReq Job=%127s FindMedia=%d pool_name=%127s media_type=%127s\n";
54 static char Get_Vol_Info[] = "CatReq Job=%127s GetVolInfo VolName=%127s write=%d\n";
56 static char Update_media[] = "CatReq Job=%127s UpdateMedia VolName=%s"
57 " VolJobs=%u VolFiles=%u VolBlocks=%u VolBytes=%lld VolMounts=%u"
58 " VolErrors=%u VolWrites=%u MaxVolBytes=%lld EndTime=%lld VolStatus=%10s"
59 " Slot=%d relabel=%d InChanger=%d VolReadTime=%s VolWriteTime=%s"
60 " VolFirstWritten=%lld VolParts=%u\n";
62 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
63 " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
64 " StartBlock=%u EndBlock=%u Copy=%d Strip=%d MediaId=%" lld "\n";
67 /* Responses sent to Storage daemon */
68 static char OK_media[] = "1000 OK VolName=%s VolJobs=%u VolFiles=%u"
69 " VolBlocks=%u VolBytes=%s VolMounts=%u VolErrors=%u VolWrites=%u"
70 " MaxVolBytes=%s VolCapacityBytes=%s VolStatus=%s Slot=%d"
71 " MaxVolJobs=%u MaxVolFiles=%u InChanger=%d VolReadTime=%s"
72 " VolWriteTime=%s EndFile=%u EndBlock=%u VolParts=%u LabelType=%d"
75 static char OK_create[] = "1000 OK CreateJobMedia\n";
78 static int send_volume_info_to_storage_daemon(JCR *jcr, BSOCK *sd, MEDIA_DBR *mr)
81 char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50], ed6[50];
83 jcr->MediaId = mr->MediaId;
84 pm_strcpy(jcr->VolumeName, mr->VolumeName);
85 bash_spaces(mr->VolumeName);
86 stat = bnet_fsend(sd, OK_media, mr->VolumeName, mr->VolJobs,
87 mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
88 mr->VolMounts, mr->VolErrors, mr->VolWrites,
89 edit_uint64(mr->MaxVolBytes, ed2),
90 edit_uint64(mr->VolCapacityBytes, ed3),
91 mr->VolStatus, mr->Slot, mr->MaxVolJobs, mr->MaxVolFiles,
93 edit_int64(mr->VolReadTime, ed4),
94 edit_int64(mr->VolWriteTime, ed5),
95 mr->EndFile, mr->EndBlock,
98 edit_uint64(mr->MediaId, ed6));
99 unbash_spaces(mr->VolumeName);
100 Dmsg2(100, "Vol Info for %s: %s", jcr->Job, sd->msg);
104 void catalog_request(JCR *jcr, BSOCK *bs)
106 char ed_vrt[50], ed_vwt[50];
109 char Job[MAX_NAME_LENGTH];
110 char pool_name[MAX_NAME_LENGTH];
111 int index, ok, label, writing;
116 utime_t VolFirstWritten;
117 utime_t VolLastWritten;
119 memset(&mr, 0, sizeof(mr));
120 memset(&sdmr, 0, sizeof(sdmr));
121 memset(&jm, 0, sizeof(jm));
125 * Request to find next appendable Volume for this Job
127 Dmsg1(100, "catreq %s", bs->msg);
129 omsg = get_memory(bs->msglen+1);
130 pm_strcpy(omsg, bs->msg);
131 bs->fsend(_("1990 Invalid Catalog Request: %s"), omsg);
132 Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request; DB not open: %s"), omsg);
137 * Find next appendable medium for SD
139 if (sscanf(bs->msg, Find_media, &Job, &index, &pool_name, &mr.MediaType) == 4) {
140 memset(&pr, 0, sizeof(pr));
141 bstrncpy(pr.Name, pool_name, sizeof(pr.Name));
142 unbash_spaces(pr.Name);
143 ok = db_get_pool_record(jcr, jcr->db, &pr);
145 mr.PoolId = pr.PoolId;
146 mr.StorageId = jcr->wstore->StorageId;
147 ok = find_next_volume_for_append(jcr, &mr, index, fnv_create_vol, fnv_prune);
148 Dmsg3(050, "find_media ok=%d idx=%d vol=%s\n", ok, index, mr.VolumeName);
151 * Send Find Media response to Storage daemon
154 send_volume_info_to_storage_daemon(jcr, bs, &mr);
156 bs->fsend(_("1901 No Media.\n"));
157 Dmsg0(500, "1901 No Media.\n");
161 * Request to find specific Volume information
163 } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
164 Dmsg1(100, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
168 unbash_spaces(mr.VolumeName);
169 if (db_get_media_record(jcr, jcr->db, &mr)) {
170 const char *reason = NULL; /* detailed reason for rejection */
172 * If we are reading, accept any volume (reason == NULL)
173 * If we are writing, check if the Volume is valid
174 * for this job, and do a recycle if necessary
178 * SD wants to write this Volume, so make
179 * sure it is suitable for this job, i.e.
180 * Pool matches, and it is either Append or Recycle
181 * and Media Type matches and Pool allows any volume.
183 if (mr.PoolId != jcr->jr.PoolId) {
184 reason = _("not in Pool");
185 } else if (strcmp(mr.MediaType, jcr->wstore->media_type) != 0) {
186 reason = _("not correct MediaType");
189 * Now try recycling if necessary
190 * reason set non-NULL if we cannot use it
192 check_if_volume_valid_or_recyclable(jcr, &mr, &reason);
195 if (!reason && mr.Enabled != 1) {
196 reason = _("is not Enabled");
198 if (reason == NULL) {
200 * Send Find Media response to Storage daemon
202 send_volume_info_to_storage_daemon(jcr, bs, &mr);
204 /* Not suitable volume */
205 bs->fsend(_("1998 Volume \"%s\" status is %s, %s.\n"), mr.VolumeName,
206 mr.VolStatus, reason);
210 bs->fsend(_("1997 Volume \"%s\" not in catalog.\n"), mr.VolumeName);
211 Dmsg1(100, "1997 Volume \"%s\" not in catalog.\n", mr.VolumeName);
215 * Request to update Media record. Comes typically at the end
216 * of a Storage daemon Job Session, when labeling/relabeling a
217 * Volume, or when an EOF mark is written.
219 } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName,
220 &sdmr.VolJobs, &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes,
221 &sdmr.VolMounts, &sdmr.VolErrors, &sdmr.VolWrites, &sdmr.MaxVolBytes,
222 &VolLastWritten, &sdmr.VolStatus, &sdmr.Slot, &label, &sdmr.InChanger,
223 ed_vrt, ed_vwt, &VolFirstWritten,
224 &sdmr.VolParts) == 19) {
226 sdmr.VolReadTime = str_to_int64(ed_vrt);
227 sdmr.VolWriteTime = str_to_int64(ed_vwt);
230 Dmsg3(400, "Update media %s oldStat=%s newStat=%s\n", sdmr.VolumeName,
231 mr.VolStatus, sdmr.VolStatus);
232 bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
233 unbash_spaces(mr.VolumeName);
234 if (!db_get_media_record(jcr, jcr->db, &mr)) {
235 Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
236 mr.VolumeName, db_strerror(jcr->db));
237 bs->fsend(_("1991 Catalog Request for vol=%s failed: %s"),
238 mr.VolumeName, db_strerror(jcr->db));
242 /* Set first written time if this is first job */
243 if (mr.FirstWritten == 0) {
244 if (VolFirstWritten == 0) {
245 mr.FirstWritten = jcr->start_time; /* use Job start time as first write */
247 mr.FirstWritten = VolFirstWritten;
249 mr.set_first_written = true;
251 /* If we just labeled the tape set time */
252 if (label || mr.LabelDate == 0) {
253 mr.LabelDate = jcr->start_time;
254 mr.set_label_date = true;
255 if (mr.InitialWrite == 0) {
256 mr.InitialWrite = jcr->start_time;
258 Dmsg2(400, "label=%d labeldate=%d\n", label, mr.LabelDate);
261 * Insanity check for VolFiles get set to a smaller value
263 if (sdmr.VolFiles < mr.VolFiles) {
264 Jmsg(jcr, M_FATAL, 0, _("Volume Files at %u being set to %u"
265 " for Volume \"%s\". This is incorrect.\n"),
266 mr.VolFiles, sdmr.VolFiles, mr.VolumeName);
267 bs->fsend(_("1992 Update Media error. VolFiles=%u, CatFiles=%u\n"),
268 sdmr.VolFiles, mr.VolFiles);
273 Dmsg2(400, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
275 * Check if the volume has been written by the job,
276 * and update the LastWritten field if needed.
278 if (mr.VolBlocks != sdmr.VolBlocks && VolLastWritten != 0) {
279 mr.LastWritten = VolLastWritten;
281 /* Copy updated values to original media record */
282 mr.VolJobs = sdmr.VolJobs;
283 mr.VolFiles = sdmr.VolFiles;
284 mr.VolBlocks = sdmr.VolBlocks;
285 mr.VolBytes = sdmr.VolBytes;
286 mr.VolMounts = sdmr.VolMounts;
287 mr.VolErrors = sdmr.VolErrors;
288 mr.VolWrites = sdmr.VolWrites;
290 mr.InChanger = sdmr.InChanger;
291 mr.VolReadTime = sdmr.VolReadTime;
292 mr.VolWriteTime = sdmr.VolWriteTime;
293 mr.VolParts = sdmr.VolParts;
294 bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
296 * Update to point to the last device used to write the Volume.
297 * However, do so only if we are writing the tape, i.e.
298 * the number of VolBlocks has increased.
300 if (jcr->wstore && jcr->wstore->StorageId && mr.VolBlocks != sdmr.VolBlocks) {
301 mr.StorageId = jcr->wstore->StorageId;
304 Dmsg2(400, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
306 * Update the database, then before sending the response to the
307 * SD, check if the Volume has expired.
309 if (!db_update_media_record(jcr, jcr->db, &mr)) {
310 Jmsg(jcr, M_FATAL, 0, _("Catalog error updating Media record. %s"),
311 db_strerror(jcr->db));
312 bs->fsend(_("1993 Update Media error\n"));
313 Dmsg0(400, "send error\n");
315 (void)has_volume_expired(jcr, &mr);
316 send_volume_info_to_storage_daemon(jcr, bs, &mr);
321 * Request to create a JobMedia record
323 } else if (sscanf(bs->msg, Create_job_media, &Job,
324 &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
325 &jm.StartBlock, &jm.EndBlock, &jm.Copy, &Stripe, &MediaId) == 10) {
328 jm.JobId = jcr->mig_jcr->JobId;
330 jm.JobId = jcr->JobId;
332 jm.MediaId = MediaId;
333 Dmsg6(400, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
334 jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
335 if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
336 Jmsg(jcr, M_FATAL, 0, _("Catalog error creating JobMedia record. %s"),
337 db_strerror(jcr->db));
338 bs->fsend(_("1991 Update JobMedia error\n"));
340 Dmsg0(400, "JobMedia record created\n");
341 bs->fsend(OK_create);
345 omsg = get_memory(bs->msglen+1);
346 pm_strcpy(omsg, bs->msg);
347 bs->fsend(_("1990 Invalid Catalog Request: %s"), omsg);
348 Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request: %s"), omsg);
351 Dmsg1(400, ">CatReq response: %s", bs->msg);
352 Dmsg1(400, "Leave catreq jcr 0x%x\n", jcr);
357 * Update File Attributes in the catalog with data
358 * sent by the Storage daemon. Note, we receive the whole
359 * attribute record, but we select out only the stat packet,
360 * VolSessionId, VolSessionTime, FileIndex, file type, and
361 * file name to store in the catalog.
363 void catalog_update(JCR *jcr, BSOCK *bs)
366 uint32_t VolSessionId, VolSessionTime;
378 if (job_canceled(jcr) || !jcr->pool->catalog_files) {
379 goto bail_out; /* user disabled cataloging */
382 omsg = get_memory(bs->msglen+1);
383 pm_strcpy(omsg, bs->msg);
384 bs->fsend(_("1991 Invalid Catalog Update: %s"), omsg);
385 Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog Update; DB not open: %s"), omsg);
390 /* Start transaction allocates jcr->attr and jcr->ar if needed */
391 db_start_transaction(jcr, jcr->db); /* start transaction if not already open */
394 /* Start by scanning directly in the message buffer to get Stream
395 * there may be a cached attr so we cannot yet write into
396 * jcr->attr or jcr->ar
399 skip_nonspaces(&p); /* UpdCat */
401 skip_nonspaces(&p); /* Job=nnn */
403 skip_nonspaces(&p); /* FileAttributes */
406 unser_uint32(VolSessionId);
407 unser_uint32(VolSessionTime);
408 unser_int32(FileIndex);
410 unser_uint32(data_len);
411 p += unser_length(p);
413 Dmsg1(400, "UpdCat msg=%s\n", bs->msg);
414 Dmsg5(400, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
415 VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
417 if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_UNIX_ATTRIBUTES_EX) {
418 if (jcr->cached_attribute) {
419 Dmsg2(400, "Cached attr. Stream=%d fname=%s\n", ar->Stream, ar->fname);
420 if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
421 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
424 /* Any cached attr is flushed so we can reuse jcr->attr and jcr->ar */
425 jcr->attr = check_pool_memory_size(jcr->attr, bs->msglen);
426 memcpy(jcr->attr, bs->msg, bs->msglen);
427 p = jcr->attr - bs->msg + p; /* point p into jcr->attr */
428 skip_nonspaces(&p); /* skip FileIndex */
430 filetype = str_to_int32(p); /* TODO: choose between unserialize and str_to_int32 */
431 skip_nonspaces(&p); /* skip FileType */
434 len = strlen(fname); /* length before attributes */
435 attr = &fname[len+1];
437 Dmsg2(400, "dird<stored: stream=%d %s\n", Stream, fname);
438 Dmsg1(400, "dird<stored: attr=%s\n", attr);
441 if (filetype == FT_DELETED) {
442 ar->FileIndex = 0; /* special value */
444 ar->FileIndex = FileIndex;
449 ar->JobId = jcr->mig_jcr->JobId;
451 ar->JobId = jcr->JobId;
454 ar->DigestType = CRYPTO_DIGEST_NONE;
455 jcr->cached_attribute = true;
457 Dmsg2(400, "dird<filed: stream=%d %s\n", Stream, fname);
458 Dmsg1(400, "dird<filed: attr=%s\n", attr);
460 } else if (crypto_digest_stream_type(Stream) != CRYPTO_DIGEST_NONE) {
462 if (ar->FileIndex != FileIndex) {
463 Jmsg(jcr, M_WARNING, 0, _("Got %s but not same File as attributes\n"), stream_to_ascii(Stream));
465 /* Update digest in catalog */
466 char digestbuf[BASE64_SIZE(CRYPTO_DIGEST_MAX_SIZE)];
468 int type = CRYPTO_DIGEST_NONE;
471 case STREAM_MD5_DIGEST:
472 len = CRYPTO_DIGEST_MD5_SIZE;
473 type = CRYPTO_DIGEST_MD5;
475 case STREAM_SHA1_DIGEST:
476 len = CRYPTO_DIGEST_SHA1_SIZE;
477 type = CRYPTO_DIGEST_SHA1;
479 case STREAM_SHA256_DIGEST:
480 len = CRYPTO_DIGEST_SHA256_SIZE;
481 type = CRYPTO_DIGEST_SHA256;
483 case STREAM_SHA512_DIGEST:
484 len = CRYPTO_DIGEST_SHA512_SIZE;
485 type = CRYPTO_DIGEST_SHA512;
488 /* Never reached ... */
489 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating file digest. Unsupported digest stream type: %d"),
493 bin_to_base64(digestbuf, sizeof(digestbuf), fname, len, true);
494 Dmsg3(400, "DigestLen=%d Digest=%s type=%d\n", strlen(digestbuf), digestbuf, Stream);
495 if (jcr->cached_attribute) {
496 ar->Digest = digestbuf;
497 ar->DigestType = type;
498 Dmsg2(400, "Cached attr with digest. Stream=%d fname=%s\n", ar->Stream, ar->fname);
499 if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
500 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
502 jcr->cached_attribute = false;
504 if (!db_add_digest_to_file_record(jcr, jcr->db, ar->FileId, digestbuf, type)) {
505 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating file digest. %s"),
506 db_strerror(jcr->db));
512 if (job_canceled(jcr)) {
513 cancel_storage_daemon_job(jcr);