blob: b381faade0a771af089db82069f7a80a2841796c [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
40
41extern void _tl_restart(struct drbd_conf *mdev, enum drbd_req_event what);
42int drbd_send_state_req(struct drbd_conf *, union drbd_state, union drbd_state);
Philipp Reisner00d56942011-02-09 18:09:48 +010043static int w_after_state_ch(struct drbd_work *w, int unused);
Philipp Reisnerb8907332011-01-27 14:07:51 +010044static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
45 union drbd_state ns, enum chg_state_flags flags);
46static void after_conn_state_ch(struct drbd_tconn *tconn, union drbd_state os,
47 union drbd_state ns, enum chg_state_flags flags);
Philipp Reisnera75f34a2011-02-09 15:10:33 +010048static enum drbd_state_rv is_valid_state(struct drbd_conf *, union drbd_state);
49static enum drbd_state_rv is_valid_soft_transition(union drbd_state, union drbd_state);
Philipp Reisner35095022011-02-09 16:29:33 +010050static enum drbd_state_rv is_valid_transition(union drbd_state os, union drbd_state ns);
Philipp Reisnera75f34a2011-02-09 15:10:33 +010051static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state os,
52 union drbd_state ns, const char **warn_sync_abort);
Philipp Reisnerb8907332011-01-27 14:07:51 +010053
54/**
55 * cl_wide_st_chg() - true if the state change is a cluster wide one
56 * @mdev: DRBD device.
57 * @os: old (current) state.
58 * @ns: new (wanted) state.
59 */
60static int cl_wide_st_chg(struct drbd_conf *mdev,
61 union drbd_state os, union drbd_state ns)
62{
63 return (os.conn >= C_CONNECTED && ns.conn >= C_CONNECTED &&
64 ((os.role != R_PRIMARY && ns.role == R_PRIMARY) ||
65 (os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
66 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S) ||
67 (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))) ||
68 (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
69 (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S);
70}
71
72enum drbd_state_rv
73drbd_change_state(struct drbd_conf *mdev, enum chg_state_flags f,
74 union drbd_state mask, union drbd_state val)
75{
76 unsigned long flags;
77 union drbd_state os, ns;
78 enum drbd_state_rv rv;
79
80 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
81 os = mdev->state;
82 ns.i = (os.i & ~mask.i) | val.i;
83 rv = _drbd_set_state(mdev, ns, f, NULL);
84 ns = mdev->state;
85 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
86
87 return rv;
88}
89
90/**
91 * drbd_force_state() - Impose a change which happens outside our control on our state
92 * @mdev: DRBD device.
93 * @mask: mask of state bits to change.
94 * @val: value of new state bits.
95 */
96void drbd_force_state(struct drbd_conf *mdev,
97 union drbd_state mask, union drbd_state val)
98{
99 drbd_change_state(mdev, CS_HARD, mask, val);
100}
101
102static enum drbd_state_rv
103_req_st_cond(struct drbd_conf *mdev, union drbd_state mask,
104 union drbd_state val)
105{
106 union drbd_state os, ns;
107 unsigned long flags;
108 enum drbd_state_rv rv;
109
110 if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &mdev->flags))
111 return SS_CW_SUCCESS;
112
113 if (test_and_clear_bit(CL_ST_CHG_FAIL, &mdev->flags))
114 return SS_CW_FAILED_BY_PEER;
115
Philipp Reisnerb8907332011-01-27 14:07:51 +0100116 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
117 os = mdev->state;
118 ns.i = (os.i & ~mask.i) | val.i;
119 ns = sanitize_state(mdev, os, ns, NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100120 rv = is_valid_transition(os, ns);
121 if (rv == SS_SUCCESS)
122 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100123
124 if (!cl_wide_st_chg(mdev, os, ns))
125 rv = SS_CW_NO_NEED;
Philipp Reisner35095022011-02-09 16:29:33 +0100126 if (rv == SS_UNKNOWN_ERROR) {
Philipp Reisnerb8907332011-01-27 14:07:51 +0100127 rv = is_valid_state(mdev, ns);
128 if (rv == SS_SUCCESS) {
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100129 rv = is_valid_soft_transition(os, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100130 if (rv == SS_SUCCESS)
131 rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
132 }
133 }
134 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
135
136 return rv;
137}
138
139/**
140 * drbd_req_state() - Perform an eventually cluster wide state change
141 * @mdev: DRBD device.
142 * @mask: mask of state bits to change.
143 * @val: value of new state bits.
144 * @f: flags
145 *
146 * Should not be called directly, use drbd_request_state() or
147 * _drbd_request_state().
148 */
149static enum drbd_state_rv
150drbd_req_state(struct drbd_conf *mdev, union drbd_state mask,
151 union drbd_state val, enum chg_state_flags f)
152{
153 struct completion done;
154 unsigned long flags;
155 union drbd_state os, ns;
156 enum drbd_state_rv rv;
157
158 init_completion(&done);
159
160 if (f & CS_SERIALIZE)
161 mutex_lock(&mdev->state_mutex);
162
163 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
164 os = mdev->state;
165 ns.i = (os.i & ~mask.i) | val.i;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100166 ns = sanitize_state(mdev, os, ns, NULL);
Philipp Reisner35095022011-02-09 16:29:33 +0100167 rv = is_valid_transition(os, ns);
168 if (rv < SS_SUCCESS)
169 goto abort;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100170
171 if (cl_wide_st_chg(mdev, os, ns)) {
172 rv = is_valid_state(mdev, ns);
173 if (rv == SS_SUCCESS)
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100174 rv = is_valid_soft_transition(os, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100175 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
176
177 if (rv < SS_SUCCESS) {
178 if (f & CS_VERBOSE)
179 print_st_err(mdev, os, ns, rv);
180 goto abort;
181 }
182
183 drbd_state_lock(mdev);
184 if (!drbd_send_state_req(mdev, mask, val)) {
185 drbd_state_unlock(mdev);
186 rv = SS_CW_FAILED_BY_PEER;
187 if (f & CS_VERBOSE)
188 print_st_err(mdev, os, ns, rv);
189 goto abort;
190 }
191
192 wait_event(mdev->state_wait,
193 (rv = _req_st_cond(mdev, mask, val)));
194
195 if (rv < SS_SUCCESS) {
196 drbd_state_unlock(mdev);
197 if (f & CS_VERBOSE)
198 print_st_err(mdev, os, ns, rv);
199 goto abort;
200 }
201 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
202 os = mdev->state;
203 ns.i = (os.i & ~mask.i) | val.i;
204 rv = _drbd_set_state(mdev, ns, f, &done);
205 drbd_state_unlock(mdev);
206 } else {
207 rv = _drbd_set_state(mdev, ns, f, &done);
208 }
209
210 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
211
212 if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
213 D_ASSERT(current != mdev->tconn->worker.task);
214 wait_for_completion(&done);
215 }
216
217abort:
218 if (f & CS_SERIALIZE)
219 mutex_unlock(&mdev->state_mutex);
220
221 return rv;
222}
223
224/**
225 * _drbd_request_state() - Request a state change (with flags)
226 * @mdev: DRBD device.
227 * @mask: mask of state bits to change.
228 * @val: value of new state bits.
229 * @f: flags
230 *
231 * Cousin of drbd_request_state(), useful with the CS_WAIT_COMPLETE
232 * flag, or when logging of failed state change requests is not desired.
233 */
234enum drbd_state_rv
235_drbd_request_state(struct drbd_conf *mdev, union drbd_state mask,
236 union drbd_state val, enum chg_state_flags f)
237{
238 enum drbd_state_rv rv;
239
240 wait_event(mdev->state_wait,
241 (rv = drbd_req_state(mdev, mask, val, f)) != SS_IN_TRANSIENT_STATE);
242
243 return rv;
244}
245
246static void print_st(struct drbd_conf *mdev, char *name, union drbd_state ns)
247{
248 dev_err(DEV, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c%c%c }\n",
249 name,
250 drbd_conn_str(ns.conn),
251 drbd_role_str(ns.role),
252 drbd_role_str(ns.peer),
253 drbd_disk_str(ns.disk),
254 drbd_disk_str(ns.pdsk),
255 is_susp(ns) ? 's' : 'r',
256 ns.aftr_isp ? 'a' : '-',
257 ns.peer_isp ? 'p' : '-',
258 ns.user_isp ? 'u' : '-',
259 ns.susp_fen ? 'F' : '-',
260 ns.susp_nod ? 'N' : '-'
261 );
262}
263
264void print_st_err(struct drbd_conf *mdev, union drbd_state os,
265 union drbd_state ns, enum drbd_state_rv err)
266{
267 if (err == SS_IN_TRANSIENT_STATE)
268 return;
269 dev_err(DEV, "State change failed: %s\n", drbd_set_st_err_str(err));
270 print_st(mdev, " state", os);
271 print_st(mdev, "wanted", ns);
272}
273
274
275/**
276 * is_valid_state() - Returns an SS_ error code if ns is not valid
277 * @mdev: DRBD device.
278 * @ns: State to consider.
279 */
280static enum drbd_state_rv
281is_valid_state(struct drbd_conf *mdev, union drbd_state ns)
282{
283 /* See drbd_state_sw_errors in drbd_strings.c */
284
285 enum drbd_fencing_p fp;
286 enum drbd_state_rv rv = SS_SUCCESS;
287
288 fp = FP_DONT_CARE;
289 if (get_ldev(mdev)) {
290 fp = mdev->ldev->dc.fencing;
291 put_ldev(mdev);
292 }
293
294 if (get_net_conf(mdev->tconn)) {
295 if (!mdev->tconn->net_conf->two_primaries &&
296 ns.role == R_PRIMARY && ns.peer == R_PRIMARY)
297 rv = SS_TWO_PRIMARIES;
298 put_net_conf(mdev->tconn);
299 }
300
301 if (rv <= 0)
302 /* already found a reason to abort */;
303 else if (ns.role == R_SECONDARY && mdev->open_cnt)
304 rv = SS_DEVICE_IN_USE;
305
306 else if (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.disk < D_UP_TO_DATE)
307 rv = SS_NO_UP_TO_DATE_DISK;
308
309 else if (fp >= FP_RESOURCE &&
310 ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
311 rv = SS_PRIMARY_NOP;
312
313 else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
314 rv = SS_NO_UP_TO_DATE_DISK;
315
316 else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
317 rv = SS_NO_LOCAL_DISK;
318
319 else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
320 rv = SS_NO_REMOTE_DISK;
321
322 else if (ns.conn > C_CONNECTED && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
323 rv = SS_NO_UP_TO_DATE_DISK;
324
325 else if ((ns.conn == C_CONNECTED ||
326 ns.conn == C_WF_BITMAP_S ||
327 ns.conn == C_SYNC_SOURCE ||
328 ns.conn == C_PAUSED_SYNC_S) &&
329 ns.disk == D_OUTDATED)
330 rv = SS_CONNECTED_OUTDATES;
331
332 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
333 (mdev->sync_conf.verify_alg[0] == 0))
334 rv = SS_NO_VERIFY_ALG;
335
336 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
337 mdev->tconn->agreed_pro_version < 88)
338 rv = SS_NOT_SUPPORTED;
339
340 else if (ns.conn >= C_CONNECTED && ns.pdsk == D_UNKNOWN)
341 rv = SS_CONNECTED_OUTDATES;
342
343 return rv;
344}
345
346/**
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100347 * is_valid_soft_transition() - Returns an SS_ error code if the state transition is not possible
Philipp Reisner35095022011-02-09 16:29:33 +0100348 * This function limits state transitions that may be declined by DRBD. I.e.
349 * user requests (aka soft transitions).
Philipp Reisnerb8907332011-01-27 14:07:51 +0100350 * @mdev: DRBD device.
351 * @ns: new state.
352 * @os: old state.
353 */
354static enum drbd_state_rv
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100355is_valid_soft_transition(union drbd_state os, union drbd_state ns)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100356{
357 enum drbd_state_rv rv = SS_SUCCESS;
358
359 if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
360 os.conn > C_CONNECTED)
361 rv = SS_RESYNC_RUNNING;
362
363 if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
364 rv = SS_ALREADY_STANDALONE;
365
366 if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
367 rv = SS_IS_DISKLESS;
368
369 if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
370 rv = SS_NO_NET_CONFIG;
371
372 if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
373 rv = SS_LOWER_THAN_OUTDATED;
374
375 if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
376 rv = SS_IN_TRANSIENT_STATE;
377
378 if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
379 rv = SS_IN_TRANSIENT_STATE;
380
381 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
382 rv = SS_NEED_CONNECTION;
383
384 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
385 ns.conn != os.conn && os.conn > C_CONNECTED)
386 rv = SS_RESYNC_RUNNING;
387
388 if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
389 os.conn < C_CONNECTED)
390 rv = SS_NEED_CONNECTION;
391
392 if ((ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)
393 && os.conn < C_WF_REPORT_PARAMS)
394 rv = SS_NEED_CONNECTION; /* No NetworkFailure -> SyncTarget etc... */
395
396 return rv;
397}
398
Philipp Reisnerfda74112011-02-10 10:38:06 +0100399static enum drbd_state_rv
400is_valid_conn_transition(enum drbd_conns oc, enum drbd_conns nc)
401{
402 enum drbd_state_rv rv = SS_SUCCESS;
403
404 /* Disallow Network errors to configure a device's network part */
405 if ((nc >= C_TIMEOUT && nc <= C_TEAR_DOWN) && oc <= C_DISCONNECTING)
406 rv = SS_NEED_CONNECTION;
407
408 /* After a network error only C_UNCONNECTED or C_DISCONNECTING may follow. */
409 if (oc >= C_TIMEOUT && oc <= C_TEAR_DOWN && nc != C_UNCONNECTED && nc != C_DISCONNECTING)
410 rv = SS_IN_TRANSIENT_STATE;
411
412 /* After C_DISCONNECTING only C_STANDALONE may follow */
413 if (oc == C_DISCONNECTING && nc != C_STANDALONE)
414 rv = SS_IN_TRANSIENT_STATE;
415
416 return rv;
417}
418
419
Philipp Reisnerb8907332011-01-27 14:07:51 +0100420/**
Philipp Reisner35095022011-02-09 16:29:33 +0100421 * is_valid_transition() - Returns an SS_ error code if the state transition is not possible
422 * This limits hard state transitions. Hard state transitions are facts there are
423 * imposed on DRBD by the environment. E.g. disk broke or network broke down.
424 * But those hard state transitions are still not allowed to do everything.
425 * @ns: new state.
426 * @os: old state.
427 */
428static enum drbd_state_rv
429is_valid_transition(union drbd_state os, union drbd_state ns)
430{
Philipp Reisnerfda74112011-02-10 10:38:06 +0100431 enum drbd_state_rv rv;
Philipp Reisner35095022011-02-09 16:29:33 +0100432
Philipp Reisnerfda74112011-02-10 10:38:06 +0100433 rv = is_valid_conn_transition(os.conn, ns.conn);
Philipp Reisner35095022011-02-09 16:29:33 +0100434
435 /* we cannot fail (again) if we already detached */
436 if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
437 rv = SS_IS_DISKLESS;
438
439 return rv;
440}
441
442/**
Philipp Reisnerb8907332011-01-27 14:07:51 +0100443 * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
444 * @mdev: DRBD device.
445 * @os: old state.
446 * @ns: new state.
447 * @warn_sync_abort:
448 *
449 * When we loose connection, we have to set the state of the peers disk (pdsk)
450 * to D_UNKNOWN. This rule and many more along those lines are in this function.
451 */
452static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state os,
453 union drbd_state ns, const char **warn_sync_abort)
454{
455 enum drbd_fencing_p fp;
456 enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
457
458 fp = FP_DONT_CARE;
459 if (get_ldev(mdev)) {
460 fp = mdev->ldev->dc.fencing;
461 put_ldev(mdev);
462 }
463
Philipp Reisnerb8907332011-01-27 14:07:51 +0100464 /* if we are only D_ATTACHING yet,
465 * we can (and should) go directly to D_DISKLESS. */
466 if (ns.disk == D_FAILED && os.disk == D_ATTACHING)
467 ns.disk = D_DISKLESS;
468
Philipp Reisner35095022011-02-09 16:29:33 +0100469 /* Implications from connection to peer and peer_isp */
Philipp Reisnerb8907332011-01-27 14:07:51 +0100470 if (ns.conn < C_CONNECTED) {
471 ns.peer_isp = 0;
472 ns.peer = R_UNKNOWN;
473 if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
474 ns.pdsk = D_UNKNOWN;
475 }
476
477 /* Clear the aftr_isp when becoming unconfigured */
478 if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
479 ns.aftr_isp = 0;
480
481 /* Abort resync if a disk fails/detaches */
482 if (os.conn > C_CONNECTED && ns.conn > C_CONNECTED &&
483 (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
484 if (warn_sync_abort)
485 *warn_sync_abort =
486 os.conn == C_VERIFY_S || os.conn == C_VERIFY_T ?
487 "Online-verify" : "Resync";
488 ns.conn = C_CONNECTED;
489 }
490
491 /* Connection breaks down before we finished "Negotiating" */
492 if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
493 get_ldev_if_state(mdev, D_NEGOTIATING)) {
494 if (mdev->ed_uuid == mdev->ldev->md.uuid[UI_CURRENT]) {
495 ns.disk = mdev->new_state_tmp.disk;
496 ns.pdsk = mdev->new_state_tmp.pdsk;
497 } else {
498 dev_alert(DEV, "Connection lost while negotiating, no data!\n");
499 ns.disk = D_DISKLESS;
500 ns.pdsk = D_UNKNOWN;
501 }
502 put_ldev(mdev);
503 }
504
505 /* D_CONSISTENT and D_OUTDATED vanish when we get connected */
506 if (ns.conn >= C_CONNECTED && ns.conn < C_AHEAD) {
507 if (ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED)
508 ns.disk = D_UP_TO_DATE;
509 if (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)
510 ns.pdsk = D_UP_TO_DATE;
511 }
512
513 /* Implications of the connection stat on the disk states */
514 disk_min = D_DISKLESS;
515 disk_max = D_UP_TO_DATE;
516 pdsk_min = D_INCONSISTENT;
517 pdsk_max = D_UNKNOWN;
518 switch ((enum drbd_conns)ns.conn) {
519 case C_WF_BITMAP_T:
520 case C_PAUSED_SYNC_T:
521 case C_STARTING_SYNC_T:
522 case C_WF_SYNC_UUID:
523 case C_BEHIND:
524 disk_min = D_INCONSISTENT;
525 disk_max = D_OUTDATED;
526 pdsk_min = D_UP_TO_DATE;
527 pdsk_max = D_UP_TO_DATE;
528 break;
529 case C_VERIFY_S:
530 case C_VERIFY_T:
531 disk_min = D_UP_TO_DATE;
532 disk_max = D_UP_TO_DATE;
533 pdsk_min = D_UP_TO_DATE;
534 pdsk_max = D_UP_TO_DATE;
535 break;
536 case C_CONNECTED:
537 disk_min = D_DISKLESS;
538 disk_max = D_UP_TO_DATE;
539 pdsk_min = D_DISKLESS;
540 pdsk_max = D_UP_TO_DATE;
541 break;
542 case C_WF_BITMAP_S:
543 case C_PAUSED_SYNC_S:
544 case C_STARTING_SYNC_S:
545 case C_AHEAD:
546 disk_min = D_UP_TO_DATE;
547 disk_max = D_UP_TO_DATE;
548 pdsk_min = D_INCONSISTENT;
549 pdsk_max = D_CONSISTENT; /* D_OUTDATED would be nice. But explicit outdate necessary*/
550 break;
551 case C_SYNC_TARGET:
552 disk_min = D_INCONSISTENT;
553 disk_max = D_INCONSISTENT;
554 pdsk_min = D_UP_TO_DATE;
555 pdsk_max = D_UP_TO_DATE;
556 break;
557 case C_SYNC_SOURCE:
558 disk_min = D_UP_TO_DATE;
559 disk_max = D_UP_TO_DATE;
560 pdsk_min = D_INCONSISTENT;
561 pdsk_max = D_INCONSISTENT;
562 break;
563 case C_STANDALONE:
564 case C_DISCONNECTING:
565 case C_UNCONNECTED:
566 case C_TIMEOUT:
567 case C_BROKEN_PIPE:
568 case C_NETWORK_FAILURE:
569 case C_PROTOCOL_ERROR:
570 case C_TEAR_DOWN:
571 case C_WF_CONNECTION:
572 case C_WF_REPORT_PARAMS:
573 case C_MASK:
574 break;
575 }
576 if (ns.disk > disk_max)
577 ns.disk = disk_max;
578
579 if (ns.disk < disk_min) {
580 dev_warn(DEV, "Implicitly set disk from %s to %s\n",
581 drbd_disk_str(ns.disk), drbd_disk_str(disk_min));
582 ns.disk = disk_min;
583 }
584 if (ns.pdsk > pdsk_max)
585 ns.pdsk = pdsk_max;
586
587 if (ns.pdsk < pdsk_min) {
588 dev_warn(DEV, "Implicitly set pdsk from %s to %s\n",
589 drbd_disk_str(ns.pdsk), drbd_disk_str(pdsk_min));
590 ns.pdsk = pdsk_min;
591 }
592
593 if (fp == FP_STONITH &&
594 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED) &&
595 !(os.role == R_PRIMARY && os.conn < C_CONNECTED && os.pdsk > D_OUTDATED))
596 ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
597
598 if (mdev->sync_conf.on_no_data == OND_SUSPEND_IO &&
599 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE) &&
600 !(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE))
601 ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
602
603 if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
604 if (ns.conn == C_SYNC_SOURCE)
605 ns.conn = C_PAUSED_SYNC_S;
606 if (ns.conn == C_SYNC_TARGET)
607 ns.conn = C_PAUSED_SYNC_T;
608 } else {
609 if (ns.conn == C_PAUSED_SYNC_S)
610 ns.conn = C_SYNC_SOURCE;
611 if (ns.conn == C_PAUSED_SYNC_T)
612 ns.conn = C_SYNC_TARGET;
613 }
614
615 return ns;
616}
617
618void drbd_resume_al(struct drbd_conf *mdev)
619{
620 if (test_and_clear_bit(AL_SUSPENDED, &mdev->flags))
621 dev_info(DEV, "Resumed AL updates\n");
622}
623
624/* helper for __drbd_set_state */
625static void set_ov_position(struct drbd_conf *mdev, enum drbd_conns cs)
626{
627 if (mdev->tconn->agreed_pro_version < 90)
628 mdev->ov_start_sector = 0;
629 mdev->rs_total = drbd_bm_bits(mdev);
630 mdev->ov_position = 0;
631 if (cs == C_VERIFY_T) {
632 /* starting online verify from an arbitrary position
633 * does not fit well into the existing protocol.
634 * on C_VERIFY_T, we initialize ov_left and friends
635 * implicitly in receive_DataRequest once the
636 * first P_OV_REQUEST is received */
637 mdev->ov_start_sector = ~(sector_t)0;
638 } else {
639 unsigned long bit = BM_SECT_TO_BIT(mdev->ov_start_sector);
640 if (bit >= mdev->rs_total) {
641 mdev->ov_start_sector =
642 BM_BIT_TO_SECT(mdev->rs_total - 1);
643 mdev->rs_total = 1;
644 } else
645 mdev->rs_total -= bit;
646 mdev->ov_position = mdev->ov_start_sector;
647 }
648 mdev->ov_left = mdev->rs_total;
649}
650
651/**
652 * __drbd_set_state() - Set a new DRBD state
653 * @mdev: DRBD device.
654 * @ns: new state.
655 * @flags: Flags
656 * @done: Optional completion, that will get completed after the after_state_ch() finished
657 *
658 * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
659 */
660enum drbd_state_rv
661__drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
662 enum chg_state_flags flags, struct completion *done)
663{
664 union drbd_state os;
665 enum drbd_state_rv rv = SS_SUCCESS;
666 const char *warn_sync_abort = NULL;
667 struct after_state_chg_work *ascw;
668
669 os = mdev->state;
670
671 ns = sanitize_state(mdev, os, ns, &warn_sync_abort);
672
673 if (ns.i == os.i)
674 return SS_NOTHING_TO_DO;
675
Philipp Reisner35095022011-02-09 16:29:33 +0100676 rv = is_valid_transition(os, ns);
677 if (rv < SS_SUCCESS)
678 return rv;
679
Philipp Reisnerb8907332011-01-27 14:07:51 +0100680 if (!(flags & CS_HARD)) {
681 /* pre-state-change checks ; only look at ns */
682 /* See drbd_state_sw_errors in drbd_strings.c */
683
684 rv = is_valid_state(mdev, ns);
685 if (rv < SS_SUCCESS) {
686 /* If the old state was illegal as well, then let
687 this happen...*/
688
689 if (is_valid_state(mdev, os) == rv)
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100690 rv = is_valid_soft_transition(os, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100691 } else
Philipp Reisnera75f34a2011-02-09 15:10:33 +0100692 rv = is_valid_soft_transition(os, ns);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100693 }
694
695 if (rv < SS_SUCCESS) {
696 if (flags & CS_VERBOSE)
697 print_st_err(mdev, os, ns, rv);
698 return rv;
699 }
700
701 if (warn_sync_abort)
702 dev_warn(DEV, "%s aborted.\n", warn_sync_abort);
703
704 {
705 char *pbp, pb[300];
706 pbp = pb;
707 *pbp = 0;
708 if (ns.role != os.role)
709 pbp += sprintf(pbp, "role( %s -> %s ) ",
710 drbd_role_str(os.role),
711 drbd_role_str(ns.role));
712 if (ns.peer != os.peer)
713 pbp += sprintf(pbp, "peer( %s -> %s ) ",
714 drbd_role_str(os.peer),
715 drbd_role_str(ns.peer));
716 if (ns.conn != os.conn)
717 pbp += sprintf(pbp, "conn( %s -> %s ) ",
718 drbd_conn_str(os.conn),
719 drbd_conn_str(ns.conn));
720 if (ns.disk != os.disk)
721 pbp += sprintf(pbp, "disk( %s -> %s ) ",
722 drbd_disk_str(os.disk),
723 drbd_disk_str(ns.disk));
724 if (ns.pdsk != os.pdsk)
725 pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
726 drbd_disk_str(os.pdsk),
727 drbd_disk_str(ns.pdsk));
728 if (is_susp(ns) != is_susp(os))
729 pbp += sprintf(pbp, "susp( %d -> %d ) ",
730 is_susp(os),
731 is_susp(ns));
732 if (ns.aftr_isp != os.aftr_isp)
733 pbp += sprintf(pbp, "aftr_isp( %d -> %d ) ",
734 os.aftr_isp,
735 ns.aftr_isp);
736 if (ns.peer_isp != os.peer_isp)
737 pbp += sprintf(pbp, "peer_isp( %d -> %d ) ",
738 os.peer_isp,
739 ns.peer_isp);
740 if (ns.user_isp != os.user_isp)
741 pbp += sprintf(pbp, "user_isp( %d -> %d ) ",
742 os.user_isp,
743 ns.user_isp);
744 dev_info(DEV, "%s\n", pb);
745 }
746
747 /* solve the race between becoming unconfigured,
748 * worker doing the cleanup, and
749 * admin reconfiguring us:
750 * on (re)configure, first set CONFIG_PENDING,
751 * then wait for a potentially exiting worker,
752 * start the worker, and schedule one no_op.
753 * then proceed with configuration.
754 */
755 if (ns.disk == D_DISKLESS &&
756 ns.conn == C_STANDALONE &&
757 ns.role == R_SECONDARY &&
758 !test_and_set_bit(CONFIG_PENDING, &mdev->flags))
759 set_bit(DEVICE_DYING, &mdev->flags);
760
761 /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
762 * on the ldev here, to be sure the transition -> D_DISKLESS resp.
763 * drbd_ldev_destroy() won't happen before our corresponding
764 * after_state_ch works run, where we put_ldev again. */
765 if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
766 (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
767 atomic_inc(&mdev->local_cnt);
768
769 mdev->state = ns;
770
771 if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
772 drbd_print_uuids(mdev, "attached to UUIDs");
773
774 wake_up(&mdev->misc_wait);
775 wake_up(&mdev->state_wait);
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100776 wake_up(&mdev->tconn->ping_wait);
Philipp Reisnerb8907332011-01-27 14:07:51 +0100777
778 /* aborted verify run. log the last position */
779 if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
780 ns.conn < C_CONNECTED) {
781 mdev->ov_start_sector =
782 BM_BIT_TO_SECT(drbd_bm_bits(mdev) - mdev->ov_left);
783 dev_info(DEV, "Online Verify reached sector %llu\n",
784 (unsigned long long)mdev->ov_start_sector);
785 }
786
787 if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
788 (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
789 dev_info(DEV, "Syncer continues.\n");
790 mdev->rs_paused += (long)jiffies
791 -(long)mdev->rs_mark_time[mdev->rs_last_mark];
792 if (ns.conn == C_SYNC_TARGET)
793 mod_timer(&mdev->resync_timer, jiffies);
794 }
795
796 if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
797 (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
798 dev_info(DEV, "Resync suspended\n");
799 mdev->rs_mark_time[mdev->rs_last_mark] = jiffies;
800 }
801
802 if (os.conn == C_CONNECTED &&
803 (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
804 unsigned long now = jiffies;
805 int i;
806
807 set_ov_position(mdev, ns.conn);
808 mdev->rs_start = now;
809 mdev->rs_last_events = 0;
810 mdev->rs_last_sect_ev = 0;
811 mdev->ov_last_oos_size = 0;
812 mdev->ov_last_oos_start = 0;
813
814 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
815 mdev->rs_mark_left[i] = mdev->ov_left;
816 mdev->rs_mark_time[i] = now;
817 }
818
819 drbd_rs_controller_reset(mdev);
820
821 if (ns.conn == C_VERIFY_S) {
822 dev_info(DEV, "Starting Online Verify from sector %llu\n",
823 (unsigned long long)mdev->ov_position);
824 mod_timer(&mdev->resync_timer, jiffies);
825 }
826 }
827
828 if (get_ldev(mdev)) {
829 u32 mdf = mdev->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
830 MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
831 MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
832
833 if (test_bit(CRASHED_PRIMARY, &mdev->flags))
834 mdf |= MDF_CRASHED_PRIMARY;
835 if (mdev->state.role == R_PRIMARY ||
836 (mdev->state.pdsk < D_INCONSISTENT && mdev->state.peer == R_PRIMARY))
837 mdf |= MDF_PRIMARY_IND;
838 if (mdev->state.conn > C_WF_REPORT_PARAMS)
839 mdf |= MDF_CONNECTED_IND;
840 if (mdev->state.disk > D_INCONSISTENT)
841 mdf |= MDF_CONSISTENT;
842 if (mdev->state.disk > D_OUTDATED)
843 mdf |= MDF_WAS_UP_TO_DATE;
844 if (mdev->state.pdsk <= D_OUTDATED && mdev->state.pdsk >= D_INCONSISTENT)
845 mdf |= MDF_PEER_OUT_DATED;
846 if (mdf != mdev->ldev->md.flags) {
847 mdev->ldev->md.flags = mdf;
848 drbd_md_mark_dirty(mdev);
849 }
850 if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
851 drbd_set_ed_uuid(mdev, mdev->ldev->md.uuid[UI_CURRENT]);
852 put_ldev(mdev);
853 }
854
855 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
856 if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
857 os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
858 set_bit(CONSIDER_RESYNC, &mdev->flags);
859
860 /* Receiver should clean up itself */
861 if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
862 drbd_thread_stop_nowait(&mdev->tconn->receiver);
863
864 /* Now the receiver finished cleaning up itself, it should die */
865 if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
866 drbd_thread_stop_nowait(&mdev->tconn->receiver);
867
868 /* Upon network failure, we need to restart the receiver. */
869 if (os.conn > C_TEAR_DOWN &&
870 ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
871 drbd_thread_restart_nowait(&mdev->tconn->receiver);
872
873 /* Resume AL writing if we get a connection */
874 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED)
875 drbd_resume_al(mdev);
876
877 ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
878 if (ascw) {
879 ascw->os = os;
880 ascw->ns = ns;
881 ascw->flags = flags;
882 ascw->w.cb = w_after_state_ch;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100883 ascw->w.mdev = mdev;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100884 ascw->done = done;
885 drbd_queue_work(&mdev->tconn->data.work, &ascw->w);
886 } else {
887 dev_warn(DEV, "Could not kmalloc an ascw\n");
888 }
889
890 return rv;
891}
892
Philipp Reisner00d56942011-02-09 18:09:48 +0100893static int w_after_state_ch(struct drbd_work *w, int unused)
Philipp Reisnerb8907332011-01-27 14:07:51 +0100894{
895 struct after_state_chg_work *ascw =
896 container_of(w, struct after_state_chg_work, w);
Philipp Reisner00d56942011-02-09 18:09:48 +0100897 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb8907332011-01-27 14:07:51 +0100898
899 after_state_ch(mdev, ascw->os, ascw->ns, ascw->flags);
900 if (ascw->flags & CS_WAIT_COMPLETE) {
901 D_ASSERT(ascw->done != NULL);
902 complete(ascw->done);
903 }
904 kfree(ascw);
905
906 return 1;
907}
908
909static void abw_start_sync(struct drbd_conf *mdev, int rv)
910{
911 if (rv) {
912 dev_err(DEV, "Writing the bitmap failed not starting resync.\n");
913 _drbd_request_state(mdev, NS(conn, C_CONNECTED), CS_VERBOSE);
914 return;
915 }
916
917 switch (mdev->state.conn) {
918 case C_STARTING_SYNC_T:
919 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
920 break;
921 case C_STARTING_SYNC_S:
922 drbd_start_resync(mdev, C_SYNC_SOURCE);
923 break;
924 }
925}
926
927int drbd_bitmap_io_from_worker(struct drbd_conf *mdev,
928 int (*io_fn)(struct drbd_conf *),
929 char *why, enum bm_flag flags)
930{
931 int rv;
932
933 D_ASSERT(current == mdev->tconn->worker.task);
934
935 /* open coded non-blocking drbd_suspend_io(mdev); */
936 set_bit(SUSPEND_IO, &mdev->flags);
937
938 drbd_bm_lock(mdev, why, flags);
939 rv = io_fn(mdev);
940 drbd_bm_unlock(mdev);
941
942 drbd_resume_io(mdev);
943
944 return rv;
945}
946
947/**
948 * after_state_ch() - Perform after state change actions that may sleep
949 * @mdev: DRBD device.
950 * @os: old state.
951 * @ns: new state.
952 * @flags: Flags
953 */
954static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
955 union drbd_state ns, enum chg_state_flags flags)
956{
957 enum drbd_fencing_p fp;
958 enum drbd_req_event what = NOTHING;
959 union drbd_state nsm = (union drbd_state){ .i = -1 };
960
961 if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
962 clear_bit(CRASHED_PRIMARY, &mdev->flags);
963 if (mdev->p_uuid)
964 mdev->p_uuid[UI_FLAGS] &= ~((u64)2);
965 }
966
967 fp = FP_DONT_CARE;
968 if (get_ldev(mdev)) {
969 fp = mdev->ldev->dc.fencing;
970 put_ldev(mdev);
971 }
972
973 /* Inform userspace about the change... */
974 drbd_bcast_state(mdev, ns);
975
976 if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
977 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
978 drbd_khelper(mdev, "pri-on-incon-degr");
979
980 /* Here we have the actions that are performed after a
981 state change. This function might sleep */
982
983 nsm.i = -1;
984 if (ns.susp_nod) {
985 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED)
986 what = RESEND;
987
988 if (os.disk == D_ATTACHING && ns.disk > D_ATTACHING)
989 what = RESTART_FROZEN_DISK_IO;
990
991 if (what != NOTHING)
992 nsm.susp_nod = 0;
993 }
994
995 if (ns.susp_fen) {
996 /* case1: The outdate peer handler is successful: */
997 if (os.pdsk > D_OUTDATED && ns.pdsk <= D_OUTDATED) {
998 tl_clear(mdev);
999 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
1000 drbd_uuid_new_current(mdev);
1001 clear_bit(NEW_CUR_UUID, &mdev->flags);
1002 }
1003 spin_lock_irq(&mdev->tconn->req_lock);
1004 _drbd_set_state(_NS(mdev, susp_fen, 0), CS_VERBOSE, NULL);
1005 spin_unlock_irq(&mdev->tconn->req_lock);
1006 }
1007 /* case2: The connection was established again: */
1008 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
1009 clear_bit(NEW_CUR_UUID, &mdev->flags);
1010 what = RESEND;
1011 nsm.susp_fen = 0;
1012 }
1013 }
1014
1015 if (what != NOTHING) {
1016 spin_lock_irq(&mdev->tconn->req_lock);
1017 _tl_restart(mdev, what);
1018 nsm.i &= mdev->state.i;
1019 _drbd_set_state(mdev, nsm, CS_VERBOSE, NULL);
1020 spin_unlock_irq(&mdev->tconn->req_lock);
1021 }
1022
1023 /* Became sync source. With protocol >= 96, we still need to send out
1024 * the sync uuid now. Need to do that before any drbd_send_state, or
1025 * the other side may go "paused sync" before receiving the sync uuids,
1026 * which is unexpected. */
1027 if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
1028 (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
1029 mdev->tconn->agreed_pro_version >= 96 && get_ldev(mdev)) {
1030 drbd_gen_and_send_sync_uuid(mdev);
1031 put_ldev(mdev);
1032 }
1033
1034 /* Do not change the order of the if above and the two below... */
1035 if (os.pdsk == D_DISKLESS && ns.pdsk > D_DISKLESS) { /* attach on the peer */
1036 drbd_send_uuids(mdev);
1037 drbd_send_state(mdev);
1038 }
1039 /* No point in queuing send_bitmap if we don't have a connection
1040 * anymore, so check also the _current_ state, not only the new state
1041 * at the time this work was queued. */
1042 if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
1043 mdev->state.conn == C_WF_BITMAP_S)
1044 drbd_queue_bitmap_io(mdev, &drbd_send_bitmap, NULL,
1045 "send_bitmap (WFBitMapS)",
1046 BM_LOCKED_TEST_ALLOWED);
1047
1048 /* Lost contact to peer's copy of the data */
1049 if ((os.pdsk >= D_INCONSISTENT &&
1050 os.pdsk != D_UNKNOWN &&
1051 os.pdsk != D_OUTDATED)
1052 && (ns.pdsk < D_INCONSISTENT ||
1053 ns.pdsk == D_UNKNOWN ||
1054 ns.pdsk == D_OUTDATED)) {
1055 if (get_ldev(mdev)) {
1056 if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
1057 mdev->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
1058 if (is_susp(mdev->state)) {
1059 set_bit(NEW_CUR_UUID, &mdev->flags);
1060 } else {
1061 drbd_uuid_new_current(mdev);
1062 drbd_send_uuids(mdev);
1063 }
1064 }
1065 put_ldev(mdev);
1066 }
1067 }
1068
1069 if (ns.pdsk < D_INCONSISTENT && get_ldev(mdev)) {
1070 if (ns.peer == R_PRIMARY && mdev->ldev->md.uuid[UI_BITMAP] == 0) {
1071 drbd_uuid_new_current(mdev);
1072 drbd_send_uuids(mdev);
1073 }
1074
1075 /* D_DISKLESS Peer becomes secondary */
1076 if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
1077 /* We may still be Primary ourselves.
1078 * No harm done if the bitmap still changes,
1079 * redirtied pages will follow later. */
1080 drbd_bitmap_io_from_worker(mdev, &drbd_bm_write,
1081 "demote diskless peer", BM_LOCKED_SET_ALLOWED);
1082 put_ldev(mdev);
1083 }
1084
1085 /* Write out all changed bits on demote.
1086 * Though, no need to da that just yet
1087 * if there is a resync going on still */
1088 if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
1089 mdev->state.conn <= C_CONNECTED && get_ldev(mdev)) {
1090 /* No changes to the bitmap expected this time, so assert that,
1091 * even though no harm was done if it did change. */
1092 drbd_bitmap_io_from_worker(mdev, &drbd_bm_write,
1093 "demote", BM_LOCKED_TEST_ALLOWED);
1094 put_ldev(mdev);
1095 }
1096
1097 /* Last part of the attaching process ... */
1098 if (ns.conn >= C_CONNECTED &&
1099 os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
1100 drbd_send_sizes(mdev, 0, 0); /* to start sync... */
1101 drbd_send_uuids(mdev);
1102 drbd_send_state(mdev);
1103 }
1104
1105 /* We want to pause/continue resync, tell peer. */
1106 if (ns.conn >= C_CONNECTED &&
1107 ((os.aftr_isp != ns.aftr_isp) ||
1108 (os.user_isp != ns.user_isp)))
1109 drbd_send_state(mdev);
1110
1111 /* In case one of the isp bits got set, suspend other devices. */
1112 if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
1113 (ns.aftr_isp || ns.peer_isp || ns.user_isp))
1114 suspend_other_sg(mdev);
1115
1116 /* Make sure the peer gets informed about eventual state
1117 changes (ISP bits) while we were in WFReportParams. */
1118 if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
1119 drbd_send_state(mdev);
1120
1121 if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
1122 drbd_send_state(mdev);
1123
1124 /* We are in the progress to start a full sync... */
1125 if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
1126 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
1127 /* no other bitmap changes expected during this phase */
1128 drbd_queue_bitmap_io(mdev,
1129 &drbd_bmio_set_n_write, &abw_start_sync,
1130 "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
1131
1132 /* We are invalidating our self... */
1133 if (os.conn < C_CONNECTED && ns.conn < C_CONNECTED &&
1134 os.disk > D_INCONSISTENT && ns.disk == D_INCONSISTENT)
1135 /* other bitmap operation expected during this phase */
1136 drbd_queue_bitmap_io(mdev, &drbd_bmio_set_n_write, NULL,
1137 "set_n_write from invalidate", BM_LOCKED_MASK);
1138
1139 /* first half of local IO error, failure to attach,
1140 * or administrative detach */
1141 if (os.disk != D_FAILED && ns.disk == D_FAILED) {
1142 enum drbd_io_error_p eh;
1143 int was_io_error;
1144 /* corresponding get_ldev was in __drbd_set_state, to serialize
1145 * our cleanup here with the transition to D_DISKLESS,
1146 * so it is safe to dreference ldev here. */
1147 eh = mdev->ldev->dc.on_io_error;
1148 was_io_error = test_and_clear_bit(WAS_IO_ERROR, &mdev->flags);
1149
1150 /* current state still has to be D_FAILED,
1151 * there is only one way out: to D_DISKLESS,
1152 * and that may only happen after our put_ldev below. */
1153 if (mdev->state.disk != D_FAILED)
1154 dev_err(DEV,
1155 "ASSERT FAILED: disk is %s during detach\n",
1156 drbd_disk_str(mdev->state.disk));
1157
1158 if (drbd_send_state(mdev))
1159 dev_warn(DEV, "Notified peer that I am detaching my disk\n");
1160 else
1161 dev_err(DEV, "Sending state for detaching disk failed\n");
1162
1163 drbd_rs_cancel_all(mdev);
1164
1165 /* In case we want to get something to stable storage still,
1166 * this may be the last chance.
1167 * Following put_ldev may transition to D_DISKLESS. */
1168 drbd_md_sync(mdev);
1169 put_ldev(mdev);
1170
1171 if (was_io_error && eh == EP_CALL_HELPER)
1172 drbd_khelper(mdev, "local-io-error");
1173 }
1174
1175 /* second half of local IO error, failure to attach,
1176 * or administrative detach,
1177 * after local_cnt references have reached zero again */
1178 if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
1179 /* We must still be diskless,
1180 * re-attach has to be serialized with this! */
1181 if (mdev->state.disk != D_DISKLESS)
1182 dev_err(DEV,
1183 "ASSERT FAILED: disk is %s while going diskless\n",
1184 drbd_disk_str(mdev->state.disk));
1185
1186 mdev->rs_total = 0;
1187 mdev->rs_failed = 0;
1188 atomic_set(&mdev->rs_pending_cnt, 0);
1189
1190 if (drbd_send_state(mdev))
1191 dev_warn(DEV, "Notified peer that I'm now diskless.\n");
1192 /* corresponding get_ldev in __drbd_set_state
1193 * this may finally trigger drbd_ldev_destroy. */
1194 put_ldev(mdev);
1195 }
1196
1197 /* Notify peer that I had a local IO error, and did not detached.. */
1198 if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT)
1199 drbd_send_state(mdev);
1200
1201 /* Disks got bigger while they were detached */
1202 if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
1203 test_and_clear_bit(RESYNC_AFTER_NEG, &mdev->flags)) {
1204 if (ns.conn == C_CONNECTED)
1205 resync_after_online_grow(mdev);
1206 }
1207
1208 /* A resync finished or aborted, wake paused devices... */
1209 if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
1210 (os.peer_isp && !ns.peer_isp) ||
1211 (os.user_isp && !ns.user_isp))
1212 resume_next_sg(mdev);
1213
1214 /* sync target done with resync. Explicitly notify peer, even though
1215 * it should (at least for non-empty resyncs) already know itself. */
1216 if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
1217 drbd_send_state(mdev);
1218
1219 /* This triggers bitmap writeout of potentially still unwritten pages
1220 * if the resync finished cleanly, or aborted because of peer disk
1221 * failure, or because of connection loss.
1222 * For resync aborted because of local disk failure, we cannot do
1223 * any bitmap writeout anymore.
1224 * No harm done if some bits change during this phase.
1225 */
1226 if (os.conn > C_CONNECTED && ns.conn <= C_CONNECTED && get_ldev(mdev)) {
1227 drbd_queue_bitmap_io(mdev, &drbd_bm_write, NULL,
1228 "write from resync_finished", BM_LOCKED_SET_ALLOWED);
1229 put_ldev(mdev);
1230 }
1231
1232 if (ns.disk == D_DISKLESS &&
1233 ns.conn == C_STANDALONE &&
1234 ns.role == R_SECONDARY) {
1235 if (os.aftr_isp != ns.aftr_isp)
1236 resume_next_sg(mdev);
1237 }
1238
1239 after_conn_state_ch(mdev->tconn, os, ns, flags);
1240 drbd_md_sync(mdev);
1241}
1242
1243static void after_conn_state_ch(struct drbd_tconn *tconn, union drbd_state os,
1244 union drbd_state ns, enum chg_state_flags flags)
1245{
1246 /* Upon network configuration, we need to start the receiver */
1247 if (os.conn == C_STANDALONE && ns.conn == C_UNCONNECTED)
1248 drbd_thread_start(&tconn->receiver);
1249
1250 if (ns.disk == D_DISKLESS &&
1251 ns.conn == C_STANDALONE &&
1252 ns.role == R_SECONDARY) {
1253 /* if (test_bit(DEVICE_DYING, &mdev->flags)) TODO: DEVICE_DYING functionality */
1254 drbd_thread_stop_nowait(&tconn->worker);
1255 }
1256}