xref: /netbsd/sys/ufs/ufs/ufs_bswap.h (revision bf9ec67e)
1 /*	$NetBSD: ufs_bswap.h,v 1.10 2002/01/31 19:18:18 tv Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Manuel Bouyer.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #if defined(_KERNEL_OPT)
36 #include "opt_ffs.h"
37 #endif
38 
39 #include <machine/bswap.h>
40 
41 /* Macros to access UFS flags */
42 #ifdef FFS_EI
43 #define	UFS_MPNEEDSWAP(mp)	(VFSTOUFS(mp)->um_flags & UFS_NEEDSWAP)
44 #define UFS_FSNEEDSWAP(fs)	((fs)->fs_flags & FS_SWAPPED)
45 #define	UFS_IPNEEDSWAP(ip)	UFS_MPNEEDSWAP(ITOV(ip)->v_mount)
46 #else
47 #define	UFS_MPNEEDSWAP(mp) (0)
48 #define UFS_FSNEEDSWAP(fs) (0)
49 #define	UFS_IPNEEDSWAP(ip) (0)
50 #endif
51 
52 #if !defined(_KERNEL) || defined(FFS_EI)
53 /* inlines for access to swapped data */
54 static __inline u_int16_t ufs_rw16 __P((u_int16_t, int));
55 static __inline u_int32_t ufs_rw32 __P((u_int32_t, int));
56 static __inline u_int64_t ufs_rw64 __P((u_int64_t, int));
57 
58 static __inline u_int16_t
59 ufs_rw16(a, ns)
60 	u_int16_t a;
61 	int ns;
62 {
63 	return ((ns) ?  bswap16(a) : (a));
64 }
65 static __inline u_int32_t
66 ufs_rw32(a, ns)
67 	u_int32_t a;
68 	int ns;
69 {
70 	return ((ns) ?  bswap32(a) : (a));
71 }
72 static __inline u_int64_t
73 ufs_rw64(a, ns)
74 	u_int64_t a;
75 	int ns;
76 {
77 	return ((ns) ?  bswap64(a) : (a));
78 }
79 #else
80 #define ufs_rw16(a, ns) (a)
81 #define ufs_rw32(a, ns) (a)
82 #define ufs_rw64(a, ns) (a)
83 #endif
84 
85 #define ufs_add16(a, b, ns) \
86 	(a) = ufs_rw16(ufs_rw16((a), (ns)) + (b), (ns))
87 #define ufs_add32(a, b, ns) \
88 	(a) = ufs_rw32(ufs_rw32((a), (ns)) + (b), (ns))
89 #define ufs_add64(a, b, ns) \
90 	(a) = ufs_rw64(ufs_rw64((a), (ns)) + (b), (ns))
91