]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/acquire.c
kes Update projects file for next vote.
[bacula/bacula] / bacula / src / stored / acquire.c
index edb5f7002ed38fcc2ebcf39d860d1578329cd22f..7d423dc959bd2a372ceed803a272b1665017a28f 100644 (file)
@@ -6,25 +6,38 @@
  *   Version $Id$
  */
 /*
-   Copyright (C) 2002-2006 Kern Sibbald
+   Bacula® - The Network Backup Solution
 
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License
-   version 2 as amended with additional clauses defined in the
-   file LICENSE in the main source directory.
+   Copyright (C) 2002-2006 Free Software Foundation Europe e.V.
 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
-   the file LICENSE for additional details.
+   The main author of Bacula is Kern Sibbald, with contributions from
+   many others, a complete list can be found in the file AUTHORS.
+   This program is Free Software; you can redistribute it and/or
+   modify it under the terms of version two of the GNU General Public
+   License as published by the Free Software Foundation plus additions
+   that are listed in the file LICENSE.
 
- */
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
+
+   Bacula® is a registered trademark of John Walker.
+   The licensor of Bacula is the Free Software Foundation Europe
+   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
+   Switzerland, email:ftf@fsfeurope.org.
+*/
 
 #include "bacula.h"                   /* pull in global headers */
 #include "stored.h"                   /* pull in Storage Deamon headers */
 
 /* Forward referenced functions */
-static void detach_dcr_from_dev(DCR *dcr);
+static void attach_dcr_to_dev(DCR *dcr);
 
 
 /*********************************************************************
@@ -61,14 +74,18 @@ bool acquire_device_for_read(DCR *dcr)
    /* Find next Volume, if any */
    vol = jcr->VolList;
    if (!vol) {
-      Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
+      char ed1[50];
+      Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %s canceled.\n"), 
+         edit_int64(jcr->JobId, ed1));
       goto get_out;
    }
-   jcr->CurVolume++;
-   for (i=1; i<jcr->CurVolume; i++) {
+   jcr->CurReadVolume++;
+   for (i=1; i<jcr->CurReadVolume; i++) {
       vol = vol->next;
    }
    if (!vol) {
+      Jmsg(jcr, M_FATAL, 0, _("Logic error: no next volume. Numvol=%d Curvol=%d\n"),
+         jcr->NumReadVolumes, jcr->CurReadVolume);
       goto get_out;                   /* should not happen */   
    }
    bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
@@ -120,9 +137,7 @@ bool acquire_device_for_read(DCR *dcr)
          jcr->read_dcr = dcr; 
          dcr->device = new_dcr->device;
          dcr->max_job_spool_size = dcr->device->max_job_spool_size;
-         if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
-            dev->attached_dcrs->append(dcr);  /* attach dcr to device */
-         }
+         attach_dcr_to_dev(dcr);
          new_dcr->VolumeName[0] = 0;
          free_dcr(new_dcr);
          dev->block(BST_DOING_ACQUIRE); 
@@ -134,7 +149,7 @@ bool acquire_device_for_read(DCR *dcr)
          bstrncpy(dcr->pool_name, store->pool_name, sizeof(dcr->pool_name));
          bstrncpy(dcr->pool_type, store->pool_type, sizeof(dcr->pool_type));
       } else if (stat == 0) {   /* device busy */
-         Dmsg1(000, "Device %s is busy.\n", vol->device);
+         Pmsg1(000, "Device %s is busy.\n", vol->device);
       } else {
          /* error */
          Jmsg1(jcr, M_FATAL, 0, _("No suitable device found to read Volume \"%s\"\n"),
@@ -166,9 +181,14 @@ bool acquire_device_for_read(DCR *dcr)
       }
       dev->clear_labeled();              /* force reread of label */
       if (job_canceled(jcr)) {
-         Mmsg1(dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
+         char ed1[50];
+         Mmsg1(dev->errmsg, _("Job %s canceled.\n"), edit_int64(jcr->JobId, ed1));
+         Jmsg(jcr, M_INFO, 0, dev->errmsg);
          goto get_out;                /* error return */
       }
+
+      autoload_device(dcr, 0, NULL);
+
       /*
        * This code ensures that the device is ready for
        * reading. If it is a file, it opens it.
@@ -176,15 +196,9 @@ bool acquire_device_for_read(DCR *dcr)
        */
       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, _("Read open device %s Volume \"%s\" failed (EIO): ERR=%s\n"),
-                 dev->print_name(), dcr->VolumeName, dev->bstrerror());
-            goto default_path;
-         }
-         
-         Jmsg3(jcr, M_FATAL, 0, _("Read open device %s Volume \"%s\" failed: ERR=%s\n"),
-             dev->print_name(), dcr->VolumeName, dev->bstrerror());
-         goto get_out;
+        Jmsg3(jcr, M_WARNING, 0, _("Read open device %s Volume \"%s\" failed: ERR=%s\n"),
+              dev->print_name(), dcr->VolumeName, dev->bstrerror());
+         goto default_path;
       }
       Dmsg1(100, "opened dev %s OK\n", dev->print_name());
       
@@ -385,11 +399,12 @@ DCR *acquire_device_for_append(DCR *dcr)
          }
          goto get_out;
       }
+      Dmsg2(190, "Output pos=%u:%u\n", dcr->dev->file, dcr->dev->block_num);
    }
 
    dev->num_writers++;                /* we are now a writer */
-   if (jcr->NumVolumes == 0) {
-      jcr->NumVolumes = 1;
+   if (jcr->NumWriteVolumes == 0) {
+      jcr->NumWriteVolumes = 1;
    }
    P(dev->mutex);
    if (dcr->reserved_device) {
@@ -421,6 +436,9 @@ get_out:
  * This job is done, so release the device. From a Unix standpoint,
  *  the device remains open.
  *
+ * Note, if we are spooling, we may enter with the device locked.
+ * However, in all cases, unlock the device when leaving.
+ *
  */
 bool release_device(DCR *dcr)
 {
@@ -428,7 +446,10 @@ bool release_device(DCR *dcr)
    DEVICE *dev = dcr->dev;
    bool ok = true;
 
-   lock_device(dev);
+   /* lock only if not already locked by this thread */
+   if (!dcr->dev_locked) {
+      lock_device(dev);
+   }
    Dmsg2(100, "release_device device %s is %s\n", dev->print_name(), dev->is_tape()?"tape":"disk");
 
    /* if device is reserved, job never started, so release the reserve here */
@@ -459,8 +480,8 @@ bool release_device(DCR *dcr)
                dcr->VolCatInfo.VolCatName, jcr->Job);
          }
          /* If no more writers, write an EOF */
-         if (!dev->num_writers && dev->can_write()) {
-            weof_dev(dev, 1);
+         if (!dev->num_writers && dev->can_write() && dev->block_num > 0) {
+            dev->weof(1);
             write_ansi_ibm_labels(dcr, ANSI_EOF_LABEL, dev->VolHdr.VolumeName);
          }
          if (!dev->at_weot()) {
@@ -481,7 +502,8 @@ bool release_device(DCR *dcr)
    }
 
    /* If no writers, close if file or !CAP_ALWAYS_OPEN */
-   if (dev->num_writers == 0 && (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN))) {
+   if (dev->num_writers == 0 && (!dev->is_tape() || !dev->has_cap(CAP_ALWAYSOPEN))) {
+      dvd_remove_empty_part(dcr);        /* get rid of any empty spool part */
       dev->close();
    }
 
@@ -511,6 +533,7 @@ bool release_device(DCR *dcr)
       Dmsg1(400, "alert status=%d\n", status);
       free_pool_memory(alert);
    }
+   dcr->dev_locked = false;              /* set no longer locked */
    unlock_device(dev);
    if (jcr->read_dcr == dcr) {
       jcr->read_dcr = NULL;
@@ -537,11 +560,7 @@ DCR *new_dcr(JCR *jcr, DEVICE *dev)
       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 */
-      }
+      attach_dcr_to_dev(dcr);
    }
    dcr->spool_fd = -1;
    return dcr;
@@ -573,7 +592,18 @@ static void remove_dcr_from_dcrs(DCR *dcr)
 }
 #endif
 
-static void detach_dcr_from_dev(DCR *dcr)
+static void attach_dcr_to_dev(DCR *dcr)
+{
+   DEVICE *dev = dcr->dev;
+   JCR *jcr = dcr->jcr;
+
+   if (!dcr->attached_to_dev && dev->is_open() && jcr && jcr->JobType != JT_SYSTEM) {
+      dev->attached_dcrs->append(dcr);  /* attach dcr to device */
+      dcr->attached_to_dev = true;
+   }
+}
+
+void detach_dcr_from_dev(DCR *dcr)
 {
    DEVICE *dev = dcr->dev;
 
@@ -594,9 +624,10 @@ static void detach_dcr_from_dev(DCR *dcr)
       unlock_device(dev);
    }
 
-   /* Detach this dcr only if the dev is initialized */
-   if (dcr->dev->fd != 0 && dcr->jcr && dcr->jcr->JobType != JT_SYSTEM) {
+   /* Detach this dcr only if attached */
+   if (dcr->attached_to_dev) {
       dcr->dev->attached_dcrs->remove(dcr);  /* detach dcr from device */
+      dcr->attached_to_dev = false;
 //    remove_dcr_from_dcrs(dcr);      /* remove dcr from jcr list */
    }
    free_unused_volume(dcr);           /* free unused vols attached to this dcr */
@@ -611,7 +642,9 @@ static void detach_dcr_from_dev(DCR *dcr)
 void free_dcr(DCR *dcr)
 {
 
-   detach_dcr_from_dev(dcr);
+   if (dcr->dev) {
+      detach_dcr_from_dev(dcr);
+   }
 
    if (dcr->block) {
       free_block(dcr->block);