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\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;
75 memset(&mr, 0, sizeof(mr));
76 memset(&jm, 0, sizeof(jm));
79 * Request to find next appendable Volume for this Job
81 Dmsg1(20, "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) == 2) {
125 Dmsg1(120, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
129 unbash_spaces(mr.VolumeName);
130 if (db_get_media_record(jcr->db, &mr)) {
131 jcr->MediaId = mr.MediaId;
132 Dmsg1(20, "VolumeInfo MediaId=%d\n", jcr->MediaId);
133 strcpy(jcr->VolumeName, mr.VolumeName);
135 * Make sure this volume is suitable for this job, i.e.
136 * it is either Append or Recycle and Media Type matches.
138 if (mr.PoolId == jcr->PoolId &&
139 (strcmp(mr.VolStatus, "Append") == 0 ||
140 strcmp(mr.VolStatus, "Recycle") == 0) &&
141 strcmp(mr.MediaType, jcr->store->media_type) == 0) {
143 * Send Find Media response to Storage daemon
145 bash_spaces(mr.VolumeName);
146 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
147 mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
148 mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
149 mr.VolStatus, mr.Slot);
150 Dmsg4(100, "get_media_record PoolId=%d wanted %d, Status=%s, \
151 MediaType=%s\n", mr.PoolId, jcr->PoolId, mr.VolStatus, mr.MediaType);
153 /* Not suitable volume */
154 bnet_fsend(bs, "1998 Volume not appropriate.\n");
158 bnet_fsend(bs, "1999 Volume Not Found.\n");
163 * Request to update Media record. Comes typically at the end
164 * of a Storage daemon Job Session
166 } else if (sscanf(bs->msg, Update_media, &Job, &mr.VolumeName, &mr.VolJobs,
167 &mr.VolFiles, &mr.VolBlocks, &mr.VolBytes, &mr.VolMounts, &mr.VolErrors,
168 &mr.VolWrites, &mr.VolMaxBytes, &mr.LastWritten, &mr.VolStatus,
169 &mr.Slot, &relabel) == 14) {
171 * Update Media Record
173 if ((mr.VolMaxBytes > 0 && mr.VolBytes >= mr.VolMaxBytes) ||
174 (mr.VolBytes > 0 && jcr->pool->use_volume_once)) {
175 strcpy(mr.VolStatus, "Full");
178 Dmsg2(100, "db_update_media_record. Stat=%s Vol=%s\n",
179 mr.VolStatus, mr.VolumeName);
180 if (db_update_media_record(jcr->db, &mr)) {
181 bnet_fsend(bs, OK_update);
182 Dmsg0(90, "send OK\n");
184 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
185 db_strerror(jcr->db));
186 bnet_fsend(bs, "1992 Update Media error\n");
187 Dmsg0(90, "send error\n");
191 * Request to create a JobMedia record
193 } else if (sscanf(bs->msg, Create_job_media, &Job,
194 &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
195 &jm.StartBlock, &jm.EndBlock) == 7) {
197 jm.JobId = jcr->JobId;
198 jm.MediaId = jcr->MediaId;
199 Dmsg6(100, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
200 jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
201 if(!db_create_jobmedia_record(jcr->db, &jm)) {
202 Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
203 db_strerror(jcr->db));
204 bnet_fsend(bs, "1991 Update JobMedia error\n");
206 Dmsg0(100, "JobMedia record created\n");
207 bnet_fsend(bs, OK_update);
211 omsg = (char *) get_memory(bs->msglen+1);
212 strcpy(omsg, bs->msg);
213 bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
216 Dmsg1(20, ">CatReq response: %s", bs->msg);
221 * Update File Attributes in the catalog with data
222 * sent by the Storage daemon.
224 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
227 uint32_t VolSessionId, VolSessionTime;
236 if (!jcr->pool->catalog_files) {
239 db_start_transaction(jcr->db); /* start transaction if not already open */
240 skip_nonspaces(&p); /* UpdCat */
242 skip_nonspaces(&p); /* Job=nnn */
244 skip_nonspaces(&p); /* FileAttributes */
247 unser_uint32(VolSessionId);
248 unser_uint32(VolSessionTime);
249 unser_int32(FileIndex);
251 unser_uint32(data_len);
252 p += unser_length(p);
254 Dmsg1(99, "UpdCat msg=%s\n", bs->msg);
255 Dmsg5(99, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
256 VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
258 if (Stream == STREAM_UNIX_ATTRIBUTES) {
259 skip_nonspaces(&p); /* skip FileIndex */
261 skip_nonspaces(&p); /* skip FileType */
264 len = strlen(fname); /* length before attributes */
265 attr = &fname[len+1];
267 Dmsg2(99, "dird<stored: stream=%d %s\n", Stream, fname);
268 Dmsg1(99, "dird<stored: attr=%s\n", attr);
271 ar.FileIndex = FileIndex;
274 ar.JobId = jcr->JobId;
276 Dmsg2(11, "dird<filed: stream=%d %s\n", Stream, fname);
277 Dmsg1(20, "dird<filed: attr=%s\n", attr);
279 /* ***FIXME*** fix link field */
280 if (!db_create_file_attributes_record(jcr->db, &ar)) {
281 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
283 /* Save values for MD5 update */
284 jcr->FileId = ar.FileId;
285 jcr->FileIndex = FileIndex;
286 } else if (Stream == STREAM_MD5_SIGNATURE) {
288 if (jcr->FileIndex != FileIndex) {
289 Jmsg(jcr, M_WARNING, 0, "Got MD5 but not same block as attributes\n");
291 /* Update MD5 signature in catalog */
292 char MD5buf[50]; /* 24 bytes should be enough */
293 bin_to_base64(MD5buf, fname, 16);
294 Dmsg2(90, "MD5len=%d MD5=%s\n", strlen(MD5buf), MD5buf);
295 if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, MD5buf)) {
296 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5. %s"),
297 db_strerror(jcr->db));