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