]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/catreq.c
kes Add additional sm_check() code on debug level 1.
[bacula/bacula] / bacula / src / dird / catreq.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2001-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation plus additions
11    that are listed in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *
30  *   Bacula Director -- catreq.c -- handles the message channel
31  *    catalog request from the Storage daemon.
32  *
33  *     Kern Sibbald, March MMI
34  *
35  *    This routine runs as a thread and must be thread reentrant.
36  *
37  *  Basic tasks done here:
38  *      Handle Catalog services.
39  *
40  *   Version $Id$
41  */
42
43 #include "bacula.h"
44 #include "dird.h"
45 #include "findlib/find.h"
46
47 /*
48  * Handle catalog request
49  *  For now, we simply return next Volume to be used
50  */
51
52 /* Requests from the Storage daemon */
53 static char Find_media[] = "CatReq Job=%127s FindMedia=%d pool_name=%127s media_type=%127s\n";
54 static char Get_Vol_Info[] = "CatReq Job=%127s GetVolInfo VolName=%127s write=%d\n";
55
56 static char Update_media[] = "CatReq Job=%127s UpdateMedia VolName=%s"
57    " VolJobs=%u VolFiles=%u VolBlocks=%u VolBytes=%" lld " VolMounts=%u"
58    " VolErrors=%u VolWrites=%u MaxVolBytes=%" lld " EndTime=%d VolStatus=%10s"
59    " Slot=%d relabel=%d InChanger=%d VolReadTime=%" lld " VolWriteTime=%" lld
60    " VolFirstWritten=%" lld " VolParts=%u\n";
61
62 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
63    " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
64    " StartBlock=%u EndBlock=%u Copy=%d Strip=%d MediaId=%" lld "\n";
65
66
67 /* Responses  sent to Storage daemon */
68 static char OK_media[] = "1000 OK VolName=%s VolJobs=%u VolFiles=%u"
69    " VolBlocks=%u VolBytes=%s VolMounts=%u VolErrors=%u VolWrites=%u"
70    " MaxVolBytes=%s VolCapacityBytes=%s VolStatus=%s Slot=%d"
71    " MaxVolJobs=%u MaxVolFiles=%u InChanger=%d VolReadTime=%s"
72    " VolWriteTime=%s EndFile=%u EndBlock=%u VolParts=%u LabelType=%d"
73    " MediaId=%s\n";
74
75 static char OK_create[] = "1000 OK CreateJobMedia\n";
76
77
78 static int send_volume_info_to_storage_daemon(JCR *jcr, BSOCK *sd, MEDIA_DBR *mr)
79 {
80    int stat;
81    char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50], ed6[50];
82
83    jcr->MediaId = mr->MediaId;
84    pm_strcpy(jcr->VolumeName, mr->VolumeName);
85    bash_spaces(mr->VolumeName);
86    stat = bnet_fsend(sd, OK_media, mr->VolumeName, mr->VolJobs,
87       mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
88       mr->VolMounts, mr->VolErrors, mr->VolWrites,
89       edit_uint64(mr->MaxVolBytes, ed2),
90       edit_uint64(mr->VolCapacityBytes, ed3),
91       mr->VolStatus, mr->Slot, mr->MaxVolJobs, mr->MaxVolFiles,
92       mr->InChanger,
93       edit_int64(mr->VolReadTime, ed4),
94       edit_int64(mr->VolWriteTime, ed5),
95       mr->EndFile, mr->EndBlock,
96       mr->VolParts,
97       mr->LabelType,
98       edit_uint64(mr->MediaId, ed6));
99    unbash_spaces(mr->VolumeName);
100    Dmsg2(100, "Vol Info for %s: %s", jcr->Job, sd->msg);
101    return stat;
102 }
103
104 void catalog_request(JCR *jcr, BSOCK *bs)
105 {
106    MEDIA_DBR mr, sdmr;
107    JOBMEDIA_DBR jm;
108    char Job[MAX_NAME_LENGTH];
109    char pool_name[MAX_NAME_LENGTH];
110    int index, ok, label, writing;
111    POOLMEM *omsg;
112    POOL_DBR pr;
113    uint32_t Stripe;
114    uint64_t MediaId;
115    utime_t VolFirstWritten;
116
117    memset(&mr, 0, sizeof(mr));
118    memset(&sdmr, 0, sizeof(sdmr));
119    memset(&jm, 0, sizeof(jm));
120    Dsm_check(1);      
121
122    /*
123     * Request to find next appendable Volume for this Job
124     */
125    Dmsg1(100, "catreq %s", bs->msg);
126    if (!jcr->db) {
127       omsg = get_memory(bs->msglen+1);
128       pm_strcpy(omsg, bs->msg);
129       bnet_fsend(bs, _("1990 Invalid Catalog Request: %s"), omsg);    
130       Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request; DB not open: %s"), omsg);
131       free_memory(omsg);
132       return;
133    }
134    /*
135     * Find next appendable medium for SD
136     */
137    if (sscanf(bs->msg, Find_media, &Job, &index, &pool_name, &mr.MediaType) == 4) {
138       memset(&pr, 0, sizeof(pr));
139       bstrncpy(pr.Name, pool_name, sizeof(pr.Name));
140       unbash_spaces(pr.Name);
141       ok = db_get_pool_record(jcr, jcr->db, &pr);
142       if (ok) {
143          mr.PoolId = pr.PoolId;
144          mr.StorageId = jcr->wstore->StorageId;
145          ok = find_next_volume_for_append(jcr, &mr, index, fnv_create_vol, fnv_prune);
146          Dmsg3(050, "find_media ok=%d idx=%d vol=%s\n", ok, index, mr.VolumeName);
147       }
148       /*
149        * Send Find Media response to Storage daemon
150        */
151       if (ok) {
152          send_volume_info_to_storage_daemon(jcr, bs, &mr);
153       } else {
154          bnet_fsend(bs, _("1901 No Media.\n"));
155          Dmsg0(500, "1901 No Media.\n");
156       }
157
158    /*
159     * Request to find specific Volume information
160     */
161    } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
162       Dmsg1(100, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
163       /*
164        * Find the Volume
165        */
166       unbash_spaces(mr.VolumeName);
167       if (db_get_media_record(jcr, jcr->db, &mr)) {
168          const char *reason = NULL;           /* detailed reason for rejection */
169          /*
170           * If we are reading, accept any volume (reason == NULL)
171           * If we are writing, check if the Volume is valid
172           *   for this job, and do a recycle if necessary
173           */
174          if (writing) {
175             /*
176              * SD wants to write this Volume, so make
177              *   sure it is suitable for this job, i.e.
178              *   Pool matches, and it is either Append or Recycle
179              *   and Media Type matches and Pool allows any volume.
180              */
181             if (mr.PoolId != jcr->jr.PoolId) {
182                reason = _("not in Pool");
183             } else if (strcmp(mr.MediaType, jcr->wstore->media_type) != 0) {
184                reason = _("not correct MediaType");
185             } else {
186                /*
187                 * Now try recycling if necessary
188                 *   reason set non-NULL if we cannot use it
189                 */
190                check_if_volume_valid_or_recyclable(jcr, &mr, &reason);
191             }
192          }
193          if (!reason && mr.Enabled != 1) {
194             reason = _("is not Enabled");
195          }
196          if (reason == NULL) {
197             /*
198              * Send Find Media response to Storage daemon
199              */
200             send_volume_info_to_storage_daemon(jcr, bs, &mr);
201          } else {
202             /* Not suitable volume */
203             bnet_fsend(bs, _("1998 Volume \"%s\" status is %s, %s.\n"), mr.VolumeName,
204                mr.VolStatus, reason);
205          }
206
207       } else {
208          bnet_fsend(bs, _("1997 Volume \"%s\" not in catalog.\n"), mr.VolumeName);
209          Dmsg1(100, "1997 Volume \"%s\" not in catalog.\n", mr.VolumeName);
210       }
211
212    /*
213     * Request to update Media record. Comes typically at the end
214     *  of a Storage daemon Job Session, when labeling/relabeling a
215     *  Volume, or when an EOF mark is written.
216     */
217    } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName,
218       &sdmr.VolJobs, &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes,
219       &sdmr.VolMounts, &sdmr.VolErrors, &sdmr.VolWrites, &sdmr.MaxVolBytes,
220       &sdmr.LastWritten, &sdmr.VolStatus, &sdmr.Slot, &label, &sdmr.InChanger,
221       &sdmr.VolReadTime, &sdmr.VolWriteTime, &VolFirstWritten,
222       &sdmr.VolParts) == 19) {
223
224       db_lock(jcr->db);
225       Dmsg3(400, "Update media %s oldStat=%s newStat=%s\n", sdmr.VolumeName,
226          mr.VolStatus, sdmr.VolStatus);
227       bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
228       unbash_spaces(mr.VolumeName);
229       if (!db_get_media_record(jcr, jcr->db, &mr)) {
230          Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
231               mr.VolumeName, db_strerror(jcr->db));
232          bnet_fsend(bs, _("1991 Catalog Request for vol=%s failed: %s"),
233             mr.VolumeName, db_strerror(jcr->db));
234          db_unlock(jcr->db);
235          return;
236       }
237       /* Set first written time if this is first job */
238       if (mr.FirstWritten == 0) {
239          if (VolFirstWritten == 0) {
240             mr.FirstWritten = jcr->start_time;   /* use Job start time as first write */
241          } else {
242             mr.FirstWritten = VolFirstWritten;
243          }
244          mr.set_first_written = true;
245       }
246       /* If we just labeled the tape set time */
247       if (label || mr.LabelDate == 0) {
248          mr.LabelDate = jcr->start_time;
249          mr.set_label_date = true;
250          if (mr.InitialWrite == 0) {
251             mr.InitialWrite = jcr->start_time;
252          }
253          Dmsg2(400, "label=%d labeldate=%d\n", label, mr.LabelDate);
254       } else {
255          /*
256           * Insanity check for VolFiles get set to a smaller value
257           */
258          if (sdmr.VolFiles < mr.VolFiles) {
259             Jmsg(jcr, M_FATAL, 0, _("Volume Files at %u being set to %u"
260                  " for Volume \"%s\". This is incorrect.\n"),
261                mr.VolFiles, sdmr.VolFiles, mr.VolumeName);
262             bnet_fsend(bs, _("1992 Update Media error. VolFiles=%u, CatFiles=%u\n"),
263                sdmr.VolFiles, mr.VolFiles);
264             db_unlock(jcr->db);
265             return;
266          }
267       }
268       Dmsg2(400, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
269       /* Copy updated values to original media record */
270       mr.VolJobs      = sdmr.VolJobs;
271       mr.VolFiles     = sdmr.VolFiles;
272       mr.VolBlocks    = sdmr.VolBlocks;
273       mr.VolBytes     = sdmr.VolBytes;
274       mr.VolMounts    = sdmr.VolMounts;
275       mr.VolErrors    = sdmr.VolErrors;
276       mr.VolWrites    = sdmr.VolWrites;
277       mr.LastWritten  = sdmr.LastWritten;
278       mr.Slot         = sdmr.Slot;
279       mr.InChanger    = sdmr.InChanger;
280       mr.VolReadTime  = sdmr.VolReadTime;
281       mr.VolWriteTime = sdmr.VolWriteTime;
282       mr.VolParts     = sdmr.VolParts;
283       bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
284       if (jcr->wstore && jcr->wstore->StorageId) {
285          mr.StorageId = jcr->wstore->StorageId;
286       }
287
288       Dmsg2(400, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
289       /*
290        * Update the database, then before sending the response to the
291        *  SD, check if the Volume has expired.
292        */
293       if (!db_update_media_record(jcr, jcr->db, &mr)) {
294          Jmsg(jcr, M_FATAL, 0, _("Catalog error updating Media record. %s"),
295             db_strerror(jcr->db));
296          bnet_fsend(bs, _("1993 Update Media error\n"));
297          Dmsg0(400, "send error\n");
298       } else {
299          (void)has_volume_expired(jcr, &mr);
300          send_volume_info_to_storage_daemon(jcr, bs, &mr);
301       }
302       db_unlock(jcr->db);
303
304    /*
305     * Request to create a JobMedia record
306     */
307    } else if (sscanf(bs->msg, Create_job_media, &Job,
308       &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
309       &jm.StartBlock, &jm.EndBlock, &jm.Copy, &Stripe, &MediaId) == 10) {
310
311       if (jcr->mig_jcr) {
312          jm.JobId = jcr->mig_jcr->JobId;
313       } else {
314          jm.JobId = jcr->JobId;
315       }
316       jm.MediaId = MediaId;
317       Dmsg6(400, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
318          jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
319       if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
320          Jmsg(jcr, M_FATAL, 0, _("Catalog error creating JobMedia record. %s"),
321             db_strerror(jcr->db));
322          bnet_fsend(bs, _("1991 Update JobMedia error\n"));
323       } else {
324          Dmsg0(400, "JobMedia record created\n");
325          bnet_fsend(bs, OK_create);
326       }
327
328    } else {
329       omsg = get_memory(bs->msglen+1);
330       pm_strcpy(omsg, bs->msg);
331       bnet_fsend(bs, _("1990 Invalid Catalog Request: %s"), omsg);
332       Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request: %s"), omsg);
333       free_memory(omsg);
334    }
335    Dmsg1(400, ">CatReq response: %s", bs->msg);
336    Dmsg1(400, "Leave catreq jcr 0x%x\n", jcr);
337    return;
338 }
339
340 /*
341  * Update File Attributes in the catalog with data
342  *  sent by the Storage daemon.  Note, we receive the whole
343  *  attribute record, but we select out only the stat packet,
344  *  VolSessionId, VolSessionTime, FileIndex, and file name
345  *  to store in the catalog.
346  */
347 void catalog_update(JCR *jcr, BSOCK *bs)
348 {
349    unser_declare;
350    uint32_t VolSessionId, VolSessionTime;
351    int32_t Stream;
352    uint32_t FileIndex;
353    uint32_t data_len;
354    char *p;
355    int len;
356    char *fname, *attr;
357    ATTR_DBR *ar = NULL;
358    POOLMEM *omsg;
359
360    Dsm_check(1);
361    if (!jcr->pool->catalog_files) {
362       return;                         /* user disabled cataloging */
363    }
364    if (!jcr->db) {
365       omsg = get_memory(bs->msglen+1);
366       pm_strcpy(omsg, bs->msg);
367       bnet_fsend(bs, _("1991 Invalid Catalog Update: %s"), omsg);    
368       Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog Update; DB not open: %s"), omsg);
369       free_memory(omsg);
370       return;
371    }
372
373    /* Start transaction allocates jcr->attr and jcr->ar if needed */
374    db_start_transaction(jcr, jcr->db);     /* start transaction if not already open */
375    ar = jcr->ar;      
376
377    /* Start by scanning directly in the message buffer to get Stream   
378     *  there may be a cached attr so we cannot yet write into
379     *  jcr->attr or jcr->ar  
380     */
381    p = bs->msg;
382    skip_nonspaces(&p);                /* UpdCat */
383    skip_spaces(&p);
384    skip_nonspaces(&p);                /* Job=nnn */
385    skip_spaces(&p);
386    skip_nonspaces(&p);                /* FileAttributes */
387    p += 1;
388    unser_begin(p, 0);
389    unser_uint32(VolSessionId);
390    unser_uint32(VolSessionTime);
391    unser_int32(FileIndex);
392    unser_int32(Stream);
393    unser_uint32(data_len);
394    p += unser_length(p);
395
396    Dmsg1(400, "UpdCat msg=%s\n", bs->msg);
397    Dmsg5(400, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
398       VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
399
400    if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_UNIX_ATTRIBUTES_EX) {
401       if (jcr->cached_attribute) {
402          Dmsg2(400, "Cached attr. Stream=%d fname=%s\n", ar->Stream, ar->fname);
403          if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
404             Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
405          }
406       }
407       /* Any cached attr is flushed so we can reuse jcr->attr and jcr->ar */
408       jcr->attr = check_pool_memory_size(jcr->attr, bs->msglen);
409       memcpy(jcr->attr, bs->msg, bs->msglen);
410       p = jcr->attr - bs->msg + p;    /* point p into jcr->attr */
411       skip_nonspaces(&p);             /* skip FileIndex */
412       skip_spaces(&p);
413       skip_nonspaces(&p);             /* skip FileType */
414       skip_spaces(&p);
415       fname = p;
416       len = strlen(fname);        /* length before attributes */
417       attr = &fname[len+1];
418
419       Dmsg2(400, "dird<stored: stream=%d %s\n", Stream, fname);
420       Dmsg1(400, "dird<stored: attr=%s\n", attr);
421       ar->attr = attr;
422       ar->fname = fname;
423       ar->FileIndex = FileIndex;
424       ar->Stream = Stream;
425       ar->link = NULL;
426       if (jcr->mig_jcr) {
427          ar->JobId = jcr->mig_jcr->JobId;
428       } else {
429          ar->JobId = jcr->JobId;
430       }
431       ar->Digest = NULL;
432       ar->DigestType = CRYPTO_DIGEST_NONE;
433       jcr->cached_attribute = true;
434
435       Dmsg2(400, "dird<filed: stream=%d %s\n", Stream, fname);
436       Dmsg1(400, "dird<filed: attr=%s\n", attr);
437
438    } else if (crypto_digest_stream_type(Stream) != CRYPTO_DIGEST_NONE) {
439       fname = p;
440       if (ar->FileIndex != FileIndex) {
441          Jmsg(jcr, M_WARNING, 0, _("Got %s but not same File as attributes\n"), stream_to_ascii(Stream));
442       } else {
443          /* Update digest in catalog */
444          char digestbuf[BASE64_SIZE(CRYPTO_DIGEST_MAX_SIZE)];
445          int len = 0;
446          int type = CRYPTO_DIGEST_NONE;
447
448          switch(Stream) {
449          case STREAM_MD5_DIGEST:
450             len = CRYPTO_DIGEST_MD5_SIZE;
451             type = CRYPTO_DIGEST_MD5;
452             break;
453          case STREAM_SHA1_DIGEST:
454             len = CRYPTO_DIGEST_SHA1_SIZE;
455             type = CRYPTO_DIGEST_SHA1;
456             break;
457          case STREAM_SHA256_DIGEST:
458             len = CRYPTO_DIGEST_SHA256_SIZE;
459             type = CRYPTO_DIGEST_SHA256;
460             break;
461          case STREAM_SHA512_DIGEST:
462             len = CRYPTO_DIGEST_SHA512_SIZE;
463             type = CRYPTO_DIGEST_SHA512;
464             break;
465          default:
466             /* Never reached ... */
467             Jmsg(jcr, M_ERROR, 0, _("Catalog error updating file digest. Unsupported digest stream type: %d"),
468                  Stream);
469          }
470
471          bin_to_base64(digestbuf, sizeof(digestbuf), fname, len, true);
472          Dmsg3(400, "DigestLen=%d Digest=%s type=%d\n", strlen(digestbuf), digestbuf, Stream);
473          if (jcr->cached_attribute) {
474             ar->Digest = digestbuf;
475             ar->DigestType = type;
476             Dmsg2(400, "Cached attr with digest. Stream=%d fname=%s\n", ar->Stream, ar->fname);
477             if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
478                Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
479             }
480             jcr->cached_attribute = false; 
481          } else {
482             if (!db_add_digest_to_file_record(jcr, jcr->db, ar->FileId, digestbuf, type)) {
483                Jmsg(jcr, M_ERROR, 0, _("Catalog error updating file digest. %s"),
484                   db_strerror(jcr->db));
485             }
486          }
487       }
488    }
489 }