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