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