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