]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/catreq.c
Autochanger for read + 64 bit addrs + Session key, see kes14Sep02
[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              */
142             if (mr.PoolId == jcr->PoolId && 
143                 (strcmp(mr.VolStatus, "Append") == 0 ||
144                  strcmp(mr.VolStatus, "Recycle") == 0 ||
145                  strcmp(mr.MediaType, jcr->store->media_type) == 0)) {
146                VolSuitable = 1;
147             }
148          }
149          if (VolSuitable) {
150             /*
151              * Send Find Media response to Storage daemon 
152              */
153             bash_spaces(mr.VolumeName);
154             bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
155                mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
156                mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
157                mr.VolStatus, mr.Slot);
158             Dmsg5(200, "get_media_record PoolId=%d wanted %d, Status=%s, Slot=%d \
159 MediaType=%s\n", mr.PoolId, jcr->PoolId, mr.VolStatus, mr.Slot, mr.MediaType);
160          } else { 
161             /* Not suitable volume */
162             bnet_fsend(bs, "1998 Volume not appropriate.\n");
163          }
164
165       } else {
166          bnet_fsend(bs, "1999 Volume Not Found.\n");
167       }
168
169    
170    /*
171     * Request to update Media record. Comes typically at the end
172     *  of a Storage daemon Job Session
173     */
174    } else if (sscanf(bs->msg, Update_media, &Job, &mr.VolumeName, &mr.VolJobs,
175       &mr.VolFiles, &mr.VolBlocks, &mr.VolBytes, &mr.VolMounts, &mr.VolErrors,
176       &mr.VolWrites, &mr.VolMaxBytes, &mr.LastWritten, &mr.VolStatus, 
177       &mr.Slot, &relabel) == 14) {
178       /*     
179        * Update Media Record
180        */
181       if ((mr.VolMaxBytes > 0 && mr.VolBytes >= mr.VolMaxBytes) ||
182           (mr.VolBytes > 0 && jcr->pool->use_volume_once)) {
183          strcpy(mr.VolStatus, "Full");
184       }
185
186       Dmsg2(100, "db_update_media_record. Stat=%s Vol=%s\n",
187          mr.VolStatus, mr.VolumeName);
188       if (db_update_media_record(jcr->db, &mr)) {
189          bnet_fsend(bs, OK_update);
190          Dmsg0(190, "send OK\n");
191       } else {
192          Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
193             db_strerror(jcr->db));
194          bnet_fsend(bs, "1992 Update Media error\n");
195          Dmsg0(190, "send error\n");
196       }
197
198    /*
199     * Request to create a JobMedia record
200     */
201    } else if (sscanf(bs->msg, Create_job_media, &Job,
202       &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
203       &jm.StartBlock, &jm.EndBlock) == 7) {
204
205       jm.JobId = jcr->JobId;
206       jm.MediaId = jcr->MediaId;
207       Dmsg6(100, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
208          jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
209       if(!db_create_jobmedia_record(jcr->db, &jm)) {
210          Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
211             db_strerror(jcr->db));
212          bnet_fsend(bs, "1991 Update JobMedia error\n");
213       } else {
214          Dmsg0(100, "JobMedia record created\n");
215          bnet_fsend(bs, OK_update);
216       }
217
218    } else {
219       omsg = (char *) get_memory(bs->msglen+1);
220       strcpy(omsg, bs->msg);
221       bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);    
222       free_memory(omsg);
223    }
224    Dmsg1(120, ">CatReq response: %s", bs->msg);
225    return;
226 }
227
228 /*
229  * Update File Attributes in the catalog with data
230  *  sent by the Storage daemon.
231  */
232 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
233 {
234    unser_declare;
235    uint32_t VolSessionId, VolSessionTime;
236    int32_t Stream;
237    uint32_t FileIndex;
238    uint32_t data_len;
239    char *p = bs->msg;
240    int len;
241    char *fname, *attr;
242    ATTR_DBR ar;
243
244    if (!jcr->pool->catalog_files) {
245       return;
246    }
247    db_start_transaction(jcr->db);     /* start transaction if not already open */
248    skip_nonspaces(&p);                /* UpdCat */
249    skip_spaces(&p);
250    skip_nonspaces(&p);                /* Job=nnn */
251    skip_spaces(&p);
252    skip_nonspaces(&p);                /* FileAttributes */
253    p += 1;
254    unser_begin(p, 0);
255    unser_uint32(VolSessionId);
256    unser_uint32(VolSessionTime);
257    unser_int32(FileIndex);
258    unser_int32(Stream);
259    unser_uint32(data_len);
260    p += unser_length(p);
261
262    Dmsg1(99, "UpdCat msg=%s\n", bs->msg);
263    Dmsg5(99, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
264       VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
265
266    if (Stream == STREAM_UNIX_ATTRIBUTES) {
267       skip_nonspaces(&p);             /* skip FileIndex */
268       skip_spaces(&p);
269       skip_nonspaces(&p);             /* skip FileType */
270       skip_spaces(&p);
271       fname = p;
272       len = strlen(fname);        /* length before attributes */
273       attr = &fname[len+1];
274
275       Dmsg2(109, "dird<stored: stream=%d %s\n", Stream, fname);
276       Dmsg1(109, "dird<stored: attr=%s\n", attr);
277       ar.attr = attr; 
278       ar.fname = fname;
279       ar.FileIndex = FileIndex;
280       ar.Stream = Stream;
281       ar.link = NULL;
282       ar.JobId = jcr->JobId;
283
284       Dmsg2(111, "dird<filed: stream=%d %s\n", Stream, fname);
285       Dmsg1(120, "dird<filed: attr=%s\n", attr);
286
287       /* ***FIXME*** fix link field */
288       if (!db_create_file_attributes_record(jcr->db, &ar)) {
289          Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
290       }
291       /* Save values for MD5 update */
292       jcr->FileId = ar.FileId;
293       jcr->FileIndex = FileIndex;
294    } else if (Stream == STREAM_MD5_SIGNATURE) {
295       fname = p;
296       if (jcr->FileIndex != FileIndex) {    
297          Jmsg(jcr, M_WARNING, 0, "Got MD5 but not same block as attributes\n");
298       } else {
299          /* Update MD5 signature in catalog */
300          char MD5buf[50];           /* 24 bytes should be enough */
301          bin_to_base64(MD5buf, fname, 16);
302          Dmsg2(190, "MD5len=%d MD5=%s\n", strlen(MD5buf), MD5buf);
303          if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, MD5buf)) {
304             Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5. %s"), 
305                db_strerror(jcr->db));
306          }
307       }
308    }
309 }