]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/catreq.c
60697cafa40268c1699ef52219823cef865d89ca
[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  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;
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) == 2) {
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          jcr->MediaId = mr.MediaId;
132          Dmsg1(120, "VolumeInfo MediaId=%d\n", jcr->MediaId);
133          strcpy(jcr->VolumeName, mr.VolumeName);
134          /* 
135           * Make sure this volume is suitable for this job, i.e.
136           *  it is either Append or Recycle and Media Type matches.
137           */
138          if (mr.PoolId == jcr->PoolId && 
139              (strcmp(mr.VolStatus, "Append") == 0 ||
140               strcmp(mr.VolStatus, "Recycle") == 0) &&
141              strcmp(mr.MediaType, jcr->store->media_type) == 0) {
142             /*
143              * Send Find Media response to Storage daemon 
144              */
145             bash_spaces(mr.VolumeName);
146             bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
147                mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
148                mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
149                mr.VolStatus, mr.Slot);
150             Dmsg4(100, "get_media_record PoolId=%d wanted %d, Status=%s, \
151 MediaType=%s\n", mr.PoolId, jcr->PoolId, mr.VolStatus, mr.MediaType);
152          } else { 
153             /* Not suitable volume */
154             bnet_fsend(bs, "1998 Volume not appropriate.\n");
155          }
156
157       } else {
158          bnet_fsend(bs, "1999 Volume Not Found.\n");
159       }
160
161    
162    /*
163     * Request to update Media record. Comes typically at the end
164     *  of a Storage daemon Job Session
165     */
166    } else if (sscanf(bs->msg, Update_media, &Job, &mr.VolumeName, &mr.VolJobs,
167       &mr.VolFiles, &mr.VolBlocks, &mr.VolBytes, &mr.VolMounts, &mr.VolErrors,
168       &mr.VolWrites, &mr.VolMaxBytes, &mr.LastWritten, &mr.VolStatus, 
169       &mr.Slot, &relabel) == 14) {
170       /*     
171        * Update Media Record
172        */
173       if ((mr.VolMaxBytes > 0 && mr.VolBytes >= mr.VolMaxBytes) ||
174           (mr.VolBytes > 0 && jcr->pool->use_volume_once)) {
175          strcpy(mr.VolStatus, "Full");
176       }
177
178       Dmsg2(100, "db_update_media_record. Stat=%s Vol=%s\n",
179          mr.VolStatus, mr.VolumeName);
180       if (db_update_media_record(jcr->db, &mr)) {
181          bnet_fsend(bs, OK_update);
182          Dmsg0(190, "send OK\n");
183       } else {
184          Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
185             db_strerror(jcr->db));
186          bnet_fsend(bs, "1992 Update Media error\n");
187          Dmsg0(190, "send error\n");
188       }
189
190    /*
191     * Request to create a JobMedia record
192     */
193    } else if (sscanf(bs->msg, Create_job_media, &Job,
194       &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
195       &jm.StartBlock, &jm.EndBlock) == 7) {
196
197       jm.JobId = jcr->JobId;
198       jm.MediaId = jcr->MediaId;
199       Dmsg6(100, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
200          jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
201       if(!db_create_jobmedia_record(jcr->db, &jm)) {
202          Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
203             db_strerror(jcr->db));
204          bnet_fsend(bs, "1991 Update JobMedia error\n");
205       } else {
206          Dmsg0(100, "JobMedia record created\n");
207          bnet_fsend(bs, OK_update);
208       }
209
210    } else {
211       omsg = (char *) get_memory(bs->msglen+1);
212       strcpy(omsg, bs->msg);
213       bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);    
214       free_memory(omsg);
215    }
216    Dmsg1(120, ">CatReq response: %s", bs->msg);
217    return;
218 }
219
220 /*
221  * Update File Attributes in the catalog with data
222  *  sent by the Storage daemon.
223  */
224 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
225 {
226    unser_declare;
227    uint32_t VolSessionId, VolSessionTime;
228    int32_t Stream;
229    uint32_t FileIndex;
230    uint32_t data_len;
231    char *p = bs->msg;
232    int len;
233    char *fname, *attr;
234    ATTR_DBR ar;
235
236    if (!jcr->pool->catalog_files) {
237       return;
238    }
239    db_start_transaction(jcr->db);     /* start transaction if not already open */
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(109, "dird<stored: stream=%d %s\n", Stream, fname);
268       Dmsg1(109, "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(111, "dird<filed: stream=%d %s\n", Stream, fname);
277       Dmsg1(120, "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(190, "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 }