Tizen 2.1 base
[external/device-mapper.git] / lib / locking / locking.h
1 /*
2  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.  
3  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
4  *
5  * This file is part of LVM2.
6  *
7  * This copyrighted material is made available to anyone wishing to use,
8  * modify, copy, or redistribute it subject to the terms and conditions
9  * of the GNU Lesser General Public License v.2.1.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program; if not, write to the Free Software Foundation,
13  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14  */
15
16 #ifndef _LVM_LOCKING_H
17 #define _LVM_LOCKING_H
18
19 #include "uuid.h"
20 #include "config.h"
21
22 int init_locking(int type, struct cmd_context *cmd, int suppress_messages);
23 void fin_locking(void);
24 void reset_locking(void);
25 int vg_write_lock_held(void);
26 int locking_is_clustered(void);
27
28 int remote_lock_held(const char *vol);
29
30 /*
31  * LCK_VG:
32  *   Lock/unlock on-disk volume group data.
33  *   Use VG_ORPHANS to lock all orphan PVs.
34  *   Use VG_GLOBAL as a global lock and to wipe the internal cache.
35  *   char *vol holds volume group name.
36  *   Set LCK_CACHE flag when manipulating 'vol' metadata in the internal cache.
37  *   (Like commit, revert or invalidate metadata.)
38  *   If more than one lock needs to be held simultaneously, they must be
39  *   acquired in alphabetical order of 'vol' (to avoid deadlocks), with
40  *   VG_ORPHANS last.
41  *
42  * LCK_LV:
43  *   Lock/unlock an individual logical volume
44  *   char *vol holds lvid
45  */
46 int lock_vol(struct cmd_context *cmd, const char *vol, uint32_t flags);
47
48 /*
49  * Internal locking representation.
50  *   LCK_VG: Uses prefix V_ unless the vol begins with # (i.e. #global or #orphans)
51  *           or the LCK_CACHE flag is set when it uses the prefix P_.
52  * If LCK_CACHE is set, we do not take out a real lock.
53  * NB In clustered situations, LCK_CACHE is not propagated directly to remote nodes.
54  * (It can be deduced from lock name.)
55  */
56
57 /*
58  * Does the LVM1 driver have this VG active?
59  */
60 int check_lvm1_vg_inactive(struct cmd_context *cmd, const char *vgname);
61
62 /*
63  * Lock type - these numbers are the same as VMS and the IBM DLM
64  */
65 #define LCK_TYPE_MASK   0x00000007U
66
67 #define LCK_NULL        0x00000000U     /* LCK$_NLMODE */
68 #define LCK_READ        0x00000001U     /* LCK$_CRMODE */
69                                         /* LCK$_CWMODE */
70 #define LCK_PREAD       0x00000003U     /* LCK$_PRMODE */
71 #define LCK_WRITE       0x00000004U     /* LCK$_PWMODE */
72 #define LCK_EXCL        0x00000005U     /* LCK$_EXMODE */
73 #define LCK_UNLOCK      0x00000006U     /* This is ours */
74
75 /*
76  * Lock flags - these numbers are the same as DLM
77  */
78 #define LCKF_NOQUEUE    0x00000001U     /* LKF$_NOQUEUE */
79 #define LCKF_CONVERT    0x00000004U     /* LKF$_CONVERT */
80
81 /*
82  * Lock scope
83  */
84 #define LCK_SCOPE_MASK  0x00000008U
85 #define LCK_VG          0x00000000U
86 #define LCK_LV          0x00000008U
87
88 /*
89  * Lock bits
90  */
91 #define LCK_NONBLOCK    0x00000010U     /* Don't block waiting for lock? */
92 #define LCK_HOLD        0x00000020U     /* Hold lock when lock_vol returns? */
93 #define LCK_LOCAL       0x00000040U     /* Don't propagate to other nodes */
94 #define LCK_CLUSTER_VG  0x00000080U     /* VG is clustered */
95 #define LCK_CACHE       0x00000100U     /* Operation on cache only using P_ lock */
96 #define LCK_ORIGIN_ONLY 0x00000200U     /* Operation should bypass any snapshots */
97
98 /*
99  * Additional lock bits for cluster communication via args[1]
100  */
101 #define LCK_PARTIAL_MODE                0x01    /* Partial activation? */
102 #define LCK_MIRROR_NOSYNC_MODE          0x02    /* Mirrors don't require sync */
103 #define LCK_DMEVENTD_MONITOR_MODE       0x04    /* Register with dmeventd */
104 #define LCK_CONVERT                     0x08    /* Convert existing lock */
105 #define LCK_ORIGIN_ONLY_MODE            0x20    /* Same as above */
106
107 /*
108  * Special cases of VG locks.
109  */
110 #define VG_ORPHANS      "#orphans"
111 #define VG_GLOBAL       "#global"
112
113 /*
114  * Common combinations
115  */
116 #define LCK_NONE                (LCK_VG | LCK_NULL)
117
118 #define LCK_VG_READ             (LCK_VG | LCK_READ | LCK_HOLD)
119 #define LCK_VG_WRITE            (LCK_VG | LCK_WRITE | LCK_HOLD)
120 #define LCK_VG_UNLOCK           (LCK_VG | LCK_UNLOCK)
121 #define LCK_VG_DROP_CACHE       (LCK_VG | LCK_WRITE | LCK_CACHE)
122
123 /* FIXME: LCK_HOLD abused here */
124 #define LCK_VG_COMMIT           (LCK_VG | LCK_WRITE | LCK_CACHE | LCK_HOLD)
125 #define LCK_VG_REVERT           (LCK_VG | LCK_READ  | LCK_CACHE | LCK_HOLD)
126
127 #define LCK_VG_BACKUP           (LCK_VG | LCK_CACHE)
128
129 #define LCK_LV_EXCLUSIVE        (LCK_LV | LCK_EXCL)
130 #define LCK_LV_SUSPEND          (LCK_LV | LCK_WRITE)
131 #define LCK_LV_RESUME           (LCK_LV | LCK_UNLOCK)
132 #define LCK_LV_ACTIVATE         (LCK_LV | LCK_READ)
133 #define LCK_LV_DEACTIVATE       (LCK_LV | LCK_NULL)
134
135 #define LCK_MASK (LCK_TYPE_MASK | LCK_SCOPE_MASK)
136
137 #define LCK_LV_CLUSTERED(lv)    \
138         (vg_is_clustered((lv)->vg) ? LCK_CLUSTER_VG : 0)
139
140 #define lock_lv_vol(cmd, lv, flags)     \
141         (find_replicator_vgs((lv)) ? \
142                 lock_vol(cmd, (lv)->lvid.s, flags | LCK_LV_CLUSTERED(lv)) : \
143                 0)
144
145 #define unlock_vg(cmd, vol)     lock_vol(cmd, vol, LCK_VG_UNLOCK)
146 #define unlock_and_free_vg(cmd, vg, vol) \
147         do { \
148                 unlock_vg(cmd, vol); \
149                 free_vg(vg); \
150         } while (0)
151
152 #define resume_lv(cmd, lv)      lock_lv_vol(cmd, lv, LCK_LV_RESUME)
153 #define resume_lv_origin(cmd, lv)       lock_lv_vol(cmd, lv, LCK_LV_RESUME | LCK_ORIGIN_ONLY)
154 #define suspend_lv(cmd, lv)     lock_lv_vol(cmd, lv, LCK_LV_SUSPEND | LCK_HOLD)
155 #define suspend_lv_origin(cmd, lv)      lock_lv_vol(cmd, lv, LCK_LV_SUSPEND | LCK_HOLD | LCK_ORIGIN_ONLY)
156 #define deactivate_lv(cmd, lv)  lock_lv_vol(cmd, lv, LCK_LV_DEACTIVATE)
157 #define activate_lv(cmd, lv)    lock_lv_vol(cmd, lv, LCK_LV_ACTIVATE | LCK_HOLD)
158 #define activate_lv_excl(cmd, lv)       \
159                                 lock_lv_vol(cmd, lv, LCK_LV_EXCLUSIVE | LCK_HOLD)
160 #define activate_lv_local(cmd, lv)      \
161         lock_lv_vol(cmd, lv, LCK_LV_ACTIVATE | LCK_HOLD | LCK_LOCAL)
162 #define deactivate_lv_local(cmd, lv)    \
163         lock_lv_vol(cmd, lv, LCK_LV_DEACTIVATE | LCK_LOCAL)
164 #define drop_cached_metadata(vg)        \
165         lock_vol((vg)->cmd, (vg)->name, LCK_VG_DROP_CACHE)
166 #define remote_commit_cached_metadata(vg)       \
167         lock_vol((vg)->cmd, (vg)->name, LCK_VG_COMMIT)
168 #define remote_revert_cached_metadata(vg)       \
169         lock_vol((vg)->cmd, (vg)->name, LCK_VG_REVERT)
170 #define remote_backup_metadata(vg)      \
171         lock_vol((vg)->cmd, (vg)->name, LCK_VG_BACKUP)
172
173 /* Process list of LVs */
174 int suspend_lvs(struct cmd_context *cmd, struct dm_list *lvs);
175 int resume_lvs(struct cmd_context *cmd, struct dm_list *lvs);
176 int activate_lvs(struct cmd_context *cmd, struct dm_list *lvs, unsigned exclusive);
177
178 /* Interrupt handling */
179 void sigint_clear(void);
180 void sigint_allow(void);
181 void sigint_restore(void);
182 int sigint_caught(void);
183
184 #endif