blob: 4a75b7a03b9d713e2ad5104a55f0d25bba378633 [file] [log] [blame]
Philipp Reisnerb8907332011-01-27 14:07:51 +01001/*
2 drbd_state.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
11 from Logicworks, Inc. for making SDP replication support possible.
12
13 drbd is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
16 any later version.
17
18 drbd is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with drbd; see the file COPYING. If not, write to
25 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/drbd_limits.h>
29#include "drbd_int.h"
Andreas Gruenbachera3603a62011-05-30 11:47:37 +020030#include "drbd_protocol.h"
Philipp Reisnerb8907332011-01-27 14:07:51 +010031#include "drbd_req.h"
32
33struct after_state_chg_work {
34 struct drbd_work w;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +020035 struct drbd_device *device;
Philipp Reisnerb8907332011-01-27 14:07:51 +010036 union drbd_state os;
37 union drbd_state ns;
38 enum chg_state_flags flags;
39 struct completion *done;
40};
41
Philipp Reisnerd942ae42011-05-31 13:07:24 +020042enum sanitize_state_warnings {
43 NO_WARNING,
44 ABORTED_ONLINE_VERIFY,
45 ABORTED_RESYNC,
46 CONNECTION_LOST_NEGOTIATING,
47 IMPLICITLY_UPGRADED_DISK,
48 IMPLICITLY_UPGRADED_PDSK,
49};
50
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +010051static int w_after_state_ch(struct drbd_work *w, int unused);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020052static void after_state_ch(struct drbd_device *device, union drbd_state os,
Philipp Reisnerb8907332011-01-27 14:07:51 +010053 union drbd_state ns, enum chg_state_flags flags);
Andreas Gruenbacher54761692011-05-30 16:15:21 +020054static enum drbd_state_rv is_valid_state(struct drbd_device *, union drbd_state);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +020055static enum drbd_state_rv is_valid_soft_transition(union drbd_state, union drbd_state, struct drbd_connection *);
Philipp Reisner35095022011-02-09 16:29:33 +010056static enum drbd_state_rv is_valid_transition(union drbd_state os, union drbd_state ns);
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +020057static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state os,
58 union drbd_state ns, enum sanitize_state_warnings *warn);
Philipp Reisnerb8907332011-01-27 14:07:51 +010059
Philipp Reisner2aebfab2011-03-28 16:48:11 +020060static inline bool is_susp(union drbd_state s)
61{
62 return s.susp || s.susp_nod || s.susp_fen;
63}
64
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +020065bool conn_all_vols_unconf(struct drbd_connection *connection)
Philipp Reisner0e29d162011-02-18 14:23:11 +010066{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +020067 struct drbd_peer_device *peer_device;
Philipp Reisner695d08f2011-04-11 22:53:32 -070068 bool rv = true;
Philipp Reisnere90285e2011-03-22 12:51:21 +010069 int vnr;
Philipp Reisner0e29d162011-02-18 14:23:11 +010070
Philipp Reisner695d08f2011-04-11 22:53:32 -070071 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +020072 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
73 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020074 if (device->state.disk != D_DISKLESS ||
75 device->state.conn != C_STANDALONE ||
76 device->state.role != R_SECONDARY) {
Philipp Reisner695d08f2011-04-11 22:53:32 -070077 rv = false;
78 break;
79 }
Philipp Reisner0e29d162011-02-18 14:23:11 +010080 }
Philipp Reisner695d08f2011-04-11 22:53:32 -070081 rcu_read_unlock();
82
83 return rv;
Philipp Reisner0e29d162011-02-18 14:23:11 +010084}
85
Philipp Reisnercb703452011-03-24 11:03:07 +010086/* Unfortunately the states where not correctly ordered, when
87 they where defined. therefore can not use max_t() here. */
88static enum drbd_role max_role(enum drbd_role role1, enum drbd_role role2)
89{
90 if (role1 == R_PRIMARY || role2 == R_PRIMARY)
91 return R_PRIMARY;
92 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
93 return R_SECONDARY;
94 return R_UNKNOWN;
95}
96static enum drbd_role min_role(enum drbd_role role1, enum drbd_role role2)
97{
98 if (role1 == R_UNKNOWN || role2 == R_UNKNOWN)
99 return R_UNKNOWN;
100 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
101 return R_SECONDARY;
102 return R_PRIMARY;
103}
104
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200105enum drbd_role conn_highest_role(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100106{
107 enum drbd_role role = R_UNKNOWN;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200108 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100109 int vnr;
110
Philipp Reisner695d08f2011-04-11 22:53:32 -0700111 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200112 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
113 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200114 role = max_role(role, device->state.role);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200115 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700116 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100117
118 return role;
119}
120
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200121enum drbd_role conn_highest_peer(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100122{
123 enum drbd_role peer = R_UNKNOWN;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200124 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100125 int vnr;
126
Philipp Reisner695d08f2011-04-11 22:53:32 -0700127 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200128 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
129 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200130 peer = max_role(peer, device->state.peer);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200131 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700132 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100133
134 return peer;
135}
136
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200137enum drbd_disk_state conn_highest_disk(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100138{
139 enum drbd_disk_state ds = D_DISKLESS;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200140 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100141 int vnr;
142
Philipp Reisner695d08f2011-04-11 22:53:32 -0700143 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200144 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
145 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200146 ds = max_t(enum drbd_disk_state, ds, device->state.disk);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200147 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700148 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100149
150 return ds;
151}
152
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200153enum drbd_disk_state conn_lowest_disk(struct drbd_connection *connection)
Philipp Reisner46692652011-03-29 18:15:49 +0200154{
155 enum drbd_disk_state ds = D_MASK;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200156 struct drbd_peer_device *peer_device;
Philipp Reisner46692652011-03-29 18:15:49 +0200157 int vnr;
158
Philipp Reisner695d08f2011-04-11 22:53:32 -0700159 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200160 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
161 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200162 ds = min_t(enum drbd_disk_state, ds, device->state.disk);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200163 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700164 rcu_read_unlock();
Philipp Reisner46692652011-03-29 18:15:49 +0200165
166 return ds;
167}
168
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200169enum drbd_disk_state conn_highest_pdsk(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100170{
171 enum drbd_disk_state ds = D_DISKLESS;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200172 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100173 int vnr;
174
Philipp Reisner695d08f2011-04-11 22:53:32 -0700175 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200176 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
177 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200178 ds = max_t(enum drbd_disk_state, ds, device->state.pdsk);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200179 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700180 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100181
182 return ds;
183}
184
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200185enum drbd_conns conn_lowest_conn(struct drbd_connection *connection)
Philipp Reisner19f83c72011-03-29 14:21:03 +0200186{
187 enum drbd_conns conn = C_MASK;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200188 struct drbd_peer_device *peer_device;
Philipp Reisner19f83c72011-03-29 14:21:03 +0200189 int vnr;
190
Philipp Reisner695d08f2011-04-11 22:53:32 -0700191 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200192 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
193 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200194 conn = min_t(enum drbd_conns, conn, device->state.conn);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200195 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700196 rcu_read_unlock();
Philipp Reisner19f83c72011-03-29 14:21:03 +0200197
198 return conn;
199}
200
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200201static bool no_peer_wf_report_params(struct drbd_connection *connection)
Philipp Reisner79702012012-08-28 11:33:35 +0200202{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200203 struct drbd_peer_device *peer_device;
Philipp Reisner79702012012-08-28 11:33:35 +0200204 int vnr;
205 bool rv = true;
206
207 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200208 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
209 if (peer_device->device->state.conn == C_WF_REPORT_PARAMS) {
Philipp Reisner79702012012-08-28 11:33:35 +0200210 rv = false;
211 break;
212 }
213 rcu_read_unlock();
214
215 return rv;
216}
217
218
Philipp Reisnerb8907332011-01-27 14:07:51 +0100219/**
220 * cl_wide_st_chg() - true if the state change is a cluster wide one
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200221 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100222 * @os: old (current) state.
223 * @ns: new (wanted) state.
224 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200225static int cl_wide_st_chg(struct drbd_device *device,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100226 union drbd_state os, union drbd_state ns)
227{
228 return (os.conn >= C_CONNECTED && ns.conn >= C_CONNECTED &&
229 ((os.role != R_PRIMARY && ns.role == R_PRIMARY) ||
230 (os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
231 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S) ||
Lars Ellenberge8744f52012-03-26 15:57:00 +0200232 (os.disk != D_FAILED && ns.disk == D_FAILED))) ||
Philipp Reisnerb8907332011-01-27 14:07:51 +0100233 (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
Philipp Reisner369bea62011-07-06 23:04:44 +0200234 (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S) ||
235 (os.conn == C_CONNECTED && ns.conn == C_WF_REPORT_PARAMS);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100236}
237
Philipp Reisner56707f92011-02-16 14:57:50 +0100238static union drbd_state
239apply_mask_val(union drbd_state os, union drbd_state mask, union drbd_state val)
240{
241 union drbd_state ns;
242 ns.i = (os.i & ~mask.i) | val.i;
243 return ns;
244}
245
Philipp Reisnerb8907332011-01-27 14:07:51 +0100246enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200247drbd_change_state(struct drbd_device *device, enum chg_state_flags f,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100248 union drbd_state mask, union drbd_state val)
249{
250 unsigned long flags;
Philipp Reisner56707f92011-02-16 14:57:50 +0100251 union drbd_state ns;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100252 enum drbd_state_rv rv;
253
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200254 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200255 ns = apply_mask_val(drbd_read_state(device), mask, val);
256 rv = _drbd_set_state(device, ns, f, NULL);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200257 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100258
259 return rv;
260}
261
262/**
263 * drbd_force_state() - Impose a change which happens outside our control on our state
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200264 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100265 * @mask: mask of state bits to change.
266 * @val: value of new state bits.
267 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200268void drbd_force_state(struct drbd_device *device,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100269 union drbd_state mask, union drbd_state val)
270{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200271 drbd_change_state(device, CS_HARD, mask, val);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100272}
273
274static enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200275_req_st_cond(struct drbd_device *device, union drbd_state mask,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100276 union drbd_state val)
277{
278 union drbd_state os, ns;
279 unsigned long flags;
280 enum drbd_state_rv rv;
281
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200282 if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100283 return SS_CW_SUCCESS;
284
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200285 if (test_and_clear_bit(CL_ST_CHG_FAIL, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100286 return SS_CW_FAILED_BY_PEER;
287
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200288 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200289 os = drbd_read_state(device);
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +0200290 ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100291 rv = is_valid_transition(os, ns);
Philipp Reisnera3025a22012-09-03 15:39:01 +0200292 if (rv >= SS_SUCCESS)
Philipp Reisner35095022011-02-09 16:29:33 +0100293 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100294
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200295 if (!cl_wide_st_chg(device, os, ns))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100296 rv = SS_CW_NO_NEED;
Philipp Reisner35095022011-02-09 16:29:33 +0100297 if (rv == SS_UNKNOWN_ERROR) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200298 rv = is_valid_state(device, ns);
Philipp Reisnera3025a22012-09-03 15:39:01 +0200299 if (rv >= SS_SUCCESS) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200300 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
Philipp Reisnera3025a22012-09-03 15:39:01 +0200301 if (rv >= SS_SUCCESS)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100302 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
303 }
304 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200305 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100306
307 return rv;
308}
309
310/**
311 * drbd_req_state() - Perform an eventually cluster wide state change
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200312 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100313 * @mask: mask of state bits to change.
314 * @val: value of new state bits.
315 * @f: flags
316 *
317 * Should not be called directly, use drbd_request_state() or
318 * _drbd_request_state().
319 */
320static enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200321drbd_req_state(struct drbd_device *device, union drbd_state mask,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100322 union drbd_state val, enum chg_state_flags f)
323{
324 struct completion done;
325 unsigned long flags;
326 union drbd_state os, ns;
327 enum drbd_state_rv rv;
328
329 init_completion(&done);
330
331 if (f & CS_SERIALIZE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200332 mutex_lock(device->state_mutex);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100333
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200334 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200335 os = drbd_read_state(device);
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +0200336 ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100337 rv = is_valid_transition(os, ns);
Lars Ellenberg3c5e5f62011-03-15 16:04:09 +0100338 if (rv < SS_SUCCESS) {
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200339 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisner35095022011-02-09 16:29:33 +0100340 goto abort;
Lars Ellenberg3c5e5f62011-03-15 16:04:09 +0100341 }
Philipp Reisnerb8907332011-01-27 14:07:51 +0100342
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200343 if (cl_wide_st_chg(device, os, ns)) {
344 rv = is_valid_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100345 if (rv == SS_SUCCESS)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200346 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200347 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100348
349 if (rv < SS_SUCCESS) {
350 if (f & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200351 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100352 goto abort;
353 }
354
Andreas Gruenbacher69a22772011-08-09 00:47:13 +0200355 if (drbd_send_state_req(first_peer_device(device), mask, val)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100356 rv = SS_CW_FAILED_BY_PEER;
357 if (f & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200358 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100359 goto abort;
360 }
361
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200362 wait_event(device->state_wait,
363 (rv = _req_st_cond(device, mask, val)));
Philipp Reisnerb8907332011-01-27 14:07:51 +0100364
365 if (rv < SS_SUCCESS) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100366 if (f & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200367 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100368 goto abort;
369 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200370 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200371 ns = apply_mask_val(drbd_read_state(device), mask, val);
372 rv = _drbd_set_state(device, ns, f, &done);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100373 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200374 rv = _drbd_set_state(device, ns, f, &done);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100375 }
376
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200377 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100378
379 if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200380 D_ASSERT(device, current != first_peer_device(device)->connection->worker.task);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100381 wait_for_completion(&done);
382 }
383
384abort:
385 if (f & CS_SERIALIZE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200386 mutex_unlock(device->state_mutex);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100387
388 return rv;
389}
390
391/**
392 * _drbd_request_state() - Request a state change (with flags)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200393 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100394 * @mask: mask of state bits to change.
395 * @val: value of new state bits.
396 * @f: flags
397 *
398 * Cousin of drbd_request_state(), useful with the CS_WAIT_COMPLETE
399 * flag, or when logging of failed state change requests is not desired.
400 */
401enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200402_drbd_request_state(struct drbd_device *device, union drbd_state mask,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100403 union drbd_state val, enum chg_state_flags f)
404{
405 enum drbd_state_rv rv;
406
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200407 wait_event(device->state_wait,
408 (rv = drbd_req_state(device, mask, val, f)) != SS_IN_TRANSIENT_STATE);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100409
410 return rv;
411}
412
Lars Ellenberg8ce953a2014-02-27 09:46:18 +0100413static void print_st(struct drbd_device *device, const char *name, union drbd_state ns)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100414{
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200415 drbd_err(device, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c%c%c }\n",
Philipp Reisnerb8907332011-01-27 14:07:51 +0100416 name,
417 drbd_conn_str(ns.conn),
418 drbd_role_str(ns.role),
419 drbd_role_str(ns.peer),
420 drbd_disk_str(ns.disk),
421 drbd_disk_str(ns.pdsk),
422 is_susp(ns) ? 's' : 'r',
423 ns.aftr_isp ? 'a' : '-',
424 ns.peer_isp ? 'p' : '-',
425 ns.user_isp ? 'u' : '-',
426 ns.susp_fen ? 'F' : '-',
427 ns.susp_nod ? 'N' : '-'
428 );
429}
430
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200431void print_st_err(struct drbd_device *device, union drbd_state os,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100432 union drbd_state ns, enum drbd_state_rv err)
433{
434 if (err == SS_IN_TRANSIENT_STATE)
435 return;
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200436 drbd_err(device, "State change failed: %s\n", drbd_set_st_err_str(err));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200437 print_st(device, " state", os);
438 print_st(device, "wanted", ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100439}
440
Philipp Reisner435693e2011-03-25 15:11:30 +0100441static long print_state_change(char *pb, union drbd_state os, union drbd_state ns,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100442 enum chg_state_flags flags)
443{
Philipp Reisner435693e2011-03-25 15:11:30 +0100444 char *pbp;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100445 pbp = pb;
446 *pbp = 0;
Philipp Reisner706cb242011-03-29 15:20:27 +0200447
Philipp Reisner435693e2011-03-25 15:11:30 +0100448 if (ns.role != os.role && flags & CS_DC_ROLE)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100449 pbp += sprintf(pbp, "role( %s -> %s ) ",
450 drbd_role_str(os.role),
451 drbd_role_str(ns.role));
Philipp Reisner435693e2011-03-25 15:11:30 +0100452 if (ns.peer != os.peer && flags & CS_DC_PEER)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100453 pbp += sprintf(pbp, "peer( %s -> %s ) ",
454 drbd_role_str(os.peer),
455 drbd_role_str(ns.peer));
Philipp Reisner435693e2011-03-25 15:11:30 +0100456 if (ns.conn != os.conn && flags & CS_DC_CONN)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100457 pbp += sprintf(pbp, "conn( %s -> %s ) ",
458 drbd_conn_str(os.conn),
459 drbd_conn_str(ns.conn));
Philipp Reisner435693e2011-03-25 15:11:30 +0100460 if (ns.disk != os.disk && flags & CS_DC_DISK)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100461 pbp += sprintf(pbp, "disk( %s -> %s ) ",
462 drbd_disk_str(os.disk),
463 drbd_disk_str(ns.disk));
Philipp Reisner435693e2011-03-25 15:11:30 +0100464 if (ns.pdsk != os.pdsk && flags & CS_DC_PDSK)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100465 pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
466 drbd_disk_str(os.pdsk),
467 drbd_disk_str(ns.pdsk));
Philipp Reisner706cb242011-03-29 15:20:27 +0200468
469 return pbp - pb;
470}
471
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200472static void drbd_pr_state_change(struct drbd_device *device, union drbd_state os, union drbd_state ns,
Philipp Reisner706cb242011-03-29 15:20:27 +0200473 enum chg_state_flags flags)
474{
475 char pb[300];
476 char *pbp = pb;
477
478 pbp += print_state_change(pbp, os, ns, flags ^ CS_DC_MASK);
479
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100480 if (ns.aftr_isp != os.aftr_isp)
481 pbp += sprintf(pbp, "aftr_isp( %d -> %d ) ",
482 os.aftr_isp,
483 ns.aftr_isp);
484 if (ns.peer_isp != os.peer_isp)
485 pbp += sprintf(pbp, "peer_isp( %d -> %d ) ",
486 os.peer_isp,
487 ns.peer_isp);
488 if (ns.user_isp != os.user_isp)
489 pbp += sprintf(pbp, "user_isp( %d -> %d ) ",
490 os.user_isp,
491 ns.user_isp);
Philipp Reisner435693e2011-03-25 15:11:30 +0100492
Philipp Reisner706cb242011-03-29 15:20:27 +0200493 if (pbp != pb)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200494 drbd_info(device, "%s\n", pb);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100495}
Philipp Reisnerb8907332011-01-27 14:07:51 +0100496
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200497static void conn_pr_state_change(struct drbd_connection *connection, union drbd_state os, union drbd_state ns,
Philipp Reisner435693e2011-03-25 15:11:30 +0100498 enum chg_state_flags flags)
499{
500 char pb[300];
Philipp Reisner706cb242011-03-29 15:20:27 +0200501 char *pbp = pb;
Philipp Reisner435693e2011-03-25 15:11:30 +0100502
Philipp Reisner706cb242011-03-29 15:20:27 +0200503 pbp += print_state_change(pbp, os, ns, flags);
504
505 if (is_susp(ns) != is_susp(os) && flags & CS_DC_SUSP)
506 pbp += sprintf(pbp, "susp( %d -> %d ) ",
507 is_susp(os),
508 is_susp(ns));
509
510 if (pbp != pb)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200511 drbd_info(connection, "%s\n", pb);
Philipp Reisner435693e2011-03-25 15:11:30 +0100512}
513
514
Philipp Reisnerb8907332011-01-27 14:07:51 +0100515/**
516 * is_valid_state() - Returns an SS_ error code if ns is not valid
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200517 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100518 * @ns: State to consider.
519 */
520static enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200521is_valid_state(struct drbd_device *device, union drbd_state ns)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100522{
523 /* See drbd_state_sw_errors in drbd_strings.c */
524
525 enum drbd_fencing_p fp;
526 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200527 struct net_conf *nc;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100528
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200529 rcu_read_lock();
Philipp Reisnerb8907332011-01-27 14:07:51 +0100530 fp = FP_DONT_CARE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200531 if (get_ldev(device)) {
532 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
533 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100534 }
535
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200536 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200537 if (nc) {
538 if (!nc->two_primaries && ns.role == R_PRIMARY) {
Philipp Reisner047e95e22011-03-16 14:43:36 +0100539 if (ns.peer == R_PRIMARY)
540 rv = SS_TWO_PRIMARIES;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200541 else if (conn_highest_peer(first_peer_device(device)->connection) == R_PRIMARY)
Philipp Reisner047e95e22011-03-16 14:43:36 +0100542 rv = SS_O_VOL_PEER_PRI;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200543 }
Philipp Reisnerb8907332011-01-27 14:07:51 +0100544 }
545
546 if (rv <= 0)
547 /* already found a reason to abort */;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200548 else if (ns.role == R_SECONDARY && device->open_cnt)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100549 rv = SS_DEVICE_IN_USE;
550
551 else if (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.disk < D_UP_TO_DATE)
552 rv = SS_NO_UP_TO_DATE_DISK;
553
554 else if (fp >= FP_RESOURCE &&
555 ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
556 rv = SS_PRIMARY_NOP;
557
558 else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
559 rv = SS_NO_UP_TO_DATE_DISK;
560
561 else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
562 rv = SS_NO_LOCAL_DISK;
563
564 else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
565 rv = SS_NO_REMOTE_DISK;
566
567 else if (ns.conn > C_CONNECTED && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
568 rv = SS_NO_UP_TO_DATE_DISK;
569
570 else if ((ns.conn == C_CONNECTED ||
571 ns.conn == C_WF_BITMAP_S ||
572 ns.conn == C_SYNC_SOURCE ||
573 ns.conn == C_PAUSED_SYNC_S) &&
574 ns.disk == D_OUTDATED)
575 rv = SS_CONNECTED_OUTDATES;
576
577 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
Philipp Reisner44ed1672011-04-19 17:10:19 +0200578 (nc->verify_alg[0] == 0))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100579 rv = SS_NO_VERIFY_ALG;
580
581 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200582 first_peer_device(device)->connection->agreed_pro_version < 88)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100583 rv = SS_NOT_SUPPORTED;
584
Philipp Reisner5c4f13d2013-03-27 14:08:37 +0100585 else if (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
586 rv = SS_NO_UP_TO_DATE_DISK;
587
588 else if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
589 ns.pdsk == D_UNKNOWN)
590 rv = SS_NEED_CONNECTION;
591
Philipp Reisnerb8907332011-01-27 14:07:51 +0100592 else if (ns.conn >= C_CONNECTED && ns.pdsk == D_UNKNOWN)
593 rv = SS_CONNECTED_OUTDATES;
594
Philipp Reisner44ed1672011-04-19 17:10:19 +0200595 rcu_read_unlock();
596
Philipp Reisnerb8907332011-01-27 14:07:51 +0100597 return rv;
598}
599
600/**
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100601 * is_valid_soft_transition() - Returns an SS_ error code if the state transition is not possible
Philipp Reisner35095022011-02-09 16:29:33 +0100602 * This function limits state transitions that may be declined by DRBD. I.e.
603 * user requests (aka soft transitions).
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200604 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100605 * @ns: new state.
606 * @os: old state.
607 */
608static enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200609is_valid_soft_transition(union drbd_state os, union drbd_state ns, struct drbd_connection *connection)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100610{
611 enum drbd_state_rv rv = SS_SUCCESS;
612
613 if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
614 os.conn > C_CONNECTED)
615 rv = SS_RESYNC_RUNNING;
616
617 if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
618 rv = SS_ALREADY_STANDALONE;
619
620 if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
621 rv = SS_IS_DISKLESS;
622
623 if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
624 rv = SS_NO_NET_CONFIG;
625
626 if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
627 rv = SS_LOWER_THAN_OUTDATED;
628
629 if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
630 rv = SS_IN_TRANSIENT_STATE;
631
Philipp Reisner2325eb62011-03-15 16:56:18 +0100632 /* if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
633 rv = SS_IN_TRANSIENT_STATE; */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100634
Philipp Reisnera1096a62012-04-06 12:07:34 +0200635 /* While establishing a connection only allow cstate to change.
636 Delay/refuse role changes, detach attach etc... */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200637 if (test_bit(STATE_SENT, &connection->flags) &&
Philipp Reisnera1096a62012-04-06 12:07:34 +0200638 !(os.conn == C_WF_REPORT_PARAMS ||
639 (ns.conn == C_WF_REPORT_PARAMS && os.conn == C_WF_CONNECTION)))
640 rv = SS_IN_TRANSIENT_STATE;
641
Philipp Reisnerb8907332011-01-27 14:07:51 +0100642 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
643 rv = SS_NEED_CONNECTION;
644
645 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
646 ns.conn != os.conn && os.conn > C_CONNECTED)
647 rv = SS_RESYNC_RUNNING;
648
649 if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
650 os.conn < C_CONNECTED)
651 rv = SS_NEED_CONNECTION;
652
653 if ((ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)
654 && os.conn < C_WF_REPORT_PARAMS)
655 rv = SS_NEED_CONNECTION; /* No NetworkFailure -> SyncTarget etc... */
656
Philipp Reisner2bd5ed52013-03-27 14:08:40 +0100657 if (ns.conn == C_DISCONNECTING && ns.pdsk == D_OUTDATED &&
658 os.conn < C_CONNECTED && os.pdsk > D_OUTDATED)
659 rv = SS_OUTDATE_WO_CONN;
660
Philipp Reisnerb8907332011-01-27 14:07:51 +0100661 return rv;
662}
663
Philipp Reisnerfda74112011-02-10 10:38:06 +0100664static enum drbd_state_rv
665is_valid_conn_transition(enum drbd_conns oc, enum drbd_conns nc)
666{
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200667 /* no change -> nothing to do, at least for the connection part */
668 if (oc == nc)
669 return SS_NOTHING_TO_DO;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100670
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200671 /* disconnect of an unconfigured connection does not make sense */
672 if (oc == C_STANDALONE && nc == C_DISCONNECTING)
673 return SS_ALREADY_STANDALONE;
674
675 /* from C_STANDALONE, we start with C_UNCONNECTED */
676 if (oc == C_STANDALONE && nc != C_UNCONNECTED)
677 return SS_NEED_CONNECTION;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100678
Philipp Reisner25b0d6c2012-02-14 12:12:35 +0100679 /* When establishing a connection we need to go through WF_REPORT_PARAMS!
680 Necessary to do the right thing upon invalidate-remote on a disconnected resource */
681 if (oc < C_WF_REPORT_PARAMS && nc >= C_CONNECTED)
682 return SS_NEED_CONNECTION;
683
Philipp Reisnerfda74112011-02-10 10:38:06 +0100684 /* After a network error only C_UNCONNECTED or C_DISCONNECTING may follow. */
685 if (oc >= C_TIMEOUT && oc <= C_TEAR_DOWN && nc != C_UNCONNECTED && nc != C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200686 return SS_IN_TRANSIENT_STATE;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100687
688 /* After C_DISCONNECTING only C_STANDALONE may follow */
689 if (oc == C_DISCONNECTING && nc != C_STANDALONE)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200690 return SS_IN_TRANSIENT_STATE;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100691
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200692 return SS_SUCCESS;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100693}
694
695
Philipp Reisnerb8907332011-01-27 14:07:51 +0100696/**
Philipp Reisner35095022011-02-09 16:29:33 +0100697 * is_valid_transition() - Returns an SS_ error code if the state transition is not possible
698 * This limits hard state transitions. Hard state transitions are facts there are
699 * imposed on DRBD by the environment. E.g. disk broke or network broke down.
700 * But those hard state transitions are still not allowed to do everything.
701 * @ns: new state.
702 * @os: old state.
703 */
704static enum drbd_state_rv
705is_valid_transition(union drbd_state os, union drbd_state ns)
706{
Philipp Reisnerfda74112011-02-10 10:38:06 +0100707 enum drbd_state_rv rv;
Philipp Reisner35095022011-02-09 16:29:33 +0100708
Philipp Reisnerfda74112011-02-10 10:38:06 +0100709 rv = is_valid_conn_transition(os.conn, ns.conn);
Philipp Reisner35095022011-02-09 16:29:33 +0100710
711 /* we cannot fail (again) if we already detached */
712 if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
713 rv = SS_IS_DISKLESS;
714
715 return rv;
716}
717
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200718static void print_sanitize_warnings(struct drbd_device *device, enum sanitize_state_warnings warn)
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200719{
720 static const char *msg_table[] = {
721 [NO_WARNING] = "",
722 [ABORTED_ONLINE_VERIFY] = "Online-verify aborted.",
723 [ABORTED_RESYNC] = "Resync aborted.",
724 [CONNECTION_LOST_NEGOTIATING] = "Connection lost while negotiating, no data!",
725 [IMPLICITLY_UPGRADED_DISK] = "Implicitly upgraded disk",
726 [IMPLICITLY_UPGRADED_PDSK] = "Implicitly upgraded pdsk",
727 };
728
729 if (warn != NO_WARNING)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200730 drbd_warn(device, "%s\n", msg_table[warn]);
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200731}
732
Philipp Reisner35095022011-02-09 16:29:33 +0100733/**
Philipp Reisnerb8907332011-01-27 14:07:51 +0100734 * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200735 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100736 * @os: old state.
737 * @ns: new state.
738 * @warn_sync_abort:
739 *
740 * When we loose connection, we have to set the state of the peers disk (pdsk)
741 * to D_UNKNOWN. This rule and many more along those lines are in this function.
742 */
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +0200743static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state os,
744 union drbd_state ns, enum sanitize_state_warnings *warn)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100745{
746 enum drbd_fencing_p fp;
747 enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
748
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200749 if (warn)
750 *warn = NO_WARNING;
751
Philipp Reisnerb8907332011-01-27 14:07:51 +0100752 fp = FP_DONT_CARE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200753 if (get_ldev(device)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200754 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200755 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200756 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200757 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100758 }
759
Philipp Reisner35095022011-02-09 16:29:33 +0100760 /* Implications from connection to peer and peer_isp */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100761 if (ns.conn < C_CONNECTED) {
762 ns.peer_isp = 0;
763 ns.peer = R_UNKNOWN;
764 if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
765 ns.pdsk = D_UNKNOWN;
766 }
767
768 /* Clear the aftr_isp when becoming unconfigured */
769 if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
770 ns.aftr_isp = 0;
771
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100772 /* An implication of the disk states onto the connection state */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100773 /* Abort resync if a disk fails/detaches */
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100774 if (ns.conn > C_CONNECTED && (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200775 if (warn)
776 *warn = ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T ?
777 ABORTED_ONLINE_VERIFY : ABORTED_RESYNC;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100778 ns.conn = C_CONNECTED;
779 }
780
781 /* Connection breaks down before we finished "Negotiating" */
782 if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200783 get_ldev_if_state(device, D_NEGOTIATING)) {
784 if (device->ed_uuid == device->ldev->md.uuid[UI_CURRENT]) {
785 ns.disk = device->new_state_tmp.disk;
786 ns.pdsk = device->new_state_tmp.pdsk;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100787 } else {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200788 if (warn)
789 *warn = CONNECTION_LOST_NEGOTIATING;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100790 ns.disk = D_DISKLESS;
791 ns.pdsk = D_UNKNOWN;
792 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200793 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100794 }
795
796 /* D_CONSISTENT and D_OUTDATED vanish when we get connected */
797 if (ns.conn >= C_CONNECTED && ns.conn < C_AHEAD) {
798 if (ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED)
799 ns.disk = D_UP_TO_DATE;
800 if (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)
801 ns.pdsk = D_UP_TO_DATE;
802 }
803
804 /* Implications of the connection stat on the disk states */
805 disk_min = D_DISKLESS;
806 disk_max = D_UP_TO_DATE;
807 pdsk_min = D_INCONSISTENT;
808 pdsk_max = D_UNKNOWN;
809 switch ((enum drbd_conns)ns.conn) {
810 case C_WF_BITMAP_T:
811 case C_PAUSED_SYNC_T:
812 case C_STARTING_SYNC_T:
813 case C_WF_SYNC_UUID:
814 case C_BEHIND:
815 disk_min = D_INCONSISTENT;
816 disk_max = D_OUTDATED;
817 pdsk_min = D_UP_TO_DATE;
818 pdsk_max = D_UP_TO_DATE;
819 break;
820 case C_VERIFY_S:
821 case C_VERIFY_T:
822 disk_min = D_UP_TO_DATE;
823 disk_max = D_UP_TO_DATE;
824 pdsk_min = D_UP_TO_DATE;
825 pdsk_max = D_UP_TO_DATE;
826 break;
827 case C_CONNECTED:
828 disk_min = D_DISKLESS;
829 disk_max = D_UP_TO_DATE;
830 pdsk_min = D_DISKLESS;
831 pdsk_max = D_UP_TO_DATE;
832 break;
833 case C_WF_BITMAP_S:
834 case C_PAUSED_SYNC_S:
835 case C_STARTING_SYNC_S:
836 case C_AHEAD:
837 disk_min = D_UP_TO_DATE;
838 disk_max = D_UP_TO_DATE;
839 pdsk_min = D_INCONSISTENT;
840 pdsk_max = D_CONSISTENT; /* D_OUTDATED would be nice. But explicit outdate necessary*/
841 break;
842 case C_SYNC_TARGET:
843 disk_min = D_INCONSISTENT;
844 disk_max = D_INCONSISTENT;
845 pdsk_min = D_UP_TO_DATE;
846 pdsk_max = D_UP_TO_DATE;
847 break;
848 case C_SYNC_SOURCE:
849 disk_min = D_UP_TO_DATE;
850 disk_max = D_UP_TO_DATE;
851 pdsk_min = D_INCONSISTENT;
852 pdsk_max = D_INCONSISTENT;
853 break;
854 case C_STANDALONE:
855 case C_DISCONNECTING:
856 case C_UNCONNECTED:
857 case C_TIMEOUT:
858 case C_BROKEN_PIPE:
859 case C_NETWORK_FAILURE:
860 case C_PROTOCOL_ERROR:
861 case C_TEAR_DOWN:
862 case C_WF_CONNECTION:
863 case C_WF_REPORT_PARAMS:
864 case C_MASK:
865 break;
866 }
867 if (ns.disk > disk_max)
868 ns.disk = disk_max;
869
870 if (ns.disk < disk_min) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200871 if (warn)
872 *warn = IMPLICITLY_UPGRADED_DISK;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100873 ns.disk = disk_min;
874 }
875 if (ns.pdsk > pdsk_max)
876 ns.pdsk = pdsk_max;
877
878 if (ns.pdsk < pdsk_min) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200879 if (warn)
880 *warn = IMPLICITLY_UPGRADED_PDSK;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100881 ns.pdsk = pdsk_min;
882 }
883
884 if (fp == FP_STONITH &&
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +0200885 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED) &&
886 !(os.role == R_PRIMARY && os.conn < C_CONNECTED && os.pdsk > D_OUTDATED))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100887 ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
888
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +0200889 if (device->resource->res_opts.on_no_data == OND_SUSPEND_IO &&
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +0200890 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE) &&
891 !(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100892 ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
893
894 if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
895 if (ns.conn == C_SYNC_SOURCE)
896 ns.conn = C_PAUSED_SYNC_S;
897 if (ns.conn == C_SYNC_TARGET)
898 ns.conn = C_PAUSED_SYNC_T;
899 } else {
900 if (ns.conn == C_PAUSED_SYNC_S)
901 ns.conn = C_SYNC_SOURCE;
902 if (ns.conn == C_PAUSED_SYNC_T)
903 ns.conn = C_SYNC_TARGET;
904 }
905
906 return ns;
907}
908
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200909void drbd_resume_al(struct drbd_device *device)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100910{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200911 if (test_and_clear_bit(AL_SUSPENDED, &device->flags))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200912 drbd_info(device, "Resumed AL updates\n");
Philipp Reisnerb8907332011-01-27 14:07:51 +0100913}
914
915/* helper for __drbd_set_state */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200916static void set_ov_position(struct drbd_device *device, enum drbd_conns cs)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100917{
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200918 if (first_peer_device(device)->connection->agreed_pro_version < 90)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200919 device->ov_start_sector = 0;
920 device->rs_total = drbd_bm_bits(device);
921 device->ov_position = 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100922 if (cs == C_VERIFY_T) {
923 /* starting online verify from an arbitrary position
924 * does not fit well into the existing protocol.
925 * on C_VERIFY_T, we initialize ov_left and friends
926 * implicitly in receive_DataRequest once the
927 * first P_OV_REQUEST is received */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200928 device->ov_start_sector = ~(sector_t)0;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100929 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200930 unsigned long bit = BM_SECT_TO_BIT(device->ov_start_sector);
931 if (bit >= device->rs_total) {
932 device->ov_start_sector =
933 BM_BIT_TO_SECT(device->rs_total - 1);
934 device->rs_total = 1;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100935 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200936 device->rs_total -= bit;
937 device->ov_position = device->ov_start_sector;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100938 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200939 device->ov_left = device->rs_total;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100940}
941
942/**
943 * __drbd_set_state() - Set a new DRBD state
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200944 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100945 * @ns: new state.
946 * @flags: Flags
947 * @done: Optional completion, that will get completed after the after_state_ch() finished
948 *
949 * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
950 */
951enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200952__drbd_set_state(struct drbd_device *device, union drbd_state ns,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100953 enum chg_state_flags flags, struct completion *done)
954{
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100955 struct drbd_peer_device *peer_device = first_peer_device(device);
956 struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100957 union drbd_state os;
958 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200959 enum sanitize_state_warnings ssw;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100960 struct after_state_chg_work *ascw;
961
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200962 os = drbd_read_state(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100963
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +0200964 ns = sanitize_state(device, os, ns, &ssw);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100965 if (ns.i == os.i)
966 return SS_NOTHING_TO_DO;
967
Philipp Reisner35095022011-02-09 16:29:33 +0100968 rv = is_valid_transition(os, ns);
969 if (rv < SS_SUCCESS)
970 return rv;
971
Philipp Reisnerb8907332011-01-27 14:07:51 +0100972 if (!(flags & CS_HARD)) {
973 /* pre-state-change checks ; only look at ns */
974 /* See drbd_state_sw_errors in drbd_strings.c */
975
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200976 rv = is_valid_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100977 if (rv < SS_SUCCESS) {
978 /* If the old state was illegal as well, then let
979 this happen...*/
980
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200981 if (is_valid_state(device, os) == rv)
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100982 rv = is_valid_soft_transition(os, ns, connection);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100983 } else
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100984 rv = is_valid_soft_transition(os, ns, connection);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100985 }
986
987 if (rv < SS_SUCCESS) {
988 if (flags & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200989 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100990 return rv;
991 }
992
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200993 print_sanitize_warnings(device, ssw);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100994
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200995 drbd_pr_state_change(device, os, ns, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100996
Philipp Reisner706cb242011-03-29 15:20:27 +0200997 /* Display changes to the susp* flags that where caused by the call to
998 sanitize_state(). Only display it here if we where not called from
999 _conn_request_state() */
1000 if (!(flags & CS_DC_SUSP))
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001001 conn_pr_state_change(connection, os, ns,
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001002 (flags & ~CS_DC_MASK) | CS_DC_SUSP);
Philipp Reisner706cb242011-03-29 15:20:27 +02001003
Philipp Reisnerb8907332011-01-27 14:07:51 +01001004 /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
1005 * on the ldev here, to be sure the transition -> D_DISKLESS resp.
1006 * drbd_ldev_destroy() won't happen before our corresponding
1007 * after_state_ch works run, where we put_ldev again. */
1008 if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
1009 (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001010 atomic_inc(&device->local_cnt);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001011
Lars Ellenberg5ab7d2c2014-01-27 15:58:22 +01001012 if (!is_sync_state(os.conn) && is_sync_state(ns.conn))
1013 clear_bit(RS_DONE, &device->flags);
1014
Lars Ellenbergba3c6fb2014-02-11 08:57:18 +01001015 /* changes to local_cnt and device flags should be visible before
1016 * changes to state, which again should be visible before anything else
1017 * depending on that change happens. */
1018 smp_wmb();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001019 device->state.i = ns.i;
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02001020 device->resource->susp = ns.susp;
1021 device->resource->susp_nod = ns.susp_nod;
1022 device->resource->susp_fen = ns.susp_fen;
Lars Ellenbergba3c6fb2014-02-11 08:57:18 +01001023 smp_wmb();
Philipp Reisnerb8907332011-01-27 14:07:51 +01001024
Lars Ellenberg2681f7f2013-01-21 15:43:41 +01001025 /* put replicated vs not-replicated requests in seperate epochs */
Lars Ellenbergba3c6fb2014-02-11 08:57:18 +01001026 if (drbd_should_do_remote((union drbd_dev_state)os.i) !=
1027 drbd_should_do_remote((union drbd_dev_state)ns.i))
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001028 start_new_tl_epoch(connection);
Lars Ellenberg2681f7f2013-01-21 15:43:41 +01001029
Philipp Reisnerb8907332011-01-27 14:07:51 +01001030 if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001031 drbd_print_uuids(device, "attached to UUIDs");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001032
Philipp Reisner79702012012-08-28 11:33:35 +02001033 /* Wake up role changes, that were delayed because of connection establishing */
1034 if (os.conn == C_WF_REPORT_PARAMS && ns.conn != C_WF_REPORT_PARAMS &&
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001035 no_peer_wf_report_params(connection))
1036 clear_bit(STATE_SENT, &connection->flags);
Philipp Reisner79702012012-08-28 11:33:35 +02001037
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001038 wake_up(&device->misc_wait);
1039 wake_up(&device->state_wait);
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001040 wake_up(&connection->ping_wait);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001041
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001042 /* Aborted verify run, or we reached the stop sector.
1043 * Log the last position, unless end-of-device. */
Philipp Reisnerb8907332011-01-27 14:07:51 +01001044 if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001045 ns.conn <= C_CONNECTED) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001046 device->ov_start_sector =
1047 BM_BIT_TO_SECT(drbd_bm_bits(device) - device->ov_left);
1048 if (device->ov_left)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001049 drbd_info(device, "Online Verify reached sector %llu\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001050 (unsigned long long)device->ov_start_sector);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001051 }
1052
1053 if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
1054 (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001055 drbd_info(device, "Syncer continues.\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001056 device->rs_paused += (long)jiffies
1057 -(long)device->rs_mark_time[device->rs_last_mark];
Philipp Reisnerb8907332011-01-27 14:07:51 +01001058 if (ns.conn == C_SYNC_TARGET)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001059 mod_timer(&device->resync_timer, jiffies);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001060 }
1061
1062 if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
1063 (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001064 drbd_info(device, "Resync suspended\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001065 device->rs_mark_time[device->rs_last_mark] = jiffies;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001066 }
1067
1068 if (os.conn == C_CONNECTED &&
1069 (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
1070 unsigned long now = jiffies;
1071 int i;
1072
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001073 set_ov_position(device, ns.conn);
1074 device->rs_start = now;
1075 device->rs_last_events = 0;
1076 device->rs_last_sect_ev = 0;
1077 device->ov_last_oos_size = 0;
1078 device->ov_last_oos_start = 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001079
1080 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001081 device->rs_mark_left[i] = device->ov_left;
1082 device->rs_mark_time[i] = now;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001083 }
1084
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001085 drbd_rs_controller_reset(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001086
1087 if (ns.conn == C_VERIFY_S) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001088 drbd_info(device, "Starting Online Verify from sector %llu\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001089 (unsigned long long)device->ov_position);
1090 mod_timer(&device->resync_timer, jiffies);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001091 }
1092 }
1093
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001094 if (get_ldev(device)) {
1095 u32 mdf = device->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
Philipp Reisnerb8907332011-01-27 14:07:51 +01001096 MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
1097 MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
1098
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02001099 mdf &= ~MDF_AL_CLEAN;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001100 if (test_bit(CRASHED_PRIMARY, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +01001101 mdf |= MDF_CRASHED_PRIMARY;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001102 if (device->state.role == R_PRIMARY ||
1103 (device->state.pdsk < D_INCONSISTENT && device->state.peer == R_PRIMARY))
Philipp Reisnerb8907332011-01-27 14:07:51 +01001104 mdf |= MDF_PRIMARY_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001105 if (device->state.conn > C_WF_REPORT_PARAMS)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001106 mdf |= MDF_CONNECTED_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001107 if (device->state.disk > D_INCONSISTENT)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001108 mdf |= MDF_CONSISTENT;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001109 if (device->state.disk > D_OUTDATED)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001110 mdf |= MDF_WAS_UP_TO_DATE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001111 if (device->state.pdsk <= D_OUTDATED && device->state.pdsk >= D_INCONSISTENT)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001112 mdf |= MDF_PEER_OUT_DATED;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001113 if (mdf != device->ldev->md.flags) {
1114 device->ldev->md.flags = mdf;
1115 drbd_md_mark_dirty(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001116 }
1117 if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001118 drbd_set_ed_uuid(device, device->ldev->md.uuid[UI_CURRENT]);
1119 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001120 }
1121
1122 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
1123 if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
1124 os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001125 set_bit(CONSIDER_RESYNC, &device->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001126
1127 /* Receiver should clean up itself */
1128 if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001129 drbd_thread_stop_nowait(&connection->receiver);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001130
1131 /* Now the receiver finished cleaning up itself, it should die */
1132 if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001133 drbd_thread_stop_nowait(&connection->receiver);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001134
1135 /* Upon network failure, we need to restart the receiver. */
Philipp Reisner823bd832012-11-08 15:04:36 +01001136 if (os.conn > C_WF_CONNECTION &&
Philipp Reisnerb8907332011-01-27 14:07:51 +01001137 ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001138 drbd_thread_restart_nowait(&connection->receiver);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001139
1140 /* Resume AL writing if we get a connection */
Philipp Reisner28e448b2013-06-25 16:50:06 +02001141 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001142 drbd_resume_al(device);
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001143 connection->connect_cnt++;
Philipp Reisner28e448b2013-06-25 16:50:06 +02001144 }
Philipp Reisnerb8907332011-01-27 14:07:51 +01001145
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001146 /* remember last attach time so request_timer_fn() won't
1147 * kill newly established sessions while we are still trying to thaw
1148 * previously frozen IO */
1149 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
1150 ns.disk > D_NEGOTIATING)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001151 device->last_reattach_jif = jiffies;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001152
Philipp Reisnerb8907332011-01-27 14:07:51 +01001153 ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
1154 if (ascw) {
1155 ascw->os = os;
1156 ascw->ns = ns;
1157 ascw->flags = flags;
1158 ascw->w.cb = w_after_state_ch;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001159 ascw->device = device;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001160 ascw->done = done;
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001161 drbd_queue_work(&connection->sender_work,
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001162 &ascw->w);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001163 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001164 drbd_err(device, "Could not kmalloc an ascw\n");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001165 }
1166
1167 return rv;
1168}
1169
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001170static int w_after_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001171{
1172 struct after_state_chg_work *ascw =
1173 container_of(w, struct after_state_chg_work, w);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001174 struct drbd_device *device = ascw->device;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001175
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001176 after_state_ch(device, ascw->os, ascw->ns, ascw->flags);
Andreas Gruenbacher137975c2011-08-24 08:19:41 +02001177 if (ascw->flags & CS_WAIT_COMPLETE)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001178 complete(ascw->done);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001179 kfree(ascw);
1180
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001181 return 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001182}
1183
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001184static void abw_start_sync(struct drbd_device *device, int rv)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001185{
1186 if (rv) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001187 drbd_err(device, "Writing the bitmap failed not starting resync.\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001188 _drbd_request_state(device, NS(conn, C_CONNECTED), CS_VERBOSE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001189 return;
1190 }
1191
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001192 switch (device->state.conn) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001193 case C_STARTING_SYNC_T:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001194 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001195 break;
1196 case C_STARTING_SYNC_S:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001197 drbd_start_resync(device, C_SYNC_SOURCE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001198 break;
1199 }
1200}
1201
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001202int drbd_bitmap_io_from_worker(struct drbd_device *device,
Andreas Gruenbacher54761692011-05-30 16:15:21 +02001203 int (*io_fn)(struct drbd_device *),
Philipp Reisnerb8907332011-01-27 14:07:51 +01001204 char *why, enum bm_flag flags)
1205{
1206 int rv;
1207
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001208 D_ASSERT(device, current == first_peer_device(device)->connection->worker.task);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001209
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001210 /* open coded non-blocking drbd_suspend_io(device); */
1211 set_bit(SUSPEND_IO, &device->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001212
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001213 drbd_bm_lock(device, why, flags);
1214 rv = io_fn(device);
1215 drbd_bm_unlock(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001216
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001217 drbd_resume_io(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001218
1219 return rv;
1220}
1221
1222/**
1223 * after_state_ch() - Perform after state change actions that may sleep
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001224 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +01001225 * @os: old state.
1226 * @ns: new state.
1227 * @flags: Flags
1228 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001229static void after_state_ch(struct drbd_device *device, union drbd_state os,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001230 union drbd_state ns, enum chg_state_flags flags)
1231{
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02001232 struct drbd_resource *resource = device->resource;
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001233 struct drbd_peer_device *peer_device = first_peer_device(device);
1234 struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001235 struct sib_info sib;
1236
1237 sib.sib_reason = SIB_STATE_CHANGE;
1238 sib.os = os;
1239 sib.ns = ns;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001240
1241 if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001242 clear_bit(CRASHED_PRIMARY, &device->flags);
1243 if (device->p_uuid)
1244 device->p_uuid[UI_FLAGS] &= ~((u64)2);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001245 }
1246
Philipp Reisnerb8907332011-01-27 14:07:51 +01001247 /* Inform userspace about the change... */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001248 drbd_bcast_event(device, &sib);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001249
1250 if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
1251 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001252 drbd_khelper(device, "pri-on-incon-degr");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001253
1254 /* Here we have the actions that are performed after a
1255 state change. This function might sleep */
1256
Philipp Reisnerb8907332011-01-27 14:07:51 +01001257 if (ns.susp_nod) {
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001258 enum drbd_req_event what = NOTHING;
1259
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001260 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001261 if (os.conn < C_CONNECTED && conn_lowest_conn(connection) >= C_CONNECTED)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001262 what = RESEND;
1263
Philipp Reisner3fb47462011-07-15 18:44:26 +02001264 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001265 conn_lowest_disk(connection) > D_NEGOTIATING)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001266 what = RESTART_FROZEN_DISK_IO;
1267
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02001268 if (resource->susp_nod && what != NOTHING) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001269 _tl_restart(connection, what);
1270 _conn_request_state(connection,
Philipp Reisner892fdd12012-08-27 17:20:12 +02001271 (union drbd_state) { { .susp_nod = 1 } },
1272 (union drbd_state) { { .susp_nod = 0 } },
1273 CS_VERBOSE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001274 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001275 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001276 }
1277
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001278 if (ns.susp_fen) {
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001279 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02001280 if (resource->susp_fen && conn_lowest_conn(connection) >= C_CONNECTED) {
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001281 /* case2: The connection was established again: */
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001282 struct drbd_peer_device *peer_device;
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001283 int vnr;
1284
1285 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001286 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
1287 clear_bit(NEW_CUR_UUID, &peer_device->device->flags);
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001288 rcu_read_unlock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001289 _tl_restart(connection, RESEND);
1290 _conn_request_state(connection,
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001291 (union drbd_state) { { .susp_fen = 1 } },
1292 (union drbd_state) { { .susp_fen = 0 } },
1293 CS_VERBOSE);
1294 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001295 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001296 }
1297
Philipp Reisnerb8907332011-01-27 14:07:51 +01001298 /* Became sync source. With protocol >= 96, we still need to send out
1299 * the sync uuid now. Need to do that before any drbd_send_state, or
1300 * the other side may go "paused sync" before receiving the sync uuids,
1301 * which is unexpected. */
1302 if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
1303 (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001304 connection->agreed_pro_version >= 96 && get_ldev(device)) {
1305 drbd_gen_and_send_sync_uuid(peer_device);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001306 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001307 }
1308
1309 /* Do not change the order of the if above and the two below... */
Philipp Reisner369bea62011-07-06 23:04:44 +02001310 if (os.pdsk == D_DISKLESS &&
1311 ns.pdsk > D_DISKLESS && ns.pdsk != D_UNKNOWN) { /* attach on the peer */
Lars Ellenberga3248962012-07-30 09:10:41 +02001312 /* we probably will start a resync soon.
1313 * make sure those things are properly reset. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001314 device->rs_total = 0;
1315 device->rs_failed = 0;
1316 atomic_set(&device->rs_pending_cnt, 0);
1317 drbd_rs_cancel_all(device);
Lars Ellenberga3248962012-07-30 09:10:41 +02001318
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001319 drbd_send_uuids(peer_device);
1320 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001321 }
1322 /* No point in queuing send_bitmap if we don't have a connection
1323 * anymore, so check also the _current_ state, not only the new state
1324 * at the time this work was queued. */
1325 if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001326 device->state.conn == C_WF_BITMAP_S)
1327 drbd_queue_bitmap_io(device, &drbd_send_bitmap, NULL,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001328 "send_bitmap (WFBitMapS)",
1329 BM_LOCKED_TEST_ALLOWED);
1330
1331 /* Lost contact to peer's copy of the data */
1332 if ((os.pdsk >= D_INCONSISTENT &&
1333 os.pdsk != D_UNKNOWN &&
1334 os.pdsk != D_OUTDATED)
1335 && (ns.pdsk < D_INCONSISTENT ||
1336 ns.pdsk == D_UNKNOWN ||
1337 ns.pdsk == D_OUTDATED)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001338 if (get_ldev(device)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001339 if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001340 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1341 if (drbd_suspended(device)) {
1342 set_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001343 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001344 drbd_uuid_new_current(device);
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001345 drbd_send_uuids(peer_device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001346 }
1347 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001348 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001349 }
1350 }
1351
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001352 if (ns.pdsk < D_INCONSISTENT && get_ldev(device)) {
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001353 if (os.peer == R_SECONDARY && ns.peer == R_PRIMARY &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001354 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1355 drbd_uuid_new_current(device);
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001356 drbd_send_uuids(peer_device);
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001357 }
Philipp Reisnerb8907332011-01-27 14:07:51 +01001358 /* D_DISKLESS Peer becomes secondary */
1359 if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
1360 /* We may still be Primary ourselves.
1361 * No harm done if the bitmap still changes,
1362 * redirtied pages will follow later. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001363 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001364 "demote diskless peer", BM_LOCKED_SET_ALLOWED);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001365 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001366 }
1367
1368 /* Write out all changed bits on demote.
1369 * Though, no need to da that just yet
1370 * if there is a resync going on still */
1371 if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001372 device->state.conn <= C_CONNECTED && get_ldev(device)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001373 /* No changes to the bitmap expected this time, so assert that,
1374 * even though no harm was done if it did change. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001375 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001376 "demote", BM_LOCKED_TEST_ALLOWED);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001377 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001378 }
1379
1380 /* Last part of the attaching process ... */
1381 if (ns.conn >= C_CONNECTED &&
1382 os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001383 drbd_send_sizes(peer_device, 0, 0); /* to start sync... */
1384 drbd_send_uuids(peer_device);
1385 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001386 }
1387
1388 /* We want to pause/continue resync, tell peer. */
1389 if (ns.conn >= C_CONNECTED &&
1390 ((os.aftr_isp != ns.aftr_isp) ||
1391 (os.user_isp != ns.user_isp)))
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001392 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001393
1394 /* In case one of the isp bits got set, suspend other devices. */
1395 if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
1396 (ns.aftr_isp || ns.peer_isp || ns.user_isp))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001397 suspend_other_sg(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001398
1399 /* Make sure the peer gets informed about eventual state
1400 changes (ISP bits) while we were in WFReportParams. */
1401 if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001402 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001403
1404 if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001405 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001406
1407 /* We are in the progress to start a full sync... */
1408 if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
1409 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
1410 /* no other bitmap changes expected during this phase */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001411 drbd_queue_bitmap_io(device,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001412 &drbd_bmio_set_n_write, &abw_start_sync,
1413 "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
1414
Philipp Reisnerb8907332011-01-27 14:07:51 +01001415 /* first half of local IO error, failure to attach,
1416 * or administrative detach */
1417 if (os.disk != D_FAILED && ns.disk == D_FAILED) {
Philipp Reisner32db80f2012-02-22 11:51:57 +01001418 enum drbd_io_error_p eh = EP_PASS_ON;
1419 int was_io_error = 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001420 /* corresponding get_ldev was in __drbd_set_state, to serialize
Philipp Reisner32db80f2012-02-22 11:51:57 +01001421 * our cleanup here with the transition to D_DISKLESS.
1422 * But is is still not save to dreference ldev here, since
1423 * we might come from an failed Attach before ldev was set. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001424 if (device->ldev) {
Philipp Reisner32db80f2012-02-22 11:51:57 +01001425 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001426 eh = rcu_dereference(device->ldev->disk_conf)->on_io_error;
Philipp Reisner32db80f2012-02-22 11:51:57 +01001427 rcu_read_unlock();
Philipp Reisnerb8907332011-01-27 14:07:51 +01001428
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001429 was_io_error = test_and_clear_bit(WAS_IO_ERROR, &device->flags);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001430
Lars Ellenberg6f1a6562012-07-30 09:11:01 +02001431 if (was_io_error && eh == EP_CALL_HELPER)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001432 drbd_khelper(device, "local-io-error");
Lars Ellenberg6f1a6562012-07-30 09:11:01 +02001433
Lars Ellenberg0c849662012-07-30 09:07:28 +02001434 /* Immediately allow completion of all application IO,
1435 * that waits for completion from the local disk,
1436 * if this was a force-detach due to disk_timeout
1437 * or administrator request (drbdsetup detach --force).
1438 * Do NOT abort otherwise.
1439 * Aborting local requests may cause serious problems,
1440 * if requests are completed to upper layers already,
1441 * and then later the already submitted local bio completes.
1442 * This can cause DMA into former bio pages that meanwhile
1443 * have been re-used for other things.
1444 * So aborting local requests may cause crashes,
1445 * or even worse, silent data corruption.
1446 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001447 if (test_and_clear_bit(FORCE_DETACH, &device->flags))
1448 tl_abort_disk_io(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001449
Philipp Reisner32db80f2012-02-22 11:51:57 +01001450 /* current state still has to be D_FAILED,
1451 * there is only one way out: to D_DISKLESS,
1452 * and that may only happen after our put_ldev below. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001453 if (device->state.disk != D_FAILED)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001454 drbd_err(device,
Philipp Reisner32db80f2012-02-22 11:51:57 +01001455 "ASSERT FAILED: disk is %s during detach\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001456 drbd_disk_str(device->state.disk));
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001457
Philipp Reisner32db80f2012-02-22 11:51:57 +01001458 if (ns.conn >= C_CONNECTED)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001459 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001460
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001461 drbd_rs_cancel_all(device);
Philipp Reisner32db80f2012-02-22 11:51:57 +01001462
1463 /* In case we want to get something to stable storage still,
1464 * this may be the last chance.
1465 * Following put_ldev may transition to D_DISKLESS. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001466 drbd_md_sync(device);
Philipp Reisner32db80f2012-02-22 11:51:57 +01001467 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001468 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001469 }
1470
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001471 /* second half of local IO error, failure to attach,
1472 * or administrative detach,
1473 * after local_cnt references have reached zero again */
1474 if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
1475 /* We must still be diskless,
1476 * re-attach has to be serialized with this! */
1477 if (device->state.disk != D_DISKLESS)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001478 drbd_err(device,
1479 "ASSERT FAILED: disk is %s while going diskless\n",
1480 drbd_disk_str(device->state.disk));
Philipp Reisnerb8907332011-01-27 14:07:51 +01001481
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001482 if (ns.conn >= C_CONNECTED)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001483 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001484 /* corresponding get_ldev in __drbd_set_state
1485 * this may finally trigger drbd_ldev_destroy. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001486 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001487 }
1488
1489 /* Notify peer that I had a local IO error, and did not detached.. */
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001490 if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT && ns.conn >= C_CONNECTED)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001491 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001492
1493 /* Disks got bigger while they were detached */
1494 if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001495 test_and_clear_bit(RESYNC_AFTER_NEG, &device->flags)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001496 if (ns.conn == C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001497 resync_after_online_grow(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001498 }
1499
1500 /* A resync finished or aborted, wake paused devices... */
1501 if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
1502 (os.peer_isp && !ns.peer_isp) ||
1503 (os.user_isp && !ns.user_isp))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001504 resume_next_sg(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001505
1506 /* sync target done with resync. Explicitly notify peer, even though
1507 * it should (at least for non-empty resyncs) already know itself. */
1508 if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001509 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001510
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001511 /* Verify finished, or reached stop sector. Peer did not know about
1512 * the stop sector, and we may even have changed the stop sector during
1513 * verify to interrupt/stop early. Send the new state. */
1514 if (os.conn == C_VERIFY_S && ns.conn == C_CONNECTED
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001515 && verify_can_do_stop_sector(device))
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001516 drbd_send_state(peer_device, ns);
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001517
Philipp Reisnerb8907332011-01-27 14:07:51 +01001518 /* This triggers bitmap writeout of potentially still unwritten pages
1519 * if the resync finished cleanly, or aborted because of peer disk
1520 * failure, or because of connection loss.
1521 * For resync aborted because of local disk failure, we cannot do
1522 * any bitmap writeout anymore.
1523 * No harm done if some bits change during this phase.
1524 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001525 if (os.conn > C_CONNECTED && ns.conn <= C_CONNECTED && get_ldev(device)) {
1526 drbd_queue_bitmap_io(device, &drbd_bm_write_copy_pages, NULL,
Lars Ellenberga220d292012-05-07 12:07:18 +02001527 "write from resync_finished", BM_LOCKED_CHANGE_ALLOWED);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001528 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001529 }
1530
1531 if (ns.disk == D_DISKLESS &&
1532 ns.conn == C_STANDALONE &&
1533 ns.role == R_SECONDARY) {
1534 if (os.aftr_isp != ns.aftr_isp)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001535 resume_next_sg(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001536 }
1537
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001538 drbd_md_sync(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001539}
1540
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001541struct after_conn_state_chg_work {
Andreas Gruenbacher4c007602011-08-25 15:14:48 +02001542 struct drbd_work w;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001543 enum drbd_conns oc;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001544 union drbd_state ns_min;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001545 union drbd_state ns_max; /* new, max state, over all devices */
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001546 enum chg_state_flags flags;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001547 struct drbd_connection *connection;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001548};
Philipp Reisnerb8907332011-01-27 14:07:51 +01001549
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001550static int w_after_conn_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001551{
1552 struct after_conn_state_chg_work *acscw =
Andreas Gruenbacher4c007602011-08-25 15:14:48 +02001553 container_of(w, struct after_conn_state_chg_work, w);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001554 struct drbd_connection *connection = acscw->connection;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001555 enum drbd_conns oc = acscw->oc;
Philipp Reisner5f082f92011-03-29 13:20:58 +02001556 union drbd_state ns_max = acscw->ns_max;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001557 struct drbd_peer_device *peer_device;
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001558 int vnr;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001559
1560 kfree(acscw);
1561
1562 /* Upon network configuration, we need to start the receiver */
Philipp Reisner5f082f92011-03-29 13:20:58 +02001563 if (oc == C_STANDALONE && ns_max.conn == C_UNCONNECTED)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001564 drbd_thread_start(&connection->receiver);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001565
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02001566 if (oc == C_DISCONNECTING && ns_max.conn == C_STANDALONE) {
1567 struct net_conf *old_conf;
1568
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001569 mutex_lock(&connection->resource->conf_update);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001570 old_conf = connection->net_conf;
1571 connection->my_addr_len = 0;
1572 connection->peer_addr_len = 0;
Monam Agarwalccdd6a92014-03-23 02:02:29 +05301573 RCU_INIT_POINTER(connection->net_conf, NULL);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001574 conn_free_crypto(connection);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001575 mutex_unlock(&connection->resource->conf_update);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02001576
1577 synchronize_rcu();
1578 kfree(old_conf);
1579 }
1580
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001581 if (ns_max.susp_fen) {
1582 /* case1: The outdate peer handler is successful: */
1583 if (ns_max.pdsk <= D_OUTDATED) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001584 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001585 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1586 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001587 if (test_bit(NEW_CUR_UUID, &device->flags)) {
1588 drbd_uuid_new_current(device);
1589 clear_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001590 }
1591 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001592 rcu_read_unlock();
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001593 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001594 _tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
1595 _conn_request_state(connection,
Philipp Reisner8a0bab22012-08-07 13:28:00 +02001596 (union drbd_state) { { .susp_fen = 1 } },
1597 (union drbd_state) { { .susp_fen = 0 } },
1598 CS_VERBOSE);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001599 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001600 }
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001601 }
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02001602 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner19fffd72012-08-28 16:48:03 +02001603
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001604 conn_md_sync(connection);
Philipp Reisner19fffd72012-08-28 16:48:03 +02001605
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001606 return 0;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001607}
1608
Lars Ellenberg8ce953a2014-02-27 09:46:18 +01001609static void conn_old_common_state(struct drbd_connection *connection, union drbd_state *pcs, enum chg_state_flags *pf)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001610{
Philipp Reisner435693e2011-03-25 15:11:30 +01001611 enum chg_state_flags flags = ~0;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001612 struct drbd_peer_device *peer_device;
Philipp Reisner435693e2011-03-25 15:11:30 +01001613 int vnr, first_vol = 1;
Philipp Reisnere0e16652011-07-11 17:04:23 +02001614 union drbd_dev_state os, cs = {
1615 { .role = R_SECONDARY,
1616 .peer = R_UNKNOWN,
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001617 .conn = connection->cstate,
Philipp Reisnere0e16652011-07-11 17:04:23 +02001618 .disk = D_DISKLESS,
1619 .pdsk = D_UNKNOWN,
1620 } };
Philipp Reisner88ef5942011-03-25 14:31:11 +01001621
Philipp Reisner695d08f2011-04-11 22:53:32 -07001622 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001623 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1624 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001625 os = device->state;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001626
Philipp Reisner435693e2011-03-25 15:11:30 +01001627 if (first_vol) {
1628 cs = os;
1629 first_vol = 0;
1630 continue;
1631 }
Philipp Reisner88ef5942011-03-25 14:31:11 +01001632
Philipp Reisner435693e2011-03-25 15:11:30 +01001633 if (cs.role != os.role)
1634 flags &= ~CS_DC_ROLE;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001635
Philipp Reisner435693e2011-03-25 15:11:30 +01001636 if (cs.peer != os.peer)
1637 flags &= ~CS_DC_PEER;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001638
Philipp Reisner435693e2011-03-25 15:11:30 +01001639 if (cs.conn != os.conn)
1640 flags &= ~CS_DC_CONN;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001641
Philipp Reisner435693e2011-03-25 15:11:30 +01001642 if (cs.disk != os.disk)
1643 flags &= ~CS_DC_DISK;
1644
1645 if (cs.pdsk != os.pdsk)
1646 flags &= ~CS_DC_PDSK;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001647 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001648 rcu_read_unlock();
Philipp Reisner88ef5942011-03-25 14:31:11 +01001649
Philipp Reisner435693e2011-03-25 15:11:30 +01001650 *pf |= CS_DC_MASK;
1651 *pf &= flags;
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02001652 (*pcs).i = cs.i;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001653}
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001654
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001655static enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001656conn_is_valid_transition(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisner88ef5942011-03-25 14:31:11 +01001657 enum chg_state_flags flags)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001658{
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001659 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001660 union drbd_state ns, os;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001661 struct drbd_peer_device *peer_device;
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001662 int vnr;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001663
Philipp Reisner695d08f2011-04-11 22:53:32 -07001664 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001665 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1666 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001667 os = drbd_read_state(device);
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +02001668 ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001669
Philipp Reisner778bcf22011-03-28 12:55:03 +02001670 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1671 ns.disk = os.disk;
1672
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001673 if (ns.i == os.i)
1674 continue;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001675
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001676 rv = is_valid_transition(os, ns);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001677
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001678 if (rv >= SS_SUCCESS && !(flags & CS_HARD)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001679 rv = is_valid_state(device, ns);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001680 if (rv < SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001681 if (is_valid_state(device, os) == rv)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001682 rv = is_valid_soft_transition(os, ns, connection);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001683 } else
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001684 rv = is_valid_soft_transition(os, ns, connection);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001685 }
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001686
1687 if (rv < SS_SUCCESS) {
1688 if (flags & CS_VERBOSE)
1689 print_st_err(device, os, ns, rv);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001690 break;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001691 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001692 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001693 rcu_read_unlock();
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001694
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001695 return rv;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001696}
1697
Lars Ellenberg8ce953a2014-02-27 09:46:18 +01001698static void
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001699conn_set_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001700 union drbd_state *pns_min, union drbd_state *pns_max, enum chg_state_flags flags)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001701{
Philipp Reisnerf132f552011-07-18 10:44:24 +02001702 union drbd_state ns, os, ns_max = { };
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001703 union drbd_state ns_min = {
1704 { .role = R_MASK,
1705 .peer = R_MASK,
Philipp Reisnere0e16652011-07-11 17:04:23 +02001706 .conn = val.conn,
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001707 .disk = D_MASK,
1708 .pdsk = D_MASK
1709 } };
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001710 struct drbd_peer_device *peer_device;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001711 enum drbd_state_rv rv;
Philipp Reisnerf132f552011-07-18 10:44:24 +02001712 int vnr, number_of_volumes = 0;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001713
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001714 if (mask.conn == C_MASK) {
1715 /* remember last connect time so request_timer_fn() won't
1716 * kill newly established sessions while we are still trying to thaw
1717 * previously frozen IO */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001718 if (connection->cstate != C_WF_REPORT_PARAMS && val.conn == C_WF_REPORT_PARAMS)
1719 connection->last_reconnect_jif = jiffies;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001720
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001721 connection->cstate = val.conn;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001722 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001723
Philipp Reisner695d08f2011-04-11 22:53:32 -07001724 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001725 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1726 struct drbd_device *device = peer_device->device;
Philipp Reisnerf132f552011-07-18 10:44:24 +02001727 number_of_volumes++;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001728 os = drbd_read_state(device);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001729 ns = apply_mask_val(os, mask, val);
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +02001730 ns = sanitize_state(device, os, ns, NULL);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001731
Philipp Reisner778bcf22011-03-28 12:55:03 +02001732 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1733 ns.disk = os.disk;
1734
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001735 rv = __drbd_set_state(device, ns, flags, NULL);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001736 if (rv < SS_SUCCESS)
1737 BUG();
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001738
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001739 ns.i = device->state.i;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001740 ns_max.role = max_role(ns.role, ns_max.role);
1741 ns_max.peer = max_role(ns.peer, ns_max.peer);
1742 ns_max.conn = max_t(enum drbd_conns, ns.conn, ns_max.conn);
1743 ns_max.disk = max_t(enum drbd_disk_state, ns.disk, ns_max.disk);
1744 ns_max.pdsk = max_t(enum drbd_disk_state, ns.pdsk, ns_max.pdsk);
1745
1746 ns_min.role = min_role(ns.role, ns_min.role);
1747 ns_min.peer = min_role(ns.peer, ns_min.peer);
1748 ns_min.conn = min_t(enum drbd_conns, ns.conn, ns_min.conn);
1749 ns_min.disk = min_t(enum drbd_disk_state, ns.disk, ns_min.disk);
1750 ns_min.pdsk = min_t(enum drbd_disk_state, ns.pdsk, ns_min.pdsk);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001751 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001752 rcu_read_unlock();
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001753
Philipp Reisnerf132f552011-07-18 10:44:24 +02001754 if (number_of_volumes == 0) {
1755 ns_min = ns_max = (union drbd_state) { {
1756 .role = R_SECONDARY,
1757 .peer = R_UNKNOWN,
1758 .conn = val.conn,
1759 .disk = D_DISKLESS,
1760 .pdsk = D_UNKNOWN
1761 } };
1762 }
1763
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02001764 ns_min.susp = ns_max.susp = connection->resource->susp;
1765 ns_min.susp_nod = ns_max.susp_nod = connection->resource->susp_nod;
1766 ns_min.susp_fen = ns_max.susp_fen = connection->resource->susp_fen;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001767
1768 *pns_min = ns_min;
1769 *pns_max = ns_max;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001770}
1771
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001772static enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001773_conn_rq_cond(struct drbd_connection *connection, union drbd_state mask, union drbd_state val)
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001774{
Philipp Reisner88ea6852014-04-28 18:43:20 +02001775 enum drbd_state_rv err, rv = SS_UNKNOWN_ERROR; /* continue waiting */;
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001776
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001777 if (test_and_clear_bit(CONN_WD_ST_CHG_OKAY, &connection->flags))
Philipp Reisner88ea6852014-04-28 18:43:20 +02001778 rv = SS_CW_SUCCESS;
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001779
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001780 if (test_and_clear_bit(CONN_WD_ST_CHG_FAIL, &connection->flags))
Philipp Reisner88ea6852014-04-28 18:43:20 +02001781 rv = SS_CW_FAILED_BY_PEER;
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001782
Philipp Reisner88ea6852014-04-28 18:43:20 +02001783 err = conn_is_valid_transition(connection, mask, val, 0);
1784 if (err == SS_SUCCESS && connection->cstate == C_WF_REPORT_PARAMS)
1785 return rv;
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001786
Philipp Reisner88ea6852014-04-28 18:43:20 +02001787 return err;
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001788}
1789
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001790enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001791_conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001792 enum chg_state_flags flags)
1793{
1794 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001795 struct after_conn_state_chg_work *acscw;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001796 enum drbd_conns oc = connection->cstate;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001797 union drbd_state ns_max, ns_min, os;
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001798 bool have_mutex = false;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001799
Philipp Reisner07fc9612012-08-28 11:07:56 +02001800 if (mask.conn) {
1801 rv = is_valid_conn_transition(oc, val.conn);
1802 if (rv < SS_SUCCESS)
1803 goto abort;
1804 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001805
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001806 rv = conn_is_valid_transition(connection, mask, val, flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001807 if (rv < SS_SUCCESS)
1808 goto abort;
1809
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001810 if (oc == C_WF_REPORT_PARAMS && val.conn == C_DISCONNECTING &&
1811 !(flags & (CS_LOCAL_ONLY | CS_HARD))) {
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001812
1813 /* This will be a cluster-wide state change.
1814 * Need to give up the spinlock, grab the mutex,
1815 * then send the state change request, ... */
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001816 spin_unlock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001817 mutex_lock(&connection->cstate_mutex);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001818 have_mutex = true;
1819
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001820 set_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
1821 if (conn_send_state_req(connection, mask, val)) {
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001822 /* sending failed. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001823 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001824 rv = SS_CW_FAILED_BY_PEER;
1825 /* need to re-aquire the spin lock, though */
1826 goto abort_unlocked;
1827 }
1828
1829 if (val.conn == C_DISCONNECTING)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001830 set_bit(DISCONNECT_SENT, &connection->flags);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001831
1832 /* ... and re-aquire the spinlock.
1833 * If _conn_rq_cond() returned >= SS_SUCCESS, we must call
1834 * conn_set_state() within the same spinlock. */
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001835 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001836 wait_event_lock_irq(connection->ping_wait,
1837 (rv = _conn_rq_cond(connection, mask, val)),
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001838 connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001839 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001840 if (rv < SS_SUCCESS)
1841 goto abort;
1842 }
1843
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001844 conn_old_common_state(connection, &os, &flags);
Philipp Reisner706cb242011-03-29 15:20:27 +02001845 flags |= CS_DC_SUSP;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001846 conn_set_state(connection, mask, val, &ns_min, &ns_max, flags);
1847 conn_pr_state_change(connection, os, ns_max, flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001848
1849 acscw = kmalloc(sizeof(*acscw), GFP_ATOMIC);
1850 if (acscw) {
Philipp Reisner435693e2011-03-25 15:11:30 +01001851 acscw->oc = os.conn;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001852 acscw->ns_min = ns_min;
Philipp Reisner5f082f92011-03-29 13:20:58 +02001853 acscw->ns_max = ns_max;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001854 acscw->flags = flags;
Andreas Gruenbacher4c007602011-08-25 15:14:48 +02001855 acscw->w.cb = w_after_conn_state_ch;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001856 kref_get(&connection->kref);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001857 acscw->connection = connection;
Andreas Gruenbacher4c007602011-08-25 15:14:48 +02001858 drbd_queue_work(&connection->sender_work, &acscw->w);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001859 } else {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +02001860 drbd_err(connection, "Could not kmalloc an acscw\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001861 }
1862
Philipp Reisnera01842e2011-12-13 17:40:53 +01001863 abort:
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001864 if (have_mutex) {
1865 /* mutex_unlock() "... must not be used in interrupt context.",
1866 * so give up the spinlock, then re-aquire it */
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001867 spin_unlock_irq(&connection->resource->req_lock);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001868 abort_unlocked:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001869 mutex_unlock(&connection->cstate_mutex);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001870 spin_lock_irq(&connection->resource->req_lock);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001871 }
1872 if (rv < SS_SUCCESS && flags & CS_VERBOSE) {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +02001873 drbd_err(connection, "State change failed: %s\n", drbd_set_st_err_str(rv));
1874 drbd_err(connection, " mask = 0x%x val = 0x%x\n", mask.i, val.i);
1875 drbd_err(connection, " old_conn:%s wanted_conn:%s\n", drbd_conn_str(oc), drbd_conn_str(val.conn));
Philipp Reisnera01842e2011-12-13 17:40:53 +01001876 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001877 return rv;
1878}
1879
1880enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001881conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001882 enum chg_state_flags flags)
1883{
1884 enum drbd_state_rv rv;
1885
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001886 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001887 rv = _conn_request_state(connection, mask, val, flags);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001888 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001889
1890 return rv;
1891}