]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/dev.c
Updates for debugging disk seek
[bacula/bacula] / bacula / src / stored / dev.c
index c89b81007fe4e01a054b9a12272dfd480174e774..459a9caa94de46d1d2fe53ed145d6ed2fe11993b 100644 (file)
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000-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) 2000-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.
+*/
 
 /*
  * Handling I/O errors and end of tape conditions are a bit tricky.
@@ -49,7 +62,7 @@
  * On either an I/O error or end of tape,
  * we will stop writing on the physical device (no I/O recovery is
  * attempted at least in this daemon). The state flag will be sent
- * to include ST_EOT, which is ephimeral, and ST_WEOT, which is
+ * to include ST_EOT, which is ephemeral, and ST_WEOT, which is
  * persistent. Lots of routines clear ST_EOT, but ST_WEOT is
  * cleared only when the problem goes away.  Now when ST_WEOT
  * is set all calls to write_block_to_device() call the fix_up
@@ -80,7 +93,7 @@
 #endif
 
 /* Forward referenced functions */
-void set_os_device_parameters(DEVICE *dev);
+void set_os_device_parameters(DCR *dcr);   
 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat);
 static char *mode_to_str(int mode);
 
@@ -118,7 +131,7 @@ init_dev(JCR *jcr, DEVRES *device)
       } else if (S_ISCHR(statp.st_mode)) {
          device->dev_type = B_TAPE_DEV;
       } else if (S_ISFIFO(statp.st_mode)) {
-         device->dev_type = B_FILE_DEV;
+         device->dev_type = B_FIFO_DEV;
       } else if (!(device->cap_bits & CAP_REQMOUNT)) {
          Jmsg2(jcr, M_ERROR, 0, _("%s is an unknown device type. Must be tape or directory\n"
                " or have RequiresMount=yes for DVD. st_mode=%x\n"),
@@ -324,15 +337,19 @@ void DEVICE::open_tape_device(DCR *dcr, int omode)
 {
    file_size = 0;
    int timeout = max_open_wait;
+#if !defined(HAVE_WIN32)
    struct mtop mt_com;
    utime_t start_time = time(NULL);
+#endif
 
 
    Dmsg0(29, "Open dev: device is tape\n");
 
    get_autochanger_loaded_slot(dcr);
 
+   openmode = omode;
    set_mode(omode);
+
    if (timeout < 1) {
       timeout = 1;
    }
@@ -390,7 +407,7 @@ void DEVICE::open_tape_device(DCR *dcr, int omode)
             }
             dev_errno = 0;
             lock_door();
-            set_os_device_parameters(this);      /* do system dependent stuff */
+            set_os_device_parameters(dcr);       /* do system dependent stuff */
             break;                               /* Successfully opened and rewound */
          }
       }
@@ -445,7 +462,7 @@ void DEVICE::open_file_device(DCR *dcr, int omode)
          return;
       }
 
-      if (archive_name.c_str()[strlen(archive_name.c_str())-1] != '/') {
+      if (!IsPathSeparator(archive_name.c_str()[strlen(archive_name.c_str())-1])) {
          pm_strcat(archive_name, "/");
       }
       pm_strcat(archive_name, VolCatInfo.VolCatName);
@@ -489,8 +506,8 @@ void DEVICE::open_dvd_device(DCR *dcr, int omode)
    /*
     * Handle opening of DVD Volume
     */     
-   Dmsg3(29, "Enter: open_dvd_dev: %s dev=%s mode=%s\n", is_dvd()?"DVD":"disk",
-         archive_name.c_str(), mode_to_str(omode));
+   Dmsg2(29, "Enter: open_dvd_dev: DVD vol=%s mode=%s\n", 
+         &dcr->VolCatInfo, mode_to_str(omode));
 
    /*
     * For a DVD we must always pull the state info from dcr->VolCatInfo
@@ -527,6 +544,7 @@ void DEVICE::open_dvd_device(DCR *dcr, int omode)
     *   OPEN_READ_ONLY as writing would be an error.
     */
    Dmsg2(29, "open DVD part=%d num_dvd_parts=%d\n", part, num_dvd_parts);
+   /* Now find the name of the part that we want to access */
    if (part <= num_dvd_parts) {
       omode = OPEN_READ_ONLY;
       make_mounted_dvd_filename(this, archive_name);
@@ -544,7 +562,7 @@ void DEVICE::open_dvd_device(DCR *dcr, int omode)
    Dmsg3(99, "open_dvd_device: part=%d num_dvd_parts=%d, VolCatInfo.VolCatParts=%d\n",
       part, num_dvd_parts, dcr->VolCatInfo.VolCatParts);
      
-   if (mount_dvd(this, 1)) {
+   if (mount(1)) {
       Dmsg0(99, "DVD device mounted.\n");
       if (num_dvd_parts == 0 && !truncating) {
          /*
@@ -556,18 +574,46 @@ void DEVICE::open_dvd_device(DCR *dcr, int omode)
          if (!check_can_write_on_non_blank_dvd(dcr)) {
             Mmsg(errmsg, _("The DVD in device %s contains data, please blank it before writing.\n"), print_name());
             Emsg0(M_FATAL, 0, errmsg);
-            unmount_dvd(this, 1); /* Unmount the device, so the operator can change it. */
+            unmount(1); /* Unmount the device, so the operator can change it. */
             clear_opened();
             return;
          }
          blank_dvd = true;
+      } else {
+         /*
+          * Ensure that we have the correct DVD loaded by looking for part1.
+          * We only succeed the open if it exists. Failure to do this could
+          * leave us trying to add a part to a different DVD!
+          */
+         uint32_t oldpart = part;
+         struct stat statp;
+         POOL_MEM part1_name(PM_FNAME);
+         part = 1;
+         make_mounted_dvd_filename(this, part1_name);
+         part = oldpart;
+         if (stat(part1_name.c_str(), &statp) < 0) {
+            berrno be;
+            Mmsg(errmsg, _("Unable to stat DVD part 1 file %s: ERR=%s\n"),
+               part1_name.c_str(), be.strerror());
+            Emsg0(M_FATAL, 0, errmsg);
+            clear_opened();
+            return;
+         }
+         if (!S_ISREG(statp.st_mode)) {
+            /* It is not a regular file */
+            Mmsg(errmsg, _("DVD part 1 is not a regular file %s.\n"),
+               part1_name.c_str());
+            Emsg0(M_FATAL, 0, errmsg);
+            clear_opened();
+            return;
+         }
       }
    } else {
       Dmsg0(99, "DVD device mount failed.\n");
       /* We cannot mount the device */
       if (num_dvd_parts == 0) {
          /* Run free space, check there is a media. */
-         if (!update_free_space_dev(this)) {
+         if (!update_freespace()) {
             Emsg0(M_FATAL, 0, errmsg);
             clear_opened();
             return;
@@ -721,7 +767,7 @@ bool DEVICE::rewind(DCR *dcr)
          break;
       }
    } else if (is_file() || is_dvd()) {
-      if (lseek(dcr, (off_t)0, SEEK_SET) < 0) {
+      if (lseek(dcr, (boffset_t)0, SEEK_SET) < 0) {
          berrno be;
          dev_errno = errno;
          Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
@@ -804,7 +850,7 @@ bool DEVICE::eod(DCR *dcr)
    struct mtop mt_com;
    struct mtget mt_stat;
    bool ok = true;
-   off_t pos;
+   boffset_t pos;
 
    if (fd < 0) {
       dev_errno = EBADF;
@@ -828,7 +874,7 @@ bool DEVICE::eod(DCR *dcr)
       return true;
    }
    if (!is_tape()) {
-      pos = lseek(dcr, (off_t)0, SEEK_END);
+      pos = lseek(dcr, (boffset_t)0, SEEK_END);
 //    Dmsg1(100, "====== Seek to %lld\n", pos);
       if (pos >= 0) {
          update_pos(dcr);
@@ -911,12 +957,12 @@ bool DEVICE::eod(DCR *dcr)
          /*
           * Avoid infinite loop by ensuring we advance.
           */
-         if (file_num == (int)file) {
+         if (!at_eot() && file_num == (int)file) {
             struct mtget mt_stat;
             Dmsg1(100, "fsf did not advance from file %d\n", file_num);
             set_ateof();
             if (dev_get_os_pos(this, &mt_stat)) {
-               Dmsg2(100, "Adjust file from %d to %d\n", file , mt_stat.mt_fileno);
+               Dmsg2(100, "Adjust file from %d to %d\n", file_num, mt_stat.mt_fileno);
                file = mt_stat.mt_fileno;
             }       
             break;
@@ -954,7 +1000,7 @@ bool DEVICE::eod(DCR *dcr)
  */
 bool DEVICE::update_pos(DCR *dcr)
 {
-   off_t pos;
+   boffset_t pos;
    bool ok = true;
 
    if (!is_open()) {
@@ -968,7 +1014,7 @@ bool DEVICE::update_pos(DCR *dcr)
    if (is_file() || is_dvd()) {
       file = 0;
       file_addr = 0;
-      pos = lseek(dcr, (off_t)0, SEEK_CUR);
+      pos = lseek(dcr, (boffset_t)0, SEEK_CUR);
       if (pos < 0) {
          berrno be;
          dev_errno = errno;
@@ -1537,9 +1583,9 @@ bool DEVICE::reposition(DCR *dcr, uint32_t rfile, uint32_t rblock)
    }
 
    if (!is_tape()) {
-      off_t pos = (((off_t)rfile)<<32) + (off_t)rblock;
+      boffset_t pos = (((boffset_t)rfile)<<32) | rblock;
       Dmsg1(100, "===== lseek to %d\n", (int)pos);
-      if (lseek(dcr, pos, SEEK_SET) == (off_t)-1) {
+      if (lseek(dcr, pos, SEEK_SET) == (boffset_t)-1) {
          berrno be;
          dev_errno = errno;
          Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
@@ -1553,8 +1599,7 @@ bool DEVICE::reposition(DCR *dcr, uint32_t rfile, uint32_t rblock)
    }
 
    /* After this point, we are tape only */
-   Dmsg4(100, "reposition from %u:%u to %u:%u\n",
-      file, block_num, rfile, rblock);
+   Dmsg4(100, "reposition from %u:%u to %u:%u\n", file, block_num, rfile, rblock);
    if (rfile < file) {
       Dmsg0(100, "Rewind\n");
       if (!rewind(NULL)) {
@@ -1581,6 +1626,17 @@ bool DEVICE::reposition(DCR *dcr, uint32_t rfile, uint32_t rblock)
       /* Ignore errors as Bacula can read to the correct block */
       Dmsg1(100, "fsr %d\n", rblock-block_num);
       return fsr(rblock-block_num);
+   } else {
+      while (rblock > block_num) {
+         if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
+            berrno be;
+            dev_errno = errno;
+            Dmsg2(30, "Failed to find requested block on %s: ERR=%s",
+               print_name(), be.strerror());
+            return false;
+         }
+         Dmsg2(300, "moving forward wanted_blk=%d at_blk=%d\n", rblock, block_num);
+      }
    }
    return true;
 }
@@ -1803,8 +1859,8 @@ void DEVICE::close()
 
    switch (dev_type) {
    case B_TAPE_DEV:
-      tape_close(fd);
       unlock_door(); 
+      tape_close(fd);
       break;
    default:
       ::close(fd);
@@ -1852,14 +1908,18 @@ void DEVICE::close_part(DCR *dcr)
    dcr->VolCatInfo = saveVolCatInfo;  /* structure assignment */
 }
 
-off_t DEVICE::lseek(DCR *dcr, off_t offset, int whence)
+boffset_t DEVICE::lseek(DCR *dcr, boffset_t offset, int whence)
 {
    switch (dev_type) {
    case B_DVD_DEV:
       return lseek_dvd(dcr, offset, whence);
    case B_FILE_DEV:
-      return ::lseek(fd, offset, whence);
-   }  
+#if defined(HAVE_WIN32)
+      return ::_lseeki64(fd, (__int64)offset, whence);
+#else
+      return ::lseek(fd, (off_t)offset, whence);
+#endif
+   }
    return -1;
 }
 
@@ -1909,7 +1969,7 @@ bool DEVICE::mount(int timeout)
  */
 bool DEVICE::unmount(int timeout) 
 {
-   Dmsg0(90, "Enter unmount_dvd\n");
+   Dmsg0(90, "Enter unmount\n");
    if (is_mounted()) {
       return do_mount(0, timeout);
    }
@@ -1939,9 +1999,10 @@ bool DEVICE::do_mount(int mount, int dotimeout)
       icmd = device->unmount_command;
    }
    
+   clear_freespace_ok();
    edit_mount_codes(ocmd, icmd);
    
-   Dmsg2(100, "do_mount_dvd: cmd=%s mounted=%d\n", ocmd.c_str(), !!is_mounted());
+   Dmsg2(100, "do_mount: cmd=%s mounted=%d\n", ocmd.c_str(), !!is_mounted());
 
    if (dotimeout) {
       /* Try at most 1 time to (un)mount the device. This should perhaps be configurable. */
@@ -1949,7 +2010,7 @@ bool DEVICE::do_mount(int mount, int dotimeout)
    } else {
       timeout = 0;
    }
-   results = get_memory(2000);
+   results = get_memory(4000);
    results[0] = 0;
 
    /* If busy retry each second */
@@ -1957,7 +2018,10 @@ bool DEVICE::do_mount(int mount, int dotimeout)
    while ((status = run_program_full_output(ocmd.c_str(), 
                        max_open_wait/2, results)) != 0) {
       /* Doesn't work with internationalization (This is not a problem) */
-      if (fnmatch("*is already mounted on", results, 0) == 0) {
+      if (mount && fnmatch("*is already mounted on*", results, 0) == 0) {
+         break;
+      }
+      if (!mount && fnmatch("* not mounted*", results, 0) == 0) {
          break;
       }
       if (timeout-- > 0) {
@@ -1970,9 +2034,18 @@ bool DEVICE::do_mount(int mount, int dotimeout)
          bmicrosleep(1, 0);
          continue;
       }
-      Dmsg2(40, "Device %s cannot be mounted. ERR=%s\n", print_name(), results);
-      Mmsg(errmsg, _("Device %s cannot be mounted. ERR=%s\n"), 
-           print_name(), results);
+      if (status != 0) {
+         berrno be;
+         Dmsg5(40, "Device %s cannot be %smounted. stat=%d result=%s ERR=%s\n", print_name(),
+              (mount ? "" : "un"), status, results, be.strerror(status));
+         Mmsg(errmsg, _("Device %s cannot be %smounted. ERR=%s\n"), 
+              print_name(), (mount ? "" : "un"), be.strerror(status));
+      } else {
+         Dmsg4(40, "Device %s cannot be %smounted. stat=%d ERR=%s\n", print_name(),
+              (mount ? "" : "un"), status, results);
+         Mmsg(errmsg, _("Device %s cannot be %smounted. ERR=%s\n"), 
+              print_name(), (mount ? "" : "un"), results);
+      }
       /*
        * Now, just to be sure it is not mounted, try to read the
        *  filesystem.
@@ -2017,8 +2090,18 @@ bool DEVICE::do_mount(int mount, int dotimeout)
       Dmsg1(29, "do_mount: got %d files in the mount point (not counting ., .. and .keep)\n", count);
       
       if (count > 0) {
-         mount = 1;                      /* If we got more than ., .. and .keep */
-         break;                          /*   there must be something mounted */
+         /* If we got more than ., .. and .keep */
+         /*   there must be something mounted */
+         if (mount) {
+            Dmsg1(100, "Did Mount by count=%d\n", count);
+            break;
+         } else {
+            /* An unmount request. We failed to unmount - report an error */
+            set_mounted(true);
+            free_pool_memory(results);
+            Dmsg0(200, "== error mount=1 wanted unmount\n");
+            return false;
+         }
       }
 get_out:
       set_mounted(false);
@@ -2030,6 +2113,10 @@ get_out:
    
    set_mounted(mount);              /* set/clear mounted flag */
    free_pool_memory(results);
+   /* Do not check free space when unmounting */
+   if (mount && !update_freespace()) {
+      return false;
+   }
    Dmsg1(200, "============ mount=%d\n", mount);
    return true;
 }
@@ -2214,8 +2301,10 @@ bool double_dev_wait_time(DEVICE *dev)
 }
 
 
-void set_os_device_parameters(DEVICE *dev)
+void set_os_device_parameters(DCR *dcr)
 {
+   DEVICE *dev = dcr->dev;
+
 #if defined(HAVE_LINUX_OS) || defined(HAVE_WIN32)
    struct mtop mt_com;
 
@@ -2279,6 +2368,22 @@ void set_os_device_parameters(DEVICE *dev)
          dev->clrerror(MTSETBSIZ);
       }
    }
+/* Turn this on later when fully tested */
+#if defined(xxxMTIOCSETEOTMODEL) 
+   uint32_t neof;
+   if (dev_cap(dev, CAP_TWOEOF)) {
+      neof = 2;
+   } else {
+      neof = 1;
+   }
+   if (ioctl(dev->fd, MTIOCSETEOTMODEL, (caddr_t)&neof) < 0) {
+      berrno be;
+      dev->dev_errno = errno;         /* save errno */
+      Mmsg2(dev->errmsg, _("Unable to set eotmodel on device %s: ERR=%s\n"),
+            dev->print_name(), be.strerror(dev->dev_errno));
+      Jmsg(dcr->jcr, M_FATAL, 0, dev->errmsg);
+   }
+#endif
    return;
 #endif