1*93c68cc4SChristoph Böhmwalder /* SPDX-License-Identifier: GPL-2.0-only */
2b8907339SPhilipp Reisner #ifndef DRBD_STATE_H
3b8907339SPhilipp Reisner #define DRBD_STATE_H
4b8907339SPhilipp Reisner
554761697SAndreas Gruenbacher struct drbd_device;
6bde89a9eSAndreas Gruenbacher struct drbd_connection;
7b8907339SPhilipp Reisner
8b8907339SPhilipp Reisner /**
9b8907339SPhilipp Reisner * DOC: DRBD State macros
10b8907339SPhilipp Reisner *
11b8907339SPhilipp Reisner * These macros are used to express state changes in easily readable form.
12b8907339SPhilipp Reisner *
13b8907339SPhilipp Reisner * The NS macros expand to a mask and a value, that can be bit ored onto the
14b8907339SPhilipp Reisner * current state as soon as the spinlock (req_lock) was taken.
15b8907339SPhilipp Reisner *
16b8907339SPhilipp Reisner * The _NS macros are used for state functions that get called with the
17b8907339SPhilipp Reisner * spinlock. These macros expand directly to the new state value.
18b8907339SPhilipp Reisner *
19b8907339SPhilipp Reisner * Besides the basic forms NS() and _NS() additional _?NS[23] are defined
20b8907339SPhilipp Reisner * to express state changes that affect more than one aspect of the state.
21b8907339SPhilipp Reisner *
22b8907339SPhilipp Reisner * E.g. NS2(conn, C_CONNECTED, peer, R_SECONDARY)
23b8907339SPhilipp Reisner * Means that the network connection was established and that the peer
24b8907339SPhilipp Reisner * is in secondary role.
25b8907339SPhilipp Reisner */
26b8907339SPhilipp Reisner #define role_MASK R_MASK
27b8907339SPhilipp Reisner #define peer_MASK R_MASK
28b8907339SPhilipp Reisner #define disk_MASK D_MASK
29b8907339SPhilipp Reisner #define pdsk_MASK D_MASK
30b8907339SPhilipp Reisner #define conn_MASK C_MASK
31b8907339SPhilipp Reisner #define susp_MASK 1
32b8907339SPhilipp Reisner #define user_isp_MASK 1
33b8907339SPhilipp Reisner #define aftr_isp_MASK 1
34b8907339SPhilipp Reisner #define susp_nod_MASK 1
35b8907339SPhilipp Reisner #define susp_fen_MASK 1
36b8907339SPhilipp Reisner
37b8907339SPhilipp Reisner #define NS(T, S) \
38b8907339SPhilipp Reisner ({ union drbd_state mask; mask.i = 0; mask.T = T##_MASK; mask; }), \
39b8907339SPhilipp Reisner ({ union drbd_state val; val.i = 0; val.T = (S); val; })
40b8907339SPhilipp Reisner #define NS2(T1, S1, T2, S2) \
41b8907339SPhilipp Reisner ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
42b8907339SPhilipp Reisner mask.T2 = T2##_MASK; mask; }), \
43b8907339SPhilipp Reisner ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
44b8907339SPhilipp Reisner val.T2 = (S2); val; })
45b8907339SPhilipp Reisner #define NS3(T1, S1, T2, S2, T3, S3) \
46b8907339SPhilipp Reisner ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
47b8907339SPhilipp Reisner mask.T2 = T2##_MASK; mask.T3 = T3##_MASK; mask; }), \
48b8907339SPhilipp Reisner ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
49b8907339SPhilipp Reisner val.T2 = (S2); val.T3 = (S3); val; })
50b8907339SPhilipp Reisner
51b8907339SPhilipp Reisner #define _NS(D, T, S) \
5278bae59bSPhilipp Reisner D, ({ union drbd_state __ns; __ns = drbd_read_state(D); __ns.T = (S); __ns; })
53b8907339SPhilipp Reisner #define _NS2(D, T1, S1, T2, S2) \
5478bae59bSPhilipp Reisner D, ({ union drbd_state __ns; __ns = drbd_read_state(D); __ns.T1 = (S1); \
55b8907339SPhilipp Reisner __ns.T2 = (S2); __ns; })
56b8907339SPhilipp Reisner #define _NS3(D, T1, S1, T2, S2, T3, S3) \
5778bae59bSPhilipp Reisner D, ({ union drbd_state __ns; __ns = drbd_read_state(D); __ns.T1 = (S1); \
58b8907339SPhilipp Reisner __ns.T2 = (S2); __ns.T3 = (S3); __ns; })
59b8907339SPhilipp Reisner
60b8907339SPhilipp Reisner enum chg_state_flags {
61435693e8SPhilipp Reisner CS_HARD = 1 << 0,
62435693e8SPhilipp Reisner CS_VERBOSE = 1 << 1,
63435693e8SPhilipp Reisner CS_WAIT_COMPLETE = 1 << 2,
64435693e8SPhilipp Reisner CS_SERIALIZE = 1 << 3,
65b8907339SPhilipp Reisner CS_ORDERED = CS_WAIT_COMPLETE + CS_SERIALIZE,
66435693e8SPhilipp Reisner CS_LOCAL_ONLY = 1 << 4, /* Do not consider a device pair wide state change */
67435693e8SPhilipp Reisner CS_DC_ROLE = 1 << 5, /* DC = display as connection state change */
68435693e8SPhilipp Reisner CS_DC_PEER = 1 << 6,
69435693e8SPhilipp Reisner CS_DC_CONN = 1 << 7,
70435693e8SPhilipp Reisner CS_DC_DISK = 1 << 8,
71435693e8SPhilipp Reisner CS_DC_PDSK = 1 << 9,
72706cb24cSPhilipp Reisner CS_DC_SUSP = 1 << 10,
73435693e8SPhilipp Reisner CS_DC_MASK = CS_DC_ROLE + CS_DC_PEER + CS_DC_CONN + CS_DC_DISK + CS_DC_PDSK,
74706cb24cSPhilipp Reisner CS_IGN_OUTD_FAIL = 1 << 11,
7533d32fa7SLars Ellenberg
7633d32fa7SLars Ellenberg /* Make sure no meta data IO is in flight, by calling
7733d32fa7SLars Ellenberg * drbd_md_get_buffer(). Used for graceful detach. */
7833d32fa7SLars Ellenberg CS_INHIBIT_MD_IO = 1 << 12,
79b8907339SPhilipp Reisner };
80b8907339SPhilipp Reisner
81da9fbc27SPhilipp Reisner /* drbd_dev_state and drbd_state are different types. This is to stress the
82da9fbc27SPhilipp Reisner small difference. There is no suspended flag (.susp), and no suspended
83da9fbc27SPhilipp Reisner while fence handler runs flas (susp_fen). */
84da9fbc27SPhilipp Reisner union drbd_dev_state {
85da9fbc27SPhilipp Reisner struct {
86da9fbc27SPhilipp Reisner #if defined(__LITTLE_ENDIAN_BITFIELD)
87da9fbc27SPhilipp Reisner unsigned role:2 ; /* 3/4 primary/secondary/unknown */
88da9fbc27SPhilipp Reisner unsigned peer:2 ; /* 3/4 primary/secondary/unknown */
89da9fbc27SPhilipp Reisner unsigned conn:5 ; /* 17/32 cstates */
90da9fbc27SPhilipp Reisner unsigned disk:4 ; /* 8/16 from D_DISKLESS to D_UP_TO_DATE */
91da9fbc27SPhilipp Reisner unsigned pdsk:4 ; /* 8/16 from D_DISKLESS to D_UP_TO_DATE */
92da9fbc27SPhilipp Reisner unsigned _unused:1 ;
93da9fbc27SPhilipp Reisner unsigned aftr_isp:1 ; /* isp .. imposed sync pause */
94da9fbc27SPhilipp Reisner unsigned peer_isp:1 ;
95da9fbc27SPhilipp Reisner unsigned user_isp:1 ;
96da9fbc27SPhilipp Reisner unsigned _pad:11; /* 0 unused */
97da9fbc27SPhilipp Reisner #elif defined(__BIG_ENDIAN_BITFIELD)
98da9fbc27SPhilipp Reisner unsigned _pad:11;
99da9fbc27SPhilipp Reisner unsigned user_isp:1 ;
100da9fbc27SPhilipp Reisner unsigned peer_isp:1 ;
101da9fbc27SPhilipp Reisner unsigned aftr_isp:1 ; /* isp .. imposed sync pause */
102da9fbc27SPhilipp Reisner unsigned _unused:1 ;
103da9fbc27SPhilipp Reisner unsigned pdsk:4 ; /* 8/16 from D_DISKLESS to D_UP_TO_DATE */
104da9fbc27SPhilipp Reisner unsigned disk:4 ; /* 8/16 from D_DISKLESS to D_UP_TO_DATE */
105da9fbc27SPhilipp Reisner unsigned conn:5 ; /* 17/32 cstates */
106da9fbc27SPhilipp Reisner unsigned peer:2 ; /* 3/4 primary/secondary/unknown */
107da9fbc27SPhilipp Reisner unsigned role:2 ; /* 3/4 primary/secondary/unknown */
108da9fbc27SPhilipp Reisner #else
109da9fbc27SPhilipp Reisner # error "this endianess is not supported"
110da9fbc27SPhilipp Reisner #endif
111da9fbc27SPhilipp Reisner };
112da9fbc27SPhilipp Reisner unsigned int i;
113da9fbc27SPhilipp Reisner };
114da9fbc27SPhilipp Reisner
115b30ab791SAndreas Gruenbacher extern enum drbd_state_rv drbd_change_state(struct drbd_device *device,
116b8907339SPhilipp Reisner enum chg_state_flags f,
117b8907339SPhilipp Reisner union drbd_state mask,
118b8907339SPhilipp Reisner union drbd_state val);
11954761697SAndreas Gruenbacher extern void drbd_force_state(struct drbd_device *, union drbd_state,
120b8907339SPhilipp Reisner union drbd_state);
12154761697SAndreas Gruenbacher extern enum drbd_state_rv _drbd_request_state(struct drbd_device *,
122b8907339SPhilipp Reisner union drbd_state,
123b8907339SPhilipp Reisner union drbd_state,
124b8907339SPhilipp Reisner enum chg_state_flags);
125a8821531SPhilipp Reisner
126a8821531SPhilipp Reisner extern enum drbd_state_rv
127a8821531SPhilipp Reisner _drbd_request_state_holding_state_mutex(struct drbd_device *, union drbd_state,
128a8821531SPhilipp Reisner union drbd_state, enum chg_state_flags);
129a8821531SPhilipp Reisner
13028bc3b8cSAndreas Gruenbacher extern enum drbd_state_rv _drbd_set_state(struct drbd_device *, union drbd_state,
131b8907339SPhilipp Reisner enum chg_state_flags,
132b8907339SPhilipp Reisner struct completion *done);
13354761697SAndreas Gruenbacher extern void print_st_err(struct drbd_device *, union drbd_state,
1342c38f035SLuc Van Oostenryck union drbd_state, enum drbd_state_rv);
135b8907339SPhilipp Reisner
136bbeb641cSPhilipp Reisner enum drbd_state_rv
137bde89a9eSAndreas Gruenbacher _conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
138bbeb641cSPhilipp Reisner enum chg_state_flags flags);
139bbeb641cSPhilipp Reisner
140bbeb641cSPhilipp Reisner enum drbd_state_rv
141bde89a9eSAndreas Gruenbacher conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
142bbeb641cSPhilipp Reisner enum chg_state_flags flags);
143bbeb641cSPhilipp Reisner
144b30ab791SAndreas Gruenbacher extern void drbd_resume_al(struct drbd_device *device);
145bde89a9eSAndreas Gruenbacher extern bool conn_all_vols_unconf(struct drbd_connection *connection);
146b8907339SPhilipp Reisner
147b8907339SPhilipp Reisner /**
1487e5fec31SFabian Frederick * drbd_request_state() - Request a state change
149b30ab791SAndreas Gruenbacher * @device: DRBD device.
150b8907339SPhilipp Reisner * @mask: mask of state bits to change.
151b8907339SPhilipp Reisner * @val: value of new state bits.
152b8907339SPhilipp Reisner *
153b8907339SPhilipp Reisner * This is the most graceful way of requesting a state change. It is verbose
154b8907339SPhilipp Reisner * quite verbose in case the state change is not possible, and all those
155b8907339SPhilipp Reisner * state changes are globally serialized.
156b8907339SPhilipp Reisner */
drbd_request_state(struct drbd_device * device,union drbd_state mask,union drbd_state val)157b30ab791SAndreas Gruenbacher static inline int drbd_request_state(struct drbd_device *device,
158b8907339SPhilipp Reisner union drbd_state mask,
159b8907339SPhilipp Reisner union drbd_state val)
160b8907339SPhilipp Reisner {
161b30ab791SAndreas Gruenbacher return _drbd_request_state(device, mask, val, CS_VERBOSE + CS_ORDERED);
162b8907339SPhilipp Reisner }
163b8907339SPhilipp Reisner
16433d32fa7SLars Ellenberg /* for use in adm_detach() (drbd_adm_detach(), drbd_adm_down()) */
1655816a093SNathan Chancellor int drbd_request_detach_interruptible(struct drbd_device *device);
16633d32fa7SLars Ellenberg
167bde89a9eSAndreas Gruenbacher enum drbd_role conn_highest_role(struct drbd_connection *connection);
168bde89a9eSAndreas Gruenbacher enum drbd_role conn_highest_peer(struct drbd_connection *connection);
169bde89a9eSAndreas Gruenbacher enum drbd_disk_state conn_highest_disk(struct drbd_connection *connection);
170bde89a9eSAndreas Gruenbacher enum drbd_disk_state conn_lowest_disk(struct drbd_connection *connection);
171bde89a9eSAndreas Gruenbacher enum drbd_disk_state conn_highest_pdsk(struct drbd_connection *connection);
172bde89a9eSAndreas Gruenbacher enum drbd_conns conn_lowest_conn(struct drbd_connection *connection);
173cb703454SPhilipp Reisner
174b8907339SPhilipp Reisner #endif
175