]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/dev.c
dhb Added comments for each function. There are questions above
[bacula/bacula] / bacula / src / stored / dev.c
index 7a57709b5b7fae04a530724899dc31b1efd1810b..689fa9589cd43b48cefb2d1a062fbb27cb01d829 100644 (file)
@@ -218,7 +218,7 @@ init_dev(JCR *jcr, DEVRES *device)
    dev->errmsg = get_pool_memory(PM_EMSG);
    *dev->errmsg = 0;
 
-   if ((errstat = pthread_mutex_init(&dev->mutex, NULL)) != 0) {
+   if ((errstat = pthread_mutex_init(&dev->m_mutex, NULL)) != 0) {
       berrno be;
       dev->dev_errno = errstat;
       Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
@@ -242,12 +242,14 @@ init_dev(JCR *jcr, DEVRES *device)
       Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
    }
+#ifdef xxx
    if ((errstat = rwl_init(&dev->lock)) != 0) {
       berrno be;
       dev->dev_errno = errstat;
       Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
    }
+#endif
 
    dev->clear_opened();
    dev->attached_dcrs = New(dlist(dcr, &dcr->dev_link));
@@ -782,19 +784,19 @@ void DEVICE::block(int why)
 {
    lock_device(this);
    block_device(this, why);
-   V(mutex);
+   unlock();
 }
 
 void DEVICE::unblock()
 {  
-   P(mutex);
+   lock();   
    unblock_device(this);
-   V(mutex);
+   unlock();
 }
 
 const char *DEVICE::print_blocked() const 
 {
-   switch (dev_blocked) {
+   switch (m_blocked) {
    case BST_NOT_BLOCKED:
       return "BST_NOT_BLOCKED";
    case BST_UNMOUNTED:
@@ -848,9 +850,9 @@ void DEVICE::set_ateot()
 bool DEVICE::eod(DCR *dcr)
 {
    struct mtop mt_com;
-   struct mtget mt_stat;
    bool ok = true;
    boffset_t pos;
+   int32_t os_file;
 
    if (m_fd < 0) {
       dev_errno = EBADF;
@@ -891,7 +893,7 @@ bool DEVICE::eod(DCR *dcr)
    if (has_cap(CAP_FASTFSF) && !has_cap(CAP_EOM)) {
       Dmsg0(100,"Using FAST FSF for EOM\n");
       /* If unknown position, rewind */
-      if (!dev_get_os_pos(this, &mt_stat)) {
+      if (get_os_tape_file() < 0) {
         if (!rewind(NULL)) {
           return false;
         }
@@ -924,16 +926,17 @@ bool DEVICE::eod(DCR *dcr)
          return false;
       }
 
-      if (!dev_get_os_pos(this, &mt_stat)) {
+      os_file = get_os_tape_file();
+      if (os_file < 0) {
          berrno be;
          clrerror(-1);
          Mmsg2(errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
             print_name(), be.strerror());
          return false;
       }
-      Dmsg2(100, "EOD file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
+      Dmsg1(100, "EOD file=%d\n", os_file);
       set_ateof();
-      file = mt_stat.mt_fileno;
+      file = os_file;
    } else {
 #else
    {
@@ -958,12 +961,12 @@ bool DEVICE::eod(DCR *dcr)
           * Avoid infinite loop by ensuring we advance.
           */
          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_num, mt_stat.mt_fileno);
-               file = mt_stat.mt_fileno;
+            os_file = get_os_tape_file();
+            if (os_file >= 0) {
+               Dmsg2(100, "Adjust file from %d to %d\n", file_num, os_file);
+               file = os_file;
             }       
             break;
          }
@@ -975,13 +978,13 @@ bool DEVICE::eod(DCR *dcr)
     * the second EOF.
     */
    if (has_cap(CAP_BSFATEOM)) {
-      struct mtget mt_stat;
       /* Backup over EOF */
       ok = bsf(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;
+      os_file = get_os_tape_file();
+      if (os_file >= 0) {
+         Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", file , os_file);
+         file = os_file;
       } else {
          file++;                       /* wing it -- not correct on all OSes */
       }
@@ -1253,7 +1256,7 @@ bool DEVICE::offline_or_rewind()
  */
 bool DEVICE::fsf(int num)
 {
-   struct mtget mt_stat;
+   int32_t os_file;
    struct mtop mt_com;
    int stat = 0;
 
@@ -1290,7 +1293,7 @@ bool DEVICE::fsf(int num)
       mt_com.mt_op = MTFSF;
       mt_com.mt_count = num;
       stat = tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
-      if (stat < 0 || !dev_get_os_pos(this, &mt_stat)) {
+      if (stat < 0 || (os_file=get_os_tape_file()) < 0) {
          berrno be;
          set_eot();
          Dmsg0(200, "Set ST_EOT\n");
@@ -1300,9 +1303,9 @@ bool DEVICE::fsf(int num)
          Dmsg1(200, "%s", errmsg);
          return false;
       }
-      Dmsg2(200, "fsf file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
+      Dmsg1(200, "fsf file=%d\n", os_file);
       set_ateof();
-      file = mt_stat.mt_fileno;
+      file = os_file;
       return true;
 
    /*
@@ -1699,7 +1702,6 @@ bool DEVICE::weof(int num)
 void DEVICE::clrerror(int func)
 {
    const char *msg = NULL;
-   struct mtget mt_stat;
    char buf[100];
 
    dev_errno = errno;         /* save errno */
@@ -1802,7 +1804,7 @@ void DEVICE::clrerror(int func)
     */
 
    /* On some systems such as NetBSD, this clears all errors */
-   tape_ioctl(m_fd, MTIOCGET, (char *)&mt_stat);
+   get_os_tape_file();
 
 /* Found on Linux */
 #ifdef MTIOCLRERR
@@ -2257,6 +2259,18 @@ const char *DEVICE::name() const
    return device->hdr.name;
 }
 
+/* Returns file position on tape or -1 */
+int32_t DEVICE::get_os_tape_file()
+{
+   struct mtget mt_stat;
+
+   if (has_cap(CAP_MTIOCGET) &&
+       tape_ioctl(m_fd, MTIOCGET, (char *)&mt_stat) == 0) {
+      return mt_stat.mt_fileno;
+   }
+   return -1;
+}
+
 char *
 dev_vol_name(DEVICE *dev)
 {
@@ -2283,11 +2297,11 @@ void DEVICE::term(void)
       free_pool_memory(errmsg);
       errmsg = NULL;
    }
-   pthread_mutex_destroy(&mutex);
+   pthread_mutex_destroy(&m_mutex);
    pthread_cond_destroy(&wait);
    pthread_cond_destroy(&wait_next_vol);
    pthread_mutex_destroy(&spool_mutex);
-   rwl_destroy(&lock);
+// rwl_destroy(&lock);
    if (attached_dcrs) {
       delete attached_dcrs;
       attached_dcrs = NULL;