Home | History | Annotate | Line # | Download | only in lfs
lfs_accessors.h revision 1.35
      1 /*	$NetBSD: lfs_accessors.h,v 1.35 2015/10/03 08:28:16 dholland Exp $	*/
      2 
      3 /*  from NetBSD: lfs.h,v 1.165 2015/07/24 06:59:32 dholland Exp  */
      4 /*  from NetBSD: dinode.h,v 1.22 2013/01/22 09:39:18 dholland Exp  */
      5 /*  from NetBSD: dir.h,v 1.21 2009/07/22 04:49:19 dholland Exp  */
      6 
      7 /*-
      8  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      9  * All rights reserved.
     10  *
     11  * This code is derived from software contributed to The NetBSD Foundation
     12  * by Konrad E. Schroder <perseant (at) hhhh.org>.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 /*-
     36  * Copyright (c) 1991, 1993
     37  *	The Regents of the University of California.  All rights reserved.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. Neither the name of the University nor the names of its contributors
     48  *    may be used to endorse or promote products derived from this software
     49  *    without specific prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  *
     63  *	@(#)lfs.h	8.9 (Berkeley) 5/8/95
     64  */
     65 /*
     66  * Copyright (c) 2002 Networks Associates Technology, Inc.
     67  * All rights reserved.
     68  *
     69  * This software was developed for the FreeBSD Project by Marshall
     70  * Kirk McKusick and Network Associates Laboratories, the Security
     71  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
     72  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
     73  * research program
     74  *
     75  * Copyright (c) 1982, 1989, 1993
     76  *	The Regents of the University of California.  All rights reserved.
     77  * (c) UNIX System Laboratories, Inc.
     78  * All or some portions of this file are derived from material licensed
     79  * to the University of California by American Telephone and Telegraph
     80  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     81  * the permission of UNIX System Laboratories, Inc.
     82  *
     83  * Redistribution and use in source and binary forms, with or without
     84  * modification, are permitted provided that the following conditions
     85  * are met:
     86  * 1. Redistributions of source code must retain the above copyright
     87  *    notice, this list of conditions and the following disclaimer.
     88  * 2. Redistributions in binary form must reproduce the above copyright
     89  *    notice, this list of conditions and the following disclaimer in the
     90  *    documentation and/or other materials provided with the distribution.
     91  * 3. Neither the name of the University nor the names of its contributors
     92  *    may be used to endorse or promote products derived from this software
     93  *    without specific prior written permission.
     94  *
     95  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     96  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     97  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     98  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     99  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    100  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    101  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    102  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    103  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    104  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    105  * SUCH DAMAGE.
    106  *
    107  *	@(#)dinode.h	8.9 (Berkeley) 3/29/95
    108  */
    109 /*
    110  * Copyright (c) 1982, 1986, 1989, 1993
    111  *	The Regents of the University of California.  All rights reserved.
    112  * (c) UNIX System Laboratories, Inc.
    113  * All or some portions of this file are derived from material licensed
    114  * to the University of California by American Telephone and Telegraph
    115  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
    116  * the permission of UNIX System Laboratories, Inc.
    117  *
    118  * Redistribution and use in source and binary forms, with or without
    119  * modification, are permitted provided that the following conditions
    120  * are met:
    121  * 1. Redistributions of source code must retain the above copyright
    122  *    notice, this list of conditions and the following disclaimer.
    123  * 2. Redistributions in binary form must reproduce the above copyright
    124  *    notice, this list of conditions and the following disclaimer in the
    125  *    documentation and/or other materials provided with the distribution.
    126  * 3. Neither the name of the University nor the names of its contributors
    127  *    may be used to endorse or promote products derived from this software
    128  *    without specific prior written permission.
    129  *
    130  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    131  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    132  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    133  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    134  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    135  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    136  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    137  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    138  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    139  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    140  * SUCH DAMAGE.
    141  *
    142  *	@(#)dir.h	8.5 (Berkeley) 4/27/95
    143  */
    144 
    145 #ifndef _UFS_LFS_LFS_ACCESSORS_H_
    146 #define _UFS_LFS_LFS_ACCESSORS_H_
    147 
    148 #if defined(_KERNEL_OPT)
    149 #include "opt_lfs.h"
    150 #endif
    151 
    152 #include <sys/bswap.h>
    153 
    154 #if !defined(_KERNEL) && !defined(_STANDALONE)
    155 #include <assert.h>
    156 #define KASSERT assert
    157 #endif
    158 
    159 /*
    160  * STRUCT_LFS is used by the libsa code to get accessors that work
    161  * with struct salfs instead of struct lfs, and by the cleaner to
    162  * get accessors that work with struct clfs.
    163  */
    164 
    165 #ifndef STRUCT_LFS
    166 #define STRUCT_LFS struct lfs
    167 #endif
    168 
    169 /*
    170  * byte order
    171  */
    172 
    173 /*
    174  * For now at least, the bootblocks shall not be endian-independent.
    175  * We can see later if it fits in the size budget. Also disable the
    176  * byteswapping if LFS_EI is off.
    177  *
    178  * Caution: these functions "know" that bswap16/32/64 are unsigned,
    179  * and if that changes will likely break silently.
    180  */
    181 
    182 #if defined(_STANDALONE) || (defined(_KERNEL) && !defined(LFS_EI))
    183 #define LFS_SWAP_int16_t(fs, val) (val)
    184 #define LFS_SWAP_int32_t(fs, val) (val)
    185 #define LFS_SWAP_int64_t(fs, val) (val)
    186 #define LFS_SWAP_uint16_t(fs, val) (val)
    187 #define LFS_SWAP_uint32_t(fs, val) (val)
    188 #define LFS_SWAP_uint64_t(fs, val) (val)
    189 #else
    190 #define LFS_SWAP_int16_t(fs, val) \
    191 	((fs)->lfs_dobyteswap ? (int16_t)bswap16(val) : (val))
    192 #define LFS_SWAP_int32_t(fs, val) \
    193 	((fs)->lfs_dobyteswap ? (int32_t)bswap32(val) : (val))
    194 #define LFS_SWAP_int64_t(fs, val) \
    195 	((fs)->lfs_dobyteswap ? (int64_t)bswap64(val) : (val))
    196 #define LFS_SWAP_uint16_t(fs, val) \
    197 	((fs)->lfs_dobyteswap ? bswap16(val) : (val))
    198 #define LFS_SWAP_uint32_t(fs, val) \
    199 	((fs)->lfs_dobyteswap ? bswap32(val) : (val))
    200 #define LFS_SWAP_uint64_t(fs, val) \
    201 	((fs)->lfs_dobyteswap ? bswap64(val) : (val))
    202 #endif
    203 
    204 /*
    205  * For handling directories we will need to know if the volume is
    206  * little-endian.
    207  */
    208 #if BYTE_ORDER == LITTLE_ENDIAN
    209 #define LFS_LITTLE_ENDIAN_ONDISK(fs) (!(fs)->lfs_dobyteswap)
    210 #else
    211 #define LFS_LITTLE_ENDIAN_ONDISK(fs) ((fs)->lfs_dobyteswap)
    212 #endif
    213 
    214 
    215 /*
    216  * directories
    217  */
    218 
    219 #define LFS_DIRHEADERSIZE(fs) \
    220 	((fs)->lfs_is64 ? sizeof(struct lfs_dirheader64) : sizeof(struct lfs_dirheader32))
    221 
    222 /*
    223  * The LFS_DIRSIZ macro gives the minimum record length which will hold
    224  * the directory entry.  This requires the amount of space in struct lfs_direct
    225  * without the d_name field, plus enough space for the name with a terminating
    226  * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
    227  */
    228 #define	LFS_DIRECTSIZ(fs, namlen) \
    229 	(LFS_DIRHEADERSIZE(fs) + (((namlen)+1 + 3) &~ 3))
    230 
    231 /*
    232  * The size of the largest possible directory entry. This is
    233  * used by ulfs_dirhash to figure the size of an array, so we
    234  * need a single constant value true for both lfs32 and lfs64.
    235  */
    236 #define LFS_MAXDIRENTRYSIZE \
    237 	(sizeof(struct lfs_dirheader64) + (((LFS_MAXNAMLEN+1)+1 + 3) & ~3))
    238 
    239 #if (BYTE_ORDER == LITTLE_ENDIAN)
    240 #define LFS_OLDDIRSIZ(oldfmt, dp, needswap)	\
    241     (((oldfmt) && !(needswap)) ?		\
    242     LFS_DIRECTSIZ((dp)->d_type) : LFS_DIRECTSIZ((dp)->d_namlen))
    243 #else
    244 #define LFS_OLDDIRSIZ(oldfmt, dp, needswap)	\
    245     (((oldfmt) && (needswap)) ?			\
    246     LFS_DIRECTSIZ((dp)->d_type) : LFS_DIRECTSIZ((dp)->d_namlen))
    247 #endif
    248 
    249 #define LFS_DIRSIZ(fs, dp) LFS_DIRECTSIZ(fs, lfs_dir_getnamlen(fs, dp))
    250 
    251 /* Constants for the first argument of LFS_OLDDIRSIZ */
    252 #define LFS_OLDDIRFMT	1
    253 #define LFS_NEWDIRFMT	0
    254 
    255 #define LFS_NEXTDIR(fs, dp) \
    256 	((LFS_DIRHEADER *)((char *)(dp) + lfs_dir_getreclen(fs, dp)))
    257 
    258 static __unused inline char *
    259 lfs_dir_nameptr(const STRUCT_LFS *fs, LFS_DIRHEADER *dh)
    260 {
    261 	if (fs->lfs_is64) {
    262 		return (char *)(&dh->u_64 + 1);
    263 	} else {
    264 		return (char *)(&dh->u_32 + 1);
    265 	}
    266 }
    267 
    268 static __unused inline uint64_t
    269 lfs_dir_getino(const STRUCT_LFS *fs, const LFS_DIRHEADER *dh)
    270 {
    271 	if (fs->lfs_is64) {
    272 		uint64_t ino;
    273 
    274 		/*
    275 		 * XXX we can probably write this in a way that's both
    276 		 * still legal and generates better code.
    277 		 */
    278 		memcpy(&ino, &dh->u_64.dh_inoA, sizeof(dh->u_64.dh_inoA));
    279 		memcpy((char *)&ino + sizeof(dh->u_64.dh_inoA),
    280 		       &dh->u_64.dh_inoB,
    281 		       sizeof(dh->u_64.dh_inoB));
    282 		return LFS_SWAP_uint64_t(fs, ino);
    283 	} else {
    284 		return LFS_SWAP_uint32_t(fs, dh->u_32.dh_ino);
    285 	}
    286 }
    287 
    288 static __unused inline uint16_t
    289 lfs_dir_getreclen(const STRUCT_LFS *fs, const LFS_DIRHEADER *dh)
    290 {
    291 	if (fs->lfs_is64) {
    292 		return LFS_SWAP_uint16_t(fs, dh->u_64.dh_reclen);
    293 	} else {
    294 		return LFS_SWAP_uint16_t(fs, dh->u_32.dh_reclen);
    295 	}
    296 }
    297 
    298 static __unused inline uint8_t
    299 lfs_dir_gettype(const STRUCT_LFS *fs, const LFS_DIRHEADER *dh)
    300 {
    301 	if (fs->lfs_is64) {
    302 		KASSERT(fs->lfs_hasolddirfmt == 0);
    303 		return dh->u_64.dh_type;
    304 	} else if (fs->lfs_hasolddirfmt) {
    305 		return LFS_DT_UNKNOWN;
    306 	} else {
    307 		return dh->u_32.dh_type;
    308 	}
    309 }
    310 
    311 static __unused inline uint8_t
    312 lfs_dir_getnamlen(const STRUCT_LFS *fs, const LFS_DIRHEADER *dh)
    313 {
    314 	if (fs->lfs_is64) {
    315 		KASSERT(fs->lfs_hasolddirfmt == 0);
    316 		return dh->u_64.dh_type;
    317 	} else if (fs->lfs_hasolddirfmt && LFS_LITTLE_ENDIAN_ONDISK(fs)) {
    318 		/* low-order byte of old 16-bit namlen field */
    319 		return dh->u_32.dh_type;
    320 	} else {
    321 		return dh->u_32.dh_namlen;
    322 	}
    323 }
    324 
    325 static __unused inline void
    326 lfs_dir_setino(STRUCT_LFS *fs, LFS_DIRHEADER *dh, uint64_t ino)
    327 {
    328 	if (fs->lfs_is64) {
    329 
    330 		ino = LFS_SWAP_uint64_t(fs, ino);
    331 		/*
    332 		 * XXX we can probably write this in a way that's both
    333 		 * still legal and generates better code.
    334 		 */
    335 		memcpy(&dh->u_64.dh_inoA, &ino, sizeof(dh->u_64.dh_inoA));
    336 		memcpy(&dh->u_64.dh_inoB,
    337 		       (char *)&ino + sizeof(dh->u_64.dh_inoA),
    338 		       sizeof(dh->u_64.dh_inoB));
    339 	} else {
    340 		dh->u_32.dh_ino = LFS_SWAP_uint32_t(fs, ino);
    341 	}
    342 }
    343 
    344 static __unused inline void
    345 lfs_dir_setreclen(STRUCT_LFS *fs, LFS_DIRHEADER *dh, uint16_t reclen)
    346 {
    347 	if (fs->lfs_is64) {
    348 		dh->u_64.dh_reclen = LFS_SWAP_uint16_t(fs, reclen);
    349 	} else {
    350 		dh->u_32.dh_reclen = LFS_SWAP_uint16_t(fs, reclen);
    351 	}
    352 }
    353 
    354 static __unused inline void
    355 lfs_dir_settype(const STRUCT_LFS *fs, LFS_DIRHEADER *dh, uint8_t type)
    356 {
    357 	if (fs->lfs_is64) {
    358 		KASSERT(fs->lfs_hasolddirfmt == 0);
    359 		dh->u_64.dh_type = type;
    360 	} else if (fs->lfs_hasolddirfmt) {
    361 		/* do nothing */
    362 		return;
    363 	} else {
    364 		dh->u_32.dh_type = type;
    365 	}
    366 }
    367 
    368 static __unused inline void
    369 lfs_dir_setnamlen(const STRUCT_LFS *fs, LFS_DIRHEADER *dh, uint8_t namlen)
    370 {
    371 	if (fs->lfs_is64) {
    372 		KASSERT(fs->lfs_hasolddirfmt == 0);
    373 		dh->u_64.dh_namlen = namlen;
    374 	} else if (fs->lfs_hasolddirfmt && LFS_LITTLE_ENDIAN_ONDISK(fs)) {
    375 		/* low-order byte of old 16-bit namlen field */
    376 		dh->u_32.dh_type = namlen;
    377 	} else {
    378 		dh->u_32.dh_namlen = namlen;
    379 	}
    380 }
    381 
    382 static __unused inline void
    383 lfs_copydirname(STRUCT_LFS *fs, char *dest, const char *src,
    384 		unsigned namlen, unsigned reclen)
    385 {
    386 	unsigned spacelen;
    387 
    388 	KASSERT(reclen > LFS_DIRHEADERSIZE(fs));
    389 	spacelen = reclen - LFS_DIRHEADERSIZE(fs);
    390 
    391 	/* must always be at least 1 byte as a null terminator */
    392 	KASSERT(spacelen > namlen);
    393 
    394 	memcpy(dest, src, namlen);
    395 	memset(dest + namlen, '\0', spacelen - namlen);
    396 }
    397 
    398 static __unused LFS_DIRHEADER *
    399 lfs_dirtemplate_dotdot(STRUCT_LFS *fs, union lfs_dirtemplate *dt)
    400 {
    401 	/* XXX blah, be nice to have a way to do this w/o casts */
    402 	if (fs->lfs_is64) {
    403 		return (LFS_DIRHEADER *)&dt->u_64.dotdot_header;
    404 	} else {
    405 		return (LFS_DIRHEADER *)&dt->u_32.dotdot_header;
    406 	}
    407 }
    408 
    409 static __unused char *
    410 lfs_dirtemplate_dotdotname(STRUCT_LFS *fs, union lfs_dirtemplate *dt)
    411 {
    412 	if (fs->lfs_is64) {
    413 		return dt->u_64.dotdot_name;
    414 	} else {
    415 		return dt->u_32.dotdot_name;
    416 	}
    417 }
    418 
    419 /*
    420  * dinodes
    421  */
    422 
    423 /*
    424  * Maximum length of a symlink that can be stored within the inode.
    425  */
    426 #define LFS32_MAXSYMLINKLEN	((ULFS_NDADDR + ULFS_NIADDR) * sizeof(int32_t))
    427 #define LFS64_MAXSYMLINKLEN	((ULFS_NDADDR + ULFS_NIADDR) * sizeof(int64_t))
    428 
    429 #define LFS_MAXSYMLINKLEN(fs) \
    430 	((fs)->lfs_is64 ? LFS64_MAXSYMLINKLEN : LFS32_MAXSYMLINKLEN)
    431 
    432 #define DINOSIZE(fs) ((fs)->lfs_is64 ? sizeof(struct lfs64_dinode) : sizeof(struct lfs32_dinode))
    433 
    434 #define DINO_IN_BLOCK(fs, base, ix) \
    435 	((union lfs_dinode *)((char *)(base) + DINOSIZE(fs) * (ix)))
    436 
    437 static __unused inline void
    438 lfs_copy_dinode(STRUCT_LFS *fs,
    439     union lfs_dinode *dst, const union lfs_dinode *src)
    440 {
    441 	/*
    442 	 * We can do structure assignment of the structs, but not of
    443 	 * the whole union, as the union is the size of the (larger)
    444 	 * 64-bit struct and on a 32-bit fs the upper half of it might
    445 	 * be off the end of a buffer or otherwise invalid.
    446 	 */
    447 	if (fs->lfs_is64) {
    448 		dst->u_64 = src->u_64;
    449 	} else {
    450 		dst->u_32 = src->u_32;
    451 	}
    452 }
    453 
    454 #define LFS_DEF_DINO_ACCESSOR(type, type32, field) \
    455 	static __unused inline type				\
    456 	lfs_dino_get##field(STRUCT_LFS *fs, union lfs_dinode *dip) \
    457 	{							\
    458 		if (fs->lfs_is64) {				\
    459 			return LFS_SWAP_##type(fs, dip->u_64.di_##field); \
    460 		} else {					\
    461 			return LFS_SWAP_##type32(fs, dip->u_32.di_##field); \
    462 		}						\
    463 	}							\
    464 	static __unused inline void				\
    465 	lfs_dino_set##field(STRUCT_LFS *fs, union lfs_dinode *dip, type val) \
    466 	{							\
    467 		if (fs->lfs_is64) {				\
    468 			type *p = &dip->u_64.di_##field;	\
    469 			(void)p;				\
    470 			dip->u_64.di_##field = LFS_SWAP_##type(fs, val); \
    471 		} else {					\
    472 			type32 *p = &dip->u_32.di_##field;	\
    473 			(void)p;				\
    474 			dip->u_32.di_##field = LFS_SWAP_##type32(fs, val); \
    475 		}						\
    476 	}							\
    477 
    478 LFS_DEF_DINO_ACCESSOR(uint16_t, uint16_t, mode);
    479 LFS_DEF_DINO_ACCESSOR(int16_t, int16_t, nlink);
    480 LFS_DEF_DINO_ACCESSOR(uint64_t, uint32_t, inumber);
    481 LFS_DEF_DINO_ACCESSOR(uint64_t, uint64_t, size);
    482 LFS_DEF_DINO_ACCESSOR(int64_t, int32_t, atime);
    483 LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, atimensec);
    484 LFS_DEF_DINO_ACCESSOR(int64_t, int32_t, mtime);
    485 LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, mtimensec);
    486 LFS_DEF_DINO_ACCESSOR(int64_t, int32_t, ctime);
    487 LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, ctimensec);
    488 LFS_DEF_DINO_ACCESSOR(uint32_t, uint32_t, flags);
    489 LFS_DEF_DINO_ACCESSOR(uint64_t, uint32_t, blocks);
    490 LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, gen);
    491 LFS_DEF_DINO_ACCESSOR(uint32_t, uint32_t, uid);
    492 LFS_DEF_DINO_ACCESSOR(uint32_t, uint32_t, gid);
    493 
    494 /* XXX this should be done differently (it's a fake field) */
    495 LFS_DEF_DINO_ACCESSOR(uint64_t, int32_t, rdev);
    496 
    497 static __unused inline daddr_t
    498 lfs_dino_getdb(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix)
    499 {
    500 	KASSERT(ix < ULFS_NDADDR);
    501 	if (fs->lfs_is64) {
    502 		return dip->u_64.di_db[ix];
    503 	} else {
    504 		return dip->u_32.di_db[ix];
    505 	}
    506 }
    507 
    508 static __unused inline daddr_t
    509 lfs_dino_getib(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix)
    510 {
    511 	KASSERT(ix < ULFS_NIADDR);
    512 	if (fs->lfs_is64) {
    513 		return dip->u_64.di_ib[ix];
    514 	} else {
    515 		return dip->u_32.di_ib[ix];
    516 	}
    517 }
    518 
    519 static __unused inline void
    520 lfs_dino_setdb(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix, daddr_t val)
    521 {
    522 	KASSERT(ix < ULFS_NDADDR);
    523 	if (fs->lfs_is64) {
    524 		dip->u_64.di_db[ix] = val;
    525 	} else {
    526 		dip->u_32.di_db[ix] = val;
    527 	}
    528 }
    529 
    530 static __unused inline void
    531 lfs_dino_setib(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix, daddr_t val)
    532 {
    533 	KASSERT(ix < ULFS_NIADDR);
    534 	if (fs->lfs_is64) {
    535 		dip->u_64.di_ib[ix] = val;
    536 	} else {
    537 		dip->u_32.di_ib[ix] = val;
    538 	}
    539 }
    540 
    541 /* birthtime is present only in the 64-bit inode */
    542 static __unused inline void
    543 lfs_dino_setbirthtime(STRUCT_LFS *fs, union lfs_dinode *dip,
    544     const struct timespec *ts)
    545 {
    546 	if (fs->lfs_is64) {
    547 		dip->u_64.di_birthtime = ts->tv_sec;
    548 		dip->u_64.di_birthnsec = ts->tv_nsec;
    549 	} else {
    550 		/* drop it on the floor */
    551 	}
    552 }
    553 
    554 /*
    555  * indirect blocks
    556  */
    557 
    558 static __unused inline daddr_t
    559 lfs_iblock_get(STRUCT_LFS *fs, void *block, unsigned ix)
    560 {
    561 	if (fs->lfs_is64) {
    562 		// XXX re-enable these asserts after reorging this file
    563 		//KASSERT(ix < lfs_sb_getbsize(fs) / sizeof(int64_t));
    564 		return (daddr_t)(((int64_t *)block)[ix]);
    565 	} else {
    566 		//KASSERT(ix < lfs_sb_getbsize(fs) / sizeof(int32_t));
    567 		/* must sign-extend or UNWRITTEN gets trashed */
    568 		return (daddr_t)(int64_t)(((int32_t *)block)[ix]);
    569 	}
    570 }
    571 
    572 static __unused inline void
    573 lfs_iblock_set(STRUCT_LFS *fs, void *block, unsigned ix, daddr_t val)
    574 {
    575 	if (fs->lfs_is64) {
    576 		//KASSERT(ix < lfs_sb_getbsize(fs) / sizeof(int64_t));
    577 		((int64_t *)block)[ix] = val;
    578 	} else {
    579 		//KASSERT(ix < lfs_sb_getbsize(fs) / sizeof(int32_t));
    580 		((int32_t *)block)[ix] = val;
    581 	}
    582 }
    583 
    584 /*
    585  * "struct buf" associated definitions
    586  */
    587 
    588 # define LFS_LOCK_BUF(bp) do {						\
    589 	if (((bp)->b_flags & B_LOCKED) == 0 && bp->b_iodone == NULL) {	\
    590 		mutex_enter(&lfs_lock);					\
    591 		++locked_queue_count;					\
    592 		locked_queue_bytes += bp->b_bufsize;			\
    593 		mutex_exit(&lfs_lock);					\
    594 	}								\
    595 	(bp)->b_flags |= B_LOCKED;					\
    596 } while (0)
    597 
    598 # define LFS_UNLOCK_BUF(bp) do {					\
    599 	if (((bp)->b_flags & B_LOCKED) != 0 && bp->b_iodone == NULL) {	\
    600 		mutex_enter(&lfs_lock);					\
    601 		--locked_queue_count;					\
    602 		locked_queue_bytes -= bp->b_bufsize;			\
    603 		if (locked_queue_count < LFS_WAIT_BUFS &&		\
    604 		    locked_queue_bytes < LFS_WAIT_BYTES)		\
    605 			cv_broadcast(&locked_queue_cv);			\
    606 		mutex_exit(&lfs_lock);					\
    607 	}								\
    608 	(bp)->b_flags &= ~B_LOCKED;					\
    609 } while (0)
    610 
    611 /*
    612  * "struct inode" associated definitions
    613  */
    614 
    615 #define LFS_SET_UINO(ip, flags) do {					\
    616 	if (((flags) & IN_ACCESSED) && !((ip)->i_flag & IN_ACCESSED))	\
    617 		lfs_sb_adduinodes((ip)->i_lfs, 1);			\
    618 	if (((flags) & IN_CLEANING) && !((ip)->i_flag & IN_CLEANING))	\
    619 		lfs_sb_adduinodes((ip)->i_lfs, 1);			\
    620 	if (((flags) & IN_MODIFIED) && !((ip)->i_flag & IN_MODIFIED))	\
    621 		lfs_sb_adduinodes((ip)->i_lfs, 1);			\
    622 	(ip)->i_flag |= (flags);					\
    623 } while (0)
    624 
    625 #define LFS_CLR_UINO(ip, flags) do {					\
    626 	if (((flags) & IN_ACCESSED) && ((ip)->i_flag & IN_ACCESSED))	\
    627 		lfs_sb_subuinodes((ip)->i_lfs, 1);			\
    628 	if (((flags) & IN_CLEANING) && ((ip)->i_flag & IN_CLEANING))	\
    629 		lfs_sb_subuinodes((ip)->i_lfs, 1);			\
    630 	if (((flags) & IN_MODIFIED) && ((ip)->i_flag & IN_MODIFIED))	\
    631 		lfs_sb_subuinodes((ip)->i_lfs, 1);			\
    632 	(ip)->i_flag &= ~(flags);					\
    633 	if (lfs_sb_getuinodes((ip)->i_lfs) < 0) {			\
    634 		panic("lfs_uinodes < 0");				\
    635 	}								\
    636 } while (0)
    637 
    638 #define LFS_ITIMES(ip, acc, mod, cre) \
    639 	while ((ip)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY)) \
    640 		lfs_itimes(ip, acc, mod, cre)
    641 
    642 /*
    643  * On-disk and in-memory checkpoint segment usage structure.
    644  */
    645 
    646 #define	SEGUPB(fs)	(lfs_sb_getsepb(fs))
    647 #define	SEGTABSIZE_SU(fs)						\
    648 	((lfs_sb_getnseg(fs) + SEGUPB(fs) - 1) / lfs_sb_getsepb(fs))
    649 
    650 #ifdef _KERNEL
    651 # define SHARE_IFLOCK(F) 						\
    652   do {									\
    653 	rw_enter(&(F)->lfs_iflock, RW_READER);				\
    654   } while(0)
    655 # define UNSHARE_IFLOCK(F)						\
    656   do {									\
    657 	rw_exit(&(F)->lfs_iflock);					\
    658   } while(0)
    659 #else /* ! _KERNEL */
    660 # define SHARE_IFLOCK(F)
    661 # define UNSHARE_IFLOCK(F)
    662 #endif /* ! _KERNEL */
    663 
    664 /* Read in the block with a specific segment usage entry from the ifile. */
    665 #define	LFS_SEGENTRY(SP, F, IN, BP) do {				\
    666 	int _e;								\
    667 	SHARE_IFLOCK(F);						\
    668 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    669 	if ((_e = bread((F)->lfs_ivnode,				\
    670 	    ((IN) / lfs_sb_getsepb(F)) + lfs_sb_getcleansz(F),		\
    671 	    lfs_sb_getbsize(F), 0, &(BP))) != 0)			\
    672 		panic("lfs: ifile read: %d", _e);			\
    673 	if (lfs_sb_getversion(F) == 1)					\
    674 		(SP) = (SEGUSE *)((SEGUSE_V1 *)(BP)->b_data +		\
    675 			((IN) & (lfs_sb_getsepb(F) - 1)));		\
    676 	else								\
    677 		(SP) = (SEGUSE *)(BP)->b_data + ((IN) % lfs_sb_getsepb(F)); \
    678 	UNSHARE_IFLOCK(F);						\
    679 } while (0)
    680 
    681 #define LFS_WRITESEGENTRY(SP, F, IN, BP) do {				\
    682 	if ((SP)->su_nbytes == 0)					\
    683 		(SP)->su_flags |= SEGUSE_EMPTY;				\
    684 	else								\
    685 		(SP)->su_flags &= ~SEGUSE_EMPTY;			\
    686 	(F)->lfs_suflags[(F)->lfs_activesb][(IN)] = (SP)->su_flags;	\
    687 	LFS_BWRITE_LOG(BP);						\
    688 } while (0)
    689 
    690 /*
    691  * FINFO (file info) entries.
    692  */
    693 
    694 /* Size of an on-disk block pointer, e.g. in an indirect block. */
    695 /* XXX: move to a more suitable location in this file */
    696 #define LFS_BLKPTRSIZE(fs) ((fs)->lfs_is64 ? sizeof(int64_t) : sizeof(int32_t))
    697 
    698 /* Size of an on-disk inode number. */
    699 /* XXX: move to a more suitable location in this file */
    700 #define LFS_INUMSIZE(fs) ((fs)->lfs_is64 ? sizeof(int64_t) : sizeof(int32_t))
    701 
    702 /* size of a FINFO, without the block pointers */
    703 #define	FINFOSIZE(fs)	((fs)->lfs_is64 ? sizeof(FINFO64) : sizeof(FINFO32))
    704 
    705 /* Full size of the provided FINFO record, including its block pointers. */
    706 #define FINFO_FULLSIZE(fs, fip) \
    707 	(FINFOSIZE(fs) + lfs_fi_getnblocks(fs, fip) * LFS_BLKPTRSIZE(fs))
    708 
    709 #define NEXT_FINFO(fs, fip) \
    710 	((FINFO *)((char *)(fip) + FINFO_FULLSIZE(fs, fip)))
    711 
    712 #define LFS_DEF_FI_ACCESSOR(type, type32, field) \
    713 	static __unused inline type				\
    714 	lfs_fi_get##field(STRUCT_LFS *fs, FINFO *fip)		\
    715 	{							\
    716 		if (fs->lfs_is64) {				\
    717 			return fip->u_64.fi_##field; 		\
    718 		} else {					\
    719 			return fip->u_32.fi_##field; 		\
    720 		}						\
    721 	}							\
    722 	static __unused inline void				\
    723 	lfs_fi_set##field(STRUCT_LFS *fs, FINFO *fip, type val) \
    724 	{							\
    725 		if (fs->lfs_is64) {				\
    726 			type *p = &fip->u_64.fi_##field;	\
    727 			(void)p;				\
    728 			fip->u_64.fi_##field = val;		\
    729 		} else {					\
    730 			type32 *p = &fip->u_32.fi_##field;	\
    731 			(void)p;				\
    732 			fip->u_32.fi_##field = val;		\
    733 		}						\
    734 	}							\
    735 
    736 LFS_DEF_FI_ACCESSOR(uint32_t, uint32_t, nblocks);
    737 LFS_DEF_FI_ACCESSOR(uint32_t, uint32_t, version);
    738 LFS_DEF_FI_ACCESSOR(uint64_t, uint32_t, ino);
    739 LFS_DEF_FI_ACCESSOR(uint32_t, uint32_t, lastlength);
    740 
    741 static __unused inline daddr_t
    742 lfs_fi_getblock(STRUCT_LFS *fs, FINFO *fip, unsigned index)
    743 {
    744 	void *firstblock;
    745 
    746 	firstblock = (char *)fip + FINFOSIZE(fs);
    747 	KASSERT(index < lfs_fi_getnblocks(fs, fip));
    748 	if (fs->lfs_is64) {
    749 		return ((int64_t *)firstblock)[index];
    750 	} else {
    751 		return ((int32_t *)firstblock)[index];
    752 	}
    753 }
    754 
    755 static __unused inline void
    756 lfs_fi_setblock(STRUCT_LFS *fs, FINFO *fip, unsigned index, daddr_t blk)
    757 {
    758 	void *firstblock;
    759 
    760 	firstblock = (char *)fip + FINFOSIZE(fs);
    761 	KASSERT(index < lfs_fi_getnblocks(fs, fip));
    762 	if (fs->lfs_is64) {
    763 		((int64_t *)firstblock)[index] = blk;
    764 	} else {
    765 		((int32_t *)firstblock)[index] = blk;
    766 	}
    767 }
    768 
    769 /*
    770  * inode info entries (in the segment summary)
    771  */
    772 
    773 #define IINFOSIZE(fs)	((fs)->lfs_is64 ? sizeof(IINFO64) : sizeof(IINFO32))
    774 
    775 /* iinfos scroll backward from the end of the segment summary block */
    776 #define SEGSUM_IINFOSTART(fs, buf) \
    777 	((IINFO *)((char *)buf + lfs_sb_getsumsize(fs) - IINFOSIZE(fs)))
    778 
    779 #define NEXTLOWER_IINFO(fs, iip) \
    780 	((IINFO *)((char *)(iip) - IINFOSIZE(fs)))
    781 
    782 #define NTH_IINFO(fs, buf, n) \
    783 	((IINFO *)((char *)SEGSUM_IINFOSTART(fs, buf) - (n)*IINFOSIZE(fs)))
    784 
    785 static __unused inline uint64_t
    786 lfs_ii_getblock(STRUCT_LFS *fs, IINFO *iip)
    787 {
    788 	if (fs->lfs_is64) {
    789 		return iip->u_64.ii_block;
    790 	} else {
    791 		return iip->u_32.ii_block;
    792 	}
    793 }
    794 
    795 static __unused inline void
    796 lfs_ii_setblock(STRUCT_LFS *fs, IINFO *iip, uint64_t block)
    797 {
    798 	if (fs->lfs_is64) {
    799 		iip->u_64.ii_block = block;
    800 	} else {
    801 		iip->u_32.ii_block = block;
    802 	}
    803 }
    804 
    805 /*
    806  * Index file inode entries.
    807  */
    808 
    809 #define IFILE_ENTRYSIZE(fs) \
    810 	((fs)->lfs_is64 ? sizeof(IFILE64) : sizeof(IFILE32))
    811 
    812 /*
    813  * LFSv1 compatibility code is not allowed to touch if_atime, since it
    814  * may not be mapped!
    815  */
    816 /* Read in the block with a specific inode from the ifile. */
    817 #define	LFS_IENTRY(IP, F, IN, BP) do {					\
    818 	int _e;								\
    819 	SHARE_IFLOCK(F);						\
    820 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    821 	if ((_e = bread((F)->lfs_ivnode,				\
    822 	(IN) / lfs_sb_getifpb(F) + lfs_sb_getcleansz(F) + lfs_sb_getsegtabsz(F), \
    823 	lfs_sb_getbsize(F), 0, &(BP))) != 0)				\
    824 		panic("lfs: ifile ino %d read %d", (int)(IN), _e);	\
    825 	if ((F)->lfs_is64) {						\
    826 		(IP) = (IFILE *)((IFILE64 *)(BP)->b_data +		\
    827 				 (IN) % lfs_sb_getifpb(F));		\
    828 	} else if (lfs_sb_getversion(F) > 1) {				\
    829 		(IP) = (IFILE *)((IFILE32 *)(BP)->b_data +		\
    830 				(IN) % lfs_sb_getifpb(F)); 		\
    831 	} else {							\
    832 		(IP) = (IFILE *)((IFILE_V1 *)(BP)->b_data +		\
    833 				 (IN) % lfs_sb_getifpb(F));		\
    834 	}								\
    835 	UNSHARE_IFLOCK(F);						\
    836 } while (0)
    837 #define LFS_IENTRY_NEXT(IP, F) do { \
    838 	if ((F)->lfs_is64) {						\
    839 		(IP) = (IFILE *)((IFILE64 *)(IP) + 1);			\
    840 	} else if (lfs_sb_getversion(F) > 1) {				\
    841 		(IP) = (IFILE *)((IFILE32 *)(IP) + 1);			\
    842 	} else {							\
    843 		(IP) = (IFILE *)((IFILE_V1 *)(IP) + 1);			\
    844 	}								\
    845 } while (0)
    846 
    847 #define LFS_DEF_IF_ACCESSOR(type, type32, field) \
    848 	static __unused inline type				\
    849 	lfs_if_get##field(STRUCT_LFS *fs, IFILE *ifp)		\
    850 	{							\
    851 		if (fs->lfs_is64) {				\
    852 			return ifp->u_64.if_##field; 		\
    853 		} else {					\
    854 			return ifp->u_32.if_##field; 		\
    855 		}						\
    856 	}							\
    857 	static __unused inline void				\
    858 	lfs_if_set##field(STRUCT_LFS *fs, IFILE *ifp, type val) \
    859 	{							\
    860 		if (fs->lfs_is64) {				\
    861 			type *p = &ifp->u_64.if_##field;	\
    862 			(void)p;				\
    863 			ifp->u_64.if_##field = val;		\
    864 		} else {					\
    865 			type32 *p = &ifp->u_32.if_##field;	\
    866 			(void)p;				\
    867 			ifp->u_32.if_##field = val;		\
    868 		}						\
    869 	}							\
    870 
    871 LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, version);
    872 LFS_DEF_IF_ACCESSOR(int64_t, int32_t, daddr);
    873 LFS_DEF_IF_ACCESSOR(u_int64_t, u_int32_t, nextfree);
    874 LFS_DEF_IF_ACCESSOR(u_int64_t, u_int32_t, atime_sec);
    875 LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, atime_nsec);
    876 
    877 /*
    878  * Cleaner information structure.  This resides in the ifile and is used
    879  * to pass information from the kernel to the cleaner.
    880  */
    881 
    882 #define	CLEANSIZE_SU(fs)						\
    883 	((((fs)->lfs_is64 ? sizeof(CLEANERINFO64) : sizeof(CLEANERINFO32)) + \
    884 		lfs_sb_getbsize(fs) - 1) >> lfs_sb_getbshift(fs))
    885 
    886 #define LFS_DEF_CI_ACCESSOR(type, type32, field) \
    887 	static __unused inline type				\
    888 	lfs_ci_get##field(STRUCT_LFS *fs, CLEANERINFO *cip)	\
    889 	{							\
    890 		if (fs->lfs_is64) {				\
    891 			return cip->u_64.field; 		\
    892 		} else {					\
    893 			return cip->u_32.field; 		\
    894 		}						\
    895 	}							\
    896 	static __unused inline void				\
    897 	lfs_ci_set##field(STRUCT_LFS *fs, CLEANERINFO *cip, type val) \
    898 	{							\
    899 		if (fs->lfs_is64) {				\
    900 			type *p = &cip->u_64.field;		\
    901 			(void)p;				\
    902 			cip->u_64.field = val;			\
    903 		} else {					\
    904 			type32 *p = &cip->u_32.field;		\
    905 			(void)p;				\
    906 			cip->u_32.field = val;			\
    907 		}						\
    908 	}							\
    909 
    910 LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, clean);
    911 LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, dirty);
    912 LFS_DEF_CI_ACCESSOR(int64_t, int32_t, bfree);
    913 LFS_DEF_CI_ACCESSOR(int64_t, int32_t, avail);
    914 LFS_DEF_CI_ACCESSOR(u_int64_t, u_int32_t, free_head);
    915 LFS_DEF_CI_ACCESSOR(u_int64_t, u_int32_t, free_tail);
    916 LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, flags);
    917 
    918 static __unused inline void
    919 lfs_ci_shiftcleantodirty(STRUCT_LFS *fs, CLEANERINFO *cip, unsigned num)
    920 {
    921 	lfs_ci_setclean(fs, cip, lfs_ci_getclean(fs, cip) - num);
    922 	lfs_ci_setdirty(fs, cip, lfs_ci_getdirty(fs, cip) + num);
    923 }
    924 
    925 static __unused inline void
    926 lfs_ci_shiftdirtytoclean(STRUCT_LFS *fs, CLEANERINFO *cip, unsigned num)
    927 {
    928 	lfs_ci_setdirty(fs, cip, lfs_ci_getdirty(fs, cip) - num);
    929 	lfs_ci_setclean(fs, cip, lfs_ci_getclean(fs, cip) + num);
    930 }
    931 
    932 /* Read in the block with the cleaner info from the ifile. */
    933 #define LFS_CLEANERINFO(CP, F, BP) do {					\
    934 	SHARE_IFLOCK(F);						\
    935 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    936 	if (bread((F)->lfs_ivnode,					\
    937 	    (daddr_t)0, lfs_sb_getbsize(F), 0, &(BP)))			\
    938 		panic("lfs: ifile read");				\
    939 	(CP) = (CLEANERINFO *)(BP)->b_data;				\
    940 	UNSHARE_IFLOCK(F);						\
    941 } while (0)
    942 
    943 /*
    944  * Synchronize the Ifile cleaner info with current avail and bfree.
    945  */
    946 #define LFS_SYNC_CLEANERINFO(cip, fs, bp, w) do {		 	\
    947     mutex_enter(&lfs_lock);						\
    948     if ((w) || lfs_ci_getbfree(fs, cip) != lfs_sb_getbfree(fs) ||	\
    949 	lfs_ci_getavail(fs, cip) != lfs_sb_getavail(fs) - fs->lfs_ravail - \
    950 	fs->lfs_favail) {	 					\
    951 	lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs));		 	\
    952 	lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs) - fs->lfs_ravail -	\
    953 		fs->lfs_favail);				 	\
    954 	if (((bp)->b_flags & B_GATHERED) == 0) {		 	\
    955 		fs->lfs_flags |= LFS_IFDIRTY;				\
    956 	}								\
    957 	mutex_exit(&lfs_lock);						\
    958 	(void) LFS_BWRITE_LOG(bp); /* Ifile */			 	\
    959     } else {							 	\
    960 	mutex_exit(&lfs_lock);						\
    961 	brelse(bp, 0);						 	\
    962     }									\
    963 } while (0)
    964 
    965 /*
    966  * Get the head of the inode free list.
    967  * Always called with the segment lock held.
    968  */
    969 #define LFS_GET_HEADFREE(FS, CIP, BP, FREEP) do {			\
    970 	if (lfs_sb_getversion(FS) > 1) {				\
    971 		LFS_CLEANERINFO((CIP), (FS), (BP));			\
    972 		lfs_sb_setfreehd(FS, lfs_ci_getfree_head(FS, CIP));	\
    973 		brelse(BP, 0);						\
    974 	}								\
    975 	*(FREEP) = lfs_sb_getfreehd(FS);				\
    976 } while (0)
    977 
    978 #define LFS_PUT_HEADFREE(FS, CIP, BP, VAL) do {				\
    979 	lfs_sb_setfreehd(FS, VAL);					\
    980 	if (lfs_sb_getversion(FS) > 1) {				\
    981 		LFS_CLEANERINFO((CIP), (FS), (BP));			\
    982 		lfs_ci_setfree_head(FS, CIP, VAL);			\
    983 		LFS_BWRITE_LOG(BP);					\
    984 		mutex_enter(&lfs_lock);					\
    985 		(FS)->lfs_flags |= LFS_IFDIRTY;				\
    986 		mutex_exit(&lfs_lock);					\
    987 	}								\
    988 } while (0)
    989 
    990 #define LFS_GET_TAILFREE(FS, CIP, BP, FREEP) do {			\
    991 	LFS_CLEANERINFO((CIP), (FS), (BP));				\
    992 	*(FREEP) = lfs_ci_getfree_tail(FS, CIP);			\
    993 	brelse(BP, 0);							\
    994 } while (0)
    995 
    996 #define LFS_PUT_TAILFREE(FS, CIP, BP, VAL) do {				\
    997 	LFS_CLEANERINFO((CIP), (FS), (BP));				\
    998 	lfs_ci_setfree_tail(FS, CIP, VAL);				\
    999 	LFS_BWRITE_LOG(BP);						\
   1000 	mutex_enter(&lfs_lock);						\
   1001 	(FS)->lfs_flags |= LFS_IFDIRTY;					\
   1002 	mutex_exit(&lfs_lock);						\
   1003 } while (0)
   1004 
   1005 /*
   1006  * On-disk segment summary information
   1007  */
   1008 
   1009 #define SEGSUM_SIZE(fs) \
   1010 	(fs->lfs_is64 ? sizeof(SEGSUM64) : \
   1011 	 lfs_sb_getversion(fs) > 1 ? sizeof(SEGSUM32) : sizeof(SEGSUM_V1))
   1012 
   1013 /*
   1014  * The SEGSUM structure is followed by FINFO structures. Get the pointer
   1015  * to the first FINFO.
   1016  *
   1017  * XXX this can't be a macro yet; this file needs to be resorted.
   1018  */
   1019 #if 0
   1020 static __unused inline FINFO *
   1021 segsum_finfobase(STRUCT_LFS *fs, SEGSUM *ssp)
   1022 {
   1023 	return (FINFO *)((char *)ssp + SEGSUM_SIZE(fs));
   1024 }
   1025 #else
   1026 #define SEGSUM_FINFOBASE(fs, ssp) \
   1027 	((FINFO *)((char *)(ssp) + SEGSUM_SIZE(fs)));
   1028 #endif
   1029 
   1030 #define LFS_DEF_SS_ACCESSOR(type, type32, field) \
   1031 	static __unused inline type				\
   1032 	lfs_ss_get##field(STRUCT_LFS *fs, SEGSUM *ssp)		\
   1033 	{							\
   1034 		if (fs->lfs_is64) {				\
   1035 			return ssp->u_64.ss_##field; 		\
   1036 		} else {					\
   1037 			return ssp->u_32.ss_##field; 		\
   1038 		}						\
   1039 	}							\
   1040 	static __unused inline void				\
   1041 	lfs_ss_set##field(STRUCT_LFS *fs, SEGSUM *ssp, type val) \
   1042 	{							\
   1043 		if (fs->lfs_is64) {				\
   1044 			type *p = &ssp->u_64.ss_##field;	\
   1045 			(void)p;				\
   1046 			ssp->u_64.ss_##field = val;		\
   1047 		} else {					\
   1048 			type32 *p = &ssp->u_32.ss_##field;	\
   1049 			(void)p;				\
   1050 			ssp->u_32.ss_##field = val;		\
   1051 		}						\
   1052 	}							\
   1053 
   1054 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, sumsum);
   1055 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, datasum);
   1056 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, magic);
   1057 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, ident);
   1058 LFS_DEF_SS_ACCESSOR(int64_t, int32_t, next);
   1059 LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, nfinfo);
   1060 LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, ninos);
   1061 LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, flags);
   1062 LFS_DEF_SS_ACCESSOR(uint64_t, uint32_t, reclino);
   1063 LFS_DEF_SS_ACCESSOR(uint64_t, uint64_t, serial);
   1064 LFS_DEF_SS_ACCESSOR(uint64_t, uint64_t, create);
   1065 
   1066 static __unused inline size_t
   1067 lfs_ss_getsumstart(STRUCT_LFS *fs)
   1068 {
   1069 	/* These are actually all the same. */
   1070 	if (fs->lfs_is64) {
   1071 		return offsetof(SEGSUM64, ss_datasum);
   1072 	} else /* if (lfs_sb_getversion(fs) > 1) */ {
   1073 		return offsetof(SEGSUM32, ss_datasum);
   1074 	} /* else {
   1075 		return offsetof(SEGSUM_V1, ss_datasum);
   1076 	} */
   1077 	/*
   1078 	 * XXX ^^^ until this file is resorted lfs_sb_getversion isn't
   1079 	 * defined yet.
   1080 	 */
   1081 }
   1082 
   1083 static __unused inline uint32_t
   1084 lfs_ss_getocreate(STRUCT_LFS *fs, SEGSUM *ssp)
   1085 {
   1086 	KASSERT(fs->lfs_is64 == 0);
   1087 	/* XXX need to resort this file before we can do this */
   1088 	//KASSERT(lfs_sb_getversion(fs) == 1);
   1089 
   1090 	return ssp->u_v1.ss_create;
   1091 }
   1092 
   1093 static __unused inline void
   1094 lfs_ss_setocreate(STRUCT_LFS *fs, SEGSUM *ssp, uint32_t val)
   1095 {
   1096 	KASSERT(fs->lfs_is64 == 0);
   1097 	/* XXX need to resort this file before we can do this */
   1098 	//KASSERT(lfs_sb_getversion(fs) == 1);
   1099 
   1100 	ssp->u_v1.ss_create = val;
   1101 }
   1102 
   1103 
   1104 /*
   1105  * Super block.
   1106  */
   1107 
   1108 /*
   1109  * Generate accessors for the on-disk superblock fields with cpp.
   1110  */
   1111 
   1112 #define LFS_DEF_SB_ACCESSOR_FULL(type, type32, field) \
   1113 	static __unused inline type				\
   1114 	lfs_sb_get##field(STRUCT_LFS *fs)			\
   1115 	{							\
   1116 		if (fs->lfs_is64) {				\
   1117 			return fs->lfs_dlfs_u.u_64.dlfs_##field; \
   1118 		} else {					\
   1119 			return fs->lfs_dlfs_u.u_32.dlfs_##field; \
   1120 		}						\
   1121 	}							\
   1122 	static __unused inline void				\
   1123 	lfs_sb_set##field(STRUCT_LFS *fs, type val)		\
   1124 	{							\
   1125 		if (fs->lfs_is64) {				\
   1126 			fs->lfs_dlfs_u.u_64.dlfs_##field = val;	\
   1127 		} else {					\
   1128 			fs->lfs_dlfs_u.u_32.dlfs_##field = val;	\
   1129 		}						\
   1130 	}							\
   1131 	static __unused inline void				\
   1132 	lfs_sb_add##field(STRUCT_LFS *fs, type val)		\
   1133 	{							\
   1134 		if (fs->lfs_is64) {				\
   1135 			type *p64 = &fs->lfs_dlfs_u.u_64.dlfs_##field; \
   1136 			*p64 += val;				\
   1137 		} else {					\
   1138 			type32 *p32 = &fs->lfs_dlfs_u.u_32.dlfs_##field; \
   1139 			*p32 += val;				\
   1140 		}						\
   1141 	}							\
   1142 	static __unused inline void				\
   1143 	lfs_sb_sub##field(STRUCT_LFS *fs, type val)		\
   1144 	{							\
   1145 		if (fs->lfs_is64) {				\
   1146 			type *p64 = &fs->lfs_dlfs_u.u_64.dlfs_##field; \
   1147 			*p64 -= val;				\
   1148 		} else {					\
   1149 			type32 *p32 = &fs->lfs_dlfs_u.u_32.dlfs_##field; \
   1150 			*p32 -= val;				\
   1151 		}						\
   1152 	}
   1153 
   1154 #define LFS_DEF_SB_ACCESSOR(t, f) LFS_DEF_SB_ACCESSOR_FULL(t, t, f)
   1155 
   1156 #define LFS_DEF_SB_ACCESSOR_32ONLY(type, field, val64) \
   1157 	static __unused inline type				\
   1158 	lfs_sb_get##field(STRUCT_LFS *fs)			\
   1159 	{							\
   1160 		if (fs->lfs_is64) {				\
   1161 			return val64;				\
   1162 		} else {					\
   1163 			return fs->lfs_dlfs_u.u_32.dlfs_##field; \
   1164 		}						\
   1165 	}
   1166 
   1167 #define lfs_magic lfs_dlfs.dlfs_magic
   1168 LFS_DEF_SB_ACCESSOR(u_int32_t, version);
   1169 LFS_DEF_SB_ACCESSOR_FULL(u_int64_t, u_int32_t, size);
   1170 LFS_DEF_SB_ACCESSOR(u_int32_t, ssize);
   1171 LFS_DEF_SB_ACCESSOR_FULL(u_int64_t, u_int32_t, dsize);
   1172 LFS_DEF_SB_ACCESSOR(u_int32_t, bsize);
   1173 LFS_DEF_SB_ACCESSOR(u_int32_t, fsize);
   1174 LFS_DEF_SB_ACCESSOR(u_int32_t, frag);
   1175 LFS_DEF_SB_ACCESSOR_FULL(uint64_t, uint32_t, freehd);
   1176 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, bfree);
   1177 LFS_DEF_SB_ACCESSOR_FULL(uint64_t, uint32_t, nfiles);
   1178 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, avail);
   1179 LFS_DEF_SB_ACCESSOR(int32_t, uinodes);
   1180 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, idaddr);
   1181 LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, ifile, LFS_IFILE_INUM);
   1182 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, lastseg);
   1183 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, nextseg);
   1184 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, curseg);
   1185 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, offset);
   1186 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, lastpseg);
   1187 LFS_DEF_SB_ACCESSOR(u_int32_t, inopf);
   1188 LFS_DEF_SB_ACCESSOR(u_int32_t, minfree);
   1189 LFS_DEF_SB_ACCESSOR(uint64_t, maxfilesize);
   1190 LFS_DEF_SB_ACCESSOR(u_int32_t, fsbpseg);
   1191 LFS_DEF_SB_ACCESSOR(u_int32_t, inopb);
   1192 LFS_DEF_SB_ACCESSOR(u_int32_t, ifpb);
   1193 LFS_DEF_SB_ACCESSOR(u_int32_t, sepb);
   1194 LFS_DEF_SB_ACCESSOR(u_int32_t, nindir);
   1195 LFS_DEF_SB_ACCESSOR(u_int32_t, nseg);
   1196 LFS_DEF_SB_ACCESSOR(u_int32_t, nspf);
   1197 LFS_DEF_SB_ACCESSOR(u_int32_t, cleansz);
   1198 LFS_DEF_SB_ACCESSOR(u_int32_t, segtabsz);
   1199 LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, segmask, 0);
   1200 LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, segshift, 0);
   1201 LFS_DEF_SB_ACCESSOR(u_int64_t, bmask);
   1202 LFS_DEF_SB_ACCESSOR(u_int32_t, bshift);
   1203 LFS_DEF_SB_ACCESSOR(u_int64_t, ffmask);
   1204 LFS_DEF_SB_ACCESSOR(u_int32_t, ffshift);
   1205 LFS_DEF_SB_ACCESSOR(u_int64_t, fbmask);
   1206 LFS_DEF_SB_ACCESSOR(u_int32_t, fbshift);
   1207 LFS_DEF_SB_ACCESSOR(u_int32_t, blktodb);
   1208 LFS_DEF_SB_ACCESSOR(u_int32_t, fsbtodb);
   1209 LFS_DEF_SB_ACCESSOR(u_int32_t, sushift);
   1210 LFS_DEF_SB_ACCESSOR(int32_t, maxsymlinklen);
   1211 LFS_DEF_SB_ACCESSOR(u_int32_t, cksum);
   1212 LFS_DEF_SB_ACCESSOR(u_int16_t, pflags);
   1213 LFS_DEF_SB_ACCESSOR(u_int32_t, nclean);
   1214 LFS_DEF_SB_ACCESSOR(int32_t, dmeta);
   1215 LFS_DEF_SB_ACCESSOR(u_int32_t, minfreeseg);
   1216 LFS_DEF_SB_ACCESSOR(u_int32_t, sumsize);
   1217 LFS_DEF_SB_ACCESSOR(u_int64_t, serial);
   1218 LFS_DEF_SB_ACCESSOR(u_int32_t, ibsize);
   1219 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, s0addr);
   1220 LFS_DEF_SB_ACCESSOR(u_int64_t, tstamp);
   1221 LFS_DEF_SB_ACCESSOR(u_int32_t, inodefmt);
   1222 LFS_DEF_SB_ACCESSOR(u_int32_t, interleave);
   1223 LFS_DEF_SB_ACCESSOR(u_int32_t, ident);
   1224 LFS_DEF_SB_ACCESSOR(u_int32_t, resvseg);
   1225 
   1226 /* special-case accessors */
   1227 
   1228 /*
   1229  * the v1 otstamp field lives in what's now dlfs_inopf
   1230  */
   1231 #define lfs_sb_getotstamp(fs) lfs_sb_getinopf(fs)
   1232 #define lfs_sb_setotstamp(fs, val) lfs_sb_setinopf(fs, val)
   1233 
   1234 /*
   1235  * lfs_sboffs is an array
   1236  */
   1237 static __unused inline int32_t
   1238 lfs_sb_getsboff(STRUCT_LFS *fs, unsigned n)
   1239 {
   1240 #ifdef KASSERT /* ugh */
   1241 	KASSERT(n < LFS_MAXNUMSB);
   1242 #endif
   1243 	if (fs->lfs_is64) {
   1244 		return fs->lfs_dlfs_u.u_64.dlfs_sboffs[n];
   1245 	} else {
   1246 		return fs->lfs_dlfs_u.u_32.dlfs_sboffs[n];
   1247 	}
   1248 }
   1249 static __unused inline void
   1250 lfs_sb_setsboff(STRUCT_LFS *fs, unsigned n, int32_t val)
   1251 {
   1252 #ifdef KASSERT /* ugh */
   1253 	KASSERT(n < LFS_MAXNUMSB);
   1254 #endif
   1255 	if (fs->lfs_is64) {
   1256 		fs->lfs_dlfs_u.u_64.dlfs_sboffs[n] = val;
   1257 	} else {
   1258 		fs->lfs_dlfs_u.u_32.dlfs_sboffs[n] = val;
   1259 	}
   1260 }
   1261 
   1262 /*
   1263  * lfs_fsmnt is a string
   1264  */
   1265 static __unused inline const char *
   1266 lfs_sb_getfsmnt(STRUCT_LFS *fs)
   1267 {
   1268 	if (fs->lfs_is64) {
   1269 		return fs->lfs_dlfs_u.u_64.dlfs_fsmnt;
   1270 	} else {
   1271 		return fs->lfs_dlfs_u.u_32.dlfs_fsmnt;
   1272 	}
   1273 }
   1274 
   1275 static __unused inline void
   1276 lfs_sb_setfsmnt(STRUCT_LFS *fs, const char *str)
   1277 {
   1278 	if (fs->lfs_is64) {
   1279 		(void)strncpy(fs->lfs_dlfs_u.u_64.dlfs_fsmnt, str,
   1280 			sizeof(fs->lfs_dlfs_u.u_64.dlfs_fsmnt));
   1281 	} else {
   1282 		(void)strncpy(fs->lfs_dlfs_u.u_32.dlfs_fsmnt, str,
   1283 			sizeof(fs->lfs_dlfs_u.u_32.dlfs_fsmnt));
   1284 	}
   1285 }
   1286 
   1287 /* Highest addressable fsb */
   1288 #define LFS_MAX_DADDR(fs) \
   1289 	((fs)->lfs_is64 ? 0x7fffffffffffffff : 0x7fffffff)
   1290 
   1291 /* LFS_NINDIR is the number of indirects in a file system block. */
   1292 #define	LFS_NINDIR(fs)	(lfs_sb_getnindir(fs))
   1293 
   1294 /* LFS_INOPB is the number of inodes in a secondary storage block. */
   1295 #define	LFS_INOPB(fs)	(lfs_sb_getinopb(fs))
   1296 /* LFS_INOPF is the number of inodes in a fragment. */
   1297 #define LFS_INOPF(fs)	(lfs_sb_getinopf(fs))
   1298 
   1299 #define	lfs_blkoff(fs, loc)	((int)((loc) & lfs_sb_getbmask(fs)))
   1300 #define lfs_fragoff(fs, loc)    /* calculates (loc % fs->lfs_fsize) */ \
   1301     ((int)((loc) & lfs_sb_getffmask(fs)))
   1302 
   1303 /* XXX: lowercase these as they're no longer macros */
   1304 /* Frags to diskblocks */
   1305 static __unused inline uint64_t
   1306 LFS_FSBTODB(STRUCT_LFS *fs, uint64_t b)
   1307 {
   1308 #if defined(_KERNEL)
   1309 	return b << (lfs_sb_getffshift(fs) - DEV_BSHIFT);
   1310 #else
   1311 	return b << lfs_sb_getfsbtodb(fs);
   1312 #endif
   1313 }
   1314 /* Diskblocks to frags */
   1315 static __unused inline uint64_t
   1316 LFS_DBTOFSB(STRUCT_LFS *fs, uint64_t b)
   1317 {
   1318 #if defined(_KERNEL)
   1319 	return b >> (lfs_sb_getffshift(fs) - DEV_BSHIFT);
   1320 #else
   1321 	return b >> lfs_sb_getfsbtodb(fs);
   1322 #endif
   1323 }
   1324 
   1325 #define	lfs_lblkno(fs, loc)	((loc) >> lfs_sb_getbshift(fs))
   1326 #define	lfs_lblktosize(fs, blk)	((blk) << lfs_sb_getbshift(fs))
   1327 
   1328 /* Frags to bytes */
   1329 static __unused inline uint64_t
   1330 lfs_fsbtob(STRUCT_LFS *fs, uint64_t b)
   1331 {
   1332 	return b << lfs_sb_getffshift(fs);
   1333 }
   1334 /* Bytes to frags */
   1335 static __unused inline uint64_t
   1336 lfs_btofsb(STRUCT_LFS *fs, uint64_t b)
   1337 {
   1338 	return b >> lfs_sb_getffshift(fs);
   1339 }
   1340 
   1341 #define lfs_numfrags(fs, loc)	/* calculates (loc / fs->lfs_fsize) */	\
   1342 	((loc) >> lfs_sb_getffshift(fs))
   1343 #define lfs_blkroundup(fs, size)/* calculates roundup(size, lfs_sb_getbsize(fs)) */ \
   1344 	((off_t)(((size) + lfs_sb_getbmask(fs)) & (~lfs_sb_getbmask(fs))))
   1345 #define lfs_fragroundup(fs, size)/* calculates roundup(size, fs->lfs_fsize) */ \
   1346 	((off_t)(((size) + lfs_sb_getffmask(fs)) & (~lfs_sb_getffmask(fs))))
   1347 #define lfs_fragstoblks(fs, frags)/* calculates (frags / fs->fs_frag) */ \
   1348 	((frags) >> lfs_sb_getfbshift(fs))
   1349 #define lfs_blkstofrags(fs, blks)/* calculates (blks * fs->fs_frag) */ \
   1350 	((blks) << lfs_sb_getfbshift(fs))
   1351 #define lfs_fragnum(fs, fsb)	/* calculates (fsb % fs->lfs_frag) */	\
   1352 	((fsb) & ((fs)->lfs_frag - 1))
   1353 #define lfs_blknum(fs, fsb)	/* calculates rounddown(fsb, fs->lfs_frag) */ \
   1354 	((fsb) &~ ((fs)->lfs_frag - 1))
   1355 #define lfs_dblksize(fs, dp, lbn) \
   1356 	(((lbn) >= ULFS_NDADDR || lfs_dino_getsize(fs, dp) >= ((lbn) + 1) << lfs_sb_getbshift(fs)) \
   1357 	    ? lfs_sb_getbsize(fs) \
   1358 	    : (lfs_fragroundup(fs, lfs_blkoff(fs, lfs_dino_getsize(fs, dp)))))
   1359 
   1360 #define	lfs_segsize(fs)	(lfs_sb_getversion(fs) == 1 ?	     		\
   1361 			   lfs_lblktosize((fs), lfs_sb_getssize(fs)) :	\
   1362 			   lfs_sb_getssize(fs))
   1363 /* XXX segtod produces a result in frags despite the 'd' */
   1364 #define lfs_segtod(fs, seg) (lfs_btofsb(fs, lfs_segsize(fs)) * (seg))
   1365 #define	lfs_dtosn(fs, daddr)	/* block address to segment number */	\
   1366 	((uint32_t)(((daddr) - lfs_sb_gets0addr(fs)) / lfs_segtod((fs), 1)))
   1367 #define lfs_sntod(fs, sn)	/* segment number to disk address */	\
   1368 	((daddr_t)(lfs_segtod((fs), (sn)) + lfs_sb_gets0addr(fs)))
   1369 
   1370 /* XXX, blah. make this appear only if struct inode is defined */
   1371 #ifdef _UFS_LFS_LFS_INODE_H_
   1372 static __unused inline uint32_t
   1373 lfs_blksize(STRUCT_LFS *fs, struct inode *ip, uint64_t lbn)
   1374 {
   1375 	if (lbn >= ULFS_NDADDR || lfs_dino_getsize(fs, ip->i_din) >= (lbn + 1) << lfs_sb_getbshift(fs)) {
   1376 		return lfs_sb_getbsize(fs);
   1377 	} else {
   1378 		return lfs_fragroundup(fs, lfs_blkoff(fs, lfs_dino_getsize(fs, ip->i_din)));
   1379 	}
   1380 }
   1381 #endif
   1382 
   1383 /*
   1384  * union lfs_blocks
   1385  */
   1386 
   1387 static __unused inline void
   1388 lfs_blocks_fromvoid(STRUCT_LFS *fs, union lfs_blocks *bp, void *p)
   1389 {
   1390 	if (fs->lfs_is64) {
   1391 		bp->b64 = p;
   1392 	} else {
   1393 		bp->b32 = p;
   1394 	}
   1395 }
   1396 
   1397 static __unused inline void
   1398 lfs_blocks_fromfinfo(STRUCT_LFS *fs, union lfs_blocks *bp, FINFO *fip)
   1399 {
   1400 	void *firstblock;
   1401 
   1402 	firstblock = (char *)fip + FINFOSIZE(fs);
   1403 	if (fs->lfs_is64) {
   1404 		bp->b64 = (int64_t *)firstblock;
   1405 	}  else {
   1406 		bp->b32 = (int32_t *)firstblock;
   1407 	}
   1408 }
   1409 
   1410 static __unused inline daddr_t
   1411 lfs_blocks_get(STRUCT_LFS *fs, union lfs_blocks *bp, unsigned index)
   1412 {
   1413 	if (fs->lfs_is64) {
   1414 		return bp->b64[index];
   1415 	} else {
   1416 		return bp->b32[index];
   1417 	}
   1418 }
   1419 
   1420 static __unused inline void
   1421 lfs_blocks_set(STRUCT_LFS *fs, union lfs_blocks *bp, unsigned index, daddr_t val)
   1422 {
   1423 	if (fs->lfs_is64) {
   1424 		bp->b64[index] = val;
   1425 	} else {
   1426 		bp->b32[index] = val;
   1427 	}
   1428 }
   1429 
   1430 static __unused inline void
   1431 lfs_blocks_inc(STRUCT_LFS *fs, union lfs_blocks *bp)
   1432 {
   1433 	if (fs->lfs_is64) {
   1434 		bp->b64++;
   1435 	} else {
   1436 		bp->b32++;
   1437 	}
   1438 }
   1439 
   1440 static __unused inline int
   1441 lfs_blocks_eq(STRUCT_LFS *fs, union lfs_blocks *bp1, union lfs_blocks *bp2)
   1442 {
   1443 	if (fs->lfs_is64) {
   1444 		return bp1->b64 == bp2->b64;
   1445 	} else {
   1446 		return bp1->b32 == bp2->b32;
   1447 	}
   1448 }
   1449 
   1450 static __unused inline int
   1451 lfs_blocks_sub(STRUCT_LFS *fs, union lfs_blocks *bp1, union lfs_blocks *bp2)
   1452 {
   1453 	/* (remember that the pointers are typed) */
   1454 	if (fs->lfs_is64) {
   1455 		return bp1->b64 - bp2->b64;
   1456 	} else {
   1457 		return bp1->b32 - bp2->b32;
   1458 	}
   1459 }
   1460 
   1461 /*
   1462  * struct segment
   1463  */
   1464 
   1465 
   1466 /*
   1467  * Macros for determining free space on the disk, with the variable metadata
   1468  * of segment summaries and inode blocks taken into account.
   1469  */
   1470 /*
   1471  * Estimate number of clean blocks not available for writing because
   1472  * they will contain metadata or overhead.  This is calculated as
   1473  *
   1474  *		E = ((C * M / D) * D + (0) * (T - D)) / T
   1475  * or more simply
   1476  *		E = (C * M) / T
   1477  *
   1478  * where
   1479  * C is the clean space,
   1480  * D is the dirty space,
   1481  * M is the dirty metadata, and
   1482  * T = C + D is the total space on disk.
   1483  *
   1484  * This approximates the old formula of E = C * M / D when D is close to T,
   1485  * but avoids falsely reporting "disk full" when the sample size (D) is small.
   1486  */
   1487 #define LFS_EST_CMETA(F) ((						\
   1488 	(lfs_sb_getdmeta(F) * (int64_t)lfs_sb_getnclean(F)) / 		\
   1489 	(lfs_sb_getnseg(F))))
   1490 
   1491 /* Estimate total size of the disk not including metadata */
   1492 #define LFS_EST_NONMETA(F) (lfs_sb_getdsize(F) - lfs_sb_getdmeta(F) - LFS_EST_CMETA(F))
   1493 
   1494 /* Estimate number of blocks actually available for writing */
   1495 #define LFS_EST_BFREE(F) (lfs_sb_getbfree(F) > LFS_EST_CMETA(F) ?	     \
   1496 			  lfs_sb_getbfree(F) - LFS_EST_CMETA(F) : 0)
   1497 
   1498 /* Amount of non-meta space not available to mortal man */
   1499 #define LFS_EST_RSVD(F) ((LFS_EST_NONMETA(F) *			     \
   1500 				   (u_int64_t)lfs_sb_getminfree(F)) /	     \
   1501 				  100)
   1502 
   1503 /* Can credential C write BB blocks? XXX: kauth_cred_geteuid is abusive */
   1504 #define ISSPACE(F, BB, C)						\
   1505 	((((C) == NOCRED || kauth_cred_geteuid(C) == 0) &&		\
   1506 	  LFS_EST_BFREE(F) >= (BB)) ||					\
   1507 	 (kauth_cred_geteuid(C) != 0 && IS_FREESPACE(F, BB)))
   1508 
   1509 /* Can an ordinary user write BB blocks */
   1510 #define IS_FREESPACE(F, BB)						\
   1511 	  (LFS_EST_BFREE(F) >= (BB) + LFS_EST_RSVD(F))
   1512 
   1513 /*
   1514  * The minimum number of blocks to create a new inode.  This is:
   1515  * directory direct block (1) + ULFS_NIADDR indirect blocks + inode block (1) +
   1516  * ifile direct block (1) + ULFS_NIADDR indirect blocks = 3 + 2 * ULFS_NIADDR blocks.
   1517  */
   1518 #define LFS_NRESERVE(F) (lfs_btofsb((F), (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(F)))
   1519 
   1520 
   1521 
   1522 #endif /* _UFS_LFS_LFS_ACCESSORS_H_ */
   1523