-
Notifications
You must be signed in to change notification settings - Fork 92
/
drbd_state.c
5937 lines (5039 loc) · 196 KB
/
drbd_state.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-only
/*
drbd_state.c
This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
from Logicworks, Inc. for making SDP replication support possible.
*/
#include <linux/drbd_limits.h>
#include <linux/random.h>
#include <linux/jiffies.h>
#include "drbd_int.h"
#include "drbd_protocol.h"
#include "drbd_req.h"
#include "drbd_state_change.h"
struct after_state_change_work {
struct drbd_work w;
struct drbd_state_change *state_change;
struct completion *done;
};
struct quorum_info {
int up_to_date;
int present;
int voters;
int quorum_at;
int diskless_majority_at;
int min_redundancy_at;
};
struct quorum_detail {
int up_to_date;
int present;
int outdated;
int diskless;
int missing_diskless;
int quorumless;
int unknown;
int quorate_peers;
};
struct change_context {
struct drbd_resource *resource;
int vnr;
union drbd_state mask;
union drbd_state val;
int target_node_id;
enum chg_state_flags flags;
bool change_local_state_last;
const char **err_str;
};
enum change_phase {
PH_LOCAL_COMMIT,
PH_PREPARE,
PH_84_COMMIT,
PH_COMMIT,
};
struct change_disk_state_context {
struct change_context context;
struct drbd_device *device;
};
static bool lost_contact_to_peer_data(enum drbd_disk_state *peer_disk_state);
static bool peer_returns_diskless(struct drbd_peer_device *peer_device,
enum drbd_disk_state os, enum drbd_disk_state ns);
static void print_state_change(struct drbd_resource *resource, const char *prefix);
static void finish_state_change(struct drbd_resource *);
static int w_after_state_change(struct drbd_work *w, int unused);
static enum drbd_state_rv is_valid_soft_transition(struct drbd_resource *);
static enum drbd_state_rv is_valid_transition(struct drbd_resource *resource);
static void sanitize_state(struct drbd_resource *resource);
static void ensure_exposed_data_uuid(struct drbd_device *device);
static enum drbd_state_rv change_peer_state(struct drbd_connection *, int, union drbd_state,
union drbd_state, unsigned long *);
static void check_wrongly_set_mdf_exists(struct drbd_device *);
static void update_members(struct drbd_resource *resource);
/* We need to stay consistent if we are neighbor of a diskless primary with
different UUID. This function should be used if the device was D_UP_TO_DATE
before.
*/
static bool may_return_to_up_to_date(struct drbd_device *device, enum which_state which)
{
struct drbd_peer_device *peer_device;
bool rv = true;
rcu_read_lock();
for_each_peer_device_rcu(peer_device, device) {
if (peer_device->disk_state[which] == D_DISKLESS &&
peer_device->connection->peer_role[which] == R_PRIMARY &&
peer_device->current_uuid != drbd_current_uuid(device)) {
rv = false;
break;
}
}
rcu_read_unlock();
return rv;
}
/**
* may_be_up_to_date() - check if transition from D_CONSISTENT to D_UP_TO_DATE is allowed
*
* When fencing is enabled, it may only transition from D_CONSISTENT to D_UP_TO_DATE
* when ether all peers are connected, or outdated.
*/
static bool may_be_up_to_date(struct drbd_device *device, enum which_state which) __must_hold(local)
{
bool all_peers_outdated = true;
int node_id;
if (!may_return_to_up_to_date(device, which))
return false;
rcu_read_lock();
for (node_id = 0; node_id < DRBD_NODE_ID_MAX; node_id++) {
struct drbd_peer_md *peer_md = &device->ldev->md.peers[node_id];
struct drbd_peer_device *peer_device;
enum drbd_disk_state peer_disk_state;
bool want_bitmap = true;
if (node_id == device->ldev->md.node_id)
continue;
if (!(peer_md->flags & MDF_HAVE_BITMAP) && !(peer_md->flags & MDF_NODE_EXISTS))
continue;
if (!(peer_md->flags & MDF_PEER_FENCING))
continue;
peer_device = peer_device_by_node_id(device, node_id);
if (peer_device) {
struct peer_device_conf *pdc = rcu_dereference(peer_device->conf);
want_bitmap = pdc->bitmap;
peer_disk_state = peer_device->disk_state[NEW];
} else {
peer_disk_state = D_UNKNOWN;
}
switch (peer_disk_state) {
case D_DISKLESS:
if (!(peer_md->flags & MDF_PEER_DEVICE_SEEN))
continue;
fallthrough;
case D_ATTACHING:
case D_DETACHING:
case D_FAILED:
case D_NEGOTIATING:
case D_UNKNOWN:
if (!want_bitmap)
continue;
if ((peer_md->flags & MDF_PEER_OUTDATED))
continue;
break;
case D_INCONSISTENT:
case D_OUTDATED:
continue;
case D_CONSISTENT:
case D_UP_TO_DATE:
/* These states imply that there is a connection. If there is
a connection we do not need to insist that the peer was
outdated. */
continue;
case D_MASK: ;
}
all_peers_outdated = false;
}
rcu_read_unlock();
return all_peers_outdated;
}
static bool stable_up_to_date_neighbor(struct drbd_device *device)
{
struct drbd_peer_device *peer_device;
bool rv = false;
rcu_read_lock();
for_each_peer_device_rcu(peer_device, device) {
if (peer_device->disk_state[NEW] == D_UP_TO_DATE &&
peer_device->uuid_flags & UUID_FLAG_STABLE && /* primary is also stable */
peer_device->current_uuid == drbd_current_uuid(device)) {
rv = true;
break;
}
}
rcu_read_unlock();
return rv;
}
/**
* disk_state_from_md() - determine initial disk state
*
* When a disk is attached to a device, we set the disk state to D_NEGOTIATING.
* We then wait for all connected peers to send the peer disk state. Once that
* has happened, we can determine the actual disk state based on the peer disk
* states and the state of the disk itself.
*
* The initial disk state becomes D_UP_TO_DATE without fencing or when we know
* that all peers have been outdated, and D_CONSISTENT otherwise.
*
* The caller either needs to have a get_ldev() reference, or need to call
* this function only if disk_state[NOW] >= D_NEGOTIATING and holding the
* state_rwlock.
*/
enum drbd_disk_state disk_state_from_md(struct drbd_device *device) __must_hold(local)
{
enum drbd_disk_state disk_state;
if (!drbd_md_test_flag(device->ldev, MDF_CONSISTENT))
disk_state = D_INCONSISTENT;
else if (!drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
disk_state = D_OUTDATED;
else
disk_state = may_be_up_to_date(device, NOW) ? D_UP_TO_DATE : D_CONSISTENT;
return disk_state;
}
bool is_suspended_fen(struct drbd_resource *resource, enum which_state which)
{
struct drbd_connection *connection;
bool rv = false;
rcu_read_lock();
for_each_connection_rcu(connection, resource) {
if (connection->susp_fen[which]) {
rv = true;
break;
}
}
rcu_read_unlock();
return rv;
}
bool resource_is_suspended(struct drbd_resource *resource, enum which_state which)
{
bool rv = resource->susp_user[which] || resource->susp_nod[which] ||
resource->susp_quorum[which] || resource->susp_uuid[which];
if (rv)
return rv;
return is_suspended_fen(resource, which);
}
static void count_objects(struct drbd_resource *resource,
struct drbd_state_change_object_count *ocnt)
{
struct drbd_path *path;
struct drbd_device *device;
struct drbd_connection *connection;
int vnr;
lockdep_assert_held(&resource->state_rwlock);
ocnt->n_devices = 0;
ocnt->n_connections = 0;
ocnt->n_paths = 0;
idr_for_each_entry(&resource->devices, device, vnr)
ocnt->n_devices++;
for_each_connection(connection, resource) {
ocnt->n_connections++;
list_for_each_entry(path, &connection->transport.paths, list) {
ocnt->n_paths++;
}
}
}
static struct drbd_state_change *alloc_state_change(struct drbd_state_change_object_count *ocnt, gfp_t flags)
{
struct drbd_state_change *state_change;
unsigned int size;
size = sizeof(struct drbd_state_change) +
ocnt->n_devices * sizeof(struct drbd_device_state_change) +
ocnt->n_connections * sizeof(struct drbd_connection_state_change) +
ocnt->n_devices * ocnt->n_connections * sizeof(struct drbd_peer_device_state_change) +
ocnt->n_paths * sizeof(struct drbd_path_state);
state_change = kzalloc(size, flags);
if (!state_change)
return NULL;
state_change->n_connections = ocnt->n_connections;
state_change->n_devices = ocnt->n_devices;
state_change->n_paths = ocnt->n_paths;
state_change->devices = (void *)(state_change + 1);
state_change->connections = (void *)&state_change->devices[ocnt->n_devices];
state_change->peer_devices = (void *)&state_change->connections[ocnt->n_connections];
state_change->paths = (void*)&state_change->peer_devices[ocnt->n_devices*ocnt->n_connections];
return state_change;
}
struct drbd_state_change *remember_state_change(struct drbd_resource *resource, gfp_t gfp)
{
struct drbd_state_change *state_change;
struct drbd_device *device;
struct drbd_connection *connection;
struct drbd_state_change_object_count ocnt;
int vnr;
struct drbd_device_state_change *device_state_change;
struct drbd_peer_device_state_change *peer_device_state_change;
struct drbd_connection_state_change *connection_state_change;
struct drbd_path_state *path_state; /* yes, not a _change :-( */
lockdep_assert_held(&resource->state_rwlock);
count_objects(resource, &ocnt);
state_change = alloc_state_change(&ocnt, gfp);
if (!state_change)
return NULL;
kref_get(&resource->kref);
kref_debug_get(&resource->kref_debug, 5);
state_change->resource->resource = resource;
memcpy(state_change->resource->role,
resource->role, sizeof(resource->role));
memcpy(state_change->resource->susp,
resource->susp_user, sizeof(resource->susp_user));
memcpy(state_change->resource->susp_nod,
resource->susp_nod, sizeof(resource->susp_nod));
memcpy(state_change->resource->susp_uuid,
resource->susp_uuid, sizeof(resource->susp_uuid));
memcpy(state_change->resource->fail_io,
resource->fail_io, sizeof(resource->fail_io));
device_state_change = state_change->devices;
peer_device_state_change = state_change->peer_devices;
idr_for_each_entry(&resource->devices, device, vnr) {
struct drbd_peer_device *peer_device;
kref_get(&device->kref);
kref_debug_get(&device->kref_debug, 2);
device_state_change->device = device;
memcpy(device_state_change->disk_state,
device->disk_state, sizeof(device->disk_state));
memcpy(device_state_change->have_quorum,
device->have_quorum, sizeof(device->have_quorum));
/* The peer_devices for each device have to be enumerated in
the order of the connections. We may not use for_each_peer_device() here. */
for_each_connection(connection, resource) {
peer_device = conn_peer_device(connection, device->vnr);
peer_device_state_change->peer_device = peer_device;
memcpy(peer_device_state_change->disk_state,
peer_device->disk_state, sizeof(peer_device->disk_state));
memcpy(peer_device_state_change->repl_state,
peer_device->repl_state, sizeof(peer_device->repl_state));
memcpy(peer_device_state_change->resync_susp_user,
peer_device->resync_susp_user,
sizeof(peer_device->resync_susp_user));
memcpy(peer_device_state_change->resync_susp_peer,
peer_device->resync_susp_peer,
sizeof(peer_device->resync_susp_peer));
memcpy(peer_device_state_change->resync_susp_dependency,
peer_device->resync_susp_dependency,
sizeof(peer_device->resync_susp_dependency));
memcpy(peer_device_state_change->resync_susp_other_c,
peer_device->resync_susp_other_c,
sizeof(peer_device->resync_susp_other_c));
memcpy(peer_device_state_change->resync_active,
peer_device->resync_active,
sizeof(peer_device->resync_active));
peer_device_state_change++;
}
device_state_change++;
}
connection_state_change = state_change->connections;
path_state = state_change->paths;
for_each_connection(connection, resource) {
struct drbd_path *path;
kref_get(&connection->kref);
kref_debug_get(&connection->kref_debug, 7);
connection_state_change->connection = connection;
memcpy(connection_state_change->cstate,
connection->cstate, sizeof(connection->cstate));
memcpy(connection_state_change->peer_role,
connection->peer_role, sizeof(connection->peer_role));
memcpy(connection_state_change->susp_fen,
connection->susp_fen, sizeof(connection->susp_fen));
list_for_each_entry(path, &connection->transport.paths, list) {
/* Share the connection kref with above.
* Could also share the pointer, but would then need to
* remember an additional n_paths per connection
* count/offset (connection_state_change->n_paths++)
* to be able to associate the paths with its connection.
* So why not directly store the pointer here again. */
path_state->connection = connection;
kref_get(&path->kref);
path_state->path = path;
path_state->path_established = path->established;
path_state++;
}
connection_state_change++;
}
return state_change;
}
void copy_old_to_new_state_change(struct drbd_state_change *state_change)
{
struct drbd_resource_state_change *resource_state_change = &state_change->resource[0];
unsigned int n_device, n_connection, n_peer_device, n_peer_devices;
#define OLD_TO_NEW(x) \
(x[NEW] = x[OLD])
OLD_TO_NEW(resource_state_change->role);
OLD_TO_NEW(resource_state_change->susp);
OLD_TO_NEW(resource_state_change->susp_nod);
OLD_TO_NEW(resource_state_change->susp_uuid);
OLD_TO_NEW(resource_state_change->fail_io);
for (n_connection = 0; n_connection < state_change->n_connections; n_connection++) {
struct drbd_connection_state_change *connection_state_change =
&state_change->connections[n_connection];
OLD_TO_NEW(connection_state_change->peer_role);
OLD_TO_NEW(connection_state_change->cstate);
OLD_TO_NEW(connection_state_change->susp_fen);
}
for (n_device = 0; n_device < state_change->n_devices; n_device++) {
struct drbd_device_state_change *device_state_change =
&state_change->devices[n_device];
OLD_TO_NEW(device_state_change->disk_state);
OLD_TO_NEW(device_state_change->have_quorum);
}
n_peer_devices = state_change->n_devices * state_change->n_connections;
for (n_peer_device = 0; n_peer_device < n_peer_devices; n_peer_device++) {
struct drbd_peer_device_state_change *p =
&state_change->peer_devices[n_peer_device];
OLD_TO_NEW(p->disk_state);
OLD_TO_NEW(p->repl_state);
OLD_TO_NEW(p->resync_susp_user);
OLD_TO_NEW(p->resync_susp_peer);
OLD_TO_NEW(p->resync_susp_dependency);
OLD_TO_NEW(p->resync_susp_other_c);
OLD_TO_NEW(p->resync_active);
}
#undef OLD_TO_NEW
}
void forget_state_change(struct drbd_state_change *state_change)
{
unsigned int n;
if (!state_change)
return;
if (state_change->resource->resource) {
kref_debug_put(&state_change->resource->resource->kref_debug, 5);
kref_put(&state_change->resource->resource->kref, drbd_destroy_resource);
}
for (n = 0; n < state_change->n_devices; n++) {
struct drbd_device *device = state_change->devices[n].device;
if (device) {
kref_debug_put(&device->kref_debug, 2);
kref_put(&device->kref, drbd_destroy_device);
}
}
for (n = 0; n < state_change->n_connections; n++) {
struct drbd_connection *connection =
state_change->connections[n].connection;
if (connection) {
kref_debug_put(&connection->kref_debug, 7);
kref_put(&connection->kref, drbd_destroy_connection);
}
}
for (n = 0; n < state_change->n_paths; n++) {
struct drbd_path *path = state_change->paths[n].path;
if (path) {
kref_put(&path->kref, drbd_destroy_path);
}
}
kfree(state_change);
}
static bool state_has_changed(struct drbd_resource *resource)
{
struct drbd_connection *connection;
struct drbd_device *device;
int vnr;
if (resource->state_change_flags & CS_FORCE_RECALC)
return true;
if (resource->role[OLD] != resource->role[NEW] ||
resource->susp_user[OLD] != resource->susp_user[NEW] ||
resource->susp_nod[OLD] != resource->susp_nod[NEW] ||
resource->susp_quorum[OLD] != resource->susp_quorum[NEW] ||
resource->susp_uuid[OLD] != resource->susp_uuid[NEW] ||
resource->fail_io[OLD] != resource->fail_io[NEW])
return true;
for_each_connection(connection, resource) {
if (connection->cstate[OLD] != connection->cstate[NEW] ||
connection->peer_role[OLD] != connection->peer_role[NEW] ||
connection->susp_fen[OLD] != connection->susp_fen[NEW])
return true;
}
idr_for_each_entry(&resource->devices, device, vnr) {
struct drbd_peer_device *peer_device;
if (device->disk_state[OLD] != device->disk_state[NEW] ||
device->have_quorum[OLD] != device->have_quorum[NEW])
return true;
for_each_peer_device(peer_device, device) {
if (peer_device->disk_state[OLD] != peer_device->disk_state[NEW] ||
peer_device->repl_state[OLD] != peer_device->repl_state[NEW] ||
peer_device->resync_susp_user[OLD] !=
peer_device->resync_susp_user[NEW] ||
peer_device->resync_susp_peer[OLD] !=
peer_device->resync_susp_peer[NEW] ||
peer_device->resync_susp_dependency[OLD] !=
peer_device->resync_susp_dependency[NEW] ||
peer_device->resync_susp_other_c[OLD] !=
peer_device->resync_susp_other_c[NEW] ||
peer_device->resync_active[OLD] !=
peer_device->resync_active[NEW] ||
peer_device->uuid_flags & UUID_FLAG_GOT_STABLE)
return true;
}
}
return false;
}
static void ___begin_state_change(struct drbd_resource *resource)
{
struct drbd_connection *connection;
struct drbd_device *device;
int vnr;
resource->role[NEW] = resource->role[NOW];
resource->susp_user[NEW] = resource->susp_user[NOW];
resource->susp_nod[NEW] = resource->susp_nod[NOW];
resource->susp_quorum[NEW] = resource->susp_quorum[NOW];
resource->susp_uuid[NEW] = resource->susp_uuid[NOW];
resource->fail_io[NEW] = resource->fail_io[NOW];
for_each_connection_rcu(connection, resource) {
connection->cstate[NEW] = connection->cstate[NOW];
connection->peer_role[NEW] = connection->peer_role[NOW];
connection->susp_fen[NEW] = connection->susp_fen[NOW];
}
idr_for_each_entry(&resource->devices, device, vnr) {
struct drbd_peer_device *peer_device;
device->disk_state[NEW] = device->disk_state[NOW];
device->have_quorum[NEW] = device->have_quorum[NOW];
for_each_peer_device_rcu(peer_device, device) {
peer_device->disk_state[NEW] = peer_device->disk_state[NOW];
peer_device->repl_state[NEW] = peer_device->repl_state[NOW];
peer_device->resync_susp_user[NEW] =
peer_device->resync_susp_user[NOW];
peer_device->resync_susp_peer[NEW] =
peer_device->resync_susp_peer[NOW];
peer_device->resync_susp_dependency[NEW] =
peer_device->resync_susp_dependency[NOW];
peer_device->resync_susp_other_c[NEW] =
peer_device->resync_susp_other_c[NOW];
peer_device->resync_active[NEW] =
peer_device->resync_active[NOW];
}
}
}
static void __begin_state_change(struct drbd_resource *resource)
{
rcu_read_lock();
___begin_state_change(resource);
}
static enum drbd_state_rv try_state_change(struct drbd_resource *resource)
{
enum drbd_state_rv rv;
if (!state_has_changed(resource))
return SS_NOTHING_TO_DO;
sanitize_state(resource);
rv = is_valid_transition(resource);
if (rv >= SS_SUCCESS && !(resource->state_change_flags & CS_HARD))
rv = is_valid_soft_transition(resource);
return rv;
}
static void apply_update_to_exposed_data_uuid(struct drbd_resource *resource)
{
struct drbd_device *device;
int vnr;
idr_for_each_entry(&resource->devices, device, vnr) {
u64 nedu = device->next_exposed_data_uuid;
int changed = 0;
if (!nedu)
continue;
if (device->disk_state[NOW] < D_INCONSISTENT)
changed = drbd_set_exposed_data_uuid(device, nedu);
device->next_exposed_data_uuid = 0;
if (changed)
drbd_info(device, "Executing delayed exposed data uuid update: %016llX\n",
(unsigned long long)device->exposed_data_uuid);
else
drbd_info(device, "Canceling delayed exposed data uuid update\n");
}
}
void __clear_remote_state_change(struct drbd_resource *resource)
{
struct drbd_connection *connection, *tmp;
bool is_connect = resource->twopc_reply.is_connect;
int initiator_node_id = resource->twopc_reply.initiator_node_id;
resource->remote_state_change = false;
resource->twopc_reply.initiator_node_id = -1;
resource->twopc_reply.tid = 0;
list_for_each_entry_safe(connection, tmp, &resource->twopc_parents, twopc_parent_list) {
if (is_connect && connection->peer_node_id == initiator_node_id)
abort_connect(connection);
kref_debug_put(&connection->kref_debug, 9);
kref_put(&connection->kref, drbd_destroy_connection);
}
INIT_LIST_HEAD(&resource->twopc_parents);
wake_up(&resource->twopc_wait);
/* Do things that where postponed to after two-phase commits finished */
apply_update_to_exposed_data_uuid(resource);
}
static bool state_is_stable(struct drbd_device *device)
{
struct drbd_peer_device *peer_device;
bool stable = true;
/* DO NOT add a default clause, we want the compiler to warn us
* for any newly introduced state we may have forgotten to add here */
rcu_read_lock();
for_each_peer_device_rcu(peer_device, device) {
switch (peer_device->repl_state[NOW]) {
/* New io is only accepted when the peer device is unknown or there is
* a well-established connection. */
case L_OFF:
case L_ESTABLISHED:
case L_SYNC_SOURCE:
case L_SYNC_TARGET:
case L_VERIFY_S:
case L_VERIFY_T:
case L_PAUSED_SYNC_S:
case L_PAUSED_SYNC_T:
case L_AHEAD:
case L_BEHIND:
case L_STARTING_SYNC_S:
case L_STARTING_SYNC_T:
break;
/* Allow IO in BM exchange states with new protocols */
case L_WF_BITMAP_S:
if (peer_device->connection->agreed_pro_version < 96)
stable = false;
break;
/* no new io accepted in these states */
case L_WF_BITMAP_T:
case L_WF_SYNC_UUID:
stable = false;
break;
}
if (!stable)
break;
}
rcu_read_unlock();
switch (device->disk_state[NOW]) {
case D_DISKLESS:
case D_INCONSISTENT:
case D_OUTDATED:
case D_CONSISTENT:
case D_UP_TO_DATE:
case D_FAILED:
case D_DETACHING:
/* disk state is stable as well. */
break;
/* no new io accepted during transitional states */
case D_ATTACHING:
case D_NEGOTIATING:
case D_UNKNOWN:
case D_MASK:
stable = false;
}
return stable;
}
static bool drbd_state_change_is_connect(struct drbd_resource *resource)
{
struct drbd_connection *connection;
for_each_connection(connection, resource) {
if (connection->cstate[NOW] == C_CONNECTING &&
connection->cstate[NEW] == C_CONNECTED)
return true;
}
return false;
}
static struct after_state_change_work *alloc_after_state_change_work(struct drbd_resource *resource)
{
struct after_state_change_work *work;
lockdep_assert_held(&resource->state_rwlock);
work = kmalloc(sizeof(*work), GFP_ATOMIC);
if (work) {
work->state_change = remember_state_change(resource, GFP_ATOMIC);
if (!work->state_change) {
kfree(work);
work = NULL;
}
}
if (!work)
drbd_err(resource, "Could not allocate after state change work\n");
return work;
}
static void queue_after_state_change_work(struct drbd_resource *resource,
struct completion *done,
struct after_state_change_work *work)
{
if (work) {
work->w.cb = w_after_state_change;
work->done = done;
drbd_queue_work(&resource->work, &work->w);
} else if (done) {
complete(done);
}
}
static enum drbd_state_rv ___end_state_change(struct drbd_resource *resource, struct completion *done,
enum drbd_state_rv rv)
{
enum chg_state_flags flags = resource->state_change_flags;
struct drbd_connection *connection;
struct drbd_device *device;
bool is_connect;
unsigned int pro_ver;
int vnr;
bool all_devs_have_quorum = true;
struct after_state_change_work *work;
if (flags & CS_ABORT)
goto out;
if (rv >= SS_SUCCESS)
rv = try_state_change(resource);
if (rv < SS_SUCCESS) {
if (flags & CS_VERBOSE) {
drbd_err(resource, "State change failed: %s\n", drbd_set_st_err_str(rv));
print_state_change(resource, "Failed: ");
}
goto out;
}
if (flags & CS_PREPARE)
goto out;
finish_state_change(resource);
update_members(resource);
/* Check whether we are establishing a connection before applying the change. */
is_connect = drbd_state_change_is_connect(resource);
/* This remembers the state change, so call before applying the change. */
work = alloc_after_state_change_work(resource);
/* changes to local_cnt and device flags should be visible before
* changes to state, which again should be visible before anything else
* depending on that change happens. */
smp_wmb();
resource->role[NOW] = resource->role[NEW];
resource->susp_user[NOW] = resource->susp_user[NEW];
resource->susp_nod[NOW] = resource->susp_nod[NEW];
resource->susp_quorum[NOW] = resource->susp_quorum[NEW];
resource->susp_uuid[NOW] = resource->susp_uuid[NEW];
resource->fail_io[NOW] = resource->fail_io[NEW];
resource->cached_susp = resource_is_suspended(resource, NEW);
pro_ver = PRO_VERSION_MAX;
for_each_connection(connection, resource) {
connection->cstate[NOW] = connection->cstate[NEW];
connection->peer_role[NOW] = connection->peer_role[NEW];
connection->susp_fen[NOW] = connection->susp_fen[NEW];
pro_ver = min_t(unsigned int, pro_ver,
connection->agreed_pro_version);
wake_up(&connection->ee_wait);
}
resource->cached_min_aggreed_protocol_version = pro_ver;
idr_for_each_entry(&resource->devices, device, vnr) {
struct res_opts *o = &resource->res_opts;
struct drbd_peer_device *peer_device;
device->disk_state[NOW] = device->disk_state[NEW];
device->have_quorum[NOW] = device->have_quorum[NEW];
if (!device->have_quorum[NOW])
all_devs_have_quorum = false;
for_each_peer_device(peer_device, device) {
peer_device->disk_state[NOW] = peer_device->disk_state[NEW];
peer_device->repl_state[NOW] = peer_device->repl_state[NEW];
peer_device->resync_susp_user[NOW] =
peer_device->resync_susp_user[NEW];
peer_device->resync_susp_peer[NOW] =
peer_device->resync_susp_peer[NEW];
peer_device->resync_susp_dependency[NOW] =
peer_device->resync_susp_dependency[NEW];
peer_device->resync_susp_other_c[NOW] =
peer_device->resync_susp_other_c[NEW];
peer_device->resync_active[NOW] =
peer_device->resync_active[NEW];
}
device->cached_state_unstable = !state_is_stable(device);
device->cached_err_io =
(o->on_no_quorum == ONQ_IO_ERROR && !device->have_quorum[NOW]) ||
(o->on_no_data == OND_IO_ERROR && !drbd_data_accessible(device, NOW)) ||
resource->fail_io[NEW];
}
resource->cached_all_devices_have_quorum = all_devs_have_quorum;
smp_wmb(); /* Make the NEW_CUR_UUID bit visible after the state change! */
idr_for_each_entry(&resource->devices, device, vnr) {
struct drbd_peer_device *peer_device;
if (test_bit(__NEW_CUR_UUID, &device->flags)) {
clear_bit(__NEW_CUR_UUID, &device->flags);
set_bit(NEW_CUR_UUID, &device->flags);
}
wake_up(&device->al_wait);
wake_up(&device->misc_wait);
/* Due to the exclusivity of two-phase commits, there can only
* be one connection being established at once. Hence it is OK
* to release uuid_sem for all connections if the state change
* is establishing any connection. */
if (is_connect) {
for_each_peer_device(peer_device, device) {
if (test_and_clear_bit(HOLDING_UUID_READ_LOCK, &peer_device->flags))
up_read_non_owner(&device->uuid_sem);
}
}
}
wake_up_all(&resource->state_wait);
/* Call this after applying the state change from NEW to NOW. */
queue_after_state_change_work(resource, done, work);
out:
rcu_read_unlock();
if ((flags & CS_TWOPC) && !(flags & CS_PREPARE))
__clear_remote_state_change(resource);
resource->state_change_err_str = NULL;
return rv;
}
void state_change_lock(struct drbd_resource *resource, unsigned long *irq_flags, enum chg_state_flags flags)
{
if ((flags & CS_SERIALIZE) && !(flags & (CS_ALREADY_SERIALIZED | CS_PREPARED))) {
WARN_ONCE(current == resource->worker.task,
"worker should not initiate state changes with CS_SERIALIZE\n");
down(&resource->state_sem);
}
write_lock_irqsave(&resource->state_rwlock, *irq_flags);
resource->state_change_flags = flags;
}
static void __state_change_unlock(struct drbd_resource *resource, unsigned long *irq_flags, struct completion *done)
{
enum chg_state_flags flags = resource->state_change_flags;
resource->state_change_flags = 0;
write_unlock_irqrestore(&resource->state_rwlock, *irq_flags);
if (done && expect(resource, current != resource->worker.task))
wait_for_completion(done);
if ((flags & CS_SERIALIZE) && !(flags & (CS_ALREADY_SERIALIZED | CS_PREPARE)))
up(&resource->state_sem);
}
void state_change_unlock(struct drbd_resource *resource, unsigned long *irq_flags)
{
__state_change_unlock(resource, irq_flags, NULL);
}
/**
* abort_prepared_state_change
*
* Use when a remote state change request was prepared but neither committed
* nor aborted; the remote state change still "holds the state mutex".
*/
void abort_prepared_state_change(struct drbd_resource *resource)
{
up(&resource->state_sem);
}
void begin_state_change_locked(struct drbd_resource *resource, enum chg_state_flags flags)
{
BUG_ON(flags & (CS_SERIALIZE | CS_WAIT_COMPLETE | CS_PREPARE | CS_ABORT));
resource->state_change_flags = flags;
__begin_state_change(resource);
}
enum drbd_state_rv end_state_change_locked(struct drbd_resource *resource)
{
return ___end_state_change(resource, NULL, SS_SUCCESS);
}
void begin_state_change(struct drbd_resource *resource, unsigned long *irq_flags, enum chg_state_flags flags)
{
state_change_lock(resource, irq_flags, flags);
__begin_state_change(resource);
}
static enum drbd_state_rv __end_state_change(struct drbd_resource *resource,
unsigned long *irq_flags,
enum drbd_state_rv rv)
{
enum chg_state_flags flags = resource->state_change_flags;
struct completion __done, *done = NULL;
if ((flags & CS_WAIT_COMPLETE) && !(flags & (CS_PREPARE | CS_ABORT))) {
done = &__done;
init_completion(done);
}
rv = ___end_state_change(resource, done, rv);
__state_change_unlock(resource, irq_flags, rv >= SS_SUCCESS ? done : NULL);
return rv;
}
enum drbd_state_rv end_state_change(struct drbd_resource *resource, unsigned long *irq_flags)
{
return __end_state_change(resource, irq_flags, SS_SUCCESS);
}
void abort_state_change(struct drbd_resource *resource, unsigned long *irq_flags)
{
resource->state_change_flags &= ~CS_VERBOSE;
__end_state_change(resource, irq_flags, SS_UNKNOWN_ERROR);
}
void abort_state_change_locked(struct drbd_resource *resource)
{
resource->state_change_flags &= ~CS_VERBOSE;
___end_state_change(resource, NULL, SS_UNKNOWN_ERROR);
}
static void begin_remote_state_change(struct drbd_resource *resource, unsigned long *irq_flags)
{
rcu_read_unlock();
write_unlock_irqrestore(&resource->state_rwlock, *irq_flags);
}
static void __end_remote_state_change(struct drbd_resource *resource, enum chg_state_flags flags)