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