1*0a6a1f1dSLionel Sambuc /* $NetBSD: lfs_cksum.c,v 1.30 2015/08/02 18:18:10 dholland Exp $ */
2d65f6f70SBen Gras
3d65f6f70SBen Gras /*-
4d65f6f70SBen Gras * Copyright (c) 1999, 2000, 2001, 2002 The NetBSD Foundation, Inc.
5d65f6f70SBen Gras * All rights reserved.
6d65f6f70SBen Gras *
7d65f6f70SBen Gras * This code is derived from software contributed to The NetBSD Foundation
8d65f6f70SBen Gras * by Konrad E. Schroder <perseant@hhhh.org>.
9d65f6f70SBen Gras *
10d65f6f70SBen Gras * Redistribution and use in source and binary forms, with or without
11d65f6f70SBen Gras * modification, are permitted provided that the following conditions
12d65f6f70SBen Gras * are met:
13d65f6f70SBen Gras * 1. Redistributions of source code must retain the above copyright
14d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer.
15d65f6f70SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
16d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer in the
17d65f6f70SBen Gras * documentation and/or other materials provided with the distribution.
18d65f6f70SBen Gras *
19d65f6f70SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d65f6f70SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d65f6f70SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d65f6f70SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d65f6f70SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d65f6f70SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d65f6f70SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d65f6f70SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d65f6f70SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d65f6f70SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d65f6f70SBen Gras * POSSIBILITY OF SUCH DAMAGE.
30d65f6f70SBen Gras */
31d65f6f70SBen Gras /*-
32d65f6f70SBen Gras * Copyright (c) 1991, 1993
33d65f6f70SBen Gras * The Regents of the University of California. All rights reserved.
34d65f6f70SBen Gras *
35d65f6f70SBen Gras * Redistribution and use in source and binary forms, with or without
36d65f6f70SBen Gras * modification, are permitted provided that the following conditions
37d65f6f70SBen Gras * are met:
38d65f6f70SBen Gras * 1. Redistributions of source code must retain the above copyright
39d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer.
40d65f6f70SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
41d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer in the
42d65f6f70SBen Gras * documentation and/or other materials provided with the distribution.
43d65f6f70SBen Gras * 3. Neither the name of the University nor the names of its contributors
44d65f6f70SBen Gras * may be used to endorse or promote products derived from this software
45d65f6f70SBen Gras * without specific prior written permission.
46d65f6f70SBen Gras *
47d65f6f70SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48d65f6f70SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49d65f6f70SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50d65f6f70SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51d65f6f70SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52d65f6f70SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53d65f6f70SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54d65f6f70SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55d65f6f70SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56d65f6f70SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57d65f6f70SBen Gras * SUCH DAMAGE.
58d65f6f70SBen Gras *
59d65f6f70SBen Gras * @(#)lfs_cksum.c 8.2 (Berkeley) 10/9/94
60d65f6f70SBen Gras */
61d65f6f70SBen Gras
62d65f6f70SBen Gras #include <sys/cdefs.h>
63*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: lfs_cksum.c,v 1.30 2015/08/02 18:18:10 dholland Exp $");
64d65f6f70SBen Gras
65d65f6f70SBen Gras #include <sys/param.h>
66d65f6f70SBen Gras #ifdef _KERNEL
67d65f6f70SBen Gras # include <sys/systm.h>
68d65f6f70SBen Gras # include <sys/lock.h>
69d65f6f70SBen Gras #else
70d65f6f70SBen Gras # include <stddef.h>
71d65f6f70SBen Gras #endif
72d65f6f70SBen Gras #include <sys/mount.h>
73d65f6f70SBen Gras #include <ufs/lfs/lfs.h>
74d65f6f70SBen Gras #include <ufs/lfs/lfs_extern.h>
75d65f6f70SBen Gras
76d65f6f70SBen Gras /*
77d65f6f70SBen Gras * Simple, general purpose, fast checksum. Data must be short-aligned.
78d65f6f70SBen Gras * Returns a u_long in case we ever want to do something more rigorous.
79d65f6f70SBen Gras *
80d65f6f70SBen Gras * XXX
81d65f6f70SBen Gras * Use the TCP/IP checksum instead.
82d65f6f70SBen Gras */
83d65f6f70SBen Gras u_int32_t
lfs_cksum_part(void * str,size_t len,u_int32_t sum)84d65f6f70SBen Gras lfs_cksum_part(void *str, size_t len, u_int32_t sum)
85d65f6f70SBen Gras {
86d65f6f70SBen Gras
87d65f6f70SBen Gras len &= ~(sizeof(u_int16_t) - 1);
88d65f6f70SBen Gras for (; len; len -= sizeof(u_int16_t)) {
89d65f6f70SBen Gras sum ^= *(u_int16_t *)str;
90d65f6f70SBen Gras str = (void *)((u_int16_t *)str + 1);
91d65f6f70SBen Gras }
92d65f6f70SBen Gras return (sum);
93d65f6f70SBen Gras }
94d65f6f70SBen Gras
95d65f6f70SBen Gras u_int32_t
cksum(void * str,size_t len)96d65f6f70SBen Gras cksum(void *str, size_t len)
97d65f6f70SBen Gras {
98d65f6f70SBen Gras
99d65f6f70SBen Gras return lfs_cksum_fold(lfs_cksum_part(str, len, 0));
100d65f6f70SBen Gras }
101d65f6f70SBen Gras
102d65f6f70SBen Gras u_int32_t
lfs_sb_cksum(struct lfs * fs)103*0a6a1f1dSLionel Sambuc lfs_sb_cksum(struct lfs *fs)
104d65f6f70SBen Gras {
105*0a6a1f1dSLionel Sambuc void *ptr;
106d65f6f70SBen Gras size_t size;
107d65f6f70SBen Gras
108*0a6a1f1dSLionel Sambuc if (fs->lfs_is64) {
109*0a6a1f1dSLionel Sambuc ptr = &fs->lfs_dlfs_u.u_64;
110*0a6a1f1dSLionel Sambuc size = (size_t)offsetof(struct dlfs64, dlfs_cksum);
111*0a6a1f1dSLionel Sambuc } else {
112*0a6a1f1dSLionel Sambuc ptr = &fs->lfs_dlfs_u.u_32;
113*0a6a1f1dSLionel Sambuc size = (size_t)offsetof(struct dlfs64, dlfs_cksum);
114*0a6a1f1dSLionel Sambuc }
115*0a6a1f1dSLionel Sambuc
116*0a6a1f1dSLionel Sambuc return cksum(ptr, size);
117d65f6f70SBen Gras }
118