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