]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/dev.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / stored / dev.c
index a00a6e25ea74af9c1b922601350f704d8525074a..294c17d9c33cda68bc667f92c6506eb5768a683a 100644 (file)
@@ -103,7 +103,7 @@ init_dev(JCR *jcr, DEVRES *device)
    DCR *dcr = NULL;
    DEVICE *dev;
 
-   
+
    /* If no device type specified, try to guess */
    if (!device->dev_type) {
       /* Check that device is available */
@@ -129,8 +129,9 @@ init_dev(JCR *jcr, DEVRES *device)
       }
    }
 
-   dev = (DEVICE *)malloc(sizeof(DEVICE));
+   dev = (DEVICE *)get_memory(sizeof(DEVICE));
    memset(dev, 0, sizeof(DEVICE));
+   dev->state = ST_MALLOC;
 
    /* Copy user supplied device parameters from Resource */
    dev->dev_name = get_memory(strlen(device->device_name)+1);
@@ -138,7 +139,6 @@ init_dev(JCR *jcr, DEVRES *device)
    dev->prt_name = get_memory(strlen(device->device_name) + strlen(device->hdr.name) + 20);
    /* We edit "Resource-name" (physical-name) */
    Mmsg(dev->prt_name, "\"%s\" (%s)", device->hdr.name, device->device_name);
-   Dmsg1(400, "Allocate dev=%s\n", dev->print_name());
    dev->capabilities = device->cap_bits;
    dev->min_block_size = device->min_block_size;
    dev->max_block_size = device->max_block_size;
@@ -154,7 +154,7 @@ init_dev(JCR *jcr, DEVRES *device)
    dev->autoselect = device->autoselect;
    dev->dev_type = device->dev_type;
    if (dev->is_tape()) { /* No parts on tapes */
-      dev->max_part_size = 0;
+       dev->max_part_size = 0;
    } else {
       dev->max_part_size = device->max_part_size;
    }
@@ -162,9 +162,7 @@ init_dev(JCR *jcr, DEVRES *device)
    if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
       dev->vol_poll_interval = 60;
    }
-   /* Link the dev and device structures together */
    dev->device = device;
-   device->dev = dev;
 
    if (dev->is_fifo()) {
       dev->capabilities |= CAP_STREAM; /* set stream device */
@@ -274,7 +272,7 @@ DEVICE::open(DCR *dcr, int omode)
       bstrncpy(VolCatInfo.VolCatName, dcr->VolumeName, sizeof(VolCatInfo.VolCatName));
    }
 
-   Dmsg4(29, "open dev: type=%d dev_name=%s vol=%s mode=%s\n", dev_type,
+   Dmsg4(29, "open dev: tape=%d dev_name=%s vol=%s mode=%s\n", is_tape(),
          print_name(), VolCatInfo.VolCatName, mode_to_str(omode));
    state &= ~(ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
    label_type = B_BACULA_LABEL;
@@ -285,12 +283,9 @@ DEVICE::open(DCR *dcr, int omode)
       open_dvd_device(dcr, omode);
    } else {
       Dmsg1(100, "call open_file_device mode=%s\n", mode_to_str(omode));
-      open_file_device(dcr, omode);
+      open_file_device(omode);
    }
    state |= preserve;                 /* reset any important state info */
-   if (preserve) {
-      Dmsg1(000, "preserve=0x%x\n", preserve);
-   }
    return fd;
 }
 
@@ -301,7 +296,11 @@ void DEVICE::set_mode(int new_mode)
       mode = O_CREAT | O_RDWR | O_BINARY;
       break;
    case OPEN_READ_WRITE:
-      mode = O_RDWR | O_BINARY;
+      if (is_dvd() || is_file()) {
+         mode = O_CREAT | O_RDWR | O_BINARY;
+      } else {
+         mode = O_RDWR | O_BINARY;
+      }
       break;
    case OPEN_READ_ONLY:
       mode = O_RDONLY | O_BINARY;
@@ -405,14 +404,10 @@ void DEVICE::set_blocking()
 /*
  * Open a file device
  */
-void DEVICE::open_file_device(DCR *dcr, int omode) 
+void DEVICE::open_file_device(int omode) 
 {
    POOL_MEM archive_name(PM_FNAME);
 
-   if (is_autochanger()) {
-      get_autochanger_loaded_slot(dcr);
-   }
-
    /*
     * Handle opening of File Archive (not a tape)
     */     
@@ -437,12 +432,15 @@ void DEVICE::open_file_device(DCR *dcr, int omode)
       pm_strcat(archive_name, VolCatInfo.VolCatName);
    }
 
-   mount(1);                          /* do mount if required */
          
+   Dmsg3(29, "open dev: %s dev=%s mode=%s\n", is_dvd()?"DVD":"disk",
+         archive_name.c_str(), mode_to_str(omode));
    openmode = omode;
+   Dmsg2(100, "openmode=%d %s\n", openmode, mode_to_str(openmode));
+   
    set_mode(omode);
    /* If creating file, give 0640 permissions */
-   Dmsg3(29, "open disk: mode=%s open(%s, 0x%x, 0640)\n", mode_to_str(omode), 
+   Dmsg3(29, "mode=%s open(%s, 0x%x, 0640)\n", mode_to_str(omode), 
          archive_name.c_str(), mode);
    /* Use system open() */
    if ((fd = ::open(archive_name.c_str(), mode, 0640)) < 0) {
@@ -456,13 +454,13 @@ void DEVICE::open_file_device(DCR *dcr, int omode)
       dev_errno = 0;
       update_pos_dev(this);                /* update position */
    }
-   Dmsg4(29, "open dev: disk fd=%d opened, part=%d/%d, part_size=%u\n", 
-      fd, part, num_parts, part_size);
+   Dmsg5(29, "open dev: %s fd=%d opened, part=%d/%d, part_size=%u\n", 
+      is_dvd()?"DVD":"disk", fd, part, num_parts, 
+      part_size);
 }
 
 /*
- * Open a DVD device. N.B. at this point, dcr->VolCatInfo.VolCatName 
- *  (NB:??? I think it's VolCatInfo.VolCatName that is right)
+ * Open a DVD device. N.B. at this point, dcr->VolCatInfo.VolCatName (NB:??? I think it's VolCatInfo.VolCatName that is right)
  *  has the desired Volume name, but there is NO assurance that
  *  any other field of VolCatInfo is correct.
  */
@@ -499,20 +497,21 @@ void DEVICE::open_dvd_device(DCR *dcr, int omode)
       dcr->dev->num_parts = dcr->VolCatInfo.VolCatParts;
    }
 
-   if (mount_dvd(this, 1)) {
+   if (mount_dev(this, 1)) {
       if ((num_parts == 0) && (!truncating)) {
          /* If we can mount the device, and we are not truncating the DVD, we usually want to abort. */
          /* There is one exception, if there is only one 0-sized file on the DVD, with the right volume name,
-          * we continue (it's the method used by truncate_dvd to truncate a volume). */
+          * we continue (it's the method used by truncate_dvd_dev to truncate a volume). */
          if (!check_can_write_on_non_blank_dvd(dcr)) {
             Mmsg(errmsg, _("The media in the device %s is not empty, please blank it before writing anything to it.\n"), print_name());
             Emsg0(M_FATAL, 0, errmsg);
-            unmount_dvd(this, 1); /* Unmount the device, so the operator can change it. */
+            unmount_dev(this, 1); /* Unmount the device, so the operator can change it. */
             clear_opened();
             return;
          }
       }
-   } else {
+   }
+   else {
       /* We cannot mount the device */
       if (num_parts == 0) {
          /* Run free space, check there is a media. */
@@ -625,7 +624,7 @@ bool DEVICE::rewind(DCR *dcr)
    unsigned int i;
    bool first = true;
 
-   Dmsg3(400, "rewind res=%d fd=%d %s\n", reserved_device, fd, print_name());
+   Dmsg3(29, "rewind res=%d fd=%d %s\n", reserved_device, fd, print_name());
    if (fd < 0) {
       if (!is_dvd()) { /* In case of major error, the fd is not open on DVD, so we don't want to abort. */
          dev_errno = EBADF;
@@ -760,54 +759,56 @@ void DEVICE::set_ateot()
  *  Returns: true  on succes
  *           false on error
  */
-bool DEVICE::eod()
+bool
+eod_dev(DEVICE *dev)
 {
    struct mtop mt_com;
    struct mtget mt_stat;
    bool ok = true;
    off_t pos;
 
-   if (fd < 0) {
-      dev_errno = EBADF;
-      Mmsg1(errmsg, _("Bad call to eod_dev. Device %s not open\n"), print_name());
+   if (dev->fd < 0) {
+      dev->dev_errno = EBADF;
+      Mmsg1(dev->errmsg, _("Bad call to eod_dev. Device %s not open\n"),
+            dev->print_name());
       return false;
    }
 
 #if defined (__digital__) && defined (__unix__)
-   return fsf(VolCatInfo.VolCatFiles);
+   return dev->fsf(dev->VolCatInfo.VolCatFiles);
 #endif
 
    Dmsg0(29, "eod_dev\n");
-   if (at_eot()) {
+   if (dev->at_eot()) {
       return true;
    }
-   state &= ~(ST_EOF);  /* remove EOF flags */
-   block_num = file = 0;
-   file_size = 0;
-   file_addr = 0;
-   if (is_fifo() || is_prog()) {
+   dev->state &= ~(ST_EOF);  /* remove EOF flags */
+   dev->block_num = dev->file = 0;
+   dev->file_size = 0;
+   dev->file_addr = 0;
+   if (dev->is_fifo() || dev->is_prog()) {
       return true;
    }
-   if (!is_tape()) {
-      pos = lseek_dev(this, (off_t)0, SEEK_END);
+   if (!dev->is_tape()) {
+      pos = lseek_dev(dev, (off_t)0, SEEK_END);
 //    Dmsg1(100, "====== Seek to %lld\n", pos);
       if (pos >= 0) {
-         update_pos_dev(this);
-         state |= ST_EOT;
+         update_pos_dev(dev);
+         dev->state |= ST_EOT;
          return true;
       }
-      dev_errno = errno;
+      dev->dev_errno = errno;
       berrno be;
-      Mmsg2(errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
-             print_name(), be.strerror());
+      Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
+             dev->print_name(), be.strerror());
       return false;
    }
 #ifdef MTEOM
-   if (has_cap(CAP_FASTFSF) && !has_cap(CAP_EOM)) {
+   if (dev_cap(dev, CAP_FASTFSF) && !dev_cap(dev, CAP_EOM)) {
       Dmsg0(100,"Using FAST FSF for EOM\n");
       /* If unknown position, rewind */
-      if (!dev_get_os_pos(this, &mt_stat)) {
-        if (!rewind(NULL)) {
+      if (!dev_get_os_pos(dev, &mt_stat)) {
+        if (!dev->rewind(NULL)) {
           return false;
         }
       }
@@ -822,33 +823,33 @@ bool DEVICE::eod()
       }
    }
 
-   if (has_cap(CAP_MTIOCGET) && (has_cap(CAP_FASTFSF) || has_cap(CAP_EOM))) {
-      if (has_cap(CAP_EOM)) {
+   if (dev_cap(dev, CAP_MTIOCGET) && (dev_cap(dev, CAP_FASTFSF) || dev_cap(dev, CAP_EOM))) {
+      if (dev_cap(dev, CAP_EOM)) {
          Dmsg0(100,"Using EOM for EOM\n");
          mt_com.mt_op = MTEOM;
          mt_com.mt_count = 1;
       }
 
-      if (ioctl(fd, MTIOCTOP, (char *)&mt_com) < 0) {
+      if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
          berrno be;
-         clrerror_dev(this, mt_com.mt_op);
+         clrerror_dev(dev, mt_com.mt_op);
          Dmsg1(50, "ioctl error: %s\n", be.strerror());
-         update_pos_dev(this);
-         Mmsg2(errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
-            print_name(), be.strerror());
+         update_pos_dev(dev);
+         Mmsg2(dev->errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
+            dev->print_name(), be.strerror());
          return false;
       }
 
-      if (!dev_get_os_pos(this, &mt_stat)) {
+      if (!dev_get_os_pos(dev, &mt_stat)) {
          berrno be;
-         clrerror_dev(this, -1);
-         Mmsg2(errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
-            print_name(), be.strerror());
+         clrerror_dev(dev, -1);
+         Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
+            dev->print_name(), be.strerror());
          return false;
       }
       Dmsg2(100, "EOD file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
-      set_ateof();
-      file = mt_stat.mt_fileno;
+      dev->set_ateof();
+      dev->file = mt_stat.mt_fileno;
    } else {
 #else
    {
@@ -856,29 +857,29 @@ bool DEVICE::eod()
       /*
        * Rewind then use FSF until EOT reached
        */
-      if (!rewind(NULL)) {
+      if (!dev->rewind(NULL)) {
          return false;
       }
       /*
        * Move file by file to the end of the tape
        */
       int file_num;
-      for (file_num=file; !at_eot(); file_num++) {
+      for (file_num=dev->file; !dev->at_eot(); file_num++) {
          Dmsg0(200, "eod_dev: doing fsf 1\n");
-         if (!fsf(1)) {
+         if (!dev->fsf(1)) {
             Dmsg0(200, "fsf error.\n");
             return false;
          }
          /*
           * Avoid infinite loop by ensuring we advance.
           */
-         if (file_num == (int)file) {
+         if (file_num == (int)dev->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);
-               file = mt_stat.mt_fileno;
+            dev->set_ateof();
+            if (dev_get_os_pos(dev, &mt_stat)) {
+               Dmsg2(100, "Adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
+               dev->file = mt_stat.mt_fileno;
             }       
             break;
          }
@@ -889,21 +890,21 @@ bool DEVICE::eod()
     * MTEOM, so we must backup so that appending overwrites
     * the second EOF.
     */
-   if (has_cap(CAP_BSFATEOM)) {
+   if (dev_cap(dev, CAP_BSFATEOM)) {
       struct mtget mt_stat;
       /* Backup over EOF */
-      ok = bsf(1);
+      ok = bsf_dev(dev, 1);
       /* If BSF worked and fileno is known (not -1), set file */
-      if (dev_get_os_pos(this, &mt_stat)) {
-         Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", file , mt_stat.mt_fileno);
-         file = mt_stat.mt_fileno;
+      if (dev_get_os_pos(dev, &mt_stat)) {
+         Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
+         dev->file = mt_stat.mt_fileno;
       } else {
-         file++;                 /* wing it -- not correct on all OSes */
+         dev->file++;                 /* wing it -- not correct on all OSes */
       }
    } else {
-      update_pos_dev(this);                   /* update position */
+      update_pos_dev(dev);                   /* update position */
    }
-   Dmsg1(200, "EOD dev->file=%d\n", file);
+   Dmsg1(200, "EOD dev->file=%d\n", dev->file);
    return ok;
 }
 
@@ -1018,7 +1019,7 @@ uint32_t status_dev(DEVICE *dev)
          Pmsg0(-20, " IM_REP_EN");
       }
 #endif /* !SunOS && !OSF */
-      if (dev->has_cap(CAP_MTIOCGET)) {
+      if (dev_cap(dev, CAP_MTIOCGET)) {
          Pmsg2(-20, _(" file=%d block=%d\n"), mt_stat.mt_fileno, mt_stat.mt_blkno);
       } else {
          Pmsg2(-20, _(" file=%d block=%d\n"), -1, -1);
@@ -1080,44 +1081,44 @@ bool load_dev(DEVICE *dev)
  *  Returns: true  on success
  *           false on failure
  */
-bool DEVICE::offline()
+bool offline_dev(DEVICE *dev)
 {
    struct mtop mt_com;
 
-   if (!is_tape()) {
+   if (!dev || dev->fd < 0 || !dev->is_tape()) {
       return true;                    /* device not open */
    }
 
-   state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_EOF|ST_WEOT);  /* remove EOF/EOT flags */
-   block_num = file = 0;
-   file_size = 0;
-   file_addr = 0;
-   part = 0;
+   dev->state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_EOF|ST_WEOT);  /* remove EOF/EOT flags */
+   dev->block_num = dev->file = 0;
+   dev->file_size = 0;
+   dev->file_addr = 0;
+   dev->part = 0;
 #ifdef MTUNLOCK
    mt_com.mt_op = MTUNLOCK;
    mt_com.mt_count = 1;
-   ioctl(fd, MTIOCTOP, (char *)&mt_com);
+   ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
 #endif
    mt_com.mt_op = MTOFFL;
    mt_com.mt_count = 1;
-   if (ioctl(fd, MTIOCTOP, (char *)&mt_com) < 0) {
+   if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
       berrno be;
-      dev_errno = errno;
-      Mmsg2(errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
-         print_name(), be.strerror());
+      dev->dev_errno = errno;
+      Mmsg2(dev->errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
+         dev->print_name(), be.strerror());
       return false;
    }
-   Dmsg1(100, "Offlined device %s\n", print_name());
+   Dmsg1(100, "Offlined device %s\n", dev->print_name());
    return true;
 }
 
-bool DEVICE::offline_or_rewind()
+bool offline_or_rewind_dev(DEVICE *dev)
 {
-   if (fd < 0) {
+   if (dev->fd < 0) {
       return false;
    }
-   if (has_cap(CAP_OFFLINEUNMOUNT)) {
-      return offline();
+   if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
+      return offline_dev(dev);
    } else {
    /*
     * Note, this rewind probably should not be here (it wasn't
@@ -1126,8 +1127,8 @@ bool DEVICE::offline_or_rewind()
     *  such as backspacing after writing and EOF. If it is not
     *  done, all future references to the drive get and I/O error.
     */
-      clrerror_dev(this, MTREW);
-      return rewind(NULL);
+      clrerror_dev(dev, MTREW);
+      return dev->rewind(NULL);
    }
 }
 
@@ -1170,7 +1171,7 @@ bool DEVICE::fsf(int num)
     *  the SCSI driver will ensure that we do not
     *  forward space past the end of the medium.
     */
-   if (has_cap(CAP_FSF) && has_cap(CAP_MTIOCGET) && has_cap(CAP_FASTFSF)) {
+   if (dev_cap(this, CAP_FSF) && dev_cap(this, CAP_MTIOCGET) && dev_cap(this, CAP_FASTFSF)) {
       mt_com.mt_op = MTFSF;
       mt_com.mt_count = num;
       stat = ioctl(fd, MTIOCTOP, (char *)&mt_com);
@@ -1196,7 +1197,7 @@ bool DEVICE::fsf(int num)
     *  is the only way we can be sure that we don't read
     *  two consecutive EOF marks, which means End of Data.
     */
-   } else if (has_cap(CAP_FSF)) {
+   } else if (dev_cap(this, CAP_FSF)) {
       POOLMEM *rbuf;
       int rbuf_len;
       Dmsg0(200, "FSF has cap_fsf\n");
@@ -1296,38 +1297,39 @@ bool DEVICE::fsf(int num)
  *  Returns: false on failure
  *           true  on success
  */
-bool DEVICE::bsf(int num)
+bool
+bsf_dev(DEVICE *dev, int num)
 {
    struct mtop mt_com;
    int stat;
 
-   if (fd < 0) {
-      dev_errno = EBADF;
-      Mmsg0(errmsg, _("Bad call to bsf. Device not open\n"));
-      Emsg0(M_FATAL, 0, errmsg);
+   if (dev->fd < 0) {
+      dev->dev_errno = EBADF;
+      Mmsg0(dev->errmsg, _("Bad call to bsf_dev. Device not open\n"));
+      Emsg0(M_FATAL, 0, dev->errmsg);
       return false;
    }
 
-   if (!is_tape()) {
-      Mmsg1(errmsg, _("Device %s cannot BSF because it is not a tape.\n"),
-         print_name());
+   if (!dev->is_tape()) {
+      Mmsg1(dev->errmsg, _("Device %s cannot BSF because it is not a tape.\n"),
+         dev->print_name());
       return false;
    }
-   Dmsg0(29, "bsf\n");
-   state &= ~(ST_EOT|ST_EOF);
-   file -= num;
-   file_addr = 0;
-   file_size = 0;
+   Dmsg0(29, "bsf_dev\n");
+   dev->state &= ~(ST_EOT|ST_EOF);
+   dev->file -= num;
+   dev->file_addr = 0;
+   dev->file_size = 0;
    mt_com.mt_op = MTBSF;
    mt_com.mt_count = num;
-   stat = ioctl(fd, MTIOCTOP, (char *)&mt_com);
+   stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
    if (stat < 0) {
       berrno be;
-      clrerror_dev(this, MTBSF);
-      Mmsg2(errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"),
-         print_name(), be.strerror());
+      clrerror_dev(dev, MTBSF);
+      Mmsg2(dev->errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"),
+         dev->print_name(), be.strerror());
    }
-   update_pos_dev(this);
+   update_pos_dev(dev);
    return stat == 0;
 }
 
@@ -1352,7 +1354,7 @@ bool DEVICE::fsr(int num)
    if (!is_tape()) {
       return false;
    }
-   if (!has_cap(CAP_FSR)) {
+   if (!dev_cap(this, CAP_FSR)) {
       Mmsg1(errmsg, _("ioctl MTFSR not permitted on %s.\n"), print_name());
       return false;
    }
@@ -1393,40 +1395,41 @@ bool DEVICE::fsr(int num)
  *   Returns:  false on failure
  *             true  on success
  */
-bool DEVICE::bsr(int num)
+bool
+bsr_dev(DEVICE *dev, int num)
 {
    struct mtop mt_com;
    int stat;
 
-   if (fd < 0) {
-      dev_errno = EBADF;
-      Mmsg0(errmsg, _("Bad call to bsr_dev. Device not open\n"));
-      Emsg0(M_FATAL, 0, errmsg);
+   if (dev->fd < 0) {
+      dev->dev_errno = EBADF;
+      Mmsg0(dev->errmsg, _("Bad call to bsr_dev. Device not open\n"));
+      Emsg0(M_FATAL, 0, dev->errmsg);
       return false;
    }
 
-   if (!is_tape()) {
+   if (!dev->is_tape()) {
       return false;
    }
 
-   if (!has_cap(CAP_BSR)) {
-      Mmsg1(errmsg, _("ioctl MTBSR not permitted on %s.\n"), print_name());
+   if (!dev_cap(dev, CAP_BSR)) {
+      Mmsg1(dev->errmsg, _("ioctl MTBSR not permitted on %s.\n"), dev->print_name());
       return false;
    }
 
    Dmsg0(29, "bsr_dev\n");
-   block_num -= num;
-   state &= ~(ST_EOF|ST_EOT|ST_EOF);
+   dev->block_num -= num;
+   dev->state &= ~(ST_EOF|ST_EOT|ST_EOF);
    mt_com.mt_op = MTBSR;
    mt_com.mt_count = num;
-   stat = ioctl(fd, MTIOCTOP, (char *)&mt_com);
+   stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
    if (stat < 0) {
       berrno be;
-      clrerror_dev(this, MTBSR);
-      Mmsg2(errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"),
-         print_name(), be.strerror());
+      clrerror_dev(dev, MTBSR);
+      Mmsg2(dev->errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"),
+         dev->print_name(), be.strerror());
    }
-   update_pos_dev(this);
+   update_pos_dev(dev);
    return stat == 0;
 }
 
@@ -1435,58 +1438,59 @@ bool DEVICE::bsr(int num)
  * Returns: false on failure
  *          true  on success
  */
-bool DEVICE::reposition(uint32_t rfile, uint32_t rblock)
+bool
+reposition_dev(DEVICE *dev, uint32_t file, uint32_t block)
 {
-   if (fd < 0) {
-      dev_errno = EBADF;
-      Mmsg0(errmsg, _("Bad call to reposition. Device not open\n"));
-      Emsg0(M_FATAL, 0, errmsg);
+   if (dev->fd < 0) {
+      dev->dev_errno = EBADF;
+      Mmsg0(dev->errmsg, _("Bad call to reposition_dev. Device not open\n"));
+      Emsg0(M_FATAL, 0, dev->errmsg);
       return false;
    }
 
-   if (!is_tape()) {
-      off_t pos = (((off_t)rfile)<<32) + (off_t)rblock;
+   if (!dev->is_tape()) {
+      off_t pos = (((off_t)file)<<32) + (off_t)block;
       Dmsg1(100, "===== lseek_dev to %d\n", (int)pos);
-      if (lseek_dev(this, pos, SEEK_SET) == (off_t)-1) {
+      if (lseek_dev(dev, pos, SEEK_SET) == (off_t)-1) {
          berrno be;
-         dev_errno = errno;
-         Mmsg2(errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
-            print_name(), be.strerror());
+         dev->dev_errno = errno;
+         Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
+            dev->print_name(), be.strerror());
          return false;
       }
-      file = rfile;
-      block_num = rblock;
-      file_addr = pos;
+      dev->file = file;
+      dev->block_num = block;
+      dev->file_addr = pos;
       return true;
    }
-   Dmsg4(100, "reposition from %u:%u to %u:%u\n",
-      file, block_num, rfile, rblock);
-   if (rfile < file) {
+   Dmsg4(100, "reposition_dev from %u:%u to %u:%u\n",
+      dev->file, dev->block_num, file, block);
+   if (file < dev->file) {
       Dmsg0(100, "Rewind\n");
-      if (!rewind(NULL)) {
+      if (!dev->rewind(NULL)) {
          return false;
       }
    }
-   if (rfile > file) {
-      Dmsg1(100, "fsf %d\n", rfile-file);
-      if (!fsf(rfile-file)) {
-         Dmsg1(100, "fsf failed! ERR=%s\n", bstrerror());
+   if (file > dev->file) {
+      Dmsg1(100, "fsf %d\n", file-dev->file);
+      if (!dev->fsf(file-dev->file)) {
+         Dmsg1(100, "fsf failed! ERR=%s\n", strerror_dev(dev));
          return false;
       }
-      Dmsg2(100, "wanted_file=%d at_file=%d\n", rfile, file);
+      Dmsg2(100, "wanted_file=%d at_file=%d\n", file, dev->file);
    }
-   if (rblock < block_num) {
-      Dmsg2(100, "wanted_blk=%d at_blk=%d\n", rblock, block_num);
-      Dmsg0(100, "bsf 1\n");
-      bsf(1);
+   if (block < dev->block_num) {
+      Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
+      Dmsg0(100, "bsf_dev 1\n");
+      bsf_dev(dev, 1);
       Dmsg0(100, "fsf_dev 1\n");
-      fsf(1);
-      Dmsg2(100, "wanted_blk=%d at_blk=%d\n", rblock, block_num);
+      dev->fsf(1);
+      Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
    }
-   if (has_cap(CAP_POSITIONBLOCKS) && rblock > block_num) {
+   if (dev_cap(dev, CAP_POSITIONBLOCKS) && block > dev->block_num) {
       /* Ignore errors as Bacula can read to the correct block */
-      Dmsg1(100, "fsr %d\n", rblock-block_num);
-      return fsr(rblock-block_num);
+      Dmsg1(100, "fsr %d\n", block-dev->block_num);
+      return dev->fsr(block-dev->block_num);
    }
    return true;
 }
@@ -1541,6 +1545,18 @@ weof_dev(DEVICE *dev, int num)
    return stat;
 }
 
+/*
+ * Return string message with last error in English
+ *  Be careful not to call this routine from within dev.c
+ *  while editing an Mmsg() or you will end up in a recursive
+ *  loop creating a Segmentation Violation.
+ */
+char *
+strerror_dev(DEVICE *dev)
+{
+   return dev->errmsg;
+}
+
 
 /*
  * If implemented in system, clear the tape
@@ -1661,31 +1677,33 @@ clrerror_dev(DEVICE *dev, int func)
 }
 
 /*
- * Close the device
+ * Flush buffer contents
+ *  No longer used.
  */
-void DEVICE::close()
+int flush_dev(DEVICE *dev)
 {
-   Dmsg1(100, "close_dev %s\n", print_name());
-   if (has_cap(CAP_OFFLINEUNMOUNT)) {
-      offline();
-   }
-   if (fd >= 0) {
-      ::close(fd);
-   } else {
-      return;                         /* already closed */
+   return 1;
+}
+
+static void do_close(DEVICE *dev)
+{
+
+   Dmsg1(100, "really close_dev %s\n", dev->print_name());
+   if (dev->fd >= 0) {
+      ::close(dev->fd);
    }
 
-   if (is_dvd() && !unmount_dvd(this, 1)) {
-      Dmsg1(0, "Cannot unmount device %s.\n", print_name());
+   if (!unmount_dev(dev, 1)) {
+      Dmsg1(0, "Cannot unmount device %s.\n", dev->print_name());
    }
    
    /* Remove the last part file if it is empty */
-   if (num_parts > 0) {
+   if (dev->num_parts > 0) {
       struct stat statp;
       POOL_MEM archive_name(PM_FNAME);
-      part = num_parts;
-      Dmsg1(100, "Call make_dvd_filename. Vol=%s\n", VolCatInfo.VolCatName);
-      make_spooled_dvd_filename(this, archive_name);
+      dev->part = dev->num_parts;
+      Dmsg1(100, "Call make_dvd_filename. Vol=%s\n", dev->VolCatInfo.VolCatName);
+      make_spooled_dvd_filename(dev, archive_name);
       /* Check that the part file is empty */
       if ((stat(archive_name.c_str(), &statp) == 0) && (statp.st_size == 0)) {
          Dmsg1(100, "unlink(%s)\n", archive_name.c_str());
@@ -1694,263 +1712,59 @@ void DEVICE::close()
    }
    
    /* Clean up device packet so it can be reused */
-   clear_opened();
-   state &= ~(ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF);
-   label_type = B_BACULA_LABEL;
-   file = block_num = 0;
-   file_size = 0;
-   file_addr = 0;
-   part = 0;
-   num_parts = 0;
-   part_size = 0;
-   part_start = 0;
-   EndFile = EndBlock = 0;
-   memset(&VolCatInfo, 0, sizeof(VolCatInfo));
-   free_volume(this);
-   memset(&VolHdr, 0, sizeof(VolHdr));
-   if (tid) {
-      stop_thread_timer(tid);
-      tid = 0;
-   }
-   openmode = 0;
+   dev->clear_opened();
+   dev->state &= ~(ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF);
+   dev->label_type = B_BACULA_LABEL;
+   dev->file = dev->block_num = 0;
+   dev->file_size = 0;
+   dev->file_addr = 0;
+   dev->part = 0;
+   dev->num_parts = 0;
+   dev->part_size = 0;
+   dev->part_start = 0;
+   dev->EndFile = dev->EndBlock = 0;
+   free_volume(dev);
+   memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
+   memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
+   if (dev->tid) {
+      stop_thread_timer(dev->tid);
+      dev->tid = 0;
+   }
+   dev->openmode = 0;
 }
 
+/*
+ * Close the device
+ */
+void DEVICE::close()
+{
+   do_close(this);
+}
 
 
-bool DEVICE::truncate(DCR *dcr) /* We need the DCR for DVD-writing */
+bool truncate_dev(DCR *dcr) /* We need the DCR for DVD-writing */
 {
-   Dmsg1(100, "truncate_dev %s\n", print_name());
-   if (is_tape()) {
+   DEVICE *dev = dcr->dev;
+
+   Dmsg1(100, "truncate_dev %s\n", dev->print_name());
+   if (dev->is_tape()) {
       return true;                    /* we don't really truncate tapes */
       /* maybe we should rewind and write and eof ???? */
    }
    
-   if (is_dvd()) {
-      return truncate_dvd(dcr);
+   if (dev->is_dvd()) {
+      return truncate_dvd_dev(dcr);
    }
    
-   if (ftruncate(fd, 0) != 0) {
+   if (ftruncate(dev->fd, 0) != 0) {
       berrno be;
-      Mmsg2(errmsg, _("Unable to truncate device %s. ERR=%s\n"), 
-            print_name(), be.strerror());
-      return false;
-   }
-   return true;
-}
-
-/* Mount the device.
- * If timeout, wait until the mount command returns 0.
- * If !timeout, try to mount the device only once.
- */
-bool DEVICE::mount(int timeout) 
-{
-   Dmsg0(90, "Enter mount\n");
-   if (is_mounted()) {
-      return true;
-   } else if (requires_mount()) {
-      return do_mount(1, timeout);
-   }       
-   return true;
-}
-
-/* Unmount the device
- * If timeout, wait until the unmount command returns 0.
- * If !timeout, try to unmount the device only once.
- */
-bool DEVICE::unmount(int timeout) 
-{
-   Dmsg0(90, "Enter unmount_dvd\n");
-   if (is_mounted()) {
-      return do_mount(0, timeout);
-   }
-   return true;
-}
-
-/* (Un)mount the device */
-bool DEVICE::do_mount(int mount, int dotimeout) 
-{
-   POOL_MEM ocmd(PM_FNAME);
-   POOLMEM *results;
-   char *icmd;
-   int status, timeout;
-   
-   sm_check(__FILE__, __LINE__, false);
-   if (mount) {
-      if (is_mounted()) {
-         Dmsg0(200, "======= mount=1\n");
-         return true;
-      }
-      icmd = device->mount_command;
-   } else {
-      if (!is_mounted()) {
-         Dmsg0(200, "======= mount=0\n");
-         return true;
-      }
-      icmd = device->unmount_command;
-   }
-   
-   edit_mount_codes(ocmd, icmd);
-   
-   Dmsg2(000, "do_mount_dvd: 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. */
-      timeout = 1;
-   } else {
-      timeout = 0;
-   }
-   results = get_memory(2000);
-   results[0] = 0;
-   /* If busy retry each second */
-   while ((status = run_program_full_output(ocmd.c_str(), 
-                       max_open_wait/2, results)) != 0) {
-      /* Doesn't work with internationalisation (This is not a problem) */
-      if (fnmatch("*is already mounted on", results, 0) == 0) {
-         break;
-      }
-      if (timeout-- > 0) {
-         /* Sometimes the device cannot be mounted because it is already mounted.
-          * Try to unmount it, then remount it */
-         if (mount) {
-            Dmsg1(400, "Trying to unmount the device %s...\n", print_name());
-            do_mount(0, 0);
-         }
-         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);
-      /*
-       * Now, just to be sure it is not mounted, try to read the
-       *  filesystem.
-       */
-      DIR* dp;
-      struct dirent *entry, *result;
-      int name_max;
-      int count;
-      
-      name_max = pathconf(".", _PC_NAME_MAX);
-      if (name_max < 1024) {
-         name_max = 1024;
-      }
-         
-      if (!(dp = opendir(device->mount_point))) {
-         berrno be;
-         dev_errno = errno;
-         Dmsg3(29, "do_mount: failed to open dir %s (dev=%s), ERR=%s\n", 
-               device->mount_point, print_name(), be.strerror());
-         goto get_out;
-      }
-      
-      entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
-      count = 0;
-      while (1) {
-         if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
-            dev_errno = EIO;
-            Dmsg2(129, "do_mount: failed to find suitable file in dir %s (dev=%s)\n", 
-                  device->mount_point, print_name());
-            break;
-         }
-         if ((strcmp(result->d_name, ".")) && (strcmp(result->d_name, "..")) && (strcmp(result->d_name, ".keep"))) {
-            count++; /* result->d_name != ., .. or .keep (Gentoo-specific) */
-            break;
-         } else {
-            Dmsg2(129, "do_mount: ignoring %s in %s\n", result->d_name, device->mount_point);
-         }
-      }
-      free(entry);
-      closedir(dp);
-      
-      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 */
-      }
-get_out:
-      set_mounted(false);
-      sm_check(__FILE__, __LINE__, false);
-      free_pool_memory(results);
-      Dmsg0(200, "============ mount=0\n");
+      Mmsg2(dev->errmsg, _("Unable to truncate device %s. ERR=%s\n"), 
+            dev->print_name(), be.strerror());
       return false;
    }
-   
-   set_mounted(mount);              /* set/clear mounted flag */
-   free_pool_memory(results);
-   Dmsg1(200, "============ mount=%d\n", mount);
    return true;
 }
 
-/*
- * Edit codes into (Un)MountCommand, Write(First)PartCommand
- *  %% = %
- *  %a = archive device name
- *  %e = erase (set if cannot mount and first part)
- *  %n = part number
- *  %m = mount point
- *  %v = last part name
- *
- *  omsg = edited output message
- *  imsg = input string containing edit codes (%x)
- *
- */
-void DEVICE::edit_mount_codes(POOL_MEM &omsg, const char *imsg)
-{
-   const char *p;
-   const char *str;
-   char add[20];
-   
-   POOL_MEM archive_name(PM_FNAME);
-
-   omsg.c_str()[0] = 0;
-   Dmsg1(800, "edit_mount_codes: %s\n", imsg);
-   for (p=imsg; *p; p++) {
-      if (*p == '%') {
-         switch (*++p) {
-         case '%':
-            str = "%";
-            break;
-         case 'a':
-            str = dev_name;
-            break;
-         case 'e':
-            if (num_parts == 0) {
-               str = "1";
-            } else {
-               str = "0";
-            }
-            break;
-         case 'n':
-            bsnprintf(add, sizeof(add), "%d", part);
-            str = add;
-            break;
-         case 'm':
-            str = device->mount_point;
-            break;
-         case 'v':
-            make_spooled_dvd_filename(this, archive_name);
-            str = archive_name.c_str();
-            break;
-         default:
-            add[0] = '%';
-            add[1] = *p;
-            add[2] = 0;
-            str = add;
-            break;
-         }
-      } else {
-         add[0] = *p;
-         add[1] = 0;
-         str = add;
-      }
-      Dmsg1(1900, "add_str %s\n", str);
-      pm_strcat(omsg, (char *)str);
-      Dmsg1(1800, "omsg=%s\n", omsg.c_str());
-   }
-}
-
-
 /* Return the resource name for the device */
 const char *DEVICE::name() const
 {
@@ -1978,35 +1792,41 @@ uint32_t dev_file(DEVICE *dev)
 /*
  * Free memory allocated for the device
  */
-void DEVICE::term(void)
+void
+term_dev(DEVICE *dev)
 {
-   Dmsg1(900, "term dev: %s\n", print_name());
-   close();
-   if (dev_name) {
-      free_memory(dev_name);
-      dev_name = NULL;
-   }
-   if (prt_name) {
-      free_memory(prt_name);
-      prt_name = NULL;
-   }
-   if (errmsg) {
-      free_pool_memory(errmsg);
-      errmsg = NULL;
-   }
-   pthread_mutex_destroy(&mutex);
-   pthread_cond_destroy(&wait);
-   pthread_cond_destroy(&wait_next_vol);
-   pthread_mutex_destroy(&spool_mutex);
-   rwl_destroy(&lock);
-   if (attached_dcrs) {
-      delete attached_dcrs;
-      attached_dcrs = NULL;
-   }
-   if (device) {
-      device->dev = NULL;
-   }
-   free((char *)this);
+   if (!dev) {
+      dev->dev_errno = EBADF;
+      Mmsg0(dev->errmsg, _("Bad call to term_dev. Device not open\n"));
+      Emsg0(M_FATAL, 0, dev->errmsg);
+      return;
+   }
+   Dmsg1(29, "term_dev: %s\n", dev->print_name());
+   do_close(dev);
+   if (dev->dev_name) {
+      free_memory(dev->dev_name);
+      dev->dev_name = NULL;
+   }
+   if (dev->prt_name) {
+      free_memory(dev->prt_name);
+      dev->prt_name = NULL;
+   }
+   if (dev->errmsg) {
+      free_pool_memory(dev->errmsg);
+      dev->errmsg = NULL;
+   }
+   pthread_mutex_destroy(&dev->mutex);
+   pthread_cond_destroy(&dev->wait);
+   pthread_cond_destroy(&dev->wait_next_vol);
+   pthread_mutex_destroy(&dev->spool_mutex);
+   rwl_destroy(&dev->lock);
+   if (dev->attached_dcrs) {
+      delete dev->attached_dcrs;
+      dev->attached_dcrs = NULL;
+   }
+   if (dev->state & ST_MALLOC) {
+      free_pool_memory((POOLMEM *)dev);
+   }
 }
 
 /*
@@ -2082,10 +1902,10 @@ void set_os_device_parameters(DEVICE *dev)
       }
       mt_com.mt_op = MTSETDRVBUFFER;
       mt_com.mt_count = MT_ST_CLEARBOOLEANS;
-      if (!dev->has_cap(CAP_TWOEOF)) {
+      if (!dev_cap(dev, CAP_TWOEOF)) {
          mt_com.mt_count |= MT_ST_TWO_FM;
       }
-      if (dev->has_cap(CAP_EOM)) {
+      if (dev_cap(dev, CAP_EOM)) {
          mt_com.mt_count |= MT_ST_FAST_MTEOM;
       }
       if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
@@ -2143,7 +1963,7 @@ void set_os_device_parameters(DEVICE *dev)
 
 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat)
 {
-   return dev->has_cap(CAP_MTIOCGET) && 
+   return dev_cap(dev, CAP_MTIOCGET) && 
           ioctl(dev->fd, MTIOCGET, (char *)mt_stat) == 0 &&
           mt_stat->mt_fileno >= 0;
 }