]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/catreq.c
0a74a5b79a4f0bb5130d215437261a63882f950d
[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, relabel, writing, retry = 0;
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(120, "catreq %s", bs->msg);
84    if (sscanf(bs->msg, Find_media, &Job, &index) == 2) {
85       mr.PoolId = jcr->PoolId;
86       strcpy(mr.MediaType, jcr->store->media_type);
87       Dmsg2(120, "CatReq FindMedia: Id=%d, MediaType=%s\n",
88          mr.PoolId, mr.MediaType);
89       /*
90        * Find the Next Volume for Append
91        */
92 next_volume:
93       strcpy(mr.VolStatus, "Append");  /* want only appendable volumes */
94       ok = db_find_next_volume(jcr, jcr->db, index, &mr);  
95       Dmsg2(100, "catreq after find_next_vol ok=%d FW=%d\n", ok, mr.FirstWritten);
96       if (!ok) {
97          /* Well, try finding recycled tapes */
98          ok = find_recycled_volume(jcr, &mr);
99          Dmsg2(100, "find_recycled_volume1 %d FW=%d\n", ok, mr.FirstWritten);
100          if (!ok) {
101             prune_volumes(jcr);  
102             ok = recycle_a_volume(jcr, &mr);
103             Dmsg2(200, "find_recycled_volume2 %d FW=%d\n", ok, mr.FirstWritten);
104             if (!ok) {
105                /* See if we can create a new Volume */
106                ok = newVolume(jcr, &mr);
107             }
108          }
109       }
110       /* Check if use duration has expired */
111       Dmsg2(100, "VolJobs=%d FirstWritten=%d\n", mr.VolJobs, mr.FirstWritten);
112       if (ok && mr.VolJobs > 0 && mr.VolUseDuration > 0 && 
113            strcmp(mr.VolStatus, "Recycle") != 0) {
114          utime_t now = time(NULL);
115          if (mr.VolUseDuration <= (now - mr.FirstWritten)) {
116             Dmsg4(100, "Duration=%d now=%d start=%d now-start=%d\n",
117                (int)jcr->pool->VolUseDuration, (int)now, (int)mr.FirstWritten, 
118                (int)(now-mr.FirstWritten));
119             Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "       
120                "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
121             strcpy(mr.VolStatus, "Used");  /* yes, mark as used */
122             if (!db_update_media_record(jcr, jcr->db, &mr)) {
123                Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
124                     db_strerror(jcr->db));
125             } else if (retry++ < 200) {     /* sanity check */
126                goto next_volume;
127             } else {
128                Jmsg(jcr, M_ERROR, 0, _(
129 "We seem to be looping trying to find the next volume. I give up. Ask operator.\n"));
130             }
131             ok = FALSE;               /* give up */
132          }
133       }
134
135       /*
136        * Send Find Media response to Storage daemon 
137        */
138       if (ok) {
139          char ed1[50], ed2[50], ed3[50];
140          jcr->MediaId = mr.MediaId;
141          pm_strcpy(&jcr->VolumeName, mr.VolumeName);
142          bash_spaces(mr.VolumeName);
143          bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
144             mr.VolFiles, mr.VolBlocks, edit_uint64(mr.VolBytes, ed1),
145             mr.VolMounts, mr.VolErrors, mr.VolWrites, 
146             edit_uint64(mr.MaxVolBytes, ed2), 
147             edit_uint64(mr.VolCapacityBytes, ed3),
148             mr.VolStatus, mr.Slot, mr.MaxVolJobs, mr.MaxVolFiles);
149          Dmsg1(200, "Find media: %s", bs->msg);
150       } else {
151          bnet_fsend(bs, "1999 No Media\n");
152       }
153
154    /* 
155     * Request to find specific Volume information
156     */
157    } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
158       Dmsg1(120, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
159       /*
160        * Find the Volume
161        */
162       unbash_spaces(mr.VolumeName);
163       if (db_get_media_record(jcr, jcr->db, &mr)) {
164          int VolSuitable = 0;
165          jcr->MediaId = mr.MediaId;
166          Dmsg1(120, "VolumeInfo MediaId=%d\n", jcr->MediaId);
167          pm_strcpy(&jcr->VolumeName, mr.VolumeName);
168          if (!writing) {
169             VolSuitable = 1;          /* accept anything for read */
170          } else {
171             /* 
172              * Make sure this volume is suitable for this job, i.e.
173              *  it is either Append or Recycle and Media Type matches 
174              *  and Pool allows any volume.
175              */
176             if (mr.PoolId == jcr->PoolId && 
177                 (strcmp(mr.VolStatus, "Append") == 0 ||
178                  strcmp(mr.VolStatus, "Recycle") == 0) &&
179                  strcmp(mr.MediaType, jcr->store->media_type) == 0 &&
180                  jcr->pool->accept_any_volume) {
181                VolSuitable = 1;
182             }
183          }
184          if (VolSuitable) {
185             char ed1[50], ed2[50], ed3[50];
186             /*
187              * Send Find Media response to Storage daemon 
188              */
189             bash_spaces(mr.VolumeName);
190             bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
191                mr.VolFiles, mr.VolBlocks, edit_uint64(mr.VolBytes, ed1),
192                mr.VolMounts, mr.VolErrors, mr.VolWrites, 
193                edit_uint64(mr.MaxVolBytes, ed2), 
194                edit_uint64(mr.VolCapacityBytes, ed3),
195                mr.VolStatus, mr.Slot, mr.MaxVolJobs, mr.MaxVolFiles);
196             Dmsg1(200, "Vol Info: %s", bs->msg);
197          } else { 
198             /* Not suitable volume */
199             bnet_fsend(bs, "1998 Volume \"%s\"not appropriate.\n",
200                mr.VolumeName);
201          }
202
203       } else {
204          bnet_fsend(bs, "1999 Volume \"%s\" not found.\n", mr.VolumeName);
205       }
206
207    
208    /*
209     * Request to update Media record. Comes typically at the end
210     *  of a Storage daemon Job Session
211     */
212    } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName, &sdmr.VolJobs,
213       &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes, &sdmr.VolMounts, &sdmr.VolErrors,
214       &sdmr.VolWrites, &sdmr.MaxVolBytes, &sdmr.LastWritten, &sdmr.VolStatus, 
215       &sdmr.Slot, &relabel) == 14) {
216
217       bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
218       if (!db_get_media_record(jcr, jcr->db, &mr)) {
219          Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
220               mr.VolumeName, db_strerror(jcr->db));
221          bnet_fsend(bs, "1991 Catalog Request failed: %s", db_strerror(jcr->db));
222          return;
223       }
224       /* Set first written time if this is first job */
225       if (mr.VolJobs == 0 || sdmr.VolJobs == 1) {
226          mr.FirstWritten = jcr->start_time;   /* use Job start time as first write */
227       }
228       Dmsg2(200, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
229       /* Copy updated values to original media record */
230       mr.VolJobs     = sdmr.VolJobs;
231       mr.VolFiles    = sdmr.VolFiles;
232       mr.VolBlocks   = sdmr.VolBlocks;
233       mr.VolBytes    = sdmr.VolBytes;
234       mr.VolMounts   = sdmr.VolMounts;
235       mr.VolErrors   = sdmr.VolErrors;
236       mr.VolWrites   = sdmr.VolWrites;
237       mr.LastWritten = sdmr.LastWritten;
238       bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
239       mr.Slot = sdmr.Slot;
240
241       /*     
242        * Update Media Record
243        */
244
245       /* First handle Max Volume Bytes */
246       if ((mr.MaxVolBytes > 0 && mr.VolBytes >= mr.MaxVolBytes)) {
247          Jmsg(jcr, M_INFO, 0, _("Max Volume bytes exceeded. "             
248              "Marking Volume \"%s\" as Full.\n"), mr.VolumeName);
249          strcpy(mr.VolStatus, "Full");
250
251       /* Now see if Volume should only be used once */
252       } else if (mr.VolBytes > 0 && jcr->pool->use_volume_once) {
253          Jmsg(jcr, M_INFO, 0, _("Volume used once. "             
254              "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
255          strcpy(mr.VolStatus, "Used");
256
257       /* Now see if Max Jobs written to volume */
258       } else if (mr.MaxVolJobs > 0 && mr.MaxVolJobs <= mr.VolJobs) {
259          Jmsg(jcr, M_INFO, 0, _("Max Volume jobs exceeded. "       
260              "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
261          strcpy(mr.VolStatus, "Used");
262
263       /* Now see if Max Files written to volume */
264       } else if (mr.MaxVolFiles > 0 && mr.MaxVolFiles <= mr.VolFiles) {
265          Jmsg(jcr, M_INFO, 0, _("Max Volume files exceeded. "       
266              "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
267          strcpy(mr.VolStatus, "Used");
268
269       /* Finally, check Use duration expiration */
270       } else if (mr.VolUseDuration > 0) {
271          utime_t now = time(NULL);
272          /* See if Vol Use has expired */
273          if (mr.VolUseDuration <= (now - mr.FirstWritten)) {
274             Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "       
275                "Marking Volume \"%s\"as Used.\n"), mr.VolumeName);
276             strcpy(mr.VolStatus, "Used");  /* yes, mark as used */
277          }
278       }
279
280       Dmsg2(200, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
281       if (db_update_media_record(jcr, jcr->db, &mr)) {
282          bnet_fsend(bs, OK_update);
283          Dmsg0(190, "send OK\n");
284       } else {
285          Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
286             db_strerror(jcr->db));
287          bnet_fsend(bs, "1992 Update Media error\n");
288          Dmsg0(190, "send error\n");
289       }
290
291    /*
292     * Request to create a JobMedia record
293     */
294    } else if (sscanf(bs->msg, Create_job_media, &Job,
295       &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
296       &jm.StartBlock, &jm.EndBlock) == 7) {
297
298       jm.JobId = jcr->JobId;
299       jm.MediaId = jcr->MediaId;
300       Dmsg6(100, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
301          jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
302       if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
303          Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
304             db_strerror(jcr->db));
305          bnet_fsend(bs, "1991 Update JobMedia error\n");
306       } else {
307          Dmsg0(100, "JobMedia record created\n");
308          bnet_fsend(bs, OK_update);
309       }
310
311    } else {
312       omsg = get_memory(bs->msglen+1);
313       pm_strcpy(&omsg, bs->msg);
314       bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);    
315       Jmsg1(jcr, M_ERROR, 0, _("Invalid Catalog request: %s"), omsg);
316       free_memory(omsg);
317    }
318    Dmsg1(120, ">CatReq response: %s", bs->msg);
319    Dmsg1(200, "Leave catreq jcr 0x%x\n", jcr);
320    return;
321 }
322
323 /*
324  * Update File Attributes in the catalog with data
325  *  sent by the Storage daemon.
326  */
327 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
328 {
329    unser_declare;
330    uint32_t VolSessionId, VolSessionTime;
331    int32_t Stream;
332    uint32_t FileIndex;
333    uint32_t data_len;
334    char *p = bs->msg;
335    int len;
336    char *fname, *attr;
337    ATTR_DBR ar;
338
339    if (!jcr->pool->catalog_files) {
340       return;
341    }
342    db_start_transaction(jcr, jcr->db);     /* start transaction if not already open */
343    skip_nonspaces(&p);                /* UpdCat */
344    skip_spaces(&p);
345    skip_nonspaces(&p);                /* Job=nnn */
346    skip_spaces(&p);
347    skip_nonspaces(&p);                /* FileAttributes */
348    p += 1;
349    unser_begin(p, 0);
350    unser_uint32(VolSessionId);
351    unser_uint32(VolSessionTime);
352    unser_int32(FileIndex);
353    unser_int32(Stream);
354    unser_uint32(data_len);
355    p += unser_length(p);
356
357    Dmsg1(99, "UpdCat msg=%s\n", bs->msg);
358    Dmsg5(99, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
359       VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
360
361    if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_WIN32_ATTRIBUTES) {
362       skip_nonspaces(&p);             /* skip FileIndex */
363       skip_spaces(&p);
364       skip_nonspaces(&p);             /* skip FileType */
365       skip_spaces(&p);
366       fname = p;
367       len = strlen(fname);        /* length before attributes */
368       attr = &fname[len+1];
369
370       Dmsg2(109, "dird<stored: stream=%d %s\n", Stream, fname);
371       Dmsg1(109, "dird<stored: attr=%s\n", attr);
372       ar.attr = attr; 
373       ar.fname = fname;
374       ar.FileIndex = FileIndex;
375       ar.Stream = Stream;
376       ar.link = NULL;
377       ar.JobId = jcr->JobId;
378
379       Dmsg2(111, "dird<filed: stream=%d %s\n", Stream, fname);
380       Dmsg1(120, "dird<filed: attr=%s\n", attr);
381
382       if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
383          Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
384       }
385       /* Save values for MD5 update */
386       jcr->FileId = ar.FileId;
387       jcr->FileIndex = FileIndex;
388    } else if (Stream == STREAM_MD5_SIGNATURE) {
389       fname = p;
390       if (jcr->FileIndex != FileIndex) {    
391          Jmsg(jcr, M_WARNING, 0, "Got MD5 but not same File as attributes\n");
392       } else {
393          /* Update MD5 signature in catalog */
394          char MD5buf[50];           /* 24 bytes should be enough */
395          bin_to_base64(MD5buf, fname, 16);
396          Dmsg2(190, "MD5len=%d MD5=%s\n", strlen(MD5buf), MD5buf);
397          if (!db_add_MD5_to_file_record(jcr, jcr->db, jcr->FileId, MD5buf)) {
398             Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5. %s"), 
399                db_strerror(jcr->db));
400          }
401       }
402    }
403 }