1185222Ssam/*	$NetBSD: ufs_bswap.h,v 1.13 2003/10/05 17:48:50 bouyer Exp $	*/
2185222Ssam
3185222Ssam/*
4185222Ssam * Copyright (c) 1998 Manuel Bouyer.
5185222Ssam *
6185222Ssam * Redistribution and use in source and binary forms, with or without
7185222Ssam * modification, are permitted provided that the following conditions
8185222Ssam * are met:
9185222Ssam * 1. Redistributions of source code must retain the above copyright
10185222Ssam *    notice, this list of conditions and the following disclaimer.
11185222Ssam * 2. Redistributions in binary form must reproduce the above copyright
12185222Ssam *    notice, this list of conditions and the following disclaimer in the
13185222Ssam *    documentation and/or other materials provided with the distribution.
14185222Ssam * 3. All advertising materials mentioning features or use of this software
15185222Ssam *    must display the following acknowledgement:
16185222Ssam *	This product includes software developed by Manuel Bouyer.
17185222Ssam * 4. The name of the author may not be used to endorse or promote products
18185222Ssam *    derived from this software without specific prior written permission.
19185222Ssam *
20185222Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21185222Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22185222Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23185222Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24185222Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25185222Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26185222Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27185222Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28185222Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29185222Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30185222Ssam *
31186334Ssam * $FreeBSD$
32185222Ssam */
33185222Ssam
34185222Ssam#ifndef _UFS_UFS_BSWAP_H_
35185222Ssam#define _UFS_UFS_BSWAP_H_
36185222Ssam
37185222Ssam#if defined(_KERNEL_OPT)
38185222Ssam#include "opt_ffs.h"
39185222Ssam#endif
40185222Ssam
41186261Ssam#include <sys/endian.h>
42185222Ssam
43253675Smarius#include "makefs.h"
44253675Smarius
45185222Ssam/* Macros to access UFS flags */
46185222Ssam#ifdef FFS_EI
47185222Ssam#define	UFS_MPNEEDSWAP(mp)	(VFSTOUFS(mp)->um_flags & UFS_NEEDSWAP)
48185222Ssam#define UFS_FSNEEDSWAP(fs)	((fs)->fs_flags & FS_SWAPPED)
49185222Ssam#define	UFS_IPNEEDSWAP(ip)	UFS_MPNEEDSWAP(ITOV(ip)->v_mount)
50185222Ssam#else
51185222Ssam#define	UFS_MPNEEDSWAP(mp) (0)
52185222Ssam#define UFS_FSNEEDSWAP(fs) (0)
53185222Ssam#define	UFS_IPNEEDSWAP(ip) (0)
54185222Ssam#endif
55185222Ssam
56185222Ssam#if !defined(_KERNEL) || defined(FFS_EI)
57185222Ssam/* inlines for access to swapped data */
58185222Ssamstatic __inline u_int16_t ufs_rw16 __P((u_int16_t, int));
59185222Ssamstatic __inline u_int32_t ufs_rw32 __P((u_int32_t, int));
60185222Ssamstatic __inline u_int64_t ufs_rw64 __P((u_int64_t, int));
61185222Ssam
62185222Ssamstatic __inline u_int16_t
63189090Sedufs_rw16(u_int16_t a, int ns)
64185222Ssam{
65185222Ssam	return ((ns) ?  bswap16(a) : (a));
66185222Ssam}
67185222Ssamstatic __inline u_int32_t
68189090Sedufs_rw32(u_int32_t a, int ns)
69185222Ssam{
70185222Ssam	return ((ns) ?  bswap32(a) : (a));
71185222Ssam}
72185222Ssamstatic __inline u_int64_t
73189090Sedufs_rw64(u_int64_t a, int ns)
74185222Ssam{
75185222Ssam	return ((ns) ?  bswap64(a) : (a));
76185222Ssam}
77185222Ssam#else
78185222Ssam#define ufs_rw16(a, ns) ((uint16_t)(a))
79185222Ssam#define ufs_rw32(a, ns) ((uint32_t)(a))
80185222Ssam#define ufs_rw64(a, ns) ((uint64_t)(a))
81185222Ssam#endif
82185222Ssam
83185222Ssam#define ufs_add16(a, b, ns) \
84185222Ssam	(a) = ufs_rw16(ufs_rw16((a), (ns)) + (b), (ns))
85185222Ssam#define ufs_add32(a, b, ns) \
86185222Ssam	(a) = ufs_rw32(ufs_rw32((a), (ns)) + (b), (ns))
87185222Ssam#define ufs_add64(a, b, ns) \
88185222Ssam	(a) = ufs_rw64(ufs_rw64((a), (ns)) + (b), (ns))
89185222Ssam
90185222Ssam#endif /* !_UFS_UFS_BSWAP_H_ */
91