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