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