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