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