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