4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
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>.
10 Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
11 from Logicworks, Inc. for making SDP replication support possible.
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)
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.
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.
28 #include <linux/drbd_limits.h>
30 #include "drbd_protocol.h"
33 struct after_state_chg_work {
35 struct drbd_device *device;
38 enum chg_state_flags flags;
39 struct completion *done;
42 enum sanitize_state_warnings {
44 ABORTED_ONLINE_VERIFY,
46 CONNECTION_LOST_NEGOTIATING,
47 IMPLICITLY_UPGRADED_DISK,
48 IMPLICITLY_UPGRADED_PDSK,
51 static int w_after_state_ch(struct drbd_work *w, int unused);
52 static void after_state_ch(struct drbd_device *device, union drbd_state os,
53 union drbd_state ns, enum chg_state_flags flags);
54 static enum drbd_state_rv is_valid_state(struct drbd_device *, union drbd_state);
55 static enum drbd_state_rv is_valid_soft_transition(union drbd_state, union drbd_state, struct drbd_connection *);
56 static enum drbd_state_rv is_valid_transition(union drbd_state os, union drbd_state ns);
57 static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state ns,
58 enum sanitize_state_warnings *warn);
60 static inline bool is_susp(union drbd_state s)
62 return s.susp || s.susp_nod || s.susp_fen;
65 bool conn_all_vols_unconf(struct drbd_connection *connection)
67 struct drbd_peer_device *peer_device;
72 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
73 struct drbd_device *device = peer_device->device;
74 if (device->state.disk != D_DISKLESS ||
75 device->state.conn != C_STANDALONE ||
76 device->state.role != R_SECONDARY) {
86 /* Unfortunately the states where not correctly ordered, when
87 they where defined. therefore can not use max_t() here. */
88 static enum drbd_role max_role(enum drbd_role role1, enum drbd_role role2)
90 if (role1 == R_PRIMARY || role2 == R_PRIMARY)
92 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
96 static enum drbd_role min_role(enum drbd_role role1, enum drbd_role role2)
98 if (role1 == R_UNKNOWN || role2 == R_UNKNOWN)
100 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
105 enum drbd_role conn_highest_role(struct drbd_connection *connection)
107 enum drbd_role role = R_UNKNOWN;
108 struct drbd_peer_device *peer_device;
112 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
113 struct drbd_device *device = peer_device->device;
114 role = max_role(role, device->state.role);
121 enum drbd_role conn_highest_peer(struct drbd_connection *connection)
123 enum drbd_role peer = R_UNKNOWN;
124 struct drbd_peer_device *peer_device;
128 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
129 struct drbd_device *device = peer_device->device;
130 peer = max_role(peer, device->state.peer);
137 enum drbd_disk_state conn_highest_disk(struct drbd_connection *connection)
139 enum drbd_disk_state ds = D_DISKLESS;
140 struct drbd_peer_device *peer_device;
144 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
145 struct drbd_device *device = peer_device->device;
146 ds = max_t(enum drbd_disk_state, ds, device->state.disk);
153 enum drbd_disk_state conn_lowest_disk(struct drbd_connection *connection)
155 enum drbd_disk_state ds = D_MASK;
156 struct drbd_peer_device *peer_device;
160 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
161 struct drbd_device *device = peer_device->device;
162 ds = min_t(enum drbd_disk_state, ds, device->state.disk);
169 enum drbd_disk_state conn_highest_pdsk(struct drbd_connection *connection)
171 enum drbd_disk_state ds = D_DISKLESS;
172 struct drbd_peer_device *peer_device;
176 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
177 struct drbd_device *device = peer_device->device;
178 ds = max_t(enum drbd_disk_state, ds, device->state.pdsk);
185 enum drbd_conns conn_lowest_conn(struct drbd_connection *connection)
187 enum drbd_conns conn = C_MASK;
188 struct drbd_peer_device *peer_device;
192 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
193 struct drbd_device *device = peer_device->device;
194 conn = min_t(enum drbd_conns, conn, device->state.conn);
201 static bool no_peer_wf_report_params(struct drbd_connection *connection)
203 struct drbd_peer_device *peer_device;
208 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
209 if (peer_device->device->state.conn == C_WF_REPORT_PARAMS) {
220 * cl_wide_st_chg() - true if the state change is a cluster wide one
221 * @device: DRBD device.
222 * @os: old (current) state.
223 * @ns: new (wanted) state.
225 static int cl_wide_st_chg(struct drbd_device *device,
226 union drbd_state os, union drbd_state ns)
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) ||
232 (os.disk != D_FAILED && ns.disk == D_FAILED))) ||
233 (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
234 (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S) ||
235 (os.conn == C_CONNECTED && ns.conn == C_WF_REPORT_PARAMS);
238 static union drbd_state
239 apply_mask_val(union drbd_state os, union drbd_state mask, union drbd_state val)
242 ns.i = (os.i & ~mask.i) | val.i;
247 drbd_change_state(struct drbd_device *device, enum chg_state_flags f,
248 union drbd_state mask, union drbd_state val)
252 enum drbd_state_rv rv;
254 spin_lock_irqsave(&device->resource->req_lock, flags);
255 ns = apply_mask_val(drbd_read_state(device), mask, val);
256 rv = _drbd_set_state(device, ns, f, NULL);
257 spin_unlock_irqrestore(&device->resource->req_lock, flags);
263 * drbd_force_state() - Impose a change which happens outside our control on our state
264 * @device: DRBD device.
265 * @mask: mask of state bits to change.
266 * @val: value of new state bits.
268 void drbd_force_state(struct drbd_device *device,
269 union drbd_state mask, union drbd_state val)
271 drbd_change_state(device, CS_HARD, mask, val);
274 static enum drbd_state_rv
275 _req_st_cond(struct drbd_device *device, union drbd_state mask,
276 union drbd_state val)
278 union drbd_state os, ns;
280 enum drbd_state_rv rv;
282 if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &device->flags))
283 return SS_CW_SUCCESS;
285 if (test_and_clear_bit(CL_ST_CHG_FAIL, &device->flags))
286 return SS_CW_FAILED_BY_PEER;
288 spin_lock_irqsave(&device->resource->req_lock, flags);
289 os = drbd_read_state(device);
290 ns = sanitize_state(device, apply_mask_val(os, mask, val), NULL);
291 rv = is_valid_transition(os, ns);
292 if (rv >= SS_SUCCESS)
293 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
295 if (!cl_wide_st_chg(device, os, ns))
297 if (rv == SS_UNKNOWN_ERROR) {
298 rv = is_valid_state(device, ns);
299 if (rv >= SS_SUCCESS) {
300 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
301 if (rv >= SS_SUCCESS)
302 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
305 spin_unlock_irqrestore(&device->resource->req_lock, flags);
311 * drbd_req_state() - Perform an eventually cluster wide state change
312 * @device: DRBD device.
313 * @mask: mask of state bits to change.
314 * @val: value of new state bits.
317 * Should not be called directly, use drbd_request_state() or
318 * _drbd_request_state().
320 static enum drbd_state_rv
321 drbd_req_state(struct drbd_device *device, union drbd_state mask,
322 union drbd_state val, enum chg_state_flags f)
324 struct completion done;
326 union drbd_state os, ns;
327 enum drbd_state_rv rv;
329 init_completion(&done);
331 if (f & CS_SERIALIZE)
332 mutex_lock(device->state_mutex);
334 spin_lock_irqsave(&device->resource->req_lock, flags);
335 os = drbd_read_state(device);
336 ns = sanitize_state(device, apply_mask_val(os, mask, val), NULL);
337 rv = is_valid_transition(os, ns);
338 if (rv < SS_SUCCESS) {
339 spin_unlock_irqrestore(&device->resource->req_lock, flags);
343 if (cl_wide_st_chg(device, os, ns)) {
344 rv = is_valid_state(device, ns);
345 if (rv == SS_SUCCESS)
346 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
347 spin_unlock_irqrestore(&device->resource->req_lock, flags);
349 if (rv < SS_SUCCESS) {
351 print_st_err(device, os, ns, rv);
355 if (drbd_send_state_req(first_peer_device(device), mask, val)) {
356 rv = SS_CW_FAILED_BY_PEER;
358 print_st_err(device, os, ns, rv);
362 wait_event(device->state_wait,
363 (rv = _req_st_cond(device, mask, val)));
365 if (rv < SS_SUCCESS) {
367 print_st_err(device, os, ns, rv);
370 spin_lock_irqsave(&device->resource->req_lock, flags);
371 ns = apply_mask_val(drbd_read_state(device), mask, val);
372 rv = _drbd_set_state(device, ns, f, &done);
374 rv = _drbd_set_state(device, ns, f, &done);
377 spin_unlock_irqrestore(&device->resource->req_lock, flags);
379 if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
380 D_ASSERT(device, current != first_peer_device(device)->connection->worker.task);
381 wait_for_completion(&done);
385 if (f & CS_SERIALIZE)
386 mutex_unlock(device->state_mutex);
392 * _drbd_request_state() - Request a state change (with flags)
393 * @device: DRBD device.
394 * @mask: mask of state bits to change.
395 * @val: value of new state bits.
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.
402 _drbd_request_state(struct drbd_device *device, union drbd_state mask,
403 union drbd_state val, enum chg_state_flags f)
405 enum drbd_state_rv rv;
407 wait_event(device->state_wait,
408 (rv = drbd_req_state(device, mask, val, f)) != SS_IN_TRANSIENT_STATE);
413 static void print_st(struct drbd_device *device, char *name, union drbd_state ns)
415 drbd_err(device, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c%c%c }\n",
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' : '-'
431 void print_st_err(struct drbd_device *device, union drbd_state os,
432 union drbd_state ns, enum drbd_state_rv err)
434 if (err == SS_IN_TRANSIENT_STATE)
436 drbd_err(device, "State change failed: %s\n", drbd_set_st_err_str(err));
437 print_st(device, " state", os);
438 print_st(device, "wanted", ns);
441 static long print_state_change(char *pb, union drbd_state os, union drbd_state ns,
442 enum chg_state_flags flags)
448 if (ns.role != os.role && flags & CS_DC_ROLE)
449 pbp += sprintf(pbp, "role( %s -> %s ) ",
450 drbd_role_str(os.role),
451 drbd_role_str(ns.role));
452 if (ns.peer != os.peer && flags & CS_DC_PEER)
453 pbp += sprintf(pbp, "peer( %s -> %s ) ",
454 drbd_role_str(os.peer),
455 drbd_role_str(ns.peer));
456 if (ns.conn != os.conn && flags & CS_DC_CONN)
457 pbp += sprintf(pbp, "conn( %s -> %s ) ",
458 drbd_conn_str(os.conn),
459 drbd_conn_str(ns.conn));
460 if (ns.disk != os.disk && flags & CS_DC_DISK)
461 pbp += sprintf(pbp, "disk( %s -> %s ) ",
462 drbd_disk_str(os.disk),
463 drbd_disk_str(ns.disk));
464 if (ns.pdsk != os.pdsk && flags & CS_DC_PDSK)
465 pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
466 drbd_disk_str(os.pdsk),
467 drbd_disk_str(ns.pdsk));
472 static void drbd_pr_state_change(struct drbd_device *device, union drbd_state os, union drbd_state ns,
473 enum chg_state_flags flags)
478 pbp += print_state_change(pbp, os, ns, flags ^ CS_DC_MASK);
480 if (ns.aftr_isp != os.aftr_isp)
481 pbp += sprintf(pbp, "aftr_isp( %d -> %d ) ",
484 if (ns.peer_isp != os.peer_isp)
485 pbp += sprintf(pbp, "peer_isp( %d -> %d ) ",
488 if (ns.user_isp != os.user_isp)
489 pbp += sprintf(pbp, "user_isp( %d -> %d ) ",
494 drbd_info(device, "%s\n", pb);
497 static void conn_pr_state_change(struct drbd_connection *connection, union drbd_state os, union drbd_state ns,
498 enum chg_state_flags flags)
503 pbp += print_state_change(pbp, os, ns, flags);
505 if (is_susp(ns) != is_susp(os) && flags & CS_DC_SUSP)
506 pbp += sprintf(pbp, "susp( %d -> %d ) ",
511 drbd_info(connection, "%s\n", pb);
516 * is_valid_state() - Returns an SS_ error code if ns is not valid
517 * @device: DRBD device.
518 * @ns: State to consider.
520 static enum drbd_state_rv
521 is_valid_state(struct drbd_device *device, union drbd_state ns)
523 /* See drbd_state_sw_errors in drbd_strings.c */
525 enum drbd_fencing_p fp;
526 enum drbd_state_rv rv = SS_SUCCESS;
531 if (get_ldev(device)) {
532 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
536 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
538 if (!nc->two_primaries && ns.role == R_PRIMARY) {
539 if (ns.peer == R_PRIMARY)
540 rv = SS_TWO_PRIMARIES;
541 else if (conn_highest_peer(first_peer_device(device)->connection) == R_PRIMARY)
542 rv = SS_O_VOL_PEER_PRI;
547 /* already found a reason to abort */;
548 else if (ns.role == R_SECONDARY && device->open_cnt)
549 rv = SS_DEVICE_IN_USE;
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;
554 else if (fp >= FP_RESOURCE &&
555 ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
558 else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
559 rv = SS_NO_UP_TO_DATE_DISK;
561 else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
562 rv = SS_NO_LOCAL_DISK;
564 else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
565 rv = SS_NO_REMOTE_DISK;
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;
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;
577 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
578 (nc->verify_alg[0] == 0))
579 rv = SS_NO_VERIFY_ALG;
581 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
582 first_peer_device(device)->connection->agreed_pro_version < 88)
583 rv = SS_NOT_SUPPORTED;
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;
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;
592 else if (ns.conn >= C_CONNECTED && ns.pdsk == D_UNKNOWN)
593 rv = SS_CONNECTED_OUTDATES;
601 * is_valid_soft_transition() - Returns an SS_ error code if the state transition is not possible
602 * This function limits state transitions that may be declined by DRBD. I.e.
603 * user requests (aka soft transitions).
604 * @device: DRBD device.
608 static enum drbd_state_rv
609 is_valid_soft_transition(union drbd_state os, union drbd_state ns, struct drbd_connection *connection)
611 enum drbd_state_rv rv = SS_SUCCESS;
613 if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
614 os.conn > C_CONNECTED)
615 rv = SS_RESYNC_RUNNING;
617 if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
618 rv = SS_ALREADY_STANDALONE;
620 if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
623 if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
624 rv = SS_NO_NET_CONFIG;
626 if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
627 rv = SS_LOWER_THAN_OUTDATED;
629 if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
630 rv = SS_IN_TRANSIENT_STATE;
632 /* if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
633 rv = SS_IN_TRANSIENT_STATE; */
635 /* While establishing a connection only allow cstate to change.
636 Delay/refuse role changes, detach attach etc... */
637 if (test_bit(STATE_SENT, &connection->flags) &&
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;
642 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
643 rv = SS_NEED_CONNECTION;
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;
649 if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
650 os.conn < C_CONNECTED)
651 rv = SS_NEED_CONNECTION;
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... */
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;
664 static enum drbd_state_rv
665 is_valid_conn_transition(enum drbd_conns oc, enum drbd_conns nc)
667 /* no change -> nothing to do, at least for the connection part */
669 return SS_NOTHING_TO_DO;
671 /* disconnect of an unconfigured connection does not make sense */
672 if (oc == C_STANDALONE && nc == C_DISCONNECTING)
673 return SS_ALREADY_STANDALONE;
675 /* from C_STANDALONE, we start with C_UNCONNECTED */
676 if (oc == C_STANDALONE && nc != C_UNCONNECTED)
677 return SS_NEED_CONNECTION;
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;
684 /* After a network error only C_UNCONNECTED or C_DISCONNECTING may follow. */
685 if (oc >= C_TIMEOUT && oc <= C_TEAR_DOWN && nc != C_UNCONNECTED && nc != C_DISCONNECTING)
686 return SS_IN_TRANSIENT_STATE;
688 /* After C_DISCONNECTING only C_STANDALONE may follow */
689 if (oc == C_DISCONNECTING && nc != C_STANDALONE)
690 return SS_IN_TRANSIENT_STATE;
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.
704 static enum drbd_state_rv
705 is_valid_transition(union drbd_state os, union drbd_state ns)
707 enum drbd_state_rv rv;
709 rv = is_valid_conn_transition(os.conn, ns.conn);
711 /* we cannot fail (again) if we already detached */
712 if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
718 static void print_sanitize_warnings(struct drbd_device *device, enum sanitize_state_warnings warn)
720 static const char *msg_table[] = {
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",
729 if (warn != NO_WARNING)
730 drbd_warn(device, "%s\n", msg_table[warn]);
734 * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
735 * @device: DRBD device.
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.
743 static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state ns,
744 enum sanitize_state_warnings *warn)
746 enum drbd_fencing_p fp;
747 enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
753 if (get_ldev(device)) {
755 fp = rcu_dereference(device->ldev->disk_conf)->fencing;
760 /* Implications from connection to peer and peer_isp */
761 if (ns.conn < C_CONNECTED) {
764 if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
768 /* Clear the aftr_isp when becoming unconfigured */
769 if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
772 /* An implication of the disk states onto the connection state */
773 /* Abort resync if a disk fails/detaches */
774 if (ns.conn > C_CONNECTED && (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
776 *warn = ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T ?
777 ABORTED_ONLINE_VERIFY : ABORTED_RESYNC;
778 ns.conn = C_CONNECTED;
781 /* Connection breaks down before we finished "Negotiating" */
782 if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
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;
789 *warn = CONNECTION_LOST_NEGOTIATING;
790 ns.disk = D_DISKLESS;
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;
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) {
811 case C_PAUSED_SYNC_T:
812 case C_STARTING_SYNC_T:
815 disk_min = D_INCONSISTENT;
816 disk_max = D_OUTDATED;
817 pdsk_min = D_UP_TO_DATE;
818 pdsk_max = D_UP_TO_DATE;
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;
828 disk_min = D_DISKLESS;
829 disk_max = D_UP_TO_DATE;
830 pdsk_min = D_DISKLESS;
831 pdsk_max = D_UP_TO_DATE;
834 case C_PAUSED_SYNC_S:
835 case C_STARTING_SYNC_S:
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*/
843 disk_min = D_INCONSISTENT;
844 disk_max = D_INCONSISTENT;
845 pdsk_min = D_UP_TO_DATE;
846 pdsk_max = D_UP_TO_DATE;
849 disk_min = D_UP_TO_DATE;
850 disk_max = D_UP_TO_DATE;
851 pdsk_min = D_INCONSISTENT;
852 pdsk_max = D_INCONSISTENT;
855 case C_DISCONNECTING:
859 case C_NETWORK_FAILURE:
860 case C_PROTOCOL_ERROR:
862 case C_WF_CONNECTION:
863 case C_WF_REPORT_PARAMS:
867 if (ns.disk > disk_max)
870 if (ns.disk < disk_min) {
872 *warn = IMPLICITLY_UPGRADED_DISK;
875 if (ns.pdsk > pdsk_max)
878 if (ns.pdsk < pdsk_min) {
880 *warn = IMPLICITLY_UPGRADED_PDSK;
884 if (fp == FP_STONITH &&
885 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED))
886 ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
888 if (device->resource->res_opts.on_no_data == OND_SUSPEND_IO &&
889 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
890 ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
892 if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
893 if (ns.conn == C_SYNC_SOURCE)
894 ns.conn = C_PAUSED_SYNC_S;
895 if (ns.conn == C_SYNC_TARGET)
896 ns.conn = C_PAUSED_SYNC_T;
898 if (ns.conn == C_PAUSED_SYNC_S)
899 ns.conn = C_SYNC_SOURCE;
900 if (ns.conn == C_PAUSED_SYNC_T)
901 ns.conn = C_SYNC_TARGET;
907 void drbd_resume_al(struct drbd_device *device)
909 if (test_and_clear_bit(AL_SUSPENDED, &device->flags))
910 drbd_info(device, "Resumed AL updates\n");
913 /* helper for __drbd_set_state */
914 static void set_ov_position(struct drbd_device *device, enum drbd_conns cs)
916 if (first_peer_device(device)->connection->agreed_pro_version < 90)
917 device->ov_start_sector = 0;
918 device->rs_total = drbd_bm_bits(device);
919 device->ov_position = 0;
920 if (cs == C_VERIFY_T) {
921 /* starting online verify from an arbitrary position
922 * does not fit well into the existing protocol.
923 * on C_VERIFY_T, we initialize ov_left and friends
924 * implicitly in receive_DataRequest once the
925 * first P_OV_REQUEST is received */
926 device->ov_start_sector = ~(sector_t)0;
928 unsigned long bit = BM_SECT_TO_BIT(device->ov_start_sector);
929 if (bit >= device->rs_total) {
930 device->ov_start_sector =
931 BM_BIT_TO_SECT(device->rs_total - 1);
932 device->rs_total = 1;
934 device->rs_total -= bit;
935 device->ov_position = device->ov_start_sector;
937 device->ov_left = device->rs_total;
941 * __drbd_set_state() - Set a new DRBD state
942 * @device: DRBD device.
945 * @done: Optional completion, that will get completed after the after_state_ch() finished
947 * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
950 __drbd_set_state(struct drbd_device *device, union drbd_state ns,
951 enum chg_state_flags flags, struct completion *done)
954 enum drbd_state_rv rv = SS_SUCCESS;
955 enum sanitize_state_warnings ssw;
956 struct after_state_chg_work *ascw;
957 bool did_remote, should_do_remote;
959 os = drbd_read_state(device);
961 ns = sanitize_state(device, ns, &ssw);
963 return SS_NOTHING_TO_DO;
965 rv = is_valid_transition(os, ns);
969 if (!(flags & CS_HARD)) {
970 /* pre-state-change checks ; only look at ns */
971 /* See drbd_state_sw_errors in drbd_strings.c */
973 rv = is_valid_state(device, ns);
974 if (rv < SS_SUCCESS) {
975 /* If the old state was illegal as well, then let
978 if (is_valid_state(device, os) == rv)
979 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
981 rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
984 if (rv < SS_SUCCESS) {
985 if (flags & CS_VERBOSE)
986 print_st_err(device, os, ns, rv);
990 print_sanitize_warnings(device, ssw);
992 drbd_pr_state_change(device, os, ns, flags);
994 /* Display changes to the susp* flags that where caused by the call to
995 sanitize_state(). Only display it here if we where not called from
996 _conn_request_state() */
997 if (!(flags & CS_DC_SUSP))
998 conn_pr_state_change(first_peer_device(device)->connection, os, ns,
999 (flags & ~CS_DC_MASK) | CS_DC_SUSP);
1001 /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
1002 * on the ldev here, to be sure the transition -> D_DISKLESS resp.
1003 * drbd_ldev_destroy() won't happen before our corresponding
1004 * after_state_ch works run, where we put_ldev again. */
1005 if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
1006 (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
1007 atomic_inc(&device->local_cnt);
1009 did_remote = drbd_should_do_remote(device->state);
1010 device->state.i = ns.i;
1011 should_do_remote = drbd_should_do_remote(device->state);
1012 device->resource->susp = ns.susp;
1013 device->resource->susp_nod = ns.susp_nod;
1014 device->resource->susp_fen = ns.susp_fen;
1016 /* put replicated vs not-replicated requests in seperate epochs */
1017 if (did_remote != should_do_remote)
1018 start_new_tl_epoch(first_peer_device(device)->connection);
1020 if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
1021 drbd_print_uuids(device, "attached to UUIDs");
1023 /* Wake up role changes, that were delayed because of connection establishing */
1024 if (os.conn == C_WF_REPORT_PARAMS && ns.conn != C_WF_REPORT_PARAMS &&
1025 no_peer_wf_report_params(first_peer_device(device)->connection))
1026 clear_bit(STATE_SENT, &first_peer_device(device)->connection->flags);
1028 wake_up(&device->misc_wait);
1029 wake_up(&device->state_wait);
1030 wake_up(&first_peer_device(device)->connection->ping_wait);
1032 /* Aborted verify run, or we reached the stop sector.
1033 * Log the last position, unless end-of-device. */
1034 if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
1035 ns.conn <= C_CONNECTED) {
1036 device->ov_start_sector =
1037 BM_BIT_TO_SECT(drbd_bm_bits(device) - device->ov_left);
1038 if (device->ov_left)
1039 drbd_info(device, "Online Verify reached sector %llu\n",
1040 (unsigned long long)device->ov_start_sector);
1043 if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
1044 (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
1045 drbd_info(device, "Syncer continues.\n");
1046 device->rs_paused += (long)jiffies
1047 -(long)device->rs_mark_time[device->rs_last_mark];
1048 if (ns.conn == C_SYNC_TARGET)
1049 mod_timer(&device->resync_timer, jiffies);
1052 if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
1053 (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
1054 drbd_info(device, "Resync suspended\n");
1055 device->rs_mark_time[device->rs_last_mark] = jiffies;
1058 if (os.conn == C_CONNECTED &&
1059 (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
1060 unsigned long now = jiffies;
1063 set_ov_position(device, ns.conn);
1064 device->rs_start = now;
1065 device->rs_last_events = 0;
1066 device->rs_last_sect_ev = 0;
1067 device->ov_last_oos_size = 0;
1068 device->ov_last_oos_start = 0;
1070 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1071 device->rs_mark_left[i] = device->ov_left;
1072 device->rs_mark_time[i] = now;
1075 drbd_rs_controller_reset(device);
1077 if (ns.conn == C_VERIFY_S) {
1078 drbd_info(device, "Starting Online Verify from sector %llu\n",
1079 (unsigned long long)device->ov_position);
1080 mod_timer(&device->resync_timer, jiffies);
1084 if (get_ldev(device)) {
1085 u32 mdf = device->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
1086 MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
1087 MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
1089 mdf &= ~MDF_AL_CLEAN;
1090 if (test_bit(CRASHED_PRIMARY, &device->flags))
1091 mdf |= MDF_CRASHED_PRIMARY;
1092 if (device->state.role == R_PRIMARY ||
1093 (device->state.pdsk < D_INCONSISTENT && device->state.peer == R_PRIMARY))
1094 mdf |= MDF_PRIMARY_IND;
1095 if (device->state.conn > C_WF_REPORT_PARAMS)
1096 mdf |= MDF_CONNECTED_IND;
1097 if (device->state.disk > D_INCONSISTENT)
1098 mdf |= MDF_CONSISTENT;
1099 if (device->state.disk > D_OUTDATED)
1100 mdf |= MDF_WAS_UP_TO_DATE;
1101 if (device->state.pdsk <= D_OUTDATED && device->state.pdsk >= D_INCONSISTENT)
1102 mdf |= MDF_PEER_OUT_DATED;
1103 if (mdf != device->ldev->md.flags) {
1104 device->ldev->md.flags = mdf;
1105 drbd_md_mark_dirty(device);
1107 if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
1108 drbd_set_ed_uuid(device, device->ldev->md.uuid[UI_CURRENT]);
1112 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
1113 if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
1114 os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
1115 set_bit(CONSIDER_RESYNC, &device->flags);
1117 /* Receiver should clean up itself */
1118 if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
1119 drbd_thread_stop_nowait(&first_peer_device(device)->connection->receiver);
1121 /* Now the receiver finished cleaning up itself, it should die */
1122 if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
1123 drbd_thread_stop_nowait(&first_peer_device(device)->connection->receiver);
1125 /* Upon network failure, we need to restart the receiver. */
1126 if (os.conn > C_WF_CONNECTION &&
1127 ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
1128 drbd_thread_restart_nowait(&first_peer_device(device)->connection->receiver);
1130 /* Resume AL writing if we get a connection */
1131 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
1132 drbd_resume_al(device);
1133 first_peer_device(device)->connection->connect_cnt++;
1136 /* remember last attach time so request_timer_fn() won't
1137 * kill newly established sessions while we are still trying to thaw
1138 * previously frozen IO */
1139 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
1140 ns.disk > D_NEGOTIATING)
1141 device->last_reattach_jif = jiffies;
1143 ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
1147 ascw->flags = flags;
1148 ascw->w.cb = w_after_state_ch;
1149 ascw->device = device;
1151 drbd_queue_work(&first_peer_device(device)->connection->sender_work,
1154 drbd_err(device, "Could not kmalloc an ascw\n");
1160 static int w_after_state_ch(struct drbd_work *w, int unused)
1162 struct after_state_chg_work *ascw =
1163 container_of(w, struct after_state_chg_work, w);
1164 struct drbd_device *device = ascw->device;
1166 after_state_ch(device, ascw->os, ascw->ns, ascw->flags);
1167 if (ascw->flags & CS_WAIT_COMPLETE)
1168 complete(ascw->done);
1174 static void abw_start_sync(struct drbd_device *device, int rv)
1177 drbd_err(device, "Writing the bitmap failed not starting resync.\n");
1178 _drbd_request_state(device, NS(conn, C_CONNECTED), CS_VERBOSE);
1182 switch (device->state.conn) {
1183 case C_STARTING_SYNC_T:
1184 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
1186 case C_STARTING_SYNC_S:
1187 drbd_start_resync(device, C_SYNC_SOURCE);
1192 int drbd_bitmap_io_from_worker(struct drbd_device *device,
1193 int (*io_fn)(struct drbd_device *),
1194 char *why, enum bm_flag flags)
1198 D_ASSERT(device, current == first_peer_device(device)->connection->worker.task);
1200 /* open coded non-blocking drbd_suspend_io(device); */
1201 set_bit(SUSPEND_IO, &device->flags);
1203 drbd_bm_lock(device, why, flags);
1205 drbd_bm_unlock(device);
1207 drbd_resume_io(device);
1213 * after_state_ch() - Perform after state change actions that may sleep
1214 * @device: DRBD device.
1219 static void after_state_ch(struct drbd_device *device, union drbd_state os,
1220 union drbd_state ns, enum chg_state_flags flags)
1222 struct drbd_resource *resource = device->resource;
1223 struct sib_info sib;
1225 sib.sib_reason = SIB_STATE_CHANGE;
1229 if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
1230 clear_bit(CRASHED_PRIMARY, &device->flags);
1232 device->p_uuid[UI_FLAGS] &= ~((u64)2);
1235 /* Inform userspace about the change... */
1236 drbd_bcast_event(device, &sib);
1238 if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
1239 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
1240 drbd_khelper(device, "pri-on-incon-degr");
1242 /* Here we have the actions that are performed after a
1243 state change. This function might sleep */
1246 struct drbd_connection *connection = first_peer_device(device)->connection;
1247 enum drbd_req_event what = NOTHING;
1249 spin_lock_irq(&device->resource->req_lock);
1250 if (os.conn < C_CONNECTED && conn_lowest_conn(connection) >= C_CONNECTED)
1253 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
1254 conn_lowest_disk(connection) > D_NEGOTIATING)
1255 what = RESTART_FROZEN_DISK_IO;
1257 if (resource->susp_nod && what != NOTHING) {
1258 _tl_restart(connection, what);
1259 _conn_request_state(connection,
1260 (union drbd_state) { { .susp_nod = 1 } },
1261 (union drbd_state) { { .susp_nod = 0 } },
1264 spin_unlock_irq(&device->resource->req_lock);
1268 struct drbd_connection *connection = first_peer_device(device)->connection;
1270 spin_lock_irq(&device->resource->req_lock);
1271 if (resource->susp_fen && conn_lowest_conn(connection) >= C_CONNECTED) {
1272 /* case2: The connection was established again: */
1273 struct drbd_peer_device *peer_device;
1277 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
1278 clear_bit(NEW_CUR_UUID, &peer_device->device->flags);
1280 _tl_restart(connection, RESEND);
1281 _conn_request_state(connection,
1282 (union drbd_state) { { .susp_fen = 1 } },
1283 (union drbd_state) { { .susp_fen = 0 } },
1286 spin_unlock_irq(&device->resource->req_lock);
1289 /* Became sync source. With protocol >= 96, we still need to send out
1290 * the sync uuid now. Need to do that before any drbd_send_state, or
1291 * the other side may go "paused sync" before receiving the sync uuids,
1292 * which is unexpected. */
1293 if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
1294 (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
1295 first_peer_device(device)->connection->agreed_pro_version >= 96 && get_ldev(device)) {
1296 drbd_gen_and_send_sync_uuid(first_peer_device(device));
1300 /* Do not change the order of the if above and the two below... */
1301 if (os.pdsk == D_DISKLESS &&
1302 ns.pdsk > D_DISKLESS && ns.pdsk != D_UNKNOWN) { /* attach on the peer */
1303 /* we probably will start a resync soon.
1304 * make sure those things are properly reset. */
1305 device->rs_total = 0;
1306 device->rs_failed = 0;
1307 atomic_set(&device->rs_pending_cnt, 0);
1308 drbd_rs_cancel_all(device);
1310 drbd_send_uuids(first_peer_device(device));
1311 drbd_send_state(first_peer_device(device), ns);
1313 /* No point in queuing send_bitmap if we don't have a connection
1314 * anymore, so check also the _current_ state, not only the new state
1315 * at the time this work was queued. */
1316 if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
1317 device->state.conn == C_WF_BITMAP_S)
1318 drbd_queue_bitmap_io(device, &drbd_send_bitmap, NULL,
1319 "send_bitmap (WFBitMapS)",
1320 BM_LOCKED_TEST_ALLOWED);
1322 /* Lost contact to peer's copy of the data */
1323 if ((os.pdsk >= D_INCONSISTENT &&
1324 os.pdsk != D_UNKNOWN &&
1325 os.pdsk != D_OUTDATED)
1326 && (ns.pdsk < D_INCONSISTENT ||
1327 ns.pdsk == D_UNKNOWN ||
1328 ns.pdsk == D_OUTDATED)) {
1329 if (get_ldev(device)) {
1330 if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
1331 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1332 if (drbd_suspended(device)) {
1333 set_bit(NEW_CUR_UUID, &device->flags);
1335 drbd_uuid_new_current(device);
1336 drbd_send_uuids(first_peer_device(device));
1343 if (ns.pdsk < D_INCONSISTENT && get_ldev(device)) {
1344 if (os.peer == R_SECONDARY && ns.peer == R_PRIMARY &&
1345 device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1346 drbd_uuid_new_current(device);
1347 drbd_send_uuids(first_peer_device(device));
1349 /* D_DISKLESS Peer becomes secondary */
1350 if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
1351 /* We may still be Primary ourselves.
1352 * No harm done if the bitmap still changes,
1353 * redirtied pages will follow later. */
1354 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
1355 "demote diskless peer", BM_LOCKED_SET_ALLOWED);
1359 /* Write out all changed bits on demote.
1360 * Though, no need to da that just yet
1361 * if there is a resync going on still */
1362 if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
1363 device->state.conn <= C_CONNECTED && get_ldev(device)) {
1364 /* No changes to the bitmap expected this time, so assert that,
1365 * even though no harm was done if it did change. */
1366 drbd_bitmap_io_from_worker(device, &drbd_bm_write,
1367 "demote", BM_LOCKED_TEST_ALLOWED);
1371 /* Last part of the attaching process ... */
1372 if (ns.conn >= C_CONNECTED &&
1373 os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
1374 drbd_send_sizes(first_peer_device(device), 0, 0); /* to start sync... */
1375 drbd_send_uuids(first_peer_device(device));
1376 drbd_send_state(first_peer_device(device), ns);
1379 /* We want to pause/continue resync, tell peer. */
1380 if (ns.conn >= C_CONNECTED &&
1381 ((os.aftr_isp != ns.aftr_isp) ||
1382 (os.user_isp != ns.user_isp)))
1383 drbd_send_state(first_peer_device(device), ns);
1385 /* In case one of the isp bits got set, suspend other devices. */
1386 if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
1387 (ns.aftr_isp || ns.peer_isp || ns.user_isp))
1388 suspend_other_sg(device);
1390 /* Make sure the peer gets informed about eventual state
1391 changes (ISP bits) while we were in WFReportParams. */
1392 if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
1393 drbd_send_state(first_peer_device(device), ns);
1395 if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
1396 drbd_send_state(first_peer_device(device), ns);
1398 /* We are in the progress to start a full sync... */
1399 if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
1400 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
1401 /* no other bitmap changes expected during this phase */
1402 drbd_queue_bitmap_io(device,
1403 &drbd_bmio_set_n_write, &abw_start_sync,
1404 "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
1406 /* first half of local IO error, failure to attach,
1407 * or administrative detach */
1408 if (os.disk != D_FAILED && ns.disk == D_FAILED) {
1409 enum drbd_io_error_p eh = EP_PASS_ON;
1410 int was_io_error = 0;
1411 /* corresponding get_ldev was in __drbd_set_state, to serialize
1412 * our cleanup here with the transition to D_DISKLESS.
1413 * But is is still not save to dreference ldev here, since
1414 * we might come from an failed Attach before ldev was set. */
1417 eh = rcu_dereference(device->ldev->disk_conf)->on_io_error;
1420 was_io_error = test_and_clear_bit(WAS_IO_ERROR, &device->flags);
1422 if (was_io_error && eh == EP_CALL_HELPER)
1423 drbd_khelper(device, "local-io-error");
1425 /* Immediately allow completion of all application IO,
1426 * that waits for completion from the local disk,
1427 * if this was a force-detach due to disk_timeout
1428 * or administrator request (drbdsetup detach --force).
1429 * Do NOT abort otherwise.
1430 * Aborting local requests may cause serious problems,
1431 * if requests are completed to upper layers already,
1432 * and then later the already submitted local bio completes.
1433 * This can cause DMA into former bio pages that meanwhile
1434 * have been re-used for other things.
1435 * So aborting local requests may cause crashes,
1436 * or even worse, silent data corruption.
1438 if (test_and_clear_bit(FORCE_DETACH, &device->flags))
1439 tl_abort_disk_io(device);
1441 /* current state still has to be D_FAILED,
1442 * there is only one way out: to D_DISKLESS,
1443 * and that may only happen after our put_ldev below. */
1444 if (device->state.disk != D_FAILED)
1446 "ASSERT FAILED: disk is %s during detach\n",
1447 drbd_disk_str(device->state.disk));
1449 if (ns.conn >= C_CONNECTED)
1450 drbd_send_state(first_peer_device(device), ns);
1452 drbd_rs_cancel_all(device);
1454 /* In case we want to get something to stable storage still,
1455 * this may be the last chance.
1456 * Following put_ldev may transition to D_DISKLESS. */
1457 drbd_md_sync(device);
1462 /* second half of local IO error, failure to attach,
1463 * or administrative detach,
1464 * after local_cnt references have reached zero again */
1465 if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
1466 /* We must still be diskless,
1467 * re-attach has to be serialized with this! */
1468 if (device->state.disk != D_DISKLESS)
1470 "ASSERT FAILED: disk is %s while going diskless\n",
1471 drbd_disk_str(device->state.disk));
1473 if (ns.conn >= C_CONNECTED)
1474 drbd_send_state(first_peer_device(device), ns);
1475 /* corresponding get_ldev in __drbd_set_state
1476 * this may finally trigger drbd_ldev_destroy. */
1480 /* Notify peer that I had a local IO error, and did not detached.. */
1481 if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT && ns.conn >= C_CONNECTED)
1482 drbd_send_state(first_peer_device(device), ns);
1484 /* Disks got bigger while they were detached */
1485 if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
1486 test_and_clear_bit(RESYNC_AFTER_NEG, &device->flags)) {
1487 if (ns.conn == C_CONNECTED)
1488 resync_after_online_grow(device);
1491 /* A resync finished or aborted, wake paused devices... */
1492 if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
1493 (os.peer_isp && !ns.peer_isp) ||
1494 (os.user_isp && !ns.user_isp))
1495 resume_next_sg(device);
1497 /* sync target done with resync. Explicitly notify peer, even though
1498 * it should (at least for non-empty resyncs) already know itself. */
1499 if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
1500 drbd_send_state(first_peer_device(device), ns);
1502 /* Verify finished, or reached stop sector. Peer did not know about
1503 * the stop sector, and we may even have changed the stop sector during
1504 * verify to interrupt/stop early. Send the new state. */
1505 if (os.conn == C_VERIFY_S && ns.conn == C_CONNECTED
1506 && verify_can_do_stop_sector(device))
1507 drbd_send_state(first_peer_device(device), ns);
1509 /* This triggers bitmap writeout of potentially still unwritten pages
1510 * if the resync finished cleanly, or aborted because of peer disk
1511 * failure, or because of connection loss.
1512 * For resync aborted because of local disk failure, we cannot do
1513 * any bitmap writeout anymore.
1514 * No harm done if some bits change during this phase.
1516 if (os.conn > C_CONNECTED && ns.conn <= C_CONNECTED && get_ldev(device)) {
1517 drbd_queue_bitmap_io(device, &drbd_bm_write_copy_pages, NULL,
1518 "write from resync_finished", BM_LOCKED_CHANGE_ALLOWED);
1522 if (ns.disk == D_DISKLESS &&
1523 ns.conn == C_STANDALONE &&
1524 ns.role == R_SECONDARY) {
1525 if (os.aftr_isp != ns.aftr_isp)
1526 resume_next_sg(device);
1529 drbd_md_sync(device);
1532 struct after_conn_state_chg_work {
1535 union drbd_state ns_min;
1536 union drbd_state ns_max; /* new, max state, over all devices */
1537 enum chg_state_flags flags;
1538 struct drbd_connection *connection;
1541 static int w_after_conn_state_ch(struct drbd_work *w, int unused)
1543 struct after_conn_state_chg_work *acscw =
1544 container_of(w, struct after_conn_state_chg_work, w);
1545 struct drbd_connection *connection = acscw->connection;
1546 enum drbd_conns oc = acscw->oc;
1547 union drbd_state ns_max = acscw->ns_max;
1548 struct drbd_peer_device *peer_device;
1553 /* Upon network configuration, we need to start the receiver */
1554 if (oc == C_STANDALONE && ns_max.conn == C_UNCONNECTED)
1555 drbd_thread_start(&connection->receiver);
1557 if (oc == C_DISCONNECTING && ns_max.conn == C_STANDALONE) {
1558 struct net_conf *old_conf;
1560 mutex_lock(&connection->resource->conf_update);
1561 old_conf = connection->net_conf;
1562 connection->my_addr_len = 0;
1563 connection->peer_addr_len = 0;
1564 rcu_assign_pointer(connection->net_conf, NULL);
1565 conn_free_crypto(connection);
1566 mutex_unlock(&connection->resource->conf_update);
1572 if (ns_max.susp_fen) {
1573 /* case1: The outdate peer handler is successful: */
1574 if (ns_max.pdsk <= D_OUTDATED) {
1576 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1577 struct drbd_device *device = peer_device->device;
1578 if (test_bit(NEW_CUR_UUID, &device->flags)) {
1579 drbd_uuid_new_current(device);
1580 clear_bit(NEW_CUR_UUID, &device->flags);
1584 spin_lock_irq(&connection->resource->req_lock);
1585 _tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
1586 _conn_request_state(connection,
1587 (union drbd_state) { { .susp_fen = 1 } },
1588 (union drbd_state) { { .susp_fen = 0 } },
1590 spin_unlock_irq(&connection->resource->req_lock);
1593 kref_put(&connection->kref, drbd_destroy_connection);
1595 conn_md_sync(connection);
1600 void conn_old_common_state(struct drbd_connection *connection, union drbd_state *pcs, enum chg_state_flags *pf)
1602 enum chg_state_flags flags = ~0;
1603 struct drbd_peer_device *peer_device;
1604 int vnr, first_vol = 1;
1605 union drbd_dev_state os, cs = {
1606 { .role = R_SECONDARY,
1608 .conn = connection->cstate,
1614 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1615 struct drbd_device *device = peer_device->device;
1624 if (cs.role != os.role)
1625 flags &= ~CS_DC_ROLE;
1627 if (cs.peer != os.peer)
1628 flags &= ~CS_DC_PEER;
1630 if (cs.conn != os.conn)
1631 flags &= ~CS_DC_CONN;
1633 if (cs.disk != os.disk)
1634 flags &= ~CS_DC_DISK;
1636 if (cs.pdsk != os.pdsk)
1637 flags &= ~CS_DC_PDSK;
1646 static enum drbd_state_rv
1647 conn_is_valid_transition(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
1648 enum chg_state_flags flags)
1650 enum drbd_state_rv rv = SS_SUCCESS;
1651 union drbd_state ns, os;
1652 struct drbd_peer_device *peer_device;
1656 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1657 struct drbd_device *device = peer_device->device;
1658 os = drbd_read_state(device);
1659 ns = sanitize_state(device, apply_mask_val(os, mask, val), NULL);
1661 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1667 rv = is_valid_transition(os, ns);
1669 if (rv >= SS_SUCCESS && !(flags & CS_HARD)) {
1670 rv = is_valid_state(device, ns);
1671 if (rv < SS_SUCCESS) {
1672 if (is_valid_state(device, os) == rv)
1673 rv = is_valid_soft_transition(os, ns, connection);
1675 rv = is_valid_soft_transition(os, ns, connection);
1678 if (rv < SS_SUCCESS) {
1679 if (flags & CS_VERBOSE)
1680 print_st_err(device, os, ns, rv);
1690 conn_set_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
1691 union drbd_state *pns_min, union drbd_state *pns_max, enum chg_state_flags flags)
1693 union drbd_state ns, os, ns_max = { };
1694 union drbd_state ns_min = {
1701 struct drbd_peer_device *peer_device;
1702 enum drbd_state_rv rv;
1703 int vnr, number_of_volumes = 0;
1705 if (mask.conn == C_MASK) {
1706 /* remember last connect time so request_timer_fn() won't
1707 * kill newly established sessions while we are still trying to thaw
1708 * previously frozen IO */
1709 if (connection->cstate != C_WF_REPORT_PARAMS && val.conn == C_WF_REPORT_PARAMS)
1710 connection->last_reconnect_jif = jiffies;
1712 connection->cstate = val.conn;
1716 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1717 struct drbd_device *device = peer_device->device;
1718 number_of_volumes++;
1719 os = drbd_read_state(device);
1720 ns = apply_mask_val(os, mask, val);
1721 ns = sanitize_state(device, ns, NULL);
1723 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1726 rv = __drbd_set_state(device, ns, flags, NULL);
1727 if (rv < SS_SUCCESS)
1730 ns.i = device->state.i;
1731 ns_max.role = max_role(ns.role, ns_max.role);
1732 ns_max.peer = max_role(ns.peer, ns_max.peer);
1733 ns_max.conn = max_t(enum drbd_conns, ns.conn, ns_max.conn);
1734 ns_max.disk = max_t(enum drbd_disk_state, ns.disk, ns_max.disk);
1735 ns_max.pdsk = max_t(enum drbd_disk_state, ns.pdsk, ns_max.pdsk);
1737 ns_min.role = min_role(ns.role, ns_min.role);
1738 ns_min.peer = min_role(ns.peer, ns_min.peer);
1739 ns_min.conn = min_t(enum drbd_conns, ns.conn, ns_min.conn);
1740 ns_min.disk = min_t(enum drbd_disk_state, ns.disk, ns_min.disk);
1741 ns_min.pdsk = min_t(enum drbd_disk_state, ns.pdsk, ns_min.pdsk);
1745 if (number_of_volumes == 0) {
1746 ns_min = ns_max = (union drbd_state) { {
1747 .role = R_SECONDARY,
1755 ns_min.susp = ns_max.susp = connection->resource->susp;
1756 ns_min.susp_nod = ns_max.susp_nod = connection->resource->susp_nod;
1757 ns_min.susp_fen = ns_max.susp_fen = connection->resource->susp_fen;
1763 static enum drbd_state_rv
1764 _conn_rq_cond(struct drbd_connection *connection, union drbd_state mask, union drbd_state val)
1766 enum drbd_state_rv rv;
1768 if (test_and_clear_bit(CONN_WD_ST_CHG_OKAY, &connection->flags))
1769 return SS_CW_SUCCESS;
1771 if (test_and_clear_bit(CONN_WD_ST_CHG_FAIL, &connection->flags))
1772 return SS_CW_FAILED_BY_PEER;
1774 rv = conn_is_valid_transition(connection, mask, val, 0);
1775 if (rv == SS_SUCCESS && connection->cstate == C_WF_REPORT_PARAMS)
1776 rv = SS_UNKNOWN_ERROR; /* continue waiting */
1782 _conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
1783 enum chg_state_flags flags)
1785 enum drbd_state_rv rv = SS_SUCCESS;
1786 struct after_conn_state_chg_work *acscw;
1787 enum drbd_conns oc = connection->cstate;
1788 union drbd_state ns_max, ns_min, os;
1789 bool have_mutex = false;
1792 rv = is_valid_conn_transition(oc, val.conn);
1793 if (rv < SS_SUCCESS)
1797 rv = conn_is_valid_transition(connection, mask, val, flags);
1798 if (rv < SS_SUCCESS)
1801 if (oc == C_WF_REPORT_PARAMS && val.conn == C_DISCONNECTING &&
1802 !(flags & (CS_LOCAL_ONLY | CS_HARD))) {
1804 /* This will be a cluster-wide state change.
1805 * Need to give up the spinlock, grab the mutex,
1806 * then send the state change request, ... */
1807 spin_unlock_irq(&connection->resource->req_lock);
1808 mutex_lock(&connection->cstate_mutex);
1811 set_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
1812 if (conn_send_state_req(connection, mask, val)) {
1813 /* sending failed. */
1814 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
1815 rv = SS_CW_FAILED_BY_PEER;
1816 /* need to re-aquire the spin lock, though */
1817 goto abort_unlocked;
1820 if (val.conn == C_DISCONNECTING)
1821 set_bit(DISCONNECT_SENT, &connection->flags);
1823 /* ... and re-aquire the spinlock.
1824 * If _conn_rq_cond() returned >= SS_SUCCESS, we must call
1825 * conn_set_state() within the same spinlock. */
1826 spin_lock_irq(&connection->resource->req_lock);
1827 wait_event_lock_irq(connection->ping_wait,
1828 (rv = _conn_rq_cond(connection, mask, val)),
1829 connection->resource->req_lock);
1830 clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
1831 if (rv < SS_SUCCESS)
1835 conn_old_common_state(connection, &os, &flags);
1836 flags |= CS_DC_SUSP;
1837 conn_set_state(connection, mask, val, &ns_min, &ns_max, flags);
1838 conn_pr_state_change(connection, os, ns_max, flags);
1840 acscw = kmalloc(sizeof(*acscw), GFP_ATOMIC);
1842 acscw->oc = os.conn;
1843 acscw->ns_min = ns_min;
1844 acscw->ns_max = ns_max;
1845 acscw->flags = flags;
1846 acscw->w.cb = w_after_conn_state_ch;
1847 kref_get(&connection->kref);
1848 acscw->connection = connection;
1849 drbd_queue_work(&connection->sender_work, &acscw->w);
1851 drbd_err(connection, "Could not kmalloc an acscw\n");
1856 /* mutex_unlock() "... must not be used in interrupt context.",
1857 * so give up the spinlock, then re-aquire it */
1858 spin_unlock_irq(&connection->resource->req_lock);
1860 mutex_unlock(&connection->cstate_mutex);
1861 spin_lock_irq(&connection->resource->req_lock);
1863 if (rv < SS_SUCCESS && flags & CS_VERBOSE) {
1864 drbd_err(connection, "State change failed: %s\n", drbd_set_st_err_str(rv));
1865 drbd_err(connection, " mask = 0x%x val = 0x%x\n", mask.i, val.i);
1866 drbd_err(connection, " old_conn:%s wanted_conn:%s\n", drbd_conn_str(oc), drbd_conn_str(val.conn));
1872 conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
1873 enum chg_state_flags flags)
1875 enum drbd_state_rv rv;
1877 spin_lock_irq(&connection->resource->req_lock);
1878 rv = _conn_request_state(connection, mask, val, flags);
1879 spin_unlock_irq(&connection->resource->req_lock);