Merge remote-tracking branches 'asoc/topic/tlv320aic3x', 'asoc/topic/width', 'asoc...
[linux-2.6-block.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
PR
31#include "drbd_req.h"
32
33struct after_state_chg_work {
34 struct drbd_work w;
84b8c06b 35 struct drbd_device *device;
b8907339
PR
36 union drbd_state os;
37 union drbd_state ns;
38 enum chg_state_flags flags;
39 struct completion *done;
40};
41
d942ae44
PR
42enum sanitize_state_warnings {
43 NO_WARNING,
44 ABORTED_ONLINE_VERIFY,
45 ABORTED_RESYNC,
46 CONNECTION_LOST_NEGOTIATING,
47 IMPLICITLY_UPGRADED_DISK,
48 IMPLICITLY_UPGRADED_PDSK,
49};
50
99920dc5 51static int w_after_state_ch(struct drbd_work *w, int unused);
b30ab791 52static void after_state_ch(struct drbd_device *device, union drbd_state os,
b8907339 53 union drbd_state ns, enum chg_state_flags flags);
54761697 54static enum drbd_state_rv is_valid_state(struct drbd_device *, union drbd_state);
bde89a9e 55static enum drbd_state_rv is_valid_soft_transition(union drbd_state, union drbd_state, struct drbd_connection *);
3509502d 56static enum drbd_state_rv is_valid_transition(union drbd_state os, union drbd_state ns);
d7fe69c6
PR
57static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state os,
58 union drbd_state ns, enum sanitize_state_warnings *warn);
b8907339 59
2aebfabb
PR
60static inline bool is_susp(union drbd_state s)
61{
62 return s.susp || s.susp_nod || s.susp_fen;
63}
64
bde89a9e 65bool conn_all_vols_unconf(struct drbd_connection *connection)
0e29d163 66{
c06ece6b 67 struct drbd_peer_device *peer_device;
695d08fa 68 bool rv = true;
e90285e0 69 int vnr;
0e29d163 70
695d08fa 71 rcu_read_lock();
c06ece6b
AG
72 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
73 struct drbd_device *device = peer_device->device;
b30ab791
AG
74 if (device->state.disk != D_DISKLESS ||
75 device->state.conn != C_STANDALONE ||
76 device->state.role != R_SECONDARY) {
695d08fa
PR
77 rv = false;
78 break;
79 }
0e29d163 80 }
695d08fa
PR
81 rcu_read_unlock();
82
83 return rv;
0e29d163
PR
84}
85
cb703454
PR
86/* Unfortunately the states where not correctly ordered, when
87 they where defined. therefore can not use max_t() here. */
88static enum drbd_role max_role(enum drbd_role role1, enum drbd_role role2)
89{
90 if (role1 == R_PRIMARY || role2 == R_PRIMARY)
91 return R_PRIMARY;
92 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
93 return R_SECONDARY;
94 return R_UNKNOWN;
95}
96static enum drbd_role min_role(enum drbd_role role1, enum drbd_role role2)
97{
98 if (role1 == R_UNKNOWN || role2 == R_UNKNOWN)
99 return R_UNKNOWN;
100 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
101 return R_SECONDARY;
102 return R_PRIMARY;
103}
104
bde89a9e 105enum drbd_role conn_highest_role(struct drbd_connection *connection)
cb703454
PR
106{
107 enum drbd_role role = R_UNKNOWN;
c06ece6b 108 struct drbd_peer_device *peer_device;
cb703454
PR
109 int vnr;
110
695d08fa 111 rcu_read_lock();
c06ece6b
AG
112 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
113 struct drbd_device *device = peer_device->device;
b30ab791 114 role = max_role(role, device->state.role);
c06ece6b 115 }
695d08fa 116 rcu_read_unlock();
cb703454
PR
117
118 return role;
119}
120
bde89a9e 121enum drbd_role conn_highest_peer(struct drbd_connection *connection)
cb703454
PR
122{
123 enum drbd_role peer = R_UNKNOWN;
c06ece6b 124 struct drbd_peer_device *peer_device;
cb703454
PR
125 int vnr;
126
695d08fa 127 rcu_read_lock();
c06ece6b
AG
128 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
129 struct drbd_device *device = peer_device->device;
b30ab791 130 peer = max_role(peer, device->state.peer);
c06ece6b 131 }
695d08fa 132 rcu_read_unlock();
cb703454
PR
133
134 return peer;
135}
136
bde89a9e 137enum drbd_disk_state conn_highest_disk(struct drbd_connection *connection)
cb703454
PR
138{
139 enum drbd_disk_state ds = D_DISKLESS;
c06ece6b 140 struct drbd_peer_device *peer_device;
cb703454
PR
141 int vnr;
142
695d08fa 143 rcu_read_lock();
c06ece6b
AG
144 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
145 struct drbd_device *device = peer_device->device;
b30ab791 146 ds = max_t(enum drbd_disk_state, ds, device->state.disk);
c06ece6b 147 }
695d08fa 148 rcu_read_unlock();
cb703454
PR
149
150 return ds;
151}
152
bde89a9e 153enum drbd_disk_state conn_lowest_disk(struct drbd_connection *connection)
4669265a
PR
154{
155 enum drbd_disk_state ds = D_MASK;
c06ece6b 156 struct drbd_peer_device *peer_device;
4669265a
PR
157 int vnr;
158
695d08fa 159 rcu_read_lock();
c06ece6b
AG
160 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
161 struct drbd_device *device = peer_device->device;
b30ab791 162 ds = min_t(enum drbd_disk_state, ds, device->state.disk);
c06ece6b 163 }
695d08fa 164 rcu_read_unlock();
4669265a
PR
165
166 return ds;
167}
168
bde89a9e 169enum drbd_disk_state conn_highest_pdsk(struct drbd_connection *connection)
cb703454
PR
170{
171 enum drbd_disk_state ds = D_DISKLESS;
c06ece6b 172 struct drbd_peer_device *peer_device;
cb703454
PR
173 int vnr;
174
695d08fa 175 rcu_read_lock();
c06ece6b
AG
176 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
177 struct drbd_device *device = peer_device->device;
b30ab791 178 ds = max_t(enum drbd_disk_state, ds, device->state.pdsk);
c06ece6b 179 }
695d08fa 180 rcu_read_unlock();
cb703454
PR
181
182 return ds;
183}
184
bde89a9e 185enum drbd_conns conn_lowest_conn(struct drbd_connection *connection)
19f83c76
PR
186{
187 enum drbd_conns conn = C_MASK;
c06ece6b 188 struct drbd_peer_device *peer_device;
19f83c76
PR
189 int vnr;
190
695d08fa 191 rcu_read_lock();
c06ece6b
AG
192 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
193 struct drbd_device *device = peer_device->device;
b30ab791 194 conn = min_t(enum drbd_conns, conn, device->state.conn);
c06ece6b 195 }
695d08fa 196 rcu_read_unlock();
19f83c76
PR
197
198 return conn;
199}
200
bde89a9e 201static bool no_peer_wf_report_params(struct drbd_connection *connection)
79702011 202{
c06ece6b 203 struct drbd_peer_device *peer_device;
79702011
PR
204 int vnr;
205 bool rv = true;
206
207 rcu_read_lock();
c06ece6b
AG
208 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
209 if (peer_device->device->state.conn == C_WF_REPORT_PARAMS) {
79702011
PR
210 rv = false;
211 break;
212 }
213 rcu_read_unlock();
214
215 return rv;
216}
217
218
b8907339
PR
219/**
220 * cl_wide_st_chg() - true if the state change is a cluster wide one
b30ab791 221 * @device: DRBD device.
b8907339
PR
222 * @os: old (current) state.
223 * @ns: new (wanted) state.
224 */
b30ab791 225static int cl_wide_st_chg(struct drbd_device *device,
b8907339
PR
226 union drbd_state os, union drbd_state ns)
227{
228 return (os.conn >= C_CONNECTED && ns.conn >= C_CONNECTED &&
229 ((os.role != R_PRIMARY && ns.role == R_PRIMARY) ||
230 (os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
231 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S) ||
e8744f5a 232 (os.disk != D_FAILED && ns.disk == D_FAILED))) ||
b8907339 233 (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
369bea63
PR
234 (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S) ||
235 (os.conn == C_CONNECTED && ns.conn == C_WF_REPORT_PARAMS);
b8907339
PR
236}
237
56707f9e
PR
238static union drbd_state
239apply_mask_val(union drbd_state os, union drbd_state mask, union drbd_state val)
240{
241 union drbd_state ns;
242 ns.i = (os.i & ~mask.i) | val.i;
243 return ns;
244}
245
b8907339 246enum drbd_state_rv
b30ab791 247drbd_change_state(struct drbd_device *device, enum chg_state_flags f,
b8907339
PR
248 union drbd_state mask, union drbd_state val)
249{
250 unsigned long flags;
56707f9e 251 union drbd_state ns;
b8907339
PR
252 enum drbd_state_rv rv;
253
0500813f 254 spin_lock_irqsave(&device->resource->req_lock, flags);
b30ab791
AG
255 ns = apply_mask_val(drbd_read_state(device), mask, val);
256 rv = _drbd_set_state(device, ns, f, NULL);
0500813f 257 spin_unlock_irqrestore(&device->resource->req_lock, flags);
b8907339
PR
258
259 return rv;
260}
261
262/**
263 * drbd_force_state() - Impose a change which happens outside our control on our state
b30ab791 264 * @device: DRBD device.
b8907339
PR
265 * @mask: mask of state bits to change.
266 * @val: value of new state bits.
267 */
b30ab791 268void drbd_force_state(struct drbd_device *device,
b8907339
PR
269 union drbd_state mask, union drbd_state val)
270{
b30ab791 271 drbd_change_state(device, CS_HARD, mask, val);
b8907339
PR
272}
273
274static enum drbd_state_rv
b30ab791 275_req_st_cond(struct drbd_device *device, union drbd_state mask,
b8907339
PR
276 union drbd_state val)
277{
278 union drbd_state os, ns;
279 unsigned long flags;
280 enum drbd_state_rv rv;
281
b30ab791 282 if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &device->flags))
b8907339
PR
283 return SS_CW_SUCCESS;
284
b30ab791 285 if (test_and_clear_bit(CL_ST_CHG_FAIL, &device->flags))
b8907339
PR
286 return SS_CW_FAILED_BY_PEER;
287
0500813f 288 spin_lock_irqsave(&device->resource->req_lock, flags);
b30ab791 289 os = drbd_read_state(device);
d7fe69c6 290 ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
3509502d 291 rv = is_valid_transition(os, ns);
a3025a27 292 if (rv >= SS_SUCCESS)
3509502d 293 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
b8907339 294
b30ab791 295 if (!cl_wide_st_chg(device, os, ns))
b8907339 296 rv = SS_CW_NO_NEED;
3509502d 297 if (rv == SS_UNKNOWN_ERROR) {
b30ab791 298 rv = is_valid_state(device, ns);
a3025a27 299 if (rv >= SS_SUCCESS) {
a6b32bc3 300 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
a3025a27 301 if (rv >= SS_SUCCESS)
b8907339
PR
302 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
303 }
304 }
0500813f 305 spin_unlock_irqrestore(&device->resource->req_lock, flags);
b8907339
PR
306
307 return rv;
308}
309
310/**
311 * drbd_req_state() - Perform an eventually cluster wide state change
b30ab791 312 * @device: DRBD device.
b8907339
PR
313 * @mask: mask of state bits to change.
314 * @val: value of new state bits.
315 * @f: flags
316 *
317 * Should not be called directly, use drbd_request_state() or
318 * _drbd_request_state().
319 */
320static enum drbd_state_rv
b30ab791 321drbd_req_state(struct drbd_device *device, union drbd_state mask,
b8907339
PR
322 union drbd_state val, enum chg_state_flags f)
323{
324 struct completion done;
325 unsigned long flags;
326 union drbd_state os, ns;
327 enum drbd_state_rv rv;
328
329 init_completion(&done);
330
331 if (f & CS_SERIALIZE)
b30ab791 332 mutex_lock(device->state_mutex);
b8907339 333
0500813f 334 spin_lock_irqsave(&device->resource->req_lock, flags);
b30ab791 335 os = drbd_read_state(device);
d7fe69c6 336 ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
3509502d 337 rv = is_valid_transition(os, ns);
3c5e5f6a 338 if (rv < SS_SUCCESS) {
0500813f 339 spin_unlock_irqrestore(&device->resource->req_lock, flags);
3509502d 340 goto abort;
3c5e5f6a 341 }
b8907339 342
b30ab791
AG
343 if (cl_wide_st_chg(device, os, ns)) {
344 rv = is_valid_state(device, ns);
b8907339 345 if (rv == SS_SUCCESS)
a6b32bc3 346 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
0500813f 347 spin_unlock_irqrestore(&device->resource->req_lock, flags);
b8907339
PR
348
349 if (rv < SS_SUCCESS) {
350 if (f & CS_VERBOSE)
b30ab791 351 print_st_err(device, os, ns, rv);
b8907339
PR
352 goto abort;
353 }
354
69a22773 355 if (drbd_send_state_req(first_peer_device(device), mask, val)) {
b8907339
PR
356 rv = SS_CW_FAILED_BY_PEER;
357 if (f & CS_VERBOSE)
b30ab791 358 print_st_err(device, os, ns, rv);
b8907339
PR
359 goto abort;
360 }
361
b30ab791
AG
362 wait_event(device->state_wait,
363 (rv = _req_st_cond(device, mask, val)));
b8907339
PR
364
365 if (rv < SS_SUCCESS) {
b8907339 366 if (f & CS_VERBOSE)
b30ab791 367 print_st_err(device, os, ns, rv);
b8907339
PR
368 goto abort;
369 }
0500813f 370 spin_lock_irqsave(&device->resource->req_lock, flags);
b30ab791
AG
371 ns = apply_mask_val(drbd_read_state(device), mask, val);
372 rv = _drbd_set_state(device, ns, f, &done);
b8907339 373 } else {
b30ab791 374 rv = _drbd_set_state(device, ns, f, &done);
b8907339
PR
375 }
376
0500813f 377 spin_unlock_irqrestore(&device->resource->req_lock, flags);
b8907339
PR
378
379 if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
0b0ba1ef 380 D_ASSERT(device, current != first_peer_device(device)->connection->worker.task);
b8907339
PR
381 wait_for_completion(&done);
382 }
383
384abort:
385 if (f & CS_SERIALIZE)
b30ab791 386 mutex_unlock(device->state_mutex);
b8907339
PR
387
388 return rv;
389}
390
391/**
392 * _drbd_request_state() - Request a state change (with flags)
b30ab791 393 * @device: DRBD device.
b8907339
PR
394 * @mask: mask of state bits to change.
395 * @val: value of new state bits.
396 * @f: flags
397 *
398 * Cousin of drbd_request_state(), useful with the CS_WAIT_COMPLETE
399 * flag, or when logging of failed state change requests is not desired.
400 */
401enum drbd_state_rv
b30ab791 402_drbd_request_state(struct drbd_device *device, union drbd_state mask,
b8907339
PR
403 union drbd_state val, enum chg_state_flags f)
404{
405 enum drbd_state_rv rv;
406
b30ab791
AG
407 wait_event(device->state_wait,
408 (rv = drbd_req_state(device, mask, val, f)) != SS_IN_TRANSIENT_STATE);
b8907339
PR
409
410 return rv;
411}
412
b30ab791 413static void print_st(struct drbd_device *device, char *name, union drbd_state ns)
b8907339 414{
d0180171 415 drbd_err(device, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c%c%c }\n",
b8907339
PR
416 name,
417 drbd_conn_str(ns.conn),
418 drbd_role_str(ns.role),
419 drbd_role_str(ns.peer),
420 drbd_disk_str(ns.disk),
421 drbd_disk_str(ns.pdsk),
422 is_susp(ns) ? 's' : 'r',
423 ns.aftr_isp ? 'a' : '-',
424 ns.peer_isp ? 'p' : '-',
425 ns.user_isp ? 'u' : '-',
426 ns.susp_fen ? 'F' : '-',
427 ns.susp_nod ? 'N' : '-'
428 );
429}
430
b30ab791 431void print_st_err(struct drbd_device *device, union drbd_state os,
b8907339
PR
432 union drbd_state ns, enum drbd_state_rv err)
433{
434 if (err == SS_IN_TRANSIENT_STATE)
435 return;
d0180171 436 drbd_err(device, "State change failed: %s\n", drbd_set_st_err_str(err));
b30ab791
AG
437 print_st(device, " state", os);
438 print_st(device, "wanted", ns);
b8907339
PR
439}
440
435693e8 441static long print_state_change(char *pb, union drbd_state os, union drbd_state ns,
bbeb641c
PR
442 enum chg_state_flags flags)
443{
435693e8 444 char *pbp;
bbeb641c
PR
445 pbp = pb;
446 *pbp = 0;
706cb24c 447
435693e8 448 if (ns.role != os.role && flags & CS_DC_ROLE)
bbeb641c
PR
449 pbp += sprintf(pbp, "role( %s -> %s ) ",
450 drbd_role_str(os.role),
451 drbd_role_str(ns.role));
435693e8 452 if (ns.peer != os.peer && flags & CS_DC_PEER)
bbeb641c
PR
453 pbp += sprintf(pbp, "peer( %s -> %s ) ",
454 drbd_role_str(os.peer),
455 drbd_role_str(ns.peer));
435693e8 456 if (ns.conn != os.conn && flags & CS_DC_CONN)
bbeb641c
PR
457 pbp += sprintf(pbp, "conn( %s -> %s ) ",
458 drbd_conn_str(os.conn),
459 drbd_conn_str(ns.conn));
435693e8 460 if (ns.disk != os.disk && flags & CS_DC_DISK)
bbeb641c
PR
461 pbp += sprintf(pbp, "disk( %s -> %s ) ",
462 drbd_disk_str(os.disk),
463 drbd_disk_str(ns.disk));
435693e8 464 if (ns.pdsk != os.pdsk && flags & CS_DC_PDSK)
bbeb641c
PR
465 pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
466 drbd_disk_str(os.pdsk),
467 drbd_disk_str(ns.pdsk));
706cb24c
PR
468
469 return pbp - pb;
470}
471
b30ab791 472static void drbd_pr_state_change(struct drbd_device *device, union drbd_state os, union drbd_state ns,
706cb24c
PR
473 enum chg_state_flags flags)
474{
475 char pb[300];
476 char *pbp = pb;
477
478 pbp += print_state_change(pbp, os, ns, flags ^ CS_DC_MASK);
479
bbeb641c
PR
480 if (ns.aftr_isp != os.aftr_isp)
481 pbp += sprintf(pbp, "aftr_isp( %d -> %d ) ",
482 os.aftr_isp,
483 ns.aftr_isp);
484 if (ns.peer_isp != os.peer_isp)
485 pbp += sprintf(pbp, "peer_isp( %d -> %d ) ",
486 os.peer_isp,
487 ns.peer_isp);
488 if (ns.user_isp != os.user_isp)
489 pbp += sprintf(pbp, "user_isp( %d -> %d ) ",
490 os.user_isp,
491 ns.user_isp);
435693e8 492
706cb24c 493 if (pbp != pb)
d0180171 494 drbd_info(device, "%s\n", pb);
bbeb641c 495}
b8907339 496
bde89a9e 497static void conn_pr_state_change(struct drbd_connection *connection, union drbd_state os, union drbd_state ns,
435693e8
PR
498 enum chg_state_flags flags)
499{
500 char pb[300];
706cb24c
PR
501 char *pbp = pb;
502
503 pbp += print_state_change(pbp, os, ns, flags);
435693e8 504
706cb24c
PR
505 if (is_susp(ns) != is_susp(os) && flags & CS_DC_SUSP)
506 pbp += sprintf(pbp, "susp( %d -> %d ) ",
507 is_susp(os),
508 is_susp(ns));
509
510 if (pbp != pb)
1ec861eb 511 drbd_info(connection, "%s\n", pb);
435693e8
PR
512}
513
514
b8907339
PR
515/**
516 * is_valid_state() - Returns an SS_ error code if ns is not valid
b30ab791 517 * @device: DRBD device.
b8907339
PR
518 * @ns: State to consider.
519 */
520static enum drbd_state_rv
b30ab791 521is_valid_state(struct drbd_device *device, union drbd_state ns)
b8907339
PR
522{
523 /* See drbd_state_sw_errors in drbd_strings.c */
524
525 enum drbd_fencing_p fp;
526 enum drbd_state_rv rv = SS_SUCCESS;
44ed167d 527 struct net_conf *nc;
b8907339 528
daeda1cc 529 rcu_read_lock();
b8907339 530 fp = FP_DONT_CARE;
b30ab791
AG
531 if (get_ldev(device)) {
532 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
533 put_ldev(device);
b8907339
PR
534 }
535
a6b32bc3 536 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
44ed167d
PR
537 if (nc) {
538 if (!nc->two_primaries && ns.role == R_PRIMARY) {
047e95e2
PR
539 if (ns.peer == R_PRIMARY)
540 rv = SS_TWO_PRIMARIES;
a6b32bc3 541 else if (conn_highest_peer(first_peer_device(device)->connection) == R_PRIMARY)
047e95e2 542 rv = SS_O_VOL_PEER_PRI;
44ed167d 543 }
b8907339
PR
544 }
545
546 if (rv <= 0)
547 /* already found a reason to abort */;
b30ab791 548 else if (ns.role == R_SECONDARY && device->open_cnt)
b8907339
PR
549 rv = SS_DEVICE_IN_USE;
550
551 else if (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.disk < D_UP_TO_DATE)
552 rv = SS_NO_UP_TO_DATE_DISK;
553
554 else if (fp >= FP_RESOURCE &&
555 ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
556 rv = SS_PRIMARY_NOP;
557
558 else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
559 rv = SS_NO_UP_TO_DATE_DISK;
560
561 else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
562 rv = SS_NO_LOCAL_DISK;
563
564 else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
565 rv = SS_NO_REMOTE_DISK;
566
567 else if (ns.conn > C_CONNECTED && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
568 rv = SS_NO_UP_TO_DATE_DISK;
569
570 else if ((ns.conn == C_CONNECTED ||
571 ns.conn == C_WF_BITMAP_S ||
572 ns.conn == C_SYNC_SOURCE ||
573 ns.conn == C_PAUSED_SYNC_S) &&
574 ns.disk == D_OUTDATED)
575 rv = SS_CONNECTED_OUTDATES;
576
577 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
44ed167d 578 (nc->verify_alg[0] == 0))
b8907339
PR
579 rv = SS_NO_VERIFY_ALG;
580
581 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
a6b32bc3 582 first_peer_device(device)->connection->agreed_pro_version < 88)
b8907339
PR
583 rv = SS_NOT_SUPPORTED;
584
5c4f13d9
PR
585 else if (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
586 rv = SS_NO_UP_TO_DATE_DISK;
587
588 else if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
589 ns.pdsk == D_UNKNOWN)
590 rv = SS_NEED_CONNECTION;
591
b8907339
PR
592 else if (ns.conn >= C_CONNECTED && ns.pdsk == D_UNKNOWN)
593 rv = SS_CONNECTED_OUTDATES;
594
44ed167d
PR
595 rcu_read_unlock();
596
b8907339
PR
597 return rv;
598}
599
600/**
a75f34ad 601 * is_valid_soft_transition() - Returns an SS_ error code if the state transition is not possible
3509502d
PR
602 * This function limits state transitions that may be declined by DRBD. I.e.
603 * user requests (aka soft transitions).
b30ab791 604 * @device: DRBD device.
b8907339
PR
605 * @ns: new state.
606 * @os: old state.
607 */
608static enum drbd_state_rv
bde89a9e 609is_valid_soft_transition(union drbd_state os, union drbd_state ns, struct drbd_connection *connection)
b8907339
PR
610{
611 enum drbd_state_rv rv = SS_SUCCESS;
612
613 if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
614 os.conn > C_CONNECTED)
615 rv = SS_RESYNC_RUNNING;
616
617 if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
618 rv = SS_ALREADY_STANDALONE;
619
620 if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
621 rv = SS_IS_DISKLESS;
622
623 if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
624 rv = SS_NO_NET_CONFIG;
625
626 if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
627 rv = SS_LOWER_THAN_OUTDATED;
628
629 if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
630 rv = SS_IN_TRANSIENT_STATE;
631
2325eb66
PR
632 /* if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
633 rv = SS_IN_TRANSIENT_STATE; */
b8907339 634
a1096a6e
PR
635 /* While establishing a connection only allow cstate to change.
636 Delay/refuse role changes, detach attach etc... */
bde89a9e 637 if (test_bit(STATE_SENT, &connection->flags) &&
a1096a6e
PR
638 !(os.conn == C_WF_REPORT_PARAMS ||
639 (ns.conn == C_WF_REPORT_PARAMS && os.conn == C_WF_CONNECTION)))
640 rv = SS_IN_TRANSIENT_STATE;
641
b8907339
PR
642 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
643 rv = SS_NEED_CONNECTION;
644
645 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
646 ns.conn != os.conn && os.conn > C_CONNECTED)
647 rv = SS_RESYNC_RUNNING;
648
649 if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
650 os.conn < C_CONNECTED)
651 rv = SS_NEED_CONNECTION;
652
653 if ((ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)
654 && os.conn < C_WF_REPORT_PARAMS)
655 rv = SS_NEED_CONNECTION; /* No NetworkFailure -> SyncTarget etc... */
656
2bd5ed5d
PR
657 if (ns.conn == C_DISCONNECTING && ns.pdsk == D_OUTDATED &&
658 os.conn < C_CONNECTED && os.pdsk > D_OUTDATED)
659 rv = SS_OUTDATE_WO_CONN;
660
b8907339
PR
661 return rv;
662}
663
3509502d 664static enum drbd_state_rv
fda74117 665is_valid_conn_transition(enum drbd_conns oc, enum drbd_conns nc)
3509502d 666{
d9cc6e23
LE
667 /* no change -> nothing to do, at least for the connection part */
668 if (oc == nc)
669 return SS_NOTHING_TO_DO;
3509502d 670
d9cc6e23
LE
671 /* disconnect of an unconfigured connection does not make sense */
672 if (oc == C_STANDALONE && nc == C_DISCONNECTING)
673 return SS_ALREADY_STANDALONE;
674
675 /* from C_STANDALONE, we start with C_UNCONNECTED */
676 if (oc == C_STANDALONE && nc != C_UNCONNECTED)
677 return SS_NEED_CONNECTION;
3509502d 678
25b0d6c8
PR
679 /* When establishing a connection we need to go through WF_REPORT_PARAMS!
680 Necessary to do the right thing upon invalidate-remote on a disconnected resource */
681 if (oc < C_WF_REPORT_PARAMS && nc >= C_CONNECTED)
682 return SS_NEED_CONNECTION;
683
3509502d 684 /* After a network error only C_UNCONNECTED or C_DISCONNECTING may follow. */
fda74117 685 if (oc >= C_TIMEOUT && oc <= C_TEAR_DOWN && nc != C_UNCONNECTED && nc != C_DISCONNECTING)
d9cc6e23 686 return SS_IN_TRANSIENT_STATE;
3509502d
PR
687
688 /* After C_DISCONNECTING only C_STANDALONE may follow */
fda74117 689 if (oc == C_DISCONNECTING && nc != C_STANDALONE)
d9cc6e23 690 return SS_IN_TRANSIENT_STATE;
3509502d 691
d9cc6e23 692 return SS_SUCCESS;
fda74117
PR
693}
694
695
696/**
697 * is_valid_transition() - Returns an SS_ error code if the state transition is not possible
698 * This limits hard state transitions. Hard state transitions are facts there are
699 * imposed on DRBD by the environment. E.g. disk broke or network broke down.
700 * But those hard state transitions are still not allowed to do everything.
701 * @ns: new state.
702 * @os: old state.
703 */
704static enum drbd_state_rv
705is_valid_transition(union drbd_state os, union drbd_state ns)
706{
707 enum drbd_state_rv rv;
708
709 rv = is_valid_conn_transition(os.conn, ns.conn);
710
3509502d
PR
711 /* we cannot fail (again) if we already detached */
712 if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
713 rv = SS_IS_DISKLESS;
714
715 return rv;
716}
717
b30ab791 718static void print_sanitize_warnings(struct drbd_device *device, enum sanitize_state_warnings warn)
d942ae44
PR
719{
720 static const char *msg_table[] = {
721 [NO_WARNING] = "",
722 [ABORTED_ONLINE_VERIFY] = "Online-verify aborted.",
723 [ABORTED_RESYNC] = "Resync aborted.",
724 [CONNECTION_LOST_NEGOTIATING] = "Connection lost while negotiating, no data!",
725 [IMPLICITLY_UPGRADED_DISK] = "Implicitly upgraded disk",
726 [IMPLICITLY_UPGRADED_PDSK] = "Implicitly upgraded pdsk",
727 };
728
729 if (warn != NO_WARNING)
d0180171 730 drbd_warn(device, "%s\n", msg_table[warn]);
d942ae44
PR
731}
732
b8907339
PR
733/**
734 * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
b30ab791 735 * @device: DRBD device.
b8907339
PR
736 * @os: old state.
737 * @ns: new state.
738 * @warn_sync_abort:
739 *
740 * When we loose connection, we have to set the state of the peers disk (pdsk)
741 * to D_UNKNOWN. This rule and many more along those lines are in this function.
742 */
d7fe69c6
PR
743static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state os,
744 union drbd_state ns, enum sanitize_state_warnings *warn)
b8907339
PR
745{
746 enum drbd_fencing_p fp;
747 enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
748
d942ae44
PR
749 if (warn)
750 *warn = NO_WARNING;
751
b8907339 752 fp = FP_DONT_CARE;
b30ab791 753 if (get_ldev(device)) {
daeda1cc 754 rcu_read_lock();
b30ab791 755 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
daeda1cc 756 rcu_read_unlock();
b30ab791 757 put_ldev(device);
b8907339
PR
758 }
759
3509502d 760 /* Implications from connection to peer and peer_isp */
b8907339
PR
761 if (ns.conn < C_CONNECTED) {
762 ns.peer_isp = 0;
763 ns.peer = R_UNKNOWN;
764 if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
765 ns.pdsk = D_UNKNOWN;
766 }
767
768 /* Clear the aftr_isp when becoming unconfigured */
769 if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
770 ns.aftr_isp = 0;
771
4308a0a3 772 /* An implication of the disk states onto the connection state */
b8907339 773 /* Abort resync if a disk fails/detaches */
4308a0a3 774 if (ns.conn > C_CONNECTED && (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
d942ae44
PR
775 if (warn)
776 *warn = ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T ?
777 ABORTED_ONLINE_VERIFY : ABORTED_RESYNC;
b8907339
PR
778 ns.conn = C_CONNECTED;
779 }
780
781 /* Connection breaks down before we finished "Negotiating" */
782 if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
b30ab791
AG
783 get_ldev_if_state(device, D_NEGOTIATING)) {
784 if (device->ed_uuid == device->ldev->md.uuid[UI_CURRENT]) {
785 ns.disk = device->new_state_tmp.disk;
786 ns.pdsk = device->new_state_tmp.pdsk;
b8907339 787 } else {
d942ae44
PR
788 if (warn)
789 *warn = CONNECTION_LOST_NEGOTIATING;
b8907339
PR
790 ns.disk = D_DISKLESS;
791 ns.pdsk = D_UNKNOWN;
792 }
b30ab791 793 put_ldev(device);
b8907339
PR
794 }
795
796 /* D_CONSISTENT and D_OUTDATED vanish when we get connected */
797 if (ns.conn >= C_CONNECTED && ns.conn < C_AHEAD) {
798 if (ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED)
799 ns.disk = D_UP_TO_DATE;
800 if (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)
801 ns.pdsk = D_UP_TO_DATE;
802 }
803
804 /* Implications of the connection stat on the disk states */
805 disk_min = D_DISKLESS;
806 disk_max = D_UP_TO_DATE;
807 pdsk_min = D_INCONSISTENT;
808 pdsk_max = D_UNKNOWN;
809 switch ((enum drbd_conns)ns.conn) {
810 case C_WF_BITMAP_T:
811 case C_PAUSED_SYNC_T:
812 case C_STARTING_SYNC_T:
813 case C_WF_SYNC_UUID:
814 case C_BEHIND:
815 disk_min = D_INCONSISTENT;
816 disk_max = D_OUTDATED;
817 pdsk_min = D_UP_TO_DATE;
818 pdsk_max = D_UP_TO_DATE;
819 break;
820 case C_VERIFY_S:
821 case C_VERIFY_T:
822 disk_min = D_UP_TO_DATE;
823 disk_max = D_UP_TO_DATE;
824 pdsk_min = D_UP_TO_DATE;
825 pdsk_max = D_UP_TO_DATE;
826 break;
827 case C_CONNECTED:
828 disk_min = D_DISKLESS;
829 disk_max = D_UP_TO_DATE;
830 pdsk_min = D_DISKLESS;
831 pdsk_max = D_UP_TO_DATE;
832 break;
833 case C_WF_BITMAP_S:
834 case C_PAUSED_SYNC_S:
835 case C_STARTING_SYNC_S:
836 case C_AHEAD:
837 disk_min = D_UP_TO_DATE;
838 disk_max = D_UP_TO_DATE;
839 pdsk_min = D_INCONSISTENT;
840 pdsk_max = D_CONSISTENT; /* D_OUTDATED would be nice. But explicit outdate necessary*/
841 break;
842 case C_SYNC_TARGET:
843 disk_min = D_INCONSISTENT;
844 disk_max = D_INCONSISTENT;
845 pdsk_min = D_UP_TO_DATE;
846 pdsk_max = D_UP_TO_DATE;
847 break;
848 case C_SYNC_SOURCE:
849 disk_min = D_UP_TO_DATE;
850 disk_max = D_UP_TO_DATE;
851 pdsk_min = D_INCONSISTENT;
852 pdsk_max = D_INCONSISTENT;
853 break;
854 case C_STANDALONE:
855 case C_DISCONNECTING:
856 case C_UNCONNECTED:
857 case C_TIMEOUT:
858 case C_BROKEN_PIPE:
859 case C_NETWORK_FAILURE:
860 case C_PROTOCOL_ERROR:
861 case C_TEAR_DOWN:
862 case C_WF_CONNECTION:
863 case C_WF_REPORT_PARAMS:
864 case C_MASK:
865 break;
866 }
867 if (ns.disk > disk_max)
868 ns.disk = disk_max;
869
870 if (ns.disk < disk_min) {
d942ae44
PR
871 if (warn)
872 *warn = IMPLICITLY_UPGRADED_DISK;
b8907339
PR
873 ns.disk = disk_min;
874 }
875 if (ns.pdsk > pdsk_max)
876 ns.pdsk = pdsk_max;
877
878 if (ns.pdsk < pdsk_min) {
d942ae44
PR
879 if (warn)
880 *warn = IMPLICITLY_UPGRADED_PDSK;
b8907339
PR
881 ns.pdsk = pdsk_min;
882 }
883
884 if (fp == FP_STONITH &&
d7fe69c6
PR
885 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED) &&
886 !(os.role == R_PRIMARY && os.conn < C_CONNECTED && os.pdsk > D_OUTDATED))
b8907339
PR
887 ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
888
eb6bea67 889 if (device->resource->res_opts.on_no_data == OND_SUSPEND_IO &&
d7fe69c6
PR
890 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE) &&
891 !(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE))
b8907339
PR
892 ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
893
894 if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
895 if (ns.conn == C_SYNC_SOURCE)
896 ns.conn = C_PAUSED_SYNC_S;
897 if (ns.conn == C_SYNC_TARGET)
898 ns.conn = C_PAUSED_SYNC_T;
899 } else {
900 if (ns.conn == C_PAUSED_SYNC_S)
901 ns.conn = C_SYNC_SOURCE;
902 if (ns.conn == C_PAUSED_SYNC_T)
903 ns.conn = C_SYNC_TARGET;
904 }
905
906 return ns;
907}
908
b30ab791 909void drbd_resume_al(struct drbd_device *device)
b8907339 910{
b30ab791 911 if (test_and_clear_bit(AL_SUSPENDED, &device->flags))
d0180171 912 drbd_info(device, "Resumed AL updates\n");
b8907339
PR
913}
914
915/* helper for __drbd_set_state */
b30ab791 916static void set_ov_position(struct drbd_device *device, enum drbd_conns cs)
b8907339 917{
a6b32bc3 918 if (first_peer_device(device)->connection->agreed_pro_version < 90)
b30ab791
AG
919 device->ov_start_sector = 0;
920 device->rs_total = drbd_bm_bits(device);
921 device->ov_position = 0;
b8907339
PR
922 if (cs == C_VERIFY_T) {
923 /* starting online verify from an arbitrary position
924 * does not fit well into the existing protocol.
925 * on C_VERIFY_T, we initialize ov_left and friends
926 * implicitly in receive_DataRequest once the
927 * first P_OV_REQUEST is received */
b30ab791 928 device->ov_start_sector = ~(sector_t)0;
b8907339 929 } else {
b30ab791
AG
930 unsigned long bit = BM_SECT_TO_BIT(device->ov_start_sector);
931 if (bit >= device->rs_total) {
932 device->ov_start_sector =
933 BM_BIT_TO_SECT(device->rs_total - 1);
934 device->rs_total = 1;
b8907339 935 } else
b30ab791
AG
936 device->rs_total -= bit;
937 device->ov_position = device->ov_start_sector;
b8907339 938 }
b30ab791 939 device->ov_left = device->rs_total;
b8907339
PR
940}
941
942/**
943 * __drbd_set_state() - Set a new DRBD state
b30ab791 944 * @device: DRBD device.
b8907339
PR
945 * @ns: new state.
946 * @flags: Flags
947 * @done: Optional completion, that will get completed after the after_state_ch() finished
948 *
949 * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
950 */
951enum drbd_state_rv
b30ab791 952__drbd_set_state(struct drbd_device *device, union drbd_state ns,
b8907339
PR
953 enum chg_state_flags flags, struct completion *done)
954{
955 union drbd_state os;
956 enum drbd_state_rv rv = SS_SUCCESS;
d942ae44 957 enum sanitize_state_warnings ssw;
b8907339 958 struct after_state_chg_work *ascw;
2681f7f6 959 bool did_remote, should_do_remote;
b8907339 960
b30ab791 961 os = drbd_read_state(device);
b8907339 962
d7fe69c6 963 ns = sanitize_state(device, os, ns, &ssw);
b8907339
PR
964 if (ns.i == os.i)
965 return SS_NOTHING_TO_DO;
966
3509502d
PR
967 rv = is_valid_transition(os, ns);
968 if (rv < SS_SUCCESS)
969 return rv;
970
b8907339
PR
971 if (!(flags & CS_HARD)) {
972 /* pre-state-change checks ; only look at ns */
973 /* See drbd_state_sw_errors in drbd_strings.c */
974
b30ab791 975 rv = is_valid_state(device, ns);
b8907339
PR
976 if (rv < SS_SUCCESS) {
977 /* If the old state was illegal as well, then let
978 this happen...*/
979
b30ab791 980 if (is_valid_state(device, os) == rv)
a6b32bc3 981 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
b8907339 982 } else
a6b32bc3 983 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
b8907339
PR
984 }
985
986 if (rv < SS_SUCCESS) {
987 if (flags & CS_VERBOSE)
b30ab791 988 print_st_err(device, os, ns, rv);
b8907339
PR
989 return rv;
990 }
991
b30ab791 992 print_sanitize_warnings(device, ssw);
b8907339 993
b30ab791 994 drbd_pr_state_change(device, os, ns, flags);
b8907339 995
706cb24c
PR
996 /* Display changes to the susp* flags that where caused by the call to
997 sanitize_state(). Only display it here if we where not called from
998 _conn_request_state() */
999 if (!(flags & CS_DC_SUSP))
a6b32bc3
AG
1000 conn_pr_state_change(first_peer_device(device)->connection, os, ns,
1001 (flags & ~CS_DC_MASK) | CS_DC_SUSP);
706cb24c 1002
b8907339
PR
1003 /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
1004 * on the ldev here, to be sure the transition -> D_DISKLESS resp.
1005 * drbd_ldev_destroy() won't happen before our corresponding
1006 * after_state_ch works run, where we put_ldev again. */
1007 if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
1008 (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
b30ab791 1009 atomic_inc(&device->local_cnt);
b8907339 1010
b30ab791
AG
1011 did_remote = drbd_should_do_remote(device->state);
1012 device->state.i = ns.i;
1013 should_do_remote = drbd_should_do_remote(device->state);
6bbf53ca
AG
1014 device->resource->susp = ns.susp;
1015 device->resource->susp_nod = ns.susp_nod;
1016 device->resource->susp_fen = ns.susp_fen;
b8907339 1017
2681f7f6
LE
1018 /* put replicated vs not-replicated requests in seperate epochs */
1019 if (did_remote != should_do_remote)
a6b32bc3 1020 start_new_tl_epoch(first_peer_device(device)->connection);
2681f7f6 1021
b8907339 1022 if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
b30ab791 1023 drbd_print_uuids(device, "attached to UUIDs");
b8907339 1024
79702011
PR
1025 /* Wake up role changes, that were delayed because of connection establishing */
1026 if (os.conn == C_WF_REPORT_PARAMS && ns.conn != C_WF_REPORT_PARAMS &&
a6b32bc3
AG
1027 no_peer_wf_report_params(first_peer_device(device)->connection))
1028 clear_bit(STATE_SENT, &first_peer_device(device)->connection->flags);
79702011 1029
b30ab791
AG
1030 wake_up(&device->misc_wait);
1031 wake_up(&device->state_wait);
a6b32bc3 1032 wake_up(&first_peer_device(device)->connection->ping_wait);
b8907339 1033
58ffa580
LE
1034 /* Aborted verify run, or we reached the stop sector.
1035 * Log the last position, unless end-of-device. */
b8907339 1036 if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
58ffa580 1037 ns.conn <= C_CONNECTED) {
b30ab791
AG
1038 device->ov_start_sector =
1039 BM_BIT_TO_SECT(drbd_bm_bits(device) - device->ov_left);
1040 if (device->ov_left)
d0180171 1041 drbd_info(device, "Online Verify reached sector %llu\n",
b30ab791 1042 (unsigned long long)device->ov_start_sector);
b8907339
PR
1043 }
1044
1045 if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
1046 (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
d0180171 1047 drbd_info(device, "Syncer continues.\n");
b30ab791
AG
1048 device->rs_paused += (long)jiffies
1049 -(long)device->rs_mark_time[device->rs_last_mark];
b8907339 1050 if (ns.conn == C_SYNC_TARGET)
b30ab791 1051 mod_timer(&device->resync_timer, jiffies);
b8907339
PR
1052 }
1053
1054 if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
1055 (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
d0180171 1056 drbd_info(device, "Resync suspended\n");
b30ab791 1057 device->rs_mark_time[device->rs_last_mark] = jiffies;
b8907339
PR
1058 }
1059
1060 if (os.conn == C_CONNECTED &&
1061 (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
1062 unsigned long now = jiffies;
1063 int i;
1064
b30ab791
AG
1065 set_ov_position(device, ns.conn);
1066 device->rs_start = now;
1067 device->rs_last_events = 0;
1068 device->rs_last_sect_ev = 0;
1069 device->ov_last_oos_size = 0;
1070 device->ov_last_oos_start = 0;
b8907339
PR
1071
1072 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
b30ab791
AG
1073 device->rs_mark_left[i] = device->ov_left;
1074 device->rs_mark_time[i] = now;
b8907339
PR
1075 }
1076
b30ab791 1077 drbd_rs_controller_reset(device);
b8907339
PR
1078
1079 if (ns.conn == C_VERIFY_S) {
d0180171 1080 drbd_info(device, "Starting Online Verify from sector %llu\n",
b30ab791
AG
1081 (unsigned long long)device->ov_position);
1082 mod_timer(&device->resync_timer, jiffies);
b8907339
PR
1083 }
1084 }
1085
b30ab791
AG
1086 if (get_ldev(device)) {
1087 u32 mdf = device->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
b8907339
PR
1088 MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
1089 MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
1090
d5d7ebd4 1091 mdf &= ~MDF_AL_CLEAN;
b30ab791 1092 if (test_bit(CRASHED_PRIMARY, &device->flags))
b8907339 1093 mdf |= MDF_CRASHED_PRIMARY;
b30ab791
AG
1094 if (device->state.role == R_PRIMARY ||
1095 (device->state.pdsk < D_INCONSISTENT && device->state.peer == R_PRIMARY))
b8907339 1096 mdf |= MDF_PRIMARY_IND;
b30ab791 1097 if (device->state.conn > C_WF_REPORT_PARAMS)
b8907339 1098 mdf |= MDF_CONNECTED_IND;
b30ab791 1099 if (device->state.disk > D_INCONSISTENT)
b8907339 1100 mdf |= MDF_CONSISTENT;
b30ab791 1101 if (device->state.disk > D_OUTDATED)
b8907339 1102 mdf |= MDF_WAS_UP_TO_DATE;
b30ab791 1103 if (device->state.pdsk <= D_OUTDATED && device->state.pdsk >= D_INCONSISTENT)
b8907339 1104 mdf |= MDF_PEER_OUT_DATED;
b30ab791
AG
1105 if (mdf != device->ldev->md.flags) {
1106 device->ldev->md.flags = mdf;
1107 drbd_md_mark_dirty(device);
b8907339
PR
1108 }
1109 if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
b30ab791
AG
1110 drbd_set_ed_uuid(device, device->ldev->md.uuid[UI_CURRENT]);
1111 put_ldev(device);
b8907339
PR
1112 }
1113
1114 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
1115 if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
1116 os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
b30ab791 1117 set_bit(CONSIDER_RESYNC, &device->flags);
b8907339
PR
1118
1119 /* Receiver should clean up itself */
1120 if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
a6b32bc3 1121 drbd_thread_stop_nowait(&first_peer_device(device)->connection->receiver);
b8907339
PR
1122
1123 /* Now the receiver finished cleaning up itself, it should die */
1124 if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
a6b32bc3 1125 drbd_thread_stop_nowait(&first_peer_device(device)->connection->receiver);
b8907339
PR
1126
1127 /* Upon network failure, we need to restart the receiver. */
823bd832 1128 if (os.conn > C_WF_CONNECTION &&
b8907339 1129 ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
a6b32bc3 1130 drbd_thread_restart_nowait(&first_peer_device(device)->connection->receiver);
b8907339
PR
1131
1132 /* Resume AL writing if we get a connection */
28e448bb 1133 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
b30ab791 1134 drbd_resume_al(device);
a6b32bc3 1135 first_peer_device(device)->connection->connect_cnt++;
28e448bb 1136 }
b8907339 1137
07be15b1
LE
1138 /* remember last attach time so request_timer_fn() won't
1139 * kill newly established sessions while we are still trying to thaw
1140 * previously frozen IO */
1141 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
1142 ns.disk > D_NEGOTIATING)
b30ab791 1143 device->last_reattach_jif = jiffies;
07be15b1 1144
b8907339
PR
1145 ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
1146 if (ascw) {
1147 ascw->os = os;
1148 ascw->ns = ns;
1149 ascw->flags = flags;
1150 ascw->w.cb = w_after_state_ch;
84b8c06b 1151 ascw->device = device;
b8907339 1152 ascw->done = done;
84b8c06b
AG
1153 drbd_queue_work(&first_peer_device(device)->connection->sender_work,
1154 &ascw->w);
b8907339 1155 } else {
d0180171 1156 drbd_err(device, "Could not kmalloc an ascw\n");
b8907339
PR
1157 }
1158
1159 return rv;
1160}
1161
99920dc5 1162static int w_after_state_ch(struct drbd_work *w, int unused)
b8907339
PR
1163{
1164 struct after_state_chg_work *ascw =
1165 container_of(w, struct after_state_chg_work, w);
84b8c06b 1166 struct drbd_device *device = ascw->device;
b8907339 1167
b30ab791 1168 after_state_ch(device, ascw->os, ascw->ns, ascw->flags);
137975c1 1169 if (ascw->flags & CS_WAIT_COMPLETE)
b8907339 1170 complete(ascw->done);
b8907339
PR
1171 kfree(ascw);
1172
99920dc5 1173 return 0;
b8907339
PR
1174}
1175
b30ab791 1176static void abw_start_sync(struct drbd_device *device, int rv)
b8907339
PR
1177{
1178 if (rv) {
d0180171 1179 drbd_err(device, "Writing the bitmap failed not starting resync.\n");
b30ab791 1180 _drbd_request_state(device, NS(conn, C_CONNECTED), CS_VERBOSE);
b8907339
PR
1181 return;
1182 }
1183
b30ab791 1184 switch (device->state.conn) {
b8907339 1185 case C_STARTING_SYNC_T:
b30ab791 1186 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
b8907339
PR
1187 break;
1188 case C_STARTING_SYNC_S:
b30ab791 1189 drbd_start_resync(device, C_SYNC_SOURCE);
b8907339
PR
1190 break;
1191 }
1192}
1193
b30ab791 1194int drbd_bitmap_io_from_worker(struct drbd_device *device,
54761697 1195 int (*io_fn)(struct drbd_device *),
b8907339
PR
1196 char *why, enum bm_flag flags)
1197{
1198 int rv;
1199
0b0ba1ef 1200 D_ASSERT(device, current == first_peer_device(device)->connection->worker.task);
b8907339 1201
b30ab791
AG
1202 /* open coded non-blocking drbd_suspend_io(device); */
1203 set_bit(SUSPEND_IO, &device->flags);
b8907339 1204
b30ab791
AG
1205 drbd_bm_lock(device, why, flags);
1206 rv = io_fn(device);
1207 drbd_bm_unlock(device);
b8907339 1208
b30ab791 1209 drbd_resume_io(device);
b8907339
PR
1210
1211 return rv;
1212}
1213
1214/**
1215 * after_state_ch() - Perform after state change actions that may sleep
b30ab791 1216 * @device: DRBD device.
b8907339
PR
1217 * @os: old state.
1218 * @ns: new state.
1219 * @flags: Flags
1220 */
b30ab791 1221static void after_state_ch(struct drbd_device *device, union drbd_state os,
b8907339
PR
1222 union drbd_state ns, enum chg_state_flags flags)
1223{
6bbf53ca 1224 struct drbd_resource *resource = device->resource;
3b98c0c2
LE
1225 struct sib_info sib;
1226
1227 sib.sib_reason = SIB_STATE_CHANGE;
1228 sib.os = os;
1229 sib.ns = ns;
b8907339
PR
1230
1231 if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
b30ab791
AG
1232 clear_bit(CRASHED_PRIMARY, &device->flags);
1233 if (device->p_uuid)
1234 device->p_uuid[UI_FLAGS] &= ~((u64)2);
b8907339
PR
1235 }
1236
b8907339 1237 /* Inform userspace about the change... */
b30ab791 1238 drbd_bcast_event(device, &sib);
b8907339
PR
1239
1240 if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
1241 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
b30ab791 1242 drbd_khelper(device, "pri-on-incon-degr");
b8907339
PR
1243
1244 /* Here we have the actions that are performed after a
1245 state change. This function might sleep */
1246
b8907339 1247 if (ns.susp_nod) {
a6b32bc3 1248 struct drbd_connection *connection = first_peer_device(device)->connection;
a6d00c8e
PR
1249 enum drbd_req_event what = NOTHING;
1250
0500813f 1251 spin_lock_irq(&device->resource->req_lock);
bde89a9e 1252 if (os.conn < C_CONNECTED && conn_lowest_conn(connection) >= C_CONNECTED)
b8907339
PR
1253 what = RESEND;
1254
3fb4746d 1255 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
bde89a9e 1256 conn_lowest_disk(connection) > D_NEGOTIATING)
b8907339
PR
1257 what = RESTART_FROZEN_DISK_IO;
1258
6bbf53ca 1259 if (resource->susp_nod && what != NOTHING) {
bde89a9e
AG
1260 _tl_restart(connection, what);
1261 _conn_request_state(connection,
892fdd1a
PR
1262 (union drbd_state) { { .susp_nod = 1 } },
1263 (union drbd_state) { { .susp_nod = 0 } },
1264 CS_VERBOSE);
b8907339 1265 }
0500813f 1266 spin_unlock_irq(&device->resource->req_lock);
b8907339
PR
1267 }
1268
88f79ec4 1269 if (ns.susp_fen) {
a6b32bc3 1270 struct drbd_connection *connection = first_peer_device(device)->connection;
88f79ec4 1271
0500813f 1272 spin_lock_irq(&device->resource->req_lock);
6bbf53ca 1273 if (resource->susp_fen && conn_lowest_conn(connection) >= C_CONNECTED) {
88f79ec4 1274 /* case2: The connection was established again: */
c06ece6b 1275 struct drbd_peer_device *peer_device;
88f79ec4
PR
1276 int vnr;
1277
1278 rcu_read_lock();
c06ece6b
AG
1279 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
1280 clear_bit(NEW_CUR_UUID, &peer_device->device->flags);
88f79ec4 1281 rcu_read_unlock();
bde89a9e
AG
1282 _tl_restart(connection, RESEND);
1283 _conn_request_state(connection,
88f79ec4
PR
1284 (union drbd_state) { { .susp_fen = 1 } },
1285 (union drbd_state) { { .susp_fen = 0 } },
1286 CS_VERBOSE);
1287 }
0500813f 1288 spin_unlock_irq(&device->resource->req_lock);
88f79ec4
PR
1289 }
1290
b8907339
PR
1291 /* Became sync source. With protocol >= 96, we still need to send out
1292 * the sync uuid now. Need to do that before any drbd_send_state, or
1293 * the other side may go "paused sync" before receiving the sync uuids,
1294 * which is unexpected. */
1295 if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
1296 (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
a6b32bc3 1297 first_peer_device(device)->connection->agreed_pro_version >= 96 && get_ldev(device)) {
69a22773 1298 drbd_gen_and_send_sync_uuid(first_peer_device(device));
b30ab791 1299 put_ldev(device);
b8907339
PR
1300 }
1301
1302 /* Do not change the order of the if above and the two below... */
369bea63
PR
1303 if (os.pdsk == D_DISKLESS &&
1304 ns.pdsk > D_DISKLESS && ns.pdsk != D_UNKNOWN) { /* attach on the peer */
a324896b
LE
1305 /* we probably will start a resync soon.
1306 * make sure those things are properly reset. */
b30ab791
AG
1307 device->rs_total = 0;
1308 device->rs_failed = 0;
1309 atomic_set(&device->rs_pending_cnt, 0);
1310 drbd_rs_cancel_all(device);
a324896b 1311
69a22773
AG
1312 drbd_send_uuids(first_peer_device(device));
1313 drbd_send_state(first_peer_device(device), ns);
b8907339
PR
1314 }
1315 /* No point in queuing send_bitmap if we don't have a connection
1316 * anymore, so check also the _current_ state, not only the new state
1317 * at the time this work was queued. */
1318 if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
b30ab791
AG
1319 device->state.conn == C_WF_BITMAP_S)
1320 drbd_queue_bitmap_io(device, &drbd_send_bitmap, NULL,
b8907339
PR
1321 "send_bitmap (WFBitMapS)",
1322 BM_LOCKED_TEST_ALLOWED);
1323
1324 /* Lost contact to peer's copy of the data */
1325 if ((os.pdsk >= D_INCONSISTENT &&
1326 os.pdsk != D_UNKNOWN &&
1327 os.pdsk != D_OUTDATED)
1328 && (ns.pdsk < D_INCONSISTENT ||
1329 ns.pdsk == D_UNKNOWN ||
1330 ns.pdsk == D_OUTDATED)) {
b30ab791 1331 if (get_ldev(device)) {
b8907339 1332 if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
b30ab791
AG
1333 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1334 if (drbd_suspended(device)) {
1335 set_bit(NEW_CUR_UUID, &device->flags);
b8907339 1336 } else {
b30ab791 1337 drbd_uuid_new_current(device);
69a22773 1338 drbd_send_uuids(first_peer_device(device));
b8907339
PR
1339 }
1340 }
b30ab791 1341 put_ldev(device);
b8907339
PR
1342 }
1343 }
1344
b30ab791 1345 if (ns.pdsk < D_INCONSISTENT && get_ldev(device)) {
0cfac5dd 1346 if (os.peer == R_SECONDARY && ns.peer == R_PRIMARY &&
b30ab791
AG
1347 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1348 drbd_uuid_new_current(device);
69a22773 1349 drbd_send_uuids(first_peer_device(device));
0cfac5dd 1350 }
b8907339
PR
1351 /* D_DISKLESS Peer becomes secondary */
1352 if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
1353 /* We may still be Primary ourselves.
1354 * No harm done if the bitmap still changes,
1355 * redirtied pages will follow later. */
b30ab791 1356 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
b8907339 1357 "demote diskless peer", BM_LOCKED_SET_ALLOWED);
b30ab791 1358 put_ldev(device);
b8907339
PR
1359 }
1360
1361 /* Write out all changed bits on demote.
1362 * Though, no need to da that just yet
1363 * if there is a resync going on still */
1364 if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
b30ab791 1365 device->state.conn <= C_CONNECTED && get_ldev(device)) {
b8907339
PR
1366 /* No changes to the bitmap expected this time, so assert that,
1367 * even though no harm was done if it did change. */
b30ab791 1368 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
b8907339 1369 "demote", BM_LOCKED_TEST_ALLOWED);
b30ab791 1370 put_ldev(device);
b8907339
PR
1371 }
1372
1373 /* Last part of the attaching process ... */
1374 if (ns.conn >= C_CONNECTED &&
1375 os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
69a22773
AG
1376 drbd_send_sizes(first_peer_device(device), 0, 0); /* to start sync... */
1377 drbd_send_uuids(first_peer_device(device));
1378 drbd_send_state(first_peer_device(device), ns);
b8907339
PR
1379 }
1380
1381 /* We want to pause/continue resync, tell peer. */
1382 if (ns.conn >= C_CONNECTED &&
1383 ((os.aftr_isp != ns.aftr_isp) ||
1384 (os.user_isp != ns.user_isp)))
69a22773 1385 drbd_send_state(first_peer_device(device), ns);
b8907339
PR
1386
1387 /* In case one of the isp bits got set, suspend other devices. */
1388 if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
1389 (ns.aftr_isp || ns.peer_isp || ns.user_isp))
b30ab791 1390 suspend_other_sg(device);
b8907339
PR
1391
1392 /* Make sure the peer gets informed about eventual state
1393 changes (ISP bits) while we were in WFReportParams. */
1394 if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
69a22773 1395 drbd_send_state(first_peer_device(device), ns);
b8907339
PR
1396
1397 if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
69a22773 1398 drbd_send_state(first_peer_device(device), ns);
b8907339
PR
1399
1400 /* We are in the progress to start a full sync... */
1401 if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
1402 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
1403 /* no other bitmap changes expected during this phase */
b30ab791 1404 drbd_queue_bitmap_io(device,
b8907339
PR
1405 &drbd_bmio_set_n_write, &abw_start_sync,
1406 "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
1407
b8907339
PR
1408 /* first half of local IO error, failure to attach,
1409 * or administrative detach */
1410 if (os.disk != D_FAILED && ns.disk == D_FAILED) {
32db80f6
PR
1411 enum drbd_io_error_p eh = EP_PASS_ON;
1412 int was_io_error = 0;
b8907339 1413 /* corresponding get_ldev was in __drbd_set_state, to serialize
32db80f6
PR
1414 * our cleanup here with the transition to D_DISKLESS.
1415 * But is is still not save to dreference ldev here, since
1416 * we might come from an failed Attach before ldev was set. */
b30ab791 1417 if (device->ldev) {
32db80f6 1418 rcu_read_lock();
b30ab791 1419 eh = rcu_dereference(device->ldev->disk_conf)->on_io_error;
32db80f6
PR
1420 rcu_read_unlock();
1421
b30ab791 1422 was_io_error = test_and_clear_bit(WAS_IO_ERROR, &device->flags);
32db80f6 1423
6f1a6563 1424 if (was_io_error && eh == EP_CALL_HELPER)
b30ab791 1425 drbd_khelper(device, "local-io-error");
6f1a6563 1426
0c849666
LE
1427 /* Immediately allow completion of all application IO,
1428 * that waits for completion from the local disk,
1429 * if this was a force-detach due to disk_timeout
1430 * or administrator request (drbdsetup detach --force).
1431 * Do NOT abort otherwise.
1432 * Aborting local requests may cause serious problems,
1433 * if requests are completed to upper layers already,
1434 * and then later the already submitted local bio completes.
1435 * This can cause DMA into former bio pages that meanwhile
1436 * have been re-used for other things.
1437 * So aborting local requests may cause crashes,
1438 * or even worse, silent data corruption.
1439 */
b30ab791
AG
1440 if (test_and_clear_bit(FORCE_DETACH, &device->flags))
1441 tl_abort_disk_io(device);
32db80f6
PR
1442
1443 /* current state still has to be D_FAILED,
1444 * there is only one way out: to D_DISKLESS,
1445 * and that may only happen after our put_ldev below. */
b30ab791 1446 if (device->state.disk != D_FAILED)
d0180171 1447 drbd_err(device,
32db80f6 1448 "ASSERT FAILED: disk is %s during detach\n",
b30ab791 1449 drbd_disk_str(device->state.disk));
32db80f6
PR
1450
1451 if (ns.conn >= C_CONNECTED)
69a22773 1452 drbd_send_state(first_peer_device(device), ns);
32db80f6 1453
b30ab791 1454 drbd_rs_cancel_all(device);
32db80f6
PR
1455
1456 /* In case we want to get something to stable storage still,
1457 * this may be the last chance.
1458 * Following put_ldev may transition to D_DISKLESS. */
b30ab791 1459 drbd_md_sync(device);
32db80f6 1460 }
b30ab791 1461 put_ldev(device);
b8907339
PR
1462 }
1463
b30ab791
AG
1464 /* second half of local IO error, failure to attach,
1465 * or administrative detach,
1466 * after local_cnt references have reached zero again */
1467 if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
1468 /* We must still be diskless,
1469 * re-attach has to be serialized with this! */
1470 if (device->state.disk != D_DISKLESS)
d0180171
AG
1471 drbd_err(device,
1472 "ASSERT FAILED: disk is %s while going diskless\n",
1473 drbd_disk_str(device->state.disk));
b8907339 1474
6ab9b1b6 1475 if (ns.conn >= C_CONNECTED)
69a22773 1476 drbd_send_state(first_peer_device(device), ns);
b8907339
PR
1477 /* corresponding get_ldev in __drbd_set_state
1478 * this may finally trigger drbd_ldev_destroy. */
b30ab791 1479 put_ldev(device);
b8907339
PR
1480 }
1481
1482 /* Notify peer that I had a local IO error, and did not detached.. */
6ab9b1b6 1483 if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT && ns.conn >= C_CONNECTED)
69a22773 1484 drbd_send_state(first_peer_device(device), ns);
b8907339
PR
1485
1486 /* Disks got bigger while they were detached */
1487 if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
b30ab791 1488 test_and_clear_bit(RESYNC_AFTER_NEG, &device->flags)) {
b8907339 1489 if (ns.conn == C_CONNECTED)
b30ab791 1490 resync_after_online_grow(device);
b8907339
PR
1491 }
1492
1493 /* A resync finished or aborted, wake paused devices... */
1494 if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
1495 (os.peer_isp && !ns.peer_isp) ||
1496 (os.user_isp && !ns.user_isp))
b30ab791 1497 resume_next_sg(device);
b8907339
PR
1498
1499 /* sync target done with resync. Explicitly notify peer, even though
1500 * it should (at least for non-empty resyncs) already know itself. */
1501 if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
69a22773 1502 drbd_send_state(first_peer_device(device), ns);
b8907339 1503
58ffa580
LE
1504 /* Verify finished, or reached stop sector. Peer did not know about
1505 * the stop sector, and we may even have changed the stop sector during
1506 * verify to interrupt/stop early. Send the new state. */
1507 if (os.conn == C_VERIFY_S && ns.conn == C_CONNECTED
b30ab791 1508 && verify_can_do_stop_sector(device))
69a22773 1509 drbd_send_state(first_peer_device(device), ns);
58ffa580 1510
b8907339
PR
1511 /* This triggers bitmap writeout of potentially still unwritten pages
1512 * if the resync finished cleanly, or aborted because of peer disk
1513 * failure, or because of connection loss.
1514 * For resync aborted because of local disk failure, we cannot do
1515 * any bitmap writeout anymore.
1516 * No harm done if some bits change during this phase.
1517 */
b30ab791
AG
1518 if (os.conn > C_CONNECTED && ns.conn <= C_CONNECTED && get_ldev(device)) {
1519 drbd_queue_bitmap_io(device, &drbd_bm_write_copy_pages, NULL,
a220d291 1520 "write from resync_finished", BM_LOCKED_CHANGE_ALLOWED);
b30ab791 1521 put_ldev(device);
b8907339
PR
1522 }
1523
1524 if (ns.disk == D_DISKLESS &&
1525 ns.conn == C_STANDALONE &&
1526 ns.role == R_SECONDARY) {
1527 if (os.aftr_isp != ns.aftr_isp)
b30ab791 1528 resume_next_sg(device);
b8907339
PR
1529 }
1530
b30ab791 1531 drbd_md_sync(device);
b8907339
PR
1532}
1533
bbeb641c 1534struct after_conn_state_chg_work {
4c007603 1535 struct drbd_work w;
bbeb641c 1536 enum drbd_conns oc;
8c7e16c3 1537 union drbd_state ns_min;
b30ab791 1538 union drbd_state ns_max; /* new, max state, over all devices */
bbeb641c 1539 enum chg_state_flags flags;
84b8c06b 1540 struct drbd_connection *connection;
bbeb641c
PR
1541};
1542
99920dc5 1543static int w_after_conn_state_ch(struct drbd_work *w, int unused)
bbeb641c
PR
1544{
1545 struct after_conn_state_chg_work *acscw =
4c007603 1546 container_of(w, struct after_conn_state_chg_work, w);
84b8c06b 1547 struct drbd_connection *connection = acscw->connection;
bbeb641c 1548 enum drbd_conns oc = acscw->oc;
5f082f98 1549 union drbd_state ns_max = acscw->ns_max;
c06ece6b 1550 struct drbd_peer_device *peer_device;
a6d00c8e 1551 int vnr;
bbeb641c
PR
1552
1553 kfree(acscw);
1554
b8907339 1555 /* Upon network configuration, we need to start the receiver */
5f082f98 1556 if (oc == C_STANDALONE && ns_max.conn == C_UNCONNECTED)
bde89a9e 1557 drbd_thread_start(&connection->receiver);
b8907339 1558
f3dfa40a
LE
1559 if (oc == C_DISCONNECTING && ns_max.conn == C_STANDALONE) {
1560 struct net_conf *old_conf;
1561
0500813f 1562 mutex_lock(&connection->resource->conf_update);
bde89a9e
AG
1563 old_conf = connection->net_conf;
1564 connection->my_addr_len = 0;
1565 connection->peer_addr_len = 0;
1566 rcu_assign_pointer(connection->net_conf, NULL);
1567 conn_free_crypto(connection);
0500813f 1568 mutex_unlock(&connection->resource->conf_update);
f3dfa40a
LE
1569
1570 synchronize_rcu();
1571 kfree(old_conf);
1572 }
1573
a6d00c8e
PR
1574 if (ns_max.susp_fen) {
1575 /* case1: The outdate peer handler is successful: */
1576 if (ns_max.pdsk <= D_OUTDATED) {
695d08fa 1577 rcu_read_lock();
c06ece6b
AG
1578 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1579 struct drbd_device *device = peer_device->device;
b30ab791
AG
1580 if (test_bit(NEW_CUR_UUID, &device->flags)) {
1581 drbd_uuid_new_current(device);
1582 clear_bit(NEW_CUR_UUID, &device->flags);
a6d00c8e
PR
1583 }
1584 }
695d08fa 1585 rcu_read_unlock();
0500813f 1586 spin_lock_irq(&connection->resource->req_lock);
bde89a9e
AG
1587 _tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
1588 _conn_request_state(connection,
8a0bab2a
PR
1589 (union drbd_state) { { .susp_fen = 1 } },
1590 (union drbd_state) { { .susp_fen = 0 } },
1591 CS_VERBOSE);
0500813f 1592 spin_unlock_irq(&connection->resource->req_lock);
a6d00c8e 1593 }
a6d00c8e 1594 }
05a10ec7 1595 kref_put(&connection->kref, drbd_destroy_connection);
19fffd7b 1596
bde89a9e 1597 conn_md_sync(connection);
19fffd7b 1598
99920dc5 1599 return 0;
bbeb641c
PR
1600}
1601
bde89a9e 1602void conn_old_common_state(struct drbd_connection *connection, union drbd_state *pcs, enum chg_state_flags *pf)
88ef594e 1603{
435693e8 1604 enum chg_state_flags flags = ~0;
c06ece6b 1605 struct drbd_peer_device *peer_device;
435693e8 1606 int vnr, first_vol = 1;
e0e16653
PR
1607 union drbd_dev_state os, cs = {
1608 { .role = R_SECONDARY,
1609 .peer = R_UNKNOWN,
bde89a9e 1610 .conn = connection->cstate,
e0e16653
PR
1611 .disk = D_DISKLESS,
1612 .pdsk = D_UNKNOWN,
1613 } };
88ef594e 1614
695d08fa 1615 rcu_read_lock();
c06ece6b
AG
1616 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1617 struct drbd_device *device = peer_device->device;
b30ab791 1618 os = device->state;
88ef594e 1619
435693e8
PR
1620 if (first_vol) {
1621 cs = os;
1622 first_vol = 0;
1623 continue;
1624 }
1625
1626 if (cs.role != os.role)
1627 flags &= ~CS_DC_ROLE;
1628
1629 if (cs.peer != os.peer)
1630 flags &= ~CS_DC_PEER;
1631
1632 if (cs.conn != os.conn)
1633 flags &= ~CS_DC_CONN;
88ef594e 1634
435693e8
PR
1635 if (cs.disk != os.disk)
1636 flags &= ~CS_DC_DISK;
88ef594e 1637
435693e8
PR
1638 if (cs.pdsk != os.pdsk)
1639 flags &= ~CS_DC_PDSK;
1640 }
695d08fa 1641 rcu_read_unlock();
435693e8
PR
1642
1643 *pf |= CS_DC_MASK;
1644 *pf &= flags;
da9fbc27 1645 (*pcs).i = cs.i;
88ef594e 1646}
bbeb641c 1647
bd0c824a 1648static enum drbd_state_rv
bde89a9e 1649conn_is_valid_transition(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
88ef594e 1650 enum chg_state_flags flags)
bbeb641c 1651{
bd0c824a 1652 enum drbd_state_rv rv = SS_SUCCESS;
bbeb641c 1653 union drbd_state ns, os;
c06ece6b 1654 struct drbd_peer_device *peer_device;
bd0c824a 1655 int vnr;
bbeb641c 1656
695d08fa 1657 rcu_read_lock();
c06ece6b
AG
1658 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1659 struct drbd_device *device = peer_device->device;
b30ab791 1660 os = drbd_read_state(device);
d7fe69c6 1661 ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
bd0c824a 1662
778bcf2e
PR
1663 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1664 ns.disk = os.disk;
1665
bd0c824a
PR
1666 if (ns.i == os.i)
1667 continue;
bbeb641c 1668
bd0c824a 1669 rv = is_valid_transition(os, ns);
bd0c824a 1670
c06ece6b 1671 if (rv >= SS_SUCCESS && !(flags & CS_HARD)) {
b30ab791 1672 rv = is_valid_state(device, ns);
bd0c824a 1673 if (rv < SS_SUCCESS) {
b30ab791 1674 if (is_valid_state(device, os) == rv)
bde89a9e 1675 rv = is_valid_soft_transition(os, ns, connection);
bd0c824a 1676 } else
bde89a9e 1677 rv = is_valid_soft_transition(os, ns, connection);
bd0c824a 1678 }
c06ece6b
AG
1679
1680 if (rv < SS_SUCCESS) {
1681 if (flags & CS_VERBOSE)
1682 print_st_err(device, os, ns, rv);
bd0c824a 1683 break;
c06ece6b 1684 }
bbeb641c 1685 }
695d08fa 1686 rcu_read_unlock();
bbeb641c 1687
bd0c824a 1688 return rv;
bbeb641c
PR
1689}
1690
8c7e16c3 1691void
bde89a9e 1692conn_set_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
8c7e16c3 1693 union drbd_state *pns_min, union drbd_state *pns_max, enum chg_state_flags flags)
bbeb641c 1694{
f132f554 1695 union drbd_state ns, os, ns_max = { };
8c7e16c3
PR
1696 union drbd_state ns_min = {
1697 { .role = R_MASK,
1698 .peer = R_MASK,
e0e16653 1699 .conn = val.conn,
8c7e16c3
PR
1700 .disk = D_MASK,
1701 .pdsk = D_MASK
1702 } };
c06ece6b 1703 struct drbd_peer_device *peer_device;
bbeb641c 1704 enum drbd_state_rv rv;
f132f554 1705 int vnr, number_of_volumes = 0;
bbeb641c 1706
07be15b1
LE
1707 if (mask.conn == C_MASK) {
1708 /* remember last connect time so request_timer_fn() won't
1709 * kill newly established sessions while we are still trying to thaw
1710 * previously frozen IO */
bde89a9e
AG
1711 if (connection->cstate != C_WF_REPORT_PARAMS && val.conn == C_WF_REPORT_PARAMS)
1712 connection->last_reconnect_jif = jiffies;
07be15b1 1713
bde89a9e 1714 connection->cstate = val.conn;
07be15b1 1715 }
bd0c824a 1716
695d08fa 1717 rcu_read_lock();
c06ece6b
AG
1718 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1719 struct drbd_device *device = peer_device->device;
f132f554 1720 number_of_volumes++;
b30ab791 1721 os = drbd_read_state(device);
bd0c824a 1722 ns = apply_mask_val(os, mask, val);
d7fe69c6 1723 ns = sanitize_state(device, os, ns, NULL);
bbeb641c 1724
778bcf2e
PR
1725 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1726 ns.disk = os.disk;
1727
b30ab791 1728 rv = __drbd_set_state(device, ns, flags, NULL);
bd0c824a
PR
1729 if (rv < SS_SUCCESS)
1730 BUG();
bbeb641c 1731
b30ab791 1732 ns.i = device->state.i;
8c7e16c3
PR
1733 ns_max.role = max_role(ns.role, ns_max.role);
1734 ns_max.peer = max_role(ns.peer, ns_max.peer);
1735 ns_max.conn = max_t(enum drbd_conns, ns.conn, ns_max.conn);
1736 ns_max.disk = max_t(enum drbd_disk_state, ns.disk, ns_max.disk);
1737 ns_max.pdsk = max_t(enum drbd_disk_state, ns.pdsk, ns_max.pdsk);
1738
1739 ns_min.role = min_role(ns.role, ns_min.role);
1740 ns_min.peer = min_role(ns.peer, ns_min.peer);
1741 ns_min.conn = min_t(enum drbd_conns, ns.conn, ns_min.conn);
1742 ns_min.disk = min_t(enum drbd_disk_state, ns.disk, ns_min.disk);
1743 ns_min.pdsk = min_t(enum drbd_disk_state, ns.pdsk, ns_min.pdsk);
bd0c824a 1744 }
695d08fa 1745 rcu_read_unlock();
bbeb641c 1746
f132f554
PR
1747 if (number_of_volumes == 0) {
1748 ns_min = ns_max = (union drbd_state) { {
1749 .role = R_SECONDARY,
1750 .peer = R_UNKNOWN,
1751 .conn = val.conn,
1752 .disk = D_DISKLESS,
1753 .pdsk = D_UNKNOWN
1754 } };
1755 }
1756
6bbf53ca
AG
1757 ns_min.susp = ns_max.susp = connection->resource->susp;
1758 ns_min.susp_nod = ns_max.susp_nod = connection->resource->susp_nod;
1759 ns_min.susp_fen = ns_max.susp_fen = connection->resource->susp_fen;
8c7e16c3
PR
1760
1761 *pns_min = ns_min;
1762 *pns_max = ns_max;
bbeb641c
PR
1763}
1764
df24aa45 1765static enum drbd_state_rv
bde89a9e 1766_conn_rq_cond(struct drbd_connection *connection, union drbd_state mask, union drbd_state val)
df24aa45 1767{
88ea685d 1768 enum drbd_state_rv err, rv = SS_UNKNOWN_ERROR; /* continue waiting */;
df24aa45 1769
bde89a9e 1770 if (test_and_clear_bit(CONN_WD_ST_CHG_OKAY, &connection->flags))
88ea685d 1771 rv = SS_CW_SUCCESS;
df24aa45 1772
bde89a9e 1773 if (test_and_clear_bit(CONN_WD_ST_CHG_FAIL, &connection->flags))
88ea685d 1774 rv = SS_CW_FAILED_BY_PEER;
df24aa45 1775
88ea685d
PR
1776 err = conn_is_valid_transition(connection, mask, val, 0);
1777 if (err == SS_SUCCESS && connection->cstate == C_WF_REPORT_PARAMS)
1778 return rv;
df24aa45 1779
88ea685d 1780 return err;
df24aa45
PR
1781}
1782
bbeb641c 1783enum drbd_state_rv
bde89a9e 1784_conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
bbeb641c
PR
1785 enum chg_state_flags flags)
1786{
1787 enum drbd_state_rv rv = SS_SUCCESS;
bbeb641c 1788 struct after_conn_state_chg_work *acscw;
bde89a9e 1789 enum drbd_conns oc = connection->cstate;
8c7e16c3 1790 union drbd_state ns_max, ns_min, os;
c02abda2 1791 bool have_mutex = false;
bbeb641c 1792
07fc9619
PR
1793 if (mask.conn) {
1794 rv = is_valid_conn_transition(oc, val.conn);
1795 if (rv < SS_SUCCESS)
1796 goto abort;
1797 }
bbeb641c 1798
bde89a9e 1799 rv = conn_is_valid_transition(connection, mask, val, flags);
bbeb641c
PR
1800 if (rv < SS_SUCCESS)
1801 goto abort;
1802
df24aa45
PR
1803 if (oc == C_WF_REPORT_PARAMS && val.conn == C_DISCONNECTING &&
1804 !(flags & (CS_LOCAL_ONLY | CS_HARD))) {
c02abda2
LE
1805
1806 /* This will be a cluster-wide state change.
1807 * Need to give up the spinlock, grab the mutex,
1808 * then send the state change request, ... */
0500813f 1809 spin_unlock_irq(&connection->resource->req_lock);
bde89a9e 1810 mutex_lock(&connection->cstate_mutex);
c02abda2
LE
1811 have_mutex = true;
1812
bde89a9e
AG
1813 set_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
1814 if (conn_send_state_req(connection, mask, val)) {
c02abda2 1815 /* sending failed. */
bde89a9e 1816 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
c02abda2
LE
1817 rv = SS_CW_FAILED_BY_PEER;
1818 /* need to re-aquire the spin lock, though */
1819 goto abort_unlocked;
1820 }
1821
1822 if (val.conn == C_DISCONNECTING)
bde89a9e 1823 set_bit(DISCONNECT_SENT, &connection->flags);
c02abda2
LE
1824
1825 /* ... and re-aquire the spinlock.
1826 * If _conn_rq_cond() returned >= SS_SUCCESS, we must call
1827 * conn_set_state() within the same spinlock. */
0500813f 1828 spin_lock_irq(&connection->resource->req_lock);
bde89a9e
AG
1829 wait_event_lock_irq(connection->ping_wait,
1830 (rv = _conn_rq_cond(connection, mask, val)),
0500813f 1831 connection->resource->req_lock);
bde89a9e 1832 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
df24aa45
PR
1833 if (rv < SS_SUCCESS)
1834 goto abort;
1835 }
1836
bde89a9e 1837 conn_old_common_state(connection, &os, &flags);
706cb24c 1838 flags |= CS_DC_SUSP;
bde89a9e
AG
1839 conn_set_state(connection, mask, val, &ns_min, &ns_max, flags);
1840 conn_pr_state_change(connection, os, ns_max, flags);
bbeb641c
PR
1841
1842 acscw = kmalloc(sizeof(*acscw), GFP_ATOMIC);
1843 if (acscw) {
435693e8 1844 acscw->oc = os.conn;
8c7e16c3 1845 acscw->ns_min = ns_min;
5f082f98 1846 acscw->ns_max = ns_max;
bbeb641c 1847 acscw->flags = flags;
4c007603 1848 acscw->w.cb = w_after_conn_state_ch;
bde89a9e 1849 kref_get(&connection->kref);
84b8c06b 1850 acscw->connection = connection;
4c007603 1851 drbd_queue_work(&connection->sender_work, &acscw->w);
bbeb641c 1852 } else {
1ec861eb 1853 drbd_err(connection, "Could not kmalloc an acscw\n");
b8907339 1854 }
bbeb641c 1855
a01842eb 1856 abort:
c02abda2
LE
1857 if (have_mutex) {
1858 /* mutex_unlock() "... must not be used in interrupt context.",
1859 * so give up the spinlock, then re-aquire it */
0500813f 1860 spin_unlock_irq(&connection->resource->req_lock);
c02abda2 1861 abort_unlocked:
bde89a9e 1862 mutex_unlock(&connection->cstate_mutex);
0500813f 1863 spin_lock_irq(&connection->resource->req_lock);
c02abda2
LE
1864 }
1865 if (rv < SS_SUCCESS && flags & CS_VERBOSE) {
1ec861eb
AG
1866 drbd_err(connection, "State change failed: %s\n", drbd_set_st_err_str(rv));
1867 drbd_err(connection, " mask = 0x%x val = 0x%x\n", mask.i, val.i);
1868 drbd_err(connection, " old_conn:%s wanted_conn:%s\n", drbd_conn_str(oc), drbd_conn_str(val.conn));
a01842eb 1869 }
bbeb641c
PR
1870 return rv;
1871}
1872
1873enum drbd_state_rv
bde89a9e 1874conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
bbeb641c
PR
1875 enum chg_state_flags flags)
1876{
1877 enum drbd_state_rv rv;
1878
0500813f 1879 spin_lock_irq(&connection->resource->req_lock);
bde89a9e 1880 rv = _conn_request_state(connection, mask, val, flags);
0500813f 1881 spin_unlock_irq(&connection->resource->req_lock);
bbeb641c
PR
1882
1883 return rv;
b8907339 1884}