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