]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/acquire.c
- Landon merged his data encription changes into the HEAD
[bacula/bacula] / bacula / src / stored / acquire.c
index 6776086724041192dafdcff5e8204414a16ccec1..e0ce9c5be3ff54ebcd71d16e3877ac96453a54f1 100644 (file)
  */
 DCR *new_dcr(JCR *jcr, DEVICE *dev)
 {
-   if (jcr && jcr->dcr) {
-      return jcr->dcr;
-   }
    DCR *dcr = (DCR *)malloc(sizeof(DCR));
    memset(dcr, 0, sizeof(DCR));
-   if (jcr) {
-      jcr->dcr = dcr;
-   }
    dcr->jcr = jcr;
-   dcr->dev = dev;
    if (dev) {
+      dcr->dev = dev;
       dcr->device = dev->device;
+      dcr->block = new_block(dev);
+      dcr->rec = new_record();
+      dcr->max_job_spool_size = dev->device->max_job_spool_size;
+      /* Attach this dcr only if dev is initialized */
+      if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
+         dev->attached_dcrs->append(dcr);  /* attach dcr to device */
+//       jcr->dcrs->append(dcr);         /* put dcr in list for Job */
+      }
    }
-   dcr->block = new_block(dev);
-   dcr->rec = new_record();
    dcr->spool_fd = -1;
-   dcr->max_job_spool_size = dev->device->max_job_spool_size;
-   /* Attach this dcr only if dev is initialized */
-   if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
-      dev->attached_dcrs->append(dcr);  /* attach dcr to device */
-//    jcr->dcrs->append(dcr);         /* put dcr in list for Job */
-   }
    return dcr;
 }
 
@@ -187,31 +181,19 @@ DCR *acquire_device_for_read(DCR *dcr)
        * reading. If it is a file, it opens it.
        * If it is a tape, it checks the volume name
        */
-      for ( ; !dev->is_open(); ) {
-         Dmsg1(100, "bstored: open vol=%s\n", dcr->VolumeName);
-         if (dev->open(dcr, OPEN_READ_ONLY) < 0) {
-            if (dev->dev_errno == EIO) {   /* no tape loaded */
-              Jmsg3(jcr, M_WARNING, 0, _("Open device %s Volume \"%s\" failed (EIO): ERR=%s\n"),
-                    dev->print_name(), dcr->VolumeName, strerror_dev(dev));
-               goto default_path;
-            }
-            
-#ifdef xxx_needed
-            /* If we have a dvd that requires mount, 
-             * we need to try to open the label, so the info can be reported
-             * if a wrong volume has been mounted.   
-             */
-            if (dev->is_dvd() && (dcr->VolCatInfo.VolCatParts > 0)) {
-               break;
-            }  
-#endif
-            
-            Jmsg3(jcr, M_FATAL, 0, _("Open device %s Volume \"%s\" failed: ERR=%s\n"),
-                dev->print_name(), dcr->VolumeName, strerror_dev(dev));
-            goto get_out;
+      Dmsg1(100, "bstored: open vol=%s\n", dcr->VolumeName);
+      if (dev->open(dcr, OPEN_READ_ONLY) < 0) {
+         if (dev->dev_errno == EIO) {   /* no tape loaded */
+           Jmsg3(jcr, M_WARNING, 0, _("Open device %s Volume \"%s\" failed (EIO): ERR=%s\n"),
+                 dev->print_name(), dcr->VolumeName, strerror_dev(dev));
+            goto default_path;
          }
-         Dmsg1(100, "opened dev %s OK\n", dev->print_name());
+         
+         Jmsg3(jcr, M_FATAL, 0, _("Open device %s Volume \"%s\" failed: ERR=%s\n"),
+             dev->print_name(), dcr->VolumeName, strerror_dev(dev));
+         goto get_out;
       }
+      Dmsg1(100, "opened dev %s OK\n", dev->print_name());
       
       /* Read Volume Label */
       
@@ -287,7 +269,6 @@ default_path:
 get_out:
    dev->unblock();
    if (!vol_ok) {
-      free_dcr(dcr);
       dcr = NULL;
    }
    return dcr;
@@ -328,6 +309,7 @@ DCR *acquire_device_for_append(DCR *dcr)
     */
    if (dev->can_read()) {
       Jmsg1(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev->print_name());
+      Dmsg1(200, "Device %s is busy reading.\n", dev->print_name());
       goto get_out;
    }
 
@@ -355,8 +337,11 @@ DCR *acquire_device_for_append(DCR *dcr)
          if (dcr->VolumeName[0]) {
             free_unused_volume(dcr);
          }
-         if (dev->num_writers != 0 || dev->reserved_device) {
-            Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
+         if (dev->num_writers != 0) {
+            Jmsg3(jcr, M_FATAL, 0, _("Wanted Volume \"%s\", but device %s is busy writing on \"%s\" .\n"), 
+                 dcr->VolumeName, dev->print_name(), dev->VolHdr.VolumeName);
+            Dmsg3(200, "Wanted Volume \"%s\", but device %s is busy writing on \"%s\" .\n",  
+                 dcr->VolumeName, dev->print_name(), dev->VolHdr.VolumeName);
             goto get_out;
          }
          /* Wrong tape mounted, release it, then fall through to get correct one */
@@ -396,6 +381,8 @@ DCR *acquire_device_for_append(DCR *dcr)
             /* Reduce "noise" -- don't print if job canceled */
             Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
                dev->print_name());
+            Dmsg1(200, "Could not ready device %s for append.\n", 
+               dev->print_name());
          }
          goto get_out;
       }
@@ -429,6 +416,7 @@ bool release_device(DCR *dcr)
    JCR *jcr = dcr->jcr;
    DEVICE *dev = dcr->dev;
    bool ok = true;
+   bool was_reading = false;
 
    lock_device(dev);
    Dmsg1(100, "release_device device is %s\n", dev->is_tape()?"tape":"disk");
@@ -442,6 +430,7 @@ bool release_device(DCR *dcr)
 
    if (dev->can_read()) {
       dev->clear_read();              /* clear read bit */
+      was_reading = true;
 
       /******FIXME**** send read volume usage statistics to director */
 
@@ -516,7 +505,11 @@ bool release_device(DCR *dcr)
    }
    unlock_device(dev);
    free_dcr(dcr);
-   jcr->dcr = NULL;
+   if (was_reading) {
+      jcr->read_dcr = NULL;
+   } else {
+      jcr->dcr = NULL;
+   }
    pthread_cond_broadcast(&wait_device_release);
    return ok;
 }