This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Manage.c
1760 lines (1642 loc) · 45.8 KB
/
Manage.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
/*
* mdadm - manage Linux "md" devices aka RAID arrays.
*
* Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Neil Brown
* Email: <neilb@suse.de>
*/
#include "mdadm.h"
#include "md_u.h"
#include "md_p.h"
#include <ctype.h>
int Manage_ro(char *devname, int fd, int readonly)
{
/* switch to readonly or rw
*
* requires >= 0.90.0
* first check that array is runing
* use RESTART_ARRAY_RW or STOP_ARRAY_RO
*
*/
struct mdinfo *mdi;
int rv = 0;
/* If this is an externally-managed array, we need to modify the
* metadata_version so that mdmon doesn't undo our change.
*/
mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION);
if (mdi &&
mdi->array.major_version == -1 &&
is_subarray(mdi->text_version)) {
char vers[64];
strcpy(vers, "external:");
strcat(vers, mdi->text_version);
if (readonly > 0) {
int rv;
/* We set readonly ourselves. */
vers[9] = '-';
sysfs_set_str(mdi, NULL, "metadata_version", vers);
close(fd);
rv = sysfs_set_str(mdi, NULL, "array_state", "readonly");
if (rv < 0) {
pr_err("failed to set readonly for %s: %s\n",
devname, strerror(errno));
vers[9] = mdi->text_version[0];
sysfs_set_str(mdi, NULL, "metadata_version", vers);
rv = 1;
goto out;
}
} else {
char *cp;
/* We cannot set read/write - must signal mdmon */
vers[9] = '/';
sysfs_set_str(mdi, NULL, "metadata_version", vers);
cp = strchr(vers+10, '/');
if (cp)
*cp = 0;
ping_monitor(vers+10);
if (mdi->array.level <= 0)
sysfs_set_str(mdi, NULL, "array_state", "active");
}
goto out;
}
if (!md_array_active(fd)) {
pr_err("%s does not appear to be active.\n", devname);
rv = 1;
goto out;
}
if (readonly > 0) {
if (ioctl(fd, STOP_ARRAY_RO, NULL)) {
pr_err("failed to set readonly for %s: %s\n",
devname, strerror(errno));
rv = 1;
goto out;
}
} else if (readonly < 0) {
if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
pr_err("failed to set writable for %s: %s\n",
devname, strerror(errno));
rv = 1;
goto out;
}
}
out:
sysfs_free(mdi);
return rv;
}
static void remove_devices(char *devnm, char *path)
{
/*
* Remove names at 'path' - possibly with
* partition suffixes - which link to the 'standard'
* name for devnm. These were probably created
* by mdadm when the array was assembled.
*/
char base[40];
char *path2;
char link[1024];
int n;
int part;
char *be;
char *pe;
if (!path)
return;
sprintf(base, "/dev/%s", devnm);
be = base + strlen(base);
path2 = xmalloc(strlen(path)+20);
strcpy(path2, path);
pe = path2 + strlen(path2);
for (part = 0; part < 16; part++) {
if (part) {
sprintf(be, "p%d", part);
if (isdigit(pe[-1]))
sprintf(pe, "p%d", part);
else
sprintf(pe, "%d", part);
}
n = readlink(path2, link, sizeof(link));
if (n > 0 && (int)strlen(base) == n &&
strncmp(link, base, n) == 0)
unlink(path2);
}
free(path2);
}
int Manage_run(char *devname, int fd, struct context *c)
{
/* Run the array. Array must already be configured
* Requires >= 0.90.0
*/
char nm[32], *nmp;
nmp = fd2devnm(fd);
if (!nmp) {
pr_err("Cannot find %s in sysfs!!\n", devname);
return 1;
}
strcpy(nm, nmp);
return IncrementalScan(c, nm);
}
int Manage_stop(char *devname, int fd, int verbose, int will_retry)
{
/* Stop the array. Array must already be configured
* 'will_retry' means that error messages are not wanted.
*/
int rv = 0;
struct map_ent *map = NULL;
struct mdinfo *mdi;
char devnm[32];
char container[32];
int err;
int count;
char buf[32];
unsigned long long rd1, rd2;
if (will_retry && verbose == 0)
verbose = -1;
strcpy(devnm, fd2devnm(fd));
/* Get EXCL access first. If this fails, then attempting
* to stop is probably a bad idea.
*/
mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_COMPONENT|GET_VERSION);
if (mdi && is_subarray(mdi->text_version)) {
char *sl;
strncpy(container, mdi->text_version+1, sizeof(container));
container[sizeof(container)-1] = 0;
sl = strchr(container, '/');
if (sl)
*sl = 0;
} else
container[0] = 0;
close(fd);
count = 5;
while (((fd = ((devname[0] == '/')
?open(devname, O_RDONLY|O_EXCL)
:open_dev_flags(devnm, O_RDONLY|O_EXCL))) < 0 ||
strcmp(fd2devnm(fd), devnm) != 0) && container[0] &&
mdmon_running(container) && count) {
/* Can't open, so something might be wrong. However it
* is a container, so we might be racing with mdmon, so
* retry for a bit.
*/
if (fd >= 0)
close(fd);
flush_mdmon(container);
count--;
}
if (fd < 0 || strcmp(fd2devnm(fd), devnm) != 0) {
if (fd >= 0)
close(fd);
if (verbose >= 0)
pr_err("Cannot get exclusive access to %s:Perhaps a running process, mounted filesystem or active volume group?\n",
devname);
return 1;
}
/* If this is an mdmon managed array, just write 'inactive'
* to the array state and let mdmon clear up.
*/
if (mdi &&
mdi->array.level > 0 &&
is_subarray(mdi->text_version)) {
int err;
/* This is mdmon managed. */
close(fd);
/* As we had an O_EXCL open, any use of the device
* which blocks STOP_ARRAY is probably a transient use,
* so it is reasonable to retry for a while - 5 seconds.
*/
count = 25;
while (count &&
(err = sysfs_set_str(mdi, NULL,
"array_state",
"inactive")) < 0 &&
errno == EBUSY) {
usleep(200000);
count--;
}
if (err) {
if (verbose >= 0)
pr_err("failed to stop array %s: %s\n",
devname, strerror(errno));
rv = 1;
goto out;
}
/* Give monitor a chance to act */
ping_monitor(mdi->text_version);
fd = open_dev_excl(devnm);
if (fd < 0) {
if (verbose >= 0)
pr_err("failed to completely stop %s: Device is busy\n",
devname);
rv = 1;
goto out;
}
} else if (mdi &&
mdi->array.major_version == -1 &&
mdi->array.minor_version == -2 &&
!is_subarray(mdi->text_version)) {
struct mdstat_ent *mds, *m;
/* container, possibly mdmon-managed.
* Make sure mdmon isn't opening it, which
* would interfere with the 'stop'
*/
ping_monitor(mdi->sys_name);
/* now check that there are no existing arrays
* which are members of this array
*/
mds = mdstat_read(0, 0);
for (m = mds; m; m = m->next)
if (m->metadata_version &&
strncmp(m->metadata_version, "external:", 9)==0 &&
metadata_container_matches(m->metadata_version+9,
devnm)) {
if (verbose >= 0)
pr_err("Cannot stop container %s: member %s still active\n",
devname, m->devnm);
free_mdstat(mds);
rv = 1;
goto out;
}
}
/* If the array is undergoing a reshape which changes the number
* of devices, then it would be nice to stop it at a point where
* it has completed a full number of stripes in both old and
* new layouts as this will allow the reshape to be reverted.
* So if 'sync_action' is "reshape" and 'raid_disks' shows two
* different numbers, then
* - freeze reshape
* - set sync_max to next multiple of both data_disks and
* chunk sizes (or next but one)
* - unfreeze reshape
* - wait on 'sync_completed' for that point to be reached.
*/
if (mdi && (mdi->array.level >= 4 && mdi->array.level <= 6) &&
sysfs_attribute_available(mdi, NULL, "sync_action") &&
sysfs_attribute_available(mdi, NULL, "reshape_direction") &&
sysfs_get_str(mdi, NULL, "sync_action", buf, 20) > 0 &&
strcmp(buf, "reshape\n") == 0 &&
sysfs_get_two(mdi, NULL, "raid_disks", &rd1, &rd2) == 2) {
unsigned long long position, curr;
unsigned long long chunk1, chunk2;
unsigned long long rddiv, chunkdiv;
unsigned long long sectors;
unsigned long long sync_max, old_sync_max;
unsigned long long completed;
int backwards = 0;
int delay;
int scfd;
delay = 40;
while (rd1 > rd2 && delay > 0 &&
sysfs_get_ll(mdi, NULL, "sync_max", &old_sync_max) == 0) {
/* must be in the critical section - wait a bit */
delay -= 1;
usleep(100000);
}
if (sysfs_set_str(mdi, NULL, "sync_action", "frozen") != 0)
goto done;
/* Array is frozen */
rd1 -= mdi->array.level == 6 ? 2 : 1;
rd2 -= mdi->array.level == 6 ? 2 : 1;
sysfs_get_str(mdi, NULL, "reshape_direction", buf, sizeof(buf));
if (strncmp(buf, "back", 4) == 0)
backwards = 1;
if (sysfs_get_ll(mdi, NULL, "reshape_position", &position) != 0) {
/* reshape must have finished now */
sysfs_set_str(mdi, NULL, "sync_action", "idle");
goto done;
}
sysfs_get_two(mdi, NULL, "chunk_size", &chunk1, &chunk2);
chunk1 /= 512;
chunk2 /= 512;
rddiv = GCD(rd1, rd2);
chunkdiv = GCD(chunk1, chunk2);
sectors = (chunk1/chunkdiv) * chunk2 * (rd1/rddiv) * rd2;
if (backwards) {
/* Need to subtract 'reshape_position' from
* array size to get equivalent of sync_max.
* Size calculation based on raid5_size in kernel.
*/
unsigned long long size = mdi->component_size;
size &= ~(chunk1-1);
size &= ~(chunk2-1);
/* rd1 must be smaller */
/* Reshape may have progressed further backwards than
* recorded, so target even further back (hence "-1")
*/
position = (position / sectors - 1) * sectors;
/* rd1 is always the conversion factor between 'sync'
* position and 'reshape' position.
* We read 1 "new" stripe worth of data from where-ever,
* and when write out that full stripe.
*/
sync_max = size - position/rd1;
} else {
/* Reshape will very likely be beyond position, and it may
* be too late to stop at '+1', so aim for '+2'
*/
position = (position / sectors + 2) * sectors;
sync_max = position/rd1;
}
if (sysfs_get_ll(mdi, NULL, "sync_max", &old_sync_max) < 0)
old_sync_max = mdi->component_size;
/* Must not advance sync_max as that could confuse
* the reshape monitor */
if (sync_max < old_sync_max)
sysfs_set_num(mdi, NULL, "sync_max", sync_max);
sysfs_set_str(mdi, NULL, "sync_action", "idle");
/* That should have set things going again. Now we
* wait a little while (3 second max) for sync_completed
* to reach the target.
* The reshape process can block for 500msec if
* the sync speed limit is hit, so we need to wait
* a lot longer than that. 1 second is usually
* enough. 3 is safe.
*/
delay = 3000;
scfd = sysfs_open(mdi->sys_name, NULL, "sync_completed");
while (scfd >= 0 && delay > 0 && old_sync_max > 0) {
unsigned long long max_completed;
sysfs_get_ll(mdi, NULL, "reshape_position", &curr);
sysfs_fd_get_str(scfd, buf, sizeof(buf));
if (strncmp(buf, "none", 4) == 0) {
/* Either reshape has aborted, or hasn't
* quite started yet. Wait a bit and
* check 'sync_action' to see.
*/
usleep(10000);
sysfs_get_str(mdi, NULL, "sync_action", buf, sizeof(buf));
if (strncmp(buf, "reshape", 7) != 0)
break;
}
if (sysfs_fd_get_two(scfd, &completed,
&max_completed) == 2 &&
/* 'completed' sometimes reads as max-uulong */
completed < max_completed &&
(completed > sync_max ||
(completed == sync_max && curr != position))) {
while (completed > sync_max) {
sync_max += sectors / rd1;
if (backwards)
position -= sectors;
else
position += sectors;
}
if (sync_max < old_sync_max)
sysfs_set_num(mdi, NULL, "sync_max", sync_max);
}
if (!backwards && curr >= position)
break;
if (backwards && curr <= position)
break;
sysfs_wait(scfd, &delay);
}
if (scfd >= 0)
close(scfd);
}
done:
/* As we have an O_EXCL open, any use of the device
* which blocks STOP_ARRAY is probably a transient use,
* so it is reasonable to retry for a while - 5 seconds.
*/
count = 25; err = 0;
while (count && fd >= 0 &&
(err = ioctl(fd, STOP_ARRAY, NULL)) < 0 && errno == EBUSY) {
usleep(200000);
count --;
}
if (fd >= 0 && err) {
if (verbose >= 0) {
pr_err("failed to stop array %s: %s\n",
devname, strerror(errno));
if (errno == EBUSY)
cont_err("Perhaps a running process, mounted filesystem or active volume group?\n");
}
rv = 1;
goto out;
}
if (get_linux_version() < 2006028) {
/* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
* was stopped, so We'll do it here just to be sure. Drop any
* partitions as well...
*/
if (fd >= 0)
ioctl(fd, BLKRRPART, 0);
if (mdi)
sysfs_uevent(mdi, "change");
}
if (devnm[0] && use_udev()) {
struct map_ent *mp = map_by_devnm(&map, devnm);
remove_devices(devnm, mp ? mp->path : NULL);
}
if (verbose >= 0)
pr_err("stopped %s\n", devname);
map_lock(&map);
map_remove(&map, devnm);
map_unlock(&map);
out:
sysfs_free(mdi);
return rv;
}
static struct mddev_dev *add_one(struct mddev_dev *dv, char *name, char disp)
{
struct mddev_dev *new;
new = xmalloc(sizeof(*new));
memset(new, 0, sizeof(*new));
new->devname = xstrdup(name);
new->disposition = disp;
new->next = dv->next;
dv->next = new;
return new;
}
static void add_faulty(struct mddev_dev *dv, int fd, char disp)
{
mdu_array_info_t array;
mdu_disk_info_t disk;
int remaining_disks;
int i;
if (md_get_array_info(fd, &array) != 0)
return;
remaining_disks = array.nr_disks;
for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
char buf[40];
disk.number = i;
if (md_get_disk_info(fd, &disk) != 0)
continue;
if (disk.major == 0 && disk.minor == 0)
continue;
remaining_disks--;
if ((disk.state & 1) == 0) /* not faulty */
continue;
sprintf(buf, "%d:%d", disk.major, disk.minor);
dv = add_one(dv, buf, disp);
}
}
static void add_detached(struct mddev_dev *dv, int fd, char disp)
{
mdu_array_info_t array;
mdu_disk_info_t disk;
int remaining_disks;
int i;
if (md_get_array_info(fd, &array) != 0)
return;
remaining_disks = array.nr_disks;
for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
char buf[40];
int sfd;
disk.number = i;
if (md_get_disk_info(fd, &disk) != 0)
continue;
if (disk.major == 0 && disk.minor == 0)
continue;
remaining_disks--;
if (disp == 'f' && (disk.state & 1) != 0) /* already faulty */
continue;
sprintf(buf, "%d:%d", disk.major, disk.minor);
sfd = dev_open(buf, O_RDONLY);
if (sfd >= 0) {
/* Not detached */
close(sfd);
continue;
}
if (errno != ENXIO)
/* Probably not detached */
continue;
dv = add_one(dv, buf, disp);
}
}
static void add_set(struct mddev_dev *dv, int fd, char set_char)
{
mdu_array_info_t array;
mdu_disk_info_t disk;
int remaining_disks;
int copies, set;
int i;
if (md_get_array_info(fd, &array) != 0)
return;
if (array.level != 10)
return;
copies = ((array.layout & 0xff) *
((array.layout >> 8) & 0xff));
if (array.raid_disks % copies)
return;
remaining_disks = array.nr_disks;
for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
char buf[40];
disk.number = i;
if (md_get_disk_info(fd, &disk) != 0)
continue;
if (disk.major == 0 && disk.minor == 0)
continue;
remaining_disks--;
set = disk.raid_disk % copies;
if (set_char != set + 'A')
continue;
sprintf(buf, "%d:%d", disk.major, disk.minor);
dv = add_one(dv, buf, dv->disposition);
}
}
int attempt_re_add(int fd, int tfd, struct mddev_dev *dv,
struct supertype *dev_st, struct supertype *tst,
unsigned long rdev,
char *update, char *devname, int verbose,
mdu_array_info_t *array)
{
struct mdinfo mdi;
int duuid[4];
int ouuid[4];
dev_st->ss->getinfo_super(dev_st, &mdi, NULL);
dev_st->ss->uuid_from_super(dev_st, ouuid);
if (tst->sb)
tst->ss->uuid_from_super(tst, duuid);
else
/* Assume uuid matches: kernel will check */
memcpy(duuid, ouuid, sizeof(ouuid));
if ((mdi.disk.state & (1<<MD_DISK_ACTIVE)) &&
!(mdi.disk.state & (1<<MD_DISK_FAULTY)) &&
memcmp(duuid, ouuid, sizeof(ouuid))==0) {
/* Looks like it is worth a
* try. Need to make sure
* kernel will accept it
* though.
*/
mdu_disk_info_t disc;
/* re-add doesn't work for version-1 superblocks
* before 2.6.18 :-(
*/
if (array->major_version == 1 &&
get_linux_version() <= 2006018)
goto skip_re_add;
disc.number = mdi.disk.number;
if (md_get_disk_info(fd, &disc) != 0 ||
disc.major != 0 || disc.minor != 0)
goto skip_re_add;
disc.major = major(rdev);
disc.minor = minor(rdev);
disc.number = mdi.disk.number;
disc.raid_disk = mdi.disk.raid_disk;
disc.state = mdi.disk.state;
if (array->state & (1 << MD_SB_CLUSTERED)) {
/* extra flags are needed when adding to a cluster as
* there are two cases to distinguish
*/
if (dv->disposition == 'c')
disc.state |= (1 << MD_DISK_CANDIDATE);
else
disc.state |= (1 << MD_DISK_CLUSTER_ADD);
}
if (dv->writemostly == FlagSet)
disc.state |= 1 << MD_DISK_WRITEMOSTLY;
if (dv->writemostly == FlagClear)
disc.state &= ~(1 << MD_DISK_WRITEMOSTLY);
if (dv->failfast == FlagSet)
disc.state |= 1 << MD_DISK_FAILFAST;
if (dv->failfast == FlagClear)
disc.state &= ~(1 << MD_DISK_FAILFAST);
remove_partitions(tfd);
if (update || dv->writemostly != FlagDefault ||
dv->failfast != FlagDefault) {
int rv = -1;
tfd = dev_open(dv->devname, O_RDWR);
if (tfd < 0) {
pr_err("failed to open %s for superblock update during re-add\n", dv->devname);
return -1;
}
if (dv->writemostly == FlagSet)
rv = dev_st->ss->update_super(
dev_st, NULL, "writemostly",
devname, verbose, 0, NULL);
if (dv->writemostly == FlagClear)
rv = dev_st->ss->update_super(
dev_st, NULL, "readwrite",
devname, verbose, 0, NULL);
if (dv->failfast == FlagSet)
rv = dev_st->ss->update_super(
dev_st, NULL, "failfast",
devname, verbose, 0, NULL);
if (dv->failfast == FlagClear)
rv = dev_st->ss->update_super(
dev_st, NULL, "nofailfast",
devname, verbose, 0, NULL);
if (update)
rv = dev_st->ss->update_super(
dev_st, NULL, update,
devname, verbose, 0, NULL);
if (rv == 0)
rv = dev_st->ss->store_super(dev_st, tfd);
close(tfd);
if (rv != 0) {
pr_err("failed to update superblock during re-add\n");
return -1;
}
}
/* don't even try if disk is marked as faulty */
errno = 0;
if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
if (verbose >= 0)
pr_err("re-added %s\n", dv->devname);
return 1;
}
if (errno == ENOMEM || errno == EROFS) {
pr_err("add new device failed for %s: %s\n",
dv->devname, strerror(errno));
if (dv->disposition == 'M')
return 0;
return -1;
}
}
skip_re_add:
return 0;
}
int Manage_add(int fd, int tfd, struct mddev_dev *dv,
struct supertype *tst, mdu_array_info_t *array,
int force, int verbose, char *devname,
char *update, unsigned long rdev, unsigned long long array_size,
int raid_slot)
{
unsigned long long ldsize;
struct supertype *dev_st;
int j;
mdu_disk_info_t disc;
if (!get_dev_size(tfd, dv->devname, &ldsize)) {
if (dv->disposition == 'M')
return 0;
else
return -1;
}
if (tst->ss == &super0 && ldsize > 4ULL*1024*1024*1024*1024) {
/* More than 4TB is wasted on v0.90 */
if (!force) {
pr_err("%s is larger than %s can effectively use.\n"
" Add --force is you really want to add this device.\n",
dv->devname, devname);
return -1;
}
pr_err("%s is larger than %s can effectively use.\n"
" Adding anyway as --force was given.\n",
dv->devname, devname);
}
if (array->not_persistent == 0 || tst->ss->external) {
/* need to find a sample superblock to copy, and
* a spare slot to use.
* For 'external' array (well, container based),
* We can just load the metadata for the array->
*/
int array_failed;
if (tst->sb)
/* already loaded */;
else if (tst->ss->external) {
tst->ss->load_container(tst, fd, NULL);
} else for (j = 0; j < tst->max_devs; j++) {
char *dev;
int dfd;
disc.number = j;
if (md_get_disk_info(fd, &disc))
continue;
if (disc.major==0 && disc.minor==0)
continue;
if ((disc.state & 4)==0) /* sync */
continue;
/* Looks like a good device to try */
dev = map_dev(disc.major, disc.minor, 1);
if (!dev)
continue;
dfd = dev_open(dev, O_RDONLY);
if (dfd < 0)
continue;
if (tst->ss->load_super(tst, dfd,
NULL)) {
close(dfd);
continue;
}
close(dfd);
break;
}
/* FIXME this is a bad test to be using */
if (!tst->sb && (dv->disposition != 'a' &&
dv->disposition != 'S')) {
/* we are re-adding a device to a
* completely dead array - have to depend
* on kernel to check
*/
} else if (!tst->sb) {
pr_err("cannot load array metadata from %s\n", devname);
return -1;
}
/* Make sure device is large enough */
if (dv->disposition != 'j' && /* skip size check for Journal */
tst->sb &&
tst->ss->avail_size(tst, ldsize/512, INVALID_SECTORS) <
array_size) {
if (dv->disposition == 'M')
return 0;
pr_err("%s not large enough to join array\n",
dv->devname);
return -1;
}
/* Possibly this device was recently part of
* the array and was temporarily removed, and
* is now being re-added. If so, we can
* simply re-add it.
*/
if (array->not_persistent == 0) {
dev_st = dup_super(tst);
dev_st->ss->load_super(dev_st, tfd, NULL);
if (dev_st->sb && dv->disposition != 'S') {
int rv;
rv = attempt_re_add(fd, tfd, dv, dev_st, tst,
rdev, update, devname,
verbose, array);
dev_st->ss->free_super(dev_st);
if (rv)
return rv;
}
}
if (dv->disposition == 'M') {
if (verbose > 0)
pr_err("--re-add for %s to %s is not possible\n",
dv->devname, devname);
return 0;
}
if (dv->disposition == 'A') {
pr_err("--re-add for %s to %s is not possible\n",
dv->devname, devname);
return -1;
}
if (array->active_disks < array->raid_disks) {
char *avail = xcalloc(array->raid_disks, 1);
int d;
int found = 0;
for (d = 0; d < MAX_DISKS && found < array->nr_disks; d++) {
disc.number = d;
if (md_get_disk_info(fd, &disc))
continue;
if (disc.major == 0 && disc.minor == 0)
continue;
if (!(disc.state & (1<<MD_DISK_SYNC)))
continue;
avail[disc.raid_disk] = 1;
found++;
}
array_failed = !enough(array->level, array->raid_disks,
array->layout, 1, avail);
free(avail);
} else
array_failed = 0;
if (array_failed) {
pr_err("%s has failed so using --add cannot work and might destroy\n",
devname);
pr_err("data on %s. You should stop the array and re-assemble it.\n",
dv->devname);
return -1;
}
} else {
/* non-persistent. Must ensure that new drive
* is at least array->size big.
*/
if (ldsize/512 < array_size) {
pr_err("%s not large enough to join array\n",
dv->devname);
return -1;
}
}
/* committed to really trying this device now*/
remove_partitions(tfd);
/* in 2.6.17 and earlier, version-1 superblocks won't
* use the number we write, but will choose a free number.
* we must choose the same free number, which requires
* starting at 'raid_disks' and counting up
*/
for (j = array->raid_disks; j < tst->max_devs; j++) {
disc.number = j;
if (md_get_disk_info(fd, &disc))
break;
if (disc.major==0 && disc.minor==0)
break;
if (disc.state & 8) /* removed */
break;
}
disc.major = major(rdev);
disc.minor = minor(rdev);
if (raid_slot < 0)
disc.number = j;
else
disc.number = raid_slot;
disc.state = 0;
/* only add journal to array that supports journaling */
if (dv->disposition == 'j') {
struct mdinfo *mdp;
mdp = sysfs_read(fd, NULL, GET_ARRAY_STATE);
if (!mdp) {
pr_err("%s unable to read array state.\n", devname);
return -1;
}
if (mdp->array_state != ARRAY_READONLY) {
sysfs_free(mdp);
pr_err("%s is not readonly, cannot add journal.\n", devname);
return -1;
}
sysfs_free(mdp);
disc.raid_disk = 0;
}
if (array->not_persistent==0) {
int dfd;
if (dv->disposition == 'j')
disc.state |= (1 << MD_DISK_JOURNAL) | (1 << MD_DISK_SYNC);
if (dv->writemostly == FlagSet)
disc.state |= 1 << MD_DISK_WRITEMOSTLY;
if (dv->failfast == FlagSet)
disc.state |= 1 << MD_DISK_FAILFAST;
dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
if (tst->ss->add_to_super(tst, &disc, dfd,
dv->devname, INVALID_SECTORS))
return -1;
if (tst->ss->write_init_super(tst))
return -1;
} else if (dv->disposition == 'A') {
/* this had better be raid1.
* As we are "--re-add"ing we must find a spare slot
* to fill.
*/
char *used = xcalloc(array->raid_disks, 1);
for (j = 0; j < tst->max_devs; j++) {
mdu_disk_info_t disc2;
disc2.number = j;
if (md_get_disk_info(fd, &disc2))
continue;
if (disc2.major==0 && disc2.minor==0)
continue;
if (disc2.state & 8) /* removed */
continue;
if (disc2.raid_disk < 0)
continue;
if (disc2.raid_disk > array->raid_disks)
continue;
used[disc2.raid_disk] = 1;
}
for (j = 0 ; j < array->raid_disks; j++)
if (!used[j]) {
disc.raid_disk = j;
disc.state |= (1<<MD_DISK_SYNC);
break;
}
free(used);
}
if (array->state & (1 << MD_SB_CLUSTERED)) {
if (dv->disposition == 'c')
disc.state |= (1 << MD_DISK_CANDIDATE);
else
disc.state |= (1 << MD_DISK_CLUSTER_ADD);
}
if (dv->writemostly == FlagSet)
disc.state |= (1 << MD_DISK_WRITEMOSTLY);
if (dv->failfast == FlagSet)
disc.state |= (1 << MD_DISK_FAILFAST);
if (tst->ss->external) {
/* add a disk
* to an external metadata container */
struct mdinfo new_mdi;
struct mdinfo *sra;
int container_fd;
char devnm[32];
int dfd;
strcpy(devnm, fd2devnm(fd));
container_fd = open_dev_excl(devnm);
if (container_fd < 0) {
pr_err("add failed for %s: could not get exclusive access to container\n",
dv->devname);
tst->ss->free_super(tst);
return -1;
}
Kill(dv->devname, NULL, 0, -1, 0);
dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
if (tst->ss->add_to_super(tst, &disc, dfd,
dv->devname, INVALID_SECTORS)) {
close(dfd);
close(container_fd);