]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/dev.c
tweaks
[bacula/bacula] / bacula / src / stored / dev.c
index c86658797dbaad7e3de1f2c834c2d6b2ab90be72..474fa45d411518191f24eb9df0370aa50aec828f 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-2007 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.
@@ -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"),
@@ -179,9 +192,8 @@ init_dev(JCR *jcr, DEVRES *device)
       if (!device->mount_point || stat(device->mount_point, &statp) < 0) {
          berrno be;
          dev->dev_errno = errno;
-         Jmsg2(jcr, M_ERROR, 0, _("Unable to stat mount point %s: ERR=%s\n"), 
+         Jmsg2(jcr, M_ERROR_TERM, 0, _("Unable to stat mount point %s: ERR=%s\n"), 
             device->mount_point, be.strerror());
-         return NULL;
       }
    }
    if (dev->is_dvd()) {
@@ -324,15 +336,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 +406,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 +461,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);
@@ -504,9 +520,9 @@ void DEVICE::open_dvd_device(DCR *dcr, int omode)
    Dmsg1(100, "Volume=%s\n", VolCatInfo.VolCatName);
 
    if (VolCatInfo.VolCatName[0] == 0) {
-      Dmsg1(10,  "Could not open file device %s. No Volume name given.\n",
+      Dmsg1(10,  "Could not open DVD device %s. No Volume name given.\n",
          print_name());
-      Mmsg(errmsg, _("Could not open file device %s. No Volume name given.\n"),
+      Mmsg(errmsg, _("Could not open DVD device %s. No Volume name given.\n"),
          print_name());
       clear_opened();
       return;
@@ -750,7 +766,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"),
@@ -833,7 +849,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;
@@ -857,7 +873,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);
@@ -940,12 +956,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;
@@ -983,7 +999,7 @@ bool DEVICE::eod(DCR *dcr)
  */
 bool DEVICE::update_pos(DCR *dcr)
 {
-   off_t pos;
+   boffset_t pos;
    bool ok = true;
 
    if (!is_open()) {
@@ -997,7 +1013,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;
@@ -1309,7 +1325,7 @@ bool DEVICE::fsf(int num)
       mt_com.mt_count = 1;
       while (num-- && !at_eot()) {
          Dmsg0(100, "Doing read before fsf\n");
-         if ((stat = tape_read(fd, (char *)rbuf, rbuf_len)) < 0) {
+         if ((stat = this->read((char *)rbuf, rbuf_len)) < 0) {
             if (errno == ENOMEM) {     /* tape record exceeds buf len */
                stat = rbuf_len;        /* This is OK */
             /*
@@ -1566,9 +1582,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"),
@@ -1582,8 +1598,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)) {
@@ -1610,6 +1625,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;
 }
@@ -1687,32 +1713,32 @@ void DEVICE::clrerror(int func)
    if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
       switch (func) {
       case -1:
-         break;              /* ignore message printed later */
+         break;                  /* ignore message printed later */
       case MTWEOF:
          msg = "WTWEOF";
-         capabilities &= ~CAP_EOF; /* turn off feature */
+         clear_cap(CAP_EOF);     /* turn off feature */
          break;
 #ifdef MTEOM
       case MTEOM:
          msg = "WTEOM";
-         capabilities &= ~CAP_EOM; /* turn off feature */
+         clear_cap(CAP_EOM);     /* turn off feature */
          break;
 #endif
       case MTFSF:
          msg = "MTFSF";
-         capabilities &= ~CAP_FSF; /* turn off feature */
+         clear_cap(CAP_FSF);     /* turn off feature */
          break;
       case MTBSF:
          msg = "MTBSF";
-         capabilities &= ~CAP_BSF; /* turn off feature */
+         clear_cap(CAP_BSF);     /* turn off feature */
          break;
       case MTFSR:
          msg = "MTFSR";
-         capabilities &= ~CAP_FSR; /* turn off feature */
+         clear_cap(CAP_FSR);     /* turn off feature */
          break;
       case MTBSR:
          msg = "MTBSR";
-         capabilities &= ~CAP_BSR; /* turn off feature */
+         clear_cap(CAP_BSR);     /* turn off feature */
          break;
       case MTREW:
          msg = "MTREW";
@@ -1832,8 +1858,8 @@ void DEVICE::close()
 
    switch (dev_type) {
    case B_TAPE_DEV:
-      tape_close(fd);
       unlock_door(); 
+      tape_close(fd);
       break;
    default:
       ::close(fd);
@@ -1881,14 +1907,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;
 }
 
@@ -2162,6 +2192,63 @@ void DEVICE::edit_mount_codes(POOL_MEM &omsg, const char *imsg)
    }
 }
 
+/* return the last timer interval (ms) */
+btime_t DEVICE::get_timer_count()
+{
+   btime_t old = last_timer;
+   last_timer = get_current_btime();
+   return last_timer - old;
+}
+
+/* read from fd */
+ssize_t DEVICE::read(void *buf, size_t len)
+{
+   ssize_t read_len ;
+
+   get_timer_count();
+
+   if (this->is_tape()) {
+      read_len = tape_read(fd, buf, len);
+   } else {
+      read_len = ::read(fd, buf, len);
+   }
+
+   last_tick = get_timer_count();
+
+   DevReadTime += last_tick;
+   VolCatInfo.VolReadTime += last_tick;
+
+   if (read_len > 0) {          /* skip error */
+      DevReadBytes += read_len;
+   }
+
+   return read_len;   
+}
+
+/* write to fd */
+ssize_t DEVICE::write(const void *buf, size_t len)
+{
+   ssize_t write_len ;
+
+   get_timer_count();
+
+   if (this->is_tape()) {
+      write_len = tape_write(fd, buf, len);
+   } else {
+      write_len = ::write(fd, buf, len);
+   }
+
+   last_tick = get_timer_count();
+
+   DevWriteTime += last_tick;
+   VolCatInfo.VolWriteTime += last_tick;
+
+   if (write_len > 0) {         /* skip error */
+      DevWriteBytes += write_len;
+   }
+
+   return write_len;   
+}
 
 /* Return the resource name for the device */
 const char *DEVICE::name() const
@@ -2270,8 +2357,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;
 
@@ -2335,6 +2424,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->has_cap(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