4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/param.h>
31 static uint_t
*mddb_crctab
= NULL
;
36 #define MD_ZALLOC(x) Zalloc(x)
37 #define MD_FREE(x, y) Free(x)
39 #define MD_ZALLOC(x) kmem_zalloc(x, KM_SLEEP)
40 #define MD_FREE(x, y) kmem_free(x, y)
41 #include <sys/thread.h>
42 #include <sys/types.h>
44 #include <sys/debug.h>
45 #endif /* ! _KERNEL */
46 #include <sys/lvm/md_crc.h>
48 #define MDDB_CRCMAGIC 987654
56 uint_t poly
= 0x04c11db7;
58 crctab
= (uint_t
*)MD_ZALLOC(256 * sizeof (int));
59 for (b
= 0; b
< 256; b
++) {
60 for (v
= b
<< (24), i
= 0; i
< 8; i
++) {
61 if (v
& ((unsigned int)1 << 31)) {
73 * crc function that allows a number of areas to be skipped (ignored)
74 * during the crc computation. The result area of the record is also ignored
75 * during the crc computation. Ignored areas are used for data that may
76 * be changed after record has been crcgen'd, but before the data has been
77 * written to disk or for when a multi-owner diskset may have multiple
78 * nodes writing the same record data with the exception of the timestamp field.
79 * The list of skip areas must be in ascending order of offset and if any
80 * areas overlap, the list will be modified.
85 uchar_t
*record
, /* record to be check-summed */
86 uint_t
*result
, /* put check-sum here(really u_long) */
87 size_t size
, /* size of record in bytes */
88 crc_skip_t
*skip
/* list of areas to skip */
97 * Check skip areas to see if they overlap (this should never happen,
98 * but is handled just in case something changes in the future).
99 * Also the skip list must be in ascending order of offset, assert
100 * error if this is not the case.
101 * If any 2 adjacent skip areas overlap, then the skip areas will
102 * be merged into 1 skip area and the other skip area is freed.
103 * If any 2 adjacent skip areas abut (border) each other, then skip
104 * areas are not merged, but are left as 2 independent skip areas.
105 * If the skip areas are identical, no change is made to either skip
106 * area since this is handled later.
110 for (s
= skip
; s
!= NULL
; s
= s
->skip_next
) {
116 ASSERT(s
->skip_offset
> p
->skip_offset
);
118 assert(s
->skip_offset
> p
->skip_offset
);
120 if ((p
->skip_offset
+ p
->skip_size
) > s
->skip_offset
) {
122 * Current area overlaps previous, modify
123 * previous area and release current
125 p
->skip_size
+= s
->skip_size
- (p
->skip_offset
126 + p
->skip_size
- s
->skip_offset
);
127 p
->skip_next
= s
->skip_next
;
128 MD_FREE(s
, sizeof (crc_skip_t
));
136 mddb_crctab
= crcgentab();
138 crctab
= mddb_crctab
;
139 newcrc
= MDDB_CRCMAGIC
;
144 /* Skip the result pointer */
145 if (record
== (uchar_t
*)result
) {
146 record
+= sizeof (uint_t
);
147 size
-= (sizeof (uint_t
) - 1);
152 * Skip over next skip area if non-null
154 if ((s
) && (record
== (recaddr
+ (s
->skip_offset
)))) {
155 record
+= s
->skip_size
;
156 size
-= (s
->skip_size
- 1);
161 newcrc
= (newcrc
<< 8) ^ crctab
[(newcrc
>> 24) ^ *record
++];
164 /* If we are checking, we either get a 0 - OK, or 1 - Not OK result */
166 if (*((uint_t
*)result
) == newcrc
)
172 * If we are generating, we stuff the result, if we have a result
173 * pointer, and return the value.
176 *((uint_t
*)result
) = newcrc
;
184 MD_FREE((caddr_t
)mddb_crctab
, 256 * sizeof (int));