1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2013 Free Software Foundation Europe e.V.
5    Copyright (C) 2013-2019 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Pulled out of dev.h
24  *
25  * Kern Sibbald, MMXIII
26  *
27  */
28 /**
29  * @file
30  * volume management definitions
31  *
32  */
33 
34 /*
35  * Some details of how volume reservations work
36  *
37  * class VolumeReservationItem:
38  *   SetInUse()     volume being used on current drive
39  *   ClearInUse()   no longer being used.  Can be re-used or moved.
40  *   SetSwapping()   set volume being moved to another drive
41  *   IsSwapping()    volume is being moved to another drive
42  *   ClearSwapping() volume normal
43  *
44  */
45 
46 #ifndef BAREOS_STORED_VOL_MGR_H_
47 #define BAREOS_STORED_VOL_MGR_H_ 1
48 
49 class dlist;
50 
51 namespace storagedaemon {
52 
53 class VolumeReservationItem;
54 VolumeReservationItem* vol_walk_start();
55 VolumeReservationItem* VolWalkNext(VolumeReservationItem* prev_vol);
56 void VolWalkEnd(VolumeReservationItem* vol);
57 VolumeReservationItem* read_vol_walk_start();
58 VolumeReservationItem* ReadVolWalkNext(VolumeReservationItem* prev_vol);
59 void ReadVolWalkEnd(VolumeReservationItem* vol);
60 
61 /**
62  * Volume reservation class -- see vol_mgr.c and reserve.c
63  */
64 class VolumeReservationItem {
65   bool swapping_{false};          /**< set when swapping to another drive */
66   bool in_use_{false};            /**< set when volume reserved or in use */
67   bool reading_{false};           /**< set when reading */
68   slot_number_t slot_{0};         /**< slot of swapping volume */
69   uint32_t JobId_{0};             /**< JobId for read volumes */
70   volatile int32_t use_count_{0}; /**< Use count */
71   pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER; /**< Vol muntex */
72  public:
73   dlink link;
74   char* vol_name{nullptr}; /**< Volume name */
75   Device* dev{nullptr};    /**< Pointer to device to which we are attached */
76 
InitMutex()77   void InitMutex() { pthread_mutex_init(&mutex_, NULL); }
DestroyMutex()78   void DestroyMutex() { pthread_mutex_destroy(&mutex_); }
Lock()79   void Lock() { P(mutex_); }
Unlock()80   void Unlock() { V(mutex_); }
IncUseCount(void)81   void IncUseCount(void)
82   {
83     P(mutex_);
84     use_count_++;
85     V(mutex_);
86   }
DecUseCount(void)87   void DecUseCount(void)
88   {
89     P(mutex_);
90     use_count_--;
91     V(mutex_);
92   }
UseCount()93   int32_t UseCount() const { return use_count_; }
IsSwapping()94   bool IsSwapping() const { return swapping_; }
is_reading()95   bool is_reading() const { return reading_; }
IsWriting()96   bool IsWriting() const { return !reading_; }
SetReading()97   void SetReading() { reading_ = true; }
clear_reading()98   void clear_reading() { reading_ = false; }
SetSwapping()99   void SetSwapping() { swapping_ = true; }
ClearSwapping()100   void ClearSwapping() { swapping_ = false; }
IsInUse()101   bool IsInUse() const { return in_use_; }
SetInUse()102   void SetInUse() { in_use_ = true; }
ClearInUse()103   void ClearInUse() { in_use_ = false; }
SetSlotNumber(slot_number_t slot)104   void SetSlotNumber(slot_number_t slot) { slot_ = slot; }
InvalidateSlotNumber()105   void InvalidateSlotNumber() { slot_ = kInvalidSlotNumber; }
GetSlot()106   slot_number_t GetSlot() const { return slot_; }
GetJobid()107   uint32_t GetJobid() const { return JobId_; }
SetJobid(uint32_t JobId)108   void SetJobid(uint32_t JobId) { JobId_ = JobId; }
109 };
110 
111 #define foreach_vol(vol) \
112   for (vol = vol_walk_start(); vol; (vol = VolWalkNext(vol)))
113 
114 #define endeach_vol(vol) VolWalkEnd(vol)
115 
116 #define foreach_read_vol(vol) \
117   for (vol = read_vol_walk_start(); vol; (vol = ReadVolWalkNext(vol)))
118 
119 #define endeach_read_vol(vol) ReadVolWalkEnd(vol)
120 
121 void InitVolListLock();
122 void TermVolListLock();
123 VolumeReservationItem* reserve_volume(DeviceControlRecord* dcr,
124                                       const char* VolumeName);
125 bool FreeVolume(Device* dev);
126 bool IsVolListEmpty();
127 dlist* dup_vol_list(JobControlRecord* jcr);
128 void FreeTempVolList(dlist* temp_vol_list);
129 bool VolumeUnused(DeviceControlRecord* dcr);
130 void CreateVolumeLists();
131 void FreeVolumeLists();
132 bool IsVolumeInUse(DeviceControlRecord* dcr);
133 void AddReadVolume(JobControlRecord* jcr, const char* VolumeName);
134 void RemoveReadVolume(JobControlRecord* jcr, const char* VolumeName);
135 
136 } /* namespace storagedaemon */
137 
138 #endif
139