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