1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <sys/param.h>
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate static uint_t *mddb_crctab = NULL;
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate #ifndef _KERNEL
34*0Sstevel@tonic-gate #include <meta.h>
35*0Sstevel@tonic-gate #include <assert.h>
36*0Sstevel@tonic-gate #define MD_ZALLOC(x) Zalloc(x)
37*0Sstevel@tonic-gate #define MD_FREE(x, y) Free(x)
38*0Sstevel@tonic-gate #else /* _KERNEL */
39*0Sstevel@tonic-gate #define MD_ZALLOC(x) kmem_zalloc(x, KM_SLEEP)
40*0Sstevel@tonic-gate #define MD_FREE(x, y) kmem_free(x, y)
41*0Sstevel@tonic-gate #include <sys/thread.h>
42*0Sstevel@tonic-gate #include <sys/types.h>
43*0Sstevel@tonic-gate #include <sys/kmem.h>
44*0Sstevel@tonic-gate #include <sys/debug.h>
45*0Sstevel@tonic-gate #endif /* ! _KERNEL */
46*0Sstevel@tonic-gate #include <sys/lvm/md_crc.h>
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate #define MDDB_CRCMAGIC 987654
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate static uint_t *
crcgentab(void)51*0Sstevel@tonic-gate crcgentab(void)
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate int b, i;
54*0Sstevel@tonic-gate uint_t v;
55*0Sstevel@tonic-gate uint_t *crctab;
56*0Sstevel@tonic-gate uint_t poly = 0x04c11db7;
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate crctab = (uint_t *)MD_ZALLOC(256 * sizeof (int));
59*0Sstevel@tonic-gate for (b = 0; b < 256; b++) {
60*0Sstevel@tonic-gate for (v = b << (24), i = 0; i < 8; i++) {
61*0Sstevel@tonic-gate if (v & ((unsigned int)1 << 31)) {
62*0Sstevel@tonic-gate v = (v << 1) ^ poly;
63*0Sstevel@tonic-gate } else {
64*0Sstevel@tonic-gate v = v << 1;
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate crctab[b] = v;
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate return (crctab);
70*0Sstevel@tonic-gate }
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate * crc function that allows a number of areas to be skipped (ignored)
74*0Sstevel@tonic-gate * during the crc computation. The result area of the record is also ignored
75*0Sstevel@tonic-gate * during the crc computation. Ignored areas are used for data that may
76*0Sstevel@tonic-gate * be changed after record has been crcgen'd, but before the data has been
77*0Sstevel@tonic-gate * written to disk or for when a multi-owner diskset may have multiple
78*0Sstevel@tonic-gate * nodes writing the same record data with the exception of the timestamp field.
79*0Sstevel@tonic-gate * The list of skip areas must be in ascending order of offset and if any
80*0Sstevel@tonic-gate * areas overlap, the list will be modified.
81*0Sstevel@tonic-gate */
82*0Sstevel@tonic-gate uint_t
crcfunc(uint_t check,uchar_t * record,uint_t * result,size_t size,crc_skip_t * skip)83*0Sstevel@tonic-gate crcfunc(
84*0Sstevel@tonic-gate uint_t check,
85*0Sstevel@tonic-gate uchar_t *record, /* record to be check-summed */
86*0Sstevel@tonic-gate uint_t *result, /* put check-sum here(really u_long) */
87*0Sstevel@tonic-gate size_t size, /* size of record in bytes */
88*0Sstevel@tonic-gate crc_skip_t *skip /* list of areas to skip */
89*0Sstevel@tonic-gate )
90*0Sstevel@tonic-gate {
91*0Sstevel@tonic-gate uint_t newcrc;
92*0Sstevel@tonic-gate uint_t *crctab;
93*0Sstevel@tonic-gate uchar_t *recaddr;
94*0Sstevel@tonic-gate crc_skip_t *s, *p;
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate * Check skip areas to see if they overlap (this should never happen,
98*0Sstevel@tonic-gate * but is handled just in case something changes in the future).
99*0Sstevel@tonic-gate * Also the skip list must be in ascending order of offset, assert
100*0Sstevel@tonic-gate * error if this is not the case.
101*0Sstevel@tonic-gate * If any 2 adjacent skip areas overlap, then the skip areas will
102*0Sstevel@tonic-gate * be merged into 1 skip area and the other skip area is freed.
103*0Sstevel@tonic-gate * If any 2 adjacent skip areas abut (border) each other, then skip
104*0Sstevel@tonic-gate * areas are not merged, but are left as 2 independent skip areas.
105*0Sstevel@tonic-gate * If the skip areas are identical, no change is made to either skip
106*0Sstevel@tonic-gate * area since this is handled later.
107*0Sstevel@tonic-gate */
108*0Sstevel@tonic-gate if (skip) {
109*0Sstevel@tonic-gate p = NULL;
110*0Sstevel@tonic-gate for (s = skip; s != NULL; s = s->skip_next) {
111*0Sstevel@tonic-gate if (p == NULL) {
112*0Sstevel@tonic-gate p = s;
113*0Sstevel@tonic-gate continue;
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate #ifdef _KERNEL
116*0Sstevel@tonic-gate ASSERT(s->skip_offset > p->skip_offset);
117*0Sstevel@tonic-gate #else
118*0Sstevel@tonic-gate assert(s->skip_offset > p->skip_offset);
119*0Sstevel@tonic-gate #endif
120*0Sstevel@tonic-gate if ((p->skip_offset + p->skip_size) > s->skip_offset) {
121*0Sstevel@tonic-gate /*
122*0Sstevel@tonic-gate * Current area overlaps previous, modify
123*0Sstevel@tonic-gate * previous area and release current
124*0Sstevel@tonic-gate */
125*0Sstevel@tonic-gate p->skip_size += s->skip_size - (p->skip_offset
126*0Sstevel@tonic-gate + p->skip_size - s->skip_offset);
127*0Sstevel@tonic-gate p->skip_next = s->skip_next;
128*0Sstevel@tonic-gate MD_FREE(s, sizeof (crc_skip_t));
129*0Sstevel@tonic-gate s = p;
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate p = s;
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate if (! mddb_crctab)
136*0Sstevel@tonic-gate mddb_crctab = crcgentab();
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate crctab = mddb_crctab;
139*0Sstevel@tonic-gate newcrc = MDDB_CRCMAGIC;
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate recaddr = record;
142*0Sstevel@tonic-gate s = skip;
143*0Sstevel@tonic-gate while (size--) {
144*0Sstevel@tonic-gate /* Skip the result pointer */
145*0Sstevel@tonic-gate if (record == (uchar_t *)result) {
146*0Sstevel@tonic-gate record += sizeof (uint_t);
147*0Sstevel@tonic-gate size -= (sizeof (uint_t) - 1);
148*0Sstevel@tonic-gate continue;
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate /*
152*0Sstevel@tonic-gate * Skip over next skip area if non-null
153*0Sstevel@tonic-gate */
154*0Sstevel@tonic-gate if ((s) && (record == (recaddr + (s->skip_offset)))) {
155*0Sstevel@tonic-gate record += s->skip_size;
156*0Sstevel@tonic-gate size -= (s->skip_size - 1);
157*0Sstevel@tonic-gate s = s->skip_next;
158*0Sstevel@tonic-gate continue;
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate newcrc = (newcrc << 8) ^ crctab[(newcrc >> 24) ^ *record++];
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate /* If we are checking, we either get a 0 - OK, or 1 - Not OK result */
165*0Sstevel@tonic-gate if (check) {
166*0Sstevel@tonic-gate if (*((uint_t *)result) == newcrc)
167*0Sstevel@tonic-gate return (0);
168*0Sstevel@tonic-gate return (1);
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate /*
172*0Sstevel@tonic-gate * If we are generating, we stuff the result, if we have a result
173*0Sstevel@tonic-gate * pointer, and return the value.
174*0Sstevel@tonic-gate */
175*0Sstevel@tonic-gate if (result != NULL)
176*0Sstevel@tonic-gate *((uint_t *)result) = newcrc;
177*0Sstevel@tonic-gate return (newcrc);
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate void
crcfreetab(void)181*0Sstevel@tonic-gate crcfreetab(void)
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate if (mddb_crctab) {
184*0Sstevel@tonic-gate MD_FREE((caddr_t)mddb_crctab, 256 * sizeof (int));
185*0Sstevel@tonic-gate mddb_crctab = NULL;
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate }
188