blob: 36703fee3b0cebb4d87d8ee03f60c53f3d151fae [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
32struct 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 Gruenbacher99920dc2011-03-16 15:31:39 +010040static int w_after_state_ch(struct drbd_work *w, int unused);
Philipp Reisnerb8907332011-01-27 14:07:51 +010041static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
42 union drbd_state ns, enum chg_state_flags flags);
Philipp Reisner0e29d162011-02-18 14:23:11 +010043static void after_all_state_ch(struct drbd_tconn *tconn);
Philipp Reisnera75f34a2011-02-09 15:10:33 +010044static enum drbd_state_rv is_valid_state(struct drbd_conf *, union drbd_state);
45static enum drbd_state_rv is_valid_soft_transition(union drbd_state, union drbd_state);
Philipp Reisner35095022011-02-09 16:29:33 +010046static enum drbd_state_rv is_valid_transition(union drbd_state os, union drbd_state ns);
Philipp Reisner4308a0a2011-02-10 11:24:38 +010047static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state ns,
48 const char **warn_sync_abort);
Philipp Reisnerb8907332011-01-27 14:07:51 +010049
Philipp Reisner2aebfab2011-03-28 16:48:11 +020050static inline bool is_susp(union drbd_state s)
51{
52 return s.susp || s.susp_nod || s.susp_fen;
53}
54
Lars Ellenbergd0456c72011-03-10 23:28:13 +010055bool conn_all_vols_unconf(struct drbd_tconn *tconn)
Philipp Reisner0e29d162011-02-18 14:23:11 +010056{
57 struct drbd_conf *mdev;
Philipp Reisnere90285e2011-03-22 12:51:21 +010058 int vnr;
Philipp Reisner0e29d162011-02-18 14:23:11 +010059
Philipp Reisnere90285e2011-03-22 12:51:21 +010060 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Lars Ellenbergd0456c72011-03-10 23:28:13 +010061 if (mdev->state.disk != D_DISKLESS ||
62 mdev->state.conn != C_STANDALONE ||
63 mdev->state.role != R_SECONDARY)
64 return false;
Philipp Reisner0e29d162011-02-18 14:23:11 +010065 }
Lars Ellenbergd0456c72011-03-10 23:28:13 +010066 return true;
Philipp Reisner0e29d162011-02-18 14:23:11 +010067}
68
Philipp Reisnercb703452011-03-24 11:03:07 +010069/* Unfortunately the states where not correctly ordered, when
70 they where defined. therefore can not use max_t() here. */
71static 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}
79static 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
88enum 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
100enum 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
112enum 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
124enum 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 Reisner19f83c72011-03-29 14:21:03 +0200136enum 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 Reisnerb8907332011-01-27 14:07:51 +0100148/**
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 */
154static 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 Reisner56707f92011-02-16 14:57:50 +0100166static union drbd_state
167apply_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 Reisnerb8907332011-01-27 14:07:51 +0100174enum drbd_state_rv
175drbd_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 Reisner56707f92011-02-16 14:57:50 +0100179 union drbd_state ns;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100180 enum drbd_state_rv rv;
181
182 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisner78bae592011-03-28 15:40:12 +0200183 ns = apply_mask_val(drbd_read_state(mdev), mask, val);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100184 rv = _drbd_set_state(mdev, ns, f, NULL);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100185 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 */
196void 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
202static 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 Reisnerb8907332011-01-27 14:07:51 +0100216 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisner78bae592011-03-28 15:40:12 +0200217 os = drbd_read_state(mdev);
Philipp Reisner56707f92011-02-16 14:57:50 +0100218 ns = sanitize_state(mdev, apply_mask_val(os, mask, val), NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100219 rv = is_valid_transition(os, ns);
220 if (rv == SS_SUCCESS)
221 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100222
223 if (!cl_wide_st_chg(mdev, os, ns))
224 rv = SS_CW_NO_NEED;
Philipp Reisner35095022011-02-09 16:29:33 +0100225 if (rv == SS_UNKNOWN_ERROR) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100226 rv = is_valid_state(mdev, ns);
227 if (rv == SS_SUCCESS) {
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100228 rv = is_valid_soft_transition(os, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100229 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 */
248static enum drbd_state_rv
249drbd_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 Reisner8410da82011-02-11 20:11:10 +0100260 mutex_lock(mdev->state_mutex);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100261
262 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Philipp Reisner78bae592011-03-28 15:40:12 +0200263 os = drbd_read_state(mdev);
Philipp Reisner56707f92011-02-16 14:57:50 +0100264 ns = sanitize_state(mdev, apply_mask_val(os, mask, val), NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100265 rv = is_valid_transition(os, ns);
Lars Ellenberg3c5e5f62011-03-15 16:04:09 +0100266 if (rv < SS_SUCCESS) {
267 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Philipp Reisner35095022011-02-09 16:29:33 +0100268 goto abort;
Lars Ellenberg3c5e5f62011-03-15 16:04:09 +0100269 }
Philipp Reisnerb8907332011-01-27 14:07:51 +0100270
271 if (cl_wide_st_chg(mdev, os, ns)) {
272 rv = is_valid_state(mdev, ns);
273 if (rv == SS_SUCCESS)
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100274 rv = is_valid_soft_transition(os, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100275 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 Gruenbacherd24ae212011-03-16 01:19:27 +0100283 if (drbd_send_state_req(mdev, mask, val)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100284 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 Reisnerb8907332011-01-27 14:07:51 +0100294 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 Reisner78bae592011-03-28 15:40:12 +0200299 ns = apply_mask_val(drbd_read_state(mdev), mask, val);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100300 rv = _drbd_set_state(mdev, ns, f, &done);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100301 } 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
312abort:
313 if (f & CS_SERIALIZE)
Philipp Reisner8410da82011-02-11 20:11:10 +0100314 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100315
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 */
329enum 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
341static 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
359void 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 Reisner435693e2011-03-25 15:11:30 +0100369static long print_state_change(char *pb, union drbd_state os, union drbd_state ns,
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100370 enum chg_state_flags flags)
371{
Philipp Reisner435693e2011-03-25 15:11:30 +0100372 char *pbp;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100373 pbp = pb;
374 *pbp = 0;
Philipp Reisner435693e2011-03-25 15:11:30 +0100375 if (ns.role != os.role && flags & CS_DC_ROLE)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100376 pbp += sprintf(pbp, "role( %s -> %s ) ",
377 drbd_role_str(os.role),
378 drbd_role_str(ns.role));
Philipp Reisner435693e2011-03-25 15:11:30 +0100379 if (ns.peer != os.peer && flags & CS_DC_PEER)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100380 pbp += sprintf(pbp, "peer( %s -> %s ) ",
381 drbd_role_str(os.peer),
382 drbd_role_str(ns.peer));
Philipp Reisner435693e2011-03-25 15:11:30 +0100383 if (ns.conn != os.conn && flags & CS_DC_CONN)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100384 pbp += sprintf(pbp, "conn( %s -> %s ) ",
385 drbd_conn_str(os.conn),
386 drbd_conn_str(ns.conn));
Philipp Reisner435693e2011-03-25 15:11:30 +0100387 if (ns.disk != os.disk && flags & CS_DC_DISK)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100388 pbp += sprintf(pbp, "disk( %s -> %s ) ",
389 drbd_disk_str(os.disk),
390 drbd_disk_str(ns.disk));
Philipp Reisner435693e2011-03-25 15:11:30 +0100391 if (ns.pdsk != os.pdsk && flags & CS_DC_PDSK)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100392 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 Reisner435693e2011-03-25 15:11:30 +0100411
412 return pbp - pb;
413}
414
415static 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 Reisnerbbeb6412011-02-10 13:45:46 +0100421 dev_info(DEV, "%s\n", pb);
422}
Philipp Reisnerb8907332011-01-27 14:07:51 +0100423
Philipp Reisner435693e2011-03-25 15:11:30 +0100424static 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 Reisnerb8907332011-01-27 14:07:51 +0100434/**
435 * is_valid_state() - Returns an SS_ error code if ns is not valid
436 * @mdev: DRBD device.
437 * @ns: State to consider.
438 */
439static enum drbd_state_rv
440is_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 Reisner047e95e22011-03-16 14:43:36 +0100454 if (!mdev->tconn->net_conf->two_primaries && ns.role == R_PRIMARY) {
455 if (ns.peer == R_PRIMARY)
456 rv = SS_TWO_PRIMARIES;
Philipp Reisnercb703452011-03-24 11:03:07 +0100457 else if (conn_highest_peer(mdev->tconn) == R_PRIMARY)
Philipp Reisner047e95e22011-03-16 14:43:36 +0100458 rv = SS_O_VOL_PEER_PRI;
459 }
Philipp Reisnerb8907332011-01-27 14:07:51 +0100460 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 Ellenbergf3990022011-03-23 14:31:09 +0100495 (mdev->tconn->net_conf->verify_alg[0] == 0))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100496 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 Reisnera75f34a2011-02-09 15:10:33 +0100509 * is_valid_soft_transition() - Returns an SS_ error code if the state transition is not possible
Philipp Reisner35095022011-02-09 16:29:33 +0100510 * This function limits state transitions that may be declined by DRBD. I.e.
511 * user requests (aka soft transitions).
Philipp Reisnerb8907332011-01-27 14:07:51 +0100512 * @mdev: DRBD device.
513 * @ns: new state.
514 * @os: old state.
515 */
516static enum drbd_state_rv
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100517is_valid_soft_transition(union drbd_state os, union drbd_state ns)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100518{
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 Reisner2325eb62011-03-15 16:56:18 +0100540 /* if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
541 rv = SS_IN_TRANSIENT_STATE; */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100542
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 Reisnerfda74112011-02-10 10:38:06 +0100561static enum drbd_state_rv
562is_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 Reisnerb8907332011-01-27 14:07:51 +0100582/**
Philipp Reisner35095022011-02-09 16:29:33 +0100583 * 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 */
590static enum drbd_state_rv
591is_valid_transition(union drbd_state os, union drbd_state ns)
592{
Philipp Reisnerfda74112011-02-10 10:38:06 +0100593 enum drbd_state_rv rv;
Philipp Reisner35095022011-02-09 16:29:33 +0100594
Philipp Reisnerfda74112011-02-10 10:38:06 +0100595 rv = is_valid_conn_transition(os.conn, ns.conn);
Philipp Reisner35095022011-02-09 16:29:33 +0100596
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 Reisner4308a0a2011-02-10 11:24:38 +0100601 /* 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 Reisner35095022011-02-09 16:29:33 +0100608 return rv;
609}
610
611/**
Philipp Reisnerb8907332011-01-27 14:07:51 +0100612 * 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 Reisner4308a0a2011-02-10 11:24:38 +0100621static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state ns,
622 const char **warn_sync_abort)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100623{
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 Reisner35095022011-02-09 16:29:33 +0100633 /* Implications from connection to peer and peer_isp */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100634 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 Reisner4308a0a2011-02-10 11:24:38 +0100645 /* An implication of the disk states onto the connection state */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100646 /* Abort resync if a disk fails/detaches */
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100647 if (ns.conn > C_CONNECTED && (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100648 if (warn_sync_abort)
649 *warn_sync_abort =
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100650 ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T ?
Philipp Reisnerb8907332011-01-27 14:07:51 +0100651 "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 Reisner4308a0a2011-02-10 11:24:38 +0100758 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100759 ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
760
Lars Ellenbergf3990022011-03-23 14:31:09 +0100761 if (mdev->tconn->res_opts.on_no_data == OND_SUSPEND_IO &&
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100762 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
Philipp Reisnerb8907332011-01-27 14:07:51 +0100763 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
780void 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 */
787static 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 */
822enum 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 Reisner78bae592011-03-28 15:40:12 +0200831 os = drbd_read_state(mdev);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100832
Philipp Reisner4308a0a2011-02-10 11:24:38 +0100833 ns = sanitize_state(mdev, ns, &warn_sync_abort);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100834 if (ns.i == os.i)
835 return SS_NOTHING_TO_DO;
836
Philipp Reisner35095022011-02-09 16:29:33 +0100837 rv = is_valid_transition(os, ns);
838 if (rv < SS_SUCCESS)
839 return rv;
840
Philipp Reisnerb8907332011-01-27 14:07:51 +0100841 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 Reisnera75f34a2011-02-09 15:10:33 +0100851 rv = is_valid_soft_transition(os, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100852 } else
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100853 rv = is_valid_soft_transition(os, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100854 }
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 Reisner435693e2011-03-25 15:11:30 +0100865 drbd_pr_state_change(mdev, os, ns, flags);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100866
Philipp Reisnerb8907332011-01-27 14:07:51 +0100867 /* 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 Reisnerda9fbc22011-03-29 10:52:01 +0200875 mdev->state.i = ns.i;
Philipp Reisner8e0af252011-03-28 16:18:39 +0200876 mdev->tconn->susp = ns.susp;
877 mdev->tconn->susp_nod = ns.susp_nod;
878 mdev->tconn->susp_fen = ns.susp_fen;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100879
Philipp Reisner0e29d162011-02-18 14:23:11 +0100880 /* 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 Reisnerb8907332011-01-27 14:07:51 +0100892 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 Reisner2a67d8b2011-02-09 14:10:32 +0100897 wake_up(&mdev->tconn->ping_wait);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100898
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 Reisnera21e9292011-02-08 15:08:49 +01001004 ascw->w.mdev = mdev;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001005 ascw->done = done;
1006 drbd_queue_work(&mdev->tconn->data.work, &ascw->w);
1007 } else {
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001008 dev_err(DEV, "Could not kmalloc an ascw\n");
Philipp Reisnerb8907332011-01-27 14:07:51 +01001009 }
1010
1011 return rv;
1012}
1013
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001014static int w_after_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerb8907332011-01-27 14:07:51 +01001015{
1016 struct after_state_chg_work *ascw =
1017 container_of(w, struct after_state_chg_work, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001018 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001019
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 Gruenbacher99920dc2011-03-16 15:31:39 +01001027 return 0;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001028}
1029
1030static 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
1048int 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 */
1075static 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 Gruenbacher69f5ec72010-12-13 22:50:15 +01001080 union drbd_state nsm;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001081 struct sib_info sib;
1082
1083 sib.sib_reason = SIB_STATE_CHANGE;
1084 sib.os = os;
1085 sib.ns = ns;
Philipp Reisnerb8907332011-01-27 14:07:51 +01001086
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 Ellenberg3b98c0c2011-03-07 12:49:34 +01001100 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001101
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 Reisner2f5cdd02011-02-21 14:29:27 +01001124 tl_clear(mdev->tconn);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001125 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 Reisner2f5cdd02011-02-21 14:29:27 +01001143 _tl_restart(mdev->tconn, what);
Philipp Reisnerb8907332011-01-27 14:07:51 +01001144 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 Reisner2aebfab2011-03-28 16:48:11 +02001184 if (drbd_suspended(mdev)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001185 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 Gruenbacher927036f2011-03-16 00:50:00 +01001284 if (!drbd_send_state(mdev))
Philipp Reisnerb8907332011-01-27 14:07:51 +01001285 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 Gruenbacher927036f2011-03-16 00:50:00 +01001316 if (!drbd_send_state(mdev))
Philipp Reisnerb8907332011-01-27 14:07:51 +01001317 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 Reisner0e29d162011-02-18 14:23:11 +01001365 after_all_state_ch(mdev->tconn);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001366
Philipp Reisnerb8907332011-01-27 14:07:51 +01001367 drbd_md_sync(mdev);
1368}
1369
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001370struct after_conn_state_chg_work {
1371 struct drbd_work w;
1372 enum drbd_conns oc;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001373 union drbd_state ns_min;
Philipp Reisner5f082f92011-03-29 13:20:58 +02001374 union drbd_state ns_max; /* new, max state, over all mdevs */
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001375 enum chg_state_flags flags;
1376};
Philipp Reisnerb8907332011-01-27 14:07:51 +01001377
Philipp Reisner0e29d162011-02-18 14:23:11 +01001378static void after_all_state_ch(struct drbd_tconn *tconn)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001379{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001380 if (conn_all_vols_unconf(tconn) &&
1381 test_bit(OBJECT_DYING, &tconn->flags)) {
Philipp Reisnerb8907332011-01-27 14:07:51 +01001382 drbd_thread_stop_nowait(&tconn->worker);
1383 }
1384}
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001385
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001386static int w_after_conn_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001387{
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 Reisner5f082f92011-03-29 13:20:58 +02001392 union drbd_state ns_max = acscw->ns_max;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001393
1394 kfree(acscw);
1395
1396 /* Upon network configuration, we need to start the receiver */
Philipp Reisner5f082f92011-03-29 13:20:58 +02001397 if (oc == C_STANDALONE && ns_max.conn == C_UNCONNECTED)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001398 drbd_thread_start(&tconn->receiver);
1399
1400 //conn_err(tconn, STATE_FMT, STATE_ARGS("nms", nms));
Philipp Reisner0e29d162011-02-18 14:23:11 +01001401 after_all_state_ch(tconn);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001402
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001403 return 0;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001404}
1405
Philipp Reisner435693e2011-03-25 15:11:30 +01001406void conn_old_common_state(struct drbd_tconn *tconn, union drbd_state *pcs, enum chg_state_flags *pf)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001407{
Philipp Reisner435693e2011-03-25 15:11:30 +01001408 enum chg_state_flags flags = ~0;
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02001409 union drbd_dev_state os, cs = {}; /* old_state, common_state */
Philipp Reisner88ef5942011-03-25 14:31:11 +01001410 struct drbd_conf *mdev;
Philipp Reisner435693e2011-03-25 15:11:30 +01001411 int vnr, first_vol = 1;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001412
1413 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1414 os = mdev->state;
1415
Philipp Reisner435693e2011-03-25 15:11:30 +01001416 if (first_vol) {
1417 cs = os;
1418 first_vol = 0;
1419 continue;
1420 }
Philipp Reisner88ef5942011-03-25 14:31:11 +01001421
Philipp Reisner435693e2011-03-25 15:11:30 +01001422 if (cs.role != os.role)
1423 flags &= ~CS_DC_ROLE;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001424
Philipp Reisner435693e2011-03-25 15:11:30 +01001425 if (cs.peer != os.peer)
1426 flags &= ~CS_DC_PEER;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001427
Philipp Reisner435693e2011-03-25 15:11:30 +01001428 if (cs.conn != os.conn)
1429 flags &= ~CS_DC_CONN;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001430
Philipp Reisner435693e2011-03-25 15:11:30 +01001431 if (cs.disk != os.disk)
1432 flags &= ~CS_DC_DISK;
1433
1434 if (cs.pdsk != os.pdsk)
1435 flags &= ~CS_DC_PDSK;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001436 }
1437
Philipp Reisner435693e2011-03-25 15:11:30 +01001438 *pf |= CS_DC_MASK;
1439 *pf &= flags;
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02001440 (*pcs).i = cs.i;
Philipp Reisner88ef5942011-03-25 14:31:11 +01001441}
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001442
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001443static enum drbd_state_rv
1444conn_is_valid_transition(struct drbd_tconn *tconn, union drbd_state mask, union drbd_state val,
Philipp Reisner88ef5942011-03-25 14:31:11 +01001445 enum chg_state_flags flags)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001446{
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001447 enum drbd_state_rv rv = SS_SUCCESS;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001448 union drbd_state ns, os;
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001449 struct drbd_conf *mdev;
1450 int vnr;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001451
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001452 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner78bae592011-03-28 15:40:12 +02001453 os = drbd_read_state(mdev);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001454 ns = sanitize_state(mdev, apply_mask_val(os, mask, val), NULL);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001455
Philipp Reisner778bcf22011-03-28 12:55:03 +02001456 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1457 ns.disk = os.disk;
1458
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001459 if (ns.i == os.i)
1460 continue;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001461
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001462 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 Reisnerbbeb6412011-02-10 13:45:46 +01001476 }
1477
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001478 if (rv < SS_SUCCESS && flags & CS_VERBOSE)
1479 print_st_err(mdev, os, ns, rv);
1480
1481 return rv;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001482}
1483
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001484void
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001485conn_set_state(struct drbd_tconn *tconn, union drbd_state mask, union drbd_state val,
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001486 union drbd_state *pns_min, union drbd_state *pns_max, enum chg_state_flags flags)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001487{
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001488 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 Reisnerbd0c8242011-03-25 12:02:20 +01001495 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001496 enum drbd_state_rv rv;
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001497 int vnr;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001498
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001499 if (mask.conn == C_MASK)
1500 tconn->cstate = val.conn;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001501
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001502 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner78bae592011-03-28 15:40:12 +02001503 os = drbd_read_state(mdev);
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001504 ns = apply_mask_val(os, mask, val);
1505 ns = sanitize_state(mdev, ns, NULL);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001506
Philipp Reisner778bcf22011-03-28 12:55:03 +02001507 if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
1508 ns.disk = os.disk;
1509
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001510 rv = __drbd_set_state(mdev, ns, flags, NULL);
1511 if (rv < SS_SUCCESS)
1512 BUG();
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001513
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001514 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 Reisnerbd0c8242011-03-25 12:02:20 +01001526 }
1527
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001528 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 Reisnerbbeb6412011-02-10 13:45:46 +01001534}
1535
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001536static enum drbd_state_rv
1537_conn_rq_cond(struct drbd_tconn *tconn, union drbd_state mask, union drbd_state val)
1538{
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001539 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 Reisnerdf24aa42011-02-15 11:14:44 +01001547 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 Reisner435693e2011-03-25 15:11:30 +01001551 rv = conn_is_valid_transition(tconn, mask, val, 0);
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001552
Philipp Reisnerbd0c8242011-03-25 12:02:20 +01001553 if (rv == SS_SUCCESS)
1554 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001555
1556 spin_unlock_irq(&tconn->req_lock);
1557
1558 return rv;
1559}
1560
1561static enum drbd_state_rv
1562conn_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 Gruenbachercaee1c32011-03-16 01:17:35 +01001570 if (conn_send_state_req(tconn, mask, val)) {
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001571 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
1579abort:
1580 mutex_unlock(&tconn->cstate_mutex);
1581 spin_lock_irq(&tconn->req_lock);
1582
1583 return rv;
1584}
1585
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001586enum 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 Reisnerbbeb6412011-02-10 13:45:46 +01001591 struct after_conn_state_chg_work *acscw;
1592 enum drbd_conns oc = tconn->cstate;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001593 union drbd_state ns_max, ns_min, os;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001594
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001595 rv = is_valid_conn_transition(oc, val.conn);
1596 if (rv < SS_SUCCESS)
1597 goto abort;
1598
Philipp Reisner88ef5942011-03-25 14:31:11 +01001599 rv = conn_is_valid_transition(tconn, mask, val, flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001600 if (rv < SS_SUCCESS)
1601 goto abort;
1602
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001603 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 Reisner435693e2011-03-25 15:11:30 +01001610 conn_old_common_state(tconn, &os, &flags);
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001611 conn_set_state(tconn, mask, val, &ns_min, &ns_max, flags);
Philipp Reisner5f082f92011-03-29 13:20:58 +02001612 conn_pr_state_change(tconn, os, ns_max, flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001613
1614 acscw = kmalloc(sizeof(*acscw), GFP_ATOMIC);
1615 if (acscw) {
Philipp Reisner435693e2011-03-25 15:11:30 +01001616 acscw->oc = os.conn;
Philipp Reisner8c7e16c2011-03-29 14:01:02 +02001617 acscw->ns_min = ns_min;
Philipp Reisner5f082f92011-03-29 13:20:58 +02001618 acscw->ns_max = ns_max;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001619 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
1627abort:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001628 return rv;
1629}
1630
1631enum drbd_state_rv
1632conn_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}