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=%d VolFiles=%d VolBlocks=%d VolBytes=%" lld " VolMounts=%d\
49 VolErrors=%d VolWrites=%d VolMaxBytes=%" lld " EndTime=%d VolStatus=%10s\
50 Slot=%d relabel=%d\n";
52 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia \
53 FirstIndex=%d LastIndex=%d StartFile=%d EndFile=%d \
54 StartBlock=%d EndBlock=%d\n";
57 /* Responses sent to Storage daemon */
58 static char OK_media[] = "1000 OK VolName=%s VolJobs=%d VolFiles=%d\
59 VolBlocks=%d VolBytes=%" lld " VolMounts=%d VolErrors=%d VolWrites=%d\
60 VolMaxBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%s Slot=%d\n";
62 static char OK_update[] = "1000 OK UpdateMedia\n";
64 /* static char FileAttributes[] = "UpdCat Job=%127s FileAttributes "; */
67 void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
71 char Job[MAX_NAME_LENGTH];
72 int index, ok, relabel, writing;
75 memset(&mr, 0, sizeof(mr));
76 memset(&jm, 0, sizeof(jm));
79 * Request to find next appendable Volume for this Job
81 Dmsg1(120, "catreq %s", bs->msg);
82 if (sscanf(bs->msg, Find_media, &Job, &index) == 2) {
83 mr.PoolId = jcr->PoolId;
84 strcpy(mr.MediaType, jcr->store->media_type);
85 strcpy(mr.VolStatus, "Append");
86 Dmsg3(120, "CatReq FindMedia: Id=%d, MediaType=%s, Status=%s\n",
87 mr.PoolId, mr.MediaType, mr.VolStatus);
91 ok = db_find_next_volume(jcr->db, index, &mr);
93 /* Well, try finding recycled tapes */
94 ok = find_recycled_volume(jcr, &mr);
95 Dmsg1(100, "find_recycled_volume1 %d\n", ok);
98 ok = recycle_a_volume(jcr, &mr);
99 Dmsg1(100, "find_recycled_volume2 %d\n", ok);
101 /* See if we can create a new Volume */
102 ok = newVolume(jcr, &mr);
107 * Send Find Media response to Storage daemon
110 jcr->MediaId = mr.MediaId;
111 strcpy(jcr->VolumeName, mr.VolumeName);
112 bash_spaces(mr.VolumeName);
113 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
114 mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
115 mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
116 mr.VolStatus, mr.Slot);
118 bnet_fsend(bs, "1999 No Media\n");
122 * Request to find specific Volume information
124 } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
125 Dmsg1(120, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
129 unbash_spaces(mr.VolumeName);
130 if (db_get_media_record(jcr->db, &mr)) {
132 jcr->MediaId = mr.MediaId;
133 Dmsg1(120, "VolumeInfo MediaId=%d\n", jcr->MediaId);
134 strcpy(jcr->VolumeName, mr.VolumeName);
136 VolSuitable = 1; /* accept anything for read */
139 * Make sure this volume is suitable for this job, i.e.
140 * it is either Append or Recycle and Media Type matches.
142 if (mr.PoolId == jcr->PoolId &&
143 (strcmp(mr.VolStatus, "Append") == 0 ||
144 strcmp(mr.VolStatus, "Recycle") == 0 ||
145 strcmp(mr.MediaType, jcr->store->media_type) == 0)) {
151 * Send Find Media response to Storage daemon
153 bash_spaces(mr.VolumeName);
154 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
155 mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
156 mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
157 mr.VolStatus, mr.Slot);
158 Dmsg5(200, "get_media_record PoolId=%d wanted %d, Status=%s, Slot=%d \
159 MediaType=%s\n", mr.PoolId, jcr->PoolId, mr.VolStatus, mr.Slot, mr.MediaType);
161 /* Not suitable volume */
162 bnet_fsend(bs, "1998 Volume not appropriate.\n");
166 bnet_fsend(bs, "1999 Volume Not Found.\n");
171 * Request to update Media record. Comes typically at the end
172 * of a Storage daemon Job Session
174 } else if (sscanf(bs->msg, Update_media, &Job, &mr.VolumeName, &mr.VolJobs,
175 &mr.VolFiles, &mr.VolBlocks, &mr.VolBytes, &mr.VolMounts, &mr.VolErrors,
176 &mr.VolWrites, &mr.VolMaxBytes, &mr.LastWritten, &mr.VolStatus,
177 &mr.Slot, &relabel) == 14) {
179 * Update Media Record
181 if ((mr.VolMaxBytes > 0 && mr.VolBytes >= mr.VolMaxBytes) ||
182 (mr.VolBytes > 0 && jcr->pool->use_volume_once)) {
183 strcpy(mr.VolStatus, "Full");
186 Dmsg2(100, "db_update_media_record. Stat=%s Vol=%s\n",
187 mr.VolStatus, mr.VolumeName);
188 if (db_update_media_record(jcr->db, &mr)) {
189 bnet_fsend(bs, OK_update);
190 Dmsg0(190, "send OK\n");
192 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
193 db_strerror(jcr->db));
194 bnet_fsend(bs, "1992 Update Media error\n");
195 Dmsg0(190, "send error\n");
199 * Request to create a JobMedia record
201 } else if (sscanf(bs->msg, Create_job_media, &Job,
202 &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
203 &jm.StartBlock, &jm.EndBlock) == 7) {
205 jm.JobId = jcr->JobId;
206 jm.MediaId = jcr->MediaId;
207 Dmsg6(100, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
208 jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
209 if(!db_create_jobmedia_record(jcr->db, &jm)) {
210 Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
211 db_strerror(jcr->db));
212 bnet_fsend(bs, "1991 Update JobMedia error\n");
214 Dmsg0(100, "JobMedia record created\n");
215 bnet_fsend(bs, OK_update);
219 omsg = (char *) get_memory(bs->msglen+1);
220 strcpy(omsg, bs->msg);
221 bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
224 Dmsg1(120, ">CatReq response: %s", bs->msg);
229 * Update File Attributes in the catalog with data
230 * sent by the Storage daemon.
232 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
235 uint32_t VolSessionId, VolSessionTime;
244 if (!jcr->pool->catalog_files) {
247 db_start_transaction(jcr->db); /* start transaction if not already open */
248 skip_nonspaces(&p); /* UpdCat */
250 skip_nonspaces(&p); /* Job=nnn */
252 skip_nonspaces(&p); /* FileAttributes */
255 unser_uint32(VolSessionId);
256 unser_uint32(VolSessionTime);
257 unser_int32(FileIndex);
259 unser_uint32(data_len);
260 p += unser_length(p);
262 Dmsg1(99, "UpdCat msg=%s\n", bs->msg);
263 Dmsg5(99, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
264 VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
266 if (Stream == STREAM_UNIX_ATTRIBUTES) {
267 skip_nonspaces(&p); /* skip FileIndex */
269 skip_nonspaces(&p); /* skip FileType */
272 len = strlen(fname); /* length before attributes */
273 attr = &fname[len+1];
275 Dmsg2(109, "dird<stored: stream=%d %s\n", Stream, fname);
276 Dmsg1(109, "dird<stored: attr=%s\n", attr);
279 ar.FileIndex = FileIndex;
282 ar.JobId = jcr->JobId;
284 Dmsg2(111, "dird<filed: stream=%d %s\n", Stream, fname);
285 Dmsg1(120, "dird<filed: attr=%s\n", attr);
287 /* ***FIXME*** fix link field */
288 if (!db_create_file_attributes_record(jcr->db, &ar)) {
289 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
291 /* Save values for MD5 update */
292 jcr->FileId = ar.FileId;
293 jcr->FileIndex = FileIndex;
294 } else if (Stream == STREAM_MD5_SIGNATURE) {
296 if (jcr->FileIndex != FileIndex) {
297 Jmsg(jcr, M_WARNING, 0, "Got MD5 but not same block as attributes\n");
299 /* Update MD5 signature in catalog */
300 char MD5buf[50]; /* 24 bytes should be enough */
301 bin_to_base64(MD5buf, fname, 16);
302 Dmsg2(190, "MD5len=%d MD5=%s\n", strlen(MD5buf), MD5buf);
303 if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, MD5buf)) {
304 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5. %s"),
305 db_strerror(jcr->db));