From 9cab4c6a72ecf5222ce70a4963d1a60670ca3528 Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Sat, 4 Feb 2012 16:28:16 +0100 Subject: [PATCH] Lock read acquire in SD to prevent to read jobs getting the same thing --- bacula/src/stored/acquire.c | 13 ++++++++++--- bacula/src/stored/dev.c | 8 +++++++- bacula/src/stored/dev.h | 3 ++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/bacula/src/stored/acquire.c b/bacula/src/stored/acquire.c index fcd0f33b3a..96bdc062da 100644 --- a/bacula/src/stored/acquire.c +++ b/bacula/src/stored/acquire.c @@ -1,7 +1,7 @@ /* Bacula(R) - The Network Backup Solution - Copyright (C) 2002-2011 Free Software Foundation Europe e.V. + Copyright (C) 2002-2012 Free Software Foundation Europe e.V. The main author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. @@ -40,7 +40,6 @@ static void attach_dcr_to_dev(DCR *dcr); static void detach_dcr_from_dev(DCR *dcr); static void set_dcr_from_vol(DCR *dcr, VOL_LIST *vol); - /********************************************************************* * Acquire device for reading. * The drive should have previously been reserved by calling @@ -62,6 +61,7 @@ bool acquire_device_for_read(DCR *dcr) int vol_label_status; int retry = 0; + P(dev->read_acquire_mutex); Dmsg2(950, "dcr=%p dev=%p\n", dcr, dcr->dev); Dmsg2(950, "MediaType dcr=%s dev=%s\n", dcr->media_type, dev->device->media_type); dev->dblock(BST_DOING_ACQUIRE); @@ -151,7 +151,13 @@ bool acquire_device_for_read(DCR *dcr) release_reserve_messages(jcr); /* release queued messages */ unlock_reservations(); - if (stat == 1) { + if (stat == 1) { /* found new device to use */ + /* + * Switching devices, so acquire lock on new device, + * then release the old one. + */ + P(dcr->dev->read_acquire_mutex); + V(dev->read_acquire_mutex); dev = dcr->dev; /* get new device pointer */ dev->dblock(BST_DOING_ACQUIRE); @@ -349,6 +355,7 @@ get_out: } Dmsg2(950, "dcr=%p dev=%p\n", dcr, dcr->dev); Dmsg2(950, "MediaType dcr=%s dev=%s\n", dcr->media_type, dev->device->media_type); + V(dev->read_acquire_mutex); return ok; } diff --git a/bacula/src/stored/dev.c b/bacula/src/stored/dev.c index cece9f2476..8bf46d396a 100644 --- a/bacula/src/stored/dev.c +++ b/bacula/src/stored/dev.c @@ -1,7 +1,7 @@ /* Bacula® - The Network Backup Solution - Copyright (C) 2000-2010 Free Software Foundation Europe e.V. + Copyright (C) 2000-2012 Free Software Foundation Europe e.V. The main author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. @@ -291,6 +291,12 @@ init_dev(JCR *jcr, DEVRES *device) bthread_mutex_set_priority(&dev->m_mutex, PRIO_SD_DEV_ACCESS); bthread_mutex_set_priority(&dev->spool_mutex, PRIO_SD_DEV_SPOOL); bthread_mutex_set_priority(&dev->acquire_mutex, PRIO_SD_DEV_ACQUIRE); + if ((errstat = pthread_mutex_init(&dev->read_acquire_mutex, NULL)) != 0) { + berrno be; + dev->dev_errno = errstat; + Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(errstat)); + Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg); + } #ifdef xxx if ((errstat = rwl_init(&dev->lock)) != 0) { berrno be; diff --git a/bacula/src/stored/dev.h b/bacula/src/stored/dev.h index 28c378820b..d6d1004ee5 100644 --- a/bacula/src/stored/dev.h +++ b/bacula/src/stored/dev.h @@ -1,7 +1,7 @@ /* Bacula® - The Network Backup Solution - Copyright (C) 2000-2011 Free Software Foundation Europe e.V. + Copyright (C) 2000-2012 Free Software Foundation Europe e.V. The main author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. @@ -225,6 +225,7 @@ public: bthread_mutex_t m_mutex; /* access control */ bthread_mutex_t spool_mutex; /* mutex for updating spool_size */ bthread_mutex_t acquire_mutex; /* mutex for acquire code */ + pthread_mutex_t read_acquire_mutex; /* mutex for acquire read code */ pthread_cond_t wait; /* thread wait variable */ pthread_cond_t wait_next_vol; /* wait for tape to be mounted */ pthread_t no_wait_id; /* this thread must not wait */ -- 2.39.5