blob: eea0c4aec9789efc4d7e561e03a569278147e159 [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"
Andreas Gruenbachera2972842014-07-31 17:41:33 +020032#include "drbd_state_change.h"
Philipp Reisnerb8907332011-01-27 14:07:51 +010033
34struct after_state_chg_work {
35 struct drbd_work w;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +020036 struct drbd_device *device;
Philipp Reisnerb8907332011-01-27 14:07:51 +010037 union drbd_state os;
38 union drbd_state ns;
39 enum chg_state_flags flags;
40 struct completion *done;
Andreas Gruenbachera2972842014-07-31 17:41:33 +020041 struct drbd_state_change *state_change;
Philipp Reisnerb8907332011-01-27 14:07:51 +010042};
43
Philipp Reisnerd942ae42011-05-31 13:07:24 +020044enum sanitize_state_warnings {
45 NO_WARNING,
46 ABORTED_ONLINE_VERIFY,
47 ABORTED_RESYNC,
48 CONNECTION_LOST_NEGOTIATING,
49 IMPLICITLY_UPGRADED_DISK,
50 IMPLICITLY_UPGRADED_PDSK,
51};
52
Andreas Gruenbachera2972842014-07-31 17:41:33 +020053static void count_objects(struct drbd_resource *resource,
54 unsigned int *n_devices,
55 unsigned int *n_connections)
56{
57 struct drbd_device *device;
58 struct drbd_connection *connection;
59 int vnr;
60
61 *n_devices = 0;
62 *n_connections = 0;
63
64 idr_for_each_entry(&resource->devices, device, vnr)
65 (*n_devices)++;
Lars Ellenberg2b479762015-02-25 13:53:28 +010066 for_each_connection(connection, resource)
Andreas Gruenbachera2972842014-07-31 17:41:33 +020067 (*n_connections)++;
Andreas Gruenbachera2972842014-07-31 17:41:33 +020068}
69
70static struct drbd_state_change *alloc_state_change(unsigned int n_devices, unsigned int n_connections, gfp_t gfp)
71{
72 struct drbd_state_change *state_change;
73 unsigned int size, n;
74
75 size = sizeof(struct drbd_state_change) +
76 n_devices * sizeof(struct drbd_device_state_change) +
77 n_connections * sizeof(struct drbd_connection_state_change) +
78 n_devices * n_connections * sizeof(struct drbd_peer_device_state_change);
79 state_change = kmalloc(size, gfp);
80 if (!state_change)
81 return NULL;
82 state_change->n_devices = n_devices;
83 state_change->n_connections = n_connections;
84 state_change->devices = (void *)(state_change + 1);
85 state_change->connections = (void *)&state_change->devices[n_devices];
86 state_change->peer_devices = (void *)&state_change->connections[n_connections];
87 state_change->resource->resource = NULL;
88 for (n = 0; n < n_devices; n++)
89 state_change->devices[n].device = NULL;
90 for (n = 0; n < n_connections; n++)
91 state_change->connections[n].connection = NULL;
92 return state_change;
93}
94
95struct drbd_state_change *remember_old_state(struct drbd_resource *resource, gfp_t gfp)
96{
97 struct drbd_state_change *state_change;
98 struct drbd_device *device;
99 unsigned int n_devices;
100 struct drbd_connection *connection;
101 unsigned int n_connections;
102 int vnr;
103
104 struct drbd_device_state_change *device_state_change;
105 struct drbd_peer_device_state_change *peer_device_state_change;
106 struct drbd_connection_state_change *connection_state_change;
107
Lars Ellenberg2b479762015-02-25 13:53:28 +0100108 /* Caller holds req_lock spinlock.
109 * No state, no device IDR, no connections lists can change. */
Andreas Gruenbachera2972842014-07-31 17:41:33 +0200110 count_objects(resource, &n_devices, &n_connections);
Andreas Gruenbachera2972842014-07-31 17:41:33 +0200111 state_change = alloc_state_change(n_devices, n_connections, gfp);
112 if (!state_change)
113 return NULL;
114
Andreas Gruenbachera2972842014-07-31 17:41:33 +0200115 kref_get(&resource->kref);
116 state_change->resource->resource = resource;
117 state_change->resource->role[OLD] =
118 conn_highest_role(first_connection(resource));
119 state_change->resource->susp[OLD] = resource->susp;
120 state_change->resource->susp_nod[OLD] = resource->susp_nod;
121 state_change->resource->susp_fen[OLD] = resource->susp_fen;
122
Lars Ellenberg2b479762015-02-25 13:53:28 +0100123 connection_state_change = state_change->connections;
124 for_each_connection(connection, resource) {
125 kref_get(&connection->kref);
126 connection_state_change->connection = connection;
127 connection_state_change->cstate[OLD] =
128 connection->cstate;
129 connection_state_change->peer_role[OLD] =
130 conn_highest_peer(connection);
131 connection_state_change++;
132 }
133
Andreas Gruenbachera2972842014-07-31 17:41:33 +0200134 device_state_change = state_change->devices;
135 peer_device_state_change = state_change->peer_devices;
136 idr_for_each_entry(&resource->devices, device, vnr) {
137 kref_get(&device->kref);
138 device_state_change->device = device;
139 device_state_change->disk_state[OLD] = device->state.disk;
140
141 /* The peer_devices for each device have to be enumerated in
142 the order of the connections. We may not use for_each_peer_device() here. */
143 for_each_connection(connection, resource) {
144 struct drbd_peer_device *peer_device;
145
Andreas Gruenbachera2972842014-07-31 17:41:33 +0200146 peer_device = conn_peer_device(connection, device->vnr);
147 peer_device_state_change->peer_device = peer_device;
148 peer_device_state_change->disk_state[OLD] =
149 device->state.pdsk;
150 peer_device_state_change->repl_state[OLD] =
151 max_t(enum drbd_conns,
152 C_WF_REPORT_PARAMS, device->state.conn);
153 peer_device_state_change->resync_susp_user[OLD] =
154 device->state.user_isp;
155 peer_device_state_change->resync_susp_peer[OLD] =
156 device->state.peer_isp;
157 peer_device_state_change->resync_susp_dependency[OLD] =
158 device->state.aftr_isp;
159 peer_device_state_change++;
160 }
161 device_state_change++;
162 }
163
Andreas Gruenbachera2972842014-07-31 17:41:33 +0200164 return state_change;
165}
166
167static void remember_new_state(struct drbd_state_change *state_change)
168{
169 struct drbd_resource_state_change *resource_state_change;
170 struct drbd_resource *resource;
171 unsigned int n;
172
173 if (!state_change)
174 return;
175
176 resource_state_change = &state_change->resource[0];
177 resource = resource_state_change->resource;
178
179 resource_state_change->role[NEW] =
180 conn_highest_role(first_connection(resource));
181 resource_state_change->susp[NEW] = resource->susp;
182 resource_state_change->susp_nod[NEW] = resource->susp_nod;
183 resource_state_change->susp_fen[NEW] = resource->susp_fen;
184
185 for (n = 0; n < state_change->n_devices; n++) {
186 struct drbd_device_state_change *device_state_change =
187 &state_change->devices[n];
188 struct drbd_device *device = device_state_change->device;
189
190 device_state_change->disk_state[NEW] = device->state.disk;
191 }
192
193 for (n = 0; n < state_change->n_connections; n++) {
194 struct drbd_connection_state_change *connection_state_change =
195 &state_change->connections[n];
196 struct drbd_connection *connection =
197 connection_state_change->connection;
198
199 connection_state_change->cstate[NEW] = connection->cstate;
200 connection_state_change->peer_role[NEW] =
201 conn_highest_peer(connection);
202 }
203
204 for (n = 0; n < state_change->n_devices * state_change->n_connections; n++) {
205 struct drbd_peer_device_state_change *peer_device_state_change =
206 &state_change->peer_devices[n];
207 struct drbd_device *device =
208 peer_device_state_change->peer_device->device;
209 union drbd_dev_state state = device->state;
210
211 peer_device_state_change->disk_state[NEW] = state.pdsk;
212 peer_device_state_change->repl_state[NEW] =
213 max_t(enum drbd_conns, C_WF_REPORT_PARAMS, state.conn);
214 peer_device_state_change->resync_susp_user[NEW] =
215 state.user_isp;
216 peer_device_state_change->resync_susp_peer[NEW] =
217 state.peer_isp;
218 peer_device_state_change->resync_susp_dependency[NEW] =
219 state.aftr_isp;
220 }
221}
222
223void copy_old_to_new_state_change(struct drbd_state_change *state_change)
224{
225 struct drbd_resource_state_change *resource_state_change = &state_change->resource[0];
226 unsigned int n_device, n_connection, n_peer_device, n_peer_devices;
227
228#define OLD_TO_NEW(x) \
229 (x[NEW] = x[OLD])
230
231 OLD_TO_NEW(resource_state_change->role);
232 OLD_TO_NEW(resource_state_change->susp);
233 OLD_TO_NEW(resource_state_change->susp_nod);
234 OLD_TO_NEW(resource_state_change->susp_fen);
235
236 for (n_connection = 0; n_connection < state_change->n_connections; n_connection++) {
237 struct drbd_connection_state_change *connection_state_change =
238 &state_change->connections[n_connection];
239
240 OLD_TO_NEW(connection_state_change->peer_role);
241 OLD_TO_NEW(connection_state_change->cstate);
242 }
243
244 for (n_device = 0; n_device < state_change->n_devices; n_device++) {
245 struct drbd_device_state_change *device_state_change =
246 &state_change->devices[n_device];
247
248 OLD_TO_NEW(device_state_change->disk_state);
249 }
250
251 n_peer_devices = state_change->n_devices * state_change->n_connections;
252 for (n_peer_device = 0; n_peer_device < n_peer_devices; n_peer_device++) {
253 struct drbd_peer_device_state_change *p =
254 &state_change->peer_devices[n_peer_device];
255
256 OLD_TO_NEW(p->disk_state);
257 OLD_TO_NEW(p->repl_state);
258 OLD_TO_NEW(p->resync_susp_user);
259 OLD_TO_NEW(p->resync_susp_peer);
260 OLD_TO_NEW(p->resync_susp_dependency);
261 }
262
263#undef OLD_TO_NEW
264}
265
266void forget_state_change(struct drbd_state_change *state_change)
267{
268 unsigned int n;
269
270 if (!state_change)
271 return;
272
273 if (state_change->resource->resource)
274 kref_put(&state_change->resource->resource->kref, drbd_destroy_resource);
275 for (n = 0; n < state_change->n_devices; n++) {
276 struct drbd_device *device = state_change->devices[n].device;
277
278 if (device)
279 kref_put(&device->kref, drbd_destroy_device);
280 }
281 for (n = 0; n < state_change->n_connections; n++) {
282 struct drbd_connection *connection =
283 state_change->connections[n].connection;
284
285 if (connection)
286 kref_put(&connection->kref, drbd_destroy_connection);
287 }
288 kfree(state_change);
289}
290
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100291static int w_after_state_ch(struct drbd_work *w, int unused);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200292static void after_state_ch(struct drbd_device *device, union drbd_state os,
Andreas Gruenbachera2972842014-07-31 17:41:33 +0200293 union drbd_state ns, enum chg_state_flags flags,
294 struct drbd_state_change *);
Andreas Gruenbacher54761692011-05-30 16:15:21 +0200295static enum drbd_state_rv is_valid_state(struct drbd_device *, union drbd_state);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200296static enum drbd_state_rv is_valid_soft_transition(union drbd_state, union drbd_state, struct drbd_connection *);
Philipp Reisner35095022011-02-09 16:29:33 +0100297static enum drbd_state_rv is_valid_transition(union drbd_state os, union drbd_state ns);
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +0200298static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state os,
299 union drbd_state ns, enum sanitize_state_warnings *warn);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100300
Philipp Reisner2aebfab2011-03-28 16:48:11 +0200301static inline bool is_susp(union drbd_state s)
302{
303 return s.susp || s.susp_nod || s.susp_fen;
304}
305
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200306bool conn_all_vols_unconf(struct drbd_connection *connection)
Philipp Reisner0e29d162011-02-18 14:23:11 +0100307{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200308 struct drbd_peer_device *peer_device;
Philipp Reisner695d08f2011-04-11 22:53:32 -0700309 bool rv = true;
Philipp Reisnere90285e2011-03-22 12:51:21 +0100310 int vnr;
Philipp Reisner0e29d162011-02-18 14:23:11 +0100311
Philipp Reisner695d08f2011-04-11 22:53:32 -0700312 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200313 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
314 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200315 if (device->state.disk != D_DISKLESS ||
316 device->state.conn != C_STANDALONE ||
317 device->state.role != R_SECONDARY) {
Philipp Reisner695d08f2011-04-11 22:53:32 -0700318 rv = false;
319 break;
320 }
Philipp Reisner0e29d162011-02-18 14:23:11 +0100321 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700322 rcu_read_unlock();
323
324 return rv;
Philipp Reisner0e29d162011-02-18 14:23:11 +0100325}
326
Philipp Reisnercb703452011-03-24 11:03:07 +0100327/* Unfortunately the states where not correctly ordered, when
328 they where defined. therefore can not use max_t() here. */
329static enum drbd_role max_role(enum drbd_role role1, enum drbd_role role2)
330{
331 if (role1 == R_PRIMARY || role2 == R_PRIMARY)
332 return R_PRIMARY;
333 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
334 return R_SECONDARY;
335 return R_UNKNOWN;
336}
Andreas Gruenbachera2972842014-07-31 17:41:33 +0200337
Philipp Reisnercb703452011-03-24 11:03:07 +0100338static enum drbd_role min_role(enum drbd_role role1, enum drbd_role role2)
339{
340 if (role1 == R_UNKNOWN || role2 == R_UNKNOWN)
341 return R_UNKNOWN;
342 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
343 return R_SECONDARY;
344 return R_PRIMARY;
345}
346
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200347enum drbd_role conn_highest_role(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100348{
349 enum drbd_role role = R_UNKNOWN;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200350 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100351 int vnr;
352
Philipp Reisner695d08f2011-04-11 22:53:32 -0700353 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200354 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
355 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200356 role = max_role(role, device->state.role);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200357 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700358 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100359
360 return role;
361}
362
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200363enum drbd_role conn_highest_peer(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100364{
365 enum drbd_role peer = R_UNKNOWN;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200366 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100367 int vnr;
368
Philipp Reisner695d08f2011-04-11 22:53:32 -0700369 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200370 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
371 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200372 peer = max_role(peer, device->state.peer);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200373 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700374 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100375
376 return peer;
377}
378
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200379enum drbd_disk_state conn_highest_disk(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100380{
Andreas Gruenbacher11f8b2b2014-09-11 14:29:05 +0200381 enum drbd_disk_state disk_state = D_DISKLESS;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200382 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100383 int vnr;
384
Philipp Reisner695d08f2011-04-11 22:53:32 -0700385 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200386 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
387 struct drbd_device *device = peer_device->device;
Andreas Gruenbacher11f8b2b2014-09-11 14:29:05 +0200388 disk_state = max_t(enum drbd_disk_state, disk_state, device->state.disk);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200389 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700390 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100391
Andreas Gruenbacher11f8b2b2014-09-11 14:29:05 +0200392 return disk_state;
Philipp Reisnercb703452011-03-24 11:03:07 +0100393}
394
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200395enum drbd_disk_state conn_lowest_disk(struct drbd_connection *connection)
Philipp Reisner46692652011-03-29 18:15:49 +0200396{
Andreas Gruenbacher11f8b2b2014-09-11 14:29:05 +0200397 enum drbd_disk_state disk_state = D_MASK;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200398 struct drbd_peer_device *peer_device;
Philipp Reisner46692652011-03-29 18:15:49 +0200399 int vnr;
400
Philipp Reisner695d08f2011-04-11 22:53:32 -0700401 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200402 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
403 struct drbd_device *device = peer_device->device;
Andreas Gruenbacher11f8b2b2014-09-11 14:29:05 +0200404 disk_state = min_t(enum drbd_disk_state, disk_state, device->state.disk);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200405 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700406 rcu_read_unlock();
Philipp Reisner46692652011-03-29 18:15:49 +0200407
Andreas Gruenbacher11f8b2b2014-09-11 14:29:05 +0200408 return disk_state;
Philipp Reisner46692652011-03-29 18:15:49 +0200409}
410
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200411enum drbd_disk_state conn_highest_pdsk(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100412{
Andreas Gruenbacher11f8b2b2014-09-11 14:29:05 +0200413 enum drbd_disk_state disk_state = D_DISKLESS;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200414 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100415 int vnr;
416
Philipp Reisner695d08f2011-04-11 22:53:32 -0700417 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200418 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
419 struct drbd_device *device = peer_device->device;
Andreas Gruenbacher11f8b2b2014-09-11 14:29:05 +0200420 disk_state = max_t(enum drbd_disk_state, disk_state, device->state.pdsk);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200421 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700422 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100423
Andreas Gruenbacher11f8b2b2014-09-11 14:29:05 +0200424 return disk_state;
Philipp Reisnercb703452011-03-24 11:03:07 +0100425}
426
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200427enum drbd_conns conn_lowest_conn(struct drbd_connection *connection)
Philipp Reisner19f83c72011-03-29 14:21:03 +0200428{
429 enum drbd_conns conn = C_MASK;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200430 struct drbd_peer_device *peer_device;
Philipp Reisner19f83c72011-03-29 14:21:03 +0200431 int vnr;
432
Philipp Reisner695d08f2011-04-11 22:53:32 -0700433 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200434 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
435 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200436 conn = min_t(enum drbd_conns, conn, device->state.conn);
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200437 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700438 rcu_read_unlock();
Philipp Reisner19f83c72011-03-29 14:21:03 +0200439
440 return conn;
441}
442
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200443static bool no_peer_wf_report_params(struct drbd_connection *connection)
Philipp Reisner79702012012-08-28 11:33:35 +0200444{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200445 struct drbd_peer_device *peer_device;
Philipp Reisner79702012012-08-28 11:33:35 +0200446 int vnr;
447 bool rv = true;
448
449 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200450 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
451 if (peer_device->device->state.conn == C_WF_REPORT_PARAMS) {
Philipp Reisner79702012012-08-28 11:33:35 +0200452 rv = false;
453 break;
454 }
455 rcu_read_unlock();
456
457 return rv;
458}
459
Philipp Reisnera8821532014-11-10 17:21:11 +0100460static void wake_up_all_devices(struct drbd_connection *connection)
461{
462 struct drbd_peer_device *peer_device;
463 int vnr;
464
465 rcu_read_lock();
466 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
467 wake_up(&peer_device->device->state_wait);
468 rcu_read_unlock();
469
470}
471
Philipp Reisner79702012012-08-28 11:33:35 +0200472
Philipp Reisnerb8907332011-01-27 14:07:51 +0100473/**
474 * cl_wide_st_chg() - true if the state change is a cluster wide one
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200475 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100476 * @os: old (current) state.
477 * @ns: new (wanted) state.
478 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200479static int cl_wide_st_chg(struct drbd_device *device,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100480 union drbd_state os, union drbd_state ns)
481{
482 return (os.conn >= C_CONNECTED && ns.conn >= C_CONNECTED &&
483 ((os.role != R_PRIMARY && ns.role == R_PRIMARY) ||
484 (os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
485 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S) ||
Lars Ellenberge8744f52012-03-26 15:57:00 +0200486 (os.disk != D_FAILED && ns.disk == D_FAILED))) ||
Philipp Reisnerb8907332011-01-27 14:07:51 +0100487 (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
Philipp Reisner369bea62011-07-06 23:04:44 +0200488 (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S) ||
489 (os.conn == C_CONNECTED && ns.conn == C_WF_REPORT_PARAMS);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100490}
491
Philipp Reisner56707f92011-02-16 14:57:50 +0100492static union drbd_state
493apply_mask_val(union drbd_state os, union drbd_state mask, union drbd_state val)
494{
495 union drbd_state ns;
496 ns.i = (os.i & ~mask.i) | val.i;
497 return ns;
498}
499
Philipp Reisnerb8907332011-01-27 14:07:51 +0100500enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200501drbd_change_state(struct drbd_device *device, enum chg_state_flags f,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100502 union drbd_state mask, union drbd_state val)
503{
504 unsigned long flags;
Philipp Reisner56707f92011-02-16 14:57:50 +0100505 union drbd_state ns;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100506 enum drbd_state_rv rv;
507
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200508 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200509 ns = apply_mask_val(drbd_read_state(device), mask, val);
510 rv = _drbd_set_state(device, ns, f, NULL);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200511 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100512
513 return rv;
514}
515
516/**
517 * drbd_force_state() - Impose a change which happens outside our control on our state
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200518 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100519 * @mask: mask of state bits to change.
520 * @val: value of new state bits.
521 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200522void drbd_force_state(struct drbd_device *device,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100523 union drbd_state mask, union drbd_state val)
524{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200525 drbd_change_state(device, CS_HARD, mask, val);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100526}
527
528static enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200529_req_st_cond(struct drbd_device *device, union drbd_state mask,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100530 union drbd_state val)
531{
532 union drbd_state os, ns;
533 unsigned long flags;
534 enum drbd_state_rv rv;
535
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200536 if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100537 return SS_CW_SUCCESS;
538
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200539 if (test_and_clear_bit(CL_ST_CHG_FAIL, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100540 return SS_CW_FAILED_BY_PEER;
541
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200542 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200543 os = drbd_read_state(device);
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +0200544 ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100545 rv = is_valid_transition(os, ns);
Philipp Reisnera3025a22012-09-03 15:39:01 +0200546 if (rv >= SS_SUCCESS)
Philipp Reisner35095022011-02-09 16:29:33 +0100547 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100548
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200549 if (!cl_wide_st_chg(device, os, ns))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100550 rv = SS_CW_NO_NEED;
Philipp Reisner35095022011-02-09 16:29:33 +0100551 if (rv == SS_UNKNOWN_ERROR) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200552 rv = is_valid_state(device, ns);
Philipp Reisnera3025a22012-09-03 15:39:01 +0200553 if (rv >= SS_SUCCESS) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200554 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
Philipp Reisnera3025a22012-09-03 15:39:01 +0200555 if (rv >= SS_SUCCESS)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100556 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
557 }
558 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200559 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100560
561 return rv;
562}
563
564/**
565 * drbd_req_state() - Perform an eventually cluster wide state change
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200566 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100567 * @mask: mask of state bits to change.
568 * @val: value of new state bits.
569 * @f: flags
570 *
571 * Should not be called directly, use drbd_request_state() or
572 * _drbd_request_state().
573 */
574static enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200575drbd_req_state(struct drbd_device *device, union drbd_state mask,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100576 union drbd_state val, enum chg_state_flags f)
577{
578 struct completion done;
579 unsigned long flags;
580 union drbd_state os, ns;
581 enum drbd_state_rv rv;
582
583 init_completion(&done);
584
585 if (f & CS_SERIALIZE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200586 mutex_lock(device->state_mutex);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100587
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200588 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200589 os = drbd_read_state(device);
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +0200590 ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100591 rv = is_valid_transition(os, ns);
Lars Ellenberg3c5e5f62011-03-15 16:04:09 +0100592 if (rv < SS_SUCCESS) {
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200593 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisner35095022011-02-09 16:29:33 +0100594 goto abort;
Lars Ellenberg3c5e5f62011-03-15 16:04:09 +0100595 }
Philipp Reisnerb8907332011-01-27 14:07:51 +0100596
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200597 if (cl_wide_st_chg(device, os, ns)) {
598 rv = is_valid_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100599 if (rv == SS_SUCCESS)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200600 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200601 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100602
603 if (rv < SS_SUCCESS) {
604 if (f & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200605 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100606 goto abort;
607 }
608
Andreas Gruenbacher69a22772011-08-09 00:47:13 +0200609 if (drbd_send_state_req(first_peer_device(device), mask, val)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100610 rv = SS_CW_FAILED_BY_PEER;
611 if (f & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200612 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100613 goto abort;
614 }
615
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200616 wait_event(device->state_wait,
617 (rv = _req_st_cond(device, mask, val)));
Philipp Reisnerb8907332011-01-27 14:07:51 +0100618
619 if (rv < SS_SUCCESS) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100620 if (f & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200621 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100622 goto abort;
623 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200624 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200625 ns = apply_mask_val(drbd_read_state(device), mask, val);
626 rv = _drbd_set_state(device, ns, f, &done);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100627 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200628 rv = _drbd_set_state(device, ns, f, &done);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100629 }
630
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200631 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100632
633 if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200634 D_ASSERT(device, current != first_peer_device(device)->connection->worker.task);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100635 wait_for_completion(&done);
636 }
637
638abort:
639 if (f & CS_SERIALIZE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200640 mutex_unlock(device->state_mutex);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100641
642 return rv;
643}
644
645/**
646 * _drbd_request_state() - Request a state change (with flags)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200647 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100648 * @mask: mask of state bits to change.
649 * @val: value of new state bits.
650 * @f: flags
651 *
652 * Cousin of drbd_request_state(), useful with the CS_WAIT_COMPLETE
653 * flag, or when logging of failed state change requests is not desired.
654 */
655enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200656_drbd_request_state(struct drbd_device *device, union drbd_state mask,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100657 union drbd_state val, enum chg_state_flags f)
658{
659 enum drbd_state_rv rv;
660
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200661 wait_event(device->state_wait,
662 (rv = drbd_req_state(device, mask, val, f)) != SS_IN_TRANSIENT_STATE);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100663
664 return rv;
665}
666
Philipp Reisnera8821532014-11-10 17:21:11 +0100667enum drbd_state_rv
668_drbd_request_state_holding_state_mutex(struct drbd_device *device, union drbd_state mask,
669 union drbd_state val, enum chg_state_flags f)
670{
671 enum drbd_state_rv rv;
672
673 BUG_ON(f & CS_SERIALIZE);
674
675 wait_event_cmd(device->state_wait,
676 (rv = drbd_req_state(device, mask, val, f)) != SS_IN_TRANSIENT_STATE,
677 mutex_unlock(device->state_mutex),
678 mutex_lock(device->state_mutex));
679
680 return rv;
681}
682
Lars Ellenberg8ce953a2014-02-27 09:46:18 +0100683static void print_st(struct drbd_device *device, const char *name, union drbd_state ns)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100684{
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200685 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 +0100686 name,
687 drbd_conn_str(ns.conn),
688 drbd_role_str(ns.role),
689 drbd_role_str(ns.peer),
690 drbd_disk_str(ns.disk),
691 drbd_disk_str(ns.pdsk),
692 is_susp(ns) ? 's' : 'r',
693 ns.aftr_isp ? 'a' : '-',
694 ns.peer_isp ? 'p' : '-',
695 ns.user_isp ? 'u' : '-',
696 ns.susp_fen ? 'F' : '-',
697 ns.susp_nod ? 'N' : '-'
698 );
699}
700
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200701void print_st_err(struct drbd_device *device, union drbd_state os,
Philipp Reisnerb8907332011-01-27 14:07:51 +0100702 union drbd_state ns, enum drbd_state_rv err)
703{
704 if (err == SS_IN_TRANSIENT_STATE)
705 return;
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200706 drbd_err(device, "State change failed: %s\n", drbd_set_st_err_str(err));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200707 print_st(device, " state", os);
708 print_st(device, "wanted", ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100709}
710
Philipp Reisner435693e2011-03-25 15:11:30 +0100711static long print_state_change(char *pb, union drbd_state os, union drbd_state ns,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100712 enum chg_state_flags flags)
713{
Philipp Reisner435693e2011-03-25 15:11:30 +0100714 char *pbp;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100715 pbp = pb;
716 *pbp = 0;
Philipp Reisner706cb242011-03-29 15:20:27 +0200717
Philipp Reisner435693e2011-03-25 15:11:30 +0100718 if (ns.role != os.role && flags & CS_DC_ROLE)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100719 pbp += sprintf(pbp, "role( %s -> %s ) ",
720 drbd_role_str(os.role),
721 drbd_role_str(ns.role));
Philipp Reisner435693e2011-03-25 15:11:30 +0100722 if (ns.peer != os.peer && flags & CS_DC_PEER)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100723 pbp += sprintf(pbp, "peer( %s -> %s ) ",
724 drbd_role_str(os.peer),
725 drbd_role_str(ns.peer));
Philipp Reisner435693e2011-03-25 15:11:30 +0100726 if (ns.conn != os.conn && flags & CS_DC_CONN)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100727 pbp += sprintf(pbp, "conn( %s -> %s ) ",
728 drbd_conn_str(os.conn),
729 drbd_conn_str(ns.conn));
Philipp Reisner435693e2011-03-25 15:11:30 +0100730 if (ns.disk != os.disk && flags & CS_DC_DISK)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100731 pbp += sprintf(pbp, "disk( %s -> %s ) ",
732 drbd_disk_str(os.disk),
733 drbd_disk_str(ns.disk));
Philipp Reisner435693e2011-03-25 15:11:30 +0100734 if (ns.pdsk != os.pdsk && flags & CS_DC_PDSK)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100735 pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
736 drbd_disk_str(os.pdsk),
737 drbd_disk_str(ns.pdsk));
Philipp Reisner706cb242011-03-29 15:20:27 +0200738
739 return pbp - pb;
740}
741
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200742static void drbd_pr_state_change(struct drbd_device *device, union drbd_state os, union drbd_state ns,
Philipp Reisner706cb242011-03-29 15:20:27 +0200743 enum chg_state_flags flags)
744{
745 char pb[300];
746 char *pbp = pb;
747
748 pbp += print_state_change(pbp, os, ns, flags ^ CS_DC_MASK);
749
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100750 if (ns.aftr_isp != os.aftr_isp)
751 pbp += sprintf(pbp, "aftr_isp( %d -> %d ) ",
752 os.aftr_isp,
753 ns.aftr_isp);
754 if (ns.peer_isp != os.peer_isp)
755 pbp += sprintf(pbp, "peer_isp( %d -> %d ) ",
756 os.peer_isp,
757 ns.peer_isp);
758 if (ns.user_isp != os.user_isp)
759 pbp += sprintf(pbp, "user_isp( %d -> %d ) ",
760 os.user_isp,
761 ns.user_isp);
Philipp Reisner435693e2011-03-25 15:11:30 +0100762
Philipp Reisner706cb242011-03-29 15:20:27 +0200763 if (pbp != pb)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200764 drbd_info(device, "%s\n", pb);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100765}
Philipp Reisnerb8907332011-01-27 14:07:51 +0100766
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200767static void conn_pr_state_change(struct drbd_connection *connection, union drbd_state os, union drbd_state ns,
Philipp Reisner435693e2011-03-25 15:11:30 +0100768 enum chg_state_flags flags)
769{
770 char pb[300];
Philipp Reisner706cb242011-03-29 15:20:27 +0200771 char *pbp = pb;
Philipp Reisner435693e2011-03-25 15:11:30 +0100772
Philipp Reisner706cb242011-03-29 15:20:27 +0200773 pbp += print_state_change(pbp, os, ns, flags);
774
775 if (is_susp(ns) != is_susp(os) && flags & CS_DC_SUSP)
776 pbp += sprintf(pbp, "susp( %d -> %d ) ",
777 is_susp(os),
778 is_susp(ns));
779
780 if (pbp != pb)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200781 drbd_info(connection, "%s\n", pb);
Philipp Reisner435693e2011-03-25 15:11:30 +0100782}
783
784
Philipp Reisnerb8907332011-01-27 14:07:51 +0100785/**
786 * is_valid_state() - Returns an SS_ error code if ns is not valid
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200787 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100788 * @ns: State to consider.
789 */
790static enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200791is_valid_state(struct drbd_device *device, union drbd_state ns)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100792{
793 /* See drbd_state_sw_errors in drbd_strings.c */
794
795 enum drbd_fencing_p fp;
796 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200797 struct net_conf *nc;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100798
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200799 rcu_read_lock();
Philipp Reisnerb8907332011-01-27 14:07:51 +0100800 fp = FP_DONT_CARE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200801 if (get_ldev(device)) {
802 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
803 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100804 }
805
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200806 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200807 if (nc) {
808 if (!nc->two_primaries && ns.role == R_PRIMARY) {
Philipp Reisner047e95e22011-03-16 14:43:36 +0100809 if (ns.peer == R_PRIMARY)
810 rv = SS_TWO_PRIMARIES;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200811 else if (conn_highest_peer(first_peer_device(device)->connection) == R_PRIMARY)
Philipp Reisner047e95e22011-03-16 14:43:36 +0100812 rv = SS_O_VOL_PEER_PRI;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200813 }
Philipp Reisnerb8907332011-01-27 14:07:51 +0100814 }
815
816 if (rv <= 0)
Roland Kammerer4e526a02016-06-14 00:26:36 +0200817 goto out; /* already found a reason to abort */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200818 else if (ns.role == R_SECONDARY && device->open_cnt)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100819 rv = SS_DEVICE_IN_USE;
820
821 else if (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.disk < D_UP_TO_DATE)
822 rv = SS_NO_UP_TO_DATE_DISK;
823
824 else if (fp >= FP_RESOURCE &&
825 ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
826 rv = SS_PRIMARY_NOP;
827
828 else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
829 rv = SS_NO_UP_TO_DATE_DISK;
830
831 else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
832 rv = SS_NO_LOCAL_DISK;
833
834 else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
835 rv = SS_NO_REMOTE_DISK;
836
837 else if (ns.conn > C_CONNECTED && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
838 rv = SS_NO_UP_TO_DATE_DISK;
839
840 else if ((ns.conn == C_CONNECTED ||
841 ns.conn == C_WF_BITMAP_S ||
842 ns.conn == C_SYNC_SOURCE ||
843 ns.conn == C_PAUSED_SYNC_S) &&
844 ns.disk == D_OUTDATED)
845 rv = SS_CONNECTED_OUTDATES;
846
847 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
Philipp Reisner44ed1672011-04-19 17:10:19 +0200848 (nc->verify_alg[0] == 0))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100849 rv = SS_NO_VERIFY_ALG;
850
851 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200852 first_peer_device(device)->connection->agreed_pro_version < 88)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100853 rv = SS_NOT_SUPPORTED;
854
Philipp Reisner5c4f13d2013-03-27 14:08:37 +0100855 else if (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
856 rv = SS_NO_UP_TO_DATE_DISK;
857
858 else if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
859 ns.pdsk == D_UNKNOWN)
860 rv = SS_NEED_CONNECTION;
861
Philipp Reisnerb8907332011-01-27 14:07:51 +0100862 else if (ns.conn >= C_CONNECTED && ns.pdsk == D_UNKNOWN)
863 rv = SS_CONNECTED_OUTDATES;
864
Roland Kammerer4e526a02016-06-14 00:26:36 +0200865out:
Philipp Reisner44ed1672011-04-19 17:10:19 +0200866 rcu_read_unlock();
867
Philipp Reisnerb8907332011-01-27 14:07:51 +0100868 return rv;
869}
870
871/**
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100872 * is_valid_soft_transition() - Returns an SS_ error code if the state transition is not possible
Philipp Reisner35095022011-02-09 16:29:33 +0100873 * This function limits state transitions that may be declined by DRBD. I.e.
874 * user requests (aka soft transitions).
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200875 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +0100876 * @ns: new state.
877 * @os: old state.
878 */
879static enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200880is_valid_soft_transition(union drbd_state os, union drbd_state ns, struct drbd_connection *connection)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100881{
882 enum drbd_state_rv rv = SS_SUCCESS;
883
884 if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
885 os.conn > C_CONNECTED)
886 rv = SS_RESYNC_RUNNING;
887
888 if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
889 rv = SS_ALREADY_STANDALONE;
890
891 if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
892 rv = SS_IS_DISKLESS;
893
894 if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
895 rv = SS_NO_NET_CONFIG;
896
897 if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
898 rv = SS_LOWER_THAN_OUTDATED;
899
900 if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
901 rv = SS_IN_TRANSIENT_STATE;
902
Philipp Reisnera1096a62012-04-06 12:07:34 +0200903 /* While establishing a connection only allow cstate to change.
Philipp Reisnera8821532014-11-10 17:21:11 +0100904 Delay/refuse role changes, detach attach etc... (they do not touch cstate) */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200905 if (test_bit(STATE_SENT, &connection->flags) &&
Philipp Reisnera8821532014-11-10 17:21:11 +0100906 !((ns.conn == C_WF_REPORT_PARAMS && os.conn == C_WF_CONNECTION) ||
907 (ns.conn >= C_CONNECTED && os.conn == C_WF_REPORT_PARAMS)))
Philipp Reisnera1096a62012-04-06 12:07:34 +0200908 rv = SS_IN_TRANSIENT_STATE;
909
Lars Ellenberg31d64602016-06-14 00:26:33 +0200910 /* Do not promote during resync handshake triggered by "force primary".
911 * This is a hack. It should really be rejected by the peer during the
912 * cluster wide state change request. */
913 if (os.role != R_PRIMARY && ns.role == R_PRIMARY
914 && ns.pdsk == D_UP_TO_DATE
915 && ns.disk != D_UP_TO_DATE && ns.disk != D_DISKLESS
916 && (ns.conn <= C_WF_SYNC_UUID || ns.conn != os.conn))
917 rv = SS_IN_TRANSIENT_STATE;
918
Philipp Reisnerb8907332011-01-27 14:07:51 +0100919 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
920 rv = SS_NEED_CONNECTION;
921
922 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
923 ns.conn != os.conn && os.conn > C_CONNECTED)
924 rv = SS_RESYNC_RUNNING;
925
926 if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
927 os.conn < C_CONNECTED)
928 rv = SS_NEED_CONNECTION;
929
930 if ((ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)
931 && os.conn < C_WF_REPORT_PARAMS)
932 rv = SS_NEED_CONNECTION; /* No NetworkFailure -> SyncTarget etc... */
933
Philipp Reisner2bd5ed52013-03-27 14:08:40 +0100934 if (ns.conn == C_DISCONNECTING && ns.pdsk == D_OUTDATED &&
935 os.conn < C_CONNECTED && os.pdsk > D_OUTDATED)
936 rv = SS_OUTDATE_WO_CONN;
937
Philipp Reisnerb8907332011-01-27 14:07:51 +0100938 return rv;
939}
940
Philipp Reisnerfda74112011-02-10 10:38:06 +0100941static enum drbd_state_rv
942is_valid_conn_transition(enum drbd_conns oc, enum drbd_conns nc)
943{
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200944 /* no change -> nothing to do, at least for the connection part */
945 if (oc == nc)
946 return SS_NOTHING_TO_DO;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100947
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200948 /* disconnect of an unconfigured connection does not make sense */
949 if (oc == C_STANDALONE && nc == C_DISCONNECTING)
950 return SS_ALREADY_STANDALONE;
951
952 /* from C_STANDALONE, we start with C_UNCONNECTED */
953 if (oc == C_STANDALONE && nc != C_UNCONNECTED)
954 return SS_NEED_CONNECTION;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100955
Philipp Reisner25b0d6c2012-02-14 12:12:35 +0100956 /* When establishing a connection we need to go through WF_REPORT_PARAMS!
957 Necessary to do the right thing upon invalidate-remote on a disconnected resource */
958 if (oc < C_WF_REPORT_PARAMS && nc >= C_CONNECTED)
959 return SS_NEED_CONNECTION;
960
Philipp Reisnerfda74112011-02-10 10:38:06 +0100961 /* After a network error only C_UNCONNECTED or C_DISCONNECTING may follow. */
962 if (oc >= C_TIMEOUT && oc <= C_TEAR_DOWN && nc != C_UNCONNECTED && nc != C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200963 return SS_IN_TRANSIENT_STATE;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100964
965 /* After C_DISCONNECTING only C_STANDALONE may follow */
966 if (oc == C_DISCONNECTING && nc != C_STANDALONE)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200967 return SS_IN_TRANSIENT_STATE;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100968
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200969 return SS_SUCCESS;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100970}
971
972
Philipp Reisnerb8907332011-01-27 14:07:51 +0100973/**
Philipp Reisner35095022011-02-09 16:29:33 +0100974 * is_valid_transition() - Returns an SS_ error code if the state transition is not possible
975 * This limits hard state transitions. Hard state transitions are facts there are
976 * imposed on DRBD by the environment. E.g. disk broke or network broke down.
977 * But those hard state transitions are still not allowed to do everything.
978 * @ns: new state.
979 * @os: old state.
980 */
981static enum drbd_state_rv
982is_valid_transition(union drbd_state os, union drbd_state ns)
983{
Philipp Reisnerfda74112011-02-10 10:38:06 +0100984 enum drbd_state_rv rv;
Philipp Reisner35095022011-02-09 16:29:33 +0100985
Philipp Reisnerfda74112011-02-10 10:38:06 +0100986 rv = is_valid_conn_transition(os.conn, ns.conn);
Philipp Reisner35095022011-02-09 16:29:33 +0100987
988 /* we cannot fail (again) if we already detached */
989 if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
990 rv = SS_IS_DISKLESS;
991
992 return rv;
993}
994
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200995static void print_sanitize_warnings(struct drbd_device *device, enum sanitize_state_warnings warn)
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200996{
997 static const char *msg_table[] = {
998 [NO_WARNING] = "",
999 [ABORTED_ONLINE_VERIFY] = "Online-verify aborted.",
1000 [ABORTED_RESYNC] = "Resync aborted.",
1001 [CONNECTION_LOST_NEGOTIATING] = "Connection lost while negotiating, no data!",
1002 [IMPLICITLY_UPGRADED_DISK] = "Implicitly upgraded disk",
1003 [IMPLICITLY_UPGRADED_PDSK] = "Implicitly upgraded pdsk",
1004 };
1005
1006 if (warn != NO_WARNING)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001007 drbd_warn(device, "%s\n", msg_table[warn]);
Philipp Reisnerd942ae42011-05-31 13:07:24 +02001008}
1009
Philipp Reisner35095022011-02-09 16:29:33 +01001010/**
Philipp Reisnerb8907332011-01-27 14:07:51 +01001011 * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001012 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +01001013 * @os: old state.
1014 * @ns: new state.
1015 * @warn_sync_abort:
1016 *
1017 * When we loose connection, we have to set the state of the peers disk (pdsk)
1018 * to D_UNKNOWN. This rule and many more along those lines are in this function.
1019 */
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +02001020static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state os,
1021 union drbd_state ns, enum sanitize_state_warnings *warn)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001022{
1023 enum drbd_fencing_p fp;
1024 enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
1025
Philipp Reisnerd942ae42011-05-31 13:07:24 +02001026 if (warn)
1027 *warn = NO_WARNING;
1028
Philipp Reisnerb8907332011-01-27 14:07:51 +01001029 fp = FP_DONT_CARE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001030 if (get_ldev(device)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001031 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001032 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001033 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001034 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001035 }
1036
Philipp Reisner35095022011-02-09 16:29:33 +01001037 /* Implications from connection to peer and peer_isp */
Philipp Reisnerb8907332011-01-27 14:07:51 +01001038 if (ns.conn < C_CONNECTED) {
1039 ns.peer_isp = 0;
1040 ns.peer = R_UNKNOWN;
1041 if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
1042 ns.pdsk = D_UNKNOWN;
1043 }
1044
1045 /* Clear the aftr_isp when becoming unconfigured */
1046 if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
1047 ns.aftr_isp = 0;
1048
Philipp Reisner4308a0a2011-02-10 11:24:38 +01001049 /* An implication of the disk states onto the connection state */
Philipp Reisnerb8907332011-01-27 14:07:51 +01001050 /* Abort resync if a disk fails/detaches */
Philipp Reisner4308a0a2011-02-10 11:24:38 +01001051 if (ns.conn > C_CONNECTED && (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +02001052 if (warn)
1053 *warn = ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T ?
1054 ABORTED_ONLINE_VERIFY : ABORTED_RESYNC;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001055 ns.conn = C_CONNECTED;
1056 }
1057
1058 /* Connection breaks down before we finished "Negotiating" */
1059 if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001060 get_ldev_if_state(device, D_NEGOTIATING)) {
1061 if (device->ed_uuid == device->ldev->md.uuid[UI_CURRENT]) {
1062 ns.disk = device->new_state_tmp.disk;
1063 ns.pdsk = device->new_state_tmp.pdsk;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001064 } else {
Philipp Reisnerd942ae42011-05-31 13:07:24 +02001065 if (warn)
1066 *warn = CONNECTION_LOST_NEGOTIATING;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001067 ns.disk = D_DISKLESS;
1068 ns.pdsk = D_UNKNOWN;
1069 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001070 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001071 }
1072
1073 /* D_CONSISTENT and D_OUTDATED vanish when we get connected */
1074 if (ns.conn >= C_CONNECTED && ns.conn < C_AHEAD) {
1075 if (ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED)
1076 ns.disk = D_UP_TO_DATE;
1077 if (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)
1078 ns.pdsk = D_UP_TO_DATE;
1079 }
1080
1081 /* Implications of the connection stat on the disk states */
1082 disk_min = D_DISKLESS;
1083 disk_max = D_UP_TO_DATE;
1084 pdsk_min = D_INCONSISTENT;
1085 pdsk_max = D_UNKNOWN;
1086 switch ((enum drbd_conns)ns.conn) {
1087 case C_WF_BITMAP_T:
1088 case C_PAUSED_SYNC_T:
1089 case C_STARTING_SYNC_T:
1090 case C_WF_SYNC_UUID:
1091 case C_BEHIND:
1092 disk_min = D_INCONSISTENT;
1093 disk_max = D_OUTDATED;
1094 pdsk_min = D_UP_TO_DATE;
1095 pdsk_max = D_UP_TO_DATE;
1096 break;
1097 case C_VERIFY_S:
1098 case C_VERIFY_T:
1099 disk_min = D_UP_TO_DATE;
1100 disk_max = D_UP_TO_DATE;
1101 pdsk_min = D_UP_TO_DATE;
1102 pdsk_max = D_UP_TO_DATE;
1103 break;
1104 case C_CONNECTED:
1105 disk_min = D_DISKLESS;
1106 disk_max = D_UP_TO_DATE;
1107 pdsk_min = D_DISKLESS;
1108 pdsk_max = D_UP_TO_DATE;
1109 break;
1110 case C_WF_BITMAP_S:
1111 case C_PAUSED_SYNC_S:
1112 case C_STARTING_SYNC_S:
1113 case C_AHEAD:
1114 disk_min = D_UP_TO_DATE;
1115 disk_max = D_UP_TO_DATE;
1116 pdsk_min = D_INCONSISTENT;
1117 pdsk_max = D_CONSISTENT; /* D_OUTDATED would be nice. But explicit outdate necessary*/
1118 break;
1119 case C_SYNC_TARGET:
1120 disk_min = D_INCONSISTENT;
1121 disk_max = D_INCONSISTENT;
1122 pdsk_min = D_UP_TO_DATE;
1123 pdsk_max = D_UP_TO_DATE;
1124 break;
1125 case C_SYNC_SOURCE:
1126 disk_min = D_UP_TO_DATE;
1127 disk_max = D_UP_TO_DATE;
1128 pdsk_min = D_INCONSISTENT;
1129 pdsk_max = D_INCONSISTENT;
1130 break;
1131 case C_STANDALONE:
1132 case C_DISCONNECTING:
1133 case C_UNCONNECTED:
1134 case C_TIMEOUT:
1135 case C_BROKEN_PIPE:
1136 case C_NETWORK_FAILURE:
1137 case C_PROTOCOL_ERROR:
1138 case C_TEAR_DOWN:
1139 case C_WF_CONNECTION:
1140 case C_WF_REPORT_PARAMS:
1141 case C_MASK:
1142 break;
1143 }
1144 if (ns.disk > disk_max)
1145 ns.disk = disk_max;
1146
1147 if (ns.disk < disk_min) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +02001148 if (warn)
1149 *warn = IMPLICITLY_UPGRADED_DISK;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001150 ns.disk = disk_min;
1151 }
1152 if (ns.pdsk > pdsk_max)
1153 ns.pdsk = pdsk_max;
1154
1155 if (ns.pdsk < pdsk_min) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +02001156 if (warn)
1157 *warn = IMPLICITLY_UPGRADED_PDSK;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001158 ns.pdsk = pdsk_min;
1159 }
1160
1161 if (fp == FP_STONITH &&
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +02001162 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED) &&
1163 !(os.role == R_PRIMARY && os.conn < C_CONNECTED && os.pdsk > D_OUTDATED))
Philipp Reisnerb8907332011-01-27 14:07:51 +01001164 ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
1165
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02001166 if (device->resource->res_opts.on_no_data == OND_SUSPEND_IO &&
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +02001167 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE) &&
1168 !(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE))
Philipp Reisnerb8907332011-01-27 14:07:51 +01001169 ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
1170
1171 if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
1172 if (ns.conn == C_SYNC_SOURCE)
1173 ns.conn = C_PAUSED_SYNC_S;
1174 if (ns.conn == C_SYNC_TARGET)
1175 ns.conn = C_PAUSED_SYNC_T;
1176 } else {
1177 if (ns.conn == C_PAUSED_SYNC_S)
1178 ns.conn = C_SYNC_SOURCE;
1179 if (ns.conn == C_PAUSED_SYNC_T)
1180 ns.conn = C_SYNC_TARGET;
1181 }
1182
1183 return ns;
1184}
1185
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001186void drbd_resume_al(struct drbd_device *device)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001187{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001188 if (test_and_clear_bit(AL_SUSPENDED, &device->flags))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001189 drbd_info(device, "Resumed AL updates\n");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001190}
1191
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001192/* helper for _drbd_set_state */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001193static void set_ov_position(struct drbd_device *device, enum drbd_conns cs)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001194{
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001195 if (first_peer_device(device)->connection->agreed_pro_version < 90)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001196 device->ov_start_sector = 0;
1197 device->rs_total = drbd_bm_bits(device);
1198 device->ov_position = 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001199 if (cs == C_VERIFY_T) {
1200 /* starting online verify from an arbitrary position
1201 * does not fit well into the existing protocol.
1202 * on C_VERIFY_T, we initialize ov_left and friends
1203 * implicitly in receive_DataRequest once the
1204 * first P_OV_REQUEST is received */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001205 device->ov_start_sector = ~(sector_t)0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001206 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001207 unsigned long bit = BM_SECT_TO_BIT(device->ov_start_sector);
1208 if (bit >= device->rs_total) {
1209 device->ov_start_sector =
1210 BM_BIT_TO_SECT(device->rs_total - 1);
1211 device->rs_total = 1;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001212 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001213 device->rs_total -= bit;
1214 device->ov_position = device->ov_start_sector;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001215 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001216 device->ov_left = device->rs_total;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001217}
1218
1219/**
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001220 * _drbd_set_state() - Set a new DRBD state
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001221 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +01001222 * @ns: new state.
1223 * @flags: Flags
1224 * @done: Optional completion, that will get completed after the after_state_ch() finished
1225 *
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001226 * Caller needs to hold req_lock. Do not call directly.
Philipp Reisnerb8907332011-01-27 14:07:51 +01001227 */
1228enum drbd_state_rv
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001229_drbd_set_state(struct drbd_device *device, union drbd_state ns,
1230 enum chg_state_flags flags, struct completion *done)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001231{
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001232 struct drbd_peer_device *peer_device = first_peer_device(device);
1233 struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001234 union drbd_state os;
1235 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerd942ae42011-05-31 13:07:24 +02001236 enum sanitize_state_warnings ssw;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001237 struct after_state_chg_work *ascw;
Andreas Gruenbachera2972842014-07-31 17:41:33 +02001238 struct drbd_state_change *state_change;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001239
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001240 os = drbd_read_state(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001241
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +02001242 ns = sanitize_state(device, os, ns, &ssw);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001243 if (ns.i == os.i)
1244 return SS_NOTHING_TO_DO;
1245
Philipp Reisner35095022011-02-09 16:29:33 +01001246 rv = is_valid_transition(os, ns);
1247 if (rv < SS_SUCCESS)
1248 return rv;
1249
Philipp Reisnerb8907332011-01-27 14:07:51 +01001250 if (!(flags & CS_HARD)) {
1251 /* pre-state-change checks ; only look at ns */
1252 /* See drbd_state_sw_errors in drbd_strings.c */
1253
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001254 rv = is_valid_state(device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001255 if (rv < SS_SUCCESS) {
1256 /* If the old state was illegal as well, then let
1257 this happen...*/
1258
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001259 if (is_valid_state(device, os) == rv)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001260 rv = is_valid_soft_transition(os, ns, connection);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001261 } else
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001262 rv = is_valid_soft_transition(os, ns, connection);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001263 }
1264
1265 if (rv < SS_SUCCESS) {
1266 if (flags & CS_VERBOSE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001267 print_st_err(device, os, ns, rv);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001268 return rv;
1269 }
1270
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001271 print_sanitize_warnings(device, ssw);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001272
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001273 drbd_pr_state_change(device, os, ns, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001274
Philipp Reisner706cb242011-03-29 15:20:27 +02001275 /* Display changes to the susp* flags that where caused by the call to
1276 sanitize_state(). Only display it here if we where not called from
1277 _conn_request_state() */
1278 if (!(flags & CS_DC_SUSP))
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001279 conn_pr_state_change(connection, os, ns,
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001280 (flags & ~CS_DC_MASK) | CS_DC_SUSP);
Philipp Reisner706cb242011-03-29 15:20:27 +02001281
Philipp Reisnerb8907332011-01-27 14:07:51 +01001282 /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
1283 * on the ldev here, to be sure the transition -> D_DISKLESS resp.
1284 * drbd_ldev_destroy() won't happen before our corresponding
1285 * after_state_ch works run, where we put_ldev again. */
1286 if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
1287 (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001288 atomic_inc(&device->local_cnt);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001289
Lars Ellenberg5ab7d2c2014-01-27 15:58:22 +01001290 if (!is_sync_state(os.conn) && is_sync_state(ns.conn))
1291 clear_bit(RS_DONE, &device->flags);
1292
Andreas Gruenbachera2972842014-07-31 17:41:33 +02001293 /* FIXME: Have any flags been set earlier in this function already? */
1294 state_change = remember_old_state(device->resource, GFP_ATOMIC);
1295
Lars Ellenbergba3c6fb2014-02-11 08:57:18 +01001296 /* changes to local_cnt and device flags should be visible before
1297 * changes to state, which again should be visible before anything else
1298 * depending on that change happens. */
1299 smp_wmb();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001300 device->state.i = ns.i;
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02001301 device->resource->susp = ns.susp;
1302 device->resource->susp_nod = ns.susp_nod;
1303 device->resource->susp_fen = ns.susp_fen;
Lars Ellenbergba3c6fb2014-02-11 08:57:18 +01001304 smp_wmb();
Philipp Reisnerb8907332011-01-27 14:07:51 +01001305
Andreas Gruenbachera2972842014-07-31 17:41:33 +02001306 remember_new_state(state_change);
1307
Lars Ellenberg2681f7f2013-01-21 15:43:41 +01001308 /* put replicated vs not-replicated requests in seperate epochs */
Lars Ellenbergba3c6fb2014-02-11 08:57:18 +01001309 if (drbd_should_do_remote((union drbd_dev_state)os.i) !=
1310 drbd_should_do_remote((union drbd_dev_state)ns.i))
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001311 start_new_tl_epoch(connection);
Lars Ellenberg2681f7f2013-01-21 15:43:41 +01001312
Philipp Reisnerb8907332011-01-27 14:07:51 +01001313 if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001314 drbd_print_uuids(device, "attached to UUIDs");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001315
Philipp Reisner79702012012-08-28 11:33:35 +02001316 /* Wake up role changes, that were delayed because of connection establishing */
1317 if (os.conn == C_WF_REPORT_PARAMS && ns.conn != C_WF_REPORT_PARAMS &&
Philipp Reisnera8821532014-11-10 17:21:11 +01001318 no_peer_wf_report_params(connection)) {
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001319 clear_bit(STATE_SENT, &connection->flags);
Philipp Reisnera8821532014-11-10 17:21:11 +01001320 wake_up_all_devices(connection);
1321 }
Philipp Reisner79702012012-08-28 11:33:35 +02001322
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001323 wake_up(&device->misc_wait);
1324 wake_up(&device->state_wait);
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001325 wake_up(&connection->ping_wait);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001326
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001327 /* Aborted verify run, or we reached the stop sector.
1328 * Log the last position, unless end-of-device. */
Philipp Reisnerb8907332011-01-27 14:07:51 +01001329 if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001330 ns.conn <= C_CONNECTED) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001331 device->ov_start_sector =
1332 BM_BIT_TO_SECT(drbd_bm_bits(device) - device->ov_left);
1333 if (device->ov_left)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001334 drbd_info(device, "Online Verify reached sector %llu\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001335 (unsigned long long)device->ov_start_sector);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001336 }
1337
1338 if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
1339 (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001340 drbd_info(device, "Syncer continues.\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001341 device->rs_paused += (long)jiffies
1342 -(long)device->rs_mark_time[device->rs_last_mark];
Philipp Reisnerb8907332011-01-27 14:07:51 +01001343 if (ns.conn == C_SYNC_TARGET)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001344 mod_timer(&device->resync_timer, jiffies);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001345 }
1346
1347 if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
1348 (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001349 drbd_info(device, "Resync suspended\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001350 device->rs_mark_time[device->rs_last_mark] = jiffies;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001351 }
1352
1353 if (os.conn == C_CONNECTED &&
1354 (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
1355 unsigned long now = jiffies;
1356 int i;
1357
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001358 set_ov_position(device, ns.conn);
1359 device->rs_start = now;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001360 device->rs_last_sect_ev = 0;
1361 device->ov_last_oos_size = 0;
1362 device->ov_last_oos_start = 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001363
1364 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001365 device->rs_mark_left[i] = device->ov_left;
1366 device->rs_mark_time[i] = now;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001367 }
1368
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001369 drbd_rs_controller_reset(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001370
1371 if (ns.conn == C_VERIFY_S) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001372 drbd_info(device, "Starting Online Verify from sector %llu\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001373 (unsigned long long)device->ov_position);
1374 mod_timer(&device->resync_timer, jiffies);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001375 }
1376 }
1377
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001378 if (get_ldev(device)) {
1379 u32 mdf = device->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
Philipp Reisnerb8907332011-01-27 14:07:51 +01001380 MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
1381 MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
1382
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02001383 mdf &= ~MDF_AL_CLEAN;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001384 if (test_bit(CRASHED_PRIMARY, &device->flags))
Philipp Reisnerb8907332011-01-27 14:07:51 +01001385 mdf |= MDF_CRASHED_PRIMARY;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001386 if (device->state.role == R_PRIMARY ||
1387 (device->state.pdsk < D_INCONSISTENT && device->state.peer == R_PRIMARY))
Philipp Reisnerb8907332011-01-27 14:07:51 +01001388 mdf |= MDF_PRIMARY_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001389 if (device->state.conn > C_WF_REPORT_PARAMS)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001390 mdf |= MDF_CONNECTED_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001391 if (device->state.disk > D_INCONSISTENT)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001392 mdf |= MDF_CONSISTENT;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001393 if (device->state.disk > D_OUTDATED)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001394 mdf |= MDF_WAS_UP_TO_DATE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001395 if (device->state.pdsk <= D_OUTDATED && device->state.pdsk >= D_INCONSISTENT)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001396 mdf |= MDF_PEER_OUT_DATED;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001397 if (mdf != device->ldev->md.flags) {
1398 device->ldev->md.flags = mdf;
1399 drbd_md_mark_dirty(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001400 }
1401 if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001402 drbd_set_ed_uuid(device, device->ldev->md.uuid[UI_CURRENT]);
1403 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001404 }
1405
1406 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
1407 if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
1408 os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001409 set_bit(CONSIDER_RESYNC, &device->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001410
1411 /* Receiver should clean up itself */
1412 if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001413 drbd_thread_stop_nowait(&connection->receiver);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001414
1415 /* Now the receiver finished cleaning up itself, it should die */
1416 if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001417 drbd_thread_stop_nowait(&connection->receiver);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001418
1419 /* Upon network failure, we need to restart the receiver. */
Philipp Reisner823bd832012-11-08 15:04:36 +01001420 if (os.conn > C_WF_CONNECTION &&
Philipp Reisnerb8907332011-01-27 14:07:51 +01001421 ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001422 drbd_thread_restart_nowait(&connection->receiver);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001423
1424 /* Resume AL writing if we get a connection */
Philipp Reisner28e448b2013-06-25 16:50:06 +02001425 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001426 drbd_resume_al(device);
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001427 connection->connect_cnt++;
Philipp Reisner28e448b2013-06-25 16:50:06 +02001428 }
Philipp Reisnerb8907332011-01-27 14:07:51 +01001429
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001430 /* remember last attach time so request_timer_fn() won't
1431 * kill newly established sessions while we are still trying to thaw
1432 * previously frozen IO */
1433 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
1434 ns.disk > D_NEGOTIATING)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001435 device->last_reattach_jif = jiffies;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001436
Philipp Reisnerb8907332011-01-27 14:07:51 +01001437 ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
1438 if (ascw) {
1439 ascw->os = os;
1440 ascw->ns = ns;
1441 ascw->flags = flags;
1442 ascw->w.cb = w_after_state_ch;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001443 ascw->device = device;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001444 ascw->done = done;
Andreas Gruenbachera2972842014-07-31 17:41:33 +02001445 ascw->state_change = state_change;
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001446 drbd_queue_work(&connection->sender_work,
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001447 &ascw->w);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001448 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001449 drbd_err(device, "Could not kmalloc an ascw\n");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001450 }
1451
1452 return rv;
1453}
1454
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001455static int w_after_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001456{
1457 struct after_state_chg_work *ascw =
1458 container_of(w, struct after_state_chg_work, w);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001459 struct drbd_device *device = ascw->device;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001460
Andreas Gruenbachera2972842014-07-31 17:41:33 +02001461 after_state_ch(device, ascw->os, ascw->ns, ascw->flags, ascw->state_change);
1462 forget_state_change(ascw->state_change);
Andreas Gruenbacher137975c2011-08-24 08:19:41 +02001463 if (ascw->flags & CS_WAIT_COMPLETE)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001464 complete(ascw->done);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001465 kfree(ascw);
1466
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001467 return 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001468}
1469
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001470static void abw_start_sync(struct drbd_device *device, int rv)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001471{
1472 if (rv) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001473 drbd_err(device, "Writing the bitmap failed not starting resync.\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001474 _drbd_request_state(device, NS(conn, C_CONNECTED), CS_VERBOSE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001475 return;
1476 }
1477
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001478 switch (device->state.conn) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001479 case C_STARTING_SYNC_T:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001480 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001481 break;
1482 case C_STARTING_SYNC_S:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001483 drbd_start_resync(device, C_SYNC_SOURCE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001484 break;
1485 }
1486}
1487
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001488int drbd_bitmap_io_from_worker(struct drbd_device *device,
Andreas Gruenbacher54761692011-05-30 16:15:21 +02001489 int (*io_fn)(struct drbd_device *),
Philipp Reisnerb8907332011-01-27 14:07:51 +01001490 char *why, enum bm_flag flags)
1491{
1492 int rv;
1493
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001494 D_ASSERT(device, current == first_peer_device(device)->connection->worker.task);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001495
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001496 /* open coded non-blocking drbd_suspend_io(device); */
Philipp Reisner7dbb4382013-02-28 10:30:19 +01001497 atomic_inc(&device->suspend_cnt);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001498
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001499 drbd_bm_lock(device, why, flags);
1500 rv = io_fn(device);
1501 drbd_bm_unlock(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001502
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001503 drbd_resume_io(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001504
1505 return rv;
1506}
1507
Andreas Gruenbachera2972842014-07-31 17:41:33 +02001508void notify_resource_state_change(struct sk_buff *skb,
1509 unsigned int seq,
1510 struct drbd_resource_state_change *resource_state_change,
1511 enum drbd_notification_type type)
1512{
1513 struct drbd_resource *resource = resource_state_change->resource;
1514 struct resource_info resource_info = {
1515 .res_role = resource_state_change->role[NEW],
1516 .res_susp = resource_state_change->susp[NEW],
1517 .res_susp_nod = resource_state_change->susp_nod[NEW],
1518 .res_susp_fen = resource_state_change->susp_fen[NEW],
1519 };
1520
1521 notify_resource_state(skb, seq, resource, &resource_info, type);
1522}
1523
1524void notify_connection_state_change(struct sk_buff *skb,
1525 unsigned int seq,
1526 struct drbd_connection_state_change *connection_state_change,
1527 enum drbd_notification_type type)
1528{
1529 struct drbd_connection *connection = connection_state_change->connection;
1530 struct connection_info connection_info = {
1531 .conn_connection_state = connection_state_change->cstate[NEW],
1532 .conn_role = connection_state_change->peer_role[NEW],
1533 };
1534
1535 notify_connection_state(skb, seq, connection, &connection_info, type);
1536}
1537
1538void notify_device_state_change(struct sk_buff *skb,
1539 unsigned int seq,
1540 struct drbd_device_state_change *device_state_change,
1541 enum drbd_notification_type type)
1542{
1543 struct drbd_device *device = device_state_change->device;
1544 struct device_info device_info = {
1545 .dev_disk_state = device_state_change->disk_state[NEW],
1546 };
1547
1548 notify_device_state(skb, seq, device, &device_info, type);
1549}
1550
1551void notify_peer_device_state_change(struct sk_buff *skb,
1552 unsigned int seq,
1553 struct drbd_peer_device_state_change *p,
1554 enum drbd_notification_type type)
1555{
1556 struct drbd_peer_device *peer_device = p->peer_device;
1557 struct peer_device_info peer_device_info = {
1558 .peer_repl_state = p->repl_state[NEW],
1559 .peer_disk_state = p->disk_state[NEW],
1560 .peer_resync_susp_user = p->resync_susp_user[NEW],
1561 .peer_resync_susp_peer = p->resync_susp_peer[NEW],
1562 .peer_resync_susp_dependency = p->resync_susp_dependency[NEW],
1563 };
1564
1565 notify_peer_device_state(skb, seq, peer_device, &peer_device_info, type);
1566}
1567
1568static void broadcast_state_change(struct drbd_state_change *state_change)
1569{
1570 struct drbd_resource_state_change *resource_state_change = &state_change->resource[0];
1571 bool resource_state_has_changed;
1572 unsigned int n_device, n_connection, n_peer_device, n_peer_devices;
1573 void (*last_func)(struct sk_buff *, unsigned int, void *,
1574 enum drbd_notification_type) = NULL;
1575 void *uninitialized_var(last_arg);
1576
1577#define HAS_CHANGED(state) ((state)[OLD] != (state)[NEW])
1578#define FINAL_STATE_CHANGE(type) \
1579 ({ if (last_func) \
1580 last_func(NULL, 0, last_arg, type); \
1581 })
1582#define REMEMBER_STATE_CHANGE(func, arg, type) \
1583 ({ FINAL_STATE_CHANGE(type | NOTIFY_CONTINUES); \
1584 last_func = (typeof(last_func))func; \
1585 last_arg = arg; \
1586 })
1587
1588 mutex_lock(&notification_mutex);
1589
1590 resource_state_has_changed =
1591 HAS_CHANGED(resource_state_change->role) ||
1592 HAS_CHANGED(resource_state_change->susp) ||
1593 HAS_CHANGED(resource_state_change->susp_nod) ||
1594 HAS_CHANGED(resource_state_change->susp_fen);
1595
1596 if (resource_state_has_changed)
1597 REMEMBER_STATE_CHANGE(notify_resource_state_change,
1598 resource_state_change, NOTIFY_CHANGE);
1599
1600 for (n_connection = 0; n_connection < state_change->n_connections; n_connection++) {
1601 struct drbd_connection_state_change *connection_state_change =
1602 &state_change->connections[n_connection];
1603
1604 if (HAS_CHANGED(connection_state_change->peer_role) ||
1605 HAS_CHANGED(connection_state_change->cstate))
1606 REMEMBER_STATE_CHANGE(notify_connection_state_change,
1607 connection_state_change, NOTIFY_CHANGE);
1608 }
1609
1610 for (n_device = 0; n_device < state_change->n_devices; n_device++) {
1611 struct drbd_device_state_change *device_state_change =
1612 &state_change->devices[n_device];
1613
1614 if (HAS_CHANGED(device_state_change->disk_state))
1615 REMEMBER_STATE_CHANGE(notify_device_state_change,
1616 device_state_change, NOTIFY_CHANGE);
1617 }
1618
1619 n_peer_devices = state_change->n_devices * state_change->n_connections;
1620 for (n_peer_device = 0; n_peer_device < n_peer_devices; n_peer_device++) {
1621 struct drbd_peer_device_state_change *p =
1622 &state_change->peer_devices[n_peer_device];
1623
1624 if (HAS_CHANGED(p->disk_state) ||
1625 HAS_CHANGED(p->repl_state) ||
1626 HAS_CHANGED(p->resync_susp_user) ||
1627 HAS_CHANGED(p->resync_susp_peer) ||
1628 HAS_CHANGED(p->resync_susp_dependency))
1629 REMEMBER_STATE_CHANGE(notify_peer_device_state_change,
1630 p, NOTIFY_CHANGE);
1631 }
1632
1633 FINAL_STATE_CHANGE(NOTIFY_CHANGE);
1634 mutex_unlock(&notification_mutex);
1635
1636#undef HAS_CHANGED
1637#undef FINAL_STATE_CHANGE
1638#undef REMEMBER_STATE_CHANGE
1639}
1640
Lars Ellenberg20004e22016-06-14 00:26:34 +02001641/* takes old and new peer disk state */
1642static bool lost_contact_to_peer_data(enum drbd_disk_state os, enum drbd_disk_state ns)
1643{
1644 if ((os >= D_INCONSISTENT && os != D_UNKNOWN && os != D_OUTDATED)
1645 && (ns < D_INCONSISTENT || ns == D_UNKNOWN || ns == D_OUTDATED))
1646 return true;
1647
1648 /* Scenario, starting with normal operation
1649 * Connected Primary/Secondary UpToDate/UpToDate
1650 * NetworkFailure Primary/Unknown UpToDate/DUnknown (frozen)
1651 * ...
1652 * Connected Primary/Secondary UpToDate/Diskless (resumed; needs to bump uuid!)
1653 */
1654 if (os == D_UNKNOWN
1655 && (ns == D_DISKLESS || ns == D_FAILED || ns == D_OUTDATED))
1656 return true;
1657
1658 return false;
1659}
1660
Philipp Reisnerb8907332011-01-27 14:07:51 +01001661/**
1662 * after_state_ch() - Perform after state change actions that may sleep
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001663 * @device: DRBD device.
Philipp Reisnerb8907332011-01-27 14:07:51 +01001664 * @os: old state.
1665 * @ns: new state.
1666 * @flags: Flags
1667 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001668static void after_state_ch(struct drbd_device *device, union drbd_state os,
Andreas Gruenbachera2972842014-07-31 17:41:33 +02001669 union drbd_state ns, enum chg_state_flags flags,
1670 struct drbd_state_change *state_change)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001671{
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02001672 struct drbd_resource *resource = device->resource;
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001673 struct drbd_peer_device *peer_device = first_peer_device(device);
1674 struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001675 struct sib_info sib;
1676
Andreas Gruenbachera2972842014-07-31 17:41:33 +02001677 broadcast_state_change(state_change);
1678
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001679 sib.sib_reason = SIB_STATE_CHANGE;
1680 sib.os = os;
1681 sib.ns = ns;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001682
Lars Ellenberg2f2abea2014-03-27 10:34:13 +01001683 if ((os.disk != D_UP_TO_DATE || os.pdsk != D_UP_TO_DATE)
1684 && (ns.disk == D_UP_TO_DATE && ns.pdsk == D_UP_TO_DATE)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001685 clear_bit(CRASHED_PRIMARY, &device->flags);
1686 if (device->p_uuid)
1687 device->p_uuid[UI_FLAGS] &= ~((u64)2);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001688 }
1689
Philipp Reisnerb8907332011-01-27 14:07:51 +01001690 /* Inform userspace about the change... */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001691 drbd_bcast_event(device, &sib);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001692
1693 if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
1694 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001695 drbd_khelper(device, "pri-on-incon-degr");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001696
1697 /* Here we have the actions that are performed after a
1698 state change. This function might sleep */
1699
Philipp Reisnerb8907332011-01-27 14:07:51 +01001700 if (ns.susp_nod) {
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001701 enum drbd_req_event what = NOTHING;
1702
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001703 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001704 if (os.conn < C_CONNECTED && conn_lowest_conn(connection) >= C_CONNECTED)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001705 what = RESEND;
1706
Philipp Reisner3fb47462011-07-15 18:44:26 +02001707 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
Lars Ellenbergaf614942016-06-14 00:26:28 +02001708 conn_lowest_disk(connection) == D_UP_TO_DATE)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001709 what = RESTART_FROZEN_DISK_IO;
1710
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02001711 if (resource->susp_nod && what != NOTHING) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001712 _tl_restart(connection, what);
1713 _conn_request_state(connection,
Philipp Reisner892fdd12012-08-27 17:20:12 +02001714 (union drbd_state) { { .susp_nod = 1 } },
1715 (union drbd_state) { { .susp_nod = 0 } },
1716 CS_VERBOSE);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001717 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001718 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001719 }
1720
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001721 if (ns.susp_fen) {
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001722 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02001723 if (resource->susp_fen && conn_lowest_conn(connection) >= C_CONNECTED) {
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001724 /* case2: The connection was established again: */
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001725 struct drbd_peer_device *peer_device;
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001726 int vnr;
1727
1728 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001729 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
1730 clear_bit(NEW_CUR_UUID, &peer_device->device->flags);
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001731 rcu_read_unlock();
Lars Ellenberg20004e22016-06-14 00:26:34 +02001732
1733 /* We should actively create a new uuid, _before_
1734 * we resume/resent, if the peer is diskless
1735 * (recovery from a multiple error scenario).
1736 * Currently, this happens with a slight delay
1737 * below when checking lost_contact_to_peer_data() ...
1738 */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001739 _tl_restart(connection, RESEND);
1740 _conn_request_state(connection,
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001741 (union drbd_state) { { .susp_fen = 1 } },
1742 (union drbd_state) { { .susp_fen = 0 } },
1743 CS_VERBOSE);
1744 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001745 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisner88f79ec2012-08-27 17:16:21 +02001746 }
1747
Philipp Reisnerb8907332011-01-27 14:07:51 +01001748 /* Became sync source. With protocol >= 96, we still need to send out
1749 * the sync uuid now. Need to do that before any drbd_send_state, or
1750 * the other side may go "paused sync" before receiving the sync uuids,
1751 * which is unexpected. */
1752 if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
1753 (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001754 connection->agreed_pro_version >= 96 && get_ldev(device)) {
1755 drbd_gen_and_send_sync_uuid(peer_device);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001756 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001757 }
1758
1759 /* Do not change the order of the if above and the two below... */
Philipp Reisner369bea62011-07-06 23:04:44 +02001760 if (os.pdsk == D_DISKLESS &&
1761 ns.pdsk > D_DISKLESS && ns.pdsk != D_UNKNOWN) { /* attach on the peer */
Lars Ellenberga3248962012-07-30 09:10:41 +02001762 /* we probably will start a resync soon.
1763 * make sure those things are properly reset. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001764 device->rs_total = 0;
1765 device->rs_failed = 0;
1766 atomic_set(&device->rs_pending_cnt, 0);
1767 drbd_rs_cancel_all(device);
Lars Ellenberga3248962012-07-30 09:10:41 +02001768
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001769 drbd_send_uuids(peer_device);
1770 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001771 }
1772 /* No point in queuing send_bitmap if we don't have a connection
1773 * anymore, so check also the _current_ state, not only the new state
1774 * at the time this work was queued. */
1775 if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001776 device->state.conn == C_WF_BITMAP_S)
1777 drbd_queue_bitmap_io(device, &drbd_send_bitmap, NULL,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001778 "send_bitmap (WFBitMapS)",
1779 BM_LOCKED_TEST_ALLOWED);
1780
1781 /* Lost contact to peer's copy of the data */
Lars Ellenberg20004e22016-06-14 00:26:34 +02001782 if (lost_contact_to_peer_data(os.pdsk, ns.pdsk)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001783 if (get_ldev(device)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001784 if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001785 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1786 if (drbd_suspended(device)) {
1787 set_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001788 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001789 drbd_uuid_new_current(device);
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001790 drbd_send_uuids(peer_device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001791 }
1792 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001793 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001794 }
1795 }
1796
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001797 if (ns.pdsk < D_INCONSISTENT && get_ldev(device)) {
Lars Ellenberg9bd2eb22015-02-19 14:01:50 +01001798 if (os.peer != R_PRIMARY && ns.peer == R_PRIMARY &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001799 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1800 drbd_uuid_new_current(device);
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001801 drbd_send_uuids(peer_device);
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001802 }
Philipp Reisnerb8907332011-01-27 14:07:51 +01001803 /* D_DISKLESS Peer becomes secondary */
1804 if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
1805 /* We may still be Primary ourselves.
1806 * No harm done if the bitmap still changes,
1807 * redirtied pages will follow later. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001808 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001809 "demote diskless peer", BM_LOCKED_SET_ALLOWED);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001810 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001811 }
1812
1813 /* Write out all changed bits on demote.
1814 * Though, no need to da that just yet
1815 * if there is a resync going on still */
1816 if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001817 device->state.conn <= C_CONNECTED && get_ldev(device)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001818 /* No changes to the bitmap expected this time, so assert that,
1819 * even though no harm was done if it did change. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001820 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001821 "demote", BM_LOCKED_TEST_ALLOWED);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001822 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001823 }
1824
1825 /* Last part of the attaching process ... */
1826 if (ns.conn >= C_CONNECTED &&
1827 os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001828 drbd_send_sizes(peer_device, 0, 0); /* to start sync... */
1829 drbd_send_uuids(peer_device);
1830 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001831 }
1832
1833 /* We want to pause/continue resync, tell peer. */
1834 if (ns.conn >= C_CONNECTED &&
1835 ((os.aftr_isp != ns.aftr_isp) ||
1836 (os.user_isp != ns.user_isp)))
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001837 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001838
1839 /* In case one of the isp bits got set, suspend other devices. */
1840 if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
1841 (ns.aftr_isp || ns.peer_isp || ns.user_isp))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001842 suspend_other_sg(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001843
1844 /* Make sure the peer gets informed about eventual state
1845 changes (ISP bits) while we were in WFReportParams. */
1846 if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001847 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001848
1849 if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001850 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001851
1852 /* We are in the progress to start a full sync... */
1853 if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
1854 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
1855 /* no other bitmap changes expected during this phase */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001856 drbd_queue_bitmap_io(device,
Philipp Reisnerb8907332011-01-27 14:07:51 +01001857 &drbd_bmio_set_n_write, &abw_start_sync,
1858 "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
1859
Philipp Reisnerb8907332011-01-27 14:07:51 +01001860 /* first half of local IO error, failure to attach,
1861 * or administrative detach */
1862 if (os.disk != D_FAILED && ns.disk == D_FAILED) {
Philipp Reisner32db80f2012-02-22 11:51:57 +01001863 enum drbd_io_error_p eh = EP_PASS_ON;
1864 int was_io_error = 0;
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001865 /* corresponding get_ldev was in _drbd_set_state, to serialize
Philipp Reisner32db80f2012-02-22 11:51:57 +01001866 * our cleanup here with the transition to D_DISKLESS.
1867 * But is is still not save to dreference ldev here, since
1868 * we might come from an failed Attach before ldev was set. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001869 if (device->ldev) {
Philipp Reisner32db80f2012-02-22 11:51:57 +01001870 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001871 eh = rcu_dereference(device->ldev->disk_conf)->on_io_error;
Philipp Reisner32db80f2012-02-22 11:51:57 +01001872 rcu_read_unlock();
Philipp Reisnerb8907332011-01-27 14:07:51 +01001873
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001874 was_io_error = test_and_clear_bit(WAS_IO_ERROR, &device->flags);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001875
Lars Ellenbergdc995622015-02-19 13:43:55 +01001876 /* Intentionally call this handler first, before drbd_send_state().
1877 * See: 2932204 drbd: call local-io-error handler early
1878 * People may chose to hard-reset the box from this handler.
1879 * It is useful if this looks like a "regular node crash". */
Lars Ellenberg6f1a6562012-07-30 09:11:01 +02001880 if (was_io_error && eh == EP_CALL_HELPER)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001881 drbd_khelper(device, "local-io-error");
Lars Ellenberg6f1a6562012-07-30 09:11:01 +02001882
Lars Ellenberg0c849662012-07-30 09:07:28 +02001883 /* Immediately allow completion of all application IO,
1884 * that waits for completion from the local disk,
1885 * if this was a force-detach due to disk_timeout
1886 * or administrator request (drbdsetup detach --force).
1887 * Do NOT abort otherwise.
1888 * Aborting local requests may cause serious problems,
1889 * if requests are completed to upper layers already,
1890 * and then later the already submitted local bio completes.
1891 * This can cause DMA into former bio pages that meanwhile
1892 * have been re-used for other things.
1893 * So aborting local requests may cause crashes,
1894 * or even worse, silent data corruption.
1895 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001896 if (test_and_clear_bit(FORCE_DETACH, &device->flags))
1897 tl_abort_disk_io(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001898
Philipp Reisner32db80f2012-02-22 11:51:57 +01001899 /* current state still has to be D_FAILED,
1900 * there is only one way out: to D_DISKLESS,
1901 * and that may only happen after our put_ldev below. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001902 if (device->state.disk != D_FAILED)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001903 drbd_err(device,
Philipp Reisner32db80f2012-02-22 11:51:57 +01001904 "ASSERT FAILED: disk is %s during detach\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001905 drbd_disk_str(device->state.disk));
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001906
Philipp Reisner32db80f2012-02-22 11:51:57 +01001907 if (ns.conn >= C_CONNECTED)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001908 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001909
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001910 drbd_rs_cancel_all(device);
Philipp Reisner32db80f2012-02-22 11:51:57 +01001911
1912 /* In case we want to get something to stable storage still,
1913 * this may be the last chance.
1914 * Following put_ldev may transition to D_DISKLESS. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001915 drbd_md_sync(device);
Philipp Reisner32db80f2012-02-22 11:51:57 +01001916 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001917 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001918 }
1919
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001920 /* second half of local IO error, failure to attach,
1921 * or administrative detach,
1922 * after local_cnt references have reached zero again */
1923 if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
1924 /* We must still be diskless,
1925 * re-attach has to be serialized with this! */
1926 if (device->state.disk != D_DISKLESS)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001927 drbd_err(device,
1928 "ASSERT FAILED: disk is %s while going diskless\n",
1929 drbd_disk_str(device->state.disk));
Philipp Reisnerb8907332011-01-27 14:07:51 +01001930
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001931 if (ns.conn >= C_CONNECTED)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001932 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001933 /* corresponding get_ldev in __drbd_set_state
1934 * this may finally trigger drbd_ldev_destroy. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001935 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001936 }
1937
1938 /* Notify peer that I had a local IO error, and did not detached.. */
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001939 if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT && ns.conn >= C_CONNECTED)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001940 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001941
1942 /* Disks got bigger while they were detached */
1943 if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001944 test_and_clear_bit(RESYNC_AFTER_NEG, &device->flags)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001945 if (ns.conn == C_CONNECTED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001946 resync_after_online_grow(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001947 }
1948
1949 /* A resync finished or aborted, wake paused devices... */
1950 if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
1951 (os.peer_isp && !ns.peer_isp) ||
1952 (os.user_isp && !ns.user_isp))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001953 resume_next_sg(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001954
1955 /* sync target done with resync. Explicitly notify peer, even though
1956 * it should (at least for non-empty resyncs) already know itself. */
1957 if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001958 drbd_send_state(peer_device, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001959
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001960 /* Verify finished, or reached stop sector. Peer did not know about
1961 * the stop sector, and we may even have changed the stop sector during
1962 * verify to interrupt/stop early. Send the new state. */
1963 if (os.conn == C_VERIFY_S && ns.conn == C_CONNECTED
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001964 && verify_can_do_stop_sector(device))
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001965 drbd_send_state(peer_device, ns);
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001966
Philipp Reisnerb8907332011-01-27 14:07:51 +01001967 /* This triggers bitmap writeout of potentially still unwritten pages
1968 * if the resync finished cleanly, or aborted because of peer disk
Lars Ellenbergbe115b62016-06-14 00:26:11 +02001969 * failure, or on transition from resync back to AHEAD/BEHIND.
1970 *
1971 * Connection loss is handled in drbd_disconnected() by the receiver.
1972 *
Philipp Reisnerb8907332011-01-27 14:07:51 +01001973 * For resync aborted because of local disk failure, we cannot do
1974 * any bitmap writeout anymore.
Lars Ellenbergbe115b62016-06-14 00:26:11 +02001975 *
Philipp Reisnerb8907332011-01-27 14:07:51 +01001976 * No harm done if some bits change during this phase.
1977 */
Lars Ellenbergbe115b62016-06-14 00:26:11 +02001978 if ((os.conn > C_CONNECTED && os.conn < C_AHEAD) &&
1979 (ns.conn == C_CONNECTED || ns.conn >= C_AHEAD) && get_ldev(device)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001980 drbd_queue_bitmap_io(device, &drbd_bm_write_copy_pages, NULL,
Lars Ellenberga220d292012-05-07 12:07:18 +02001981 "write from resync_finished", BM_LOCKED_CHANGE_ALLOWED);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001982 put_ldev(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001983 }
1984
1985 if (ns.disk == D_DISKLESS &&
1986 ns.conn == C_STANDALONE &&
1987 ns.role == R_SECONDARY) {
1988 if (os.aftr_isp != ns.aftr_isp)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001989 resume_next_sg(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001990 }
1991
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001992 drbd_md_sync(device);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001993}
1994
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001995struct after_conn_state_chg_work {
Andreas Gruenbacher4c007602011-08-25 15:14:48 +02001996 struct drbd_work w;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001997 enum drbd_conns oc;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001998 union drbd_state ns_min;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001999 union drbd_state ns_max; /* new, max state, over all devices */
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002000 enum chg_state_flags flags;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02002001 struct drbd_connection *connection;
Andreas Gruenbachera2972842014-07-31 17:41:33 +02002002 struct drbd_state_change *state_change;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002003};
Philipp Reisnerb8907332011-01-27 14:07:51 +01002004
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01002005static int w_after_conn_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002006{
2007 struct after_conn_state_chg_work *acscw =
Andreas Gruenbacher4c007602011-08-25 15:14:48 +02002008 container_of(w, struct after_conn_state_chg_work, w);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02002009 struct drbd_connection *connection = acscw->connection;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002010 enum drbd_conns oc = acscw->oc;
Philipp Reisner5f082f92011-03-29 13:20:58 +02002011 union drbd_state ns_max = acscw->ns_max;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002012 struct drbd_peer_device *peer_device;
Philipp Reisnera6d00c82011-03-29 18:16:11 +02002013 int vnr;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002014
Andreas Gruenbachera2972842014-07-31 17:41:33 +02002015 broadcast_state_change(acscw->state_change);
2016 forget_state_change(acscw->state_change);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002017 kfree(acscw);
2018
2019 /* Upon network configuration, we need to start the receiver */
Philipp Reisner5f082f92011-03-29 13:20:58 +02002020 if (oc == C_STANDALONE && ns_max.conn == C_UNCONNECTED)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002021 drbd_thread_start(&connection->receiver);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002022
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002023 if (oc == C_DISCONNECTING && ns_max.conn == C_STANDALONE) {
2024 struct net_conf *old_conf;
2025
Andreas Gruenbachera2972842014-07-31 17:41:33 +02002026 mutex_lock(&notification_mutex);
2027 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
2028 notify_peer_device_state(NULL, 0, peer_device, NULL,
2029 NOTIFY_DESTROY | NOTIFY_CONTINUES);
2030 notify_connection_state(NULL, 0, connection, NULL, NOTIFY_DESTROY);
2031 mutex_unlock(&notification_mutex);
2032
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002033 mutex_lock(&connection->resource->conf_update);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002034 old_conf = connection->net_conf;
2035 connection->my_addr_len = 0;
2036 connection->peer_addr_len = 0;
Monam Agarwalccdd6a92014-03-23 02:02:29 +05302037 RCU_INIT_POINTER(connection->net_conf, NULL);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002038 conn_free_crypto(connection);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002039 mutex_unlock(&connection->resource->conf_update);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002040
2041 synchronize_rcu();
2042 kfree(old_conf);
2043 }
2044
Philipp Reisnera6d00c82011-03-29 18:16:11 +02002045 if (ns_max.susp_fen) {
2046 /* case1: The outdate peer handler is successful: */
2047 if (ns_max.pdsk <= D_OUTDATED) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07002048 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002049 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2050 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002051 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2052 drbd_uuid_new_current(device);
2053 clear_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisnera6d00c82011-03-29 18:16:11 +02002054 }
2055 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002056 rcu_read_unlock();
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002057 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002058 _tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
2059 _conn_request_state(connection,
Philipp Reisner8a0bab22012-08-07 13:28:00 +02002060 (union drbd_state) { { .susp_fen = 1 } },
2061 (union drbd_state) { { .susp_fen = 0 } },
2062 CS_VERBOSE);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002063 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisnera6d00c82011-03-29 18:16:11 +02002064 }
Philipp Reisnera6d00c82011-03-29 18:16:11 +02002065 }
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02002066 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner19fffd72012-08-28 16:48:03 +02002067
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002068 conn_md_sync(connection);
Philipp Reisner19fffd72012-08-28 16:48:03 +02002069
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01002070 return 0;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002071}
2072
Lars Ellenberg8ce953a2014-02-27 09:46:18 +01002073static void conn_old_common_state(struct drbd_connection *connection, union drbd_state *pcs, enum chg_state_flags *pf)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002074{
Philipp Reisner435693e2011-03-25 15:11:30 +01002075 enum chg_state_flags flags = ~0;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002076 struct drbd_peer_device *peer_device;
Philipp Reisner435693e2011-03-25 15:11:30 +01002077 int vnr, first_vol = 1;
Philipp Reisnere0e16652011-07-11 17:04:23 +02002078 union drbd_dev_state os, cs = {
2079 { .role = R_SECONDARY,
2080 .peer = R_UNKNOWN,
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002081 .conn = connection->cstate,
Philipp Reisnere0e16652011-07-11 17:04:23 +02002082 .disk = D_DISKLESS,
2083 .pdsk = D_UNKNOWN,
2084 } };
Philipp Reisner88ef5942011-03-25 14:31:11 +01002085
Philipp Reisner695d08f2011-04-11 22:53:32 -07002086 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002087 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2088 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002089 os = device->state;
Philipp Reisner88ef5942011-03-25 14:31:11 +01002090
Philipp Reisner435693e2011-03-25 15:11:30 +01002091 if (first_vol) {
2092 cs = os;
2093 first_vol = 0;
2094 continue;
2095 }
Philipp Reisner88ef5942011-03-25 14:31:11 +01002096
Philipp Reisner435693e2011-03-25 15:11:30 +01002097 if (cs.role != os.role)
2098 flags &= ~CS_DC_ROLE;
Philipp Reisner88ef5942011-03-25 14:31:11 +01002099
Philipp Reisner435693e2011-03-25 15:11:30 +01002100 if (cs.peer != os.peer)
2101 flags &= ~CS_DC_PEER;
Philipp Reisner88ef5942011-03-25 14:31:11 +01002102
Philipp Reisner435693e2011-03-25 15:11:30 +01002103 if (cs.conn != os.conn)
2104 flags &= ~CS_DC_CONN;
Philipp Reisner88ef5942011-03-25 14:31:11 +01002105
Philipp Reisner435693e2011-03-25 15:11:30 +01002106 if (cs.disk != os.disk)
2107 flags &= ~CS_DC_DISK;
2108
2109 if (cs.pdsk != os.pdsk)
2110 flags &= ~CS_DC_PDSK;
Philipp Reisner88ef5942011-03-25 14:31:11 +01002111 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002112 rcu_read_unlock();
Philipp Reisner88ef5942011-03-25 14:31:11 +01002113
Philipp Reisner435693e2011-03-25 15:11:30 +01002114 *pf |= CS_DC_MASK;
2115 *pf &= flags;
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002116 (*pcs).i = cs.i;
Philipp Reisner88ef5942011-03-25 14:31:11 +01002117}
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002118
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002119static enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002120conn_is_valid_transition(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisner88ef5942011-03-25 14:31:11 +01002121 enum chg_state_flags flags)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002122{
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002123 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002124 union drbd_state ns, os;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002125 struct drbd_peer_device *peer_device;
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002126 int vnr;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002127
Philipp Reisner695d08f2011-04-11 22:53:32 -07002128 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002129 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2130 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002131 os = drbd_read_state(device);
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +02002132 ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002133
Philipp Reisner778bcf22011-03-28 12:55:03 +02002134 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
2135 ns.disk = os.disk;
2136
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002137 if (ns.i == os.i)
2138 continue;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002139
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002140 rv = is_valid_transition(os, ns);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002141
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002142 if (rv >= SS_SUCCESS && !(flags & CS_HARD)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002143 rv = is_valid_state(device, ns);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002144 if (rv < SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002145 if (is_valid_state(device, os) == rv)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002146 rv = is_valid_soft_transition(os, ns, connection);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002147 } else
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002148 rv = is_valid_soft_transition(os, ns, connection);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002149 }
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002150
2151 if (rv < SS_SUCCESS) {
2152 if (flags & CS_VERBOSE)
2153 print_st_err(device, os, ns, rv);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002154 break;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002155 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002156 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002157 rcu_read_unlock();
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002158
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002159 return rv;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002160}
2161
Lars Ellenberg8ce953a2014-02-27 09:46:18 +01002162static void
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002163conn_set_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02002164 union drbd_state *pns_min, union drbd_state *pns_max, enum chg_state_flags flags)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002165{
Philipp Reisnerf132f552011-07-18 10:44:24 +02002166 union drbd_state ns, os, ns_max = { };
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02002167 union drbd_state ns_min = {
2168 { .role = R_MASK,
2169 .peer = R_MASK,
Philipp Reisnere0e16652011-07-11 17:04:23 +02002170 .conn = val.conn,
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02002171 .disk = D_MASK,
2172 .pdsk = D_MASK
2173 } };
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002174 struct drbd_peer_device *peer_device;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002175 enum drbd_state_rv rv;
Philipp Reisnerf132f552011-07-18 10:44:24 +02002176 int vnr, number_of_volumes = 0;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002177
Lars Ellenberg07be15b2012-05-07 11:53:08 +02002178 if (mask.conn == C_MASK) {
2179 /* remember last connect time so request_timer_fn() won't
2180 * kill newly established sessions while we are still trying to thaw
2181 * previously frozen IO */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002182 if (connection->cstate != C_WF_REPORT_PARAMS && val.conn == C_WF_REPORT_PARAMS)
2183 connection->last_reconnect_jif = jiffies;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02002184
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002185 connection->cstate = val.conn;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02002186 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002187
Philipp Reisner695d08f2011-04-11 22:53:32 -07002188 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002189 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2190 struct drbd_device *device = peer_device->device;
Philipp Reisnerf132f552011-07-18 10:44:24 +02002191 number_of_volumes++;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002192 os = drbd_read_state(device);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002193 ns = apply_mask_val(os, mask, val);
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +02002194 ns = sanitize_state(device, os, ns, NULL);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002195
Philipp Reisner778bcf22011-03-28 12:55:03 +02002196 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
2197 ns.disk = os.disk;
2198
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02002199 rv = _drbd_set_state(device, ns, flags, NULL);
Fabian Frederick7e5fec32016-06-14 00:26:35 +02002200 BUG_ON(rv < SS_SUCCESS);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002201 ns.i = device->state.i;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02002202 ns_max.role = max_role(ns.role, ns_max.role);
2203 ns_max.peer = max_role(ns.peer, ns_max.peer);
2204 ns_max.conn = max_t(enum drbd_conns, ns.conn, ns_max.conn);
2205 ns_max.disk = max_t(enum drbd_disk_state, ns.disk, ns_max.disk);
2206 ns_max.pdsk = max_t(enum drbd_disk_state, ns.pdsk, ns_max.pdsk);
2207
2208 ns_min.role = min_role(ns.role, ns_min.role);
2209 ns_min.peer = min_role(ns.peer, ns_min.peer);
2210 ns_min.conn = min_t(enum drbd_conns, ns.conn, ns_min.conn);
2211 ns_min.disk = min_t(enum drbd_disk_state, ns.disk, ns_min.disk);
2212 ns_min.pdsk = min_t(enum drbd_disk_state, ns.pdsk, ns_min.pdsk);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002213 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002214 rcu_read_unlock();
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01002215
Philipp Reisnerf132f552011-07-18 10:44:24 +02002216 if (number_of_volumes == 0) {
2217 ns_min = ns_max = (union drbd_state) { {
2218 .role = R_SECONDARY,
2219 .peer = R_UNKNOWN,
2220 .conn = val.conn,
2221 .disk = D_DISKLESS,
2222 .pdsk = D_UNKNOWN
2223 } };
2224 }
2225
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02002226 ns_min.susp = ns_max.susp = connection->resource->susp;
2227 ns_min.susp_nod = ns_max.susp_nod = connection->resource->susp_nod;
2228 ns_min.susp_fen = ns_max.susp_fen = connection->resource->susp_fen;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02002229
2230 *pns_min = ns_min;
2231 *pns_max = ns_max;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002232}
2233
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01002234static enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002235_conn_rq_cond(struct drbd_connection *connection, union drbd_state mask, union drbd_state val)
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01002236{
Philipp Reisner88ea6852014-04-28 18:43:20 +02002237 enum drbd_state_rv err, rv = SS_UNKNOWN_ERROR; /* continue waiting */;
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01002238
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002239 if (test_and_clear_bit(CONN_WD_ST_CHG_OKAY, &connection->flags))
Philipp Reisner88ea6852014-04-28 18:43:20 +02002240 rv = SS_CW_SUCCESS;
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01002241
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002242 if (test_and_clear_bit(CONN_WD_ST_CHG_FAIL, &connection->flags))
Philipp Reisner88ea6852014-04-28 18:43:20 +02002243 rv = SS_CW_FAILED_BY_PEER;
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01002244
Philipp Reisner88ea6852014-04-28 18:43:20 +02002245 err = conn_is_valid_transition(connection, mask, val, 0);
2246 if (err == SS_SUCCESS && connection->cstate == C_WF_REPORT_PARAMS)
2247 return rv;
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01002248
Philipp Reisner88ea6852014-04-28 18:43:20 +02002249 return err;
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01002250}
2251
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002252enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002253_conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002254 enum chg_state_flags flags)
2255{
2256 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002257 struct after_conn_state_chg_work *acscw;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002258 enum drbd_conns oc = connection->cstate;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02002259 union drbd_state ns_max, ns_min, os;
Lars Ellenbergc02abda2012-08-22 16:15:26 +02002260 bool have_mutex = false;
Andreas Gruenbachera2972842014-07-31 17:41:33 +02002261 struct drbd_state_change *state_change;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002262
Philipp Reisner07fc9612012-08-28 11:07:56 +02002263 if (mask.conn) {
2264 rv = is_valid_conn_transition(oc, val.conn);
2265 if (rv < SS_SUCCESS)
2266 goto abort;
2267 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002268
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002269 rv = conn_is_valid_transition(connection, mask, val, flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002270 if (rv < SS_SUCCESS)
2271 goto abort;
2272
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01002273 if (oc == C_WF_REPORT_PARAMS && val.conn == C_DISCONNECTING &&
2274 !(flags & (CS_LOCAL_ONLY | CS_HARD))) {
Lars Ellenbergc02abda2012-08-22 16:15:26 +02002275
2276 /* This will be a cluster-wide state change.
2277 * Need to give up the spinlock, grab the mutex,
2278 * then send the state change request, ... */
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002279 spin_unlock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002280 mutex_lock(&connection->cstate_mutex);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02002281 have_mutex = true;
2282
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002283 set_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
2284 if (conn_send_state_req(connection, mask, val)) {
Lars Ellenbergc02abda2012-08-22 16:15:26 +02002285 /* sending failed. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002286 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02002287 rv = SS_CW_FAILED_BY_PEER;
2288 /* need to re-aquire the spin lock, though */
2289 goto abort_unlocked;
2290 }
2291
2292 if (val.conn == C_DISCONNECTING)
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002293 set_bit(DISCONNECT_SENT, &connection->flags);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02002294
2295 /* ... and re-aquire the spinlock.
2296 * If _conn_rq_cond() returned >= SS_SUCCESS, we must call
2297 * conn_set_state() within the same spinlock. */
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002298 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002299 wait_event_lock_irq(connection->ping_wait,
2300 (rv = _conn_rq_cond(connection, mask, val)),
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002301 connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002302 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01002303 if (rv < SS_SUCCESS)
2304 goto abort;
2305 }
2306
Andreas Gruenbachera2972842014-07-31 17:41:33 +02002307 state_change = remember_old_state(connection->resource, GFP_ATOMIC);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002308 conn_old_common_state(connection, &os, &flags);
Philipp Reisner706cb242011-03-29 15:20:27 +02002309 flags |= CS_DC_SUSP;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002310 conn_set_state(connection, mask, val, &ns_min, &ns_max, flags);
2311 conn_pr_state_change(connection, os, ns_max, flags);
Andreas Gruenbachera2972842014-07-31 17:41:33 +02002312 remember_new_state(state_change);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002313
2314 acscw = kmalloc(sizeof(*acscw), GFP_ATOMIC);
2315 if (acscw) {
Philipp Reisner435693e2011-03-25 15:11:30 +01002316 acscw->oc = os.conn;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02002317 acscw->ns_min = ns_min;
Philipp Reisner5f082f92011-03-29 13:20:58 +02002318 acscw->ns_max = ns_max;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002319 acscw->flags = flags;
Andreas Gruenbacher4c007602011-08-25 15:14:48 +02002320 acscw->w.cb = w_after_conn_state_ch;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002321 kref_get(&connection->kref);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02002322 acscw->connection = connection;
Andreas Gruenbachera2972842014-07-31 17:41:33 +02002323 acscw->state_change = state_change;
Andreas Gruenbacher4c007602011-08-25 15:14:48 +02002324 drbd_queue_work(&connection->sender_work, &acscw->w);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002325 } else {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +02002326 drbd_err(connection, "Could not kmalloc an acscw\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002327 }
2328
Philipp Reisnera01842e2011-12-13 17:40:53 +01002329 abort:
Lars Ellenbergc02abda2012-08-22 16:15:26 +02002330 if (have_mutex) {
2331 /* mutex_unlock() "... must not be used in interrupt context.",
2332 * so give up the spinlock, then re-aquire it */
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002333 spin_unlock_irq(&connection->resource->req_lock);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02002334 abort_unlocked:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002335 mutex_unlock(&connection->cstate_mutex);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002336 spin_lock_irq(&connection->resource->req_lock);
Lars Ellenbergc02abda2012-08-22 16:15:26 +02002337 }
2338 if (rv < SS_SUCCESS && flags & CS_VERBOSE) {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +02002339 drbd_err(connection, "State change failed: %s\n", drbd_set_st_err_str(rv));
2340 drbd_err(connection, " mask = 0x%x val = 0x%x\n", mask.i, val.i);
2341 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 +01002342 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002343 return rv;
2344}
2345
2346enum drbd_state_rv
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002347conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002348 enum chg_state_flags flags)
2349{
2350 enum drbd_state_rv rv;
2351
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002352 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002353 rv = _conn_request_state(connection, mask, val, flags);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002354 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01002355
2356 return rv;
2357}