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