Home | History | Annotate | Line # | Download | only in lfs
lfs_accessors.h revision 1.15
      1 /*	$NetBSD: lfs_accessors.h,v 1.15 2015/08/29 21:04:22 mlelstv 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 static __unused inline void
    183 lfs_copy_dinode(STRUCT_LFS *fs,
    184     union lfs_dinode *dst, const union lfs_dinode *src)
    185 {
    186 	/*
    187 	 * We can do structure assignment of the structs, but not of
    188 	 * the whole union, as the union is the size of the (larger)
    189 	 * 64-bit struct and on a 32-bit fs the upper half of it might
    190 	 * be off the end of a buffer or otherwise invalid.
    191 	 */
    192 	if (fs->lfs_is64) {
    193 		dst->u_64 = src->u_64;
    194 	} else {
    195 		dst->u_32 = src->u_32;
    196 	}
    197 }
    198 
    199 #define LFS_DEF_DINO_ACCESSOR(type, type32, field) \
    200 	static __unused inline type				\
    201 	lfs_dino_get##field(STRUCT_LFS *fs, union lfs_dinode *dip) \
    202 	{							\
    203 		if (fs->lfs_is64) {				\
    204 			return dip->u_64.di_##field; 		\
    205 		} else {					\
    206 			return dip->u_32.di_##field; 		\
    207 		}						\
    208 	}							\
    209 	static __unused inline void				\
    210 	lfs_dino_set##field(STRUCT_LFS *fs, union lfs_dinode *dip, type val) \
    211 	{							\
    212 		if (fs->lfs_is64) {				\
    213 			type *p = &dip->u_64.di_##field;	\
    214 			(void)p;				\
    215 			dip->u_64.di_##field = val;		\
    216 		} else {					\
    217 			type32 *p = &dip->u_32.di_##field;	\
    218 			(void)p;				\
    219 			dip->u_32.di_##field = val;		\
    220 		}						\
    221 	}							\
    222 
    223 LFS_DEF_DINO_ACCESSOR(uint16_t, uint16_t, mode);
    224 LFS_DEF_DINO_ACCESSOR(int16_t, int16_t, nlink);
    225 LFS_DEF_DINO_ACCESSOR(uint64_t, uint32_t, inumber);
    226 LFS_DEF_DINO_ACCESSOR(uint64_t, uint64_t, size);
    227 LFS_DEF_DINO_ACCESSOR(int64_t, int32_t, atime);
    228 LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, atimensec);
    229 LFS_DEF_DINO_ACCESSOR(int64_t, int32_t, mtime);
    230 LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, mtimensec);
    231 LFS_DEF_DINO_ACCESSOR(int64_t, int32_t, ctime);
    232 LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, ctimensec);
    233 LFS_DEF_DINO_ACCESSOR(uint32_t, uint32_t, flags);
    234 LFS_DEF_DINO_ACCESSOR(uint64_t, uint32_t, blocks);
    235 LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, gen);
    236 LFS_DEF_DINO_ACCESSOR(uint32_t, uint32_t, uid);
    237 LFS_DEF_DINO_ACCESSOR(uint32_t, uint32_t, gid);
    238 
    239 static __unused inline daddr_t
    240 lfs_dino_getdb(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix)
    241 {
    242 	KASSERT(ix < ULFS_NDADDR);
    243 	if (fs->lfs_is64) {
    244 		return dip->u_64.di_db[ix];
    245 	} else {
    246 		return dip->u_32.di_db[ix];
    247 	}
    248 }
    249 
    250 static __unused inline daddr_t
    251 lfs_dino_getib(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix)
    252 {
    253 	KASSERT(ix < ULFS_NIADDR);
    254 	if (fs->lfs_is64) {
    255 		return dip->u_64.di_ib[ix];
    256 	} else {
    257 		return dip->u_32.di_ib[ix];
    258 	}
    259 }
    260 
    261 static __unused inline void
    262 lfs_dino_setdb(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix, daddr_t val)
    263 {
    264 	KASSERT(ix < ULFS_NDADDR);
    265 	if (fs->lfs_is64) {
    266 		dip->u_64.di_db[ix] = val;
    267 	} else {
    268 		dip->u_32.di_db[ix] = val;
    269 	}
    270 }
    271 
    272 static __unused inline void
    273 lfs_dino_setib(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix, daddr_t val)
    274 {
    275 	KASSERT(ix < ULFS_NIADDR);
    276 	if (fs->lfs_is64) {
    277 		dip->u_64.di_ib[ix] = val;
    278 	} else {
    279 		dip->u_32.di_ib[ix] = val;
    280 	}
    281 }
    282 
    283 /*
    284  * "struct buf" associated definitions
    285  */
    286 
    287 # define LFS_LOCK_BUF(bp) do {						\
    288 	if (((bp)->b_flags & B_LOCKED) == 0 && bp->b_iodone == NULL) {	\
    289 		mutex_enter(&lfs_lock);					\
    290 		++locked_queue_count;					\
    291 		locked_queue_bytes += bp->b_bufsize;			\
    292 		mutex_exit(&lfs_lock);					\
    293 	}								\
    294 	(bp)->b_flags |= B_LOCKED;					\
    295 } while (0)
    296 
    297 # define LFS_UNLOCK_BUF(bp) do {					\
    298 	if (((bp)->b_flags & B_LOCKED) != 0 && bp->b_iodone == NULL) {	\
    299 		mutex_enter(&lfs_lock);					\
    300 		--locked_queue_count;					\
    301 		locked_queue_bytes -= bp->b_bufsize;			\
    302 		if (locked_queue_count < LFS_WAIT_BUFS &&		\
    303 		    locked_queue_bytes < LFS_WAIT_BYTES)		\
    304 			cv_broadcast(&locked_queue_cv);			\
    305 		mutex_exit(&lfs_lock);					\
    306 	}								\
    307 	(bp)->b_flags &= ~B_LOCKED;					\
    308 } while (0)
    309 
    310 /*
    311  * "struct inode" associated definitions
    312  */
    313 
    314 #define LFS_SET_UINO(ip, flags) do {					\
    315 	if (((flags) & IN_ACCESSED) && !((ip)->i_flag & IN_ACCESSED))	\
    316 		lfs_sb_adduinodes((ip)->i_lfs, 1);			\
    317 	if (((flags) & IN_CLEANING) && !((ip)->i_flag & IN_CLEANING))	\
    318 		lfs_sb_adduinodes((ip)->i_lfs, 1);			\
    319 	if (((flags) & IN_MODIFIED) && !((ip)->i_flag & IN_MODIFIED))	\
    320 		lfs_sb_adduinodes((ip)->i_lfs, 1);			\
    321 	(ip)->i_flag |= (flags);					\
    322 } while (0)
    323 
    324 #define LFS_CLR_UINO(ip, flags) do {					\
    325 	if (((flags) & IN_ACCESSED) && ((ip)->i_flag & IN_ACCESSED))	\
    326 		lfs_sb_subuinodes((ip)->i_lfs, 1);			\
    327 	if (((flags) & IN_CLEANING) && ((ip)->i_flag & IN_CLEANING))	\
    328 		lfs_sb_subuinodes((ip)->i_lfs, 1);			\
    329 	if (((flags) & IN_MODIFIED) && ((ip)->i_flag & IN_MODIFIED))	\
    330 		lfs_sb_subuinodes((ip)->i_lfs, 1);			\
    331 	(ip)->i_flag &= ~(flags);					\
    332 	if (lfs_sb_getuinodes((ip)->i_lfs) < 0) {			\
    333 		panic("lfs_uinodes < 0");				\
    334 	}								\
    335 } while (0)
    336 
    337 #define LFS_ITIMES(ip, acc, mod, cre) \
    338 	while ((ip)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY)) \
    339 		lfs_itimes(ip, acc, mod, cre)
    340 
    341 /*
    342  * On-disk and in-memory checkpoint segment usage structure.
    343  */
    344 
    345 #define	SEGUPB(fs)	(lfs_sb_getsepb(fs))
    346 #define	SEGTABSIZE_SU(fs)						\
    347 	((lfs_sb_getnseg(fs) + SEGUPB(fs) - 1) / lfs_sb_getsepb(fs))
    348 
    349 #ifdef _KERNEL
    350 # define SHARE_IFLOCK(F) 						\
    351   do {									\
    352 	rw_enter(&(F)->lfs_iflock, RW_READER);				\
    353   } while(0)
    354 # define UNSHARE_IFLOCK(F)						\
    355   do {									\
    356 	rw_exit(&(F)->lfs_iflock);					\
    357   } while(0)
    358 #else /* ! _KERNEL */
    359 # define SHARE_IFLOCK(F)
    360 # define UNSHARE_IFLOCK(F)
    361 #endif /* ! _KERNEL */
    362 
    363 /* Read in the block with a specific segment usage entry from the ifile. */
    364 #define	LFS_SEGENTRY(SP, F, IN, BP) do {				\
    365 	int _e;								\
    366 	SHARE_IFLOCK(F);						\
    367 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    368 	if ((_e = bread((F)->lfs_ivnode,				\
    369 	    ((IN) / lfs_sb_getsepb(F)) + lfs_sb_getcleansz(F),		\
    370 	    lfs_sb_getbsize(F), 0, &(BP))) != 0)			\
    371 		panic("lfs: ifile read: %d", _e);			\
    372 	if (lfs_sb_getversion(F) == 1)					\
    373 		(SP) = (SEGUSE *)((SEGUSE_V1 *)(BP)->b_data +		\
    374 			((IN) & (lfs_sb_getsepb(F) - 1)));		\
    375 	else								\
    376 		(SP) = (SEGUSE *)(BP)->b_data + ((IN) % lfs_sb_getsepb(F)); \
    377 	UNSHARE_IFLOCK(F);						\
    378 } while (0)
    379 
    380 #define LFS_WRITESEGENTRY(SP, F, IN, BP) do {				\
    381 	if ((SP)->su_nbytes == 0)					\
    382 		(SP)->su_flags |= SEGUSE_EMPTY;				\
    383 	else								\
    384 		(SP)->su_flags &= ~SEGUSE_EMPTY;			\
    385 	(F)->lfs_suflags[(F)->lfs_activesb][(IN)] = (SP)->su_flags;	\
    386 	LFS_BWRITE_LOG(BP);						\
    387 } while (0)
    388 
    389 /*
    390  * FINFO (file info) entries.
    391  */
    392 
    393 /* Size of an on-disk block pointer, e.g. in an indirect block. */
    394 /* XXX: move to a more suitable location in this file */
    395 #define LFS_BLKPTRSIZE(fs) ((fs)->lfs_is64 ? sizeof(int64_t) : sizeof(int32_t))
    396 
    397 /* Size of an on-disk inode number. */
    398 /* XXX: move to a more suitable location in this file */
    399 #define LFS_INUMSIZE(fs) ((fs)->lfs_is64 ? sizeof(int64_t) : sizeof(int32_t))
    400 
    401 /* size of a FINFO, without the block pointers */
    402 #define	FINFOSIZE(fs)	((fs)->lfs_is64 ? sizeof(FINFO64) : sizeof(FINFO32))
    403 
    404 /* Full size of the provided FINFO record, including its block pointers. */
    405 #define FINFO_FULLSIZE(fs, fip) \
    406 	(FINFOSIZE(fs) + lfs_fi_getnblocks(fs, fip) * LFS_BLKPTRSIZE(fs))
    407 
    408 #define NEXT_FINFO(fs, fip) \
    409 	((FINFO *)((char *)(fip) + FINFO_FULLSIZE(fs, fip)))
    410 
    411 #define LFS_DEF_FI_ACCESSOR(type, type32, field) \
    412 	static __unused inline type				\
    413 	lfs_fi_get##field(STRUCT_LFS *fs, FINFO *fip)		\
    414 	{							\
    415 		if (fs->lfs_is64) {				\
    416 			return fip->u_64.fi_##field; 		\
    417 		} else {					\
    418 			return fip->u_32.fi_##field; 		\
    419 		}						\
    420 	}							\
    421 	static __unused inline void				\
    422 	lfs_fi_set##field(STRUCT_LFS *fs, FINFO *fip, type val) \
    423 	{							\
    424 		if (fs->lfs_is64) {				\
    425 			type *p = &fip->u_64.fi_##field;	\
    426 			(void)p;				\
    427 			fip->u_64.fi_##field = val;		\
    428 		} else {					\
    429 			type32 *p = &fip->u_32.fi_##field;	\
    430 			(void)p;				\
    431 			fip->u_32.fi_##field = val;		\
    432 		}						\
    433 	}							\
    434 
    435 LFS_DEF_FI_ACCESSOR(uint32_t, uint32_t, nblocks);
    436 LFS_DEF_FI_ACCESSOR(uint32_t, uint32_t, version);
    437 LFS_DEF_FI_ACCESSOR(uint64_t, uint32_t, ino);
    438 LFS_DEF_FI_ACCESSOR(uint32_t, uint32_t, lastlength);
    439 
    440 static __unused inline daddr_t
    441 lfs_fi_getblock(STRUCT_LFS *fs, FINFO *fip, unsigned index)
    442 {
    443 	void *firstblock;
    444 
    445 	firstblock = (char *)fip + FINFOSIZE(fs);
    446 	KASSERT(index < lfs_fi_getnblocks(fs, fip));
    447 	if (fs->lfs_is64) {
    448 		return ((int64_t *)firstblock)[index];
    449 	} else {
    450 		return ((int32_t *)firstblock)[index];
    451 	}
    452 }
    453 
    454 static __unused inline void
    455 lfs_fi_setblock(STRUCT_LFS *fs, FINFO *fip, unsigned index, daddr_t blk)
    456 {
    457 	void *firstblock;
    458 
    459 	firstblock = (char *)fip + FINFOSIZE(fs);
    460 	KASSERT(index < lfs_fi_getnblocks(fs, fip));
    461 	if (fs->lfs_is64) {
    462 		((int64_t *)firstblock)[index] = blk;
    463 	} else {
    464 		((int32_t *)firstblock)[index] = blk;
    465 	}
    466 }
    467 
    468 /*
    469  * Index file inode entries.
    470  */
    471 
    472 /*
    473  * LFSv1 compatibility code is not allowed to touch if_atime, since it
    474  * may not be mapped!
    475  */
    476 /* Read in the block with a specific inode from the ifile. */
    477 #define	LFS_IENTRY(IP, F, IN, BP) do {					\
    478 	int _e;								\
    479 	SHARE_IFLOCK(F);						\
    480 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    481 	if ((_e = bread((F)->lfs_ivnode,				\
    482 	(IN) / lfs_sb_getifpb(F) + lfs_sb_getcleansz(F) + lfs_sb_getsegtabsz(F), \
    483 	lfs_sb_getbsize(F), 0, &(BP))) != 0)				\
    484 		panic("lfs: ifile ino %d read %d", (int)(IN), _e);	\
    485 	if ((F)->lfs_is64) {						\
    486 		(IP) = (IFILE *)((IFILE64 *)(BP)->b_data +		\
    487 				 (IN) % lfs_sb_getifpb(F));		\
    488 	} else if (lfs_sb_getversion(F) > 1) {				\
    489 		(IP) = (IFILE *)((IFILE32 *)(BP)->b_data +		\
    490 				(IN) % lfs_sb_getifpb(F)); 		\
    491 	} else {							\
    492 		(IP) = (IFILE *)((IFILE_V1 *)(BP)->b_data +		\
    493 				 (IN) % lfs_sb_getifpb(F));		\
    494 	}								\
    495 	UNSHARE_IFLOCK(F);						\
    496 } while (0)
    497 #define LFS_IENTRY_NEXT(IP, F) do { \
    498 	if ((F)->lfs_is64) {						\
    499 		(IP) = (IFILE *)((IFILE64 *)(IP) + 1);			\
    500 	} else if (lfs_sb_getversion(F) > 1) {				\
    501 		(IP) = (IFILE *)((IFILE32 *)(IP) + 1);			\
    502 	} else {							\
    503 		(IP) = (IFILE *)((IFILE_V1 *)(IP) + 1);			\
    504 	}								\
    505 } while (0)
    506 
    507 #define LFS_DEF_IF_ACCESSOR(type, type32, field) \
    508 	static __unused inline type				\
    509 	lfs_if_get##field(STRUCT_LFS *fs, IFILE *ifp)		\
    510 	{							\
    511 		if (fs->lfs_is64) {				\
    512 			return ifp->u_64.if_##field; 		\
    513 		} else {					\
    514 			return ifp->u_32.if_##field; 		\
    515 		}						\
    516 	}							\
    517 	static __unused inline void				\
    518 	lfs_if_set##field(STRUCT_LFS *fs, IFILE *ifp, type val) \
    519 	{							\
    520 		if (fs->lfs_is64) {				\
    521 			type *p = &ifp->u_64.if_##field;	\
    522 			(void)p;				\
    523 			ifp->u_64.if_##field = val;		\
    524 		} else {					\
    525 			type32 *p = &ifp->u_32.if_##field;	\
    526 			(void)p;				\
    527 			ifp->u_32.if_##field = val;		\
    528 		}						\
    529 	}							\
    530 
    531 LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, version);
    532 LFS_DEF_IF_ACCESSOR(int64_t, int32_t, daddr);
    533 LFS_DEF_IF_ACCESSOR(u_int64_t, u_int32_t, nextfree);
    534 LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, atime_sec);
    535 LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, atime_nsec);
    536 
    537 /*
    538  * Cleaner information structure.  This resides in the ifile and is used
    539  * to pass information from the kernel to the cleaner.
    540  */
    541 
    542 #define	CLEANSIZE_SU(fs)						\
    543 	((((fs)->lfs_is64 ? sizeof(CLEANERINFO64) : sizeof(CLEANERINFO32)) + \
    544 		lfs_sb_getbsize(fs) - 1) >> lfs_sb_getbshift(fs))
    545 
    546 #define LFS_DEF_CI_ACCESSOR(type, type32, field) \
    547 	static __unused inline type				\
    548 	lfs_ci_get##field(STRUCT_LFS *fs, CLEANERINFO *cip)	\
    549 	{							\
    550 		if (fs->lfs_is64) {				\
    551 			return cip->u_64.field; 		\
    552 		} else {					\
    553 			return cip->u_32.field; 		\
    554 		}						\
    555 	}							\
    556 	static __unused inline void				\
    557 	lfs_ci_set##field(STRUCT_LFS *fs, CLEANERINFO *cip, type val) \
    558 	{							\
    559 		if (fs->lfs_is64) {				\
    560 			type *p = &cip->u_64.field;		\
    561 			(void)p;				\
    562 			cip->u_64.field = val;			\
    563 		} else {					\
    564 			type32 *p = &cip->u_32.field;		\
    565 			(void)p;				\
    566 			cip->u_32.field = val;			\
    567 		}						\
    568 	}							\
    569 
    570 LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, clean);
    571 LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, dirty);
    572 LFS_DEF_CI_ACCESSOR(int64_t, int32_t, bfree);
    573 LFS_DEF_CI_ACCESSOR(int64_t, int32_t, avail);
    574 LFS_DEF_CI_ACCESSOR(u_int64_t, u_int32_t, free_head);
    575 LFS_DEF_CI_ACCESSOR(u_int64_t, u_int32_t, free_tail);
    576 LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, flags);
    577 
    578 static __unused inline void
    579 lfs_ci_shiftcleantodirty(STRUCT_LFS *fs, CLEANERINFO *cip, unsigned num)
    580 {
    581 	lfs_ci_setclean(fs, cip, lfs_ci_getclean(fs, cip) - num);
    582 	lfs_ci_setdirty(fs, cip, lfs_ci_getdirty(fs, cip) + num);
    583 }
    584 
    585 static __unused inline void
    586 lfs_ci_shiftdirtytoclean(STRUCT_LFS *fs, CLEANERINFO *cip, unsigned num)
    587 {
    588 	lfs_ci_setdirty(fs, cip, lfs_ci_getdirty(fs, cip) - num);
    589 	lfs_ci_setclean(fs, cip, lfs_ci_getclean(fs, cip) + num);
    590 }
    591 
    592 /* Read in the block with the cleaner info from the ifile. */
    593 #define LFS_CLEANERINFO(CP, F, BP) do {					\
    594 	SHARE_IFLOCK(F);						\
    595 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    596 	if (bread((F)->lfs_ivnode,					\
    597 	    (daddr_t)0, lfs_sb_getbsize(F), 0, &(BP)))			\
    598 		panic("lfs: ifile read");				\
    599 	(CP) = (CLEANERINFO *)(BP)->b_data;				\
    600 	UNSHARE_IFLOCK(F);						\
    601 } while (0)
    602 
    603 /*
    604  * Synchronize the Ifile cleaner info with current avail and bfree.
    605  */
    606 #define LFS_SYNC_CLEANERINFO(cip, fs, bp, w) do {		 	\
    607     mutex_enter(&lfs_lock);						\
    608     if ((w) || lfs_ci_getbfree(fs, cip) != lfs_sb_getbfree(fs) ||	\
    609 	lfs_ci_getavail(fs, cip) != lfs_sb_getavail(fs) - fs->lfs_ravail - \
    610 	fs->lfs_favail) {	 					\
    611 	lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs));		 	\
    612 	lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs) - fs->lfs_ravail -	\
    613 		fs->lfs_favail);				 	\
    614 	if (((bp)->b_flags & B_GATHERED) == 0) {		 	\
    615 		fs->lfs_flags |= LFS_IFDIRTY;				\
    616 	}								\
    617 	mutex_exit(&lfs_lock);						\
    618 	(void) LFS_BWRITE_LOG(bp); /* Ifile */			 	\
    619     } else {							 	\
    620 	mutex_exit(&lfs_lock);						\
    621 	brelse(bp, 0);						 	\
    622     }									\
    623 } while (0)
    624 
    625 /*
    626  * Get the head of the inode free list.
    627  * Always called with the segment lock held.
    628  */
    629 #define LFS_GET_HEADFREE(FS, CIP, BP, FREEP) do {			\
    630 	if (lfs_sb_getversion(FS) > 1) {				\
    631 		LFS_CLEANERINFO((CIP), (FS), (BP));			\
    632 		lfs_sb_setfreehd(FS, lfs_ci_getfree_head(FS, CIP));	\
    633 		brelse(BP, 0);						\
    634 	}								\
    635 	*(FREEP) = lfs_sb_getfreehd(FS);				\
    636 } while (0)
    637 
    638 #define LFS_PUT_HEADFREE(FS, CIP, BP, VAL) do {				\
    639 	lfs_sb_setfreehd(FS, VAL);					\
    640 	if (lfs_sb_getversion(FS) > 1) {				\
    641 		LFS_CLEANERINFO((CIP), (FS), (BP));			\
    642 		lfs_ci_setfree_head(FS, CIP, VAL);			\
    643 		LFS_BWRITE_LOG(BP);					\
    644 		mutex_enter(&lfs_lock);					\
    645 		(FS)->lfs_flags |= LFS_IFDIRTY;				\
    646 		mutex_exit(&lfs_lock);					\
    647 	}								\
    648 } while (0)
    649 
    650 #define LFS_GET_TAILFREE(FS, CIP, BP, FREEP) do {			\
    651 	LFS_CLEANERINFO((CIP), (FS), (BP));				\
    652 	*(FREEP) = lfs_ci_getfree_tail(FS, CIP);			\
    653 	brelse(BP, 0);							\
    654 } while (0)
    655 
    656 #define LFS_PUT_TAILFREE(FS, CIP, BP, VAL) do {				\
    657 	LFS_CLEANERINFO((CIP), (FS), (BP));				\
    658 	lfs_ci_setfree_tail(FS, CIP, VAL);				\
    659 	LFS_BWRITE_LOG(BP);						\
    660 	mutex_enter(&lfs_lock);						\
    661 	(FS)->lfs_flags |= LFS_IFDIRTY;					\
    662 	mutex_exit(&lfs_lock);						\
    663 } while (0)
    664 
    665 /*
    666  * On-disk segment summary information
    667  */
    668 
    669 #define SEGSUM_SIZE(fs) \
    670 	(fs->lfs_is64 ? sizeof(SEGSUM64) : \
    671 	 lfs_sb_getversion(fs) > 1 ? sizeof(SEGSUM32) : sizeof(SEGSUM_V1))
    672 
    673 /*
    674  * The SEGSUM structure is followed by FINFO structures. Get the pointer
    675  * to the first FINFO.
    676  *
    677  * XXX this can't be a macro yet; this file needs to be resorted.
    678  */
    679 #if 0
    680 static __unused inline FINFO *
    681 segsum_finfobase(STRUCT_LFS *fs, SEGSUM *ssp)
    682 {
    683 	return (FINFO *)((char *)ssp + SEGSUM_SIZE(fs));
    684 }
    685 #else
    686 #define SEGSUM_FINFOBASE(fs, ssp) \
    687 	((FINFO *)((char *)(ssp) + SEGSUM_SIZE(fs)));
    688 #endif
    689 
    690 #define LFS_DEF_SS_ACCESSOR(type, type32, field) \
    691 	static __unused inline type				\
    692 	lfs_ss_get##field(STRUCT_LFS *fs, SEGSUM *ssp)		\
    693 	{							\
    694 		if (fs->lfs_is64) {				\
    695 			return ssp->u_64.ss_##field; 		\
    696 		} else {					\
    697 			return ssp->u_32.ss_##field; 		\
    698 		}						\
    699 	}							\
    700 	static __unused inline void				\
    701 	lfs_ss_set##field(STRUCT_LFS *fs, SEGSUM *ssp, type val) \
    702 	{							\
    703 		if (fs->lfs_is64) {				\
    704 			type *p = &ssp->u_64.ss_##field;	\
    705 			(void)p;				\
    706 			ssp->u_64.ss_##field = val;		\
    707 		} else {					\
    708 			type32 *p = &ssp->u_32.ss_##field;	\
    709 			(void)p;				\
    710 			ssp->u_32.ss_##field = val;		\
    711 		}						\
    712 	}							\
    713 
    714 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, sumsum);
    715 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, datasum);
    716 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, magic);
    717 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, ident);
    718 LFS_DEF_SS_ACCESSOR(int64_t, int32_t, next);
    719 LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, nfinfo);
    720 LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, ninos);
    721 LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, flags);
    722 LFS_DEF_SS_ACCESSOR(uint64_t, uint32_t, reclino);
    723 LFS_DEF_SS_ACCESSOR(uint64_t, uint64_t, serial);
    724 LFS_DEF_SS_ACCESSOR(uint64_t, uint64_t, create);
    725 
    726 static __unused inline size_t
    727 lfs_ss_getsumstart(STRUCT_LFS *fs)
    728 {
    729 	/* These are actually all the same. */
    730 	if (fs->lfs_is64) {
    731 		return offsetof(SEGSUM64, ss_datasum);
    732 	} else /* if (lfs_sb_getversion(fs) > 1) */ {
    733 		return offsetof(SEGSUM32, ss_datasum);
    734 	} /* else {
    735 		return offsetof(SEGSUM_V1, ss_datasum);
    736 	} */
    737 	/*
    738 	 * XXX ^^^ until this file is resorted lfs_sb_getversion isn't
    739 	 * defined yet.
    740 	 */
    741 }
    742 
    743 static __unused inline uint32_t
    744 lfs_ss_getocreate(STRUCT_LFS *fs, SEGSUM *ssp)
    745 {
    746 	KASSERT(fs->lfs_is64 == 0);
    747 	/* XXX need to resort this file before we can do this */
    748 	//KASSERT(lfs_sb_getversion(fs) == 1);
    749 
    750 	return ssp->u_v1.ss_create;
    751 }
    752 
    753 static __unused inline void
    754 lfs_ss_setocreate(STRUCT_LFS *fs, SEGSUM *ssp, uint32_t val)
    755 {
    756 	KASSERT(fs->lfs_is64 == 0);
    757 	/* XXX need to resort this file before we can do this */
    758 	//KASSERT(lfs_sb_getversion(fs) == 1);
    759 
    760 	ssp->u_v1.ss_create = val;
    761 }
    762 
    763 
    764 /*
    765  * Super block.
    766  */
    767 
    768 /*
    769  * Generate accessors for the on-disk superblock fields with cpp.
    770  */
    771 
    772 #define LFS_DEF_SB_ACCESSOR_FULL(type, type32, field) \
    773 	static __unused inline type				\
    774 	lfs_sb_get##field(STRUCT_LFS *fs)			\
    775 	{							\
    776 		if (fs->lfs_is64) {				\
    777 			return fs->lfs_dlfs_u.u_64.dlfs_##field; \
    778 		} else {					\
    779 			return fs->lfs_dlfs_u.u_32.dlfs_##field; \
    780 		}						\
    781 	}							\
    782 	static __unused inline void				\
    783 	lfs_sb_set##field(STRUCT_LFS *fs, type val)		\
    784 	{							\
    785 		if (fs->lfs_is64) {				\
    786 			fs->lfs_dlfs_u.u_64.dlfs_##field = val;	\
    787 		} else {					\
    788 			fs->lfs_dlfs_u.u_32.dlfs_##field = val;	\
    789 		}						\
    790 	}							\
    791 	static __unused inline void				\
    792 	lfs_sb_add##field(STRUCT_LFS *fs, type val)		\
    793 	{							\
    794 		if (fs->lfs_is64) {				\
    795 			type *p64 = &fs->lfs_dlfs_u.u_64.dlfs_##field; \
    796 			*p64 += val;				\
    797 		} else {					\
    798 			type32 *p32 = &fs->lfs_dlfs_u.u_32.dlfs_##field; \
    799 			*p32 += val;				\
    800 		}						\
    801 	}							\
    802 	static __unused inline void				\
    803 	lfs_sb_sub##field(STRUCT_LFS *fs, type val)		\
    804 	{							\
    805 		if (fs->lfs_is64) {				\
    806 			type *p64 = &fs->lfs_dlfs_u.u_64.dlfs_##field; \
    807 			*p64 -= val;				\
    808 		} else {					\
    809 			type32 *p32 = &fs->lfs_dlfs_u.u_32.dlfs_##field; \
    810 			*p32 -= val;				\
    811 		}						\
    812 	}
    813 
    814 #define LFS_DEF_SB_ACCESSOR(t, f) LFS_DEF_SB_ACCESSOR_FULL(t, t, f)
    815 
    816 #define LFS_DEF_SB_ACCESSOR_32ONLY(type, field, val64) \
    817 	static __unused inline type				\
    818 	lfs_sb_get##field(STRUCT_LFS *fs)			\
    819 	{							\
    820 		if (fs->lfs_is64) {				\
    821 			return val64;				\
    822 		} else {					\
    823 			return fs->lfs_dlfs_u.u_32.dlfs_##field; \
    824 		}						\
    825 	}
    826 
    827 #define lfs_magic lfs_dlfs.dlfs_magic
    828 LFS_DEF_SB_ACCESSOR(u_int32_t, version);
    829 LFS_DEF_SB_ACCESSOR_FULL(u_int64_t, u_int32_t, size);
    830 LFS_DEF_SB_ACCESSOR(u_int32_t, ssize);
    831 LFS_DEF_SB_ACCESSOR_FULL(u_int64_t, u_int32_t, dsize);
    832 LFS_DEF_SB_ACCESSOR(u_int32_t, bsize);
    833 LFS_DEF_SB_ACCESSOR(u_int32_t, fsize);
    834 LFS_DEF_SB_ACCESSOR(u_int32_t, frag);
    835 LFS_DEF_SB_ACCESSOR(u_int32_t, freehd);
    836 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, bfree);
    837 LFS_DEF_SB_ACCESSOR(u_int32_t, nfiles);
    838 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, avail);
    839 LFS_DEF_SB_ACCESSOR(int32_t, uinodes);
    840 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, idaddr);
    841 LFS_DEF_SB_ACCESSOR(u_int32_t, ifile);
    842 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, lastseg);
    843 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, nextseg);
    844 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, curseg);
    845 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, offset);
    846 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, lastpseg);
    847 LFS_DEF_SB_ACCESSOR(u_int32_t, inopf);
    848 LFS_DEF_SB_ACCESSOR(u_int32_t, minfree);
    849 LFS_DEF_SB_ACCESSOR(uint64_t, maxfilesize);
    850 LFS_DEF_SB_ACCESSOR(u_int32_t, fsbpseg);
    851 LFS_DEF_SB_ACCESSOR(u_int32_t, inopb);
    852 LFS_DEF_SB_ACCESSOR(u_int32_t, ifpb);
    853 LFS_DEF_SB_ACCESSOR(u_int32_t, sepb);
    854 LFS_DEF_SB_ACCESSOR(u_int32_t, nindir);
    855 LFS_DEF_SB_ACCESSOR(u_int32_t, nseg);
    856 LFS_DEF_SB_ACCESSOR(u_int32_t, nspf);
    857 LFS_DEF_SB_ACCESSOR(u_int32_t, cleansz);
    858 LFS_DEF_SB_ACCESSOR(u_int32_t, segtabsz);
    859 LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, segmask, 0);
    860 LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, segshift, 0);
    861 LFS_DEF_SB_ACCESSOR(u_int64_t, bmask);
    862 LFS_DEF_SB_ACCESSOR(u_int32_t, bshift);
    863 LFS_DEF_SB_ACCESSOR(u_int64_t, ffmask);
    864 LFS_DEF_SB_ACCESSOR(u_int32_t, ffshift);
    865 LFS_DEF_SB_ACCESSOR(u_int64_t, fbmask);
    866 LFS_DEF_SB_ACCESSOR(u_int32_t, fbshift);
    867 LFS_DEF_SB_ACCESSOR(u_int32_t, blktodb);
    868 LFS_DEF_SB_ACCESSOR(u_int32_t, fsbtodb);
    869 LFS_DEF_SB_ACCESSOR(u_int32_t, sushift);
    870 LFS_DEF_SB_ACCESSOR(int32_t, maxsymlinklen);
    871 LFS_DEF_SB_ACCESSOR(u_int32_t, cksum);
    872 LFS_DEF_SB_ACCESSOR(u_int16_t, pflags);
    873 LFS_DEF_SB_ACCESSOR(u_int32_t, nclean);
    874 LFS_DEF_SB_ACCESSOR(int32_t, dmeta);
    875 LFS_DEF_SB_ACCESSOR(u_int32_t, minfreeseg);
    876 LFS_DEF_SB_ACCESSOR(u_int32_t, sumsize);
    877 LFS_DEF_SB_ACCESSOR(u_int64_t, serial);
    878 LFS_DEF_SB_ACCESSOR(u_int32_t, ibsize);
    879 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, s0addr);
    880 LFS_DEF_SB_ACCESSOR(u_int64_t, tstamp);
    881 LFS_DEF_SB_ACCESSOR(u_int32_t, inodefmt);
    882 LFS_DEF_SB_ACCESSOR(u_int32_t, interleave);
    883 LFS_DEF_SB_ACCESSOR(u_int32_t, ident);
    884 LFS_DEF_SB_ACCESSOR(u_int32_t, resvseg);
    885 
    886 /* special-case accessors */
    887 
    888 /*
    889  * the v1 otstamp field lives in what's now dlfs_inopf
    890  */
    891 #define lfs_sb_getotstamp(fs) lfs_sb_getinopf(fs)
    892 #define lfs_sb_setotstamp(fs, val) lfs_sb_setinopf(fs, val)
    893 
    894 /*
    895  * lfs_sboffs is an array
    896  */
    897 static __unused inline int32_t
    898 lfs_sb_getsboff(STRUCT_LFS *fs, unsigned n)
    899 {
    900 #ifdef KASSERT /* ugh */
    901 	KASSERT(n < LFS_MAXNUMSB);
    902 #endif
    903 	if (fs->lfs_is64) {
    904 		return fs->lfs_dlfs_u.u_64.dlfs_sboffs[n];
    905 	} else {
    906 		return fs->lfs_dlfs_u.u_32.dlfs_sboffs[n];
    907 	}
    908 }
    909 static __unused inline void
    910 lfs_sb_setsboff(STRUCT_LFS *fs, unsigned n, int32_t val)
    911 {
    912 #ifdef KASSERT /* ugh */
    913 	KASSERT(n < LFS_MAXNUMSB);
    914 #endif
    915 	if (fs->lfs_is64) {
    916 		fs->lfs_dlfs_u.u_64.dlfs_sboffs[n] = val;
    917 	} else {
    918 		fs->lfs_dlfs_u.u_32.dlfs_sboffs[n] = val;
    919 	}
    920 }
    921 
    922 /*
    923  * lfs_fsmnt is a string
    924  */
    925 static __unused inline const char *
    926 lfs_sb_getfsmnt(STRUCT_LFS *fs)
    927 {
    928 	if (fs->lfs_is64) {
    929 		return fs->lfs_dlfs_u.u_64.dlfs_fsmnt;
    930 	} else {
    931 		return fs->lfs_dlfs_u.u_32.dlfs_fsmnt;
    932 	}
    933 }
    934 
    935 static __unused inline void
    936 lfs_sb_setfsmnt(STRUCT_LFS *fs, const char *str)
    937 {
    938 	if (fs->lfs_is64) {
    939 		(void)strncpy(fs->lfs_dlfs_u.u_64.dlfs_fsmnt, str,
    940 			sizeof(fs->lfs_dlfs_u.u_64.dlfs_fsmnt));
    941 	} else {
    942 		(void)strncpy(fs->lfs_dlfs_u.u_32.dlfs_fsmnt, str,
    943 			sizeof(fs->lfs_dlfs_u.u_32.dlfs_fsmnt));
    944 	}
    945 }
    946 
    947 /* Highest addressable fsb */
    948 #define LFS_MAX_DADDR(fs) \
    949 	((fs)->lfs_is64 ? 0x7fffffffffffffff : 0x7fffffff)
    950 
    951 /* LFS_NINDIR is the number of indirects in a file system block. */
    952 #define	LFS_NINDIR(fs)	(lfs_sb_getnindir(fs))
    953 
    954 /* LFS_INOPB is the number of inodes in a secondary storage block. */
    955 #define	LFS_INOPB(fs)	(lfs_sb_getinopb(fs))
    956 /* LFS_INOPF is the number of inodes in a fragment. */
    957 #define LFS_INOPF(fs)	(lfs_sb_getinopf(fs))
    958 
    959 #define	lfs_blkoff(fs, loc)	((int)((loc) & lfs_sb_getbmask(fs)))
    960 #define lfs_fragoff(fs, loc)    /* calculates (loc % fs->lfs_fsize) */ \
    961     ((int)((loc) & lfs_sb_getffmask(fs)))
    962 
    963 /* XXX: lowercase these as they're no longer macros */
    964 /* Frags to diskblocks */
    965 static __unused inline uint64_t
    966 LFS_FSBTODB(STRUCT_LFS *fs, uint64_t b)
    967 {
    968 #if defined(_KERNEL)
    969 	return b << (lfs_sb_getffshift(fs) - DEV_BSHIFT);
    970 #else
    971 	return b << lfs_sb_getfsbtodb(fs);
    972 #endif
    973 }
    974 /* Diskblocks to frags */
    975 static __unused inline uint64_t
    976 LFS_DBTOFSB(STRUCT_LFS *fs, uint64_t b)
    977 {
    978 #if defined(_KERNEL)
    979 	return b >> (lfs_sb_getffshift(fs) - DEV_BSHIFT);
    980 #else
    981 	return b >> lfs_sb_getfsbtodb(fs);
    982 #endif
    983 }
    984 
    985 #define	lfs_lblkno(fs, loc)	((loc) >> lfs_sb_getbshift(fs))
    986 #define	lfs_lblktosize(fs, blk)	((blk) << lfs_sb_getbshift(fs))
    987 
    988 /* Frags to bytes */
    989 static __unused inline uint64_t
    990 lfs_fsbtob(STRUCT_LFS *fs, uint64_t b)
    991 {
    992 	return b << lfs_sb_getffshift(fs);
    993 }
    994 /* Bytes to frags */
    995 static __unused inline uint64_t
    996 lfs_btofsb(STRUCT_LFS *fs, uint64_t b)
    997 {
    998 	return b >> lfs_sb_getffshift(fs);
    999 }
   1000 
   1001 #define lfs_numfrags(fs, loc)	/* calculates (loc / fs->lfs_fsize) */	\
   1002 	((loc) >> lfs_sb_getffshift(fs))
   1003 #define lfs_blkroundup(fs, size)/* calculates roundup(size, lfs_sb_getbsize(fs)) */ \
   1004 	((off_t)(((size) + lfs_sb_getbmask(fs)) & (~lfs_sb_getbmask(fs))))
   1005 #define lfs_fragroundup(fs, size)/* calculates roundup(size, fs->lfs_fsize) */ \
   1006 	((off_t)(((size) + lfs_sb_getffmask(fs)) & (~lfs_sb_getffmask(fs))))
   1007 #define lfs_fragstoblks(fs, frags)/* calculates (frags / fs->fs_frag) */ \
   1008 	((frags) >> lfs_sb_getfbshift(fs))
   1009 #define lfs_blkstofrags(fs, blks)/* calculates (blks * fs->fs_frag) */ \
   1010 	((blks) << lfs_sb_getfbshift(fs))
   1011 #define lfs_fragnum(fs, fsb)	/* calculates (fsb % fs->lfs_frag) */	\
   1012 	((fsb) & ((fs)->lfs_frag - 1))
   1013 #define lfs_blknum(fs, fsb)	/* calculates rounddown(fsb, fs->lfs_frag) */ \
   1014 	((fsb) &~ ((fs)->lfs_frag - 1))
   1015 #define lfs_dblksize(fs, dp, lbn) \
   1016 	(((lbn) >= ULFS_NDADDR || lfs_dino_getsize(fs, dp) >= ((lbn) + 1) << lfs_sb_getbshift(fs)) \
   1017 	    ? lfs_sb_getbsize(fs) \
   1018 	    : (lfs_fragroundup(fs, lfs_blkoff(fs, lfs_dino_getsize(fs, dp)))))
   1019 
   1020 #define	lfs_segsize(fs)	(lfs_sb_getversion(fs) == 1 ?	     		\
   1021 			   lfs_lblktosize((fs), lfs_sb_getssize(fs)) :	\
   1022 			   lfs_sb_getssize(fs))
   1023 /* XXX segtod produces a result in frags despite the 'd' */
   1024 #define lfs_segtod(fs, seg) (lfs_btofsb(fs, lfs_segsize(fs)) * (seg))
   1025 #define	lfs_dtosn(fs, daddr)	/* block address to segment number */	\
   1026 	((uint32_t)(((daddr) - lfs_sb_gets0addr(fs)) / lfs_segtod((fs), 1)))
   1027 #define lfs_sntod(fs, sn)	/* segment number to disk address */	\
   1028 	((daddr_t)(lfs_segtod((fs), (sn)) + lfs_sb_gets0addr(fs)))
   1029 
   1030 /* XXX, blah. make this appear only if struct inode is defined */
   1031 #ifdef _UFS_LFS_LFS_INODE_H_
   1032 static __unused inline uint32_t
   1033 lfs_blksize(STRUCT_LFS *fs, struct inode *ip, uint64_t lbn)
   1034 {
   1035 	if (lbn >= ULFS_NDADDR || ip->i_ffs1_size >= (lbn + 1) << lfs_sb_getbshift(fs)) {
   1036 		return lfs_sb_getbsize(fs);
   1037 	} else {
   1038 		return lfs_fragroundup(fs, lfs_blkoff(fs, ip->i_ffs1_size));
   1039 	}
   1040 }
   1041 #endif
   1042 
   1043 /*
   1044  * union lfs_blocks
   1045  */
   1046 
   1047 static __unused inline void
   1048 lfs_blocks_fromvoid(STRUCT_LFS *fs, union lfs_blocks *bp, void *p)
   1049 {
   1050 	if (fs->lfs_is64) {
   1051 		bp->b64 = p;
   1052 	} else {
   1053 		bp->b32 = p;
   1054 	}
   1055 }
   1056 
   1057 static __unused inline void
   1058 lfs_blocks_fromfinfo(STRUCT_LFS *fs, union lfs_blocks *bp, FINFO *fip)
   1059 {
   1060 	void *firstblock;
   1061 
   1062 	firstblock = (char *)fip + FINFOSIZE(fs);
   1063 	if (fs->lfs_is64) {
   1064 		bp->b64 = (int64_t *)firstblock;
   1065 	}  else {
   1066 		bp->b32 = (int32_t *)firstblock;
   1067 	}
   1068 }
   1069 
   1070 static __unused inline daddr_t
   1071 lfs_blocks_get(STRUCT_LFS *fs, union lfs_blocks *bp, unsigned index)
   1072 {
   1073 	if (fs->lfs_is64) {
   1074 		return bp->b64[index];
   1075 	} else {
   1076 		return bp->b32[index];
   1077 	}
   1078 }
   1079 
   1080 static __unused inline void
   1081 lfs_blocks_set(STRUCT_LFS *fs, union lfs_blocks *bp, unsigned index, daddr_t val)
   1082 {
   1083 	if (fs->lfs_is64) {
   1084 		bp->b64[index] = val;
   1085 	} else {
   1086 		bp->b32[index] = val;
   1087 	}
   1088 }
   1089 
   1090 static __unused inline void
   1091 lfs_blocks_inc(STRUCT_LFS *fs, union lfs_blocks *bp)
   1092 {
   1093 	if (fs->lfs_is64) {
   1094 		bp->b64++;
   1095 	} else {
   1096 		bp->b32++;
   1097 	}
   1098 }
   1099 
   1100 static __unused inline int
   1101 lfs_blocks_eq(STRUCT_LFS *fs, union lfs_blocks *bp1, union lfs_blocks *bp2)
   1102 {
   1103 	if (fs->lfs_is64) {
   1104 		return bp1->b64 == bp2->b64;
   1105 	} else {
   1106 		return bp1->b32 == bp2->b32;
   1107 	}
   1108 }
   1109 
   1110 static __unused inline int
   1111 lfs_blocks_sub(STRUCT_LFS *fs, union lfs_blocks *bp1, union lfs_blocks *bp2)
   1112 {
   1113 	/* (remember that the pointers are typed) */
   1114 	if (fs->lfs_is64) {
   1115 		return bp1->b64 - bp2->b64;
   1116 	} else {
   1117 		return bp1->b32 - bp2->b32;
   1118 	}
   1119 }
   1120 
   1121 /*
   1122  * struct segment
   1123  */
   1124 
   1125 
   1126 /*
   1127  * Macros for determining free space on the disk, with the variable metadata
   1128  * of segment summaries and inode blocks taken into account.
   1129  */
   1130 /*
   1131  * Estimate number of clean blocks not available for writing because
   1132  * they will contain metadata or overhead.  This is calculated as
   1133  *
   1134  *		E = ((C * M / D) * D + (0) * (T - D)) / T
   1135  * or more simply
   1136  *		E = (C * M) / T
   1137  *
   1138  * where
   1139  * C is the clean space,
   1140  * D is the dirty space,
   1141  * M is the dirty metadata, and
   1142  * T = C + D is the total space on disk.
   1143  *
   1144  * This approximates the old formula of E = C * M / D when D is close to T,
   1145  * but avoids falsely reporting "disk full" when the sample size (D) is small.
   1146  */
   1147 #define LFS_EST_CMETA(F) (int32_t)((					\
   1148 	(lfs_sb_getdmeta(F) * (int64_t)lfs_sb_getnclean(F)) / 		\
   1149 	(lfs_sb_getnseg(F))))
   1150 
   1151 /* Estimate total size of the disk not including metadata */
   1152 #define LFS_EST_NONMETA(F) (lfs_sb_getdsize(F) - lfs_sb_getdmeta(F) - LFS_EST_CMETA(F))
   1153 
   1154 /* Estimate number of blocks actually available for writing */
   1155 #define LFS_EST_BFREE(F) (lfs_sb_getbfree(F) > LFS_EST_CMETA(F) ?	     \
   1156 			  lfs_sb_getbfree(F) - LFS_EST_CMETA(F) : 0)
   1157 
   1158 /* Amount of non-meta space not available to mortal man */
   1159 #define LFS_EST_RSVD(F) (int32_t)((LFS_EST_NONMETA(F) *			     \
   1160 				   (u_int64_t)lfs_sb_getminfree(F)) /	     \
   1161 				  100)
   1162 
   1163 /* Can credential C write BB blocks? XXX: kauth_cred_geteuid is abusive */
   1164 #define ISSPACE(F, BB, C)						\
   1165 	((((C) == NOCRED || kauth_cred_geteuid(C) == 0) &&		\
   1166 	  LFS_EST_BFREE(F) >= (BB)) ||					\
   1167 	 (kauth_cred_geteuid(C) != 0 && IS_FREESPACE(F, BB)))
   1168 
   1169 /* Can an ordinary user write BB blocks */
   1170 #define IS_FREESPACE(F, BB)						\
   1171 	  (LFS_EST_BFREE(F) >= (BB) + LFS_EST_RSVD(F))
   1172 
   1173 /*
   1174  * The minimum number of blocks to create a new inode.  This is:
   1175  * directory direct block (1) + ULFS_NIADDR indirect blocks + inode block (1) +
   1176  * ifile direct block (1) + ULFS_NIADDR indirect blocks = 3 + 2 * ULFS_NIADDR blocks.
   1177  */
   1178 #define LFS_NRESERVE(F) (lfs_btofsb((F), (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(F)))
   1179 
   1180 
   1181 
   1182 #endif /* _UFS_LFS_LFS_ACCESSORS_H_ */
   1183