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\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];
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);
88 if (db_find_next_volume(jcr->db, index, &mr)) {
89 jcr->MediaId = mr.MediaId;
90 Dmsg1(20, "Find_next_vol MediaId=%d\n", jcr->MediaId);
91 strcpy(jcr->VolumeName, mr.VolumeName);
94 /* Well, try finding recycled tapes */
95 ok = find_recycled_volume(jcr, &mr);
97 if (prune_volumes(jcr)) {
98 ok = recycle_a_volume(jcr, &mr);
101 /* See if we can create a new Volume */
107 * Send Find Media response to Storage daemon
110 bash_spaces(mr.VolumeName);
111 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
112 mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
113 mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
116 bnet_fsend(bs, "1999 No Media\n");
120 * Request to find specific Volume information
122 } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName) == 2) {
123 Dmsg1(120, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
127 unbash_spaces(mr.VolumeName);
128 if (db_get_media_record(jcr->db, &mr)) {
129 jcr->MediaId = mr.MediaId;
130 Dmsg1(20, "VolumeInfo MediaId=%d\n", jcr->MediaId);
131 strcpy(jcr->VolumeName, mr.VolumeName);
133 * Make sure this volume is suitable for this job, i.e.
134 * it is either Append or Recycle and Media Type matches.
136 if (mr.PoolId == jcr->PoolId &&
137 (strcmp(mr.VolStatus, "Append") == 0 ||
138 strcmp(mr.VolStatus, "Recycle") == 0) &&
139 strcmp(mr.MediaType, jcr->store->media_type) == 0) {
141 * Send Find Media response to Storage daemon
143 bash_spaces(mr.VolumeName);
144 bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
145 mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
146 mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
149 Dmsg4(000, "get_media_record PoolId=%d wanted %d, Status=%s, \
150 MediaType=%s\n", mr.PoolId, jcr->PoolId, mr.VolStatus, mr.MediaType);
151 /* Not suitable volume */
152 bnet_fsend(bs, "1998 Volume not appropriate.\n");
156 bnet_fsend(bs, "1999 Volume Not Found.\n");
161 * Request to update Media record. Comes typically at the end
162 * of a Storage daemon Job Session
164 } else if (sscanf(bs->msg, Update_media, &Job, &mr.VolumeName, &mr.VolJobs,
165 &mr.VolFiles, &mr.VolBlocks, &mr.VolBytes, &mr.VolMounts, &mr.VolErrors,
166 &mr.VolWrites, &mr.VolMaxBytes, &mr.LastWritten, &mr.VolStatus,
167 &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
168 &jm.StartBlock, &jm.EndBlock) == 18) {
170 * Update Media Record
172 if ((mr.VolMaxBytes > 0 && mr.VolBytes >= mr.VolMaxBytes) ||
173 (mr.VolBytes > 0 && jcr->pool->use_volume_once)) {
174 strcpy(mr.VolStatus, "Full");
177 jm.JobId = jcr->JobId;
178 jm.MediaId = jcr->MediaId;
180 * A single write means we just labeled the tape,
181 * so no need to create a jobmedia record.
182 * Otherwise, record the fact that this job used this Volume
184 if (mr.VolWrites > 1) {
185 Dmsg4(20, "create_jobmedia JobId=%d MediaId=%d FI=%d LI=%d\n",
186 jm.JobId, jm.MediaId, jm.FirstIndex, jm.LastIndex);
187 if(!db_create_jobmedia_record(jcr->db, &jm)) {
188 Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
189 db_strerror(jcr->db));
190 bnet_fsend(bs, "1991 Update JobMedia error\n");
192 Dmsg0(20, "JobMedia record created\n");
196 Dmsg0(20, "db_update_media_record\n");
197 if (db_update_media_record(jcr->db, &mr)) {
198 bnet_fsend(bs, OK_update);
199 Dmsg0(90, "send OK\n");
201 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
202 db_strerror(jcr->db));
203 bnet_fsend(bs, "1992 Update Media error\n");
204 Dmsg0(90, "send error\n");
207 omsg = (char *) get_memory(bs->msglen+1);
208 strcpy(omsg, bs->msg);
209 bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
212 Dmsg1(20, ">CatReq response: %s", bs->msg);
217 * Update File Attributes in the catalog with data
218 * sent by the Storage daemon.
220 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
223 uint32_t VolSessionId, VolSessionTime;
232 if (!jcr->pool->catalog_files) {
235 skip_nonspaces(&p); /* UpdCat */
237 skip_nonspaces(&p); /* Job=nnn */
239 skip_nonspaces(&p); /* FileAttributes */
242 unser_uint32(VolSessionId);
243 unser_uint32(VolSessionTime);
244 unser_int32(FileIndex);
246 unser_uint32(data_len);
247 p += unser_length(p);
249 Dmsg1(99, "UpdCat msg=%s\n", bs->msg);
250 Dmsg5(99, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
251 VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
253 if (Stream == STREAM_UNIX_ATTRIBUTES) {
254 skip_nonspaces(&p); /* skip FileIndex */
256 skip_nonspaces(&p); /* skip FileType */
259 len = strlen(fname); /* length before attributes */
260 attr = &fname[len+1];
262 Dmsg2(99, "dird<stored: stream=%d %s\n", Stream, fname);
263 Dmsg1(99, "dird<stored: attr=%s\n", attr);
266 ar.FileIndex = FileIndex;
269 ar.JobId = jcr->JobId;
271 Dmsg2(11, "dird<filed: stream=%d %s\n", Stream, fname);
272 Dmsg1(20, "dird<filed: attr=%s\n", attr);
274 /* ***FIXME*** fix link field */
275 if (!db_create_file_attributes_record(jcr->db, &ar)) {
276 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
278 /* Save values for MD5 update */
279 jcr->FileId = ar.FileId;
280 jcr->FileIndex = FileIndex;
281 } else if (Stream == STREAM_MD5_SIGNATURE) {
283 if (jcr->FileIndex != FileIndex) {
284 Jmsg(jcr, M_WARNING, 0, "Got MD5 but not same block as attributes\n");
286 /* Update MD5 signature in catalog */
287 char MD5buf[50]; /* 24 bytes should be enough */
288 bin_to_base64(MD5buf, fname, 16);
289 Dmsg2(90, "MD5len=%d MD5=%s\n", strlen(MD5buf), MD5buf);
290 if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, MD5buf)) {
291 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5. %s"),
292 db_strerror(jcr->db));