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