1 /*
2    Bacula(R) - The Network Backup Solution
3 
4    Copyright (C) 2000-2020 Kern Sibbald
5 
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8 
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13 
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16 
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  * Definitions for locking and blocking functions in the SD
21  *
22  * Kern Sibbald, pulled out of dev.h June 2007
23  *
24  */
25 
26 
27 #ifndef __LOCK_H
28 #define __LOCK_H 1
29 
30 void    _lock_reservations(const char *file="**Unknown**", int line=0);
31 void    _unlock_reservations();
32 void    _lock_volumes(const char *file="**Unknown**", int line=0);
33 void    _unlock_volumes();
34 
35 #ifdef  SD_DEBUG_LOCK
36 
37 #define lock_reservations() \
38          do { Dmsg3(sd_dbglvl, "lock_reservations at %s:%d precnt=%d\n", \
39               __FILE__, __LINE__, \
40               reservations_lock_count); \
41               _lock_reservations(__FILE__, __LINE__); \
42               Dmsg0(sd_dbglvl, "lock_reservations: got lock\n"); \
43          } while (0)
44 #define unlock_reservations() \
45          do { Dmsg3(sd_dbglvl, "unlock_reservations at %s:%d precnt=%d\n", \
46               __FILE__, __LINE__, \
47               reservations_lock_count); \
48                    _unlock_reservations(); } while (0)
49 
50 #define lock_volumes() \
51          do { Dmsg3(sd_dbglvl, "lock_volumes at %s:%d precnt=%d\n", \
52               __FILE__, __LINE__, \
53               vol_list_lock_count); \
54               _lock_volumes(__FILE__, __LINE__); \
55               Dmsg0(sd_dbglvl, "lock_volumes: got lock\n"); \
56          } while (0)
57 
58 #define unlock_volumes() \
59          do { Dmsg3(sd_dbglvl, "unlock_volumes at %s:%d precnt=%d\n", \
60               __FILE__, __LINE__, \
61               vol_list_lock_count); \
62                    _unlock_volumes(); } while (0)
63 
64 #else
65 
66 #define lock_reservations() _lock_reservations(__FILE__, __LINE__)
67 #define unlock_reservations() _unlock_reservations()
68 #define lock_volumes() _lock_volumes(__FILE__, __LINE__)
69 #define unlock_volumes() _unlock_volumes()
70 
71 #endif
72 
73 #ifdef DEV_DEBUG_LOCK
74 #define Lock() dbg_Lock(__FILE__, __LINE__)
75 #define Unlock() dbg_Unlock(__FILE__, __LINE__)
76 #define rLock(locked) dbg_rLock(__FILE__, __LINE__, locked)
77 #define rUnlock() dbg_rUnlock(__FILE__, __LINE__)
78 #endif
79 
80 #ifdef SD_DEBUG_LOCK
81 #define Lock_acquire() dbg_Lock_acquire(__FILE__, __LINE__)
82 #define Unlock_acquire() dbg_Unlock_acquire(__FILE__, __LINE__)
83 #define Lock_read_acquire() dbg_Lock_read_acquire(__FILE__, __LINE__)
84 #define Unlock_read_acquire() dbg_Unlock_read_acquire(__FILE__, __LINE__)
85 #define Lock_VolCatInfo() dbg_Lock_VolCatInfo(__FILE__, __LINE__)
86 #define Unlock_VolCatInfo() dbg_Unlock_VolCatInfo(__FILE__, __LINE__)
87 #endif
88 
89 #define block_device(d, s)          _block_device(__FILE__, __LINE__, (d), s)
90 #define unblock_device(d)           _unblock_device(__FILE__, __LINE__, (d))
91 
92 #define obtain_device_block(d, p, r, s) (d)->_obtain_device_block(__FILE__, __LINE__, (p), (r), (s))
93 #define give_back_device_block(d, p) _give_back_device_block(__FILE__, __LINE__, (d), (p))
94 
95 /* m_blocked states (mutually exclusive) */
96 enum {
97    BST_NOT_BLOCKED = 0,               /* not blocked */
98    BST_UNMOUNTED,                     /* User unmounted device */
99    BST_WAITING_FOR_SYSOP,             /* Waiting for operator to mount tape */
100    BST_DOING_ACQUIRE,                 /* Opening/validating/moving tape */
101    BST_WRITING_LABEL,                 /* Labeling a tape */
102    BST_UNMOUNTED_WAITING_FOR_SYSOP,   /* User unmounted during wait for op */
103    BST_MOUNT,                         /* Mount request */
104    BST_DESPOOLING,                    /* Despooling -- i.e. multiple writes */
105    BST_RELEASING                      /* Releasing the device */
106 };
107 
108 typedef struct s_steal_lock {
109    pthread_t  no_wait_id;             /* id of no wait thread */
110    int        dev_blocked;            /* state */
111    int        dev_prev_blocked;       /* previous blocked state */
112    uint32_t   blocked_by;             /* previous blocker */
113 } bsteal_lock_t;
114 
115 /*
116  * Used in unblock() call
117  */
118 enum {
119    DEV_LOCKED = true,
120    DEV_UNLOCKED = false
121 };
122 
123 #endif
124