blob: 10c89314ff2b5dc930b009d8117e9690bce739fe [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 Gruenbacherc06ece62011-06-21 17:23:59 +020066 struct drbd_peer_device *peer_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 Gruenbacherc06ece62011-06-21 17:23:59 +020071 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
72 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020073 if (device->state.disk != D_DISKLESS ||
74 device->state.conn != C_STANDALONE ||
75 device->state.role != R_SECONDARY) {
Philipp Reisner695d08f2011-04-11 22:53:32 -070076 rv = false;
77 break;
78 }
Philipp Reisner0e29d162011-02-18 14:23:11 +010079 }
Philipp Reisner695d08f2011-04-11 22:53:32 -070080 rcu_read_unlock();
81
82 return rv;
Philipp Reisner0e29d162011-02-18 14:23:11 +010083}
84
Philipp Reisnercb703452011-03-24 11:03:07 +010085/* Unfortunately the states where not correctly ordered, when
86 they where defined. therefore can not use max_t() here. */
87static enum drbd_role max_role(enum drbd_role role1, enum drbd_role role2)
88{
89 if (role1 == R_PRIMARY || role2 == R_PRIMARY)
90 return R_PRIMARY;
91 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
92 return R_SECONDARY;
93 return R_UNKNOWN;
94}
95static enum drbd_role min_role(enum drbd_role role1, enum drbd_role role2)
96{
97 if (role1 == R_UNKNOWN || role2 == R_UNKNOWN)
98 return R_UNKNOWN;
99 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
100 return R_SECONDARY;
101 return R_PRIMARY;
102}
103
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200104enum drbd_role conn_highest_role(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100105{
106 enum drbd_role role = R_UNKNOWN;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200107 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100108 int vnr;
109
Philipp Reisner695d08f2011-04-11 22:53:32 -0700110 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200111 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
112 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200113 role = max_role(role, device->state.role);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200114 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700115 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100116
117 return role;
118}
119
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200120enum drbd_role conn_highest_peer(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100121{
122 enum drbd_role peer = R_UNKNOWN;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200123 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100124 int vnr;
125
Philipp Reisner695d08f2011-04-11 22:53:32 -0700126 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200127 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
128 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200129 peer = max_role(peer, device->state.peer);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200130 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700131 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100132
133 return peer;
134}
135
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200136enum drbd_disk_state conn_highest_disk(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100137{
138 enum drbd_disk_state ds = D_DISKLESS;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200139 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100140 int vnr;
141
Philipp Reisner695d08f2011-04-11 22:53:32 -0700142 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200143 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
144 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200145 ds = max_t(enum drbd_disk_state, ds, device->state.disk);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200146 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700147 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100148
149 return ds;
150}
151
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200152enum drbd_disk_state conn_lowest_disk(struct drbd_connection *connection)
Philipp Reisner46692652011-03-29 18:15:49 +0200153{
154 enum drbd_disk_state ds = D_MASK;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200155 struct drbd_peer_device *peer_device;
Philipp Reisner46692652011-03-29 18:15:49 +0200156 int vnr;
157
Philipp Reisner695d08f2011-04-11 22:53:32 -0700158 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200159 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
160 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200161 ds = min_t(enum drbd_disk_state, ds, device->state.disk);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200162 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700163 rcu_read_unlock();
Philipp Reisner46692652011-03-29 18:15:49 +0200164
165 return ds;
166}
167
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200168enum drbd_disk_state conn_highest_pdsk(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100169{
170 enum drbd_disk_state ds = D_DISKLESS;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200171 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100172 int vnr;
173
Philipp Reisner695d08f2011-04-11 22:53:32 -0700174 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200175 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
176 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200177 ds = max_t(enum drbd_disk_state, ds, device->state.pdsk);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200178 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700179 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100180
181 return ds;
182}
183
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200184enum drbd_conns conn_lowest_conn(struct drbd_connection *connection)
Philipp Reisner19f83c72011-03-29 14:21:03 +0200185{
186 enum drbd_conns conn = C_MASK;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200187 struct drbd_peer_device *peer_device;
Philipp Reisner19f83c72011-03-29 14:21:03 +0200188 int vnr;
189
Philipp Reisner695d08f2011-04-11 22:53:32 -0700190 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200191 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
192 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200193 conn = min_t(enum drbd_conns, conn, device->state.conn);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200194 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700195 rcu_read_unlock();
Philipp Reisner19f83c72011-03-29 14:21:03 +0200196
197 return conn;
198}
199
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200200static bool no_peer_wf_report_params(struct drbd_connection *connection)
Philipp Reisner79702012012-08-28 11:33:35 +0200201{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200202 struct drbd_peer_device *peer_device;
Philipp Reisner79702012012-08-28 11:33:35 +0200203 int vnr;
204 bool rv = true;
205
206 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200207 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
208 if (peer_device->device->state.conn == C_WF_REPORT_PARAMS) {
Philipp Reisner79702012012-08-28 11:33:35 +0200209 rv = false;
210 break;
211 }
212 rcu_read_unlock();
213
214 return rv;
215}
216
217
Philipp Reisnerb8907332011-01-27 14:07:51 +0100218/**
219 * cl_wide_st_chg() - true if the state change is a cluster wide one
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200220 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100221 * @os: old (current) state.
222 * @ns: new (wanted) state.
223 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200224static int cl_wide_st_chg(struct drbd_device *device,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100225 union drbd_state os, union drbd_state ns)
226{
227 return (os.conn >= C_CONNECTED && ns.conn >= C_CONNECTED &&
228 ((os.role != R_PRIMARY && ns.role == R_PRIMARY) ||
229 (os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
230 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S) ||
Lars Ellenberge8744f52012-03-26 15:57:00 +0200231 (os.disk != D_FAILED && ns.disk == D_FAILED))) ||
Philipp Reisnerb8907332011-01-27 14:07:51 +0100232 (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
Philipp Reisner369bea62011-07-06 23:04:44 +0200233 (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S) ||
234 (os.conn == C_CONNECTED && ns.conn == C_WF_REPORT_PARAMS);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100235}
236
Philipp Reisner56707f92011-02-16 14:57:50 +0100237static union drbd_state
238apply_mask_val(union drbd_state os, union drbd_state mask, union drbd_state val)
239{
240 union drbd_state ns;
241 ns.i = (os.i & ~mask.i) | val.i;
242 return ns;
243}
244
Philipp Reisnerb8907332011-01-27 14:07:51 +0100245enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200246drbd_change_state(struct drbd_device *device, enum chg_state_flags f,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100247 union drbd_state mask, union drbd_state val)
248{
249 unsigned long flags;
Philipp Reisner56707f92011-02-16 14:57:50 +0100250 union drbd_state ns;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100251 enum drbd_state_rv rv;
252
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200253 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200254 ns = apply_mask_val(drbd_read_state(device), mask, val);
255 rv = _drbd_set_state(device, ns, f, NULL);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200256 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100257
258 return rv;
259}
260
261/**
262 * drbd_force_state() - Impose a change which happens outside our control on our state
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200263 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100264 * @mask: mask of state bits to change.
265 * @val: value of new state bits.
266 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200267void drbd_force_state(struct drbd_device *device,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100268 union drbd_state mask, union drbd_state val)
269{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200270 drbd_change_state(device, CS_HARD, mask, val);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100271}
272
273static enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200274_req_st_cond(struct drbd_device *device, union drbd_state mask,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100275 union drbd_state val)
276{
277 union drbd_state os, ns;
278 unsigned long flags;
279 enum drbd_state_rv rv;
280
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200281 if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100282 return SS_CW_SUCCESS;
283
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200284 if (test_and_clear_bit(CL_ST_CHG_FAIL, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100285 return SS_CW_FAILED_BY_PEER;
286
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200287 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200288 os = drbd_read_state(device);
289 ns = sanitize_state(device, apply_mask_val(os, mask, val), NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100290 rv = is_valid_transition(os, ns);
Philipp Reisnera3025a22012-09-03 15:39:01 +0200291 if (rv >= SS_SUCCESS)
Philipp Reisner35095022011-02-09 16:29:33 +0100292 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100293
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200294 if (!cl_wide_st_chg(device, os, ns))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100295 rv = SS_CW_NO_NEED;
Philipp Reisner35095022011-02-09 16:29:33 +0100296 if (rv == SS_UNKNOWN_ERROR) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200297 rv = is_valid_state(device, ns);
Philipp Reisnera3025a22012-09-03 15:39:01 +0200298 if (rv >= SS_SUCCESS) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200299 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
Philipp Reisnera3025a22012-09-03 15:39:01 +0200300 if (rv >= SS_SUCCESS)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100301 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
302 }
303 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200304 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100305
306 return rv;
307}
308
309/**
310 * drbd_req_state() - Perform an eventually cluster wide state change
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200311 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100312 * @mask: mask of state bits to change.
313 * @val: value of new state bits.
314 * @f: flags
315 *
316 * Should not be called directly, use drbd_request_state() or
317 * _drbd_request_state().
318 */
319static enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200320drbd_req_state(struct drbd_device *device, union drbd_state mask,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100321 union drbd_state val, enum chg_state_flags f)
322{
323 struct completion done;
324 unsigned long flags;
325 union drbd_state os, ns;
326 enum drbd_state_rv rv;
327
328 init_completion(&done);
329
330 if (f & CS_SERIALIZE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200331 mutex_lock(device->state_mutex);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100332
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200333 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200334 os = drbd_read_state(device);
335 ns = sanitize_state(device, apply_mask_val(os, mask, val), NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100336 rv = is_valid_transition(os, ns);
Lars Ellenberg3c5e5f62011-03-15 16:04:09 +0100337 if (rv < SS_SUCCESS) {
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200338 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisner35095022011-02-09 16:29:33 +0100339 goto abort;
Lars Ellenberg3c5e5f62011-03-15 16:04:09 +0100340 }
Philipp Reisnerb8907332011-01-27 14:07:51 +0100341
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200342 if (cl_wide_st_chg(device, os, ns)) {
343 rv = is_valid_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100344 if (rv == SS_SUCCESS)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200345 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200346 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100347
348 if (rv < SS_SUCCESS) {
349 if (f & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200350 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100351 goto abort;
352 }
353
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200354 if (drbd_send_state_req(device, mask, val)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100355 rv = SS_CW_FAILED_BY_PEER;
356 if (f & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200357 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100358 goto abort;
359 }
360
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200361 wait_event(device->state_wait,
362 (rv = _req_st_cond(device, mask, val)));
Philipp Reisnerb8907332011-01-27 14:07:51 +0100363
364 if (rv < SS_SUCCESS) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100365 if (f & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200366 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100367 goto abort;
368 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200369 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200370 ns = apply_mask_val(drbd_read_state(device), mask, val);
371 rv = _drbd_set_state(device, ns, f, &done);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100372 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200373 rv = _drbd_set_state(device, ns, f, &done);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100374 }
375
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200376 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100377
378 if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200379 D_ASSERT(device, current != first_peer_device(device)->connection->worker.task);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100380 wait_for_completion(&done);
381 }
382
383abort:
384 if (f & CS_SERIALIZE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200385 mutex_unlock(device->state_mutex);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100386
387 return rv;
388}
389
390/**
391 * _drbd_request_state() - Request a state change (with flags)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200392 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100393 * @mask: mask of state bits to change.
394 * @val: value of new state bits.
395 * @f: flags
396 *
397 * Cousin of drbd_request_state(), useful with the CS_WAIT_COMPLETE
398 * flag, or when logging of failed state change requests is not desired.
399 */
400enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200401_drbd_request_state(struct drbd_device *device, union drbd_state mask,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100402 union drbd_state val, enum chg_state_flags f)
403{
404 enum drbd_state_rv rv;
405
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200406 wait_event(device->state_wait,
407 (rv = drbd_req_state(device, mask, val, f)) != SS_IN_TRANSIENT_STATE);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100408
409 return rv;
410}
411
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200412static void print_st(struct drbd_device *device, char *name, union drbd_state ns)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100413{
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200414 drbd_err(device, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c%c%c }\n",
Philipp Reisnerb8907332011-01-27 14:07:51 +0100415 name,
416 drbd_conn_str(ns.conn),
417 drbd_role_str(ns.role),
418 drbd_role_str(ns.peer),
419 drbd_disk_str(ns.disk),
420 drbd_disk_str(ns.pdsk),
421 is_susp(ns) ? 's' : 'r',
422 ns.aftr_isp ? 'a' : '-',
423 ns.peer_isp ? 'p' : '-',
424 ns.user_isp ? 'u' : '-',
425 ns.susp_fen ? 'F' : '-',
426 ns.susp_nod ? 'N' : '-'
427 );
428}
429
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200430void print_st_err(struct drbd_device *device, union drbd_state os,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100431 union drbd_state ns, enum drbd_state_rv err)
432{
433 if (err == SS_IN_TRANSIENT_STATE)
434 return;
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200435 drbd_err(device, "State change failed: %s\n", drbd_set_st_err_str(err));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200436 print_st(device, " state", os);
437 print_st(device, "wanted", ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100438}
439
Philipp Reisner435693e2011-03-25 15:11:30 +0100440static long print_state_change(char *pb, union drbd_state os, union drbd_state ns,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100441 enum chg_state_flags flags)
442{
Philipp Reisner435693e2011-03-25 15:11:30 +0100443 char *pbp;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100444 pbp = pb;
445 *pbp = 0;
Philipp Reisner706cb242011-03-29 15:20:27 +0200446
Philipp Reisner435693e2011-03-25 15:11:30 +0100447 if (ns.role != os.role && flags & CS_DC_ROLE)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100448 pbp += sprintf(pbp, "role( %s -> %s ) ",
449 drbd_role_str(os.role),
450 drbd_role_str(ns.role));
Philipp Reisner435693e2011-03-25 15:11:30 +0100451 if (ns.peer != os.peer && flags & CS_DC_PEER)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100452 pbp += sprintf(pbp, "peer( %s -> %s ) ",
453 drbd_role_str(os.peer),
454 drbd_role_str(ns.peer));
Philipp Reisner435693e2011-03-25 15:11:30 +0100455 if (ns.conn != os.conn && flags & CS_DC_CONN)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100456 pbp += sprintf(pbp, "conn( %s -> %s ) ",
457 drbd_conn_str(os.conn),
458 drbd_conn_str(ns.conn));
Philipp Reisner435693e2011-03-25 15:11:30 +0100459 if (ns.disk != os.disk && flags & CS_DC_DISK)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100460 pbp += sprintf(pbp, "disk( %s -> %s ) ",
461 drbd_disk_str(os.disk),
462 drbd_disk_str(ns.disk));
Philipp Reisner435693e2011-03-25 15:11:30 +0100463 if (ns.pdsk != os.pdsk && flags & CS_DC_PDSK)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100464 pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
465 drbd_disk_str(os.pdsk),
466 drbd_disk_str(ns.pdsk));
Philipp Reisner706cb242011-03-29 15:20:27 +0200467
468 return pbp - pb;
469}
470
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200471static void drbd_pr_state_change(struct drbd_device *device, union drbd_state os, union drbd_state ns,
Philipp Reisner706cb242011-03-29 15:20:27 +0200472 enum chg_state_flags flags)
473{
474 char pb[300];
475 char *pbp = pb;
476
477 pbp += print_state_change(pbp, os, ns, flags ^ CS_DC_MASK);
478
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100479 if (ns.aftr_isp != os.aftr_isp)
480 pbp += sprintf(pbp, "aftr_isp( %d -> %d ) ",
481 os.aftr_isp,
482 ns.aftr_isp);
483 if (ns.peer_isp != os.peer_isp)
484 pbp += sprintf(pbp, "peer_isp( %d -> %d ) ",
485 os.peer_isp,
486 ns.peer_isp);
487 if (ns.user_isp != os.user_isp)
488 pbp += sprintf(pbp, "user_isp( %d -> %d ) ",
489 os.user_isp,
490 ns.user_isp);
Philipp Reisner435693e2011-03-25 15:11:30 +0100491
Philipp Reisner706cb242011-03-29 15:20:27 +0200492 if (pbp != pb)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200493 drbd_info(device, "%s\n", pb);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100494}
Philipp Reisnerb8907332011-01-27 14:07:51 +0100495
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200496static void conn_pr_state_change(struct drbd_connection *connection, union drbd_state os, union drbd_state ns,
Philipp Reisner435693e2011-03-25 15:11:30 +0100497 enum chg_state_flags flags)
498{
499 char pb[300];
Philipp Reisner706cb242011-03-29 15:20:27 +0200500 char *pbp = pb;
Philipp Reisner435693e2011-03-25 15:11:30 +0100501
Philipp Reisner706cb242011-03-29 15:20:27 +0200502 pbp += print_state_change(pbp, os, ns, flags);
503
504 if (is_susp(ns) != is_susp(os) && flags & CS_DC_SUSP)
505 pbp += sprintf(pbp, "susp( %d -> %d ) ",
506 is_susp(os),
507 is_susp(ns));
508
509 if (pbp != pb)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200510 drbd_info(connection, "%s\n", pb);
Philipp Reisner435693e2011-03-25 15:11:30 +0100511}
512
513
Philipp Reisnerb8907332011-01-27 14:07:51 +0100514/**
515 * is_valid_state() - Returns an SS_ error code if ns is not valid
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200516 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100517 * @ns: State to consider.
518 */
519static enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200520is_valid_state(struct drbd_device *device, union drbd_state ns)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100521{
522 /* See drbd_state_sw_errors in drbd_strings.c */
523
524 enum drbd_fencing_p fp;
525 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200526 struct net_conf *nc;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100527
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200528 rcu_read_lock();
Philipp Reisnerb8907332011-01-27 14:07:51 +0100529 fp = FP_DONT_CARE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200530 if (get_ldev(device)) {
531 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
532 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100533 }
534
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200535 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200536 if (nc) {
537 if (!nc->two_primaries && ns.role == R_PRIMARY) {
Philipp Reisner047e95e22011-03-16 14:43:36 +0100538 if (ns.peer == R_PRIMARY)
539 rv = SS_TWO_PRIMARIES;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200540 else if (conn_highest_peer(first_peer_device(device)->connection) == R_PRIMARY)
Philipp Reisner047e95e22011-03-16 14:43:36 +0100541 rv = SS_O_VOL_PEER_PRI;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200542 }
Philipp Reisnerb8907332011-01-27 14:07:51 +0100543 }
544
545 if (rv <= 0)
546 /* already found a reason to abort */;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200547 else if (ns.role == R_SECONDARY && device->open_cnt)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100548 rv = SS_DEVICE_IN_USE;
549
550 else if (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.disk < D_UP_TO_DATE)
551 rv = SS_NO_UP_TO_DATE_DISK;
552
553 else if (fp >= FP_RESOURCE &&
554 ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
555 rv = SS_PRIMARY_NOP;
556
557 else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
558 rv = SS_NO_UP_TO_DATE_DISK;
559
560 else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
561 rv = SS_NO_LOCAL_DISK;
562
563 else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
564 rv = SS_NO_REMOTE_DISK;
565
566 else if (ns.conn > C_CONNECTED && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
567 rv = SS_NO_UP_TO_DATE_DISK;
568
569 else if ((ns.conn == C_CONNECTED ||
570 ns.conn == C_WF_BITMAP_S ||
571 ns.conn == C_SYNC_SOURCE ||
572 ns.conn == C_PAUSED_SYNC_S) &&
573 ns.disk == D_OUTDATED)
574 rv = SS_CONNECTED_OUTDATES;
575
576 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
Philipp Reisner44ed1672011-04-19 17:10:19 +0200577 (nc->verify_alg[0] == 0))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100578 rv = SS_NO_VERIFY_ALG;
579
580 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200581 first_peer_device(device)->connection->agreed_pro_version < 88)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100582 rv = SS_NOT_SUPPORTED;
583
Philipp Reisner5c4f13d2013-03-27 14:08:37 +0100584 else if (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
585 rv = SS_NO_UP_TO_DATE_DISK;
586
587 else if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
588 ns.pdsk == D_UNKNOWN)
589 rv = SS_NEED_CONNECTION;
590
Philipp Reisnerb8907332011-01-27 14:07:51 +0100591 else if (ns.conn >= C_CONNECTED && ns.pdsk == D_UNKNOWN)
592 rv = SS_CONNECTED_OUTDATES;
593
Philipp Reisner44ed1672011-04-19 17:10:19 +0200594 rcu_read_unlock();
595
Philipp Reisnerb8907332011-01-27 14:07:51 +0100596 return rv;
597}
598
599/**
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100600 * is_valid_soft_transition() - Returns an SS_ error code if the state transition is not possible
Philipp Reisner35095022011-02-09 16:29:33 +0100601 * This function limits state transitions that may be declined by DRBD. I.e.
602 * user requests (aka soft transitions).
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200603 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100604 * @ns: new state.
605 * @os: old state.
606 */
607static enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200608is_valid_soft_transition(union drbd_state os, union drbd_state ns, struct drbd_connection *connection)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100609{
610 enum drbd_state_rv rv = SS_SUCCESS;
611
612 if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
613 os.conn > C_CONNECTED)
614 rv = SS_RESYNC_RUNNING;
615
616 if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
617 rv = SS_ALREADY_STANDALONE;
618
619 if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
620 rv = SS_IS_DISKLESS;
621
622 if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
623 rv = SS_NO_NET_CONFIG;
624
625 if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
626 rv = SS_LOWER_THAN_OUTDATED;
627
628 if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
629 rv = SS_IN_TRANSIENT_STATE;
630
Philipp Reisner2325eb62011-03-15 16:56:18 +0100631 /* if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
632 rv = SS_IN_TRANSIENT_STATE; */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100633
Philipp Reisnera1096a62012-04-06 12:07:34 +0200634 /* While establishing a connection only allow cstate to change.
635 Delay/refuse role changes, detach attach etc... */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200636 if (test_bit(STATE_SENT, &connection->flags) &&
Philipp Reisnera1096a62012-04-06 12:07:34 +0200637 !(os.conn == C_WF_REPORT_PARAMS ||
638 (ns.conn == C_WF_REPORT_PARAMS && os.conn == C_WF_CONNECTION)))
639 rv = SS_IN_TRANSIENT_STATE;
640
Philipp Reisnerb8907332011-01-27 14:07:51 +0100641 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
642 rv = SS_NEED_CONNECTION;
643
644 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
645 ns.conn != os.conn && os.conn > C_CONNECTED)
646 rv = SS_RESYNC_RUNNING;
647
648 if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
649 os.conn < C_CONNECTED)
650 rv = SS_NEED_CONNECTION;
651
652 if ((ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)
653 && os.conn < C_WF_REPORT_PARAMS)
654 rv = SS_NEED_CONNECTION; /* No NetworkFailure -> SyncTarget etc... */
655
Philipp Reisner2bd5ed52013-03-27 14:08:40 +0100656 if (ns.conn == C_DISCONNECTING && ns.pdsk == D_OUTDATED &&
657 os.conn < C_CONNECTED && os.pdsk > D_OUTDATED)
658 rv = SS_OUTDATE_WO_CONN;
659
Philipp Reisnerb8907332011-01-27 14:07:51 +0100660 return rv;
661}
662
Philipp Reisnerfda74112011-02-10 10:38:06 +0100663static enum drbd_state_rv
664is_valid_conn_transition(enum drbd_conns oc, enum drbd_conns nc)
665{
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200666 /* no change -> nothing to do, at least for the connection part */
667 if (oc == nc)
668 return SS_NOTHING_TO_DO;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100669
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200670 /* disconnect of an unconfigured connection does not make sense */
671 if (oc == C_STANDALONE && nc == C_DISCONNECTING)
672 return SS_ALREADY_STANDALONE;
673
674 /* from C_STANDALONE, we start with C_UNCONNECTED */
675 if (oc == C_STANDALONE && nc != C_UNCONNECTED)
676 return SS_NEED_CONNECTION;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100677
Philipp Reisner25b0d6c2012-02-14 12:12:35 +0100678 /* When establishing a connection we need to go through WF_REPORT_PARAMS!
679 Necessary to do the right thing upon invalidate-remote on a disconnected resource */
680 if (oc < C_WF_REPORT_PARAMS && nc >= C_CONNECTED)
681 return SS_NEED_CONNECTION;
682
Philipp Reisnerfda74112011-02-10 10:38:06 +0100683 /* After a network error only C_UNCONNECTED or C_DISCONNECTING may follow. */
684 if (oc >= C_TIMEOUT && oc <= C_TEAR_DOWN && nc != C_UNCONNECTED && nc != C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200685 return SS_IN_TRANSIENT_STATE;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100686
687 /* After C_DISCONNECTING only C_STANDALONE may follow */
688 if (oc == C_DISCONNECTING && nc != C_STANDALONE)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200689 return SS_IN_TRANSIENT_STATE;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100690
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200691 return SS_SUCCESS;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100692}
693
694
Philipp Reisnerb8907332011-01-27 14:07:51 +0100695/**
Philipp Reisner35095022011-02-09 16:29:33 +0100696 * is_valid_transition() - Returns an SS_ error code if the state transition is not possible
697 * This limits hard state transitions. Hard state transitions are facts there are
698 * imposed on DRBD by the environment. E.g. disk broke or network broke down.
699 * But those hard state transitions are still not allowed to do everything.
700 * @ns: new state.
701 * @os: old state.
702 */
703static enum drbd_state_rv
704is_valid_transition(union drbd_state os, union drbd_state ns)
705{
Philipp Reisnerfda74112011-02-10 10:38:06 +0100706 enum drbd_state_rv rv;
Philipp Reisner35095022011-02-09 16:29:33 +0100707
Philipp Reisnerfda74112011-02-10 10:38:06 +0100708 rv = is_valid_conn_transition(os.conn, ns.conn);
Philipp Reisner35095022011-02-09 16:29:33 +0100709
710 /* we cannot fail (again) if we already detached */
711 if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
712 rv = SS_IS_DISKLESS;
713
714 return rv;
715}
716
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200717static void print_sanitize_warnings(struct drbd_device *device, enum sanitize_state_warnings warn)
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200718{
719 static const char *msg_table[] = {
720 [NO_WARNING] = "",
721 [ABORTED_ONLINE_VERIFY] = "Online-verify aborted.",
722 [ABORTED_RESYNC] = "Resync aborted.",
723 [CONNECTION_LOST_NEGOTIATING] = "Connection lost while negotiating, no data!",
724 [IMPLICITLY_UPGRADED_DISK] = "Implicitly upgraded disk",
725 [IMPLICITLY_UPGRADED_PDSK] = "Implicitly upgraded pdsk",
726 };
727
728 if (warn != NO_WARNING)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200729 drbd_warn(device, "%s\n", msg_table[warn]);
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200730}
731
Philipp Reisner35095022011-02-09 16:29:33 +0100732/**
Philipp Reisnerb8907332011-01-27 14:07:51 +0100733 * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200734 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100735 * @os: old state.
736 * @ns: new state.
737 * @warn_sync_abort:
738 *
739 * When we loose connection, we have to set the state of the peers disk (pdsk)
740 * to D_UNKNOWN. This rule and many more along those lines are in this function.
741 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200742static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state ns,
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200743 enum sanitize_state_warnings *warn)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100744{
745 enum drbd_fencing_p fp;
746 enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
747
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200748 if (warn)
749 *warn = NO_WARNING;
750
Philipp Reisnerb8907332011-01-27 14:07:51 +0100751 fp = FP_DONT_CARE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200752 if (get_ldev(device)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200753 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200754 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200755 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200756 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100757 }
758
Philipp Reisner35095022011-02-09 16:29:33 +0100759 /* Implications from connection to peer and peer_isp */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100760 if (ns.conn < C_CONNECTED) {
761 ns.peer_isp = 0;
762 ns.peer = R_UNKNOWN;
763 if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
764 ns.pdsk = D_UNKNOWN;
765 }
766
767 /* Clear the aftr_isp when becoming unconfigured */
768 if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
769 ns.aftr_isp = 0;
770
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100771 /* An implication of the disk states onto the connection state */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100772 /* Abort resync if a disk fails/detaches */
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100773 if (ns.conn > C_CONNECTED && (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200774 if (warn)
775 *warn = ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T ?
776 ABORTED_ONLINE_VERIFY : ABORTED_RESYNC;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100777 ns.conn = C_CONNECTED;
778 }
779
780 /* Connection breaks down before we finished "Negotiating" */
781 if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200782 get_ldev_if_state(device, D_NEGOTIATING)) {
783 if (device->ed_uuid == device->ldev->md.uuid[UI_CURRENT]) {
784 ns.disk = device->new_state_tmp.disk;
785 ns.pdsk = device->new_state_tmp.pdsk;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100786 } else {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200787 if (warn)
788 *warn = CONNECTION_LOST_NEGOTIATING;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100789 ns.disk = D_DISKLESS;
790 ns.pdsk = D_UNKNOWN;
791 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200792 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100793 }
794
795 /* D_CONSISTENT and D_OUTDATED vanish when we get connected */
796 if (ns.conn >= C_CONNECTED && ns.conn < C_AHEAD) {
797 if (ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED)
798 ns.disk = D_UP_TO_DATE;
799 if (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)
800 ns.pdsk = D_UP_TO_DATE;
801 }
802
803 /* Implications of the connection stat on the disk states */
804 disk_min = D_DISKLESS;
805 disk_max = D_UP_TO_DATE;
806 pdsk_min = D_INCONSISTENT;
807 pdsk_max = D_UNKNOWN;
808 switch ((enum drbd_conns)ns.conn) {
809 case C_WF_BITMAP_T:
810 case C_PAUSED_SYNC_T:
811 case C_STARTING_SYNC_T:
812 case C_WF_SYNC_UUID:
813 case C_BEHIND:
814 disk_min = D_INCONSISTENT;
815 disk_max = D_OUTDATED;
816 pdsk_min = D_UP_TO_DATE;
817 pdsk_max = D_UP_TO_DATE;
818 break;
819 case C_VERIFY_S:
820 case C_VERIFY_T:
821 disk_min = D_UP_TO_DATE;
822 disk_max = D_UP_TO_DATE;
823 pdsk_min = D_UP_TO_DATE;
824 pdsk_max = D_UP_TO_DATE;
825 break;
826 case C_CONNECTED:
827 disk_min = D_DISKLESS;
828 disk_max = D_UP_TO_DATE;
829 pdsk_min = D_DISKLESS;
830 pdsk_max = D_UP_TO_DATE;
831 break;
832 case C_WF_BITMAP_S:
833 case C_PAUSED_SYNC_S:
834 case C_STARTING_SYNC_S:
835 case C_AHEAD:
836 disk_min = D_UP_TO_DATE;
837 disk_max = D_UP_TO_DATE;
838 pdsk_min = D_INCONSISTENT;
839 pdsk_max = D_CONSISTENT; /* D_OUTDATED would be nice. But explicit outdate necessary*/
840 break;
841 case C_SYNC_TARGET:
842 disk_min = D_INCONSISTENT;
843 disk_max = D_INCONSISTENT;
844 pdsk_min = D_UP_TO_DATE;
845 pdsk_max = D_UP_TO_DATE;
846 break;
847 case C_SYNC_SOURCE:
848 disk_min = D_UP_TO_DATE;
849 disk_max = D_UP_TO_DATE;
850 pdsk_min = D_INCONSISTENT;
851 pdsk_max = D_INCONSISTENT;
852 break;
853 case C_STANDALONE:
854 case C_DISCONNECTING:
855 case C_UNCONNECTED:
856 case C_TIMEOUT:
857 case C_BROKEN_PIPE:
858 case C_NETWORK_FAILURE:
859 case C_PROTOCOL_ERROR:
860 case C_TEAR_DOWN:
861 case C_WF_CONNECTION:
862 case C_WF_REPORT_PARAMS:
863 case C_MASK:
864 break;
865 }
866 if (ns.disk > disk_max)
867 ns.disk = disk_max;
868
869 if (ns.disk < disk_min) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200870 if (warn)
871 *warn = IMPLICITLY_UPGRADED_DISK;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100872 ns.disk = disk_min;
873 }
874 if (ns.pdsk > pdsk_max)
875 ns.pdsk = pdsk_max;
876
877 if (ns.pdsk < pdsk_min) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200878 if (warn)
879 *warn = IMPLICITLY_UPGRADED_PDSK;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100880 ns.pdsk = pdsk_min;
881 }
882
883 if (fp == FP_STONITH &&
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100884 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100885 ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
886
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +0200887 if (device->resource->res_opts.on_no_data == OND_SUSPEND_IO &&
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100888 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100889 ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
890
891 if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
892 if (ns.conn == C_SYNC_SOURCE)
893 ns.conn = C_PAUSED_SYNC_S;
894 if (ns.conn == C_SYNC_TARGET)
895 ns.conn = C_PAUSED_SYNC_T;
896 } else {
897 if (ns.conn == C_PAUSED_SYNC_S)
898 ns.conn = C_SYNC_SOURCE;
899 if (ns.conn == C_PAUSED_SYNC_T)
900 ns.conn = C_SYNC_TARGET;
901 }
902
903 return ns;
904}
905
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200906void drbd_resume_al(struct drbd_device *device)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100907{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200908 if (test_and_clear_bit(AL_SUSPENDED, &device->flags))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200909 drbd_info(device, "Resumed AL updates\n");
Philipp Reisnerb8907332011-01-27 14:07:51 +0100910}
911
912/* helper for __drbd_set_state */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200913static void set_ov_position(struct drbd_device *device, enum drbd_conns cs)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100914{
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200915 if (first_peer_device(device)->connection->agreed_pro_version < 90)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200916 device->ov_start_sector = 0;
917 device->rs_total = drbd_bm_bits(device);
918 device->ov_position = 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100919 if (cs == C_VERIFY_T) {
920 /* starting online verify from an arbitrary position
921 * does not fit well into the existing protocol.
922 * on C_VERIFY_T, we initialize ov_left and friends
923 * implicitly in receive_DataRequest once the
924 * first P_OV_REQUEST is received */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200925 device->ov_start_sector = ~(sector_t)0;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100926 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200927 unsigned long bit = BM_SECT_TO_BIT(device->ov_start_sector);
928 if (bit >= device->rs_total) {
929 device->ov_start_sector =
930 BM_BIT_TO_SECT(device->rs_total - 1);
931 device->rs_total = 1;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100932 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200933 device->rs_total -= bit;
934 device->ov_position = device->ov_start_sector;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100935 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200936 device->ov_left = device->rs_total;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100937}
938
939/**
940 * __drbd_set_state() - Set a new DRBD state
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200941 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100942 * @ns: new state.
943 * @flags: Flags
944 * @done: Optional completion, that will get completed after the after_state_ch() finished
945 *
946 * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
947 */
948enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200949__drbd_set_state(struct drbd_device *device, union drbd_state ns,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100950 enum chg_state_flags flags, struct completion *done)
951{
952 union drbd_state os;
953 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200954 enum sanitize_state_warnings ssw;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100955 struct after_state_chg_work *ascw;
Lars Ellenberg2681f7f2013-01-21 15:43:41 +0100956 bool did_remote, should_do_remote;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100957
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200958 os = drbd_read_state(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100959
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200960 ns = sanitize_state(device, ns, &ssw);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100961 if (ns.i == os.i)
962 return SS_NOTHING_TO_DO;
963
Philipp Reisner35095022011-02-09 16:29:33 +0100964 rv = is_valid_transition(os, ns);
965 if (rv < SS_SUCCESS)
966 return rv;
967
Philipp Reisnerb8907332011-01-27 14:07:51 +0100968 if (!(flags & CS_HARD)) {
969 /* pre-state-change checks ; only look at ns */
970 /* See drbd_state_sw_errors in drbd_strings.c */
971
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200972 rv = is_valid_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100973 if (rv < SS_SUCCESS) {
974 /* If the old state was illegal as well, then let
975 this happen...*/
976
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200977 if (is_valid_state(device, os) == rv)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200978 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100979 } else
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200980 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100981 }
982
983 if (rv < SS_SUCCESS) {
984 if (flags & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200985 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100986 return rv;
987 }
988
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200989 print_sanitize_warnings(device, ssw);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100990
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200991 drbd_pr_state_change(device, os, ns, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100992
Philipp Reisner706cb242011-03-29 15:20:27 +0200993 /* Display changes to the susp* flags that where caused by the call to
994 sanitize_state(). Only display it here if we where not called from
995 _conn_request_state() */
996 if (!(flags & CS_DC_SUSP))
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200997 conn_pr_state_change(first_peer_device(device)->connection, os, ns,
998 (flags & ~CS_DC_MASK) | CS_DC_SUSP);
Philipp Reisner706cb242011-03-29 15:20:27 +0200999
Philipp Reisnerb8907332011-01-27 14:07:51 +01001000 /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
1001 * on the ldev here, to be sure the transition -> D_DISKLESS resp.
1002 * drbd_ldev_destroy() won't happen before our corresponding
1003 * after_state_ch works run, where we put_ldev again. */
1004 if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
1005 (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001006 atomic_inc(&device->local_cnt);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001007
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001008 did_remote = drbd_should_do_remote(device->state);
1009 device->state.i = ns.i;
1010 should_do_remote = drbd_should_do_remote(device->state);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001011 first_peer_device(device)->connection->susp = ns.susp;
1012 first_peer_device(device)->connection->susp_nod = ns.susp_nod;
1013 first_peer_device(device)->connection->susp_fen = ns.susp_fen;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001014
Lars Ellenberg2681f7f2013-01-21 15:43:41 +01001015 /* put replicated vs not-replicated requests in seperate epochs */
1016 if (did_remote != should_do_remote)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001017 start_new_tl_epoch(first_peer_device(device)->connection);
Lars Ellenberg2681f7f2013-01-21 15:43:41 +01001018
Philipp Reisnerb8907332011-01-27 14:07:51 +01001019 if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001020 drbd_print_uuids(device, "attached to UUIDs");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001021
Philipp Reisner79702012012-08-28 11:33:35 +02001022 /* Wake up role changes, that were delayed because of connection establishing */
1023 if (os.conn == C_WF_REPORT_PARAMS && ns.conn != C_WF_REPORT_PARAMS &&
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001024 no_peer_wf_report_params(first_peer_device(device)->connection))
1025 clear_bit(STATE_SENT, &first_peer_device(device)->connection->flags);
Philipp Reisner79702012012-08-28 11:33:35 +02001026
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001027 wake_up(&device->misc_wait);
1028 wake_up(&device->state_wait);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001029 wake_up(&first_peer_device(device)->connection->ping_wait);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001030
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001031 /* Aborted verify run, or we reached the stop sector.
1032 * Log the last position, unless end-of-device. */
Philipp Reisnerb8907332011-01-27 14:07:51 +01001033 if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001034 ns.conn <= C_CONNECTED) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001035 device->ov_start_sector =
1036 BM_BIT_TO_SECT(drbd_bm_bits(device) - device->ov_left);
1037 if (device->ov_left)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001038 drbd_info(device, "Online Verify reached sector %llu\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001039 (unsigned long long)device->ov_start_sector);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001040 }
1041
1042 if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
1043 (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001044 drbd_info(device, "Syncer continues.\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001045 device->rs_paused += (long)jiffies
1046 -(long)device->rs_mark_time[device->rs_last_mark];
Philipp Reisnerb8907332011-01-27 14:07:51 +01001047 if (ns.conn == C_SYNC_TARGET)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001048 mod_timer(&device->resync_timer, jiffies);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001049 }
1050
1051 if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
1052 (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001053 drbd_info(device, "Resync suspended\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001054 device->rs_mark_time[device->rs_last_mark] = jiffies;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001055 }
1056
1057 if (os.conn == C_CONNECTED &&
1058 (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
1059 unsigned long now = jiffies;
1060 int i;
1061
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001062 set_ov_position(device, ns.conn);
1063 device->rs_start = now;
1064 device->rs_last_events = 0;
1065 device->rs_last_sect_ev = 0;
1066 device->ov_last_oos_size = 0;
1067 device->ov_last_oos_start = 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001068
1069 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001070 device->rs_mark_left[i] = device->ov_left;
1071 device->rs_mark_time[i] = now;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001072 }
1073
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001074 drbd_rs_controller_reset(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001075
1076 if (ns.conn == C_VERIFY_S) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001077 drbd_info(device, "Starting Online Verify from sector %llu\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001078 (unsigned long long)device->ov_position);
1079 mod_timer(&device->resync_timer, jiffies);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001080 }
1081 }
1082
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001083 if (get_ldev(device)) {
1084 u32 mdf = device->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
Philipp Reisnerb8907332011-01-27 14:07:51 +01001085 MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
1086 MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
1087
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02001088 mdf &= ~MDF_AL_CLEAN;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001089 if (test_bit(CRASHED_PRIMARY, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +01001090 mdf |= MDF_CRASHED_PRIMARY;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001091 if (device->state.role == R_PRIMARY ||
1092 (device->state.pdsk < D_INCONSISTENT && device->state.peer == R_PRIMARY))
Philipp Reisnerb8907332011-01-27 14:07:51 +01001093 mdf |= MDF_PRIMARY_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001094 if (device->state.conn > C_WF_REPORT_PARAMS)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001095 mdf |= MDF_CONNECTED_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001096 if (device->state.disk > D_INCONSISTENT)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001097 mdf |= MDF_CONSISTENT;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001098 if (device->state.disk > D_OUTDATED)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001099 mdf |= MDF_WAS_UP_TO_DATE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001100 if (device->state.pdsk <= D_OUTDATED && device->state.pdsk >= D_INCONSISTENT)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001101 mdf |= MDF_PEER_OUT_DATED;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001102 if (mdf != device->ldev->md.flags) {
1103 device->ldev->md.flags = mdf;
1104 drbd_md_mark_dirty(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001105 }
1106 if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001107 drbd_set_ed_uuid(device, device->ldev->md.uuid[UI_CURRENT]);
1108 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001109 }
1110
1111 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
1112 if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
1113 os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001114 set_bit(CONSIDER_RESYNC, &device->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001115
1116 /* Receiver should clean up itself */
1117 if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001118 drbd_thread_stop_nowait(&first_peer_device(device)->connection->receiver);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001119
1120 /* Now the receiver finished cleaning up itself, it should die */
1121 if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001122 drbd_thread_stop_nowait(&first_peer_device(device)->connection->receiver);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001123
1124 /* Upon network failure, we need to restart the receiver. */
Philipp Reisner823bd832012-11-08 15:04:36 +01001125 if (os.conn > C_WF_CONNECTION &&
Philipp Reisnerb8907332011-01-27 14:07:51 +01001126 ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001127 drbd_thread_restart_nowait(&first_peer_device(device)->connection->receiver);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001128
1129 /* Resume AL writing if we get a connection */
Philipp Reisner28e448b2013-06-25 16:50:06 +02001130 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001131 drbd_resume_al(device);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001132 first_peer_device(device)->connection->connect_cnt++;
Philipp Reisner28e448b2013-06-25 16:50:06 +02001133 }
Philipp Reisnerb8907332011-01-27 14:07:51 +01001134
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001135 /* remember last attach time so request_timer_fn() won't
1136 * kill newly established sessions while we are still trying to thaw
1137 * previously frozen IO */
1138 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
1139 ns.disk > D_NEGOTIATING)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001140 device->last_reattach_jif = jiffies;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001141
Philipp Reisnerb8907332011-01-27 14:07:51 +01001142 ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
1143 if (ascw) {
1144 ascw->os = os;
1145 ascw->ns = ns;
1146 ascw->flags = flags;
1147 ascw->w.cb = w_after_state_ch;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001148 ascw->w.device = device;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001149 ascw->done = done;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001150 drbd_queue_work(&first_peer_device(device)->connection->sender_work, &ascw->w);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001151 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001152 drbd_err(device, "Could not kmalloc an ascw\n");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001153 }
1154
1155 return rv;
1156}
1157
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001158static int w_after_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001159{
1160 struct after_state_chg_work *ascw =
1161 container_of(w, struct after_state_chg_work, w);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001162 struct drbd_device *device = w->device;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001163
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001164 after_state_ch(device, ascw->os, ascw->ns, ascw->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001165 if (ascw->flags & CS_WAIT_COMPLETE) {
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001166 D_ASSERT(device, ascw->done != NULL);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001167 complete(ascw->done);
1168 }
1169 kfree(ascw);
1170
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001171 return 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001172}
1173
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001174static void abw_start_sync(struct drbd_device *device, int rv)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001175{
1176 if (rv) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001177 drbd_err(device, "Writing the bitmap failed not starting resync.\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001178 _drbd_request_state(device, NS(conn, C_CONNECTED), CS_VERBOSE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001179 return;
1180 }
1181
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001182 switch (device->state.conn) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001183 case C_STARTING_SYNC_T:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001184 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001185 break;
1186 case C_STARTING_SYNC_S:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001187 drbd_start_resync(device, C_SYNC_SOURCE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001188 break;
1189 }
1190}
1191
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001192int drbd_bitmap_io_from_worker(struct drbd_device *device,
Andreas Gruenbacher54761692011-05-30 16:15:21 +02001193 int (*io_fn)(struct drbd_device *),
Philipp Reisnerb8907332011-01-27 14:07:51 +01001194 char *why, enum bm_flag flags)
1195{
1196 int rv;
1197
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001198 D_ASSERT(device, current == first_peer_device(device)->connection->worker.task);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001199
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001200 /* open coded non-blocking drbd_suspend_io(device); */
1201 set_bit(SUSPEND_IO, &device->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001202
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001203 drbd_bm_lock(device, why, flags);
1204 rv = io_fn(device);
1205 drbd_bm_unlock(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001206
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001207 drbd_resume_io(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001208
1209 return rv;
1210}
1211
1212/**
1213 * after_state_ch() - Perform after state change actions that may sleep
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001214 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +01001215 * @os: old state.
1216 * @ns: new state.
1217 * @flags: Flags
1218 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001219static void after_state_ch(struct drbd_device *device, union drbd_state os,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001220 union drbd_state ns, enum chg_state_flags flags)
1221{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001222 struct sib_info sib;
1223
1224 sib.sib_reason = SIB_STATE_CHANGE;
1225 sib.os = os;
1226 sib.ns = ns;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001227
1228 if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001229 clear_bit(CRASHED_PRIMARY, &device->flags);
1230 if (device->p_uuid)
1231 device->p_uuid[UI_FLAGS] &= ~((u64)2);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001232 }
1233
Philipp Reisnerb8907332011-01-27 14:07:51 +01001234 /* Inform userspace about the change... */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001235 drbd_bcast_event(device, &sib);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001236
1237 if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
1238 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001239 drbd_khelper(device, "pri-on-incon-degr");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001240
1241 /* Here we have the actions that are performed after a
1242 state change. This function might sleep */
1243
Philipp Reisnerb8907332011-01-27 14:07:51 +01001244 if (ns.susp_nod) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001245 struct drbd_connection *connection = first_peer_device(device)->connection;
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001246 enum drbd_req_event what = NOTHING;
1247
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001248 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001249 if (os.conn < C_CONNECTED && conn_lowest_conn(connection) >= C_CONNECTED)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001250 what = RESEND;
1251
Philipp Reisner3fb47462011-07-15 18:44:26 +02001252 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001253 conn_lowest_disk(connection) > D_NEGOTIATING)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001254 what = RESTART_FROZEN_DISK_IO;
1255
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001256 if (connection->susp_nod && what != NOTHING) {
1257 _tl_restart(connection, what);
1258 _conn_request_state(connection,
Philipp Reisner892fdd12012-08-27 17:20:12 +02001259 (union drbd_state) { { .susp_nod = 1 } },
1260 (union drbd_state) { { .susp_nod = 0 } },
1261 CS_VERBOSE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001262 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001263 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001264 }
1265
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001266 if (ns.susp_fen) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001267 struct drbd_connection *connection = first_peer_device(device)->connection;
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001268
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001269 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001270 if (connection->susp_fen && conn_lowest_conn(connection) >= C_CONNECTED) {
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001271 /* case2: The connection was established again: */
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001272 struct drbd_peer_device *peer_device;
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001273 int vnr;
1274
1275 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001276 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
1277 clear_bit(NEW_CUR_UUID, &peer_device->device->flags);
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001278 rcu_read_unlock();
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001279 _tl_restart(connection, RESEND);
1280 _conn_request_state(connection,
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001281 (union drbd_state) { { .susp_fen = 1 } },
1282 (union drbd_state) { { .susp_fen = 0 } },
1283 CS_VERBOSE);
1284 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001285 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001286 }
1287
Philipp Reisnerb8907332011-01-27 14:07:51 +01001288 /* Became sync source. With protocol >= 96, we still need to send out
1289 * the sync uuid now. Need to do that before any drbd_send_state, or
1290 * the other side may go "paused sync" before receiving the sync uuids,
1291 * which is unexpected. */
1292 if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
1293 (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001294 first_peer_device(device)->connection->agreed_pro_version >= 96 && get_ldev(device)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001295 drbd_gen_and_send_sync_uuid(device);
1296 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001297 }
1298
1299 /* Do not change the order of the if above and the two below... */
Philipp Reisner369bea62011-07-06 23:04:44 +02001300 if (os.pdsk == D_DISKLESS &&
1301 ns.pdsk > D_DISKLESS && ns.pdsk != D_UNKNOWN) { /* attach on the peer */
Lars Ellenberga3248962012-07-30 09:10:41 +02001302 /* we probably will start a resync soon.
1303 * make sure those things are properly reset. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001304 device->rs_total = 0;
1305 device->rs_failed = 0;
1306 atomic_set(&device->rs_pending_cnt, 0);
1307 drbd_rs_cancel_all(device);
Lars Ellenberga3248962012-07-30 09:10:41 +02001308
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001309 drbd_send_uuids(device);
1310 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001311 }
1312 /* No point in queuing send_bitmap if we don't have a connection
1313 * anymore, so check also the _current_ state, not only the new state
1314 * at the time this work was queued. */
1315 if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001316 device->state.conn == C_WF_BITMAP_S)
1317 drbd_queue_bitmap_io(device, &drbd_send_bitmap, NULL,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001318 "send_bitmap (WFBitMapS)",
1319 BM_LOCKED_TEST_ALLOWED);
1320
1321 /* Lost contact to peer's copy of the data */
1322 if ((os.pdsk >= D_INCONSISTENT &&
1323 os.pdsk != D_UNKNOWN &&
1324 os.pdsk != D_OUTDATED)
1325 && (ns.pdsk < D_INCONSISTENT ||
1326 ns.pdsk == D_UNKNOWN ||
1327 ns.pdsk == D_OUTDATED)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001328 if (get_ldev(device)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001329 if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001330 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1331 if (drbd_suspended(device)) {
1332 set_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001333 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001334 drbd_uuid_new_current(device);
1335 drbd_send_uuids(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001336 }
1337 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001338 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001339 }
1340 }
1341
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001342 if (ns.pdsk < D_INCONSISTENT && get_ldev(device)) {
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001343 if (os.peer == R_SECONDARY && ns.peer == R_PRIMARY &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001344 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1345 drbd_uuid_new_current(device);
1346 drbd_send_uuids(device);
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001347 }
Philipp Reisnerb8907332011-01-27 14:07:51 +01001348 /* D_DISKLESS Peer becomes secondary */
1349 if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
1350 /* We may still be Primary ourselves.
1351 * No harm done if the bitmap still changes,
1352 * redirtied pages will follow later. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001353 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001354 "demote diskless peer", BM_LOCKED_SET_ALLOWED);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001355 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001356 }
1357
1358 /* Write out all changed bits on demote.
1359 * Though, no need to da that just yet
1360 * if there is a resync going on still */
1361 if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001362 device->state.conn <= C_CONNECTED && get_ldev(device)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001363 /* No changes to the bitmap expected this time, so assert that,
1364 * even though no harm was done if it did change. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001365 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001366 "demote", BM_LOCKED_TEST_ALLOWED);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001367 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001368 }
1369
1370 /* Last part of the attaching process ... */
1371 if (ns.conn >= C_CONNECTED &&
1372 os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001373 drbd_send_sizes(device, 0, 0); /* to start sync... */
1374 drbd_send_uuids(device);
1375 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001376 }
1377
1378 /* We want to pause/continue resync, tell peer. */
1379 if (ns.conn >= C_CONNECTED &&
1380 ((os.aftr_isp != ns.aftr_isp) ||
1381 (os.user_isp != ns.user_isp)))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001382 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001383
1384 /* In case one of the isp bits got set, suspend other devices. */
1385 if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
1386 (ns.aftr_isp || ns.peer_isp || ns.user_isp))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001387 suspend_other_sg(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001388
1389 /* Make sure the peer gets informed about eventual state
1390 changes (ISP bits) while we were in WFReportParams. */
1391 if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001392 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001393
1394 if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001395 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001396
1397 /* We are in the progress to start a full sync... */
1398 if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
1399 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
1400 /* no other bitmap changes expected during this phase */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001401 drbd_queue_bitmap_io(device,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001402 &drbd_bmio_set_n_write, &abw_start_sync,
1403 "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
1404
Philipp Reisnerb8907332011-01-27 14:07:51 +01001405 /* first half of local IO error, failure to attach,
1406 * or administrative detach */
1407 if (os.disk != D_FAILED && ns.disk == D_FAILED) {
Philipp Reisner32db80f2012-02-22 11:51:57 +01001408 enum drbd_io_error_p eh = EP_PASS_ON;
1409 int was_io_error = 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001410 /* corresponding get_ldev was in __drbd_set_state, to serialize
Philipp Reisner32db80f2012-02-22 11:51:57 +01001411 * our cleanup here with the transition to D_DISKLESS.
1412 * But is is still not save to dreference ldev here, since
1413 * we might come from an failed Attach before ldev was set. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001414 if (device->ldev) {
Philipp Reisner32db80f2012-02-22 11:51:57 +01001415 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001416 eh = rcu_dereference(device->ldev->disk_conf)->on_io_error;
Philipp Reisner32db80f2012-02-22 11:51:57 +01001417 rcu_read_unlock();
Philipp Reisnerb8907332011-01-27 14:07:51 +01001418
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001419 was_io_error = test_and_clear_bit(WAS_IO_ERROR, &device->flags);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001420
Lars Ellenberg6f1a6562012-07-30 09:11:01 +02001421 if (was_io_error && eh == EP_CALL_HELPER)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001422 drbd_khelper(device, "local-io-error");
Lars Ellenberg6f1a6562012-07-30 09:11:01 +02001423
Lars Ellenberg0c849662012-07-30 09:07:28 +02001424 /* Immediately allow completion of all application IO,
1425 * that waits for completion from the local disk,
1426 * if this was a force-detach due to disk_timeout
1427 * or administrator request (drbdsetup detach --force).
1428 * Do NOT abort otherwise.
1429 * Aborting local requests may cause serious problems,
1430 * if requests are completed to upper layers already,
1431 * and then later the already submitted local bio completes.
1432 * This can cause DMA into former bio pages that meanwhile
1433 * have been re-used for other things.
1434 * So aborting local requests may cause crashes,
1435 * or even worse, silent data corruption.
1436 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001437 if (test_and_clear_bit(FORCE_DETACH, &device->flags))
1438 tl_abort_disk_io(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001439
Philipp Reisner32db80f2012-02-22 11:51:57 +01001440 /* current state still has to be D_FAILED,
1441 * there is only one way out: to D_DISKLESS,
1442 * and that may only happen after our put_ldev below. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001443 if (device->state.disk != D_FAILED)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001444 drbd_err(device,
Philipp Reisner32db80f2012-02-22 11:51:57 +01001445 "ASSERT FAILED: disk is %s during detach\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001446 drbd_disk_str(device->state.disk));
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001447
Philipp Reisner32db80f2012-02-22 11:51:57 +01001448 if (ns.conn >= C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001449 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001450
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001451 drbd_rs_cancel_all(device);
Philipp Reisner32db80f2012-02-22 11:51:57 +01001452
1453 /* In case we want to get something to stable storage still,
1454 * this may be the last chance.
1455 * Following put_ldev may transition to D_DISKLESS. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001456 drbd_md_sync(device);
Philipp Reisner32db80f2012-02-22 11:51:57 +01001457 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001458 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001459 }
1460
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001461 /* second half of local IO error, failure to attach,
1462 * or administrative detach,
1463 * after local_cnt references have reached zero again */
1464 if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
1465 /* We must still be diskless,
1466 * re-attach has to be serialized with this! */
1467 if (device->state.disk != D_DISKLESS)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001468 drbd_err(device,
1469 "ASSERT FAILED: disk is %s while going diskless\n",
1470 drbd_disk_str(device->state.disk));
Philipp Reisnerb8907332011-01-27 14:07:51 +01001471
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001472 if (ns.conn >= C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001473 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001474 /* corresponding get_ldev in __drbd_set_state
1475 * this may finally trigger drbd_ldev_destroy. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001476 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001477 }
1478
1479 /* Notify peer that I had a local IO error, and did not detached.. */
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001480 if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT && ns.conn >= C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001481 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001482
1483 /* Disks got bigger while they were detached */
1484 if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001485 test_and_clear_bit(RESYNC_AFTER_NEG, &device->flags)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001486 if (ns.conn == C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001487 resync_after_online_grow(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001488 }
1489
1490 /* A resync finished or aborted, wake paused devices... */
1491 if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
1492 (os.peer_isp && !ns.peer_isp) ||
1493 (os.user_isp && !ns.user_isp))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001494 resume_next_sg(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001495
1496 /* sync target done with resync. Explicitly notify peer, even though
1497 * it should (at least for non-empty resyncs) already know itself. */
1498 if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001499 drbd_send_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001500
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001501 /* Verify finished, or reached stop sector. Peer did not know about
1502 * the stop sector, and we may even have changed the stop sector during
1503 * verify to interrupt/stop early. Send the new state. */
1504 if (os.conn == C_VERIFY_S && ns.conn == C_CONNECTED
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001505 && verify_can_do_stop_sector(device))
1506 drbd_send_state(device, ns);
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001507
Philipp Reisnerb8907332011-01-27 14:07:51 +01001508 /* This triggers bitmap writeout of potentially still unwritten pages
1509 * if the resync finished cleanly, or aborted because of peer disk
1510 * failure, or because of connection loss.
1511 * For resync aborted because of local disk failure, we cannot do
1512 * any bitmap writeout anymore.
1513 * No harm done if some bits change during this phase.
1514 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001515 if (os.conn > C_CONNECTED && ns.conn <= C_CONNECTED && get_ldev(device)) {
1516 drbd_queue_bitmap_io(device, &drbd_bm_write_copy_pages, NULL,
Lars Ellenberga220d292012-05-07 12:07:18 +02001517 "write from resync_finished", BM_LOCKED_CHANGE_ALLOWED);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001518 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001519 }
1520
1521 if (ns.disk == D_DISKLESS &&
1522 ns.conn == C_STANDALONE &&
1523 ns.role == R_SECONDARY) {
1524 if (os.aftr_isp != ns.aftr_isp)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001525 resume_next_sg(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001526 }
1527
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001528 drbd_md_sync(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001529}
1530
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001531struct after_conn_state_chg_work {
1532 struct drbd_work w;
1533 enum drbd_conns oc;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001534 union drbd_state ns_min;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001535 union drbd_state ns_max; /* new, max state, over all devices */
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001536 enum chg_state_flags flags;
1537};
Philipp Reisnerb8907332011-01-27 14:07:51 +01001538
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001539static int w_after_conn_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001540{
1541 struct after_conn_state_chg_work *acscw =
1542 container_of(w, struct after_conn_state_chg_work, w);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001543 struct drbd_connection *connection = w->connection;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001544 enum drbd_conns oc = acscw->oc;
Philipp Reisner5f082f92011-03-29 13:20:58 +02001545 union drbd_state ns_max = acscw->ns_max;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001546 struct drbd_peer_device *peer_device;
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001547 int vnr;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001548
1549 kfree(acscw);
1550
1551 /* Upon network configuration, we need to start the receiver */
Philipp Reisner5f082f92011-03-29 13:20:58 +02001552 if (oc == C_STANDALONE && ns_max.conn == C_UNCONNECTED)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001553 drbd_thread_start(&connection->receiver);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001554
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02001555 if (oc == C_DISCONNECTING && ns_max.conn == C_STANDALONE) {
1556 struct net_conf *old_conf;
1557
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001558 mutex_lock(&connection->resource->conf_update);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001559 old_conf = connection->net_conf;
1560 connection->my_addr_len = 0;
1561 connection->peer_addr_len = 0;
1562 rcu_assign_pointer(connection->net_conf, NULL);
1563 conn_free_crypto(connection);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001564 mutex_unlock(&connection->resource->conf_update);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02001565
1566 synchronize_rcu();
1567 kfree(old_conf);
1568 }
1569
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001570 if (ns_max.susp_fen) {
1571 /* case1: The outdate peer handler is successful: */
1572 if (ns_max.pdsk <= D_OUTDATED) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001573 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001574 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1575 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001576 if (test_bit(NEW_CUR_UUID, &device->flags)) {
1577 drbd_uuid_new_current(device);
1578 clear_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001579 }
1580 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001581 rcu_read_unlock();
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001582 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001583 _tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
1584 _conn_request_state(connection,
Philipp Reisner8a0bab22012-08-07 13:28:00 +02001585 (union drbd_state) { { .susp_fen = 1 } },
1586 (union drbd_state) { { .susp_fen = 0 } },
1587 CS_VERBOSE);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001588 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001589 }
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001590 }
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02001591 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner19fffd72012-08-28 16:48:03 +02001592
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001593 conn_md_sync(connection);
Philipp Reisner19fffd72012-08-28 16:48:03 +02001594
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001595 return 0;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001596}
1597
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001598void conn_old_common_state(struct drbd_connection *connection, union drbd_state *pcs, enum chg_state_flags *pf)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001599{
Philipp Reisner435693e2011-03-25 15:11:30 +01001600 enum chg_state_flags flags = ~0;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001601 struct drbd_peer_device *peer_device;
Philipp Reisner435693e2011-03-25 15:11:30 +01001602 int vnr, first_vol = 1;
Philipp Reisnere0e16652011-07-11 17:04:23 +02001603 union drbd_dev_state os, cs = {
1604 { .role = R_SECONDARY,
1605 .peer = R_UNKNOWN,
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001606 .conn = connection->cstate,
Philipp Reisnere0e16652011-07-11 17:04:23 +02001607 .disk = D_DISKLESS,
1608 .pdsk = D_UNKNOWN,
1609 } };
Philipp Reisner88ef5942011-03-25 14:31:11 +01001610
Philipp Reisner695d08f2011-04-11 22:53:32 -07001611 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001612 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1613 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001614 os = device->state;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001615
Philipp Reisner435693e2011-03-25 15:11:30 +01001616 if (first_vol) {
1617 cs = os;
1618 first_vol = 0;
1619 continue;
1620 }
Philipp Reisner88ef5942011-03-25 14:31:11 +01001621
Philipp Reisner435693e2011-03-25 15:11:30 +01001622 if (cs.role != os.role)
1623 flags &= ~CS_DC_ROLE;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001624
Philipp Reisner435693e2011-03-25 15:11:30 +01001625 if (cs.peer != os.peer)
1626 flags &= ~CS_DC_PEER;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001627
Philipp Reisner435693e2011-03-25 15:11:30 +01001628 if (cs.conn != os.conn)
1629 flags &= ~CS_DC_CONN;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001630
Philipp Reisner435693e2011-03-25 15:11:30 +01001631 if (cs.disk != os.disk)
1632 flags &= ~CS_DC_DISK;
1633
1634 if (cs.pdsk != os.pdsk)
1635 flags &= ~CS_DC_PDSK;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001636 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001637 rcu_read_unlock();
Philipp Reisner88ef5942011-03-25 14:31:11 +01001638
Philipp Reisner435693e2011-03-25 15:11:30 +01001639 *pf |= CS_DC_MASK;
1640 *pf &= flags;
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02001641 (*pcs).i = cs.i;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001642}
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001643
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001644static enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001645conn_is_valid_transition(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisner88ef5942011-03-25 14:31:11 +01001646 enum chg_state_flags flags)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001647{
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001648 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001649 union drbd_state ns, os;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001650 struct drbd_peer_device *peer_device;
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001651 int vnr;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001652
Philipp Reisner695d08f2011-04-11 22:53:32 -07001653 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001654 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1655 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001656 os = drbd_read_state(device);
1657 ns = sanitize_state(device, apply_mask_val(os, mask, val), NULL);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001658
Philipp Reisner778bcf22011-03-28 12:55:03 +02001659 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1660 ns.disk = os.disk;
1661
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001662 if (ns.i == os.i)
1663 continue;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001664
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001665 rv = is_valid_transition(os, ns);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001666
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001667 if (rv >= SS_SUCCESS && !(flags & CS_HARD)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001668 rv = is_valid_state(device, ns);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001669 if (rv < SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001670 if (is_valid_state(device, os) == rv)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001671 rv = is_valid_soft_transition(os, ns, connection);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001672 } else
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001673 rv = is_valid_soft_transition(os, ns, connection);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001674 }
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001675
1676 if (rv < SS_SUCCESS) {
1677 if (flags & CS_VERBOSE)
1678 print_st_err(device, os, ns, rv);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001679 break;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001680 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001681 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001682 rcu_read_unlock();
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001683
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001684 return rv;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001685}
1686
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001687void
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001688conn_set_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001689 union drbd_state *pns_min, union drbd_state *pns_max, enum chg_state_flags flags)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001690{
Philipp Reisnerf132f552011-07-18 10:44:24 +02001691 union drbd_state ns, os, ns_max = { };
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001692 union drbd_state ns_min = {
1693 { .role = R_MASK,
1694 .peer = R_MASK,
Philipp Reisnere0e16652011-07-11 17:04:23 +02001695 .conn = val.conn,
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001696 .disk = D_MASK,
1697 .pdsk = D_MASK
1698 } };
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001699 struct drbd_peer_device *peer_device;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001700 enum drbd_state_rv rv;
Philipp Reisnerf132f552011-07-18 10:44:24 +02001701 int vnr, number_of_volumes = 0;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001702
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001703 if (mask.conn == C_MASK) {
1704 /* remember last connect time so request_timer_fn() won't
1705 * kill newly established sessions while we are still trying to thaw
1706 * previously frozen IO */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001707 if (connection->cstate != C_WF_REPORT_PARAMS && val.conn == C_WF_REPORT_PARAMS)
1708 connection->last_reconnect_jif = jiffies;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001709
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001710 connection->cstate = val.conn;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001711 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001712
Philipp Reisner695d08f2011-04-11 22:53:32 -07001713 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001714 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1715 struct drbd_device *device = peer_device->device;
Philipp Reisnerf132f552011-07-18 10:44:24 +02001716 number_of_volumes++;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001717 os = drbd_read_state(device);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001718 ns = apply_mask_val(os, mask, val);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001719 ns = sanitize_state(device, ns, NULL);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001720
Philipp Reisner778bcf22011-03-28 12:55:03 +02001721 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1722 ns.disk = os.disk;
1723
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001724 rv = __drbd_set_state(device, ns, flags, NULL);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001725 if (rv < SS_SUCCESS)
1726 BUG();
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001727
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001728 ns.i = device->state.i;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001729 ns_max.role = max_role(ns.role, ns_max.role);
1730 ns_max.peer = max_role(ns.peer, ns_max.peer);
1731 ns_max.conn = max_t(enum drbd_conns, ns.conn, ns_max.conn);
1732 ns_max.disk = max_t(enum drbd_disk_state, ns.disk, ns_max.disk);
1733 ns_max.pdsk = max_t(enum drbd_disk_state, ns.pdsk, ns_max.pdsk);
1734
1735 ns_min.role = min_role(ns.role, ns_min.role);
1736 ns_min.peer = min_role(ns.peer, ns_min.peer);
1737 ns_min.conn = min_t(enum drbd_conns, ns.conn, ns_min.conn);
1738 ns_min.disk = min_t(enum drbd_disk_state, ns.disk, ns_min.disk);
1739 ns_min.pdsk = min_t(enum drbd_disk_state, ns.pdsk, ns_min.pdsk);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001740 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001741 rcu_read_unlock();
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001742
Philipp Reisnerf132f552011-07-18 10:44:24 +02001743 if (number_of_volumes == 0) {
1744 ns_min = ns_max = (union drbd_state) { {
1745 .role = R_SECONDARY,
1746 .peer = R_UNKNOWN,
1747 .conn = val.conn,
1748 .disk = D_DISKLESS,
1749 .pdsk = D_UNKNOWN
1750 } };
1751 }
1752
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001753 ns_min.susp = ns_max.susp = connection->susp;
1754 ns_min.susp_nod = ns_max.susp_nod = connection->susp_nod;
1755 ns_min.susp_fen = ns_max.susp_fen = connection->susp_fen;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001756
1757 *pns_min = ns_min;
1758 *pns_max = ns_max;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001759}
1760
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001761static enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001762_conn_rq_cond(struct drbd_connection *connection, union drbd_state mask, union drbd_state val)
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001763{
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001764 enum drbd_state_rv rv;
1765
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001766 if (test_and_clear_bit(CONN_WD_ST_CHG_OKAY, &connection->flags))
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001767 return SS_CW_SUCCESS;
1768
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001769 if (test_and_clear_bit(CONN_WD_ST_CHG_FAIL, &connection->flags))
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001770 return SS_CW_FAILED_BY_PEER;
1771
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001772 rv = conn_is_valid_transition(connection, mask, val, 0);
1773 if (rv == SS_SUCCESS && connection->cstate == C_WF_REPORT_PARAMS)
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01001774 rv = SS_UNKNOWN_ERROR; /* continue waiting */
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001775
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001776 return rv;
1777}
1778
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001779enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001780_conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001781 enum chg_state_flags flags)
1782{
1783 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001784 struct after_conn_state_chg_work *acscw;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001785 enum drbd_conns oc = connection->cstate;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001786 union drbd_state ns_max, ns_min, os;
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001787 bool have_mutex = false;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001788
Philipp Reisner07fc9612012-08-28 11:07:56 +02001789 if (mask.conn) {
1790 rv = is_valid_conn_transition(oc, val.conn);
1791 if (rv < SS_SUCCESS)
1792 goto abort;
1793 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001794
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001795 rv = conn_is_valid_transition(connection, mask, val, flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001796 if (rv < SS_SUCCESS)
1797 goto abort;
1798
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001799 if (oc == C_WF_REPORT_PARAMS && val.conn == C_DISCONNECTING &&
1800 !(flags & (CS_LOCAL_ONLY | CS_HARD))) {
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001801
1802 /* This will be a cluster-wide state change.
1803 * Need to give up the spinlock, grab the mutex,
1804 * then send the state change request, ... */
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001805 spin_unlock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001806 mutex_lock(&connection->cstate_mutex);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001807 have_mutex = true;
1808
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001809 set_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
1810 if (conn_send_state_req(connection, mask, val)) {
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001811 /* sending failed. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001812 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001813 rv = SS_CW_FAILED_BY_PEER;
1814 /* need to re-aquire the spin lock, though */
1815 goto abort_unlocked;
1816 }
1817
1818 if (val.conn == C_DISCONNECTING)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001819 set_bit(DISCONNECT_SENT, &connection->flags);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001820
1821 /* ... and re-aquire the spinlock.
1822 * If _conn_rq_cond() returned >= SS_SUCCESS, we must call
1823 * conn_set_state() within the same spinlock. */
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001824 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001825 wait_event_lock_irq(connection->ping_wait,
1826 (rv = _conn_rq_cond(connection, mask, val)),
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001827 connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001828 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001829 if (rv < SS_SUCCESS)
1830 goto abort;
1831 }
1832
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001833 conn_old_common_state(connection, &os, &flags);
Philipp Reisner706cb242011-03-29 15:20:27 +02001834 flags |= CS_DC_SUSP;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001835 conn_set_state(connection, mask, val, &ns_min, &ns_max, flags);
1836 conn_pr_state_change(connection, os, ns_max, flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001837
1838 acscw = kmalloc(sizeof(*acscw), GFP_ATOMIC);
1839 if (acscw) {
Philipp Reisner435693e2011-03-25 15:11:30 +01001840 acscw->oc = os.conn;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001841 acscw->ns_min = ns_min;
Philipp Reisner5f082f92011-03-29 13:20:58 +02001842 acscw->ns_max = ns_max;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001843 acscw->flags = flags;
1844 acscw->w.cb = w_after_conn_state_ch;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001845 kref_get(&connection->kref);
1846 acscw->w.connection = connection;
1847 drbd_queue_work(&connection->sender_work, &acscw->w);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001848 } else {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +02001849 drbd_err(connection, "Could not kmalloc an acscw\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001850 }
1851
Philipp Reisnera01842e2011-12-13 17:40:53 +01001852 abort:
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001853 if (have_mutex) {
1854 /* mutex_unlock() "... must not be used in interrupt context.",
1855 * so give up the spinlock, then re-aquire it */
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001856 spin_unlock_irq(&connection->resource->req_lock);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001857 abort_unlocked:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001858 mutex_unlock(&connection->cstate_mutex);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001859 spin_lock_irq(&connection->resource->req_lock);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02001860 }
1861 if (rv < SS_SUCCESS && flags & CS_VERBOSE) {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +02001862 drbd_err(connection, "State change failed: %s\n", drbd_set_st_err_str(rv));
1863 drbd_err(connection, " mask = 0x%x val = 0x%x\n", mask.i, val.i);
1864 drbd_err(connection, " old_conn:%s wanted_conn:%s\n", drbd_conn_str(oc), drbd_conn_str(val.conn));
Philipp Reisnera01842e2011-12-13 17:40:53 +01001865 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001866 return rv;
1867}
1868
1869enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001870conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001871 enum chg_state_flags flags)
1872{
1873 enum drbd_state_rv rv;
1874
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001875 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001876 rv = _conn_request_state(connection, mask, val, flags);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001877 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001878
1879 return rv;
1880}