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