blob: 64937536be0f40dadce892e2e3ed98dd0c3b794b [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;
35 union drbd_state os;
36 union drbd_state ns;
37 enum chg_state_flags flags;
38 struct completion *done;
39};
40
Philipp Reisnerd942ae42011-05-31 13:07:24 +020041enum sanitize_state_warnings {
42 NO_WARNING,
43 ABORTED_ONLINE_VERIFY,
44 ABORTED_RESYNC,
45 CONNECTION_LOST_NEGOTIATING,
46 IMPLICITLY_UPGRADED_DISK,
47 IMPLICITLY_UPGRADED_PDSK,
48};
49
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +010050static int w_after_state_ch(struct drbd_work *w, int unused);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020051static void after_state_ch(struct drbd_device *device, union drbd_state os,
Philipp Reisnerb8907332011-01-27 14:07:51 +010052 union drbd_state ns, enum chg_state_flags flags);
Andreas Gruenbacher54761692011-05-30 16:15:21 +020053static enum drbd_state_rv is_valid_state(struct drbd_device *, union drbd_state);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +020054static enum drbd_state_rv is_valid_soft_transition(union drbd_state, union drbd_state, struct drbd_connection *);
Philipp Reisner35095022011-02-09 16:29:33 +010055static enum drbd_state_rv is_valid_transition(union drbd_state os, union drbd_state ns);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020056static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state ns,
Philipp Reisnerd942ae42011-05-31 13:07:24 +020057 enum sanitize_state_warnings *warn);
Philipp Reisnerb8907332011-01-27 14:07:51 +010058
Philipp Reisner2aebfab2011-03-28 16:48:11 +020059static inline bool is_susp(union drbd_state s)
60{
61 return s.susp || s.susp_nod || s.susp_fen;
62}
63
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +020064bool conn_all_vols_unconf(struct drbd_connection *connection)
Philipp Reisner0e29d162011-02-18 14:23:11 +010065{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020066 struct drbd_device *device;
Philipp Reisner695d08f2011-04-11 22:53:32 -070067 bool rv = true;
Philipp Reisnere90285e2011-03-22 12:51:21 +010068 int vnr;
Philipp Reisner0e29d162011-02-18 14:23:11 +010069
Philipp Reisner695d08f2011-04-11 22:53:32 -070070 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +020071 idr_for_each_entry(&connection->volumes, device, vnr) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020072 if (device->state.disk != D_DISKLESS ||
73 device->state.conn != C_STANDALONE ||
74 device->state.role != R_SECONDARY) {
Philipp Reisner695d08f2011-04-11 22:53:32 -070075 rv = false;
76 break;
77 }
Philipp Reisner0e29d162011-02-18 14:23:11 +010078 }
Philipp Reisner695d08f2011-04-11 22:53:32 -070079 rcu_read_unlock();
80
81 return rv;
Philipp Reisner0e29d162011-02-18 14:23:11 +010082}
83
Philipp Reisnercb703452011-03-24 11:03:07 +010084/* Unfortunately the states where not correctly ordered, when
85 they where defined. therefore can not use max_t() here. */
86static enum drbd_role max_role(enum drbd_role role1, enum drbd_role role2)
87{
88 if (role1 == R_PRIMARY || role2 == R_PRIMARY)
89 return R_PRIMARY;
90 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
91 return R_SECONDARY;
92 return R_UNKNOWN;
93}
94static enum drbd_role min_role(enum drbd_role role1, enum drbd_role role2)
95{
96 if (role1 == R_UNKNOWN || role2 == R_UNKNOWN)
97 return R_UNKNOWN;
98 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
99 return R_SECONDARY;
100 return R_PRIMARY;
101}
102
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200103enum drbd_role conn_highest_role(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100104{
105 enum drbd_role role = R_UNKNOWN;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200106 struct drbd_device *device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100107 int vnr;
108
Philipp Reisner695d08f2011-04-11 22:53:32 -0700109 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200110 idr_for_each_entry(&connection->volumes, device, vnr)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200111 role = max_role(role, device->state.role);
Philipp Reisner695d08f2011-04-11 22:53:32 -0700112 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100113
114 return role;
115}
116
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200117enum drbd_role conn_highest_peer(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100118{
119 enum drbd_role peer = R_UNKNOWN;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200120 struct drbd_device *device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100121 int vnr;
122
Philipp Reisner695d08f2011-04-11 22:53:32 -0700123 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200124 idr_for_each_entry(&connection->volumes, device, vnr)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200125 peer = max_role(peer, device->state.peer);
Philipp Reisner695d08f2011-04-11 22:53:32 -0700126 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100127
128 return peer;
129}
130
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200131enum drbd_disk_state conn_highest_disk(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100132{
133 enum drbd_disk_state ds = D_DISKLESS;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200134 struct drbd_device *device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100135 int vnr;
136
Philipp Reisner695d08f2011-04-11 22:53:32 -0700137 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200138 idr_for_each_entry(&connection->volumes, device, vnr)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200139 ds = max_t(enum drbd_disk_state, ds, device->state.disk);
Philipp Reisner695d08f2011-04-11 22:53:32 -0700140 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100141
142 return ds;
143}
144
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200145enum drbd_disk_state conn_lowest_disk(struct drbd_connection *connection)
Philipp Reisner46692652011-03-29 18:15:49 +0200146{
147 enum drbd_disk_state ds = D_MASK;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200148 struct drbd_device *device;
Philipp Reisner46692652011-03-29 18:15:49 +0200149 int vnr;
150
Philipp Reisner695d08f2011-04-11 22:53:32 -0700151 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200152 idr_for_each_entry(&connection->volumes, device, vnr)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200153 ds = min_t(enum drbd_disk_state, ds, device->state.disk);
Philipp Reisner695d08f2011-04-11 22:53:32 -0700154 rcu_read_unlock();
Philipp Reisner46692652011-03-29 18:15:49 +0200155
156 return ds;
157}
158
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200159enum drbd_disk_state conn_highest_pdsk(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100160{
161 enum drbd_disk_state ds = D_DISKLESS;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200162 struct drbd_device *device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100163 int vnr;
164
Philipp Reisner695d08f2011-04-11 22:53:32 -0700165 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200166 idr_for_each_entry(&connection->volumes, device, vnr)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200167 ds = max_t(enum drbd_disk_state, ds, device->state.pdsk);
Philipp Reisner695d08f2011-04-11 22:53:32 -0700168 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100169
170 return ds;
171}
172
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200173enum drbd_conns conn_lowest_conn(struct drbd_connection *connection)
Philipp Reisner19f83c72011-03-29 14:21:03 +0200174{
175 enum drbd_conns conn = C_MASK;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200176 struct drbd_device *device;
Philipp Reisner19f83c72011-03-29 14:21:03 +0200177 int vnr;
178
Philipp Reisner695d08f2011-04-11 22:53:32 -0700179 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200180 idr_for_each_entry(&connection->volumes, device, vnr)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200181 conn = min_t(enum drbd_conns, conn, device->state.conn);
Philipp Reisner695d08f2011-04-11 22:53:32 -0700182 rcu_read_unlock();
Philipp Reisner19f83c72011-03-29 14:21:03 +0200183
184 return conn;
185}
186
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200187static bool no_peer_wf_report_params(struct drbd_connection *connection)
Philipp Reisner79702012012-08-28 11:33:35 +0200188{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200189 struct drbd_device *device;
Philipp Reisner79702012012-08-28 11:33:35 +0200190 int vnr;
191 bool rv = true;
192
193 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200194 idr_for_each_entry(&connection->volumes, device, vnr)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200195 if (device->state.conn == C_WF_REPORT_PARAMS) {
Philipp Reisner79702012012-08-28 11:33:35 +0200196 rv = false;
197 break;
198 }
199 rcu_read_unlock();
200
201 return rv;
202}
203
204
Philipp Reisnerb8907332011-01-27 14:07:51 +0100205/**
206 * cl_wide_st_chg() - true if the state change is a cluster wide one
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200207 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100208 * @os: old (current) state.
209 * @ns: new (wanted) state.
210 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200211static int cl_wide_st_chg(struct drbd_device *device,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100212 union drbd_state os, union drbd_state ns)
213{
214 return (os.conn >= C_CONNECTED && ns.conn >= C_CONNECTED &&
215 ((os.role != R_PRIMARY && ns.role == R_PRIMARY) ||
216 (os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
217 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S) ||
Lars Ellenberge8744f52012-03-26 15:57:00 +0200218 (os.disk != D_FAILED && ns.disk == D_FAILED))) ||
Philipp Reisnerb8907332011-01-27 14:07:51 +0100219 (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
Philipp Reisner369bea62011-07-06 23:04:44 +0200220 (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S) ||
221 (os.conn == C_CONNECTED && ns.conn == C_WF_REPORT_PARAMS);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100222}
223
Philipp Reisner56707f92011-02-16 14:57:50 +0100224static union drbd_state
225apply_mask_val(union drbd_state os, union drbd_state mask, union drbd_state val)
226{
227 union drbd_state ns;
228 ns.i = (os.i & ~mask.i) | val.i;
229 return ns;
230}
231
Philipp Reisnerb8907332011-01-27 14:07:51 +0100232enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200233drbd_change_state(struct drbd_device *device, enum chg_state_flags f,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100234 union drbd_state mask, union drbd_state val)
235{
236 unsigned long flags;
Philipp Reisner56707f92011-02-16 14:57:50 +0100237 union drbd_state ns;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100238 enum drbd_state_rv rv;
239
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200240 spin_lock_irqsave(&first_peer_device(device)->connection->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200241 ns = apply_mask_val(drbd_read_state(device), mask, val);
242 rv = _drbd_set_state(device, ns, f, NULL);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200243 spin_unlock_irqrestore(&first_peer_device(device)->connection->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100244
245 return rv;
246}
247
248/**
249 * drbd_force_state() - Impose a change which happens outside our control on our state
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200250 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100251 * @mask: mask of state bits to change.
252 * @val: value of new state bits.
253 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200254void drbd_force_state(struct drbd_device *device,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100255 union drbd_state mask, union drbd_state val)
256{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200257 drbd_change_state(device, CS_HARD, mask, val);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100258}
259
260static enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200261_req_st_cond(struct drbd_device *device, union drbd_state mask,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100262 union drbd_state val)
263{
264 union drbd_state os, ns;
265 unsigned long flags;
266 enum drbd_state_rv rv;
267
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200268 if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100269 return SS_CW_SUCCESS;
270
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200271 if (test_and_clear_bit(CL_ST_CHG_FAIL, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100272 return SS_CW_FAILED_BY_PEER;
273
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200274 spin_lock_irqsave(&first_peer_device(device)->connection->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200275 os = drbd_read_state(device);
276 ns = sanitize_state(device, apply_mask_val(os, mask, val), NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100277 rv = is_valid_transition(os, ns);
Philipp Reisnera3025a22012-09-03 15:39:01 +0200278 if (rv >= SS_SUCCESS)
Philipp Reisner35095022011-02-09 16:29:33 +0100279 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100280
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200281 if (!cl_wide_st_chg(device, os, ns))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100282 rv = SS_CW_NO_NEED;
Philipp Reisner35095022011-02-09 16:29:33 +0100283 if (rv == SS_UNKNOWN_ERROR) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200284 rv = is_valid_state(device, ns);
Philipp Reisnera3025a22012-09-03 15:39:01 +0200285 if (rv >= SS_SUCCESS) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200286 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
Philipp Reisnera3025a22012-09-03 15:39:01 +0200287 if (rv >= SS_SUCCESS)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100288 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
289 }
290 }
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200291 spin_unlock_irqrestore(&first_peer_device(device)->connection->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100292
293 return rv;
294}
295
296/**
297 * drbd_req_state() - Perform an eventually cluster wide state change
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200298 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100299 * @mask: mask of state bits to change.
300 * @val: value of new state bits.
301 * @f: flags
302 *
303 * Should not be called directly, use drbd_request_state() or
304 * _drbd_request_state().
305 */
306static enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200307drbd_req_state(struct drbd_device *device, union drbd_state mask,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100308 union drbd_state val, enum chg_state_flags f)
309{
310 struct completion done;
311 unsigned long flags;
312 union drbd_state os, ns;
313 enum drbd_state_rv rv;
314
315 init_completion(&done);
316
317 if (f & CS_SERIALIZE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200318 mutex_lock(device->state_mutex);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100319
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200320 spin_lock_irqsave(&first_peer_device(device)->connection->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200321 os = drbd_read_state(device);
322 ns = sanitize_state(device, apply_mask_val(os, mask, val), NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100323 rv = is_valid_transition(os, ns);
Lars Ellenberg3c5e5f62011-03-15 16:04:09 +0100324 if (rv < SS_SUCCESS) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200325 spin_unlock_irqrestore(&first_peer_device(device)->connection->req_lock, flags);
Philipp Reisner35095022011-02-09 16:29:33 +0100326 goto abort;
Lars Ellenberg3c5e5f62011-03-15 16:04:09 +0100327 }
Philipp Reisnerb8907332011-01-27 14:07:51 +0100328
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200329 if (cl_wide_st_chg(device, os, ns)) {
330 rv = is_valid_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100331 if (rv == SS_SUCCESS)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200332 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
333 spin_unlock_irqrestore(&first_peer_device(device)->connection->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100334
335 if (rv < SS_SUCCESS) {
336 if (f & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200337 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100338 goto abort;
339 }
340
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200341 if (drbd_send_state_req(device, mask, val)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100342 rv = SS_CW_FAILED_BY_PEER;
343 if (f & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200344 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100345 goto abort;
346 }
347
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200348 wait_event(device->state_wait,
349 (rv = _req_st_cond(device, mask, val)));
Philipp Reisnerb8907332011-01-27 14:07:51 +0100350
351 if (rv < SS_SUCCESS) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100352 if (f & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200353 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100354 goto abort;
355 }
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200356 spin_lock_irqsave(&first_peer_device(device)->connection->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200357 ns = apply_mask_val(drbd_read_state(device), mask, val);
358 rv = _drbd_set_state(device, ns, f, &done);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100359 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200360 rv = _drbd_set_state(device, ns, f, &done);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100361 }
362
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200363 spin_unlock_irqrestore(&first_peer_device(device)->connection->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100364
365 if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200366 D_ASSERT(current != first_peer_device(device)->connection->worker.task);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100367 wait_for_completion(&done);
368 }
369
370abort:
371 if (f & CS_SERIALIZE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200372 mutex_unlock(device->state_mutex);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100373
374 return rv;
375}
376
377/**
378 * _drbd_request_state() - Request a state change (with flags)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200379 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100380 * @mask: mask of state bits to change.
381 * @val: value of new state bits.
382 * @f: flags
383 *
384 * Cousin of drbd_request_state(), useful with the CS_WAIT_COMPLETE
385 * flag, or when logging of failed state change requests is not desired.
386 */
387enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200388_drbd_request_state(struct drbd_device *device, union drbd_state mask,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100389 union drbd_state val, enum chg_state_flags f)
390{
391 enum drbd_state_rv rv;
392
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200393 wait_event(device->state_wait,
394 (rv = drbd_req_state(device, mask, val, f)) != SS_IN_TRANSIENT_STATE);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100395
396 return rv;
397}
398
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200399static void print_st(struct drbd_device *device, char *name, union drbd_state ns)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100400{
401 dev_err(DEV, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c%c%c }\n",
402 name,
403 drbd_conn_str(ns.conn),
404 drbd_role_str(ns.role),
405 drbd_role_str(ns.peer),
406 drbd_disk_str(ns.disk),
407 drbd_disk_str(ns.pdsk),
408 is_susp(ns) ? 's' : 'r',
409 ns.aftr_isp ? 'a' : '-',
410 ns.peer_isp ? 'p' : '-',
411 ns.user_isp ? 'u' : '-',
412 ns.susp_fen ? 'F' : '-',
413 ns.susp_nod ? 'N' : '-'
414 );
415}
416
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200417void print_st_err(struct drbd_device *device, union drbd_state os,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100418 union drbd_state ns, enum drbd_state_rv err)
419{
420 if (err == SS_IN_TRANSIENT_STATE)
421 return;
422 dev_err(DEV, "State change failed: %s\n", drbd_set_st_err_str(err));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200423 print_st(device, " state", os);
424 print_st(device, "wanted", ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100425}
426
Philipp Reisner435693e2011-03-25 15:11:30 +0100427static long print_state_change(char *pb, union drbd_state os, union drbd_state ns,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100428 enum chg_state_flags flags)
429{
Philipp Reisner435693e2011-03-25 15:11:30 +0100430 char *pbp;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100431 pbp = pb;
432 *pbp = 0;
Philipp Reisner706cb242011-03-29 15:20:27 +0200433
Philipp Reisner435693e2011-03-25 15:11:30 +0100434 if (ns.role != os.role && flags & CS_DC_ROLE)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100435 pbp += sprintf(pbp, "role( %s -> %s ) ",
436 drbd_role_str(os.role),
437 drbd_role_str(ns.role));
Philipp Reisner435693e2011-03-25 15:11:30 +0100438 if (ns.peer != os.peer && flags & CS_DC_PEER)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100439 pbp += sprintf(pbp, "peer( %s -> %s ) ",
440 drbd_role_str(os.peer),
441 drbd_role_str(ns.peer));
Philipp Reisner435693e2011-03-25 15:11:30 +0100442 if (ns.conn != os.conn && flags & CS_DC_CONN)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100443 pbp += sprintf(pbp, "conn( %s -> %s ) ",
444 drbd_conn_str(os.conn),
445 drbd_conn_str(ns.conn));
Philipp Reisner435693e2011-03-25 15:11:30 +0100446 if (ns.disk != os.disk && flags & CS_DC_DISK)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100447 pbp += sprintf(pbp, "disk( %s -> %s ) ",
448 drbd_disk_str(os.disk),
449 drbd_disk_str(ns.disk));
Philipp Reisner435693e2011-03-25 15:11:30 +0100450 if (ns.pdsk != os.pdsk && flags & CS_DC_PDSK)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100451 pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
452 drbd_disk_str(os.pdsk),
453 drbd_disk_str(ns.pdsk));
Philipp Reisner706cb242011-03-29 15:20:27 +0200454
455 return pbp - pb;
456}
457
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200458static void drbd_pr_state_change(struct drbd_device *device, union drbd_state os, union drbd_state ns,
Philipp Reisner706cb242011-03-29 15:20:27 +0200459 enum chg_state_flags flags)
460{
461 char pb[300];
462 char *pbp = pb;
463
464 pbp += print_state_change(pbp, os, ns, flags ^ CS_DC_MASK);
465
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100466 if (ns.aftr_isp != os.aftr_isp)
467 pbp += sprintf(pbp, "aftr_isp( %d -> %d ) ",
468 os.aftr_isp,
469 ns.aftr_isp);
470 if (ns.peer_isp != os.peer_isp)
471 pbp += sprintf(pbp, "peer_isp( %d -> %d ) ",
472 os.peer_isp,
473 ns.peer_isp);
474 if (ns.user_isp != os.user_isp)
475 pbp += sprintf(pbp, "user_isp( %d -> %d ) ",
476 os.user_isp,
477 ns.user_isp);
Philipp Reisner435693e2011-03-25 15:11:30 +0100478
Philipp Reisner706cb242011-03-29 15:20:27 +0200479 if (pbp != pb)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100480 dev_info(DEV, "%s\n", pb);
481}
Philipp Reisnerb8907332011-01-27 14:07:51 +0100482
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200483static void conn_pr_state_change(struct drbd_connection *connection, union drbd_state os, union drbd_state ns,
Philipp Reisner435693e2011-03-25 15:11:30 +0100484 enum chg_state_flags flags)
485{
486 char pb[300];
Philipp Reisner706cb242011-03-29 15:20:27 +0200487 char *pbp = pb;
Philipp Reisner435693e2011-03-25 15:11:30 +0100488
Philipp Reisner706cb242011-03-29 15:20:27 +0200489 pbp += print_state_change(pbp, os, ns, flags);
490
491 if (is_susp(ns) != is_susp(os) && flags & CS_DC_SUSP)
492 pbp += sprintf(pbp, "susp( %d -> %d ) ",
493 is_susp(os),
494 is_susp(ns));
495
496 if (pbp != pb)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200497 conn_info(connection, "%s\n", pb);
Philipp Reisner435693e2011-03-25 15:11:30 +0100498}
499
500
Philipp Reisnerb8907332011-01-27 14:07:51 +0100501/**
502 * is_valid_state() - Returns an SS_ error code if ns is not valid
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200503 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100504 * @ns: State to consider.
505 */
506static enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200507is_valid_state(struct drbd_device *device, union drbd_state ns)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100508{
509 /* See drbd_state_sw_errors in drbd_strings.c */
510
511 enum drbd_fencing_p fp;
512 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200513 struct net_conf *nc;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100514
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200515 rcu_read_lock();
Philipp Reisnerb8907332011-01-27 14:07:51 +0100516 fp = FP_DONT_CARE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200517 if (get_ldev(device)) {
518 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
519 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100520 }
521
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200522 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200523 if (nc) {
524 if (!nc->two_primaries && ns.role == R_PRIMARY) {
Philipp Reisner047e95e22011-03-16 14:43:36 +0100525 if (ns.peer == R_PRIMARY)
526 rv = SS_TWO_PRIMARIES;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200527 else if (conn_highest_peer(first_peer_device(device)->connection) == R_PRIMARY)
Philipp Reisner047e95e22011-03-16 14:43:36 +0100528 rv = SS_O_VOL_PEER_PRI;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200529 }
Philipp Reisnerb8907332011-01-27 14:07:51 +0100530 }
531
532 if (rv <= 0)
533 /* already found a reason to abort */;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200534 else if (ns.role == R_SECONDARY && device->open_cnt)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100535 rv = SS_DEVICE_IN_USE;
536
537 else if (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.disk < D_UP_TO_DATE)
538 rv = SS_NO_UP_TO_DATE_DISK;
539
540 else if (fp >= FP_RESOURCE &&
541 ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
542 rv = SS_PRIMARY_NOP;
543
544 else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
545 rv = SS_NO_UP_TO_DATE_DISK;
546
547 else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
548 rv = SS_NO_LOCAL_DISK;
549
550 else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
551 rv = SS_NO_REMOTE_DISK;
552
553 else if (ns.conn > C_CONNECTED && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
554 rv = SS_NO_UP_TO_DATE_DISK;
555
556 else if ((ns.conn == C_CONNECTED ||
557 ns.conn == C_WF_BITMAP_S ||
558 ns.conn == C_SYNC_SOURCE ||
559 ns.conn == C_PAUSED_SYNC_S) &&
560 ns.disk == D_OUTDATED)
561 rv = SS_CONNECTED_OUTDATES;
562
563 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
Philipp Reisner44ed1672011-04-19 17:10:19 +0200564 (nc->verify_alg[0] == 0))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100565 rv = SS_NO_VERIFY_ALG;
566
567 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200568 first_peer_device(device)->connection->agreed_pro_version < 88)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100569 rv = SS_NOT_SUPPORTED;
570
Philipp Reisner5c4f13d2013-03-27 14:08:37 +0100571 else if (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
572 rv = SS_NO_UP_TO_DATE_DISK;
573
574 else if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
575 ns.pdsk == D_UNKNOWN)
576 rv = SS_NEED_CONNECTION;
577
Philipp Reisnerb8907332011-01-27 14:07:51 +0100578 else if (ns.conn >= C_CONNECTED && ns.pdsk == D_UNKNOWN)
579 rv = SS_CONNECTED_OUTDATES;
580
Philipp Reisner44ed1672011-04-19 17:10:19 +0200581 rcu_read_unlock();
582
Philipp Reisnerb8907332011-01-27 14:07:51 +0100583 return rv;
584}
585
586/**
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100587 * is_valid_soft_transition() - Returns an SS_ error code if the state transition is not possible
Philipp Reisner35095022011-02-09 16:29:33 +0100588 * This function limits state transitions that may be declined by DRBD. I.e.
589 * user requests (aka soft transitions).
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200590 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100591 * @ns: new state.
592 * @os: old state.
593 */
594static enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200595is_valid_soft_transition(union drbd_state os, union drbd_state ns, struct drbd_connection *connection)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100596{
597 enum drbd_state_rv rv = SS_SUCCESS;
598
599 if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
600 os.conn > C_CONNECTED)
601 rv = SS_RESYNC_RUNNING;
602
603 if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
604 rv = SS_ALREADY_STANDALONE;
605
606 if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
607 rv = SS_IS_DISKLESS;
608
609 if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
610 rv = SS_NO_NET_CONFIG;
611
612 if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
613 rv = SS_LOWER_THAN_OUTDATED;
614
615 if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
616 rv = SS_IN_TRANSIENT_STATE;
617
Philipp Reisner2325eb62011-03-15 16:56:18 +0100618 /* if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
619 rv = SS_IN_TRANSIENT_STATE; */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100620
Philipp Reisnera1096a62012-04-06 12:07:34 +0200621 /* While establishing a connection only allow cstate to change.
622 Delay/refuse role changes, detach attach etc... */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200623 if (test_bit(STATE_SENT, &connection->flags) &&
Philipp Reisnera1096a62012-04-06 12:07:34 +0200624 !(os.conn == C_WF_REPORT_PARAMS ||
625 (ns.conn == C_WF_REPORT_PARAMS && os.conn == C_WF_CONNECTION)))
626 rv = SS_IN_TRANSIENT_STATE;
627
Philipp Reisnerb8907332011-01-27 14:07:51 +0100628 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
629 rv = SS_NEED_CONNECTION;
630
631 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
632 ns.conn != os.conn && os.conn > C_CONNECTED)
633 rv = SS_RESYNC_RUNNING;
634
635 if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
636 os.conn < C_CONNECTED)
637 rv = SS_NEED_CONNECTION;
638
639 if ((ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)
640 && os.conn < C_WF_REPORT_PARAMS)
641 rv = SS_NEED_CONNECTION; /* No NetworkFailure -> SyncTarget etc... */
642
Philipp Reisner2bd5ed52013-03-27 14:08:40 +0100643 if (ns.conn == C_DISCONNECTING && ns.pdsk == D_OUTDATED &&
644 os.conn < C_CONNECTED && os.pdsk > D_OUTDATED)
645 rv = SS_OUTDATE_WO_CONN;
646
Philipp Reisnerb8907332011-01-27 14:07:51 +0100647 return rv;
648}
649
Philipp Reisnerfda74112011-02-10 10:38:06 +0100650static enum drbd_state_rv
651is_valid_conn_transition(enum drbd_conns oc, enum drbd_conns nc)
652{
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200653 /* no change -> nothing to do, at least for the connection part */
654 if (oc == nc)
655 return SS_NOTHING_TO_DO;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100656
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200657 /* disconnect of an unconfigured connection does not make sense */
658 if (oc == C_STANDALONE && nc == C_DISCONNECTING)
659 return SS_ALREADY_STANDALONE;
660
661 /* from C_STANDALONE, we start with C_UNCONNECTED */
662 if (oc == C_STANDALONE && nc != C_UNCONNECTED)
663 return SS_NEED_CONNECTION;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100664
Philipp Reisner25b0d6c2012-02-14 12:12:35 +0100665 /* When establishing a connection we need to go through WF_REPORT_PARAMS!
666 Necessary to do the right thing upon invalidate-remote on a disconnected resource */
667 if (oc < C_WF_REPORT_PARAMS && nc >= C_CONNECTED)
668 return SS_NEED_CONNECTION;
669
Philipp Reisnerfda74112011-02-10 10:38:06 +0100670 /* After a network error only C_UNCONNECTED or C_DISCONNECTING may follow. */
671 if (oc >= C_TIMEOUT && oc <= C_TEAR_DOWN && nc != C_UNCONNECTED && nc != C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200672 return SS_IN_TRANSIENT_STATE;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100673
674 /* After C_DISCONNECTING only C_STANDALONE may follow */
675 if (oc == C_DISCONNECTING && nc != C_STANDALONE)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200676 return SS_IN_TRANSIENT_STATE;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100677
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200678 return SS_SUCCESS;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100679}
680
681
Philipp Reisnerb8907332011-01-27 14:07:51 +0100682/**
Philipp Reisner35095022011-02-09 16:29:33 +0100683 * is_valid_transition() - Returns an SS_ error code if the state transition is not possible
684 * This limits hard state transitions. Hard state transitions are facts there are
685 * imposed on DRBD by the environment. E.g. disk broke or network broke down.
686 * But those hard state transitions are still not allowed to do everything.
687 * @ns: new state.
688 * @os: old state.
689 */
690static enum drbd_state_rv
691is_valid_transition(union drbd_state os, union drbd_state ns)
692{
Philipp Reisnerfda74112011-02-10 10:38:06 +0100693 enum drbd_state_rv rv;
Philipp Reisner35095022011-02-09 16:29:33 +0100694
Philipp Reisnerfda74112011-02-10 10:38:06 +0100695 rv = is_valid_conn_transition(os.conn, ns.conn);
Philipp Reisner35095022011-02-09 16:29:33 +0100696
697 /* we cannot fail (again) if we already detached */
698 if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
699 rv = SS_IS_DISKLESS;
700
701 return rv;
702}
703
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200704static void print_sanitize_warnings(struct drbd_device *device, enum sanitize_state_warnings warn)
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200705{
706 static const char *msg_table[] = {
707 [NO_WARNING] = "",
708 [ABORTED_ONLINE_VERIFY] = "Online-verify aborted.",
709 [ABORTED_RESYNC] = "Resync aborted.",
710 [CONNECTION_LOST_NEGOTIATING] = "Connection lost while negotiating, no data!",
711 [IMPLICITLY_UPGRADED_DISK] = "Implicitly upgraded disk",
712 [IMPLICITLY_UPGRADED_PDSK] = "Implicitly upgraded pdsk",
713 };
714
715 if (warn != NO_WARNING)
716 dev_warn(DEV, "%s\n", msg_table[warn]);
717}
718
Philipp Reisner35095022011-02-09 16:29:33 +0100719/**
Philipp Reisnerb8907332011-01-27 14:07:51 +0100720 * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200721 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100722 * @os: old state.
723 * @ns: new state.
724 * @warn_sync_abort:
725 *
726 * When we loose connection, we have to set the state of the peers disk (pdsk)
727 * to D_UNKNOWN. This rule and many more along those lines are in this function.
728 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200729static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state ns,
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200730 enum sanitize_state_warnings *warn)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100731{
732 enum drbd_fencing_p fp;
733 enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
734
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200735 if (warn)
736 *warn = NO_WARNING;
737
Philipp Reisnerb8907332011-01-27 14:07:51 +0100738 fp = FP_DONT_CARE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200739 if (get_ldev(device)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200740 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200741 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200742 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200743 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100744 }
745
Philipp Reisner35095022011-02-09 16:29:33 +0100746 /* Implications from connection to peer and peer_isp */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100747 if (ns.conn < C_CONNECTED) {
748 ns.peer_isp = 0;
749 ns.peer = R_UNKNOWN;
750 if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
751 ns.pdsk = D_UNKNOWN;
752 }
753
754 /* Clear the aftr_isp when becoming unconfigured */
755 if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
756 ns.aftr_isp = 0;
757
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100758 /* An implication of the disk states onto the connection state */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100759 /* Abort resync if a disk fails/detaches */
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100760 if (ns.conn > C_CONNECTED && (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200761 if (warn)
762 *warn = ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T ?
763 ABORTED_ONLINE_VERIFY : ABORTED_RESYNC;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100764 ns.conn = C_CONNECTED;
765 }
766
767 /* Connection breaks down before we finished "Negotiating" */
768 if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200769 get_ldev_if_state(device, D_NEGOTIATING)) {
770 if (device->ed_uuid == device->ldev->md.uuid[UI_CURRENT]) {
771 ns.disk = device->new_state_tmp.disk;
772 ns.pdsk = device->new_state_tmp.pdsk;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100773 } else {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200774 if (warn)
775 *warn = CONNECTION_LOST_NEGOTIATING;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100776 ns.disk = D_DISKLESS;
777 ns.pdsk = D_UNKNOWN;
778 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200779 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100780 }
781
782 /* D_CONSISTENT and D_OUTDATED vanish when we get connected */
783 if (ns.conn >= C_CONNECTED && ns.conn < C_AHEAD) {
784 if (ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED)
785 ns.disk = D_UP_TO_DATE;
786 if (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)
787 ns.pdsk = D_UP_TO_DATE;
788 }
789
790 /* Implications of the connection stat on the disk states */
791 disk_min = D_DISKLESS;
792 disk_max = D_UP_TO_DATE;
793 pdsk_min = D_INCONSISTENT;
794 pdsk_max = D_UNKNOWN;
795 switch ((enum drbd_conns)ns.conn) {
796 case C_WF_BITMAP_T:
797 case C_PAUSED_SYNC_T:
798 case C_STARTING_SYNC_T:
799 case C_WF_SYNC_UUID:
800 case C_BEHIND:
801 disk_min = D_INCONSISTENT;
802 disk_max = D_OUTDATED;
803 pdsk_min = D_UP_TO_DATE;
804 pdsk_max = D_UP_TO_DATE;
805 break;
806 case C_VERIFY_S:
807 case C_VERIFY_T:
808 disk_min = D_UP_TO_DATE;
809 disk_max = D_UP_TO_DATE;
810 pdsk_min = D_UP_TO_DATE;
811 pdsk_max = D_UP_TO_DATE;
812 break;
813 case C_CONNECTED:
814 disk_min = D_DISKLESS;
815 disk_max = D_UP_TO_DATE;
816 pdsk_min = D_DISKLESS;
817 pdsk_max = D_UP_TO_DATE;
818 break;
819 case C_WF_BITMAP_S:
820 case C_PAUSED_SYNC_S:
821 case C_STARTING_SYNC_S:
822 case C_AHEAD:
823 disk_min = D_UP_TO_DATE;
824 disk_max = D_UP_TO_DATE;
825 pdsk_min = D_INCONSISTENT;
826 pdsk_max = D_CONSISTENT; /* D_OUTDATED would be nice. But explicit outdate necessary*/
827 break;
828 case C_SYNC_TARGET:
829 disk_min = D_INCONSISTENT;
830 disk_max = D_INCONSISTENT;
831 pdsk_min = D_UP_TO_DATE;
832 pdsk_max = D_UP_TO_DATE;
833 break;
834 case C_SYNC_SOURCE:
835 disk_min = D_UP_TO_DATE;
836 disk_max = D_UP_TO_DATE;
837 pdsk_min = D_INCONSISTENT;
838 pdsk_max = D_INCONSISTENT;
839 break;
840 case C_STANDALONE:
841 case C_DISCONNECTING:
842 case C_UNCONNECTED:
843 case C_TIMEOUT:
844 case C_BROKEN_PIPE:
845 case C_NETWORK_FAILURE:
846 case C_PROTOCOL_ERROR:
847 case C_TEAR_DOWN:
848 case C_WF_CONNECTION:
849 case C_WF_REPORT_PARAMS:
850 case C_MASK:
851 break;
852 }
853 if (ns.disk > disk_max)
854 ns.disk = disk_max;
855
856 if (ns.disk < disk_min) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200857 if (warn)
858 *warn = IMPLICITLY_UPGRADED_DISK;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100859 ns.disk = disk_min;
860 }
861 if (ns.pdsk > pdsk_max)
862 ns.pdsk = pdsk_max;
863
864 if (ns.pdsk < pdsk_min) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200865 if (warn)
866 *warn = IMPLICITLY_UPGRADED_PDSK;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100867 ns.pdsk = pdsk_min;
868 }
869
870 if (fp == FP_STONITH &&
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100871 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100872 ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
873
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +0200874 if (device->resource->res_opts.on_no_data == OND_SUSPEND_IO &&
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100875 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100876 ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
877
878 if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
879 if (ns.conn == C_SYNC_SOURCE)
880 ns.conn = C_PAUSED_SYNC_S;
881 if (ns.conn == C_SYNC_TARGET)
882 ns.conn = C_PAUSED_SYNC_T;
883 } else {
884 if (ns.conn == C_PAUSED_SYNC_S)
885 ns.conn = C_SYNC_SOURCE;
886 if (ns.conn == C_PAUSED_SYNC_T)
887 ns.conn = C_SYNC_TARGET;
888 }
889
890 return ns;
891}
892
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200893void drbd_resume_al(struct drbd_device *device)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100894{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200895 if (test_and_clear_bit(AL_SUSPENDED, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100896 dev_info(DEV, "Resumed AL updates\n");
897}
898
899/* helper for __drbd_set_state */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200900static void set_ov_position(struct drbd_device *device, enum drbd_conns cs)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100901{
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200902 if (first_peer_device(device)->connection->agreed_pro_version < 90)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200903 device->ov_start_sector = 0;
904 device->rs_total = drbd_bm_bits(device);
905 device->ov_position = 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100906 if (cs == C_VERIFY_T) {
907 /* starting online verify from an arbitrary position
908 * does not fit well into the existing protocol.
909 * on C_VERIFY_T, we initialize ov_left and friends
910 * implicitly in receive_DataRequest once the
911 * first P_OV_REQUEST is received */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200912 device->ov_start_sector = ~(sector_t)0;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100913 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200914 unsigned long bit = BM_SECT_TO_BIT(device->ov_start_sector);
915 if (bit >= device->rs_total) {
916 device->ov_start_sector =
917 BM_BIT_TO_SECT(device->rs_total - 1);
918 device->rs_total = 1;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100919 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200920 device->rs_total -= bit;
921 device->ov_position = device->ov_start_sector;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100922 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200923 device->ov_left = device->rs_total;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100924}
925
926/**
927 * __drbd_set_state() - Set a new DRBD state
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200928 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100929 * @ns: new state.
930 * @flags: Flags
931 * @done: Optional completion, that will get completed after the after_state_ch() finished
932 *
933 * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
934 */
935enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200936__drbd_set_state(struct drbd_device *device, union drbd_state ns,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100937 enum chg_state_flags flags, struct completion *done)
938{
939 union drbd_state os;
940 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200941 enum sanitize_state_warnings ssw;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100942 struct after_state_chg_work *ascw;
Lars Ellenberg2681f7f2013-01-21 15:43:41 +0100943 bool did_remote, should_do_remote;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100944
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200945 os = drbd_read_state(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100946
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200947 ns = sanitize_state(device, ns, &ssw);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100948 if (ns.i == os.i)
949 return SS_NOTHING_TO_DO;
950
Philipp Reisner35095022011-02-09 16:29:33 +0100951 rv = is_valid_transition(os, ns);
952 if (rv < SS_SUCCESS)
953 return rv;
954
Philipp Reisnerb8907332011-01-27 14:07:51 +0100955 if (!(flags & CS_HARD)) {
956 /* pre-state-change checks ; only look at ns */
957 /* See drbd_state_sw_errors in drbd_strings.c */
958
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200959 rv = is_valid_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100960 if (rv < SS_SUCCESS) {
961 /* If the old state was illegal as well, then let
962 this happen...*/
963
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200964 if (is_valid_state(device, os) == rv)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200965 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100966 } else
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200967 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100968 }
969
970 if (rv < SS_SUCCESS) {
971 if (flags & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200972 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100973 return rv;
974 }
975
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200976 print_sanitize_warnings(device, ssw);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100977
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200978 drbd_pr_state_change(device, os, ns, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100979
Philipp Reisner706cb242011-03-29 15:20:27 +0200980 /* Display changes to the susp* flags that where caused by the call to
981 sanitize_state(). Only display it here if we where not called from
982 _conn_request_state() */
983 if (!(flags & CS_DC_SUSP))
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200984 conn_pr_state_change(first_peer_device(device)->connection, os, ns,
985 (flags & ~CS_DC_MASK) | CS_DC_SUSP);
Philipp Reisner706cb242011-03-29 15:20:27 +0200986
Philipp Reisnerb8907332011-01-27 14:07:51 +0100987 /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
988 * on the ldev here, to be sure the transition -> D_DISKLESS resp.
989 * drbd_ldev_destroy() won't happen before our corresponding
990 * after_state_ch works run, where we put_ldev again. */
991 if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
992 (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200993 atomic_inc(&device->local_cnt);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100994
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200995 did_remote = drbd_should_do_remote(device->state);
996 device->state.i = ns.i;
997 should_do_remote = drbd_should_do_remote(device->state);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200998 first_peer_device(device)->connection->susp = ns.susp;
999 first_peer_device(device)->connection->susp_nod = ns.susp_nod;
1000 first_peer_device(device)->connection->susp_fen = ns.susp_fen;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001001
Lars Ellenberg2681f7f2013-01-21 15:43:41 +01001002 /* put replicated vs not-replicated requests in seperate epochs */
1003 if (did_remote != should_do_remote)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001004 start_new_tl_epoch(first_peer_device(device)->connection);
Lars Ellenberg2681f7f2013-01-21 15:43:41 +01001005
Philipp Reisnerb8907332011-01-27 14:07:51 +01001006 if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001007 drbd_print_uuids(device, "attached to UUIDs");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001008
Philipp Reisner79702012012-08-28 11:33:35 +02001009 /* Wake up role changes, that were delayed because of connection establishing */
1010 if (os.conn == C_WF_REPORT_PARAMS && ns.conn != C_WF_REPORT_PARAMS &&
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001011 no_peer_wf_report_params(first_peer_device(device)->connection))
1012 clear_bit(STATE_SENT, &first_peer_device(device)->connection->flags);
Philipp Reisner79702012012-08-28 11:33:35 +02001013
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001014 wake_up(&device->misc_wait);
1015 wake_up(&device->state_wait);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001016 wake_up(&first_peer_device(device)->connection->ping_wait);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001017
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001018 /* Aborted verify run, or we reached the stop sector.
1019 * Log the last position, unless end-of-device. */
Philipp Reisnerb8907332011-01-27 14:07:51 +01001020 if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001021 ns.conn <= C_CONNECTED) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001022 device->ov_start_sector =
1023 BM_BIT_TO_SECT(drbd_bm_bits(device) - device->ov_left);
1024 if (device->ov_left)
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001025 dev_info(DEV, "Online Verify reached sector %llu\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001026 (unsigned long long)device->ov_start_sector);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001027 }
1028
1029 if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
1030 (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
1031 dev_info(DEV, "Syncer continues.\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001032 device->rs_paused += (long)jiffies
1033 -(long)device->rs_mark_time[device->rs_last_mark];
Philipp Reisnerb8907332011-01-27 14:07:51 +01001034 if (ns.conn == C_SYNC_TARGET)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001035 mod_timer(&device->resync_timer, jiffies);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001036 }
1037
1038 if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
1039 (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
1040 dev_info(DEV, "Resync suspended\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001041 device->rs_mark_time[device->rs_last_mark] = jiffies;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001042 }
1043
1044 if (os.conn == C_CONNECTED &&
1045 (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
1046 unsigned long now = jiffies;
1047 int i;
1048
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001049 set_ov_position(device, ns.conn);
1050 device->rs_start = now;
1051 device->rs_last_events = 0;
1052 device->rs_last_sect_ev = 0;
1053 device->ov_last_oos_size = 0;
1054 device->ov_last_oos_start = 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001055
1056 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001057 device->rs_mark_left[i] = device->ov_left;
1058 device->rs_mark_time[i] = now;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001059 }
1060
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001061 drbd_rs_controller_reset(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001062
1063 if (ns.conn == C_VERIFY_S) {
1064 dev_info(DEV, "Starting Online Verify from sector %llu\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001065 (unsigned long long)device->ov_position);
1066 mod_timer(&device->resync_timer, jiffies);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001067 }
1068 }
1069
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001070 if (get_ldev(device)) {
1071 u32 mdf = device->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
Philipp Reisnerb8907332011-01-27 14:07:51 +01001072 MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
1073 MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
1074
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02001075 mdf &= ~MDF_AL_CLEAN;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001076 if (test_bit(CRASHED_PRIMARY, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +01001077 mdf |= MDF_CRASHED_PRIMARY;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001078 if (device->state.role == R_PRIMARY ||
1079 (device->state.pdsk < D_INCONSISTENT && device->state.peer == R_PRIMARY))
Philipp Reisnerb8907332011-01-27 14:07:51 +01001080 mdf |= MDF_PRIMARY_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001081 if (device->state.conn > C_WF_REPORT_PARAMS)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001082 mdf |= MDF_CONNECTED_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001083 if (device->state.disk > D_INCONSISTENT)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001084 mdf |= MDF_CONSISTENT;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001085 if (device->state.disk > D_OUTDATED)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001086 mdf |= MDF_WAS_UP_TO_DATE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001087 if (device->state.pdsk <= D_OUTDATED && device->state.pdsk >= D_INCONSISTENT)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001088 mdf |= MDF_PEER_OUT_DATED;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001089 if (mdf != device->ldev->md.flags) {
1090 device->ldev->md.flags = mdf;
1091 drbd_md_mark_dirty(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001092 }
1093 if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001094 drbd_set_ed_uuid(device, device->ldev->md.uuid[UI_CURRENT]);
1095 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001096 }
1097
1098 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
1099 if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
1100 os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001101 set_bit(CONSIDER_RESYNC, &device->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001102
1103 /* Receiver should clean up itself */
1104 if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001105 drbd_thread_stop_nowait(&first_peer_device(device)->connection->receiver);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001106
1107 /* Now the receiver finished cleaning up itself, it should die */
1108 if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001109 drbd_thread_stop_nowait(&first_peer_device(device)->connection->receiver);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001110
1111 /* Upon network failure, we need to restart the receiver. */
Philipp Reisner823bd832012-11-08 15:04:36 +01001112 if (os.conn > C_WF_CONNECTION &&
Philipp Reisnerb8907332011-01-27 14:07:51 +01001113 ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001114 drbd_thread_restart_nowait(&first_peer_device(device)->connection->receiver);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001115
1116 /* Resume AL writing if we get a connection */
Philipp Reisner28e448b2013-06-25 16:50:06 +02001117 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001118 drbd_resume_al(device);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001119 first_peer_device(device)->connection->connect_cnt++;
Philipp Reisner28e448b2013-06-25 16:50:06 +02001120 }
Philipp Reisnerb8907332011-01-27 14:07:51 +01001121
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001122 /* remember last attach time so request_timer_fn() won't
1123 * kill newly established sessions while we are still trying to thaw
1124 * previously frozen IO */
1125 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
1126 ns.disk > D_NEGOTIATING)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001127 device->last_reattach_jif = jiffies;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001128
Philipp Reisnerb8907332011-01-27 14:07:51 +01001129 ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
1130 if (ascw) {
1131 ascw->os = os;
1132 ascw->ns = ns;
1133 ascw->flags = flags;
1134 ascw->w.cb = w_after_state_ch;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001135 ascw->w.device = device;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001136 ascw->done = done;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001137 drbd_queue_work(&first_peer_device(device)->connection->sender_work, &ascw->w);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001138 } else {
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001139 dev_err(DEV, "Could not kmalloc an ascw\n");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001140 }
1141
1142 return rv;
1143}
1144
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001145static int w_after_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001146{
1147 struct after_state_chg_work *ascw =
1148 container_of(w, struct after_state_chg_work, w);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001149 struct drbd_device *device = w->device;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001150
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001151 after_state_ch(device, ascw->os, ascw->ns, ascw->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001152 if (ascw->flags & CS_WAIT_COMPLETE) {
1153 D_ASSERT(ascw->done != NULL);
1154 complete(ascw->done);
1155 }
1156 kfree(ascw);
1157
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001158 return 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001159}
1160
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001161static void abw_start_sync(struct drbd_device *device, int rv)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001162{
1163 if (rv) {
1164 dev_err(DEV, "Writing the bitmap failed not starting resync.\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001165 _drbd_request_state(device, NS(conn, C_CONNECTED), CS_VERBOSE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001166 return;
1167 }
1168
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001169 switch (device->state.conn) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001170 case C_STARTING_SYNC_T:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001171 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001172 break;
1173 case C_STARTING_SYNC_S:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001174 drbd_start_resync(device, C_SYNC_SOURCE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001175 break;
1176 }
1177}
1178
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001179int drbd_bitmap_io_from_worker(struct drbd_device *device,
Andreas Gruenbacher54761692011-05-30 16:15:21 +02001180 int (*io_fn)(struct drbd_device *),
Philipp Reisnerb8907332011-01-27 14:07:51 +01001181 char *why, enum bm_flag flags)
1182{
1183 int rv;
1184
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001185 D_ASSERT(current == first_peer_device(device)->connection->worker.task);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001186
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001187 /* open coded non-blocking drbd_suspend_io(device); */
1188 set_bit(SUSPEND_IO, &device->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001189
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001190 drbd_bm_lock(device, why, flags);
1191 rv = io_fn(device);
1192 drbd_bm_unlock(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001193
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001194 drbd_resume_io(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001195
1196 return rv;
1197}
1198
1199/**
1200 * after_state_ch() - Perform after state change actions that may sleep
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001201 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +01001202 * @os: old state.
1203 * @ns: new state.
1204 * @flags: Flags
1205 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001206static void after_state_ch(struct drbd_device *device, union drbd_state os,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001207 union drbd_state ns, enum chg_state_flags flags)
1208{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001209 struct sib_info sib;
1210
1211 sib.sib_reason = SIB_STATE_CHANGE;
1212 sib.os = os;
1213 sib.ns = ns;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001214
1215 if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001216 clear_bit(CRASHED_PRIMARY, &device->flags);
1217 if (device->p_uuid)
1218 device->p_uuid[UI_FLAGS] &= ~((u64)2);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001219 }
1220
Philipp Reisnerb8907332011-01-27 14:07:51 +01001221 /* Inform userspace about the change... */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001222 drbd_bcast_event(device, &sib);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001223
1224 if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
1225 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001226 drbd_khelper(device, "pri-on-incon-degr");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001227
1228 /* Here we have the actions that are performed after a
1229 state change. This function might sleep */
1230
Philipp Reisnerb8907332011-01-27 14:07:51 +01001231 if (ns.susp_nod) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001232 struct drbd_connection *connection = first_peer_device(device)->connection;
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001233 enum drbd_req_event what = NOTHING;
1234
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001235 spin_lock_irq(&connection->req_lock);
1236 if (os.conn < C_CONNECTED && conn_lowest_conn(connection) >= C_CONNECTED)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001237 what = RESEND;
1238
Philipp Reisner3fb47462011-07-15 18:44:26 +02001239 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001240 conn_lowest_disk(connection) > D_NEGOTIATING)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001241 what = RESTART_FROZEN_DISK_IO;
1242
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001243 if (connection->susp_nod && what != NOTHING) {
1244 _tl_restart(connection, what);
1245 _conn_request_state(connection,
Philipp Reisner892fdd12012-08-27 17:20:12 +02001246 (union drbd_state) { { .susp_nod = 1 } },
1247 (union drbd_state) { { .susp_nod = 0 } },
1248 CS_VERBOSE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001249 }
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001250 spin_unlock_irq(&connection->req_lock);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001251 }
1252
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001253 if (ns.susp_fen) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001254 struct drbd_connection *connection = first_peer_device(device)->connection;
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001255
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001256 spin_lock_irq(&connection->req_lock);
1257 if (connection->susp_fen && conn_lowest_conn(connection) >= C_CONNECTED) {
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001258 /* case2: The connection was established again: */
Andreas Gruenbacher54761692011-05-30 16:15:21 +02001259 struct drbd_device *odev;
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001260 int vnr;
1261
1262 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001263 idr_for_each_entry(&connection->volumes, odev, vnr)
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001264 clear_bit(NEW_CUR_UUID, &odev->flags);
1265 rcu_read_unlock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001266 _tl_restart(connection, RESEND);
1267 _conn_request_state(connection,
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001268 (union drbd_state) { { .susp_fen = 1 } },
1269 (union drbd_state) { { .susp_fen = 0 } },
1270 CS_VERBOSE);
1271 }
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001272 spin_unlock_irq(&connection->req_lock);
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001273 }
1274
Philipp Reisnerb8907332011-01-27 14:07:51 +01001275 /* Became sync source. With protocol >= 96, we still need to send out
1276 * the sync uuid now. Need to do that before any drbd_send_state, or
1277 * the other side may go "paused sync" before receiving the sync uuids,
1278 * which is unexpected. */
1279 if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
1280 (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001281 first_peer_device(device)->connection->agreed_pro_version >= 96 && get_ldev(device)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001282 drbd_gen_and_send_sync_uuid(device);
1283 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001284 }
1285
1286 /* Do not change the order of the if above and the two below... */
Philipp Reisner369bea62011-07-06 23:04:44 +02001287 if (os.pdsk == D_DISKLESS &&
1288 ns.pdsk > D_DISKLESS && ns.pdsk != D_UNKNOWN) { /* attach on the peer */
Lars Ellenberga3248962012-07-30 09:10:41 +02001289 /* we probably will start a resync soon.
1290 * make sure those things are properly reset. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001291 device->rs_total = 0;
1292 device->rs_failed = 0;
1293 atomic_set(&device->rs_pending_cnt, 0);
1294 drbd_rs_cancel_all(device);
Lars Ellenberga3248962012-07-30 09:10:41 +02001295
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001296 drbd_send_uuids(device);
1297 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001298 }
1299 /* No point in queuing send_bitmap if we don't have a connection
1300 * anymore, so check also the _current_ state, not only the new state
1301 * at the time this work was queued. */
1302 if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001303 device->state.conn == C_WF_BITMAP_S)
1304 drbd_queue_bitmap_io(device, &drbd_send_bitmap, NULL,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001305 "send_bitmap (WFBitMapS)",
1306 BM_LOCKED_TEST_ALLOWED);
1307
1308 /* Lost contact to peer's copy of the data */
1309 if ((os.pdsk >= D_INCONSISTENT &&
1310 os.pdsk != D_UNKNOWN &&
1311 os.pdsk != D_OUTDATED)
1312 && (ns.pdsk < D_INCONSISTENT ||
1313 ns.pdsk == D_UNKNOWN ||
1314 ns.pdsk == D_OUTDATED)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001315 if (get_ldev(device)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001316 if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001317 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1318 if (drbd_suspended(device)) {
1319 set_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001320 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001321 drbd_uuid_new_current(device);
1322 drbd_send_uuids(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001323 }
1324 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001325 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001326 }
1327 }
1328
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001329 if (ns.pdsk < D_INCONSISTENT && get_ldev(device)) {
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001330 if (os.peer == R_SECONDARY && ns.peer == R_PRIMARY &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001331 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1332 drbd_uuid_new_current(device);
1333 drbd_send_uuids(device);
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001334 }
Philipp Reisnerb8907332011-01-27 14:07:51 +01001335 /* D_DISKLESS Peer becomes secondary */
1336 if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
1337 /* We may still be Primary ourselves.
1338 * No harm done if the bitmap still changes,
1339 * redirtied pages will follow later. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001340 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001341 "demote diskless peer", BM_LOCKED_SET_ALLOWED);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001342 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001343 }
1344
1345 /* Write out all changed bits on demote.
1346 * Though, no need to da that just yet
1347 * if there is a resync going on still */
1348 if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001349 device->state.conn <= C_CONNECTED && get_ldev(device)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001350 /* No changes to the bitmap expected this time, so assert that,
1351 * even though no harm was done if it did change. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001352 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001353 "demote", BM_LOCKED_TEST_ALLOWED);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001354 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001355 }
1356
1357 /* Last part of the attaching process ... */
1358 if (ns.conn >= C_CONNECTED &&
1359 os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001360 drbd_send_sizes(device, 0, 0); /* to start sync... */
1361 drbd_send_uuids(device);
1362 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001363 }
1364
1365 /* We want to pause/continue resync, tell peer. */
1366 if (ns.conn >= C_CONNECTED &&
1367 ((os.aftr_isp != ns.aftr_isp) ||
1368 (os.user_isp != ns.user_isp)))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001369 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001370
1371 /* In case one of the isp bits got set, suspend other devices. */
1372 if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
1373 (ns.aftr_isp || ns.peer_isp || ns.user_isp))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001374 suspend_other_sg(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001375
1376 /* Make sure the peer gets informed about eventual state
1377 changes (ISP bits) while we were in WFReportParams. */
1378 if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001379 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001380
1381 if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001382 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001383
1384 /* We are in the progress to start a full sync... */
1385 if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
1386 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
1387 /* no other bitmap changes expected during this phase */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001388 drbd_queue_bitmap_io(device,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001389 &drbd_bmio_set_n_write, &abw_start_sync,
1390 "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
1391
Philipp Reisnerb8907332011-01-27 14:07:51 +01001392 /* first half of local IO error, failure to attach,
1393 * or administrative detach */
1394 if (os.disk != D_FAILED && ns.disk == D_FAILED) {
Philipp Reisner32db80f2012-02-22 11:51:57 +01001395 enum drbd_io_error_p eh = EP_PASS_ON;
1396 int was_io_error = 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001397 /* corresponding get_ldev was in __drbd_set_state, to serialize
Philipp Reisner32db80f2012-02-22 11:51:57 +01001398 * our cleanup here with the transition to D_DISKLESS.
1399 * But is is still not save to dreference ldev here, since
1400 * we might come from an failed Attach before ldev was set. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001401 if (device->ldev) {
Philipp Reisner32db80f2012-02-22 11:51:57 +01001402 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001403 eh = rcu_dereference(device->ldev->disk_conf)->on_io_error;
Philipp Reisner32db80f2012-02-22 11:51:57 +01001404 rcu_read_unlock();
Philipp Reisnerb8907332011-01-27 14:07:51 +01001405
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001406 was_io_error = test_and_clear_bit(WAS_IO_ERROR, &device->flags);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001407
Lars Ellenberg6f1a6562012-07-30 09:11:01 +02001408 if (was_io_error && eh == EP_CALL_HELPER)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001409 drbd_khelper(device, "local-io-error");
Lars Ellenberg6f1a6562012-07-30 09:11:01 +02001410
Lars Ellenberg0c849662012-07-30 09:07:28 +02001411 /* Immediately allow completion of all application IO,
1412 * that waits for completion from the local disk,
1413 * if this was a force-detach due to disk_timeout
1414 * or administrator request (drbdsetup detach --force).
1415 * Do NOT abort otherwise.
1416 * Aborting local requests may cause serious problems,
1417 * if requests are completed to upper layers already,
1418 * and then later the already submitted local bio completes.
1419 * This can cause DMA into former bio pages that meanwhile
1420 * have been re-used for other things.
1421 * So aborting local requests may cause crashes,
1422 * or even worse, silent data corruption.
1423 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001424 if (test_and_clear_bit(FORCE_DETACH, &device->flags))
1425 tl_abort_disk_io(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001426
Philipp Reisner32db80f2012-02-22 11:51:57 +01001427 /* current state still has to be D_FAILED,
1428 * there is only one way out: to D_DISKLESS,
1429 * and that may only happen after our put_ldev below. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001430 if (device->state.disk != D_FAILED)
Philipp Reisner32db80f2012-02-22 11:51:57 +01001431 dev_err(DEV,
1432 "ASSERT FAILED: disk is %s during detach\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001433 drbd_disk_str(device->state.disk));
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001434
Philipp Reisner32db80f2012-02-22 11:51:57 +01001435 if (ns.conn >= C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001436 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001437
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001438 drbd_rs_cancel_all(device);
Philipp Reisner32db80f2012-02-22 11:51:57 +01001439
1440 /* In case we want to get something to stable storage still,
1441 * this may be the last chance.
1442 * Following put_ldev may transition to D_DISKLESS. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001443 drbd_md_sync(device);
Philipp Reisner32db80f2012-02-22 11:51:57 +01001444 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001445 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001446 }
1447
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001448 /* second half of local IO error, failure to attach,
1449 * or administrative detach,
1450 * after local_cnt references have reached zero again */
1451 if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
1452 /* We must still be diskless,
1453 * re-attach has to be serialized with this! */
1454 if (device->state.disk != D_DISKLESS)
1455 dev_err(DEV,
1456 "ASSERT FAILED: disk is %s while going diskless\n",
1457 drbd_disk_str(device->state.disk));
Philipp Reisnerb8907332011-01-27 14:07:51 +01001458
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001459 if (ns.conn >= C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001460 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001461 /* corresponding get_ldev in __drbd_set_state
1462 * this may finally trigger drbd_ldev_destroy. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001463 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001464 }
1465
1466 /* Notify peer that I had a local IO error, and did not detached.. */
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001467 if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT && ns.conn >= C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001468 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001469
1470 /* Disks got bigger while they were detached */
1471 if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001472 test_and_clear_bit(RESYNC_AFTER_NEG, &device->flags)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001473 if (ns.conn == C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001474 resync_after_online_grow(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001475 }
1476
1477 /* A resync finished or aborted, wake paused devices... */
1478 if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
1479 (os.peer_isp && !ns.peer_isp) ||
1480 (os.user_isp && !ns.user_isp))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001481 resume_next_sg(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001482
1483 /* sync target done with resync. Explicitly notify peer, even though
1484 * it should (at least for non-empty resyncs) already know itself. */
1485 if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001486 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001487
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001488 /* Verify finished, or reached stop sector. Peer did not know about
1489 * the stop sector, and we may even have changed the stop sector during
1490 * verify to interrupt/stop early. Send the new state. */
1491 if (os.conn == C_VERIFY_S && ns.conn == C_CONNECTED
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001492 && verify_can_do_stop_sector(device))
1493 drbd_send_state(device, ns);
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001494
Philipp Reisnerb8907332011-01-27 14:07:51 +01001495 /* This triggers bitmap writeout of potentially still unwritten pages
1496 * if the resync finished cleanly, or aborted because of peer disk
1497 * failure, or because of connection loss.
1498 * For resync aborted because of local disk failure, we cannot do
1499 * any bitmap writeout anymore.
1500 * No harm done if some bits change during this phase.
1501 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001502 if (os.conn > C_CONNECTED && ns.conn <= C_CONNECTED && get_ldev(device)) {
1503 drbd_queue_bitmap_io(device, &drbd_bm_write_copy_pages, NULL,
Lars Ellenberga220d292012-05-07 12:07:18 +02001504 "write from resync_finished", BM_LOCKED_CHANGE_ALLOWED);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001505 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001506 }
1507
1508 if (ns.disk == D_DISKLESS &&
1509 ns.conn == C_STANDALONE &&
1510 ns.role == R_SECONDARY) {
1511 if (os.aftr_isp != ns.aftr_isp)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001512 resume_next_sg(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001513 }
1514
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001515 drbd_md_sync(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001516}
1517
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001518struct after_conn_state_chg_work {
1519 struct drbd_work w;
1520 enum drbd_conns oc;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001521 union drbd_state ns_min;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001522 union drbd_state ns_max; /* new, max state, over all devices */
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001523 enum chg_state_flags flags;
1524};
Philipp Reisnerb8907332011-01-27 14:07:51 +01001525
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001526static int w_after_conn_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001527{
1528 struct after_conn_state_chg_work *acscw =
1529 container_of(w, struct after_conn_state_chg_work, w);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001530 struct drbd_connection *connection = w->connection;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001531 enum drbd_conns oc = acscw->oc;
Philipp Reisner5f082f92011-03-29 13:20:58 +02001532 union drbd_state ns_max = acscw->ns_max;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001533 struct drbd_device *device;
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001534 int vnr;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001535
1536 kfree(acscw);
1537
1538 /* Upon network configuration, we need to start the receiver */
Philipp Reisner5f082f92011-03-29 13:20:58 +02001539 if (oc == C_STANDALONE && ns_max.conn == C_UNCONNECTED)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001540 drbd_thread_start(&connection->receiver);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001541
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02001542 if (oc == C_DISCONNECTING && ns_max.conn == C_STANDALONE) {
1543 struct net_conf *old_conf;
1544
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001545 mutex_lock(&connection->conf_update);
1546 old_conf = connection->net_conf;
1547 connection->my_addr_len = 0;
1548 connection->peer_addr_len = 0;
1549 rcu_assign_pointer(connection->net_conf, NULL);
1550 conn_free_crypto(connection);
1551 mutex_unlock(&connection->conf_update);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02001552
1553 synchronize_rcu();
1554 kfree(old_conf);
1555 }
1556
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001557 if (ns_max.susp_fen) {
1558 /* case1: The outdate peer handler is successful: */
1559 if (ns_max.pdsk <= D_OUTDATED) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001560 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001561 idr_for_each_entry(&connection->volumes, device, vnr) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001562 if (test_bit(NEW_CUR_UUID, &device->flags)) {
1563 drbd_uuid_new_current(device);
1564 clear_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001565 }
1566 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001567 rcu_read_unlock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001568 spin_lock_irq(&connection->req_lock);
1569 _tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
1570 _conn_request_state(connection,
Philipp Reisner8a0bab22012-08-07 13:28:00 +02001571 (union drbd_state) { { .susp_fen = 1 } },
1572 (union drbd_state) { { .susp_fen = 0 } },
1573 CS_VERBOSE);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001574 spin_unlock_irq(&connection->req_lock);
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001575 }
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001576 }
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02001577 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner19fffd72012-08-28 16:48:03 +02001578
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001579 conn_md_sync(connection);
Philipp Reisner19fffd72012-08-28 16:48:03 +02001580
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001581 return 0;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001582}
1583
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001584void conn_old_common_state(struct drbd_connection *connection, union drbd_state *pcs, enum chg_state_flags *pf)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001585{
Philipp Reisner435693e2011-03-25 15:11:30 +01001586 enum chg_state_flags flags = ~0;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001587 struct drbd_device *device;
Philipp Reisner435693e2011-03-25 15:11:30 +01001588 int vnr, first_vol = 1;
Philipp Reisnere0e16652011-07-11 17:04:23 +02001589 union drbd_dev_state os, cs = {
1590 { .role = R_SECONDARY,
1591 .peer = R_UNKNOWN,
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001592 .conn = connection->cstate,
Philipp Reisnere0e16652011-07-11 17:04:23 +02001593 .disk = D_DISKLESS,
1594 .pdsk = D_UNKNOWN,
1595 } };
Philipp Reisner88ef5942011-03-25 14:31:11 +01001596
Philipp Reisner695d08f2011-04-11 22:53:32 -07001597 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001598 idr_for_each_entry(&connection->volumes, device, vnr) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001599 os = device->state;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001600
Philipp Reisner435693e2011-03-25 15:11:30 +01001601 if (first_vol) {
1602 cs = os;
1603 first_vol = 0;
1604 continue;
1605 }
Philipp Reisner88ef5942011-03-25 14:31:11 +01001606
Philipp Reisner435693e2011-03-25 15:11:30 +01001607 if (cs.role != os.role)
1608 flags &= ~CS_DC_ROLE;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001609
Philipp Reisner435693e2011-03-25 15:11:30 +01001610 if (cs.peer != os.peer)
1611 flags &= ~CS_DC_PEER;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001612
Philipp Reisner435693e2011-03-25 15:11:30 +01001613 if (cs.conn != os.conn)
1614 flags &= ~CS_DC_CONN;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001615
Philipp Reisner435693e2011-03-25 15:11:30 +01001616 if (cs.disk != os.disk)
1617 flags &= ~CS_DC_DISK;
1618
1619 if (cs.pdsk != os.pdsk)
1620 flags &= ~CS_DC_PDSK;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001621 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001622 rcu_read_unlock();
Philipp Reisner88ef5942011-03-25 14:31:11 +01001623
Philipp Reisner435693e2011-03-25 15:11:30 +01001624 *pf |= CS_DC_MASK;
1625 *pf &= flags;
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02001626 (*pcs).i = cs.i;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001627}
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001628
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001629static enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001630conn_is_valid_transition(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisner88ef5942011-03-25 14:31:11 +01001631 enum chg_state_flags flags)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001632{
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001633 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001634 union drbd_state ns, os;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001635 struct drbd_device *device;
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001636 int vnr;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001637
Philipp Reisner695d08f2011-04-11 22:53:32 -07001638 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001639 idr_for_each_entry(&connection->volumes, device, vnr) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001640 os = drbd_read_state(device);
1641 ns = sanitize_state(device, apply_mask_val(os, mask, val), NULL);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001642
Philipp Reisner778bcf22011-03-28 12:55:03 +02001643 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1644 ns.disk = os.disk;
1645
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001646 if (ns.i == os.i)
1647 continue;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001648
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001649 rv = is_valid_transition(os, ns);
1650 if (rv < SS_SUCCESS)
1651 break;
1652
1653 if (!(flags & CS_HARD)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001654 rv = is_valid_state(device, ns);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001655 if (rv < SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001656 if (is_valid_state(device, os) == rv)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001657 rv = is_valid_soft_transition(os, ns, connection);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001658 } else
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001659 rv = is_valid_soft_transition(os, ns, connection);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001660 }
1661 if (rv < SS_SUCCESS)
1662 break;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001663 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001664 rcu_read_unlock();
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001665
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001666 if (rv < SS_SUCCESS && flags & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001667 print_st_err(device, os, ns, rv);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001668
1669 return rv;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001670}
1671
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001672void
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001673conn_set_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001674 union drbd_state *pns_min, union drbd_state *pns_max, enum chg_state_flags flags)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001675{
Philipp Reisnerf132f552011-07-18 10:44:24 +02001676 union drbd_state ns, os, ns_max = { };
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001677 union drbd_state ns_min = {
1678 { .role = R_MASK,
1679 .peer = R_MASK,
Philipp Reisnere0e16652011-07-11 17:04:23 +02001680 .conn = val.conn,
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001681 .disk = D_MASK,
1682 .pdsk = D_MASK
1683 } };
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001684 struct drbd_device *device;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001685 enum drbd_state_rv rv;
Philipp Reisnerf132f552011-07-18 10:44:24 +02001686 int vnr, number_of_volumes = 0;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001687
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001688 if (mask.conn == C_MASK) {
1689 /* remember last connect time so request_timer_fn() won't
1690 * kill newly established sessions while we are still trying to thaw
1691 * previously frozen IO */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001692 if (connection->cstate != C_WF_REPORT_PARAMS && val.conn == C_WF_REPORT_PARAMS)
1693 connection->last_reconnect_jif = jiffies;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001694
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001695 connection->cstate = val.conn;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001696 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001697
Philipp Reisner695d08f2011-04-11 22:53:32 -07001698 rcu_read_lock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001699 idr_for_each_entry(&connection->volumes, device, vnr) {
Philipp Reisnerf132f552011-07-18 10:44:24 +02001700 number_of_volumes++;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001701 os = drbd_read_state(device);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001702 ns = apply_mask_val(os, mask, val);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001703 ns = sanitize_state(device, ns, NULL);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001704
Philipp Reisner778bcf22011-03-28 12:55:03 +02001705 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1706 ns.disk = os.disk;
1707
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001708 rv = __drbd_set_state(device, ns, flags, NULL);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001709 if (rv < SS_SUCCESS)
1710 BUG();
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001711
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001712 ns.i = device->state.i;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001713 ns_max.role = max_role(ns.role, ns_max.role);
1714 ns_max.peer = max_role(ns.peer, ns_max.peer);
1715 ns_max.conn = max_t(enum drbd_conns, ns.conn, ns_max.conn);
1716 ns_max.disk = max_t(enum drbd_disk_state, ns.disk, ns_max.disk);
1717 ns_max.pdsk = max_t(enum drbd_disk_state, ns.pdsk, ns_max.pdsk);
1718
1719 ns_min.role = min_role(ns.role, ns_min.role);
1720 ns_min.peer = min_role(ns.peer, ns_min.peer);
1721 ns_min.conn = min_t(enum drbd_conns, ns.conn, ns_min.conn);
1722 ns_min.disk = min_t(enum drbd_disk_state, ns.disk, ns_min.disk);
1723 ns_min.pdsk = min_t(enum drbd_disk_state, ns.pdsk, ns_min.pdsk);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001724 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001725 rcu_read_unlock();
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001726
Philipp Reisnerf132f552011-07-18 10:44:24 +02001727 if (number_of_volumes == 0) {
1728 ns_min = ns_max = (union drbd_state) { {
1729 .role = R_SECONDARY,
1730 .peer = R_UNKNOWN,
1731 .conn = val.conn,
1732 .disk = D_DISKLESS,
1733 .pdsk = D_UNKNOWN
1734 } };
1735 }
1736
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001737 ns_min.susp = ns_max.susp = connection->susp;
1738 ns_min.susp_nod = ns_max.susp_nod = connection->susp_nod;
1739 ns_min.susp_fen = ns_max.susp_fen = connection->susp_fen;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001740
1741 *pns_min = ns_min;
1742 *pns_max = ns_max;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001743}
1744
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001745static enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001746_conn_rq_cond(struct drbd_connection *connection, union drbd_state mask, union drbd_state val)
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001747{
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001748 enum drbd_state_rv rv;
1749
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001750 if (test_and_clear_bit(CONN_WD_ST_CHG_OKAY, &connection->flags))
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001751 return SS_CW_SUCCESS;
1752
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001753 if (test_and_clear_bit(CONN_WD_ST_CHG_FAIL, &connection->flags))
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001754 return SS_CW_FAILED_BY_PEER;
1755
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001756 rv = conn_is_valid_transition(connection, mask, val, 0);
1757 if (rv == SS_SUCCESS && connection->cstate == C_WF_REPORT_PARAMS)
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01001758 rv = SS_UNKNOWN_ERROR; /* continue waiting */
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001759
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001760 return rv;
1761}
1762
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001763enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001764_conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001765 enum chg_state_flags flags)
1766{
1767 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001768 struct after_conn_state_chg_work *acscw;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001769 enum drbd_conns oc = connection->cstate;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001770 union drbd_state ns_max, ns_min, os;
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001771 bool have_mutex = false;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001772
Philipp Reisner07fc9612012-08-28 11:07:56 +02001773 if (mask.conn) {
1774 rv = is_valid_conn_transition(oc, val.conn);
1775 if (rv < SS_SUCCESS)
1776 goto abort;
1777 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001778
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001779 rv = conn_is_valid_transition(connection, mask, val, flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001780 if (rv < SS_SUCCESS)
1781 goto abort;
1782
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001783 if (oc == C_WF_REPORT_PARAMS && val.conn == C_DISCONNECTING &&
1784 !(flags & (CS_LOCAL_ONLY | CS_HARD))) {
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001785
1786 /* This will be a cluster-wide state change.
1787 * Need to give up the spinlock, grab the mutex,
1788 * then send the state change request, ... */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001789 spin_unlock_irq(&connection->req_lock);
1790 mutex_lock(&connection->cstate_mutex);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001791 have_mutex = true;
1792
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001793 set_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
1794 if (conn_send_state_req(connection, mask, val)) {
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001795 /* sending failed. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001796 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001797 rv = SS_CW_FAILED_BY_PEER;
1798 /* need to re-aquire the spin lock, though */
1799 goto abort_unlocked;
1800 }
1801
1802 if (val.conn == C_DISCONNECTING)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001803 set_bit(DISCONNECT_SENT, &connection->flags);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001804
1805 /* ... and re-aquire the spinlock.
1806 * If _conn_rq_cond() returned >= SS_SUCCESS, we must call
1807 * conn_set_state() within the same spinlock. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001808 spin_lock_irq(&connection->req_lock);
1809 wait_event_lock_irq(connection->ping_wait,
1810 (rv = _conn_rq_cond(connection, mask, val)),
1811 connection->req_lock);
1812 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001813 if (rv < SS_SUCCESS)
1814 goto abort;
1815 }
1816
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001817 conn_old_common_state(connection, &os, &flags);
Philipp Reisner706cb242011-03-29 15:20:27 +02001818 flags |= CS_DC_SUSP;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001819 conn_set_state(connection, mask, val, &ns_min, &ns_max, flags);
1820 conn_pr_state_change(connection, os, ns_max, flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001821
1822 acscw = kmalloc(sizeof(*acscw), GFP_ATOMIC);
1823 if (acscw) {
Philipp Reisner435693e2011-03-25 15:11:30 +01001824 acscw->oc = os.conn;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001825 acscw->ns_min = ns_min;
Philipp Reisner5f082f92011-03-29 13:20:58 +02001826 acscw->ns_max = ns_max;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001827 acscw->flags = flags;
1828 acscw->w.cb = w_after_conn_state_ch;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001829 kref_get(&connection->kref);
1830 acscw->w.connection = connection;
1831 drbd_queue_work(&connection->sender_work, &acscw->w);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001832 } else {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001833 conn_err(connection, "Could not kmalloc an acscw\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001834 }
1835
Philipp Reisnera01842e2011-12-13 17:40:53 +01001836 abort:
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001837 if (have_mutex) {
1838 /* mutex_unlock() "... must not be used in interrupt context.",
1839 * so give up the spinlock, then re-aquire it */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001840 spin_unlock_irq(&connection->req_lock);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001841 abort_unlocked:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001842 mutex_unlock(&connection->cstate_mutex);
1843 spin_lock_irq(&connection->req_lock);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001844 }
1845 if (rv < SS_SUCCESS && flags & CS_VERBOSE) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001846 conn_err(connection, "State change failed: %s\n", drbd_set_st_err_str(rv));
1847 conn_err(connection, " mask = 0x%x val = 0x%x\n", mask.i, val.i);
1848 conn_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 +01001849 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001850 return rv;
1851}
1852
1853enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001854conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001855 enum chg_state_flags flags)
1856{
1857 enum drbd_state_rv rv;
1858
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001859 spin_lock_irq(&connection->req_lock);
1860 rv = _conn_request_state(connection, mask, val, flags);
1861 spin_unlock_irq(&connection->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001862
1863 return rv;
1864}