ufs_bswap.h revision 189090
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: head/usr.sbin/makefs/ffs/ufs_bswap.h 189090 2009-02-26 20:59:05Z ed $ 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 43185222Ssam/* Macros to access UFS flags */ 44185222Ssam#ifdef FFS_EI 45185222Ssam#define UFS_MPNEEDSWAP(mp) (VFSTOUFS(mp)->um_flags & UFS_NEEDSWAP) 46185222Ssam#define UFS_FSNEEDSWAP(fs) ((fs)->fs_flags & FS_SWAPPED) 47185222Ssam#define UFS_IPNEEDSWAP(ip) UFS_MPNEEDSWAP(ITOV(ip)->v_mount) 48185222Ssam#else 49185222Ssam#define UFS_MPNEEDSWAP(mp) (0) 50185222Ssam#define UFS_FSNEEDSWAP(fs) (0) 51185222Ssam#define UFS_IPNEEDSWAP(ip) (0) 52185222Ssam#endif 53185222Ssam 54185222Ssam#if !defined(_KERNEL) || defined(FFS_EI) 55185222Ssam/* inlines for access to swapped data */ 56185222Ssamstatic __inline u_int16_t ufs_rw16 __P((u_int16_t, int)); 57185222Ssamstatic __inline u_int32_t ufs_rw32 __P((u_int32_t, int)); 58185222Ssamstatic __inline u_int64_t ufs_rw64 __P((u_int64_t, int)); 59185222Ssam 60185222Ssamstatic __inline u_int16_t 61189090Sedufs_rw16(u_int16_t a, int ns) 62185222Ssam{ 63185222Ssam return ((ns) ? bswap16(a) : (a)); 64185222Ssam} 65185222Ssamstatic __inline u_int32_t 66189090Sedufs_rw32(u_int32_t a, int ns) 67185222Ssam{ 68185222Ssam return ((ns) ? bswap32(a) : (a)); 69185222Ssam} 70185222Ssamstatic __inline u_int64_t 71189090Sedufs_rw64(u_int64_t a, int ns) 72185222Ssam{ 73185222Ssam return ((ns) ? bswap64(a) : (a)); 74185222Ssam} 75185222Ssam#else 76185222Ssam#define ufs_rw16(a, ns) ((uint16_t)(a)) 77185222Ssam#define ufs_rw32(a, ns) ((uint32_t)(a)) 78185222Ssam#define ufs_rw64(a, ns) ((uint64_t)(a)) 79185222Ssam#endif 80185222Ssam 81185222Ssam#define ufs_add16(a, b, ns) \ 82185222Ssam (a) = ufs_rw16(ufs_rw16((a), (ns)) + (b), (ns)) 83185222Ssam#define ufs_add32(a, b, ns) \ 84185222Ssam (a) = ufs_rw32(ufs_rw32((a), (ns)) + (b), (ns)) 85185222Ssam#define ufs_add64(a, b, ns) \ 86185222Ssam (a) = ufs_rw64(ufs_rw64((a), (ns)) + (b), (ns)) 87185222Ssam 88185222Ssam#endif /* !_UFS_UFS_BSWAP_H_ */ 89