src/stored/dircmd.c
static void label_volume_if_ok(DCR *dcr, char *oldname,
                               char *newname, char *poolname,
                               int slot, int relabel)
{
   BSOCK *dir = dcr->jcr->dir_bsock;
   bsteal_lock_t hold;
   DEVICE *dev = dcr->dev;
   int label_status;
   int mode;
   const char *volname = (relabel == 1) ? oldname : newname;
   uint64_t volCatBytes;

   if (!obtain_device_block(dev,
                            &hold,
                            1,     /* one try */
                            BST_WRITING_LABEL)) {
      send_dir_busy_message(dir, dev);
      return;
   }
   dev->Unlock();

   Dmsg1(100, "Stole device %s lock, writing label.\n", dev->print_name());

   Dmsg0(90, "try_autoload_device - looking for volume_info\n");
   if (!try_autoload_device(dcr->jcr, dcr, slot, volname)) {
      goto bail_out;                  /* error */
   }


   if (relabel) {
      dev->truncating = true;         /* let open_device() know we will truncate it */
   }
   /* Set old volume name for open if relabeling */
   dcr->setVolCatName(volname);

   /* Ensure that the device is open -- autoload_device() closes it */
   if (dev->is_tape()) {
      mode = OPEN_READ_WRITE;
   } else {
      mode = CREATE_READ_WRITE;
   }
   if (!dev->open_device(dcr, mode)) {
      dir->fsend(_("3929 Unable to open device \"%s\": ERR=%s\n"),
         dev->print_name(), dev->bstrerror());
      goto bail_out;
   }

   /* See what we have for a Volume */
   label_status = dev->read_dev_volume_label(dcr);

   /* Set new volume name */
   dcr->setVolCatName(newname);
   switch(label_status) {
   case VOL_NAME_ERROR:
   case VOL_VERSION_ERROR:
   case VOL_LABEL_ERROR:
   case VOL_OK:
      if (!relabel) {
         dir->fsend(_(
            "3920 Cannot label Volume because it is already labeled: \"%s\"\n"),
             dev->VolHdr.VolumeName);
         break;
      }

      /* Relabel request. If oldname matches, continue */
      if (strcmp(oldname, dev->VolHdr.VolumeName) != 0) {
         dir->fsend(_("3921 Wrong volume mounted.\n"));
         break;
      }
      if (dev->label_type != B_BACULA_LABEL) {
         dir->fsend(_("3922 Cannot relabel an ANSI/IBM labeled Volume.\n"));
         break;
      }
      /* Fall through wanted! */
   case VOL_IO_ERROR:
   case VOL_NO_LABEL:
      if (!dev->write_volume_label(dcr, newname, poolname,
              relabel, true /* write label now */)) {
         dir->fsend(_("3912 Failed to label Volume %s: ERR=%s\n"),
            newname, dcr->jcr->errmsg);
         break;
      }
      volCatBytes = dev->VolCatInfo.VolCatBytes;
      /*
      * After writing label, create a new part
      */
      if (dev->is_cloud()) {
         dev->set_append();
         if (!dev->open_next_part(dcr)) {
            dir->fsend(_("3913 Failed to open next part: ERR=%s\n"), dcr->jcr->errmsg);
            break;
         }
      }
      bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
      /* The following 3000 OK label. string is scanned in ua_label.c */
      int type;
      if (dev->dev_type == B_FILE_DEV || dev->dev_type == B_ALIGNED_DEV ||
          dev->dev_type == B_CLOUD_DEV)
      {
         type = dev->dev_type;
      } else {
         type = 0;
      }
      dir->fsend("3000 OK label. VolBytes=%lld VolABytes=%lld VolType=%d Volume=\"%s\" Device=%s\n",
                 volCatBytes, dev->VolCatInfo.VolCatAdataBytes,
                 type, newname, dev->print_name());
      break;
   case VOL_TYPE_ERROR:
      dir->fsend(_("3917 Failed to label Volume: ERR=%s\n"), dcr->jcr->errmsg);
      break;
   case VOL_NO_MEDIA:
      dir->fsend(_("3918 Failed to label Volume (no media): ERR=%s\n"), dcr->jcr->errmsg);
      break;
   default:
      dir->fsend(_("3919 Cannot label Volume. "
                   "Unknown status %d from read_volume_label()\n"), label_status);
      break;
   }

bail_out:
   if (dev->is_open() && !dev->has_cap(CAP_ALWAYSOPEN)) {
      dev->close(dcr);
   }

   dev->end_of_job(dcr);

   if (!dev->is_open()) {
      dev->clear_volhdr();
   }
   volume_unused(dcr);                   /* no longer using volume */
   dev->Lock();
   give_back_device_block(dev, &hold);
   return;
}
/*
 * Read the tape label and determine if we can safely
 * label the tape (not a Bacula volume), then label it.
 *
 *  Enter with the mutex set
 */
Read the tape label and determine if we can safely
label the tape (not a Bacula volume), then label it.

Enter with the mutex set
Read the tape label and determine if we can safely label the tape ( not a Bacula volume ) then label it . Enter with the mutex set
static void label_volume_if_ok(DCR *dcr, char *oldname,
                               char *newname, char *poolname,
                               int slot, int relabel)
{
   BSOCK *dir = dcr->jcr->dir_bsock;
   bsteal_lock_t hold;
   DEVICE *dev = dcr->dev;
   int label_status;
   int mode;
   const char *volname = (relabel == 1) ? oldname : newname;
   uint64_t volCatBytes;
   if (!obtain_device_block(dev,
                            &hold,
                            1,     
                            BST_WRITING_LABEL)) {
      send_dir_busy_message(dir, dev);
      return;
   }
   dev->Unlock();
   Dmsg1(100, "Stole device %s lock, writing label.\n", dev->print_name());
   Dmsg0(90, "try_autoload_device - looking for volume_info\n");
   if (!try_autoload_device(dcr->jcr, dcr, slot, volname)) {
      goto bail_out;                  
   }
   if (relabel) {
      dev->truncating = true;         
   }
   dcr->setVolCatName(volname);
   if (dev->is_tape()) {
      mode = OPEN_READ_WRITE;
   } else {
      mode = CREATE_READ_WRITE;
   }
   if (!dev->open_device(dcr, mode)) {
      dir->fsend(_("3929 Unable to open device \"%s\": ERR=%s\n"),
         dev->print_name(), dev->bstrerror());
      goto bail_out;
   }
   label_status = dev->read_dev_volume_label(dcr);
   dcr->setVolCatName(newname);
   switch(label_status) {
   case VOL_NAME_ERROR:
   case VOL_VERSION_ERROR:
   case VOL_LABEL_ERROR:
   case VOL_OK:
      if (!relabel) {
         dir->fsend(_(
            "3920 Cannot label Volume because it is already labeled: \"%s\"\n"),
             dev->VolHdr.VolumeName);
         break;
      }
      if (strcmp(oldname, dev->VolHdr.VolumeName) != 0) {
         dir->fsend(_("3921 Wrong volume mounted.\n"));
         break;
      }
      if (dev->label_type != B_BACULA_LABEL) {
         dir->fsend(_("3922 Cannot relabel an ANSI/IBM labeled Volume.\n"));
         break;
      }
   case VOL_IO_ERROR:
   case VOL_NO_LABEL:
      if (!dev->write_volume_label(dcr, newname, poolname,
              relabel, true )) {
         dir->fsend(_("3912 Failed to label Volume %s: ERR=%s\n"),
            newname, dcr->jcr->errmsg);
         break;
      }
      volCatBytes = dev->VolCatInfo.VolCatBytes;
      if (dev->is_cloud()) {
         dev->set_append();
         if (!dev->open_next_part(dcr)) {
            dir->fsend(_("3913 Failed to open next part: ERR=%s\n"), dcr->jcr->errmsg);
            break;
         }
      }
      bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
      int type;
      if (dev->dev_type == B_FILE_DEV || dev->dev_type == B_ALIGNED_DEV ||
          dev->dev_type == B_CLOUD_DEV)
      {
         type = dev->dev_type;
      } else {
         type = 0;
      }
      dir->fsend("3000 OK label. VolBytes=%lld VolABytes=%lld VolType=%d Volume=\"%s\" Device=%s\n",
                 volCatBytes, dev->VolCatInfo.VolCatAdataBytes,
                 type, newname, dev->print_name());
      break;
   case VOL_TYPE_ERROR:
      dir->fsend(_("3917 Failed to label Volume: ERR=%s\n"), dcr->jcr->errmsg);
      break;
   case VOL_NO_MEDIA:
      dir->fsend(_("3918 Failed to label Volume (no media): ERR=%s\n"), dcr->jcr->errmsg);
      break;
   default:
      dir->fsend(_("3919 Cannot label Volume. "
                   "Unknown status %d from read_volume_label()\n"), label_status);
      break;
   }
bail_out:
   if (dev->is_open() && !dev->has_cap(CAP_ALWAYSOPEN)) {
      dev->close(dcr);
   }
   dev->end_of_job(dcr);
   if (!dev->is_open()) {
      dev->clear_volhdr();
   }
   volume_unused(dcr);                   
   dev->Lock();
   give_back_device_block(dev, &hold);
   return;
}
static void label_volume_if_ok ( DCR * dcr , char * oldname , char * newname , char * poolname , int slot , int relabel ) { BSOCK * dir = dcr -> jcr -> dir_bsock ; bsteal_lock_t hold ; DEVICE * dev = dcr -> dev ; int label_status ; int mode ; const char * volname = ( relabel == 1 ) ? oldname : newname ; uint64_t volCatBytes ; if ( ! obtain_device_block ( dev , & hold , 1 , BST_WRITING_LABEL ) ) { send_dir_busy_message ( dir , dev ) ; return ; } dev -> Unlock ( ) ; Dmsg1 ( 100 , " \n " , dev -> print_name ( ) ) ; Dmsg0 ( 90 , " \n " ) ; if ( ! try_autoload_device ( dcr -> jcr , dcr , slot , volname ) ) { goto bail_out ; } if ( relabel ) { dev -> truncating = true ; } dcr -> setVolCatName ( volname ) ; if ( dev -> is_tape ( ) ) { mode = OPEN_READ_WRITE ; } else { mode = CREATE_READ_WRITE ; } if ( ! dev -> open_device ( dcr , mode ) ) { dir -> fsend ( _ ( " \" \" \n " ) , dev -> print_name ( ) , dev -> bstrerror ( ) ) ; goto bail_out ; } label_status = dev -> read_dev_volume_label ( dcr ) ; dcr -> setVolCatName ( newname ) ; switch ( label_status ) { case VOL_NAME_ERROR : case VOL_VERSION_ERROR : case VOL_LABEL_ERROR : case VOL_OK : if ( ! relabel ) { dir -> fsend ( _ ( " \" \" \n " ) , dev -> VolHdr . VolumeName ) ; break ; } if ( strcmp ( oldname , dev -> VolHdr . VolumeName ) != 0 ) { dir -> fsend ( _ ( " \n " ) ) ; break ; } if ( dev -> label_type != B_BACULA_LABEL ) { dir -> fsend ( _ ( " \n " ) ) ; break ; } case VOL_IO_ERROR : case VOL_NO_LABEL : if ( ! dev -> write_volume_label ( dcr , newname , poolname , relabel , true ) ) { dir -> fsend ( _ ( " \n " ) , newname , dcr -> jcr -> errmsg ) ; break ; } volCatBytes = dev -> VolCatInfo . VolCatBytes ; if ( dev -> is_cloud ( ) ) { dev -> set_append ( ) ; if ( ! dev -> open_next_part ( dcr ) ) { dir -> fsend ( _ ( " \n " ) , dcr -> jcr -> errmsg ) ; break ; } } bstrncpy ( dcr -> VolumeName , newname , sizeof ( dcr -> VolumeName ) ) ; int type ; if ( dev -> dev_type == B_FILE_DEV || dev -> dev_type == B_ALIGNED_DEV || dev -> dev_type == B_CLOUD_DEV ) { type = dev -> dev_type ; } else { type = 0 ; } dir -> fsend ( " \" \" \n " , volCatBytes , dev -> VolCatInfo . VolCatAdataBytes , type , newname , dev -> print_name ( ) ) ; break ; case VOL_TYPE_ERROR : dir -> fsend ( _ ( " \n " ) , dcr -> jcr -> errmsg ) ; break ; case VOL_NO_MEDIA : dir -> fsend ( _ ( " \n " ) , dcr -> jcr -> errmsg ) ; break ; default : dir -> fsend ( _ ( " " " \n " ) , label_status ) ; break ; } bail_out : if ( dev -> is_open ( ) && ! dev -> has_cap ( CAP_ALWAYSOPEN ) ) { dev -> close ( dcr ) ; } dev -> end_of_job ( dcr ) ; if ( ! dev -> is_open ( ) ) { dev -> clear_volhdr ( ) ; } volume_unused ( dcr ) ; dev -> Lock ( ) ; give_back_device_block ( dev , & hold ) ; return ; }
Read the tape label and determine if we can safely
 label the tape (not a Bacula volume), then label it.
Read the tape label and determine if we can safely label the tape ( not a Bacula volume ) then label it .
/* one try */ /* error */ /* let open_device() know we will truncate it */ /* Set old volume name for open if relabeling */ /* Ensure that the device is open -- autoload_device() closes it */ /* See what we have for a Volume */ /* Set new volume name */ /* Relabel request. If oldname matches, continue */ /* Fall through wanted! */ /* write label now */ /*
      * After writing label, create a new part
      */ /* The following 3000 OK label. string is scanned in ua_label.c */ /* no longer using volume */