rmap: introduce rmap_walk_locked()
[deliverable/linux.git] / drivers / block / drbd / drbd_state.c
CommitLineData
b8907339
PR
1/*
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"
a3603a6e 30#include "drbd_protocol.h"
b8907339 31#include "drbd_req.h"
a2972846 32#include "drbd_state_change.h"
b8907339
PR
33
34struct after_state_chg_work {
35 struct drbd_work w;
84b8c06b 36 struct drbd_device *device;
b8907339
PR
37 union drbd_state os;
38 union drbd_state ns;
39 enum chg_state_flags flags;
40 struct completion *done;
a2972846 41 struct drbd_state_change *state_change;
b8907339
PR
42};
43
d942ae44
PR
44enum 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
a2972846
AG
53static 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)++;
2b479766 66 for_each_connection(connection, resource)
a2972846 67 (*n_connections)++;
a2972846
AG
68}
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
2b479766
LE
108 /* Caller holds req_lock spinlock.
109 * No state, no device IDR, no connections lists can change. */
a2972846 110 count_objects(resource, &n_devices, &n_connections);
a2972846
AG
111 state_change = alloc_state_change(n_devices, n_connections, gfp);
112 if (!state_change)
113 return NULL;
114
a2972846
AG
115 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
2b479766
LE
123 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
a2972846
AG
134 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
a2972846
AG
146 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
a2972846
AG
164 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
99920dc5 291static int w_after_state_ch(struct drbd_work *w, int unused);
b30ab791 292static void after_state_ch(struct drbd_device *device, union drbd_state os,
a2972846
AG
293 union drbd_state ns, enum chg_state_flags flags,
294 struct drbd_state_change *);
54761697 295static enum drbd_state_rv is_valid_state(struct drbd_device *, union drbd_state);
bde89a9e 296static enum drbd_state_rv is_valid_soft_transition(union drbd_state, union drbd_state, struct drbd_connection *);
3509502d 297static enum drbd_state_rv is_valid_transition(union drbd_state os, union drbd_state ns);
d7fe69c6
PR
298static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state os,
299 union drbd_state ns, enum sanitize_state_warnings *warn);
b8907339 300
2aebfabb
PR
301static inline bool is_susp(union drbd_state s)
302{
303 return s.susp || s.susp_nod || s.susp_fen;
304}
305
bde89a9e 306bool conn_all_vols_unconf(struct drbd_connection *connection)
0e29d163 307{
c06ece6b 308 struct drbd_peer_device *peer_device;
695d08fa 309 bool rv = true;
e90285e0 310 int vnr;
0e29d163 311
695d08fa 312 rcu_read_lock();
c06ece6b
AG
313 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
314 struct drbd_device *device = peer_device->device;
b30ab791
AG
315 if (device->state.disk != D_DISKLESS ||
316 device->state.conn != C_STANDALONE ||
317 device->state.role != R_SECONDARY) {
695d08fa
PR
318 rv = false;
319 break;
320 }
0e29d163 321 }
695d08fa
PR
322 rcu_read_unlock();
323
324 return rv;
0e29d163
PR
325}
326
cb703454
PR
327/* 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}
a2972846 337
cb703454
PR
338static 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
bde89a9e 347enum drbd_role conn_highest_role(struct drbd_connection *connection)
cb703454
PR
348{
349 enum drbd_role role = R_UNKNOWN;
c06ece6b 350 struct drbd_peer_device *peer_device;
cb703454
PR
351 int vnr;
352
695d08fa 353 rcu_read_lock();
c06ece6b
AG
354 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
355 struct drbd_device *device = peer_device->device;
b30ab791 356 role = max_role(role, device->state.role);
c06ece6b 357 }
695d08fa 358 rcu_read_unlock();
cb703454
PR
359
360 return role;
361}
362
bde89a9e 363enum drbd_role conn_highest_peer(struct drbd_connection *connection)
cb703454
PR
364{
365 enum drbd_role peer = R_UNKNOWN;
c06ece6b 366 struct drbd_peer_device *peer_device;
cb703454
PR
367 int vnr;
368
695d08fa 369 rcu_read_lock();
c06ece6b
AG
370 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
371 struct drbd_device *device = peer_device->device;
b30ab791 372 peer = max_role(peer, device->state.peer);
c06ece6b 373 }
695d08fa 374 rcu_read_unlock();
cb703454
PR
375
376 return peer;
377}
378
bde89a9e 379enum drbd_disk_state conn_highest_disk(struct drbd_connection *connection)
cb703454 380{
11f8b2b6 381 enum drbd_disk_state disk_state = D_DISKLESS;
c06ece6b 382 struct drbd_peer_device *peer_device;
cb703454
PR
383 int vnr;
384
695d08fa 385 rcu_read_lock();
c06ece6b
AG
386 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
387 struct drbd_device *device = peer_device->device;
11f8b2b6 388 disk_state = max_t(enum drbd_disk_state, disk_state, device->state.disk);
c06ece6b 389 }
695d08fa 390 rcu_read_unlock();
cb703454 391
11f8b2b6 392 return disk_state;
cb703454
PR
393}
394
bde89a9e 395enum drbd_disk_state conn_lowest_disk(struct drbd_connection *connection)
4669265a 396{
11f8b2b6 397 enum drbd_disk_state disk_state = D_MASK;
c06ece6b 398 struct drbd_peer_device *peer_device;
4669265a
PR
399 int vnr;
400
695d08fa 401 rcu_read_lock();
c06ece6b
AG
402 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
403 struct drbd_device *device = peer_device->device;
11f8b2b6 404 disk_state = min_t(enum drbd_disk_state, disk_state, device->state.disk);
c06ece6b 405 }
695d08fa 406 rcu_read_unlock();
4669265a 407
11f8b2b6 408 return disk_state;
4669265a
PR
409}
410
bde89a9e 411enum drbd_disk_state conn_highest_pdsk(struct drbd_connection *connection)
cb703454 412{
11f8b2b6 413 enum drbd_disk_state disk_state = D_DISKLESS;
c06ece6b 414 struct drbd_peer_device *peer_device;
cb703454
PR
415 int vnr;
416
695d08fa 417 rcu_read_lock();
c06ece6b
AG
418 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
419 struct drbd_device *device = peer_device->device;
11f8b2b6 420 disk_state = max_t(enum drbd_disk_state, disk_state, device->state.pdsk);
c06ece6b 421 }
695d08fa 422 rcu_read_unlock();
cb703454 423
11f8b2b6 424 return disk_state;
cb703454
PR
425}
426
bde89a9e 427enum drbd_conns conn_lowest_conn(struct drbd_connection *connection)
19f83c76
PR
428{
429 enum drbd_conns conn = C_MASK;
c06ece6b 430 struct drbd_peer_device *peer_device;
19f83c76
PR
431 int vnr;
432
695d08fa 433 rcu_read_lock();
c06ece6b
AG
434 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
435 struct drbd_device *device = peer_device->device;
b30ab791 436 conn = min_t(enum drbd_conns, conn, device->state.conn);
c06ece6b 437 }
695d08fa 438 rcu_read_unlock();
19f83c76
PR
439
440 return conn;
441}
442
bde89a9e 443static bool no_peer_wf_report_params(struct drbd_connection *connection)
79702011 444{
c06ece6b 445 struct drbd_peer_device *peer_device;
79702011
PR
446 int vnr;
447 bool rv = true;
448
449 rcu_read_lock();
c06ece6b
AG
450 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
451 if (peer_device->device->state.conn == C_WF_REPORT_PARAMS) {
79702011
PR
452 rv = false;
453 break;
454 }
455 rcu_read_unlock();
456
457 return rv;
458}
459
a8821531
PR
460static 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
79702011 472
b8907339
PR
473/**
474 * cl_wide_st_chg() - true if the state change is a cluster wide one
b30ab791 475 * @device: DRBD device.
b8907339
PR
476 * @os: old (current) state.
477 * @ns: new (wanted) state.
478 */
b30ab791 479static int cl_wide_st_chg(struct drbd_device *device,
b8907339
PR
480 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) ||
e8744f5a 486 (os.disk != D_FAILED && ns.disk == D_FAILED))) ||
b8907339 487 (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
369bea63
PR
488 (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S) ||
489 (os.conn == C_CONNECTED && ns.conn == C_WF_REPORT_PARAMS);
b8907339
PR
490}
491
56707f9e
PR
492static 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
b8907339 500enum drbd_state_rv
b30ab791 501drbd_change_state(struct drbd_device *device, enum chg_state_flags f,
b8907339
PR
502 union drbd_state mask, union drbd_state val)
503{
504 unsigned long flags;
56707f9e 505 union drbd_state ns;
b8907339
PR
506 enum drbd_state_rv rv;
507
0500813f 508 spin_lock_irqsave(&device->resource->req_lock, flags);
b30ab791
AG
509 ns = apply_mask_val(drbd_read_state(device), mask, val);
510 rv = _drbd_set_state(device, ns, f, NULL);
0500813f 511 spin_unlock_irqrestore(&device->resource->req_lock, flags);
b8907339
PR
512
513 return rv;
514}
515
516/**
517 * drbd_force_state() - Impose a change which happens outside our control on our state
b30ab791 518 * @device: DRBD device.
b8907339
PR
519 * @mask: mask of state bits to change.
520 * @val: value of new state bits.
521 */
b30ab791 522void drbd_force_state(struct drbd_device *device,
b8907339
PR
523 union drbd_state mask, union drbd_state val)
524{
b30ab791 525 drbd_change_state(device, CS_HARD, mask, val);
b8907339
PR
526}
527
528static enum drbd_state_rv
b30ab791 529_req_st_cond(struct drbd_device *device, union drbd_state mask,
b8907339
PR
530 union drbd_state val)
531{
532 union drbd_state os, ns;
533 unsigned long flags;
534 enum drbd_state_rv rv;
535
b30ab791 536 if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &device->flags))
b8907339
PR
537 return SS_CW_SUCCESS;
538
b30ab791 539 if (test_and_clear_bit(CL_ST_CHG_FAIL, &device->flags))
b8907339
PR
540 return SS_CW_FAILED_BY_PEER;
541
0500813f 542 spin_lock_irqsave(&device->resource->req_lock, flags);
b30ab791 543 os = drbd_read_state(device);
d7fe69c6 544 ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
3509502d 545 rv = is_valid_transition(os, ns);
a3025a27 546 if (rv >= SS_SUCCESS)
3509502d 547 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
b8907339 548
b30ab791 549 if (!cl_wide_st_chg(device, os, ns))
b8907339 550 rv = SS_CW_NO_NEED;
3509502d 551 if (rv == SS_UNKNOWN_ERROR) {
b30ab791 552 rv = is_valid_state(device, ns);
a3025a27 553 if (rv >= SS_SUCCESS) {
a6b32bc3 554 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
a3025a27 555 if (rv >= SS_SUCCESS)
b8907339
PR
556 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
557 }
558 }
0500813f 559 spin_unlock_irqrestore(&device->resource->req_lock, flags);
b8907339
PR
560
561 return rv;
562}
563
564/**
565 * drbd_req_state() - Perform an eventually cluster wide state change
b30ab791 566 * @device: DRBD device.
b8907339
PR
567 * @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
b30ab791 575drbd_req_state(struct drbd_device *device, union drbd_state mask,
b8907339
PR
576 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)
b30ab791 586 mutex_lock(device->state_mutex);
b8907339 587
0500813f 588 spin_lock_irqsave(&device->resource->req_lock, flags);
b30ab791 589 os = drbd_read_state(device);
d7fe69c6 590 ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
3509502d 591 rv = is_valid_transition(os, ns);
3c5e5f6a 592 if (rv < SS_SUCCESS) {
0500813f 593 spin_unlock_irqrestore(&device->resource->req_lock, flags);
3509502d 594 goto abort;
3c5e5f6a 595 }
b8907339 596
b30ab791
AG
597 if (cl_wide_st_chg(device, os, ns)) {
598 rv = is_valid_state(device, ns);
b8907339 599 if (rv == SS_SUCCESS)
a6b32bc3 600 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
0500813f 601 spin_unlock_irqrestore(&device->resource->req_lock, flags);
b8907339
PR
602
603 if (rv < SS_SUCCESS) {
604 if (f & CS_VERBOSE)
b30ab791 605 print_st_err(device, os, ns, rv);
b8907339
PR
606 goto abort;
607 }
608
69a22773 609 if (drbd_send_state_req(first_peer_device(device), mask, val)) {
b8907339
PR
610 rv = SS_CW_FAILED_BY_PEER;
611 if (f & CS_VERBOSE)
b30ab791 612 print_st_err(device, os, ns, rv);
b8907339
PR
613 goto abort;
614 }
615
b30ab791
AG
616 wait_event(device->state_wait,
617 (rv = _req_st_cond(device, mask, val)));
b8907339
PR
618
619 if (rv < SS_SUCCESS) {
b8907339 620 if (f & CS_VERBOSE)
b30ab791 621 print_st_err(device, os, ns, rv);
b8907339
PR
622 goto abort;
623 }
0500813f 624 spin_lock_irqsave(&device->resource->req_lock, flags);
b30ab791
AG
625 ns = apply_mask_val(drbd_read_state(device), mask, val);
626 rv = _drbd_set_state(device, ns, f, &done);
b8907339 627 } else {
b30ab791 628 rv = _drbd_set_state(device, ns, f, &done);
b8907339
PR
629 }
630
0500813f 631 spin_unlock_irqrestore(&device->resource->req_lock, flags);
b8907339
PR
632
633 if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
0b0ba1ef 634 D_ASSERT(device, current != first_peer_device(device)->connection->worker.task);
b8907339
PR
635 wait_for_completion(&done);
636 }
637
638abort:
639 if (f & CS_SERIALIZE)
b30ab791 640 mutex_unlock(device->state_mutex);
b8907339
PR
641
642 return rv;
643}
644
645/**
646 * _drbd_request_state() - Request a state change (with flags)
b30ab791 647 * @device: DRBD device.
b8907339
PR
648 * @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
b30ab791 656_drbd_request_state(struct drbd_device *device, union drbd_state mask,
b8907339
PR
657 union drbd_state val, enum chg_state_flags f)
658{
659 enum drbd_state_rv rv;
660
b30ab791
AG
661 wait_event(device->state_wait,
662 (rv = drbd_req_state(device, mask, val, f)) != SS_IN_TRANSIENT_STATE);
b8907339
PR
663
664 return rv;
665}
666
a8821531
PR
667enum 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
8ce953aa 683static void print_st(struct drbd_device *device, const char *name, union drbd_state ns)
b8907339 684{
d0180171 685 drbd_err(device, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c%c%c }\n",
b8907339
PR
686 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
b30ab791 701void print_st_err(struct drbd_device *device, union drbd_state os,
b8907339
PR
702 union drbd_state ns, enum drbd_state_rv err)
703{
704 if (err == SS_IN_TRANSIENT_STATE)
705 return;
d0180171 706 drbd_err(device, "State change failed: %s\n", drbd_set_st_err_str(err));
b30ab791
AG
707 print_st(device, " state", os);
708 print_st(device, "wanted", ns);
b8907339
PR
709}
710
435693e8 711static long print_state_change(char *pb, union drbd_state os, union drbd_state ns,
bbeb641c
PR
712 enum chg_state_flags flags)
713{
435693e8 714 char *pbp;
bbeb641c
PR
715 pbp = pb;
716 *pbp = 0;
706cb24c 717
435693e8 718 if (ns.role != os.role && flags & CS_DC_ROLE)
bbeb641c
PR
719 pbp += sprintf(pbp, "role( %s -> %s ) ",
720 drbd_role_str(os.role),
721 drbd_role_str(ns.role));
435693e8 722 if (ns.peer != os.peer && flags & CS_DC_PEER)
bbeb641c
PR
723 pbp += sprintf(pbp, "peer( %s -> %s ) ",
724 drbd_role_str(os.peer),
725 drbd_role_str(ns.peer));
435693e8 726 if (ns.conn != os.conn && flags & CS_DC_CONN)
bbeb641c
PR
727 pbp += sprintf(pbp, "conn( %s -> %s ) ",
728 drbd_conn_str(os.conn),
729 drbd_conn_str(ns.conn));
435693e8 730 if (ns.disk != os.disk && flags & CS_DC_DISK)
bbeb641c
PR
731 pbp += sprintf(pbp, "disk( %s -> %s ) ",
732 drbd_disk_str(os.disk),
733 drbd_disk_str(ns.disk));
435693e8 734 if (ns.pdsk != os.pdsk && flags & CS_DC_PDSK)
bbeb641c
PR
735 pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
736 drbd_disk_str(os.pdsk),
737 drbd_disk_str(ns.pdsk));
706cb24c
PR
738
739 return pbp - pb;
740}
741
b30ab791 742static void drbd_pr_state_change(struct drbd_device *device, union drbd_state os, union drbd_state ns,
706cb24c
PR
743 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
bbeb641c
PR
750 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);
435693e8 762
706cb24c 763 if (pbp != pb)
d0180171 764 drbd_info(device, "%s\n", pb);
bbeb641c 765}
b8907339 766
bde89a9e 767static void conn_pr_state_change(struct drbd_connection *connection, union drbd_state os, union drbd_state ns,
435693e8
PR
768 enum chg_state_flags flags)
769{
770 char pb[300];
706cb24c
PR
771 char *pbp = pb;
772
773 pbp += print_state_change(pbp, os, ns, flags);
435693e8 774
706cb24c
PR
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)
1ec861eb 781 drbd_info(connection, "%s\n", pb);
435693e8
PR
782}
783
784
b8907339
PR
785/**
786 * is_valid_state() - Returns an SS_ error code if ns is not valid
b30ab791 787 * @device: DRBD device.
b8907339
PR
788 * @ns: State to consider.
789 */
790static enum drbd_state_rv
b30ab791 791is_valid_state(struct drbd_device *device, union drbd_state ns)
b8907339
PR
792{
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;
44ed167d 797 struct net_conf *nc;
b8907339 798
daeda1cc 799 rcu_read_lock();
b8907339 800 fp = FP_DONT_CARE;
b30ab791
AG
801 if (get_ldev(device)) {
802 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
803 put_ldev(device);
b8907339
PR
804 }
805
a6b32bc3 806 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
44ed167d
PR
807 if (nc) {
808 if (!nc->two_primaries && ns.role == R_PRIMARY) {
047e95e2
PR
809 if (ns.peer == R_PRIMARY)
810 rv = SS_TWO_PRIMARIES;
a6b32bc3 811 else if (conn_highest_peer(first_peer_device(device)->connection) == R_PRIMARY)
047e95e2 812 rv = SS_O_VOL_PEER_PRI;
44ed167d 813 }
b8907339
PR
814 }
815
816 if (rv <= 0)
817 /* already found a reason to abort */;
b30ab791 818 else if (ns.role == R_SECONDARY && device->open_cnt)
b8907339
PR
819 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) &&
44ed167d 848 (nc->verify_alg[0] == 0))
b8907339
PR
849 rv = SS_NO_VERIFY_ALG;
850
851 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
a6b32bc3 852 first_peer_device(device)->connection->agreed_pro_version < 88)
b8907339
PR
853 rv = SS_NOT_SUPPORTED;
854
5c4f13d9
PR
855 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
b8907339
PR
862 else if (ns.conn >= C_CONNECTED && ns.pdsk == D_UNKNOWN)
863 rv = SS_CONNECTED_OUTDATES;
864
44ed167d
PR
865 rcu_read_unlock();
866
b8907339
PR
867 return rv;
868}
869
870/**
a75f34ad 871 * is_valid_soft_transition() - Returns an SS_ error code if the state transition is not possible
3509502d
PR
872 * This function limits state transitions that may be declined by DRBD. I.e.
873 * user requests (aka soft transitions).
b30ab791 874 * @device: DRBD device.
b8907339
PR
875 * @ns: new state.
876 * @os: old state.
877 */
878static enum drbd_state_rv
bde89a9e 879is_valid_soft_transition(union drbd_state os, union drbd_state ns, struct drbd_connection *connection)
b8907339
PR
880{
881 enum drbd_state_rv rv = SS_SUCCESS;
882
883 if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
884 os.conn > C_CONNECTED)
885 rv = SS_RESYNC_RUNNING;
886
887 if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
888 rv = SS_ALREADY_STANDALONE;
889
890 if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
891 rv = SS_IS_DISKLESS;
892
893 if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
894 rv = SS_NO_NET_CONFIG;
895
896 if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
897 rv = SS_LOWER_THAN_OUTDATED;
898
899 if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
900 rv = SS_IN_TRANSIENT_STATE;
901
a1096a6e 902 /* While establishing a connection only allow cstate to change.
a8821531 903 Delay/refuse role changes, detach attach etc... (they do not touch cstate) */
bde89a9e 904 if (test_bit(STATE_SENT, &connection->flags) &&
a8821531
PR
905 !((ns.conn == C_WF_REPORT_PARAMS && os.conn == C_WF_CONNECTION) ||
906 (ns.conn >= C_CONNECTED && os.conn == C_WF_REPORT_PARAMS)))
a1096a6e
PR
907 rv = SS_IN_TRANSIENT_STATE;
908
b8907339
PR
909 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
910 rv = SS_NEED_CONNECTION;
911
912 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
913 ns.conn != os.conn && os.conn > C_CONNECTED)
914 rv = SS_RESYNC_RUNNING;
915
916 if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
917 os.conn < C_CONNECTED)
918 rv = SS_NEED_CONNECTION;
919
920 if ((ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)
921 && os.conn < C_WF_REPORT_PARAMS)
922 rv = SS_NEED_CONNECTION; /* No NetworkFailure -> SyncTarget etc... */
923
2bd5ed5d
PR
924 if (ns.conn == C_DISCONNECTING && ns.pdsk == D_OUTDATED &&
925 os.conn < C_CONNECTED && os.pdsk > D_OUTDATED)
926 rv = SS_OUTDATE_WO_CONN;
927
b8907339
PR
928 return rv;
929}
930
3509502d 931static enum drbd_state_rv
fda74117 932is_valid_conn_transition(enum drbd_conns oc, enum drbd_conns nc)
3509502d 933{
d9cc6e23
LE
934 /* no change -> nothing to do, at least for the connection part */
935 if (oc == nc)
936 return SS_NOTHING_TO_DO;
3509502d 937
d9cc6e23
LE
938 /* disconnect of an unconfigured connection does not make sense */
939 if (oc == C_STANDALONE && nc == C_DISCONNECTING)
940 return SS_ALREADY_STANDALONE;
941
942 /* from C_STANDALONE, we start with C_UNCONNECTED */
943 if (oc == C_STANDALONE && nc != C_UNCONNECTED)
944 return SS_NEED_CONNECTION;
3509502d 945
25b0d6c8
PR
946 /* When establishing a connection we need to go through WF_REPORT_PARAMS!
947 Necessary to do the right thing upon invalidate-remote on a disconnected resource */
948 if (oc < C_WF_REPORT_PARAMS && nc >= C_CONNECTED)
949 return SS_NEED_CONNECTION;
950
3509502d 951 /* After a network error only C_UNCONNECTED or C_DISCONNECTING may follow. */
fda74117 952 if (oc >= C_TIMEOUT && oc <= C_TEAR_DOWN && nc != C_UNCONNECTED && nc != C_DISCONNECTING)
d9cc6e23 953 return SS_IN_TRANSIENT_STATE;
3509502d
PR
954
955 /* After C_DISCONNECTING only C_STANDALONE may follow */
fda74117 956 if (oc == C_DISCONNECTING && nc != C_STANDALONE)
d9cc6e23 957 return SS_IN_TRANSIENT_STATE;
3509502d 958
d9cc6e23 959 return SS_SUCCESS;
fda74117
PR
960}
961
962
963/**
964 * is_valid_transition() - Returns an SS_ error code if the state transition is not possible
965 * This limits hard state transitions. Hard state transitions are facts there are
966 * imposed on DRBD by the environment. E.g. disk broke or network broke down.
967 * But those hard state transitions are still not allowed to do everything.
968 * @ns: new state.
969 * @os: old state.
970 */
971static enum drbd_state_rv
972is_valid_transition(union drbd_state os, union drbd_state ns)
973{
974 enum drbd_state_rv rv;
975
976 rv = is_valid_conn_transition(os.conn, ns.conn);
977
3509502d
PR
978 /* we cannot fail (again) if we already detached */
979 if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
980 rv = SS_IS_DISKLESS;
981
982 return rv;
983}
984
b30ab791 985static void print_sanitize_warnings(struct drbd_device *device, enum sanitize_state_warnings warn)
d942ae44
PR
986{
987 static const char *msg_table[] = {
988 [NO_WARNING] = "",
989 [ABORTED_ONLINE_VERIFY] = "Online-verify aborted.",
990 [ABORTED_RESYNC] = "Resync aborted.",
991 [CONNECTION_LOST_NEGOTIATING] = "Connection lost while negotiating, no data!",
992 [IMPLICITLY_UPGRADED_DISK] = "Implicitly upgraded disk",
993 [IMPLICITLY_UPGRADED_PDSK] = "Implicitly upgraded pdsk",
994 };
995
996 if (warn != NO_WARNING)
d0180171 997 drbd_warn(device, "%s\n", msg_table[warn]);
d942ae44
PR
998}
999
b8907339
PR
1000/**
1001 * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
b30ab791 1002 * @device: DRBD device.
b8907339
PR
1003 * @os: old state.
1004 * @ns: new state.
1005 * @warn_sync_abort:
1006 *
1007 * When we loose connection, we have to set the state of the peers disk (pdsk)
1008 * to D_UNKNOWN. This rule and many more along those lines are in this function.
1009 */
d7fe69c6
PR
1010static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state os,
1011 union drbd_state ns, enum sanitize_state_warnings *warn)
b8907339
PR
1012{
1013 enum drbd_fencing_p fp;
1014 enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
1015
d942ae44
PR
1016 if (warn)
1017 *warn = NO_WARNING;
1018
b8907339 1019 fp = FP_DONT_CARE;
b30ab791 1020 if (get_ldev(device)) {
daeda1cc 1021 rcu_read_lock();
b30ab791 1022 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
daeda1cc 1023 rcu_read_unlock();
b30ab791 1024 put_ldev(device);
b8907339
PR
1025 }
1026
3509502d 1027 /* Implications from connection to peer and peer_isp */
b8907339
PR
1028 if (ns.conn < C_CONNECTED) {
1029 ns.peer_isp = 0;
1030 ns.peer = R_UNKNOWN;
1031 if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
1032 ns.pdsk = D_UNKNOWN;
1033 }
1034
1035 /* Clear the aftr_isp when becoming unconfigured */
1036 if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
1037 ns.aftr_isp = 0;
1038
4308a0a3 1039 /* An implication of the disk states onto the connection state */
b8907339 1040 /* Abort resync if a disk fails/detaches */
4308a0a3 1041 if (ns.conn > C_CONNECTED && (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
d942ae44
PR
1042 if (warn)
1043 *warn = ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T ?
1044 ABORTED_ONLINE_VERIFY : ABORTED_RESYNC;
b8907339
PR
1045 ns.conn = C_CONNECTED;
1046 }
1047
1048 /* Connection breaks down before we finished "Negotiating" */
1049 if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
b30ab791
AG
1050 get_ldev_if_state(device, D_NEGOTIATING)) {
1051 if (device->ed_uuid == device->ldev->md.uuid[UI_CURRENT]) {
1052 ns.disk = device->new_state_tmp.disk;
1053 ns.pdsk = device->new_state_tmp.pdsk;
b8907339 1054 } else {
d942ae44
PR
1055 if (warn)
1056 *warn = CONNECTION_LOST_NEGOTIATING;
b8907339
PR
1057 ns.disk = D_DISKLESS;
1058 ns.pdsk = D_UNKNOWN;
1059 }
b30ab791 1060 put_ldev(device);
b8907339
PR
1061 }
1062
1063 /* D_CONSISTENT and D_OUTDATED vanish when we get connected */
1064 if (ns.conn >= C_CONNECTED && ns.conn < C_AHEAD) {
1065 if (ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED)
1066 ns.disk = D_UP_TO_DATE;
1067 if (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)
1068 ns.pdsk = D_UP_TO_DATE;
1069 }
1070
1071 /* Implications of the connection stat on the disk states */
1072 disk_min = D_DISKLESS;
1073 disk_max = D_UP_TO_DATE;
1074 pdsk_min = D_INCONSISTENT;
1075 pdsk_max = D_UNKNOWN;
1076 switch ((enum drbd_conns)ns.conn) {
1077 case C_WF_BITMAP_T:
1078 case C_PAUSED_SYNC_T:
1079 case C_STARTING_SYNC_T:
1080 case C_WF_SYNC_UUID:
1081 case C_BEHIND:
1082 disk_min = D_INCONSISTENT;
1083 disk_max = D_OUTDATED;
1084 pdsk_min = D_UP_TO_DATE;
1085 pdsk_max = D_UP_TO_DATE;
1086 break;
1087 case C_VERIFY_S:
1088 case C_VERIFY_T:
1089 disk_min = D_UP_TO_DATE;
1090 disk_max = D_UP_TO_DATE;
1091 pdsk_min = D_UP_TO_DATE;
1092 pdsk_max = D_UP_TO_DATE;
1093 break;
1094 case C_CONNECTED:
1095 disk_min = D_DISKLESS;
1096 disk_max = D_UP_TO_DATE;
1097 pdsk_min = D_DISKLESS;
1098 pdsk_max = D_UP_TO_DATE;
1099 break;
1100 case C_WF_BITMAP_S:
1101 case C_PAUSED_SYNC_S:
1102 case C_STARTING_SYNC_S:
1103 case C_AHEAD:
1104 disk_min = D_UP_TO_DATE;
1105 disk_max = D_UP_TO_DATE;
1106 pdsk_min = D_INCONSISTENT;
1107 pdsk_max = D_CONSISTENT; /* D_OUTDATED would be nice. But explicit outdate necessary*/
1108 break;
1109 case C_SYNC_TARGET:
1110 disk_min = D_INCONSISTENT;
1111 disk_max = D_INCONSISTENT;
1112 pdsk_min = D_UP_TO_DATE;
1113 pdsk_max = D_UP_TO_DATE;
1114 break;
1115 case C_SYNC_SOURCE:
1116 disk_min = D_UP_TO_DATE;
1117 disk_max = D_UP_TO_DATE;
1118 pdsk_min = D_INCONSISTENT;
1119 pdsk_max = D_INCONSISTENT;
1120 break;
1121 case C_STANDALONE:
1122 case C_DISCONNECTING:
1123 case C_UNCONNECTED:
1124 case C_TIMEOUT:
1125 case C_BROKEN_PIPE:
1126 case C_NETWORK_FAILURE:
1127 case C_PROTOCOL_ERROR:
1128 case C_TEAR_DOWN:
1129 case C_WF_CONNECTION:
1130 case C_WF_REPORT_PARAMS:
1131 case C_MASK:
1132 break;
1133 }
1134 if (ns.disk > disk_max)
1135 ns.disk = disk_max;
1136
1137 if (ns.disk < disk_min) {
d942ae44
PR
1138 if (warn)
1139 *warn = IMPLICITLY_UPGRADED_DISK;
b8907339
PR
1140 ns.disk = disk_min;
1141 }
1142 if (ns.pdsk > pdsk_max)
1143 ns.pdsk = pdsk_max;
1144
1145 if (ns.pdsk < pdsk_min) {
d942ae44
PR
1146 if (warn)
1147 *warn = IMPLICITLY_UPGRADED_PDSK;
b8907339
PR
1148 ns.pdsk = pdsk_min;
1149 }
1150
1151 if (fp == FP_STONITH &&
d7fe69c6
PR
1152 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED) &&
1153 !(os.role == R_PRIMARY && os.conn < C_CONNECTED && os.pdsk > D_OUTDATED))
b8907339
PR
1154 ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
1155
eb6bea67 1156 if (device->resource->res_opts.on_no_data == OND_SUSPEND_IO &&
d7fe69c6
PR
1157 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE) &&
1158 !(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE))
b8907339
PR
1159 ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
1160
1161 if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
1162 if (ns.conn == C_SYNC_SOURCE)
1163 ns.conn = C_PAUSED_SYNC_S;
1164 if (ns.conn == C_SYNC_TARGET)
1165 ns.conn = C_PAUSED_SYNC_T;
1166 } else {
1167 if (ns.conn == C_PAUSED_SYNC_S)
1168 ns.conn = C_SYNC_SOURCE;
1169 if (ns.conn == C_PAUSED_SYNC_T)
1170 ns.conn = C_SYNC_TARGET;
1171 }
1172
1173 return ns;
1174}
1175
b30ab791 1176void drbd_resume_al(struct drbd_device *device)
b8907339 1177{
b30ab791 1178 if (test_and_clear_bit(AL_SUSPENDED, &device->flags))
d0180171 1179 drbd_info(device, "Resumed AL updates\n");
b8907339
PR
1180}
1181
28bc3b8c 1182/* helper for _drbd_set_state */
b30ab791 1183static void set_ov_position(struct drbd_device *device, enum drbd_conns cs)
b8907339 1184{
a6b32bc3 1185 if (first_peer_device(device)->connection->agreed_pro_version < 90)
b30ab791
AG
1186 device->ov_start_sector = 0;
1187 device->rs_total = drbd_bm_bits(device);
1188 device->ov_position = 0;
b8907339
PR
1189 if (cs == C_VERIFY_T) {
1190 /* starting online verify from an arbitrary position
1191 * does not fit well into the existing protocol.
1192 * on C_VERIFY_T, we initialize ov_left and friends
1193 * implicitly in receive_DataRequest once the
1194 * first P_OV_REQUEST is received */
b30ab791 1195 device->ov_start_sector = ~(sector_t)0;
b8907339 1196 } else {
b30ab791
AG
1197 unsigned long bit = BM_SECT_TO_BIT(device->ov_start_sector);
1198 if (bit >= device->rs_total) {
1199 device->ov_start_sector =
1200 BM_BIT_TO_SECT(device->rs_total - 1);
1201 device->rs_total = 1;
b8907339 1202 } else
b30ab791
AG
1203 device->rs_total -= bit;
1204 device->ov_position = device->ov_start_sector;
b8907339 1205 }
b30ab791 1206 device->ov_left = device->rs_total;
b8907339
PR
1207}
1208
1209/**
28bc3b8c 1210 * _drbd_set_state() - Set a new DRBD state
b30ab791 1211 * @device: DRBD device.
b8907339
PR
1212 * @ns: new state.
1213 * @flags: Flags
1214 * @done: Optional completion, that will get completed after the after_state_ch() finished
1215 *
28bc3b8c 1216 * Caller needs to hold req_lock. Do not call directly.
b8907339
PR
1217 */
1218enum drbd_state_rv
28bc3b8c
AG
1219_drbd_set_state(struct drbd_device *device, union drbd_state ns,
1220 enum chg_state_flags flags, struct completion *done)
b8907339 1221{
44a4d551
LE
1222 struct drbd_peer_device *peer_device = first_peer_device(device);
1223 struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
b8907339
PR
1224 union drbd_state os;
1225 enum drbd_state_rv rv = SS_SUCCESS;
d942ae44 1226 enum sanitize_state_warnings ssw;
b8907339 1227 struct after_state_chg_work *ascw;
a2972846 1228 struct drbd_state_change *state_change;
b8907339 1229
b30ab791 1230 os = drbd_read_state(device);
b8907339 1231
d7fe69c6 1232 ns = sanitize_state(device, os, ns, &ssw);
b8907339
PR
1233 if (ns.i == os.i)
1234 return SS_NOTHING_TO_DO;
1235
3509502d
PR
1236 rv = is_valid_transition(os, ns);
1237 if (rv < SS_SUCCESS)
1238 return rv;
1239
b8907339
PR
1240 if (!(flags & CS_HARD)) {
1241 /* pre-state-change checks ; only look at ns */
1242 /* See drbd_state_sw_errors in drbd_strings.c */
1243
b30ab791 1244 rv = is_valid_state(device, ns);
b8907339
PR
1245 if (rv < SS_SUCCESS) {
1246 /* If the old state was illegal as well, then let
1247 this happen...*/
1248
b30ab791 1249 if (is_valid_state(device, os) == rv)
44a4d551 1250 rv = is_valid_soft_transition(os, ns, connection);
b8907339 1251 } else
44a4d551 1252 rv = is_valid_soft_transition(os, ns, connection);
b8907339
PR
1253 }
1254
1255 if (rv < SS_SUCCESS) {
1256 if (flags & CS_VERBOSE)
b30ab791 1257 print_st_err(device, os, ns, rv);
b8907339
PR
1258 return rv;
1259 }
1260
b30ab791 1261 print_sanitize_warnings(device, ssw);
b8907339 1262
b30ab791 1263 drbd_pr_state_change(device, os, ns, flags);
b8907339 1264
706cb24c
PR
1265 /* Display changes to the susp* flags that where caused by the call to
1266 sanitize_state(). Only display it here if we where not called from
1267 _conn_request_state() */
1268 if (!(flags & CS_DC_SUSP))
44a4d551 1269 conn_pr_state_change(connection, os, ns,
a6b32bc3 1270 (flags & ~CS_DC_MASK) | CS_DC_SUSP);
706cb24c 1271
b8907339
PR
1272 /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
1273 * on the ldev here, to be sure the transition -> D_DISKLESS resp.
1274 * drbd_ldev_destroy() won't happen before our corresponding
1275 * after_state_ch works run, where we put_ldev again. */
1276 if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
1277 (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
b30ab791 1278 atomic_inc(&device->local_cnt);
b8907339 1279
5ab7d2c0
LE
1280 if (!is_sync_state(os.conn) && is_sync_state(ns.conn))
1281 clear_bit(RS_DONE, &device->flags);
1282
a2972846
AG
1283 /* FIXME: Have any flags been set earlier in this function already? */
1284 state_change = remember_old_state(device->resource, GFP_ATOMIC);
1285
ba3c6fb8
LE
1286 /* changes to local_cnt and device flags should be visible before
1287 * changes to state, which again should be visible before anything else
1288 * depending on that change happens. */
1289 smp_wmb();
b30ab791 1290 device->state.i = ns.i;
6bbf53ca
AG
1291 device->resource->susp = ns.susp;
1292 device->resource->susp_nod = ns.susp_nod;
1293 device->resource->susp_fen = ns.susp_fen;
ba3c6fb8 1294 smp_wmb();
b8907339 1295
a2972846
AG
1296 remember_new_state(state_change);
1297
2681f7f6 1298 /* put replicated vs not-replicated requests in seperate epochs */
ba3c6fb8
LE
1299 if (drbd_should_do_remote((union drbd_dev_state)os.i) !=
1300 drbd_should_do_remote((union drbd_dev_state)ns.i))
44a4d551 1301 start_new_tl_epoch(connection);
2681f7f6 1302
b8907339 1303 if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
b30ab791 1304 drbd_print_uuids(device, "attached to UUIDs");
b8907339 1305
79702011
PR
1306 /* Wake up role changes, that were delayed because of connection establishing */
1307 if (os.conn == C_WF_REPORT_PARAMS && ns.conn != C_WF_REPORT_PARAMS &&
a8821531 1308 no_peer_wf_report_params(connection)) {
44a4d551 1309 clear_bit(STATE_SENT, &connection->flags);
a8821531
PR
1310 wake_up_all_devices(connection);
1311 }
79702011 1312
b30ab791
AG
1313 wake_up(&device->misc_wait);
1314 wake_up(&device->state_wait);
44a4d551 1315 wake_up(&connection->ping_wait);
b8907339 1316
58ffa580
LE
1317 /* Aborted verify run, or we reached the stop sector.
1318 * Log the last position, unless end-of-device. */
b8907339 1319 if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
58ffa580 1320 ns.conn <= C_CONNECTED) {
b30ab791
AG
1321 device->ov_start_sector =
1322 BM_BIT_TO_SECT(drbd_bm_bits(device) - device->ov_left);
1323 if (device->ov_left)
d0180171 1324 drbd_info(device, "Online Verify reached sector %llu\n",
b30ab791 1325 (unsigned long long)device->ov_start_sector);
b8907339
PR
1326 }
1327
1328 if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
1329 (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
d0180171 1330 drbd_info(device, "Syncer continues.\n");
b30ab791
AG
1331 device->rs_paused += (long)jiffies
1332 -(long)device->rs_mark_time[device->rs_last_mark];
b8907339 1333 if (ns.conn == C_SYNC_TARGET)
b30ab791 1334 mod_timer(&device->resync_timer, jiffies);
b8907339
PR
1335 }
1336
1337 if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
1338 (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
d0180171 1339 drbd_info(device, "Resync suspended\n");
b30ab791 1340 device->rs_mark_time[device->rs_last_mark] = jiffies;
b8907339
PR
1341 }
1342
1343 if (os.conn == C_CONNECTED &&
1344 (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
1345 unsigned long now = jiffies;
1346 int i;
1347
b30ab791
AG
1348 set_ov_position(device, ns.conn);
1349 device->rs_start = now;
b30ab791
AG
1350 device->rs_last_sect_ev = 0;
1351 device->ov_last_oos_size = 0;
1352 device->ov_last_oos_start = 0;
b8907339
PR
1353
1354 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
b30ab791
AG
1355 device->rs_mark_left[i] = device->ov_left;
1356 device->rs_mark_time[i] = now;
b8907339
PR
1357 }
1358
b30ab791 1359 drbd_rs_controller_reset(device);
b8907339
PR
1360
1361 if (ns.conn == C_VERIFY_S) {
d0180171 1362 drbd_info(device, "Starting Online Verify from sector %llu\n",
b30ab791
AG
1363 (unsigned long long)device->ov_position);
1364 mod_timer(&device->resync_timer, jiffies);
b8907339
PR
1365 }
1366 }
1367
b30ab791
AG
1368 if (get_ldev(device)) {
1369 u32 mdf = device->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
b8907339
PR
1370 MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
1371 MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
1372
d5d7ebd4 1373 mdf &= ~MDF_AL_CLEAN;
b30ab791 1374 if (test_bit(CRASHED_PRIMARY, &device->flags))
b8907339 1375 mdf |= MDF_CRASHED_PRIMARY;
b30ab791
AG
1376 if (device->state.role == R_PRIMARY ||
1377 (device->state.pdsk < D_INCONSISTENT && device->state.peer == R_PRIMARY))
b8907339 1378 mdf |= MDF_PRIMARY_IND;
b30ab791 1379 if (device->state.conn > C_WF_REPORT_PARAMS)
b8907339 1380 mdf |= MDF_CONNECTED_IND;
b30ab791 1381 if (device->state.disk > D_INCONSISTENT)
b8907339 1382 mdf |= MDF_CONSISTENT;
b30ab791 1383 if (device->state.disk > D_OUTDATED)
b8907339 1384 mdf |= MDF_WAS_UP_TO_DATE;
b30ab791 1385 if (device->state.pdsk <= D_OUTDATED && device->state.pdsk >= D_INCONSISTENT)
b8907339 1386 mdf |= MDF_PEER_OUT_DATED;
b30ab791
AG
1387 if (mdf != device->ldev->md.flags) {
1388 device->ldev->md.flags = mdf;
1389 drbd_md_mark_dirty(device);
b8907339
PR
1390 }
1391 if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
b30ab791
AG
1392 drbd_set_ed_uuid(device, device->ldev->md.uuid[UI_CURRENT]);
1393 put_ldev(device);
b8907339
PR
1394 }
1395
1396 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
1397 if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
1398 os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
b30ab791 1399 set_bit(CONSIDER_RESYNC, &device->flags);
b8907339
PR
1400
1401 /* Receiver should clean up itself */
1402 if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
44a4d551 1403 drbd_thread_stop_nowait(&connection->receiver);
b8907339
PR
1404
1405 /* Now the receiver finished cleaning up itself, it should die */
1406 if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
44a4d551 1407 drbd_thread_stop_nowait(&connection->receiver);
b8907339
PR
1408
1409 /* Upon network failure, we need to restart the receiver. */
823bd832 1410 if (os.conn > C_WF_CONNECTION &&
b8907339 1411 ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
44a4d551 1412 drbd_thread_restart_nowait(&connection->receiver);
b8907339
PR
1413
1414 /* Resume AL writing if we get a connection */
28e448bb 1415 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
b30ab791 1416 drbd_resume_al(device);
44a4d551 1417 connection->connect_cnt++;
28e448bb 1418 }
b8907339 1419
07be15b1
LE
1420 /* remember last attach time so request_timer_fn() won't
1421 * kill newly established sessions while we are still trying to thaw
1422 * previously frozen IO */
1423 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
1424 ns.disk > D_NEGOTIATING)
b30ab791 1425 device->last_reattach_jif = jiffies;
07be15b1 1426
b8907339
PR
1427 ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
1428 if (ascw) {
1429 ascw->os = os;
1430 ascw->ns = ns;
1431 ascw->flags = flags;
1432 ascw->w.cb = w_after_state_ch;
84b8c06b 1433 ascw->device = device;
b8907339 1434 ascw->done = done;
a2972846 1435 ascw->state_change = state_change;
44a4d551 1436 drbd_queue_work(&connection->sender_work,
84b8c06b 1437 &ascw->w);
b8907339 1438 } else {
d0180171 1439 drbd_err(device, "Could not kmalloc an ascw\n");
b8907339
PR
1440 }
1441
1442 return rv;
1443}
1444
99920dc5 1445static int w_after_state_ch(struct drbd_work *w, int unused)
b8907339
PR
1446{
1447 struct after_state_chg_work *ascw =
1448 container_of(w, struct after_state_chg_work, w);
84b8c06b 1449 struct drbd_device *device = ascw->device;
b8907339 1450
a2972846
AG
1451 after_state_ch(device, ascw->os, ascw->ns, ascw->flags, ascw->state_change);
1452 forget_state_change(ascw->state_change);
137975c1 1453 if (ascw->flags & CS_WAIT_COMPLETE)
b8907339 1454 complete(ascw->done);
b8907339
PR
1455 kfree(ascw);
1456
99920dc5 1457 return 0;
b8907339
PR
1458}
1459
b30ab791 1460static void abw_start_sync(struct drbd_device *device, int rv)
b8907339
PR
1461{
1462 if (rv) {
d0180171 1463 drbd_err(device, "Writing the bitmap failed not starting resync.\n");
b30ab791 1464 _drbd_request_state(device, NS(conn, C_CONNECTED), CS_VERBOSE);
b8907339
PR
1465 return;
1466 }
1467
b30ab791 1468 switch (device->state.conn) {
b8907339 1469 case C_STARTING_SYNC_T:
b30ab791 1470 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
b8907339
PR
1471 break;
1472 case C_STARTING_SYNC_S:
b30ab791 1473 drbd_start_resync(device, C_SYNC_SOURCE);
b8907339
PR
1474 break;
1475 }
1476}
1477
b30ab791 1478int drbd_bitmap_io_from_worker(struct drbd_device *device,
54761697 1479 int (*io_fn)(struct drbd_device *),
b8907339
PR
1480 char *why, enum bm_flag flags)
1481{
1482 int rv;
1483
0b0ba1ef 1484 D_ASSERT(device, current == first_peer_device(device)->connection->worker.task);
b8907339 1485
b30ab791 1486 /* open coded non-blocking drbd_suspend_io(device); */
7dbb4386 1487 atomic_inc(&device->suspend_cnt);
b8907339 1488
b30ab791
AG
1489 drbd_bm_lock(device, why, flags);
1490 rv = io_fn(device);
1491 drbd_bm_unlock(device);
b8907339 1492
b30ab791 1493 drbd_resume_io(device);
b8907339
PR
1494
1495 return rv;
1496}
1497
a2972846
AG
1498void notify_resource_state_change(struct sk_buff *skb,
1499 unsigned int seq,
1500 struct drbd_resource_state_change *resource_state_change,
1501 enum drbd_notification_type type)
1502{
1503 struct drbd_resource *resource = resource_state_change->resource;
1504 struct resource_info resource_info = {
1505 .res_role = resource_state_change->role[NEW],
1506 .res_susp = resource_state_change->susp[NEW],
1507 .res_susp_nod = resource_state_change->susp_nod[NEW],
1508 .res_susp_fen = resource_state_change->susp_fen[NEW],
1509 };
1510
1511 notify_resource_state(skb, seq, resource, &resource_info, type);
1512}
1513
1514void notify_connection_state_change(struct sk_buff *skb,
1515 unsigned int seq,
1516 struct drbd_connection_state_change *connection_state_change,
1517 enum drbd_notification_type type)
1518{
1519 struct drbd_connection *connection = connection_state_change->connection;
1520 struct connection_info connection_info = {
1521 .conn_connection_state = connection_state_change->cstate[NEW],
1522 .conn_role = connection_state_change->peer_role[NEW],
1523 };
1524
1525 notify_connection_state(skb, seq, connection, &connection_info, type);
1526}
1527
1528void notify_device_state_change(struct sk_buff *skb,
1529 unsigned int seq,
1530 struct drbd_device_state_change *device_state_change,
1531 enum drbd_notification_type type)
1532{
1533 struct drbd_device *device = device_state_change->device;
1534 struct device_info device_info = {
1535 .dev_disk_state = device_state_change->disk_state[NEW],
1536 };
1537
1538 notify_device_state(skb, seq, device, &device_info, type);
1539}
1540
1541void notify_peer_device_state_change(struct sk_buff *skb,
1542 unsigned int seq,
1543 struct drbd_peer_device_state_change *p,
1544 enum drbd_notification_type type)
1545{
1546 struct drbd_peer_device *peer_device = p->peer_device;
1547 struct peer_device_info peer_device_info = {
1548 .peer_repl_state = p->repl_state[NEW],
1549 .peer_disk_state = p->disk_state[NEW],
1550 .peer_resync_susp_user = p->resync_susp_user[NEW],
1551 .peer_resync_susp_peer = p->resync_susp_peer[NEW],
1552 .peer_resync_susp_dependency = p->resync_susp_dependency[NEW],
1553 };
1554
1555 notify_peer_device_state(skb, seq, peer_device, &peer_device_info, type);
1556}
1557
1558static void broadcast_state_change(struct drbd_state_change *state_change)
1559{
1560 struct drbd_resource_state_change *resource_state_change = &state_change->resource[0];
1561 bool resource_state_has_changed;
1562 unsigned int n_device, n_connection, n_peer_device, n_peer_devices;
1563 void (*last_func)(struct sk_buff *, unsigned int, void *,
1564 enum drbd_notification_type) = NULL;
1565 void *uninitialized_var(last_arg);
1566
1567#define HAS_CHANGED(state) ((state)[OLD] != (state)[NEW])
1568#define FINAL_STATE_CHANGE(type) \
1569 ({ if (last_func) \
1570 last_func(NULL, 0, last_arg, type); \
1571 })
1572#define REMEMBER_STATE_CHANGE(func, arg, type) \
1573 ({ FINAL_STATE_CHANGE(type | NOTIFY_CONTINUES); \
1574 last_func = (typeof(last_func))func; \
1575 last_arg = arg; \
1576 })
1577
1578 mutex_lock(&notification_mutex);
1579
1580 resource_state_has_changed =
1581 HAS_CHANGED(resource_state_change->role) ||
1582 HAS_CHANGED(resource_state_change->susp) ||
1583 HAS_CHANGED(resource_state_change->susp_nod) ||
1584 HAS_CHANGED(resource_state_change->susp_fen);
1585
1586 if (resource_state_has_changed)
1587 REMEMBER_STATE_CHANGE(notify_resource_state_change,
1588 resource_state_change, NOTIFY_CHANGE);
1589
1590 for (n_connection = 0; n_connection < state_change->n_connections; n_connection++) {
1591 struct drbd_connection_state_change *connection_state_change =
1592 &state_change->connections[n_connection];
1593
1594 if (HAS_CHANGED(connection_state_change->peer_role) ||
1595 HAS_CHANGED(connection_state_change->cstate))
1596 REMEMBER_STATE_CHANGE(notify_connection_state_change,
1597 connection_state_change, NOTIFY_CHANGE);
1598 }
1599
1600 for (n_device = 0; n_device < state_change->n_devices; n_device++) {
1601 struct drbd_device_state_change *device_state_change =
1602 &state_change->devices[n_device];
1603
1604 if (HAS_CHANGED(device_state_change->disk_state))
1605 REMEMBER_STATE_CHANGE(notify_device_state_change,
1606 device_state_change, NOTIFY_CHANGE);
1607 }
1608
1609 n_peer_devices = state_change->n_devices * state_change->n_connections;
1610 for (n_peer_device = 0; n_peer_device < n_peer_devices; n_peer_device++) {
1611 struct drbd_peer_device_state_change *p =
1612 &state_change->peer_devices[n_peer_device];
1613
1614 if (HAS_CHANGED(p->disk_state) ||
1615 HAS_CHANGED(p->repl_state) ||
1616 HAS_CHANGED(p->resync_susp_user) ||
1617 HAS_CHANGED(p->resync_susp_peer) ||
1618 HAS_CHANGED(p->resync_susp_dependency))
1619 REMEMBER_STATE_CHANGE(notify_peer_device_state_change,
1620 p, NOTIFY_CHANGE);
1621 }
1622
1623 FINAL_STATE_CHANGE(NOTIFY_CHANGE);
1624 mutex_unlock(&notification_mutex);
1625
1626#undef HAS_CHANGED
1627#undef FINAL_STATE_CHANGE
1628#undef REMEMBER_STATE_CHANGE
1629}
1630
b8907339
PR
1631/**
1632 * after_state_ch() - Perform after state change actions that may sleep
b30ab791 1633 * @device: DRBD device.
b8907339
PR
1634 * @os: old state.
1635 * @ns: new state.
1636 * @flags: Flags
1637 */
b30ab791 1638static void after_state_ch(struct drbd_device *device, union drbd_state os,
a2972846
AG
1639 union drbd_state ns, enum chg_state_flags flags,
1640 struct drbd_state_change *state_change)
b8907339 1641{
6bbf53ca 1642 struct drbd_resource *resource = device->resource;
44a4d551
LE
1643 struct drbd_peer_device *peer_device = first_peer_device(device);
1644 struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
3b98c0c2
LE
1645 struct sib_info sib;
1646
a2972846
AG
1647 broadcast_state_change(state_change);
1648
3b98c0c2
LE
1649 sib.sib_reason = SIB_STATE_CHANGE;
1650 sib.os = os;
1651 sib.ns = ns;
b8907339 1652
2f2abeae
LE
1653 if ((os.disk != D_UP_TO_DATE || os.pdsk != D_UP_TO_DATE)
1654 && (ns.disk == D_UP_TO_DATE && ns.pdsk == D_UP_TO_DATE)) {
b30ab791
AG
1655 clear_bit(CRASHED_PRIMARY, &device->flags);
1656 if (device->p_uuid)
1657 device->p_uuid[UI_FLAGS] &= ~((u64)2);
b8907339
PR
1658 }
1659
b8907339 1660 /* Inform userspace about the change... */
b30ab791 1661 drbd_bcast_event(device, &sib);
b8907339
PR
1662
1663 if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
1664 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
b30ab791 1665 drbd_khelper(device, "pri-on-incon-degr");
b8907339
PR
1666
1667 /* Here we have the actions that are performed after a
1668 state change. This function might sleep */
1669
b8907339 1670 if (ns.susp_nod) {
a6d00c8e
PR
1671 enum drbd_req_event what = NOTHING;
1672
0500813f 1673 spin_lock_irq(&device->resource->req_lock);
bde89a9e 1674 if (os.conn < C_CONNECTED && conn_lowest_conn(connection) >= C_CONNECTED)
b8907339
PR
1675 what = RESEND;
1676
3fb4746d 1677 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
bde89a9e 1678 conn_lowest_disk(connection) > D_NEGOTIATING)
b8907339
PR
1679 what = RESTART_FROZEN_DISK_IO;
1680
6bbf53ca 1681 if (resource->susp_nod && what != NOTHING) {
bde89a9e
AG
1682 _tl_restart(connection, what);
1683 _conn_request_state(connection,
892fdd1a
PR
1684 (union drbd_state) { { .susp_nod = 1 } },
1685 (union drbd_state) { { .susp_nod = 0 } },
1686 CS_VERBOSE);
b8907339 1687 }
0500813f 1688 spin_unlock_irq(&device->resource->req_lock);
b8907339
PR
1689 }
1690
88f79ec4 1691 if (ns.susp_fen) {
0500813f 1692 spin_lock_irq(&device->resource->req_lock);
6bbf53ca 1693 if (resource->susp_fen && conn_lowest_conn(connection) >= C_CONNECTED) {
88f79ec4 1694 /* case2: The connection was established again: */
c06ece6b 1695 struct drbd_peer_device *peer_device;
88f79ec4
PR
1696 int vnr;
1697
1698 rcu_read_lock();
c06ece6b
AG
1699 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
1700 clear_bit(NEW_CUR_UUID, &peer_device->device->flags);
88f79ec4 1701 rcu_read_unlock();
bde89a9e
AG
1702 _tl_restart(connection, RESEND);
1703 _conn_request_state(connection,
88f79ec4
PR
1704 (union drbd_state) { { .susp_fen = 1 } },
1705 (union drbd_state) { { .susp_fen = 0 } },
1706 CS_VERBOSE);
1707 }
0500813f 1708 spin_unlock_irq(&device->resource->req_lock);
88f79ec4
PR
1709 }
1710
b8907339
PR
1711 /* Became sync source. With protocol >= 96, we still need to send out
1712 * the sync uuid now. Need to do that before any drbd_send_state, or
1713 * the other side may go "paused sync" before receiving the sync uuids,
1714 * which is unexpected. */
1715 if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
1716 (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
44a4d551
LE
1717 connection->agreed_pro_version >= 96 && get_ldev(device)) {
1718 drbd_gen_and_send_sync_uuid(peer_device);
b30ab791 1719 put_ldev(device);
b8907339
PR
1720 }
1721
1722 /* Do not change the order of the if above and the two below... */
369bea63
PR
1723 if (os.pdsk == D_DISKLESS &&
1724 ns.pdsk > D_DISKLESS && ns.pdsk != D_UNKNOWN) { /* attach on the peer */
a324896b
LE
1725 /* we probably will start a resync soon.
1726 * make sure those things are properly reset. */
b30ab791
AG
1727 device->rs_total = 0;
1728 device->rs_failed = 0;
1729 atomic_set(&device->rs_pending_cnt, 0);
1730 drbd_rs_cancel_all(device);
a324896b 1731
44a4d551
LE
1732 drbd_send_uuids(peer_device);
1733 drbd_send_state(peer_device, ns);
b8907339
PR
1734 }
1735 /* No point in queuing send_bitmap if we don't have a connection
1736 * anymore, so check also the _current_ state, not only the new state
1737 * at the time this work was queued. */
1738 if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
b30ab791
AG
1739 device->state.conn == C_WF_BITMAP_S)
1740 drbd_queue_bitmap_io(device, &drbd_send_bitmap, NULL,
b8907339
PR
1741 "send_bitmap (WFBitMapS)",
1742 BM_LOCKED_TEST_ALLOWED);
1743
1744 /* Lost contact to peer's copy of the data */
1745 if ((os.pdsk >= D_INCONSISTENT &&
1746 os.pdsk != D_UNKNOWN &&
1747 os.pdsk != D_OUTDATED)
1748 && (ns.pdsk < D_INCONSISTENT ||
1749 ns.pdsk == D_UNKNOWN ||
1750 ns.pdsk == D_OUTDATED)) {
b30ab791 1751 if (get_ldev(device)) {
b8907339 1752 if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
b30ab791
AG
1753 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1754 if (drbd_suspended(device)) {
1755 set_bit(NEW_CUR_UUID, &device->flags);
b8907339 1756 } else {
b30ab791 1757 drbd_uuid_new_current(device);
44a4d551 1758 drbd_send_uuids(peer_device);
b8907339
PR
1759 }
1760 }
b30ab791 1761 put_ldev(device);
b8907339
PR
1762 }
1763 }
1764
b30ab791 1765 if (ns.pdsk < D_INCONSISTENT && get_ldev(device)) {
9bd2eb2c 1766 if (os.peer != R_PRIMARY && ns.peer == R_PRIMARY &&
b30ab791
AG
1767 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1768 drbd_uuid_new_current(device);
44a4d551 1769 drbd_send_uuids(peer_device);
0cfac5dd 1770 }
b8907339
PR
1771 /* D_DISKLESS Peer becomes secondary */
1772 if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
1773 /* We may still be Primary ourselves.
1774 * No harm done if the bitmap still changes,
1775 * redirtied pages will follow later. */
b30ab791 1776 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
b8907339 1777 "demote diskless peer", BM_LOCKED_SET_ALLOWED);
b30ab791 1778 put_ldev(device);
b8907339
PR
1779 }
1780
1781 /* Write out all changed bits on demote.
1782 * Though, no need to da that just yet
1783 * if there is a resync going on still */
1784 if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
b30ab791 1785 device->state.conn <= C_CONNECTED && get_ldev(device)) {
b8907339
PR
1786 /* No changes to the bitmap expected this time, so assert that,
1787 * even though no harm was done if it did change. */
b30ab791 1788 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
b8907339 1789 "demote", BM_LOCKED_TEST_ALLOWED);
b30ab791 1790 put_ldev(device);
b8907339
PR
1791 }
1792
1793 /* Last part of the attaching process ... */
1794 if (ns.conn >= C_CONNECTED &&
1795 os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
44a4d551
LE
1796 drbd_send_sizes(peer_device, 0, 0); /* to start sync... */
1797 drbd_send_uuids(peer_device);
1798 drbd_send_state(peer_device, ns);
b8907339
PR
1799 }
1800
1801 /* We want to pause/continue resync, tell peer. */
1802 if (ns.conn >= C_CONNECTED &&
1803 ((os.aftr_isp != ns.aftr_isp) ||
1804 (os.user_isp != ns.user_isp)))
44a4d551 1805 drbd_send_state(peer_device, ns);
b8907339
PR
1806
1807 /* In case one of the isp bits got set, suspend other devices. */
1808 if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
1809 (ns.aftr_isp || ns.peer_isp || ns.user_isp))
b30ab791 1810 suspend_other_sg(device);
b8907339
PR
1811
1812 /* Make sure the peer gets informed about eventual state
1813 changes (ISP bits) while we were in WFReportParams. */
1814 if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
44a4d551 1815 drbd_send_state(peer_device, ns);
b8907339
PR
1816
1817 if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
44a4d551 1818 drbd_send_state(peer_device, ns);
b8907339
PR
1819
1820 /* We are in the progress to start a full sync... */
1821 if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
1822 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
1823 /* no other bitmap changes expected during this phase */
b30ab791 1824 drbd_queue_bitmap_io(device,
b8907339
PR
1825 &drbd_bmio_set_n_write, &abw_start_sync,
1826 "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
1827
b8907339
PR
1828 /* first half of local IO error, failure to attach,
1829 * or administrative detach */
1830 if (os.disk != D_FAILED && ns.disk == D_FAILED) {
32db80f6
PR
1831 enum drbd_io_error_p eh = EP_PASS_ON;
1832 int was_io_error = 0;
28bc3b8c 1833 /* corresponding get_ldev was in _drbd_set_state, to serialize
32db80f6
PR
1834 * our cleanup here with the transition to D_DISKLESS.
1835 * But is is still not save to dreference ldev here, since
1836 * we might come from an failed Attach before ldev was set. */
b30ab791 1837 if (device->ldev) {
32db80f6 1838 rcu_read_lock();
b30ab791 1839 eh = rcu_dereference(device->ldev->disk_conf)->on_io_error;
32db80f6
PR
1840 rcu_read_unlock();
1841
b30ab791 1842 was_io_error = test_and_clear_bit(WAS_IO_ERROR, &device->flags);
32db80f6 1843
dc99562a
LE
1844 /* Intentionally call this handler first, before drbd_send_state().
1845 * See: 2932204 drbd: call local-io-error handler early
1846 * People may chose to hard-reset the box from this handler.
1847 * It is useful if this looks like a "regular node crash". */
6f1a6563 1848 if (was_io_error && eh == EP_CALL_HELPER)
b30ab791 1849 drbd_khelper(device, "local-io-error");
6f1a6563 1850
0c849666
LE
1851 /* Immediately allow completion of all application IO,
1852 * that waits for completion from the local disk,
1853 * if this was a force-detach due to disk_timeout
1854 * or administrator request (drbdsetup detach --force).
1855 * Do NOT abort otherwise.
1856 * Aborting local requests may cause serious problems,
1857 * if requests are completed to upper layers already,
1858 * and then later the already submitted local bio completes.
1859 * This can cause DMA into former bio pages that meanwhile
1860 * have been re-used for other things.
1861 * So aborting local requests may cause crashes,
1862 * or even worse, silent data corruption.
1863 */
b30ab791
AG
1864 if (test_and_clear_bit(FORCE_DETACH, &device->flags))
1865 tl_abort_disk_io(device);
32db80f6
PR
1866
1867 /* current state still has to be D_FAILED,
1868 * there is only one way out: to D_DISKLESS,
1869 * and that may only happen after our put_ldev below. */
b30ab791 1870 if (device->state.disk != D_FAILED)
d0180171 1871 drbd_err(device,
32db80f6 1872 "ASSERT FAILED: disk is %s during detach\n",
b30ab791 1873 drbd_disk_str(device->state.disk));
32db80f6
PR
1874
1875 if (ns.conn >= C_CONNECTED)
44a4d551 1876 drbd_send_state(peer_device, ns);
32db80f6 1877
b30ab791 1878 drbd_rs_cancel_all(device);
32db80f6
PR
1879
1880 /* In case we want to get something to stable storage still,
1881 * this may be the last chance.
1882 * Following put_ldev may transition to D_DISKLESS. */
b30ab791 1883 drbd_md_sync(device);
32db80f6 1884 }
b30ab791 1885 put_ldev(device);
b8907339
PR
1886 }
1887
b30ab791
AG
1888 /* second half of local IO error, failure to attach,
1889 * or administrative detach,
1890 * after local_cnt references have reached zero again */
1891 if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
1892 /* We must still be diskless,
1893 * re-attach has to be serialized with this! */
1894 if (device->state.disk != D_DISKLESS)
d0180171
AG
1895 drbd_err(device,
1896 "ASSERT FAILED: disk is %s while going diskless\n",
1897 drbd_disk_str(device->state.disk));
b8907339 1898
6ab9b1b6 1899 if (ns.conn >= C_CONNECTED)
44a4d551 1900 drbd_send_state(peer_device, ns);
b8907339
PR
1901 /* corresponding get_ldev in __drbd_set_state
1902 * this may finally trigger drbd_ldev_destroy. */
b30ab791 1903 put_ldev(device);
b8907339
PR
1904 }
1905
1906 /* Notify peer that I had a local IO error, and did not detached.. */
6ab9b1b6 1907 if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT && ns.conn >= C_CONNECTED)
44a4d551 1908 drbd_send_state(peer_device, ns);
b8907339
PR
1909
1910 /* Disks got bigger while they were detached */
1911 if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
b30ab791 1912 test_and_clear_bit(RESYNC_AFTER_NEG, &device->flags)) {
b8907339 1913 if (ns.conn == C_CONNECTED)
b30ab791 1914 resync_after_online_grow(device);
b8907339
PR
1915 }
1916
1917 /* A resync finished or aborted, wake paused devices... */
1918 if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
1919 (os.peer_isp && !ns.peer_isp) ||
1920 (os.user_isp && !ns.user_isp))
b30ab791 1921 resume_next_sg(device);
b8907339
PR
1922
1923 /* sync target done with resync. Explicitly notify peer, even though
1924 * it should (at least for non-empty resyncs) already know itself. */
1925 if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
44a4d551 1926 drbd_send_state(peer_device, ns);
b8907339 1927
58ffa580
LE
1928 /* Verify finished, or reached stop sector. Peer did not know about
1929 * the stop sector, and we may even have changed the stop sector during
1930 * verify to interrupt/stop early. Send the new state. */
1931 if (os.conn == C_VERIFY_S && ns.conn == C_CONNECTED
b30ab791 1932 && verify_can_do_stop_sector(device))
44a4d551 1933 drbd_send_state(peer_device, ns);
58ffa580 1934
b8907339
PR
1935 /* This triggers bitmap writeout of potentially still unwritten pages
1936 * if the resync finished cleanly, or aborted because of peer disk
1937 * failure, or because of connection loss.
1938 * For resync aborted because of local disk failure, we cannot do
1939 * any bitmap writeout anymore.
1940 * No harm done if some bits change during this phase.
1941 */
b30ab791
AG
1942 if (os.conn > C_CONNECTED && ns.conn <= C_CONNECTED && get_ldev(device)) {
1943 drbd_queue_bitmap_io(device, &drbd_bm_write_copy_pages, NULL,
a220d291 1944 "write from resync_finished", BM_LOCKED_CHANGE_ALLOWED);
b30ab791 1945 put_ldev(device);
b8907339
PR
1946 }
1947
1948 if (ns.disk == D_DISKLESS &&
1949 ns.conn == C_STANDALONE &&
1950 ns.role == R_SECONDARY) {
1951 if (os.aftr_isp != ns.aftr_isp)
b30ab791 1952 resume_next_sg(device);
b8907339
PR
1953 }
1954
b30ab791 1955 drbd_md_sync(device);
b8907339
PR
1956}
1957
bbeb641c 1958struct after_conn_state_chg_work {
4c007603 1959 struct drbd_work w;
bbeb641c 1960 enum drbd_conns oc;
8c7e16c3 1961 union drbd_state ns_min;
b30ab791 1962 union drbd_state ns_max; /* new, max state, over all devices */
bbeb641c 1963 enum chg_state_flags flags;
84b8c06b 1964 struct drbd_connection *connection;
a2972846 1965 struct drbd_state_change *state_change;
bbeb641c
PR
1966};
1967
99920dc5 1968static int w_after_conn_state_ch(struct drbd_work *w, int unused)
bbeb641c
PR
1969{
1970 struct after_conn_state_chg_work *acscw =
4c007603 1971 container_of(w, struct after_conn_state_chg_work, w);
84b8c06b 1972 struct drbd_connection *connection = acscw->connection;
bbeb641c 1973 enum drbd_conns oc = acscw->oc;
5f082f98 1974 union drbd_state ns_max = acscw->ns_max;
c06ece6b 1975 struct drbd_peer_device *peer_device;
a6d00c8e 1976 int vnr;
bbeb641c 1977
a2972846
AG
1978 broadcast_state_change(acscw->state_change);
1979 forget_state_change(acscw->state_change);
bbeb641c
PR
1980 kfree(acscw);
1981
b8907339 1982 /* Upon network configuration, we need to start the receiver */
5f082f98 1983 if (oc == C_STANDALONE && ns_max.conn == C_UNCONNECTED)
bde89a9e 1984 drbd_thread_start(&connection->receiver);
b8907339 1985
f3dfa40a
LE
1986 if (oc == C_DISCONNECTING && ns_max.conn == C_STANDALONE) {
1987 struct net_conf *old_conf;
1988
a2972846
AG
1989 mutex_lock(&notification_mutex);
1990 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
1991 notify_peer_device_state(NULL, 0, peer_device, NULL,
1992 NOTIFY_DESTROY | NOTIFY_CONTINUES);
1993 notify_connection_state(NULL, 0, connection, NULL, NOTIFY_DESTROY);
1994 mutex_unlock(&notification_mutex);
1995
0500813f 1996 mutex_lock(&connection->resource->conf_update);
bde89a9e
AG
1997 old_conf = connection->net_conf;
1998 connection->my_addr_len = 0;
1999 connection->peer_addr_len = 0;
ccdd6a93 2000 RCU_INIT_POINTER(connection->net_conf, NULL);
bde89a9e 2001 conn_free_crypto(connection);
0500813f 2002 mutex_unlock(&connection->resource->conf_update);
f3dfa40a
LE
2003
2004 synchronize_rcu();
2005 kfree(old_conf);
2006 }
2007
a6d00c8e
PR
2008 if (ns_max.susp_fen) {
2009 /* case1: The outdate peer handler is successful: */
2010 if (ns_max.pdsk <= D_OUTDATED) {
695d08fa 2011 rcu_read_lock();
c06ece6b
AG
2012 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2013 struct drbd_device *device = peer_device->device;
b30ab791
AG
2014 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2015 drbd_uuid_new_current(device);
2016 clear_bit(NEW_CUR_UUID, &device->flags);
a6d00c8e
PR
2017 }
2018 }
695d08fa 2019 rcu_read_unlock();
0500813f 2020 spin_lock_irq(&connection->resource->req_lock);
bde89a9e
AG
2021 _tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
2022 _conn_request_state(connection,
8a0bab2a
PR
2023 (union drbd_state) { { .susp_fen = 1 } },
2024 (union drbd_state) { { .susp_fen = 0 } },
2025 CS_VERBOSE);
0500813f 2026 spin_unlock_irq(&connection->resource->req_lock);
a6d00c8e 2027 }
a6d00c8e 2028 }
05a10ec7 2029 kref_put(&connection->kref, drbd_destroy_connection);
19fffd7b 2030
bde89a9e 2031 conn_md_sync(connection);
19fffd7b 2032
99920dc5 2033 return 0;
bbeb641c
PR
2034}
2035
8ce953aa 2036static void conn_old_common_state(struct drbd_connection *connection, union drbd_state *pcs, enum chg_state_flags *pf)
88ef594e 2037{
435693e8 2038 enum chg_state_flags flags = ~0;
c06ece6b 2039 struct drbd_peer_device *peer_device;
435693e8 2040 int vnr, first_vol = 1;
e0e16653
PR
2041 union drbd_dev_state os, cs = {
2042 { .role = R_SECONDARY,
2043 .peer = R_UNKNOWN,
bde89a9e 2044 .conn = connection->cstate,
e0e16653
PR
2045 .disk = D_DISKLESS,
2046 .pdsk = D_UNKNOWN,
2047 } };
88ef594e 2048
695d08fa 2049 rcu_read_lock();
c06ece6b
AG
2050 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2051 struct drbd_device *device = peer_device->device;
b30ab791 2052 os = device->state;
88ef594e 2053
435693e8
PR
2054 if (first_vol) {
2055 cs = os;
2056 first_vol = 0;
2057 continue;
2058 }
2059
2060 if (cs.role != os.role)
2061 flags &= ~CS_DC_ROLE;
2062
2063 if (cs.peer != os.peer)
2064 flags &= ~CS_DC_PEER;
2065
2066 if (cs.conn != os.conn)
2067 flags &= ~CS_DC_CONN;
88ef594e 2068
435693e8
PR
2069 if (cs.disk != os.disk)
2070 flags &= ~CS_DC_DISK;
88ef594e 2071
435693e8
PR
2072 if (cs.pdsk != os.pdsk)
2073 flags &= ~CS_DC_PDSK;
2074 }
695d08fa 2075 rcu_read_unlock();
435693e8
PR
2076
2077 *pf |= CS_DC_MASK;
2078 *pf &= flags;
da9fbc27 2079 (*pcs).i = cs.i;
88ef594e 2080}
bbeb641c 2081
bd0c824a 2082static enum drbd_state_rv
bde89a9e 2083conn_is_valid_transition(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
88ef594e 2084 enum chg_state_flags flags)
bbeb641c 2085{
bd0c824a 2086 enum drbd_state_rv rv = SS_SUCCESS;
bbeb641c 2087 union drbd_state ns, os;
c06ece6b 2088 struct drbd_peer_device *peer_device;
bd0c824a 2089 int vnr;
bbeb641c 2090
695d08fa 2091 rcu_read_lock();
c06ece6b
AG
2092 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2093 struct drbd_device *device = peer_device->device;
b30ab791 2094 os = drbd_read_state(device);
d7fe69c6 2095 ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
bd0c824a 2096
778bcf2e
PR
2097 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
2098 ns.disk = os.disk;
2099
bd0c824a
PR
2100 if (ns.i == os.i)
2101 continue;
bbeb641c 2102
bd0c824a 2103 rv = is_valid_transition(os, ns);
bd0c824a 2104
c06ece6b 2105 if (rv >= SS_SUCCESS && !(flags & CS_HARD)) {
b30ab791 2106 rv = is_valid_state(device, ns);
bd0c824a 2107 if (rv < SS_SUCCESS) {
b30ab791 2108 if (is_valid_state(device, os) == rv)
bde89a9e 2109 rv = is_valid_soft_transition(os, ns, connection);
bd0c824a 2110 } else
bde89a9e 2111 rv = is_valid_soft_transition(os, ns, connection);
bd0c824a 2112 }
c06ece6b
AG
2113
2114 if (rv < SS_SUCCESS) {
2115 if (flags & CS_VERBOSE)
2116 print_st_err(device, os, ns, rv);
bd0c824a 2117 break;
c06ece6b 2118 }
bbeb641c 2119 }
695d08fa 2120 rcu_read_unlock();
bbeb641c 2121
bd0c824a 2122 return rv;
bbeb641c
PR
2123}
2124
8ce953aa 2125static void
bde89a9e 2126conn_set_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
8c7e16c3 2127 union drbd_state *pns_min, union drbd_state *pns_max, enum chg_state_flags flags)
bbeb641c 2128{
f132f554 2129 union drbd_state ns, os, ns_max = { };
8c7e16c3
PR
2130 union drbd_state ns_min = {
2131 { .role = R_MASK,
2132 .peer = R_MASK,
e0e16653 2133 .conn = val.conn,
8c7e16c3
PR
2134 .disk = D_MASK,
2135 .pdsk = D_MASK
2136 } };
c06ece6b 2137 struct drbd_peer_device *peer_device;
bbeb641c 2138 enum drbd_state_rv rv;
f132f554 2139 int vnr, number_of_volumes = 0;
bbeb641c 2140
07be15b1
LE
2141 if (mask.conn == C_MASK) {
2142 /* remember last connect time so request_timer_fn() won't
2143 * kill newly established sessions while we are still trying to thaw
2144 * previously frozen IO */
bde89a9e
AG
2145 if (connection->cstate != C_WF_REPORT_PARAMS && val.conn == C_WF_REPORT_PARAMS)
2146 connection->last_reconnect_jif = jiffies;
07be15b1 2147
bde89a9e 2148 connection->cstate = val.conn;
07be15b1 2149 }
bd0c824a 2150
695d08fa 2151 rcu_read_lock();
c06ece6b
AG
2152 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2153 struct drbd_device *device = peer_device->device;
f132f554 2154 number_of_volumes++;
b30ab791 2155 os = drbd_read_state(device);
bd0c824a 2156 ns = apply_mask_val(os, mask, val);
d7fe69c6 2157 ns = sanitize_state(device, os, ns, NULL);
bbeb641c 2158
778bcf2e
PR
2159 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
2160 ns.disk = os.disk;
2161
28bc3b8c 2162 rv = _drbd_set_state(device, ns, flags, NULL);
bd0c824a
PR
2163 if (rv < SS_SUCCESS)
2164 BUG();
bbeb641c 2165
b30ab791 2166 ns.i = device->state.i;
8c7e16c3
PR
2167 ns_max.role = max_role(ns.role, ns_max.role);
2168 ns_max.peer = max_role(ns.peer, ns_max.peer);
2169 ns_max.conn = max_t(enum drbd_conns, ns.conn, ns_max.conn);
2170 ns_max.disk = max_t(enum drbd_disk_state, ns.disk, ns_max.disk);
2171 ns_max.pdsk = max_t(enum drbd_disk_state, ns.pdsk, ns_max.pdsk);
2172
2173 ns_min.role = min_role(ns.role, ns_min.role);
2174 ns_min.peer = min_role(ns.peer, ns_min.peer);
2175 ns_min.conn = min_t(enum drbd_conns, ns.conn, ns_min.conn);
2176 ns_min.disk = min_t(enum drbd_disk_state, ns.disk, ns_min.disk);
2177 ns_min.pdsk = min_t(enum drbd_disk_state, ns.pdsk, ns_min.pdsk);
bd0c824a 2178 }
695d08fa 2179 rcu_read_unlock();
bbeb641c 2180
f132f554
PR
2181 if (number_of_volumes == 0) {
2182 ns_min = ns_max = (union drbd_state) { {
2183 .role = R_SECONDARY,
2184 .peer = R_UNKNOWN,
2185 .conn = val.conn,
2186 .disk = D_DISKLESS,
2187 .pdsk = D_UNKNOWN
2188 } };
2189 }
2190
6bbf53ca
AG
2191 ns_min.susp = ns_max.susp = connection->resource->susp;
2192 ns_min.susp_nod = ns_max.susp_nod = connection->resource->susp_nod;
2193 ns_min.susp_fen = ns_max.susp_fen = connection->resource->susp_fen;
8c7e16c3
PR
2194
2195 *pns_min = ns_min;
2196 *pns_max = ns_max;
bbeb641c
PR
2197}
2198
df24aa45 2199static enum drbd_state_rv
bde89a9e 2200_conn_rq_cond(struct drbd_connection *connection, union drbd_state mask, union drbd_state val)
df24aa45 2201{
88ea685d 2202 enum drbd_state_rv err, rv = SS_UNKNOWN_ERROR; /* continue waiting */;
df24aa45 2203
bde89a9e 2204 if (test_and_clear_bit(CONN_WD_ST_CHG_OKAY, &connection->flags))
88ea685d 2205 rv = SS_CW_SUCCESS;
df24aa45 2206
bde89a9e 2207 if (test_and_clear_bit(CONN_WD_ST_CHG_FAIL, &connection->flags))
88ea685d 2208 rv = SS_CW_FAILED_BY_PEER;
df24aa45 2209
88ea685d
PR
2210 err = conn_is_valid_transition(connection, mask, val, 0);
2211 if (err == SS_SUCCESS && connection->cstate == C_WF_REPORT_PARAMS)
2212 return rv;
df24aa45 2213
88ea685d 2214 return err;
df24aa45
PR
2215}
2216
bbeb641c 2217enum drbd_state_rv
bde89a9e 2218_conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
bbeb641c
PR
2219 enum chg_state_flags flags)
2220{
2221 enum drbd_state_rv rv = SS_SUCCESS;
bbeb641c 2222 struct after_conn_state_chg_work *acscw;
bde89a9e 2223 enum drbd_conns oc = connection->cstate;
8c7e16c3 2224 union drbd_state ns_max, ns_min, os;
c02abda2 2225 bool have_mutex = false;
a2972846 2226 struct drbd_state_change *state_change;
bbeb641c 2227
07fc9619
PR
2228 if (mask.conn) {
2229 rv = is_valid_conn_transition(oc, val.conn);
2230 if (rv < SS_SUCCESS)
2231 goto abort;
2232 }
bbeb641c 2233
bde89a9e 2234 rv = conn_is_valid_transition(connection, mask, val, flags);
bbeb641c
PR
2235 if (rv < SS_SUCCESS)
2236 goto abort;
2237
df24aa45
PR
2238 if (oc == C_WF_REPORT_PARAMS && val.conn == C_DISCONNECTING &&
2239 !(flags & (CS_LOCAL_ONLY | CS_HARD))) {
c02abda2
LE
2240
2241 /* This will be a cluster-wide state change.
2242 * Need to give up the spinlock, grab the mutex,
2243 * then send the state change request, ... */
0500813f 2244 spin_unlock_irq(&connection->resource->req_lock);
bde89a9e 2245 mutex_lock(&connection->cstate_mutex);
c02abda2
LE
2246 have_mutex = true;
2247
bde89a9e
AG
2248 set_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
2249 if (conn_send_state_req(connection, mask, val)) {
c02abda2 2250 /* sending failed. */
bde89a9e 2251 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
c02abda2
LE
2252 rv = SS_CW_FAILED_BY_PEER;
2253 /* need to re-aquire the spin lock, though */
2254 goto abort_unlocked;
2255 }
2256
2257 if (val.conn == C_DISCONNECTING)
bde89a9e 2258 set_bit(DISCONNECT_SENT, &connection->flags);
c02abda2
LE
2259
2260 /* ... and re-aquire the spinlock.
2261 * If _conn_rq_cond() returned >= SS_SUCCESS, we must call
2262 * conn_set_state() within the same spinlock. */
0500813f 2263 spin_lock_irq(&connection->resource->req_lock);
bde89a9e
AG
2264 wait_event_lock_irq(connection->ping_wait,
2265 (rv = _conn_rq_cond(connection, mask, val)),
0500813f 2266 connection->resource->req_lock);
bde89a9e 2267 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
df24aa45
PR
2268 if (rv < SS_SUCCESS)
2269 goto abort;
2270 }
2271
a2972846 2272 state_change = remember_old_state(connection->resource, GFP_ATOMIC);
bde89a9e 2273 conn_old_common_state(connection, &os, &flags);
706cb24c 2274 flags |= CS_DC_SUSP;
bde89a9e
AG
2275 conn_set_state(connection, mask, val, &ns_min, &ns_max, flags);
2276 conn_pr_state_change(connection, os, ns_max, flags);
a2972846 2277 remember_new_state(state_change);
bbeb641c
PR
2278
2279 acscw = kmalloc(sizeof(*acscw), GFP_ATOMIC);
2280 if (acscw) {
435693e8 2281 acscw->oc = os.conn;
8c7e16c3 2282 acscw->ns_min = ns_min;
5f082f98 2283 acscw->ns_max = ns_max;
bbeb641c 2284 acscw->flags = flags;
4c007603 2285 acscw->w.cb = w_after_conn_state_ch;
bde89a9e 2286 kref_get(&connection->kref);
84b8c06b 2287 acscw->connection = connection;
a2972846 2288 acscw->state_change = state_change;
4c007603 2289 drbd_queue_work(&connection->sender_work, &acscw->w);
bbeb641c 2290 } else {
1ec861eb 2291 drbd_err(connection, "Could not kmalloc an acscw\n");
b8907339 2292 }
bbeb641c 2293
a01842eb 2294 abort:
c02abda2
LE
2295 if (have_mutex) {
2296 /* mutex_unlock() "... must not be used in interrupt context.",
2297 * so give up the spinlock, then re-aquire it */
0500813f 2298 spin_unlock_irq(&connection->resource->req_lock);
c02abda2 2299 abort_unlocked:
bde89a9e 2300 mutex_unlock(&connection->cstate_mutex);
0500813f 2301 spin_lock_irq(&connection->resource->req_lock);
c02abda2
LE
2302 }
2303 if (rv < SS_SUCCESS && flags & CS_VERBOSE) {
1ec861eb
AG
2304 drbd_err(connection, "State change failed: %s\n", drbd_set_st_err_str(rv));
2305 drbd_err(connection, " mask = 0x%x val = 0x%x\n", mask.i, val.i);
2306 drbd_err(connection, " old_conn:%s wanted_conn:%s\n", drbd_conn_str(oc), drbd_conn_str(val.conn));
a01842eb 2307 }
bbeb641c
PR
2308 return rv;
2309}
2310
2311enum drbd_state_rv
bde89a9e 2312conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
bbeb641c
PR
2313 enum chg_state_flags flags)
2314{
2315 enum drbd_state_rv rv;
2316
0500813f 2317 spin_lock_irq(&connection->resource->req_lock);
bde89a9e 2318 rv = _conn_request_state(connection, mask, val, flags);
0500813f 2319 spin_unlock_irq(&connection->resource->req_lock);
bbeb641c
PR
2320
2321 return rv;
b8907339 2322}
This page took 0.302369 seconds and 5 git commands to generate.