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