1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-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, pulled out of dev.h June 2007
24  */
25 /**
26  * @file
27  * Definitions for locking and blocking functions in the SD
28  */
29 
30 #ifndef BAREOS_STORED_LOCK_H_
31 #define BAREOS_STORED_LOCK_H_ 1
32 
33 #include "stored/dev.h"
34 
35 namespace storagedaemon {
36 
37 #ifdef SD_DEBUG_LOCK
38 #define r_dlock() _r_dlock(__FILE__, __LINE__);     /* in lock.c */
39 #define r_dunlock() _r_dunlock(__FILE__, __LINE__); /* in lock.c */
40 #define dlock() _dlock(__FILE__, __LINE__);         /* in lock.c */
41 #define dunlock() _dunlock(__FILE__, __LINE__);     /* in lock.c */
42 #endif
43 
44 #define BlockDevice(d, s) _blockDevice(__FILE__, __LINE__, (d), s)
45 #define UnblockDevice(d) _unBlockDevice(__FILE__, __LINE__, (d))
46 #define StealDeviceLock(d, p, s) \
47   _stealDeviceLock(__FILE__, __LINE__, (d), (p), s)
48 #define GiveBackDeviceLock(d, p) \
49   _giveBackDeviceLock(__FILE__, __LINE__, (d), (p))
50 
51 /**
52  * blocked_ states (mutually exclusive)
53  */
54 enum
55 {
56   BST_NOT_BLOCKED = 0,             /**< Not blocked */
57   BST_UNMOUNTED,                   /**< User unmounted device */
58   BST_WAITING_FOR_SYSOP,           /**< Waiting for operator to mount tape */
59   BST_DOING_ACQUIRE,               /**< Opening/validating/moving tape */
60   BST_WRITING_LABEL,               /**< Labeling a tape */
61   BST_UNMOUNTED_WAITING_FOR_SYSOP, /**< User unmounted during wait for op */
62   BST_MOUNT,                       /**< Mount request */
63   BST_DESPOOLING,                  /**< Despooling -- i.e. multiple writes */
64   BST_RELEASING                    /**< Releasing the device */
65 };
66 
67 typedef struct s_steal_lock {
68   pthread_t no_wait_id; /**< id of no wait thread */
69   int dev_blocked;      /**< state */
70   int dev_prev_blocked; /**< previous blocked state */
71 } bsteal_lock_t;
72 
73 /**
74  * Used in unblock() call
75  */
76 enum
77 {
78   DEV_LOCKED = true,
79   DEV_UNLOCKED = false
80 };
81 
82 class Device;
83 
84 void _lockDevice(const char* file, int line, Device* dev);
85 void _unlockDevice(const char* file, int line, Device* dev);
86 void _blockDevice(const char* file, int line, Device* dev, int state);
87 void _unBlockDevice(const char* file, int line, Device* dev);
88 void _stealDeviceLock(const char* file,
89                       int line,
90                       Device* dev,
91                       bsteal_lock_t* hold,
92                       int state);
93 void _giveBackDeviceLock(const char* file,
94                          int line,
95                          Device* dev,
96                          bsteal_lock_t* hold);
97 
98 } /* namespace storagedaemon */
99 
100 #endif
101