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