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