blob: c16349aec23c8c41188b3779e82089307639016a [file] [log] [blame]
Philipp Reisnerb8907332011-01-27 14:07:51 +01001/*
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 Reisnercdfda632011-07-05 15:38:59 +020032/* in drbd_main.c */
Andreas Gruenbacher71fc7ee2011-07-17 23:06:12 +020033extern void tl_abort_disk_io(struct drbd_conf *mdev);
Philipp Reisnercdfda632011-07-05 15:38:59 +020034
Philipp Reisnerb8907332011-01-27 14:07:51 +010035struct 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 Reisnerd942ae42011-05-31 13:07:24 +020043enum 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 Gruenbacher99920dc2011-03-16 15:31:39 +010052static int w_after_state_ch(struct drbd_work *w, int unused);
Philipp Reisnerb8907332011-01-27 14:07:51 +010053static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
54 union drbd_state ns, enum chg_state_flags flags);
Philipp Reisnera75f34a2011-02-09 15:10:33 +010055static enum drbd_state_rv is_valid_state(struct drbd_conf *, union drbd_state);
Philipp Reisnera1096a62012-04-06 12:07:34 +020056static enum drbd_state_rv is_valid_soft_transition(union drbd_state, union drbd_state, struct drbd_tconn *);
Philipp Reisner35095022011-02-09 16:29:33 +010057static enum drbd_state_rv is_valid_transition(union drbd_state os, union drbd_state ns);
Philipp Reisner4308a0a2011-02-10 11:24:38 +010058static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state ns,
Philipp Reisnerd942ae42011-05-31 13:07:24 +020059 enum sanitize_state_warnings *warn);
Philipp Reisnerb8907332011-01-27 14:07:51 +010060
Philipp Reisner2aebfab2011-03-28 16:48:11 +020061static inline bool is_susp(union drbd_state s)
62{
63 return s.susp || s.susp_nod || s.susp_fen;
64}
65
Lars Ellenbergd0456c72011-03-10 23:28:13 +010066bool conn_all_vols_unconf(struct drbd_tconn *tconn)
Philipp Reisner0e29d162011-02-18 14:23:11 +010067{
68 struct drbd_conf *mdev;
Philipp Reisner695d08f2011-04-11 22:53:32 -070069 bool rv = true;
Philipp Reisnere90285e2011-03-22 12:51:21 +010070 int vnr;
Philipp Reisner0e29d162011-02-18 14:23:11 +010071
Philipp Reisner695d08f2011-04-11 22:53:32 -070072 rcu_read_lock();
Philipp Reisnere90285e2011-03-22 12:51:21 +010073 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Lars Ellenbergd0456c72011-03-10 23:28:13 +010074 if (mdev->state.disk != D_DISKLESS ||
75 mdev->state.conn != C_STANDALONE ||
Philipp Reisner695d08f2011-04-11 22:53:32 -070076 mdev->state.role != R_SECONDARY) {
77 rv = false;
78 break;
79 }
Philipp Reisner0e29d162011-02-18 14:23:11 +010080 }
Philipp Reisner695d08f2011-04-11 22:53:32 -070081 rcu_read_unlock();
82
83 return rv;
Philipp Reisner0e29d162011-02-18 14:23:11 +010084}
85
Philipp Reisnercb703452011-03-24 11:03:07 +010086/* Unfortunately the states where not correctly ordered, when
87 they where defined. therefore can not use max_t() here. */
88static enum drbd_role max_role(enum drbd_role role1, enum drbd_role role2)
89{
90 if (role1 == R_PRIMARY || role2 == R_PRIMARY)
91 return R_PRIMARY;
92 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
93 return R_SECONDARY;
94 return R_UNKNOWN;
95}
96static enum drbd_role min_role(enum drbd_role role1, enum drbd_role role2)
97{
98 if (role1 == R_UNKNOWN || role2 == R_UNKNOWN)
99 return R_UNKNOWN;
100 if (role1 == R_SECONDARY || role2 == R_SECONDARY)
101 return R_SECONDARY;
102 return R_PRIMARY;
103}
104
105enum 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 Reisner695d08f2011-04-11 22:53:32 -0700111 rcu_read_lock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100112 idr_for_each_entry(&tconn->volumes, mdev, vnr)
113 role = max_role(role, mdev->state.role);
Philipp Reisner695d08f2011-04-11 22:53:32 -0700114 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100115
116 return role;
117}
118
119enum 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 Reisner695d08f2011-04-11 22:53:32 -0700125 rcu_read_lock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100126 idr_for_each_entry(&tconn->volumes, mdev, vnr)
127 peer = max_role(peer, mdev->state.peer);
Philipp Reisner695d08f2011-04-11 22:53:32 -0700128 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100129
130 return peer;
131}
132
133enum 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 Reisner695d08f2011-04-11 22:53:32 -0700139 rcu_read_lock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100140 idr_for_each_entry(&tconn->volumes, mdev, vnr)
141 ds = max_t(enum drbd_disk_state, ds, mdev->state.disk);
Philipp Reisner695d08f2011-04-11 22:53:32 -0700142 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100143
144 return ds;
145}
146
Philipp Reisner46692652011-03-29 18:15:49 +0200147enum 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 Reisner695d08f2011-04-11 22:53:32 -0700153 rcu_read_lock();
Philipp Reisner46692652011-03-29 18:15:49 +0200154 idr_for_each_entry(&tconn->volumes, mdev, vnr)
155 ds = min_t(enum drbd_disk_state, ds, mdev->state.disk);
Philipp Reisner695d08f2011-04-11 22:53:32 -0700156 rcu_read_unlock();
Philipp Reisner46692652011-03-29 18:15:49 +0200157
158 return ds;
159}
160
Philipp Reisnercb703452011-03-24 11:03:07 +0100161enum 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 Reisner695d08f2011-04-11 22:53:32 -0700167 rcu_read_lock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100168 idr_for_each_entry(&tconn->volumes, mdev, vnr)
169 ds = max_t(enum drbd_disk_state, ds, mdev->state.pdsk);
Philipp Reisner695d08f2011-04-11 22:53:32 -0700170 rcu_read_unlock();
Philipp Reisnercb703452011-03-24 11:03:07 +0100171
172 return ds;
173}
174
Philipp Reisner19f83c72011-03-29 14:21:03 +0200175enum 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 Reisner695d08f2011-04-11 22:53:32 -0700181 rcu_read_lock();
Philipp Reisner19f83c72011-03-29 14:21:03 +0200182 idr_for_each_entry(&tconn->volumes, mdev, vnr)
183 conn = min_t(enum drbd_conns, conn, mdev->state.conn);
Philipp Reisner695d08f2011-04-11 22:53:32 -0700184 rcu_read_unlock();
Philipp Reisner19f83c72011-03-29 14:21:03 +0200185
186 return conn;
187}
188
Philipp Reisnerb8907332011-01-27 14:07:51 +0100189/**
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 */
195static 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) ||
Lars Ellenberge8744f52012-03-26 15:57:00 +0200202 (os.disk != D_FAILED && ns.disk == D_FAILED))) ||
Philipp Reisnerb8907332011-01-27 14:07:51 +0100203 (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
Philipp Reisner369bea62011-07-06 23:04:44 +0200204 (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S) ||
205 (os.conn == C_CONNECTED && ns.conn == C_WF_REPORT_PARAMS);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100206}
207
Philipp Reisner56707f92011-02-16 14:57:50 +0100208static union drbd_state
209apply_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 Reisnerb8907332011-01-27 14:07:51 +0100216enum drbd_state_rv
217drbd_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 Reisner56707f92011-02-16 14:57:50 +0100221 union drbd_state ns;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100222 enum drbd_state_rv rv;
223
224 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisner78bae592011-03-28 15:40:12 +0200225 ns = apply_mask_val(drbd_read_state(mdev), mask, val);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100226 rv = _drbd_set_state(mdev, ns, f, NULL);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100227 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 */
238void 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
244static 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 Reisnerb8907332011-01-27 14:07:51 +0100258 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisner78bae592011-03-28 15:40:12 +0200259 os = drbd_read_state(mdev);
Philipp Reisner56707f92011-02-16 14:57:50 +0100260 ns = sanitize_state(mdev, apply_mask_val(os, mask, val), NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100261 rv = is_valid_transition(os, ns);
262 if (rv == SS_SUCCESS)
263 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100264
265 if (!cl_wide_st_chg(mdev, os, ns))
266 rv = SS_CW_NO_NEED;
Philipp Reisner35095022011-02-09 16:29:33 +0100267 if (rv == SS_UNKNOWN_ERROR) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100268 rv = is_valid_state(mdev, ns);
269 if (rv == SS_SUCCESS) {
Philipp Reisnera1096a62012-04-06 12:07:34 +0200270 rv = is_valid_soft_transition(os, ns, mdev->tconn);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100271 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 */
290static enum drbd_state_rv
291drbd_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 Reisner8410da82011-02-11 20:11:10 +0100302 mutex_lock(mdev->state_mutex);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100303
304 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisner78bae592011-03-28 15:40:12 +0200305 os = drbd_read_state(mdev);
Philipp Reisner56707f92011-02-16 14:57:50 +0100306 ns = sanitize_state(mdev, apply_mask_val(os, mask, val), NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100307 rv = is_valid_transition(os, ns);
Lars Ellenberg3c5e5f62011-03-15 16:04:09 +0100308 if (rv < SS_SUCCESS) {
309 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Philipp Reisner35095022011-02-09 16:29:33 +0100310 goto abort;
Lars Ellenberg3c5e5f62011-03-15 16:04:09 +0100311 }
Philipp Reisnerb8907332011-01-27 14:07:51 +0100312
313 if (cl_wide_st_chg(mdev, os, ns)) {
314 rv = is_valid_state(mdev, ns);
315 if (rv == SS_SUCCESS)
Philipp Reisnera1096a62012-04-06 12:07:34 +0200316 rv = is_valid_soft_transition(os, ns, mdev->tconn);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100317 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 Gruenbacherd24ae212011-03-16 01:19:27 +0100325 if (drbd_send_state_req(mdev, mask, val)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100326 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 Reisnerb8907332011-01-27 14:07:51 +0100336 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 Reisner78bae592011-03-28 15:40:12 +0200341 ns = apply_mask_val(drbd_read_state(mdev), mask, val);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100342 rv = _drbd_set_state(mdev, ns, f, &done);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100343 } 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
354abort:
355 if (f & CS_SERIALIZE)
Philipp Reisner8410da82011-02-11 20:11:10 +0100356 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100357
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 */
371enum 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
383static 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
401void 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 Reisner435693e2011-03-25 15:11:30 +0100411static long print_state_change(char *pb, union drbd_state os, union drbd_state ns,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100412 enum chg_state_flags flags)
413{
Philipp Reisner435693e2011-03-25 15:11:30 +0100414 char *pbp;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100415 pbp = pb;
416 *pbp = 0;
Philipp Reisner706cb242011-03-29 15:20:27 +0200417
Philipp Reisner435693e2011-03-25 15:11:30 +0100418 if (ns.role != os.role && flags & CS_DC_ROLE)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100419 pbp += sprintf(pbp, "role( %s -> %s ) ",
420 drbd_role_str(os.role),
421 drbd_role_str(ns.role));
Philipp Reisner435693e2011-03-25 15:11:30 +0100422 if (ns.peer != os.peer && flags & CS_DC_PEER)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100423 pbp += sprintf(pbp, "peer( %s -> %s ) ",
424 drbd_role_str(os.peer),
425 drbd_role_str(ns.peer));
Philipp Reisner435693e2011-03-25 15:11:30 +0100426 if (ns.conn != os.conn && flags & CS_DC_CONN)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100427 pbp += sprintf(pbp, "conn( %s -> %s ) ",
428 drbd_conn_str(os.conn),
429 drbd_conn_str(ns.conn));
Philipp Reisner435693e2011-03-25 15:11:30 +0100430 if (ns.disk != os.disk && flags & CS_DC_DISK)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100431 pbp += sprintf(pbp, "disk( %s -> %s ) ",
432 drbd_disk_str(os.disk),
433 drbd_disk_str(ns.disk));
Philipp Reisner435693e2011-03-25 15:11:30 +0100434 if (ns.pdsk != os.pdsk && flags & CS_DC_PDSK)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100435 pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
436 drbd_disk_str(os.pdsk),
437 drbd_disk_str(ns.pdsk));
Philipp Reisner706cb242011-03-29 15:20:27 +0200438
439 return pbp - pb;
440}
441
442static 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 Reisnerbbeb6412011-02-10 13:45:46 +0100450 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 Reisner435693e2011-03-25 15:11:30 +0100462
Philipp Reisner706cb242011-03-29 15:20:27 +0200463 if (pbp != pb)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100464 dev_info(DEV, "%s\n", pb);
465}
Philipp Reisnerb8907332011-01-27 14:07:51 +0100466
Philipp Reisner435693e2011-03-25 15:11:30 +0100467static 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 Reisner706cb242011-03-29 15:20:27 +0200471 char *pbp = pb;
Philipp Reisner435693e2011-03-25 15:11:30 +0100472
Philipp Reisner706cb242011-03-29 15:20:27 +0200473 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 Reisner435693e2011-03-25 15:11:30 +0100481 conn_info(tconn, "%s\n", pb);
482}
483
484
Philipp Reisnerb8907332011-01-27 14:07:51 +0100485/**
486 * is_valid_state() - Returns an SS_ error code if ns is not valid
487 * @mdev: DRBD device.
488 * @ns: State to consider.
489 */
490static enum drbd_state_rv
491is_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 Reisner44ed1672011-04-19 17:10:19 +0200497 struct net_conf *nc;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100498
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200499 rcu_read_lock();
Philipp Reisnerb8907332011-01-27 14:07:51 +0100500 fp = FP_DONT_CARE;
501 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200502 fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100503 put_ldev(mdev);
504 }
505
Philipp Reisner44ed1672011-04-19 17:10:19 +0200506 nc = rcu_dereference(mdev->tconn->net_conf);
507 if (nc) {
508 if (!nc->two_primaries && ns.role == R_PRIMARY) {
Philipp Reisner047e95e22011-03-16 14:43:36 +0100509 if (ns.peer == R_PRIMARY)
510 rv = SS_TWO_PRIMARIES;
Philipp Reisnercb703452011-03-24 11:03:07 +0100511 else if (conn_highest_peer(mdev->tconn) == R_PRIMARY)
Philipp Reisner047e95e22011-03-16 14:43:36 +0100512 rv = SS_O_VOL_PEER_PRI;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200513 }
Philipp Reisnerb8907332011-01-27 14:07:51 +0100514 }
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 Reisner44ed1672011-04-19 17:10:19 +0200548 (nc->verify_alg[0] == 0))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100549 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 Reisner44ed1672011-04-19 17:10:19 +0200558 rcu_read_unlock();
559
Philipp Reisnerb8907332011-01-27 14:07:51 +0100560 return rv;
561}
562
563/**
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100564 * is_valid_soft_transition() - Returns an SS_ error code if the state transition is not possible
Philipp Reisner35095022011-02-09 16:29:33 +0100565 * This function limits state transitions that may be declined by DRBD. I.e.
566 * user requests (aka soft transitions).
Philipp Reisnerb8907332011-01-27 14:07:51 +0100567 * @mdev: DRBD device.
568 * @ns: new state.
569 * @os: old state.
570 */
571static enum drbd_state_rv
Philipp Reisnera1096a62012-04-06 12:07:34 +0200572is_valid_soft_transition(union drbd_state os, union drbd_state ns, struct drbd_tconn *tconn)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100573{
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 Reisner2325eb62011-03-15 16:56:18 +0100595 /* if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
596 rv = SS_IN_TRANSIENT_STATE; */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100597
Philipp Reisnera1096a62012-04-06 12:07:34 +0200598 /* While establishing a connection only allow cstate to change.
599 Delay/refuse role changes, detach attach etc... */
600 if (test_bit(STATE_SENT, &tconn->flags) &&
601 !(os.conn == C_WF_REPORT_PARAMS ||
602 (ns.conn == C_WF_REPORT_PARAMS && os.conn == C_WF_CONNECTION)))
603 rv = SS_IN_TRANSIENT_STATE;
604
Philipp Reisnerb8907332011-01-27 14:07:51 +0100605 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
606 rv = SS_NEED_CONNECTION;
607
608 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
609 ns.conn != os.conn && os.conn > C_CONNECTED)
610 rv = SS_RESYNC_RUNNING;
611
612 if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
613 os.conn < C_CONNECTED)
614 rv = SS_NEED_CONNECTION;
615
616 if ((ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)
617 && os.conn < C_WF_REPORT_PARAMS)
618 rv = SS_NEED_CONNECTION; /* No NetworkFailure -> SyncTarget etc... */
619
620 return rv;
621}
622
Philipp Reisnerfda74112011-02-10 10:38:06 +0100623static enum drbd_state_rv
624is_valid_conn_transition(enum drbd_conns oc, enum drbd_conns nc)
625{
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200626 /* no change -> nothing to do, at least for the connection part */
627 if (oc == nc)
628 return SS_NOTHING_TO_DO;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100629
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200630 /* disconnect of an unconfigured connection does not make sense */
631 if (oc == C_STANDALONE && nc == C_DISCONNECTING)
632 return SS_ALREADY_STANDALONE;
633
634 /* from C_STANDALONE, we start with C_UNCONNECTED */
635 if (oc == C_STANDALONE && nc != C_UNCONNECTED)
636 return SS_NEED_CONNECTION;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100637
Philipp Reisner25b0d6c2012-02-14 12:12:35 +0100638 /* When establishing a connection we need to go through WF_REPORT_PARAMS!
639 Necessary to do the right thing upon invalidate-remote on a disconnected resource */
640 if (oc < C_WF_REPORT_PARAMS && nc >= C_CONNECTED)
641 return SS_NEED_CONNECTION;
642
Philipp Reisnerfda74112011-02-10 10:38:06 +0100643 /* After a network error only C_UNCONNECTED or C_DISCONNECTING may follow. */
644 if (oc >= C_TIMEOUT && oc <= C_TEAR_DOWN && nc != C_UNCONNECTED && nc != C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200645 return SS_IN_TRANSIENT_STATE;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100646
647 /* After C_DISCONNECTING only C_STANDALONE may follow */
648 if (oc == C_DISCONNECTING && nc != C_STANDALONE)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200649 return SS_IN_TRANSIENT_STATE;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100650
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +0200651 return SS_SUCCESS;
Philipp Reisnerfda74112011-02-10 10:38:06 +0100652}
653
654
Philipp Reisnerb8907332011-01-27 14:07:51 +0100655/**
Philipp Reisner35095022011-02-09 16:29:33 +0100656 * is_valid_transition() - Returns an SS_ error code if the state transition is not possible
657 * This limits hard state transitions. Hard state transitions are facts there are
658 * imposed on DRBD by the environment. E.g. disk broke or network broke down.
659 * But those hard state transitions are still not allowed to do everything.
660 * @ns: new state.
661 * @os: old state.
662 */
663static enum drbd_state_rv
664is_valid_transition(union drbd_state os, union drbd_state ns)
665{
Philipp Reisnerfda74112011-02-10 10:38:06 +0100666 enum drbd_state_rv rv;
Philipp Reisner35095022011-02-09 16:29:33 +0100667
Philipp Reisnerfda74112011-02-10 10:38:06 +0100668 rv = is_valid_conn_transition(os.conn, ns.conn);
Philipp Reisner35095022011-02-09 16:29:33 +0100669
670 /* we cannot fail (again) if we already detached */
671 if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
672 rv = SS_IS_DISKLESS;
673
674 return rv;
675}
676
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200677static void print_sanitize_warnings(struct drbd_conf *mdev, enum sanitize_state_warnings warn)
678{
679 static const char *msg_table[] = {
680 [NO_WARNING] = "",
681 [ABORTED_ONLINE_VERIFY] = "Online-verify aborted.",
682 [ABORTED_RESYNC] = "Resync aborted.",
683 [CONNECTION_LOST_NEGOTIATING] = "Connection lost while negotiating, no data!",
684 [IMPLICITLY_UPGRADED_DISK] = "Implicitly upgraded disk",
685 [IMPLICITLY_UPGRADED_PDSK] = "Implicitly upgraded pdsk",
686 };
687
688 if (warn != NO_WARNING)
689 dev_warn(DEV, "%s\n", msg_table[warn]);
690}
691
Philipp Reisner35095022011-02-09 16:29:33 +0100692/**
Philipp Reisnerb8907332011-01-27 14:07:51 +0100693 * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
694 * @mdev: DRBD device.
695 * @os: old state.
696 * @ns: new state.
697 * @warn_sync_abort:
698 *
699 * When we loose connection, we have to set the state of the peers disk (pdsk)
700 * to D_UNKNOWN. This rule and many more along those lines are in this function.
701 */
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100702static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state ns,
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200703 enum sanitize_state_warnings *warn)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100704{
705 enum drbd_fencing_p fp;
706 enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
707
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200708 if (warn)
709 *warn = NO_WARNING;
710
Philipp Reisnerb8907332011-01-27 14:07:51 +0100711 fp = FP_DONT_CARE;
712 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200713 rcu_read_lock();
714 fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
715 rcu_read_unlock();
Philipp Reisnerb8907332011-01-27 14:07:51 +0100716 put_ldev(mdev);
717 }
718
Philipp Reisner35095022011-02-09 16:29:33 +0100719 /* Implications from connection to peer and peer_isp */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100720 if (ns.conn < C_CONNECTED) {
721 ns.peer_isp = 0;
722 ns.peer = R_UNKNOWN;
723 if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
724 ns.pdsk = D_UNKNOWN;
725 }
726
727 /* Clear the aftr_isp when becoming unconfigured */
728 if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
729 ns.aftr_isp = 0;
730
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100731 /* An implication of the disk states onto the connection state */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100732 /* Abort resync if a disk fails/detaches */
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100733 if (ns.conn > C_CONNECTED && (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200734 if (warn)
735 *warn = ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T ?
736 ABORTED_ONLINE_VERIFY : ABORTED_RESYNC;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100737 ns.conn = C_CONNECTED;
738 }
739
740 /* Connection breaks down before we finished "Negotiating" */
741 if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
742 get_ldev_if_state(mdev, D_NEGOTIATING)) {
743 if (mdev->ed_uuid == mdev->ldev->md.uuid[UI_CURRENT]) {
744 ns.disk = mdev->new_state_tmp.disk;
745 ns.pdsk = mdev->new_state_tmp.pdsk;
746 } else {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200747 if (warn)
748 *warn = CONNECTION_LOST_NEGOTIATING;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100749 ns.disk = D_DISKLESS;
750 ns.pdsk = D_UNKNOWN;
751 }
752 put_ldev(mdev);
753 }
754
755 /* D_CONSISTENT and D_OUTDATED vanish when we get connected */
756 if (ns.conn >= C_CONNECTED && ns.conn < C_AHEAD) {
757 if (ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED)
758 ns.disk = D_UP_TO_DATE;
759 if (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)
760 ns.pdsk = D_UP_TO_DATE;
761 }
762
763 /* Implications of the connection stat on the disk states */
764 disk_min = D_DISKLESS;
765 disk_max = D_UP_TO_DATE;
766 pdsk_min = D_INCONSISTENT;
767 pdsk_max = D_UNKNOWN;
768 switch ((enum drbd_conns)ns.conn) {
769 case C_WF_BITMAP_T:
770 case C_PAUSED_SYNC_T:
771 case C_STARTING_SYNC_T:
772 case C_WF_SYNC_UUID:
773 case C_BEHIND:
774 disk_min = D_INCONSISTENT;
775 disk_max = D_OUTDATED;
776 pdsk_min = D_UP_TO_DATE;
777 pdsk_max = D_UP_TO_DATE;
778 break;
779 case C_VERIFY_S:
780 case C_VERIFY_T:
781 disk_min = D_UP_TO_DATE;
782 disk_max = D_UP_TO_DATE;
783 pdsk_min = D_UP_TO_DATE;
784 pdsk_max = D_UP_TO_DATE;
785 break;
786 case C_CONNECTED:
787 disk_min = D_DISKLESS;
788 disk_max = D_UP_TO_DATE;
789 pdsk_min = D_DISKLESS;
790 pdsk_max = D_UP_TO_DATE;
791 break;
792 case C_WF_BITMAP_S:
793 case C_PAUSED_SYNC_S:
794 case C_STARTING_SYNC_S:
795 case C_AHEAD:
796 disk_min = D_UP_TO_DATE;
797 disk_max = D_UP_TO_DATE;
798 pdsk_min = D_INCONSISTENT;
799 pdsk_max = D_CONSISTENT; /* D_OUTDATED would be nice. But explicit outdate necessary*/
800 break;
801 case C_SYNC_TARGET:
802 disk_min = D_INCONSISTENT;
803 disk_max = D_INCONSISTENT;
804 pdsk_min = D_UP_TO_DATE;
805 pdsk_max = D_UP_TO_DATE;
806 break;
807 case C_SYNC_SOURCE:
808 disk_min = D_UP_TO_DATE;
809 disk_max = D_UP_TO_DATE;
810 pdsk_min = D_INCONSISTENT;
811 pdsk_max = D_INCONSISTENT;
812 break;
813 case C_STANDALONE:
814 case C_DISCONNECTING:
815 case C_UNCONNECTED:
816 case C_TIMEOUT:
817 case C_BROKEN_PIPE:
818 case C_NETWORK_FAILURE:
819 case C_PROTOCOL_ERROR:
820 case C_TEAR_DOWN:
821 case C_WF_CONNECTION:
822 case C_WF_REPORT_PARAMS:
823 case C_MASK:
824 break;
825 }
826 if (ns.disk > disk_max)
827 ns.disk = disk_max;
828
829 if (ns.disk < disk_min) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200830 if (warn)
831 *warn = IMPLICITLY_UPGRADED_DISK;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100832 ns.disk = disk_min;
833 }
834 if (ns.pdsk > pdsk_max)
835 ns.pdsk = pdsk_max;
836
837 if (ns.pdsk < pdsk_min) {
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200838 if (warn)
839 *warn = IMPLICITLY_UPGRADED_PDSK;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100840 ns.pdsk = pdsk_min;
841 }
842
843 if (fp == FP_STONITH &&
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100844 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100845 ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
846
Lars Ellenbergf3990022011-03-23 14:31:09 +0100847 if (mdev->tconn->res_opts.on_no_data == OND_SUSPEND_IO &&
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100848 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100849 ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
850
851 if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
852 if (ns.conn == C_SYNC_SOURCE)
853 ns.conn = C_PAUSED_SYNC_S;
854 if (ns.conn == C_SYNC_TARGET)
855 ns.conn = C_PAUSED_SYNC_T;
856 } else {
857 if (ns.conn == C_PAUSED_SYNC_S)
858 ns.conn = C_SYNC_SOURCE;
859 if (ns.conn == C_PAUSED_SYNC_T)
860 ns.conn = C_SYNC_TARGET;
861 }
862
863 return ns;
864}
865
866void drbd_resume_al(struct drbd_conf *mdev)
867{
868 if (test_and_clear_bit(AL_SUSPENDED, &mdev->flags))
869 dev_info(DEV, "Resumed AL updates\n");
870}
871
872/* helper for __drbd_set_state */
873static void set_ov_position(struct drbd_conf *mdev, enum drbd_conns cs)
874{
875 if (mdev->tconn->agreed_pro_version < 90)
876 mdev->ov_start_sector = 0;
877 mdev->rs_total = drbd_bm_bits(mdev);
878 mdev->ov_position = 0;
879 if (cs == C_VERIFY_T) {
880 /* starting online verify from an arbitrary position
881 * does not fit well into the existing protocol.
882 * on C_VERIFY_T, we initialize ov_left and friends
883 * implicitly in receive_DataRequest once the
884 * first P_OV_REQUEST is received */
885 mdev->ov_start_sector = ~(sector_t)0;
886 } else {
887 unsigned long bit = BM_SECT_TO_BIT(mdev->ov_start_sector);
888 if (bit >= mdev->rs_total) {
889 mdev->ov_start_sector =
890 BM_BIT_TO_SECT(mdev->rs_total - 1);
891 mdev->rs_total = 1;
892 } else
893 mdev->rs_total -= bit;
894 mdev->ov_position = mdev->ov_start_sector;
895 }
896 mdev->ov_left = mdev->rs_total;
897}
898
899/**
900 * __drbd_set_state() - Set a new DRBD state
901 * @mdev: DRBD device.
902 * @ns: new state.
903 * @flags: Flags
904 * @done: Optional completion, that will get completed after the after_state_ch() finished
905 *
906 * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
907 */
908enum drbd_state_rv
909__drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
910 enum chg_state_flags flags, struct completion *done)
911{
912 union drbd_state os;
913 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200914 enum sanitize_state_warnings ssw;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100915 struct after_state_chg_work *ascw;
916
Philipp Reisner78bae592011-03-28 15:40:12 +0200917 os = drbd_read_state(mdev);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100918
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200919 ns = sanitize_state(mdev, ns, &ssw);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100920 if (ns.i == os.i)
921 return SS_NOTHING_TO_DO;
922
Philipp Reisner35095022011-02-09 16:29:33 +0100923 rv = is_valid_transition(os, ns);
924 if (rv < SS_SUCCESS)
925 return rv;
926
Philipp Reisnerb8907332011-01-27 14:07:51 +0100927 if (!(flags & CS_HARD)) {
928 /* pre-state-change checks ; only look at ns */
929 /* See drbd_state_sw_errors in drbd_strings.c */
930
931 rv = is_valid_state(mdev, ns);
932 if (rv < SS_SUCCESS) {
933 /* If the old state was illegal as well, then let
934 this happen...*/
935
936 if (is_valid_state(mdev, os) == rv)
Philipp Reisnera1096a62012-04-06 12:07:34 +0200937 rv = is_valid_soft_transition(os, ns, mdev->tconn);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100938 } else
Philipp Reisnera1096a62012-04-06 12:07:34 +0200939 rv = is_valid_soft_transition(os, ns, mdev->tconn);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100940 }
941
942 if (rv < SS_SUCCESS) {
943 if (flags & CS_VERBOSE)
944 print_st_err(mdev, os, ns, rv);
945 return rv;
946 }
947
Philipp Reisnerd942ae42011-05-31 13:07:24 +0200948 print_sanitize_warnings(mdev, ssw);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100949
Philipp Reisner435693e2011-03-25 15:11:30 +0100950 drbd_pr_state_change(mdev, os, ns, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100951
Philipp Reisner706cb242011-03-29 15:20:27 +0200952 /* Display changes to the susp* flags that where caused by the call to
953 sanitize_state(). Only display it here if we where not called from
954 _conn_request_state() */
955 if (!(flags & CS_DC_SUSP))
956 conn_pr_state_change(mdev->tconn, os, ns, (flags & ~CS_DC_MASK) | CS_DC_SUSP);
957
Philipp Reisnerb8907332011-01-27 14:07:51 +0100958 /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
959 * on the ldev here, to be sure the transition -> D_DISKLESS resp.
960 * drbd_ldev_destroy() won't happen before our corresponding
961 * after_state_ch works run, where we put_ldev again. */
962 if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
963 (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
964 atomic_inc(&mdev->local_cnt);
965
Philipp Reisnerda9fbc22011-03-29 10:52:01 +0200966 mdev->state.i = ns.i;
Philipp Reisner8e0af252011-03-28 16:18:39 +0200967 mdev->tconn->susp = ns.susp;
968 mdev->tconn->susp_nod = ns.susp_nod;
969 mdev->tconn->susp_fen = ns.susp_fen;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100970
971 if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
972 drbd_print_uuids(mdev, "attached to UUIDs");
973
974 wake_up(&mdev->misc_wait);
975 wake_up(&mdev->state_wait);
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100976 wake_up(&mdev->tconn->ping_wait);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100977
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200978 /* Aborted verify run, or we reached the stop sector.
979 * Log the last position, unless end-of-device. */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100980 if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200981 ns.conn <= C_CONNECTED) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100982 mdev->ov_start_sector =
983 BM_BIT_TO_SECT(drbd_bm_bits(mdev) - mdev->ov_left);
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200984 if (mdev->ov_left)
985 dev_info(DEV, "Online Verify reached sector %llu\n",
986 (unsigned long long)mdev->ov_start_sector);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100987 }
988
989 if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
990 (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
991 dev_info(DEV, "Syncer continues.\n");
992 mdev->rs_paused += (long)jiffies
993 -(long)mdev->rs_mark_time[mdev->rs_last_mark];
994 if (ns.conn == C_SYNC_TARGET)
995 mod_timer(&mdev->resync_timer, jiffies);
996 }
997
998 if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
999 (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
1000 dev_info(DEV, "Resync suspended\n");
1001 mdev->rs_mark_time[mdev->rs_last_mark] = jiffies;
1002 }
1003
1004 if (os.conn == C_CONNECTED &&
1005 (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
1006 unsigned long now = jiffies;
1007 int i;
1008
1009 set_ov_position(mdev, ns.conn);
1010 mdev->rs_start = now;
1011 mdev->rs_last_events = 0;
1012 mdev->rs_last_sect_ev = 0;
1013 mdev->ov_last_oos_size = 0;
1014 mdev->ov_last_oos_start = 0;
1015
1016 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1017 mdev->rs_mark_left[i] = mdev->ov_left;
1018 mdev->rs_mark_time[i] = now;
1019 }
1020
1021 drbd_rs_controller_reset(mdev);
1022
1023 if (ns.conn == C_VERIFY_S) {
1024 dev_info(DEV, "Starting Online Verify from sector %llu\n",
1025 (unsigned long long)mdev->ov_position);
1026 mod_timer(&mdev->resync_timer, jiffies);
1027 }
1028 }
1029
1030 if (get_ldev(mdev)) {
1031 u32 mdf = mdev->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
1032 MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
1033 MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
1034
Lars Ellenbergd5d7ebd2011-07-05 20:59:26 +02001035 mdf &= ~MDF_AL_CLEAN;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001036 if (test_bit(CRASHED_PRIMARY, &mdev->flags))
1037 mdf |= MDF_CRASHED_PRIMARY;
1038 if (mdev->state.role == R_PRIMARY ||
1039 (mdev->state.pdsk < D_INCONSISTENT && mdev->state.peer == R_PRIMARY))
1040 mdf |= MDF_PRIMARY_IND;
1041 if (mdev->state.conn > C_WF_REPORT_PARAMS)
1042 mdf |= MDF_CONNECTED_IND;
1043 if (mdev->state.disk > D_INCONSISTENT)
1044 mdf |= MDF_CONSISTENT;
1045 if (mdev->state.disk > D_OUTDATED)
1046 mdf |= MDF_WAS_UP_TO_DATE;
1047 if (mdev->state.pdsk <= D_OUTDATED && mdev->state.pdsk >= D_INCONSISTENT)
1048 mdf |= MDF_PEER_OUT_DATED;
1049 if (mdf != mdev->ldev->md.flags) {
1050 mdev->ldev->md.flags = mdf;
1051 drbd_md_mark_dirty(mdev);
1052 }
1053 if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
1054 drbd_set_ed_uuid(mdev, mdev->ldev->md.uuid[UI_CURRENT]);
1055 put_ldev(mdev);
1056 }
1057
1058 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
1059 if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
1060 os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
1061 set_bit(CONSIDER_RESYNC, &mdev->flags);
1062
1063 /* Receiver should clean up itself */
1064 if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
1065 drbd_thread_stop_nowait(&mdev->tconn->receiver);
1066
1067 /* Now the receiver finished cleaning up itself, it should die */
1068 if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
1069 drbd_thread_stop_nowait(&mdev->tconn->receiver);
1070
1071 /* Upon network failure, we need to restart the receiver. */
Philipp Reisner823bd832012-11-08 15:04:36 +01001072 if (os.conn > C_WF_CONNECTION &&
Philipp Reisnerb8907332011-01-27 14:07:51 +01001073 ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
1074 drbd_thread_restart_nowait(&mdev->tconn->receiver);
1075
1076 /* Resume AL writing if we get a connection */
1077 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED)
1078 drbd_resume_al(mdev);
1079
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001080 /* remember last attach time so request_timer_fn() won't
1081 * kill newly established sessions while we are still trying to thaw
1082 * previously frozen IO */
1083 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
1084 ns.disk > D_NEGOTIATING)
1085 mdev->last_reattach_jif = jiffies;
1086
Philipp Reisnerb8907332011-01-27 14:07:51 +01001087 ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
1088 if (ascw) {
1089 ascw->os = os;
1090 ascw->ns = ns;
1091 ascw->flags = flags;
1092 ascw->w.cb = w_after_state_ch;
Philipp Reisnera21e9292011-02-08 15:08:49 +01001093 ascw->w.mdev = mdev;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001094 ascw->done = done;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01001095 drbd_queue_work(&mdev->tconn->sender_work, &ascw->w);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001096 } else {
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001097 dev_err(DEV, "Could not kmalloc an ascw\n");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001098 }
1099
1100 return rv;
1101}
1102
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001103static int w_after_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001104{
1105 struct after_state_chg_work *ascw =
1106 container_of(w, struct after_state_chg_work, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001107 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001108
1109 after_state_ch(mdev, ascw->os, ascw->ns, ascw->flags);
1110 if (ascw->flags & CS_WAIT_COMPLETE) {
1111 D_ASSERT(ascw->done != NULL);
1112 complete(ascw->done);
1113 }
1114 kfree(ascw);
1115
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001116 return 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001117}
1118
1119static void abw_start_sync(struct drbd_conf *mdev, int rv)
1120{
1121 if (rv) {
1122 dev_err(DEV, "Writing the bitmap failed not starting resync.\n");
1123 _drbd_request_state(mdev, NS(conn, C_CONNECTED), CS_VERBOSE);
1124 return;
1125 }
1126
1127 switch (mdev->state.conn) {
1128 case C_STARTING_SYNC_T:
1129 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
1130 break;
1131 case C_STARTING_SYNC_S:
1132 drbd_start_resync(mdev, C_SYNC_SOURCE);
1133 break;
1134 }
1135}
1136
1137int drbd_bitmap_io_from_worker(struct drbd_conf *mdev,
1138 int (*io_fn)(struct drbd_conf *),
1139 char *why, enum bm_flag flags)
1140{
1141 int rv;
1142
1143 D_ASSERT(current == mdev->tconn->worker.task);
1144
1145 /* open coded non-blocking drbd_suspend_io(mdev); */
1146 set_bit(SUSPEND_IO, &mdev->flags);
1147
1148 drbd_bm_lock(mdev, why, flags);
1149 rv = io_fn(mdev);
1150 drbd_bm_unlock(mdev);
1151
1152 drbd_resume_io(mdev);
1153
1154 return rv;
1155}
1156
1157/**
1158 * after_state_ch() - Perform after state change actions that may sleep
1159 * @mdev: DRBD device.
1160 * @os: old state.
1161 * @ns: new state.
1162 * @flags: Flags
1163 */
1164static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
1165 union drbd_state ns, enum chg_state_flags flags)
1166{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001167 struct sib_info sib;
1168
1169 sib.sib_reason = SIB_STATE_CHANGE;
1170 sib.os = os;
1171 sib.ns = ns;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001172
1173 if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
1174 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1175 if (mdev->p_uuid)
1176 mdev->p_uuid[UI_FLAGS] &= ~((u64)2);
1177 }
1178
Philipp Reisnerb8907332011-01-27 14:07:51 +01001179 /* Inform userspace about the change... */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001180 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001181
1182 if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
1183 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
1184 drbd_khelper(mdev, "pri-on-incon-degr");
1185
1186 /* Here we have the actions that are performed after a
1187 state change. This function might sleep */
1188
Philipp Reisnerb8907332011-01-27 14:07:51 +01001189 if (ns.susp_nod) {
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001190 enum drbd_req_event what = NOTHING;
1191
1192 if (os.conn < C_CONNECTED && conn_lowest_conn(mdev->tconn) >= C_CONNECTED)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001193 what = RESEND;
1194
Philipp Reisner3fb47462011-07-15 18:44:26 +02001195 if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
1196 conn_lowest_disk(mdev->tconn) > D_NEGOTIATING)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001197 what = RESTART_FROZEN_DISK_IO;
1198
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001199 if (what != NOTHING) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001200 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001201 _tl_restart(mdev->tconn, what);
1202 _drbd_set_state(_NS(mdev, susp_nod, 0), CS_VERBOSE, NULL);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001203 spin_unlock_irq(&mdev->tconn->req_lock);
1204 }
Philipp Reisnerb8907332011-01-27 14:07:51 +01001205 }
1206
1207 /* Became sync source. With protocol >= 96, we still need to send out
1208 * the sync uuid now. Need to do that before any drbd_send_state, or
1209 * the other side may go "paused sync" before receiving the sync uuids,
1210 * which is unexpected. */
1211 if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
1212 (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
1213 mdev->tconn->agreed_pro_version >= 96 && get_ldev(mdev)) {
1214 drbd_gen_and_send_sync_uuid(mdev);
1215 put_ldev(mdev);
1216 }
1217
1218 /* Do not change the order of the if above and the two below... */
Philipp Reisner369bea62011-07-06 23:04:44 +02001219 if (os.pdsk == D_DISKLESS &&
1220 ns.pdsk > D_DISKLESS && ns.pdsk != D_UNKNOWN) { /* attach on the peer */
Lars Ellenberga3248962012-07-30 09:10:41 +02001221 /* we probably will start a resync soon.
1222 * make sure those things are properly reset. */
1223 mdev->rs_total = 0;
1224 mdev->rs_failed = 0;
1225 atomic_set(&mdev->rs_pending_cnt, 0);
1226 drbd_rs_cancel_all(mdev);
1227
Philipp Reisnerb8907332011-01-27 14:07:51 +01001228 drbd_send_uuids(mdev);
Philipp Reisner43de7c82011-11-10 13:16:13 +01001229 drbd_send_state(mdev, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001230 }
1231 /* No point in queuing send_bitmap if we don't have a connection
1232 * anymore, so check also the _current_ state, not only the new state
1233 * at the time this work was queued. */
1234 if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
1235 mdev->state.conn == C_WF_BITMAP_S)
1236 drbd_queue_bitmap_io(mdev, &drbd_send_bitmap, NULL,
1237 "send_bitmap (WFBitMapS)",
1238 BM_LOCKED_TEST_ALLOWED);
1239
1240 /* Lost contact to peer's copy of the data */
1241 if ((os.pdsk >= D_INCONSISTENT &&
1242 os.pdsk != D_UNKNOWN &&
1243 os.pdsk != D_OUTDATED)
1244 && (ns.pdsk < D_INCONSISTENT ||
1245 ns.pdsk == D_UNKNOWN ||
1246 ns.pdsk == D_OUTDATED)) {
1247 if (get_ldev(mdev)) {
1248 if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
1249 mdev->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
Philipp Reisner2aebfab2011-03-28 16:48:11 +02001250 if (drbd_suspended(mdev)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001251 set_bit(NEW_CUR_UUID, &mdev->flags);
1252 } else {
1253 drbd_uuid_new_current(mdev);
1254 drbd_send_uuids(mdev);
1255 }
1256 }
1257 put_ldev(mdev);
1258 }
1259 }
1260
1261 if (ns.pdsk < D_INCONSISTENT && get_ldev(mdev)) {
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001262 if (os.peer == R_SECONDARY && ns.peer == R_PRIMARY &&
1263 mdev->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1264 drbd_uuid_new_current(mdev);
1265 drbd_send_uuids(mdev);
1266 }
Philipp Reisnerb8907332011-01-27 14:07:51 +01001267 /* D_DISKLESS Peer becomes secondary */
1268 if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
1269 /* We may still be Primary ourselves.
1270 * No harm done if the bitmap still changes,
1271 * redirtied pages will follow later. */
1272 drbd_bitmap_io_from_worker(mdev, &drbd_bm_write,
1273 "demote diskless peer", BM_LOCKED_SET_ALLOWED);
1274 put_ldev(mdev);
1275 }
1276
1277 /* Write out all changed bits on demote.
1278 * Though, no need to da that just yet
1279 * if there is a resync going on still */
1280 if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
1281 mdev->state.conn <= C_CONNECTED && get_ldev(mdev)) {
1282 /* No changes to the bitmap expected this time, so assert that,
1283 * even though no harm was done if it did change. */
1284 drbd_bitmap_io_from_worker(mdev, &drbd_bm_write,
1285 "demote", BM_LOCKED_TEST_ALLOWED);
1286 put_ldev(mdev);
1287 }
1288
1289 /* Last part of the attaching process ... */
1290 if (ns.conn >= C_CONNECTED &&
1291 os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
1292 drbd_send_sizes(mdev, 0, 0); /* to start sync... */
1293 drbd_send_uuids(mdev);
Philipp Reisner43de7c82011-11-10 13:16:13 +01001294 drbd_send_state(mdev, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001295 }
1296
1297 /* We want to pause/continue resync, tell peer. */
1298 if (ns.conn >= C_CONNECTED &&
1299 ((os.aftr_isp != ns.aftr_isp) ||
1300 (os.user_isp != ns.user_isp)))
Philipp Reisner43de7c82011-11-10 13:16:13 +01001301 drbd_send_state(mdev, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001302
1303 /* In case one of the isp bits got set, suspend other devices. */
1304 if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
1305 (ns.aftr_isp || ns.peer_isp || ns.user_isp))
1306 suspend_other_sg(mdev);
1307
1308 /* Make sure the peer gets informed about eventual state
1309 changes (ISP bits) while we were in WFReportParams. */
1310 if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
Philipp Reisner43de7c82011-11-10 13:16:13 +01001311 drbd_send_state(mdev, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001312
1313 if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
Philipp Reisner43de7c82011-11-10 13:16:13 +01001314 drbd_send_state(mdev, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001315
1316 /* We are in the progress to start a full sync... */
1317 if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
1318 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
1319 /* no other bitmap changes expected during this phase */
1320 drbd_queue_bitmap_io(mdev,
1321 &drbd_bmio_set_n_write, &abw_start_sync,
1322 "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
1323
1324 /* We are invalidating our self... */
1325 if (os.conn < C_CONNECTED && ns.conn < C_CONNECTED &&
1326 os.disk > D_INCONSISTENT && ns.disk == D_INCONSISTENT)
1327 /* other bitmap operation expected during this phase */
1328 drbd_queue_bitmap_io(mdev, &drbd_bmio_set_n_write, NULL,
1329 "set_n_write from invalidate", BM_LOCKED_MASK);
1330
1331 /* first half of local IO error, failure to attach,
1332 * or administrative detach */
1333 if (os.disk != D_FAILED && ns.disk == D_FAILED) {
Philipp Reisner32db80f2012-02-22 11:51:57 +01001334 enum drbd_io_error_p eh = EP_PASS_ON;
1335 int was_io_error = 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001336 /* corresponding get_ldev was in __drbd_set_state, to serialize
Philipp Reisner32db80f2012-02-22 11:51:57 +01001337 * our cleanup here with the transition to D_DISKLESS.
1338 * But is is still not save to dreference ldev here, since
1339 * we might come from an failed Attach before ldev was set. */
1340 if (mdev->ldev) {
1341 rcu_read_lock();
1342 eh = rcu_dereference(mdev->ldev->disk_conf)->on_io_error;
1343 rcu_read_unlock();
Philipp Reisnerb8907332011-01-27 14:07:51 +01001344
Philipp Reisner32db80f2012-02-22 11:51:57 +01001345 was_io_error = test_and_clear_bit(WAS_IO_ERROR, &mdev->flags);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001346
Lars Ellenberg6f1a6562012-07-30 09:11:01 +02001347 if (was_io_error && eh == EP_CALL_HELPER)
1348 drbd_khelper(mdev, "local-io-error");
1349
Lars Ellenberg0c849662012-07-30 09:07:28 +02001350 /* Immediately allow completion of all application IO,
1351 * that waits for completion from the local disk,
1352 * if this was a force-detach due to disk_timeout
1353 * or administrator request (drbdsetup detach --force).
1354 * Do NOT abort otherwise.
1355 * Aborting local requests may cause serious problems,
1356 * if requests are completed to upper layers already,
1357 * and then later the already submitted local bio completes.
1358 * This can cause DMA into former bio pages that meanwhile
1359 * have been re-used for other things.
1360 * So aborting local requests may cause crashes,
1361 * or even worse, silent data corruption.
1362 */
1363 if (test_and_clear_bit(FORCE_DETACH, &mdev->flags))
1364 tl_abort_disk_io(mdev);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001365
Philipp Reisner32db80f2012-02-22 11:51:57 +01001366 /* current state still has to be D_FAILED,
1367 * there is only one way out: to D_DISKLESS,
1368 * and that may only happen after our put_ldev below. */
1369 if (mdev->state.disk != D_FAILED)
1370 dev_err(DEV,
1371 "ASSERT FAILED: disk is %s during detach\n",
1372 drbd_disk_str(mdev->state.disk));
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001373
Philipp Reisner32db80f2012-02-22 11:51:57 +01001374 if (ns.conn >= C_CONNECTED)
1375 drbd_send_state(mdev, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001376
Philipp Reisner32db80f2012-02-22 11:51:57 +01001377 drbd_rs_cancel_all(mdev);
1378
1379 /* In case we want to get something to stable storage still,
1380 * this may be the last chance.
1381 * Following put_ldev may transition to D_DISKLESS. */
1382 drbd_md_sync(mdev);
1383 }
Philipp Reisnerb8907332011-01-27 14:07:51 +01001384 put_ldev(mdev);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001385 }
1386
1387 /* second half of local IO error, failure to attach,
1388 * or administrative detach,
1389 * after local_cnt references have reached zero again */
1390 if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
1391 /* We must still be diskless,
1392 * re-attach has to be serialized with this! */
1393 if (mdev->state.disk != D_DISKLESS)
1394 dev_err(DEV,
1395 "ASSERT FAILED: disk is %s while going diskless\n",
1396 drbd_disk_str(mdev->state.disk));
1397
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001398 if (ns.conn >= C_CONNECTED)
1399 drbd_send_state(mdev, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001400 /* corresponding get_ldev in __drbd_set_state
1401 * this may finally trigger drbd_ldev_destroy. */
1402 put_ldev(mdev);
1403 }
1404
1405 /* Notify peer that I had a local IO error, and did not detached.. */
Philipp Reisner6ab9b1b2011-12-13 18:32:18 +01001406 if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT && ns.conn >= C_CONNECTED)
Philipp Reisner43de7c82011-11-10 13:16:13 +01001407 drbd_send_state(mdev, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001408
1409 /* Disks got bigger while they were detached */
1410 if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
1411 test_and_clear_bit(RESYNC_AFTER_NEG, &mdev->flags)) {
1412 if (ns.conn == C_CONNECTED)
1413 resync_after_online_grow(mdev);
1414 }
1415
1416 /* A resync finished or aborted, wake paused devices... */
1417 if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
1418 (os.peer_isp && !ns.peer_isp) ||
1419 (os.user_isp && !ns.user_isp))
1420 resume_next_sg(mdev);
1421
1422 /* sync target done with resync. Explicitly notify peer, even though
1423 * it should (at least for non-empty resyncs) already know itself. */
1424 if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
Philipp Reisner43de7c82011-11-10 13:16:13 +01001425 drbd_send_state(mdev, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001426
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001427 /* Verify finished, or reached stop sector. Peer did not know about
1428 * the stop sector, and we may even have changed the stop sector during
1429 * verify to interrupt/stop early. Send the new state. */
1430 if (os.conn == C_VERIFY_S && ns.conn == C_CONNECTED
1431 && verify_can_do_stop_sector(mdev))
1432 drbd_send_state(mdev, ns);
1433
Philipp Reisnera1096a62012-04-06 12:07:34 +02001434 /* Wake up role changes, that were delayed because of connection establishing */
1435 if (os.conn == C_WF_REPORT_PARAMS && ns.conn != C_WF_REPORT_PARAMS) {
1436 if (test_and_clear_bit(STATE_SENT, &mdev->tconn->flags))
1437 wake_up(&mdev->state_wait);
1438 }
1439
Philipp Reisnerb8907332011-01-27 14:07:51 +01001440 /* This triggers bitmap writeout of potentially still unwritten pages
1441 * if the resync finished cleanly, or aborted because of peer disk
1442 * failure, or because of connection loss.
1443 * For resync aborted because of local disk failure, we cannot do
1444 * any bitmap writeout anymore.
1445 * No harm done if some bits change during this phase.
1446 */
1447 if (os.conn > C_CONNECTED && ns.conn <= C_CONNECTED && get_ldev(mdev)) {
Lars Ellenberga220d292012-05-07 12:07:18 +02001448 drbd_queue_bitmap_io(mdev, &drbd_bm_write_copy_pages, NULL,
1449 "write from resync_finished", BM_LOCKED_CHANGE_ALLOWED);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001450 put_ldev(mdev);
1451 }
1452
1453 if (ns.disk == D_DISKLESS &&
1454 ns.conn == C_STANDALONE &&
1455 ns.role == R_SECONDARY) {
1456 if (os.aftr_isp != ns.aftr_isp)
1457 resume_next_sg(mdev);
1458 }
1459
Philipp Reisnerb8907332011-01-27 14:07:51 +01001460 drbd_md_sync(mdev);
1461}
1462
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001463struct after_conn_state_chg_work {
1464 struct drbd_work w;
1465 enum drbd_conns oc;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001466 union drbd_state ns_min;
Philipp Reisner5f082f92011-03-29 13:20:58 +02001467 union drbd_state ns_max; /* new, max state, over all mdevs */
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001468 enum chg_state_flags flags;
1469};
Philipp Reisnerb8907332011-01-27 14:07:51 +01001470
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001471static int w_after_conn_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001472{
1473 struct after_conn_state_chg_work *acscw =
1474 container_of(w, struct after_conn_state_chg_work, w);
1475 struct drbd_tconn *tconn = w->tconn;
1476 enum drbd_conns oc = acscw->oc;
Philipp Reisner5f082f92011-03-29 13:20:58 +02001477 union drbd_state ns_max = acscw->ns_max;
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001478 union drbd_state ns_min = acscw->ns_min;
1479 struct drbd_conf *mdev;
1480 int vnr;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001481
1482 kfree(acscw);
1483
1484 /* Upon network configuration, we need to start the receiver */
Philipp Reisner5f082f92011-03-29 13:20:58 +02001485 if (oc == C_STANDALONE && ns_max.conn == C_UNCONNECTED)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001486 drbd_thread_start(&tconn->receiver);
1487
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02001488 if (oc == C_DISCONNECTING && ns_max.conn == C_STANDALONE) {
1489 struct net_conf *old_conf;
1490
Philipp Reisnera0095502011-05-03 13:14:15 +02001491 mutex_lock(&tconn->conf_update);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02001492 old_conf = tconn->net_conf;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02001493 tconn->my_addr_len = 0;
1494 tconn->peer_addr_len = 0;
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02001495 rcu_assign_pointer(tconn->net_conf, NULL);
1496 conn_free_crypto(tconn);
Philipp Reisnera0095502011-05-03 13:14:15 +02001497 mutex_unlock(&tconn->conf_update);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02001498
1499 synchronize_rcu();
1500 kfree(old_conf);
1501 }
1502
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001503 if (ns_max.susp_fen) {
1504 /* case1: The outdate peer handler is successful: */
1505 if (ns_max.pdsk <= D_OUTDATED) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001506 rcu_read_lock();
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001507 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1508 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
1509 drbd_uuid_new_current(mdev);
1510 clear_bit(NEW_CUR_UUID, &mdev->flags);
1511 }
1512 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001513 rcu_read_unlock();
Philipp Reisner8a0bab22012-08-07 13:28:00 +02001514 spin_lock_irq(&tconn->req_lock);
1515 _tl_restart(tconn, CONNECTION_LOST_WHILE_PENDING);
1516 _conn_request_state(tconn,
1517 (union drbd_state) { { .susp_fen = 1 } },
1518 (union drbd_state) { { .susp_fen = 0 } },
1519 CS_VERBOSE);
1520 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001521 }
1522 /* case2: The connection was established again: */
1523 if (ns_min.conn >= C_CONNECTED) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001524 rcu_read_lock();
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001525 idr_for_each_entry(&tconn->volumes, mdev, vnr)
1526 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner695d08f2011-04-11 22:53:32 -07001527 rcu_read_unlock();
Philipp Reisnera6d00c82011-03-29 18:16:11 +02001528 spin_lock_irq(&tconn->req_lock);
1529 _tl_restart(tconn, RESEND);
1530 _conn_request_state(tconn,
1531 (union drbd_state) { { .susp_fen = 1 } },
1532 (union drbd_state) { { .susp_fen = 0 } },
1533 CS_VERBOSE);
1534 spin_unlock_irq(&tconn->req_lock);
1535 }
1536 }
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02001537 kref_put(&tconn->kref, &conn_destroy);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001538 return 0;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001539}
1540
Philipp Reisner435693e2011-03-25 15:11:30 +01001541void conn_old_common_state(struct drbd_tconn *tconn, union drbd_state *pcs, enum chg_state_flags *pf)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001542{
Philipp Reisner435693e2011-03-25 15:11:30 +01001543 enum chg_state_flags flags = ~0;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001544 struct drbd_conf *mdev;
Philipp Reisner435693e2011-03-25 15:11:30 +01001545 int vnr, first_vol = 1;
Philipp Reisnere0e16652011-07-11 17:04:23 +02001546 union drbd_dev_state os, cs = {
1547 { .role = R_SECONDARY,
1548 .peer = R_UNKNOWN,
1549 .conn = tconn->cstate,
1550 .disk = D_DISKLESS,
1551 .pdsk = D_UNKNOWN,
1552 } };
Philipp Reisner88ef5942011-03-25 14:31:11 +01001553
Philipp Reisner695d08f2011-04-11 22:53:32 -07001554 rcu_read_lock();
Philipp Reisner88ef5942011-03-25 14:31:11 +01001555 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1556 os = mdev->state;
1557
Philipp Reisner435693e2011-03-25 15:11:30 +01001558 if (first_vol) {
1559 cs = os;
1560 first_vol = 0;
1561 continue;
1562 }
Philipp Reisner88ef5942011-03-25 14:31:11 +01001563
Philipp Reisner435693e2011-03-25 15:11:30 +01001564 if (cs.role != os.role)
1565 flags &= ~CS_DC_ROLE;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001566
Philipp Reisner435693e2011-03-25 15:11:30 +01001567 if (cs.peer != os.peer)
1568 flags &= ~CS_DC_PEER;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001569
Philipp Reisner435693e2011-03-25 15:11:30 +01001570 if (cs.conn != os.conn)
1571 flags &= ~CS_DC_CONN;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001572
Philipp Reisner435693e2011-03-25 15:11:30 +01001573 if (cs.disk != os.disk)
1574 flags &= ~CS_DC_DISK;
1575
1576 if (cs.pdsk != os.pdsk)
1577 flags &= ~CS_DC_PDSK;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001578 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001579 rcu_read_unlock();
Philipp Reisner88ef5942011-03-25 14:31:11 +01001580
Philipp Reisner435693e2011-03-25 15:11:30 +01001581 *pf |= CS_DC_MASK;
1582 *pf &= flags;
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02001583 (*pcs).i = cs.i;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001584}
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001585
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001586static enum drbd_state_rv
1587conn_is_valid_transition(struct drbd_tconn *tconn, union drbd_state mask, union drbd_state val,
Philipp Reisner88ef5942011-03-25 14:31:11 +01001588 enum chg_state_flags flags)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001589{
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001590 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001591 union drbd_state ns, os;
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001592 struct drbd_conf *mdev;
1593 int vnr;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001594
Philipp Reisner695d08f2011-04-11 22:53:32 -07001595 rcu_read_lock();
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001596 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner78bae592011-03-28 15:40:12 +02001597 os = drbd_read_state(mdev);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001598 ns = sanitize_state(mdev, apply_mask_val(os, mask, val), NULL);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001599
Philipp Reisner778bcf22011-03-28 12:55:03 +02001600 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1601 ns.disk = os.disk;
1602
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001603 if (ns.i == os.i)
1604 continue;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001605
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001606 rv = is_valid_transition(os, ns);
1607 if (rv < SS_SUCCESS)
1608 break;
1609
1610 if (!(flags & CS_HARD)) {
1611 rv = is_valid_state(mdev, ns);
1612 if (rv < SS_SUCCESS) {
1613 if (is_valid_state(mdev, os) == rv)
Philipp Reisnera1096a62012-04-06 12:07:34 +02001614 rv = is_valid_soft_transition(os, ns, tconn);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001615 } else
Philipp Reisnera1096a62012-04-06 12:07:34 +02001616 rv = is_valid_soft_transition(os, ns, tconn);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001617 }
1618 if (rv < SS_SUCCESS)
1619 break;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001620 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001621 rcu_read_unlock();
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001622
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001623 if (rv < SS_SUCCESS && flags & CS_VERBOSE)
1624 print_st_err(mdev, os, ns, rv);
1625
1626 return rv;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001627}
1628
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001629void
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001630conn_set_state(struct drbd_tconn *tconn, union drbd_state mask, union drbd_state val,
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001631 union drbd_state *pns_min, union drbd_state *pns_max, enum chg_state_flags flags)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001632{
Philipp Reisnerf132f552011-07-18 10:44:24 +02001633 union drbd_state ns, os, ns_max = { };
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001634 union drbd_state ns_min = {
1635 { .role = R_MASK,
1636 .peer = R_MASK,
Philipp Reisnere0e16652011-07-11 17:04:23 +02001637 .conn = val.conn,
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001638 .disk = D_MASK,
1639 .pdsk = D_MASK
1640 } };
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001641 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001642 enum drbd_state_rv rv;
Philipp Reisnerf132f552011-07-18 10:44:24 +02001643 int vnr, number_of_volumes = 0;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001644
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001645 if (mask.conn == C_MASK) {
1646 /* remember last connect time so request_timer_fn() won't
1647 * kill newly established sessions while we are still trying to thaw
1648 * previously frozen IO */
1649 if (tconn->cstate != C_WF_REPORT_PARAMS && val.conn == C_WF_REPORT_PARAMS)
1650 tconn->last_reconnect_jif = jiffies;
1651
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001652 tconn->cstate = val.conn;
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001653 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001654
Philipp Reisner695d08f2011-04-11 22:53:32 -07001655 rcu_read_lock();
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001656 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisnerf132f552011-07-18 10:44:24 +02001657 number_of_volumes++;
Philipp Reisner78bae592011-03-28 15:40:12 +02001658 os = drbd_read_state(mdev);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001659 ns = apply_mask_val(os, mask, val);
1660 ns = sanitize_state(mdev, ns, NULL);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001661
Philipp Reisner778bcf22011-03-28 12:55:03 +02001662 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1663 ns.disk = os.disk;
1664
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001665 rv = __drbd_set_state(mdev, ns, flags, NULL);
1666 if (rv < SS_SUCCESS)
1667 BUG();
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001668
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001669 ns.i = mdev->state.i;
1670 ns_max.role = max_role(ns.role, ns_max.role);
1671 ns_max.peer = max_role(ns.peer, ns_max.peer);
1672 ns_max.conn = max_t(enum drbd_conns, ns.conn, ns_max.conn);
1673 ns_max.disk = max_t(enum drbd_disk_state, ns.disk, ns_max.disk);
1674 ns_max.pdsk = max_t(enum drbd_disk_state, ns.pdsk, ns_max.pdsk);
1675
1676 ns_min.role = min_role(ns.role, ns_min.role);
1677 ns_min.peer = min_role(ns.peer, ns_min.peer);
1678 ns_min.conn = min_t(enum drbd_conns, ns.conn, ns_min.conn);
1679 ns_min.disk = min_t(enum drbd_disk_state, ns.disk, ns_min.disk);
1680 ns_min.pdsk = min_t(enum drbd_disk_state, ns.pdsk, ns_min.pdsk);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001681 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001682 rcu_read_unlock();
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001683
Philipp Reisnerf132f552011-07-18 10:44:24 +02001684 if (number_of_volumes == 0) {
1685 ns_min = ns_max = (union drbd_state) { {
1686 .role = R_SECONDARY,
1687 .peer = R_UNKNOWN,
1688 .conn = val.conn,
1689 .disk = D_DISKLESS,
1690 .pdsk = D_UNKNOWN
1691 } };
1692 }
1693
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001694 ns_min.susp = ns_max.susp = tconn->susp;
1695 ns_min.susp_nod = ns_max.susp_nod = tconn->susp_nod;
1696 ns_min.susp_fen = ns_max.susp_fen = tconn->susp_fen;
1697
1698 *pns_min = ns_min;
1699 *pns_max = ns_max;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001700}
1701
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001702static enum drbd_state_rv
1703_conn_rq_cond(struct drbd_tconn *tconn, union drbd_state mask, union drbd_state val)
1704{
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001705 enum drbd_state_rv rv;
1706
1707 if (test_and_clear_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags))
1708 return SS_CW_SUCCESS;
1709
1710 if (test_and_clear_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags))
1711 return SS_CW_FAILED_BY_PEER;
1712
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001713 spin_lock_irq(&tconn->req_lock);
1714 rv = tconn->cstate != C_WF_REPORT_PARAMS ? SS_CW_NO_NEED : SS_UNKNOWN_ERROR;
1715
1716 if (rv == SS_UNKNOWN_ERROR)
Philipp Reisner435693e2011-03-25 15:11:30 +01001717 rv = conn_is_valid_transition(tconn, mask, val, 0);
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001718
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001719 if (rv == SS_SUCCESS)
1720 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001721
1722 spin_unlock_irq(&tconn->req_lock);
1723
1724 return rv;
1725}
1726
1727static enum drbd_state_rv
1728conn_cl_wide(struct drbd_tconn *tconn, union drbd_state mask, union drbd_state val,
1729 enum chg_state_flags f)
1730{
1731 enum drbd_state_rv rv;
1732
1733 spin_unlock_irq(&tconn->req_lock);
1734 mutex_lock(&tconn->cstate_mutex);
1735
Philipp Reisner4d0fc3f2012-01-20 13:52:27 +01001736 set_bit(CONN_WD_ST_CHG_REQ, &tconn->flags);
Andreas Gruenbachercaee1c32011-03-16 01:17:35 +01001737 if (conn_send_state_req(tconn, mask, val)) {
Philipp Reisner4d0fc3f2012-01-20 13:52:27 +01001738 clear_bit(CONN_WD_ST_CHG_REQ, &tconn->flags);
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001739 rv = SS_CW_FAILED_BY_PEER;
1740 /* if (f & CS_VERBOSE)
1741 print_st_err(mdev, os, ns, rv); */
1742 goto abort;
1743 }
1744
1745 wait_event(tconn->ping_wait, (rv = _conn_rq_cond(tconn, mask, val)));
Philipp Reisner4d0fc3f2012-01-20 13:52:27 +01001746 clear_bit(CONN_WD_ST_CHG_REQ, &tconn->flags);
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001747
1748abort:
1749 mutex_unlock(&tconn->cstate_mutex);
1750 spin_lock_irq(&tconn->req_lock);
1751
1752 return rv;
1753}
1754
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001755enum 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 = SS_SUCCESS;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001760 struct after_conn_state_chg_work *acscw;
1761 enum drbd_conns oc = tconn->cstate;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001762 union drbd_state ns_max, ns_min, os;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001763
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001764 rv = is_valid_conn_transition(oc, val.conn);
1765 if (rv < SS_SUCCESS)
1766 goto abort;
1767
Philipp Reisner88ef5942011-03-25 14:31:11 +01001768 rv = conn_is_valid_transition(tconn, mask, val, flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001769 if (rv < SS_SUCCESS)
1770 goto abort;
1771
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001772 if (oc == C_WF_REPORT_PARAMS && val.conn == C_DISCONNECTING &&
1773 !(flags & (CS_LOCAL_ONLY | CS_HARD))) {
1774 rv = conn_cl_wide(tconn, mask, val, flags);
1775 if (rv < SS_SUCCESS)
1776 goto abort;
1777 }
1778
Philipp Reisner435693e2011-03-25 15:11:30 +01001779 conn_old_common_state(tconn, &os, &flags);
Philipp Reisner706cb242011-03-29 15:20:27 +02001780 flags |= CS_DC_SUSP;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001781 conn_set_state(tconn, mask, val, &ns_min, &ns_max, flags);
Philipp Reisner5f082f92011-03-29 13:20:58 +02001782 conn_pr_state_change(tconn, os, ns_max, flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001783
1784 acscw = kmalloc(sizeof(*acscw), GFP_ATOMIC);
1785 if (acscw) {
Philipp Reisner435693e2011-03-25 15:11:30 +01001786 acscw->oc = os.conn;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001787 acscw->ns_min = ns_min;
Philipp Reisner5f082f92011-03-29 13:20:58 +02001788 acscw->ns_max = ns_max;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001789 acscw->flags = flags;
1790 acscw->w.cb = w_after_conn_state_ch;
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +02001791 kref_get(&tconn->kref);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001792 acscw->w.tconn = tconn;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01001793 drbd_queue_work(&tconn->sender_work, &acscw->w);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001794 } else {
1795 conn_err(tconn, "Could not kmalloc an acscw\n");
1796 }
1797
Philipp Reisnera01842e2011-12-13 17:40:53 +01001798 return rv;
1799 abort:
1800 if (flags & CS_VERBOSE) {
1801 conn_err(tconn, "State change failed: %s\n", drbd_set_st_err_str(rv));
1802 conn_err(tconn, " state = { cs:%s }\n", drbd_conn_str(oc));
1803 conn_err(tconn, "wanted = { cs:%s }\n", drbd_conn_str(val.conn));
1804 }
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001805 return rv;
1806}
1807
1808enum drbd_state_rv
1809conn_request_state(struct drbd_tconn *tconn, union drbd_state mask, union drbd_state val,
1810 enum chg_state_flags flags)
1811{
1812 enum drbd_state_rv rv;
1813
1814 spin_lock_irq(&tconn->req_lock);
1815 rv = _conn_request_state(tconn, mask, val, flags);
1816 spin_unlock_irq(&tconn->req_lock);
1817
1818 return rv;
1819}