ufs_bswap.h revision 186261
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 * 31185222Ssam */ 32185222Ssam 33185222Ssam#ifndef _UFS_UFS_BSWAP_H_ 34185222Ssam#define _UFS_UFS_BSWAP_H_ 35185222Ssam 36185222Ssam#if defined(_KERNEL_OPT) 37185222Ssam#include "opt_ffs.h" 38185222Ssam#endif 39185222Ssam 40186261Ssam#include <sys/endian.h> 41185222Ssam 42185222Ssam/* Macros to access UFS flags */ 43185222Ssam#ifdef FFS_EI 44185222Ssam#define UFS_MPNEEDSWAP(mp) (VFSTOUFS(mp)->um_flags & UFS_NEEDSWAP) 45185222Ssam#define UFS_FSNEEDSWAP(fs) ((fs)->fs_flags & FS_SWAPPED) 46185222Ssam#define UFS_IPNEEDSWAP(ip) UFS_MPNEEDSWAP(ITOV(ip)->v_mount) 47185222Ssam#else 48185222Ssam#define UFS_MPNEEDSWAP(mp) (0) 49185222Ssam#define UFS_FSNEEDSWAP(fs) (0) 50185222Ssam#define UFS_IPNEEDSWAP(ip) (0) 51185222Ssam#endif 52185222Ssam 53185222Ssam#if !defined(_KERNEL) || defined(FFS_EI) 54185222Ssam/* inlines for access to swapped data */ 55185222Ssamstatic __inline u_int16_t ufs_rw16 __P((u_int16_t, int)); 56185222Ssamstatic __inline u_int32_t ufs_rw32 __P((u_int32_t, int)); 57185222Ssamstatic __inline u_int64_t ufs_rw64 __P((u_int64_t, int)); 58185222Ssam 59185222Ssamstatic __inline u_int16_t 60185222Ssamufs_rw16(a, ns) 61185222Ssam u_int16_t a; 62185222Ssam int ns; 63185222Ssam{ 64185222Ssam return ((ns) ? bswap16(a) : (a)); 65185222Ssam} 66185222Ssamstatic __inline u_int32_t 67185222Ssamufs_rw32(a, ns) 68185222Ssam u_int32_t a; 69185222Ssam int ns; 70185222Ssam{ 71185222Ssam return ((ns) ? bswap32(a) : (a)); 72185222Ssam} 73185222Ssamstatic __inline u_int64_t 74185222Ssamufs_rw64(a, ns) 75185222Ssam u_int64_t a; 76185222Ssam int ns; 77185222Ssam{ 78185222Ssam return ((ns) ? bswap64(a) : (a)); 79185222Ssam} 80185222Ssam#else 81185222Ssam#define ufs_rw16(a, ns) ((uint16_t)(a)) 82185222Ssam#define ufs_rw32(a, ns) ((uint32_t)(a)) 83185222Ssam#define ufs_rw64(a, ns) ((uint64_t)(a)) 84185222Ssam#endif 85185222Ssam 86185222Ssam#define ufs_add16(a, b, ns) \ 87185222Ssam (a) = ufs_rw16(ufs_rw16((a), (ns)) + (b), (ns)) 88185222Ssam#define ufs_add32(a, b, ns) \ 89185222Ssam (a) = ufs_rw32(ufs_rw32((a), (ns)) + (b), (ns)) 90185222Ssam#define ufs_add64(a, b, ns) \ 91185222Ssam (a) = ufs_rw64(ufs_rw64((a), (ns)) + (b), (ns)) 92185222Ssam 93185222Ssam#endif /* !_UFS_UFS_BSWAP_H_ */ 94