]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/catreq.c
- Implement IBM labels
[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-2005 Kern Sibbald
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 pool_name=%127s media_type=%127s\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=%u VolFiles=%u VolBlocks=%u VolBytes=%" lld " VolMounts=%u"
49    " VolErrors=%u VolWrites=%u MaxVolBytes=%" lld " EndTime=%d VolStatus=%10s"
50    " Slot=%d relabel=%d InChanger=%d VolReadTime=%" lld " VolWriteTime=%" lld
51    " VolParts=%u\n";
52
53 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
54    " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
55    " StartBlock=%u EndBlock=%u\n";
56
57
58 /* Responses  sent to Storage daemon */
59 static char OK_media[] = "1000 OK VolName=%s VolJobs=%u VolFiles=%u"
60    " VolBlocks=%u VolBytes=%s VolMounts=%u VolErrors=%u VolWrites=%u"
61    " MaxVolBytes=%s VolCapacityBytes=%s VolStatus=%s Slot=%d"
62    " MaxVolJobs=%u MaxVolFiles=%u InChanger=%d VolReadTime=%s"
63    " VolWriteTime=%s EndFile=%u EndBlock=%u VolParts=%u LabelType=%d\n";
64
65 static char OK_create[] = "1000 OK CreateJobMedia\n";
66
67
68 static int send_volume_info_to_storage_daemon(JCR *jcr, BSOCK *sd, MEDIA_DBR *mr)
69 {
70    int stat;
71    char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50];
72
73    jcr->MediaId = mr->MediaId;
74    pm_strcpy(jcr->VolumeName, mr->VolumeName);
75    bash_spaces(mr->VolumeName);
76    stat = bnet_fsend(sd, OK_media, mr->VolumeName, mr->VolJobs,
77       mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
78       mr->VolMounts, mr->VolErrors, mr->VolWrites,
79       edit_uint64(mr->MaxVolBytes, ed2),
80       edit_uint64(mr->VolCapacityBytes, ed3),
81       mr->VolStatus, mr->Slot, mr->MaxVolJobs, mr->MaxVolFiles,
82       mr->InChanger,
83       edit_uint64(mr->VolReadTime, ed4),
84       edit_uint64(mr->VolWriteTime, ed5),
85       mr->EndFile, mr->EndBlock,
86       mr->VolParts,
87       mr->LabelType);
88    unbash_spaces(mr->VolumeName);
89    Dmsg2(100, "Vol Info for %s: %s", jcr->Job, sd->msg);
90    return stat;
91 }
92
93 void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
94 {
95    MEDIA_DBR mr, sdmr;
96    JOBMEDIA_DBR jm;
97    char Job[MAX_NAME_LENGTH];
98    char pool_name[MAX_NAME_LENGTH];
99    int index, ok, label, writing;
100    POOLMEM *omsg;
101    POOL_DBR pr;
102
103    memset(&mr, 0, sizeof(mr));
104    memset(&sdmr, 0, sizeof(sdmr));
105    memset(&jm, 0, sizeof(jm));
106
107    /*
108     * Request to find next appendable Volume for this Job
109     */
110    Dmsg1(100, "catreq %s", bs->msg);
111    if (!jcr->db) {
112       omsg = get_memory(bs->msglen+1);
113       pm_strcpy(omsg, bs->msg);
114       bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);    
115       Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request; DB not open: %s"), omsg);
116       free_memory(omsg);
117       return;
118    }
119    /*
120     * Find next appendable medium for SD
121     */
122    if (sscanf(bs->msg, Find_media, &Job, &index, &pool_name, &mr.MediaType) == 4) {
123       memset(&pr, 0, sizeof(pr));
124       bstrncpy(pr.Name, pool_name, sizeof(pr.Name));
125       ok = db_get_pool_record(jcr, jcr->db, &pr);
126       if (ok) {
127          mr.PoolId = pr.PoolId;
128          ok = find_next_volume_for_append(jcr, &mr, true /*permit create new vol*/);
129       }
130       /*
131        * Send Find Media response to Storage daemon
132        */
133       if (ok) {
134          send_volume_info_to_storage_daemon(jcr, bs, &mr);
135       } else {
136          bnet_fsend(bs, "1901 No Media.\n");
137       }
138
139    /*
140     * Request to find specific Volume information
141     */
142    } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
143       Dmsg1(400, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
144       /*
145        * Find the Volume
146        */
147       unbash_spaces(mr.VolumeName);
148       if (db_get_media_record(jcr, jcr->db, &mr)) {
149          const char *reason = NULL;           /* detailed reason for rejection */
150          /*
151           * If we are reading, accept any volume (reason == NULL)
152           * If we are writing, check if the Volume is valid
153           *   for this job, and do a recycle if necessary
154           */
155          if (writing) {
156             /*
157              * SD wants to write this Volume, so make
158              *   sure it is suitable for this job, i.e.
159              *   Pool matches, and it is either Append or Recycle
160              *   and Media Type matches and Pool allows any volume.
161              */
162             if (mr.PoolId != jcr->PoolId) {
163                reason = "not in Pool";
164             } else if (strcmp(mr.MediaType, jcr->store->media_type) != 0) {
165                reason = "not correct MediaType";
166             } else {
167               /*
168                * ****FIXME***
169                *   This test (accept_any_volume) is turned off
170                *   because it doesn't properly check if the volume
171                *   really is out of sequence!
172                *
173                * } else if (!jcr->pool->accept_any_volume) {
174                *    reason = "Volume not in sequence";
175                */
176
177                /*
178                 * Now try recycling if necessary
179                 *   reason set non-NULL if we cannot use it
180                 */
181                check_if_volume_valid_or_recyclable(jcr, &mr, &reason);
182             }
183          }
184          if (reason == NULL) {
185             /*
186              * Send Find Media response to Storage daemon
187              */
188             send_volume_info_to_storage_daemon(jcr, bs, &mr);
189          } else {
190             /* Not suitable volume */
191             bnet_fsend(bs, "1998 Volume \"%s\" status is %s, %s.\n", mr.VolumeName,
192                mr.VolStatus, reason);
193          }
194
195       } else {
196          bnet_fsend(bs, "1997 Volume \"%s\" not in catalog.\n", mr.VolumeName);
197       }
198
199    /*
200     * Request to update Media record. Comes typically at the end
201     *  of a Storage daemon Job Session, when labeling/relabeling a
202     *  Volume, or when an EOF mark is written.
203     */
204    } else if (sscanf(bs->msg, Update_media, &Job, &sdmr.VolumeName,
205       &sdmr.VolJobs, &sdmr.VolFiles, &sdmr.VolBlocks, &sdmr.VolBytes,
206       &sdmr.VolMounts, &sdmr.VolErrors, &sdmr.VolWrites, &sdmr.MaxVolBytes,
207       &sdmr.LastWritten, &sdmr.VolStatus, &sdmr.Slot, &label, &sdmr.InChanger,
208       &sdmr.VolReadTime, &sdmr.VolWriteTime, &sdmr.VolParts) == 18) {
209
210       db_lock(jcr->db);
211       Dmsg3(400, "Update media %s oldStat=%s newStat=%s\n", sdmr.VolumeName,
212          mr.VolStatus, sdmr.VolStatus);
213       bstrncpy(mr.VolumeName, sdmr.VolumeName, sizeof(mr.VolumeName)); /* copy Volume name */
214       unbash_spaces(mr.VolumeName);
215       if (!db_get_media_record(jcr, jcr->db, &mr)) {
216          Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
217               mr.VolumeName, db_strerror(jcr->db));
218          bnet_fsend(bs, "1991 Catalog Request for vol=%s failed: %s", 
219             mr.VolumeName, db_strerror(jcr->db));
220          db_unlock(jcr->db);
221          return;
222       }
223       /* Set first written time if this is first job */
224       if (mr.FirstWritten == 0) {
225          mr.FirstWritten = jcr->start_time;   /* use Job start time as first write */
226          mr.set_first_written = true;
227       }
228       /* If we just labeled the tape set time */
229       if (label || mr.LabelDate == 0) {
230          mr.LabelDate = jcr->start_time;
231          mr.set_label_date = true;
232          Dmsg2(400, "label=%d labeldate=%d\n", label, mr.LabelDate);
233       } else {
234          /*
235           * Insanity check for VolFiles get set to a smaller value
236           */
237          if (sdmr.VolFiles < mr.VolFiles) {
238             Jmsg(jcr, M_ERROR, 0, _("ERROR!! Volume Files at %u being set to %u. This is probably wrong.\n"),
239                mr.VolFiles, sdmr.VolFiles);
240          }
241       }
242       Dmsg2(400, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
243       /* Copy updated values to original media record */
244       mr.VolJobs      = sdmr.VolJobs;
245       mr.VolFiles     = sdmr.VolFiles;
246       mr.VolBlocks    = sdmr.VolBlocks;
247       mr.VolBytes     = sdmr.VolBytes;
248       mr.VolMounts    = sdmr.VolMounts;
249       mr.VolErrors    = sdmr.VolErrors;
250       mr.VolWrites    = sdmr.VolWrites;
251       mr.LastWritten  = sdmr.LastWritten;
252       mr.Slot         = sdmr.Slot;
253       mr.InChanger    = sdmr.InChanger;
254       mr.VolReadTime  = sdmr.VolReadTime;
255       mr.VolWriteTime = sdmr.VolWriteTime;
256       mr.VolParts     = sdmr.VolParts;
257       bstrncpy(mr.VolStatus, sdmr.VolStatus, sizeof(mr.VolStatus));
258
259       Dmsg2(400, "db_update_media_record. Stat=%s Vol=%s\n", mr.VolStatus, mr.VolumeName);
260       /*
261        * Check if it has expired, and if not update the DB. Note, if
262        *   Volume has expired, has_volume_expired() will update the DB.
263        */
264       if (has_volume_expired(jcr, &mr) || db_update_media_record(jcr, jcr->db, &mr)) {
265          send_volume_info_to_storage_daemon(jcr, bs, &mr);
266       } else {
267          Jmsg(jcr, M_FATAL, 0, _("Catalog error updating Media record. %s"),
268             db_strerror(jcr->db));
269          bnet_fsend(bs, "1992 Update Media error\n");
270          Dmsg0(400, "send error\n");
271       }
272       db_unlock(jcr->db);
273
274    /*
275     * Request to create a JobMedia record
276     */
277    } else if (sscanf(bs->msg, Create_job_media, &Job,
278       &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
279       &jm.StartBlock, &jm.EndBlock) == 7) {
280
281       jm.JobId = jcr->JobId;
282       jm.MediaId = jcr->MediaId;
283       Dmsg6(400, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
284          jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
285       if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
286          Jmsg(jcr, M_FATAL, 0, _("Catalog error creating JobMedia record. %s"),
287             db_strerror(jcr->db));
288          bnet_fsend(bs, "1991 Update JobMedia error\n");
289       } else {
290          Dmsg0(400, "JobMedia record created\n");
291          bnet_fsend(bs, OK_create);
292       }
293
294    } else {
295       omsg = get_memory(bs->msglen+1);
296       pm_strcpy(omsg, bs->msg);
297       bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
298       Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request: %s"), omsg);
299       free_memory(omsg);
300    }
301    Dmsg1(400, ">CatReq response: %s", bs->msg);
302    Dmsg1(400, "Leave catreq jcr 0x%x\n", jcr);
303    return;
304 }
305
306 /*
307  * Update File Attributes in the catalog with data
308  *  sent by the Storage daemon.  Note, we receive the whole
309  *  attribute record, but we select out only the stat packet,
310  *  VolSessionId, VolSessionTime, FileIndex, and file name
311  *  to store in the catalog.
312  */
313 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
314 {
315    unser_declare;
316    uint32_t VolSessionId, VolSessionTime;
317    int32_t Stream;
318    uint32_t FileIndex;
319    uint32_t data_len;
320    char *p = bs->msg;
321    int len;
322    char *fname, *attr;
323    ATTR_DBR ar;
324
325    if (!jcr->pool->catalog_files) {
326       return;
327    }
328    db_start_transaction(jcr, jcr->db);     /* start transaction if not already open */
329    skip_nonspaces(&p);                /* UpdCat */
330    skip_spaces(&p);
331    skip_nonspaces(&p);                /* Job=nnn */
332    skip_spaces(&p);
333    skip_nonspaces(&p);                /* FileAttributes */
334    p += 1;
335    unser_begin(p, 0);
336    unser_uint32(VolSessionId);
337    unser_uint32(VolSessionTime);
338    unser_int32(FileIndex);
339    unser_int32(Stream);
340    unser_uint32(data_len);
341    p += unser_length(p);
342
343    Dmsg1(400, "UpdCat msg=%s\n", bs->msg);
344    Dmsg5(400, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
345       VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
346
347    if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_UNIX_ATTRIBUTES_EX) {
348       skip_nonspaces(&p);             /* skip FileIndex */
349       skip_spaces(&p);
350       skip_nonspaces(&p);             /* skip FileType */
351       skip_spaces(&p);
352       fname = p;
353       len = strlen(fname);        /* length before attributes */
354       attr = &fname[len+1];
355
356       Dmsg2(400, "dird<stored: stream=%d %s\n", Stream, fname);
357       Dmsg1(400, "dird<stored: attr=%s\n", attr);
358       ar.attr = attr;
359       ar.fname = fname;
360       ar.FileIndex = FileIndex;
361       ar.Stream = Stream;
362       ar.link = NULL;
363       ar.JobId = jcr->JobId;
364
365       Dmsg2(400, "dird<filed: stream=%d %s\n", Stream, fname);
366       Dmsg1(400, "dird<filed: attr=%s\n", attr);
367
368       if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
369          Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
370       }
371       /* Save values for SIG update */
372       jcr->FileId = ar.FileId;
373       jcr->FileIndex = FileIndex;
374    } else if (Stream == STREAM_MD5_SIGNATURE || Stream == STREAM_SHA1_SIGNATURE) {
375       fname = p;
376       if (jcr->FileIndex != FileIndex) {
377          Jmsg(jcr, M_WARNING, 0, "Got MD5/SHA1 but not same File as attributes\n");
378       } else {
379          /* Update signature in catalog */
380          char SIGbuf[50];           /* 24 bytes should be enough */
381          int len, type;
382          if (Stream == STREAM_MD5_SIGNATURE) {
383             len = 16;
384             type = MD5_SIG;
385          } else {
386             len = 20;
387             type = SHA1_SIG;
388          }
389          bin_to_base64(SIGbuf, fname, len);
390          Dmsg3(400, "SIGlen=%d SIG=%s type=%d\n", strlen(SIGbuf), SIGbuf, Stream);
391          if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIGbuf, type)) {
392             Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5/SHA1. %s"),
393                db_strerror(jcr->db));
394          }
395       }
396    }
397 }