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