]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/catreq.c
Important protocol change -- see kes29Oct02
[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=%d VolFiles=%d VolBlocks=%d VolBytes=%" lld " VolMounts=%d\
49  VolErrors=%d VolWrites=%d VolMaxBytes=%" lld " EndTime=%d VolStatus=%10s\
50  Slot=%d relabel=%d\n";
51
52 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia \
53  FirstIndex=%d LastIndex=%d StartFile=%d EndFile=%d \
54  StartBlock=%d EndBlock=%d\n";
55
56
57 /* Responses  sent to Storage daemon */
58 static char OK_media[] = "1000 OK VolName=%s VolJobs=%d VolFiles=%d\
59  VolBlocks=%d VolBytes=%" lld " VolMounts=%d VolErrors=%d VolWrites=%d\
60  VolMaxBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%s Slot=%d\n";
61
62 static char OK_update[] = "1000 OK UpdateMedia\n";
63
64 /* static char FileAttributes[] = "UpdCat Job=%127s FileAttributes "; */
65
66
67 void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
68 {
69    MEDIA_DBR mr; 
70    JOBMEDIA_DBR jm;
71    char Job[MAX_NAME_LENGTH];
72    int index, ok, relabel, writing;
73    char *omsg;
74
75    memset(&mr, 0, sizeof(mr));
76    memset(&jm, 0, sizeof(jm));
77
78    /*
79     * Request to find next appendable Volume for this Job
80     */
81    Dmsg1(120, "catreq %s", bs->msg);
82    if (sscanf(bs->msg, Find_media, &Job, &index) == 2) {
83       mr.PoolId = jcr->PoolId;
84       strcpy(mr.MediaType, jcr->store->media_type);
85       strcpy(mr.VolStatus, "Append");
86       Dmsg3(120, "CatReq FindMedia: Id=%d, MediaType=%s, Status=%s\n",
87          mr.PoolId, mr.MediaType, mr.VolStatus);
88       /*
89        * Find the Volume
90        */
91       ok = db_find_next_volume(jcr->db, index, &mr);  
92       if (!ok) {
93          /* Well, try finding recycled tapes */
94          ok = find_recycled_volume(jcr, &mr);
95          Dmsg1(100, "find_recycled_volume1 %d\n", ok);
96          if (!ok) {
97             prune_volumes(jcr);  
98             ok = recycle_a_volume(jcr, &mr);
99             Dmsg1(100, "find_recycled_volume2 %d\n", ok);
100             if (!ok) {
101                /* See if we can create a new Volume */
102                ok = newVolume(jcr, &mr);
103             }
104          }
105       }
106       /*
107        * Send Find Media response to Storage daemon 
108        */
109       if (ok) {
110          jcr->MediaId = mr.MediaId;
111          strcpy(jcr->VolumeName, mr.VolumeName);
112          bash_spaces(mr.VolumeName);
113          bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
114             mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
115             mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
116             mr.VolStatus, mr.Slot);
117       } else {
118          bnet_fsend(bs, "1999 No Media\n");
119       }
120
121    /* 
122     * Request to find specific Volume information
123     */
124    } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
125       Dmsg1(120, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
126       /*
127        * Find the Volume
128        */
129       unbash_spaces(mr.VolumeName);
130       if (db_get_media_record(jcr->db, &mr)) {
131          int VolSuitable = 0;
132          jcr->MediaId = mr.MediaId;
133          Dmsg1(120, "VolumeInfo MediaId=%d\n", jcr->MediaId);
134          strcpy(jcr->VolumeName, mr.VolumeName);
135          if (!writing) {
136             VolSuitable = 1;          /* accept anything for read */
137          } else {
138             /* 
139              * Make sure this volume is suitable for this job, i.e.
140              *  it is either Append or Recycle and Media Type matches 
141              *  and Pool allows any volume.
142              */
143             if (mr.PoolId == jcr->PoolId && 
144                 (strcmp(mr.VolStatus, "Append") == 0 ||
145                  strcmp(mr.VolStatus, "Recycle") == 0) &&
146                  strcmp(mr.MediaType, jcr->store->media_type) == 0 &&
147                  jcr->pool->accept_any_volume) {
148                VolSuitable = 1;
149             }
150          }
151          if (VolSuitable) {
152             /*
153              * Send Find Media response to Storage daemon 
154              */
155             bash_spaces(mr.VolumeName);
156             bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
157                mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
158                mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
159                mr.VolStatus, mr.Slot);
160             Dmsg5(200, "get_media_record PoolId=%d wanted %d, Status=%s, Slot=%d \
161 MediaType=%s\n", mr.PoolId, jcr->PoolId, mr.VolStatus, mr.Slot, mr.MediaType);
162          } else { 
163             /* Not suitable volume */
164             bnet_fsend(bs, "1998 Volume not appropriate.\n");
165          }
166
167       } else {
168          bnet_fsend(bs, "1999 Volume Not Found.\n");
169       }
170
171    
172    /*
173     * Request to update Media record. Comes typically at the end
174     *  of a Storage daemon Job Session
175     */
176    } else if (sscanf(bs->msg, Update_media, &Job, &mr.VolumeName, &mr.VolJobs,
177       &mr.VolFiles, &mr.VolBlocks, &mr.VolBytes, &mr.VolMounts, &mr.VolErrors,
178       &mr.VolWrites, &mr.VolMaxBytes, &mr.LastWritten, &mr.VolStatus, 
179       &mr.Slot, &relabel) == 14) {
180       /*     
181        * Update Media Record
182        */
183       if ((mr.VolMaxBytes > 0 && mr.VolBytes >= mr.VolMaxBytes) ||
184           (mr.VolBytes > 0 && jcr->pool->use_volume_once)) {
185          strcpy(mr.VolStatus, "Full");
186       }
187
188       Dmsg2(100, "db_update_media_record. Stat=%s Vol=%s\n",
189          mr.VolStatus, mr.VolumeName);
190       if (db_update_media_record(jcr->db, &mr)) {
191          bnet_fsend(bs, OK_update);
192          Dmsg0(190, "send OK\n");
193       } else {
194          Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
195             db_strerror(jcr->db));
196          bnet_fsend(bs, "1992 Update Media error\n");
197          Dmsg0(190, "send error\n");
198       }
199
200    /*
201     * Request to create a JobMedia record
202     */
203    } else if (sscanf(bs->msg, Create_job_media, &Job,
204       &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
205       &jm.StartBlock, &jm.EndBlock) == 7) {
206
207       jm.JobId = jcr->JobId;
208       jm.MediaId = jcr->MediaId;
209       Dmsg6(100, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
210          jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
211       if(!db_create_jobmedia_record(jcr->db, &jm)) {
212          Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
213             db_strerror(jcr->db));
214          bnet_fsend(bs, "1991 Update JobMedia error\n");
215       } else {
216          Dmsg0(100, "JobMedia record created\n");
217          bnet_fsend(bs, OK_update);
218       }
219
220    } else {
221       omsg = (char *) get_memory(bs->msglen+1);
222       strcpy(omsg, bs->msg);
223       bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);    
224       free_memory(omsg);
225    }
226    Dmsg1(120, ">CatReq response: %s", bs->msg);
227    return;
228 }
229
230 /*
231  * Update File Attributes in the catalog with data
232  *  sent by the Storage daemon.
233  */
234 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
235 {
236    unser_declare;
237    uint32_t VolSessionId, VolSessionTime;
238    int32_t Stream;
239    uint32_t FileIndex;
240    uint32_t data_len;
241    char *p = bs->msg;
242    int len;
243    char *fname, *attr;
244    ATTR_DBR ar;
245
246    if (!jcr->pool->catalog_files) {
247       return;
248    }
249    db_start_transaction(jcr->db);     /* start transaction if not already open */
250    skip_nonspaces(&p);                /* UpdCat */
251    skip_spaces(&p);
252    skip_nonspaces(&p);                /* Job=nnn */
253    skip_spaces(&p);
254    skip_nonspaces(&p);                /* FileAttributes */
255    p += 1;
256    unser_begin(p, 0);
257    unser_uint32(VolSessionId);
258    unser_uint32(VolSessionTime);
259    unser_int32(FileIndex);
260    unser_int32(Stream);
261    unser_uint32(data_len);
262    p += unser_length(p);
263
264    Dmsg1(99, "UpdCat msg=%s\n", bs->msg);
265    Dmsg5(99, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
266       VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
267
268    if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_WIN32_ATTRIBUTES) {
269       skip_nonspaces(&p);             /* skip FileIndex */
270       skip_spaces(&p);
271       skip_nonspaces(&p);             /* skip FileType */
272       skip_spaces(&p);
273       fname = p;
274       len = strlen(fname);        /* length before attributes */
275       attr = &fname[len+1];
276
277       Dmsg2(109, "dird<stored: stream=%d %s\n", Stream, fname);
278       Dmsg1(109, "dird<stored: attr=%s\n", attr);
279       ar.attr = attr; 
280       ar.fname = fname;
281       ar.FileIndex = FileIndex;
282       ar.Stream = Stream;
283       ar.link = NULL;
284       ar.JobId = jcr->JobId;
285
286       Dmsg2(111, "dird<filed: stream=%d %s\n", Stream, fname);
287       Dmsg1(120, "dird<filed: attr=%s\n", attr);
288
289       if (!db_create_file_attributes_record(jcr->db, &ar)) {
290          Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
291       }
292       /* Save values for MD5 update */
293       jcr->FileId = ar.FileId;
294       jcr->FileIndex = FileIndex;
295    } else if (Stream == STREAM_MD5_SIGNATURE) {
296       fname = p;
297       if (jcr->FileIndex != FileIndex) {    
298          Jmsg(jcr, M_WARNING, 0, "Got MD5 but not same File as attributes\n");
299       } else {
300          /* Update MD5 signature in catalog */
301          char MD5buf[50];           /* 24 bytes should be enough */
302          bin_to_base64(MD5buf, fname, 16);
303          Dmsg2(190, "MD5len=%d MD5=%s\n", strlen(MD5buf), MD5buf);
304          if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, MD5buf)) {
305             Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5. %s"), 
306                db_strerror(jcr->db));
307          }
308       }
309    }
310 }