Home | History | Annotate | Line # | Download | only in lfs
      1 /*	$NetBSD: lfs_cksum.c,v 1.30 2015/08/02 18:18:10 dholland Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     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.
     18  *
     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.
     30  */
     31 /*-
     32  * Copyright (c) 1991, 1993
     33  *	The Regents of the University of California.  All rights reserved.
     34  *
     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.
     46  *
     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.
     58  *
     59  *	@(#)lfs_cksum.c	8.2 (Berkeley) 10/9/94
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: lfs_cksum.c,v 1.30 2015/08/02 18:18:10 dholland Exp $");
     64 
     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>
     75 
     76 /*
     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.
     79  *
     80  * XXX
     81  * Use the TCP/IP checksum instead.
     82  */
     83 u_int32_t
     84 lfs_cksum_part(void *str, size_t len, u_int32_t sum)
     85 {
     86 
     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);
     91 	}
     92 	return (sum);
     93 }
     94 
     95 u_int32_t
     96 cksum(void *str, size_t len)
     97 {
     98 
     99 	return lfs_cksum_fold(lfs_cksum_part(str, len, 0));
    100 }
    101 
    102 u_int32_t
    103 lfs_sb_cksum(struct lfs *fs)
    104 {
    105 	void *ptr;
    106 	size_t size;
    107 
    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);
    114 	}
    115 
    116 	return cksum(ptr, size);
    117 }
    118