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