]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/catreq.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[bacula/bacula] / bacula / src / dird / catreq.c
1 /*
2  *
3  *   Bacula Director -- catreq.c -- handles the message channel
4  *    catalog request from the Storage daemon.
5  *
6  *     Kern Sibbald, March MMI
7  *
8  *    This routine runs as a thread and must be thread reentrant.
9  *
10  *  Basic tasks done here:
11  *      Handle Catalog services.
12  *
13  *   Version $Id$
14  */
15 /*
16    Copyright (C) 2001-2004 Kern Sibbald and John Walker
17
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.
22
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.
27
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,
31    MA 02111-1307, USA.
32
33  */
34
35 #include "bacula.h"
36 #include "dird.h"
37
38 /*
39  * Handle catalog request
40  *  For now, we simply return next Volume to be used
41  */
42
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";
46
47 static char Update_media[] = "CatReq Job=%127s UpdateMedia VolName=%s"
48 " VolJobs=%u VolFiles=%u VolBlocks=%u VolBytes=%" lld " VolMounts=%u"
49 " VolErrors=%u VolWrites=%u MaxVolBytes=%" lld " EndTime=%d VolStatus=%10s"
50 " Slot=%d relabel=%d InChanger=%d VolReadTime=%" lld " VolWriteTime=%" lld "\n";
51
52 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
53 " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
54 " StartBlock=%u EndBlock=%u\n";
55
56
57 /* Responses  sent to Storage daemon */
58 static char OK_media[] = "1000 OK VolName=%s VolJobs=%u VolFiles=%u"
59    " VolBlocks=%u VolBytes=%s VolMounts=%u VolErrors=%u VolWrites=%u"
60    " MaxVolBytes=%s VolCapacityBytes=%s VolStatus=%s Slot=%d"
61    " MaxVolJobs=%u MaxVolFiles=%u InChanger=%d VolReadTime=%s"
62    " VolWriteTime=%s EndFile=%u EndBlock=%u\n";
63
64 static char OK_create[] = "1000 OK CreateJobMedia\n";
65
66
67 static int send_volume_info_to_storage_daemon(JCR *jcr, BSOCK *sd, MEDIA_DBR *mr)
68 {
69    int stat;
70    char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50];
71
72    jcr->MediaId = mr->MediaId;
73    pm_strcpy(jcr->VolumeName, mr->VolumeName);
74    bash_spaces(mr->VolumeName);
75    stat = bnet_fsend(sd, OK_media, mr->VolumeName, mr->VolJobs,
76       mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
77       mr->VolMounts, mr->VolErrors, mr->VolWrites,
78       edit_uint64(mr->MaxVolBytes, ed2),
79       edit_uint64(mr->VolCapacityBytes, ed3),
80       mr->VolStatus, mr->Slot, mr->MaxVolJobs, mr->MaxVolFiles,
81       mr->InChanger,
82       edit_uint64(mr->VolReadTime, ed4),
83       edit_uint64(mr->VolWriteTime, ed5),
84       mr->EndFile, mr->EndBlock);
85    unbash_spaces(mr->VolumeName);
86    Dmsg2(200, "Vol Info for %s: %s", jcr->Job, sd->msg);
87    return stat;
88 }
89
90 void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
91 {
92    MEDIA_DBR mr, sdmr;
93    JOBMEDIA_DBR jm;
94    char Job[MAX_NAME_LENGTH];
95    int index, ok, label, writing;
96    POOLMEM *omsg;
97
98    memset(&mr, 0, sizeof(mr));
99    memset(&sdmr, 0, sizeof(sdmr));
100    memset(&jm, 0, sizeof(jm));
101
102    /*
103     * Request to find next appendable Volume for this Job
104     */
105    Dmsg1(200, "catreq %s", bs->msg);
106    if (sscanf(bs->msg, Find_media, &Job, &index) == 2) {
107       ok = find_next_volume_for_append(jcr, &mr, true /*permit create new vol*/);
108       /*
109        * Send Find Media response to Storage daemon
110        */
111       if (ok) {
112          send_volume_info_to_storage_daemon(jcr, bs, &mr);
113       } else {
114          bnet_fsend(bs, "1901 No Media.\n");
115       }
116
117    /*
118     * Request to find specific Volume information
119     */
120    } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
121       Dmsg1(400, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
122       /*
123        * Find the Volume
124        */
125       unbash_spaces(mr.VolumeName);
126       if (db_get_media_record(jcr, jcr->db, &mr)) {
127          const char *reason = NULL;           /* detailed reason for rejection */
128          /*
129           * If we are reading, accept any volume (reason == NULL)
130           * If we are writing, check if the Volume is valid
131           *   for this job, and do a recycle if necessary
132           */
133          if (writing) {
134             /*
135              * SD wants to write this Volume, so make
136              *   sure it is suitable for this job, i.e.
137              *   Pool matches, and it is either Append or Recycle
138              *   and Media Type matches and Pool allows any volume.
139              */
140             if (mr.PoolId != jcr->PoolId) {
141                reason = "not in Pool";
142             } else if (strcmp(mr.MediaType, jcr->store->media_type) != 0) {
143                reason = "not correct MediaType";
144             } else {
145               /*
146                * ****FIXME***
147                *   This test (accept_any_volume) is turned off
148                *   because it doesn't properly check if the volume
149                *   really is out of sequence!
150                *
151                * } else if (!jcr->pool->accept_any_volume) {
152                *    reason = "Volume not in sequence";
153                */
154
155                /*
156                 * Now try recycling if necessary
157                 *   reason set non-NULL if we cannot use it
158                 */
159                check_if_volume_valid_or_recyclable(jcr, &mr, &reason);
160             }
161          }
162          if (reason == NULL) {
163             /*
164              * Send Find Media response to Storage daemon
165              */
166             send_volume_info_to_storage_daemon(jcr, bs, &mr);
167          } else {
168             /* Not suitable volume */
169             bnet_fsend(bs, "1998 Volume \"%s\" status is %s, %s.\n", mr.VolumeName,
170                mr.VolStatus, reason);
171          }
172
173       } else {
174          bnet_fsend(bs, "1997 Volume \"%s\" not in catalog.\n", mr.VolumeName);
175       }
176
177
178    /*
179     * Request to update Media record. Comes typically at the end
180     *  of a Storage daemon Job Session, when labeling/relabeling a
181     *  Volume, or when an EOF mark is written.
182     */
183    } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName, &sdmr.VolJobs,
184       &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes, &sdmr.VolMounts, &sdmr.VolErrors,
185       &sdmr.VolWrites, &sdmr.MaxVolBytes, &sdmr.LastWritten, &sdmr.VolStatus,
186       &sdmr.Slot, &label, &sdmr.InChanger, &sdmr.VolReadTime,
187       &sdmr.VolWriteTime) == 17) {
188
189       db_lock(jcr->db);
190       Dmsg3(300, "Update media %s oldStat=%s newStat=%s\n", sdmr.VolumeName,
191          mr.VolStatus, sdmr.VolStatus);
192       bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
193       unbash_spaces(mr.VolumeName);
194       if (!db_get_media_record(jcr, jcr->db, &mr)) {
195          Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
196               mr.VolumeName, db_strerror(jcr->db));
197          bnet_fsend(bs, "1991 Catalog Request failed: %s", db_strerror(jcr->db));
198          db_unlock(jcr->db);
199          return;
200       }
201       /* Set first written time if this is first job */
202       if (mr.VolJobs == 0 || sdmr.VolJobs == 1) {
203          mr.FirstWritten = jcr->start_time;   /* use Job start time as first write */
204       }
205       /* If we just labeled the tape set time */
206       Dmsg2(300, "label=%d labeldate=%d\n", label, mr.LabelDate);
207       if (label || mr.LabelDate == 0) {
208          mr.LabelDate = time(NULL);
209       } else {
210          /*
211           * Insanity check for VolFiles get set to a smaller value
212           */
213          if (sdmr.VolFiles < mr.VolFiles) {
214             Jmsg(jcr, M_ERROR, 0, _("ERROR!! Volume Files at %u being set to %u. This is probably wrong.\n"),
215                mr.VolFiles, sdmr.VolFiles);
216          }
217       }
218       Dmsg2(300, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
219       /* Copy updated values to original media record */
220       mr.VolJobs      = sdmr.VolJobs;
221       mr.VolFiles     = sdmr.VolFiles;
222       mr.VolBlocks    = sdmr.VolBlocks;
223       mr.VolBytes     = sdmr.VolBytes;
224       mr.VolMounts    = sdmr.VolMounts;
225       mr.VolErrors    = sdmr.VolErrors;
226       mr.VolWrites    = sdmr.VolWrites;
227       mr.LastWritten  = sdmr.LastWritten;
228       mr.Slot         = sdmr.Slot;
229       mr.InChanger    = sdmr.InChanger;
230       mr.VolReadTime  = sdmr.VolReadTime;
231       mr.VolWriteTime = sdmr.VolWriteTime;
232       bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
233
234       Dmsg2(300, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
235       /*
236        * Check if it has expired, and if not update the DB. Note, if
237        *   Volume has expired, has_volume_expired() will update the DB.
238        */
239       if (has_volume_expired(jcr, &mr)) {
240          send_volume_info_to_storage_daemon(jcr, bs, &mr);
241       } else if (db_update_media_record(jcr, jcr->db, &mr)) {
242          send_volume_info_to_storage_daemon(jcr, bs, &mr);
243       } else {
244          Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
245             db_strerror(jcr->db));
246          bnet_fsend(bs, "1992 Update Media error\n");
247          Dmsg0(190, "send error\n");
248       }
249       db_unlock(jcr->db);
250
251    /*
252     * Request to create a JobMedia record
253     */
254    } else if (sscanf(bs->msg, Create_job_media, &Job,
255       &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
256       &jm.StartBlock, &jm.EndBlock) == 7) {
257
258       jm.JobId = jcr->JobId;
259       jm.MediaId = jcr->MediaId;
260       Dmsg6(300, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
261          jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
262       if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
263          Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
264             db_strerror(jcr->db));
265          bnet_fsend(bs, "1991 Update JobMedia error\n");
266       } else {
267          Dmsg0(300, "JobMedia record created\n");
268          bnet_fsend(bs, OK_create);
269       }
270
271    } else {
272       omsg = get_memory(bs->msglen+1);
273       pm_strcpy(omsg, bs->msg);
274       bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
275       Jmsg1(jcr, M_ERROR, 0, _("Invalid Catalog request: %s"), omsg);
276       free_memory(omsg);
277    }
278    Dmsg1(300, ">CatReq response: %s", bs->msg);
279    Dmsg1(200, "Leave catreq jcr 0x%x\n", jcr);
280    return;
281 }
282
283 /*
284  * Update File Attributes in the catalog with data
285  *  sent by the Storage daemon.  Note, we receive the whole
286  *  attribute record, but we select out only the stat packet,
287  *  VolSessionId, VolSessionTime, FileIndex, and file name
288  *  to store in the catalog.
289  */
290 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
291 {
292    unser_declare;
293    uint32_t VolSessionId, VolSessionTime;
294    int32_t Stream;
295    uint32_t FileIndex;
296    uint32_t data_len;
297    char *p = bs->msg;
298    int len;
299    char *fname, *attr;
300    ATTR_DBR ar;
301
302    if (!jcr->pool->catalog_files) {
303       return;
304    }
305    db_start_transaction(jcr, jcr->db);     /* start transaction if not already open */
306    skip_nonspaces(&p);                /* UpdCat */
307    skip_spaces(&p);
308    skip_nonspaces(&p);                /* Job=nnn */
309    skip_spaces(&p);
310    skip_nonspaces(&p);                /* FileAttributes */
311    p += 1;
312    unser_begin(p, 0);
313    unser_uint32(VolSessionId);
314    unser_uint32(VolSessionTime);
315    unser_int32(FileIndex);
316    unser_int32(Stream);
317    unser_uint32(data_len);
318    p += unser_length(p);
319
320    Dmsg1(300, "UpdCat msg=%s\n", bs->msg);
321    Dmsg5(300, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
322       VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
323
324    if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_UNIX_ATTRIBUTES_EX) {
325       skip_nonspaces(&p);             /* skip FileIndex */
326       skip_spaces(&p);
327       skip_nonspaces(&p);             /* skip FileType */
328       skip_spaces(&p);
329       fname = p;
330       len = strlen(fname);        /* length before attributes */
331       attr = &fname[len+1];
332
333       Dmsg2(300, "dird<stored: stream=%d %s\n", Stream, fname);
334       Dmsg1(300, "dird<stored: attr=%s\n", attr);
335       ar.attr = attr;
336       ar.fname = fname;
337       ar.FileIndex = FileIndex;
338       ar.Stream = Stream;
339       ar.link = NULL;
340       ar.JobId = jcr->JobId;
341
342       Dmsg2(300, "dird<filed: stream=%d %s\n", Stream, fname);
343       Dmsg1(120, "dird<filed: attr=%s\n", attr);
344
345       if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
346          Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
347       }
348       /* Save values for SIG update */
349       jcr->FileId = ar.FileId;
350       jcr->FileIndex = FileIndex;
351    } else if (Stream == STREAM_MD5_SIGNATURE || Stream == STREAM_SHA1_SIGNATURE) {
352       fname = p;
353       if (jcr->FileIndex != FileIndex) {
354          Jmsg(jcr, M_WARNING, 0, "Got MD5/SHA1 but not same File as attributes\n");
355       } else {
356          /* Update signature in catalog */
357          char SIGbuf[50];           /* 24 bytes should be enough */
358          int len, type;
359          if (Stream == STREAM_MD5_SIGNATURE) {
360             len = 16;
361             type = MD5_SIG;
362          } else {
363             len = 20;
364             type = SHA1_SIG;
365          }
366          bin_to_base64(SIGbuf, fname, len);
367          Dmsg3(190, "SIGlen=%d SIG=%s type=%d\n", strlen(SIGbuf), SIGbuf, Stream);
368          if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIGbuf, type)) {
369             Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5/SHA1. %s"),
370                db_strerror(jcr->db));
371          }
372       }
373    }
374 }