blob: 0276c98fbbdde5f3cf2a8a263a507167490037d5 [file] [log] [blame]
Philipp Reisnerb8907332011-01-27 14:07:51 +01001#ifndef DRBD_STATE_H
2#define DRBD_STATE_H
3
Andreas Gruenbacher54761692011-05-30 16:15:21 +02004struct drbd_device;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02005struct drbd_connection;
Philipp Reisnerb8907332011-01-27 14:07:51 +01006
7/**
8 * DOC: DRBD State macros
9 *
10 * These macros are used to express state changes in easily readable form.
11 *
12 * The NS macros expand to a mask and a value, that can be bit ored onto the
13 * current state as soon as the spinlock (req_lock) was taken.
14 *
15 * The _NS macros are used for state functions that get called with the
16 * spinlock. These macros expand directly to the new state value.
17 *
18 * Besides the basic forms NS() and _NS() additional _?NS[23] are defined
19 * to express state changes that affect more than one aspect of the state.
20 *
21 * E.g. NS2(conn, C_CONNECTED, peer, R_SECONDARY)
22 * Means that the network connection was established and that the peer
23 * is in secondary role.
24 */
25#define role_MASK R_MASK
26#define peer_MASK R_MASK
27#define disk_MASK D_MASK
28#define pdsk_MASK D_MASK
29#define conn_MASK C_MASK
30#define susp_MASK 1
31#define user_isp_MASK 1
32#define aftr_isp_MASK 1
33#define susp_nod_MASK 1
34#define susp_fen_MASK 1
35
36#define NS(T, S) \
37 ({ union drbd_state mask; mask.i = 0; mask.T = T##_MASK; mask; }), \
38 ({ union drbd_state val; val.i = 0; val.T = (S); val; })
39#define NS2(T1, S1, T2, S2) \
40 ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
41 mask.T2 = T2##_MASK; mask; }), \
42 ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
43 val.T2 = (S2); val; })
44#define NS3(T1, S1, T2, S2, T3, S3) \
45 ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
46 mask.T2 = T2##_MASK; mask.T3 = T3##_MASK; mask; }), \
47 ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
48 val.T2 = (S2); val.T3 = (S3); val; })
49
50#define _NS(D, T, S) \
Philipp Reisner78bae592011-03-28 15:40:12 +020051 D, ({ union drbd_state __ns; __ns = drbd_read_state(D); __ns.T = (S); __ns; })
Philipp Reisnerb8907332011-01-27 14:07:51 +010052#define _NS2(D, T1, S1, T2, S2) \
Philipp Reisner78bae592011-03-28 15:40:12 +020053 D, ({ union drbd_state __ns; __ns = drbd_read_state(D); __ns.T1 = (S1); \
Philipp Reisnerb8907332011-01-27 14:07:51 +010054 __ns.T2 = (S2); __ns; })
55#define _NS3(D, T1, S1, T2, S2, T3, S3) \
Philipp Reisner78bae592011-03-28 15:40:12 +020056 D, ({ union drbd_state __ns; __ns = drbd_read_state(D); __ns.T1 = (S1); \
Philipp Reisnerb8907332011-01-27 14:07:51 +010057 __ns.T2 = (S2); __ns.T3 = (S3); __ns; })
58
59enum chg_state_flags {
Philipp Reisner435693e2011-03-25 15:11:30 +010060 CS_HARD = 1 << 0,
61 CS_VERBOSE = 1 << 1,
62 CS_WAIT_COMPLETE = 1 << 2,
63 CS_SERIALIZE = 1 << 3,
64 CS_ORDERED = CS_WAIT_COMPLETE + CS_SERIALIZE,
65 CS_LOCAL_ONLY = 1 << 4, /* Do not consider a device pair wide state change */
66 CS_DC_ROLE = 1 << 5, /* DC = display as connection state change */
67 CS_DC_PEER = 1 << 6,
68 CS_DC_CONN = 1 << 7,
69 CS_DC_DISK = 1 << 8,
70 CS_DC_PDSK = 1 << 9,
Philipp Reisner706cb242011-03-29 15:20:27 +020071 CS_DC_SUSP = 1 << 10,
Philipp Reisner435693e2011-03-25 15:11:30 +010072 CS_DC_MASK = CS_DC_ROLE + CS_DC_PEER + CS_DC_CONN + CS_DC_DISK + CS_DC_PDSK,
Philipp Reisner706cb242011-03-29 15:20:27 +020073 CS_IGN_OUTD_FAIL = 1 << 11,
Lars Ellenberg33d32fa2017-08-29 10:20:43 +020074
75 /* Make sure no meta data IO is in flight, by calling
76 * drbd_md_get_buffer(). Used for graceful detach. */
77 CS_INHIBIT_MD_IO = 1 << 12,
Philipp Reisnerb8907332011-01-27 14:07:51 +010078};
79
Philipp Reisnerda9fbc22011-03-29 10:52:01 +020080/* drbd_dev_state and drbd_state are different types. This is to stress the
81 small difference. There is no suspended flag (.susp), and no suspended
82 while fence handler runs flas (susp_fen). */
83union drbd_dev_state {
84 struct {
85#if defined(__LITTLE_ENDIAN_BITFIELD)
86 unsigned role:2 ; /* 3/4 primary/secondary/unknown */
87 unsigned peer:2 ; /* 3/4 primary/secondary/unknown */
88 unsigned conn:5 ; /* 17/32 cstates */
89 unsigned disk:4 ; /* 8/16 from D_DISKLESS to D_UP_TO_DATE */
90 unsigned pdsk:4 ; /* 8/16 from D_DISKLESS to D_UP_TO_DATE */
91 unsigned _unused:1 ;
92 unsigned aftr_isp:1 ; /* isp .. imposed sync pause */
93 unsigned peer_isp:1 ;
94 unsigned user_isp:1 ;
95 unsigned _pad:11; /* 0 unused */
96#elif defined(__BIG_ENDIAN_BITFIELD)
97 unsigned _pad:11;
98 unsigned user_isp:1 ;
99 unsigned peer_isp:1 ;
100 unsigned aftr_isp:1 ; /* isp .. imposed sync pause */
101 unsigned _unused:1 ;
102 unsigned pdsk:4 ; /* 8/16 from D_DISKLESS to D_UP_TO_DATE */
103 unsigned disk:4 ; /* 8/16 from D_DISKLESS to D_UP_TO_DATE */
104 unsigned conn:5 ; /* 17/32 cstates */
105 unsigned peer:2 ; /* 3/4 primary/secondary/unknown */
106 unsigned role:2 ; /* 3/4 primary/secondary/unknown */
107#else
108# error "this endianess is not supported"
109#endif
110 };
111 unsigned int i;
112};
113
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200114extern enum drbd_state_rv drbd_change_state(struct drbd_device *device,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100115 enum chg_state_flags f,
116 union drbd_state mask,
117 union drbd_state val);
Andreas Gruenbacher54761692011-05-30 16:15:21 +0200118extern void drbd_force_state(struct drbd_device *, union drbd_state,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100119 union drbd_state);
Andreas Gruenbacher54761692011-05-30 16:15:21 +0200120extern enum drbd_state_rv _drbd_request_state(struct drbd_device *,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100121 union drbd_state,
122 union drbd_state,
123 enum chg_state_flags);
Philipp Reisnera8821532014-11-10 17:21:11 +0100124
125extern enum drbd_state_rv
126_drbd_request_state_holding_state_mutex(struct drbd_device *, union drbd_state,
127 union drbd_state, enum chg_state_flags);
128
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +0200129extern enum drbd_state_rv _drbd_set_state(struct drbd_device *, union drbd_state,
130 enum chg_state_flags,
131 struct completion *done);
Andreas Gruenbacher54761692011-05-30 16:15:21 +0200132extern void print_st_err(struct drbd_device *, union drbd_state,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100133 union drbd_state, int);
134
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100135enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200136_conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100137 enum chg_state_flags flags);
138
139enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200140conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100141 enum chg_state_flags flags);
142
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200143extern void drbd_resume_al(struct drbd_device *device);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200144extern bool conn_all_vols_unconf(struct drbd_connection *connection);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100145
146/**
Fabian Frederick7e5fec32016-06-14 00:26:35 +0200147 * drbd_request_state() - Request a state change
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200148 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100149 * @mask: mask of state bits to change.
150 * @val: value of new state bits.
151 *
152 * This is the most graceful way of requesting a state change. It is verbose
153 * quite verbose in case the state change is not possible, and all those
154 * state changes are globally serialized.
155 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200156static inline int drbd_request_state(struct drbd_device *device,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100157 union drbd_state mask,
158 union drbd_state val)
159{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200160 return _drbd_request_state(device, mask, val, CS_VERBOSE + CS_ORDERED);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100161}
162
Lars Ellenberg33d32fa2017-08-29 10:20:43 +0200163/* for use in adm_detach() (drbd_adm_detach(), drbd_adm_down()) */
164enum drbd_state_rv
165drbd_request_detach_interruptible(struct drbd_device *device);
166
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200167enum drbd_role conn_highest_role(struct drbd_connection *connection);
168enum drbd_role conn_highest_peer(struct drbd_connection *connection);
169enum drbd_disk_state conn_highest_disk(struct drbd_connection *connection);
170enum drbd_disk_state conn_lowest_disk(struct drbd_connection *connection);
171enum drbd_disk_state conn_highest_pdsk(struct drbd_connection *connection);
172enum drbd_conns conn_lowest_conn(struct drbd_connection *connection);
Philipp Reisnercb703452011-03-24 11:03:07 +0100173
Philipp Reisnerb8907332011-01-27 14:07:51 +0100174#endif