/
ccd.c
1889 lines (1655 loc) · 44.5 KB
/
ccd.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
/* $NetBSD: ccd.c,v 1.185 2020/10/06 18:45:23 mlelstv Exp $ */
/*-
* Copyright (c) 1996, 1997, 1998, 1999, 2007, 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe, and by Andrew Doran.
*
* 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
*/
/*
* "Concatenated" disk driver.
*
* Notes on concurrency:
*
* => sc_dvlock serializes access to the device nodes, excluding block I/O.
*
* => sc_iolock serializes access to (sc_flags & CCDF_INITED), disk stats,
* sc_stop, sc_bufq and b_resid from master buffers.
*
* => a combination of CCDF_INITED, sc_inflight, and sc_iolock is used to
* serialize I/O and configuration changes.
*
* => the in-core disk label does not change while the device is open.
*
* On memory consumption: ccd fans out I/O requests and so needs to
* allocate memory. If the system is desperately low on memory, we
* single thread I/O.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ccd.c,v 1.185 2020/10/06 18:45:23 mlelstv Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/errno.h>
#include <sys/buf.h>
#include <sys/kmem.h>
#include <sys/pool.h>
#include <sys/module.h>
#include <sys/namei.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/disklabel.h>
#include <sys/device.h>
#include <sys/disk.h>
#include <sys/syslog.h>
#include <sys/fcntl.h>
#include <sys/vnode.h>
#include <sys/conf.h>
#include <sys/mutex.h>
#include <sys/queue.h>
#include <sys/kauth.h>
#include <sys/kthread.h>
#include <sys/bufq.h>
#include <sys/sysctl.h>
#include <sys/compat_stub.h>
#include <uvm/uvm_extern.h>
#include <dev/ccdvar.h>
#include <dev/dkvar.h>
#include <miscfs/specfs/specdev.h> /* for v_rdev */
#include "ioconf.h"
#if defined(CCDDEBUG) && !defined(DEBUG)
#define DEBUG
#endif
#ifdef DEBUG
#define CCDB_FOLLOW 0x01
#define CCDB_INIT 0x02
#define CCDB_IO 0x04
#define CCDB_LABEL 0x08
#define CCDB_VNODE 0x10
int ccddebug = 0x00;
#endif
#define ccdunit(x) DISKUNIT(x)
struct ccdbuf {
struct buf cb_buf; /* new I/O buf */
struct buf *cb_obp; /* ptr. to original I/O buf */
struct ccd_softc *cb_sc; /* pointer to ccd softc */
int cb_comp; /* target component */
SIMPLEQ_ENTRY(ccdbuf) cb_q; /* fifo of component buffers */
};
/* component buffer pool */
static pool_cache_t ccd_cache;
#define CCD_GETBUF() pool_cache_get(ccd_cache, PR_WAITOK)
#define CCD_PUTBUF(cbp) pool_cache_put(ccd_cache, cbp)
#define CCDLABELDEV(dev) \
(MAKEDISKDEV(major((dev)), ccdunit((dev)), RAW_PART))
/* called by main() at boot time */
void ccddetach(void);
/* called by biodone() at interrupt time */
static void ccdiodone(struct buf *);
static void ccdinterleave(struct ccd_softc *);
static int ccdinit(struct ccd_softc *, char **, struct vnode **,
struct lwp *);
static struct ccdbuf *ccdbuffer(struct ccd_softc *, struct buf *,
daddr_t, void *, long);
static void ccdgetdefaultlabel(struct ccd_softc *, struct disklabel *);
static void ccdgetdisklabel(dev_t);
static void ccdmakedisklabel(struct ccd_softc *);
static void ccdstart(struct ccd_softc *);
static void ccdthread(void *);
static dev_type_open(ccdopen);
static dev_type_close(ccdclose);
static dev_type_read(ccdread);
static dev_type_write(ccdwrite);
static dev_type_ioctl(ccdioctl);
static dev_type_strategy(ccdstrategy);
static dev_type_size(ccdsize);
const struct bdevsw ccd_bdevsw = {
.d_open = ccdopen,
.d_close = ccdclose,
.d_strategy = ccdstrategy,
.d_ioctl = ccdioctl,
.d_dump = nodump,
.d_psize = ccdsize,
.d_discard = nodiscard,
.d_flag = D_DISK | D_MPSAFE
};
const struct cdevsw ccd_cdevsw = {
.d_open = ccdopen,
.d_close = ccdclose,
.d_read = ccdread,
.d_write = ccdwrite,
.d_ioctl = ccdioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_DISK | D_MPSAFE
};
static const struct dkdriver ccddkdriver = {
.d_strategy = ccdstrategy,
.d_minphys = minphys
};
#ifdef DEBUG
static void printiinfo(struct ccdiinfo *);
#endif
static LIST_HEAD(, ccd_softc) ccds = LIST_HEAD_INITIALIZER(ccds);
static kmutex_t ccd_lock;
SYSCTL_SETUP_PROTO(sysctl_kern_ccd_setup);
static struct ccd_softc *
ccdcreate(int unit) {
struct ccd_softc *sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
/* Initialize per-softc structures. */
snprintf(sc->sc_xname, sizeof(sc->sc_xname), "ccd%d", unit);
sc->sc_unit = unit;
mutex_init(&sc->sc_dvlock, MUTEX_DEFAULT, IPL_NONE);
sc->sc_iolock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
cv_init(&sc->sc_stop, "ccdstop");
cv_init(&sc->sc_push, "ccdthr");
disk_init(&sc->sc_dkdev, sc->sc_xname, &ccddkdriver);
return sc;
}
static void
ccddestroy(struct ccd_softc *sc) {
mutex_obj_free(sc->sc_iolock);
mutex_exit(&sc->sc_dvlock);
mutex_destroy(&sc->sc_dvlock);
cv_destroy(&sc->sc_stop);
cv_destroy(&sc->sc_push);
disk_destroy(&sc->sc_dkdev);
kmem_free(sc, sizeof(*sc));
}
static struct ccd_softc *
ccdget(int unit, int make) {
struct ccd_softc *sc;
if (unit < 0) {
#ifdef DIAGNOSTIC
panic("%s: unit %d!", __func__, unit);
#endif
return NULL;
}
mutex_enter(&ccd_lock);
LIST_FOREACH(sc, &ccds, sc_link) {
if (sc->sc_unit == unit) {
mutex_exit(&ccd_lock);
return sc;
}
}
mutex_exit(&ccd_lock);
if (!make)
return NULL;
if ((sc = ccdcreate(unit)) == NULL)
return NULL;
mutex_enter(&ccd_lock);
LIST_INSERT_HEAD(&ccds, sc, sc_link);
mutex_exit(&ccd_lock);
return sc;
}
static void
ccdput(struct ccd_softc *sc) {
mutex_enter(&ccd_lock);
LIST_REMOVE(sc, sc_link);
mutex_exit(&ccd_lock);
ccddestroy(sc);
}
/*
* Called by main() during pseudo-device attachment. All we need
* to do is allocate enough space for devices to be configured later.
*/
void
ccdattach(int num)
{
mutex_init(&ccd_lock, MUTEX_DEFAULT, IPL_NONE);
/* Initialize the component buffer pool. */
ccd_cache = pool_cache_init(sizeof(struct ccdbuf), 0,
0, 0, "ccdbuf", NULL, IPL_BIO, NULL, NULL, NULL);
}
void
ccddetach(void)
{
pool_cache_destroy(ccd_cache);
mutex_destroy(&ccd_lock);
}
static int
ccdinit(struct ccd_softc *cs, char **cpaths, struct vnode **vpp,
struct lwp *l)
{
struct ccdcinfo *ci = NULL;
int ix;
struct ccdgeom *ccg = &cs->sc_geom;
char *tmppath;
int error, path_alloced;
uint64_t psize, minsize;
unsigned secsize, maxsecsize;
struct disk_geom *dg;
#ifdef DEBUG
if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
printf("%s: ccdinit\n", cs->sc_xname);
#endif
/* Allocate space for the component info. */
cs->sc_cinfo = kmem_alloc(cs->sc_nccdisks * sizeof(*cs->sc_cinfo),
KM_SLEEP);
tmppath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
cs->sc_size = 0;
/*
* Verify that each component piece exists and record
* relevant information about it.
*/
maxsecsize = 0;
minsize = 0;
for (ix = 0, path_alloced = 0; ix < cs->sc_nccdisks; ix++) {
ci = &cs->sc_cinfo[ix];
ci->ci_vp = vpp[ix];
/*
* Copy in the pathname of the component.
*/
memset(tmppath, 0, MAXPATHLEN); /* sanity */
error = copyinstr(cpaths[ix], tmppath,
MAXPATHLEN, &ci->ci_pathlen);
if (ci->ci_pathlen == 0)
error = EINVAL;
if (error) {
#ifdef DEBUG
if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
printf("%s: can't copy path, error = %d\n",
cs->sc_xname, error);
#endif
goto out;
}
ci->ci_path = kmem_alloc(ci->ci_pathlen, KM_SLEEP);
memcpy(ci->ci_path, tmppath, ci->ci_pathlen);
path_alloced++;
/*
* XXX: Cache the component's dev_t.
*/
ci->ci_dev = vpp[ix]->v_rdev;
/*
* Get partition information for the component.
*/
error = getdisksize(vpp[ix], &psize, &secsize);
if (error) {
#ifdef DEBUG
if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
printf("%s: %s: disksize failed, error = %d\n",
cs->sc_xname, ci->ci_path, error);
#endif
goto out;
}
/*
* Calculate the size, truncating to an interleave
* boundary if necessary.
*/
maxsecsize = secsize > maxsecsize ? secsize : maxsecsize;
if (cs->sc_ileave > 1)
psize -= psize % cs->sc_ileave;
if (psize == 0) {
#ifdef DEBUG
if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
printf("%s: %s: size == 0\n",
cs->sc_xname, ci->ci_path);
#endif
error = ENODEV;
goto out;
}
if (minsize == 0 || psize < minsize)
minsize = psize;
ci->ci_size = psize;
cs->sc_size += psize;
}
/*
* Don't allow the interleave to be smaller than
* the biggest component sector.
*/
if ((cs->sc_ileave > 0) &&
(cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
#ifdef DEBUG
if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
printf("%s: interleave must be at least %d\n",
cs->sc_xname, (maxsecsize / DEV_BSIZE));
#endif
error = EINVAL;
goto out;
}
/*
* If uniform interleave is desired set all sizes to that of
* the smallest component.
*/
if (cs->sc_flags & CCDF_UNIFORM) {
for (ci = cs->sc_cinfo;
ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
ci->ci_size = minsize;
cs->sc_size = cs->sc_nccdisks * minsize;
}
/*
* Construct the interleave table.
*/
ccdinterleave(cs);
/*
* Create pseudo-geometry based on 1MB cylinders. It's
* pretty close.
*/
ccg->ccg_secsize = DEV_BSIZE;
ccg->ccg_ntracks = 1;
ccg->ccg_nsectors = 1024 * (1024 / ccg->ccg_secsize);
ccg->ccg_ncylinders = cs->sc_size / ccg->ccg_nsectors;
dg = &cs->sc_dkdev.dk_geom;
memset(dg, 0, sizeof(*dg));
dg->dg_secperunit = cs->sc_size;
dg->dg_secsize = ccg->ccg_secsize;
dg->dg_nsectors = ccg->ccg_nsectors;
dg->dg_ntracks = ccg->ccg_ntracks;
dg->dg_ncylinders = ccg->ccg_ncylinders;
if (cs->sc_ileave > 0)
aprint_normal("%s: Interleaving %d component%s "
"(%d block interleave)\n", cs->sc_xname,
cs->sc_nccdisks, (cs->sc_nccdisks != 0 ? "s" : ""),
cs->sc_ileave);
else
aprint_normal("%s: Concatenating %d component%s\n",
cs->sc_xname,
cs->sc_nccdisks, (cs->sc_nccdisks != 0 ? "s" : ""));
for (ix = 0; ix < cs->sc_nccdisks; ix++) {
ci = &cs->sc_cinfo[ix];
aprint_normal("%s: %s (%ju blocks)\n", cs->sc_xname,
ci->ci_path, (uintmax_t)ci->ci_size);
}
aprint_normal("%s: total %ju blocks\n", cs->sc_xname, cs->sc_size);
/*
* Create thread to handle deferred I/O.
*/
cs->sc_zap = false;
error = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL, ccdthread,
cs, &cs->sc_thread, "%s", cs->sc_xname);
if (error) {
printf("ccdinit: can't create thread: %d\n", error);
goto out;
}
/*
* Only now that everything is set up can we enable the device.
*/
mutex_enter(cs->sc_iolock);
cs->sc_flags |= CCDF_INITED;
mutex_exit(cs->sc_iolock);
kmem_free(tmppath, MAXPATHLEN);
return (0);
out:
for (ix = 0; ix < path_alloced; ix++) {
kmem_free(cs->sc_cinfo[ix].ci_path,
cs->sc_cinfo[ix].ci_pathlen);
}
kmem_free(cs->sc_cinfo, cs->sc_nccdisks * sizeof(struct ccdcinfo));
kmem_free(tmppath, MAXPATHLEN);
return (error);
}
static void
ccdinterleave(struct ccd_softc *cs)
{
struct ccdcinfo *ci, *smallci;
struct ccdiinfo *ii;
daddr_t bn, lbn;
int ix;
u_long size;
#ifdef DEBUG
if (ccddebug & CCDB_INIT)
printf("ccdinterleave(%p): ileave %d\n", cs, cs->sc_ileave);
#endif
/*
* Allocate an interleave table.
* Chances are this is too big, but we don't care.
*/
size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo);
cs->sc_itable = kmem_zalloc(size, KM_SLEEP);
/*
* Trivial case: no interleave (actually interleave of disk size).
* Each table entry represents a single component in its entirety.
*/
if (cs->sc_ileave == 0) {
bn = 0;
ii = cs->sc_itable;
for (ix = 0; ix < cs->sc_nccdisks; ix++) {
/* Allocate space for ii_index. */
ii->ii_indexsz = sizeof(int);
ii->ii_index = kmem_alloc(ii->ii_indexsz, KM_SLEEP);
ii->ii_ndisk = 1;
ii->ii_startblk = bn;
ii->ii_startoff = 0;
ii->ii_index[0] = ix;
bn += cs->sc_cinfo[ix].ci_size;
ii++;
}
ii->ii_ndisk = 0;
#ifdef DEBUG
if (ccddebug & CCDB_INIT)
printiinfo(cs->sc_itable);
#endif
return;
}
/*
* The following isn't fast or pretty; it doesn't have to be.
*/
size = 0;
bn = lbn = 0;
for (ii = cs->sc_itable; ; ii++) {
/* Allocate space for ii_index. */
ii->ii_indexsz = sizeof(int) * cs->sc_nccdisks;
ii->ii_index = kmem_alloc(ii->ii_indexsz, KM_SLEEP);
/*
* Locate the smallest of the remaining components
*/
smallci = NULL;
for (ci = cs->sc_cinfo;
ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
if (ci->ci_size > size &&
(smallci == NULL ||
ci->ci_size < smallci->ci_size))
smallci = ci;
/*
* Nobody left, all done
*/
if (smallci == NULL) {
ii->ii_ndisk = 0;
break;
}
/*
* Record starting logical block and component offset
*/
ii->ii_startblk = bn / cs->sc_ileave;
ii->ii_startoff = lbn;
/*
* Determine how many disks take part in this interleave
* and record their indices.
*/
ix = 0;
for (ci = cs->sc_cinfo;
ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
if (ci->ci_size >= smallci->ci_size)
ii->ii_index[ix++] = ci - cs->sc_cinfo;
ii->ii_ndisk = ix;
bn += ix * (smallci->ci_size - size);
lbn = smallci->ci_size / cs->sc_ileave;
size = smallci->ci_size;
}
#ifdef DEBUG
if (ccddebug & CCDB_INIT)
printiinfo(cs->sc_itable);
#endif
}
/* ARGSUSED */
static int
ccdopen(dev_t dev, int flags, int fmt, struct lwp *l)
{
int unit = ccdunit(dev);
struct ccd_softc *cs;
struct disklabel *lp;
int error = 0, part, pmask;
#ifdef DEBUG
if (ccddebug & CCDB_FOLLOW)
printf("ccdopen(0x%"PRIx64", 0x%x)\n", dev, flags);
#endif
if ((cs = ccdget(unit, 1)) == NULL)
return ENXIO;
mutex_enter(&cs->sc_dvlock);
lp = cs->sc_dkdev.dk_label;
part = DISKPART(dev);
pmask = (1 << part);
/*
* If we're initialized, check to see if there are any other
* open partitions. If not, then it's safe to update
* the in-core disklabel. Only read the disklabel if it is
* not already valid.
*/
if ((cs->sc_flags & (CCDF_INITED|CCDF_VLABEL)) == CCDF_INITED &&
cs->sc_dkdev.dk_openmask == 0)
ccdgetdisklabel(dev);
/* Check that the partition exists. */
if (part != RAW_PART) {
if (((cs->sc_flags & CCDF_INITED) == 0) ||
((part >= lp->d_npartitions) ||
(lp->d_partitions[part].p_fstype == FS_UNUSED))) {
error = ENXIO;
goto done;
}
}
/* Prevent our unit from being unconfigured while open. */
switch (fmt) {
case S_IFCHR:
cs->sc_dkdev.dk_copenmask |= pmask;
break;
case S_IFBLK:
cs->sc_dkdev.dk_bopenmask |= pmask;
break;
}
cs->sc_dkdev.dk_openmask =
cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
done:
mutex_exit(&cs->sc_dvlock);
return (error);
}
/* ARGSUSED */
static int
ccdclose(dev_t dev, int flags, int fmt, struct lwp *l)
{
int unit = ccdunit(dev);
struct ccd_softc *cs;
int part;
#ifdef DEBUG
if (ccddebug & CCDB_FOLLOW)
printf("ccdclose(0x%"PRIx64", 0x%x)\n", dev, flags);
#endif
if ((cs = ccdget(unit, 0)) == NULL)
return ENXIO;
mutex_enter(&cs->sc_dvlock);
part = DISKPART(dev);
/* ...that much closer to allowing unconfiguration... */
switch (fmt) {
case S_IFCHR:
cs->sc_dkdev.dk_copenmask &= ~(1 << part);
break;
case S_IFBLK:
cs->sc_dkdev.dk_bopenmask &= ~(1 << part);
break;
}
cs->sc_dkdev.dk_openmask =
cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
if (cs->sc_dkdev.dk_openmask == 0) {
if ((cs->sc_flags & CCDF_KLABEL) == 0)
cs->sc_flags &= ~CCDF_VLABEL;
}
mutex_exit(&cs->sc_dvlock);
return (0);
}
static bool
ccdbackoff(struct ccd_softc *cs)
{
/* XXX Arbitrary, should be a uvm call. */
return uvm_availmem(true) < (uvmexp.freemin >> 1) &&
disk_isbusy(&cs->sc_dkdev);
}
static void
ccdthread(void *cookie)
{
struct ccd_softc *cs;
cs = cookie;
#ifdef DEBUG
if (ccddebug & CCDB_FOLLOW)
printf("ccdthread: hello\n");
#endif
mutex_enter(cs->sc_iolock);
while (__predict_true(!cs->sc_zap)) {
if (bufq_peek(cs->sc_bufq) == NULL) {
/* Nothing to do. */
cv_wait(&cs->sc_push, cs->sc_iolock);
continue;
}
if (ccdbackoff(cs)) {
/* Wait for memory to become available. */
(void)cv_timedwait(&cs->sc_push, cs->sc_iolock, 1);
continue;
}
#ifdef DEBUG
if (ccddebug & CCDB_FOLLOW)
printf("ccdthread: dispatching I/O\n");
#endif
ccdstart(cs);
mutex_enter(cs->sc_iolock);
}
cs->sc_thread = NULL;
mutex_exit(cs->sc_iolock);
#ifdef DEBUG
if (ccddebug & CCDB_FOLLOW)
printf("ccdthread: goodbye\n");
#endif
kthread_exit(0);
}
static void
ccdstrategy(struct buf *bp)
{
int unit = ccdunit(bp->b_dev);
struct ccd_softc *cs;
if ((cs = ccdget(unit, 0)) == NULL)
return;
/* Must be open or reading label. */
KASSERT(cs->sc_dkdev.dk_openmask != 0 ||
(cs->sc_flags & CCDF_RLABEL) != 0);
mutex_enter(cs->sc_iolock);
/* Synchronize with device init/uninit. */
if (__predict_false((cs->sc_flags & CCDF_INITED) == 0)) {
mutex_exit(cs->sc_iolock);
#ifdef DEBUG
if (ccddebug & CCDB_FOLLOW)
printf("ccdstrategy: unit %d: not inited\n", unit);
#endif
bp->b_error = ENXIO;
bp->b_resid = bp->b_bcount;
biodone(bp);
return;
}
/* Defer to thread if system is low on memory. */
bufq_put(cs->sc_bufq, bp);
if (__predict_false(ccdbackoff(cs))) {
mutex_exit(cs->sc_iolock);
#ifdef DEBUG
if (ccddebug & CCDB_FOLLOW)
printf("ccdstrategy: holding off on I/O\n");
#endif
return;
}
ccdstart(cs);
}
static void
ccdstart(struct ccd_softc *cs)
{
daddr_t blkno;
int wlabel;
struct disklabel *lp;
long bcount, rcount;
struct ccdbuf *cbp;
char *addr;
daddr_t bn;
vnode_t *vp;
buf_t *bp;
KASSERT(mutex_owned(cs->sc_iolock));
bp = bufq_get(cs->sc_bufq);
KASSERT(bp != NULL);
disk_busy(&cs->sc_dkdev);
#ifdef DEBUG
if (ccddebug & CCDB_FOLLOW)
printf("ccdstart(%s, %p)\n", cs->sc_xname, bp);
#endif
/* If it's a nil transfer, wake up the top half now. */
if (bp->b_bcount == 0)
goto done;
lp = cs->sc_dkdev.dk_label;
/*
* Do bounds checking and adjust transfer. If there's an
* error, the bounds check will flag that for us. Convert
* the partition relative block number to an absolute.
*/
blkno = bp->b_blkno;
wlabel = cs->sc_flags & (CCDF_WLABEL|CCDF_LABELLING);
if (DISKPART(bp->b_dev) != RAW_PART) {
if (bounds_check_with_label(&cs->sc_dkdev, bp, wlabel) <= 0)
goto done;
blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
}
mutex_exit(cs->sc_iolock);
bp->b_rawblkno = blkno;
/* Allocate the component buffers and start I/O! */
bp->b_resid = bp->b_bcount;
bn = bp->b_rawblkno;
addr = bp->b_data;
for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
cbp = ccdbuffer(cs, bp, bn, addr, bcount);
rcount = cbp->cb_buf.b_bcount;
bn += btodb(rcount);
addr += rcount;
vp = cbp->cb_buf.b_vp;
if ((cbp->cb_buf.b_flags & B_READ) == 0) {
mutex_enter(vp->v_interlock);
vp->v_numoutput++;
mutex_exit(vp->v_interlock);
}
(void)VOP_STRATEGY(vp, &cbp->cb_buf);
}
return;
done:
disk_unbusy(&cs->sc_dkdev, 0, 0);
cv_broadcast(&cs->sc_stop);
cv_broadcast(&cs->sc_push);
mutex_exit(cs->sc_iolock);
bp->b_resid = bp->b_bcount;
biodone(bp);
}
/*
* Build a component buffer header.
*/
static struct ccdbuf *
ccdbuffer(struct ccd_softc *cs, struct buf *bp, daddr_t bn, void *addr,
long bcount)
{
struct ccdcinfo *ci;
struct ccdbuf *cbp;
daddr_t cbn, cboff;
u_int64_t cbc;
int ccdisk;
#ifdef DEBUG
if (ccddebug & CCDB_IO)
printf("ccdbuffer(%p, %p, %" PRId64 ", %p, %ld)\n",
cs, bp, bn, addr, bcount);
#endif
/*
* Determine which component bn falls in.
*/
cbn = bn;
cboff = 0;
/*
* Serially concatenated
*/
if (cs->sc_ileave == 0) {
daddr_t sblk;
sblk = 0;
for (ccdisk = 0, ci = &cs->sc_cinfo[ccdisk];
cbn >= sblk + ci->ci_size;
ccdisk++, ci = &cs->sc_cinfo[ccdisk])
sblk += ci->ci_size;
cbn -= sblk;
}
/*
* Interleaved
*/
else {
struct ccdiinfo *ii;
int off;
cboff = cbn % cs->sc_ileave;
cbn /= cs->sc_ileave;
for (ii = cs->sc_itable; ii->ii_ndisk; ii++)
if (ii->ii_startblk > cbn)
break;
ii--;
off = cbn - ii->ii_startblk;
if (ii->ii_ndisk == 1) {
ccdisk = ii->ii_index[0];
cbn = ii->ii_startoff + off;
} else {
ccdisk = ii->ii_index[off % ii->ii_ndisk];
cbn = ii->ii_startoff + off / ii->ii_ndisk;
}
cbn *= cs->sc_ileave;
ci = &cs->sc_cinfo[ccdisk];
}
/*
* Fill in the component buf structure.
*/
cbp = CCD_GETBUF();
KASSERT(cbp != NULL);
buf_init(&cbp->cb_buf);
cbp->cb_buf.b_flags = bp->b_flags;
cbp->cb_buf.b_oflags = bp->b_oflags;
cbp->cb_buf.b_cflags = bp->b_cflags;
cbp->cb_buf.b_iodone = ccdiodone;
cbp->cb_buf.b_proc = bp->b_proc;
cbp->cb_buf.b_dev = ci->ci_dev;
cbp->cb_buf.b_blkno = cbn + cboff;
cbp->cb_buf.b_data = addr;
cbp->cb_buf.b_vp = ci->ci_vp;
cbp->cb_buf.b_objlock = ci->ci_vp->v_interlock;
if (cs->sc_ileave == 0)
cbc = dbtob((u_int64_t)(ci->ci_size - cbn));
else
cbc = dbtob((u_int64_t)(cs->sc_ileave - cboff));
cbp->cb_buf.b_bcount = cbc < bcount ? cbc : bcount;
/*
* context for ccdiodone
*/
cbp->cb_obp = bp;
cbp->cb_sc = cs;
cbp->cb_comp = ccdisk;
BIO_COPYPRIO(&cbp->cb_buf, bp);
#ifdef DEBUG
if (ccddebug & CCDB_IO)
printf(" dev 0x%"PRIx64"(u%lu): cbp %p bn %" PRId64 " addr %p"
" bcnt %d\n",
ci->ci_dev, (unsigned long) (ci-cs->sc_cinfo), cbp,
cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
cbp->cb_buf.b_bcount);
#endif
return (cbp);
}
/*
* Called at interrupt time.
* Mark the component as done and if all components are done,
* take a ccd interrupt.
*/
static void
ccdiodone(struct buf *vbp)
{
struct ccdbuf *cbp = (struct ccdbuf *) vbp;
struct buf *bp = cbp->cb_obp;
struct ccd_softc *cs = cbp->cb_sc;
int count;
#ifdef DEBUG
if (ccddebug & CCDB_FOLLOW)
printf("ccdiodone(%p)\n", cbp);
if (ccddebug & CCDB_IO) {
printf("ccdiodone: bp %p bcount %d resid %d\n",
bp, bp->b_bcount, bp->b_resid);
printf(" dev 0x%"PRIx64"(u%d), cbp %p bn %" PRId64 " addr %p"
" bcnt %d\n",
cbp->cb_buf.b_dev, cbp->cb_comp, cbp,
cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
cbp->cb_buf.b_bcount);
}
#endif
if (cbp->cb_buf.b_error != 0) {
bp->b_error = cbp->cb_buf.b_error;