]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/dev.c
Clean up some SD message nos.
[bacula/bacula] / bacula / src / stored / dev.c
index 424936f4ee7a79acaf92e5cbd5fcc570ac01886a..e2228768825c71fa0afcd404ccad3baf3d5de3dd 100644 (file)
@@ -29,7 +29,7 @@
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
+   Copyright (C) 2000-2003 Kern Sibbald and John Walker
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -146,13 +146,12 @@ init_dev(DEVICE *dev, DEVRES *device)
    dev->capabilities = device->cap_bits;
    dev->min_block_size = device->min_block_size;
    dev->max_block_size = device->max_block_size;
-   dev->max_volume_jobs = device->max_volume_jobs;
-   dev->max_volume_files = device->max_volume_files;
    dev->max_volume_size = device->max_volume_size;
    dev->max_file_size = device->max_file_size;
    dev->volume_capacity = device->volume_capacity;
    dev->max_rewind_wait = device->max_rewind_wait;
    dev->max_open_wait = device->max_open_wait;
+   dev->max_open_vols = device->max_open_vols;
    dev->device = device;
 
    if (tape) {
@@ -224,7 +223,7 @@ open_dev(DEVICE *dev, char *VolName, int mode)
       dev->use_count++;
       Mmsg2(&dev->errmsg, _("WARNING!!!! device %s opened %d times!!!\n"), 
            dev->dev_name, dev->use_count);
-      Emsg0(M_WARNING, 0, dev->errmsg);
+      Emsg1(M_WARNING, 0, "%s", dev->errmsg);
       return dev->fd;
    }
    if (VolName) {
@@ -237,14 +236,26 @@ open_dev(DEVICE *dev, char *VolName, int mode)
    if (dev->state & (ST_TAPE|ST_FIFO)) {
       int timeout;
       Dmsg0(29, "open_dev: device is tape\n");
-      if (mode == READ_WRITE) {
+      if (mode == OPEN_READ_WRITE) {
         dev->mode = O_RDWR | O_BINARY;
-      } else {
+      } else if (mode == OPEN_READ_ONLY) {
         dev->mode = O_RDONLY | O_BINARY;
+      } else if (mode == OPEN_WRITE_ONLY) {
+        dev->mode = O_WRONLY | O_BINARY;
+      } else {
+         Emsg0(M_ABORT, 0, _("Illegal mode given to open_dev.\n")); 
       }
       timeout = dev->max_open_wait;
       errno = 0;
+      if (dev->state & ST_FIFO && timeout) {
+        /* Set open timer */
+        dev->tid = start_thread_timer(pthread_self(), timeout);
+      }
+      /* If busy retry each second for max_open_wait seconds */
       while ((dev->fd = open(dev->dev_name, dev->mode, MODE_RW)) < 0) {
+        if (errno == EAGAIN) {
+           continue;
+        }
         if (errno == EBUSY && timeout-- > 0) {
             Dmsg2(100, "Device %s busy. ERR=%s\n", dev->dev_name, strerror(errno));
            sleep(1);
@@ -253,6 +264,11 @@ open_dev(DEVICE *dev, char *VolName, int mode)
         dev->dev_errno = errno;
          Mmsg2(&dev->errmsg, _("stored: unable to open device %s: ERR=%s\n"), 
               dev->dev_name, strerror(dev->dev_errno));
+        /* Stop any open timer we set */
+        if (dev->tid) {
+           stop_thread_timer(dev->tid);
+           dev->tid = 0;
+        }
         Emsg0(M_FATAL, 0, dev->errmsg);
         break;
       }
@@ -262,6 +278,11 @@ open_dev(DEVICE *dev, char *VolName, int mode)
         dev->use_count++;
         update_pos_dev(dev);             /* update position */
       }
+      /* Stop any open() timer we started */
+      if (dev->tid) {
+        stop_thread_timer(dev->tid);
+        dev->tid = 0;
+      }
       Dmsg1(29, "open_dev: tape %d opened\n", dev->fd);
    } else {
       /*
@@ -274,12 +295,17 @@ open_dev(DEVICE *dev, char *VolName, int mode)
       }
       pm_strcat(&archive_name, VolName);
       Dmsg1(29, "open_dev: device is disk %s\n", archive_name);
-      if (mode == READ_WRITE) {
+      if (mode == OPEN_READ_WRITE) {
         dev->mode = O_CREAT | O_RDWR | O_BINARY;
-      } else {
+      } else if (mode == OPEN_READ_ONLY) {
         dev->mode = O_RDONLY | O_BINARY;
+      } else if (mode == OPEN_WRITE_ONLY) {
+        dev->mode = O_WRONLY | O_BINARY;
+      } else {
+         Emsg0(M_ABORT, 0, _("Illegal mode given to open_dev.\n")); 
       }
-      if ((dev->fd = open(archive_name, dev->mode, MODE_RW)) < 0) {
+      /* If creating file, give 0640 permissions */
+      if ((dev->fd = open(archive_name, dev->mode, 0640)) < 0) {
         dev->dev_errno = errno;
          Mmsg2(&dev->errmsg, _("Could not open: %s, ERR=%s\n"), archive_name, strerror(dev->dev_errno));
         Emsg0(M_FATAL, 0, dev->errmsg);
@@ -384,7 +410,7 @@ eod_dev(DEVICE *dev)
       }
       return 0;
    }
-   if (dev->capabilities & CAP_EOM) {
+   if (dev_cap(dev, CAP_EOM)) {
       mt_com.mt_op = MTEOM;
       mt_com.mt_count = 1;
       if ((stat=ioctl(dev->fd, MTIOCTOP, (char *)&mt_com)) < 0) {
@@ -419,9 +445,20 @@ eod_dev(DEVICE *dev)
         }
       }
    }
-   update_pos_dev(dev);                     /* update position */
+   /*
+    * Some drivers leave us after second EOF when doing
+    * MTEOM, so we must backup so that appending overwrites
+    * the second EOF.
+    */
+   if (dev_cap(dev, CAP_BSFATEOM)) {
+      stat =  (bsf_dev(dev, 1) == 0);
+      dev->file++;                   /* keep same file */
+   } else {
+      update_pos_dev(dev);                  /* update position */
+      stat = 1;
+   }
    Dmsg1(200, "EOD dev->file=%d\n", dev->file);
-   return 1;
+   return stat;
 }
 
 /*
@@ -635,7 +672,6 @@ fsf_dev(DEVICE *dev, int num)
 { 
    struct mtop mt_com;
    int stat = 0;
-   char rbuf[1024];
 
    if (dev->fd < 0) {
       dev->dev_errno = EBADF;
@@ -652,26 +688,37 @@ fsf_dev(DEVICE *dev, int num)
       Mmsg1(&dev->errmsg, _("Device %s at End of Tape.\n"), dev->dev_name);
       return 0;
    }
-   if (dev->state & ST_EOF)
+   if (dev->state & ST_EOF) {
       Dmsg0(200, "ST_EOF set on entry to FSF\n");
-   if (dev->state & ST_EOT)
+   }
+   if (dev->state & ST_EOT) {
       Dmsg0(200, "ST_EOT set on entry to FSF\n");
+   }
       
    Dmsg0(29, "fsf_dev\n");
    dev->block_num = 0;
-   if (dev->capabilities & CAP_FSF) {
+   if (dev_cap(dev, CAP_FSF)) {
+      POOLMEM *rbuf;
+      int rbuf_len;
       Dmsg0(200, "FSF has cap_fsf\n");
+      if (dev->max_block_size == 0) {
+        rbuf_len = DEFAULT_BLOCK_SIZE;
+      } else {
+        rbuf_len = dev->max_block_size;
+      }
+      rbuf = get_memory(rbuf_len);
       mt_com.mt_op = MTFSF;
       mt_com.mt_count = 1;
       while (num-- && !(dev->state & ST_EOT)) {
-         Dmsg0(200, "Doing read for fsf\n");
-        if ((stat = read(dev->fd, rbuf, sizeof(rbuf))) < 0) {
+         Dmsg0(200, "Doing read before fsf\n");
+        if ((stat = read(dev->fd, (char *)rbuf, rbuf_len)) < 0) {
            if (errno == ENOMEM) {     /* tape record exceeds buf len */
-              stat = sizeof(rbuf);   /* This is OK */
+              stat = rbuf_len;        /* This is OK */
            } else {
               dev->state |= ST_EOT;
               clrerror_dev(dev, -1);
-               Dmsg1(200, "Set ST_EOT read error %d\n", dev->dev_errno);
+               Dmsg2(200, "Set ST_EOT read errno=%d. ERR=%s\n", dev->dev_errno,
+                 strerror(dev->dev_errno));
                Mmsg2(&dev->errmsg, _("read error on %s. ERR=%s.\n"),
                  dev->dev_name, strerror(dev->dev_errno));
                Dmsg1(200, "%s", dev->errmsg);
@@ -712,6 +759,7 @@ fsf_dev(DEVICE *dev, int num)
            dev->file_addr = 0;
         }   
       }
+      free_memory(rbuf);
    
    /*
     * No FSF, so use FSR to simulate it
@@ -857,6 +905,8 @@ bsr_dev(DEVICE *dev, int num)
 
 /*
  * Write an end of file on the device
+ *   Returns: 0 on success
+ *           non-zero on failure
  */
 int 
 weof_dev(DEVICE *dev, int num)
@@ -995,6 +1045,10 @@ static void do_close(DEVICE *dev)
    memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
    memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
    dev->use_count--;
+   if (dev->tid) {
+      stop_thread_timer(dev->tid);
+      dev->tid = 0;
+   }
 }
 
 /* 
@@ -1084,7 +1138,7 @@ term_dev(DEVICE *dev)
       Emsg0(M_FATAL, 0, dev->errmsg);
       return;
    }
-   close_dev(dev);
+   do_close(dev);
    Dmsg0(29, "term_dev\n");
    if (dev->dev_name) {
       free_memory(dev->dev_name);