-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathrf_netbsdkintf.c
More file actions
4190 lines (3525 loc) · 105 KB
/
rf_netbsdkintf.c
File metadata and controls
4190 lines (3525 loc) · 105 KB
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
/* $NetBSD: rf_netbsdkintf.c,v 1.418 2025/01/08 08:25:36 andvar Exp $ */
/*-
* Copyright (c) 1996, 1997, 1998, 2008-2011 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Greg Oster; Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1988 University of Utah.
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* the Systems Programming Group of the University of Utah Computer
* Science Department.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from: Utah $Hdr: cd.c 1.6 90/11/28$
*
* @(#)cd.c 8.2 (Berkeley) 11/16/93
*/
/*
* Copyright (c) 1995 Carnegie-Mellon University.
* All rights reserved.
*
* Authors: Mark Holland, Jim Zelenka
*
* Permission to use, copy, modify and distribute this software and
* its documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
* FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie the
* rights to redistribute these changes.
*/
/***********************************************************
*
* rf_kintf.c -- the kernel interface routines for RAIDframe
*
***********************************************************/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rf_netbsdkintf.c,v 1.418 2025/01/08 08:25:36 andvar Exp $");
#ifdef _KERNEL_OPT
#include "opt_raid_autoconfig.h"
#include "opt_compat_netbsd32.h"
#endif
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/pool.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/disk.h>
#include <sys/device.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/systm.h>
#include <sys/vnode.h>
#include <sys/disklabel.h>
#include <sys/conf.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/reboot.h>
#include <sys/kauth.h>
#include <sys/module.h>
#include <sys/compat_stub.h>
#include <prop/proplib.h>
#include <dev/raidframe/raidframevar.h>
#include <dev/raidframe/raidframeio.h>
#include <dev/raidframe/rf_paritymap.h>
#include "rf_raid.h"
#include "rf_dag.h"
#include "rf_dagflags.h"
#include "rf_desc.h"
#include "rf_diskqueue.h"
#include "rf_etimer.h"
#include "rf_general.h"
#include "rf_kintf.h"
#include "rf_options.h"
#include "rf_driver.h"
#include "rf_parityscan.h"
#include "rf_threadstuff.h"
#include "ioconf.h"
#ifdef DEBUG
int rf_kdebug_level = 0;
#define db1_printf(a) if (rf_kdebug_level > 0) printf a
#else /* DEBUG */
#define db1_printf(a) { }
#endif /* DEBUG */
#define DEVICE_XNAME(dev) dev ? device_xname(dev) : "null"
#if (RF_INCLUDE_PARITY_DECLUSTERING_DS > 0)
static rf_declare_mutex2(rf_sparet_wait_mutex);
static rf_declare_cond2(rf_sparet_wait_cv);
static rf_declare_cond2(rf_sparet_resp_cv);
static RF_SparetWait_t *rf_sparet_wait_queue; /* requests to install a
* spare table */
static RF_SparetWait_t *rf_sparet_resp_queue; /* responses from
* installation process */
#endif
const int rf_b_pass = (B_PHYS|B_RAW|B_MEDIA_FLAGS);
MALLOC_DEFINE(M_RAIDFRAME, "RAIDframe", "RAIDframe structures");
/* prototypes */
static void KernelWakeupFunc(struct buf *);
static void InitBP(struct buf *, struct vnode *, unsigned,
dev_t, RF_SectorNum_t, RF_SectorCount_t, void *, void (*) (struct buf *),
void *, int);
static void raidinit(struct raid_softc *);
static int raiddoaccess(RF_Raid_t *raidPtr, struct buf *bp);
static int rf_get_component_caches(RF_Raid_t *raidPtr, int *);
static int raid_match(device_t, cfdata_t, void *);
static void raid_attach(device_t, device_t, void *);
static int raid_detach(device_t, int);
static int raidread_component_area(dev_t, struct vnode *, void *, size_t,
daddr_t, daddr_t);
static int raidwrite_component_area(dev_t, struct vnode *, void *, size_t,
daddr_t, daddr_t);
static int raidwrite_component_label(unsigned,
dev_t, struct vnode *, RF_ComponentLabel_t *);
static int raidread_component_label(unsigned,
dev_t, struct vnode *, RF_ComponentLabel_t *);
static int raid_diskstart(device_t, struct buf *bp);
static int raid_dumpblocks(device_t, void *, daddr_t, int);
static int raid_lastclose(device_t);
static dev_type_open(raidopen);
static dev_type_close(raidclose);
static dev_type_read(raidread);
static dev_type_write(raidwrite);
static dev_type_ioctl(raidioctl);
static dev_type_strategy(raidstrategy);
static dev_type_dump(raiddump);
static dev_type_size(raidsize);
const struct bdevsw raid_bdevsw = {
.d_open = raidopen,
.d_close = raidclose,
.d_strategy = raidstrategy,
.d_ioctl = raidioctl,
.d_dump = raiddump,
.d_psize = raidsize,
.d_discard = nodiscard,
.d_flag = D_DISK
};
const struct cdevsw raid_cdevsw = {
.d_open = raidopen,
.d_close = raidclose,
.d_read = raidread,
.d_write = raidwrite,
.d_ioctl = raidioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_DISK
};
static struct dkdriver rf_dkdriver = {
.d_open = raidopen,
.d_close = raidclose,
.d_strategy = raidstrategy,
.d_diskstart = raid_diskstart,
.d_dumpblocks = raid_dumpblocks,
.d_lastclose = raid_lastclose,
.d_minphys = minphys
};
#define raidunit(x) DISKUNIT(x)
#define raidsoftc(dev) (((struct raid_softc *)device_private(dev))->sc_r.softc)
extern struct cfdriver raid_cd;
CFATTACH_DECL3_NEW(raid, sizeof(struct raid_softc),
raid_match, raid_attach, raid_detach, NULL, NULL, NULL,
DVF_DETACH_SHUTDOWN);
/* Internal representation of a rf_recon_req */
struct rf_recon_req_internal {
RF_RowCol_t col;
RF_ReconReqFlags_t flags;
void *raidPtr;
};
/*
* Allow RAIDOUTSTANDING number of simultaneous IO's to this RAID device.
* Be aware that large numbers can allow the driver to consume a lot of
* kernel memory, especially on writes, and in degraded mode reads.
*
* For example: with a stripe width of 64 blocks (32k) and 5 disks,
* a single 64K write will typically require 64K for the old data,
* 64K for the old parity, and 64K for the new parity, for a total
* of 192K (if the parity buffer is not re-used immediately).
* Even it if is used immediately, that's still 128K, which when multiplied
* by say 10 requests, is 1280K, *on top* of the 640K of incoming data.
*
* Now in degraded mode, for example, a 64K read on the above setup may
* require data reconstruction, which will require *all* of the 4 remaining
* disks to participate -- 4 * 32K/disk == 128K again.
*/
#ifndef RAIDOUTSTANDING
#define RAIDOUTSTANDING 6
#endif
#define RAIDLABELDEV(dev) \
(MAKEDISKDEV(major((dev)), raidunit((dev)), RAW_PART))
/* declared here, and made public, for the benefit of KVM stuff.. */
static int raidlock(struct raid_softc *);
static void raidunlock(struct raid_softc *);
static int raid_detach_unlocked(struct raid_softc *);
static void rf_markalldirty(RF_Raid_t *);
static void rf_set_geometry(struct raid_softc *, RF_Raid_t *);
static void rf_ReconThread(struct rf_recon_req_internal *);
static void rf_RewriteParityThread(RF_Raid_t *raidPtr);
static void rf_ReconstructInPlaceThread(struct rf_recon_req_internal *);
static int rf_autoconfig(device_t);
static int rf_rescan(void);
static void rf_buildroothack(RF_ConfigSet_t *);
static RF_AutoConfig_t *rf_find_raid_components(void);
static RF_ConfigSet_t *rf_create_auto_sets(RF_AutoConfig_t *);
static int rf_does_it_fit(RF_ConfigSet_t *,RF_AutoConfig_t *);
static void rf_create_configuration(RF_AutoConfig_t *,RF_Config_t *, RF_Raid_t *);
static int rf_set_autoconfig(RF_Raid_t *, int);
static int rf_set_rootpartition(RF_Raid_t *, int);
static void rf_release_all_vps(RF_ConfigSet_t *);
static void rf_cleanup_config_set(RF_ConfigSet_t *);
static int rf_have_enough_components(RF_ConfigSet_t *);
static struct raid_softc *rf_auto_config_set(RF_ConfigSet_t *);
static void rf_fix_old_label_size(RF_ComponentLabel_t *, uint64_t);
/*
* Debugging, mostly. Set to 0 to not allow autoconfig to take place.
* Note that this is overridden by having RAID_AUTOCONFIG as an option
* in the kernel config file.
*/
#ifdef RAID_AUTOCONFIG
int raidautoconfig = 1;
#else
int raidautoconfig = 0;
#endif
static bool raidautoconfigdone = false;
struct pool rf_alloclist_pool; /* AllocList */
static LIST_HEAD(, raid_softc) raids = LIST_HEAD_INITIALIZER(raids);
static kmutex_t raid_lock;
static struct raid_softc *
raidcreate(int unit) {
struct raid_softc *sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
sc->sc_unit = unit;
cv_init(&sc->sc_cv, "raidunit");
mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
return sc;
}
static void
raiddestroy(struct raid_softc *sc) {
cv_destroy(&sc->sc_cv);
mutex_destroy(&sc->sc_mutex);
kmem_free(sc, sizeof(*sc));
}
static struct raid_softc *
raidget(int unit, bool create) {
struct raid_softc *sc;
if (unit < 0) {
#ifdef DIAGNOSTIC
panic("%s: unit %d!", __func__, unit);
#endif
return NULL;
}
mutex_enter(&raid_lock);
LIST_FOREACH(sc, &raids, sc_link) {
if (sc->sc_unit == unit) {
mutex_exit(&raid_lock);
return sc;
}
}
mutex_exit(&raid_lock);
if (!create)
return NULL;
sc = raidcreate(unit);
mutex_enter(&raid_lock);
LIST_INSERT_HEAD(&raids, sc, sc_link);
mutex_exit(&raid_lock);
return sc;
}
static void
raidput(struct raid_softc *sc) {
mutex_enter(&raid_lock);
LIST_REMOVE(sc, sc_link);
mutex_exit(&raid_lock);
raiddestroy(sc);
}
void
raidattach(int num)
{
/*
* Device attachment and associated initialization now occurs
* as part of the module initialization.
*/
}
static int
rf_autoconfig(device_t self)
{
RF_AutoConfig_t *ac_list;
RF_ConfigSet_t *config_sets;
if (!raidautoconfig || raidautoconfigdone == true)
return 0;
/* XXX This code can only be run once. */
raidautoconfigdone = true;
#ifdef __HAVE_CPU_BOOTCONF
/*
* 0. find the boot device if needed first so we can use it later
* this needs to be done before we autoconfigure any raid sets,
* because if we use wedges we are not going to be able to open
* the boot device later
*/
if (booted_device == NULL)
cpu_bootconf();
#endif
/* 1. locate all RAID components on the system */
aprint_debug("Searching for RAID components...\n");
ac_list = rf_find_raid_components();
/* 2. Sort them into their respective sets. */
config_sets = rf_create_auto_sets(ac_list);
/*
* 3. Evaluate each set and configure the valid ones.
* This gets done in rf_buildroothack().
*/
rf_buildroothack(config_sets);
return 1;
}
int
rf_inited(const struct raid_softc *rs) {
return (rs->sc_flags & RAIDF_INITED) != 0;
}
RF_Raid_t *
rf_get_raid(struct raid_softc *rs) {
return &rs->sc_r;
}
int
rf_get_unit(const struct raid_softc *rs) {
return rs->sc_unit;
}
static int
rf_containsboot(RF_Raid_t *r, device_t bdv) {
const char *bootname;
size_t len;
/* if bdv is NULL, the set can't contain it. exit early. */
if (bdv == NULL)
return 0;
bootname = device_xname(bdv);
len = strlen(bootname);
for (int col = 0; col < r->numCol; col++) {
const char *devname = r->Disks[col].devname;
devname += sizeof("/dev/") - 1;
if (strncmp(devname, "dk", 2) == 0) {
const char *parent =
dkwedge_get_parent_name(r->Disks[col].dev);
if (parent != NULL)
devname = parent;
}
if (strncmp(devname, bootname, len) == 0) {
struct raid_softc *sc = r->softc;
aprint_debug("raid%d includes boot device %s\n",
sc->sc_unit, devname);
return 1;
}
}
return 0;
}
static int
rf_rescan(void)
{
RF_AutoConfig_t *ac_list;
RF_ConfigSet_t *config_sets, *cset, *next_cset;
struct raid_softc *sc;
int raid_added;
ac_list = rf_find_raid_components();
config_sets = rf_create_auto_sets(ac_list);
raid_added = 1;
while (raid_added > 0) {
raid_added = 0;
cset = config_sets;
while (cset != NULL) {
next_cset = cset->next;
if (rf_have_enough_components(cset) &&
cset->ac->clabel->autoconfigure == 1) {
sc = rf_auto_config_set(cset);
if (sc != NULL) {
aprint_debug("raid%d: configured ok, rootable %d\n",
sc->sc_unit, cset->rootable);
/* We added one RAID set */
raid_added++;
} else {
/* The autoconfig didn't work :( */
aprint_debug("Autoconfig failed\n");
rf_release_all_vps(cset);
}
} else {
/* we're not autoconfiguring this set...
release the associated resources */
rf_release_all_vps(cset);
}
/* cleanup */
rf_cleanup_config_set(cset);
cset = next_cset;
}
if (raid_added > 0) {
/* We added at least one RAID set, so re-scan for recursive RAID */
ac_list = rf_find_raid_components();
config_sets = rf_create_auto_sets(ac_list);
}
}
return 0;
}
/*
* Example setup:
* dk1 at wd0: "raid@wd0", 171965 blocks at 32802, type: raidframe
* dk3 at wd1: "raid@wd1", 171965 blocks at 32802, type: raidframz
* raid1: Components: /dev/dk1 /dev/dk3
* dk4 at raid1: "empty@raid1", 8192 blocks at 34, type: msdos
* dk5 at raid1: "root@raid1", 163517 blocks at 8226, type: ffs
*
* If booted from wd0, booted_device will be
* disk wd0, startblk = 41092, nblks = 163517
*
* That is, dk5 with startblk computed from the beginning of wd0
* instead of beginning of raid1:
* 32802 + 64 (RF_PROTECTED_SECTORS) + 8226 = 41092
*
* In order to find the boot wedge, we must iterate on each component,
* find its offset from disk beginning, and look for the boot wedge with
* startblck adjusted.
*/
static device_t
rf_find_bootwedge(struct raid_softc *rsc)
{
RF_Raid_t *r = &rsc->sc_r;
const char *bootname;
size_t len;
device_t rdev = NULL;
if (booted_device == NULL)
goto out;
bootname = device_xname(booted_device);
len = strlen(bootname);
aprint_debug("%s: booted_device %s, startblk = %"PRId64", "
"nblks = %"PRId64"\n", __func__,
bootname, booted_startblk, booted_nblks);
for (int col = 0; col < r->numCol; col++) {
const char *devname = r->Disks[col].devname;
const char *parent;
struct disk *dk;
u_int nwedges;
struct dkwedge_info *dkwi;
struct dkwedge_list dkwl;
size_t dkwi_len;
int i;
devname += sizeof("/dev/") - 1;
if (strncmp(devname, "dk", 2) != 0)
continue;
parent = dkwedge_get_parent_name(r->Disks[col].dev);
if (parent == NULL) {
aprint_debug("%s: cannot find parent for "
"component /dev/%s", __func__, devname);
continue;
}
if (strncmp(parent, bootname, len) != 0)
continue;
aprint_debug("%s: looking up wedge %s in device %s\n",
__func__, devname, parent);
dk = disk_find(parent);
nwedges = dk->dk_nwedges;
dkwi_len = sizeof(*dkwi) * nwedges;
dkwi = RF_Malloc(dkwi_len);
dkwl.dkwl_buf = dkwi;
dkwl.dkwl_bufsize = dkwi_len;
dkwl.dkwl_nwedges = 0;
dkwl.dkwl_ncopied = 0;
if (dkwedge_list(dk, &dkwl, curlwp) == 0) {
daddr_t startblk;
for (i = 0; i < dkwl.dkwl_ncopied; i++) {
if (strcmp(dkwi[i].dkw_devname, devname) == 0)
break;
}
KASSERT(i < dkwl.dkwl_ncopied);
aprint_debug("%s: wedge %s, "
"startblk = %"PRId64", "
"nblks = %"PRId64"\n",
__func__,
dkwi[i].dkw_devname,
dkwi[i].dkw_offset,
dkwi[i].dkw_size);
startblk = booted_startblk
- dkwi[i].dkw_offset
- RF_PROTECTED_SECTORS;
aprint_debug("%s: looking for wedge in %s, "
"startblk = %"PRId64", "
"nblks = %"PRId64"\n",
__func__,
DEVICE_XNAME(rsc->sc_dksc.sc_dev),
startblk, booted_nblks);
rdev = dkwedge_find_partition(rsc->sc_dksc.sc_dev,
startblk,
booted_nblks);
if (rdev) {
aprint_debug("%s: root candidate wedge %s "
"shifted from %s\n", __func__,
device_xname(rdev),
dkwi[i].dkw_devname);
goto done;
} else {
aprint_debug("%s: not found\n", __func__);
}
}
aprint_debug("%s: nothing found for col %d\n", __func__, col);
done:
RF_Free(dkwi, dkwi_len);
}
out:
if (!rdev)
aprint_debug("%s: nothing found\n", __func__);
return rdev;
}
static void
rf_buildroothack(RF_ConfigSet_t *config_sets)
{
RF_AutoConfig_t *ac_list;
RF_ConfigSet_t *cset;
RF_ConfigSet_t *next_cset;
int num_root;
int raid_added;
struct raid_softc *sc, *rsc;
struct dk_softc *dksc = NULL; /* XXX gcc -Os: may be used uninit. */
sc = rsc = NULL;
num_root = 0;
raid_added = 1;
while (raid_added > 0) {
raid_added = 0;
cset = config_sets;
while (cset != NULL) {
next_cset = cset->next;
if (rf_have_enough_components(cset) &&
cset->ac->clabel->autoconfigure == 1) {
sc = rf_auto_config_set(cset);
if (sc != NULL) {
aprint_debug("raid%d: configured ok, rootable %d\n",
sc->sc_unit, cset->rootable);
/* We added one RAID set */
raid_added++;
if (cset->rootable) {
rsc = sc;
num_root++;
}
} else {
/* The autoconfig didn't work :( */
aprint_debug("Autoconfig failed\n");
rf_release_all_vps(cset);
}
} else {
/* we're not autoconfiguring this set...
release the associated resources */
rf_release_all_vps(cset);
}
/* cleanup */
rf_cleanup_config_set(cset);
cset = next_cset;
}
if (raid_added > 0) {
/* We added at least one RAID set, so re-scan for recursive RAID */
ac_list = rf_find_raid_components();
config_sets = rf_create_auto_sets(ac_list);
}
}
/* if the user has specified what the root device should be
then we don't touch booted_device or boothowto... */
if (rootspec != NULL) {
aprint_debug("%s: rootspec %s\n", __func__, rootspec);
return;
}
/* we found something bootable... */
if (num_root == 1) {
device_t candidate_root = NULL;
dksc = &rsc->sc_dksc;
if (dksc->sc_dkdev.dk_nwedges != 0) {
/* Find the wedge we booted from */
candidate_root = rf_find_bootwedge(rsc);
/* Try first partition */
if (candidate_root == NULL) {
size_t i = 0;
candidate_root = dkwedge_find_by_parent(
device_xname(dksc->sc_dev), &i);
}
aprint_debug("%s: candidate wedge root %s\n",
__func__, DEVICE_XNAME(candidate_root));
} else {
candidate_root = dksc->sc_dev;
}
aprint_debug("%s: candidate root = %s, booted_device = %s, "
"root_partition = %d, contains_boot=%d\n",
__func__, DEVICE_XNAME(candidate_root),
DEVICE_XNAME(booted_device), rsc->sc_r.root_partition,
rf_containsboot(&rsc->sc_r, booted_device));
/* XXX the check for booted_device == NULL can probably be
* dropped, now that rf_containsboot handles that case.
*/
if (booted_device == NULL ||
rsc->sc_r.root_partition == 1 ||
rf_containsboot(&rsc->sc_r, booted_device)) {
booted_device = candidate_root;
booted_method = "raidframe/single";
booted_partition = 0; /* XXX assume 'a' */
aprint_debug("%s: set booted_device = %s\n", __func__,
DEVICE_XNAME(booted_device));
}
} else if (num_root > 1) {
aprint_debug("%s: many roots=%d, %s\n", __func__, num_root,
DEVICE_XNAME(booted_device));
/*
* Maybe the MD code can help. If it cannot, then
* setroot() will discover that we have no
* booted_device and will ask the user if nothing was
* hardwired in the kernel config file
*/
if (booted_device == NULL)
return;
num_root = 0;
mutex_enter(&raid_lock);
LIST_FOREACH(sc, &raids, sc_link) {
RF_Raid_t *r = &sc->sc_r;
if (r->valid == 0)
continue;
if (r->root_partition == 0)
continue;
if (rf_containsboot(r, booted_device)) {
num_root++;
rsc = sc;
dksc = &rsc->sc_dksc;
}
}
mutex_exit(&raid_lock);
if (num_root == 1) {
booted_device = dksc->sc_dev;
booted_method = "raidframe/multi";
booted_partition = 0; /* XXX assume 'a' */
} else {
/* we can't guess.. require the user to answer... */
boothowto |= RB_ASKNAME;
}
}
}
static int
raidsize(dev_t dev)
{
struct raid_softc *rs;
struct dk_softc *dksc;
unsigned int unit;
unit = raidunit(dev);
if ((rs = raidget(unit, false)) == NULL)
return -1;
dksc = &rs->sc_dksc;
if ((rs->sc_flags & RAIDF_INITED) == 0)
return -1;
return dk_size(dksc, dev);
}
static int
raiddump(dev_t dev, daddr_t blkno, void *va, size_t size)
{
unsigned int unit;
struct raid_softc *rs;
struct dk_softc *dksc;
unit = raidunit(dev);
if ((rs = raidget(unit, false)) == NULL)
return ENXIO;
dksc = &rs->sc_dksc;
if ((rs->sc_flags & RAIDF_INITED) == 0)
return ENODEV;
/*
Note that blkno is relative to this particular partition.
By adding adding RF_PROTECTED_SECTORS, we get a value that
is relative to the partition used for the underlying component.
*/
blkno += RF_PROTECTED_SECTORS;
return dk_dump(dksc, dev, blkno, va, size, DK_DUMP_RECURSIVE);
}
static int
raid_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk)
{
struct raid_softc *rs = raidsoftc(dev);
const struct bdevsw *bdev;
RF_Raid_t *raidPtr;
int c, sparecol, j, scol, dumpto;
int error = 0;
raidPtr = &rs->sc_r;
/* we only support dumping to RAID 1 sets */
if (raidPtr->Layout.numDataCol != 1 ||
raidPtr->Layout.numParityCol != 1)
return EINVAL;
if ((error = raidlock(rs)) != 0)
return error;
/* figure out what device is alive.. */
/*
Look for a component to dump to. The preference for the
component to dump to is as follows:
1) the first component
2) a used_spare of the first component
3) the second component
4) a used_spare of the second component
*/
dumpto = -1;
for (c = 0; c < raidPtr->numCol; c++) {
if (raidPtr->Disks[c].status == rf_ds_optimal) {
/* this might be the one */
dumpto = c;
break;
}
}
/*
At this point we have possibly selected a live component.
If we didn't find a live component, we now check to see
if there is a relevant spared component.
*/
for (c = 0; c < raidPtr->numSpare; c++) {
sparecol = raidPtr->numCol + c;
if (raidPtr->Disks[sparecol].status == rf_ds_used_spare) {
/* How about this one? */
scol = -1;
for(j=0;j<raidPtr->numCol;j++) {
if (raidPtr->Disks[j].spareCol == sparecol) {
scol = j;
break;
}
}
if (scol == 0) {
/*
We must have found a spared first
component! We'll take that over
anything else found so far. (We
couldn't have found a real first
component before, since this is a
used spare, and it's saying that
it's replacing the first
component.) On reboot (with
autoconfiguration turned on)
sparecol will become the first
component (component0) of this set.
*/
dumpto = sparecol;
break;
} else if (scol != -1) {
/*
Must be a spared second component.
We'll dump to that if we havn't found
anything else so far.
*/
if (dumpto == -1)
dumpto = sparecol;
}
}
}
if (dumpto == -1) {
/* we couldn't find any live components to dump to!?!?
*/
error = EINVAL;
goto out;
}
bdev = bdevsw_lookup(raidPtr->Disks[dumpto].dev);
if (bdev == NULL) {
error = ENXIO;
goto out;
}
error = (*bdev->d_dump)(raidPtr->Disks[dumpto].dev,
blkno, va, nblk * raidPtr->bytesPerSector);
out:
raidunlock(rs);
return error;
}
/* ARGSUSED */
static int
raidopen(dev_t dev, int flags, int fmt,
struct lwp *l)
{
int unit = raidunit(dev);
struct raid_softc *rs;
struct dk_softc *dksc;
int error = 0;
int part, pmask;
if ((rs = raidget(unit, true)) == NULL)
return ENXIO;
if ((error = raidlock(rs)) != 0)
return error;
if ((rs->sc_flags & RAIDF_SHUTDOWN) != 0) {
error = EBUSY;
goto bad;
}
dksc = &rs->sc_dksc;
part = DISKPART(dev);
pmask = (1 << part);
if (!DK_BUSY(dksc, pmask) &&
((rs->sc_flags & RAIDF_INITED) != 0)) {
/* First one... mark things as dirty... Note that we *MUST*
have done a configure before this. I DO NOT WANT TO BE
SCRIBBLING TO RANDOM COMPONENTS UNTIL IT'S BEEN DETERMINED
THAT THEY BELONG TOGETHER!!!!! */
/* XXX should check to see if we're only open for reading
here... If so, we needn't do this, but then need some
other way of keeping track of what's happened.. */
rf_markalldirty(&rs->sc_r);
}
if ((rs->sc_flags & RAIDF_INITED) != 0)
error = dk_open(dksc, dev, flags, fmt, l);
bad:
raidunlock(rs);
return error;
}
static int
raid_lastclose(device_t self)