]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/mount.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / stored / mount.c
index 4d45f4e384c0466f9b9871766cf0e9acf1b8de12..fa7dd67f0f40c57233f4c8a80378fb3f14cbf6b5 100644 (file)
@@ -30,7 +30,8 @@
 #include "bacula.h"                   /* pull in global headers */
 #include "stored.h"                   /* pull in Storage Deamon headers */
 
-/* Forward referenced functions */
+/* Forward referenced routines */
+static void mark_volume_in_error(JCR *jcr, DEVICE *dev);
 
 
 /*
  * This routine returns a 0 only if it is REALLY
  *  impossible to get the requested Volume.
  *
- *    *****FIXME****** handle "Recycle" volume ******
  */
 int mount_next_write_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, int release)
 {
-   int retry = 0, autochanger;
-   bool ask, recycle;
+   int retry = 0;
+   bool ask = false, recycle, autochanger;
+   int vol_label_status;
 
    Dmsg0(100, "Enter mount_next_volume()\n");
 
+   /* 
+    * Attempt to mount the next volume. If something non-fatal goes
+    *  wrong, we come back here to re-try (new op messages, re-read
+    *  Volume, ...)
+    */
 mount_next_vol:
    if (retry++ > 5) {
       Jmsg(jcr, M_FATAL, 0, _("Too many errors trying to mount device %s.\n"), 
@@ -60,11 +66,11 @@ mount_next_vol:
       return 0;
    }
    if (job_canceled(jcr)) {
-      Jmsg(jcr, M_FATAL, 0, _("Job canceled.\n"));
+      Jmsg(jcr, M_FATAL, 0, _("Job %d canceled.\n"), jcr->JobId);
       return 0;
    }
-   autochanger = 0;
-   recycle = ask = false;
+   autochanger = false;               /* Assume no autochanger */
+   recycle = false;
    if (release) {
       Dmsg0(100, "mount_next_volume release=1\n");
       release_volume(jcr, dev);
@@ -73,10 +79,14 @@ mount_next_vol:
 
    /* 
     * Get Director's idea of what tape we should have mounted. 
+    *   in jcr->VolCatInfo
     */
-   if (!dir_find_next_appendable_volume(jcr) &&
-       !dir_ask_sysop_to_mount_next_volume(jcr, dev)) {
-      return 0;
+   Dmsg0(100, "Before dir_find_next\n");
+   if (!dir_find_next_appendable_volume(jcr)) {
+       Dmsg0(100, "not dir_find_next\n");
+       if (!dir_ask_sysop_to_mount_next_volume(jcr, dev)) {
+        return 0;
+       }
    }
    Dmsg2(100, "After find_next_append. Vol=%s Slot=%d\n",
         jcr->VolCatInfo.VolCatName, jcr->VolCatInfo.Slot);
@@ -94,142 +104,139 @@ mount_next_vol:
     * It assumes that the device is not already in use!
     *
     */
-
-   Dmsg0(100, "Enter ready_dev_for_append\n");
-
    dev->state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
 
-   for ( ;; ) {
-      int vol_label_status;
-      autochanger = autoload_device(jcr, dev, 1, NULL);
-      Dmsg1(100, "autoload_dev returns %d\n", autochanger);
+   autochanger = autoload_device(jcr, dev, 1, NULL);
+   Dmsg1(100, "autoload_dev returns %d\n", autochanger);
+   /*
+    * If we autochanged to correct Volume or (we have not just
+    *  released the Volume AND we can automount) we go ahead 
+    *  and read the label. If there is no tape in the drive,
+    *  we will err, recurse and ask the operator the next time.
+    */
+   if (autochanger || (!release && dev_is_tape(dev) && dev_cap(dev, CAP_AUTOMOUNT))) {
+      ask = false;                 /* don't ask SYSOP this time */
+   }
+   Dmsg2(100, "Ask=%d autochanger=%d\n", ask, autochanger);
+   release = true;                /* release next time if we "recurse" */
 
-      /*
-       * If we autochanged to correct Volume or (we have not just
-       *   released the Volume AND we can automount) we go ahead 
-       *   and read the label. If there is no tape in the drive,
-       *   we will err, recurse and ask the operator the next time.
-       */
-      if (autochanger || (!release && dev_is_tape(dev) && dev_cap(dev, CAP_AUTOMOUNT))) {
-         ask = false;                 /* don't ask SYSOP this time */
-      }
+   if (ask && !dir_ask_sysop_to_mount_next_volume(jcr, dev)) {
+      Dmsg0(100, "Error return ask_sysop ...\n");
+      return 0;             /* error return */
+   }
+   Dmsg1(100, "want vol=%s\n", jcr->VolumeName);
 
-      release = 1;                    /* release next time if we "recurse" */
+   /* Open device */
+   if  (!(dev_state(dev, ST_OPENED))) {
+       int mode;
+       if (dev_cap(dev, CAP_STREAM)) {
+         mode = OPEN_WRITE_ONLY;
+       } else {
+         mode = OPEN_READ_WRITE;
+       }
+       if (open_dev(dev, jcr->VolCatInfo.VolCatName, mode) < 0) {
+          Jmsg2(jcr, M_FATAL, 0, _("Unable to open device %s. ERR=%s\n"), 
+            dev_name(dev), strerror_dev(dev));
+         return 0;
+       }
+   }
 
-ask_again:
-      if (ask && !dir_ask_sysop_to_mount_next_volume(jcr, dev)) {
-         Dmsg0(100, "Error return ask_sysop ...\n");
-        return 0;              /* error return */
-      }
-      Dmsg1(100, "want vol=%s\n", jcr->VolumeName);
+   /*
+    * Now make sure we have the right tape mounted
+    */
+read_volume:
+   /* 
+    * If we are writing to a stream device, ASSUME the volume label
+    *  is correct.
+    */
+   if (dev_cap(dev, CAP_STREAM)) {
+      vol_label_status = VOL_OK;
+      create_volume_label(dev, jcr->VolumeName, "Default");
+      dev->VolHdr.LabelType = PRE_LABEL;
+   } else {
+      vol_label_status = read_dev_volume_label(jcr, dev, block);
+   }
 
-      /* Open device */
-      if  (!(dev->state & ST_OPENED)) {
-         int mode;
-         if (dev_cap(dev, CAP_STREAM)) {
-            mode = OPEN_WRITE_ONLY;
-         } else {
-            mode = OPEN_READ_WRITE;
-         }
-         if (open_dev(dev, jcr->VolCatInfo.VolCatName, mode) < 0) {
-             Jmsg2(jcr, M_FATAL, 0, _("Unable to open device %s. ERR=%s\n"), 
-               dev_name(dev), strerror_dev(dev));
-            return 0;
-         }
-      }
+   Dmsg2(100, "dirVol=%s dirStat=%s\n", jcr->VolumeName,
+      jcr->VolCatInfo.VolCatStatus);
+   /*
+    * At this point, dev->VolCatInfo has what is in the drive, if anything,
+    *         and   jcr->VolCatInfo has what the Director wants.
+    */
+   switch (vol_label_status) {
+   case VOL_OK:
+      Dmsg1(100, "Vol OK name=%s\n", jcr->VolumeName);
+      memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(jcr->VolCatInfo));
+      recycle = strcmp(dev->VolCatInfo.VolCatStatus, "Recycle") == 0;
+      break;                   /* got a Volume */
+   case VOL_NAME_ERROR:
+      VOLUME_CAT_INFO VolCatInfo;
 
-      /*
-       * Now make sure we have the right tape mounted
-       */
-read_volume:
+      Dmsg1(100, "Vol NAME Error Name=%s\n", jcr->VolumeName);
       /* 
-       * If we are writing to a stream device, ASSUME the volume label
-       *  is correct.
+       * OK, we got a different volume mounted. First save the
+       *  requested Volume info (jcr) structure, then query if
+       *  this volume is really OK. If not, put back the desired
+       *  volume name and continue.
        */
-      if (dev_cap(dev, CAP_STREAM)) {
-        vol_label_status = VOL_OK;
-         create_volume_label(dev, jcr->VolumeName, "Default");
-        dev->VolHdr.LabelType = PRE_LABEL;
-      } else {
-        vol_label_status = read_dev_volume_label(jcr, dev, block);
+      memcpy(&VolCatInfo, &jcr->VolCatInfo, sizeof(VolCatInfo));
+      /* Check if this is a valid Volume in the pool */
+      pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);                        
+      if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE)) {
+         Jmsg(jcr, M_WARNING, 0, _("Director wanted Volume \"%s\".\n"
+              "    Current Volume \"%s\" not acceptable because:\n"
+              "    %s"),
+            VolCatInfo.VolCatName, dev->VolHdr.VolName,
+            jcr->dir_bsock->msg);
+        /* Restore desired volume name, note device info out of sync */
+        memcpy(&jcr->VolCatInfo, &VolCatInfo, sizeof(jcr->VolCatInfo));
+        ask = true;
+        goto mount_next_vol;
       }
-      Dmsg2(100, "dirVol=%s dirStat=%s\n", jcr->VolumeName,
-        jcr->VolCatInfo.VolCatStatus);
-      /*
-       * At this point, dev->VolCatInfo has what is in the drive, if anything,
-       *         and   jcr->VolCatInfo has what the Director wants.
+      Dmsg1(100, "want new name=%s\n", jcr->VolumeName);
+      memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(dev->VolCatInfo));
+      recycle = strcmp(dev->VolCatInfo.VolCatStatus, "Recycle") == 0;
+      break;               /* got a Volume */
+   /*
+    * At this point, we assume we have a blank tape mounted.
+    */
+   case VOL_NO_LABEL:
+   case VOL_IO_ERROR:
+      /* 
+       * If permitted, we label the device, make sure we can do
+       *   it by checking that the VolCatBytes is zero => not labeled, 
+       *   once the Volume is labeled we don't want to label another
+       *   blank tape with the same name.  For disk, we go ahead and
+       *   label it anyway, because the OS insures that there is only
+       *   one Volume with that name.
+       * As noted above, at this point jcr->VolCatInfo has what
+       *   the Director wants and dev->VolCatInfo has info on the
+       *   previous tape (or nothing).
        */
-      switch (vol_label_status) {
-      case VOL_OK:
-         Dmsg1(100, "Vol OK name=%s\n", jcr->VolumeName);
-        memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(jcr->VolCatInfo));
-         recycle = strcmp(dev->VolCatInfo.VolCatStatus, "Recycle") == 0;
-        break;                    /* got a Volume */
-      case VOL_NAME_ERROR:
-        VOLUME_CAT_INFO VolCatInfo;
-
-         Dmsg1(100, "Vol NAME Error Name=%s\n", jcr->VolumeName);
-        /* 
-         * OK, we got a different volume mounted. First save the
-         *  requested Volume info (jcr) structure, then query if
-         *  this volume is really OK. If not, put back the desired
-         *  volume name and continue.
-         */
-        memcpy(&VolCatInfo, &jcr->VolCatInfo, sizeof(VolCatInfo));
-        /* Check if this is a valid Volume in the pool */
-        pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);                         
-        if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE)) {
-            Mmsg(&jcr->errmsg, _("Director wanted Volume \"%s\".\n"
-                 "    Current Volume \"%s\" not acceptable because:\n"
-                 "    %s"),
-               VolCatInfo.VolCatName, dev->VolHdr.VolName,
-               jcr->dir_bsock->msg);
-           /* Restore desired volume name, note device info out of sync */
-           memcpy(&jcr->VolCatInfo, &VolCatInfo, sizeof(jcr->VolCatInfo));
-           goto mount_error;
+      if (dev_cap(dev, CAP_LABEL) && (jcr->VolCatInfo.VolCatBytes == 0 ||
+           (!dev_is_tape(dev) && strcmp(jcr->VolCatInfo.VolCatStatus, 
+                                   "Recycle") == 0))) {
+         Dmsg0(100, "Create volume label\n");
+        if (!write_volume_label_to_dev(jcr, (DEVRES *)dev->device, jcr->VolumeName,
+               jcr->pool_name)) {
+            Dmsg0(100, "!write_vol_label\n");
+           goto mount_next_vol;
         }
-         Dmsg1(100, "want new name=%s\n", jcr->VolumeName);
+         Dmsg0(100, "dir_update_vol_info. Set Append\n");
+         /* Copy Director's info into the device info */
         memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(dev->VolCatInfo));
-         recycle = strcmp(dev->VolCatInfo.VolCatStatus, "Recycle") == 0;
-        break;                /* got a Volume */
-      /*
-       * At this point, we assume we have a blank tape mounted.
-       */
-      case VOL_NO_LABEL:
-      case VOL_IO_ERROR:
-        /* 
-         * If permitted, we label the device, make sure we can do
-         *   it by checking that the VolCatBytes is zero => not labeled. 
-         * As noted above, at this point jcr->VolCatInfo has what
-         *   the Director wants and dev->VolCatInfo has info on the
-         *   previous tape (or nothing).
-         */
-        if (dev_cap(dev, CAP_LABEL) && jcr->VolCatInfo.VolCatBytes == 0) {
-            Dmsg0(100, "Create volume label\n");
-           if (!write_volume_label_to_dev(jcr, (DEVRES *)dev->device, jcr->VolumeName,
-                  jcr->pool_name)) {
-               Dmsg0(100, "!write_vol_label\n");
-              goto mount_next_vol;
-           }
-            Dmsg0(200, "dir_update_vol_info. Set Append\n");
-            /* Copy Director's info into the device info */
-           memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(dev->VolCatInfo));
-           dir_update_volume_info(jcr, &dev->VolCatInfo, 1);  /* indicate tape labeled */
-            Jmsg(jcr, M_INFO, 0, _("Labeled new Volume \"%s\" on device %s.\n"),
-              jcr->VolumeName, dev_name(dev));
-           goto read_volume;      /* read label we just wrote */
-        } 
-        /* NOTE! Fall-through wanted. */
-      case VOL_NO_MEDIA:
-      default:
-mount_error:
-        /* Send error message */
-         Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);                         
-         Dmsg0(100, "Default\n");
-        ask = true;
-        goto ask_again;
-      }
-      break;
+        dir_update_volume_info(jcr, dev, 1);  /* indicate tape labeled */
+         Jmsg(jcr, M_INFO, 0, _("Labeled new Volume \"%s\" on device %s.\n"),
+           jcr->VolumeName, dev_name(dev));
+        goto read_volume;      /* read label we just wrote */
+      } 
+      /* NOTE! Fall-through wanted. */
+   case VOL_NO_MEDIA:
+   default:
+      /* Send error message */
+      Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);                         
+      ask = true;
+      goto mount_next_vol;
    }
 
    /* 
@@ -240,7 +247,7 @@ mount_error:
     *  VOL_LABEL.  We rewind and return the label (reconstructed)
     *  in the block so that in the case of a new tape, data can
     *  be appended just after the block label. If we are writing
-    *  an second volume, the calling routine will write the label
+    *  a second volume, the calling routine will write the label
     *  before writing the overflow block.
     *
     *  If the tape is marked as Recycle, we rewrite the label.
@@ -271,6 +278,13 @@ mount_error:
               dev_name(dev), strerror_dev(dev));
            goto mount_next_vol;
         }
+        /* 
+         * We do not return the label in the block, because if we are
+         *  running multiple simultaneous jobs, once we release the lock
+         *  some other thread may write his block over the label. So, 
+         *  we simply write it definitively now.
+         */
+#ifdef needed
         if (!rewind_dev(dev)) {
             Jmsg2(jcr, M_ERROR, 0, _("Unable to rewind device %s. ERR=%s\n"),
               dev_name(dev), strerror_dev(dev));
@@ -279,6 +293,7 @@ mount_error:
 
         /* Recreate a correct volume label and return it in the block */
         write_volume_label_to_block(jcr, dev, block);
+#endif
       }
       /* Set or reset Volume statistics */
       dev->VolCatInfo.VolCatJobs = 0;
@@ -296,9 +311,9 @@ mount_error:
         dev->VolCatInfo.VolCatWrites = 1;
         dev->VolCatInfo.VolCatReads = 1;
       }
-      Dmsg0(200, "dir_update_vol_info. Set Append\n");
+      Dmsg0(100, "dir_update_vol_info. Set Append\n");
       bstrncpy(dev->VolCatInfo.VolCatStatus, "Append", sizeof(dev->VolCatInfo.VolCatStatus));
-      dir_update_volume_info(jcr, &dev->VolCatInfo, 1);  /* indicate doing relabel */
+      dir_update_volume_info(jcr, dev, 1);  /* indicate doing relabel */
       if (recycle) {
          Jmsg(jcr, M_INFO, 0, _("Recycled volume \"%s\" on device %s, all previous data lost.\n"),
            jcr->VolumeName, dev_name(dev));
@@ -306,6 +321,10 @@ mount_error:
          Jmsg(jcr, M_INFO, 0, _("Wrote label to prelabeled Volume \"%s\" on device %s\n"),
            jcr->VolumeName, dev_name(dev));
       }
+      /*
+       * End writing real Volume label (from pre-labeled tape), or recycling
+       *  the volume.
+       */
 
    } else {
       /*
@@ -319,11 +338,7 @@ mount_error:
       if (!eod_dev(dev)) {
          Jmsg(jcr, M_ERROR, 0, _("Unable to position to end of data %s. ERR=%s\n"),
            dev_name(dev), strerror_dev(dev));
-         Jmsg(jcr, M_INFO, 0, _("Marking Volume \"%s\" in Error in Catalog.\n"),
-           jcr->VolumeName);
-         bstrncpy(dev->VolCatInfo.VolCatStatus, "Error", sizeof(dev->VolCatInfo.VolCatStatus));
-         Dmsg0(200, "dir_update_vol_info. Set Error.\n");
-        dir_update_volume_info(jcr, &dev->VolCatInfo, 0);
+        mark_volume_in_error(jcr, dev);
         goto mount_next_vol;
       }
       /* *****FIXME**** we should do some checking for files too */
@@ -339,15 +354,13 @@ mount_error:
             Jmsg(jcr, M_ERROR, 0, _("I canot write on this volume because:\n\
 The number of files mismatch! Volume=%u Catalog=%u\n"), 
                 dev_file(dev), dev->VolCatInfo.VolCatFiles);
-            bstrncpy(dev->VolCatInfo.VolCatStatus, "Error", sizeof(dev->VolCatInfo.VolCatStatus));
-            Dmsg0(200, "dir_update_vol_info. Set Error.\n");
-           dir_update_volume_info(jcr, &dev->VolCatInfo, 0);
+           mark_volume_in_error(jcr, dev);
            goto mount_next_vol;
         }
       }
       dev->VolCatInfo.VolCatMounts++;     /* Update mounts */
-      Dmsg1(200, "update volinfo mounts=%d\n", dev->VolCatInfo.VolCatMounts);
-      dir_update_volume_info(jcr, &dev->VolCatInfo, 0);
+      Dmsg1(100, "update volinfo mounts=%d\n", dev->VolCatInfo.VolCatMounts);
+      dir_update_volume_info(jcr, dev, 0);
       /* Return an empty block */
       empty_block(block);            /* we used it for reading so set for write */
    }
@@ -356,7 +369,19 @@ The number of files mismatch! Volume=%u Catalog=%u\n"),
    return 1; 
 }
 
+static void mark_volume_in_error(JCR *jcr, DEVICE *dev)
+{
+   Jmsg(jcr, M_INFO, 0, _("Marking Volume \"%s\" in Error in Catalog.\n"),
+       jcr->VolumeName);
+   bstrncpy(dev->VolCatInfo.VolCatStatus, "Error", sizeof(dev->VolCatInfo.VolCatStatus));
+   Dmsg0(100, "dir_update_vol_info. Set Error.\n");
+   dir_update_volume_info(jcr, dev, 0);
+}
 
+/* 
+ * If we are reading, we come here at the end of the tape
+ *  and see if there are more volumes to be mounted.
+ */
 int mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
 {
    Dmsg2(90, "NumVolumes=%d CurVolume=%d\n", jcr->NumVolumes, jcr->CurVolume);
@@ -383,7 +408,6 @@ int mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
  */
 void release_volume(JCR *jcr, DEVICE *dev)
 {
-
    if (jcr->WroteVol) {
       Jmsg0(jcr, M_ERROR, 0, "Hey!!!!! WroteVol non-zero !!!!!\n");
    }
@@ -395,7 +419,8 @@ void release_volume(JCR *jcr, DEVICE *dev)
    memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
    memset(&jcr->VolCatInfo, 0, sizeof(jcr->VolCatInfo));
    memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
-   dev->state &= ~ST_LABEL;       /* label not yet read */
+   /* Force re-read of label */
+   dev->state &= ~(ST_LABEL|ST_READ|ST_APPEND);
    jcr->VolumeName[0] = 0;
 
    if ((dev->state & ST_OPENED) &&