ufs_bswap.h revision 253675
1296077Sadrian/*	$NetBSD: ufs_bswap.h,v 1.13 2003/10/05 17:48:50 bouyer Exp $	*/
2296077Sadrian
3296077Sadrian/*
4296077Sadrian * Copyright (c) 1998 Manuel Bouyer.
5296077Sadrian *
6296077Sadrian * Redistribution and use in source and binary forms, with or without
7296077Sadrian * modification, are permitted provided that the following conditions
8296077Sadrian * are met:
9296077Sadrian * 1. Redistributions of source code must retain the above copyright
10296077Sadrian *    notice, this list of conditions and the following disclaimer.
11296077Sadrian * 2. Redistributions in binary form must reproduce the above copyright
12296077Sadrian *    notice, this list of conditions and the following disclaimer in the
13296077Sadrian *    documentation and/or other materials provided with the distribution.
14296077Sadrian * 3. All advertising materials mentioning features or use of this software
15296077Sadrian *    must display the following acknowledgement:
16296077Sadrian *	This product includes software developed by Manuel Bouyer.
17296077Sadrian * 4. The name of the author may not be used to endorse or promote products
18296077Sadrian *    derived from this software without specific prior written permission.
19296077Sadrian *
20296077Sadrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21296077Sadrian * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22296077Sadrian * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23296077Sadrian * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24296077Sadrian * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25296077Sadrian * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26296077Sadrian * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27296077Sadrian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28296077Sadrian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29296077Sadrian * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30296077Sadrian *
31296077Sadrian * $FreeBSD: head/usr.sbin/makefs/ffs/ufs_bswap.h 253675 2013-07-26 14:22:03Z marius $
32296077Sadrian */
33296077Sadrian
34296077Sadrian#ifndef _UFS_UFS_BSWAP_H_
35296077Sadrian#define _UFS_UFS_BSWAP_H_
36296077Sadrian
37296077Sadrian#if defined(_KERNEL_OPT)
38296077Sadrian#include "opt_ffs.h"
39296077Sadrian#endif
40296077Sadrian
41296077Sadrian#include <sys/endian.h>
42296077Sadrian
43296077Sadrian#include "makefs.h"
44296077Sadrian
45296077Sadrian/* Macros to access UFS flags */
46296077Sadrian#ifdef FFS_EI
47296077Sadrian#define	UFS_MPNEEDSWAP(mp)	(VFSTOUFS(mp)->um_flags & UFS_NEEDSWAP)
48296077Sadrian#define UFS_FSNEEDSWAP(fs)	((fs)->fs_flags & FS_SWAPPED)
49296077Sadrian#define	UFS_IPNEEDSWAP(ip)	UFS_MPNEEDSWAP(ITOV(ip)->v_mount)
50296077Sadrian#else
51296077Sadrian#define	UFS_MPNEEDSWAP(mp) (0)
52296077Sadrian#define UFS_FSNEEDSWAP(fs) (0)
53296077Sadrian#define	UFS_IPNEEDSWAP(ip) (0)
54296077Sadrian#endif
55296077Sadrian
56296077Sadrian#if !defined(_KERNEL) || defined(FFS_EI)
57296077Sadrian/* inlines for access to swapped data */
58296077Sadrianstatic __inline u_int16_t ufs_rw16 __P((u_int16_t, int));
59296077Sadrianstatic __inline u_int32_t ufs_rw32 __P((u_int32_t, int));
60296077Sadrianstatic __inline u_int64_t ufs_rw64 __P((u_int64_t, int));
61296077Sadrian
62296077Sadrianstatic __inline u_int16_t
63296077Sadrianufs_rw16(u_int16_t a, int ns)
64296077Sadrian{
65296077Sadrian	return ((ns) ?  bswap16(a) : (a));
66296077Sadrian}
67296077Sadrianstatic __inline u_int32_t
68296077Sadrianufs_rw32(u_int32_t a, int ns)
69296077Sadrian{
70296077Sadrian	return ((ns) ?  bswap32(a) : (a));
71296077Sadrian}
72296077Sadrianstatic __inline u_int64_t
73296077Sadrianufs_rw64(u_int64_t a, int ns)
74296077Sadrian{
75296077Sadrian	return ((ns) ?  bswap64(a) : (a));
76296077Sadrian}
77296077Sadrian#else
78296077Sadrian#define ufs_rw16(a, ns) ((uint16_t)(a))
79296077Sadrian#define ufs_rw32(a, ns) ((uint32_t)(a))
80296077Sadrian#define ufs_rw64(a, ns) ((uint64_t)(a))
81296077Sadrian#endif
82296077Sadrian
83296077Sadrian#define ufs_add16(a, b, ns) \
84296077Sadrian	(a) = ufs_rw16(ufs_rw16((a), (ns)) + (b), (ns))
85296077Sadrian#define ufs_add32(a, b, ns) \
86296077Sadrian	(a) = ufs_rw32(ufs_rw32((a), (ns)) + (b), (ns))
87296077Sadrian#define ufs_add64(a, b, ns) \
88296077Sadrian	(a) = ufs_rw64(ufs_rw64((a), (ns)) + (b), (ns))
89296077Sadrian
90296077Sadrian#endif /* !_UFS_UFS_BSWAP_H_ */
91296077Sadrian