ufs_bswap.h revision 196696
1249259Sdim/* $NetBSD: ufs_bswap.h,v 1.13 2003/10/05 17:48:50 bouyer Exp $ */ 2249259Sdim 3249259Sdim/* 4249259Sdim * Copyright (c) 1998 Manuel Bouyer. 5249259Sdim * 6249259Sdim * Redistribution and use in source and binary forms, with or without 7249259Sdim * modification, are permitted provided that the following conditions 8249259Sdim * are met: 9249259Sdim * 1. Redistributions of source code must retain the above copyright 10249259Sdim * notice, this list of conditions and the following disclaimer. 11249259Sdim * 2. Redistributions in binary form must reproduce the above copyright 12249259Sdim * notice, this list of conditions and the following disclaimer in the 13249259Sdim * documentation and/or other materials provided with the distribution. 14249259Sdim * 3. All advertising materials mentioning features or use of this software 15249259Sdim * must display the following acknowledgement: 16249259Sdim * This product includes software developed by Manuel Bouyer. 17249259Sdim * 4. The name of the author may not be used to endorse or promote products 18249259Sdim * derived from this software without specific prior written permission. 19249259Sdim * 20249259Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21249259Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22249259Sdim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23249259Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24249259Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25249259Sdim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26249259Sdim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27249259Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28249259Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29249259Sdim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30249259Sdim * 31249259Sdim * $FreeBSD: head/usr.sbin/makefs/ffs/ufs_bswap.h 196696 2009-08-31 13:23:55Z jhb $ 32249259Sdim */ 33249259Sdim 34249259Sdim#ifndef _UFS_UFS_BSWAP_H_ 35249259Sdim#define _UFS_UFS_BSWAP_H_ 36249259Sdim 37249259Sdim#if defined(_KERNEL_OPT) 38251662Sdim#include "opt_ffs.h" 39251662Sdim#endif 40249259Sdim 41249259Sdim#include <sys/endian.h> 42249259Sdim 43249259Sdim/* Macros to access UFS flags */ 44249259Sdim#ifdef FFS_EI 45249259Sdim#define UFS_MPNEEDSWAP(mp) (VFSTOUFS(mp)->um_flags & UFS_NEEDSWAP) 46249259Sdim#define UFS_FSNEEDSWAP(fs) ((fs)->fs_flags & FS_SWAPPED) 47249259Sdim#define UFS_IPNEEDSWAP(ip) UFS_MPNEEDSWAP(ITOV(ip)->v_mount) 48249259Sdim#else 49249259Sdim#define UFS_MPNEEDSWAP(mp) (0) 50249259Sdim#define UFS_FSNEEDSWAP(fs) (0) 51251662Sdim#define UFS_IPNEEDSWAP(ip) (0) 52251662Sdim#endif 53249259Sdim 54249259Sdim#if !defined(_KERNEL) || defined(FFS_EI) 55249259Sdim/* inlines for access to swapped data */ 56249259Sdimstatic __inline u_int16_t ufs_rw16 __P((u_int16_t, int)); 57249259Sdimstatic __inline u_int32_t ufs_rw32 __P((u_int32_t, int)); 58static __inline u_int64_t ufs_rw64 __P((u_int64_t, int)); 59 60static __inline u_int16_t 61ufs_rw16(u_int16_t a, int ns) 62{ 63 return ((ns) ? bswap16(a) : (a)); 64} 65static __inline u_int32_t 66ufs_rw32(u_int32_t a, int ns) 67{ 68 return ((ns) ? bswap32(a) : (a)); 69} 70static __inline u_int64_t 71ufs_rw64(u_int64_t a, int ns) 72{ 73 return ((ns) ? bswap64(a) : (a)); 74} 75#else 76#define ufs_rw16(a, ns) ((uint16_t)(a)) 77#define ufs_rw32(a, ns) ((uint32_t)(a)) 78#define ufs_rw64(a, ns) ((uint64_t)(a)) 79#endif 80 81#define ufs_add16(a, b, ns) \ 82 (a) = ufs_rw16(ufs_rw16((a), (ns)) + (b), (ns)) 83#define ufs_add32(a, b, ns) \ 84 (a) = ufs_rw32(ufs_rw32((a), (ns)) + (b), (ns)) 85#define ufs_add64(a, b, ns) \ 86 (a) = ufs_rw64(ufs_rw64((a), (ns)) + (b), (ns)) 87 88#endif /* !_UFS_UFS_BSWAP_H_ */ 89