1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2006-2007 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2018 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    Affero 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  * Kern Sibbald, February MMVI
24  */
25 /**
26  * @file
27  * Definitions for reservation system.
28  */
29 
30 /**
31  *   Use Device command from Director
32  *   The DIR tells us what Device Name to use, the Media Type,
33  *      the Pool Name, and the Pool Type.
34  *
35  *    Ensure that the device exists and is opened, then store
36  *      the media and pool info in the JobControlRecord.  This class is used
37  *      only temporarily in this file.
38  */
39 
40 #ifndef BAREOS_STORED_RESERVE_H_
41 #define BAREOS_STORED_RESERVE_H_ 1
42 
43 class alist;
44 
45 namespace storagedaemon {
46 
47 class DirectorStorage {
48  public:
49   alist* device;
50   bool append;
51   char name[MAX_NAME_LENGTH];
52   char media_type[MAX_NAME_LENGTH];
53   char pool_name[MAX_NAME_LENGTH];
54   char pool_type[MAX_NAME_LENGTH];
55 };
56 
57 /* Reserve context */
58 class ReserveContext {
59  public:
60   JobControlRecord* jcr;
61   char* device_name;
62   DirectorStorage* store;
63   DeviceResource* device;
64   Device* low_use_drive;            /**< Low use drive candidate */
65   int num_writers;                  /**< for selecting low use drive */
66   bool try_low_use_drive;           /**< see if low use drive available */
67   bool any_drive;                   /**< Accept any drive if set */
68   bool PreferMountedVols;           /**< Prefer volumes already mounted */
69   bool exact_match;                 /**< Want exact volume */
70   bool have_volume;                 /**< Have DIR suggested vol name */
71   bool suitable_device;             /**< at least one device is suitable */
72   bool autochanger_only;            /**< look at autochangers only */
73   bool notify_dir;                  /**< Notify DIR about device */
74   bool append;                      /**< set if append device */
75   char VolumeName[MAX_NAME_LENGTH]; /**< Vol name suggested by DIR */
76 };
77 
78 
79 void InitReservationsLock();
80 void TermReservationsLock();
81 void _lockReservations(const char* file = "**Unknown**", int line = 0);
82 void _unLockReservations();
83 void _lockVolumes(const char* file = "**Unknown**", int line = 0);
84 void _unLockVolumes();
85 void _lockReadVolumes(const char* file = "**Unknown**", int line = 0);
86 void _unLockReadVolumes();
87 void UnreserveDevice(DeviceControlRecord* dcr);
88 bool FindSuitableDeviceForJob(JobControlRecord* jcr, ReserveContext& rctx);
89 int SearchResForDevice(ReserveContext& rctx);
90 void ReleaseReserveMessages(JobControlRecord* jcr);
91 
92 #ifdef SD_DEBUG_LOCK
93 extern int reservations_lock_count;
94 extern int vol_list_lock_count;
95 extern int read_vol_list_lock_count;
96 
97 #define LockReservations()                                                  \
98   do {                                                                      \
99     Dmsg3(sd_debuglevel, "LockReservations at %s:%d precnt=%d\n", __FILE__, \
100           __LINE__, reservations_lock_count);                               \
101     _lockReservations(__FILE__, __LINE__);                                  \
102     Dmsg0(sd_debuglevel, "LockReservations: got lock\n");                   \
103   } while (0)
104 #define UnlockReservations()                                                  \
105   do {                                                                        \
106     Dmsg3(sd_debuglevel, "UnlockReservations at %s:%d precnt=%d\n", __FILE__, \
107           __LINE__, reservations_lock_count);                                 \
108     _unLockReservations();                                                    \
109   } while (0)
110 #define LockVolumes()                                                  \
111   do {                                                                 \
112     Dmsg3(sd_debuglevel, "LockVolumes at %s:%d precnt=%d\n", __FILE__, \
113           __LINE__, vol_list_lock_count);                              \
114     _lockVolumes(__FILE__, __LINE__);                                  \
115     Dmsg0(sd_debuglevel, "LockVolumes: got lock\n");                   \
116   } while (0)
117 #define UnlockVolumes()                                                  \
118   do {                                                                   \
119     Dmsg3(sd_debuglevel, "UnlockVolumes at %s:%d precnt=%d\n", __FILE__, \
120           __LINE__, vol_list_lock_count);                                \
121     _unLockVolumes();                                                    \
122   } while (0)
123 #define LockReadVolumes()                                                  \
124   do {                                                                     \
125     Dmsg3(sd_debuglevel, "LockReadVolumes at %s:%d precnt=%d\n", __FILE__, \
126           __LINE__, read_vol_list_lock_count);                             \
127     _lockReadVolumes(__FILE__, __LINE__);                                  \
128     Dmsg0(sd_debuglevel, "LockReadVolumes: got lock\n");                   \
129   } while (0)
130 #define UnlockReadVolumes()                                                  \
131   do {                                                                       \
132     Dmsg3(sd_debuglevel, "UnlockReadVolumes at %s:%d precnt=%d\n", __FILE__, \
133           __LINE__, read_vol_list_lock_count);                               \
134     _unLockReadVolumes();                                                    \
135   } while (0)
136 #else
137 #define LockReservations() _lockReservations(__FILE__, __LINE__)
138 #define UnlockReservations() _unLockReservations()
139 #define LockVolumes() _lockVolumes(__FILE__, __LINE__)
140 #define UnlockVolumes() _unLockVolumes()
141 #define LockReadVolumes() _lockReadVolumes(__FILE__, __LINE__)
142 #define UnlockReadVolumes() _unLockReadVolumes()
143 #endif
144 
145 bool use_cmd(JobControlRecord* jcr);
146 
147 } /* namespace storagedaemon */
148 
149 #endif /* BAREOS_STORED_RESERVE_H_ */
150