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  * Pulled out of dev.h
21  *
22  * Kern Sibbald, MMXIII
23  *
24  */
25 
26 /*
27  * Some details of how volume reservations work
28  *
29  * class VOLRES:
30  *   set_in_use()     volume being used on current drive
31  *   clear_in_use()   no longer being used.  Can be re-used or moved.
32  *   set_swapping()   set volume being moved to another drive
33  *   is_swapping()    volume is being moved to another drive
34  *   clear_swapping() volume normal
35  *
36  */
37 
38 #ifndef __VOL_MGR_H
39 #define __VOL_MGR_H 1
40 
41 class VOLRES;
42 VOLRES *vol_walk_start();
43 VOLRES *vol_walk_next(VOLRES *prev_vol);
44 void vol_walk_end(VOLRES *vol);
45 
46 /*
47  * Volume reservation class -- see vol_mgr.c and reserve.c
48  */
49 class VOLRES {
50    bool m_swapping;                   /* set when swapping to another drive */
51    bool m_in_use;                     /* set when volume reserved or in use */
52    bool m_reading;                    /* set when reading */
53    int32_t m_slot;                    /* slot of swapping volume */
54    uint32_t m_JobId;                  /* JobId for read volumes */
55    volatile int32_t m_use_count;      /* Use count */
56    pthread_mutex_t m_mutex;           /* Vol muntex */
57 public:
58    dlink link;
59    char *vol_name;                    /* Volume name */
60    DEVICE *dev;                       /* Pointer to device to which we are attached */
61 
init_mutex()62    void init_mutex() { pthread_mutex_init(&m_mutex, NULL); };
destroy_mutex()63    void destroy_mutex() { pthread_mutex_destroy(&m_mutex); };
vLock()64    void vLock() { P(m_mutex); };
vUnlock()65    void vUnlock() { V(m_mutex); };
inc_use_count(void)66    void inc_use_count(void) {P(m_mutex); m_use_count++; V(m_mutex); };
dec_use_count(void)67    void dec_use_count(void) {P(m_mutex); m_use_count--; V(m_mutex); };
use_count()68    int32_t use_count() const { return m_use_count; };
is_swapping()69    bool is_swapping() const { return m_swapping; };
is_reading()70    bool is_reading() const { return m_reading; };
is_writing()71    bool is_writing() const { return !m_reading; };
set_reading()72    void set_reading() { m_reading = true; };
clear_reading()73    void clear_reading() { m_reading = false; };
set_swapping()74    void set_swapping() { m_swapping = true; };
clear_swapping()75    void clear_swapping() { m_swapping = false; };
is_in_use()76    bool is_in_use() const { return m_in_use; };
set_in_use()77    void set_in_use() { m_in_use = true; };
clear_in_use()78    void clear_in_use() { m_in_use = false; };
set_slot(int32_t slot)79    void set_slot(int32_t slot) { m_slot = slot; };
clear_slot()80    void clear_slot() { m_slot = -1; };
get_slot()81    int32_t get_slot() const { return m_slot; };
get_jobid()82    uint32_t get_jobid() const { return m_JobId; };
set_jobid(uint32_t JobId)83    void set_jobid(uint32_t JobId) { m_JobId = JobId; };
84 };
85 
86 #define foreach_vol(vol) \
87    for (vol=vol_walk_start(); vol; (vol=vol_walk_next(vol)) )
88 
89 #define endeach_vol(vol) vol_walk_end(vol)
90 
91 #endif
92