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 FirstIndex=%d LastIndex=%d StartFile=%d EndFile=%d \
51 StartBlock=%d EndBlock=%d relabel=%d\n";
53 /* Responses sent to Storage daemon */
54 static char OK_media[] = "1000 OK VolName=%s VolJobs=%d VolFiles=%d\
55 VolBlocks=%d VolBytes=%" lld " VolMounts=%d VolErrors=%d VolWrites=%d\
56 VolMaxBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%s\n";
58 static char OK_update[] = "1000 OK UpdateMedia\n";
60 /* static char FileAttributes[] = "UpdCat Job=%127s FileAttributes "; */
63 void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
67 char Job[MAX_NAME_LENGTH];
68 int index, ok, relabel;
71 memset(&mr, 0, sizeof(mr));
72 memset(&jm, 0, sizeof(jm));
75 * Request to find next appendable Volume for this Job
77 Dmsg1(20, "catreq %s", bs->msg);
78 if (sscanf(bs->msg, Find_media, &Job, &index) == 2) {
79 mr.PoolId = jcr->PoolId;
80 strcpy(mr.MediaType, jcr->store->media_type);
81 strcpy(mr.VolStatus, "Append");
82 Dmsg3(120, "CatReq FindMedia: Id=%d, MediaType=%s, Status=%s\n",
83 mr.PoolId, mr.MediaType, mr.VolStatus);
87 ok = db_find_next_volume(jcr->db, index, &mr);
89 /* Well, try finding recycled tapes */
90 ok = find_recycled_volume(jcr, &mr);
91 Dmsg1(100, "find_recycled_volume1 %d\n", ok);
94 ok = recycle_a_volume(jcr, &mr);
95 Dmsg1(100, "find_recycled_volume2 %d\n", ok);
97 /* See if we can create a new Volume */
98 ok = newVolume(jcr, &mr);
103 * Send Find Media response to Storage daemon
106 jcr->MediaId = mr.MediaId;
107 strcpy(jcr->VolumeName, mr.VolumeName);
108 bash_spaces(mr.VolumeName);
109 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
110 mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
111 mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
114 bnet_fsend(bs, "1999 No Media\n");
118 * Request to find specific Volume information
120 } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName) == 2) {
121 Dmsg1(120, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
125 unbash_spaces(mr.VolumeName);
126 if (db_get_media_record(jcr->db, &mr)) {
127 jcr->MediaId = mr.MediaId;
128 Dmsg1(20, "VolumeInfo MediaId=%d\n", jcr->MediaId);
129 strcpy(jcr->VolumeName, mr.VolumeName);
131 * Make sure this volume is suitable for this job, i.e.
132 * it is either Append or Recycle and Media Type matches.
134 if (mr.PoolId == jcr->PoolId &&
135 (strcmp(mr.VolStatus, "Append") == 0 ||
136 strcmp(mr.VolStatus, "Recycle") == 0) &&
137 strcmp(mr.MediaType, jcr->store->media_type) == 0) {
139 * Send Find Media response to Storage daemon
141 bash_spaces(mr.VolumeName);
142 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
143 mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
144 mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
147 Dmsg4(100, "get_media_record PoolId=%d wanted %d, Status=%s, \
148 MediaType=%s\n", mr.PoolId, jcr->PoolId, mr.VolStatus, mr.MediaType);
149 /* Not suitable volume */
150 bnet_fsend(bs, "1998 Volume not appropriate.\n");
154 bnet_fsend(bs, "1999 Volume Not Found.\n");
159 * Request to update Media record. Comes typically at the end
160 * of a Storage daemon Job Session
162 } else if (sscanf(bs->msg, Update_media, &Job, &mr.VolumeName, &mr.VolJobs,
163 &mr.VolFiles, &mr.VolBlocks, &mr.VolBytes, &mr.VolMounts, &mr.VolErrors,
164 &mr.VolWrites, &mr.VolMaxBytes, &mr.LastWritten, &mr.VolStatus,
165 &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
166 &jm.StartBlock, &jm.EndBlock, &relabel) == 19) {
168 * Update Media Record
170 if ((mr.VolMaxBytes > 0 && mr.VolBytes >= mr.VolMaxBytes) ||
171 (mr.VolBytes > 0 && jcr->pool->use_volume_once)) {
172 strcpy(mr.VolStatus, "Full");
175 jm.JobId = jcr->JobId;
176 jm.MediaId = jcr->MediaId;
178 * If relabel is set, it means we just labeled the tape,
179 * so no need to create a jobmedia record.
180 * Otherwise, record the fact that this job used this Volume
183 Dmsg6(100, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
184 jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
185 if(!db_create_jobmedia_record(jcr->db, &jm)) {
186 Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
187 db_strerror(jcr->db));
188 bnet_fsend(bs, "1991 Update JobMedia error\n");
190 Dmsg0(20, "JobMedia record created\n");
194 Dmsg0(20, "db_update_media_record\n");
195 if (db_update_media_record(jcr->db, &mr)) {
196 bnet_fsend(bs, OK_update);
197 Dmsg0(90, "send OK\n");
199 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
200 db_strerror(jcr->db));
201 bnet_fsend(bs, "1992 Update Media error\n");
202 Dmsg0(90, "send error\n");
205 omsg = (char *) get_memory(bs->msglen+1);
206 strcpy(omsg, bs->msg);
207 bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
210 Dmsg1(20, ">CatReq response: %s", bs->msg);
215 * Update File Attributes in the catalog with data
216 * sent by the Storage daemon.
218 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
221 uint32_t VolSessionId, VolSessionTime;
230 if (!jcr->pool->catalog_files) {
233 skip_nonspaces(&p); /* UpdCat */
235 skip_nonspaces(&p); /* Job=nnn */
237 skip_nonspaces(&p); /* FileAttributes */
240 unser_uint32(VolSessionId);
241 unser_uint32(VolSessionTime);
242 unser_int32(FileIndex);
244 unser_uint32(data_len);
245 p += unser_length(p);
247 Dmsg1(99, "UpdCat msg=%s\n", bs->msg);
248 Dmsg5(99, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
249 VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
251 if (Stream == STREAM_UNIX_ATTRIBUTES) {
252 skip_nonspaces(&p); /* skip FileIndex */
254 skip_nonspaces(&p); /* skip FileType */
257 len = strlen(fname); /* length before attributes */
258 attr = &fname[len+1];
260 Dmsg2(99, "dird<stored: stream=%d %s\n", Stream, fname);
261 Dmsg1(99, "dird<stored: attr=%s\n", attr);
264 ar.FileIndex = FileIndex;
267 ar.JobId = jcr->JobId;
269 Dmsg2(11, "dird<filed: stream=%d %s\n", Stream, fname);
270 Dmsg1(20, "dird<filed: attr=%s\n", attr);
272 /* ***FIXME*** fix link field */
273 if (!db_create_file_attributes_record(jcr->db, &ar)) {
274 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
276 /* Save values for MD5 update */
277 jcr->FileId = ar.FileId;
278 jcr->FileIndex = FileIndex;
279 } else if (Stream == STREAM_MD5_SIGNATURE) {
281 if (jcr->FileIndex != FileIndex) {
282 Jmsg(jcr, M_WARNING, 0, "Got MD5 but not same block as attributes\n");
284 /* Update MD5 signature in catalog */
285 char MD5buf[50]; /* 24 bytes should be enough */
286 bin_to_base64(MD5buf, fname, 16);
287 Dmsg2(90, "MD5len=%d MD5=%s\n", strlen(MD5buf), MD5buf);
288 if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, MD5buf)) {
289 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5. %s"),
290 db_strerror(jcr->db));