]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/label.c
Integrate Nicolas' patch for direct DVD support.
[bacula/bacula] / bacula / src / stored / label.c
1 /*
2  *
3  *  label.c  Bacula routines to handle labels
4  *
5  *   Kern Sibbald, MM
6  *
7  *
8  *   Version $Id$
9  */
10 /*
11    Copyright (C) 2000-2005 Kern Sibbald
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"                   /* pull in global headers */
31 #include "stored.h"                   /* pull in Storage Deamon headers */
32
33 /* Forward referenced functions */
34 static void create_volume_label_record(DCR *dcr, DEV_RECORD *rec);
35
36 extern char my_name[];
37 extern int debug_level;
38
39 /*
40  * Read the volume label
41  *
42  *  If dcr->VolumeName == NULL, we accept any Bacula Volume
43  *  If dcr->VolumeName[0] == 0, we accept any Bacula Volume
44  *  otherwise dcr->VolumeName must match the Volume.
45  *
46  *  If VolName given, ensure that it matches
47  *
48  *  Returns VOL_  code as defined in record.h
49  *    VOL_NOT_READ
50  *    VOL_OK
51  *    VOL_NO_LABEL
52  *    VOL_IO_ERROR
53  *    VOL_NAME_ERROR
54  *    VOL_CREATE_ERROR
55  *    VOL_VERSION_ERROR
56  *    VOL_LABEL_ERROR
57  *    VOL_NO_MEDIA
58  */
59 int read_dev_volume_label(DCR *dcr)
60 {
61    JCR *jcr = dcr->jcr;
62    DEVICE *dev = dcr->dev;
63    char *VolName = dcr->VolumeName;
64    DEV_RECORD *record;
65    bool ok = false;
66    DEV_BLOCK *block = dcr->block;
67
68    Dmsg3(100, "Enter read_volume_label device=%s vol=%s dev_Vol=%s\n",
69       dev_name(dev), VolName, dev->VolHdr.VolName);
70
71    if (dev_state(dev, ST_LABEL)) {       /* did we already read label? */
72       /* Compare Volume Names allow special wild card */
73       if (VolName && *VolName && *VolName != '*' && strcmp(dev->VolHdr.VolName, VolName) != 0) {
74          Mmsg(jcr->errmsg, _("Wrong Volume mounted on device %s: Wanted %s have %s\n"),
75             dev_name(dev), VolName, dev->VolHdr.VolName);
76          /*
77           * Cancel Job if too many label errors
78           *  => we are in a loop
79           */
80          if (!dev->poll && jcr->label_errors++ > 100) {
81             Jmsg(jcr, M_FATAL, 0, "Too many tries: %s", jcr->errmsg);
82          }
83          return VOL_NAME_ERROR;
84       }
85       Dmsg0(30, "Leave read_volume_label() VOL_OK\n");
86       return VOL_OK;       /* label already read */
87    }
88
89    dev->state &= ~(ST_LABEL|ST_APPEND|ST_READ);  /* set no label, no append */
90
91    if (!rewind_dev(dev)) {
92       Mmsg(jcr->errmsg, _("Couldn't rewind device %s ERR=%s\n"), dev_name(dev),
93          strerror_dev(dev));
94       Dmsg1(30, "%s", jcr->errmsg);
95       return VOL_NO_MEDIA;
96    }
97    bstrncpy(dev->VolHdr.Id, "**error**", sizeof(dev->VolHdr.Id));
98   
99    /* Read the Volume label block */
100    record = new_record();
101    empty_block(block);
102    Dmsg0(90, "Big if statement in read_volume_label\n");
103    if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
104       Mmsg(jcr->errmsg, _("Requested Volume \"%s\" on %s is not a Bacula "
105            "labeled Volume, because: ERR=%s"), NPRT(VolName), dev_name(dev),
106            strerror_dev(dev));
107       Dmsg1(30, "%s", jcr->errmsg);
108    } else if (!read_record_from_block(block, record)) {
109       Mmsg(jcr->errmsg, _("Could not read Volume label from block.\n"));
110       Dmsg1(30, "%s", jcr->errmsg);
111    } else if (!unser_volume_label(dev, record)) {
112       Mmsg(jcr->errmsg, _("Could not unserialize Volume label: ERR=%s\n"),
113          strerror_dev(dev));
114       Dmsg1(30, "%s", jcr->errmsg);
115    } else if (strcmp(dev->VolHdr.Id, BaculaId) != 0 &&
116               strcmp(dev->VolHdr.Id, OldBaculaId) != 0) {
117       Mmsg(jcr->errmsg, _("Volume Header Id bad: %s\n"), dev->VolHdr.Id);
118       Dmsg1(30, "%s", jcr->errmsg);
119    } else {
120       ok = true;
121    }
122    if (!ok) {
123       free_record(record);
124       if (forge_on || jcr->ignore_label_errors) {
125          dev->state |= ST_LABEL;      /* set has Bacula label */
126          Jmsg(jcr, M_ERROR, 0, "%s", jcr->errmsg);
127          return VOL_OK;
128       }
129       empty_block(block);
130       rewind_dev(dev);
131       return VOL_NO_LABEL;
132    }
133
134    free_record(record);
135    /* If we are a streaming device, we only get one chance to read */
136    if (!dev_cap(dev, CAP_STREAM)) {
137       empty_block(block);
138       rewind_dev(dev);
139    }
140
141    if (dev->VolHdr.VerNum != BaculaTapeVersion &&
142        dev->VolHdr.VerNum != OldCompatibleBaculaTapeVersion1 &&
143        dev->VolHdr.VerNum != OldCompatibleBaculaTapeVersion2) {
144       Mmsg(jcr->errmsg, _("Volume on %s has wrong Bacula version. Wanted %d got %d\n"),
145          dev_name(dev), BaculaTapeVersion, dev->VolHdr.VerNum);
146       Dmsg1(30, "%s", jcr->errmsg);
147       return VOL_VERSION_ERROR;
148    }
149
150    /* We are looking for either an unused Bacula tape (PRE_LABEL) or
151     * a Bacula volume label (VOL_LABEL)
152     */
153    if (dev->VolHdr.LabelType != PRE_LABEL && dev->VolHdr.LabelType != VOL_LABEL) {
154       Mmsg(jcr->errmsg, _("Volume on %s has bad Bacula label type: %x\n"),
155           dev_name(dev), dev->VolHdr.LabelType);
156       Dmsg1(30, "%s", jcr->errmsg);
157       return VOL_LABEL_ERROR;
158    }
159
160    dev->state |= ST_LABEL;            /* set has Bacula label */
161
162    /* Compare Volume Names */
163    Dmsg2(30, "Compare Vol names: VolName=%s hdr=%s\n", VolName?VolName:"*", dev->VolHdr.VolName);
164    if (VolName && *VolName && *VolName != '*' && strcmp(dev->VolHdr.VolName, VolName) != 0) {
165       Mmsg(jcr->errmsg, _("Wrong Volume mounted on device %s: Wanted %s have %s\n"),
166            dev_name(dev), VolName, dev->VolHdr.VolName);
167       Dmsg1(30, "%s", jcr->errmsg);
168       /*
169        * Cancel Job if too many label errors
170        *  => we are in a loop
171        */
172       if (!dev->poll && jcr->label_errors++ > 100) {
173          Jmsg(jcr, M_FATAL, 0, "Too many tries: %s", jcr->errmsg);
174       }
175       return VOL_NAME_ERROR;
176    }
177    Dmsg1(30, "Copy vol_name=%s\n", dev->VolHdr.VolName);
178
179    if (debug_level >= 10) {
180       dump_volume_label(dev);
181    }
182    Dmsg0(30, "Leave read_volume_label() VOL_OK\n");
183    return VOL_OK;
184 }
185
186 /* Read the volume label by guessing the volume name. (only for mounted devices)
187  * write is true if we are reading the label before writing to the device.
188  *
189  * If the volume name cannot be guessed :
190  *  Writing : returns the label of the current file (on the harddisk).
191  *  Reading : returns an error
192  */
193 int read_dev_volume_label_guess(DCR *dcr, bool write) {
194    int vol_label_status;
195    Dmsg3(100, "Enter read_dev_volume_label_guess device=%s vol=%s dev_Vol=%s\n",
196          dev_name(dcr->dev), dcr->VolumeName, dcr->dev->VolHdr.VolName);
197    
198    if (!dev_cap(dcr->dev, CAP_REQMOUNT)) {
199       Dmsg0(100, "Leave read_dev_volume_label_guess !CAP_REQMOUNT\n");
200       return read_dev_volume_label(dcr);
201    }
202    
203    if (!write && (dcr->VolCatInfo.VolCatParts == 0)) {
204       Dmsg0(100, "Leave read_dev_volume_label_guess !writing, and VolCatParts == 0\n");
205       return read_dev_volume_label(dcr);
206    }
207    
208    /* For mounted devices, tries to guess the volume name, and read the label if possible.
209    */
210    if (open_guess_name_dev(dcr->dev) < 0) {     
211       if ((!write) || (dcr->VolCatInfo.VolCatParts > 0)) {
212          Mmsg2(dcr->jcr->errmsg, _("Requested Volume \"%s\" on %s is not a Bacula labeled Volume."),
213                dev_name(dcr->dev), dcr->VolumeName);
214          Dmsg0(100, "Leave read_dev_volume_label_guess VOL_IO_ERROR (!open_guess_name_dev)\n");
215          return VOL_NO_LABEL;
216       }
217       
218       if (write && (dcr->dev->free_space_errno < 0)) {
219          Dmsg0(100, "Leave read_dev_volume_label_guess !free_space VOL_NO_MEDIA\n");
220          Mmsg2(dcr->jcr->errmsg, _("free_space error on %s. The current medium is probably not writable. ERR=%s.\n"),
221                dcr->dev->dev_name, dcr->dev->errmsg);
222          return VOL_NO_MEDIA;
223       }
224       
225       /* If we can't guess the name, and we are writing, just reopen the right file with open_first_part. */
226       if (open_first_part(dcr->dev) < 0) {
227          berrno be;
228          Mmsg2(dcr->jcr->errmsg, _("open_first_part error on %s. ERR=%s.\n"),
229                dcr->dev->dev_name, be.strerror());
230          Dmsg0(100, "Leave read_dev_volume_label_guess VOL_IO_ERROR (!open_guess_name_dev && !open_first_part)\n");
231          return VOL_IO_ERROR;
232       }
233       
234       Dmsg0(100, "Leave read_dev_volume_label_guess !open_guess_name_dev\n");
235       return read_dev_volume_label(dcr);
236    }
237    else {
238       if (write && (dcr->dev->free_space_errno < 0)) {
239          Dmsg0(100, "Leave read_dev_volume_label_guess !free_space VOL_NO_MEDIA\n");
240          Mmsg2(dcr->jcr->errmsg, _("free_space error on %s. The current medium is probably not writable. ERR=%s.\n"),
241                dcr->dev->dev_name, dcr->dev->errmsg);
242          return VOL_NO_MEDIA;
243       }
244       
245       vol_label_status = read_dev_volume_label(dcr);
246
247       if ((!write) || (dcr->VolCatInfo.VolCatParts > 0)) {
248          Dmsg0(100, "Leave read_dev_volume_label_guess (open_guess_name_dev && (!write || dcr->VolCatInfo.VolCatParts > 0))\n");
249          return vol_label_status;
250       }
251       
252       if (open_first_part(dcr->dev) < 0) {
253          berrno be;
254          Mmsg2(dcr->jcr->errmsg, _("open_first_part error on %s. ERR=%s.\n"),
255                dcr->dev->dev_name, be.strerror());
256          Dmsg0(100, "Leave read_dev_volume_label_guess VOL_IO_ERROR (open_guess_name_dev && !open_first_part)\n");
257          return VOL_IO_ERROR;
258       }
259       
260       /* When writing, if the guessed volume name is no the right volume name, 
261        * report the error, otherwise, just continue with the right file.
262        */
263       if (vol_label_status != VOL_NAME_ERROR) {
264          Dmsg0(100, "Leave read_dev_volume_label_guess (open_guess_name_dev && !VOL_NAME_ERROR)\n");
265          dcr->dev->state &= ~ST_LABEL;
266          return read_dev_volume_label(dcr);
267       }
268       else {
269          Dmsg0(100, "Leave read_dev_volume_label_guess (open_guess_name_dev && VOL_NAME_ERROR)\n");
270          return vol_label_status;
271       }
272    }
273 }
274
275 /*  unser_volume_label
276  *
277  * Unserialize the Volume label into the device Volume_Label
278  * structure.
279  *
280  * Assumes that the record is already read.
281  *
282  * Returns: false on error
283  *          true  on success
284 */
285
286 bool unser_volume_label(DEVICE *dev, DEV_RECORD *rec)
287 {
288    ser_declare;
289
290    if (rec->FileIndex != VOL_LABEL && rec->FileIndex != PRE_LABEL) {
291       Mmsg3(dev->errmsg, _("Expecting Volume Label, got FI=%s Stream=%s len=%d\n"),
292               FI_to_ascii(rec->FileIndex),
293               stream_to_ascii(rec->Stream, rec->FileIndex),
294               rec->data_len);
295       if (!forge_on) {
296          return false;
297       }
298    }
299
300    dev->VolHdr.LabelType = rec->FileIndex;
301    dev->VolHdr.LabelSize = rec->data_len;
302
303
304    /* Unserialize the record into the Volume Header */
305    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Volume_Label);
306    ser_begin(rec->data, SER_LENGTH_Volume_Label);
307    unser_string(dev->VolHdr.Id);
308    unser_uint32(dev->VolHdr.VerNum);
309
310    if (dev->VolHdr.VerNum >= 11) {
311       unser_btime(dev->VolHdr.label_btime);
312       unser_btime(dev->VolHdr.write_btime);
313    } else { /* old way */
314       unser_float64(dev->VolHdr.label_date);
315       unser_float64(dev->VolHdr.label_time);
316    }
317    unser_float64(dev->VolHdr.write_date);    /* Unused with VerNum >= 11 */
318    unser_float64(dev->VolHdr.write_time);    /* Unused with VerNum >= 11 */
319
320    unser_string(dev->VolHdr.VolName);
321    unser_string(dev->VolHdr.PrevVolName);
322    unser_string(dev->VolHdr.PoolName);
323    unser_string(dev->VolHdr.PoolType);
324    unser_string(dev->VolHdr.MediaType);
325
326    unser_string(dev->VolHdr.HostName);
327    unser_string(dev->VolHdr.LabelProg);
328    unser_string(dev->VolHdr.ProgVersion);
329    unser_string(dev->VolHdr.ProgDate);
330
331    ser_end(rec->data, SER_LENGTH_Volume_Label);
332    Dmsg0(90, "ser_read_vol\n");
333    if (debug_level >= 90) {
334       dump_volume_label(dev);
335    }
336    return true;
337 }
338
339 /*
340  * Put a volume label into the block
341  *
342  *  Returns: false on failure
343  *           true  on success
344  */
345 bool write_volume_label_to_block(DCR *dcr)
346 {
347    DEV_RECORD rec;
348    DEVICE *dev = dcr->dev;
349    JCR *jcr = dcr->jcr;
350    DEV_BLOCK *block = dcr->block;
351
352    Dmsg0(20, "write Label in write_volume_label_to_block()\n");
353    memset(&rec, 0, sizeof(rec));
354    rec.data = get_memory(SER_LENGTH_Volume_Label);
355    empty_block(block);                /* Volume label always at beginning */
356
357    create_volume_label_record(dcr, &rec);
358
359    block->BlockNumber = 0;
360    if (!write_record_to_block(block, &rec)) {
361       free_pool_memory(rec.data);
362       Jmsg1(jcr, M_FATAL, 0, _("Cannot write Volume label to block for device %s\n"),
363          dev_name(dev));
364       return false;
365    } else {
366       Dmsg1(90, "Wrote label of %d bytes to block\n", rec.data_len);
367    }
368    free_pool_memory(rec.data);
369    return true;
370 }
371
372 /*
373  *  create_volume_label_record
374  *   Serialize label (from dev->VolHdr structure) into device record.
375  *   Assumes that the dev->VolHdr structure is properly
376  *   initialized.
377 */
378 static void create_volume_label_record(DCR *dcr, DEV_RECORD *rec)
379 {
380    ser_declare;
381    struct date_time dt;
382    DEVICE *dev = dcr->dev;
383    JCR *jcr = dcr->jcr;
384
385    /* Serialize the label into the device record. */
386
387    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Volume_Label);
388    ser_begin(rec->data, SER_LENGTH_Volume_Label);
389    ser_string(dev->VolHdr.Id);
390
391    ser_uint32(dev->VolHdr.VerNum);
392
393    if (dev->VolHdr.VerNum >= 11) {
394       ser_btime(dev->VolHdr.label_btime);
395       dev->VolHdr.write_btime = get_current_btime();
396       ser_btime(dev->VolHdr.write_btime);
397       dev->VolHdr.write_date = 0;
398       dev->VolHdr.write_time = 0;
399    } else {
400       /* OLD WAY DEPRECATED */
401       ser_float64(dev->VolHdr.label_date);
402       ser_float64(dev->VolHdr.label_time);
403       get_current_time(&dt);
404       dev->VolHdr.write_date = dt.julian_day_number;
405       dev->VolHdr.write_time = dt.julian_day_fraction;
406    }
407    ser_float64(dev->VolHdr.write_date);   /* 0 if VerNum >= 11 */
408    ser_float64(dev->VolHdr.write_time);   /* 0  if VerNum >= 11 */
409
410    ser_string(dev->VolHdr.VolName);
411    ser_string(dev->VolHdr.PrevVolName);
412    ser_string(dev->VolHdr.PoolName);
413    ser_string(dev->VolHdr.PoolType);
414    ser_string(dev->VolHdr.MediaType);
415
416    ser_string(dev->VolHdr.HostName);
417    ser_string(dev->VolHdr.LabelProg);
418    ser_string(dev->VolHdr.ProgVersion);
419    ser_string(dev->VolHdr.ProgDate);
420
421    ser_end(rec->data, SER_LENGTH_Volume_Label);
422    rec->data_len = ser_length(rec->data);
423    rec->FileIndex = dev->VolHdr.LabelType;
424    rec->VolSessionId = jcr->VolSessionId;
425    rec->VolSessionTime = jcr->VolSessionTime;
426    rec->Stream = jcr->NumVolumes;
427    Dmsg2(100, "Created Vol label rec: FI=%s len=%d\n", FI_to_ascii(rec->FileIndex),
428       rec->data_len);
429 }
430
431
432 /*
433  * Create a volume label in memory
434  */
435 void create_volume_label(DEVICE *dev, const char *VolName, const char *PoolName)
436 {
437    DEVRES *device = (DEVRES *)dev->device;
438
439    Dmsg0(90, "Start create_volume_label()\n");
440
441    ASSERT(dev != NULL);
442
443    memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
444
445    bstrncpy(dev->VolHdr.Id, BaculaId, sizeof(dev->VolHdr.Id));
446    dev->VolHdr.VerNum = BaculaTapeVersion;
447    dev->VolHdr.LabelType = PRE_LABEL;  /* Mark tape as unused */
448    bstrncpy(dev->VolHdr.VolName, VolName, sizeof(dev->VolHdr.VolName));
449    bstrncpy(dev->VolHdr.PoolName, PoolName, sizeof(dev->VolHdr.PoolName));
450    bstrncpy(dev->VolHdr.MediaType, device->media_type, sizeof(dev->VolHdr.MediaType));
451
452    bstrncpy(dev->VolHdr.PoolType, "Backup", sizeof(dev->VolHdr.PoolType));
453
454    dev->VolHdr.label_btime = get_current_btime();
455    dev->VolHdr.label_date = 0;
456    dev->VolHdr.label_time = 0;
457
458    if (gethostname(dev->VolHdr.HostName, sizeof(dev->VolHdr.HostName)) != 0) {
459       dev->VolHdr.HostName[0] = 0;
460    }
461    bstrncpy(dev->VolHdr.LabelProg, my_name, sizeof(dev->VolHdr.LabelProg));
462    sprintf(dev->VolHdr.ProgVersion, "Ver. %s %s", VERSION, BDATE);
463    sprintf(dev->VolHdr.ProgDate, "Build %s %s", __DATE__, __TIME__);
464    dev->state |= ST_LABEL;            /* set has Bacula label */
465    if (debug_level >= 90) {
466       dump_volume_label(dev);
467    }
468 }
469
470 /*
471  * Write a Volume Label
472  *  !!! Note, this is ONLY used for writing
473  *            a fresh volume label.  Any data
474  *            after the label will be destroyed,
475  *            in fact, we write the label 5 times !!!!
476  *
477  *  This routine expects that open_device() was previously called.
478  *
479  *  This routine should be used only when labeling a blank tape.
480  */
481 bool write_new_volume_label_to_dev(DCR *dcr, const char *VolName, const char *PoolName)
482 {
483    bool ok = false;
484    DEVICE *dev = dcr->dev;
485
486
487    Dmsg0(99, "write_volume_label()\n");
488    empty_block(dcr->block);
489    /* Create PRE_LABEL */
490    create_volume_label(dev, VolName, PoolName);
491
492    if (!rewind_dev(dev)) {
493       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
494       Dmsg2(30, "Bad status on %s from rewind. ERR=%s\n", dev_name(dev), strerror_dev(dev));
495       if (!forge_on) {
496          return false;
497       }
498    }
499
500    create_volume_label_record(dcr, dcr->rec);
501    dcr->rec->Stream = 0;
502
503    /* Temporarily mark in append state to enable writing */
504    dev->state |= ST_APPEND;
505    if (!write_record_to_block(dcr->block, dcr->rec)) {
506       Dmsg2(30, "Bad Label write on %s. ERR=%s\n", dev_name(dev), strerror_dev(dev));
507       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
508       goto bail_out;
509    } else {
510       Dmsg2(30, "Wrote label of %d bytes to %s\n", dcr->rec->data_len, dev_name(dev));
511    }
512
513    Dmsg0(99, "Call write_block_to_dev()\n");
514    if (!write_block_to_dev(dcr)) {
515       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
516       Dmsg2(30, "Bad Label write on %s. ERR=%s\n", dev_name(dev), strerror_dev(dev));
517       goto bail_out;
518    }
519    Dmsg0(99, " Wrote block to device\n");
520
521    if (weof_dev(dev, 1) == 0) {
522       dev->state |= ST_LABEL;
523       ok = true;
524    }
525
526    if (debug_level >= 20)  {
527       dump_volume_label(dev);
528    }
529
530 bail_out:
531    dev->state &= ~ST_APPEND;          /* remove append since this is PRE_LABEL */
532    return ok;
533 }
534
535
536 /*
537  * Create session label
538  *  The pool memory must be released by the calling program
539  */
540 void create_session_label(DCR *dcr, DEV_RECORD *rec, int label)
541 {
542    JCR *jcr = dcr->jcr;
543    ser_declare;
544
545    rec->VolSessionId   = jcr->VolSessionId;
546    rec->VolSessionTime = jcr->VolSessionTime;
547    rec->Stream         = jcr->JobId;
548
549    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Session_Label);
550    ser_begin(rec->data, SER_LENGTH_Session_Label);
551    ser_string(BaculaId);
552    ser_uint32(BaculaTapeVersion);
553
554    ser_uint32(jcr->JobId);
555
556    /* Changed in VerNum 11 */
557    ser_btime(get_current_btime());
558    ser_float64(0);
559
560    ser_string(dcr->pool_name);
561    ser_string(dcr->pool_type);
562    ser_string(jcr->job_name);         /* base Job name */
563    ser_string(jcr->client_name);
564
565    /* Added in VerNum 10 */
566    ser_string(jcr->Job);              /* Unique name of this Job */
567    ser_string(jcr->fileset_name);
568    ser_uint32(jcr->JobType);
569    ser_uint32(jcr->JobLevel);
570    /* Added in VerNum 11 */
571    ser_string(jcr->fileset_md5);
572
573    if (label == EOS_LABEL) {
574       ser_uint32(jcr->JobFiles);
575       ser_uint64(jcr->JobBytes);
576       ser_uint32(dcr->StartBlock);
577       ser_uint32(dcr->EndBlock);
578       ser_uint32(dcr->StartFile);
579       ser_uint32(dcr->EndFile);
580       ser_uint32(jcr->JobErrors);
581
582       /* Added in VerNum 11 */
583       ser_uint32(jcr->JobStatus);
584    }
585    ser_end(rec->data, SER_LENGTH_Session_Label);
586    rec->data_len = ser_length(rec->data);
587 }
588
589 /* Write session label
590  *  Returns: false on failure
591  *           true  on success
592  */
593 bool write_session_label(DCR *dcr, int label)
594 {
595    JCR *jcr = dcr->jcr;
596    DEVICE *dev = dcr->dev;
597    DEV_RECORD *rec;
598    DEV_BLOCK *block = dcr->block;
599
600    rec = new_record();
601    Dmsg1(90, "session_label record=%x\n", rec);
602    switch (label) {
603    case SOS_LABEL:
604       if (dev->state & ST_TAPE) {
605          dcr->StartBlock = dev->block_num;
606          dcr->StartFile  = dev->file;
607       } else {
608          dcr->StartBlock = (uint32_t)dev->file_addr;
609          dcr->StartFile = (uint32_t)(dev->file_addr >> 32);
610       }
611       break;
612    case EOS_LABEL:
613       if (dev->state & ST_TAPE) {
614          dcr->EndBlock = dev->EndBlock;
615          dcr->EndFile  = dev->EndFile;
616       } else {
617          dcr->EndBlock = (uint32_t)dev->file_addr;
618          dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
619       }
620       break;
621    default:
622       Jmsg1(jcr, M_ABORT, 0, _("Bad session label = %d\n"), label);
623       break;
624    }
625    create_session_label(dcr, rec, label);
626    rec->FileIndex = label;
627
628    /*
629     * We guarantee that the session record can totally fit
630     *  into a block. If not, write the block, and put it in
631     *  the next block. Having the sesssion record totally in
632     *  one block makes reading them much easier (no need to
633     *  read the next block).
634     */
635    if (!can_write_record_to_block(block, rec)) {
636       Dmsg0(100, "Cannot write session label to block.\n");
637       if (!write_block_to_device(dcr)) {
638          Dmsg0(90, "Got session label write_block_to_dev error.\n");
639          /* ****FIXME***** errno is not set here */
640          Jmsg(jcr, M_FATAL, 0, _("Error writing Session label to %s: %s\n"),
641                            dev_vol_name(dev), strerror(errno));
642          free_record(rec);
643          return false;
644       }
645    }
646    if (!write_record_to_block(block, rec)) {
647       Jmsg(jcr, M_FATAL, 0, _("Error writing Session label to %s: %s\n"),
648                         dev_vol_name(dev), strerror(errno));
649       free_record(rec);
650       return false;
651    }
652
653    Dmsg6(20, "Write sesson_label record JobId=%d FI=%s SessId=%d Strm=%s len=%d "
654              "remainder=%d\n", jcr->JobId,
655       FI_to_ascii(rec->FileIndex), rec->VolSessionId,
656       stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
657       rec->remainder);
658
659    free_record(rec);
660    Dmsg2(20, "Leave write_session_label Block=%d File=%d\n",
661       dev->block_num, dev->file);
662    return true;
663 }
664
665 void dump_volume_label(DEVICE *dev)
666 {
667    int dbl = debug_level;
668    uint32_t File;
669    const char *LabelType;
670    char buf[30];
671    struct tm tm;
672    struct date_time dt;
673
674    debug_level = 1;
675    File = dev->file;
676    switch (dev->VolHdr.LabelType) {
677       case PRE_LABEL:
678          LabelType = "PRE_LABEL";
679          break;
680       case VOL_LABEL:
681          LabelType = "VOL_LABEL";
682          break;
683       case EOM_LABEL:
684          LabelType = "EOM_LABEL";
685          break;
686       case SOS_LABEL:
687          LabelType = "SOS_LABEL";
688          break;
689       case EOS_LABEL:
690          LabelType = "EOS_LABEL";
691          break;
692       case EOT_LABEL:
693          goto bail_out;
694       default:
695          LabelType = buf;
696          sprintf(buf, "Unknown %d", dev->VolHdr.LabelType);
697          break;
698    }
699
700
701    Pmsg11(-1, "\nVolume Label:\n"
702 "Id                : %s"
703 "VerNo             : %d\n"
704 "VolName           : %s\n"
705 "PrevVolName       : %s\n"
706 "VolFile           : %d\n"
707 "LabelType         : %s\n"
708 "LabelSize         : %d\n"
709 "PoolName          : %s\n"
710 "MediaType         : %s\n"
711 "PoolType          : %s\n"
712 "HostName          : %s\n"
713 "",
714              dev->VolHdr.Id, dev->VolHdr.VerNum,
715              dev->VolHdr.VolName, dev->VolHdr.PrevVolName,
716              File, LabelType, dev->VolHdr.LabelSize,
717              dev->VolHdr.PoolName, dev->VolHdr.MediaType,
718              dev->VolHdr.PoolType, dev->VolHdr.HostName);
719
720    if (dev->VolHdr.VerNum >= 11) {
721       char dt[50];
722       bstrftime(dt, sizeof(dt), btime_to_unix(dev->VolHdr.label_btime));
723       Pmsg1(-1, "Date label written: %s\n", dt);
724    } else {
725    dt.julian_day_number   = dev->VolHdr.label_date;
726    dt.julian_day_fraction = dev->VolHdr.label_time;
727    tm_decode(&dt, &tm);
728    Pmsg5(-1,
729 "Date label written: %04d-%02d-%02d at %02d:%02d\n",
730       tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min);
731    }
732
733 bail_out:
734    debug_level = dbl;
735 }
736
737 bool unser_session_label(SESSION_LABEL *label, DEV_RECORD *rec)
738 {
739    ser_declare;
740
741    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Session_Label);
742    unser_begin(rec->data, SER_LENGTH_Session_Label);
743    unser_string(label->Id);
744    unser_uint32(label->VerNum);
745    unser_uint32(label->JobId);
746    if (label->VerNum >= 11) {
747       unser_btime(label->write_btime);
748    } else {
749       unser_float64(label->write_date);
750    }
751    unser_float64(label->write_time);
752    unser_string(label->PoolName);
753    unser_string(label->PoolType);
754    unser_string(label->JobName);
755    unser_string(label->ClientName);
756    if (label->VerNum >= 10) {
757       unser_string(label->Job);          /* Unique name of this Job */
758       unser_string(label->FileSetName);
759       unser_uint32(label->JobType);
760       unser_uint32(label->JobLevel);
761    }
762    if (label->VerNum >= 11) {
763       unser_string(label->FileSetMD5);
764    } else {
765       label->FileSetMD5[0] = 0;
766    }
767    if (rec->FileIndex == EOS_LABEL) {
768       unser_uint32(label->JobFiles);
769       unser_uint64(label->JobBytes);
770       unser_uint32(label->StartBlock);
771       unser_uint32(label->EndBlock);
772       unser_uint32(label->StartFile);
773       unser_uint32(label->EndFile);
774       unser_uint32(label->JobErrors);
775       if (label->VerNum >= 11) {
776          unser_uint32(label->JobStatus);
777       } else {
778          label->JobStatus = JS_Terminated; /* kludge */
779       }
780    }
781    return true;
782 }
783
784
785 static void dump_session_label(DEV_RECORD *rec, const char *type)
786 {
787    int dbl;
788    struct date_time dt;
789    struct tm tm;
790    SESSION_LABEL label;
791    char ec1[30], ec2[30], ec3[30], ec4[30], ec5[30], ec6[30], ec7[30];
792
793    unser_session_label(&label, rec);
794    dbl = debug_level;
795    debug_level = 1;
796    Pmsg7(-1, "\n%s Record:\n"
797 "JobId             : %d\n"
798 "VerNum            : %d\n"
799 "PoolName          : %s\n"
800 "PoolType          : %s\n"
801 "JobName           : %s\n"
802 "ClientName        : %s\n"
803 "",    type, label.JobId, label.VerNum,
804       label.PoolName, label.PoolType,
805       label.JobName, label.ClientName);
806
807    if (label.VerNum >= 10) {
808       Pmsg4(-1, ""
809 "Job (unique name) : %s\n"
810 "FileSet           : %s\n"
811 "JobType           : %c\n"
812 "JobLevel          : %c\n"
813 "", label.Job, label.FileSetName, label.JobType, label.JobLevel);
814    }
815
816    if (rec->FileIndex == EOS_LABEL) {
817       Pmsg8(-1, ""
818 "JobFiles          : %s\n"
819 "JobBytes          : %s\n"
820 "StartBlock        : %s\n"
821 "EndBlock          : %s\n"
822 "StartFile         : %s\n"
823 "EndFile           : %s\n"
824 "JobErrors         : %s\n"
825 "JobStatus         : %c\n"
826 "",
827          edit_uint64_with_commas(label.JobFiles, ec1),
828          edit_uint64_with_commas(label.JobBytes, ec2),
829          edit_uint64_with_commas(label.StartBlock, ec3),
830          edit_uint64_with_commas(label.EndBlock, ec4),
831          edit_uint64_with_commas(label.StartFile, ec5),
832          edit_uint64_with_commas(label.EndFile, ec6),
833          edit_uint64_with_commas(label.JobErrors, ec7),
834          label.JobStatus);
835    }
836    if (label.VerNum >= 11) {
837       char dt[50];
838       bstrftime(dt, sizeof(dt), btime_to_unix(label.write_btime));
839       Pmsg1(-1, _("Date written      : %s\n"), dt);
840    } else {
841       dt.julian_day_number   = label.write_date;
842       dt.julian_day_fraction = label.write_time;
843       tm_decode(&dt, &tm);
844       Pmsg5(-1, _(""
845 "Date written      : %04d-%02d-%02d at %02d:%02d\n"),
846       tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min);
847    }
848
849    debug_level = dbl;
850 }
851
852 void dump_label_record(DEVICE *dev, DEV_RECORD *rec, int verbose)
853 {
854    const char *type;
855    int dbl;
856
857    dbl = debug_level;
858    debug_level = 1;
859    switch (rec->FileIndex) {
860    case PRE_LABEL:
861       type = _("Fresh Volume");
862       break;
863    case VOL_LABEL:
864       type = _("Volume");
865       break;
866    case SOS_LABEL:
867       type = _("Begin Job Session");
868       break;
869    case EOS_LABEL:
870       type = _("End Job Session");
871       break;
872    case EOM_LABEL:
873       type = _("End of Media");
874       break;
875    case EOT_LABEL:
876       type = ("End of Tape");
877       break;
878    default:
879       type = _("Unknown");
880       break;
881    }
882    if (verbose) {
883       switch (rec->FileIndex) {
884       case PRE_LABEL:
885       case VOL_LABEL:
886          unser_volume_label(dev, rec);
887          dump_volume_label(dev);
888          break;
889       case SOS_LABEL:
890          dump_session_label(rec, type);
891          break;
892       case EOS_LABEL:
893          dump_session_label(rec, type);
894          break;
895       case EOM_LABEL:
896          Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
897             type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
898          break;
899       case EOT_LABEL:
900          Pmsg0(-1, _("End of physical tape.\n"));
901          break;
902       default:
903          Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
904             type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
905          break;
906       }
907    } else {
908       SESSION_LABEL label;
909       switch (rec->FileIndex) {
910       case SOS_LABEL:
911          unser_session_label(&label, rec);
912          Pmsg6(-1, "%s Record: SessId=%d SessTime=%d JobId=%d Level=%c Type=%c\n",
913             type, rec->VolSessionId, rec->VolSessionTime, rec->Stream,
914             label.JobLevel, label.JobType);
915          break;
916       case EOS_LABEL:
917          char ed1[30], ed2[30];
918          unser_session_label(&label, rec);
919          Pmsg6(-1, "%s Record: SessId=%d SessTime=%d JobId=%d Level=%c Type=%c\n",
920             type, rec->VolSessionId, rec->VolSessionTime, rec->Stream,
921             label.JobLevel, label.JobType);
922          Pmsg4(-1, "   Files=%s Bytes=%s Errors=%d Status=%c\n",
923             edit_uint64_with_commas(label.JobFiles, ed1),
924             edit_uint64_with_commas(label.JobBytes, ed2),
925             label.JobErrors, (char)label.JobStatus);
926          break;
927       case EOM_LABEL:
928       case PRE_LABEL:
929       case VOL_LABEL:
930       default:
931          Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
932       type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
933          break;
934       case EOT_LABEL:
935          break;
936       }
937    }
938    debug_level = dbl;
939 }