3 * label.c Bacula routines to handle labels
11 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
30 #include "bacula.h" /* pull in global headers */
31 #include "stored.h" /* pull in Storage Deamon headers */
33 /* Forward referenced functions */
34 static int create_volume_label(DEVICE *dev, char *VolName);
35 static void create_volume_label_record(JCR *jcr, DEVICE *dev, DEV_RECORD *rec);
37 extern char my_name[];
38 extern int debug_level;
41 * Read the volume label
43 * If jcr->VolumeName == NULL, we accept any Bacula Volume
44 * If jcr->VolumeName[0] == 0, we accept any Bacula Volume
45 * otherwise jcr->VolumeName must match the Volume.
47 * If VolName given, ensure that it matches
49 * Returns VOL_ code as defined in record.h
59 int read_dev_volume_label(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
61 char *VolName = jcr->VolumeName;
65 Dmsg2(30, "Enter read_volume_label device=%s vol=%s\n",
66 dev_name(dev), VolName);
68 if (dev->state & ST_LABEL) { /* did we already read label? */
69 /* Compare Volume Names */
70 if (VolName && *VolName && strcmp(dev->VolHdr.VolName, VolName) != 0) {
71 Mmsg(&jcr->errmsg, _("Volume name mismatch on device %s: Wanted %s got %s\n"),
72 dev_name(dev), VolName, dev->VolHdr.VolName);
74 * Cancel Job if too many label errors
77 if (jcr->label_errors > 100) {
78 jcr->JobStatus = JS_Cancelled;
79 Jmsg(jcr, M_FATAL, 0, "%s", jcr->errmsg);
81 return jcr->label_status = VOL_NAME_ERROR;
83 Dmsg0(30, "Leave read_volume_label() VOL_OK\n");
84 return jcr->label_status = VOL_OK; /* label already read */
87 dev->state &= ~(ST_LABEL|ST_APPEND|ST_READ); /* set no label, no append */
89 if (!rewind_dev(dev)) {
90 Mmsg(&jcr->errmsg, _("Couldn't rewind device %s ERR=%s\n"), dev_name(dev),
92 return jcr->label_status = VOL_IO_ERROR;
94 strcpy(dev->VolHdr.Id, "**error**");
96 /* Read the device label block */
97 record = new_record();
98 Dmsg0(90, "Big if statement in read_volume_label\n");
99 if (!read_block_from_dev(dev, block)) {
100 Mmsg(&jcr->errmsg, _("Volume on %s is not a Bacula labeled Volume, \
101 because:\n %s"), dev_name(dev), strerror_dev(dev));
102 } else if (!read_record_from_block(block, record)) {
103 Mmsg(&jcr->errmsg, _("Could not read Volume label from block.\n"));
104 } else if (!unser_volume_label(dev, record)) {
105 Mmsg(&jcr->errmsg, _("Could not unserialize Volume label: %s\n"),
107 } else if (strcmp(dev->VolHdr.Id, BaculaId) != 0 &&
108 strcmp(dev->VolHdr.Id, OldBaculaId) != 0) {
109 Mmsg(&jcr->errmsg, _("Volume Header Id bad: %s\n"), dev->VolHdr.Id);
117 return jcr->label_status = VOL_NO_LABEL;
124 if (dev->VolHdr.VerNum != BaculaTapeVersion &&
125 dev->VolHdr.VerNum != OldCompatibleBaculaTapeVersion1 &&
126 dev->VolHdr.VerNum != OldCompatibleBaculaTapeVersion2) {
127 Mmsg(&jcr->errmsg, _("Volume on %s has wrong Bacula version. Wanted %d got %d\n"),
128 dev_name(dev), BaculaTapeVersion, dev->VolHdr.VerNum);
129 return jcr->label_status = VOL_VERSION_ERROR;
132 /* Compare Volume Names */
133 Dmsg2(30, "Compare Vol names: VolName=%s hdr=%s\n", VolName?VolName:"*", dev->VolHdr.VolName);
134 if (VolName && *VolName && strcmp(dev->VolHdr.VolName, VolName) != 0) {
135 Mmsg(&jcr->errmsg, _("Volume name mismatch. Wanted %s got %s\n"),
136 VolName, dev->VolHdr.VolName);
138 * Cancel Job if too many label errors
139 * => we are in a loop
141 if (jcr->label_errors > 100) {
142 jcr->JobStatus = JS_Cancelled;
143 Jmsg(jcr, M_FATAL, 0, "%s", jcr->errmsg);
145 return jcr->label_status = VOL_NAME_ERROR;
147 Dmsg1(30, "Copy vol_name=%s\n", dev->VolHdr.VolName);
149 /* We are looking for either an unused Bacula tape (PRE_LABEL) or
150 * a Bacula volume label (VOL_LABEL)
152 if (dev->VolHdr.LabelType != PRE_LABEL && dev->VolHdr.LabelType != VOL_LABEL) {
153 Mmsg(&jcr->errmsg, _("Volume on %s has bad Bacula label type: %x\n"),
154 dev_name(dev), dev->VolHdr.LabelType);
155 return jcr->label_status = VOL_LABEL_ERROR;
158 dev->state |= ST_LABEL; /* set has Bacula label */
159 if (debug_level >= 10) {
160 dump_volume_label(dev);
162 Dmsg0(30, "Leave read_volume_label() VOL_OK\n");
163 return jcr->label_status = VOL_OK;
166 /* unser_volume_label
168 * Unserialize the Volume label into the device Volume_Label
171 * Assumes that the record is already read.
173 * Returns: 0 on error
177 int unser_volume_label(DEVICE *dev, DEV_RECORD *rec)
181 if (rec->FileIndex != VOL_LABEL && rec->FileIndex != PRE_LABEL) {
182 Mmsg3(&dev->errmsg, _("Expecting Volume Label, got FI=%s Stream=%s len=%d\n"),
183 FI_to_ascii(rec->FileIndex),
184 stream_to_ascii(rec->Stream, rec->FileIndex),
189 dev->VolHdr.LabelType = rec->FileIndex;
190 dev->VolHdr.LabelSize = rec->data_len;
193 /* Unserialize the record into the Volume Header */
194 ser_begin(rec->data, SER_LENGTH_Volume_Label);
195 #define Fld(x) (dev->VolHdr.x)
196 unser_string(Fld(Id));
198 unser_uint32(Fld(VerNum));
200 if (Fld(VerNum) >= 11) {
201 unser_btime(Fld(label_btime));
202 unser_btime(Fld(write_btime));
203 } else { /* old way */
204 unser_float64(Fld(label_date));
205 unser_float64(Fld(label_time));
207 unser_float64(Fld(write_date)); /* Unused with VerNum >= 11 */
208 unser_float64(Fld(write_time)); /* Unused with VerNum >= 11 */
210 unser_string(Fld(VolName));
211 unser_string(Fld(PrevVolName));
212 unser_string(Fld(PoolName));
213 unser_string(Fld(PoolType));
214 unser_string(Fld(MediaType));
216 unser_string(Fld(HostName));
217 unser_string(Fld(LabelProg));
218 unser_string(Fld(ProgVersion));
219 unser_string(Fld(ProgDate));
221 ser_end(rec->data, SER_LENGTH_Volume_Label);
223 Dmsg0(90, "ser_read_vol\n");
224 if (debug_level >= 90) {
225 dump_volume_label(dev);
231 * Put a volume label into the block
233 * Returns: 0 on failure
236 int write_volume_label_to_block(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
240 Dmsg0(20, "write Label in write_volume_label_to_block()\n");
241 memset(&rec, 0, sizeof(rec));
242 rec.data = get_memory(SER_LENGTH_Volume_Label);
244 create_volume_label_record(jcr, dev, &rec);
246 empty_block(block); /* Volume label always at beginning */
247 if (!write_record_to_block(block, &rec)) {
248 free_pool_memory(rec.data);
249 Jmsg1(jcr, M_FATAL, 0, _("Cannot write Volume label to block for device %s\n"),
253 Dmsg1(90, "Wrote label of %d bytes to block\n", rec.data_len);
255 free_pool_memory(rec.data);
260 * create_volume_label_record
261 * Serialize label (from dev->VolHdr structure) into device record.
262 * Assumes that the dev->VolHdr structure is properly
265 static void create_volume_label_record(JCR *jcr, DEVICE *dev, DEV_RECORD *rec)
270 /* Serialize the label into the device record. */
272 ser_begin(rec->data, SER_LENGTH_Volume_Label);
273 #define Fld(x) (dev->VolHdr.x)
276 ser_uint32(Fld(VerNum));
278 if (Fld(VerNum >= 11)) {
279 ser_btime(Fld(label_btime));
280 Fld(write_btime) = get_current_btime();
281 ser_btime(Fld(write_btime));
285 /* OLD WAY DEPRECATED */
286 ser_float64(Fld(label_date));
287 ser_float64(Fld(label_time));
288 get_current_time(&dt);
289 Fld(write_date) = dt.julian_day_number;
290 Fld(write_time) = dt.julian_day_fraction;
292 ser_float64(Fld(write_date)); /* 0 if VerNum >= 11 */
293 ser_float64(Fld(write_time)); /* 0 if VerNum >= 11 */
295 ser_string(Fld(VolName));
296 ser_string(Fld(PrevVolName));
297 ser_string(Fld(PoolName));
298 ser_string(Fld(PoolType));
299 ser_string(Fld(MediaType));
301 ser_string(Fld(HostName));
302 ser_string(Fld(LabelProg));
303 ser_string(Fld(ProgVersion));
304 ser_string(Fld(ProgDate));
306 ser_end(rec->data, SER_LENGTH_Volume_Label);
307 rec->data_len = ser_length(rec->data);
308 rec->FileIndex = Fld(LabelType);
309 rec->VolSessionId = jcr->VolSessionId;
310 rec->VolSessionTime = jcr->VolSessionTime;
311 rec->Stream = jcr->NumVolumes;
312 Dmsg2(100, "Created Vol label rec: FI=%s len=%d\n", FI_to_ascii(rec->FileIndex),
319 * Create a volume label in memory
320 * Returns: 0 on error
323 static int create_volume_label(DEVICE *dev, char *VolName)
326 DEVRES *device = (DEVRES *)dev->device;
328 Dmsg0(90, "Start create_volume_label()\n");
332 memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
334 /* ***FIXME*** we really need to get the volume name,
335 * pool name, and pool type from the database.
336 * We also need to pickup the MediaType.
338 strcpy(dev->VolHdr.Id, BaculaId);
339 dev->VolHdr.VerNum = BaculaTapeVersion;
340 dev->VolHdr.LabelType = PRE_LABEL; /* Mark tape as unused */
341 strcpy(dev->VolHdr.VolName, VolName);
342 strcpy(dev->VolHdr.PoolName, "Default");
343 strcpy(dev->VolHdr.MediaType, device->media_type);
344 strcpy(dev->VolHdr.PoolType, "Backup");
346 /* Put label time/date in header */
347 if (BaculaTapeVersion >= 11) {
348 dev->VolHdr.label_btime = get_current_btime();
349 dev->VolHdr.label_date = 0;
350 dev->VolHdr.label_time = 0;
352 /* OLD WAY DEPRECATED */
353 get_current_time(&dt);
354 dev->VolHdr.label_date = dt.julian_day_number;
355 dev->VolHdr.label_time = dt.julian_day_fraction;
358 strcpy(dev->VolHdr.LabelProg, my_name);
359 sprintf(dev->VolHdr.ProgVersion, "Ver. %s %s", VERSION, DATE);
360 sprintf(dev->VolHdr.ProgDate, "Build %s %s", __DATE__, __TIME__);
361 dev->state |= ST_LABEL; /* set has Bacula label */
362 if (debug_level >= 90) {
363 dump_volume_label(dev);
369 * Write a Volume Label
370 * !!! Note, this is ONLY used for writing
371 * a fresh volume label. Any data
372 * after the label will be destroyed,
373 * in fact, we write the label 5 times !!!!
375 * This routine expects that open_device() was previously called.
377 * This routine should be used only when labeling a blank tape.
379 int write_volume_label_to_dev(JCR *jcr, DEVRES *device, char *VolName, char *PoolName)
381 DEVICE *dev = device->dev;
387 Dmsg0(99, "write_volume_label()\n");
388 if (!create_volume_label(dev, VolName)) {
391 strcpy(dev->VolHdr.MediaType, device->media_type);
392 strcpy(dev->VolHdr.VolName, VolName);
393 strcpy(dev->VolHdr.PoolName, PoolName);
395 if (!rewind_dev(dev)) {
396 Dmsg2(30, "Bad status on %s from rewind. ERR=%s\n", dev_name(dev), strerror_dev(dev));
400 block = new_block(dev);
401 memset(&rec, 0, sizeof(rec));
402 rec.data = get_memory(SER_LENGTH_Volume_Label);
403 create_volume_label_record(jcr, dev, &rec);
406 if (!write_record_to_block(block, &rec)) {
407 Dmsg2(30, "Bad Label write on %s. ERR=%s\n", dev_name(dev), strerror_dev(dev));
409 free_pool_memory(rec.data);
412 Dmsg2(30, "Wrote label of %d bytes to %s\n", rec.data_len, dev_name(dev));
414 free_pool_memory(rec.data);
416 Dmsg0(99, "Call write_block_to_device()\n");
417 if (!write_block_to_dev(dev, block)) {
418 Dmsg2(30, "Bad Label write on %s. ERR=%s\n", dev_name(dev), strerror_dev(dev));
421 Dmsg0(99, " Wrote block to device\n");
425 dev->state |= ST_LABEL;
427 if (debug_level >= 20) {
428 dump_volume_label(dev);
436 * Create session label
437 * The pool memory must be released by the calling program
439 void create_session_label(JCR *jcr, DEV_RECORD *rec, int label)
444 rec->sync = 1; /* wait for completion */
445 rec->VolSessionId = jcr->VolSessionId;
446 rec->VolSessionTime = jcr->VolSessionTime;
447 rec->Stream = jcr->JobId;
449 ser_begin(rec->data, SER_LENGTH_Session_Label);
450 ser_string(BaculaId);
451 ser_uint32(BaculaTapeVersion);
453 ser_uint32(jcr->JobId);
455 if (BaculaTapeVersion >= 11) {
456 ser_btime(get_current_btime());
459 /* OLD WAY DEPRECATED */
460 get_current_time(&dt);
461 ser_float64(dt.julian_day_number);
462 ser_float64(dt.julian_day_fraction);
465 ser_string(jcr->pool_name);
466 ser_string(jcr->pool_type);
467 ser_string(jcr->job_name); /* base Job name */
468 ser_string(jcr->client_name);
470 /* Added in VerNum 10 */
471 ser_string(jcr->Job); /* Unique name of this Job */
472 ser_string(jcr->fileset_name);
473 ser_uint32(jcr->JobType);
474 ser_uint32(jcr->JobLevel);
475 if (BaculaTapeVersion >= 11) {
476 ser_string(jcr->fileset_md5);
479 if (label == EOS_LABEL) {
480 ser_uint32(jcr->JobFiles);
481 ser_uint64(jcr->JobBytes);
482 ser_uint32(jcr->StartBlock);
483 ser_uint32(jcr->EndBlock);
484 ser_uint32(jcr->StartFile);
485 ser_uint32(jcr->EndFile);
486 ser_uint32(jcr->JobErrors);
488 /* Added in VerNum 11 */
489 ser_uint32(jcr->JobStatus);
491 ser_end(rec->data, SER_LENGTH_Session_Label);
492 rec->data_len = ser_length(rec->data);
495 /* Write session label
496 * Returns: 0 on failure
499 int write_session_label(JCR *jcr, DEV_BLOCK *block, int label)
501 DEVICE *dev = jcr->device->dev;
505 Dmsg1(90, "session_label record=%x\n", rec);
508 if (dev->state & ST_TAPE) {
509 jcr->StartBlock = dev->block_num;
510 jcr->StartFile = dev->file;
512 jcr->StartBlock = (uint32_t)dev->file_addr;
513 jcr->StartFile = (uint32_t)(dev->file_addr >> 32);
517 if (dev->state & ST_TAPE) {
518 jcr->EndBlock = dev->block_num;
519 jcr->EndFile = dev->file;
521 jcr->EndBlock = (uint32_t)dev->file_addr;
522 jcr->EndFile = (uint32_t)(dev->file_addr >> 32);
526 Jmsg1(jcr, M_ABORT, 0, _("Bad session label = %d\n"), label);
529 create_session_label(jcr, rec, label);
530 rec->FileIndex = label;
533 * We guarantee that the session record can totally fit
534 * into a block. If not, write the block, and put it in
535 * the next block. Having the sesssion record totally in
536 * one block makes reading them much easier (no need to
537 * read the next block).
539 if (!can_write_record_to_block(block, rec)) {
540 Dmsg0(100, "Cannot write session label to block.\n");
541 if (!write_block_to_device(jcr, dev, block)) {
542 Dmsg0(90, "Got session label write_block_to_dev error.\n");
543 Jmsg(jcr, M_FATAL, 0, _("Error writing Session label to %s: %s\n"),
544 dev_vol_name(dev), strerror(errno));
549 if (!write_record_to_block(block, rec)) {
550 Jmsg(jcr, M_FATAL, 0, _("Error writing Session label to %s: %s\n"),
551 dev_vol_name(dev), strerror(errno));
556 Dmsg6(20, "Write sesson_label record JobId=%d FI=%s SessId=%d Strm=%s len=%d\n\
557 remainder=%d\n", jcr->JobId,
558 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
559 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
563 Dmsg2(20, "Leave write_session_label Block=%d File=%d\n",
564 dev->block_num, dev->file);
568 void dump_volume_label(DEVICE *dev)
570 int dbl = debug_level;
572 char *LabelType, buf[30];
578 switch (dev->VolHdr.LabelType) {
580 LabelType = "PRE_LABEL";
583 LabelType = "VOL_LABEL";
586 LabelType = "EOM_LABEL";
589 LabelType = "SOS_LABEL";
592 LabelType = "EOS_LABEL";
598 sprintf(buf, "Unknown %d", dev->VolHdr.LabelType);
603 Pmsg11(-1, "\nVolume Label:\n\
616 dev->VolHdr.Id, dev->VolHdr.VerNum,
617 dev->VolHdr.VolName, dev->VolHdr.PrevVolName,
618 File, LabelType, dev->VolHdr.LabelSize,
619 dev->VolHdr.PoolName, dev->VolHdr.MediaType,
620 dev->VolHdr.PoolType, dev->VolHdr.HostName);
622 if (dev->VolHdr.VerNum >= 11) {
624 bstrftime(dt, sizeof(dt), btime_to_unix(dev->VolHdr.label_btime));
625 Pmsg1(-1, "Date label written: %s\n", dt);
627 dt.julian_day_number = dev->VolHdr.label_date;
628 dt.julian_day_fraction = dev->VolHdr.label_time;
631 Date label written: %04d-%02d-%02d at %02d:%02d\n",
632 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min);
639 int unser_session_label(SESSION_LABEL *label, DEV_RECORD *rec)
643 unser_begin(rec->data, SER_LENGTH_Session_Label);
644 unser_string(label->Id);
645 unser_uint32(label->VerNum);
646 unser_uint32(label->JobId);
647 if (label->VerNum >= 11) {
648 unser_btime(label->write_btime);
650 unser_float64(label->write_date);
652 unser_float64(label->write_time);
653 unser_string(label->PoolName);
654 unser_string(label->PoolType);
655 unser_string(label->JobName);
656 unser_string(label->ClientName);
657 if (label->VerNum >= 10) {
658 unser_string(label->Job); /* Unique name of this Job */
659 unser_string(label->FileSetName);
660 unser_uint32(label->JobType);
661 unser_uint32(label->JobLevel);
663 if (label->VerNum >= 11) {
664 unser_string(label->FileSetMD5);
666 label->FileSetMD5[0] = 0;
668 if (rec->FileIndex == EOS_LABEL) {
669 unser_uint32(label->JobFiles);
670 unser_uint64(label->JobBytes);
671 unser_uint32(label->StartBlock);
672 unser_uint32(label->EndBlock);
673 unser_uint32(label->StartFile);
674 unser_uint32(label->EndFile);
675 unser_uint32(label->JobErrors);
676 if (label->VerNum >= 11) {
677 unser_uint32(label->JobStatus);
679 label->JobStatus = JS_Terminated; /* kludge */
686 static void dump_session_label(DEV_RECORD *rec, char *type)
692 char ec1[30], ec2[30], ec3[30], ec4[30], ec5[30], ec6[30], ec7[30];
694 unser_session_label(&label, rec);
697 Pmsg7(-1, "\n%s Record:\n\
704 ", type, label.JobId, label.VerNum,
705 label.PoolName, label.PoolType,
706 label.JobName, label.ClientName);
708 if (label.VerNum >= 10) {
710 Job (unique name) : %s\n\
714 ", label.Job, label.FileSetName, label.JobType, label.JobLevel);
717 if (rec->FileIndex == EOS_LABEL) {
728 edit_uint64_with_commas(label.JobFiles, ec1),
729 edit_uint64_with_commas(label.JobBytes, ec2),
730 edit_uint64_with_commas(label.StartBlock, ec3),
731 edit_uint64_with_commas(label.EndBlock, ec4),
732 edit_uint64_with_commas(label.StartFile, ec5),
733 edit_uint64_with_commas(label.EndFile, ec6),
734 edit_uint64_with_commas(label.JobErrors, ec7),
737 if (label.VerNum >= 11) {
739 bstrftime(dt, sizeof(dt), btime_to_unix(label.write_btime));
740 Pmsg1(-1, _("Date written : %s\n"), dt);
742 dt.julian_day_number = label.write_date;
743 dt.julian_day_fraction = label.write_time;
746 Date written : %04d-%02d-%02d at %02d:%02d\n"),
747 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min);
753 void dump_label_record(DEVICE *dev, DEV_RECORD *rec, int verbose)
760 switch (rec->FileIndex) {
762 type = _("Fresh Volume");
768 type = _("Begin Session");
771 type = _("End Session");
774 type = _("End of Media");
777 type = ("End of Tape");
784 switch (rec->FileIndex) {
787 unser_volume_label(dev, rec);
788 dump_volume_label(dev);
791 dump_session_label(rec, type);
794 dump_session_label(rec, type);
797 Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
798 type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
801 Pmsg0(-1, _("End of physical tape.\n"));
804 Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
805 type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
809 switch (rec->FileIndex) {
813 unser_session_label(&label, rec);
814 Pmsg6(-1, "%s Record: SessId=%d SessTime=%d JobId=%d Level=%c \
816 type, rec->VolSessionId, rec->VolSessionTime, rec->Stream,
817 label.JobLevel, label.JobType);
823 Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
824 type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);