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