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