1/*	$NetBSD: ufs_bswap.h,v 1.13 2003/10/05 17:48:50 bouyer 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 Manuel Bouyer.
17 * 4. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * $FreeBSD$
32 */
33
34#ifndef _UFS_UFS_BSWAP_H_
35#define _UFS_UFS_BSWAP_H_
36
37#if defined(_KERNEL_OPT)
38#include "opt_ffs.h"
39#endif
40
41#include <sys/endian.h>
42
43#include "makefs.h"
44
45/* Macros to access UFS flags */
46#ifdef FFS_EI
47#define	UFS_MPNEEDSWAP(mp)	(VFSTOUFS(mp)->um_flags & UFS_NEEDSWAP)
48#define UFS_FSNEEDSWAP(fs)	((fs)->fs_flags & FS_SWAPPED)
49#define	UFS_IPNEEDSWAP(ip)	UFS_MPNEEDSWAP(ITOV(ip)->v_mount)
50#else
51#define	UFS_MPNEEDSWAP(mp) (0)
52#define UFS_FSNEEDSWAP(fs) (0)
53#define	UFS_IPNEEDSWAP(ip) (0)
54#endif
55
56#if !defined(_KERNEL) || defined(FFS_EI)
57/* inlines for access to swapped data */
58static __inline u_int16_t ufs_rw16 __P((u_int16_t, int));
59static __inline u_int32_t ufs_rw32 __P((u_int32_t, int));
60static __inline u_int64_t ufs_rw64 __P((u_int64_t, int));
61
62static __inline u_int16_t
63ufs_rw16(u_int16_t a, int ns)
64{
65	return ((ns) ?  bswap16(a) : (a));
66}
67static __inline u_int32_t
68ufs_rw32(u_int32_t a, int ns)
69{
70	return ((ns) ?  bswap32(a) : (a));
71}
72static __inline u_int64_t
73ufs_rw64(u_int64_t a, int ns)
74{
75	return ((ns) ?  bswap64(a) : (a));
76}
77#else
78#define ufs_rw16(a, ns) ((uint16_t)(a))
79#define ufs_rw32(a, ns) ((uint32_t)(a))
80#define ufs_rw64(a, ns) ((uint64_t)(a))
81#endif
82
83#define ufs_add16(a, b, ns) \
84	(a) = ufs_rw16(ufs_rw16((a), (ns)) + (b), (ns))
85#define ufs_add32(a, b, ns) \
86	(a) = ufs_rw32(ufs_rw32((a), (ns)) + (b), (ns))
87#define ufs_add64(a, b, ns) \
88	(a) = ufs_rw64(ufs_rw64((a), (ns)) + (b), (ns))
89
90#endif /* !_UFS_UFS_BSWAP_H_ */
91