2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2016 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
21 * Routines for handling mounting tapes for reading and for
24 * Kern Sibbald, August MMII
28 #include "bacula.h" /* pull in global headers */
29 #include "stored.h" /* pull in Storage Deamon headers */
31 static pthread_mutex_t mount_mutex = PTHREAD_MUTEX_INITIALIZER;
48 * If release is set, we rewind the current volume,
49 * which we no longer want, and ask the user (console)
50 * to mount the next volume.
52 * Continue trying until we get it, and then ensure
53 * that we can write on it.
55 * This routine returns a 0 only if it is REALLY
56 * impossible to get the requested Volume.
58 * This routine is entered with the device blocked, but not
62 bool DCR::mount_next_write_volume()
65 bool ask = false, recycle, autochanger;
70 Dmsg2(100, "Enter mount_next_volume(release=%d) dev=%s\n", dev->must_unload(),
73 init_device_wait_timers(dcr);
78 * Attempt to mount the next volume. If something non-fatal goes
79 * wrong, we come back here to re-try (new op messages, re-read
83 Dmsg1(100, "mount_next_vol retry=%d\n", retry);
84 /* Ignore retry if this is poll request */
85 if (dev->is_nospace() || retry++ > 4) {
86 /* Last ditch effort before giving up, force operator to respond */
89 if (!dir_ask_sysop_to_mount_volume(dcr, SD_APPEND)) {
90 Jmsg(jcr, M_FATAL, 0, _("Too many errors trying to mount %s device %s.\n"),
91 dev->print_type(), dev->print_name());
92 goto no_lock_bail_out;
95 Dmsg1(90, "Continue after dir_ask_sysop_to_mount. must_load=%d\n", dev->must_load());
97 if (job_canceled(jcr)) {
98 Jmsg(jcr, M_FATAL, 0, _("Job %d canceled.\n"), jcr->JobId);
103 if (dev->must_unload()) {
104 ask = true; /* ask operator to mount tape */
107 do_swapping(SD_APPEND);
110 if (!find_a_volume()) {
114 if (job_canceled(jcr)) {
117 Dmsg3(100, "After find_a_volume. Vol=%s Slot=%d VolType=%d\n",
118 getVolCatName(), VolCatInfo.Slot, VolCatInfo.VolCatType);
120 dev->notify_newvol_in_attached_dcrs(getVolCatName());
123 * Get next volume and ready it for append
124 * This code ensures that the device is ready for
125 * writing. We start from the assumption that there
126 * may not be a tape mounted.
128 * If the device is a file, we create the output
129 * file. If it is a tape, we check the volume name
130 * and move the tape to the end of data.
133 dcr->setVolCatInfo(false); /* out of date when Vols unlocked */
134 if (autoload_device(dcr, SD_APPEND, NULL) > 0) {
140 if (dev->is_autochanger() && !VolCatInfo.InChanger) {
141 ask = true; /* not in changer, do not retry */
146 Dmsg1(100, "autoload_dev returns %d\n", autochanger);
148 * If we autochanged to correct Volume or (we have not just
149 * released the Volume AND we can automount) we go ahead
150 * and read the label. If there is no tape in the drive,
151 * we will fail, recurse and ask the operator the next time.
153 if (!dev->must_unload() && dev->is_tape() && dev->has_cap(CAP_AUTOMOUNT)) {
154 Dmsg0(250, "(1)Ask=0\n");
155 ask = false; /* don't ask SYSOP this time */
157 /* Don't ask if not removable */
158 if (!dev->is_removable()) {
159 Dmsg0(250, "(2)Ask=0\n");
162 Dmsg2(100, "Ask=%d autochanger=%d\n", ask, autochanger);
166 dcr->setVolCatInfo(false); /* out of date when Vols unlocked */
167 if (!dir_ask_sysop_to_mount_volume(dcr, SD_APPEND)) {
168 Dmsg0(150, "Error return ask_sysop ...\n");
169 goto no_lock_bail_out;
173 if (job_canceled(jcr)) {
176 Dmsg3(100, "want vol=%s devvol=%s dev=%s\n", VolumeName,
177 dev->VolHdr.VolumeName, dev->print_name());
179 if (dev->poll && dev->has_cap(CAP_CLOSEONPOLL)) {
184 /* Ensure the device is open */
185 if (dev->has_cap(CAP_STREAM)) {
186 mode = OPEN_WRITE_ONLY;
188 mode = OPEN_READ_WRITE;
190 /* Try autolabel if enabled */
191 Dmsg1(100, "Try open Vol=%s\n", getVolCatName());
192 if (!dev->open(dcr, mode)) {
193 Dmsg1(100, "Try autolabel Vol=%s\n", getVolCatName());
194 try_autolabel(false); /* try to create a new volume label */
196 while (!dev->open(dcr, mode)) {
197 Dmsg1(100, "open_device failed: ERR=%s\n", dev->bstrerror());
198 if ((dev->is_file() && dev->is_removable()) || dev->is_dvd()) {
200 Dmsg0(150, "call scan_dir_for_vol\n");
202 if (!dev->mount(0)) {
206 if (ok && dev->scan_dir_for_volume(dcr)) {
207 if (dev->open(dcr, mode)) {
208 break; /* got a valid volume */
211 if (ok && dev->is_dvd()) {
215 if (try_autolabel(false) == try_read_vol) {
216 break; /* created a new volume label */
219 /* ***FIXME*** if autochanger, before giving up try unload and load */
221 Jmsg4(jcr, M_WARNING, 0, _("Open of %s device %s Volume \"%s\" failed: ERR=%s\n"),
222 dev->print_type(), dev->print_name(), dcr->VolumeName, dev->bstrerror());
224 /* If not removable, Volume is broken. This is a serious issue here. */
225 if(dev->is_file() && !dev->is_removable()) {
226 Dmsg3(40, "Volume \"%s\" not loaded on %s device %s.\n",
227 dcr->VolumeName, dev->print_type(), dev->print_name());
228 mark_volume_in_error();
231 Dmsg0(100, "set_unload\n");
232 dev->set_unload(); /* force ask sysop */
236 Dmsg0(100, "goto mount_next_vol\n");
241 * Now check the volume label to make sure we have the right tape mounted
244 switch (check_volume_label(ask, autochanger)) {
246 Dmsg0(50, "set_unload\n");
247 dev->set_unload(); /* want a different Volume */
248 Dmsg0(100, "goto mount_next_vol\n");
258 * Check that volcatinfo is good
260 if (!dev->haveVolCatInfo()) {
261 Dmsg0(100, "Do not have volcatinfo\n");
262 if (!find_a_volume()) {
265 dev->VolCatInfo = VolCatInfo; /* structure assignment */
269 * See if we have a fresh tape or a tape with data.
271 * Note, if the LabelType is PRE_LABEL, it was labeled
272 * but never written. If so, rewrite the label but set as
273 * VOL_LABEL. We rewind and return the label (reconstructed)
274 * in the block so that in the case of a new tape, data can
275 * be appended just after the block label. If we are writing
276 * a second volume, the calling routine will write the label
277 * before writing the overflow block.
279 * If the tape is marked as Recycle, we rewrite the label.
281 recycle = strcmp(dev->VolCatInfo.VolCatStatus, "Recycle") == 0;
282 if (dev->VolHdr.LabelType == PRE_LABEL || recycle) {
283 dcr->WroteVol = false;
284 if (!dcr->rewrite_volume_label(recycle)) {
285 mark_volume_in_error();
290 * OK, at this point, we have a valid Bacula label, but
291 * we need to position to the end of the volume, since we are
292 * just now putting it into append mode.
294 Dmsg1(100, "Device previously written, moving to end of data. Expect %lld bytes\n",
295 dev->VolCatInfo.VolCatBytes);
296 Jmsg(jcr, M_INFO, 0, _("Volume \"%s\" previously written, moving to end of data.\n"),
299 if (!dev->eod(dcr)) {
300 Dmsg3(050, "Unable to position to end of data on %s device %s: ERR=%s\n",
301 dev->print_type(), dev->print_name(), dev->bstrerror());
302 Jmsg(jcr, M_ERROR, 0, _("Unable to position to end of data on %s device %s: ERR=%s\n"),
303 dev->print_type(), dev->print_name(), dev->bstrerror());
304 mark_volume_in_error();
308 if (!is_eod_valid()) {
309 Dmsg0(100, "goto mount_next_vol\n");
313 dev->VolCatInfo.VolCatMounts++; /* Update mounts */
314 Dmsg1(150, "update volinfo mounts=%d\n", dev->VolCatInfo.VolCatMounts);
315 if (!dir_update_volume_info(dcr, false, false)) {
319 /* Return an empty block */
320 empty_block(block); /* we used it for reading so set for write */
323 Dmsg1(150, "set APPEND, normal return from mount_next_write_volume. dev=%s\n",
338 * This routine is meant to be called once the first pass
339 * to ensure that we have a candidate volume to mount.
340 * Otherwise, we ask the sysop to created one.
341 * Note, mount_mutex is already locked on entry and thus
342 * must remain locked on exit from this function.
344 bool DCR::find_a_volume()
349 if (!is_suitable_volume_mounted()) {
350 bool have_vol = false;
351 /* Do we have a candidate volume? */
353 bstrncpy(VolumeName, dev->vol->vol_name, sizeof(VolumeName));
354 have_vol = dir_get_volume_info(this, GET_VOL_INFO_FOR_WRITE);
357 * Get Director's idea of what tape we should have mounted.
361 Dmsg0(200, "Before dir_find_next_appendable_volume.\n");
362 while (!dir_find_next_appendable_volume(dcr)) {
363 Dmsg0(200, "not dir_find_next\n");
364 if (job_canceled(jcr)) {
368 * Unlock the mount mutex while waiting or
369 * the Director for a new volume
372 if (dev->must_wait()) {
374 Dmsg0(40, "No appendable volume. Calling wait_for_device\n");
375 wait_for_device(dcr, retries);
378 ok = dir_ask_sysop_to_create_appendable_volume(dcr);
381 if (!ok || job_canceled(jcr)) {
384 Dmsg0(150, "Again dir_find_next_append...\n");
389 if (dcr->haveVolCatInfo()) {
392 return dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE);
395 int DCR::check_volume_label(bool &ask, bool &autochanger)
397 int vol_label_status;
402 * If we are writing to a stream device, ASSUME the volume label
405 if (dev->has_cap(CAP_STREAM)) {
406 vol_label_status = VOL_OK;
407 create_volume_header(dev, VolumeName, "Default", false /* not DVD */);
408 dev->VolHdr.LabelType = PRE_LABEL;
410 vol_label_status = read_dev_volume_label(this);
412 if (job_canceled(jcr)) {
416 Dmsg2(150, "Want dirVol=%s dirStat=%s\n", VolumeName,
417 VolCatInfo.VolCatStatus);
420 * At this point, dev->VolCatInfo has what is in the drive, if anything,
421 * and dcr->VolCatInfo has what the Director wants.
423 switch (vol_label_status) {
425 Dmsg1(150, "Vol OK name=%s\n", dev->VolHdr.VolumeName);
426 dev->VolCatInfo = VolCatInfo; /* structure assignment */
427 break; /* got a Volume */
429 VOLUME_CAT_INFO dcrVolCatInfo, devVolCatInfo;
430 char saveVolumeName[MAX_NAME_LENGTH];
432 Dmsg2(40, "Vol NAME Error Have=%s, want=%s\n", dev->VolHdr.VolumeName, VolumeName);
433 if (dev->is_volume_to_unload()) {
435 goto check_next_volume;
439 /* If not removable, Volume is broken */
440 if (!dev->is_removable()) {
441 Jmsg3(jcr, M_WARNING, 0, _("Volume \"%s\" not loaded on %s device %s.\n"),
442 VolumeName, dev->print_type(), dev->print_name());
443 Dmsg3(40, "Volume \"%s\" not loaded on %s device %s.\n",
444 VolumeName, dev->print_type(), dev->print_name());
445 mark_volume_in_error();
446 goto check_next_volume;
451 * OK, we got a different volume mounted. First save the
452 * requested Volume info (dcr) structure, then query if
453 * this volume is really OK. If not, put back the desired
454 * volume name, mark it not in changer and continue.
456 dcrVolCatInfo = VolCatInfo; /* structure assignment */
457 devVolCatInfo = dev->VolCatInfo; /* structure assignment */
458 /* Check if this is a valid Volume in the pool */
459 bstrncpy(saveVolumeName, VolumeName, sizeof(saveVolumeName));
460 bstrncpy(VolumeName, dev->VolHdr.VolumeName, sizeof(VolumeName));
461 if (!dir_get_volume_info(this, GET_VOL_INFO_FOR_WRITE)) {
462 POOL_MEM vol_info_msg;
463 pm_strcpy(vol_info_msg, jcr->dir_bsock->msg); /* save error message */
464 /* Restore desired volume name, note device info out of sync */
465 /* This gets the info regardless of the Pool */
466 bstrncpy(VolumeName, dev->VolHdr.VolumeName, sizeof(VolumeName));
467 if (autochanger && !dir_get_volume_info(this, GET_VOL_INFO_FOR_READ)) {
469 * If we get here, we know we cannot write on the Volume,
470 * and we know that we cannot read it either, so it
471 * is not in the autochanger.
473 mark_volume_not_inchanger();
475 dev->VolCatInfo = devVolCatInfo; /* structure assignment */
476 dev->set_unload(); /* unload this volume */
477 Jmsg(jcr, M_WARNING, 0, _("Director wanted Volume \"%s\".\n"
478 " Current Volume \"%s\" not acceptable because:\n"
480 dcrVolCatInfo.VolCatName, dev->VolHdr.VolumeName,
481 vol_info_msg.c_str());
483 /* Restore saved DCR before continuing */
484 bstrncpy(VolumeName, saveVolumeName, sizeof(VolumeName));
485 VolCatInfo = dcrVolCatInfo; /* structure assignment */
486 goto check_next_volume;
489 * This was not the volume we expected, but it is OK with
490 * the Director, so use it.
492 Dmsg1(150, "Got new Volume name=%s\n", VolumeName);
493 dev->VolCatInfo = VolCatInfo; /* structure assignment */
494 Dmsg1(100, "Call reserve_volume=%s\n", dev->VolHdr.VolumeName);
495 if (reserve_volume(this, dev->VolHdr.VolumeName) == NULL) {
496 if (!jcr->errmsg[0]) {
497 Jmsg3(jcr, M_WARNING, 0, _("Could not reserve volume %s on %s device %s\n"),
498 dev->VolHdr.VolumeName, dev->print_type(), dev->print_name());
500 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
503 dev->setVolCatInfo(false);
504 setVolCatInfo(false);
505 goto check_next_volume;
507 break; /* got a Volume */
509 * At this point, we assume we have a blank tape mounted.
513 Jmsg(jcr, M_FATAL, 0, "%s", jcr->errmsg);
514 mark_volume_in_error();
515 goto check_bail_out; /* we could not write on DVD */
517 /* Fall through wanted */
519 switch (try_autolabel(true)) {
521 goto check_next_volume;
523 goto check_read_volume;
529 /* NOTE! Fall-through wanted. */
532 Dmsg0(200, "VOL_NO_MEDIA or default.\n");
533 /* Send error message */
536 Dmsg1(200, "Msg suppressed by poll: %s\n", jcr->errmsg);
539 /* Needed, so the medium can be changed */
540 if (dev->requires_mount()) {
544 goto check_next_volume;
550 dev->setVolCatInfo(false);
551 setVolCatInfo(false);
553 return check_next_vol;
561 return check_read_vol;
566 bool DCR::is_suitable_volume_mounted()
570 /* Volume mounted? */
571 if (dev->VolHdr.VolumeName[0] == 0 || dev->swap_dev || dev->must_unload()) {
572 return false; /* no */
574 bstrncpy(VolumeName, dev->VolHdr.VolumeName, sizeof(VolumeName));
575 ok = dir_get_volume_info(this, GET_VOL_INFO_FOR_WRITE);
577 Dmsg1(40, "dir_get_volume_info failed: %s", jcr->errmsg);
583 bool DCR::do_unload()
585 if (dev->must_unload()) {
586 Dmsg1(100, "must_unload release %s\n", dev->print_name());
592 bool DCR::do_load(bool is_writing)
594 if (dev->must_load()) {
595 Dmsg1(100, "Must load dev=%s\n", dev->print_name());
596 if (autoload_device(this, is_writing, NULL) > 0) {
605 void DCR::do_swapping(bool is_writing)
608 * See if we are asked to swap the Volume from another device
609 * if so, unload the other device here, and attach the
610 * volume to our drive.
613 if (dev->swap_dev->must_unload()) {
615 dev->swap_dev->set_slot(dev->vol->get_slot());
617 Dmsg2(100, "Swap unloading slot=%d %s\n", dev->swap_dev->get_slot(),
618 dev->swap_dev->print_name());
619 unload_dev(this, dev->swap_dev);
622 dev->vol->clear_swapping();
623 Dmsg1(100, "=== set in_use vol=%s\n", dev->vol->vol_name);
624 dev->vol->clear_in_use();
625 dev->VolHdr.VolumeName[0] = 0; /* don't yet have right Volume */
627 Dmsg1(100, "No vol on dev=%s\n", dev->print_name());
629 if (dev->swap_dev->vol) {
630 Dmsg2(100, "Vol=%s on dev=%s\n", dev->swap_dev->vol->vol_name,
631 dev->swap_dev->print_name());
633 Dmsg2(100, "Set swap_dev=NULL for dev=%s swap_dev=%s\n",
634 dev->print_name(), dev->swap_dev->print_name());
635 dev->swap_dev = NULL;
638 Dmsg1(100, "No swap_dev set. dev->vol=%p\n", dev->vol);
640 Dmsg1(100, "No swap_dev set. dev->vol=%p\n", dev->vol);
647 * Check if the current position on the volume corresponds to
648 * what is in the catalog.
650 bool DCR::is_eod_valid()
653 char ed1[50], ed2[50];
654 if (dev->VolCatInfo.VolCatBytes == dev->part_start + dev->part_size) {
655 Jmsg(jcr, M_INFO, 0, _("Ready to append to end of Volume \"%s\""
656 " part=%d size=%s\n"), VolumeName,
657 dev->part, edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes,ed1));
659 Jmsg(jcr, M_ERROR, 0, _("Bacula cannot write on DVD Volume \"%s\" because: "
660 "The sizes do not match! Volume=%s Catalog=%s\n"),
662 edit_uint64_with_commas(dev->part_start + dev->part_size, ed1),
663 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ed2));
664 mark_volume_in_error();
667 } else if (dev->is_tape()) {
669 * Check if we are positioned on the tape at the same place
670 * that the database says we should be.
672 if (dev->VolCatInfo.VolCatFiles == dev->get_file()) {
673 Jmsg(jcr, M_INFO, 0, _("Ready to append to end of Volume \"%s\" at file=%d.\n"),
674 VolumeName, dev->get_file());
675 } else if (dev->get_file() > dev->VolCatInfo.VolCatFiles) {
676 Jmsg(jcr, M_WARNING, 0, _("For Volume \"%s\":\n"
677 "The number of files mismatch! Volume=%u Catalog=%u\n"
678 "Correcting Catalog\n"),
679 VolumeName, dev->get_file(), dev->VolCatInfo.VolCatFiles);
680 dev->VolCatInfo.VolCatFiles = dev->get_file();
681 dev->VolCatInfo.VolCatBlocks = dev->get_block_num();
682 if (!dir_update_volume_info(this, false, true)) {
683 Jmsg(jcr, M_WARNING, 0, _("Error updating Catalog\n"));
684 mark_volume_in_error();
688 Jmsg(jcr, M_ERROR, 0, _("Bacula cannot write on tape Volume \"%s\" because:\n"
689 "The number of files mismatch! Volume=%u Catalog=%u\n"),
690 VolumeName, dev->get_file(), dev->VolCatInfo.VolCatFiles);
691 mark_volume_in_error();
697 } else if (dev->is_file()) {
698 char ed1[50], ed2[50];
701 pos = dev->lseek(this, (boffset_t)0, SEEK_END);
702 if (dev->VolCatInfo.VolCatAmetaBytes == (uint64_t)pos) {
703 Jmsg(jcr, M_INFO, 0, _("Ready to append to end of Volume \"%s\""
704 " size=%s\n"), VolumeName,
705 edit_uint64_with_commas(dev->VolCatInfo.VolCatAmetaBytes, ed1));
706 } else if ((uint64_t)pos >= dev->VolCatInfo.VolCatAmetaBytes) {
707 if ((uint64_t)pos != dev->VolCatInfo.VolCatAmetaBytes) {
708 Jmsg(jcr, M_WARNING, 0, _("For Volume \"%s\":\n"
709 " The sizes do not match! Volume=%s Catalog=%s\n"
710 " Correcting Catalog\n"),
711 VolumeName, edit_uint64_with_commas(pos, ed1),
712 edit_uint64_with_commas(dev->VolCatInfo.VolCatAmetaBytes, ed2));
714 dev->VolCatInfo.VolCatAmetaBytes = pos;
715 dev->VolCatInfo.VolCatBytes = pos;
716 dev->VolCatInfo.VolCatFiles = (uint32_t)(pos >> 32);
717 if (!dir_update_volume_info(this, false, true)) {
718 Jmsg(jcr, M_WARNING, 0, _("Error updating Catalog\n"));
719 mark_volume_in_error();
723 Mmsg(jcr->errmsg, _("Bacula cannot write on disk Volume \"%s\" because: "
724 "The sizes do not match! Volume=%s Catalog=%s\n"),
726 edit_uint64_with_commas(pos, ed1),
727 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ed2));
728 Jmsg(jcr, M_ERROR, 0, jcr->errmsg);
729 Dmsg0(100, jcr->errmsg);
730 mark_volume_in_error();
739 * If permitted, we label the device, make sure we can do
740 * it by checking that the VolCatBytes is zero => not labeled,
741 * once the Volume is labeled we don't want to label another
742 * blank tape with the same name. For disk, we go ahead and
743 * label it anyway, because the OS insures that there is only
744 * one Volume with that name.
745 * As noted above, at this point dcr->VolCatInfo has what
746 * the Director wants and dev->VolCatInfo has info on the
747 * previous tape (or nothing).
750 * try_next_vol label failed, look for another volume
751 * try_read_vol labeled volume, now re-read the label
752 * try_error hard error (catalog update)
753 * try_default I couldn't do anything
755 int DCR::try_autolabel(bool opened)
759 if (dev->poll && !dev->is_tape()) {
760 Dmsg0(100, "No autolabel because polling.\n");
761 return try_default; /* if polling, don't try to create new labels */
763 /* For a tape require it to be opened and read before labeling */
764 if (!opened && (dev->is_tape() || dev->is_null())) {
767 if (dev->has_cap(CAP_LABEL) && (VolCatInfo.VolCatBytes == 0 ||
768 (!dev->is_tape() && strcmp(VolCatInfo.VolCatStatus,
770 Dmsg1(40, "Create new volume label vol=%s\n", VolumeName);
771 /* Create a new Volume label and write it to the device */
772 if (!write_new_volume_label_to_dev(dcr, VolumeName,
773 pool_name, false, /* no relabel */ false /* defer DVD label */)) {
774 Dmsg2(100, "write_vol_label failed. vol=%s, pool=%s\n",
775 VolumeName, pool_name);
777 mark_volume_in_error();
781 Dmsg0(150, "dir_update_vol_info. Set Append\n");
782 /* Copy Director's info into the device info */
783 dev->VolCatInfo = VolCatInfo; /* structure assignment */
784 if (!dir_update_volume_info(dcr, true, true)) { /* indicate tape labeled */
785 Dmsg3(100, "Update_vol_info failed no autolabel Volume \"%s\" on %s device %s.\n",
786 VolumeName, dev->print_type(), dev->print_name());
789 Jmsg(dcr->jcr, M_INFO, 0, _("Labeled new Volume \"%s\" on %s device %s.\n"),
790 VolumeName, dev->print_type(), dev->print_name());
791 Dmsg3(100, "Labeled new Volume \"%s\" on %s device %s.\n",
792 VolumeName, dev->print_type(), dev->print_name());
793 return try_read_vol; /* read label we just wrote */
795 Dmsg4(40, "=== Cannot autolabel: cap_label=%d VolCatBytes=%lld is_tape=%d VolCatStatus=%s\n",
796 dev->has_cap(CAP_LABEL), VolCatInfo.VolCatBytes, dev->is_tape(),
797 VolCatInfo.VolCatStatus);
799 if (!dev->has_cap(CAP_LABEL) && VolCatInfo.VolCatBytes == 0) {
800 Jmsg(jcr, M_WARNING, 0, _("%s device %s not configured to autolabel Volumes.\n"),
801 dev->print_type(), dev->print_name());
804 /* If not removable, Volume is broken */
805 if (!dev->is_removable()) {
806 Jmsg3(jcr, M_WARNING, 0, _("Volume \"%s\" not loaded on %s device %s.\n"),
807 VolumeName, dev->print_type(), dev->print_name());
808 Dmsg3(40, "Volume \"%s\" not loaded on %s device %s.\n",
809 VolumeName, dev->print_type(), dev->print_name());
811 mark_volume_in_error();
820 * Mark volume in error in catalog
822 void DCR::mark_volume_in_error()
824 Jmsg(jcr, M_INFO, 0, _("Marking Volume \"%s\" in Error in Catalog.\n"),
826 dev->VolCatInfo = VolCatInfo; /* structure assignment */
827 dev->setVolCatStatus("Error");
828 Dmsg0(150, "dir_update_vol_info. Set Error.\n");
829 dir_update_volume_info(this, false, false);
831 Dmsg0(50, "set_unload\n");
832 dev->set_unload(); /* must get a new volume */
836 * The Volume is not in the correct slot, so mark this
837 * Volume as not being in the Changer.
839 void DCR::mark_volume_not_inchanger()
841 Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
842 " Setting InChanger to zero in catalog.\n"),
843 getVolCatName(), VolCatInfo.Slot);
844 dev->VolCatInfo = VolCatInfo; /* structure assignment */
845 VolCatInfo.InChanger = false;
846 dev->VolCatInfo.InChanger = false;
847 Dmsg0(400, "update vol info in mount\n");
848 dir_update_volume_info(this, true, false); /* set new status */
852 * Either because we are going to hang a new volume, or because
853 * of explicit user request, we release the current volume.
855 void DCR::release_volume()
857 unload_autochanger(this, -1);
860 Jmsg0(jcr, M_ERROR, 0, _("Hey!!!!! WroteVol non-zero !!!!!\n"));
861 Pmsg0(190, "Hey!!!!! WroteVol non-zero !!!!!\n");
864 if (dev->is_open() && (!dev->is_tape() || !dev->has_cap(CAP_ALWAYSOPEN))) {
865 generate_plugin_event(jcr, bsdEventDeviceClose, this);
869 /* If we have not closed the device, then at least rewind the tape */
870 if (dev->is_open()) {
871 dev->offline_or_rewind();
875 * Erase all memory of the current volume
878 dev->block_num = dev->file = 0;
879 dev->EndBlock = dev->EndFile = 0;
880 memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
882 /* Force re-read of label */
883 dev->clear_labeled();
886 dev->label_type = B_BACULA_LABEL;
889 Dmsg0(190, "release_volume\n");
895 * Check to see if the tape position as defined by the OS is
896 * the same as our concept. If it is not,
897 * it means the user has probably manually rewound the tape.
898 * Note, we check only if num_writers == 0, but this code will
899 * also work fine for any number of writers. If num_writers > 0,
900 * we probably should cancel all jobs using this device, or
901 * perhaps even abort the SD, or at a minimum, mark the tape
902 * in error. Another strategy with num_writers == 0, would be
903 * to rewind the tape and do a new eod() request.
905 bool DCR::is_tape_position_ok()
907 if (dev->is_tape() && dev->num_writers == 0) {
908 int32_t file = dev->get_os_tape_file();
909 if (file >= 0 && file != (int32_t)dev->get_file()) {
910 Jmsg(jcr, M_ERROR, 0, _("Invalid tape position on volume \"%s\""
911 " on device %s. Expected %d, got %d\n"),
912 dev->VolHdr.VolumeName, dev->print_name(), dev->get_file(), file);
914 * If the current file is greater than zero, it means we probably
915 * have some bad count of EOF marks, so mark tape in error. Otherwise
916 * the operator might have moved the tape, so we just release it
920 mark_volume_in_error();
931 * If we are reading, we come here at the end of the tape
932 * and see if there are more volumes to be mounted.
934 bool mount_next_read_volume(DCR *dcr)
936 DEVICE *dev = dcr->dev;
938 Dmsg2(90, "NumReadVolumes=%d CurReadVolume=%d\n", jcr->NumReadVolumes, jcr->CurReadVolume);
940 volume_unused(dcr); /* release current volume */
942 * End Of Tape -- mount next Volume (if another specified)
944 if (jcr->NumReadVolumes > 1 && jcr->CurReadVolume < jcr->NumReadVolumes) {
948 dcr->set_reserved_for_read();
950 if (!acquire_device_for_read(dcr)) {
951 Jmsg3(jcr, M_FATAL, 0, _("Cannot open %s Dev=%s, Vol=%s for reading.\n"),
952 dev->print_type(), dev->print_name(), dcr->VolumeName);
953 jcr->setJobStatus(JS_FatalError); /* Jmsg is not working for *SystemJob* */
956 return true; /* next volume mounted */
958 Dmsg0(90, "End of Device reached.\n");