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